2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2006
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.
36 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
43 #define DBGC_CLASS DBGC_LOCKING
45 /* the locking database handle */
46 static TDB_CONTEXT *tdb;
48 /****************************************************************************
50 ****************************************************************************/
52 const char *lock_type_name(enum brl_type lock_type)
66 const char *lock_flav_name(enum brl_flavour lock_flav)
68 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
71 /****************************************************************************
72 Utility function called to see if a file region is locked.
73 Called in the read/write codepath.
74 ****************************************************************************/
76 BOOL is_locked(files_struct *fsp,
79 enum brl_type lock_type)
81 int snum = SNUM(fsp->conn);
82 int strict_locking = lp_strict_locking(snum);
83 enum brl_flavour lock_flav = lp_posix_cifsu_locktype();
90 if (!lp_locking(snum) || !strict_locking) {
94 if (strict_locking == Auto) {
95 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
96 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
98 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
99 (lock_type == READ_LOCK)) {
100 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
103 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
107 ret = !brl_locktest(br_lck,
117 struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
121 ret = !brl_locktest(br_lck,
131 DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
132 lock_flav_name(lock_flav),
133 (double)offset, (double)count, ret ? "locked" : "unlocked",
134 fsp->fnum, fsp->fsp_name ));
139 /****************************************************************************
140 Find out if a lock could be granted - return who is blocking us if we can't.
141 ****************************************************************************/
143 NTSTATUS query_lock(files_struct *fsp,
145 SMB_BIG_UINT *pcount,
146 SMB_BIG_UINT *poffset,
147 enum brl_type *plock_type,
148 enum brl_flavour lock_flav)
150 struct byte_range_lock *br_lck = NULL;
151 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
153 if (!OPEN_FSP(fsp) || !fsp->can_lock) {
154 return NT_STATUS_INVALID_HANDLE;
157 if (!lp_locking(SNUM(fsp->conn))) {
161 br_lck = brl_get_locks(NULL, fsp);
163 return NT_STATUS_NO_MEMORY;
166 status = brl_lockquery(br_lck,
178 /****************************************************************************
179 Utility function called by locking requests.
180 ****************************************************************************/
182 NTSTATUS do_lock(files_struct *fsp,
186 enum brl_type lock_type,
187 enum brl_flavour lock_flav,
190 struct byte_range_lock *br_lck = NULL;
191 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
193 if (!OPEN_FSP(fsp) || !fsp->can_lock) {
194 return NT_STATUS_INVALID_HANDLE;
197 if (!lp_locking(SNUM(fsp->conn))) {
201 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
203 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
204 lock_flav_name(lock_flav), lock_type_name(lock_type),
205 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
207 br_lck = brl_get_locks(NULL, fsp);
209 return NT_STATUS_NO_MEMORY;
212 status = brl_lock(br_lck,
225 /****************************************************************************
226 Utility function called by locking requests. This is *DISGUSTING*. It also
227 appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
228 is so slow on the locking tests...... ? This is the reason. Much though I hate
229 it, we need this. JRA.
230 ****************************************************************************/
232 NTSTATUS do_lock_spin(files_struct *fsp,
236 enum brl_type lock_type,
237 enum brl_flavour lock_flav,
240 int j, maxj = lp_lock_spin_count();
241 int sleeptime = lp_lock_sleep_time();
242 NTSTATUS status, ret;
248 ret = NT_STATUS_OK; /* to keep dumb compilers happy */
250 for (j = 0; j < maxj; j++) {
251 status = do_lock(fsp,
259 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
260 !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
263 /* if we do fail then return the first error code we got */
266 /* Don't spin if we blocked ourselves. */
271 /* Only spin for Windows locks. */
272 if (lock_flav == POSIX_LOCK) {
278 sys_usleep(sleeptime);
284 /****************************************************************************
285 Utility function called by unlocking requests.
286 ****************************************************************************/
288 NTSTATUS do_unlock(files_struct *fsp,
292 enum brl_flavour lock_flav)
295 struct byte_range_lock *br_lck = NULL;
297 if (!lp_locking(SNUM(fsp->conn))) {
301 if (!OPEN_FSP(fsp) || !fsp->can_lock) {
302 return NT_STATUS_INVALID_HANDLE;
305 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
306 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
308 br_lck = brl_get_locks(NULL, fsp);
310 return NT_STATUS_NO_MEMORY;
313 ok = brl_unlock(br_lck,
323 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
324 return NT_STATUS_RANGE_NOT_LOCKED;
330 /****************************************************************************
331 Remove any locks on this fd. Called from file_close().
332 ****************************************************************************/
334 void locking_close_file(files_struct *fsp)
336 struct byte_range_lock *br_lck;
337 struct process_id pid = procid_self();
339 if (!lp_locking(SNUM(fsp->conn)))
343 * Just release all the brl locks, no need to release individually.
346 br_lck = brl_get_locks(NULL,fsp);
348 brl_close_fnum(br_lck, pid);
352 if(lp_posix_locking(SNUM(fsp->conn))) {
353 /* Release all the POSIX locks.*/
354 posix_locking_close_file(fsp);
359 /****************************************************************************
360 Initialise the locking functions.
361 ****************************************************************************/
363 static int open_read_only;
365 BOOL locking_init(int read_only)
372 tdb = tdb_open_log(lock_path("locking.tdb"),
373 lp_open_files_db_hash_size(),
374 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
375 read_only?O_RDONLY:O_RDWR|O_CREAT,
379 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
383 if (!posix_locking_init(read_only))
386 open_read_only = read_only;
391 /*******************************************************************
392 Deinitialize the share_mode management.
393 ******************************************************************/
395 BOOL locking_end(void)
399 brl_shutdown(open_read_only);
401 if (tdb_close(tdb) != 0)
408 /*******************************************************************
409 Form a static locking key for a dev/inode pair.
410 ******************************************************************/
412 /* key and data records in the tdb locking database */
418 /*******************************************************************
419 Form a static locking key for a dev/inode pair.
420 ******************************************************************/
422 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
424 static struct locking_key key;
427 memset(&key, '\0', sizeof(key));
430 kbuf.dptr = (char *)&key;
431 kbuf.dsize = sizeof(key);
435 /*******************************************************************
436 Print out a share mode.
437 ********************************************************************/
439 char *share_mode_str(int num, struct share_mode_entry *e)
441 static pstring share_str;
443 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
444 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
445 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
446 "dev = 0x%x, inode = %.0f",
448 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
449 procid_str_static(&e->pid),
450 e->share_access, e->private_options,
451 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
452 (unsigned int)e->dev, (double)e->inode );
457 /*******************************************************************
458 Print out a share mode table.
459 ********************************************************************/
461 static void print_share_mode_table(struct locking_data *data)
463 int num_share_modes = data->u.s.num_share_mode_entries;
464 struct share_mode_entry *shares =
465 (struct share_mode_entry *)(data + 1);
468 for (i = 0; i < num_share_modes; i++) {
469 struct share_mode_entry entry;
471 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
472 DEBUG(10,("print_share_mode_table: %s\n",
473 share_mode_str(i, &entry)));
477 /*******************************************************************
478 Get all share mode entries for a dev/inode pair.
479 ********************************************************************/
481 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
483 struct locking_data *data;
486 if (dbuf.dsize < sizeof(struct locking_data)) {
487 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
490 data = (struct locking_data *)dbuf.dptr;
492 lck->delete_on_close = data->u.s.delete_on_close;
493 lck->initial_delete_on_close = data->u.s.initial_delete_on_close;
494 lck->num_share_modes = data->u.s.num_share_mode_entries;
496 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
497 "initial_delete_on_close: %d, "
498 "num_share_modes: %d\n",
499 lck->delete_on_close,
500 lck->initial_delete_on_close,
501 lck->num_share_modes));
503 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
504 DEBUG(0, ("invalid number of share modes: %d\n",
505 lck->num_share_modes));
506 smb_panic("PANIC: invalid number of share modes");
509 lck->share_modes = NULL;
511 if (lck->num_share_modes != 0) {
513 if (dbuf.dsize < (sizeof(struct locking_data) +
514 (lck->num_share_modes *
515 sizeof(struct share_mode_entry)))) {
516 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
519 lck->share_modes = talloc_memdup(lck, dbuf.dptr+sizeof(*data),
520 lck->num_share_modes *
521 sizeof(struct share_mode_entry));
523 if (lck->share_modes == NULL) {
524 smb_panic("talloc failed\n");
528 /* Get any delete token. */
529 if (data->u.s.delete_token_size) {
530 char *p = dbuf.dptr + sizeof(*data) +
531 (lck->num_share_modes *
532 sizeof(struct share_mode_entry));
534 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
535 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
536 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
537 data->u.s.delete_token_size));
538 smb_panic("parse_share_modes: invalid token size\n");
541 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
542 if (!lck->delete_token) {
543 smb_panic("talloc failed\n");
546 /* Copy out the uid and gid. */
547 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
549 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
552 /* Any supplementary groups ? */
553 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
554 ((data->u.s.delete_token_size -
555 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
557 if (lck->delete_token->ngroups) {
558 /* Make this a talloc child of lck->delete_token. */
559 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
560 lck->delete_token->ngroups);
561 if (!lck->delete_token) {
562 smb_panic("talloc failed\n");
565 for (i = 0; i < lck->delete_token->ngroups; i++) {
566 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
572 lck->delete_token = NULL;
575 /* Save off the associated service path and filename. */
576 lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
577 (lck->num_share_modes *
578 sizeof(struct share_mode_entry)) +
579 data->u.s.delete_token_size );
581 lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
582 (lck->num_share_modes *
583 sizeof(struct share_mode_entry)) +
584 data->u.s.delete_token_size +
585 strlen(lck->servicepath) + 1 );
588 * Ensure that each entry has a real process attached.
591 for (i = 0; i < lck->num_share_modes; i++) {
592 struct share_mode_entry *entry_p = &lck->share_modes[i];
593 DEBUG(10,("parse_share_modes: %s\n",
594 share_mode_str(i, entry_p) ));
595 if (!process_exists(entry_p->pid)) {
596 DEBUG(10,("parse_share_modes: deleted %s\n",
597 share_mode_str(i, entry_p) ));
598 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
599 lck->modified = True;
606 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
611 struct locking_data *data;
614 uint32 delete_token_size;
619 for (i=0; i<lck->num_share_modes; i++) {
620 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
625 if (num_valid == 0) {
629 sp_len = strlen(lck->servicepath);
630 delete_token_size = (lck->delete_token ?
631 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
633 result.dsize = sizeof(*data) +
634 lck->num_share_modes * sizeof(struct share_mode_entry) +
637 strlen(lck->filename) + 1;
638 result.dptr = talloc_size(lck, result.dsize);
640 if (result.dptr == NULL) {
641 smb_panic("talloc failed\n");
644 data = (struct locking_data *)result.dptr;
646 data->u.s.num_share_mode_entries = lck->num_share_modes;
647 data->u.s.delete_on_close = lck->delete_on_close;
648 data->u.s.initial_delete_on_close = lck->initial_delete_on_close;
649 data->u.s.delete_token_size = delete_token_size;
650 DEBUG(10, ("unparse_share_modes: del: %d, initial del %d, tok = %u, num: %d\n",
651 data->u.s.delete_on_close,
652 data->u.s.initial_delete_on_close,
653 (unsigned int)data->u.s.delete_token_size,
654 data->u.s.num_share_mode_entries));
655 memcpy(result.dptr + sizeof(*data), lck->share_modes,
656 sizeof(struct share_mode_entry)*lck->num_share_modes);
657 offset = sizeof(*data) +
658 sizeof(struct share_mode_entry)*lck->num_share_modes;
660 /* Store any delete on close token. */
661 if (lck->delete_token) {
662 char *p = result.dptr + offset;
664 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
667 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
669 for (i = 0; i < lck->delete_token->ngroups; i++) {
670 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
673 offset = p - result.dptr;
676 safe_strcpy(result.dptr + offset, lck->servicepath,
677 result.dsize - offset - 1);
678 offset += sp_len + 1;
679 safe_strcpy(result.dptr + offset, lck->filename,
680 result.dsize - offset - 1);
682 if (DEBUGLEVEL >= 10) {
683 print_share_mode_table(data);
689 static int share_mode_lock_destructor(void *p)
691 struct share_mode_lock *lck =
692 talloc_get_type_abort(p, struct share_mode_lock);
693 TDB_DATA key = locking_key(lck->dev, lck->ino);
696 if (!lck->modified) {
700 data = unparse_share_modes(lck);
702 if (data.dptr == NULL) {
704 /* There has been an entry before, delete it */
705 if (tdb_delete(tdb, key) == -1) {
706 smb_panic("Could not delete share entry\n");
712 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
713 smb_panic("Could not store share mode entry\n");
717 tdb_chainunlock(tdb, key);
722 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
723 SMB_DEV_T dev, SMB_INO_T ino,
724 const char *servicepath,
727 struct share_mode_lock *lck;
728 TDB_DATA key = locking_key(dev, ino);
731 lck = TALLOC_P(mem_ctx, struct share_mode_lock);
733 DEBUG(0, ("talloc failed\n"));
737 /* Ensure we set every field here as the destructor must be
738 valid even if parse_share_modes fails. */
740 lck->servicepath = NULL;
741 lck->filename = NULL;
744 lck->num_share_modes = 0;
745 lck->share_modes = NULL;
746 lck->delete_token = NULL;
747 lck->delete_on_close = False;
748 lck->initial_delete_on_close = False;
750 lck->modified = False;
752 if (tdb_chainlock(tdb, key) != 0) {
753 DEBUG(3, ("Could not lock share entry\n"));
758 /* We must set the destructor immediately after the chainlock
759 ensure the lock is cleaned up on any of the error return
762 talloc_set_destructor(lck, share_mode_lock_destructor);
764 data = tdb_fetch(tdb, key);
765 lck->fresh = (data.dptr == NULL);
769 if (fname == NULL || servicepath == NULL) {
773 lck->filename = talloc_strdup(lck, fname);
774 lck->servicepath = talloc_strdup(lck, servicepath);
775 if (lck->filename == NULL || lck->servicepath == NULL) {
776 DEBUG(0, ("talloc failed\n"));
781 if (!parse_share_modes(data, lck)) {
782 DEBUG(0, ("Could not parse share modes\n"));
784 SAFE_FREE(data.dptr);
789 SAFE_FREE(data.dptr);
794 /*******************************************************************
795 Sets the service name and filename for rename.
796 At this point we emit "file renamed" messages to all
797 process id's that have this file open.
798 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
799 ********************************************************************/
801 BOOL rename_share_filename(struct share_mode_lock *lck,
802 const char *servicepath,
815 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
816 servicepath, newname));
819 * rename_internal_fsp() and rename_internals() add './' to
820 * head of newname if newname does not contain a '/'.
822 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
826 lck->servicepath = talloc_strdup(lck, servicepath);
827 lck->filename = talloc_strdup(lck, newname);
828 if (lck->filename == NULL || lck->servicepath == NULL) {
829 DEBUG(0, ("rename_share_filename: talloc failed\n"));
832 lck->modified = True;
834 sp_len = strlen(lck->servicepath);
835 fn_len = strlen(lck->filename);
837 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
839 /* Set up the name changed message. */
840 frm = TALLOC(lck, msg_len);
845 SDEV_T_VAL(frm,0,lck->dev);
846 SINO_T_VAL(frm,8,lck->ino);
848 DEBUG(10,("rename_share_filename: msg_len = %d\n", msg_len ));
850 safe_strcpy(&frm[16], lck->servicepath, sp_len);
851 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
853 /* Send the messages. */
854 for (i=0; i<lck->num_share_modes; i++) {
855 struct share_mode_entry *se = &lck->share_modes[i];
856 if (!is_valid_share_mode_entry(se)) {
859 /* But not to ourselves... */
860 if (procid_is_me(&se->pid)) {
864 DEBUG(10,("rename_share_filename: sending rename message to pid %u "
865 "dev %x, inode %.0f sharepath %s newname %s\n",
866 (unsigned int)procid_to_pid(&se->pid),
867 (unsigned int)lck->dev, (double)lck->ino,
868 lck->servicepath, lck->filename ));
871 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
879 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
882 struct share_mode_lock *lck = get_share_mode_lock(NULL, dev, inode, NULL, NULL);
886 result = lck->delete_on_close;
891 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
895 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
896 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
897 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
899 SMB_ASSERT(num_props <= 1);
900 return (num_props != 0);
903 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
905 return (e->op_type == DEFERRED_OPEN_ENTRY);
908 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
910 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
913 /*******************************************************************
914 Fill a share mode entry.
915 ********************************************************************/
917 static void fill_share_mode_entry(struct share_mode_entry *e,
919 uint16 mid, uint16 op_type)
922 e->pid = procid_self();
923 e->share_access = fsp->share_access;
924 e->private_options = fsp->fh->private_options;
925 e->access_mask = fsp->access_mask;
927 e->op_type = op_type;
928 e->time.tv_sec = fsp->open_time.tv_sec;
929 e->time.tv_usec = fsp->open_time.tv_usec;
930 e->share_file_id = fsp->file_id;
932 e->inode = fsp->inode;
935 static void fill_deferred_open_entry(struct share_mode_entry *e,
936 const struct timeval request_time,
937 SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
940 e->pid = procid_self();
942 e->op_type = DEFERRED_OPEN_ENTRY;
943 e->time.tv_sec = request_time.tv_sec;
944 e->time.tv_usec = request_time.tv_usec;
949 static void add_share_mode_entry(struct share_mode_lock *lck,
950 const struct share_mode_entry *entry)
954 for (i=0; i<lck->num_share_modes; i++) {
955 struct share_mode_entry *e = &lck->share_modes[i];
956 if (is_unused_share_mode_entry(e)) {
962 if (i == lck->num_share_modes) {
963 /* No unused entry found */
964 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
965 &lck->share_modes, &lck->num_share_modes);
967 lck->modified = True;
970 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
971 uint16 mid, uint16 op_type)
973 struct share_mode_entry entry;
974 fill_share_mode_entry(&entry, fsp, mid, op_type);
975 add_share_mode_entry(lck, &entry);
978 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
979 struct timeval request_time,
980 SMB_DEV_T dev, SMB_INO_T ino)
982 struct share_mode_entry entry;
983 fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
984 add_share_mode_entry(lck, &entry);
987 /*******************************************************************
988 Check if two share mode entries are identical, ignoring oplock
989 and mid info and desired_access.
990 ********************************************************************/
992 static BOOL share_modes_identical(struct share_mode_entry *e1,
993 struct share_mode_entry *e2)
995 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
996 if (procid_equal(&e1->pid, &e2->pid) &&
997 e1->share_file_id == e2->share_file_id &&
998 e1->dev == e2->dev &&
999 e1->inode == e2->inode &&
1000 (e1->share_access) != (e2->share_access)) {
1001 DEBUG(0,("PANIC: share_modes_identical: share_mode "
1002 "mismatch (e1 = 0x%x, e2 = 0x%x). Logic error.\n",
1003 (unsigned int)e1->share_access,
1004 (unsigned int)e2->share_access ));
1005 smb_panic("PANIC: share_modes_identical logic error.\n");
1009 return (procid_equal(&e1->pid, &e2->pid) &&
1010 (e1->share_access) == (e2->share_access) &&
1011 e1->dev == e2->dev &&
1012 e1->inode == e2->inode &&
1013 e1->share_file_id == e2->share_file_id );
1016 static BOOL deferred_open_identical(struct share_mode_entry *e1,
1017 struct share_mode_entry *e2)
1019 return (procid_equal(&e1->pid, &e2->pid) &&
1020 (e1->op_mid == e2->op_mid) &&
1021 (e1->dev == e2->dev) &&
1022 (e1->inode == e2->inode));
1025 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1026 struct share_mode_entry *entry)
1030 for (i=0; i<lck->num_share_modes; i++) {
1031 struct share_mode_entry *e = &lck->share_modes[i];
1032 if (is_valid_share_mode_entry(entry) &&
1033 is_valid_share_mode_entry(e) &&
1034 share_modes_identical(e, entry)) {
1037 if (is_deferred_open_entry(entry) &&
1038 is_deferred_open_entry(e) &&
1039 deferred_open_identical(e, entry)) {
1046 /*******************************************************************
1047 Del the share mode of a file for this process. Return the number of
1049 ********************************************************************/
1051 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1053 struct share_mode_entry entry, *e;
1055 fill_share_mode_entry(&entry, fsp, 0, NO_OPLOCK);
1057 e = find_share_mode_entry(lck, &entry);
1062 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1063 lck->modified = True;
1067 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1069 struct share_mode_entry entry, *e;
1071 fill_deferred_open_entry(&entry, timeval_zero(),
1072 lck->dev, lck->ino, mid);
1074 e = find_share_mode_entry(lck, &entry);
1079 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1080 lck->modified = True;
1083 /*******************************************************************
1084 Remove an oplock mid and mode entry from a share mode.
1085 ********************************************************************/
1087 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1089 struct share_mode_entry entry, *e;
1091 fill_share_mode_entry(&entry, fsp, 0, NO_OPLOCK);
1093 e = find_share_mode_entry(lck, &entry);
1099 e->op_type = NO_OPLOCK;
1100 lck->modified = True;
1104 /*******************************************************************
1105 Downgrade a oplock type from exclusive to level II.
1106 ********************************************************************/
1108 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1110 struct share_mode_entry entry, *e;
1112 fill_share_mode_entry(&entry, fsp, 0, NO_OPLOCK);
1114 e = find_share_mode_entry(lck, &entry);
1119 e->op_type = LEVEL_II_OPLOCK;
1120 lck->modified = True;
1124 /****************************************************************************
1125 Deal with the internal needs of setting the delete on close flag. Note that
1126 as the tdb locking is recursive, it is safe to call this from within
1127 open_file_shared. JRA.
1128 ****************************************************************************/
1130 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1133 if (!delete_on_close) {
1134 return NT_STATUS_OK;
1138 * Only allow delete on close for writable files.
1141 if ((dosmode & aRONLY) &&
1142 !lp_delete_readonly(SNUM(fsp->conn))) {
1143 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1144 "flag set but file attribute is readonly.\n",
1146 return NT_STATUS_CANNOT_DELETE;
1150 * Only allow delete on close for writable shares.
1153 if (!CAN_WRITE(fsp->conn)) {
1154 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1155 "close flag set but write access denied on share.\n",
1157 return NT_STATUS_ACCESS_DENIED;
1161 * Only allow delete on close for files/directories opened with delete
1165 if (!(fsp->access_mask & DELETE_ACCESS)) {
1166 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1167 "close flag set but delete access denied.\n",
1169 return NT_STATUS_ACCESS_DENIED;
1172 return NT_STATUS_OK;
1175 /*************************************************************************
1176 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1177 (Should this be in locking.c.... ?).
1178 *************************************************************************/
1180 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1182 UNIX_USER_TOKEN *cpy;
1188 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1193 cpy->uid = tok->uid;
1194 cpy->gid = tok->gid;
1195 cpy->ngroups = tok->ngroups;
1197 /* Make this a talloc child of cpy. */
1198 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1202 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1207 /****************************************************************************
1208 Replace the delete on close token.
1209 ****************************************************************************/
1211 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1213 /* Ensure there's no token. */
1214 if (lck->delete_token) {
1215 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1216 lck->delete_token = NULL;
1219 /* Copy the new token (can be NULL). */
1220 lck->delete_token = copy_unix_token(lck, tok);
1221 lck->modified = True;
1224 /****************************************************************************
1225 Sets the delete on close flag over all share modes on this file.
1226 Modify the share mode entry for all files open
1227 on this device and inode to tell other smbds we have
1228 changed the delete on close flag. This will be noticed
1229 in the close code, the last closer will delete the file
1231 Note that setting this to any value clears the initial_delete_on_close flag.
1232 If delete_on_close is True this makes a copy of any UNIX_USER_TOKEN into the
1234 ****************************************************************************/
1236 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1238 struct share_mode_lock *lck;
1240 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1241 "fnum = %d, file %s\n",
1242 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1249 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1254 if (lck->delete_on_close != delete_on_close) {
1255 set_delete_on_close_token(lck, tok);
1256 lck->delete_on_close = delete_on_close;
1257 if (delete_on_close) {
1258 SMB_ASSERT(lck->delete_token != NULL);
1260 lck->modified = True;
1263 if (lck->initial_delete_on_close) {
1264 lck->initial_delete_on_close = False;
1265 lck->modified = True;
1272 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1275 struct locking_data *data;
1276 struct share_mode_entry *shares;
1277 const char *sharepath;
1280 void (*traverse_callback)(struct share_mode_entry *, const char *, const char *) = state;
1282 /* Ensure this is a locking_key record. */
1283 if (kbuf.dsize != sizeof(struct locking_key))
1286 data = (struct locking_data *)dbuf.dptr;
1287 shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1288 sharepath = dbuf.dptr + sizeof(*data) +
1289 data->u.s.num_share_mode_entries*sizeof(*shares) +
1290 data->u.s.delete_token_size;
1291 fname = dbuf.dptr + sizeof(*data) +
1292 data->u.s.num_share_mode_entries*sizeof(*shares) +
1293 data->u.s.delete_token_size +
1294 strlen(sharepath) + 1;
1296 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1297 traverse_callback(&shares[i], sharepath, fname);
1302 /*******************************************************************
1303 Call the specified function on each entry under management by the
1305 ********************************************************************/
1307 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *, const char *))
1311 return tdb_traverse(tdb, traverse_fn, fn);