2 Unix SMB/CIFS implementation.
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.
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
32 rewrtten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
40 /* the locking database handle */
41 static TDB_CONTEXT *tdb;
45 int num_share_mode_entries;
46 share_mode_entry dummy; /* Needed for alignment. */
48 /* the following two entries are implicit
49 share_mode_entry modes[num_share_mode_entries];
54 /****************************************************************************
56 ****************************************************************************/
58 static const char *lock_type_name(enum brl_type lock_type)
60 return (lock_type == READ_LOCK) ? "READ" : "WRITE";
63 /****************************************************************************
64 Utility function called to see if a file region is locked.
65 ****************************************************************************/
67 BOOL is_locked(files_struct *fsp,connection_struct *conn,
68 SMB_BIG_UINT count,SMB_BIG_UINT offset,
69 enum brl_type lock_type)
71 int snum = SNUM(conn);
72 int strict_locking = lp_strict_locking(snum);
78 if (!lp_locking(snum) || !strict_locking)
81 if (strict_locking == Auto) {
82 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
83 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
85 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK)) {
86 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
89 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
90 global_smbpid, sys_getpid(), conn->cnum,
91 offset, count, lock_type);
94 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
95 global_smbpid, sys_getpid(), conn->cnum,
96 offset, count, lock_type);
99 DEBUG(10,("is_locked: brl start=%.0f len=%.0f %s for file %s\n",
100 (double)offset, (double)count, ret ? "locked" : "unlocked",
104 * There is no lock held by an SMB daemon, check to
105 * see if there is a POSIX lock from a UNIX or NFS process.
108 if(!ret && lp_posix_locking(snum)) {
109 ret = is_posix_locked(fsp, offset, count, lock_type);
111 DEBUG(10,("is_locked: posix start=%.0f len=%.0f %s for file %s\n",
112 (double)offset, (double)count, ret ? "locked" : "unlocked",
119 /****************************************************************************
120 Utility function called by locking requests.
121 ****************************************************************************/
123 static NTSTATUS do_lock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
124 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
126 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
128 if (!lp_locking(SNUM(conn)))
131 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
133 DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
134 lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
136 if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
137 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
138 lock_pid, sys_getpid(), conn->cnum,
140 lock_type, my_lock_ctx);
142 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
145 * Try and get a POSIX lock on this range.
146 * Note that this is ok if it is a read lock
147 * overlapping on a different fd. JRA.
150 if (!set_posix_lock(fsp, offset, count, lock_type)) {
151 if (errno == EACCES || errno == EAGAIN)
152 status = NT_STATUS_FILE_LOCK_CONFLICT;
154 status = map_nt_error_from_unix(errno);
157 * We failed to map - we must now remove the brl
160 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
161 lock_pid, sys_getpid(), conn->cnum,
162 offset, count, False,
171 /****************************************************************************
172 Utility function called by locking requests. This is *DISGUSTING*. It also
173 appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
174 is so slow on the locking tests...... ? This is the reason. Much though I hate
175 it, we need this. JRA.
176 ****************************************************************************/
178 NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
179 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
181 int j, maxj = lp_lock_spin_count();
182 int sleeptime = lp_lock_sleep_time();
183 NTSTATUS status, ret;
188 ret = NT_STATUS_OK; /* to keep dumb compilers happy */
190 for (j = 0; j < maxj; j++) {
191 status = do_lock(fsp, conn, lock_pid, count, offset, lock_type, my_lock_ctx);
192 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
193 !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
196 /* if we do fail then return the first error code we got */
199 /* Don't spin if we blocked ourselves. */
204 sys_usleep(sleeptime);
209 /* Struct passed to brl_unlock. */
210 struct posix_unlock_data_struct {
216 /****************************************************************************
217 Function passed to brl_unlock to allow POSIX unlock to be done first.
218 ****************************************************************************/
220 static void posix_unlock(void *pre_data)
222 struct posix_unlock_data_struct *pdata = (struct posix_unlock_data_struct *)pre_data;
224 if (lp_posix_locking(SNUM(pdata->fsp->conn)))
225 release_posix_lock(pdata->fsp, pdata->offset, pdata->count);
228 /****************************************************************************
229 Utility function called by unlocking requests.
230 ****************************************************************************/
232 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
233 SMB_BIG_UINT count,SMB_BIG_UINT offset)
236 struct posix_unlock_data_struct posix_data;
238 if (!lp_locking(SNUM(conn)))
241 if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
242 return NT_STATUS_INVALID_HANDLE;
245 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
246 (double)offset, (double)count, fsp->fsp_name ));
249 * Remove the existing lock record from the tdb lockdb
250 * before looking at POSIX locks. If this record doesn't
251 * match then don't bother looking to remove POSIX locks.
254 posix_data.fsp = fsp;
255 posix_data.offset = offset;
256 posix_data.count = count;
258 ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
259 lock_pid, sys_getpid(), conn->cnum, offset, count,
260 False, posix_unlock, (void *)&posix_data);
263 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
264 return NT_STATUS_RANGE_NOT_LOCKED;
269 /****************************************************************************
270 Remove any locks on this fd. Called from file_close().
271 ****************************************************************************/
273 void locking_close_file(files_struct *fsp)
275 pid_t pid = sys_getpid();
277 if (!lp_locking(SNUM(fsp->conn)))
281 * Just release all the brl locks, no need to release individually.
284 brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
286 if(lp_posix_locking(SNUM(fsp->conn))) {
289 * Release all the POSIX locks.
291 posix_locking_close_file(fsp);
296 /****************************************************************************
297 Initialise the locking functions.
298 ****************************************************************************/
300 static int open_read_only;
302 BOOL locking_init(int read_only)
309 tdb = tdb_open_log(lock_path("locking.tdb"),
310 0, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
311 read_only?O_RDONLY:O_RDWR|O_CREAT,
315 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
319 if (!posix_locking_init(read_only))
322 open_read_only = read_only;
327 /*******************************************************************
328 Deinitialize the share_mode management.
329 ******************************************************************/
331 BOOL locking_end(void)
334 brl_shutdown(open_read_only);
337 if (tdb_close(tdb) != 0)
344 /*******************************************************************
345 Form a static locking key for a dev/inode pair.
346 ******************************************************************/
348 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
350 static struct locking_key key;
353 memset(&key, '\0', sizeof(key));
356 kbuf.dptr = (char *)&key;
357 kbuf.dsize = sizeof(key);
361 static TDB_DATA locking_key_fsp(files_struct *fsp)
363 return locking_key(fsp->dev, fsp->inode);
366 /*******************************************************************
367 Lock a hash bucket entry.
368 ******************************************************************/
370 BOOL lock_share_entry(connection_struct *conn,
371 SMB_DEV_T dev, SMB_INO_T inode)
373 return tdb_chainlock(tdb, locking_key(dev, inode)) == 0;
376 /*******************************************************************
377 Unlock a hash bucket entry.
378 ******************************************************************/
380 void unlock_share_entry(connection_struct *conn,
381 SMB_DEV_T dev, SMB_INO_T inode)
383 tdb_chainunlock(tdb, locking_key(dev, inode));
386 /*******************************************************************
387 Lock a hash bucket entry. use a fsp for convenience
388 ******************************************************************/
390 BOOL lock_share_entry_fsp(files_struct *fsp)
392 return tdb_chainlock(tdb, locking_key(fsp->dev, fsp->inode)) == 0;
395 /*******************************************************************
396 Unlock a hash bucket entry.
397 ******************************************************************/
399 void unlock_share_entry_fsp(files_struct *fsp)
401 tdb_chainunlock(tdb, locking_key(fsp->dev, fsp->inode));
404 /*******************************************************************
405 Print out a share mode.
406 ********************************************************************/
408 char *share_mode_str(int num, share_mode_entry *e)
410 static pstring share_str;
412 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: \
413 pid = %lu, share_mode = 0x%x, desired_access = 0x%x, port = 0x%x, type= 0x%x, file_id = %lu, dev = 0x%x, inode = %.0f",
414 num, (unsigned long)e->pid, e->share_mode, (unsigned int)e->desired_access, e->op_port, e->op_type, e->share_file_id,
415 (unsigned int)e->dev, (double)e->inode );
420 /*******************************************************************
421 Print out a share mode table.
422 ********************************************************************/
424 static void print_share_mode_table(struct locking_data *data)
426 int num_share_modes = data->u.num_share_mode_entries;
427 share_mode_entry *shares = (share_mode_entry *)(data + 1);
430 for (i = 0; i < num_share_modes; i++) {
431 share_mode_entry *entry_p = &shares[i];
432 DEBUG(10,("print_share_mode_table: %s\n", share_mode_str(i, entry_p) ));
436 /*******************************************************************
437 Get all share mode entries for a dev/inode pair.
438 ********************************************************************/
440 int get_share_modes(connection_struct *conn,
441 SMB_DEV_T dev, SMB_INO_T inode,
442 share_mode_entry **pp_shares)
445 struct locking_data *data;
447 share_mode_entry *shares = NULL;
448 TDB_DATA key = locking_key(dev, inode);
451 dbuf = tdb_fetch(tdb, key);
455 data = (struct locking_data *)dbuf.dptr;
456 num_share_modes = data->u.num_share_mode_entries;
457 if(num_share_modes) {
462 shares = (share_mode_entry *)memdup(dbuf.dptr + sizeof(*data),
463 num_share_modes * sizeof(share_mode_entry));
466 SAFE_FREE(dbuf.dptr);
470 /* Save off the associated filename. */
471 pstrcpy(fname, dbuf.dptr + sizeof(*data) + num_share_modes * sizeof(share_mode_entry));
474 * Ensure that each entry has a real process attached.
477 for (i = 0; i < num_share_modes; ) {
478 share_mode_entry *entry_p = &shares[i];
479 if (process_exists(entry_p->pid)) {
480 DEBUG(10,("get_share_modes: %s\n", share_mode_str(i, entry_p) ));
483 DEBUG(10,("get_share_modes: deleted %s\n", share_mode_str(i, entry_p) ));
484 if (num_share_modes - i - 1 > 0) {
485 memcpy( &shares[i], &shares[i+1],
486 sizeof(share_mode_entry) * (num_share_modes - i - 1));
493 /* Did we delete any ? If so, re-store in tdb. */
495 data->u.num_share_mode_entries = num_share_modes;
497 if (num_share_modes) {
498 memcpy(dbuf.dptr + sizeof(*data), shares,
499 num_share_modes * sizeof(share_mode_entry));
500 /* Append the filename. */
501 pstrcpy(dbuf.dptr + sizeof(*data) + num_share_modes * sizeof(share_mode_entry), fname);
504 /* The record has shrunk a bit */
505 dbuf.dsize -= del_count * sizeof(share_mode_entry);
507 if (data->u.num_share_mode_entries == 0) {
508 if (tdb_delete(tdb, key) == -1) {
510 SAFE_FREE(dbuf.dptr);
514 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
516 SAFE_FREE(dbuf.dptr);
523 SAFE_FREE(dbuf.dptr);
525 return num_share_modes;
528 /*******************************************************************
529 Fill a share mode entry.
530 ********************************************************************/
532 static void fill_share_mode(char *p, files_struct *fsp, uint16 port, uint16 op_type)
534 share_mode_entry *e = (share_mode_entry *)p;
535 void *x = &e->time; /* Needed to force alignment. p may not be aligned.... */
537 memset(e, '\0', sizeof(share_mode_entry));
538 e->pid = sys_getpid();
539 e->share_mode = fsp->share_mode;
540 e->desired_access = fsp->desired_access;
542 e->op_type = op_type;
543 memcpy(x, &fsp->open_time, sizeof(struct timeval));
544 e->share_file_id = fsp->file_id;
546 e->inode = fsp->inode;
549 /*******************************************************************
550 Check if two share mode entries are identical, ignoring oplock
551 and port info and desired_access.
552 ********************************************************************/
554 BOOL share_modes_identical( share_mode_entry *e1, share_mode_entry *e2)
556 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
557 if (e1->pid == e2->pid &&
558 e1->share_file_id == e2->share_file_id &&
559 e1->dev == e2->dev &&
560 e1->inode == e2->inode &&
561 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) != (e2->share_mode & ~DELETE_ON_CLOSE_FLAG)) {
562 DEBUG(0,("PANIC: share_modes_identical: share_mode missmatch (e1 = %u, e2 = %u). Logic error.\n",
563 (unsigned int)(e1->share_mode & ~DELETE_ON_CLOSE_FLAG),
564 (unsigned int)(e2->share_mode & ~DELETE_ON_CLOSE_FLAG) ));
565 smb_panic("PANIC: share_modes_identical logic error.\n");
569 return (e1->pid == e2->pid &&
570 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) == (e2->share_mode & ~DELETE_ON_CLOSE_FLAG) &&
571 e1->dev == e2->dev &&
572 e1->inode == e2->inode &&
573 e1->share_file_id == e2->share_file_id );
576 /*******************************************************************
577 Delete a specific share mode. Return the number
578 of entries left, and a memdup'ed copy of the entry deleted (if required).
579 Ignore if no entry deleted.
580 ********************************************************************/
582 ssize_t del_share_entry( SMB_DEV_T dev, SMB_INO_T inode,
583 share_mode_entry *entry, share_mode_entry **ppse)
586 struct locking_data *data;
588 share_mode_entry *shares;
590 TDB_DATA key = locking_key(dev, inode);
595 /* read in the existing share modes */
596 dbuf = tdb_fetch(tdb, key);
600 data = (struct locking_data *)dbuf.dptr;
601 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
604 * Find any with this pid and delete it
605 * by overwriting with the rest of the data
609 DEBUG(10,("del_share_entry: num_share_modes = %d\n", data->u.num_share_mode_entries ));
611 for (i=0;i<data->u.num_share_mode_entries;) {
612 if (share_modes_identical(&shares[i], entry)) {
613 DEBUG(10,("del_share_entry: deleted %s\n",
614 share_mode_str(i, &shares[i]) ));
616 *ppse = memdup(&shares[i], sizeof(*shares));
617 data->u.num_share_mode_entries--;
618 if ((dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*shares))) > 0) {
619 memmove(&shares[i], &shares[i+1],
620 dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*shares)));
624 DEBUG(10,("del_share_entry: deleting entry %d\n", i ));
632 /* the record may have shrunk a bit */
633 dbuf.dsize -= del_count * sizeof(*shares);
635 count = (ssize_t)data->u.num_share_mode_entries;
637 /* store it back in the database */
638 if (data->u.num_share_mode_entries == 0) {
639 if (tdb_delete(tdb, key) == -1)
642 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
646 DEBUG(10,("del_share_entry: Remaining table.\n"));
647 print_share_mode_table((struct locking_data *)dbuf.dptr);
648 SAFE_FREE(dbuf.dptr);
652 /*******************************************************************
653 Del the share mode of a file for this process. Return the number
654 of entries left, and a memdup'ed copy of the entry deleted.
655 ********************************************************************/
657 ssize_t del_share_mode(files_struct *fsp, share_mode_entry **ppse)
659 share_mode_entry entry;
662 * Fake up a share_mode_entry for comparisons.
665 fill_share_mode((char *)&entry, fsp, 0, 0);
666 return del_share_entry(fsp->dev, fsp->inode, &entry, ppse);
669 /*******************************************************************
670 Set the share mode of a file. Return False on fail, True on success.
671 ********************************************************************/
673 BOOL set_share_mode(files_struct *fsp, uint16 port, uint16 op_type)
676 struct locking_data *data;
679 TDB_DATA key = locking_key_fsp(fsp);
682 /* read in the existing share modes if any */
683 dbuf = tdb_fetch(tdb, key);
686 /* we'll need to create a new record */
689 pstrcpy(fname, fsp->conn->connectpath);
691 pstrcat(fname, fsp->fsp_name);
693 size = sizeof(*data) + sizeof(share_mode_entry) + strlen(fname) + 1;
694 p = (char *)SMB_MALLOC(size);
697 data = (struct locking_data *)p;
698 data->u.num_share_mode_entries = 1;
700 DEBUG(10,("set_share_mode: creating entry for file %s. num_share_modes = 1\n",
703 offset = sizeof(*data) + sizeof(share_mode_entry);
704 safe_strcpy(p + offset, fname, size - offset - 1);
705 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
708 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
711 print_share_mode_table((struct locking_data *)p);
717 /* we're adding to an existing entry - this is a bit fiddly */
718 data = (struct locking_data *)dbuf.dptr;
720 data->u.num_share_mode_entries++;
722 DEBUG(10,("set_share_mode: adding entry for file %s. new num_share_modes = %d\n",
723 fsp->fsp_name, data->u.num_share_mode_entries ));
725 size = dbuf.dsize + sizeof(share_mode_entry);
726 p = SMB_MALLOC(size);
728 SAFE_FREE(dbuf.dptr);
731 memcpy(p, dbuf.dptr, sizeof(*data));
732 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
733 memcpy(p + sizeof(*data) + sizeof(share_mode_entry), dbuf.dptr + sizeof(*data),
734 dbuf.dsize - sizeof(*data));
735 SAFE_FREE(dbuf.dptr);
738 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
740 print_share_mode_table((struct locking_data *)p);
745 /*******************************************************************
746 A generic in-place modification call for share mode entries.
747 ********************************************************************/
749 static BOOL mod_share_mode( SMB_DEV_T dev, SMB_INO_T inode, share_mode_entry *entry,
750 void (*mod_fn)(share_mode_entry *, SMB_DEV_T, SMB_INO_T, void *),
754 struct locking_data *data;
756 share_mode_entry *shares;
757 BOOL need_store=False;
759 TDB_DATA key = locking_key(dev, inode);
761 /* read in the existing share modes */
762 dbuf = tdb_fetch(tdb, key);
766 data = (struct locking_data *)dbuf.dptr;
767 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
769 /* find any with our pid and call the supplied function */
770 for (i=0;i<data->u.num_share_mode_entries;i++) {
771 if (share_modes_identical(entry, &shares[i])) {
772 mod_fn(&shares[i], dev, inode, param);
777 /* if the mod fn was called then store it back */
779 if (data->u.num_share_mode_entries == 0) {
780 if (tdb_delete(tdb, key) == -1)
783 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
788 SAFE_FREE(dbuf.dptr);
792 /*******************************************************************
793 Static function that actually does the work for the generic function
795 ********************************************************************/
797 static void remove_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode,
800 DEBUG(10,("remove_share_oplock_fn: removing oplock info for entry dev=%x ino=%.0f\n",
801 (unsigned int)dev, (double)inode ));
802 /* Delete the oplock info. */
804 entry->op_type = NO_OPLOCK;
807 /*******************************************************************
808 Remove an oplock port and mode entry from a share mode.
809 ********************************************************************/
811 BOOL remove_share_oplock(files_struct *fsp)
813 share_mode_entry entry;
815 * Fake up an entry for comparisons...
817 fill_share_mode((char *)&entry, fsp, 0, 0);
818 return mod_share_mode(fsp->dev, fsp->inode, &entry, remove_share_oplock_fn, NULL);
821 /*******************************************************************
822 Static function that actually does the work for the generic function
824 ********************************************************************/
826 static void downgrade_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode,
829 DEBUG(10,("downgrade_share_oplock_fn: downgrading oplock info for entry dev=%x ino=%.0f\n",
830 (unsigned int)dev, (double)inode ));
831 entry->op_type = LEVEL_II_OPLOCK;
834 /*******************************************************************
835 Downgrade a oplock type from exclusive to level II.
836 ********************************************************************/
838 BOOL downgrade_share_oplock(files_struct *fsp)
840 share_mode_entry entry;
842 * Fake up an entry for comparisons...
844 fill_share_mode((char *)&entry, fsp, 0, 0);
845 return mod_share_mode(fsp->dev, fsp->inode, &entry, downgrade_share_oplock_fn, NULL);
848 /*******************************************************************
849 Get/Set the delete on close flag in a set of share modes.
850 Return False on fail, True on success.
851 ********************************************************************/
853 BOOL modify_delete_flag( SMB_DEV_T dev, SMB_INO_T inode, BOOL delete_on_close)
856 struct locking_data *data;
858 share_mode_entry *shares;
859 TDB_DATA key = locking_key(dev, inode);
861 /* read in the existing share modes */
862 dbuf = tdb_fetch(tdb, key);
866 data = (struct locking_data *)dbuf.dptr;
867 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
869 /* Set/Unset the delete on close element. */
870 for (i=0;i<data->u.num_share_mode_entries;i++,shares++) {
871 shares->share_mode = (delete_on_close ?
872 (shares->share_mode | DELETE_ON_CLOSE_FLAG) :
873 (shares->share_mode & ~DELETE_ON_CLOSE_FLAG) );
877 if (data->u.num_share_mode_entries) {
878 if (tdb_store(tdb, key, dbuf, TDB_REPLACE)==-1) {
879 SAFE_FREE(dbuf.dptr);
884 SAFE_FREE(dbuf.dptr);
888 /*******************************************************************
889 Print out a deferred open entry.
890 ********************************************************************/
892 char *deferred_open_str(int num, deferred_open_entry *e)
894 static pstring de_str;
896 slprintf(de_str, sizeof(de_str)-1, "deferred_open_entry[%d]: \
897 pid = %lu, mid = %u, dev = 0x%x, inode = %.0f, port = %u, time = [%u.%06u]",
898 num, (unsigned long)e->pid, (unsigned int)e->mid, (unsigned int)e->dev, (double)e->inode,
899 (unsigned int)e->port,
900 (unsigned int)e->time.tv_sec, (unsigned int)e->time.tv_usec );
905 /* Internal data structures for deferred opens... */
907 struct de_locking_key {
913 struct deferred_open_data {
915 int num_deferred_open_entries;
916 deferred_open_entry dummy; /* Needed for alignment. */
918 /* the following two entries are implicit
919 deferred_open_entry de_entries[num_deferred_open_entries];
924 /*******************************************************************
925 Print out a deferred open table.
926 ********************************************************************/
928 static void print_deferred_open_table(struct deferred_open_data *data)
930 int num_de_entries = data->u.num_deferred_open_entries;
931 deferred_open_entry *de_entries = (deferred_open_entry *)(data + 1);
934 for (i = 0; i < num_de_entries; i++) {
935 deferred_open_entry *entry_p = &de_entries[i];
936 DEBUG(10,("print_deferred_open_table: %s\n", deferred_open_str(i, entry_p) ));
941 /*******************************************************************
942 Form a static deferred open locking key for a dev/inode pair.
943 ******************************************************************/
945 static TDB_DATA deferred_open_locking_key(SMB_DEV_T dev, SMB_INO_T inode)
947 static struct de_locking_key key;
950 memset(&key, '\0', sizeof(key));
951 memcpy(&key.name[0], "DOE", 4);
954 kbuf.dptr = (char *)&key;
955 kbuf.dsize = sizeof(key);
959 /*******************************************************************
960 Get all deferred open entries for a dev/inode pair.
961 ********************************************************************/
963 int get_deferred_opens(connection_struct *conn,
964 SMB_DEV_T dev, SMB_INO_T inode,
965 deferred_open_entry **pp_de_entries)
968 struct deferred_open_data *data;
970 deferred_open_entry *de_entries = NULL;
971 TDB_DATA key = deferred_open_locking_key(dev, inode);
973 *pp_de_entries = NULL;
975 dbuf = tdb_fetch(tdb, key);
979 data = (struct deferred_open_data *)dbuf.dptr;
980 num_de_entries = data->u.num_deferred_open_entries;
986 de_entries = (deferred_open_entry *)memdup(dbuf.dptr + sizeof(*data),
987 num_de_entries * sizeof(deferred_open_entry));
990 SAFE_FREE(dbuf.dptr);
994 /* Save off the associated filename. */
995 pstrcpy(fname, dbuf.dptr + sizeof(*data) + num_de_entries * sizeof(deferred_open_entry));
998 * Ensure that each entry has a real process attached.
1001 for (i = 0; i < num_de_entries; ) {
1002 deferred_open_entry *entry_p = &de_entries[i];
1003 if (process_exists(entry_p->pid)) {
1004 DEBUG(10,("get_deferred_opens: %s\n", deferred_open_str(i, entry_p) ));
1007 DEBUG(10,("get_deferred_opens: deleted %s\n", deferred_open_str(i, entry_p) ));
1008 if (num_de_entries - i - 1 > 0) {
1009 memcpy( &de_entries[i], &de_entries[i+1],
1010 sizeof(deferred_open_entry) * (num_de_entries - i - 1));
1017 /* Did we delete any ? If so, re-store in tdb. */
1019 data->u.num_deferred_open_entries = num_de_entries;
1021 if (num_de_entries) {
1022 memcpy(dbuf.dptr + sizeof(*data), de_entries,
1023 num_de_entries * sizeof(deferred_open_entry));
1024 /* Append the filename. */
1025 pstrcpy(dbuf.dptr + sizeof(*data) + num_de_entries * sizeof(deferred_open_entry), fname);
1028 /* The record has shrunk a bit */
1029 dbuf.dsize -= del_count * sizeof(deferred_open_entry);
1031 if (data->u.num_deferred_open_entries == 0) {
1032 if (tdb_delete(tdb, key) == -1) {
1033 SAFE_FREE(de_entries);
1034 SAFE_FREE(dbuf.dptr);
1038 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
1039 SAFE_FREE(de_entries);
1040 SAFE_FREE(dbuf.dptr);
1047 SAFE_FREE(dbuf.dptr);
1048 *pp_de_entries = de_entries;
1049 return num_de_entries;
1052 /*******************************************************************
1053 Check if two deferred open entries are identical.
1054 ********************************************************************/
1056 static BOOL deferred_open_entries_identical( deferred_open_entry *e1, deferred_open_entry *e2)
1058 return (e1->pid == e2->pid &&
1059 e1->mid == e2->mid &&
1060 e1->port == e2->port &&
1061 e1->dev == e2->dev &&
1062 e1->inode == e2->inode &&
1063 e1->time.tv_sec == e2->time.tv_sec &&
1064 e1->time.tv_usec == e2->time.tv_usec);
1067 /*******************************************************************
1068 Delete a specific deferred open entry.
1069 Ignore if no entry deleted.
1070 ********************************************************************/
1072 BOOL delete_deferred_open_entry(deferred_open_entry *entry)
1075 struct deferred_open_data *data;
1077 deferred_open_entry *de_entries;
1079 TDB_DATA key = deferred_open_locking_key(entry->dev, entry->inode);
1081 /* read in the existing share modes */
1082 dbuf = tdb_fetch(tdb, key);
1086 data = (struct deferred_open_data *)dbuf.dptr;
1087 de_entries = (deferred_open_entry *)(dbuf.dptr + sizeof(*data));
1090 * Find any with this pid and delete it
1091 * by overwriting with the rest of the data
1095 DEBUG(10,("delete_deferred_open_entry: num_deferred_open_entries = %d\n",
1096 data->u.num_deferred_open_entries ));
1098 for (i=0;i<data->u.num_deferred_open_entries;) {
1099 if (deferred_open_entries_identical(&de_entries[i], entry)) {
1100 DEBUG(10,("delete_deferred_open_entry: deleted %s\n",
1101 deferred_open_str(i, &de_entries[i]) ));
1103 data->u.num_deferred_open_entries--;
1104 if ((dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*de_entries))) > 0) {
1105 memmove(&de_entries[i], &de_entries[i+1],
1106 dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*de_entries)));
1110 DEBUG(10,("delete_deferred_open_entry: deleting entry %d\n", i ));
1117 SMB_ASSERT(del_count == 0 || del_count == 1);
1120 /* the record may have shrunk a bit */
1121 dbuf.dsize -= del_count * sizeof(*de_entries);
1123 /* store it back in the database */
1124 if (data->u.num_deferred_open_entries == 0) {
1125 if (tdb_delete(tdb, key) == -1)
1128 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
1132 DEBUG(10,("delete_deferred_open_entry: Remaining table.\n"));
1133 print_deferred_open_table((struct deferred_open_data*)dbuf.dptr);
1134 SAFE_FREE(dbuf.dptr);
1138 /*******************************************************************
1139 Fill a deferred open entry.
1140 ********************************************************************/
1142 static void fill_deferred_open(char *p, uint16 mid, struct timeval *ptv, SMB_DEV_T dev, SMB_INO_T inode, uint16 port)
1144 deferred_open_entry *e = (deferred_open_entry *)p;
1145 void *x = &e->time; /* Needed to force alignment. p may not be aligned.... */
1147 memset(e, '\0', sizeof(deferred_open_entry));
1149 e->pid = sys_getpid();
1150 memcpy(x, ptv, sizeof(struct timeval));
1156 /*******************************************************************
1157 Add a deferred open record. Return False on fail, True on success.
1158 ********************************************************************/
1160 BOOL add_deferred_open(uint16 mid, struct timeval *ptv, SMB_DEV_T dev, SMB_INO_T inode, uint16 port, const char *fname)
1163 struct deferred_open_data *data;
1166 TDB_DATA key = deferred_open_locking_key(dev, inode);
1169 /* read in the existing deferred open records if any */
1170 dbuf = tdb_fetch(tdb, key);
1173 /* we'll need to create a new record */
1175 size = sizeof(*data) + sizeof(deferred_open_entry) + strlen(fname) + 1;
1176 p = (char *)SMB_MALLOC(size);
1179 data = (struct deferred_open_data *)p;
1180 data->u.num_deferred_open_entries = 1;
1182 DEBUG(10,("add_deferred_open: creating entry for file %s. num_deferred_open_entries = 1\n",
1185 offset = sizeof(*data) + sizeof(deferred_open_entry);
1186 safe_strcpy(p + offset, fname, size - offset - 1);
1187 fill_deferred_open(p + sizeof(*data), mid, ptv, dev, inode, port);
1190 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
1193 print_deferred_open_table((struct deferred_open_data *)p);
1199 /* we're adding to an existing entry - this is a bit fiddly */
1200 data = (struct deferred_open_data *)dbuf.dptr;
1202 data->u.num_deferred_open_entries++;
1204 DEBUG(10,("add_deferred_open: adding entry for file %s. new num_deferred_open_entries = %d\n",
1205 fname, data->u.num_deferred_open_entries ));
1207 size = dbuf.dsize + sizeof(deferred_open_entry);
1208 p = SMB_MALLOC(size);
1210 SAFE_FREE(dbuf.dptr);
1213 memcpy(p, dbuf.dptr, sizeof(*data));
1214 fill_deferred_open(p + sizeof(*data), mid, ptv, dev, inode, port);
1215 memcpy(p + sizeof(*data) + sizeof(deferred_open_entry), dbuf.dptr + sizeof(*data),
1216 dbuf.dsize - sizeof(*data));
1217 SAFE_FREE(dbuf.dptr);
1220 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
1222 print_deferred_open_table((struct deferred_open_data *)p);
1227 /****************************************************************************
1228 Traverse the whole database with this function, calling traverse_callback
1230 ****************************************************************************/
1232 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1235 struct locking_data *data;
1236 share_mode_entry *shares;
1240 SHAREMODE_FN(traverse_callback) = (SHAREMODE_FN_CAST())state;
1242 /* Ensure this is a locking_key record. */
1243 if (kbuf.dsize != sizeof(struct locking_key))
1246 data = (struct locking_data *)dbuf.dptr;
1247 shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
1248 name = dbuf.dptr + sizeof(*data) + data->u.num_share_mode_entries*sizeof(*shares);
1250 for (i=0;i<data->u.num_share_mode_entries;i++) {
1251 traverse_callback(&shares[i], name);
1256 /*******************************************************************
1257 Call the specified function on each entry under management by the
1259 ********************************************************************/
1261 int share_mode_forall(SHAREMODE_FN(fn))
1265 return tdb_traverse(tdb, traverse_fn, (void*)fn);