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.
39 #include "librpc/gen_ndr/messaging.h"
42 #define DBGC_CLASS DBGC_LOCKING
44 #define NO_LOCKING_COUNT (-1)
46 /* the locking database handle */
47 static struct db_context *lock_db;
49 /****************************************************************************
51 ****************************************************************************/
53 const char *lock_type_name(enum brl_type lock_type)
60 case PENDING_READ_LOCK:
61 return "PENDING_READ";
62 case PENDING_WRITE_LOCK:
63 return "PENDING_WRITE";
69 const char *lock_flav_name(enum brl_flavour lock_flav)
71 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
74 /****************************************************************************
75 Utility function called to see if a file region is locked.
76 Called in the read/write codepath.
77 ****************************************************************************/
79 void init_strict_lock_struct(files_struct *fsp,
83 enum brl_type lock_type,
84 struct lock_struct *plock)
86 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
88 plock->context.smbpid = smbpid;
89 plock->context.tid = fsp->conn->cnum;
90 plock->context.pid = procid_self();
93 plock->fnum = fsp->fnum;
94 plock->lock_type = lock_type;
95 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
98 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
100 int strict_locking = lp_strict_locking(fsp->conn->params);
103 if (plock->size == 0) {
107 if (!lp_locking(fsp->conn->params) || !strict_locking) {
111 if (strict_locking == Auto) {
112 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
113 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
115 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
116 (plock->lock_type == READ_LOCK)) {
117 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
120 struct byte_range_lock *br_lck;
122 br_lck = brl_get_locks_readonly(fsp);
126 ret = brl_locktest(br_lck,
127 plock->context.smbpid,
135 struct byte_range_lock *br_lck;
137 br_lck = brl_get_locks_readonly(fsp);
141 ret = brl_locktest(br_lck,
142 plock->context.smbpid,
150 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
151 "len=%.0f %s for fnum %d file %s\n",
152 lock_flav_name(plock->lock_flav),
153 (double)plock->start, (double)plock->size,
154 ret ? "unlocked" : "locked",
155 plock->fnum, fsp_str_dbg(fsp)));
160 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
164 /****************************************************************************
165 Find out if a lock could be granted - return who is blocking us if we can't.
166 ****************************************************************************/
168 NTSTATUS query_lock(files_struct *fsp,
172 enum brl_type *plock_type,
173 enum brl_flavour lock_flav)
175 struct byte_range_lock *br_lck = NULL;
177 if (!fsp->can_lock) {
178 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
181 if (!lp_locking(fsp->conn->params)) {
185 br_lck = brl_get_locks_readonly(fsp);
187 return NT_STATUS_NO_MEMORY;
190 return brl_lockquery(br_lck,
199 static void increment_current_lock_count(files_struct *fsp,
200 enum brl_flavour lock_flav)
202 if (lock_flav == WINDOWS_LOCK &&
203 fsp->current_lock_count != NO_LOCKING_COUNT) {
204 /* blocking ie. pending, locks also count here,
205 * as this is an efficiency counter to avoid checking
206 * the lock db. on close. JRA. */
208 fsp->current_lock_count++;
210 /* Notice that this has had a POSIX lock request.
211 * We can't count locks after this so forget them.
213 fsp->current_lock_count = NO_LOCKING_COUNT;
217 static void decrement_current_lock_count(files_struct *fsp,
218 enum brl_flavour lock_flav)
220 if (lock_flav == WINDOWS_LOCK &&
221 fsp->current_lock_count != NO_LOCKING_COUNT) {
222 SMB_ASSERT(fsp->current_lock_count > 0);
223 fsp->current_lock_count--;
227 /****************************************************************************
228 Utility function called by locking requests.
229 ****************************************************************************/
231 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
236 enum brl_type lock_type,
237 enum brl_flavour lock_flav,
241 struct blocking_lock_record *blr)
243 struct byte_range_lock *br_lck = NULL;
245 /* silently return ok on print files as we don't do locking there */
246 if (fsp->print_file) {
247 *perr = NT_STATUS_OK;
251 if (!fsp->can_lock) {
252 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
256 if (!lp_locking(fsp->conn->params)) {
257 *perr = NT_STATUS_OK;
261 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
263 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
264 "blocking_lock=%s requested for fnum %d file %s\n",
265 lock_flav_name(lock_flav), lock_type_name(lock_type),
266 (double)offset, (double)count, blocking_lock ? "true" :
267 "false", fsp->fnum, fsp_str_dbg(fsp)));
269 br_lck = brl_get_locks(talloc_tos(), fsp);
271 *perr = NT_STATUS_NO_MEMORY;
275 *perr = brl_lock(msg_ctx,
287 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
289 increment_current_lock_count(fsp, lock_flav);
293 /****************************************************************************
294 Utility function called by unlocking requests.
295 ****************************************************************************/
297 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
302 enum brl_flavour lock_flav)
305 struct byte_range_lock *br_lck = NULL;
307 if (!fsp->can_lock) {
308 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
311 if (!lp_locking(fsp->conn->params)) {
315 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
316 (double)offset, (double)count, fsp->fnum,
319 br_lck = brl_get_locks(talloc_tos(), fsp);
321 return NT_STATUS_NO_MEMORY;
324 ok = brl_unlock(msg_ctx,
335 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
336 return NT_STATUS_RANGE_NOT_LOCKED;
339 decrement_current_lock_count(fsp, lock_flav);
343 /****************************************************************************
344 Cancel any pending blocked locks.
345 ****************************************************************************/
347 NTSTATUS do_lock_cancel(files_struct *fsp,
351 enum brl_flavour lock_flav,
352 struct blocking_lock_record *blr)
355 struct byte_range_lock *br_lck = NULL;
357 if (!fsp->can_lock) {
358 return fsp->is_directory ?
359 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
362 if (!lp_locking(fsp->conn->params)) {
363 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
366 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
367 (double)offset, (double)count, fsp->fnum,
370 br_lck = brl_get_locks(talloc_tos(), fsp);
372 return NT_STATUS_NO_MEMORY;
375 ok = brl_lock_cancel(br_lck,
386 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
387 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
390 decrement_current_lock_count(fsp, lock_flav);
394 /****************************************************************************
395 Remove any locks on this fd. Called from file_close().
396 ****************************************************************************/
398 void locking_close_file(struct messaging_context *msg_ctx,
401 struct byte_range_lock *br_lck;
403 if (!lp_locking(fsp->conn->params)) {
407 /* If we have not outstanding locks or pending
408 * locks then we don't need to look in the lock db.
411 if (fsp->current_lock_count == 0) {
415 br_lck = brl_get_locks(talloc_tos(),fsp);
418 cancel_pending_lock_requests_by_fid(fsp, br_lck);
419 brl_close_fnum(msg_ctx, br_lck);
424 /****************************************************************************
425 Initialise the locking functions.
426 ****************************************************************************/
428 static bool locking_init_internal(bool read_only)
435 lock_db = db_open(NULL, lock_path("locking.tdb"),
436 lp_open_files_db_hash_size(),
437 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
438 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
441 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
445 if (!posix_locking_init(read_only))
451 bool locking_init(void)
453 return locking_init_internal(false);
456 bool locking_init_readonly(void)
458 return locking_init_internal(true);
461 /*******************************************************************
462 Deinitialize the share_mode management.
463 ******************************************************************/
465 bool locking_end(void)
468 TALLOC_FREE(lock_db);
472 /*******************************************************************
473 Form a static locking key for a dev/inode pair.
474 ******************************************************************/
476 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
479 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
482 /*******************************************************************
483 Print out a share mode.
484 ********************************************************************/
486 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
488 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
489 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
490 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
491 "uid = %u, flags = %u, file_id %s",
493 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
494 procid_str_static(&e->pid),
495 e->share_access, e->private_options,
496 e->access_mask, (unsigned long long)e->op_mid,
497 e->op_type, e->share_file_id,
498 (unsigned int)e->uid, (unsigned int)e->flags,
499 file_id_string_tos(&e->id));
502 /*******************************************************************
503 Print out a share mode table.
504 ********************************************************************/
506 static void print_share_mode_table(struct locking_data *data)
508 int num_share_modes = data->u.s.num_share_mode_entries;
509 struct share_mode_entry *shares =
510 (struct share_mode_entry *)(data + 1);
513 for (i = 0; i < num_share_modes; i++) {
514 struct share_mode_entry entry;
518 * We need to memcpy the entry here due to alignment
519 * restrictions that are not met when directly accessing
523 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
524 str = share_mode_str(talloc_tos(), i, &entry);
526 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
531 /*******************************************************************
532 Get all share mode entries for a dev/inode pair.
533 ********************************************************************/
535 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
537 struct locking_data data;
540 if (dbuf.dsize < sizeof(struct locking_data)) {
541 smb_panic("parse_share_modes: buffer too short");
544 memcpy(&data, dbuf.dptr, sizeof(data));
546 lck->delete_on_close = data.u.s.delete_on_close;
547 lck->old_write_time = data.u.s.old_write_time;
548 lck->changed_write_time = data.u.s.changed_write_time;
549 lck->num_share_modes = data.u.s.num_share_mode_entries;
551 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
552 "cwrt: %s, tok: %u, num_share_modes: %d\n",
553 lck->delete_on_close,
554 timestring(talloc_tos(),
555 convert_timespec_to_time_t(lck->old_write_time)),
556 timestring(talloc_tos(),
557 convert_timespec_to_time_t(
558 lck->changed_write_time)),
559 (unsigned int)data.u.s.delete_token_size,
560 lck->num_share_modes));
562 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
563 DEBUG(0, ("invalid number of share modes: %d\n",
564 lck->num_share_modes));
565 smb_panic("parse_share_modes: invalid number of share modes");
568 lck->share_modes = NULL;
570 if (lck->num_share_modes != 0) {
572 if (dbuf.dsize < (sizeof(struct locking_data) +
573 (lck->num_share_modes *
574 sizeof(struct share_mode_entry)))) {
575 smb_panic("parse_share_modes: buffer too short");
578 lck->share_modes = (struct share_mode_entry *)
580 dbuf.dptr+sizeof(struct locking_data),
581 lck->num_share_modes *
582 sizeof(struct share_mode_entry));
584 if (lck->share_modes == NULL) {
585 smb_panic("parse_share_modes: talloc failed");
589 /* Get any delete token. */
590 if (data.u.s.delete_token_size) {
591 uint8 *p = dbuf.dptr + sizeof(struct locking_data) +
592 (lck->num_share_modes *
593 sizeof(struct share_mode_entry));
595 if ((data.u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
596 ((data.u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
597 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
598 data.u.s.delete_token_size));
599 smb_panic("parse_share_modes: invalid token size");
602 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
603 if (!lck->delete_token) {
604 smb_panic("parse_share_modes: talloc failed");
607 /* Copy out the uid and gid. */
608 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
610 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
613 /* Any supplementary groups ? */
614 lck->delete_token->ngroups = (data.u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
615 ((data.u.s.delete_token_size -
616 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
618 if (lck->delete_token->ngroups) {
619 /* Make this a talloc child of lck->delete_token. */
620 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
621 lck->delete_token->ngroups);
622 if (!lck->delete_token) {
623 smb_panic("parse_share_modes: talloc failed");
626 for (i = 0; i < lck->delete_token->ngroups; i++) {
627 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
633 lck->delete_token = NULL;
636 /* Save off the associated service path and filename. */
637 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
638 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
639 data.u.s.delete_token_size;
641 lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
642 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
643 data.u.s.delete_token_size +
644 strlen(lck->servicepath) + 1;
646 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
647 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
648 data.u.s.delete_token_size +
649 strlen(lck->servicepath) + 1 +
650 strlen(lck->base_name) + 1;
653 * Ensure that each entry has a real process attached.
656 for (i = 0; i < lck->num_share_modes; i++) {
657 struct share_mode_entry *entry_p = &lck->share_modes[i];
659 if (DEBUGLEVEL >= 10) {
660 str = share_mode_str(NULL, i, entry_p);
662 DEBUG(10,("parse_share_modes: %s\n",
664 if (!serverid_exists(&entry_p->pid)) {
665 DEBUG(10,("parse_share_modes: deleted %s\n",
667 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
668 lck->modified = True;
676 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
681 struct locking_data *data;
683 ssize_t sp_len, bn_len, sn_len;
684 uint32 delete_token_size;
689 for (i=0; i<lck->num_share_modes; i++) {
690 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
695 if (num_valid == 0) {
699 sp_len = strlen(lck->servicepath);
700 bn_len = strlen(lck->base_name);
701 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
703 delete_token_size = (lck->delete_token ?
704 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
706 result.dsize = sizeof(*data) +
707 lck->num_share_modes * sizeof(struct share_mode_entry) +
712 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
714 if (result.dptr == NULL) {
715 smb_panic("talloc failed");
718 data = (struct locking_data *)result.dptr;
720 data->u.s.num_share_mode_entries = lck->num_share_modes;
721 data->u.s.delete_on_close = lck->delete_on_close;
722 data->u.s.old_write_time = lck->old_write_time;
723 data->u.s.changed_write_time = lck->changed_write_time;
724 data->u.s.delete_token_size = delete_token_size;
726 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
727 "num: %d\n", data->u.s.delete_on_close,
728 timestring(talloc_tos(),
729 convert_timespec_to_time_t(lck->old_write_time)),
730 timestring(talloc_tos(),
731 convert_timespec_to_time_t(
732 lck->changed_write_time)),
733 (unsigned int)data->u.s.delete_token_size,
734 data->u.s.num_share_mode_entries));
736 memcpy(result.dptr + sizeof(*data), lck->share_modes,
737 sizeof(struct share_mode_entry)*lck->num_share_modes);
738 offset = sizeof(*data) +
739 sizeof(struct share_mode_entry)*lck->num_share_modes;
741 /* Store any delete on close token. */
742 if (lck->delete_token) {
743 uint8 *p = result.dptr + offset;
745 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
748 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
751 for (i = 0; i < lck->delete_token->ngroups; i++) {
752 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
755 offset = p - result.dptr;
758 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
759 result.dsize - offset - 1);
760 offset += sp_len + 1;
761 safe_strcpy((char *)result.dptr + offset, lck->base_name,
762 result.dsize - offset - 1);
763 offset += bn_len + 1;
764 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
765 result.dsize - offset - 1);
767 if (DEBUGLEVEL >= 10) {
768 print_share_mode_table(data);
774 static int share_mode_lock_destructor(struct share_mode_lock *lck)
779 if (!lck->modified) {
783 data = unparse_share_modes(lck);
785 if (data.dptr == NULL) {
787 /* There has been an entry before, delete it */
789 status = lck->record->delete_rec(lck->record);
790 if (!NT_STATUS_IS_OK(status)) {
793 DEBUG(0, ("delete_rec returned %s\n",
796 if (asprintf(&errmsg, "could not delete share "
798 nt_errstr(status)) == -1) {
799 smb_panic("could not delete share"
808 status = lck->record->store(lck->record, data, TDB_REPLACE);
809 if (!NT_STATUS_IS_OK(status)) {
812 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
814 if (asprintf(&errmsg, "could not store share mode entry: %s",
815 nt_errstr(status)) == -1) {
816 smb_panic("could not store share mode entry");
826 static bool fill_share_mode_lock(struct share_mode_lock *lck,
828 const char *servicepath,
829 const struct smb_filename *smb_fname,
830 TDB_DATA share_mode_data,
831 const struct timespec *old_write_time)
833 /* Ensure we set every field here as the destructor must be
834 valid even if parse_share_modes fails. */
836 lck->servicepath = NULL;
837 lck->base_name = NULL;
838 lck->stream_name = NULL;
840 lck->num_share_modes = 0;
841 lck->share_modes = NULL;
842 lck->delete_token = NULL;
843 lck->delete_on_close = False;
844 ZERO_STRUCT(lck->old_write_time);
845 ZERO_STRUCT(lck->changed_write_time);
847 lck->modified = False;
849 lck->fresh = (share_mode_data.dptr == NULL);
853 if (smb_fname == NULL || servicepath == NULL
854 || old_write_time == NULL) {
858 has_stream = smb_fname->stream_name != NULL;
860 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
861 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
862 lck->servicepath = talloc_strdup(lck, servicepath);
863 if (lck->base_name == NULL ||
864 (has_stream && lck->stream_name == NULL) ||
865 lck->servicepath == NULL) {
866 DEBUG(0, ("talloc failed\n"));
869 lck->old_write_time = *old_write_time;
871 if (!parse_share_modes(share_mode_data, lck)) {
872 DEBUG(0, ("Could not parse share modes\n"));
880 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
881 const struct file_id id,
882 const char *servicepath,
883 const struct smb_filename *smb_fname,
884 const struct timespec *old_write_time)
886 struct share_mode_lock *lck;
888 TDB_DATA key = locking_key(&id, &tmp);
890 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
891 DEBUG(0, ("talloc failed\n"));
895 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
896 DEBUG(3, ("Could not lock share entry\n"));
901 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
902 lck->record->value, old_write_time)) {
903 DEBUG(3, ("fill_share_mode_lock failed\n"));
908 talloc_set_destructor(lck, share_mode_lock_destructor);
913 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
914 const struct file_id id)
916 struct share_mode_lock *lck;
918 TDB_DATA key = locking_key(&id, &tmp);
921 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
922 DEBUG(0, ("talloc failed\n"));
926 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
927 DEBUG(3, ("Could not fetch share entry\n"));
932 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
933 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
934 "around (file not open)\n"));
942 /*******************************************************************
943 Sets the service name and filename for rename.
944 At this point we emit "file renamed" messages to all
945 process id's that have this file open.
946 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
947 ********************************************************************/
949 bool rename_share_filename(struct messaging_context *msg_ctx,
950 struct share_mode_lock *lck,
951 const char *servicepath,
952 const struct smb_filename *smb_fname_dst)
960 bool strip_two_chars = false;
961 bool has_stream = smb_fname_dst->stream_name != NULL;
963 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
964 servicepath, smb_fname_dst->base_name));
967 * rename_internal_fsp() and rename_internals() add './' to
968 * head of newname if newname does not contain a '/'.
970 if (smb_fname_dst->base_name[0] &&
971 smb_fname_dst->base_name[1] &&
972 smb_fname_dst->base_name[0] == '.' &&
973 smb_fname_dst->base_name[1] == '/') {
974 strip_two_chars = true;
977 lck->servicepath = talloc_strdup(lck, servicepath);
978 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
979 (strip_two_chars ? 2 : 0));
980 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
981 if (lck->base_name == NULL ||
982 (has_stream && lck->stream_name == NULL) ||
983 lck->servicepath == NULL) {
984 DEBUG(0, ("rename_share_filename: talloc failed\n"));
987 lck->modified = True;
989 sp_len = strlen(lck->servicepath);
990 bn_len = strlen(lck->base_name);
991 sn_len = has_stream ? strlen(lck->stream_name) : 0;
993 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
996 /* Set up the name changed message. */
997 frm = TALLOC_ARRAY(lck, char, msg_len);
1002 push_file_id_24(frm, &lck->id);
1004 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1006 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1007 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1008 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1011 /* Send the messages. */
1012 for (i=0; i<lck->num_share_modes; i++) {
1013 struct share_mode_entry *se = &lck->share_modes[i];
1014 if (!is_valid_share_mode_entry(se)) {
1017 /* But not to ourselves... */
1018 if (procid_is_me(&se->pid)) {
1022 DEBUG(10,("rename_share_filename: sending rename message to "
1023 "pid %s file_id %s sharepath %s base_name %s "
1025 procid_str_static(&se->pid),
1026 file_id_string_tos(&lck->id),
1027 lck->servicepath, lck->base_name,
1028 has_stream ? lck->stream_name : ""));
1030 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1031 (uint8 *)frm, msg_len);
1037 void get_file_infos(struct file_id id,
1038 bool *delete_on_close,
1039 struct timespec *write_time)
1041 struct share_mode_lock *lck;
1043 if (delete_on_close) {
1044 *delete_on_close = false;
1048 ZERO_STRUCTP(write_time);
1051 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1055 if (delete_on_close) {
1056 *delete_on_close = lck->delete_on_close;
1062 wt = lck->changed_write_time;
1063 if (null_timespec(wt)) {
1064 wt = lck->old_write_time;
1073 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1077 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1078 /* cope with dead entries from the process not
1079 existing. These should not be considered valid,
1080 otherwise we end up doing zero timeout sharing
1085 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1086 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1087 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1089 SMB_ASSERT(num_props <= 1);
1090 return (num_props != 0);
1093 bool is_deferred_open_entry(const struct share_mode_entry *e)
1095 return (e->op_type == DEFERRED_OPEN_ENTRY);
1098 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1100 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1103 /*******************************************************************
1104 Fill a share mode entry.
1105 ********************************************************************/
1107 static void fill_share_mode_entry(struct share_mode_entry *e,
1109 uid_t uid, uint64_t mid, uint16 op_type)
1112 e->pid = procid_self();
1113 e->share_access = fsp->share_access;
1114 e->private_options = fsp->fh->private_options;
1115 e->access_mask = fsp->access_mask;
1117 e->op_type = op_type;
1118 e->time.tv_sec = fsp->open_time.tv_sec;
1119 e->time.tv_usec = fsp->open_time.tv_usec;
1120 e->id = fsp->file_id;
1121 e->share_file_id = fsp->fh->gen_id;
1122 e->uid = (uint32)uid;
1123 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1126 static void fill_deferred_open_entry(struct share_mode_entry *e,
1127 const struct timeval request_time,
1128 struct file_id id, uint64_t mid)
1131 e->pid = procid_self();
1133 e->op_type = DEFERRED_OPEN_ENTRY;
1134 e->time.tv_sec = request_time.tv_sec;
1135 e->time.tv_usec = request_time.tv_usec;
1137 e->uid = (uint32)-1;
1141 static void add_share_mode_entry(struct share_mode_lock *lck,
1142 const struct share_mode_entry *entry)
1146 for (i=0; i<lck->num_share_modes; i++) {
1147 struct share_mode_entry *e = &lck->share_modes[i];
1148 if (is_unused_share_mode_entry(e)) {
1154 if (i == lck->num_share_modes) {
1155 /* No unused entry found */
1156 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1157 &lck->share_modes, &lck->num_share_modes);
1159 lck->modified = True;
1162 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1163 uid_t uid, uint64_t mid, uint16 op_type)
1165 struct share_mode_entry entry;
1166 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1167 add_share_mode_entry(lck, &entry);
1170 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1171 struct timeval request_time,
1174 struct share_mode_entry entry;
1175 fill_deferred_open_entry(&entry, request_time, id, mid);
1176 add_share_mode_entry(lck, &entry);
1179 /*******************************************************************
1180 Check if two share mode entries are identical, ignoring oplock
1181 and mid info and desired_access. (Removed paranoia test - it's
1182 not automatically a logic error if they are identical. JRA.)
1183 ********************************************************************/
1185 static bool share_modes_identical(struct share_mode_entry *e1,
1186 struct share_mode_entry *e2)
1188 /* We used to check for e1->share_access == e2->share_access here
1189 as well as the other fields but 2 different DOS or FCB opens
1190 sharing the same share mode entry may validly differ in
1191 fsp->share_access field. */
1193 return (procid_equal(&e1->pid, &e2->pid) &&
1194 file_id_equal(&e1->id, &e2->id) &&
1195 e1->share_file_id == e2->share_file_id );
1198 static bool deferred_open_identical(struct share_mode_entry *e1,
1199 struct share_mode_entry *e2)
1201 return (procid_equal(&e1->pid, &e2->pid) &&
1202 (e1->op_mid == e2->op_mid) &&
1203 file_id_equal(&e1->id, &e2->id));
1206 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1207 struct share_mode_entry *entry)
1211 for (i=0; i<lck->num_share_modes; i++) {
1212 struct share_mode_entry *e = &lck->share_modes[i];
1213 if (is_valid_share_mode_entry(entry) &&
1214 is_valid_share_mode_entry(e) &&
1215 share_modes_identical(e, entry)) {
1218 if (is_deferred_open_entry(entry) &&
1219 is_deferred_open_entry(e) &&
1220 deferred_open_identical(e, entry)) {
1227 /*******************************************************************
1228 Del the share mode of a file for this process. Return the number of
1230 ********************************************************************/
1232 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1234 struct share_mode_entry entry, *e;
1236 /* Don't care about the pid owner being correct here - just a search. */
1237 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1239 e = find_share_mode_entry(lck, &entry);
1244 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1245 lck->modified = True;
1249 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid)
1251 struct share_mode_entry entry, *e;
1253 fill_deferred_open_entry(&entry, timeval_zero(),
1256 e = find_share_mode_entry(lck, &entry);
1261 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1262 lck->modified = True;
1265 /*******************************************************************
1266 Remove an oplock mid and mode entry from a share mode.
1267 ********************************************************************/
1269 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1271 struct share_mode_entry entry, *e;
1273 /* Don't care about the pid owner being correct here - just a search. */
1274 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1276 e = find_share_mode_entry(lck, &entry);
1282 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1284 * Going from exclusive or batch,
1285 * we always go through FAKE_LEVEL_II
1288 e->op_type = FAKE_LEVEL_II_OPLOCK;
1290 e->op_type = NO_OPLOCK;
1292 lck->modified = True;
1296 /*******************************************************************
1297 Downgrade a oplock type from exclusive to level II.
1298 ********************************************************************/
1300 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1302 struct share_mode_entry entry, *e;
1304 /* Don't care about the pid owner being correct here - just a search. */
1305 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1307 e = find_share_mode_entry(lck, &entry);
1312 e->op_type = LEVEL_II_OPLOCK;
1313 lck->modified = True;
1317 /****************************************************************************
1318 Check if setting delete on close is allowed on this fsp.
1319 ****************************************************************************/
1321 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1324 * Only allow delete on close for writable files.
1327 if ((dosmode & aRONLY) &&
1328 !lp_delete_readonly(SNUM(fsp->conn))) {
1329 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1330 "flag set but file attribute is readonly.\n",
1332 return NT_STATUS_CANNOT_DELETE;
1336 * Only allow delete on close for writable shares.
1339 if (!CAN_WRITE(fsp->conn)) {
1340 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1341 "close flag set but write access denied on share.\n",
1343 return NT_STATUS_ACCESS_DENIED;
1347 * Only allow delete on close for files/directories opened with delete
1351 if (!(fsp->access_mask & DELETE_ACCESS)) {
1352 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1353 "close flag set but delete access denied.\n",
1355 return NT_STATUS_ACCESS_DENIED;
1358 /* Don't allow delete on close for non-empty directories. */
1359 if (fsp->is_directory) {
1360 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1361 return can_delete_directory(fsp->conn,
1362 fsp->fsp_name->base_name);
1365 return NT_STATUS_OK;
1368 /*************************************************************************
1369 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1370 (Should this be in locking.c.... ?).
1371 *************************************************************************/
1373 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, const UNIX_USER_TOKEN *tok)
1375 UNIX_USER_TOKEN *cpy;
1381 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1386 cpy->uid = tok->uid;
1387 cpy->gid = tok->gid;
1388 cpy->ngroups = tok->ngroups;
1390 /* Make this a talloc child of cpy. */
1391 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1395 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1400 /****************************************************************************
1401 Replace the delete on close token.
1402 ****************************************************************************/
1404 void set_delete_on_close_token(struct share_mode_lock *lck, const UNIX_USER_TOKEN *tok)
1406 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1408 /* Copy the new token (can be NULL). */
1409 lck->delete_token = copy_unix_token(lck, tok);
1410 lck->modified = True;
1413 /****************************************************************************
1414 Sets the delete on close flag over all share modes on this file.
1415 Modify the share mode entry for all files open
1416 on this device and inode to tell other smbds we have
1417 changed the delete on close flag. This will be noticed
1418 in the close code, the last closer will delete the file
1420 This makes a copy of any UNIX_USER_TOKEN into the
1421 lck entry. This function is used when the lock is already granted.
1422 ****************************************************************************/
1424 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1426 if (lck->delete_on_close != delete_on_close) {
1427 set_delete_on_close_token(lck, tok);
1428 lck->delete_on_close = delete_on_close;
1429 if (delete_on_close) {
1430 SMB_ASSERT(lck->delete_token != NULL);
1432 lck->modified = True;
1436 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1438 struct share_mode_lock *lck;
1440 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1441 "fnum = %d, file %s\n",
1442 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1445 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1451 set_delete_on_close_lck(lck, delete_on_close, tok);
1453 if (fsp->is_directory) {
1454 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1455 send_stat_cache_delete_message(fsp->fsp_name->base_name);
1460 fsp->delete_on_close = delete_on_close;
1465 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1467 struct share_mode_lock *lck;
1469 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1470 timestring(talloc_tos(),
1471 convert_timespec_to_time_t(write_time)),
1472 file_id_string_tos(&fileid)));
1474 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1479 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1480 lck->modified = True;
1481 lck->changed_write_time = write_time;
1488 bool set_write_time(struct file_id fileid, struct timespec write_time)
1490 struct share_mode_lock *lck;
1492 DEBUG(5,("set_write_time: %s id=%s\n",
1493 timestring(talloc_tos(),
1494 convert_timespec_to_time_t(write_time)),
1495 file_id_string_tos(&fileid)));
1497 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1502 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1503 lck->modified = True;
1504 lck->old_write_time = write_time;
1512 struct forall_state {
1513 void (*fn)(const struct share_mode_entry *entry,
1514 const char *sharepath,
1516 void *private_data);
1520 static int traverse_fn(struct db_record *rec, void *_state)
1522 struct forall_state *state = (struct forall_state *)_state;
1523 struct locking_data *data;
1524 struct share_mode_entry *shares;
1525 const char *sharepath;
1529 /* Ensure this is a locking_key record. */
1530 if (rec->key.dsize != sizeof(struct file_id))
1533 data = (struct locking_data *)rec->value.dptr;
1534 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1535 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1536 data->u.s.num_share_mode_entries*sizeof(*shares) +
1537 data->u.s.delete_token_size;
1538 fname = (const char *)rec->value.dptr + sizeof(*data) +
1539 data->u.s.num_share_mode_entries*sizeof(*shares) +
1540 data->u.s.delete_token_size +
1541 strlen(sharepath) + 1;
1543 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1544 state->fn(&shares[i], sharepath, fname,
1545 state->private_data);
1550 /*******************************************************************
1551 Call the specified function on each entry under management by the
1553 ********************************************************************/
1555 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1556 const char *, void *),
1559 struct forall_state state;
1561 if (lock_db == NULL)
1565 state.private_data = private_data;
1567 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);