]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_lock.c
6fab902858965091b11528fa704d38de48f86e1d
[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 /* ----------------------- */
37 static int set_lock(int fd, int cmd,  struct flock *lock)
38 {
39   if (fd == -2) {
40       /* We assign fd = -2 for symlinks -> do nothing */
41       if (cmd == F_GETLK)
42             lock->l_type = F_UNLCK;
43       return 0;
44   }
45   return fcntl(fd, cmd, lock);
46 }
47
48 /* ----------------------- */
49 static int XLATE_FCNTL_LOCK(int type) 
50 {
51     switch(type) {
52     case ADLOCK_RD:
53         return F_RDLCK;
54     case ADLOCK_WR:
55          return F_WRLCK;
56     case ADLOCK_CLR:
57          return F_UNLCK;
58     }
59     return -1;
60 }
61
62 /* ----------------------- */
63 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen) 
64 {
65     return (!alen && a <= b) || 
66         (!blen && b <= a) || 
67         ( (a + alen > b) && (b + blen > a) );
68 }
69
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
74
75 /* remove a lock and compact space if necessary */
76 static void adf_freelock(struct ad_fd *ad, const int i)
77 {
78 #if 0
79     adf_lock_t *lock = ad->adf_lock + i;
80
81     if (--(*lock->refcount) < 1) {
82         free(lock->refcount); 
83         if (!ad->adf_excl) {
84             lock->lock.l_type = F_UNLCK;
85             set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
86         }
87     }
88
89     ad->adf_lockcount--;
90
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));
94     }
95
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;
102
103             tmp = (struct adf_lock_t *) 
104                     realloc(ad->adf_lock, sizeof(adf_lock_t)*
105                             (ad->adf_lockcount + ARRAY_FREE_DELTA));
106             if (tmp) {
107                 ad->adf_lock = tmp;
108                 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
109             }
110     }
111 #endif
112 }
113
114
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
118  *
119  * i converted to using arrays of locks. everytime a lock
120  * gets removed, we shift all of the locks down.
121  */
122 static void adf_unlock(struct ad_fd *ad, const int fork)
123 {
124 #if 0
125     adf_lock_t *lock = ad->adf_lock;
126     int i;
127
128     for (i = 0; i < ad->adf_lockcount; i++) {
129       
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 */
133          adf_freelock(ad, i);
134          i--; /* we shifted things down, so we need to backtrack */
135          /* unlikely but realloc may have change adf_lock */
136          lock = ad->adf_lock;       
137       }
138     }
139 #endif
140 }
141
142 /* relock any byte lock that overlaps off/len. unlock everything
143  * else. */
144 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
145 {
146 #if 0
147     adf_lock_t *lock = ad->adf_lock;
148     int i;
149     
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);
153     }
154 #endif
155 }
156
157
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,
161                         const off_t off,
162                         const off_t len)
163 {
164 #if 0
165   adf_lock_t *lock = ad->adf_lock;
166   int i;
167   
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)) {
173       return i;
174     }
175   }
176 #endif
177   return -1;
178 }
179
180
181 /* search other fork lock lists */
182 static int adf_findxlock(struct ad_fd *ad, 
183                          const int fork, const int type,
184                          const off_t off,
185                          const off_t len)
186 {
187 #if 0
188   adf_lock_t *lock = ad->adf_lock;
189   int i;
190   
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)) 
196             return i;
197   } 
198 #endif
199   return -1;
200 }
201
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.
206  * 
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
210  *          of that.
211  *       2) if the header file doesn't exist, we stick the locks
212  *          in the locations specified by AD_FILELOCK_RD/WR.
213  */
214 #define LOCK_DATA_WR (0)
215 #define LOCK_DATA_RD (1)
216 #define LOCK_RSRC_WR (2)
217 #define LOCK_RSRC_RD (3)
218
219 #define LOCK_RSRC_DRD (4)
220 #define LOCK_RSRC_DWR (5)
221 #define LOCK_DATA_DRD (6)
222 #define LOCK_DATA_DWR (7)
223
224 #define LOCK_RSRC_NONE (8)
225 #define LOCK_DATA_NONE (9)
226
227 /* -------------- 
228         translate a data fork lock to an offset
229 */
230
231 static off_t df2off(int off)
232 {
233 int start = 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;
244         return start;
245 }
246
247 /* -------------- 
248         translate a resource fork lock to an offset
249 */
250
251 static off_t hf2off(int off)
252 {
253 int start = 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;
264         return start;
265 }
266
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)
270 {
271 #if 0
272   struct flock lock;
273   struct ad_fd *adf;
274   adf_lock_t *adflock;
275   int oldlock;
276   int i;
277   int type;  
278
279   lock.l_start = off;
280   type = locktype;
281   if (eid == ADEID_DFORK) {
282     adf = &ad->ad_data_fork;
283     if ((type & ADLOCK_FILELOCK)) {
284         if (ad_meta_fileno(ad) != -1) { /* META */
285             adf = ad->ad_md;
286             lock.l_start = df2off(off);
287         }
288     }
289   } else { /* rfork */
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
293          * locks are a mess.
294          */
295         errno = EACCES;
296         return -1;
297     }
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);
301     }
302     else {
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);
306     }
307   }
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. 
311     */
312   if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
313       type = (type & ~ADLOCK_WR) | ADLOCK_RD;
314   }
315   
316   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
317   lock.l_whence = SEEK_SET;
318   lock.l_len = len;
319
320   /* byte_lock(len=-1) lock whole file */
321   if (len == BYTELOCK_MAX) {
322       lock.l_len -= lock.l_start; /* otherwise  EOVERFLOW error */
323   }
324
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) {
332     errno = EACCES;
333     return -1;
334   }
335   
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;
339
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))
345        ||
346        (adflock
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) ))
351       ) {
352       errno = EINVAL;
353       return -1;
354   }
355
356
357   /* now, update our list of locks */
358   /* clear the lock */
359   if (lock.l_type == F_UNLCK) { 
360     adf_freelock(adf, i);
361     return 0;
362   }
363
364   /* attempt to lock the file. */
365   if (!adf->adf_excl && set_lock(adf->adf_fd, F_SETLK, &lock) < 0) 
366     return -1;
367
368   /* we upgraded this lock. */
369   if (adflock && (type & ADLOCK_UPGRADE)) {
370     memcpy(&adflock->lock, &lock, sizeof(lock));
371     return 0;
372   } 
373
374   /* it wasn't an upgrade */
375   oldlock = -1;
376   if (lock.l_type == F_RDLCK) {
377     oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
378   } 
379     
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));
385     if (!tmp)
386       goto fcntl_lock_err;
387     adf->adf_lock = tmp;
388     adf->adf_lockmax += ARRAY_BLOCK_SIZE;
389   } 
390   adflock = adf->adf_lock + adf->adf_lockcount;
391
392   /* fill in fields */
393   memcpy(&adflock->lock, &lock, sizeof(lock));
394   adflock->user = fork;
395   if (oldlock > -1) {
396     adflock->refcount = (adf->adf_lock + oldlock)->refcount;
397   } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
398     goto fcntl_lock_err;
399   }
400   
401   (*adflock->refcount)++;
402   adf->adf_lockcount++;
403   return 0;
404
405 fcntl_lock_err:
406   lock.l_type = F_UNLCK;
407   if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
408   return -1;
409 #endif
410   return 0;
411 }
412
413 /* -------------------------
414    we are using lock as tristate variable
415    
416    we have a lock ==> 1
417    no             ==> 0
418    error          ==> -1
419       
420 */
421 static int testlock(struct ad_fd *adf, off_t off, off_t len)
422 {
423 #if 0
424   struct flock lock;
425   adf_lock_t *plock;
426   int i;
427
428   lock.l_start = off;
429
430   plock = adf->adf_lock;
431   lock.l_whence = SEEK_SET;
432   lock.l_len = len;
433
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)) 
437         return 1;   /* */
438   }
439   /* Does another process have a lock? 
440   */
441   lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
442
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;
446   }
447   
448   if (lock.l_type == F_UNLCK) {
449       return 0;
450   }
451   return 1;
452 #endif
453   return 0;
454 }
455
456
457 /* -------------------------
458 */
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)
461 {
462   struct flock lock;
463   struct ad_fd *adf;
464   int err;
465   int type;  
466
467   lock.l_start = off;
468   type = locktype;
469   if (eid == ADEID_DFORK) {
470     adf = &ad->ad_data_fork;
471   } else {
472     /* FIXME META */
473     adf = &ad->ad_resource_fork;
474     if (adf->adf_fd == -1) {
475         /* there's no resource fork. return success */
476         return 0;
477     }
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
483     */
484     if (!(type & ADLOCK_FILELOCK))
485         lock.l_start += ad_getentryoff(ad, eid);
486   }
487
488   if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
489       type = (type & ~ADLOCK_WR) | ADLOCK_RD;
490   }
491   
492   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
493   lock.l_whence = SEEK_SET;
494   lock.l_len = len;
495
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) {
500     errno = EACCES;
501     return -1;
502   }
503
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
509    *      here. */
510   if (!adf->adf_excl) {
511        err = set_lock(adf->adf_fd, F_SETLK, &lock);
512   }
513   else {
514       err = 0;
515   }
516   if (!err && (lock.l_type == F_UNLCK))
517     adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
518
519   return err;
520 }
521
522 /* --------------------- */
523 static void ad_fcntl_unlock(struct adouble *ad, const int fork)
524 {
525   if (ad_data_fileno(ad) != -1) {
526     adf_unlock(&ad->ad_data_fork, fork);
527   }
528   if (ad_reso_fileno(ad) != -1) {
529     adf_unlock(&ad->ad_resource_fork, fork);
530   }
531
532   if (ad->ad_flags != AD_VERSION_EA) {
533     return;
534   }
535   if (ad_meta_fileno(ad) != -1) {
536     adf_unlock(&ad->ad_metadata_fork, fork);
537   }
538 }
539
540 /******************************************************************************
541  * Public functions
542  ******************************************************************************/
543
544 /* --------------- */
545 int ad_testlock(struct adouble *ad, int eid, const off_t off)
546 {
547     return 0;
548 #if 0
549   struct ad_fd *adf;
550   off_t      lock_offset;
551
552   lock_offset = off;
553   if (eid == ADEID_DFORK) {
554     adf = &ad->ad_data_fork;
555     if (ad_meta_fileno(ad) != -1) {
556         adf = ad->ad_md;
557         lock_offset = df2off(off);
558     }
559   } 
560   else { /* rfork */
561     if (ad_meta_fileno(ad) == -1) {
562         /* there's no resource fork. return no lock */
563         return 0;
564     }
565     adf = ad->ad_md;
566     lock_offset = hf2off(off);
567   }
568   return testlock(adf, lock_offset, 1);
569 #endif
570 }
571
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.
577 */
578 uint16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
579 {
580     return 0;
581 #if 0
582   u_int16_t ret = 0;
583   struct ad_fd *adf;
584   off_t off;
585
586   if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
587       off_t len;
588       /* XXX know the locks layout: 
589          AD_FILELOCK_OPEN_WR is first 
590          and use it for merging requests
591       */
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
595           */
596           adf = ad->ad_md;
597           off = LOCK_DATA_WR;
598           len = 4;
599       }
600       else {
601           /* no resource fork, only data RD/RW may exist */
602           adf = &ad->ad_data_fork;
603           off = AD_FILELOCK_OPEN_WR;
604           len = 2;
605       }
606       if (!testlock(adf, off, len))
607           return ret;
608   }
609   /* either there's a lock or we already know one 
610      fork is open
611   */
612   if (!(attrbits & ATTRBIT_DOPEN)) {
613       if (ad_meta_fileno(ad) != -1) {
614           adf = ad->ad_md;
615           off = LOCK_DATA_WR;
616       }
617       else {
618           adf = &ad->ad_data_fork;
619           off = AD_FILELOCK_OPEN_WR;
620       }
621       ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
622   }
623
624   if (!(attrbits & ATTRBIT_ROPEN)) {
625       if (ad_meta_fileno(ad) != -1) {
626           adf = ad->ad_md;
627           off = LOCK_RSRC_WR;
628           ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
629       }
630   }
631
632   return ret;
633 #endif
634 }
635
636 /* -------------------------
637    the fork is opened in Read Write, Deny Read, Deny Write mode
638    lock the whole file once   
639 */
640 int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
641 {
642     return 0;
643 #if 0
644   struct ad_fd *adf;
645   struct flock lock;
646   int    err;
647   
648   lock.l_start = 0;
649   lock.l_type = F_WRLCK;
650   lock.l_whence = SEEK_SET;
651   lock.l_len = 0;
652
653   if (eid == ADEID_DFORK) {
654     adf = &ad->ad_data_fork;
655   } else {
656     adf = &ad->ad_resource_fork;
657     lock.l_start = ad_getentryoff(ad, eid);
658   }
659   
660   err = set_lock(adf->adf_fd, F_SETLK, &lock);
661   if (!err)
662       adf->adf_excl = 1;
663   return err;
664 #endif
665 }
666
667 int ad_lock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
668 {
669     return 0;
670 }
671
672 void ad_unlock(struct adouble *ad, int user)
673 {
674     return;
675 }
676
677 int ad_tmplock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
678 {
679     return 0;
680 }