2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2000
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 12 aug 96: Erik.Devriendt@te6.siemens.be
25 added support for shared memory implementation of share mode locking
27 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
28 locking to deal with multiple share modes per open file.
30 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
33 rewrtten completely to use new tdb code. Tridge, Dec '99
35 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
42 #define DBGC_CLASS DBGC_LOCKING
44 /* the locking database handle */
45 static TDB_CONTEXT *tdb;
50 int num_share_mode_entries;
52 BOOL initial_delete_on_close; /* Only set at NTCreateX if file was created. */
54 struct share_mode_entry dummy; /* Needed for alignment. */
56 /* the following two entries are implicit
57 struct share_mode_entry modes[num_share_mode_entries];
62 /****************************************************************************
64 ****************************************************************************/
66 static const char *lock_type_name(enum brl_type lock_type)
68 return (lock_type == READ_LOCK) ? "READ" : "WRITE";
71 /****************************************************************************
72 Utility function called to see if a file region is locked.
73 ****************************************************************************/
75 BOOL is_locked(files_struct *fsp,connection_struct *conn,
76 SMB_BIG_UINT count,SMB_BIG_UINT offset,
77 enum brl_type lock_type)
79 int snum = SNUM(conn);
80 int strict_locking = lp_strict_locking(snum);
86 if (!lp_locking(snum) || !strict_locking)
89 if (strict_locking == Auto) {
90 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
91 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
93 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
94 (lock_type == READ_LOCK)) {
95 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
98 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
99 global_smbpid, procid_self(), conn->cnum,
100 offset, count, lock_type);
103 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
104 global_smbpid, procid_self(), conn->cnum,
105 offset, count, lock_type);
108 DEBUG(10,("is_locked: brl start=%.0f len=%.0f %s for file %s\n",
109 (double)offset, (double)count, ret ? "locked" : "unlocked",
113 * There is no lock held by an SMB daemon, check to
114 * see if there is a POSIX lock from a UNIX or NFS process.
117 if(!ret && lp_posix_locking(snum)) {
118 ret = is_posix_locked(fsp, offset, count, lock_type);
120 DEBUG(10,("is_locked: posix start=%.0f len=%.0f %s for file %s\n",
121 (double)offset, (double)count, ret ? "locked" : "unlocked",
128 /****************************************************************************
129 Utility function called by locking requests.
130 ****************************************************************************/
132 static NTSTATUS do_lock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
133 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
135 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
137 if (!lp_locking(SNUM(conn)))
140 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
142 DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
143 lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
145 if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
146 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
147 lock_pid, procid_self(), conn->cnum,
149 lock_type, my_lock_ctx);
151 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
154 * Try and get a POSIX lock on this range.
155 * Note that this is ok if it is a read lock
156 * overlapping on a different fd. JRA.
159 if (!set_posix_lock(fsp, offset, count, lock_type)) {
160 if (errno == EACCES || errno == EAGAIN)
161 status = NT_STATUS_FILE_LOCK_CONFLICT;
163 status = map_nt_error_from_unix(errno);
166 * We failed to map - we must now remove the brl
169 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
170 lock_pid, procid_self(), conn->cnum,
171 offset, count, False,
180 /****************************************************************************
181 Utility function called by locking requests. This is *DISGUSTING*. It also
182 appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
183 is so slow on the locking tests...... ? This is the reason. Much though I hate
184 it, we need this. JRA.
185 ****************************************************************************/
187 NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
188 SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
190 int j, maxj = lp_lock_spin_count();
191 int sleeptime = lp_lock_sleep_time();
192 NTSTATUS status, ret;
197 ret = NT_STATUS_OK; /* to keep dumb compilers happy */
199 for (j = 0; j < maxj; j++) {
200 status = do_lock(fsp, conn, lock_pid, count, offset, lock_type, my_lock_ctx);
201 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
202 !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
205 /* if we do fail then return the first error code we got */
208 /* Don't spin if we blocked ourselves. */
213 sys_usleep(sleeptime);
218 /* Struct passed to brl_unlock. */
219 struct posix_unlock_data_struct {
225 /****************************************************************************
226 Function passed to brl_unlock to allow POSIX unlock to be done first.
227 ****************************************************************************/
229 static void posix_unlock(void *pre_data)
231 struct posix_unlock_data_struct *pdata = (struct posix_unlock_data_struct *)pre_data;
233 if (lp_posix_locking(SNUM(pdata->fsp->conn)))
234 release_posix_lock(pdata->fsp, pdata->offset, pdata->count);
237 /****************************************************************************
238 Utility function called by unlocking requests.
239 ****************************************************************************/
241 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
242 SMB_BIG_UINT count,SMB_BIG_UINT offset)
245 struct posix_unlock_data_struct posix_data;
247 if (!lp_locking(SNUM(conn)))
250 if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
251 return NT_STATUS_INVALID_HANDLE;
254 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
255 (double)offset, (double)count, fsp->fsp_name ));
258 * Remove the existing lock record from the tdb lockdb
259 * before looking at POSIX locks. If this record doesn't
260 * match then don't bother looking to remove POSIX locks.
263 posix_data.fsp = fsp;
264 posix_data.offset = offset;
265 posix_data.count = count;
267 ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
268 lock_pid, procid_self(), conn->cnum, offset, count,
269 False, posix_unlock, (void *)&posix_data);
272 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
273 return NT_STATUS_RANGE_NOT_LOCKED;
278 /****************************************************************************
279 Remove any locks on this fd. Called from file_close().
280 ****************************************************************************/
282 void locking_close_file(files_struct *fsp)
284 struct process_id pid = procid_self();
286 if (!lp_locking(SNUM(fsp->conn)))
290 * Just release all the brl locks, no need to release individually.
293 brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
295 if(lp_posix_locking(SNUM(fsp->conn))) {
298 * Release all the POSIX locks.
300 posix_locking_close_file(fsp);
305 /****************************************************************************
306 Initialise the locking functions.
307 ****************************************************************************/
309 static int open_read_only;
311 BOOL locking_init(int read_only)
318 tdb = tdb_open_log(lock_path("locking.tdb"),
319 SMB_OPEN_DATABASE_TDB_HASH_SIZE, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
320 read_only?O_RDONLY:O_RDWR|O_CREAT,
324 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
328 if (!posix_locking_init(read_only))
331 open_read_only = read_only;
336 /*******************************************************************
337 Deinitialize the share_mode management.
338 ******************************************************************/
340 BOOL locking_end(void)
344 brl_shutdown(open_read_only);
346 if (tdb_close(tdb) != 0)
353 /*******************************************************************
354 Form a static locking key for a dev/inode pair.
355 ******************************************************************/
357 /* key and data records in the tdb locking database */
363 /*******************************************************************
364 Form a static locking key for a dev/inode pair.
365 ******************************************************************/
367 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
369 static struct locking_key key;
372 memset(&key, '\0', sizeof(key));
375 kbuf.dptr = (char *)&key;
376 kbuf.dsize = sizeof(key);
380 /*******************************************************************
381 Print out a share mode.
382 ********************************************************************/
384 char *share_mode_str(int num, struct share_mode_entry *e)
386 static pstring share_str;
388 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
389 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
390 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
391 "dev = 0x%x, inode = %.0f",
393 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
394 procid_str_static(&e->pid),
395 e->share_access, e->private_options,
396 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
397 (unsigned int)e->dev, (double)e->inode );
402 /*******************************************************************
403 Print out a share mode table.
404 ********************************************************************/
406 static void print_share_mode_table(struct locking_data *data)
408 int num_share_modes = data->u.s.num_share_mode_entries;
409 struct share_mode_entry *shares =
410 (struct share_mode_entry *)(data + 1);
413 for (i = 0; i < num_share_modes; i++) {
414 struct share_mode_entry entry;
416 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
417 DEBUG(10,("print_share_mode_table: %s\n",
418 share_mode_str(i, &entry)));
422 /*******************************************************************
423 Get all share mode entries for a dev/inode pair.
424 ********************************************************************/
426 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
428 struct locking_data *data;
431 if (dbuf.dsize < sizeof(struct locking_data)) {
432 DEBUG(0, ("parse_share_modes: buffer too short\n"));
436 data = (struct locking_data *)dbuf.dptr;
438 lck->delete_on_close = data->u.s.delete_on_close;
439 lck->initial_delete_on_close = data->u.s.initial_delete_on_close;
440 lck->num_share_modes = data->u.s.num_share_mode_entries;
442 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
443 "initial_delete_on_close: %d, "
444 "num_share_modes: %d\n",
445 lck->delete_on_close,
446 lck->initial_delete_on_close,
447 lck->num_share_modes));
449 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
450 DEBUG(0, ("invalid number of share modes: %d\n",
451 lck->num_share_modes));
455 lck->share_modes = NULL;
457 if (lck->num_share_modes != 0) {
459 if (dbuf.dsize < (sizeof(struct locking_data) +
460 (lck->num_share_modes *
461 sizeof(struct share_mode_entry)))) {
462 DEBUG(0, ("parse_share_modes: buffer too short\n"));
466 lck->share_modes = talloc_memdup(lck, dbuf.dptr+sizeof(*data),
467 lck->num_share_modes *
468 sizeof(struct share_mode_entry));
470 if (lck->share_modes == NULL) {
471 DEBUG(0, ("talloc failed\n"));
476 /* Save off the associated service path and filename. */
477 lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
478 (lck->num_share_modes *
479 sizeof(struct share_mode_entry)));
481 lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
482 (lck->num_share_modes *
483 sizeof(struct share_mode_entry)) +
484 strlen(lck->servicepath) + 1 );
487 * Ensure that each entry has a real process attached.
490 for (i = 0; i < lck->num_share_modes; i++) {
491 struct share_mode_entry *entry_p = &lck->share_modes[i];
492 DEBUG(10,("parse_share_modes: %s\n",
493 share_mode_str(i, entry_p) ));
494 if (!process_exists(entry_p->pid)) {
495 DEBUG(10,("parse_share_modes: deleted %s\n",
496 share_mode_str(i, entry_p) ));
497 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
498 lck->modified = True;
505 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
510 struct locking_data *data;
517 for (i=0; i<lck->num_share_modes; i++) {
518 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
523 if (num_valid == 0) {
527 sp_len = strlen(lck->servicepath);
529 result.dsize = sizeof(*data) +
530 lck->num_share_modes * sizeof(struct share_mode_entry) +
532 strlen(lck->filename) + 1;
533 result.dptr = talloc_size(lck, result.dsize);
535 if (result.dptr == NULL) {
536 smb_panic("talloc failed\n");
539 data = (struct locking_data *)result.dptr;
541 data->u.s.num_share_mode_entries = lck->num_share_modes;
542 data->u.s.delete_on_close = lck->delete_on_close;
543 data->u.s.initial_delete_on_close = lck->initial_delete_on_close;
544 DEBUG(10, ("unparse_share_modes: del: %d, initial del %d, num: %d\n",
545 data->u.s.delete_on_close,
546 data->u.s.initial_delete_on_close,
547 data->u.s.num_share_mode_entries));
548 memcpy(result.dptr + sizeof(*data), lck->share_modes,
549 sizeof(struct share_mode_entry)*lck->num_share_modes);
550 offset = sizeof(*data) +
551 sizeof(struct share_mode_entry)*lck->num_share_modes;
552 safe_strcpy(result.dptr + offset, lck->servicepath,
553 result.dsize - offset - 1);
554 offset += sp_len + 1;
555 safe_strcpy(result.dptr + offset, lck->filename,
556 result.dsize - offset - 1);
558 if (DEBUGLEVEL >= 10) {
559 print_share_mode_table(data);
565 static int share_mode_lock_destructor(void *p)
567 struct share_mode_lock *lck =
568 talloc_get_type_abort(p, struct share_mode_lock);
569 TDB_DATA key = locking_key(lck->dev, lck->ino);
572 if (!lck->modified) {
576 data = unparse_share_modes(lck);
578 if (data.dptr == NULL) {
580 /* There has been an entry before, delete it */
581 if (tdb_delete(tdb, key) == -1) {
582 smb_panic("Could not delete share entry\n");
588 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
589 smb_panic("Could not store share mode entry\n");
593 tdb_chainunlock(tdb, key);
598 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
599 SMB_DEV_T dev, SMB_INO_T ino,
600 const char *servicepath,
603 struct share_mode_lock *lck;
604 TDB_DATA key = locking_key(dev, ino);
607 lck = TALLOC_P(mem_ctx, struct share_mode_lock);
609 DEBUG(0, ("talloc failed\n"));
613 /* Ensure we set every field here as the destructor must be
614 valid even if parse_share_modes fails. */
616 lck->servicepath = NULL;
617 lck->filename = NULL;
620 lck->num_share_modes = 0;
621 lck->share_modes = NULL;
622 lck->delete_on_close = False;
623 lck->initial_delete_on_close = False;
625 lck->modified = False;
627 if (tdb_chainlock(tdb, key) != 0) {
628 DEBUG(3, ("Could not lock share entry\n"));
633 /* We must set the destructor immediately after the chainlock
634 ensure the lock is cleaned up on any of the error return
637 talloc_set_destructor(lck, share_mode_lock_destructor);
639 data = tdb_fetch(tdb, key);
640 lck->fresh = (data.dptr == NULL);
644 if (fname == NULL || servicepath == NULL) {
648 lck->filename = talloc_strdup(lck, fname);
649 lck->servicepath = talloc_strdup(lck, servicepath);
650 if (lck->filename == NULL || lck->servicepath == NULL) {
651 DEBUG(0, ("talloc failed\n"));
656 if (!parse_share_modes(data, lck)) {
657 DEBUG(0, ("Could not parse share modes\n"));
659 SAFE_FREE(data.dptr);
664 SAFE_FREE(data.dptr);
669 /*******************************************************************
670 Sets the service name and filename for rename.
671 At this point we emit "file renamed" messages to all
672 process id's that have this file open.
673 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
674 ********************************************************************/
676 BOOL rename_share_filename(struct share_mode_lock *lck,
677 const char *servicepath,
690 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
691 servicepath, newname));
694 * rename_internal_fsp() and rename_internals() add './' to
695 * head of newname if newname does not contain a '/'.
697 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
701 lck->servicepath = talloc_strdup(lck, servicepath);
702 lck->filename = talloc_strdup(lck, newname);
703 if (lck->filename == NULL || lck->servicepath == NULL) {
704 DEBUG(0, ("rename_share_filename: talloc failed\n"));
707 lck->modified = True;
709 sp_len = strlen(lck->servicepath);
710 fn_len = strlen(lck->filename);
712 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
714 /* Set up the name changed message. */
715 frm = TALLOC(lck, msg_len);
720 SDEV_T_VAL(frm,0,lck->dev);
721 SINO_T_VAL(frm,8,lck->ino);
723 DEBUG(10,("rename_share_filename: msg_len = %d\n", msg_len ));
725 safe_strcpy(&frm[16], lck->servicepath, sp_len);
726 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
728 /* Send the messages. */
729 for (i=0; i<lck->num_share_modes; i++) {
730 struct share_mode_entry *se = &lck->share_modes[i];
731 if (!is_valid_share_mode_entry(se)) {
734 /* But not to ourselves... */
735 if (procid_is_me(&se->pid)) {
739 DEBUG(10,("rename_share_filename: sending rename message to pid %u "
740 "dev %x, inode %.0f sharepath %s newname %s\n",
741 (unsigned int)procid_to_pid(&se->pid),
742 (unsigned int)lck->dev, (double)lck->ino,
743 lck->servicepath, lck->filename ));
746 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
754 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
757 struct share_mode_lock *lck = get_share_mode_lock(NULL, dev, inode, NULL, NULL);
761 result = lck->delete_on_close;
766 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
770 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
771 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
772 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
774 SMB_ASSERT(num_props <= 1);
775 return (num_props != 0);
778 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
780 return (e->op_type == DEFERRED_OPEN_ENTRY);
783 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
785 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
788 /*******************************************************************
789 Fill a share mode entry.
790 ********************************************************************/
792 static void fill_share_mode_entry(struct share_mode_entry *e,
794 uint16 mid, uint16 op_type)
797 e->pid = procid_self();
798 e->share_access = fsp->share_access;
799 e->private_options = fsp->fh->private_options;
800 e->access_mask = fsp->access_mask;
802 e->op_type = op_type;
803 e->time.tv_sec = fsp->open_time.tv_sec;
804 e->time.tv_usec = fsp->open_time.tv_usec;
805 e->share_file_id = fsp->file_id;
807 e->inode = fsp->inode;
810 static void fill_deferred_open_entry(struct share_mode_entry *e,
811 const struct timeval request_time,
812 SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
815 e->pid = procid_self();
817 e->op_type = DEFERRED_OPEN_ENTRY;
818 e->time.tv_sec = request_time.tv_sec;
819 e->time.tv_usec = request_time.tv_usec;
824 static void add_share_mode_entry(struct share_mode_lock *lck,
825 const struct share_mode_entry *entry)
829 for (i=0; i<lck->num_share_modes; i++) {
830 struct share_mode_entry *e = &lck->share_modes[i];
831 if (is_unused_share_mode_entry(e)) {
837 if (i == lck->num_share_modes) {
838 /* No unused entry found */
839 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
840 &lck->share_modes, &lck->num_share_modes);
842 lck->modified = True;
845 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
846 uint16 mid, uint16 op_type)
848 struct share_mode_entry entry;
849 fill_share_mode_entry(&entry, fsp, mid, op_type);
850 add_share_mode_entry(lck, &entry);
853 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
854 struct timeval request_time,
855 SMB_DEV_T dev, SMB_INO_T ino)
857 struct share_mode_entry entry;
858 fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
859 add_share_mode_entry(lck, &entry);
862 /*******************************************************************
863 Check if two share mode entries are identical, ignoring oplock
864 and mid info and desired_access.
865 ********************************************************************/
867 static BOOL share_modes_identical(struct share_mode_entry *e1,
868 struct share_mode_entry *e2)
870 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
871 if (procid_equal(&e1->pid, &e2->pid) &&
872 e1->share_file_id == e2->share_file_id &&
873 e1->dev == e2->dev &&
874 e1->inode == e2->inode &&
875 (e1->share_access) != (e2->share_access)) {
876 DEBUG(0,("PANIC: share_modes_identical: share_mode "
877 "mismatch (e1 = 0x%x, e2 = 0x%x). Logic error.\n",
878 (unsigned int)e1->share_access,
879 (unsigned int)e2->share_access ));
880 smb_panic("PANIC: share_modes_identical logic error.\n");
884 return (procid_equal(&e1->pid, &e2->pid) &&
885 (e1->share_access) == (e2->share_access) &&
886 e1->dev == e2->dev &&
887 e1->inode == e2->inode &&
888 e1->share_file_id == e2->share_file_id );
891 static BOOL deferred_open_identical(struct share_mode_entry *e1,
892 struct share_mode_entry *e2)
894 return (procid_equal(&e1->pid, &e2->pid) &&
895 (e1->op_mid == e2->op_mid) &&
896 (e1->dev == e2->dev) &&
897 (e1->inode == e2->inode));
900 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
901 struct share_mode_entry *entry)
905 for (i=0; i<lck->num_share_modes; i++) {
906 struct share_mode_entry *e = &lck->share_modes[i];
907 if (is_valid_share_mode_entry(entry) &&
908 is_valid_share_mode_entry(e) &&
909 share_modes_identical(e, entry)) {
912 if (is_deferred_open_entry(entry) &&
913 is_deferred_open_entry(e) &&
914 deferred_open_identical(e, entry)) {
921 /*******************************************************************
922 Del the share mode of a file for this process. Return the number of
924 ********************************************************************/
926 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
928 struct share_mode_entry entry, *e;
930 fill_share_mode_entry(&entry, fsp, 0, 0);
932 e = find_share_mode_entry(lck, &entry);
937 e->op_type = UNUSED_SHARE_MODE_ENTRY;
938 lck->modified = True;
942 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
944 struct share_mode_entry entry, *e;
946 fill_deferred_open_entry(&entry, timeval_zero(),
947 lck->dev, lck->ino, mid);
949 e = find_share_mode_entry(lck, &entry);
954 e->op_type = UNUSED_SHARE_MODE_ENTRY;
955 lck->modified = True;
958 /*******************************************************************
959 Remove an oplock mid and mode entry from a share mode.
960 ********************************************************************/
962 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
964 struct share_mode_entry entry, *e;
966 fill_share_mode_entry(&entry, fsp, 0, 0);
968 e = find_share_mode_entry(lck, &entry);
974 e->op_type = NO_OPLOCK;
975 lck->modified = True;
979 /*******************************************************************
980 Downgrade a oplock type from exclusive to level II.
981 ********************************************************************/
983 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
985 struct share_mode_entry entry, *e;
987 fill_share_mode_entry(&entry, fsp, 0, 0);
989 e = find_share_mode_entry(lck, &entry);
994 e->op_type = LEVEL_II_OPLOCK;
995 lck->modified = True;
999 /****************************************************************************
1000 Deal with the internal needs of setting the delete on close flag. Note that
1001 as the tdb locking is recursive, it is safe to call this from within
1002 open_file_shared. JRA.
1003 ****************************************************************************/
1005 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1008 if (!delete_on_close) {
1009 return NT_STATUS_OK;
1013 * Only allow delete on close for writable files.
1016 if ((dosmode & aRONLY) &&
1017 !lp_delete_readonly(SNUM(fsp->conn))) {
1018 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1019 "flag set but file attribute is readonly.\n",
1021 return NT_STATUS_CANNOT_DELETE;
1025 * Only allow delete on close for writable shares.
1028 if (!CAN_WRITE(fsp->conn)) {
1029 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1030 "close flag set but write access denied on share.\n",
1032 return NT_STATUS_ACCESS_DENIED;
1036 * Only allow delete on close for files/directories opened with delete
1040 if (!(fsp->access_mask & DELETE_ACCESS)) {
1041 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1042 "close flag set but delete access denied.\n",
1044 return NT_STATUS_ACCESS_DENIED;
1047 return NT_STATUS_OK;
1050 /****************************************************************************
1051 Sets the delete on close flag over all share modes on this file.
1052 Modify the share mode entry for all files open
1053 on this device and inode to tell other smbds we have
1054 changed the delete on close flag. This will be noticed
1055 in the close code, the last closer will delete the file
1057 Note that setting this to any value clears the initial_delete_on_close flag.
1058 ****************************************************************************/
1060 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close)
1062 struct share_mode_lock *lck;
1064 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1065 "fnum = %d, file %s\n",
1066 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1073 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1077 if (lck->delete_on_close != delete_on_close) {
1078 lck->delete_on_close = delete_on_close;
1079 lck->modified = True;
1082 if (lck->initial_delete_on_close) {
1083 lck->initial_delete_on_close = False;
1084 lck->modified = True;
1091 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1094 struct locking_data *data;
1095 struct share_mode_entry *shares;
1096 const char *sharepath;
1099 void (*traverse_callback)(struct share_mode_entry *, const char *, const char *) = state;
1101 /* Ensure this is a locking_key record. */
1102 if (kbuf.dsize != sizeof(struct locking_key))
1105 data = (struct locking_data *)dbuf.dptr;
1106 shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1107 sharepath = dbuf.dptr + sizeof(*data) +
1108 data->u.s.num_share_mode_entries*sizeof(*shares);
1109 fname = dbuf.dptr + sizeof(*data) +
1110 data->u.s.num_share_mode_entries*sizeof(*shares) +
1111 strlen(sharepath) + 1;
1113 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1114 traverse_callback(&shares[i], sharepath, fname);
1119 /*******************************************************************
1120 Call the specified function on each entry under management by the
1122 ********************************************************************/
1124 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *, const char *))
1128 return tdb_traverse(tdb, traverse_fn, fn);