]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_lock.c
Change ad_init to pass vol, add ad_init_old for stuff that doesn't have struct vol...
[netatalk.git] / libatalk / adouble / ad_lock.c
1 /* 
2  * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
3  * All Rights Reserved. See COPYRIGHT for more information.
4  *
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.
8  *
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
12  *       sorted.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <atalk/adouble.h>
20 #include <atalk/logger.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <inttypes.h>
26
27 #include <string.h>
28
29 #include "ad_lock.h"
30
31 /* ----------------------- */
32 static int set_lock(int fd, int cmd,  struct flock *lock)
33 {
34   if (fd == -2) {
35       /* We assign fd = -2 for symlinks -> do nothing */
36       if (cmd == F_GETLK)
37             lock->l_type = F_UNLCK;
38       return 0;
39   }
40   return fcntl(fd, cmd, lock);
41 }
42
43 /* ----------------------- */
44 static int XLATE_FCNTL_LOCK(int type) 
45 {
46     switch(type) {
47     case ADLOCK_RD:
48         return F_RDLCK;
49     case ADLOCK_WR:
50          return F_WRLCK;
51     case ADLOCK_CLR:
52          return F_UNLCK;
53     }
54     return -1;
55 }
56
57 /* ----------------------- */
58 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen) 
59 {
60     return (!alen && a <= b) || 
61         (!blen && b <= a) || 
62         ( (a + alen > b) && (b + blen > a) );
63 }
64
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
69
70 /* remove a lock and compact space if necessary */
71 static void adf_freelock(struct ad_fd *ad, const int i)
72 {
73     adf_lock_t *lock = ad->adf_lock + i;
74
75     if (--(*lock->refcount) < 1) {
76         free(lock->refcount); 
77     lock->lock.l_type = F_UNLCK;
78     set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
79     }
80
81     ad->adf_lockcount--;
82
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));
86     }
87
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;
94
95             tmp = (struct adf_lock_t *) 
96                     realloc(ad->adf_lock, sizeof(adf_lock_t)*
97                             (ad->adf_lockcount + ARRAY_FREE_DELTA));
98             if (tmp) {
99                 ad->adf_lock = tmp;
100                 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
101             }
102     }
103 }
104
105
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
109  *
110  * i converted to using arrays of locks. everytime a lock
111  * gets removed, we shift all of the locks down.
112  */
113 static void adf_unlock(struct ad_fd *ad, const int fork)
114 {
115     adf_lock_t *lock = ad->adf_lock;
116     int i;
117
118     for (i = 0; i < ad->adf_lockcount; i++) {
119       
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 */
123          adf_freelock(ad, i);
124          i--; /* we shifted things down, so we need to backtrack */
125          /* unlikely but realloc may have change adf_lock */
126          lock = ad->adf_lock;       
127       }
128     }
129 }
130
131 /* relock any byte lock that overlaps off/len. unlock everything
132  * else. */
133 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
134 {
135     adf_lock_t *lock = ad->adf_lock;
136     int i;
137     
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);
141     }
142 }
143
144
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,
148                         const off_t off,
149                         const off_t len)
150 {
151   adf_lock_t *lock = ad->adf_lock;
152   int i;
153   
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)) {
159       return i;
160     }
161   }
162   return -1;
163 }
164
165
166 /* search other fork lock lists */
167 static int adf_findxlock(struct ad_fd *ad, 
168                          const int fork, const int type,
169                          const off_t off,
170                          const off_t len)
171 {
172     adf_lock_t *lock = ad->adf_lock;
173     int i;
174   
175     for (i = 0; i < ad->adf_lockcount; i++) {
176         if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK))
177              ||
178              ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK)))
179             &&
180             (lock[i].user != fork)
181             && 
182             OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
183             return i;
184     } 
185     return -1;
186 }
187
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.
192  * 
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
196  *          of that.
197  *       2) if the header file doesn't exist, we stick the locks
198  *          in the locations specified by AD_FILELOCK_RD/WR.
199  */
200 #define LOCK_DATA_WR (0)
201 #define LOCK_DATA_RD (1)
202 #define LOCK_RSRC_WR (2)
203 #define LOCK_RSRC_RD (3)
204
205 #define LOCK_RSRC_DRD (4)
206 #define LOCK_RSRC_DWR (5)
207 #define LOCK_DATA_DRD (6)
208 #define LOCK_DATA_DWR (7)
209
210 #define LOCK_RSRC_NONE (8)
211 #define LOCK_DATA_NONE (9)
212
213 /* -------------- 
214         translate a data fork lock to an offset
215 */
216
217 static off_t df2off(off_t off)
218 {
219     off_t start = 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;
230         return start;
231 }
232
233 /* -------------- 
234         translate a resource fork lock to an offset
235 */
236
237 static off_t hf2off(off_t off)
238 {
239     off_t start = 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;
250         return start;
251 }
252
253 /* -------------- 
254         translate a resource fork lock to an offset
255 */
256 static off_t rf2off(off_t off)
257 {
258     off_t start = 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;
269         return start;
270 }
271
272 /*!
273  * Test a lock
274  *
275  * (1) Test against our own locks array
276  * (2) Test fcntl lock, locks from other processes
277  *
278  * @param adf     (r) handle
279  * @param off     (r) offset
280  * @param len     (r) lenght
281  *
282  * @returns           1 if there's an existing lock, 0 if there's no lock,
283  *                    -1 in case any error occured
284  */
285 static int testlock(const struct ad_fd *adf, off_t off, off_t len)
286 {
287     struct flock lock;
288     adf_lock_t *plock;
289     int i;
290
291     lock.l_start = off;
292
293     plock = adf->adf_lock;
294     lock.l_whence = SEEK_SET;
295     lock.l_len = len;
296
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)) 
300             return 1;
301     }
302
303     /* (2) Does another process have a lock? */
304     lock.l_type = (adf->adf_flags & O_RDWR) ? F_WRLCK : F_RDLCK;
305
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;
309     }
310   
311     if (lock.l_type == F_UNLCK) {
312         return 0;
313     }
314     return 1;
315 }
316
317 static uint16_t ad_openforks_v2(struct adouble *ad, uint16_t attrbits)
318 {
319   u_int16_t ret = 0;
320   struct ad_fd *adf;
321   off_t off;
322
323   if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
324       off_t len;
325       /* XXX know the locks layout: 
326          AD_FILELOCK_OPEN_WR is first 
327          and use it for merging requests
328       */
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
332           */
333           adf = ad->ad_mdp;
334           off = LOCK_DATA_WR;
335           len = 4;
336       }
337       else {
338           /* no resource fork, only data RD/RW may exist */
339           adf = &ad->ad_data_fork;
340           off = AD_FILELOCK_OPEN_WR;
341           len = 2;
342       }
343       if (!testlock(adf, off, len))
344           return ret;
345   }
346   /* either there's a lock or we already know one 
347      fork is open
348   */
349   if (!(attrbits & ATTRBIT_DOPEN)) {
350       if (ad_meta_fileno(ad) != -1) {
351           adf = ad->ad_mdp;
352           off = LOCK_DATA_WR;
353       }
354       else {
355           adf = &ad->ad_data_fork;
356           off = AD_FILELOCK_OPEN_WR;
357       }
358       ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
359   }
360
361   if (!(attrbits & ATTRBIT_ROPEN)) {
362       if (ad_meta_fileno(ad) != -1) {
363           adf = ad->ad_mdp;
364           off = LOCK_RSRC_WR;
365           ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
366       }
367   }
368
369   return ret;
370 }
371
372 static uint16_t ad_openforks_ea(struct adouble *ad, uint16_t attrbits)
373 {
374     u_int16_t ret = 0;
375     struct ad_fd *adf;
376     off_t off;
377     off_t len;
378
379   if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
380       /* Test all 4 locks at once */
381       off = AD_FILELOCK_OPEN_WR;
382       len = 4;
383       if (testlock(&ad->ad_data_fork, off, len) == 0)
384           return 0;
385   }
386
387   /* either there's a lock or we already know one fork is open */
388
389   if (!(attrbits & ATTRBIT_DOPEN)) {
390       off = AD_FILELOCK_OPEN_WR;
391       ret = testlock(&ad->ad_data_fork, off, 2) > 0 ? ATTRBIT_DOPEN : 0;
392   }
393
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;
397   }
398
399   return ret;
400 }
401
402 static int ad_testlock_v2(struct adouble *ad, int eid, const off_t off)
403 {
404     struct ad_fd *adf;
405     off_t      lock_offset;
406
407     lock_offset = off;
408     if (eid == ADEID_DFORK) {
409         adf = &ad->ad_data_fork;
410         if (ad_meta_fileno(ad) != -1) {
411             adf = ad->ad_mdp;
412             lock_offset = df2off(off);
413         }
414     } else { /* rfork */
415         if (ad_meta_fileno(ad) == -1) {
416             /* there's no resource fork. return no lock */
417             return 0;
418         }
419         adf = ad->ad_mdp;
420         lock_offset = hf2off(off);
421     }
422     return testlock(adf, lock_offset, 1);
423 }
424
425 static int ad_testlock_ea(struct adouble *ad, int eid, const off_t off)
426 {
427     off_t      lock_offset;
428
429     if (eid == ADEID_DFORK) {
430         lock_offset = df2off(off);
431     } else { /* rfork */
432         lock_offset = rf2off(off);
433     }
434     return testlock(&ad->ad_data_fork, lock_offset, 1);
435 }
436
437 /******************************************************************************
438  * Public functions
439  ******************************************************************************/
440
441 int ad_lock(struct adouble *ad, uint32_t eid, int locktype, off_t off, off_t len, int fork)
442 {
443     struct flock lock;
444     struct ad_fd *adf;
445     adf_lock_t *adflock;
446     int oldlock;
447     int i;
448     int type;  
449
450     if ((type & ADLOCK_FILELOCK) && (len != 1))
451         /* safety check */
452         return -1;
453
454     lock.l_start = off;
455     type = locktype;
456     if (eid == ADEID_DFORK) {
457         adf = &ad->ad_data_fork;
458         if ((type & ADLOCK_FILELOCK)) {
459             if (ad_meta_fileno(ad) != -1) { /* META */
460                 adf = ad->ad_mdp;
461                 lock.l_start = df2off(off);
462             }
463         }
464     } else { /* rfork */
465         switch (ad->ad_flags) {
466         case AD_VERSION2:
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. */
471                 errno = EACCES;
472                 return -1;
473             }
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);
477             } else {
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);
481             }
482             break;
483
484         case AD_VERSION_EA:
485             if (type & ADLOCK_FILELOCK) {
486                 lock.l_start = rf2off(off);
487             } else {
488                 /* it's a byterange lock on the rsrcfork -> discard it */
489                 return 0;
490             }
491             adf = &ad->ad_data_fork;
492             break;
493
494         default:
495             return -1;
496         }
497     }
498
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. 
502      */
503     if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
504         type = (type & ~ADLOCK_WR) | ADLOCK_RD;
505     }
506   
507     lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
508     lock.l_whence = SEEK_SET;
509     lock.l_len = len;
510
511     /* byte_lock(len=-1) lock whole file */
512     if (len == BYTELOCK_MAX) {
513         lock.l_len -= lock.l_start; /* otherwise  EOVERFLOW error */
514     }
515
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) {
523         errno = EACCES;
524         return -1;
525     }
526   
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;
530
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))
536          ||
537          (adflock
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) ))
542         ) {
543         errno = EINVAL;
544         return -1;
545     }
546
547
548     /* now, update our list of locks */
549     /* clear the lock */
550     if (lock.l_type == F_UNLCK) { 
551         adf_freelock(adf, i);
552         return 0;
553     }
554
555     /* attempt to lock the file. */
556     if (set_lock(adf->adf_fd, F_SETLK, &lock) < 0) 
557         return -1;
558
559     /* we upgraded this lock. */
560     if (adflock && (type & ADLOCK_UPGRADE)) {
561         memcpy(&adflock->lock, &lock, sizeof(lock));
562         return 0;
563     } 
564
565     /* it wasn't an upgrade */
566     oldlock = -1;
567     if (lock.l_type == F_RDLCK) {
568         oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
569     } 
570     
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));
576         if (!tmp)
577             goto fcntl_lock_err;
578         adf->adf_lock = tmp;
579         adf->adf_lockmax += ARRAY_BLOCK_SIZE;
580     } 
581     adflock = adf->adf_lock + adf->adf_lockcount;
582
583     /* fill in fields */
584     memcpy(&adflock->lock, &lock, sizeof(lock));
585     adflock->user = fork;
586     if (oldlock > -1) {
587         adflock->refcount = (adf->adf_lock + oldlock)->refcount;
588     } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
589         goto fcntl_lock_err;
590     }
591   
592     (*adflock->refcount)++;
593     adf->adf_lockcount++;
594     return 0;
595
596 fcntl_lock_err:
597     lock.l_type = F_UNLCK;
598     set_lock(adf->adf_fd, F_SETLK, &lock);
599     return -1;
600 }
601
602 /* -------------------------
603 */
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)
606 {
607     struct flock lock;
608     struct ad_fd *adf;
609     int err;
610     int type;  
611
612     lock.l_start = off;
613     type = locktype;
614     if (eid == ADEID_DFORK) {
615         adf = &ad->ad_data_fork;
616     } else {
617         /* FIXME META */
618         adf = &ad->ad_resource_fork;
619         if (adf->adf_fd == -1) {
620             /* there's no resource fork. return success */
621             return 0;
622         }
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
628          */
629         if (!(type & ADLOCK_FILELOCK))
630             lock.l_start += ad_getentryoff(ad, eid);
631     }
632
633     if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
634         type = (type & ~ADLOCK_WR) | ADLOCK_RD;
635     }
636   
637     lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
638     lock.l_whence = SEEK_SET;
639     lock.l_len = len;
640
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) {
645         errno = EACCES;
646         return -1;
647     }
648
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
654      *      here. */
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);
658
659     return err;
660 }
661
662 /* --------------------- */
663 void ad_unlock(struct adouble *ad, const int fork)
664 {
665     if (ad_data_fileno(ad) != -1) {
666         adf_unlock(&ad->ad_data_fork, fork);
667     }
668     if (ad_reso_fileno(ad) != -1) {
669         adf_unlock(&ad->ad_resource_fork, fork);
670     }
671 }
672
673 /*!
674  * Test for a share mode lock
675  *
676  * @param ad      (rw) handle
677  * @param eid     (r)  datafork or ressource fork
678  * @param off     (r)  sharemode lock to test
679  *
680  * @returns           1 if there's an existing lock, 0 if there's no lock,
681  *                    -1 in case any error occured
682  */
683 int ad_testlock(struct adouble *ad, int eid, const off_t off)
684 {
685     switch (ad->ad_flags) {
686     case AD_VERSION2:
687         return ad_testlock_v2(ad, eid, off);
688     case AD_VERSION_EA:
689         return ad_testlock_ea(ad, eid, off);
690     default:
691         return -1;
692     }
693 }
694
695 /*!
696  * Return if a file is open by another process.
697  *
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)
701  *
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 
706  */
707 uint16_t ad_openforks(struct adouble *ad, uint16_t attrbits)
708 {
709     switch (ad->ad_flags) {
710     case AD_VERSION2:
711         return ad_openforks_v2(ad, attrbits);
712     case AD_VERSION_EA:
713         return ad_openforks_ea(ad, attrbits);
714     default:
715         return -1;
716     }
717 }