2 Unix SMB/Netbios implementation.
4 byte range locking code
5 Updated to handle range splits/merges.
7 Copyright (C) Andrew Tridgell 1992-2000
8 Copyright (C) Jeremy Allison 1992-2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* This module implements a tdb based byte range locking service,
26 replacing the fcntl() based byte range locking previously
27 used. This allows us to provide the same semantics as NT */
31 extern int DEBUGLEVEL;
33 /* This contains elements that differentiate locks. The smbpid is a
34 client supplied pid, and is essentially the locking context for
43 /* The data in brlock records is an unsorted linear array of these
44 records. It is unnecessary to store the count as tdb provides the
48 struct lock_context context;
52 enum brl_type lock_type;
55 /* The key used in the brlock database. */
62 /* The open brlock.tdb database. */
64 static TDB_CONTEXT *tdb;
66 /****************************************************************************
67 Create a locking key - ensuring zero filled for pad purposes.
68 ****************************************************************************/
70 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
72 static struct lock_key key;
75 memset(&key, '\0', sizeof(key));
78 kbuf.dptr = (char *)&key;
79 kbuf.dsize = sizeof(key);
83 /****************************************************************************
84 See if two locking contexts are equal.
85 ****************************************************************************/
87 static BOOL brl_same_context(struct lock_context *ctx1,
88 struct lock_context *ctx2)
90 return (ctx1->pid == ctx2->pid) &&
91 (ctx1->smbpid == ctx2->smbpid) &&
92 (ctx1->tid == ctx2->tid);
95 /****************************************************************************
96 See if lock2 can be added when lock1 is in place.
97 ****************************************************************************/
99 static BOOL brl_conflict(struct lock_struct *lck1,
100 struct lock_struct *lck2)
102 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
106 if (brl_same_context(&lck1->context, &lck2->context) &&
107 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
111 if (lck1->start >= (lck2->start + lck2->size) ||
112 lck2->start >= (lck1->start + lck1->size)) {
119 /****************************************************************************
120 Check to see if this lock conflicts, but ignore our own locks on the
122 ****************************************************************************/
124 static BOOL brl_conflict_other(struct lock_struct *lck1, struct lock_struct *lck2)
126 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
129 if (brl_same_context(&lck1->context, &lck2->context) &&
130 lck1->fnum == lck2->fnum)
133 if (lck1->start >= (lck2->start + lck2->size) ||
134 lck2->start >= (lck1->start + lck1->size)) return False;
140 /****************************************************************************
141 Delete a record if it is for a dead process, if check_self is true, then
142 delete any records belonging to this pid also (there shouldn't be any).
143 ****************************************************************************/
145 static int delete_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
147 struct lock_struct *locks;
149 BOOL check_self = *(BOOL *)state;
150 pid_t mypid = sys_getpid();
152 tdb_chainlock(tdb, kbuf);
154 locks = (struct lock_struct *)dbuf.dptr;
156 count = dbuf.dsize / sizeof(*locks);
157 for (i=0; i<count; i++) {
158 struct lock_struct *lock = &locks[i];
160 /* If check_self is true we want to remove our own records. */
161 if (check_self && (mypid == lock->context.pid)) {
163 DEBUG(0,("brlock : delete_fn. LOGIC ERROR ! Shutting down and a record for my pid (%u) exists !\n",
164 (unsigned int)lock->context.pid ));
166 } else if (process_exists(lock->context.pid)) {
168 DEBUG(10,("brlock : delete_fn. pid %u exists.\n", (unsigned int)lock->context.pid ));
172 DEBUG(10,("brlock : delete_fn. Deleting record for process %u\n",
173 (unsigned int)lock->context.pid ));
175 if (count > 1 && i < count-1) {
176 memmove(&locks[i], &locks[i+1],
177 sizeof(*locks)*((count-1) - i));
184 tdb_delete(tdb, kbuf);
185 } else if (count < (dbuf.dsize / sizeof(*locks))) {
186 dbuf.dsize = count * sizeof(*locks);
187 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
190 tdb_chainunlock(tdb, kbuf);
194 /****************************************************************************
195 Open up the brlock.tdb database.
196 ****************************************************************************/
198 void brl_init(int read_only)
200 BOOL check_self = False;
204 tdb = tdb_open_log(lock_path("brlock.tdb"), 0, TDB_CLEAR_IF_FIRST|USE_TDB_MMAP_FLAG,
205 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644);
207 DEBUG(0,("Failed to open byte range locking database\n"));
211 /* delete any dead locks */
213 tdb_traverse(tdb, delete_fn, &check_self);
216 /****************************************************************************
217 Close down the brlock.tdb database.
218 ****************************************************************************/
220 void brl_shutdown(int read_only)
222 BOOL check_self = True;
227 /* delete any dead locks */
229 tdb_traverse(tdb, delete_fn, &check_self);
234 /****************************************************************************
235 Lock a range of bytes.
236 ****************************************************************************/
238 BOOL brl_lock(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
239 uint16 smbpid, pid_t pid, uint16 tid,
240 br_off start, br_off size,
241 enum brl_type lock_type)
245 struct lock_struct lock, *locks;
248 kbuf = locking_key(dev,ino);
253 if (start == 0 && size == 0) {
254 tdb_delete(tdb, kbuf);
259 tdb_chainlock(tdb, kbuf);
260 dbuf = tdb_fetch(tdb, kbuf);
262 lock.context.smbpid = smbpid;
263 lock.context.pid = pid;
264 lock.context.tid = tid;
268 lock.lock_type = lock_type;
271 /* there are existing locks - make sure they don't conflict */
272 locks = (struct lock_struct *)dbuf.dptr;
273 count = dbuf.dsize / sizeof(*locks);
274 for (i=0; i<count; i++) {
275 if (brl_conflict(&locks[i], &lock)) {
282 if (start == 0 && size == 0) {
283 if (dbuf.dptr) free(dbuf.dptr);
284 tdb_chainunlock(tdb, kbuf);
289 /* no conflicts - add it to the list of locks */
290 tp = Realloc(dbuf.dptr, dbuf.dsize + sizeof(*locks));
293 memcpy(dbuf.dptr + dbuf.dsize, &lock, sizeof(lock));
294 dbuf.dsize += sizeof(lock);
295 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
298 tdb_chainunlock(tdb, kbuf);
302 if (dbuf.dptr) free(dbuf.dptr);
303 tdb_chainunlock(tdb, kbuf);
307 /****************************************************************************
308 Unlock a range of bytes.
309 ****************************************************************************/
311 BOOL brl_unlock(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
312 uint16 smbpid, pid_t pid, uint16 tid,
313 br_off start, br_off size)
317 struct lock_struct *locks;
318 struct lock_context context;
320 kbuf = locking_key(dev,ino);
324 tdb_chainlock(tdb, kbuf);
325 dbuf = tdb_fetch(tdb, kbuf);
328 DEBUG(10,("brl_unlock: tdb_fetch failed !\n"));
332 context.smbpid = smbpid;
336 /* there are existing locks - find a match */
337 locks = (struct lock_struct *)dbuf.dptr;
338 count = dbuf.dsize / sizeof(*locks);
340 for (i=0; i<count; i++) {
341 struct lock_struct *lock = &locks[i];
343 if (lock->lock_type == WRITE_LOCK &&
344 brl_same_context(&lock->context, &context) &&
345 lock->fnum == fnum &&
346 lock->start == start &&
347 lock->size == size) {
348 /* found it - delete it */
350 tdb_delete(tdb, kbuf);
353 memmove(&locks[i], &locks[i+1],
354 sizeof(*locks)*((count-1) - i));
356 dbuf.dsize -= sizeof(*locks);
357 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
361 tdb_chainunlock(tdb, kbuf);
366 locks = (struct lock_struct *)dbuf.dptr;
367 count = dbuf.dsize / sizeof(*locks);
368 for (i=0; i<count; i++) {
369 struct lock_struct *lock = &locks[i];
371 if (brl_same_context(&lock->context, &context) &&
372 lock->fnum == fnum &&
373 lock->start == start &&
374 lock->size == size) {
375 /* found it - delete it */
377 tdb_delete(tdb, kbuf);
380 memmove(&locks[i], &locks[i+1],
381 sizeof(*locks)*((count-1) - i));
383 dbuf.dsize -= sizeof(*locks);
384 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
388 tdb_chainunlock(tdb, kbuf);
393 /* we didn't find it */
396 if (dbuf.dptr) free(dbuf.dptr);
397 tdb_chainunlock(tdb, kbuf);
402 /****************************************************************************
403 Test if we could add a lock if we wanted to.
404 ****************************************************************************/
406 BOOL brl_locktest(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
407 uint16 smbpid, pid_t pid, uint16 tid,
408 br_off start, br_off size,
409 enum brl_type lock_type, int check_self)
413 struct lock_struct lock, *locks;
415 kbuf = locking_key(dev,ino);
419 tdb_chainlock(tdb, kbuf);
420 dbuf = tdb_fetch(tdb, kbuf);
422 lock.context.smbpid = smbpid;
423 lock.context.pid = pid;
424 lock.context.tid = tid;
428 lock.lock_type = lock_type;
431 /* there are existing locks - make sure they don't conflict */
432 locks = (struct lock_struct *)dbuf.dptr;
433 count = dbuf.dsize / sizeof(*locks);
434 for (i=0; i<count; i++) {
436 if (brl_conflict(&locks[i], &lock))
440 * Our own locks don't conflict.
442 if (brl_conflict_other(&locks[i], &lock))
448 /* no conflicts - we could have added it */
450 tdb_chainunlock(tdb, kbuf);
454 if (dbuf.dptr) free(dbuf.dptr);
455 tdb_chainunlock(tdb, kbuf);
459 /****************************************************************************
460 Remove any locks associated with a open file.
461 ****************************************************************************/
463 void brl_close(SMB_DEV_T dev, SMB_INO_T ino, pid_t pid, int tid, int fnum)
466 int count, i, dcount=0;
467 struct lock_struct *locks;
469 kbuf = locking_key(dev,ino);
473 tdb_chainlock(tdb, kbuf);
474 dbuf = tdb_fetch(tdb, kbuf);
476 if (!dbuf.dptr) goto fail;
478 /* there are existing locks - remove any for this fnum */
479 locks = (struct lock_struct *)dbuf.dptr;
480 count = dbuf.dsize / sizeof(*locks);
481 for (i=0; i<count; i++) {
482 struct lock_struct *lock = &locks[i];
484 if (lock->context.tid == tid &&
485 lock->context.pid == pid &&
486 lock->fnum == fnum) {
487 /* found it - delete it */
488 if (count > 1 && i < count-1) {
489 memmove(&locks[i], &locks[i+1],
490 sizeof(*locks)*((count-1) - i));
499 tdb_delete(tdb, kbuf);
500 } else if (count < (dbuf.dsize / sizeof(*locks))) {
501 dbuf.dsize -= dcount * sizeof(*locks);
502 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
505 /* we didn't find it */
507 if (dbuf.dptr) free(dbuf.dptr);
508 tdb_chainunlock(tdb, kbuf);
511 /****************************************************************************
512 Traverse the whole database with this function, calling traverse_callback
514 ****************************************************************************/
516 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
518 struct lock_struct *locks;
519 struct lock_key *key;
522 BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
524 locks = (struct lock_struct *)dbuf.dptr;
525 key = (struct lock_key *)kbuf.dptr;
527 for (i=0;i<dbuf.dsize/sizeof(*locks);i++) {
528 traverse_callback(key->device, key->inode,
529 locks[i].context.pid,
537 /*******************************************************************
538 Call the specified function on each lock in the database.
539 ********************************************************************/
541 int brl_forall(BRLOCK_FN(fn))
544 return tdb_traverse(tdb, traverse_fn, (void *)fn);