2 * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT for more information.
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.
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
28 #include <atalk/adouble.h>
30 #include "ad_private.h"
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)))
37 #define XLATE_FCNTL_LOCK(type) ((type) == ADLOCK_RD ? F_RDLCK : \
38 ((type) == ADLOCK_WR ? F_WRLCK : \
39 ((type) == ADLOCK_CLR ? F_UNLCK : -1)))
41 #define OVERLAP(a,alen,b,blen) ((!(alen) && (a) <= (b)) || \
42 (!(blen) && (b) <= (a)) || \
43 ((((a) + (alen)) > (b)) && \
44 (((b) + (blen)) > (a))))
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
52 /* remove a lock and compact space if necessary */
53 static __inline__ void adf_freelock(struct ad_fd *ad, const int i)
55 adf_lock_t *lock = ad->adf_lock + i;
57 if (--(*lock->refcount) < 1) {
59 lock->lock.l_type = F_UNLCK;
60 fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
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));
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;
77 tmp = (struct adf_lock_t *)
78 realloc(ad->adf_lock, sizeof(adf_lock_t)*
79 (ad->adf_lockcount + ARRAY_FREE_DELTA));
82 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
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
92 * i converted to using arrays of locks. everytime a lock
93 * gets removed, we shift all of the locks down.
95 static __inline__ void adf_unlock(struct ad_fd *ad, int fd, const int user)
97 adf_lock_t *lock = ad->adf_lock;
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 */
105 i--; /* we shifted things down, so we need to backtrack */
110 /* relock any byte lock that overlaps off/len. unlock everything
112 static __inline__ void adf_relockrange(struct ad_fd *ad, int fd,
113 const off_t off, const size_t len)
115 adf_lock_t *lock = ad->adf_lock;
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);
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,
131 adf_lock_t *lock = ad->adf_lock;
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)) {
147 /* search other user lock lists */
148 static __inline__ int adf_findxlock(struct ad_fd *ad,
149 const int user, const int type,
153 adf_lock_t *lock = ad->adf_lock;
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))
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.
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
175 * 2) if the header file doesn't exist, we stick the locks
176 * in the locations specified by AD_FILELOCK_RD/WR.
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)
187 adf_lock_t *adflock, *oldlock;
191 if (eid == ADEID_DFORK) {
192 if ((type & ADLOCK_FILELOCK) && (ad_hfileno(ad) != -1)) {
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;
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;
209 lock.l_start += ad_getentryoff(ad, eid);
212 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
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) {
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;
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)))) {
241 lock.l_whence = SEEK_SET;
244 /* now, update our list of locks */
246 if (lock.l_type == F_UNLCK) {
247 adf_freelock(adf, i);
251 /* attempt to lock the file. */
252 if (fcntl(adf->adf_fd, F_SETLK, &lock) < 0)
255 /* we upgraded this lock. */
256 if (adflock && (type & ADLOCK_UPGRADE)) {
257 memcpy(&adflock->lock, &lock, sizeof(lock));
261 /* it wasn't an upgrade */
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;
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));
276 adf->adf_lockmax += ARRAY_BLOCK_SIZE;
278 adflock = adf->adf_lock + adf->adf_lockcount;
281 memcpy(&adflock->lock, &lock, sizeof(lock));
282 adflock->user = user;
284 adflock->refcount = oldlock->refcount;
285 else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
289 (*adflock->refcount)++;
290 adf->adf_lockcount++;
294 lock.l_type = F_UNLCK;
295 fcntl(adf->adf_fd, F_SETLK, &lock);
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)
311 if (eid == ADEID_DFORK) {
315 lock.l_start += ad_getentryoff(ad, eid);
317 lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
318 lock.l_whence = SEEK_SET;
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
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);
335 void ad_fcntl_unlock(struct adouble *ad, const int user)
337 if (ad->ad_df.adf_fd != -1) {
338 adf_unlock(&ad->ad_df, ad->ad_df.adf_fd, user);
340 if (ad->ad_hf.adf_fd != -1) {
341 adf_unlock(&ad->ad_hf, ad->ad_hf.adf_fd, user);
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.
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)
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;
363 if ((EWOULDBLOCK != EAGAIN) && (errno == EWOULDBLOCK))
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
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)
379 int fd, oldlock, lock_type;
381 if (eid == ADEID_DFORK) {
382 oldlock = ad->ad_df.adf_lockcount;
385 oldlock = ad->ad_hf.adf_lockcount;
389 /* if we already have a write lock, we don't need to do anything */
390 if (oldlock == LOCK_EX) {
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 */
405 /* if we don't already have a lock, just do it. */
406 return flock(fd, lock_type | LOCK_NB);