2 Unix SMB/CIFS implementation.
3 byte range locking code
4 Updated to handle range splits/merges.
6 Copyright (C) Andrew Tridgell 1992-2000
7 Copyright (C) Jeremy Allison 1992-2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This module implements a tdb based byte range locking service,
25 replacing the fcntl() based byte range locking previously
26 used. This allows us to provide the same semantics as NT */
32 /* This contains elements that differentiate locks. The smbpid is a
33 client supplied pid, and is essentially the locking context for
42 /* The data in brlock records is an unsorted linear array of these
43 records. It is unnecessary to store the count as tdb provides the
47 struct lock_context context;
51 enum brl_type lock_type;
54 /* The key used in the brlock database. */
61 /* The open brlock.tdb database. */
63 static TDB_CONTEXT *tdb;
65 /****************************************************************************
66 Create a locking key - ensuring zero filled for pad purposes.
67 ****************************************************************************/
69 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
71 static struct lock_key key;
74 memset(&key, '\0', sizeof(key));
77 kbuf.dptr = (char *)&key;
78 kbuf.dsize = sizeof(key);
82 /****************************************************************************
83 See if two locking contexts are equal.
84 ****************************************************************************/
86 static BOOL brl_same_context(struct lock_context *ctx1,
87 struct lock_context *ctx2)
89 return (ctx1->pid == ctx2->pid) &&
90 (ctx1->smbpid == ctx2->smbpid) &&
91 (ctx1->tid == ctx2->tid);
94 /****************************************************************************
95 See if lock2 can be added when lock1 is in place.
96 ****************************************************************************/
98 static BOOL brl_conflict(struct lock_struct *lck1,
99 struct lock_struct *lck2)
101 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
105 if (brl_same_context(&lck1->context, &lck2->context) &&
106 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
110 if (lck1->start >= (lck2->start + lck2->size) ||
111 lck2->start >= (lck1->start + lck1->size)) {
119 static BOOL brl_conflict1(struct lock_struct *lck1,
120 struct lock_struct *lck2)
122 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
126 if (brl_same_context(&lck1->context, &lck2->context) &&
127 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
131 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
135 if (lck1->start >= (lck2->start + lck2->size) ||
136 lck2->start >= (lck1->start + lck1->size)) {
144 /****************************************************************************
145 Check to see if this lock conflicts, but ignore our own locks on the
147 ****************************************************************************/
149 static BOOL brl_conflict_other(struct lock_struct *lck1, struct lock_struct *lck2)
151 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
154 if (brl_same_context(&lck1->context, &lck2->context) &&
155 lck1->fnum == lck2->fnum)
158 if (lck1->start >= (lck2->start + lck2->size) ||
159 lck2->start >= (lck1->start + lck1->size)) return False;
165 /****************************************************************************
166 Delete a record if it is for a dead process, if check_self is true, then
167 delete any records belonging to this pid also (there shouldn't be any).
168 ****************************************************************************/
170 static int delete_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
172 struct lock_struct *locks;
174 BOOL check_self = *(BOOL *)state;
175 pid_t mypid = sys_getpid();
177 tdb_chainlock(tdb, kbuf);
179 locks = (struct lock_struct *)dbuf.dptr;
181 count = dbuf.dsize / sizeof(*locks);
182 for (i=0; i<count; i++) {
183 struct lock_struct *lock = &locks[i];
185 /* If check_self is true we want to remove our own records. */
186 if (check_self && (mypid == lock->context.pid)) {
188 DEBUG(0,("brlock : delete_fn. LOGIC ERROR ! Shutting down and a record for my pid (%u) exists !\n",
189 (unsigned int)lock->context.pid ));
191 } else if (process_exists(lock->context.pid)) {
193 DEBUG(10,("brlock : delete_fn. pid %u exists.\n", (unsigned int)lock->context.pid ));
197 DEBUG(10,("brlock : delete_fn. Deleting record for process %u\n",
198 (unsigned int)lock->context.pid ));
200 if (count > 1 && i < count-1) {
201 memmove(&locks[i], &locks[i+1],
202 sizeof(*locks)*((count-1) - i));
209 tdb_delete(tdb, kbuf);
210 } else if (count < (dbuf.dsize / sizeof(*locks))) {
211 dbuf.dsize = count * sizeof(*locks);
212 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
215 tdb_chainunlock(tdb, kbuf);
219 /****************************************************************************
220 Open up the brlock.tdb database.
221 ****************************************************************************/
223 void brl_init(int read_only)
225 BOOL check_self = False;
229 tdb = tdb_open_log(lock_path("brlock.tdb"), 0, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
230 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644);
232 DEBUG(0,("Failed to open byte range locking database\n"));
236 /* delete any dead locks */
238 tdb_traverse(tdb, delete_fn, &check_self);
241 /****************************************************************************
242 Close down the brlock.tdb database.
243 ****************************************************************************/
245 void brl_shutdown(int read_only)
247 BOOL check_self = True;
252 /* delete any dead locks */
254 tdb_traverse(tdb, delete_fn, &check_self);
260 /****************************************************************************
261 compare two locks for sorting
262 ****************************************************************************/
263 static int lock_compare(struct lock_struct *lck1,
264 struct lock_struct *lck2)
266 if (lck1->start != lck2->start) return (lck1->start - lck2->start);
267 if (lck2->size != lck1->size) {
268 return ((int)lck1->size - (int)lck2->size);
274 /****************************************************************************
275 Lock a range of bytes.
276 ****************************************************************************/
278 NTSTATUS brl_lock(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
279 uint16 smbpid, pid_t pid, uint16 tid,
280 br_off start, br_off size,
281 enum brl_type lock_type)
285 struct lock_struct lock, *locks;
287 NTSTATUS status = NT_STATUS_OK;
288 static int last_failed = -1;
289 static br_off last_failed_start;
291 kbuf = locking_key(dev,ino);
296 if (start == 0 && size == 0) {
297 DEBUG(0,("client sent 0/0 lock - please report this\n"));
301 tdb_chainlock(tdb, kbuf);
302 dbuf = tdb_fetch(tdb, kbuf);
304 lock.context.smbpid = smbpid;
305 lock.context.pid = pid;
306 lock.context.tid = tid;
310 lock.lock_type = lock_type;
313 /* there are existing locks - make sure they don't conflict */
314 locks = (struct lock_struct *)dbuf.dptr;
315 count = dbuf.dsize / sizeof(*locks);
316 for (i=0; i<count; i++) {
317 if (brl_conflict(&locks[i], &lock)) {
318 status = NT_STATUS_LOCK_NOT_GRANTED;
322 if (lock.start == 0 && lock.size == 0 &&
323 locks[i].size == 0) {
330 /* no conflicts - add it to the list of locks */
331 tp = Realloc(dbuf.dptr, dbuf.dsize + sizeof(*locks));
333 status = NT_STATUS_NO_MEMORY;
338 memcpy(dbuf.dptr + dbuf.dsize, &lock, sizeof(lock));
339 dbuf.dsize += sizeof(lock);
342 /* sort the lock list */
343 qsort(dbuf.dptr, dbuf.dsize/sizeof(lock), sizeof(lock), lock_compare);
346 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
348 SAFE_FREE(dbuf.dptr);
349 tdb_chainunlock(tdb, kbuf);
353 /* this is a nasty hack to try to simulate the lock result cache code in w2k.
354 It isn't completely accurate as I haven't yet worked out the correct
357 if (last_failed == fnum &&
358 last_failed_start == start &&
359 NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
360 status = NT_STATUS_FILE_LOCK_CONFLICT;
363 last_failed_start = start;
365 SAFE_FREE(dbuf.dptr);
366 tdb_chainunlock(tdb, kbuf);
370 /****************************************************************************
371 Unlock a range of bytes.
372 ****************************************************************************/
374 BOOL brl_unlock(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
375 uint16 smbpid, pid_t pid, uint16 tid,
376 br_off start, br_off size)
380 struct lock_struct *locks;
381 struct lock_context context;
383 kbuf = locking_key(dev,ino);
387 tdb_chainlock(tdb, kbuf);
388 dbuf = tdb_fetch(tdb, kbuf);
391 DEBUG(10,("brl_unlock: tdb_fetch failed !\n"));
395 context.smbpid = smbpid;
399 /* there are existing locks - find a match */
400 locks = (struct lock_struct *)dbuf.dptr;
401 count = dbuf.dsize / sizeof(*locks);
404 for (i=0; i<count; i++) {
405 struct lock_struct *lock = &locks[i];
407 if (lock->lock_type == WRITE_LOCK &&
408 brl_same_context(&lock->context, &context) &&
409 lock->fnum == fnum &&
410 lock->start == start &&
411 lock->size == size) {
412 /* found it - delete it */
414 tdb_delete(tdb, kbuf);
417 memmove(&locks[i], &locks[i+1],
418 sizeof(*locks)*((count-1) - i));
420 dbuf.dsize -= sizeof(*locks);
421 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
424 SAFE_FREE(dbuf.dptr);
425 tdb_chainunlock(tdb, kbuf);
431 locks = (struct lock_struct *)dbuf.dptr;
432 count = dbuf.dsize / sizeof(*locks);
433 for (i=0; i<count; i++) {
434 struct lock_struct *lock = &locks[i];
436 if (brl_same_context(&lock->context, &context) &&
437 lock->fnum == fnum &&
438 lock->start == start &&
439 lock->size == size) {
440 /* found it - delete it */
442 tdb_delete(tdb, kbuf);
445 memmove(&locks[i], &locks[i+1],
446 sizeof(*locks)*((count-1) - i));
448 dbuf.dsize -= sizeof(*locks);
449 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
452 SAFE_FREE(dbuf.dptr);
453 tdb_chainunlock(tdb, kbuf);
458 /* we didn't find it */
461 SAFE_FREE(dbuf.dptr);
462 tdb_chainunlock(tdb, kbuf);
467 /****************************************************************************
468 Test if we could add a lock if we wanted to.
469 ****************************************************************************/
471 BOOL brl_locktest(SMB_DEV_T dev, SMB_INO_T ino, int fnum,
472 uint16 smbpid, pid_t pid, uint16 tid,
473 br_off start, br_off size,
474 enum brl_type lock_type, int check_self)
478 struct lock_struct lock, *locks;
480 kbuf = locking_key(dev,ino);
484 tdb_chainlock(tdb, kbuf);
485 dbuf = tdb_fetch(tdb, kbuf);
487 lock.context.smbpid = smbpid;
488 lock.context.pid = pid;
489 lock.context.tid = tid;
493 lock.lock_type = lock_type;
496 /* there are existing locks - make sure they don't conflict */
497 locks = (struct lock_struct *)dbuf.dptr;
498 count = dbuf.dsize / sizeof(*locks);
499 for (i=0; i<count; i++) {
501 if (brl_conflict(&locks[i], &lock))
505 * Our own locks don't conflict.
507 if (brl_conflict_other(&locks[i], &lock))
513 /* no conflicts - we could have added it */
514 SAFE_FREE(dbuf.dptr);
515 tdb_chainunlock(tdb, kbuf);
519 SAFE_FREE(dbuf.dptr);
520 tdb_chainunlock(tdb, kbuf);
524 /****************************************************************************
525 Remove any locks associated with a open file.
526 ****************************************************************************/
528 void brl_close(SMB_DEV_T dev, SMB_INO_T ino, pid_t pid, int tid, int fnum)
531 int count, i, dcount=0;
532 struct lock_struct *locks;
534 kbuf = locking_key(dev,ino);
538 tdb_chainlock(tdb, kbuf);
539 dbuf = tdb_fetch(tdb, kbuf);
541 if (!dbuf.dptr) goto fail;
543 /* there are existing locks - remove any for this fnum */
544 locks = (struct lock_struct *)dbuf.dptr;
545 count = dbuf.dsize / sizeof(*locks);
546 for (i=0; i<count; i++) {
547 struct lock_struct *lock = &locks[i];
549 if (lock->context.tid == tid &&
550 lock->context.pid == pid &&
551 lock->fnum == fnum) {
552 /* found it - delete it */
553 if (count > 1 && i < count-1) {
554 memmove(&locks[i], &locks[i+1],
555 sizeof(*locks)*((count-1) - i));
564 tdb_delete(tdb, kbuf);
565 } else if (count < (dbuf.dsize / sizeof(*locks))) {
566 dbuf.dsize -= dcount * sizeof(*locks);
567 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
570 /* we didn't find it */
572 SAFE_FREE(dbuf.dptr);
573 tdb_chainunlock(tdb, kbuf);
576 /****************************************************************************
577 Traverse the whole database with this function, calling traverse_callback
579 ****************************************************************************/
581 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
583 struct lock_struct *locks;
584 struct lock_key *key;
587 BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
589 locks = (struct lock_struct *)dbuf.dptr;
590 key = (struct lock_key *)kbuf.dptr;
592 for (i=0;i<dbuf.dsize/sizeof(*locks);i++) {
593 traverse_callback(key->device, key->inode,
594 locks[i].context.pid,
602 /*******************************************************************
603 Call the specified function on each lock in the database.
604 ********************************************************************/
606 int brl_forall(BRLOCK_FN(fn))
609 return tdb_traverse(tdb, traverse_fn, (void *)fn);