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)
58 case PENDING_READ_LOCK:
59 return "PENDING_READ";
60 case PENDING_WRITE_LOCK:
61 return "PENDING_WRITE";
67 const char *lock_flav_name(enum brl_flavour lock_flav)
69 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
72 /****************************************************************************
73 Utility function called to see if a file region is locked.
74 Called in the read/write codepath.
75 ****************************************************************************/
77 BOOL is_locked(files_struct *fsp,
81 enum brl_type lock_type)
83 int strict_locking = lp_strict_locking(fsp->conn->params);
84 enum brl_flavour lock_flav = lp_posix_cifsu_locktype(fsp);
91 if (!lp_locking(fsp->conn->params) || !strict_locking) {
95 if (strict_locking == Auto) {
96 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
97 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
99 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
100 (lock_type == READ_LOCK)) {
101 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
104 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
108 ret = !brl_locktest(br_lck,
118 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
122 ret = !brl_locktest(br_lck,
132 DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
133 lock_flav_name(lock_flav),
134 (double)offset, (double)count, ret ? "locked" : "unlocked",
135 fsp->fnum, fsp->fsp_name ));
140 /****************************************************************************
141 Find out if a lock could be granted - return who is blocking us if we can't.
142 ****************************************************************************/
144 NTSTATUS query_lock(files_struct *fsp,
146 SMB_BIG_UINT *pcount,
147 SMB_BIG_UINT *poffset,
148 enum brl_type *plock_type,
149 enum brl_flavour lock_flav)
151 struct byte_range_lock *br_lck = NULL;
152 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
154 if (!fsp->can_lock) {
155 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
158 if (!lp_locking(fsp->conn->params)) {
162 br_lck = brl_get_locks_readonly(NULL, fsp);
164 return NT_STATUS_NO_MEMORY;
167 status = brl_lockquery(br_lck,
179 /****************************************************************************
180 Utility function called by locking requests.
181 ****************************************************************************/
183 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
188 enum brl_type lock_type,
189 enum brl_flavour lock_flav,
193 struct byte_range_lock *br_lck = NULL;
195 if (!fsp->can_lock) {
196 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
200 if (!lp_locking(fsp->conn->params)) {
201 *perr = NT_STATUS_OK;
205 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
207 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
208 lock_flav_name(lock_flav), lock_type_name(lock_type),
209 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
211 br_lck = brl_get_locks(NULL, fsp);
213 *perr = NT_STATUS_NO_MEMORY;
217 *perr = brl_lock(msg_ctx,
227 /* blocking ie. pending, locks also count here,
228 * as this is an efficiency counter to avoid checking
229 * the lock db. on close. JRA. */
231 fsp->current_lock_count++;
236 /****************************************************************************
237 Utility function called by unlocking requests.
238 ****************************************************************************/
240 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
245 enum brl_flavour lock_flav)
248 struct byte_range_lock *br_lck = NULL;
250 if (!fsp->can_lock) {
251 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
254 if (!lp_locking(fsp->conn->params)) {
258 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
259 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
261 br_lck = brl_get_locks(NULL, fsp);
263 return NT_STATUS_NO_MEMORY;
266 ok = brl_unlock(msg_ctx,
277 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
278 return NT_STATUS_RANGE_NOT_LOCKED;
281 SMB_ASSERT(fsp->current_lock_count > 0);
282 fsp->current_lock_count--;
287 /****************************************************************************
288 Cancel any pending blocked locks.
289 ****************************************************************************/
291 NTSTATUS do_lock_cancel(files_struct *fsp,
295 enum brl_flavour lock_flav)
298 struct byte_range_lock *br_lck = NULL;
300 if (!fsp->can_lock) {
301 return fsp->is_directory ?
302 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
305 if (!lp_locking(fsp->conn->params)) {
306 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
309 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
310 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
312 br_lck = brl_get_locks(NULL, fsp);
314 return NT_STATUS_NO_MEMORY;
317 ok = brl_lock_cancel(br_lck,
327 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
328 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
331 SMB_ASSERT(fsp->current_lock_count > 0);
332 fsp->current_lock_count--;
337 /****************************************************************************
338 Remove any locks on this fd. Called from file_close().
339 ****************************************************************************/
341 void locking_close_file(struct messaging_context *msg_ctx,
344 struct byte_range_lock *br_lck;
346 if (!lp_locking(fsp->conn->params)) {
350 /* If we have not outstanding locks or pending
351 * locks then we don't need to look in the lock db.
354 if (fsp->current_lock_count == 0) {
358 br_lck = brl_get_locks(NULL,fsp);
361 cancel_pending_lock_requests_by_fid(fsp, br_lck);
362 brl_close_fnum(msg_ctx, br_lck);
367 /****************************************************************************
368 Initialise the locking functions.
369 ****************************************************************************/
371 static int open_read_only;
373 BOOL locking_init(int read_only)
380 tdb = tdb_open_log(lock_path("locking.tdb"),
381 lp_open_files_db_hash_size(),
382 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
383 read_only?O_RDONLY:O_RDWR|O_CREAT,
387 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
391 /* Activate the per-hashchain freelist */
392 tdb_set_max_dead(tdb, 5);
394 if (!posix_locking_init(read_only))
397 open_read_only = read_only;
402 /*******************************************************************
403 Deinitialize the share_mode management.
404 ******************************************************************/
406 BOOL locking_end(void)
410 brl_shutdown(open_read_only);
412 if (tdb_close(tdb) != 0)
419 /*******************************************************************
420 Form a static locking key for a dev/inode pair.
421 ******************************************************************/
423 /* key and data records in the tdb locking database */
429 /*******************************************************************
430 Form a static locking key for a dev/inode pair.
431 ******************************************************************/
433 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
435 static struct locking_key key;
438 memset(&key, '\0', sizeof(key));
441 kbuf.dptr = (uint8 *)&key;
442 kbuf.dsize = sizeof(key);
446 /*******************************************************************
447 Print out a share mode.
448 ********************************************************************/
450 char *share_mode_str(int num, struct share_mode_entry *e)
452 static pstring share_str;
454 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
455 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
456 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
457 "uid = %u, flags = %u, dev = 0x%x, inode = %.0f",
459 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
460 procid_str_static(&e->pid),
461 e->share_access, e->private_options,
462 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
463 (unsigned int)e->uid, (unsigned int)e->flags,
464 (unsigned int)e->dev, (double)e->inode );
469 /*******************************************************************
470 Print out a share mode table.
471 ********************************************************************/
473 static void print_share_mode_table(struct locking_data *data)
475 int num_share_modes = data->u.s.num_share_mode_entries;
476 struct share_mode_entry *shares =
477 (struct share_mode_entry *)(data + 1);
480 for (i = 0; i < num_share_modes; i++) {
481 struct share_mode_entry entry;
483 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
484 DEBUG(10,("print_share_mode_table: %s\n",
485 share_mode_str(i, &entry)));
489 /*******************************************************************
490 Get all share mode entries for a dev/inode pair.
491 ********************************************************************/
493 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
495 struct locking_data *data;
498 if (dbuf.dsize < sizeof(struct locking_data)) {
499 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
502 data = (struct locking_data *)dbuf.dptr;
504 lck->delete_on_close = data->u.s.delete_on_close;
505 lck->num_share_modes = data->u.s.num_share_mode_entries;
507 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
508 "num_share_modes: %d\n",
509 lck->delete_on_close,
510 lck->num_share_modes));
512 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
513 DEBUG(0, ("invalid number of share modes: %d\n",
514 lck->num_share_modes));
515 smb_panic("PANIC: invalid number of share modes");
518 lck->share_modes = NULL;
520 if (lck->num_share_modes != 0) {
522 if (dbuf.dsize < (sizeof(struct locking_data) +
523 (lck->num_share_modes *
524 sizeof(struct share_mode_entry)))) {
525 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
528 lck->share_modes = (struct share_mode_entry *)
529 TALLOC_MEMDUP(lck, dbuf.dptr+sizeof(*data),
530 lck->num_share_modes *
531 sizeof(struct share_mode_entry));
533 if (lck->share_modes == NULL) {
534 smb_panic("talloc failed\n");
538 /* Get any delete token. */
539 if (data->u.s.delete_token_size) {
540 uint8 *p = dbuf.dptr + sizeof(*data) +
541 (lck->num_share_modes *
542 sizeof(struct share_mode_entry));
544 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
545 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
546 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
547 data->u.s.delete_token_size));
548 smb_panic("parse_share_modes: invalid token size\n");
551 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
552 if (!lck->delete_token) {
553 smb_panic("talloc failed\n");
556 /* Copy out the uid and gid. */
557 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
559 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
562 /* Any supplementary groups ? */
563 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
564 ((data->u.s.delete_token_size -
565 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
567 if (lck->delete_token->ngroups) {
568 /* Make this a talloc child of lck->delete_token. */
569 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
570 lck->delete_token->ngroups);
571 if (!lck->delete_token) {
572 smb_panic("talloc failed\n");
575 for (i = 0; i < lck->delete_token->ngroups; i++) {
576 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
582 lck->delete_token = NULL;
585 /* Save off the associated service path and filename. */
586 lck->servicepath = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
587 (lck->num_share_modes *
588 sizeof(struct share_mode_entry)) +
589 data->u.s.delete_token_size );
590 if (lck->servicepath == NULL) {
591 smb_panic("talloc_strdup failed\n");
594 lck->filename = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
595 (lck->num_share_modes *
596 sizeof(struct share_mode_entry)) +
597 data->u.s.delete_token_size +
598 strlen(lck->servicepath) + 1 );
599 if (lck->filename == NULL) {
600 smb_panic("talloc_strdup failed\n");
604 * Ensure that each entry has a real process attached.
607 for (i = 0; i < lck->num_share_modes; i++) {
608 struct share_mode_entry *entry_p = &lck->share_modes[i];
609 DEBUG(10,("parse_share_modes: %s\n",
610 share_mode_str(i, entry_p) ));
611 if (!process_exists(entry_p->pid)) {
612 DEBUG(10,("parse_share_modes: deleted %s\n",
613 share_mode_str(i, entry_p) ));
614 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
615 lck->modified = True;
622 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
627 struct locking_data *data;
630 uint32 delete_token_size;
635 for (i=0; i<lck->num_share_modes; i++) {
636 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
641 if (num_valid == 0) {
645 sp_len = strlen(lck->servicepath);
646 delete_token_size = (lck->delete_token ?
647 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
649 result.dsize = sizeof(*data) +
650 lck->num_share_modes * sizeof(struct share_mode_entry) +
653 strlen(lck->filename) + 1;
654 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
656 if (result.dptr == NULL) {
657 smb_panic("talloc failed\n");
660 data = (struct locking_data *)result.dptr;
662 data->u.s.num_share_mode_entries = lck->num_share_modes;
663 data->u.s.delete_on_close = lck->delete_on_close;
664 data->u.s.delete_token_size = delete_token_size;
665 DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
666 data->u.s.delete_on_close,
667 (unsigned int)data->u.s.delete_token_size,
668 data->u.s.num_share_mode_entries));
669 memcpy(result.dptr + sizeof(*data), lck->share_modes,
670 sizeof(struct share_mode_entry)*lck->num_share_modes);
671 offset = sizeof(*data) +
672 sizeof(struct share_mode_entry)*lck->num_share_modes;
674 /* Store any delete on close token. */
675 if (lck->delete_token) {
676 uint8 *p = result.dptr + offset;
678 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
681 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
684 for (i = 0; i < lck->delete_token->ngroups; i++) {
685 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
688 offset = p - result.dptr;
691 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
692 result.dsize - offset - 1);
693 offset += sp_len + 1;
694 safe_strcpy((char *)result.dptr + offset, lck->filename,
695 result.dsize - offset - 1);
697 if (DEBUGLEVEL >= 10) {
698 print_share_mode_table(data);
704 static int share_mode_lock_destructor(struct share_mode_lock *lck)
706 TDB_DATA key = locking_key(lck->dev, lck->ino);
709 if (!lck->modified) {
713 data = unparse_share_modes(lck);
715 if (data.dptr == NULL) {
717 /* There has been an entry before, delete it */
718 if (tdb_delete(tdb, key) == -1) {
719 smb_panic("Could not delete share entry\n");
725 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
726 smb_panic("Could not store share mode entry\n");
730 tdb_chainunlock(tdb, key);
735 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
736 SMB_DEV_T dev, SMB_INO_T ino,
737 const char *servicepath,
740 struct share_mode_lock *lck;
741 TDB_DATA key = locking_key(dev, ino);
744 lck = TALLOC_P(mem_ctx, struct share_mode_lock);
746 DEBUG(0, ("talloc failed\n"));
750 /* Ensure we set every field here as the destructor must be
751 valid even if parse_share_modes fails. */
753 lck->servicepath = NULL;
754 lck->filename = NULL;
757 lck->num_share_modes = 0;
758 lck->share_modes = NULL;
759 lck->delete_token = NULL;
760 lck->delete_on_close = False;
762 lck->modified = False;
764 if (tdb_chainlock(tdb, key) != 0) {
765 DEBUG(3, ("Could not lock share entry\n"));
770 /* We must set the destructor immediately after the chainlock
771 ensure the lock is cleaned up on any of the error return
774 talloc_set_destructor(lck, share_mode_lock_destructor);
776 data = tdb_fetch(tdb, key);
777 lck->fresh = (data.dptr == NULL);
781 if (fname == NULL || servicepath == NULL) {
785 lck->filename = talloc_strdup(lck, fname);
786 lck->servicepath = talloc_strdup(lck, servicepath);
787 if (lck->filename == NULL || lck->servicepath == NULL) {
788 DEBUG(0, ("talloc failed\n"));
793 if (!parse_share_modes(data, lck)) {
794 DEBUG(0, ("Could not parse share modes\n"));
796 SAFE_FREE(data.dptr);
801 SAFE_FREE(data.dptr);
806 /*******************************************************************
807 Sets the service name and filename for rename.
808 At this point we emit "file renamed" messages to all
809 process id's that have this file open.
810 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
811 ********************************************************************/
813 BOOL rename_share_filename(struct messaging_context *msg_ctx,
814 struct share_mode_lock *lck,
815 const char *servicepath,
829 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
830 servicepath, newname));
833 * rename_internal_fsp() and rename_internals() add './' to
834 * head of newname if newname does not contain a '/'.
836 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
840 lck->servicepath = talloc_strdup(lck, servicepath);
841 lck->filename = talloc_strdup(lck, newname);
842 if (lck->filename == NULL || lck->servicepath == NULL) {
843 DEBUG(0, ("rename_share_filename: talloc failed\n"));
846 lck->modified = True;
848 sp_len = strlen(lck->servicepath);
849 fn_len = strlen(lck->filename);
851 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
853 /* Set up the name changed message. */
854 frm = TALLOC_ARRAY(lck, char, msg_len);
859 SDEV_T_VAL(frm,0,lck->dev);
860 SINO_T_VAL(frm,8,lck->ino);
862 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
864 safe_strcpy(&frm[16], lck->servicepath, sp_len);
865 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
867 msg = data_blob_const(frm, msg_len);
869 /* Send the messages. */
870 for (i=0; i<lck->num_share_modes; i++) {
871 struct share_mode_entry *se = &lck->share_modes[i];
872 if (!is_valid_share_mode_entry(se)) {
875 /* But not to ourselves... */
876 if (procid_is_me(&se->pid)) {
880 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
881 "dev %x, inode %.0f sharepath %s newname %s\n",
882 procid_str_static(&se->pid),
883 (unsigned int)lck->dev, (double)lck->ino,
884 lck->servicepath, lck->filename ));
886 messaging_send(msg_ctx, se->pid, MSG_SMB_FILE_RENAME, &msg);
892 static int pull_delete_on_close_flag(TDB_DATA key, TDB_DATA dbuf,
895 BOOL *result = (BOOL *)private_data;
896 struct locking_data *data;
898 if (dbuf.dsize < sizeof(struct locking_data)) {
899 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
902 data = (struct locking_data *)dbuf.dptr;
904 *result = data->u.s.delete_on_close;
908 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
910 TDB_DATA key = locking_key(dev, inode);
913 tdb_parse_record(tdb, key, pull_delete_on_close_flag,
918 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
922 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
923 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
924 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
926 SMB_ASSERT(num_props <= 1);
927 return (num_props != 0);
930 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
932 return (e->op_type == DEFERRED_OPEN_ENTRY);
935 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
937 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
940 /*******************************************************************
941 Fill a share mode entry.
942 ********************************************************************/
944 static void fill_share_mode_entry(struct share_mode_entry *e,
946 uid_t uid, uint16 mid, uint16 op_type)
949 e->pid = procid_self();
950 e->share_access = fsp->share_access;
951 e->private_options = fsp->fh->private_options;
952 e->access_mask = fsp->access_mask;
954 e->op_type = op_type;
955 e->time.tv_sec = fsp->open_time.tv_sec;
956 e->time.tv_usec = fsp->open_time.tv_usec;
958 e->inode = fsp->inode;
959 e->share_file_id = fsp->fh->file_id;
960 e->uid = (uint32)uid;
961 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
964 static void fill_deferred_open_entry(struct share_mode_entry *e,
965 const struct timeval request_time,
966 SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
969 e->pid = procid_self();
971 e->op_type = DEFERRED_OPEN_ENTRY;
972 e->time.tv_sec = request_time.tv_sec;
973 e->time.tv_usec = request_time.tv_usec;
980 static void add_share_mode_entry(struct share_mode_lock *lck,
981 const struct share_mode_entry *entry)
985 for (i=0; i<lck->num_share_modes; i++) {
986 struct share_mode_entry *e = &lck->share_modes[i];
987 if (is_unused_share_mode_entry(e)) {
993 if (i == lck->num_share_modes) {
994 /* No unused entry found */
995 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
996 &lck->share_modes, &lck->num_share_modes);
998 lck->modified = True;
1001 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1002 uid_t uid, uint16 mid, uint16 op_type)
1004 struct share_mode_entry entry;
1005 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1006 add_share_mode_entry(lck, &entry);
1009 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
1010 struct timeval request_time,
1011 SMB_DEV_T dev, SMB_INO_T ino)
1013 struct share_mode_entry entry;
1014 fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
1015 add_share_mode_entry(lck, &entry);
1018 /*******************************************************************
1019 Check if two share mode entries are identical, ignoring oplock
1020 and mid info and desired_access. (Removed paranoia test - it's
1021 not automatically a logic error if they are identical. JRA.)
1022 ********************************************************************/
1024 static BOOL share_modes_identical(struct share_mode_entry *e1,
1025 struct share_mode_entry *e2)
1027 /* We used to check for e1->share_access == e2->share_access here
1028 as well as the other fields but 2 different DOS or FCB opens
1029 sharing the same share mode entry may validly differ in
1030 fsp->share_access field. */
1032 return (procid_equal(&e1->pid, &e2->pid) &&
1033 e1->dev == e2->dev &&
1034 e1->inode == e2->inode &&
1035 e1->share_file_id == e2->share_file_id );
1038 static BOOL deferred_open_identical(struct share_mode_entry *e1,
1039 struct share_mode_entry *e2)
1041 return (procid_equal(&e1->pid, &e2->pid) &&
1042 (e1->op_mid == e2->op_mid) &&
1043 (e1->dev == e2->dev) &&
1044 (e1->inode == e2->inode));
1047 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1048 struct share_mode_entry *entry)
1052 for (i=0; i<lck->num_share_modes; i++) {
1053 struct share_mode_entry *e = &lck->share_modes[i];
1054 if (is_valid_share_mode_entry(entry) &&
1055 is_valid_share_mode_entry(e) &&
1056 share_modes_identical(e, entry)) {
1059 if (is_deferred_open_entry(entry) &&
1060 is_deferred_open_entry(e) &&
1061 deferred_open_identical(e, entry)) {
1068 /*******************************************************************
1069 Del the share mode of a file for this process. Return the number of
1071 ********************************************************************/
1073 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1075 struct share_mode_entry entry, *e;
1077 /* Don't care about the pid owner being correct here - just a search. */
1078 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1080 e = find_share_mode_entry(lck, &entry);
1085 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1086 lck->modified = True;
1090 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1092 struct share_mode_entry entry, *e;
1094 fill_deferred_open_entry(&entry, timeval_zero(),
1095 lck->dev, lck->ino, mid);
1097 e = find_share_mode_entry(lck, &entry);
1102 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1103 lck->modified = True;
1106 /*******************************************************************
1107 Remove an oplock mid and mode entry from a share mode.
1108 ********************************************************************/
1110 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1112 struct share_mode_entry entry, *e;
1114 /* Don't care about the pid owner being correct here - just a search. */
1115 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1117 e = find_share_mode_entry(lck, &entry);
1123 e->op_type = NO_OPLOCK;
1124 lck->modified = True;
1128 /*******************************************************************
1129 Downgrade a oplock type from exclusive to level II.
1130 ********************************************************************/
1132 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1134 struct share_mode_entry entry, *e;
1136 /* Don't care about the pid owner being correct here - just a search. */
1137 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1139 e = find_share_mode_entry(lck, &entry);
1144 e->op_type = LEVEL_II_OPLOCK;
1145 lck->modified = True;
1149 /****************************************************************************
1150 Deal with the internal needs of setting the delete on close flag. Note that
1151 as the tdb locking is recursive, it is safe to call this from within
1152 open_file_ntcreate. JRA.
1153 ****************************************************************************/
1155 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1158 if (!delete_on_close) {
1159 return NT_STATUS_OK;
1163 * Only allow delete on close for writable files.
1166 if ((dosmode & aRONLY) &&
1167 !lp_delete_readonly(SNUM(fsp->conn))) {
1168 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1169 "flag set but file attribute is readonly.\n",
1171 return NT_STATUS_CANNOT_DELETE;
1175 * Only allow delete on close for writable shares.
1178 if (!CAN_WRITE(fsp->conn)) {
1179 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1180 "close flag set but write access denied on share.\n",
1182 return NT_STATUS_ACCESS_DENIED;
1186 * Only allow delete on close for files/directories opened with delete
1190 if (!(fsp->access_mask & DELETE_ACCESS)) {
1191 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1192 "close flag set but delete access denied.\n",
1194 return NT_STATUS_ACCESS_DENIED;
1197 /* Don't allow delete on close for non-empty directories. */
1198 if (fsp->is_directory) {
1199 return can_delete_directory(fsp->conn, fsp->fsp_name);
1202 return NT_STATUS_OK;
1205 /*************************************************************************
1206 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1207 (Should this be in locking.c.... ?).
1208 *************************************************************************/
1210 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1212 UNIX_USER_TOKEN *cpy;
1218 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1223 cpy->uid = tok->uid;
1224 cpy->gid = tok->gid;
1225 cpy->ngroups = tok->ngroups;
1227 /* Make this a talloc child of cpy. */
1228 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1232 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1237 /****************************************************************************
1238 Replace the delete on close token.
1239 ****************************************************************************/
1241 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1243 /* Ensure there's no token. */
1244 if (lck->delete_token) {
1245 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1246 lck->delete_token = NULL;
1249 /* Copy the new token (can be NULL). */
1250 lck->delete_token = copy_unix_token(lck, tok);
1251 lck->modified = True;
1254 /****************************************************************************
1255 Sets the delete on close flag over all share modes on this file.
1256 Modify the share mode entry for all files open
1257 on this device and inode to tell other smbds we have
1258 changed the delete on close flag. This will be noticed
1259 in the close code, the last closer will delete the file
1261 This makes a copy of any UNIX_USER_TOKEN into the
1262 lck entry. This function is used when the lock is already granted.
1263 ****************************************************************************/
1265 void set_delete_on_close_lck(struct share_mode_lock *lck, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1267 if (lck->delete_on_close != delete_on_close) {
1268 set_delete_on_close_token(lck, tok);
1269 lck->delete_on_close = delete_on_close;
1270 if (delete_on_close) {
1271 SMB_ASSERT(lck->delete_token != NULL);
1273 lck->modified = True;
1277 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1279 struct share_mode_lock *lck;
1281 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1282 "fnum = %d, file %s\n",
1283 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1290 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1295 set_delete_on_close_lck(lck, delete_on_close, tok);
1297 if (fsp->is_directory) {
1298 send_stat_cache_delete_message(fsp->fsp_name);
1305 struct forall_state {
1306 void (*fn)(const struct share_mode_entry *entry,
1307 const char *sharepath,
1309 void *private_data);
1313 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1316 struct forall_state *state = (struct forall_state *)_state;
1317 struct locking_data *data;
1318 struct share_mode_entry *shares;
1319 const char *sharepath;
1323 /* Ensure this is a locking_key record. */
1324 if (kbuf.dsize != sizeof(struct locking_key))
1327 data = (struct locking_data *)dbuf.dptr;
1328 shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1329 sharepath = (const char *)dbuf.dptr + sizeof(*data) +
1330 data->u.s.num_share_mode_entries*sizeof(*shares) +
1331 data->u.s.delete_token_size;
1332 fname = (const char *)dbuf.dptr + sizeof(*data) +
1333 data->u.s.num_share_mode_entries*sizeof(*shares) +
1334 data->u.s.delete_token_size +
1335 strlen(sharepath) + 1;
1337 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1338 state->fn(&shares[i], sharepath, fname,
1339 state->private_data);
1344 /*******************************************************************
1345 Call the specified function on each entry under management by the
1347 ********************************************************************/
1349 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1350 const char *, void *),
1353 struct forall_state state;
1359 state.private_data = private_data;
1361 return tdb_traverse(tdb, traverse_fn, (void *)&state);