2 * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT for more information.
5 * Because fcntl locks are
6 * process-oriented, we need to keep around a list of file descriptors
7 * that refer to the same file.
9 * TODO: fix the race when reading/writing.
10 * keep a pool of both locks and reference counters around so that
11 * we can save on mallocs. we should also use a tree to keep things
17 #endif /* HAVE_CONFIG_H */
19 #include <atalk/adouble.h>
20 #include <atalk/logger.h>
31 /* ----------------------- */
32 static int set_lock(int fd, int cmd, struct flock *lock)
35 /* We assign fd = -2 for symlinks -> do nothing */
37 lock->l_type = F_UNLCK;
40 return fcntl(fd, cmd, lock);
43 /* ----------------------- */
44 static int XLATE_FCNTL_LOCK(int type)
57 /* ----------------------- */
58 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen)
60 return (!alen && a <= b) ||
62 ( (a + alen > b) && (b + blen > a) );
65 /* allocation for lock regions. we allocate aggressively and shrink
66 * only in large chunks. */
67 #define ARRAY_BLOCK_SIZE 10
68 #define ARRAY_FREE_DELTA 100
70 /* remove a lock and compact space if necessary */
71 static void adf_freelock(struct ad_fd *ad, const int i)
73 adf_lock_t *lock = ad->adf_lock + i;
75 if (--(*lock->refcount) < 1) {
77 lock->lock.l_type = F_UNLCK;
78 set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
83 /* move another lock into the empty space */
84 if (i < ad->adf_lockcount) {
85 memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
88 /* free extra cruft if we go past a boundary. we always want to
89 * keep at least some stuff around for allocations. this wastes
90 * a bit of space to save time on reallocations. */
91 if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
92 (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
93 struct adf_lock_t *tmp;
95 tmp = (struct adf_lock_t *)
96 realloc(ad->adf_lock, sizeof(adf_lock_t)*
97 (ad->adf_lockcount + ARRAY_FREE_DELTA));
100 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
106 /* this needs to deal with the following cases:
107 * 1) fork is the only user of the lock
108 * 2) fork shares a read lock with another open fork
110 * i converted to using arrays of locks. everytime a lock
111 * gets removed, we shift all of the locks down.
113 static void adf_unlock(struct ad_fd *ad, const int fork)
115 adf_lock_t *lock = ad->adf_lock;
118 for (i = 0; i < ad->adf_lockcount; i++) {
120 if (lock[i].user == fork) {
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 */
125 /* unlikely but realloc may have change adf_lock */
131 /* relock any byte lock that overlaps off/len. unlock everything
133 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
135 adf_lock_t *lock = ad->adf_lock;
138 for (i = 0; i < ad->adf_lockcount; i++) {
139 if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
140 set_lock(fd, F_SETLK, &lock[i].lock);
145 /* find a byte lock that overlaps off/len for a particular open fork */
146 static int adf_findlock(struct ad_fd *ad,
147 const int fork, const int type,
151 adf_lock_t *lock = ad->adf_lock;
154 for (i = 0; i < ad->adf_lockcount; i++) {
155 if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
156 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
157 (lock[i].user == fork) &&
158 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
166 /* search other fork lock lists */
167 static int adf_findxlock(struct ad_fd *ad,
168 const int fork, 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))
178 ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK)))
180 (lock[i].user != fork)
182 OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
188 /* okay, this needs to do the following:
189 * 1) check current list of locks. error on conflict.
190 * 2) apply the lock. error on conflict with another process.
191 * 3) update the list of locks this file has.
193 * NOTE: this treats synchronization locks a little differently. we
194 * do the following things for those:
195 * 1) if the header file exists, all the locks go in the beginning
197 * 2) if the header file doesn't exist, we stick the locks
198 * in the locations specified by AD_FILELOCK_RD/WR.
200 #define LOCK_DATA_WR (0)
201 #define LOCK_DATA_RD (1)
202 #define LOCK_RSRC_WR (2)
203 #define LOCK_RSRC_RD (3)
205 #define LOCK_RSRC_DRD (4)
206 #define LOCK_RSRC_DWR (5)
207 #define LOCK_DATA_DRD (6)
208 #define LOCK_DATA_DWR (7)
210 #define LOCK_RSRC_NONE (8)
211 #define LOCK_DATA_NONE (9)
214 translate a data fork lock to an offset
217 static off_t df2off(off_t off)
220 if (off == AD_FILELOCK_OPEN_WR)
221 start = LOCK_DATA_WR;
222 else if (off == AD_FILELOCK_OPEN_RD)
223 start = LOCK_DATA_RD;
224 else if (off == AD_FILELOCK_DENY_RD)
225 start = LOCK_DATA_DRD;
226 else if (off == AD_FILELOCK_DENY_WR)
227 start = LOCK_DATA_DWR;
228 else if (off == AD_FILELOCK_OPEN_NONE)
229 start = LOCK_DATA_NONE;
234 translate a resource fork lock to an offset
237 static off_t hf2off(off_t off)
240 if (off == AD_FILELOCK_OPEN_WR)
241 start = LOCK_RSRC_WR;
242 else if (off == AD_FILELOCK_OPEN_RD)
243 start = LOCK_RSRC_RD;
244 else if (off == AD_FILELOCK_DENY_RD)
245 start = LOCK_RSRC_DRD;
246 else if (off == AD_FILELOCK_DENY_WR)
247 start = LOCK_RSRC_DWR;
248 else if (off == AD_FILELOCK_OPEN_NONE)
249 start = LOCK_RSRC_NONE;
254 translate a resource fork lock to an offset
256 static off_t rf2off(off_t off)
259 if (off == AD_FILELOCK_OPEN_WR)
260 start = AD_FILELOCK_RSRC_OPEN_WR;
261 else if (off == AD_FILELOCK_OPEN_RD)
262 start = AD_FILELOCK_RSRC_OPEN_RD;
263 else if (off == AD_FILELOCK_DENY_RD)
264 start = AD_FILELOCK_RSRC_DENY_RD;
265 else if (off == AD_FILELOCK_DENY_WR)
266 start = AD_FILELOCK_RSRC_DENY_WR;
267 else if (off == AD_FILELOCK_OPEN_NONE)
268 start = AD_FILELOCK_RSRC_OPEN_NONE;
275 * (1) Test against our own locks array
276 * (2) Test fcntl lock, locks from other processes
278 * @param adf (r) handle
279 * @param off (r) offset
280 * @param len (r) lenght
282 * @returns 1 if there's an existing lock, 0 if there's no lock,
283 * -1 in case any error occured
285 static int testlock(const struct ad_fd *adf, off_t off, off_t len)
293 plock = adf->adf_lock;
294 lock.l_whence = SEEK_SET;
297 /* (1) Do we have a lock ? */
298 for (i = 0; i < adf->adf_lockcount; i++) {
299 if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len))
303 /* (2) Does another process have a lock? */
304 lock.l_type = (adf->adf_flags & O_RDWR) ? F_WRLCK : F_RDLCK;
306 if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
307 /* is that kind of error possible ?*/
308 return (errno == EACCES || errno == EAGAIN) ? 1 : -1;
311 if (lock.l_type == F_UNLCK) {
317 static uint16_t ad_openforks_v2(struct adouble *ad, uint16_t attrbits)
323 if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
325 /* XXX know the locks layout:
326 AD_FILELOCK_OPEN_WR is first
327 and use it for merging requests
329 if (ad_meta_fileno(ad) != -1) {
330 /* there's a resource fork test the four bytes for
331 * data RW/RD and fork RW/RD locks in one request
338 /* no resource fork, only data RD/RW may exist */
339 adf = &ad->ad_data_fork;
340 off = AD_FILELOCK_OPEN_WR;
343 if (!testlock(adf, off, len))
346 /* either there's a lock or we already know one
349 if (!(attrbits & ATTRBIT_DOPEN)) {
350 if (ad_meta_fileno(ad) != -1) {
355 adf = &ad->ad_data_fork;
356 off = AD_FILELOCK_OPEN_WR;
358 ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
361 if (!(attrbits & ATTRBIT_ROPEN)) {
362 if (ad_meta_fileno(ad) != -1) {
365 ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
372 static uint16_t ad_openforks_ea(struct adouble *ad, uint16_t attrbits)
379 if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
380 /* Test all 4 locks at once */
381 off = AD_FILELOCK_OPEN_WR;
383 if (testlock(&ad->ad_data_fork, off, len) == 0)
387 /* either there's a lock or we already know one fork is open */
389 if (!(attrbits & ATTRBIT_DOPEN)) {
390 off = AD_FILELOCK_OPEN_WR;
391 ret = testlock(&ad->ad_data_fork, off, 2) > 0 ? ATTRBIT_DOPEN : 0;
394 if (!(attrbits & ATTRBIT_ROPEN)) {
395 off = AD_FILELOCK_RSRC_OPEN_WR;
396 ret |= testlock(&ad->ad_data_fork, off, 2) > 0? ATTRBIT_ROPEN : 0;
402 static int ad_testlock_v2(struct adouble *ad, int eid, const off_t off)
408 if (eid == ADEID_DFORK) {
409 adf = &ad->ad_data_fork;
410 if (ad_meta_fileno(ad) != -1) {
412 lock_offset = df2off(off);
415 if (ad_meta_fileno(ad) == -1) {
416 /* there's no resource fork. return no lock */
420 lock_offset = hf2off(off);
422 return testlock(adf, lock_offset, 1);
425 static int ad_testlock_ea(struct adouble *ad, int eid, const off_t off)
429 if (eid == ADEID_DFORK) {
430 lock_offset = df2off(off);
432 lock_offset = rf2off(off);
434 return testlock(&ad->ad_data_fork, lock_offset, 1);
437 /******************************************************************************
439 ******************************************************************************/
441 int ad_lock(struct adouble *ad, uint32_t eid, int locktype, off_t off, off_t len, int fork)
450 if ((type & ADLOCK_FILELOCK) && (len != 1))
456 if (eid == ADEID_DFORK) {
457 adf = &ad->ad_data_fork;
458 if ((type & ADLOCK_FILELOCK)) {
459 if (ad_meta_fileno(ad) != -1) { /* META */
461 lock.l_start = df2off(off);
465 switch (ad->ad_flags) {
467 if (ad_meta_fileno(ad) == -1 || ad_reso_fileno(ad) == -1) {
468 /* there's no meta data. return a lock error
469 * otherwise if a second process is able to create it
470 * locks are a mess. */
474 if (type & ADLOCK_FILELOCK) {
475 adf = ad->ad_mdp; /* either resource or meta data (set in ad_open) */
476 lock.l_start = hf2off(off);
478 /* we really want the resource fork it's a byte lock */
479 adf = &ad->ad_resource_fork;
480 lock.l_start += ad_getentryoff(ad, eid);
485 if (type & ADLOCK_FILELOCK) {
486 lock.l_start = rf2off(off);
488 /* it's a byterange lock on the rsrcfork -> discard it */
491 adf = &ad->ad_data_fork;
499 /* NOTE: we can't write lock a read-only file. on those, we just
500 * make sure that we have a read lock set. that way, we at least prevent
501 * someone else from really setting a deny read/write on the file.
503 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
504 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
507 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
508 lock.l_whence = SEEK_SET;
511 /* byte_lock(len=-1) lock whole file */
512 if (len == BYTELOCK_MAX) {
513 lock.l_len -= lock.l_start; /* otherwise EOVERFLOW error */
516 /* see if it's locked by another fork.
517 * NOTE: this guarantees that any existing locks must be at most
518 * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
519 * guaranteed to be ORable. */
520 if (adf_findxlock(adf, fork, ADLOCK_WR |
521 ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
522 lock.l_start, lock.l_len) > -1) {
527 /* look for any existing lock that we may have */
528 i = adf_findlock(adf, fork, ADLOCK_RD | ADLOCK_WR, lock.l_start, lock.l_len);
529 adflock = (i < 0) ? NULL : adf->adf_lock + i;
531 /* here's what we check for:
532 1) we're trying to re-lock a lock, but we didn't specify an update.
533 2) we're trying to free only part of a lock.
534 3) we're trying to free a non-existent lock. */
535 if ( (!adflock && (lock.l_type == F_UNLCK))
538 && !(type & ADLOCK_UPGRADE)
539 && ((lock.l_type != F_UNLCK)
540 || (adflock->lock.l_start != lock.l_start)
541 || (adflock->lock.l_len != lock.l_len) ))
548 /* now, update our list of locks */
550 if (lock.l_type == F_UNLCK) {
551 adf_freelock(adf, i);
555 /* attempt to lock the file. */
556 if (set_lock(adf->adf_fd, F_SETLK, &lock) < 0)
559 /* we upgraded this lock. */
560 if (adflock && (type & ADLOCK_UPGRADE)) {
561 memcpy(&adflock->lock, &lock, sizeof(lock));
565 /* it wasn't an upgrade */
567 if (lock.l_type == F_RDLCK) {
568 oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
571 /* no more space. this will also happen if lockmax == lockcount == 0 */
572 if (adf->adf_lockmax == adf->adf_lockcount) {
573 adf_lock_t *tmp = (adf_lock_t *)
574 realloc(adf->adf_lock, sizeof(adf_lock_t)*
575 (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
579 adf->adf_lockmax += ARRAY_BLOCK_SIZE;
581 adflock = adf->adf_lock + adf->adf_lockcount;
584 memcpy(&adflock->lock, &lock, sizeof(lock));
585 adflock->user = fork;
587 adflock->refcount = (adf->adf_lock + oldlock)->refcount;
588 } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
592 (*adflock->refcount)++;
593 adf->adf_lockcount++;
597 lock.l_type = F_UNLCK;
598 set_lock(adf->adf_fd, F_SETLK, &lock);
602 /* -------------------------
604 int ad_tmplock(struct adouble *ad, const uint32_t eid, const int locktype,
605 const off_t off, const off_t len, const int fork)
614 if (eid == ADEID_DFORK) {
615 adf = &ad->ad_data_fork;
618 adf = &ad->ad_resource_fork;
619 if (adf->adf_fd == -1) {
620 /* there's no resource fork. return success */
623 /* if ADLOCK_FILELOCK we want a lock from offset 0
624 * it's used when deleting a file:
625 * in open we put read locks on meta datas
626 * in delete a write locks on the whole file
627 * so if the file is open by somebody else it fails
629 if (!(type & ADLOCK_FILELOCK))
630 lock.l_start += ad_getentryoff(ad, eid);
633 if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
634 type = (type & ~ADLOCK_WR) | ADLOCK_RD;
637 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
638 lock.l_whence = SEEK_SET;
641 /* see if it's locked by another fork. */
642 if (fork && adf_findxlock(adf, fork,
643 ADLOCK_WR | ((type & ADLOCK_WR) ? ADLOCK_RD : 0),
644 lock.l_start, lock.l_len) > -1) {
649 /* okay, we might have ranges byte-locked. we need to make sure that
650 * we restore the appropriate ranges once we're done. so, we check
651 * for overlap on an unlock and relock.
652 * XXX: in the future, all the byte locks will be sorted and contiguous.
653 * we just want to upgrade all the locks and then downgrade them
655 err = set_lock(adf->adf_fd, F_SETLK, &lock);
656 if (!err && (lock.l_type == F_UNLCK))
657 adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
662 /* --------------------- */
663 void ad_unlock(struct adouble *ad, const int fork)
665 if (ad_data_fileno(ad) != -1) {
666 adf_unlock(&ad->ad_data_fork, fork);
668 if (ad_reso_fileno(ad) != -1) {
669 adf_unlock(&ad->ad_resource_fork, fork);
674 * Test for a share mode lock
676 * @param ad (rw) handle
677 * @param eid (r) datafork or ressource fork
678 * @param off (r) sharemode lock to test
680 * @returns 1 if there's an existing lock, 0 if there's no lock,
681 * -1 in case any error occured
683 int ad_testlock(struct adouble *ad, int eid, const off_t off)
685 switch (ad->ad_flags) {
687 return ad_testlock_v2(ad, eid, off);
689 return ad_testlock_ea(ad, eid, off);
696 * Return if a file is open by another process.
698 * Optimized for the common case:
699 * - there's no locks held by another process (clients)
700 * - or we already know the answer and don't need to test (attrbits)
702 * @param ad (rw) handle
703 * @param attrbits (r) forks opened by us
704 * @returns bitflags ATTRBIT_DOPEN | ATTRBIT_ROPEN if
705 * other process has fork of file opened
707 uint16_t ad_openforks(struct adouble *ad, uint16_t attrbits)
709 switch (ad->ad_flags) {
711 return ad_openforks_v2(ad, attrbits);
713 return ad_openforks_ea(ad, attrbits);