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.
42 #define DBGC_CLASS DBGC_LOCKING
44 /* the locking database handle */
45 static TDB_CONTEXT *tdb;
47 /****************************************************************************
49 ****************************************************************************/
51 const char *lock_type_name(enum brl_type lock_type)
65 const char *lock_flav_name(enum brl_flavour lock_flav)
67 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
70 /****************************************************************************
71 Utility function called to see if a file region is locked.
72 Called in the read/write codepath.
73 ****************************************************************************/
75 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 (!fsp->can_lock) {
154 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : 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 struct byte_range_lock *do_lock(files_struct *fsp,
186 enum brl_type lock_type,
187 enum brl_flavour lock_flav,
191 struct byte_range_lock *br_lck = NULL;
193 if (!fsp->can_lock) {
194 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
198 if (!lp_locking(SNUM(fsp->conn))) {
199 *perr = NT_STATUS_OK;
203 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
205 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
206 lock_flav_name(lock_flav), lock_type_name(lock_type),
207 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
209 br_lck = brl_get_locks(NULL, fsp);
211 *perr = NT_STATUS_NO_MEMORY;
215 *perr = brl_lock(br_lck,
227 /****************************************************************************
228 Utility function called by unlocking requests.
229 ****************************************************************************/
231 NTSTATUS do_unlock(files_struct *fsp,
235 enum brl_flavour lock_flav)
238 struct byte_range_lock *br_lck = NULL;
240 if (!fsp->can_lock) {
241 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
244 if (!lp_locking(SNUM(fsp->conn))) {
248 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
249 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
251 br_lck = brl_get_locks(NULL, fsp);
253 return NT_STATUS_NO_MEMORY;
256 ok = brl_unlock(br_lck,
266 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
267 return NT_STATUS_RANGE_NOT_LOCKED;
273 /****************************************************************************
274 Cancel any pending blocked locks.
275 ****************************************************************************/
277 NTSTATUS do_lock_cancel(files_struct *fsp,
281 enum brl_flavour lock_flav)
284 struct byte_range_lock *br_lck = NULL;
286 if (!fsp->can_lock) {
287 return fsp->is_directory ?
288 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
291 if (!lp_locking(SNUM(fsp->conn))) {
292 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
295 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
296 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
298 br_lck = brl_get_locks(NULL, fsp);
300 return NT_STATUS_NO_MEMORY;
303 ok = brl_lock_cancel(br_lck,
313 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
314 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
320 /****************************************************************************
321 Remove any locks on this fd. Called from file_close().
322 ****************************************************************************/
324 void locking_close_file(files_struct *fsp)
326 struct byte_range_lock *br_lck;
328 if (!lp_locking(SNUM(fsp->conn))) {
332 br_lck = brl_get_locks(NULL,fsp);
335 cancel_pending_lock_requests_by_fid(fsp, br_lck);
336 brl_close_fnum(br_lck);
341 /****************************************************************************
342 Initialise the locking functions.
343 ****************************************************************************/
345 static int open_read_only;
347 BOOL locking_init(int read_only)
354 tdb = tdb_open_log(lock_path("locking.tdb"),
355 lp_open_files_db_hash_size(),
356 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
357 read_only?O_RDONLY:O_RDWR|O_CREAT,
361 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
365 if (!posix_locking_init(read_only))
368 open_read_only = read_only;
373 /*******************************************************************
374 Deinitialize the share_mode management.
375 ******************************************************************/
377 BOOL locking_end(void)
381 brl_shutdown(open_read_only);
383 if (tdb_close(tdb) != 0)
390 /*******************************************************************
391 Form a static locking key for a dev/inode pair.
392 ******************************************************************/
394 /* key and data records in the tdb locking database */
400 /*******************************************************************
401 Form a static locking key for a dev/inode pair.
402 ******************************************************************/
404 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
406 static struct locking_key key;
409 memset(&key, '\0', sizeof(key));
412 kbuf.dptr = (char *)&key;
413 kbuf.dsize = sizeof(key);
417 /*******************************************************************
418 Print out a share mode.
419 ********************************************************************/
421 char *share_mode_str(int num, struct share_mode_entry *e)
423 static pstring share_str;
425 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
426 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
427 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
428 "uid = %u, dev = 0x%x, inode = %.0f",
430 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
431 procid_str_static(&e->pid),
432 e->share_access, e->private_options,
433 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
434 (unsigned int)e->uid, (unsigned int)e->dev, (double)e->inode );
439 /*******************************************************************
440 Print out a share mode table.
441 ********************************************************************/
443 static void print_share_mode_table(struct locking_data *data)
445 int num_share_modes = data->u.s.num_share_mode_entries;
446 struct share_mode_entry *shares =
447 (struct share_mode_entry *)(data + 1);
450 for (i = 0; i < num_share_modes; i++) {
451 struct share_mode_entry entry;
453 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
454 DEBUG(10,("print_share_mode_table: %s\n",
455 share_mode_str(i, &entry)));
459 /*******************************************************************
460 Get all share mode entries for a dev/inode pair.
461 ********************************************************************/
463 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
465 struct locking_data *data;
468 if (dbuf.dsize < sizeof(struct locking_data)) {
469 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
472 data = (struct locking_data *)dbuf.dptr;
474 lck->delete_on_close = data->u.s.delete_on_close;
475 lck->initial_delete_on_close = data->u.s.initial_delete_on_close;
476 lck->num_share_modes = data->u.s.num_share_mode_entries;
478 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
479 "initial_delete_on_close: %d, "
480 "num_share_modes: %d\n",
481 lck->delete_on_close,
482 lck->initial_delete_on_close,
483 lck->num_share_modes));
485 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
486 DEBUG(0, ("invalid number of share modes: %d\n",
487 lck->num_share_modes));
488 smb_panic("PANIC: invalid number of share modes");
491 lck->share_modes = NULL;
493 if (lck->num_share_modes != 0) {
495 if (dbuf.dsize < (sizeof(struct locking_data) +
496 (lck->num_share_modes *
497 sizeof(struct share_mode_entry)))) {
498 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
501 lck->share_modes = (struct share_mode_entry *)
502 talloc_memdup(lck, dbuf.dptr+sizeof(*data),
503 lck->num_share_modes *
504 sizeof(struct share_mode_entry));
506 if (lck->share_modes == NULL) {
507 smb_panic("talloc failed\n");
511 /* Get any delete token. */
512 if (data->u.s.delete_token_size) {
513 char *p = dbuf.dptr + sizeof(*data) +
514 (lck->num_share_modes *
515 sizeof(struct share_mode_entry));
517 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
518 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
519 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
520 data->u.s.delete_token_size));
521 smb_panic("parse_share_modes: invalid token size\n");
524 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
525 if (!lck->delete_token) {
526 smb_panic("talloc failed\n");
529 /* Copy out the uid and gid. */
530 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
532 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
535 /* Any supplementary groups ? */
536 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
537 ((data->u.s.delete_token_size -
538 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
540 if (lck->delete_token->ngroups) {
541 /* Make this a talloc child of lck->delete_token. */
542 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
543 lck->delete_token->ngroups);
544 if (!lck->delete_token) {
545 smb_panic("talloc failed\n");
548 for (i = 0; i < lck->delete_token->ngroups; i++) {
549 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
555 lck->delete_token = NULL;
558 /* Save off the associated service path and filename. */
559 lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
560 (lck->num_share_modes *
561 sizeof(struct share_mode_entry)) +
562 data->u.s.delete_token_size );
564 lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
565 (lck->num_share_modes *
566 sizeof(struct share_mode_entry)) +
567 data->u.s.delete_token_size +
568 strlen(lck->servicepath) + 1 );
571 * Ensure that each entry has a real process attached.
574 for (i = 0; i < lck->num_share_modes; i++) {
575 struct share_mode_entry *entry_p = &lck->share_modes[i];
576 DEBUG(10,("parse_share_modes: %s\n",
577 share_mode_str(i, entry_p) ));
578 if (!process_exists(entry_p->pid)) {
579 DEBUG(10,("parse_share_modes: deleted %s\n",
580 share_mode_str(i, entry_p) ));
581 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
582 lck->modified = True;
589 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
594 struct locking_data *data;
597 uint32 delete_token_size;
602 for (i=0; i<lck->num_share_modes; i++) {
603 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
608 if (num_valid == 0) {
612 sp_len = strlen(lck->servicepath);
613 delete_token_size = (lck->delete_token ?
614 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
616 result.dsize = sizeof(*data) +
617 lck->num_share_modes * sizeof(struct share_mode_entry) +
620 strlen(lck->filename) + 1;
621 result.dptr = TALLOC_ARRAY(lck, char, result.dsize);
623 if (result.dptr == NULL) {
624 smb_panic("talloc failed\n");
627 data = (struct locking_data *)result.dptr;
629 data->u.s.num_share_mode_entries = lck->num_share_modes;
630 data->u.s.delete_on_close = lck->delete_on_close;
631 data->u.s.initial_delete_on_close = lck->initial_delete_on_close;
632 data->u.s.delete_token_size = delete_token_size;
633 DEBUG(10, ("unparse_share_modes: del: %d, initial del %d, tok = %u, num: %d\n",
634 data->u.s.delete_on_close,
635 data->u.s.initial_delete_on_close,
636 (unsigned int)data->u.s.delete_token_size,
637 data->u.s.num_share_mode_entries));
638 memcpy(result.dptr + sizeof(*data), lck->share_modes,
639 sizeof(struct share_mode_entry)*lck->num_share_modes);
640 offset = sizeof(*data) +
641 sizeof(struct share_mode_entry)*lck->num_share_modes;
643 /* Store any delete on close token. */
644 if (lck->delete_token) {
645 char *p = result.dptr + offset;
647 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
650 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
653 for (i = 0; i < lck->delete_token->ngroups; i++) {
654 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
657 offset = p - result.dptr;
660 safe_strcpy(result.dptr + offset, lck->servicepath,
661 result.dsize - offset - 1);
662 offset += sp_len + 1;
663 safe_strcpy(result.dptr + offset, lck->filename,
664 result.dsize - offset - 1);
666 if (DEBUGLEVEL >= 10) {
667 print_share_mode_table(data);
673 static int share_mode_lock_destructor(void *p)
675 struct share_mode_lock *lck =
676 talloc_get_type_abort(p, struct share_mode_lock);
677 TDB_DATA key = locking_key(lck->dev, lck->ino);
680 if (!lck->modified) {
684 data = unparse_share_modes(lck);
686 if (data.dptr == NULL) {
688 /* There has been an entry before, delete it */
689 if (tdb_delete(tdb, key) == -1) {
690 smb_panic("Could not delete share entry\n");
696 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
697 smb_panic("Could not store share mode entry\n");
701 tdb_chainunlock(tdb, key);
706 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
707 SMB_DEV_T dev, SMB_INO_T ino,
708 const char *servicepath,
711 struct share_mode_lock *lck;
712 TDB_DATA key = locking_key(dev, ino);
715 lck = TALLOC_P(mem_ctx, struct share_mode_lock);
717 DEBUG(0, ("talloc failed\n"));
721 /* Ensure we set every field here as the destructor must be
722 valid even if parse_share_modes fails. */
724 lck->servicepath = NULL;
725 lck->filename = NULL;
728 lck->num_share_modes = 0;
729 lck->share_modes = NULL;
730 lck->delete_token = NULL;
731 lck->delete_on_close = False;
732 lck->initial_delete_on_close = False;
734 lck->modified = False;
736 if (tdb_chainlock(tdb, key) != 0) {
737 DEBUG(3, ("Could not lock share entry\n"));
742 /* We must set the destructor immediately after the chainlock
743 ensure the lock is cleaned up on any of the error return
746 talloc_set_destructor(lck, share_mode_lock_destructor);
748 data = tdb_fetch(tdb, key);
749 lck->fresh = (data.dptr == NULL);
753 if (fname == NULL || servicepath == NULL) {
757 lck->filename = talloc_strdup(lck, fname);
758 lck->servicepath = talloc_strdup(lck, servicepath);
759 if (lck->filename == NULL || lck->servicepath == NULL) {
760 DEBUG(0, ("talloc failed\n"));
765 if (!parse_share_modes(data, lck)) {
766 DEBUG(0, ("Could not parse share modes\n"));
768 SAFE_FREE(data.dptr);
773 SAFE_FREE(data.dptr);
778 /*******************************************************************
779 Sets the service name and filename for rename.
780 At this point we emit "file renamed" messages to all
781 process id's that have this file open.
782 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
783 ********************************************************************/
785 BOOL rename_share_filename(struct share_mode_lock *lck,
786 const char *servicepath,
799 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
800 servicepath, newname));
803 * rename_internal_fsp() and rename_internals() add './' to
804 * head of newname if newname does not contain a '/'.
806 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
810 lck->servicepath = talloc_strdup(lck, servicepath);
811 lck->filename = talloc_strdup(lck, newname);
812 if (lck->filename == NULL || lck->servicepath == NULL) {
813 DEBUG(0, ("rename_share_filename: talloc failed\n"));
816 lck->modified = True;
818 sp_len = strlen(lck->servicepath);
819 fn_len = strlen(lck->filename);
821 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
823 /* Set up the name changed message. */
824 frm = TALLOC_ARRAY(lck, char, msg_len);
829 SDEV_T_VAL(frm,0,lck->dev);
830 SINO_T_VAL(frm,8,lck->ino);
832 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
834 safe_strcpy(&frm[16], lck->servicepath, sp_len);
835 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
837 /* Send the messages. */
838 for (i=0; i<lck->num_share_modes; i++) {
839 struct share_mode_entry *se = &lck->share_modes[i];
840 if (!is_valid_share_mode_entry(se)) {
843 /* But not to ourselves... */
844 if (procid_is_me(&se->pid)) {
848 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
849 "dev %x, inode %.0f sharepath %s newname %s\n",
850 procid_str_static(&se->pid),
851 (unsigned int)lck->dev, (double)lck->ino,
852 lck->servicepath, lck->filename ));
854 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
861 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
864 struct share_mode_lock *lck = get_share_mode_lock(NULL, dev, inode, NULL, NULL);
868 result = lck->delete_on_close;
873 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
877 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
878 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
879 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
881 SMB_ASSERT(num_props <= 1);
882 return (num_props != 0);
885 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
887 return (e->op_type == DEFERRED_OPEN_ENTRY);
890 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
892 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
895 /*******************************************************************
896 Fill a share mode entry.
897 ********************************************************************/
899 static void fill_share_mode_entry(struct share_mode_entry *e,
901 uid_t uid, uint16 mid, uint16 op_type)
904 e->pid = procid_self();
905 e->share_access = fsp->share_access;
906 e->private_options = fsp->fh->private_options;
907 e->access_mask = fsp->access_mask;
909 e->op_type = op_type;
910 e->time.tv_sec = fsp->open_time.tv_sec;
911 e->time.tv_usec = fsp->open_time.tv_usec;
913 e->inode = fsp->inode;
914 e->share_file_id = fsp->fh->file_id;
915 e->uid = (uint32)uid;
918 static void fill_deferred_open_entry(struct share_mode_entry *e,
919 const struct timeval request_time,
920 SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
923 e->pid = procid_self();
925 e->op_type = DEFERRED_OPEN_ENTRY;
926 e->time.tv_sec = request_time.tv_sec;
927 e->time.tv_usec = request_time.tv_usec;
933 static void add_share_mode_entry(struct share_mode_lock *lck,
934 const struct share_mode_entry *entry)
938 for (i=0; i<lck->num_share_modes; i++) {
939 struct share_mode_entry *e = &lck->share_modes[i];
940 if (is_unused_share_mode_entry(e)) {
946 if (i == lck->num_share_modes) {
947 /* No unused entry found */
948 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
949 &lck->share_modes, &lck->num_share_modes);
951 lck->modified = True;
954 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
955 uid_t uid, uint16 mid, uint16 op_type)
957 struct share_mode_entry entry;
958 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
959 add_share_mode_entry(lck, &entry);
962 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
963 struct timeval request_time,
964 SMB_DEV_T dev, SMB_INO_T ino)
966 struct share_mode_entry entry;
967 fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
968 add_share_mode_entry(lck, &entry);
971 /*******************************************************************
972 Check if two share mode entries are identical, ignoring oplock
973 and mid info and desired_access. (Removed paranoia test - it's
974 not automatically a logic error if they are identical. JRA.)
975 ********************************************************************/
977 static BOOL share_modes_identical(struct share_mode_entry *e1,
978 struct share_mode_entry *e2)
980 /* We used to check for e1->share_access == e2->share_access here
981 as well as the other fields but 2 different DOS or FCB opens
982 sharing the same share mode entry may validly differ in
983 fsp->share_access field. */
985 return (procid_equal(&e1->pid, &e2->pid) &&
986 e1->dev == e2->dev &&
987 e1->inode == e2->inode &&
988 e1->share_file_id == e2->share_file_id );
991 static BOOL deferred_open_identical(struct share_mode_entry *e1,
992 struct share_mode_entry *e2)
994 return (procid_equal(&e1->pid, &e2->pid) &&
995 (e1->op_mid == e2->op_mid) &&
996 (e1->dev == e2->dev) &&
997 (e1->inode == e2->inode));
1000 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1001 struct share_mode_entry *entry)
1005 for (i=0; i<lck->num_share_modes; i++) {
1006 struct share_mode_entry *e = &lck->share_modes[i];
1007 if (is_valid_share_mode_entry(entry) &&
1008 is_valid_share_mode_entry(e) &&
1009 share_modes_identical(e, entry)) {
1012 if (is_deferred_open_entry(entry) &&
1013 is_deferred_open_entry(e) &&
1014 deferred_open_identical(e, entry)) {
1021 /*******************************************************************
1022 Del the share mode of a file for this process. Return the number of
1024 ********************************************************************/
1026 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1028 struct share_mode_entry entry, *e;
1030 /* Don't care about the pid owner being correct here - just a search. */
1031 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1033 e = find_share_mode_entry(lck, &entry);
1038 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1039 lck->modified = True;
1043 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1045 struct share_mode_entry entry, *e;
1047 fill_deferred_open_entry(&entry, timeval_zero(),
1048 lck->dev, lck->ino, mid);
1050 e = find_share_mode_entry(lck, &entry);
1055 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1056 lck->modified = True;
1059 /*******************************************************************
1060 Remove an oplock mid and mode entry from a share mode.
1061 ********************************************************************/
1063 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1065 struct share_mode_entry entry, *e;
1067 /* Don't care about the pid owner being correct here - just a search. */
1068 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1070 e = find_share_mode_entry(lck, &entry);
1076 e->op_type = NO_OPLOCK;
1077 lck->modified = True;
1081 /*******************************************************************
1082 Downgrade a oplock type from exclusive to level II.
1083 ********************************************************************/
1085 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1087 struct share_mode_entry entry, *e;
1089 /* Don't care about the pid owner being correct here - just a search. */
1090 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1092 e = find_share_mode_entry(lck, &entry);
1097 e->op_type = LEVEL_II_OPLOCK;
1098 lck->modified = True;
1102 /****************************************************************************
1103 Deal with the internal needs of setting the delete on close flag. Note that
1104 as the tdb locking is recursive, it is safe to call this from within
1105 open_file_ntcreate. JRA.
1106 ****************************************************************************/
1108 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1111 if (!delete_on_close) {
1112 return NT_STATUS_OK;
1116 * Only allow delete on close for writable files.
1119 if ((dosmode & aRONLY) &&
1120 !lp_delete_readonly(SNUM(fsp->conn))) {
1121 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1122 "flag set but file attribute is readonly.\n",
1124 return NT_STATUS_CANNOT_DELETE;
1128 * Only allow delete on close for writable shares.
1131 if (!CAN_WRITE(fsp->conn)) {
1132 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1133 "close flag set but write access denied on share.\n",
1135 return NT_STATUS_ACCESS_DENIED;
1139 * Only allow delete on close for files/directories opened with delete
1143 if (!(fsp->access_mask & DELETE_ACCESS)) {
1144 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1145 "close flag set but delete access denied.\n",
1147 return NT_STATUS_ACCESS_DENIED;
1150 return NT_STATUS_OK;
1153 /*************************************************************************
1154 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1155 (Should this be in locking.c.... ?).
1156 *************************************************************************/
1158 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1160 UNIX_USER_TOKEN *cpy;
1166 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1171 cpy->uid = tok->uid;
1172 cpy->gid = tok->gid;
1173 cpy->ngroups = tok->ngroups;
1175 /* Make this a talloc child of cpy. */
1176 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1180 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1185 /****************************************************************************
1186 Replace the delete on close token.
1187 ****************************************************************************/
1189 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1191 /* Ensure there's no token. */
1192 if (lck->delete_token) {
1193 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1194 lck->delete_token = NULL;
1197 /* Copy the new token (can be NULL). */
1198 lck->delete_token = copy_unix_token(lck, tok);
1199 lck->modified = True;
1202 /****************************************************************************
1203 Sets the delete on close flag over all share modes on this file.
1204 Modify the share mode entry for all files open
1205 on this device and inode to tell other smbds we have
1206 changed the delete on close flag. This will be noticed
1207 in the close code, the last closer will delete the file
1209 Note that setting this to any value clears the initial_delete_on_close flag.
1210 If delete_on_close is True this makes a copy of any UNIX_USER_TOKEN into the
1212 ****************************************************************************/
1214 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1216 struct share_mode_lock *lck;
1218 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1219 "fnum = %d, file %s\n",
1220 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1227 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1232 if (lck->delete_on_close != delete_on_close) {
1233 set_delete_on_close_token(lck, tok);
1234 lck->delete_on_close = delete_on_close;
1235 if (delete_on_close) {
1236 SMB_ASSERT(lck->delete_token != NULL);
1238 lck->modified = True;
1241 if (lck->initial_delete_on_close) {
1242 lck->initial_delete_on_close = False;
1243 lck->modified = True;
1250 struct forall_state {
1251 void (*fn)(const struct share_mode_entry *entry,
1252 const char *sharepath,
1254 void *private_data);
1258 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1261 struct forall_state *state = (struct forall_state *)_state;
1262 struct locking_data *data;
1263 struct share_mode_entry *shares;
1264 const char *sharepath;
1268 /* Ensure this is a locking_key record. */
1269 if (kbuf.dsize != sizeof(struct locking_key))
1272 data = (struct locking_data *)dbuf.dptr;
1273 shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1274 sharepath = dbuf.dptr + sizeof(*data) +
1275 data->u.s.num_share_mode_entries*sizeof(*shares) +
1276 data->u.s.delete_token_size;
1277 fname = dbuf.dptr + sizeof(*data) +
1278 data->u.s.num_share_mode_entries*sizeof(*shares) +
1279 data->u.s.delete_token_size +
1280 strlen(sharepath) + 1;
1282 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1283 state->fn(&shares[i], sharepath, fname,
1284 state->private_data);
1289 /*******************************************************************
1290 Call the specified function on each entry under management by the
1292 ********************************************************************/
1294 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1295 const char *, void *),
1298 struct forall_state state;
1304 state.private_data = private_data;
1306 return tdb_traverse(tdb, traverse_fn, (void *)&state);