Minor comment updates ...
[Samba/gebeck_regimport.git] / source3 / locking / locking.c
blobd645a2fffb384c831755586c72b25f1e30369d74
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Revision History:
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30 support.
32 rewrtten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
37 #include "includes.h"
38 uint16 global_smbpid;
40 /* the locking database handle */
41 static TDB_CONTEXT *tdb;
43 /****************************************************************************
44 Debugging aid :-).
45 ****************************************************************************/
47 static const char *lock_type_name(enum brl_type lock_type)
49 return (lock_type == READ_LOCK) ? "READ" : "WRITE";
52 /****************************************************************************
53 Utility function called to see if a file region is locked.
54 If check_self is True, then checks on our own fd with the same locking context
55 are still made. If check_self is False, then checks are not made on our own fd
56 with the same locking context are not made.
57 ****************************************************************************/
59 BOOL is_locked(files_struct *fsp,connection_struct *conn,
60 SMB_BIG_UINT count,SMB_BIG_UINT offset,
61 enum brl_type lock_type, BOOL check_self)
63 int snum = SNUM(conn);
64 BOOL ret;
66 if (count == 0)
67 return(False);
69 if (!lp_locking(snum) || !lp_strict_locking(snum))
70 return(False);
72 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
73 global_smbpid, sys_getpid(), conn->cnum,
74 offset, count, lock_type, check_self);
76 DEBUG(10,("is_locked: brl start=%.0f len=%.0f %s for file %s\n",
77 (double)offset, (double)count, ret ? "locked" : "unlocked",
78 fsp->fsp_name ));
81 * There is no lock held by an SMB daemon, check to
82 * see if there is a POSIX lock from a UNIX or NFS process.
85 if(!ret && lp_posix_locking(snum)) {
86 ret = is_posix_locked(fsp, offset, count, lock_type);
88 DEBUG(10,("is_locked: posix start=%.0f len=%.0f %s for file %s\n",
89 (double)offset, (double)count, ret ? "locked" : "unlocked",
90 fsp->fsp_name ));
93 return ret;
96 /****************************************************************************
97 Utility function called by locking requests.
98 ****************************************************************************/
100 static NTSTATUS do_lock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
101 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
103 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
105 if (!lp_locking(SNUM(conn)))
106 return NT_STATUS_OK;
108 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
110 DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
111 lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
113 if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
114 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
115 lock_pid, sys_getpid(), conn->cnum,
116 offset, count,
117 lock_type, my_lock_ctx);
119 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
122 * Try and get a POSIX lock on this range.
123 * Note that this is ok if it is a read lock
124 * overlapping on a different fd. JRA.
127 if (!set_posix_lock(fsp, offset, count, lock_type)) {
128 if (errno == EACCES || errno == EAGAIN)
129 status = NT_STATUS_FILE_LOCK_CONFLICT;
130 else
131 status = map_nt_error_from_unix(errno);
134 * We failed to map - we must now remove the brl
135 * lock entry.
137 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
138 lock_pid, sys_getpid(), conn->cnum,
139 offset, count, False,
140 NULL, NULL);
145 return status;
148 /****************************************************************************
149 Utility function called by locking requests. This is *DISGUSTING*. It also
150 appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
151 is so slow on the locking tests...... ? This is the reason. Much though I hate
152 it, we need this. JRA.
153 ****************************************************************************/
155 NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
156 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
158 int j, maxj = lp_lock_spin_count();
159 int sleeptime = lp_lock_sleep_time();
160 NTSTATUS status, ret;
162 if (maxj <= 0)
163 maxj = 1;
165 ret = NT_STATUS_OK; /* to keep dumb compilers happy */
167 for (j = 0; j < maxj; j++) {
168 status = do_lock(fsp, conn, lock_pid, count, offset, lock_type, my_lock_ctx);
169 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
170 !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
171 return status;
173 /* if we do fail then return the first error code we got */
174 if (j == 0) {
175 ret = status;
176 /* Don't spin if we blocked ourselves. */
177 if (*my_lock_ctx)
178 return ret;
180 if (sleeptime)
181 sys_usleep(sleeptime);
183 return ret;
186 /* Struct passed to brl_unlock. */
187 struct posix_unlock_data_struct {
188 files_struct *fsp;
189 SMB_BIG_UINT offset;
190 SMB_BIG_UINT count;
193 /****************************************************************************
194 Function passed to brl_unlock to allow POSIX unlock to be done first.
195 ****************************************************************************/
197 static void posix_unlock(void *pre_data)
199 struct posix_unlock_data_struct *pdata = (struct posix_unlock_data_struct *)pre_data;
201 if (lp_posix_locking(SNUM(pdata->fsp->conn)))
202 release_posix_lock(pdata->fsp, pdata->offset, pdata->count);
205 /****************************************************************************
206 Utility function called by unlocking requests.
207 ****************************************************************************/
209 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
210 SMB_BIG_UINT count,SMB_BIG_UINT offset)
212 BOOL ok = False;
213 struct posix_unlock_data_struct posix_data;
215 if (!lp_locking(SNUM(conn)))
216 return NT_STATUS_OK;
218 if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
219 return NT_STATUS_INVALID_HANDLE;
222 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
223 (double)offset, (double)count, fsp->fsp_name ));
226 * Remove the existing lock record from the tdb lockdb
227 * before looking at POSIX locks. If this record doesn't
228 * match then don't bother looking to remove POSIX locks.
231 posix_data.fsp = fsp;
232 posix_data.offset = offset;
233 posix_data.count = count;
235 ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
236 lock_pid, sys_getpid(), conn->cnum, offset, count,
237 False, posix_unlock, (void *)&posix_data);
239 if (!ok) {
240 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
241 return NT_STATUS_RANGE_NOT_LOCKED;
243 return NT_STATUS_OK;
246 /****************************************************************************
247 Remove any locks on this fd. Called from file_close().
248 ****************************************************************************/
250 void locking_close_file(files_struct *fsp)
252 pid_t pid = sys_getpid();
254 if (!lp_locking(SNUM(fsp->conn)))
255 return;
258 * Just release all the brl locks, no need to release individually.
261 brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
263 if(lp_posix_locking(SNUM(fsp->conn))) {
266 * Release all the POSIX locks.
268 posix_locking_close_file(fsp);
273 /****************************************************************************
274 Initialise the locking functions.
275 ****************************************************************************/
277 static int open_read_only;
279 BOOL locking_init(int read_only)
281 brl_init(read_only);
283 if (tdb)
284 return True;
286 tdb = tdb_open_log(lock_path("locking.tdb"),
287 0, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
288 read_only?O_RDONLY:O_RDWR|O_CREAT,
289 0644);
291 if (!tdb) {
292 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
293 return False;
296 if (!posix_locking_init(read_only))
297 return False;
299 open_read_only = read_only;
301 return True;
304 /*******************************************************************
305 Deinitialize the share_mode management.
306 ******************************************************************/
308 BOOL locking_end(void)
311 brl_shutdown(open_read_only);
312 if (tdb) {
314 if (tdb_close(tdb) != 0)
315 return False;
318 return True;
321 /*******************************************************************
322 Form a static locking key for a dev/inode pair.
323 ******************************************************************/
325 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
327 static struct locking_key key;
328 TDB_DATA kbuf;
330 memset(&key, '\0', sizeof(key));
331 key.dev = dev;
332 key.inode = inode;
333 kbuf.dptr = (char *)&key;
334 kbuf.dsize = sizeof(key);
335 return kbuf;
338 static TDB_DATA locking_key_fsp(files_struct *fsp)
340 return locking_key(fsp->dev, fsp->inode);
343 /*******************************************************************
344 Lock a hash bucket entry.
345 ******************************************************************/
347 BOOL lock_share_entry(connection_struct *conn,
348 SMB_DEV_T dev, SMB_INO_T inode)
350 return tdb_chainlock(tdb, locking_key(dev, inode)) == 0;
353 /*******************************************************************
354 Unlock a hash bucket entry.
355 ******************************************************************/
357 void unlock_share_entry(connection_struct *conn,
358 SMB_DEV_T dev, SMB_INO_T inode)
360 tdb_chainunlock(tdb, locking_key(dev, inode));
363 /*******************************************************************
364 Lock a hash bucket entry. Use an fsp for convenience.
365 ******************************************************************/
367 BOOL lock_share_entry_fsp(files_struct *fsp)
369 return tdb_chainlock(tdb, locking_key(fsp->dev, fsp->inode)) == 0;
372 /*******************************************************************
373 Unlock a hash bucket entry. Use an fsp for convenience.
374 ******************************************************************/
376 void unlock_share_entry_fsp(files_struct *fsp)
378 tdb_chainunlock(tdb, locking_key(fsp->dev, fsp->inode));
381 /*******************************************************************
382 Print out a share mode.
383 ********************************************************************/
385 char *share_mode_str(int num, share_mode_entry *e)
387 static pstring share_str;
389 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: \
390 pid = %lu, share_mode = 0x%x, desired_access = 0x%x, port = 0x%x, type= 0x%x, file_id = %lu, dev = 0x%x, inode = %.0f",
391 num, (unsigned long)e->pid, e->share_mode, (unsigned int)e->desired_access, e->op_port, e->op_type, e->share_file_id,
392 (unsigned int)e->dev, (double)e->inode );
394 return share_str;
397 /*******************************************************************
398 Print out a share mode table.
399 ********************************************************************/
401 static void print_share_mode_table(struct locking_data *data)
403 int num_share_modes = data->u.num_share_mode_entries;
404 share_mode_entry *shares = (share_mode_entry *)(data + 1);
405 int i;
407 for (i = 0; i < num_share_modes; i++) {
408 share_mode_entry *entry_p = &shares[i];
409 DEBUG(10,("print_share_mode_table: %s\n", share_mode_str(i, entry_p) ));
413 /*******************************************************************
414 Get all share mode entries for a dev/inode pair.
415 ********************************************************************/
417 int get_share_modes(connection_struct *conn,
418 SMB_DEV_T dev, SMB_INO_T inode,
419 share_mode_entry **pp_shares)
421 TDB_DATA dbuf;
422 struct locking_data *data;
423 int num_share_modes;
424 share_mode_entry *shares = NULL;
425 TDB_DATA key = locking_key(dev, inode);
426 *pp_shares = NULL;
428 dbuf = tdb_fetch(tdb, key);
429 if (!dbuf.dptr)
430 return 0;
432 data = (struct locking_data *)dbuf.dptr;
433 num_share_modes = data->u.num_share_mode_entries;
434 if(num_share_modes) {
435 int i;
436 int del_count = 0;
438 shares = (share_mode_entry *)memdup(dbuf.dptr + sizeof(*data),
439 num_share_modes * sizeof(share_mode_entry));
441 if (!shares) {
442 SAFE_FREE(dbuf.dptr);
443 return 0;
447 * Ensure that each entry has a real process attached.
450 for (i = 0; i < num_share_modes; ) {
451 share_mode_entry *entry_p = &shares[i];
452 if (process_exists(entry_p->pid)) {
453 DEBUG(10,("get_share_modes: %s\n", share_mode_str(i, entry_p) ));
454 i++;
455 } else {
456 DEBUG(10,("get_share_modes: deleted %s\n", share_mode_str(i, entry_p) ));
457 memcpy( &shares[i], &shares[i+1],
458 sizeof(share_mode_entry) * (num_share_modes - i - 1));
459 num_share_modes--;
460 del_count++;
464 /* Did we delete any ? If so, re-store in tdb. */
465 if (del_count) {
466 data->u.num_share_mode_entries = num_share_modes;
468 if (num_share_modes)
469 memcpy(dbuf.dptr + sizeof(*data), shares,
470 num_share_modes * sizeof(share_mode_entry));
472 /* The record has shrunk a bit */
473 dbuf.dsize -= del_count * sizeof(share_mode_entry);
475 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
476 SAFE_FREE(shares);
477 SAFE_FREE(dbuf.dptr);
478 return 0;
483 SAFE_FREE(dbuf.dptr);
484 *pp_shares = shares;
485 return num_share_modes;
488 /*******************************************************************
489 Fill a share mode entry.
490 ********************************************************************/
492 static void fill_share_mode(char *p, files_struct *fsp, uint16 port, uint16 op_type)
494 share_mode_entry *e = (share_mode_entry *)p;
495 void *x = &e->time; /* Needed to force alignment. p may not be aligned.... */
497 memset(e, '\0', sizeof(share_mode_entry));
498 e->pid = sys_getpid();
499 e->share_mode = fsp->share_mode;
500 e->desired_access = fsp->desired_access;
501 e->op_port = port;
502 e->op_type = op_type;
503 memcpy(x, &fsp->open_time, sizeof(struct timeval));
504 e->share_file_id = fsp->file_id;
505 e->dev = fsp->dev;
506 e->inode = fsp->inode;
509 /*******************************************************************
510 Check if two share mode entries are identical, ignoring oplock
511 and port info and desired_access.
512 ********************************************************************/
514 BOOL share_modes_identical( share_mode_entry *e1, share_mode_entry *e2)
516 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
517 if (e1->pid == e2->pid &&
518 e1->share_file_id == e2->share_file_id &&
519 e1->dev == e2->dev &&
520 e1->inode == e2->inode &&
521 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) != (e2->share_mode & ~DELETE_ON_CLOSE_FLAG)) {
522 DEBUG(0,("PANIC: share_modes_identical: share_mode missmatch (e1 = %u, e2 = %u). Logic error.\n",
523 (unsigned int)(e1->share_mode & ~DELETE_ON_CLOSE_FLAG),
524 (unsigned int)(e2->share_mode & ~DELETE_ON_CLOSE_FLAG) ));
525 smb_panic("PANIC: share_modes_identical logic error.\n");
527 #endif
529 return (e1->pid == e2->pid &&
530 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) == (e2->share_mode & ~DELETE_ON_CLOSE_FLAG) &&
531 e1->dev == e2->dev &&
532 e1->inode == e2->inode &&
533 e1->share_file_id == e2->share_file_id );
536 /*******************************************************************
537 Delete a specific share mode. Return the number
538 of entries left, and a memdup'ed copy of the entry deleted (if required).
539 Ignore if no entry deleted.
540 ********************************************************************/
542 ssize_t del_share_entry( SMB_DEV_T dev, SMB_INO_T inode,
543 share_mode_entry *entry, share_mode_entry **ppse)
545 TDB_DATA dbuf;
546 struct locking_data *data;
547 int i, del_count=0;
548 share_mode_entry *shares;
549 ssize_t count = 0;
550 TDB_DATA key = locking_key(dev, inode);
552 if (ppse)
553 *ppse = NULL;
555 /* read in the existing share modes */
556 dbuf = tdb_fetch(tdb, key);
557 if (!dbuf.dptr)
558 return -1;
560 data = (struct locking_data *)dbuf.dptr;
561 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
564 * Find any with this pid and delete it
565 * by overwriting with the rest of the data
566 * from the record.
569 DEBUG(10,("del_share_entry: num_share_modes = %d\n", data->u.num_share_mode_entries ));
571 for (i=0;i<data->u.num_share_mode_entries;) {
572 if (share_modes_identical(&shares[i], entry)) {
573 DEBUG(10,("del_share_entry: deleted %s\n",
574 share_mode_str(i, &shares[i]) ));
575 if (ppse)
576 *ppse = memdup(&shares[i], sizeof(*shares));
577 data->u.num_share_mode_entries--;
578 memmove(&shares[i], &shares[i+1],
579 dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*shares)));
580 del_count++;
582 DEBUG(10,("del_share_entry: deleting entry %d\n", i ));
584 } else {
585 i++;
589 if (del_count) {
590 /* the record may have shrunk a bit */
591 dbuf.dsize -= del_count * sizeof(*shares);
593 count = (ssize_t)data->u.num_share_mode_entries;
595 /* store it back in the database */
596 if (data->u.num_share_mode_entries == 0) {
597 if (tdb_delete(tdb, key) == -1)
598 count = -1;
599 } else {
600 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
601 count = -1;
604 DEBUG(10,("del_share_entry: Remaining table.\n"));
605 print_share_mode_table((struct locking_data *)dbuf.dptr);
606 SAFE_FREE(dbuf.dptr);
607 return count;
610 /*******************************************************************
611 Del the share mode of a file for this process. Return the number
612 of entries left, and a memdup'ed copy of the entry deleted.
613 ********************************************************************/
615 ssize_t del_share_mode(files_struct *fsp, share_mode_entry **ppse)
617 share_mode_entry entry;
620 * Fake up a share_mode_entry for comparisons.
623 fill_share_mode((char *)&entry, fsp, 0, 0);
624 return del_share_entry(fsp->dev, fsp->inode, &entry, ppse);
627 /*******************************************************************
628 Set the share mode of a file. Return False on fail, True on success.
629 ********************************************************************/
631 BOOL set_share_mode(files_struct *fsp, uint16 port, uint16 op_type)
633 TDB_DATA dbuf;
634 struct locking_data *data;
635 char *p=NULL;
636 int size;
637 TDB_DATA key = locking_key_fsp(fsp);
638 BOOL ret = True;
640 /* read in the existing share modes if any */
641 dbuf = tdb_fetch(tdb, key);
642 if (!dbuf.dptr) {
643 size_t offset;
644 /* we'll need to create a new record */
645 pstring fname;
647 pstrcpy(fname, fsp->conn->connectpath);
648 pstrcat(fname, "/");
649 pstrcat(fname, fsp->fsp_name);
651 size = sizeof(*data) + sizeof(share_mode_entry) + strlen(fname) + 1;
652 p = (char *)malloc(size);
653 if (!p)
654 return False;
655 data = (struct locking_data *)p;
656 data->u.num_share_mode_entries = 1;
658 DEBUG(10,("set_share_mode: creating entry for file %s. num_share_modes = 1\n",
659 fsp->fsp_name ));
661 offset = sizeof(*data) + sizeof(share_mode_entry);
662 safe_strcpy(p + offset, fname, size - offset - 1);
663 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
664 dbuf.dptr = p;
665 dbuf.dsize = size;
666 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
667 ret = False;
669 print_share_mode_table((struct locking_data *)p);
671 SAFE_FREE(p);
672 return ret;
675 /* we're adding to an existing entry - this is a bit fiddly */
676 data = (struct locking_data *)dbuf.dptr;
678 data->u.num_share_mode_entries++;
680 DEBUG(10,("set_share_mode: adding entry for file %s. new num_share_modes = %d\n",
681 fsp->fsp_name, data->u.num_share_mode_entries ));
683 size = dbuf.dsize + sizeof(share_mode_entry);
684 p = malloc(size);
685 if (!p) {
686 SAFE_FREE(dbuf.dptr);
687 return False;
689 memcpy(p, dbuf.dptr, sizeof(*data));
690 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
691 memcpy(p + sizeof(*data) + sizeof(share_mode_entry), dbuf.dptr + sizeof(*data),
692 dbuf.dsize - sizeof(*data));
693 SAFE_FREE(dbuf.dptr);
694 dbuf.dptr = p;
695 dbuf.dsize = size;
696 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
697 ret = False;
698 print_share_mode_table((struct locking_data *)p);
699 SAFE_FREE(p);
700 return ret;
703 /*******************************************************************
704 A generic in-place modification call for share mode entries.
705 ********************************************************************/
707 static BOOL mod_share_mode( SMB_DEV_T dev, SMB_INO_T inode, share_mode_entry *entry,
708 void (*mod_fn)(share_mode_entry *, SMB_DEV_T, SMB_INO_T, void *),
709 void *param)
711 TDB_DATA dbuf;
712 struct locking_data *data;
713 int i;
714 share_mode_entry *shares;
715 BOOL need_store=False;
716 BOOL ret = True;
717 TDB_DATA key = locking_key(dev, inode);
719 /* read in the existing share modes */
720 dbuf = tdb_fetch(tdb, key);
721 if (!dbuf.dptr)
722 return False;
724 data = (struct locking_data *)dbuf.dptr;
725 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
727 /* find any with our pid and call the supplied function */
728 for (i=0;i<data->u.num_share_mode_entries;i++) {
729 if (share_modes_identical(entry, &shares[i])) {
730 mod_fn(&shares[i], dev, inode, param);
731 need_store=True;
735 /* if the mod fn was called then store it back */
736 if (need_store) {
737 if (data->u.num_share_mode_entries == 0) {
738 if (tdb_delete(tdb, key) == -1)
739 ret = False;
740 } else {
741 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
742 ret = False;
746 SAFE_FREE(dbuf.dptr);
747 return ret;
750 /*******************************************************************
751 Static function that actually does the work for the generic function
752 below.
753 ********************************************************************/
755 static void remove_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode,
756 void *param)
758 DEBUG(10,("remove_share_oplock_fn: removing oplock info for entry dev=%x ino=%.0f\n",
759 (unsigned int)dev, (double)inode ));
760 /* Delete the oplock info. */
761 entry->op_port = 0;
762 entry->op_type = NO_OPLOCK;
765 /*******************************************************************
766 Remove an oplock port and mode entry from a share mode.
767 ********************************************************************/
769 BOOL remove_share_oplock(files_struct *fsp)
771 share_mode_entry entry;
773 * Fake up an entry for comparisons...
775 fill_share_mode((char *)&entry, fsp, 0, 0);
776 return mod_share_mode(fsp->dev, fsp->inode, &entry, remove_share_oplock_fn, NULL);
779 /*******************************************************************
780 Static function that actually does the work for the generic function
781 below.
782 ********************************************************************/
784 static void downgrade_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode,
785 void *param)
787 DEBUG(10,("downgrade_share_oplock_fn: downgrading oplock info for entry dev=%x ino=%.0f\n",
788 (unsigned int)dev, (double)inode ));
789 entry->op_type = LEVEL_II_OPLOCK;
792 /*******************************************************************
793 Downgrade a oplock type from exclusive to level II.
794 ********************************************************************/
796 BOOL downgrade_share_oplock(files_struct *fsp)
798 share_mode_entry entry;
800 * Fake up an entry for comparisons...
802 fill_share_mode((char *)&entry, fsp, 0, 0);
803 return mod_share_mode(fsp->dev, fsp->inode, &entry, downgrade_share_oplock_fn, NULL);
806 /*******************************************************************
807 Get/Set the delete on close flag in a set of share modes.
808 Return False on fail, True on success.
809 ********************************************************************/
811 BOOL modify_delete_flag( SMB_DEV_T dev, SMB_INO_T inode, BOOL delete_on_close)
813 TDB_DATA dbuf;
814 struct locking_data *data;
815 int i;
816 share_mode_entry *shares;
817 TDB_DATA key = locking_key(dev, inode);
819 /* read in the existing share modes */
820 dbuf = tdb_fetch(tdb, key);
821 if (!dbuf.dptr)
822 return False;
824 data = (struct locking_data *)dbuf.dptr;
825 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
827 /* Set/Unset the delete on close element. */
828 for (i=0;i<data->u.num_share_mode_entries;i++,shares++) {
829 shares->share_mode = (delete_on_close ?
830 (shares->share_mode | DELETE_ON_CLOSE_FLAG) :
831 (shares->share_mode & ~DELETE_ON_CLOSE_FLAG) );
834 /* store it back */
835 if (data->u.num_share_mode_entries) {
836 if (tdb_store(tdb, key, dbuf, TDB_REPLACE)==-1) {
837 SAFE_FREE(dbuf.dptr);
838 return False;
842 SAFE_FREE(dbuf.dptr);
843 return True;
846 /****************************************************************************
847 Traverse the whole database with this function, calling traverse_callback
848 on each share mode
849 ****************************************************************************/
851 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
852 void* state)
854 struct locking_data *data;
855 share_mode_entry *shares;
856 char *name;
857 int i;
859 SHAREMODE_FN(traverse_callback) = (SHAREMODE_FN_CAST())state;
861 data = (struct locking_data *)dbuf.dptr;
862 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
863 name = dbuf.dptr + sizeof(*data) + data->u.num_share_mode_entries*sizeof(*shares);
865 for (i=0;i<data->u.num_share_mode_entries;i++) {
866 traverse_callback(&shares[i], name);
868 return 0;
871 /*******************************************************************
872 Call the specified function on each entry under management by the
873 share mode system.
874 ********************************************************************/
876 int share_mode_forall(SHAREMODE_FN(fn))
878 if (!tdb)
879 return 0;
880 return tdb_traverse(tdb, traverse_fn, (void*)fn);