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 struct db_context *lock_db;
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,
194 struct byte_range_lock *br_lck = NULL;
196 if (!fsp->can_lock) {
197 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
201 if (!lp_locking(fsp->conn->params)) {
202 *perr = NT_STATUS_OK;
206 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
208 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
209 lock_flav_name(lock_flav), lock_type_name(lock_type),
210 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
212 br_lck = brl_get_locks(NULL, fsp);
214 *perr = NT_STATUS_NO_MEMORY;
218 *perr = brl_lock(msg_ctx,
229 /* blocking ie. pending, locks also count here,
230 * as this is an efficiency counter to avoid checking
231 * the lock db. on close. JRA. */
233 fsp->current_lock_count++;
238 /****************************************************************************
239 Utility function called by unlocking requests.
240 ****************************************************************************/
242 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
247 enum brl_flavour lock_flav)
250 struct byte_range_lock *br_lck = NULL;
252 if (!fsp->can_lock) {
253 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
256 if (!lp_locking(fsp->conn->params)) {
260 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
261 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
263 br_lck = brl_get_locks(NULL, fsp);
265 return NT_STATUS_NO_MEMORY;
268 ok = brl_unlock(msg_ctx,
279 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
280 return NT_STATUS_RANGE_NOT_LOCKED;
283 SMB_ASSERT(fsp->current_lock_count > 0);
284 fsp->current_lock_count--;
289 /****************************************************************************
290 Cancel any pending blocked locks.
291 ****************************************************************************/
293 NTSTATUS do_lock_cancel(files_struct *fsp,
297 enum brl_flavour lock_flav)
300 struct byte_range_lock *br_lck = NULL;
302 if (!fsp->can_lock) {
303 return fsp->is_directory ?
304 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
307 if (!lp_locking(fsp->conn->params)) {
308 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
311 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
312 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
314 br_lck = brl_get_locks(NULL, fsp);
316 return NT_STATUS_NO_MEMORY;
319 ok = brl_lock_cancel(br_lck,
329 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
330 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
333 SMB_ASSERT(fsp->current_lock_count > 0);
334 fsp->current_lock_count--;
339 /****************************************************************************
340 Remove any locks on this fd. Called from file_close().
341 ****************************************************************************/
343 void locking_close_file(struct messaging_context *msg_ctx,
346 struct byte_range_lock *br_lck;
348 if (!lp_locking(fsp->conn->params)) {
352 /* If we have not outstanding locks or pending
353 * locks then we don't need to look in the lock db.
356 if (fsp->current_lock_count == 0) {
360 br_lck = brl_get_locks(NULL,fsp);
363 cancel_pending_lock_requests_by_fid(fsp, br_lck);
364 brl_close_fnum(msg_ctx, br_lck);
369 /****************************************************************************
370 Initialise the locking functions.
371 ****************************************************************************/
373 static int open_read_only;
375 BOOL locking_init(int read_only)
382 lock_db = db_open(NULL, lock_path("locking.tdb"),
383 lp_open_files_db_hash_size(),
386 |(read_only?0x0:TDB_CLEAR_IF_FIRST),
387 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
390 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
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)
408 brl_shutdown(open_read_only);
410 TALLOC_FREE(lock_db);
415 /*******************************************************************
416 Form a static locking key for a dev/inode pair.
417 ******************************************************************/
419 static TDB_DATA locking_key(struct file_id id)
421 static struct file_id key;
424 kbuf.dptr = (uint8 *)&key;
425 kbuf.dsize = sizeof(key);
429 /*******************************************************************
430 Print out a share mode.
431 ********************************************************************/
433 char *share_mode_str(int num, struct share_mode_entry *e)
435 static pstring share_str;
437 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
438 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
439 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
440 "uid = %u, flags = %u, file_id %s",
442 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
443 procid_str_static(&e->pid),
444 e->share_access, e->private_options,
445 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
446 (unsigned int)e->uid, (unsigned int)e->flags,
447 file_id_static_string(&e->id));
452 /*******************************************************************
453 Print out a share mode table.
454 ********************************************************************/
456 static void print_share_mode_table(struct locking_data *data)
458 int num_share_modes = data->u.s.num_share_mode_entries;
459 struct share_mode_entry *shares =
460 (struct share_mode_entry *)(data + 1);
463 for (i = 0; i < num_share_modes; i++) {
464 struct share_mode_entry entry;
466 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
467 DEBUG(10,("print_share_mode_table: %s\n",
468 share_mode_str(i, &entry)));
472 /*******************************************************************
473 Get all share mode entries for a dev/inode pair.
474 ********************************************************************/
476 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
478 struct locking_data *data;
481 if (dbuf.dsize < sizeof(struct locking_data)) {
482 smb_panic("parse_share_modes: buffer too short");
485 data = (struct locking_data *)dbuf.dptr;
487 lck->delete_on_close = data->u.s.delete_on_close;
488 lck->num_share_modes = data->u.s.num_share_mode_entries;
490 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
491 "num_share_modes: %d\n",
492 lck->delete_on_close,
493 lck->num_share_modes));
495 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
496 DEBUG(0, ("invalid number of share modes: %d\n",
497 lck->num_share_modes));
498 smb_panic("parse_share_modes: invalid number of share modes");
501 lck->share_modes = NULL;
503 if (lck->num_share_modes != 0) {
505 if (dbuf.dsize < (sizeof(struct locking_data) +
506 (lck->num_share_modes *
507 sizeof(struct share_mode_entry)))) {
508 smb_panic("parse_share_modes: buffer too short");
511 lck->share_modes = (struct share_mode_entry *)
512 TALLOC_MEMDUP(lck, dbuf.dptr+sizeof(*data),
513 lck->num_share_modes *
514 sizeof(struct share_mode_entry));
516 if (lck->share_modes == NULL) {
517 smb_panic("parse_share_modes: talloc failed");
521 /* Get any delete token. */
522 if (data->u.s.delete_token_size) {
523 uint8 *p = dbuf.dptr + sizeof(*data) +
524 (lck->num_share_modes *
525 sizeof(struct share_mode_entry));
527 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
528 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
529 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
530 data->u.s.delete_token_size));
531 smb_panic("parse_share_modes: invalid token size");
534 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
535 if (!lck->delete_token) {
536 smb_panic("parse_share_modes: talloc failed");
539 /* Copy out the uid and gid. */
540 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
542 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
545 /* Any supplementary groups ? */
546 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
547 ((data->u.s.delete_token_size -
548 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
550 if (lck->delete_token->ngroups) {
551 /* Make this a talloc child of lck->delete_token. */
552 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
553 lck->delete_token->ngroups);
554 if (!lck->delete_token) {
555 smb_panic("parse_share_modes: talloc failed");
558 for (i = 0; i < lck->delete_token->ngroups; i++) {
559 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
565 lck->delete_token = NULL;
568 /* Save off the associated service path and filename. */
569 lck->servicepath = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
570 (lck->num_share_modes *
571 sizeof(struct share_mode_entry)) +
572 data->u.s.delete_token_size );
573 if (lck->servicepath == NULL) {
574 smb_panic("parse_share_modes: talloc_strdup failed");
577 lck->filename = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
578 (lck->num_share_modes *
579 sizeof(struct share_mode_entry)) +
580 data->u.s.delete_token_size +
581 strlen(lck->servicepath) + 1 );
582 if (lck->filename == NULL) {
583 smb_panic("parse_share_modes: talloc_strdup failed");
587 * Ensure that each entry has a real process attached.
590 for (i = 0; i < lck->num_share_modes; i++) {
591 struct share_mode_entry *entry_p = &lck->share_modes[i];
592 DEBUG(10,("parse_share_modes: %s\n",
593 share_mode_str(i, entry_p) ));
594 if (!process_exists(entry_p->pid)) {
595 DEBUG(10,("parse_share_modes: deleted %s\n",
596 share_mode_str(i, entry_p) ));
597 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
598 lck->modified = True;
605 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
610 struct locking_data *data;
613 uint32 delete_token_size;
618 for (i=0; i<lck->num_share_modes; i++) {
619 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
624 if (num_valid == 0) {
628 sp_len = strlen(lck->servicepath);
629 delete_token_size = (lck->delete_token ?
630 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
632 result.dsize = sizeof(*data) +
633 lck->num_share_modes * sizeof(struct share_mode_entry) +
636 strlen(lck->filename) + 1;
637 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
639 if (result.dptr == NULL) {
640 smb_panic("talloc failed");
643 data = (struct locking_data *)result.dptr;
645 data->u.s.num_share_mode_entries = lck->num_share_modes;
646 data->u.s.delete_on_close = lck->delete_on_close;
647 data->u.s.delete_token_size = delete_token_size;
648 DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
649 data->u.s.delete_on_close,
650 (unsigned int)data->u.s.delete_token_size,
651 data->u.s.num_share_mode_entries));
652 memcpy(result.dptr + sizeof(*data), lck->share_modes,
653 sizeof(struct share_mode_entry)*lck->num_share_modes);
654 offset = sizeof(*data) +
655 sizeof(struct share_mode_entry)*lck->num_share_modes;
657 /* Store any delete on close token. */
658 if (lck->delete_token) {
659 uint8 *p = result.dptr + offset;
661 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
664 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
667 for (i = 0; i < lck->delete_token->ngroups; i++) {
668 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
671 offset = p - result.dptr;
674 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
675 result.dsize - offset - 1);
676 offset += sp_len + 1;
677 safe_strcpy((char *)result.dptr + offset, lck->filename,
678 result.dsize - offset - 1);
680 if (DEBUGLEVEL >= 10) {
681 print_share_mode_table(data);
687 static int share_mode_lock_destructor(struct share_mode_lock *lck)
692 if (!lck->modified) {
696 data = unparse_share_modes(lck);
698 if (data.dptr == NULL) {
700 /* There has been an entry before, delete it */
702 status = lck->record->delete_rec(lck->record);
703 if (!NT_STATUS_IS_OK(status)) {
704 DEBUG(0, ("delete_rec returned %s\n",
706 smb_panic("could not delete share entry");
712 status = lck->record->store(lck->record, data, TDB_REPLACE);
713 if (!NT_STATUS_IS_OK(status)) {
714 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
715 smb_panic("could not store share mode entry");
723 static BOOL fill_share_mode_lock(struct share_mode_lock *lck,
725 const char *servicepath,
727 TDB_DATA share_mode_data)
729 /* Ensure we set every field here as the destructor must be
730 valid even if parse_share_modes fails. */
732 lck->servicepath = NULL;
733 lck->filename = NULL;
735 lck->num_share_modes = 0;
736 lck->share_modes = NULL;
737 lck->delete_token = NULL;
738 lck->delete_on_close = False;
740 lck->modified = False;
742 lck->fresh = (share_mode_data.dptr == NULL);
745 if (fname == NULL || servicepath == NULL) {
748 lck->filename = talloc_strdup(lck, fname);
749 lck->servicepath = talloc_strdup(lck, servicepath);
750 if (lck->filename == NULL || lck->servicepath == NULL) {
751 DEBUG(0, ("talloc failed\n"));
755 if (!parse_share_modes(share_mode_data, lck)) {
756 DEBUG(0, ("Could not parse share modes\n"));
764 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
766 const char *servicepath,
769 struct share_mode_lock *lck;
772 key.dptr = (unsigned char *)&id;
773 key.dsize = sizeof(id);
775 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
776 DEBUG(0, ("talloc failed\n"));
780 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
781 DEBUG(3, ("Could not lock share entry\n"));
786 if (!fill_share_mode_lock(lck, id, servicepath, fname,
787 lck->record->value)) {
788 DEBUG(3, ("fill_share_mode_lock failed\n"));
793 talloc_set_destructor(lck, share_mode_lock_destructor);
798 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
800 const char *servicepath,
803 struct share_mode_lock *lck;
804 TDB_DATA key = locking_key(id);
807 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
808 DEBUG(0, ("talloc failed\n"));
812 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
813 DEBUG(3, ("Could not fetch share entry\n"));
818 if (!fill_share_mode_lock(lck, id, servicepath, fname, data)) {
819 DEBUG(3, ("fill_share_mode_lock failed\n"));
824 TALLOC_FREE(data.dptr);
829 /*******************************************************************
830 Sets the service name and filename for rename.
831 At this point we emit "file renamed" messages to all
832 process id's that have this file open.
833 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
834 ********************************************************************/
836 BOOL rename_share_filename(struct messaging_context *msg_ctx,
837 struct share_mode_lock *lck,
838 const char *servicepath,
847 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
848 servicepath, newname));
851 * rename_internal_fsp() and rename_internals() add './' to
852 * head of newname if newname does not contain a '/'.
854 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
858 lck->servicepath = talloc_strdup(lck, servicepath);
859 lck->filename = talloc_strdup(lck, newname);
860 if (lck->filename == NULL || lck->servicepath == NULL) {
861 DEBUG(0, ("rename_share_filename: talloc failed\n"));
864 lck->modified = True;
866 sp_len = strlen(lck->servicepath);
867 fn_len = strlen(lck->filename);
869 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
871 /* Set up the name changed message. */
872 frm = TALLOC_ARRAY(lck, char, msg_len);
877 push_file_id_16(frm, &lck->id);
879 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
881 safe_strcpy(&frm[16], lck->servicepath, sp_len);
882 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
884 /* Send the messages. */
885 for (i=0; i<lck->num_share_modes; i++) {
886 struct share_mode_entry *se = &lck->share_modes[i];
887 if (!is_valid_share_mode_entry(se)) {
890 /* But not to ourselves... */
891 if (procid_is_me(&se->pid)) {
895 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
896 "file_id %s sharepath %s newname %s\n",
897 procid_str_static(&se->pid),
898 file_id_static_string(&lck->id),
899 lck->servicepath, lck->filename ));
901 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
902 (uint8 *)frm, msg_len);
908 BOOL get_delete_on_close_flag(struct file_id id)
911 struct share_mode_lock *lck;
913 if (!(lck = fetch_share_mode_unlocked(NULL, id, NULL, NULL))) {
916 result = lck->delete_on_close;
921 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
925 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
926 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
927 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
929 SMB_ASSERT(num_props <= 1);
930 return (num_props != 0);
933 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
935 return (e->op_type == DEFERRED_OPEN_ENTRY);
938 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
940 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
943 /*******************************************************************
944 Fill a share mode entry.
945 ********************************************************************/
947 static void fill_share_mode_entry(struct share_mode_entry *e,
949 uid_t uid, uint16 mid, uint16 op_type)
952 e->pid = procid_self();
953 e->share_access = fsp->share_access;
954 e->private_options = fsp->fh->private_options;
955 e->access_mask = fsp->access_mask;
957 e->op_type = op_type;
958 e->time.tv_sec = fsp->open_time.tv_sec;
959 e->time.tv_usec = fsp->open_time.tv_usec;
960 e->id = fsp->file_id;
961 e->share_file_id = fsp->fh->gen_id;
962 e->uid = (uint32)uid;
963 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
966 static void fill_deferred_open_entry(struct share_mode_entry *e,
967 const struct timeval request_time,
968 struct file_id id, uint16 mid)
971 e->pid = procid_self();
973 e->op_type = DEFERRED_OPEN_ENTRY;
974 e->time.tv_sec = request_time.tv_sec;
975 e->time.tv_usec = request_time.tv_usec;
981 static void add_share_mode_entry(struct share_mode_lock *lck,
982 const struct share_mode_entry *entry)
986 for (i=0; i<lck->num_share_modes; i++) {
987 struct share_mode_entry *e = &lck->share_modes[i];
988 if (is_unused_share_mode_entry(e)) {
994 if (i == lck->num_share_modes) {
995 /* No unused entry found */
996 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
997 &lck->share_modes, &lck->num_share_modes);
999 lck->modified = True;
1002 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1003 uid_t uid, uint16 mid, uint16 op_type, BOOL initial_delete_on_close_allowed)
1005 struct share_mode_entry entry;
1006 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1007 if (initial_delete_on_close_allowed) {
1008 entry.flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1010 add_share_mode_entry(lck, &entry);
1013 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
1014 struct timeval request_time,
1017 struct share_mode_entry entry;
1018 fill_deferred_open_entry(&entry, request_time, id, mid);
1019 add_share_mode_entry(lck, &entry);
1022 /*******************************************************************
1023 Check if two share mode entries are identical, ignoring oplock
1024 and mid info and desired_access. (Removed paranoia test - it's
1025 not automatically a logic error if they are identical. JRA.)
1026 ********************************************************************/
1028 static BOOL share_modes_identical(struct share_mode_entry *e1,
1029 struct share_mode_entry *e2)
1031 /* We used to check for e1->share_access == e2->share_access here
1032 as well as the other fields but 2 different DOS or FCB opens
1033 sharing the same share mode entry may validly differ in
1034 fsp->share_access field. */
1036 return (procid_equal(&e1->pid, &e2->pid) &&
1037 file_id_equal(&e1->id, &e2->id) &&
1038 e1->share_file_id == e2->share_file_id );
1041 static BOOL deferred_open_identical(struct share_mode_entry *e1,
1042 struct share_mode_entry *e2)
1044 return (procid_equal(&e1->pid, &e2->pid) &&
1045 (e1->op_mid == e2->op_mid) &&
1046 file_id_equal(&e1->id, &e2->id));
1049 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1050 struct share_mode_entry *entry)
1054 for (i=0; i<lck->num_share_modes; i++) {
1055 struct share_mode_entry *e = &lck->share_modes[i];
1056 if (is_valid_share_mode_entry(entry) &&
1057 is_valid_share_mode_entry(e) &&
1058 share_modes_identical(e, entry)) {
1061 if (is_deferred_open_entry(entry) &&
1062 is_deferred_open_entry(e) &&
1063 deferred_open_identical(e, entry)) {
1070 /*******************************************************************
1071 Del the share mode of a file for this process. Return the number of
1073 ********************************************************************/
1075 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1077 struct share_mode_entry entry, *e;
1079 /* Don't care about the pid owner being correct here - just a search. */
1080 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1082 e = find_share_mode_entry(lck, &entry);
1087 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1088 lck->modified = True;
1092 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1094 struct share_mode_entry entry, *e;
1096 fill_deferred_open_entry(&entry, timeval_zero(),
1099 e = find_share_mode_entry(lck, &entry);
1104 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1105 lck->modified = True;
1108 /*******************************************************************
1109 Remove an oplock mid and mode entry from a share mode.
1110 ********************************************************************/
1112 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1114 struct share_mode_entry entry, *e;
1116 /* Don't care about the pid owner being correct here - just a search. */
1117 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1119 e = find_share_mode_entry(lck, &entry);
1125 e->op_type = NO_OPLOCK;
1126 lck->modified = True;
1130 /*******************************************************************
1131 Downgrade a oplock type from exclusive to level II.
1132 ********************************************************************/
1134 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1136 struct share_mode_entry entry, *e;
1138 /* Don't care about the pid owner being correct here - just a search. */
1139 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1141 e = find_share_mode_entry(lck, &entry);
1146 e->op_type = LEVEL_II_OPLOCK;
1147 lck->modified = True;
1151 /****************************************************************************
1152 Deal with the internal needs of setting the delete on close flag. Note that
1153 as the tdb locking is recursive, it is safe to call this from within
1154 open_file_ntcreate. JRA.
1155 ****************************************************************************/
1157 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1160 if (!delete_on_close) {
1161 return NT_STATUS_OK;
1165 * Only allow delete on close for writable files.
1168 if ((dosmode & aRONLY) &&
1169 !lp_delete_readonly(SNUM(fsp->conn))) {
1170 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1171 "flag set but file attribute is readonly.\n",
1173 return NT_STATUS_CANNOT_DELETE;
1177 * Only allow delete on close for writable shares.
1180 if (!CAN_WRITE(fsp->conn)) {
1181 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1182 "close flag set but write access denied on share.\n",
1184 return NT_STATUS_ACCESS_DENIED;
1188 * Only allow delete on close for files/directories opened with delete
1192 if (!(fsp->access_mask & DELETE_ACCESS)) {
1193 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1194 "close flag set but delete access denied.\n",
1196 return NT_STATUS_ACCESS_DENIED;
1199 /* Don't allow delete on close for non-empty directories. */
1200 if (fsp->is_directory) {
1201 return can_delete_directory(fsp->conn, fsp->fsp_name);
1204 return NT_STATUS_OK;
1207 /****************************************************************************
1208 Do we have an open file handle that created this entry ?
1209 ****************************************************************************/
1211 BOOL can_set_initial_delete_on_close(const struct share_mode_lock *lck)
1215 for (i=0; i<lck->num_share_modes; i++) {
1216 if (lck->share_modes[i].flags & SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE) {
1223 /*************************************************************************
1224 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1225 (Should this be in locking.c.... ?).
1226 *************************************************************************/
1228 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1230 UNIX_USER_TOKEN *cpy;
1236 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1241 cpy->uid = tok->uid;
1242 cpy->gid = tok->gid;
1243 cpy->ngroups = tok->ngroups;
1245 /* Make this a talloc child of cpy. */
1246 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1250 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1255 /****************************************************************************
1256 Replace the delete on close token.
1257 ****************************************************************************/
1259 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1261 /* Ensure there's no token. */
1262 if (lck->delete_token) {
1263 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1264 lck->delete_token = NULL;
1267 /* Copy the new token (can be NULL). */
1268 lck->delete_token = copy_unix_token(lck, tok);
1269 lck->modified = True;
1272 /****************************************************************************
1273 Sets the delete on close flag over all share modes on this file.
1274 Modify the share mode entry for all files open
1275 on this device and inode to tell other smbds we have
1276 changed the delete on close flag. This will be noticed
1277 in the close code, the last closer will delete the file
1279 This makes a copy of any UNIX_USER_TOKEN into the
1280 lck entry. This function is used when the lock is already granted.
1281 ****************************************************************************/
1283 void set_delete_on_close_lck(struct share_mode_lock *lck, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1285 if (lck->delete_on_close != delete_on_close) {
1286 set_delete_on_close_token(lck, tok);
1287 lck->delete_on_close = delete_on_close;
1288 if (delete_on_close) {
1289 SMB_ASSERT(lck->delete_token != NULL);
1291 lck->modified = True;
1295 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1297 struct share_mode_lock *lck;
1299 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1300 "fnum = %d, file %s\n",
1301 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1308 lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
1313 set_delete_on_close_lck(lck, delete_on_close, tok);
1315 if (fsp->is_directory) {
1316 send_stat_cache_delete_message(fsp->fsp_name);
1323 /****************************************************************************
1324 Sets the allow initial delete on close flag for this share mode.
1325 ****************************************************************************/
1327 BOOL set_allow_initial_delete_on_close(struct share_mode_lock *lck, files_struct *fsp, BOOL delete_on_close)
1329 struct share_mode_entry entry, *e;
1331 /* Don't care about the pid owner being correct here - just a search. */
1332 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1334 e = find_share_mode_entry(lck, &entry);
1339 if (delete_on_close) {
1340 e->flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1342 e->flags &= ~SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1344 lck->modified = True;
1348 struct forall_state {
1349 void (*fn)(const struct share_mode_entry *entry,
1350 const char *sharepath,
1352 void *private_data);
1356 static int traverse_fn(struct db_record *rec, void *_state)
1358 struct forall_state *state = (struct forall_state *)_state;
1359 struct locking_data *data;
1360 struct share_mode_entry *shares;
1361 const char *sharepath;
1365 /* Ensure this is a locking_key record. */
1366 if (rec->key.dsize != sizeof(struct file_id))
1369 data = (struct locking_data *)rec->value.dptr;
1370 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1371 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1372 data->u.s.num_share_mode_entries*sizeof(*shares) +
1373 data->u.s.delete_token_size;
1374 fname = (const char *)rec->value.dptr + sizeof(*data) +
1375 data->u.s.num_share_mode_entries*sizeof(*shares) +
1376 data->u.s.delete_token_size +
1377 strlen(sharepath) + 1;
1379 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1380 state->fn(&shares[i], sharepath, fname,
1381 state->private_data);
1386 /*******************************************************************
1387 Call the specified function on each entry under management by the
1389 ********************************************************************/
1391 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1392 const char *, void *),
1395 struct forall_state state;
1397 if (lock_db == NULL)
1401 state.private_data = private_data;
1403 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);