/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2010 The FreeBSD Foundation4*5* This software was developed by Pawel Jakub Dawidek under sponsorship from6* the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31#include <sys/queue.h>3233#include <stdbool.h>34#include <stdlib.h>35#include <unistd.h>3637#include <pjdlog.h>3839#include "rangelock.h"4041#ifndef PJDLOG_ASSERT42#include <assert.h>43#define PJDLOG_ASSERT(...) assert(__VA_ARGS__)44#endif4546#define RANGELOCKS_MAGIC 0x94310c47struct rangelocks {48int rls_magic; /* Magic value. */49TAILQ_HEAD(, rlock) rls_locks; /* List of locked ranges. */50};5152struct rlock {53off_t rl_start;54off_t rl_end;55TAILQ_ENTRY(rlock) rl_next;56};5758int59rangelock_init(struct rangelocks **rlsp)60{61struct rangelocks *rls;6263PJDLOG_ASSERT(rlsp != NULL);6465rls = malloc(sizeof(*rls));66if (rls == NULL)67return (-1);6869TAILQ_INIT(&rls->rls_locks);7071rls->rls_magic = RANGELOCKS_MAGIC;72*rlsp = rls;7374return (0);75}7677void78rangelock_free(struct rangelocks *rls)79{80struct rlock *rl;8182PJDLOG_ASSERT(rls->rls_magic == RANGELOCKS_MAGIC);8384rls->rls_magic = 0;8586while ((rl = TAILQ_FIRST(&rls->rls_locks)) != NULL) {87TAILQ_REMOVE(&rls->rls_locks, rl, rl_next);88free(rl);89}90free(rls);91}9293int94rangelock_add(struct rangelocks *rls, off_t offset, off_t length)95{96struct rlock *rl;9798PJDLOG_ASSERT(rls->rls_magic == RANGELOCKS_MAGIC);99100rl = malloc(sizeof(*rl));101if (rl == NULL)102return (-1);103rl->rl_start = offset;104rl->rl_end = offset + length;105TAILQ_INSERT_TAIL(&rls->rls_locks, rl, rl_next);106return (0);107}108109void110rangelock_del(struct rangelocks *rls, off_t offset, off_t length)111{112struct rlock *rl;113114PJDLOG_ASSERT(rls->rls_magic == RANGELOCKS_MAGIC);115116TAILQ_FOREACH(rl, &rls->rls_locks, rl_next) {117if (rl->rl_start == offset && rl->rl_end == offset + length)118break;119}120PJDLOG_ASSERT(rl != NULL);121TAILQ_REMOVE(&rls->rls_locks, rl, rl_next);122free(rl);123}124125bool126rangelock_islocked(struct rangelocks *rls, off_t offset, off_t length)127{128struct rlock *rl;129off_t end;130131PJDLOG_ASSERT(rls->rls_magic == RANGELOCKS_MAGIC);132133end = offset + length;134TAILQ_FOREACH(rl, &rls->rls_locks, rl_next) {135if (rl->rl_start < end && rl->rl_end > offset)136break;137}138return (rl != NULL);139}140141142