2 * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT for more information.
5 * Byte-range locks. This uses either whole-file flocks to fake byte
6 * locks or fcntl-based actual byte locks. Because fcntl locks are
7 * process-oriented, we need to keep around a list of file descriptors
8 * that refer to the same file. Currently, this doesn't serialize access
9 * to the locks. as a result, there's the potential for race conditions.
11 * TODO: fix the race when reading/writing.
12 * keep a pool of both locks and reference counters around so that
13 * we can save on mallocs. we should also use a tree to keep things
19 #endif /* HAVE_CONFIG_H */
21 #include <atalk/adouble.h>
31 /* translate between ADLOCK styles and specific locking mechanisms */
32 #define XLATE_FLOCK(type) ((type) == ADLOCK_RD ? LOCK_SH : \
33 ((type) == ADLOCK_WR ? LOCK_EX : \
34 ((type) == ADLOCK_CLR ? LOCK_UN : -1)))
36 /* ----------------------- */
37 static int set_lock(int fd, int cmd, struct flock *lock)
40 /* We assign fd = -2 for symlinks -> do nothing */
42 lock->l_type = F_UNLCK;
45 return fcntl(fd, cmd, lock);
48 /* ----------------------- */
49 static int XLATE_FCNTL_LOCK(int type)
62 /* ----------------------- */
63 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen)
65 return (!alen && a <= b) ||
67 ( (a + alen > b) && (b + blen > a) );
70 /* allocation for lock regions. we allocate aggressively and shrink
71 * only in large chunks. */
72 #define ARRAY_BLOCK_SIZE 10
73 #define ARRAY_FREE_DELTA 100
75 /* remove a lock and compact space if necessary */
76 static void adf_freelock(struct ad_fd *ad, const int i)
79 adf_lock_t *lock = ad->adf_lock + i;
81 if (--(*lock->refcount) < 1) {
84 lock->lock.l_type = F_UNLCK;
85 set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
91 /* move another lock into the empty space */
92 if (i < ad->adf_lockcount) {
93 memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
96 /* free extra cruft if we go past a boundary. we always want to
97 * keep at least some stuff around for allocations. this wastes
98 * a bit of space to save time on reallocations. */
99 if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
100 (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
101 struct adf_lock_t *tmp;
103 tmp = (struct adf_lock_t *)
104 realloc(ad->adf_lock, sizeof(adf_lock_t)*
105 (ad->adf_lockcount + ARRAY_FREE_DELTA));
108 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
115 /* this needs to deal with the following cases:
116 * 1) fork is the only user of the lock
117 * 2) fork shares a read lock with another open fork
119 * i converted to using arrays of locks. everytime a lock
120 * gets removed, we shift all of the locks down.
122 static void adf_unlock(struct ad_fd *ad, const int fork)
125 adf_lock_t *lock = ad->adf_lock;
128 for (i = 0; i < ad->adf_lockcount; i++) {
130 if (lock[i].user == fork) {
131 /* we're really going to delete this lock. note: read locks
132 are the only ones that allow refcounts > 1 */
134 i--; /* we shifted things down, so we need to backtrack */
135 /* unlikely but realloc may have change adf_lock */
142 /* relock any byte lock that overlaps off/len. unlock everything
144 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
147 adf_lock_t *lock = ad->adf_lock;
150 if (!ad->adf_excl) for (i = 0; i < ad->adf_lockcount; i++) {
151 if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
152 set_lock(fd, F_SETLK, &lock[i].lock);
158 /* find a byte lock that overlaps off/len for a particular open fork */
159 static int adf_findlock(struct ad_fd *ad,
160 const int fork, const int type,
165 adf_lock_t *lock = ad->adf_lock;
168 for (i = 0; i < ad->adf_lockcount; i++) {
169 if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
170 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
171 (lock[i].user == fork) &&
172 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
181 /* search other fork lock lists */
182 static int adf_findxlock(struct ad_fd *ad,
183 const int fork, const int type,
188 adf_lock_t *lock = ad->adf_lock;
191 for (i = 0; i < ad->adf_lockcount; i++) {
192 if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
193 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
194 (lock[i].user != fork) &&
195 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
202 /* okay, this needs to do the following:
203 * 1) check current list of locks. error on conflict.
204 * 2) apply the lock. error on conflict with another process.
205 * 3) update the list of locks this file has.
207 * NOTE: this treats synchronization locks a little differently. we
208 * do the following things for those:
209 * 1) if the header file exists, all the locks go in the beginning
211 * 2) if the header file doesn't exist, we stick the locks
212 * in the locations specified by AD_FILELOCK_RD/WR.
214 #define LOCK_DATA_WR (0)
215 #define LOCK_DATA_RD (1)
216 #define LOCK_RSRC_WR (2)
217 #define LOCK_RSRC_RD (3)
219 #define LOCK_RSRC_DRD (4)
220 #define LOCK_RSRC_DWR (5)
221 #define LOCK_DATA_DRD (6)
222 #define LOCK_DATA_DWR (7)
224 #define LOCK_RSRC_NONE (8)
225 #define LOCK_DATA_NONE (9)
228 translate a data fork lock to an offset
231 static off_t df2off(int off)
234 if (off == AD_FILELOCK_OPEN_WR)
235 start = LOCK_DATA_WR;
236 else if (off == AD_FILELOCK_OPEN_RD)
237 start = LOCK_DATA_RD;
238 else if (off == AD_FILELOCK_DENY_RD)
239 start = LOCK_DATA_DRD;
240 else if (off == AD_FILELOCK_DENY_WR)
241 start = LOCK_DATA_DWR;
242 else if (off == AD_FILELOCK_OPEN_NONE)
243 start = LOCK_DATA_NONE;
248 translate a resource fork lock to an offset
251 static off_t hf2off(int off)
254 if (off == AD_FILELOCK_OPEN_WR)
255 start = LOCK_RSRC_WR;
256 else if (off == AD_FILELOCK_OPEN_RD)
257 start = LOCK_RSRC_RD;
258 else if (off == AD_FILELOCK_DENY_RD)
259 start = LOCK_RSRC_DRD;
260 else if (off == AD_FILELOCK_DENY_WR)
261 start = LOCK_RSRC_DWR;
262 else if (off == AD_FILELOCK_OPEN_NONE)
263 start = LOCK_RSRC_NONE;
267 /* ------------------ */
268 static int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
269 const off_t off, const off_t len, const int fork)
281 if (eid == ADEID_DFORK) {
282 adf = &ad->ad_data_fork;
283 if ((type & ADLOCK_FILELOCK)) {
284 if (ad_meta_fileno(ad) != -1) { /* META */
286 lock.l_start = df2off(off);
290 if (ad_meta_fileno(ad) == -1 || ad_reso_fileno(ad) == -1) {
291 /* there's no meta data. return a lock error
292 * otherwise if a second process is able to create it
298 if (type & ADLOCK_FILELOCK) {
299 adf = ad->ad_md; /* either resource or meta data (set in ad_open) */
300 lock.l_start = hf2off(off);
303 /* we really want the resource fork it's a byte lock */
304 adf = &ad->ad_resource_fork;
305 lock.l_start += ad_getentryoff(ad, eid);
308 /* NOTE: we can't write lock a read-only file. on those, we just
309 * make sure that we have a read lock set. that way, we at least prevent
310 * someone else from really setting a deny read/write on the file.
312 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
313 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
316 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
317 lock.l_whence = SEEK_SET;
320 /* byte_lock(len=-1) lock whole file */
321 if (len == BYTELOCK_MAX) {
322 lock.l_len -= lock.l_start; /* otherwise EOVERFLOW error */
325 /* see if it's locked by another fork.
326 * NOTE: this guarantees that any existing locks must be at most
327 * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
328 * guaranteed to be ORable. */
329 if (adf_findxlock(adf, fork, ADLOCK_WR |
330 ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
331 lock.l_start, lock.l_len) > -1) {
336 /* look for any existing lock that we may have */
337 i = adf_findlock(adf, fork, ADLOCK_RD | ADLOCK_WR, lock.l_start, lock.l_len);
338 adflock = (i < 0) ? NULL : adf->adf_lock + i;
340 /* here's what we check for:
341 1) we're trying to re-lock a lock, but we didn't specify an update.
342 2) we're trying to free only part of a lock.
343 3) we're trying to free a non-existent lock. */
344 if ( (!adflock && (lock.l_type == F_UNLCK))
347 && !(type & ADLOCK_UPGRADE)
348 && ((lock.l_type != F_UNLCK)
349 || (adflock->lock.l_start != lock.l_start)
350 || (adflock->lock.l_len != lock.l_len) ))
357 /* now, update our list of locks */
359 if (lock.l_type == F_UNLCK) {
360 adf_freelock(adf, i);
364 /* attempt to lock the file. */
365 if (!adf->adf_excl && set_lock(adf->adf_fd, F_SETLK, &lock) < 0)
368 /* we upgraded this lock. */
369 if (adflock && (type & ADLOCK_UPGRADE)) {
370 memcpy(&adflock->lock, &lock, sizeof(lock));
374 /* it wasn't an upgrade */
376 if (lock.l_type == F_RDLCK) {
377 oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
380 /* no more space. this will also happen if lockmax == lockcount == 0 */
381 if (adf->adf_lockmax == adf->adf_lockcount) {
382 adf_lock_t *tmp = (adf_lock_t *)
383 realloc(adf->adf_lock, sizeof(adf_lock_t)*
384 (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
388 adf->adf_lockmax += ARRAY_BLOCK_SIZE;
390 adflock = adf->adf_lock + adf->adf_lockcount;
393 memcpy(&adflock->lock, &lock, sizeof(lock));
394 adflock->user = fork;
396 adflock->refcount = (adf->adf_lock + oldlock)->refcount;
397 } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
401 (*adflock->refcount)++;
402 adf->adf_lockcount++;
406 lock.l_type = F_UNLCK;
407 if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
413 /* -------------------------
414 we are using lock as tristate variable
421 static int testlock(struct ad_fd *adf, off_t off, off_t len)
430 plock = adf->adf_lock;
431 lock.l_whence = SEEK_SET;
434 /* Do we have a lock? */
435 for (i = 0; i < adf->adf_lockcount; i++) {
436 if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len))
439 /* Does another process have a lock?
441 lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
443 if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
444 /* is that kind of error possible ?*/
445 return (errno == EACCES || errno == EAGAIN)?1:-1;
448 if (lock.l_type == F_UNLCK) {
457 /* -------------------------
459 static int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
460 const off_t off, const off_t len, const int fork)
469 if (eid == ADEID_DFORK) {
470 adf = &ad->ad_data_fork;
473 adf = &ad->ad_resource_fork;
474 if (adf->adf_fd == -1) {
475 /* there's no resource fork. return success */
478 /* if ADLOCK_FILELOCK we want a lock from offset 0
479 * it's used when deleting a file:
480 * in open we put read locks on meta datas
481 * in delete a write locks on the whole file
482 * so if the file is open by somebody else it fails
484 if (!(type & ADLOCK_FILELOCK))
485 lock.l_start += ad_getentryoff(ad, eid);
488 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
489 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
492 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
493 lock.l_whence = SEEK_SET;
496 /* see if it's locked by another fork. */
497 if (fork && adf_findxlock(adf, fork, ADLOCK_WR |
498 ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
499 lock.l_start, lock.l_len) > -1) {
504 /* okay, we might have ranges byte-locked. we need to make sure that
505 * we restore the appropriate ranges once we're done. so, we check
506 * for overlap on an unlock and relock.
507 * XXX: in the future, all the byte locks will be sorted and contiguous.
508 * we just want to upgrade all the locks and then downgrade them
510 if (!adf->adf_excl) {
511 err = set_lock(adf->adf_fd, F_SETLK, &lock);
516 if (!err && (lock.l_type == F_UNLCK))
517 adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
522 /* --------------------- */
523 static void ad_fcntl_unlock(struct adouble *ad, const int fork)
525 if (ad_data_fileno(ad) != -1) {
526 adf_unlock(&ad->ad_data_fork, fork);
528 if (ad_reso_fileno(ad) != -1) {
529 adf_unlock(&ad->ad_resource_fork, fork);
532 if (ad->ad_flags != AD_VERSION_EA) {
535 if (ad_meta_fileno(ad) != -1) {
536 adf_unlock(&ad->ad_metadata_fork, fork);
540 /******************************************************************************
542 ******************************************************************************/
544 /* --------------- */
545 int ad_testlock(struct adouble *ad, int eid, const off_t off)
553 if (eid == ADEID_DFORK) {
554 adf = &ad->ad_data_fork;
555 if (ad_meta_fileno(ad) != -1) {
557 lock_offset = df2off(off);
561 if (ad_meta_fileno(ad) == -1) {
562 /* there's no resource fork. return no lock */
566 lock_offset = hf2off(off);
568 return testlock(adf, lock_offset, 1);
572 /* -------------------------
573 return if a file is open by another process.
574 Optimized for the common case:
575 - there's no locks held by another process (clients)
576 - or we already know the answer and don't need to test.
578 uint16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
586 if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
588 /* XXX know the locks layout:
589 AD_FILELOCK_OPEN_WR is first
590 and use it for merging requests
592 if (ad_meta_fileno(ad) != -1) {
593 /* there's a resource fork test the four bytes for
594 * data RW/RD and fork RW/RD locks in one request
601 /* no resource fork, only data RD/RW may exist */
602 adf = &ad->ad_data_fork;
603 off = AD_FILELOCK_OPEN_WR;
606 if (!testlock(adf, off, len))
609 /* either there's a lock or we already know one
612 if (!(attrbits & ATTRBIT_DOPEN)) {
613 if (ad_meta_fileno(ad) != -1) {
618 adf = &ad->ad_data_fork;
619 off = AD_FILELOCK_OPEN_WR;
621 ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
624 if (!(attrbits & ATTRBIT_ROPEN)) {
625 if (ad_meta_fileno(ad) != -1) {
628 ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
636 /* -------------------------
637 the fork is opened in Read Write, Deny Read, Deny Write mode
638 lock the whole file once
640 int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
649 lock.l_type = F_WRLCK;
650 lock.l_whence = SEEK_SET;
653 if (eid == ADEID_DFORK) {
654 adf = &ad->ad_data_fork;
656 adf = &ad->ad_resource_fork;
657 lock.l_start = ad_getentryoff(ad, eid);
660 err = set_lock(adf->adf_fd, F_SETLK, &lock);
667 int ad_lock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
672 void ad_unlock(struct adouble *ad, int user)
677 int ad_tmplock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)