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"
40 #include "smbd/globals.h"
42 #include "../libcli/security/security.h"
45 #define DBGC_CLASS DBGC_LOCKING
47 #define NO_LOCKING_COUNT (-1)
49 /* the locking database handle */
50 static struct db_context *lock_db;
52 /****************************************************************************
54 ****************************************************************************/
56 const char *lock_type_name(enum brl_type lock_type)
63 case PENDING_READ_LOCK:
64 return "PENDING_READ";
65 case PENDING_WRITE_LOCK:
66 return "PENDING_WRITE";
72 const char *lock_flav_name(enum brl_flavour lock_flav)
74 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
77 /****************************************************************************
78 Utility function called to see if a file region is locked.
79 Called in the read/write codepath.
80 ****************************************************************************/
82 void init_strict_lock_struct(files_struct *fsp,
86 enum brl_type lock_type,
87 struct lock_struct *plock)
89 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
91 plock->context.smblctx = smblctx;
92 plock->context.tid = fsp->conn->cnum;
93 plock->context.pid = sconn_server_id(fsp->conn->sconn);
96 plock->fnum = fsp->fnum;
97 plock->lock_type = lock_type;
98 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
101 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
103 int strict_locking = lp_strict_locking(fsp->conn->params);
106 if (plock->size == 0) {
110 if (!lp_locking(fsp->conn->params) || !strict_locking) {
114 if (strict_locking == Auto) {
115 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
116 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
118 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
119 (plock->lock_type == READ_LOCK)) {
120 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
123 struct byte_range_lock *br_lck;
125 br_lck = brl_get_locks_readonly(fsp);
129 ret = brl_locktest(br_lck,
130 plock->context.smblctx,
138 struct byte_range_lock *br_lck;
140 br_lck = brl_get_locks_readonly(fsp);
144 ret = brl_locktest(br_lck,
145 plock->context.smblctx,
153 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
154 "len=%.0f %s for fnum %d file %s\n",
155 lock_flav_name(plock->lock_flav),
156 (double)plock->start, (double)plock->size,
157 ret ? "unlocked" : "locked",
158 plock->fnum, fsp_str_dbg(fsp)));
163 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
167 /****************************************************************************
168 Find out if a lock could be granted - return who is blocking us if we can't.
169 ****************************************************************************/
171 NTSTATUS query_lock(files_struct *fsp,
175 enum brl_type *plock_type,
176 enum brl_flavour lock_flav)
178 struct byte_range_lock *br_lck = NULL;
180 if (!fsp->can_lock) {
181 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
184 if (!lp_locking(fsp->conn->params)) {
188 br_lck = brl_get_locks_readonly(fsp);
190 return NT_STATUS_NO_MEMORY;
193 return brl_lockquery(br_lck,
195 sconn_server_id(fsp->conn->sconn),
202 static void increment_current_lock_count(files_struct *fsp,
203 enum brl_flavour lock_flav)
205 if (lock_flav == WINDOWS_LOCK &&
206 fsp->current_lock_count != NO_LOCKING_COUNT) {
207 /* blocking ie. pending, locks also count here,
208 * as this is an efficiency counter to avoid checking
209 * the lock db. on close. JRA. */
211 fsp->current_lock_count++;
213 /* Notice that this has had a POSIX lock request.
214 * We can't count locks after this so forget them.
216 fsp->current_lock_count = NO_LOCKING_COUNT;
220 static void decrement_current_lock_count(files_struct *fsp,
221 enum brl_flavour lock_flav)
223 if (lock_flav == WINDOWS_LOCK &&
224 fsp->current_lock_count != NO_LOCKING_COUNT) {
225 SMB_ASSERT(fsp->current_lock_count > 0);
226 fsp->current_lock_count--;
230 /****************************************************************************
231 Utility function called by locking requests.
232 ****************************************************************************/
234 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
239 enum brl_type lock_type,
240 enum brl_flavour lock_flav,
244 struct blocking_lock_record *blr)
246 struct byte_range_lock *br_lck = NULL;
248 /* silently return ok on print files as we don't do locking there */
249 if (fsp->print_file) {
250 *perr = NT_STATUS_OK;
254 if (!fsp->can_lock) {
255 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
259 if (!lp_locking(fsp->conn->params)) {
260 *perr = NT_STATUS_OK;
264 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
266 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
267 "blocking_lock=%s requested for fnum %d file %s\n",
268 lock_flav_name(lock_flav), lock_type_name(lock_type),
269 (double)offset, (double)count, blocking_lock ? "true" :
270 "false", fsp->fnum, fsp_str_dbg(fsp)));
272 br_lck = brl_get_locks(talloc_tos(), fsp);
274 *perr = NT_STATUS_NO_MEMORY;
278 *perr = brl_lock(msg_ctx,
281 sconn_server_id(fsp->conn->sconn),
290 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
292 increment_current_lock_count(fsp, lock_flav);
296 /****************************************************************************
297 Utility function called by unlocking requests.
298 ****************************************************************************/
300 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
305 enum brl_flavour lock_flav)
308 struct byte_range_lock *br_lck = NULL;
310 if (!fsp->can_lock) {
311 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
314 if (!lp_locking(fsp->conn->params)) {
318 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
319 (double)offset, (double)count, fsp->fnum,
322 br_lck = brl_get_locks(talloc_tos(), fsp);
324 return NT_STATUS_NO_MEMORY;
327 ok = brl_unlock(msg_ctx,
330 sconn_server_id(fsp->conn->sconn),
338 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
339 return NT_STATUS_RANGE_NOT_LOCKED;
342 decrement_current_lock_count(fsp, lock_flav);
346 /****************************************************************************
347 Cancel any pending blocked locks.
348 ****************************************************************************/
350 NTSTATUS do_lock_cancel(files_struct *fsp,
354 enum brl_flavour lock_flav,
355 struct blocking_lock_record *blr)
358 struct byte_range_lock *br_lck = NULL;
360 if (!fsp->can_lock) {
361 return fsp->is_directory ?
362 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
365 if (!lp_locking(fsp->conn->params)) {
366 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
369 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
370 (double)offset, (double)count, fsp->fnum,
373 br_lck = brl_get_locks(talloc_tos(), fsp);
375 return NT_STATUS_NO_MEMORY;
378 ok = brl_lock_cancel(br_lck,
380 sconn_server_id(fsp->conn->sconn),
389 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
390 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
393 decrement_current_lock_count(fsp, lock_flav);
397 /****************************************************************************
398 Remove any locks on this fd. Called from file_close().
399 ****************************************************************************/
401 void locking_close_file(struct messaging_context *msg_ctx,
403 enum file_close_type close_type)
405 struct byte_range_lock *br_lck;
407 if (!lp_locking(fsp->conn->params)) {
411 /* If we have not outstanding locks or pending
412 * locks then we don't need to look in the lock db.
415 if (fsp->current_lock_count == 0) {
419 br_lck = brl_get_locks(talloc_tos(),fsp);
422 cancel_pending_lock_requests_by_fid(fsp, br_lck, close_type);
423 brl_close_fnum(msg_ctx, br_lck);
428 /****************************************************************************
429 Initialise the locking functions.
430 ****************************************************************************/
432 static bool locking_init_internal(bool read_only)
439 lock_db = db_open(NULL, lock_path("locking.tdb"),
440 lp_open_files_db_hash_size(),
441 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
442 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
445 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
449 if (!posix_locking_init(read_only))
455 bool locking_init(void)
457 return locking_init_internal(false);
460 bool locking_init_readonly(void)
462 return locking_init_internal(true);
465 /*******************************************************************
466 Deinitialize the share_mode management.
467 ******************************************************************/
469 bool locking_end(void)
472 TALLOC_FREE(lock_db);
476 /*******************************************************************
477 Form a static locking key for a dev/inode pair.
478 ******************************************************************/
480 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
483 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
486 /*******************************************************************
487 Print out a share mode.
488 ********************************************************************/
490 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
492 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
493 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
494 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
495 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
497 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
498 procid_str_static(&e->pid),
499 e->share_access, e->private_options,
500 e->access_mask, (unsigned long long)e->op_mid,
501 e->op_type, e->share_file_id,
502 (unsigned int)e->uid, (unsigned int)e->flags,
503 file_id_string_tos(&e->id),
504 (unsigned int)e->name_hash);
507 /*******************************************************************
508 Print out a share mode table.
509 ********************************************************************/
511 static void print_share_mode_table(struct locking_data *data)
513 int num_share_modes = data->u.s.num_share_mode_entries;
514 struct share_mode_entry *shares =
515 (struct share_mode_entry *)(data + 1);
518 for (i = 0; i < num_share_modes; i++) {
519 struct share_mode_entry entry;
523 * We need to memcpy the entry here due to alignment
524 * restrictions that are not met when directly accessing
528 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
529 str = share_mode_str(talloc_tos(), i, &entry);
531 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
536 /*******************************************************************
537 Get all share mode entries for a dev/inode pair.
538 ********************************************************************/
540 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
542 struct locking_data data;
545 if (dbuf.dsize < sizeof(struct locking_data)) {
546 smb_panic("parse_share_modes: buffer too short");
549 memcpy(&data, dbuf.dptr, sizeof(data));
551 lck->delete_on_close = data.u.s.delete_on_close;
552 lck->old_write_time = data.u.s.old_write_time;
553 lck->changed_write_time = data.u.s.changed_write_time;
554 lck->num_share_modes = data.u.s.num_share_mode_entries;
556 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
557 "cwrt: %s, tok: %u, num_share_modes: %d\n",
558 lck->delete_on_close,
559 timestring(talloc_tos(),
560 convert_timespec_to_time_t(lck->old_write_time)),
561 timestring(talloc_tos(),
562 convert_timespec_to_time_t(
563 lck->changed_write_time)),
564 (unsigned int)data.u.s.delete_token_size,
565 lck->num_share_modes));
567 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
568 DEBUG(0, ("invalid number of share modes: %d\n",
569 lck->num_share_modes));
570 smb_panic("parse_share_modes: invalid number of share modes");
573 lck->share_modes = NULL;
575 if (lck->num_share_modes != 0) {
577 if (dbuf.dsize < (sizeof(struct locking_data) +
578 (lck->num_share_modes *
579 sizeof(struct share_mode_entry)))) {
580 smb_panic("parse_share_modes: buffer too short");
583 lck->share_modes = (struct share_mode_entry *)
585 dbuf.dptr+sizeof(struct locking_data),
586 lck->num_share_modes *
587 sizeof(struct share_mode_entry));
589 if (lck->share_modes == NULL) {
590 smb_panic("parse_share_modes: talloc failed");
594 /* Get any delete token. */
595 if (data.u.s.delete_token_size) {
596 uint8 *p = dbuf.dptr + sizeof(struct locking_data) +
597 (lck->num_share_modes *
598 sizeof(struct share_mode_entry));
600 if ((data.u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
601 ((data.u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
602 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
603 data.u.s.delete_token_size));
604 smb_panic("parse_share_modes: invalid token size");
607 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
608 if (!lck->delete_token) {
609 smb_panic("parse_share_modes: talloc failed");
612 /* Copy out the uid and gid. */
613 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
615 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
618 /* Any supplementary groups ? */
619 lck->delete_token->ngroups = (data.u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
620 ((data.u.s.delete_token_size -
621 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
623 if (lck->delete_token->ngroups) {
624 /* Make this a talloc child of lck->delete_token. */
625 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
626 lck->delete_token->ngroups);
627 if (!lck->delete_token) {
628 smb_panic("parse_share_modes: talloc failed");
631 for (i = 0; i < lck->delete_token->ngroups; i++) {
632 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
638 lck->delete_token = NULL;
641 /* Save off the associated service path and filename. */
642 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
643 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
644 data.u.s.delete_token_size;
646 lck->base_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;
651 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
652 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
653 data.u.s.delete_token_size +
654 strlen(lck->servicepath) + 1 +
655 strlen(lck->base_name) + 1;
658 * Ensure that each entry has a real process attached.
661 for (i = 0; i < lck->num_share_modes; i++) {
662 struct share_mode_entry *entry_p = &lck->share_modes[i];
664 if (DEBUGLEVEL >= 10) {
665 str = share_mode_str(NULL, i, entry_p);
667 DEBUG(10,("parse_share_modes: %s\n",
669 if (!serverid_exists(&entry_p->pid)) {
670 DEBUG(10,("parse_share_modes: deleted %s\n",
672 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
673 lck->modified = True;
681 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
686 struct locking_data *data;
688 ssize_t sp_len, bn_len, sn_len;
689 uint32 delete_token_size;
694 for (i=0; i<lck->num_share_modes; i++) {
695 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
700 if (num_valid == 0) {
704 sp_len = strlen(lck->servicepath);
705 bn_len = strlen(lck->base_name);
706 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
708 delete_token_size = (lck->delete_token ?
709 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
711 result.dsize = sizeof(*data) +
712 lck->num_share_modes * sizeof(struct share_mode_entry) +
717 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
719 if (result.dptr == NULL) {
720 smb_panic("talloc failed");
723 data = (struct locking_data *)result.dptr;
725 data->u.s.num_share_mode_entries = lck->num_share_modes;
726 data->u.s.delete_on_close = lck->delete_on_close;
727 data->u.s.old_write_time = lck->old_write_time;
728 data->u.s.changed_write_time = lck->changed_write_time;
729 data->u.s.delete_token_size = delete_token_size;
731 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
732 "num: %d\n", data->u.s.delete_on_close,
733 timestring(talloc_tos(),
734 convert_timespec_to_time_t(lck->old_write_time)),
735 timestring(talloc_tos(),
736 convert_timespec_to_time_t(
737 lck->changed_write_time)),
738 (unsigned int)data->u.s.delete_token_size,
739 data->u.s.num_share_mode_entries));
741 memcpy(result.dptr + sizeof(*data), lck->share_modes,
742 sizeof(struct share_mode_entry)*lck->num_share_modes);
743 offset = sizeof(*data) +
744 sizeof(struct share_mode_entry)*lck->num_share_modes;
746 /* Store any delete on close token. */
747 if (lck->delete_token) {
748 uint8 *p = result.dptr + offset;
750 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
753 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
756 for (i = 0; i < lck->delete_token->ngroups; i++) {
757 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
760 offset = p - result.dptr;
763 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
764 result.dsize - offset - 1);
765 offset += sp_len + 1;
766 safe_strcpy((char *)result.dptr + offset, lck->base_name,
767 result.dsize - offset - 1);
768 offset += bn_len + 1;
769 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
770 result.dsize - offset - 1);
772 if (DEBUGLEVEL >= 10) {
773 print_share_mode_table(data);
779 static int share_mode_lock_destructor(struct share_mode_lock *lck)
784 if (!lck->modified) {
788 data = unparse_share_modes(lck);
790 if (data.dptr == NULL) {
792 /* There has been an entry before, delete it */
794 status = lck->record->delete_rec(lck->record);
795 if (!NT_STATUS_IS_OK(status)) {
798 DEBUG(0, ("delete_rec returned %s\n",
801 if (asprintf(&errmsg, "could not delete share "
803 nt_errstr(status)) == -1) {
804 smb_panic("could not delete share"
813 status = lck->record->store(lck->record, data, TDB_REPLACE);
814 if (!NT_STATUS_IS_OK(status)) {
817 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
819 if (asprintf(&errmsg, "could not store share mode entry: %s",
820 nt_errstr(status)) == -1) {
821 smb_panic("could not store share mode entry");
831 static bool fill_share_mode_lock(struct share_mode_lock *lck,
833 const char *servicepath,
834 const struct smb_filename *smb_fname,
835 TDB_DATA share_mode_data,
836 const struct timespec *old_write_time)
838 /* Ensure we set every field here as the destructor must be
839 valid even if parse_share_modes fails. */
841 lck->servicepath = NULL;
842 lck->base_name = NULL;
843 lck->stream_name = NULL;
845 lck->num_share_modes = 0;
846 lck->share_modes = NULL;
847 lck->delete_token = NULL;
848 lck->delete_on_close = False;
849 ZERO_STRUCT(lck->old_write_time);
850 ZERO_STRUCT(lck->changed_write_time);
852 lck->modified = False;
854 lck->fresh = (share_mode_data.dptr == NULL);
858 if (smb_fname == NULL || servicepath == NULL
859 || old_write_time == NULL) {
863 has_stream = smb_fname->stream_name != NULL;
865 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
866 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
867 lck->servicepath = talloc_strdup(lck, servicepath);
868 if (lck->base_name == NULL ||
869 (has_stream && lck->stream_name == NULL) ||
870 lck->servicepath == NULL) {
871 DEBUG(0, ("talloc failed\n"));
874 lck->old_write_time = *old_write_time;
876 if (!parse_share_modes(share_mode_data, lck)) {
877 DEBUG(0, ("Could not parse share modes\n"));
885 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
886 const struct file_id id,
887 const char *servicepath,
888 const struct smb_filename *smb_fname,
889 const struct timespec *old_write_time)
891 struct share_mode_lock *lck;
893 TDB_DATA key = locking_key(&id, &tmp);
895 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
896 DEBUG(0, ("talloc failed\n"));
900 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
901 DEBUG(3, ("Could not lock share entry\n"));
906 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
907 lck->record->value, old_write_time)) {
908 DEBUG(3, ("fill_share_mode_lock failed\n"));
913 talloc_set_destructor(lck, share_mode_lock_destructor);
918 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
919 const struct file_id id)
921 struct share_mode_lock *lck;
923 TDB_DATA key = locking_key(&id, &tmp);
926 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
927 DEBUG(0, ("talloc failed\n"));
931 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
932 DEBUG(3, ("Could not fetch share entry\n"));
937 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
938 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
939 "around (file not open)\n"));
947 /*******************************************************************
948 Sets the service name and filename for rename.
949 At this point we emit "file renamed" messages to all
950 process id's that have this file open.
951 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
952 ********************************************************************/
954 bool rename_share_filename(struct messaging_context *msg_ctx,
955 struct share_mode_lock *lck,
956 const char *servicepath,
957 uint32_t orig_name_hash,
958 uint32_t new_name_hash,
959 const struct smb_filename *smb_fname_dst)
967 bool strip_two_chars = false;
968 bool has_stream = smb_fname_dst->stream_name != NULL;
970 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
971 servicepath, smb_fname_dst->base_name));
974 * rename_internal_fsp() and rename_internals() add './' to
975 * head of newname if newname does not contain a '/'.
977 if (smb_fname_dst->base_name[0] &&
978 smb_fname_dst->base_name[1] &&
979 smb_fname_dst->base_name[0] == '.' &&
980 smb_fname_dst->base_name[1] == '/') {
981 strip_two_chars = true;
984 lck->servicepath = talloc_strdup(lck, servicepath);
985 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
986 (strip_two_chars ? 2 : 0));
987 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
988 if (lck->base_name == NULL ||
989 (has_stream && lck->stream_name == NULL) ||
990 lck->servicepath == NULL) {
991 DEBUG(0, ("rename_share_filename: talloc failed\n"));
994 lck->modified = True;
996 sp_len = strlen(lck->servicepath);
997 bn_len = strlen(lck->base_name);
998 sn_len = has_stream ? strlen(lck->stream_name) : 0;
1000 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
1003 /* Set up the name changed message. */
1004 frm = TALLOC_ARRAY(lck, char, msg_len);
1009 push_file_id_24(frm, &lck->id);
1011 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1013 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1014 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1015 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1018 /* Send the messages. */
1019 for (i=0; i<lck->num_share_modes; i++) {
1020 struct share_mode_entry *se = &lck->share_modes[i];
1021 if (!is_valid_share_mode_entry(se)) {
1025 /* If this is a hardlink to the inode
1026 with a different name, skip this. */
1027 if (se->name_hash != orig_name_hash) {
1031 se->name_hash = new_name_hash;
1033 /* But not to ourselves... */
1034 if (procid_is_me(&se->pid)) {
1038 DEBUG(10,("rename_share_filename: sending rename message to "
1039 "pid %s file_id %s sharepath %s base_name %s "
1041 procid_str_static(&se->pid),
1042 file_id_string_tos(&lck->id),
1043 lck->servicepath, lck->base_name,
1044 has_stream ? lck->stream_name : ""));
1046 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1047 (uint8 *)frm, msg_len);
1053 void get_file_infos(struct file_id id,
1054 bool *delete_on_close,
1055 struct timespec *write_time)
1057 struct share_mode_lock *lck;
1059 if (delete_on_close) {
1060 *delete_on_close = false;
1064 ZERO_STRUCTP(write_time);
1067 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1071 if (delete_on_close) {
1072 *delete_on_close = lck->delete_on_close;
1078 wt = lck->changed_write_time;
1079 if (null_timespec(wt)) {
1080 wt = lck->old_write_time;
1089 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1093 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1094 /* cope with dead entries from the process not
1095 existing. These should not be considered valid,
1096 otherwise we end up doing zero timeout sharing
1101 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1102 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1103 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1105 SMB_ASSERT(num_props <= 1);
1106 return (num_props != 0);
1109 bool is_deferred_open_entry(const struct share_mode_entry *e)
1111 return (e->op_type == DEFERRED_OPEN_ENTRY);
1114 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1116 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1119 /*******************************************************************
1120 Fill a share mode entry.
1121 ********************************************************************/
1123 static void fill_share_mode_entry(struct share_mode_entry *e,
1125 uid_t uid, uint64_t mid, uint16 op_type)
1128 e->pid = sconn_server_id(fsp->conn->sconn);
1129 e->share_access = fsp->share_access;
1130 e->private_options = fsp->fh->private_options;
1131 e->access_mask = fsp->access_mask;
1133 e->op_type = op_type;
1134 e->time.tv_sec = fsp->open_time.tv_sec;
1135 e->time.tv_usec = fsp->open_time.tv_usec;
1136 e->id = fsp->file_id;
1137 e->share_file_id = fsp->fh->gen_id;
1138 e->uid = (uint32)uid;
1139 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1140 e->name_hash = fsp->name_hash;
1143 static void fill_deferred_open_entry(struct share_mode_entry *e,
1144 const struct timeval request_time,
1146 struct server_id pid,
1152 e->op_type = DEFERRED_OPEN_ENTRY;
1153 e->time.tv_sec = request_time.tv_sec;
1154 e->time.tv_usec = request_time.tv_usec;
1156 e->uid = (uint32)-1;
1160 static void add_share_mode_entry(struct share_mode_lock *lck,
1161 const struct share_mode_entry *entry)
1165 for (i=0; i<lck->num_share_modes; i++) {
1166 struct share_mode_entry *e = &lck->share_modes[i];
1167 if (is_unused_share_mode_entry(e)) {
1173 if (i == lck->num_share_modes) {
1174 /* No unused entry found */
1175 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1176 &lck->share_modes, &lck->num_share_modes);
1178 lck->modified = True;
1181 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1182 uid_t uid, uint64_t mid, uint16 op_type)
1184 struct share_mode_entry entry;
1185 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1186 add_share_mode_entry(lck, &entry);
1189 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1190 struct timeval request_time,
1191 struct server_id pid, struct file_id id)
1193 struct share_mode_entry entry;
1194 fill_deferred_open_entry(&entry, request_time, id, pid, mid);
1195 add_share_mode_entry(lck, &entry);
1198 /*******************************************************************
1199 Check if two share mode entries are identical, ignoring oplock
1200 and mid info and desired_access. (Removed paranoia test - it's
1201 not automatically a logic error if they are identical. JRA.)
1202 ********************************************************************/
1204 static bool share_modes_identical(struct share_mode_entry *e1,
1205 struct share_mode_entry *e2)
1207 /* We used to check for e1->share_access == e2->share_access here
1208 as well as the other fields but 2 different DOS or FCB opens
1209 sharing the same share mode entry may validly differ in
1210 fsp->share_access field. */
1212 return (procid_equal(&e1->pid, &e2->pid) &&
1213 file_id_equal(&e1->id, &e2->id) &&
1214 e1->share_file_id == e2->share_file_id );
1217 static bool deferred_open_identical(struct share_mode_entry *e1,
1218 struct share_mode_entry *e2)
1220 return (procid_equal(&e1->pid, &e2->pid) &&
1221 (e1->op_mid == e2->op_mid) &&
1222 file_id_equal(&e1->id, &e2->id));
1225 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1226 struct share_mode_entry *entry)
1230 for (i=0; i<lck->num_share_modes; i++) {
1231 struct share_mode_entry *e = &lck->share_modes[i];
1232 if (is_valid_share_mode_entry(entry) &&
1233 is_valid_share_mode_entry(e) &&
1234 share_modes_identical(e, entry)) {
1237 if (is_deferred_open_entry(entry) &&
1238 is_deferred_open_entry(e) &&
1239 deferred_open_identical(e, entry)) {
1246 /*******************************************************************
1247 Del the share mode of a file for this process. Return the number of
1249 ********************************************************************/
1251 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1253 struct share_mode_entry entry, *e;
1255 /* Don't care about the pid owner being correct here - just a search. */
1256 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1258 e = find_share_mode_entry(lck, &entry);
1263 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1264 lck->modified = True;
1268 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid,
1269 struct server_id pid)
1271 struct share_mode_entry entry, *e;
1273 fill_deferred_open_entry(&entry, timeval_zero(),
1276 e = find_share_mode_entry(lck, &entry);
1281 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1282 lck->modified = True;
1285 /*******************************************************************
1286 Remove an oplock mid and mode entry from a share mode.
1287 ********************************************************************/
1289 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1291 struct share_mode_entry entry, *e;
1293 /* Don't care about the pid owner being correct here - just a search. */
1294 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1296 e = find_share_mode_entry(lck, &entry);
1302 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1304 * Going from exclusive or batch,
1305 * we always go through FAKE_LEVEL_II
1308 e->op_type = FAKE_LEVEL_II_OPLOCK;
1310 e->op_type = NO_OPLOCK;
1312 lck->modified = True;
1316 /*******************************************************************
1317 Downgrade a oplock type from exclusive to level II.
1318 ********************************************************************/
1320 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1322 struct share_mode_entry entry, *e;
1324 /* Don't care about the pid owner being correct here - just a search. */
1325 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1327 e = find_share_mode_entry(lck, &entry);
1332 e->op_type = LEVEL_II_OPLOCK;
1333 lck->modified = True;
1337 /****************************************************************************
1338 Check if setting delete on close is allowed on this fsp.
1339 ****************************************************************************/
1341 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1344 * Only allow delete on close for writable files.
1347 if ((dosmode & aRONLY) &&
1348 !lp_delete_readonly(SNUM(fsp->conn))) {
1349 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1350 "flag set but file attribute is readonly.\n",
1352 return NT_STATUS_CANNOT_DELETE;
1356 * Only allow delete on close for writable shares.
1359 if (!CAN_WRITE(fsp->conn)) {
1360 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1361 "close flag set but write access denied on share.\n",
1363 return NT_STATUS_ACCESS_DENIED;
1367 * Only allow delete on close for files/directories opened with delete
1371 if (!(fsp->access_mask & DELETE_ACCESS)) {
1372 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1373 "close flag set but delete access denied.\n",
1375 return NT_STATUS_ACCESS_DENIED;
1378 /* Don't allow delete on close for non-empty directories. */
1379 if (fsp->is_directory) {
1380 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1381 return can_delete_directory(fsp->conn,
1382 fsp->fsp_name->base_name);
1385 return NT_STATUS_OK;
1388 /*************************************************************************
1389 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1390 (Should this be in locking.c.... ?).
1391 *************************************************************************/
1393 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, const UNIX_USER_TOKEN *tok)
1395 UNIX_USER_TOKEN *cpy;
1401 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1406 cpy->uid = tok->uid;
1407 cpy->gid = tok->gid;
1408 cpy->ngroups = tok->ngroups;
1410 /* Make this a talloc child of cpy. */
1411 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1415 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1420 /****************************************************************************
1421 Replace the delete on close token.
1422 ****************************************************************************/
1424 void set_delete_on_close_token(struct share_mode_lock *lck, const UNIX_USER_TOKEN *tok)
1426 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1428 /* Copy the new token (can be NULL). */
1429 lck->delete_token = copy_unix_token(lck, tok);
1430 lck->modified = True;
1433 /****************************************************************************
1434 Sets the delete on close flag over all share modes on this file.
1435 Modify the share mode entry for all files open
1436 on this device and inode to tell other smbds we have
1437 changed the delete on close flag. This will be noticed
1438 in the close code, the last closer will delete the file
1440 This makes a copy of any UNIX_USER_TOKEN into the
1441 lck entry. This function is used when the lock is already granted.
1442 ****************************************************************************/
1444 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1446 if (lck->delete_on_close != delete_on_close) {
1447 set_delete_on_close_token(lck, tok);
1448 lck->delete_on_close = delete_on_close;
1449 if (delete_on_close) {
1450 SMB_ASSERT(lck->delete_token != NULL);
1452 lck->modified = True;
1456 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1458 struct share_mode_lock *lck;
1460 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1461 "fnum = %d, file %s\n",
1462 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1465 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1471 set_delete_on_close_lck(lck, delete_on_close, tok);
1473 if (fsp->is_directory) {
1474 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1475 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1476 fsp->fsp_name->base_name);
1481 fsp->delete_on_close = delete_on_close;
1486 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1488 struct share_mode_lock *lck;
1490 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1491 timestring(talloc_tos(),
1492 convert_timespec_to_time_t(write_time)),
1493 file_id_string_tos(&fileid)));
1495 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1500 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1501 lck->modified = True;
1502 lck->changed_write_time = write_time;
1509 bool set_write_time(struct file_id fileid, struct timespec write_time)
1511 struct share_mode_lock *lck;
1513 DEBUG(5,("set_write_time: %s id=%s\n",
1514 timestring(talloc_tos(),
1515 convert_timespec_to_time_t(write_time)),
1516 file_id_string_tos(&fileid)));
1518 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1523 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1524 lck->modified = True;
1525 lck->old_write_time = write_time;
1533 struct forall_state {
1534 void (*fn)(const struct share_mode_entry *entry,
1535 const char *sharepath,
1537 void *private_data);
1541 static int traverse_fn(struct db_record *rec, void *_state)
1543 struct forall_state *state = (struct forall_state *)_state;
1544 struct locking_data *data;
1545 struct share_mode_entry *shares;
1546 const char *sharepath;
1550 /* Ensure this is a locking_key record. */
1551 if (rec->key.dsize != sizeof(struct file_id))
1554 data = (struct locking_data *)rec->value.dptr;
1555 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1556 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1557 data->u.s.num_share_mode_entries*sizeof(*shares) +
1558 data->u.s.delete_token_size;
1559 fname = (const char *)rec->value.dptr + sizeof(*data) +
1560 data->u.s.num_share_mode_entries*sizeof(*shares) +
1561 data->u.s.delete_token_size +
1562 strlen(sharepath) + 1;
1564 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1565 state->fn(&shares[i], sharepath, fname,
1566 state->private_data);
1571 /*******************************************************************
1572 Call the specified function on each entry under management by the
1574 ********************************************************************/
1576 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1577 const char *, void *),
1580 struct forall_state state;
1582 if (lock_db == NULL)
1586 state.private_data = private_data;
1588 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);