]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_lock.c
Start locking using Berkeley db
[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  * 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. 
10  *
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
14  *       sorted.
15  */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include <atalk/adouble.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26
27 #include <string.h>
28
29 #include "ad_lock.h"
30
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)))
35
36 #ifdef DISABLE_LOCKING
37 #define fcntl(a, b, c ) (0)
38 #endif
39
40 /* ----------------------- */
41 static int set_lock(int fd, int cmd,  struct flock *lock)
42 {
43   if (fd == -2) {
44       /* We assign fd = -2 for symlinks -> do nothing */
45       if (cmd == F_GETLK)
46             lock->l_type = F_UNLCK;
47       return 0;
48   }
49   return fcntl(fd, cmd, lock);
50 }
51
52 /* ----------------------- */
53 static int XLATE_FCNTL_LOCK(int type) 
54 {
55     switch(type) {
56     case ADLOCK_RD:
57         return F_RDLCK;
58     case ADLOCK_WR:
59          return F_WRLCK;
60     case ADLOCK_CLR:
61          return F_UNLCK;
62     }
63     return -1;
64 }
65
66 /* ----------------------- */
67 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen) 
68 {
69     return (!alen && a <= b) || 
70         (!blen && b <= a) || 
71         ( (a + alen > b) && (b + blen > a) );
72 }
73
74 /* allocation for lock regions. we allocate aggressively and shrink
75  * only in large chunks. */
76 #define ARRAY_BLOCK_SIZE 10 
77 #define ARRAY_FREE_DELTA 100
78
79 /* remove a lock and compact space if necessary */
80 static void adf_freelock(struct ad_fd *ad, const int i)
81 {
82 #if 0
83     adf_lock_t *lock = ad->adf_lock + i;
84
85     if (--(*lock->refcount) < 1) {
86         free(lock->refcount); 
87         if (!ad->adf_excl) {
88             lock->lock.l_type = F_UNLCK;
89             set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
90         }
91     }
92
93     ad->adf_lockcount--;
94
95     /* move another lock into the empty space */
96     if (i < ad->adf_lockcount) {
97         memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
98     }
99
100     /* free extra cruft if we go past a boundary. we always want to
101      * keep at least some stuff around for allocations. this wastes
102      * a bit of space to save time on reallocations. */
103     if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
104         (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
105             struct adf_lock_t *tmp;
106
107             tmp = (struct adf_lock_t *) 
108                     realloc(ad->adf_lock, sizeof(adf_lock_t)*
109                             (ad->adf_lockcount + ARRAY_FREE_DELTA));
110             if (tmp) {
111                 ad->adf_lock = tmp;
112                 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
113             }
114     }
115 #endif
116 }
117
118
119 /* this needs to deal with the following cases:
120  * 1) fork is the only user of the lock 
121  * 2) fork shares a read lock with another open fork
122  *
123  * i converted to using arrays of locks. everytime a lock
124  * gets removed, we shift all of the locks down.
125  */
126 static void adf_unlock(struct ad_fd *ad, const int fork)
127 {
128 #if 0
129     adf_lock_t *lock = ad->adf_lock;
130     int i;
131
132     for (i = 0; i < ad->adf_lockcount; i++) {
133       
134       if (lock[i].user == fork) {
135         /* we're really going to delete this lock. note: read locks
136            are the only ones that allow refcounts > 1 */
137          adf_freelock(ad, i);
138          i--; /* we shifted things down, so we need to backtrack */
139          /* unlikely but realloc may have change adf_lock */
140          lock = ad->adf_lock;       
141       }
142     }
143 #endif
144 }
145
146 /* relock any byte lock that overlaps off/len. unlock everything
147  * else. */
148 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
149 {
150 #if 0
151     adf_lock_t *lock = ad->adf_lock;
152     int i;
153     
154     if (!ad->adf_excl) for (i = 0; i < ad->adf_lockcount; i++) {
155       if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
156         set_lock(fd, F_SETLK, &lock[i].lock);
157     }
158 #endif
159 }
160
161
162 /* find a byte lock that overlaps off/len for a particular open fork */
163 static int adf_findlock(struct ad_fd *ad,
164                         const int fork, const int type,
165                         const off_t off,
166                         const off_t len)
167 {
168 #if 0
169   adf_lock_t *lock = ad->adf_lock;
170   int i;
171   
172   for (i = 0; i < ad->adf_lockcount; i++) {
173     if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
174         ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
175         (lock[i].user == fork) && 
176         OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
177       return i;
178     }
179   }
180 #endif
181   return -1;
182 }
183
184
185 /* search other fork lock lists */
186 static int adf_findxlock(struct ad_fd *ad, 
187                          const int fork, const int type,
188                          const off_t off,
189                          const off_t len)
190 {
191 #if 0
192   adf_lock_t *lock = ad->adf_lock;
193   int i;
194   
195   for (i = 0; i < ad->adf_lockcount; i++) {
196     if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
197          ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
198         (lock[i].user != fork) && 
199         OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
200             return i;
201   } 
202 #endif
203   return -1;
204 }
205
206 /* okay, this needs to do the following:
207  * 1) check current list of locks. error on conflict.
208  * 2) apply the lock. error on conflict with another process.
209  * 3) update the list of locks this file has.
210  * 
211  * NOTE: this treats synchronization locks a little differently. we
212  *       do the following things for those:
213  *       1) if the header file exists, all the locks go in the beginning
214  *          of that.
215  *       2) if the header file doesn't exist, we stick the locks
216  *          in the locations specified by AD_FILELOCK_RD/WR.
217  */
218 #define LOCK_DATA_WR (0)
219 #define LOCK_DATA_RD (1)
220 #define LOCK_RSRC_WR (2)
221 #define LOCK_RSRC_RD (3)
222
223 #define LOCK_RSRC_DRD (4)
224 #define LOCK_RSRC_DWR (5)
225 #define LOCK_DATA_DRD (6)
226 #define LOCK_DATA_DWR (7)
227
228 #define LOCK_RSRC_NONE (8)
229 #define LOCK_DATA_NONE (9)
230
231 /* -------------- 
232         translate a data fork lock to an offset
233 */
234
235 static off_t df2off(int off)
236 {
237 int start = off;
238         if (off == AD_FILELOCK_OPEN_WR)
239                 start = LOCK_DATA_WR;
240         else if (off == AD_FILELOCK_OPEN_RD)
241                 start = LOCK_DATA_RD;
242     else if (off == AD_FILELOCK_DENY_RD)
243                 start = LOCK_DATA_DRD;
244         else if (off == AD_FILELOCK_DENY_WR)
245                 start = LOCK_DATA_DWR;
246         else if (off == AD_FILELOCK_OPEN_NONE)
247                 start = LOCK_DATA_NONE;
248         return start;
249 }
250
251 /* -------------- 
252         translate a resource fork lock to an offset
253 */
254
255 static off_t hf2off(int off)
256 {
257 int start = off;
258         if (off == AD_FILELOCK_OPEN_WR)
259                 start = LOCK_RSRC_WR;
260         else if (off == AD_FILELOCK_OPEN_RD)
261                 start = LOCK_RSRC_RD;
262     else if (off == AD_FILELOCK_DENY_RD)
263                 start = LOCK_RSRC_DRD;
264         else if (off == AD_FILELOCK_DENY_WR)
265                 start = LOCK_RSRC_DWR;
266         else if (off == AD_FILELOCK_OPEN_NONE)
267                 start = LOCK_RSRC_NONE;
268         return start;
269 }
270
271 /* ------------------ */
272 static int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
273                          const off_t off, const off_t len, const int fork)
274 {
275 #if 0
276   struct flock lock;
277   struct ad_fd *adf;
278   adf_lock_t *adflock;
279   int oldlock;
280   int i;
281   int type;  
282
283   lock.l_start = off;
284   type = locktype;
285   if (eid == ADEID_DFORK) {
286     adf = &ad->ad_data_fork;
287     if ((type & ADLOCK_FILELOCK)) {
288         if (ad_meta_fileno(ad) != -1) { /* META */
289             adf = ad->ad_md;
290             lock.l_start = df2off(off);
291         }
292     }
293   } else { /* rfork */
294     if (ad_meta_fileno(ad) == -1 || ad_reso_fileno(ad) == -1) {
295         /* there's no meta data. return a lock error 
296          * otherwise if a second process is able to create it
297          * locks are a mess.
298          */
299         errno = EACCES;
300         return -1;
301     }
302     if (type & ADLOCK_FILELOCK) {
303       adf = ad->ad_md;                  /* either resource or meta data (set in ad_open) */
304       lock.l_start = hf2off(off);
305     }
306     else {
307       /* we really want the resource fork it's a byte lock */
308       adf = &ad->ad_resource_fork;
309       lock.l_start += ad_getentryoff(ad, eid);
310     }
311   }
312   /* NOTE: we can't write lock a read-only file. on those, we just
313     * make sure that we have a read lock set. that way, we at least prevent
314     * someone else from really setting a deny read/write on the file. 
315     */
316   if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
317       type = (type & ~ADLOCK_WR) | ADLOCK_RD;
318   }
319   
320   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
321   lock.l_whence = SEEK_SET;
322   lock.l_len = len;
323
324   /* byte_lock(len=-1) lock whole file */
325   if (len == BYTELOCK_MAX) {
326       lock.l_len -= lock.l_start; /* otherwise  EOVERFLOW error */
327   }
328
329   /* see if it's locked by another fork. 
330    * NOTE: this guarantees that any existing locks must be at most
331    * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
332    * guaranteed to be ORable. */
333   if (adf_findxlock(adf, fork, ADLOCK_WR | 
334                     ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
335                     lock.l_start, lock.l_len) > -1) {
336     errno = EACCES;
337     return -1;
338   }
339   
340   /* look for any existing lock that we may have */
341   i = adf_findlock(adf, fork, ADLOCK_RD | ADLOCK_WR, lock.l_start, lock.l_len);
342   adflock = (i < 0) ? NULL : adf->adf_lock + i;
343
344   /* here's what we check for:
345      1) we're trying to re-lock a lock, but we didn't specify an update.
346      2) we're trying to free only part of a lock. 
347      3) we're trying to free a non-existent lock. */
348   if ( (!adflock && (lock.l_type == F_UNLCK))
349        ||
350        (adflock
351         && !(type & ADLOCK_UPGRADE)
352         && ((lock.l_type != F_UNLCK)
353             || (adflock->lock.l_start != lock.l_start)
354             || (adflock->lock.l_len != lock.l_len) ))
355       ) {
356       errno = EINVAL;
357       return -1;
358   }
359
360
361   /* now, update our list of locks */
362   /* clear the lock */
363   if (lock.l_type == F_UNLCK) { 
364     adf_freelock(adf, i);
365     return 0;
366   }
367
368   /* attempt to lock the file. */
369   if (!adf->adf_excl && set_lock(adf->adf_fd, F_SETLK, &lock) < 0) 
370     return -1;
371
372   /* we upgraded this lock. */
373   if (adflock && (type & ADLOCK_UPGRADE)) {
374     memcpy(&adflock->lock, &lock, sizeof(lock));
375     return 0;
376   } 
377
378   /* it wasn't an upgrade */
379   oldlock = -1;
380   if (lock.l_type == F_RDLCK) {
381     oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
382   } 
383     
384   /* no more space. this will also happen if lockmax == lockcount == 0 */
385   if (adf->adf_lockmax == adf->adf_lockcount) { 
386     adf_lock_t *tmp = (adf_lock_t *) 
387             realloc(adf->adf_lock, sizeof(adf_lock_t)*
388                     (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
389     if (!tmp)
390       goto fcntl_lock_err;
391     adf->adf_lock = tmp;
392     adf->adf_lockmax += ARRAY_BLOCK_SIZE;
393   } 
394   adflock = adf->adf_lock + adf->adf_lockcount;
395
396   /* fill in fields */
397   memcpy(&adflock->lock, &lock, sizeof(lock));
398   adflock->user = fork;
399   if (oldlock > -1) {
400     adflock->refcount = (adf->adf_lock + oldlock)->refcount;
401   } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
402     goto fcntl_lock_err;
403   }
404   
405   (*adflock->refcount)++;
406   adf->adf_lockcount++;
407   return 0;
408
409 fcntl_lock_err:
410   lock.l_type = F_UNLCK;
411   if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
412   return -1;
413 #endif
414   return 0;
415 }
416
417 /* -------------------------
418    we are using lock as tristate variable
419    
420    we have a lock ==> 1
421    no             ==> 0
422    error          ==> -1
423       
424 */
425 static int testlock(struct ad_fd *adf, off_t off, off_t len)
426 {
427 #if 0
428   struct flock lock;
429   adf_lock_t *plock;
430   int i;
431
432   lock.l_start = off;
433
434   plock = adf->adf_lock;
435   lock.l_whence = SEEK_SET;
436   lock.l_len = len;
437
438   /* Do we have a lock? */
439   for (i = 0; i < adf->adf_lockcount; i++) {
440     if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len)) 
441         return 1;   /* */
442   }
443   /* Does another process have a lock? 
444   */
445   lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
446
447   if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
448       /* is that kind of error possible ?*/
449       return (errno == EACCES || errno == EAGAIN)?1:-1;
450   }
451   
452   if (lock.l_type == F_UNLCK) {
453       return 0;
454   }
455   return 1;
456 #endif
457   return 0;
458 }
459
460
461 /* -------------------------
462 */
463 static int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
464                             const off_t off, const off_t len, const int fork)
465 {
466   struct flock lock;
467   struct ad_fd *adf;
468   int err;
469   int type;  
470
471   lock.l_start = off;
472   type = locktype;
473   if (eid == ADEID_DFORK) {
474     adf = &ad->ad_data_fork;
475   } else {
476     /* FIXME META */
477     adf = &ad->ad_resource_fork;
478     if (adf->adf_fd == -1) {
479         /* there's no resource fork. return success */
480         return 0;
481     }
482     /* if ADLOCK_FILELOCK we want a lock from offset 0
483      * it's used when deleting a file:
484      * in open we put read locks on meta datas
485      * in delete a write locks on the whole file
486      * so if the file is open by somebody else it fails
487     */
488     if (!(type & ADLOCK_FILELOCK))
489         lock.l_start += ad_getentryoff(ad, eid);
490   }
491
492   if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
493       type = (type & ~ADLOCK_WR) | ADLOCK_RD;
494   }
495   
496   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
497   lock.l_whence = SEEK_SET;
498   lock.l_len = len;
499
500   /* see if it's locked by another fork. */
501   if (fork && adf_findxlock(adf, fork, ADLOCK_WR | 
502                     ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
503                     lock.l_start, lock.l_len) > -1) {
504     errno = EACCES;
505     return -1;
506   }
507
508   /* okay, we might have ranges byte-locked. we need to make sure that
509    * we restore the appropriate ranges once we're done. so, we check
510    * for overlap on an unlock and relock. 
511    * XXX: in the future, all the byte locks will be sorted and contiguous.
512    *      we just want to upgrade all the locks and then downgrade them
513    *      here. */
514   if (!adf->adf_excl) {
515        err = set_lock(adf->adf_fd, F_SETLK, &lock);
516   }
517   else {
518       err = 0;
519   }
520   if (!err && (lock.l_type == F_UNLCK))
521     adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
522
523   return err;
524 }
525
526 /* --------------------- */
527 static void ad_fcntl_unlock(struct adouble *ad, const int fork)
528 {
529   if (ad_data_fileno(ad) != -1) {
530     adf_unlock(&ad->ad_data_fork, fork);
531   }
532   if (ad_reso_fileno(ad) != -1) {
533     adf_unlock(&ad->ad_resource_fork, fork);
534   }
535
536   if (ad->ad_flags != AD_VERSION_EA) {
537     return;
538   }
539   if (ad_meta_fileno(ad) != -1) {
540     adf_unlock(&ad->ad_metadata_fork, fork);
541   }
542 }
543
544 /******************************************************************************
545  * Public functions
546  ******************************************************************************/
547
548 /* --------------- */
549 int ad_testlock(struct adouble *ad, int eid, const off_t off)
550 {
551     return 0;
552 #if 0
553   struct ad_fd *adf;
554   off_t      lock_offset;
555
556   lock_offset = off;
557   if (eid == ADEID_DFORK) {
558     adf = &ad->ad_data_fork;
559     if (ad_meta_fileno(ad) != -1) {
560         adf = ad->ad_md;
561         lock_offset = df2off(off);
562     }
563   } 
564   else { /* rfork */
565     if (ad_meta_fileno(ad) == -1) {
566         /* there's no resource fork. return no lock */
567         return 0;
568     }
569     adf = ad->ad_md;
570     lock_offset = hf2off(off);
571   }
572   return testlock(adf, lock_offset, 1);
573 #endif
574 }
575
576 /* -------------------------
577    return if a file is open by another process.
578    Optimized for the common case:
579    - there's no locks held by another process (clients)
580    - or we already know the answer and don't need to test.
581 */
582 uint16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
583 {
584     return 0;
585 #if 0
586   u_int16_t ret = 0;
587   struct ad_fd *adf;
588   off_t off;
589
590   if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
591       off_t len;
592       /* XXX know the locks layout: 
593          AD_FILELOCK_OPEN_WR is first 
594          and use it for merging requests
595       */
596       if (ad_meta_fileno(ad) != -1) {
597           /* there's a resource fork test the four bytes for
598            * data RW/RD and fork RW/RD locks in one request
599           */
600           adf = ad->ad_md;
601           off = LOCK_DATA_WR;
602           len = 4;
603       }
604       else {
605           /* no resource fork, only data RD/RW may exist */
606           adf = &ad->ad_data_fork;
607           off = AD_FILELOCK_OPEN_WR;
608           len = 2;
609       }
610       if (!testlock(adf, off, len))
611           return ret;
612   }
613   /* either there's a lock or we already know one 
614      fork is open
615   */
616   if (!(attrbits & ATTRBIT_DOPEN)) {
617       if (ad_meta_fileno(ad) != -1) {
618           adf = ad->ad_md;
619           off = LOCK_DATA_WR;
620       }
621       else {
622           adf = &ad->ad_data_fork;
623           off = AD_FILELOCK_OPEN_WR;
624       }
625       ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
626   }
627
628   if (!(attrbits & ATTRBIT_ROPEN)) {
629       if (ad_meta_fileno(ad) != -1) {
630           adf = ad->ad_md;
631           off = LOCK_RSRC_WR;
632           ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
633       }
634   }
635
636   return ret;
637 #endif
638 }
639
640 /* -------------------------
641    the fork is opened in Read Write, Deny Read, Deny Write mode
642    lock the whole file once   
643 */
644 int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
645 {
646     return 0;
647 #if 0
648   struct ad_fd *adf;
649   struct flock lock;
650   int    err;
651   
652   lock.l_start = 0;
653   lock.l_type = F_WRLCK;
654   lock.l_whence = SEEK_SET;
655   lock.l_len = 0;
656
657   if (eid == ADEID_DFORK) {
658     adf = &ad->ad_data_fork;
659   } else {
660     adf = &ad->ad_resource_fork;
661     lock.l_start = ad_getentryoff(ad, eid);
662   }
663   
664   err = set_lock(adf->adf_fd, F_SETLK, &lock);
665   if (!err)
666       adf->adf_excl = 1;
667   return err;
668 #endif
669 }
670
671 int ad_lock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
672 {
673     return 0;
674 }
675
676 void ad_unlock(struct adouble *ad, int user)
677 {
678     return;
679 }
680
681 int ad_tmplock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
682 {
683     return 0;
684 }