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 3 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, see <http://www.gnu.org/licenses/>.
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
32 rewrtten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
41 #define DBGC_CLASS DBGC_LOCKING
43 #define NO_LOCKING_COUNT (-1)
45 /* the locking database handle */
46 static struct db_context *lock_db;
48 /****************************************************************************
50 ****************************************************************************/
52 const char *lock_type_name(enum brl_type lock_type)
59 case PENDING_READ_LOCK:
60 return "PENDING_READ";
61 case PENDING_WRITE_LOCK:
62 return "PENDING_WRITE";
68 const char *lock_flav_name(enum brl_flavour lock_flav)
70 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
73 /****************************************************************************
74 Utility function called to see if a file region is locked.
75 Called in the read/write codepath.
76 ****************************************************************************/
78 bool is_locked(files_struct *fsp,
82 enum brl_type lock_type)
84 int strict_locking = lp_strict_locking(fsp->conn->params);
85 enum brl_flavour lock_flav = lp_posix_cifsu_locktype(fsp);
92 if (!lp_locking(fsp->conn->params) || !strict_locking) {
96 if (strict_locking == Auto) {
97 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
98 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
100 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
101 (lock_type == READ_LOCK)) {
102 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
105 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
109 ret = !brl_locktest(br_lck,
119 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
123 ret = !brl_locktest(br_lck,
133 DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
134 lock_flav_name(lock_flav),
135 (double)offset, (double)count, ret ? "locked" : "unlocked",
136 fsp->fnum, fsp->fsp_name ));
141 /****************************************************************************
142 Find out if a lock could be granted - return who is blocking us if we can't.
143 ****************************************************************************/
145 NTSTATUS query_lock(files_struct *fsp,
147 SMB_BIG_UINT *pcount,
148 SMB_BIG_UINT *poffset,
149 enum brl_type *plock_type,
150 enum brl_flavour lock_flav)
152 struct byte_range_lock *br_lck = NULL;
153 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
155 if (!fsp->can_lock) {
156 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
159 if (!lp_locking(fsp->conn->params)) {
163 br_lck = brl_get_locks_readonly(NULL, fsp);
165 return NT_STATUS_NO_MEMORY;
168 status = brl_lockquery(br_lck,
180 /****************************************************************************
181 Utility function called by locking requests.
182 ****************************************************************************/
184 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
189 enum brl_type lock_type,
190 enum brl_flavour lock_flav,
195 struct byte_range_lock *br_lck = NULL;
197 if (!fsp->can_lock) {
198 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
202 if (!lp_locking(fsp->conn->params)) {
203 *perr = NT_STATUS_OK;
207 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
209 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
210 lock_flav_name(lock_flav), lock_type_name(lock_type),
211 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
213 br_lck = brl_get_locks(NULL, fsp);
215 *perr = NT_STATUS_NO_MEMORY;
219 *perr = brl_lock(msg_ctx,
230 if (lock_flav == WINDOWS_LOCK &&
231 fsp->current_lock_count != NO_LOCKING_COUNT) {
232 /* blocking ie. pending, locks also count here,
233 * as this is an efficiency counter to avoid checking
234 * the lock db. on close. JRA. */
236 fsp->current_lock_count++;
238 /* Notice that this has had a POSIX lock request.
239 * We can't count locks after this so forget them.
241 fsp->current_lock_count = NO_LOCKING_COUNT;
247 /****************************************************************************
248 Utility function called by unlocking requests.
249 ****************************************************************************/
251 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
256 enum brl_flavour lock_flav)
259 struct byte_range_lock *br_lck = NULL;
261 if (!fsp->can_lock) {
262 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
265 if (!lp_locking(fsp->conn->params)) {
269 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
270 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
272 br_lck = brl_get_locks(NULL, fsp);
274 return NT_STATUS_NO_MEMORY;
277 ok = brl_unlock(msg_ctx,
288 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
289 return NT_STATUS_RANGE_NOT_LOCKED;
292 if (lock_flav == WINDOWS_LOCK &&
293 fsp->current_lock_count != NO_LOCKING_COUNT) {
294 SMB_ASSERT(fsp->current_lock_count > 0);
295 fsp->current_lock_count--;
301 /****************************************************************************
302 Cancel any pending blocked locks.
303 ****************************************************************************/
305 NTSTATUS do_lock_cancel(files_struct *fsp,
309 enum brl_flavour lock_flav)
312 struct byte_range_lock *br_lck = NULL;
314 if (!fsp->can_lock) {
315 return fsp->is_directory ?
316 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
319 if (!lp_locking(fsp->conn->params)) {
320 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
323 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
324 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
326 br_lck = brl_get_locks(NULL, fsp);
328 return NT_STATUS_NO_MEMORY;
331 ok = brl_lock_cancel(br_lck,
341 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
342 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
345 if (lock_flav == WINDOWS_LOCK &&
346 fsp->current_lock_count != NO_LOCKING_COUNT) {
347 SMB_ASSERT(fsp->current_lock_count > 0);
348 fsp->current_lock_count--;
354 /****************************************************************************
355 Remove any locks on this fd. Called from file_close().
356 ****************************************************************************/
358 void locking_close_file(struct messaging_context *msg_ctx,
361 struct byte_range_lock *br_lck;
363 if (!lp_locking(fsp->conn->params)) {
367 /* If we have not outstanding locks or pending
368 * locks then we don't need to look in the lock db.
371 if (fsp->current_lock_count == 0) {
375 br_lck = brl_get_locks(NULL,fsp);
378 cancel_pending_lock_requests_by_fid(fsp, br_lck);
379 brl_close_fnum(msg_ctx, br_lck);
384 /****************************************************************************
385 Initialise the locking functions.
386 ****************************************************************************/
388 static int open_read_only;
390 bool locking_init(int read_only)
397 lock_db = db_open(NULL, lock_path("locking.tdb"), 0,
400 |(read_only?0x0:TDB_CLEAR_IF_FIRST),
401 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
404 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
408 if (!posix_locking_init(read_only))
411 open_read_only = read_only;
416 /*******************************************************************
417 Deinitialize the share_mode management.
418 ******************************************************************/
420 bool locking_end(void)
422 brl_shutdown(open_read_only);
424 TALLOC_FREE(lock_db);
429 /*******************************************************************
430 Form a static locking key for a dev/inode pair.
431 ******************************************************************/
433 static TDB_DATA locking_key(struct file_id id)
435 static struct file_id key;
438 kbuf.dptr = (uint8 *)&key;
439 kbuf.dsize = sizeof(key);
443 /*******************************************************************
444 Print out a share mode.
445 ********************************************************************/
447 char *share_mode_str(TALLOC_CTX *ctx, int num, struct share_mode_entry *e)
449 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
450 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
451 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
452 "uid = %u, flags = %u, file_id %s",
454 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
455 procid_str_static(&e->pid),
456 e->share_access, e->private_options,
457 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
458 (unsigned int)e->uid, (unsigned int)e->flags,
459 file_id_string_tos(&e->id));
462 /*******************************************************************
463 Print out a share mode table.
464 ********************************************************************/
466 static void print_share_mode_table(struct locking_data *data)
468 int num_share_modes = data->u.s.num_share_mode_entries;
469 struct share_mode_entry *shares =
470 (struct share_mode_entry *)(data + 1);
473 for (i = 0; i < num_share_modes; i++) {
474 struct share_mode_entry entry;
478 * We need to memcpy the entry here due to alignment
479 * restrictions that are not met when directly accessing
483 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
484 str = share_mode_str(talloc_tos(), i, &entry);
486 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
491 /*******************************************************************
492 Get all share mode entries for a dev/inode pair.
493 ********************************************************************/
495 static bool parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
497 struct locking_data *data;
500 if (dbuf.dsize < sizeof(struct locking_data)) {
501 smb_panic("parse_share_modes: buffer too short");
504 data = (struct locking_data *)dbuf.dptr;
506 lck->delete_on_close = data->u.s.delete_on_close;
507 lck->num_share_modes = data->u.s.num_share_mode_entries;
509 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
510 "num_share_modes: %d\n",
511 lck->delete_on_close,
512 lck->num_share_modes));
514 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
515 DEBUG(0, ("invalid number of share modes: %d\n",
516 lck->num_share_modes));
517 smb_panic("parse_share_modes: invalid number of share modes");
520 lck->share_modes = NULL;
522 if (lck->num_share_modes != 0) {
524 if (dbuf.dsize < (sizeof(struct locking_data) +
525 (lck->num_share_modes *
526 sizeof(struct share_mode_entry)))) {
527 smb_panic("parse_share_modes: buffer too short");
530 lck->share_modes = (struct share_mode_entry *)
531 TALLOC_MEMDUP(lck, dbuf.dptr+sizeof(*data),
532 lck->num_share_modes *
533 sizeof(struct share_mode_entry));
535 if (lck->share_modes == NULL) {
536 smb_panic("parse_share_modes: talloc failed");
540 /* Get any delete token. */
541 if (data->u.s.delete_token_size) {
542 uint8 *p = dbuf.dptr + sizeof(*data) +
543 (lck->num_share_modes *
544 sizeof(struct share_mode_entry));
546 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
547 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
548 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
549 data->u.s.delete_token_size));
550 smb_panic("parse_share_modes: invalid token size");
553 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
554 if (!lck->delete_token) {
555 smb_panic("parse_share_modes: talloc failed");
558 /* Copy out the uid and gid. */
559 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
561 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
564 /* Any supplementary groups ? */
565 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
566 ((data->u.s.delete_token_size -
567 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
569 if (lck->delete_token->ngroups) {
570 /* Make this a talloc child of lck->delete_token. */
571 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
572 lck->delete_token->ngroups);
573 if (!lck->delete_token) {
574 smb_panic("parse_share_modes: talloc failed");
577 for (i = 0; i < lck->delete_token->ngroups; i++) {
578 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
584 lck->delete_token = NULL;
587 /* Save off the associated service path and filename. */
588 lck->servicepath = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
589 (lck->num_share_modes *
590 sizeof(struct share_mode_entry)) +
591 data->u.s.delete_token_size );
592 if (lck->servicepath == NULL) {
593 smb_panic("parse_share_modes: talloc_strdup failed");
596 lck->filename = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
597 (lck->num_share_modes *
598 sizeof(struct share_mode_entry)) +
599 data->u.s.delete_token_size +
600 strlen(lck->servicepath) + 1 );
601 if (lck->filename == NULL) {
602 smb_panic("parse_share_modes: talloc_strdup failed");
606 * Ensure that each entry has a real process attached.
609 for (i = 0; i < lck->num_share_modes; i++) {
610 struct share_mode_entry *entry_p = &lck->share_modes[i];
611 char *str = share_mode_str(NULL, i, entry_p);
612 DEBUG(10,("parse_share_modes: %s\n",
614 if (!process_exists(entry_p->pid)) {
615 DEBUG(10,("parse_share_modes: deleted %s\n",
617 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
618 lck->modified = True;
626 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
631 struct locking_data *data;
634 uint32 delete_token_size;
639 for (i=0; i<lck->num_share_modes; i++) {
640 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
645 if (num_valid == 0) {
649 sp_len = strlen(lck->servicepath);
650 delete_token_size = (lck->delete_token ?
651 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
653 result.dsize = sizeof(*data) +
654 lck->num_share_modes * sizeof(struct share_mode_entry) +
657 strlen(lck->filename) + 1;
658 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
660 if (result.dptr == NULL) {
661 smb_panic("talloc failed");
664 data = (struct locking_data *)result.dptr;
666 data->u.s.num_share_mode_entries = lck->num_share_modes;
667 data->u.s.delete_on_close = lck->delete_on_close;
668 data->u.s.delete_token_size = delete_token_size;
669 DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
670 data->u.s.delete_on_close,
671 (unsigned int)data->u.s.delete_token_size,
672 data->u.s.num_share_mode_entries));
673 memcpy(result.dptr + sizeof(*data), lck->share_modes,
674 sizeof(struct share_mode_entry)*lck->num_share_modes);
675 offset = sizeof(*data) +
676 sizeof(struct share_mode_entry)*lck->num_share_modes;
678 /* Store any delete on close token. */
679 if (lck->delete_token) {
680 uint8 *p = result.dptr + offset;
682 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
685 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
688 for (i = 0; i < lck->delete_token->ngroups; i++) {
689 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
692 offset = p - result.dptr;
695 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
696 result.dsize - offset - 1);
697 offset += sp_len + 1;
698 safe_strcpy((char *)result.dptr + offset, lck->filename,
699 result.dsize - offset - 1);
701 if (DEBUGLEVEL >= 10) {
702 print_share_mode_table(data);
708 static int share_mode_lock_destructor(struct share_mode_lock *lck)
713 if (!lck->modified) {
717 data = unparse_share_modes(lck);
719 if (data.dptr == NULL) {
721 /* There has been an entry before, delete it */
723 status = lck->record->delete_rec(lck->record);
724 if (!NT_STATUS_IS_OK(status)) {
725 DEBUG(0, ("delete_rec returned %s\n",
727 smb_panic("could not delete share entry");
733 status = lck->record->store(lck->record, data, TDB_REPLACE);
734 if (!NT_STATUS_IS_OK(status)) {
735 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
736 smb_panic("could not store share mode entry");
744 static bool fill_share_mode_lock(struct share_mode_lock *lck,
746 const char *servicepath,
748 TDB_DATA share_mode_data)
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;
756 lck->num_share_modes = 0;
757 lck->share_modes = NULL;
758 lck->delete_token = NULL;
759 lck->delete_on_close = False;
761 lck->modified = False;
763 lck->fresh = (share_mode_data.dptr == NULL);
766 if (fname == NULL || servicepath == NULL) {
769 lck->filename = talloc_strdup(lck, fname);
770 lck->servicepath = talloc_strdup(lck, servicepath);
771 if (lck->filename == NULL || lck->servicepath == NULL) {
772 DEBUG(0, ("talloc failed\n"));
776 if (!parse_share_modes(share_mode_data, lck)) {
777 DEBUG(0, ("Could not parse share modes\n"));
785 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
787 const char *servicepath,
790 struct share_mode_lock *lck;
791 TDB_DATA key = locking_key(id);
793 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
794 DEBUG(0, ("talloc failed\n"));
798 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
799 DEBUG(3, ("Could not lock share entry\n"));
804 if (!fill_share_mode_lock(lck, id, servicepath, fname,
805 lck->record->value)) {
806 DEBUG(3, ("fill_share_mode_lock failed\n"));
811 talloc_set_destructor(lck, share_mode_lock_destructor);
816 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
818 const char *servicepath,
821 struct share_mode_lock *lck;
822 TDB_DATA key = locking_key(id);
825 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
826 DEBUG(0, ("talloc failed\n"));
830 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
831 DEBUG(3, ("Could not fetch share entry\n"));
836 if (!fill_share_mode_lock(lck, id, servicepath, fname, data)) {
837 DEBUG(3, ("fill_share_mode_lock failed\n"));
842 TALLOC_FREE(data.dptr);
847 /*******************************************************************
848 Sets the service name and filename for rename.
849 At this point we emit "file renamed" messages to all
850 process id's that have this file open.
851 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
852 ********************************************************************/
854 bool rename_share_filename(struct messaging_context *msg_ctx,
855 struct share_mode_lock *lck,
856 const char *servicepath,
865 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
866 servicepath, newname));
869 * rename_internal_fsp() and rename_internals() add './' to
870 * head of newname if newname does not contain a '/'.
872 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
876 lck->servicepath = talloc_strdup(lck, servicepath);
877 lck->filename = talloc_strdup(lck, newname);
878 if (lck->filename == NULL || lck->servicepath == NULL) {
879 DEBUG(0, ("rename_share_filename: talloc failed\n"));
882 lck->modified = True;
884 sp_len = strlen(lck->servicepath);
885 fn_len = strlen(lck->filename);
887 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
889 /* Set up the name changed message. */
890 frm = TALLOC_ARRAY(lck, char, msg_len);
895 push_file_id_16(frm, &lck->id);
897 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
899 safe_strcpy(&frm[16], lck->servicepath, sp_len);
900 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
902 /* Send the messages. */
903 for (i=0; i<lck->num_share_modes; i++) {
904 struct share_mode_entry *se = &lck->share_modes[i];
905 if (!is_valid_share_mode_entry(se)) {
908 /* But not to ourselves... */
909 if (procid_is_me(&se->pid)) {
913 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
914 "file_id %s sharepath %s newname %s\n",
915 procid_str_static(&se->pid),
916 file_id_string_tos(&lck->id),
917 lck->servicepath, lck->filename ));
919 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
920 (uint8 *)frm, msg_len);
926 bool get_delete_on_close_flag(struct file_id id)
929 struct share_mode_lock *lck;
931 if (!(lck = fetch_share_mode_unlocked(NULL, id, NULL, NULL))) {
934 result = lck->delete_on_close;
939 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
943 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
944 /* cope with dead entries from the process not
945 existing. These should not be considered valid,
946 otherwise we end up doing zero timeout sharing
951 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
952 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
953 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
955 SMB_ASSERT(num_props <= 1);
956 return (num_props != 0);
959 bool is_deferred_open_entry(const struct share_mode_entry *e)
961 return (e->op_type == DEFERRED_OPEN_ENTRY);
964 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
966 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
969 /*******************************************************************
970 Fill a share mode entry.
971 ********************************************************************/
973 static void fill_share_mode_entry(struct share_mode_entry *e,
975 uid_t uid, uint16 mid, uint16 op_type)
978 e->pid = procid_self();
979 e->share_access = fsp->share_access;
980 e->private_options = fsp->fh->private_options;
981 e->access_mask = fsp->access_mask;
983 e->op_type = op_type;
984 e->time.tv_sec = fsp->open_time.tv_sec;
985 e->time.tv_usec = fsp->open_time.tv_usec;
986 e->id = fsp->file_id;
987 e->share_file_id = fsp->fh->gen_id;
988 e->uid = (uint32)uid;
989 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
992 static void fill_deferred_open_entry(struct share_mode_entry *e,
993 const struct timeval request_time,
994 struct file_id id, uint16 mid)
997 e->pid = procid_self();
999 e->op_type = DEFERRED_OPEN_ENTRY;
1000 e->time.tv_sec = request_time.tv_sec;
1001 e->time.tv_usec = request_time.tv_usec;
1003 e->uid = (uint32)-1;
1007 static void add_share_mode_entry(struct share_mode_lock *lck,
1008 const struct share_mode_entry *entry)
1012 for (i=0; i<lck->num_share_modes; i++) {
1013 struct share_mode_entry *e = &lck->share_modes[i];
1014 if (is_unused_share_mode_entry(e)) {
1020 if (i == lck->num_share_modes) {
1021 /* No unused entry found */
1022 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1023 &lck->share_modes, &lck->num_share_modes);
1025 lck->modified = True;
1028 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1029 uid_t uid, uint16 mid, uint16 op_type, bool initial_delete_on_close_allowed)
1031 struct share_mode_entry entry;
1032 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1033 if (initial_delete_on_close_allowed) {
1034 entry.flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1036 add_share_mode_entry(lck, &entry);
1039 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
1040 struct timeval request_time,
1043 struct share_mode_entry entry;
1044 fill_deferred_open_entry(&entry, request_time, id, mid);
1045 add_share_mode_entry(lck, &entry);
1048 /*******************************************************************
1049 Check if two share mode entries are identical, ignoring oplock
1050 and mid info and desired_access. (Removed paranoia test - it's
1051 not automatically a logic error if they are identical. JRA.)
1052 ********************************************************************/
1054 static bool share_modes_identical(struct share_mode_entry *e1,
1055 struct share_mode_entry *e2)
1057 /* We used to check for e1->share_access == e2->share_access here
1058 as well as the other fields but 2 different DOS or FCB opens
1059 sharing the same share mode entry may validly differ in
1060 fsp->share_access field. */
1062 return (procid_equal(&e1->pid, &e2->pid) &&
1063 file_id_equal(&e1->id, &e2->id) &&
1064 e1->share_file_id == e2->share_file_id );
1067 static bool deferred_open_identical(struct share_mode_entry *e1,
1068 struct share_mode_entry *e2)
1070 return (procid_equal(&e1->pid, &e2->pid) &&
1071 (e1->op_mid == e2->op_mid) &&
1072 file_id_equal(&e1->id, &e2->id));
1075 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1076 struct share_mode_entry *entry)
1080 for (i=0; i<lck->num_share_modes; i++) {
1081 struct share_mode_entry *e = &lck->share_modes[i];
1082 if (is_valid_share_mode_entry(entry) &&
1083 is_valid_share_mode_entry(e) &&
1084 share_modes_identical(e, entry)) {
1087 if (is_deferred_open_entry(entry) &&
1088 is_deferred_open_entry(e) &&
1089 deferred_open_identical(e, entry)) {
1096 /*******************************************************************
1097 Del the share mode of a file for this process. Return the number of
1099 ********************************************************************/
1101 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1103 struct share_mode_entry entry, *e;
1105 /* Don't care about the pid owner being correct here - just a search. */
1106 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1108 e = find_share_mode_entry(lck, &entry);
1113 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1114 lck->modified = True;
1118 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1120 struct share_mode_entry entry, *e;
1122 fill_deferred_open_entry(&entry, timeval_zero(),
1125 e = find_share_mode_entry(lck, &entry);
1130 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1131 lck->modified = True;
1134 /*******************************************************************
1135 Remove an oplock mid and mode entry from a share mode.
1136 ********************************************************************/
1138 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1140 struct share_mode_entry entry, *e;
1142 /* Don't care about the pid owner being correct here - just a search. */
1143 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1145 e = find_share_mode_entry(lck, &entry);
1151 e->op_type = NO_OPLOCK;
1152 lck->modified = True;
1156 /*******************************************************************
1157 Downgrade a oplock type from exclusive to level II.
1158 ********************************************************************/
1160 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1162 struct share_mode_entry entry, *e;
1164 /* Don't care about the pid owner being correct here - just a search. */
1165 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1167 e = find_share_mode_entry(lck, &entry);
1172 e->op_type = LEVEL_II_OPLOCK;
1173 lck->modified = True;
1177 /****************************************************************************
1178 Deal with the internal needs of setting the delete on close flag. Note that
1179 as the tdb locking is recursive, it is safe to call this from within
1180 open_file_ntcreate. JRA.
1181 ****************************************************************************/
1183 NTSTATUS can_set_delete_on_close(files_struct *fsp, bool delete_on_close,
1186 if (!delete_on_close) {
1187 return NT_STATUS_OK;
1191 * Only allow delete on close for writable files.
1194 if ((dosmode & aRONLY) &&
1195 !lp_delete_readonly(SNUM(fsp->conn))) {
1196 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1197 "flag set but file attribute is readonly.\n",
1199 return NT_STATUS_CANNOT_DELETE;
1203 * Only allow delete on close for writable shares.
1206 if (!CAN_WRITE(fsp->conn)) {
1207 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1208 "close flag set but write access denied on share.\n",
1210 return NT_STATUS_ACCESS_DENIED;
1214 * Only allow delete on close for files/directories opened with delete
1218 if (!(fsp->access_mask & DELETE_ACCESS)) {
1219 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1220 "close flag set but delete access denied.\n",
1222 return NT_STATUS_ACCESS_DENIED;
1225 /* Don't allow delete on close for non-empty directories. */
1226 if (fsp->is_directory) {
1227 return can_delete_directory(fsp->conn, fsp->fsp_name);
1230 return NT_STATUS_OK;
1233 /****************************************************************************
1234 Do we have an open file handle that created this entry ?
1235 ****************************************************************************/
1237 bool can_set_initial_delete_on_close(const struct share_mode_lock *lck)
1241 for (i=0; i<lck->num_share_modes; i++) {
1242 if (lck->share_modes[i].flags & SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE) {
1249 /*************************************************************************
1250 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1251 (Should this be in locking.c.... ?).
1252 *************************************************************************/
1254 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1256 UNIX_USER_TOKEN *cpy;
1262 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1267 cpy->uid = tok->uid;
1268 cpy->gid = tok->gid;
1269 cpy->ngroups = tok->ngroups;
1271 /* Make this a talloc child of cpy. */
1272 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1276 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1281 /****************************************************************************
1282 Replace the delete on close token.
1283 ****************************************************************************/
1285 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1287 /* Ensure there's no token. */
1288 if (lck->delete_token) {
1289 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1290 lck->delete_token = NULL;
1293 /* Copy the new token (can be NULL). */
1294 lck->delete_token = copy_unix_token(lck, tok);
1295 lck->modified = True;
1298 /****************************************************************************
1299 Sets the delete on close flag over all share modes on this file.
1300 Modify the share mode entry for all files open
1301 on this device and inode to tell other smbds we have
1302 changed the delete on close flag. This will be noticed
1303 in the close code, the last closer will delete the file
1305 This makes a copy of any UNIX_USER_TOKEN into the
1306 lck entry. This function is used when the lock is already granted.
1307 ****************************************************************************/
1309 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, UNIX_USER_TOKEN *tok)
1311 if (lck->delete_on_close != delete_on_close) {
1312 set_delete_on_close_token(lck, tok);
1313 lck->delete_on_close = delete_on_close;
1314 if (delete_on_close) {
1315 SMB_ASSERT(lck->delete_token != NULL);
1317 lck->modified = True;
1321 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, UNIX_USER_TOKEN *tok)
1323 struct share_mode_lock *lck;
1325 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1326 "fnum = %d, file %s\n",
1327 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1334 lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
1339 set_delete_on_close_lck(lck, delete_on_close, tok);
1341 if (fsp->is_directory) {
1342 send_stat_cache_delete_message(fsp->fsp_name);
1349 /****************************************************************************
1350 Sets the allow initial delete on close flag for this share mode.
1351 ****************************************************************************/
1353 bool set_allow_initial_delete_on_close(struct share_mode_lock *lck, files_struct *fsp, bool delete_on_close)
1355 struct share_mode_entry entry, *e;
1357 /* Don't care about the pid owner being correct here - just a search. */
1358 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1360 e = find_share_mode_entry(lck, &entry);
1365 if (delete_on_close) {
1366 e->flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1368 e->flags &= ~SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1370 lck->modified = True;
1374 struct forall_state {
1375 void (*fn)(const struct share_mode_entry *entry,
1376 const char *sharepath,
1378 void *private_data);
1382 static int traverse_fn(struct db_record *rec, void *_state)
1384 struct forall_state *state = (struct forall_state *)_state;
1385 struct locking_data *data;
1386 struct share_mode_entry *shares;
1387 const char *sharepath;
1391 /* Ensure this is a locking_key record. */
1392 if (rec->key.dsize != sizeof(struct file_id))
1395 data = (struct locking_data *)rec->value.dptr;
1396 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1397 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1398 data->u.s.num_share_mode_entries*sizeof(*shares) +
1399 data->u.s.delete_token_size;
1400 fname = (const char *)rec->value.dptr + sizeof(*data) +
1401 data->u.s.num_share_mode_entries*sizeof(*shares) +
1402 data->u.s.delete_token_size +
1403 strlen(sharepath) + 1;
1405 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1406 state->fn(&shares[i], sharepath, fname,
1407 state->private_data);
1412 /*******************************************************************
1413 Call the specified function on each entry under management by the
1415 ********************************************************************/
1417 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1418 const char *, void *),
1421 struct forall_state state;
1423 if (lock_db == NULL)
1427 state.private_data = private_data;
1429 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);