]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_lock.c
implemented config.h
[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
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27
28 #include <atalk/adouble.h>
29
30 #include "ad_private.h"
31
32 /* translate between ADLOCK styles and specific locking mechanisms */
33 #define XLATE_FLOCK(type) ((type) == ADLOCK_RD ? LOCK_SH : \
34 ((type) == ADLOCK_WR ? LOCK_EX : \
35  ((type) == ADLOCK_CLR ? LOCK_UN : -1)))
36
37 #define XLATE_FCNTL_LOCK(type) ((type) == ADLOCK_RD ? F_RDLCK : \
38 ((type) == ADLOCK_WR ? F_WRLCK : \
39  ((type) == ADLOCK_CLR ? F_UNLCK : -1))) 
40      
41 #define OVERLAP(a,alen,b,blen) ((!(alen) && (a) <= (b)) || \
42                                 (!(blen) && (b) <= (a)) || \
43                                 ((((a) + (alen)) > (b)) && \
44                                 (((b) + (blen)) > (a))))
45
46
47 /* allocation for lock regions. we allocate aggressively and shrink
48  * only in large chunks. */
49 #define ARRAY_BLOCK_SIZE 10 
50 #define ARRAY_FREE_DELTA 100
51
52 /* remove a lock and compact space if necessary */
53 static __inline__ void adf_freelock(struct ad_fd *ad, const int i)
54 {
55     adf_lock_t *lock = ad->adf_lock + i;
56
57     if (--(*lock->refcount) < 1) {
58         free(lock->refcount); 
59         lock->lock.l_type = F_UNLCK;
60         fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
61     }
62
63     ad->adf_lockcount--;
64
65     /* move another lock into the empty space */
66     if (i < ad->adf_lockcount) {
67         memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
68     }
69
70     /* free extra cruft if we go past a boundary. we always want to
71      * keep at least some stuff around for allocations. this wastes
72      * a bit of space to save time on reallocations. */
73     if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
74         (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
75             struct adf_lock_t *tmp;
76
77             tmp = (struct adf_lock_t *) 
78                     realloc(ad->adf_lock, sizeof(adf_lock_t)*
79                             (ad->adf_lockcount + ARRAY_FREE_DELTA));
80             if (tmp) {
81                 ad->adf_lock = tmp;
82                 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
83             }
84     }
85 }
86
87
88 /* this needs to deal with the following cases:
89  * 1) user is the only user of the lock 
90  * 2) user shares a read lock with another user
91  *
92  * i converted to using arrays of locks. everytime a lock
93  * gets removed, we shift all of the locks down.
94  */
95 static __inline__ void adf_unlock(struct ad_fd *ad, int fd, const int user)
96 {
97     adf_lock_t *lock = ad->adf_lock;
98     int i;
99
100     for (i = 0; i < ad->adf_lockcount; i++) {
101       if (lock[i].user == user) {
102         /* we're really going to delete this lock. note: read locks
103            are the only ones that allow refcounts > 1 */
104          adf_freelock(ad, i);
105          i--; /* we shifted things down, so we need to backtrack */
106        }
107     }
108 }
109
110 /* relock any byte lock that overlaps off/len. unlock everything
111  * else. */
112 static __inline__ void adf_relockrange(struct ad_fd *ad, int fd,
113                                        const off_t off, const size_t len)
114 {
115     adf_lock_t *lock = ad->adf_lock;
116     int i;
117     
118     for (i = 0; i < ad->adf_lockcount; i++) {
119       if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
120         fcntl(fd, F_SETLK, &lock[i].lock);
121     }
122 }
123
124
125 /* find a byte lock that overlaps off/len for a particular user */
126 static __inline__ int adf_findlock(struct ad_fd *ad,
127                                    const int user, const int type,
128                                    const off_t off,
129                                    const size_t len)
130 {
131   adf_lock_t *lock = ad->adf_lock;
132   int i;
133   
134   for (i = 0; i < ad->adf_lockcount; i++) {
135     if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
136         ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
137         (lock[i].user == user) && 
138         OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
139       return i;
140     }
141   }
142
143   return -1;
144 }
145
146
147 /* search other user lock lists */
148 static __inline__  int adf_findxlock(struct ad_fd *ad, 
149                                      const int user, const int type,
150                                      const off_t off,
151                                      const size_t len)
152 {
153   adf_lock_t *lock = ad->adf_lock;
154   int i;
155   
156   for (i = 0; i < ad->adf_lockcount; i++) {
157     if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
158          ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
159         (lock[i].user != user) && 
160         OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
161             return i;
162   } 
163   return -1;
164 }
165
166 /* okay, this needs to do the following:
167  * 1) check current list of locks. error on conflict.
168  * 2) apply the lock. error on conflict with another process.
169  * 3) update the list of locks this file has.
170  * 
171  * NOTE: this treats synchronization locks a little differently. we
172  *       do the following things for those:
173  *       1) if the header file exists, all the locks go in the beginning
174  *          of that.
175  *       2) if the header file doesn't exist, we stick the locks
176  *          in the locations specified by AD_FILELOCK_RD/WR.
177  */
178 #define LOCK_RSRC_RD (0)
179 #define LOCK_RSRC_WR (1)
180 #define LOCK_DATA_RD (2)
181 #define LOCK_DATA_WR (3)
182 int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int type,
183                   const off_t off, const size_t len, const int user)
184 {
185   struct flock lock;
186   struct ad_fd *adf;
187   adf_lock_t *adflock, *oldlock;
188   int i;
189   
190   lock.l_start = off;
191   if (eid == ADEID_DFORK) {
192     if ((type & ADLOCK_FILELOCK) && (ad_hfileno(ad) != -1)) {
193       adf = &ad->ad_hf;
194       if (off == AD_FILELOCK_WR)
195         lock.l_start = LOCK_DATA_WR;
196       else if (off == AD_FILELOCK_RD)
197         lock.l_start = LOCK_DATA_RD;
198     } else
199       adf = &ad->ad_df;
200
201   } else { /* rfork */
202     adf = &ad->ad_hf;
203     if (type & ADLOCK_FILELOCK) {
204       if (off == AD_FILELOCK_WR)
205         lock.l_start = LOCK_RSRC_WR;
206       else if (off == AD_FILELOCK_RD)
207         lock.l_start = LOCK_RSRC_RD;
208     } else
209       lock.l_start += ad_getentryoff(ad, eid);
210   }
211
212   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
213
214   /* see if it's locked by another user. 
215    * NOTE: this guarantees that any existing locks must be at most
216    * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
217    * guaranteed to be ORable. */
218   if (adf_findxlock(adf, user, ADLOCK_WR | 
219                     ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
220                     lock.l_start, len) > -1) {
221     errno = EACCES;
222     return -1;
223   }
224   
225   /* look for any existing lock that we may have */
226   i = adf_findlock(adf, user, ADLOCK_RD | ADLOCK_WR, lock.l_start, len);
227   adflock = (i < 0) ? NULL : adf->adf_lock + i;
228
229   /* here's what we check for:
230      1) we're trying to re-lock a lock, but we didn't specify an update.
231      2) we're trying to free only part of a lock. 
232      3) we're trying to free a non-existent lock. */
233   if ((!adflock && (lock.l_type == F_UNLCK)) ||
234       (adflock && !(type & ADLOCK_UPGRADE) && 
235        ((lock.l_type != F_UNLCK) || (adflock->lock.l_start != lock.l_start) ||
236         (adflock->lock.l_len != len)))) {
237     errno = EINVAL;
238     return -1;
239   }
240
241   lock.l_whence = SEEK_SET;
242   lock.l_len = len;
243
244   /* now, update our list of locks */
245   /* clear the lock */
246   if (lock.l_type == F_UNLCK) { 
247     adf_freelock(adf, i);
248     return 0;
249   }
250
251   /* attempt to lock the file. */
252   if (fcntl(adf->adf_fd, F_SETLK, &lock) < 0) 
253     return -1;
254
255   /* we upgraded this lock. */
256   if (adflock && (type & ADLOCK_UPGRADE)) {
257     memcpy(&adflock->lock, &lock, sizeof(lock));
258     return 0;
259   } 
260
261   /* it wasn't an upgrade */
262   oldlock = NULL;
263   if ((lock.l_type = F_RDLCK) &&
264       ((i = adf_findxlock(adf, user, ADLOCK_RD, lock.l_start, len)) > -1)) {
265     oldlock = adf->adf_lock + i;
266   } 
267     
268   /* no more space. this will also happen if lockmax == lockcount == 0 */
269   if (adf->adf_lockmax == adf->adf_lockcount) { 
270     adf_lock_t *tmp = (adf_lock_t *) 
271             realloc(adf->adf_lock, sizeof(adf_lock_t)*
272                     (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
273     if (!tmp)
274       goto fcntl_lock_err;
275     adf->adf_lock = tmp;
276     adf->adf_lockmax += ARRAY_BLOCK_SIZE;
277   } 
278   adflock = adf->adf_lock + adf->adf_lockcount;
279
280   /* fill in fields */
281   memcpy(&adflock->lock, &lock, sizeof(lock));
282   adflock->user = user;
283   if (oldlock)
284     adflock->refcount = oldlock->refcount;
285   else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
286     goto fcntl_lock_err;
287   }
288   
289   (*adflock->refcount)++;
290   adf->adf_lockcount++;
291   return 0;
292
293 fcntl_lock_err:
294   lock.l_type = F_UNLCK;
295   fcntl(adf->adf_fd, F_SETLK, &lock);
296   return -1;
297 }
298
299
300 /* with temp locks, we don't need to distinguish within the same
301  * process as everything is single-threaded. in addition, if
302  * multi-threading gets added, it will only be in a few areas. */
303 int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int type,
304                      const off_t off, const size_t len)
305 {
306   struct flock lock;
307   struct ad_fd *adf;
308   int err;
309
310   lock.l_start = off;
311   if (eid == ADEID_DFORK) {
312     adf = &ad->ad_df;
313   } else {
314     adf = &ad->ad_hf;
315     lock.l_start += ad_getentryoff(ad, eid);
316   }
317   lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
318   lock.l_whence = SEEK_SET;
319   lock.l_len = len;
320
321   /* okay, we might have ranges byte-locked. we need to make sure that
322    * we restore the appropriate ranges once we're done. so, we check
323    * for overlap on an unlock and relock. 
324    * XXX: in the future, all the byte locks will be sorted and contiguous.
325    *      we just want to upgrade all the locks and then downgrade them
326    *      here. */
327   err = fcntl(adf->adf_fd, F_SETLK, &lock);
328   if (!err && (lock.l_type == F_UNLCK))
329     adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
330
331   return err;
332 }
333
334
335 void ad_fcntl_unlock(struct adouble *ad, const int user)
336 {
337   if (ad->ad_df.adf_fd != -1) {
338     adf_unlock(&ad->ad_df, ad->ad_df.adf_fd, user);
339   }
340   if (ad->ad_hf.adf_fd != -1) {
341     adf_unlock(&ad->ad_hf, ad->ad_hf.adf_fd, user);
342   }
343 }
344
345 /* byte-range locks. ad_lock is used by afp_bytelock and afp_openfork
346  * to establish locks. both ad_lock and ad_tmplock take 0, 0, 0 to
347  * signify locking of the entire file. in the absence of working 
348  * byte-range locks, this will default to file-wide flock-style locks.
349  */
350 int ad_flock_lock(struct adouble *ad, const u_int32_t eid, const int type,
351                   const off_t off, const size_t len, const int user)
352 {
353   int err, lock_type;
354   
355   lock_type = XLATE_FLOCK(type & ADLOCK_MASK);
356   if (eid == ADEID_DFORK) {
357     if ((err = flock(ad_dfileno(ad), lock_type | LOCK_NB)) == 0)
358       ad->ad_df.adf_lockcount = lock_type;
359   } else if ((err = flock(ad_hfileno(ad), lock_type | LOCK_NB)) == 0)
360     ad->ad_hf.adf_lockcount = lock_type;
361
362   if (err) {
363     if ((EWOULDBLOCK != EAGAIN) && (errno == EWOULDBLOCK))
364       errno = EAGAIN;
365     return -1;
366   } 
367
368   return 0; 
369 }
370
371 /* ad_tmplock is used by afpd to lock actual read/write operations. 
372  * it saves the current lock state before attempting to lock to prevent
373  * mixups. if byte-locks don't exist, it will lock the entire file with
374  * an flock. we can be a little smart here by just upgrading/downgrading
375  * locks. */
376 int ad_flock_tmplock(struct adouble *ad, const u_int32_t eid, const int type,
377                      const off_t off, const size_t len) 
378 {
379   int fd, oldlock, lock_type;
380   
381   if (eid == ADEID_DFORK) {
382     oldlock = ad->ad_df.adf_lockcount;
383     fd = ad_dfileno(ad);
384   } else {
385     oldlock = ad->ad_hf.adf_lockcount;
386     fd = ad_hfileno(ad);
387   }
388
389   /* if we already have a write lock, we don't need to do anything */
390   if (oldlock == LOCK_EX) {
391     return 0;
392   }
393
394   /* if we have a read lock, upgrade it if necessary */
395   lock_type = XLATE_FLOCK(type & ADLOCK_MASK);
396   if (oldlock == LOCK_SH) {
397     if (lock_type == LOCK_EX) 
398       return flock(fd, LOCK_EX | LOCK_NB);
399     else if (lock_type == LOCK_UN) /* reset it */
400       return flock(fd, LOCK_SH | LOCK_NB);
401     else /* do nothing */
402       return 0;
403   }
404
405   /* if we don't already have a lock, just do it. */
406   return flock(fd, lock_type | LOCK_NB);
407 }
408
409