2 * $Id: ad_lock.c,v 1.11.6.4.2.2 2008-11-25 15:16:33 didg Exp $
4 * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT for more information.
7 * Byte-range locks. This uses either whole-file flocks to fake byte
8 * locks or fcntl-based actual byte locks. Because fcntl locks are
9 * process-oriented, we need to keep around a list of file descriptors
10 * that refer to the same file. Currently, this doesn't serialize access
11 * to the locks. as a result, there's the potential for race conditions.
13 * TODO: fix the race when reading/writing.
14 * keep a pool of both locks and reference counters around so that
15 * we can save on mallocs. we should also use a tree to keep things
21 #endif /* HAVE_CONFIG_H */
23 #include <atalk/adouble.h>
31 #include "ad_private.h"
33 /* translate between ADLOCK styles and specific locking mechanisms */
34 #define XLATE_FLOCK(type) ((type) == ADLOCK_RD ? LOCK_SH : \
35 ((type) == ADLOCK_WR ? LOCK_EX : \
36 ((type) == ADLOCK_CLR ? LOCK_UN : -1)))
38 #ifdef DISABLE_LOCKING
39 #define fcntl(a, b, c ) (0)
42 /* ----------------------- */
43 static int XLATE_FCNTL_LOCK(int type)
56 /* ----------------------- */
57 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen)
59 return (!alen && a <= b) ||
61 ( (a + alen > b) && (b + blen > a) );
64 /* allocation for lock regions. we allocate aggressively and shrink
65 * only in large chunks. */
66 #define ARRAY_BLOCK_SIZE 10
67 #define ARRAY_FREE_DELTA 100
69 /* remove a lock and compact space if necessary */
70 static void adf_freelock(struct ad_fd *ad, const int i)
72 adf_lock_t *lock = ad->adf_lock + i;
74 if (--(*lock->refcount) < 1) {
77 lock->lock.l_type = F_UNLCK;
78 fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
84 /* move another lock into the empty space */
85 if (i < ad->adf_lockcount) {
86 memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
89 /* free extra cruft if we go past a boundary. we always want to
90 * keep at least some stuff around for allocations. this wastes
91 * a bit of space to save time on reallocations. */
92 if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
93 (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
94 struct adf_lock_t *tmp;
96 tmp = (struct adf_lock_t *)
97 realloc(ad->adf_lock, sizeof(adf_lock_t)*
98 (ad->adf_lockcount + ARRAY_FREE_DELTA));
101 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
107 /* this needs to deal with the following cases:
108 * 1) user is the only user of the lock
109 * 2) user shares a read lock with another user
111 * i converted to using arrays of locks. everytime a lock
112 * gets removed, we shift all of the locks down.
114 static void adf_unlock(struct ad_fd *ad, const int user)
116 adf_lock_t *lock = ad->adf_lock;
119 for (i = 0; i < ad->adf_lockcount; i++) {
120 if (lock[i].user == user) {
121 /* we're really going to delete this lock. note: read locks
122 are the only ones that allow refcounts > 1 */
124 i--; /* we shifted things down, so we need to backtrack */
129 /* relock any byte lock that overlaps off/len. unlock everything
131 static void adf_relockrange(struct ad_fd *ad, int fd,
132 const off_t off, const off_t len)
134 adf_lock_t *lock = ad->adf_lock;
137 if (!ad->adf_excl) for (i = 0; i < ad->adf_lockcount; i++) {
138 if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
139 fcntl(fd, F_SETLK, &lock[i].lock);
144 /* find a byte lock that overlaps off/len for a particular user */
145 static int adf_findlock(struct ad_fd *ad,
146 const int user, const int type,
150 adf_lock_t *lock = ad->adf_lock;
153 for (i = 0; i < ad->adf_lockcount; i++) {
154 if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
155 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
156 (lock[i].user == user) &&
157 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
166 /* search other user lock lists */
167 static int adf_findxlock(struct ad_fd *ad,
168 const int user, const int type,
172 adf_lock_t *lock = ad->adf_lock;
175 for (i = 0; i < ad->adf_lockcount; i++) {
176 if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
177 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
178 (lock[i].user != user) &&
179 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
185 /* okay, this needs to do the following:
186 * 1) check current list of locks. error on conflict.
187 * 2) apply the lock. error on conflict with another process.
188 * 3) update the list of locks this file has.
190 * NOTE: this treats synchronization locks a little differently. we
191 * do the following things for those:
192 * 1) if the header file exists, all the locks go in the beginning
194 * 2) if the header file doesn't exist, we stick the locks
195 * in the locations specified by AD_FILELOCK_RD/WR.
197 #define LOCK_RSRC_RD (0)
198 #define LOCK_RSRC_WR (1)
199 #define LOCK_DATA_RD (2)
200 #define LOCK_DATA_WR (3)
202 #define LOCK_RSRC_DRD (4)
203 #define LOCK_RSRC_DWR (5)
204 #define LOCK_DATA_DRD (6)
205 #define LOCK_DATA_DWR (7)
207 #define LOCK_RSRC_NONE (8)
208 #define LOCK_DATA_NONE (9)
211 translate a data fork lock to an offset
214 static off_t df2off(int off)
217 if (off == AD_FILELOCK_OPEN_WR)
218 start = LOCK_DATA_WR;
219 else if (off == AD_FILELOCK_OPEN_RD)
220 start = LOCK_DATA_RD;
221 else if (off == AD_FILELOCK_DENY_RD)
222 start = LOCK_DATA_DRD;
223 else if (off == AD_FILELOCK_DENY_WR)
224 start = LOCK_DATA_DWR;
225 else if (off == AD_FILELOCK_OPEN_NONE)
226 start = LOCK_DATA_NONE;
231 translate a resource fork lock to an offset
234 static off_t hf2off(int off)
237 if (off == AD_FILELOCK_OPEN_WR)
238 start = LOCK_RSRC_WR;
239 else if (off == AD_FILELOCK_OPEN_RD)
240 start = LOCK_RSRC_RD;
241 else if (off == AD_FILELOCK_DENY_RD)
242 start = LOCK_RSRC_DRD;
243 else if (off == AD_FILELOCK_DENY_WR)
244 start = LOCK_RSRC_DWR;
245 else if (off == AD_FILELOCK_OPEN_NONE)
246 start = LOCK_RSRC_NONE;
250 /* ------------------ */
251 int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
252 const off_t off, const off_t len, const int user)
263 if (eid == ADEID_DFORK) {
265 if ((type & ADLOCK_FILELOCK)) {
266 if (ad_hfileno(ad) != -1) {
267 lock.l_start = df2off(off);
273 if (adf->adf_fd == -1) {
274 /* there's no resource fork. return a lock error
275 * otherwise if a second process is able to create it
281 if (type & ADLOCK_FILELOCK)
282 lock.l_start = hf2off(off);
284 lock.l_start += ad_getentryoff(ad, eid);
286 /* NOTE: we can't write lock a read-only file. on those, we just
287 * make sure that we have a read lock set. that way, we at least prevent
288 * someone else from really setting a deny read/write on the file.
290 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
291 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
294 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
295 lock.l_whence = SEEK_SET;
298 /* byte_lock(len=-1) lock whole file */
299 if (len == BYTELOCK_MAX) {
300 lock.l_len -= lock.l_start; /* otherwise EOVERFLOW error */
303 /* see if it's locked by another user.
304 * NOTE: this guarantees that any existing locks must be at most
305 * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
306 * guaranteed to be ORable. */
307 if (adf_findxlock(adf, user, ADLOCK_WR |
308 ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
309 lock.l_start, lock.l_len) > -1) {
314 /* look for any existing lock that we may have */
315 i = adf_findlock(adf, user, ADLOCK_RD | ADLOCK_WR, lock.l_start, lock.l_len);
316 adflock = (i < 0) ? NULL : adf->adf_lock + i;
318 /* here's what we check for:
319 1) we're trying to re-lock a lock, but we didn't specify an update.
320 2) we're trying to free only part of a lock.
321 3) we're trying to free a non-existent lock. */
322 if ((!adflock && (lock.l_type == F_UNLCK)) ||
323 (adflock && !(type & ADLOCK_UPGRADE) &&
324 ((lock.l_type != F_UNLCK) || (adflock->lock.l_start != lock.l_start) ||
325 (adflock->lock.l_len != lock.l_len)))) {
331 /* now, update our list of locks */
333 if (lock.l_type == F_UNLCK) {
334 adf_freelock(adf, i);
338 /* attempt to lock the file. */
339 if (!adf->adf_excl && fcntl(adf->adf_fd, F_SETLK, &lock) < 0)
342 /* we upgraded this lock. */
343 if (adflock && (type & ADLOCK_UPGRADE)) {
344 memcpy(&adflock->lock, &lock, sizeof(lock));
348 /* it wasn't an upgrade */
350 if (lock.l_type == F_RDLCK) {
351 oldlock = adf_findxlock(adf, user, ADLOCK_RD, lock.l_start, lock.l_len);
354 /* no more space. this will also happen if lockmax == lockcount == 0 */
355 if (adf->adf_lockmax == adf->adf_lockcount) {
356 adf_lock_t *tmp = (adf_lock_t *)
357 realloc(adf->adf_lock, sizeof(adf_lock_t)*
358 (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
362 adf->adf_lockmax += ARRAY_BLOCK_SIZE;
364 adflock = adf->adf_lock + adf->adf_lockcount;
367 memcpy(&adflock->lock, &lock, sizeof(lock));
368 adflock->user = user;
370 adflock->refcount = (adf->adf_lock + oldlock)->refcount;
371 } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
375 (*adflock->refcount)++;
376 adf->adf_lockcount++;
380 lock.l_type = F_UNLCK;
381 if (!adf->adf_excl) fcntl(adf->adf_fd, F_SETLK, &lock);
385 /* -------------------------
386 we are using lock as tristate variable
393 int ad_testlock(struct adouble *ad, int eid, const off_t off)
401 if (eid == ADEID_DFORK) {
403 if ((ad_hfileno(ad) != -1)) {
405 lock.l_start = df2off(off);
409 if ((ad_hfileno(ad) == -1)) {
410 /* there's no resource fork. return no lock */
414 lock.l_start = hf2off(off);
417 plock = adf->adf_lock;
418 /* Does we have a lock? */
419 lock.l_whence = SEEK_SET;
421 for (i = 0; i < adf->adf_lockcount; i++) {
422 if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len))
425 /* Does another process have a lock?
428 lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
430 if (fcntl(adf->adf_fd, F_SETLK, &lock) < 0) {
431 return (errno == EACCES || errno == EAGAIN)?1:-1;
434 lock.l_type = F_UNLCK;
435 return fcntl(adf->adf_fd, F_SETLK, &lock);
438 /* -------------------------
440 int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
441 const off_t off, const off_t len, const int user)
450 if (eid == ADEID_DFORK) {
454 if (adf->adf_fd == -1) {
455 /* there's no resource fork. return success */
458 /* if ADLOCK_FILELOCK we want a lock from offset 0
459 * it's used when deleting a file:
460 * in open we put read locks on meta datas
461 * in delete a write locks on the whole file
462 * so if the file is open by somebody else it fails
464 if (!(type & ADLOCK_FILELOCK))
465 lock.l_start += ad_getentryoff(ad, eid);
468 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
469 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
472 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
473 lock.l_whence = SEEK_SET;
476 /* see if it's locked by another user. */
477 if (user && adf_findxlock(adf, user, ADLOCK_WR |
478 ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
479 lock.l_start, lock.l_len) > -1) {
484 /* okay, we might have ranges byte-locked. we need to make sure that
485 * we restore the appropriate ranges once we're done. so, we check
486 * for overlap on an unlock and relock.
487 * XXX: in the future, all the byte locks will be sorted and contiguous.
488 * we just want to upgrade all the locks and then downgrade them
490 if (!adf->adf_excl) {
491 err = fcntl(adf->adf_fd, F_SETLK, &lock);
496 if (!err && (lock.l_type == F_UNLCK))
497 adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
502 /* -------------------------
503 the fork is opened in Read Write, Deny Read, Deny Write mode
504 lock the whole file once
506 int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
513 lock.l_type = F_WRLCK;
514 lock.l_whence = SEEK_SET;
517 if (eid == ADEID_DFORK) {
521 lock.l_start = ad_getentryoff(ad, eid);
524 err = fcntl(adf->adf_fd, F_SETLK, &lock);
530 /* --------------------- */
531 void ad_fcntl_unlock(struct adouble *ad, const int user)
533 if (ad->ad_df.adf_fd != -1) {
534 adf_unlock(&ad->ad_df, user);
536 if (ad->ad_hf.adf_fd != -1) {
537 adf_unlock(&ad->ad_hf, user);