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"
46 #define DBGC_CLASS DBGC_LOCKING
48 #define NO_LOCKING_COUNT (-1)
50 /* the locking database handle */
51 static struct db_context *lock_db;
53 /****************************************************************************
55 ****************************************************************************/
57 const char *lock_type_name(enum brl_type lock_type)
64 case PENDING_READ_LOCK:
65 return "PENDING_READ";
66 case PENDING_WRITE_LOCK:
67 return "PENDING_WRITE";
73 const char *lock_flav_name(enum brl_flavour lock_flav)
75 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
78 /****************************************************************************
79 Utility function called to see if a file region is locked.
80 Called in the read/write codepath.
81 ****************************************************************************/
83 void init_strict_lock_struct(files_struct *fsp,
87 enum brl_type lock_type,
88 struct lock_struct *plock)
90 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
92 plock->context.smblctx = smblctx;
93 plock->context.tid = fsp->conn->cnum;
94 plock->context.pid = sconn_server_id(fsp->conn->sconn);
97 plock->fnum = fsp->fnum;
98 plock->lock_type = lock_type;
99 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
102 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
104 int strict_locking = lp_strict_locking(fsp->conn->params);
107 if (plock->size == 0) {
111 if (!lp_locking(fsp->conn->params) || !strict_locking) {
115 if (strict_locking == Auto) {
116 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
117 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
119 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
120 (plock->lock_type == READ_LOCK)) {
121 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
124 struct byte_range_lock *br_lck;
126 br_lck = brl_get_locks_readonly(fsp);
130 ret = brl_locktest(br_lck,
131 plock->context.smblctx,
139 struct byte_range_lock *br_lck;
141 br_lck = brl_get_locks_readonly(fsp);
145 ret = brl_locktest(br_lck,
146 plock->context.smblctx,
154 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
155 "len=%.0f %s for fnum %d file %s\n",
156 lock_flav_name(plock->lock_flav),
157 (double)plock->start, (double)plock->size,
158 ret ? "unlocked" : "locked",
159 plock->fnum, fsp_str_dbg(fsp)));
164 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
168 /****************************************************************************
169 Find out if a lock could be granted - return who is blocking us if we can't.
170 ****************************************************************************/
172 NTSTATUS query_lock(files_struct *fsp,
176 enum brl_type *plock_type,
177 enum brl_flavour lock_flav)
179 struct byte_range_lock *br_lck = NULL;
181 if (!fsp->can_lock) {
182 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
185 if (!lp_locking(fsp->conn->params)) {
189 br_lck = brl_get_locks_readonly(fsp);
191 return NT_STATUS_NO_MEMORY;
194 return brl_lockquery(br_lck,
196 sconn_server_id(fsp->conn->sconn),
203 static void increment_current_lock_count(files_struct *fsp,
204 enum brl_flavour lock_flav)
206 if (lock_flav == WINDOWS_LOCK &&
207 fsp->current_lock_count != NO_LOCKING_COUNT) {
208 /* blocking ie. pending, locks also count here,
209 * as this is an efficiency counter to avoid checking
210 * the lock db. on close. JRA. */
212 fsp->current_lock_count++;
214 /* Notice that this has had a POSIX lock request.
215 * We can't count locks after this so forget them.
217 fsp->current_lock_count = NO_LOCKING_COUNT;
221 static void decrement_current_lock_count(files_struct *fsp,
222 enum brl_flavour lock_flav)
224 if (lock_flav == WINDOWS_LOCK &&
225 fsp->current_lock_count != NO_LOCKING_COUNT) {
226 SMB_ASSERT(fsp->current_lock_count > 0);
227 fsp->current_lock_count--;
231 /****************************************************************************
232 Utility function called by locking requests.
233 ****************************************************************************/
235 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
240 enum brl_type lock_type,
241 enum brl_flavour lock_flav,
245 struct blocking_lock_record *blr)
247 struct byte_range_lock *br_lck = NULL;
249 /* silently return ok on print files as we don't do locking there */
250 if (fsp->print_file) {
251 *perr = NT_STATUS_OK;
255 if (!fsp->can_lock) {
256 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
260 if (!lp_locking(fsp->conn->params)) {
261 *perr = NT_STATUS_OK;
265 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
267 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
268 "blocking_lock=%s requested for fnum %d file %s\n",
269 lock_flav_name(lock_flav), lock_type_name(lock_type),
270 (double)offset, (double)count, blocking_lock ? "true" :
271 "false", fsp->fnum, fsp_str_dbg(fsp)));
273 br_lck = brl_get_locks(talloc_tos(), fsp);
275 *perr = NT_STATUS_NO_MEMORY;
279 *perr = brl_lock(msg_ctx,
282 sconn_server_id(fsp->conn->sconn),
291 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
293 increment_current_lock_count(fsp, lock_flav);
297 /****************************************************************************
298 Utility function called by unlocking requests.
299 ****************************************************************************/
301 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
306 enum brl_flavour lock_flav)
309 struct byte_range_lock *br_lck = NULL;
311 if (!fsp->can_lock) {
312 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
315 if (!lp_locking(fsp->conn->params)) {
319 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
320 (double)offset, (double)count, fsp->fnum,
323 br_lck = brl_get_locks(talloc_tos(), fsp);
325 return NT_STATUS_NO_MEMORY;
328 ok = brl_unlock(msg_ctx,
331 sconn_server_id(fsp->conn->sconn),
339 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
340 return NT_STATUS_RANGE_NOT_LOCKED;
343 decrement_current_lock_count(fsp, lock_flav);
347 /****************************************************************************
348 Cancel any pending blocked locks.
349 ****************************************************************************/
351 NTSTATUS do_lock_cancel(files_struct *fsp,
355 enum brl_flavour lock_flav,
356 struct blocking_lock_record *blr)
359 struct byte_range_lock *br_lck = NULL;
361 if (!fsp->can_lock) {
362 return fsp->is_directory ?
363 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
366 if (!lp_locking(fsp->conn->params)) {
367 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
370 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
371 (double)offset, (double)count, fsp->fnum,
374 br_lck = brl_get_locks(talloc_tos(), fsp);
376 return NT_STATUS_NO_MEMORY;
379 ok = brl_lock_cancel(br_lck,
381 sconn_server_id(fsp->conn->sconn),
390 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
391 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
394 decrement_current_lock_count(fsp, lock_flav);
398 /****************************************************************************
399 Remove any locks on this fd. Called from file_close().
400 ****************************************************************************/
402 void locking_close_file(struct messaging_context *msg_ctx,
404 enum file_close_type close_type)
406 struct byte_range_lock *br_lck;
408 if (!lp_locking(fsp->conn->params)) {
412 /* If we have not outstanding locks or pending
413 * locks then we don't need to look in the lock db.
416 if (fsp->current_lock_count == 0) {
420 br_lck = brl_get_locks(talloc_tos(),fsp);
423 cancel_pending_lock_requests_by_fid(fsp, br_lck, close_type);
424 brl_close_fnum(msg_ctx, br_lck);
429 /****************************************************************************
430 Initialise the locking functions.
431 ****************************************************************************/
433 static bool locking_init_internal(bool read_only)
440 lock_db = db_open(NULL, lock_path("locking.tdb"),
441 lp_open_files_db_hash_size(),
442 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
443 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
446 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
450 if (!posix_locking_init(read_only))
456 bool locking_init(void)
458 return locking_init_internal(false);
461 bool locking_init_readonly(void)
463 return locking_init_internal(true);
466 /*******************************************************************
467 Deinitialize the share_mode management.
468 ******************************************************************/
470 bool locking_end(void)
473 TALLOC_FREE(lock_db);
477 /*******************************************************************
478 Form a static locking key for a dev/inode pair.
479 ******************************************************************/
481 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
484 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
487 /*******************************************************************
488 Print out a share mode.
489 ********************************************************************/
491 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
493 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
494 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
495 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
496 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
498 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
499 procid_str_static(&e->pid),
500 e->share_access, e->private_options,
501 e->access_mask, (unsigned long long)e->op_mid,
502 e->op_type, e->share_file_id,
503 (unsigned int)e->uid, (unsigned int)e->flags,
504 file_id_string_tos(&e->id),
505 (unsigned int)e->name_hash);
508 /*******************************************************************
509 Print out a share mode table.
510 ********************************************************************/
512 static void print_share_mode_table(struct locking_data *data)
514 int num_share_modes = data->u.s.num_share_mode_entries;
515 struct share_mode_entry *shares =
516 (struct share_mode_entry *)(data + 1);
519 for (i = 0; i < num_share_modes; i++) {
520 struct share_mode_entry entry;
524 * We need to memcpy the entry here due to alignment
525 * restrictions that are not met when directly accessing
529 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
530 str = share_mode_str(talloc_tos(), i, &entry);
532 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
537 static int parse_delete_tokens_list(struct share_mode_lock *lck,
538 struct locking_data *pdata,
541 uint8_t *p = dbuf.dptr + sizeof(struct locking_data) +
542 (lck->num_share_modes *
543 sizeof(struct share_mode_entry));
544 uint8_t *end_ptr = dbuf.dptr + (dbuf.dsize - 2);
545 int delete_tokens_size = 0;
548 lck->delete_tokens = NULL;
550 for (i = 0; i < pdata->u.s.num_delete_token_entries; i++) {
552 struct delete_token_list *pdtl;
554 if (end_ptr - p < (sizeof(uint32_t) + sizeof(uint32_t) +
555 sizeof(uid_t) + sizeof(gid_t))) {
556 DEBUG(0,("parse_delete_tokens_list: "
557 "corrupt token list (%u)",
558 (unsigned int)(end_ptr - p)));
559 smb_panic("corrupt token list");
563 memcpy(&token_len, p, sizeof(token_len));
564 delete_tokens_size += token_len;
566 if (p + token_len > end_ptr || token_len < sizeof(token_len) +
567 sizeof(pdtl->name_hash) +
570 DEBUG(0,("parse_delete_tokens_list: "
571 "invalid token length (%u)\n",
572 (unsigned int)token_len ));
573 smb_panic("invalid token length");
577 p += sizeof(token_len);
579 pdtl = TALLOC_ZERO_P(lck, struct delete_token_list);
581 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
584 /* Copy out the name_hash. */
585 memcpy(&pdtl->name_hash, p, sizeof(pdtl->name_hash));
586 p += sizeof(pdtl->name_hash);
588 pdtl->delete_token = TALLOC_ZERO_P(pdtl, struct security_unix_token);
589 if (pdtl->delete_token == NULL) {
590 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
594 /* Copy out the uid and gid. */
595 memcpy(&pdtl->delete_token->uid, p, sizeof(uid_t));
597 memcpy(&pdtl->delete_token->gid, p, sizeof(gid_t));
600 token_len -= (sizeof(token_len) + sizeof(pdtl->name_hash) +
601 sizeof(uid_t) + sizeof(gid_t));
603 /* Any supplementary groups ? */
607 if (token_len % sizeof(gid_t) != 0) {
608 DEBUG(0,("parse_delete_tokens_list: "
609 "corrupt group list (%u)",
610 (unsigned int)(token_len % sizeof(gid_t)) ));
611 smb_panic("corrupt group list");
615 pdtl->delete_token->ngroups = token_len / sizeof(gid_t);
616 pdtl->delete_token->groups = TALLOC_ARRAY(pdtl->delete_token, gid_t,
617 pdtl->delete_token->ngroups);
618 if (pdtl->delete_token->groups == NULL) {
619 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
623 for (j = 0; j < pdtl->delete_token->ngroups; j++) {
624 memcpy(&pdtl->delete_token->groups[j], p, sizeof(gid_t));
628 /* Add to the list. */
629 DLIST_ADD(lck->delete_tokens, pdtl);
632 return delete_tokens_size;
635 /*******************************************************************
636 Get all share mode entries for a dev/inode pair.
637 ********************************************************************/
639 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
641 struct locking_data data;
642 int delete_tokens_size;
645 if (dbuf.dsize < sizeof(struct locking_data)) {
646 smb_panic("parse_share_modes: buffer too short");
649 memcpy(&data, dbuf.dptr, sizeof(data));
651 lck->old_write_time = data.u.s.old_write_time;
652 lck->changed_write_time = data.u.s.changed_write_time;
653 lck->num_share_modes = data.u.s.num_share_mode_entries;
655 DEBUG(10, ("parse_share_modes: owrt: %s, "
656 "cwrt: %s, ntok: %u, num_share_modes: %d\n",
657 timestring(talloc_tos(),
658 convert_timespec_to_time_t(lck->old_write_time)),
659 timestring(talloc_tos(),
660 convert_timespec_to_time_t(
661 lck->changed_write_time)),
662 (unsigned int)data.u.s.num_delete_token_entries,
663 lck->num_share_modes));
665 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
666 DEBUG(0, ("invalid number of share modes: %d\n",
667 lck->num_share_modes));
668 smb_panic("parse_share_modes: invalid number of share modes");
671 lck->share_modes = NULL;
673 if (lck->num_share_modes != 0) {
675 if (dbuf.dsize < (sizeof(struct locking_data) +
676 (lck->num_share_modes *
677 sizeof(struct share_mode_entry)))) {
678 smb_panic("parse_share_modes: buffer too short");
681 lck->share_modes = (struct share_mode_entry *)
683 dbuf.dptr+sizeof(struct locking_data),
684 lck->num_share_modes *
685 sizeof(struct share_mode_entry));
687 if (lck->share_modes == NULL) {
688 smb_panic("parse_share_modes: talloc failed");
692 /* Get any delete tokens. */
693 delete_tokens_size = parse_delete_tokens_list(lck, &data, dbuf);
694 if (delete_tokens_size < 0) {
695 smb_panic("parse_share_modes: parse_delete_tokens_list failed");
698 /* Save off the associated service path and filename. */
699 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
700 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
703 lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
704 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
706 strlen(lck->servicepath) + 1;
708 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
709 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
711 strlen(lck->servicepath) + 1 +
712 strlen(lck->base_name) + 1;
715 * Ensure that each entry has a real process attached.
718 for (i = 0; i < lck->num_share_modes; i++) {
719 struct share_mode_entry *entry_p = &lck->share_modes[i];
721 if (DEBUGLEVEL >= 10) {
722 str = share_mode_str(NULL, i, entry_p);
724 DEBUG(10,("parse_share_modes: %s\n",
726 if (!serverid_exists(&entry_p->pid)) {
727 DEBUG(10,("parse_share_modes: deleted %s\n",
729 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
730 lck->modified = True;
738 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
743 struct locking_data *data;
745 ssize_t sp_len, bn_len, sn_len;
746 uint32_t delete_tokens_size = 0;
747 struct delete_token_list *pdtl = NULL;
748 uint32_t num_delete_token_entries = 0;
753 for (i=0; i<lck->num_share_modes; i++) {
754 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
759 if (num_valid == 0) {
763 sp_len = strlen(lck->servicepath);
764 bn_len = strlen(lck->base_name);
765 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
767 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
768 num_delete_token_entries++;
769 delete_tokens_size += (sizeof(uint32_t) +
773 pdtl->delete_token->ngroups*sizeof(gid_t));
776 result.dsize = sizeof(*data) +
777 lck->num_share_modes * sizeof(struct share_mode_entry) +
782 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
784 if (result.dptr == NULL) {
785 smb_panic("talloc failed");
788 data = (struct locking_data *)result.dptr;
790 data->u.s.num_share_mode_entries = lck->num_share_modes;
791 data->u.s.old_write_time = lck->old_write_time;
792 data->u.s.changed_write_time = lck->changed_write_time;
793 data->u.s.num_delete_token_entries = num_delete_token_entries;
795 DEBUG(10,("unparse_share_modes: owrt: %s cwrt: %s, ntok: %u, "
797 timestring(talloc_tos(),
798 convert_timespec_to_time_t(lck->old_write_time)),
799 timestring(talloc_tos(),
800 convert_timespec_to_time_t(
801 lck->changed_write_time)),
802 (unsigned int)data->u.s.num_delete_token_entries,
803 data->u.s.num_share_mode_entries));
805 memcpy(result.dptr + sizeof(*data), lck->share_modes,
806 sizeof(struct share_mode_entry)*lck->num_share_modes);
807 offset = sizeof(*data) +
808 sizeof(struct share_mode_entry)*lck->num_share_modes;
810 /* Store any delete on close tokens. */
811 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
812 struct security_unix_token *pdt = pdtl->delete_token;
813 uint32_t token_size = sizeof(uint32_t) +
817 (pdt->ngroups * sizeof(gid_t));
818 uint8_t *p = result.dptr + offset;
820 memcpy(p, &token_size, sizeof(uint32_t));
821 p += sizeof(uint32_t);
823 memcpy(p, &pdtl->name_hash, sizeof(uint32_t));
824 p += sizeof(uint32_t);
826 memcpy(p, &pdt->uid, sizeof(uid_t));
829 memcpy(p, &pdt->gid, sizeof(gid_t));
832 for (i = 0; i < pdt->ngroups; i++) {
833 memcpy(p, &pdt->groups[i], sizeof(gid_t));
836 offset += token_size;
839 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
840 result.dsize - offset - 1);
841 offset += sp_len + 1;
842 safe_strcpy((char *)result.dptr + offset, lck->base_name,
843 result.dsize - offset - 1);
844 offset += bn_len + 1;
845 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
846 result.dsize - offset - 1);
848 if (DEBUGLEVEL >= 10) {
849 print_share_mode_table(data);
855 static int share_mode_lock_destructor(struct share_mode_lock *lck)
860 if (!lck->modified) {
864 data = unparse_share_modes(lck);
866 if (data.dptr == NULL) {
868 /* There has been an entry before, delete it */
870 status = lck->record->delete_rec(lck->record);
871 if (!NT_STATUS_IS_OK(status)) {
874 DEBUG(0, ("delete_rec returned %s\n",
877 if (asprintf(&errmsg, "could not delete share "
879 nt_errstr(status)) == -1) {
880 smb_panic("could not delete share"
889 status = lck->record->store(lck->record, data, TDB_REPLACE);
890 if (!NT_STATUS_IS_OK(status)) {
893 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
895 if (asprintf(&errmsg, "could not store share mode entry: %s",
896 nt_errstr(status)) == -1) {
897 smb_panic("could not store share mode entry");
907 static bool fill_share_mode_lock(struct share_mode_lock *lck,
909 const char *servicepath,
910 const struct smb_filename *smb_fname,
911 TDB_DATA share_mode_data,
912 const struct timespec *old_write_time)
914 /* Ensure we set every field here as the destructor must be
915 valid even if parse_share_modes fails. */
917 lck->servicepath = NULL;
918 lck->base_name = NULL;
919 lck->stream_name = NULL;
921 lck->num_share_modes = 0;
922 lck->share_modes = NULL;
923 lck->delete_tokens = NULL;
924 ZERO_STRUCT(lck->old_write_time);
925 ZERO_STRUCT(lck->changed_write_time);
927 lck->modified = False;
929 lck->fresh = (share_mode_data.dptr == NULL);
933 if (smb_fname == NULL || servicepath == NULL
934 || old_write_time == NULL) {
938 has_stream = smb_fname->stream_name != NULL;
940 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
941 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
942 lck->servicepath = talloc_strdup(lck, servicepath);
943 if (lck->base_name == NULL ||
944 (has_stream && lck->stream_name == NULL) ||
945 lck->servicepath == NULL) {
946 DEBUG(0, ("talloc failed\n"));
949 lck->old_write_time = *old_write_time;
951 if (!parse_share_modes(share_mode_data, lck)) {
952 DEBUG(0, ("Could not parse share modes\n"));
960 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
961 const struct file_id id,
962 const char *servicepath,
963 const struct smb_filename *smb_fname,
964 const struct timespec *old_write_time)
966 struct share_mode_lock *lck;
968 TDB_DATA key = locking_key(&id, &tmp);
970 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
971 DEBUG(0, ("talloc failed\n"));
975 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
976 DEBUG(3, ("Could not lock share entry\n"));
981 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
982 lck->record->value, old_write_time)) {
983 DEBUG(3, ("fill_share_mode_lock failed\n"));
988 talloc_set_destructor(lck, share_mode_lock_destructor);
993 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
994 const struct file_id id)
996 struct share_mode_lock *lck;
998 TDB_DATA key = locking_key(&id, &tmp);
1001 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
1002 DEBUG(0, ("talloc failed\n"));
1006 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
1007 DEBUG(3, ("Could not fetch share entry\n"));
1012 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
1013 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
1014 "around (file not open)\n"));
1022 /*******************************************************************
1023 Sets the service name and filename for rename.
1024 At this point we emit "file renamed" messages to all
1025 process id's that have this file open.
1026 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
1027 ********************************************************************/
1029 bool rename_share_filename(struct messaging_context *msg_ctx,
1030 struct share_mode_lock *lck,
1031 const char *servicepath,
1032 uint32_t orig_name_hash,
1033 uint32_t new_name_hash,
1034 const struct smb_filename *smb_fname_dst)
1042 bool strip_two_chars = false;
1043 bool has_stream = smb_fname_dst->stream_name != NULL;
1045 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
1046 servicepath, smb_fname_dst->base_name));
1049 * rename_internal_fsp() and rename_internals() add './' to
1050 * head of newname if newname does not contain a '/'.
1052 if (smb_fname_dst->base_name[0] &&
1053 smb_fname_dst->base_name[1] &&
1054 smb_fname_dst->base_name[0] == '.' &&
1055 smb_fname_dst->base_name[1] == '/') {
1056 strip_two_chars = true;
1059 lck->servicepath = talloc_strdup(lck, servicepath);
1060 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
1061 (strip_two_chars ? 2 : 0));
1062 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
1063 if (lck->base_name == NULL ||
1064 (has_stream && lck->stream_name == NULL) ||
1065 lck->servicepath == NULL) {
1066 DEBUG(0, ("rename_share_filename: talloc failed\n"));
1069 lck->modified = True;
1071 sp_len = strlen(lck->servicepath);
1072 bn_len = strlen(lck->base_name);
1073 sn_len = has_stream ? strlen(lck->stream_name) : 0;
1075 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
1078 /* Set up the name changed message. */
1079 frm = TALLOC_ARRAY(lck, char, msg_len);
1084 push_file_id_24(frm, &lck->id);
1086 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1088 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1089 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1090 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1093 /* Send the messages. */
1094 for (i=0; i<lck->num_share_modes; i++) {
1095 struct share_mode_entry *se = &lck->share_modes[i];
1096 if (!is_valid_share_mode_entry(se)) {
1100 /* If this is a hardlink to the inode
1101 with a different name, skip this. */
1102 if (se->name_hash != orig_name_hash) {
1106 se->name_hash = new_name_hash;
1108 /* But not to ourselves... */
1109 if (procid_is_me(&se->pid)) {
1113 DEBUG(10,("rename_share_filename: sending rename message to "
1114 "pid %s file_id %s sharepath %s base_name %s "
1116 procid_str_static(&se->pid),
1117 file_id_string_tos(&lck->id),
1118 lck->servicepath, lck->base_name,
1119 has_stream ? lck->stream_name : ""));
1121 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1122 (uint8 *)frm, msg_len);
1128 void get_file_infos(struct file_id id,
1130 bool *delete_on_close,
1131 struct timespec *write_time)
1133 struct share_mode_lock *lck;
1135 if (delete_on_close) {
1136 *delete_on_close = false;
1140 ZERO_STRUCTP(write_time);
1143 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1147 if (delete_on_close) {
1148 *delete_on_close = is_delete_on_close_set(lck, name_hash);
1154 wt = lck->changed_write_time;
1155 if (null_timespec(wt)) {
1156 wt = lck->old_write_time;
1165 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1169 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1170 /* cope with dead entries from the process not
1171 existing. These should not be considered valid,
1172 otherwise we end up doing zero timeout sharing
1177 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1178 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1179 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1181 SMB_ASSERT(num_props <= 1);
1182 return (num_props != 0);
1185 bool is_deferred_open_entry(const struct share_mode_entry *e)
1187 return (e->op_type == DEFERRED_OPEN_ENTRY);
1190 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1192 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1195 /*******************************************************************
1196 Fill a share mode entry.
1197 ********************************************************************/
1199 static void fill_share_mode_entry(struct share_mode_entry *e,
1201 uid_t uid, uint64_t mid, uint16 op_type)
1204 e->pid = sconn_server_id(fsp->conn->sconn);
1205 e->share_access = fsp->share_access;
1206 e->private_options = fsp->fh->private_options;
1207 e->access_mask = fsp->access_mask;
1209 e->op_type = op_type;
1210 e->time.tv_sec = fsp->open_time.tv_sec;
1211 e->time.tv_usec = fsp->open_time.tv_usec;
1212 e->id = fsp->file_id;
1213 e->share_file_id = fsp->fh->gen_id;
1214 e->uid = (uint32)uid;
1215 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1216 e->name_hash = fsp->name_hash;
1219 static void fill_deferred_open_entry(struct share_mode_entry *e,
1220 const struct timeval request_time,
1222 struct server_id pid,
1228 e->op_type = DEFERRED_OPEN_ENTRY;
1229 e->time.tv_sec = request_time.tv_sec;
1230 e->time.tv_usec = request_time.tv_usec;
1232 e->uid = (uint32)-1;
1236 static void add_share_mode_entry(struct share_mode_lock *lck,
1237 const struct share_mode_entry *entry)
1241 for (i=0; i<lck->num_share_modes; i++) {
1242 struct share_mode_entry *e = &lck->share_modes[i];
1243 if (is_unused_share_mode_entry(e)) {
1249 if (i == lck->num_share_modes) {
1250 /* No unused entry found */
1251 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1252 &lck->share_modes, &lck->num_share_modes);
1254 lck->modified = True;
1257 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1258 uid_t uid, uint64_t mid, uint16 op_type)
1260 struct share_mode_entry entry;
1261 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1262 add_share_mode_entry(lck, &entry);
1265 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1266 struct timeval request_time,
1267 struct server_id pid, struct file_id id)
1269 struct share_mode_entry entry;
1270 fill_deferred_open_entry(&entry, request_time, id, pid, mid);
1271 add_share_mode_entry(lck, &entry);
1274 /*******************************************************************
1275 Check if two share mode entries are identical, ignoring oplock
1276 and mid info and desired_access. (Removed paranoia test - it's
1277 not automatically a logic error if they are identical. JRA.)
1278 ********************************************************************/
1280 static bool share_modes_identical(struct share_mode_entry *e1,
1281 struct share_mode_entry *e2)
1283 /* We used to check for e1->share_access == e2->share_access here
1284 as well as the other fields but 2 different DOS or FCB opens
1285 sharing the same share mode entry may validly differ in
1286 fsp->share_access field. */
1288 return (procid_equal(&e1->pid, &e2->pid) &&
1289 file_id_equal(&e1->id, &e2->id) &&
1290 e1->share_file_id == e2->share_file_id );
1293 static bool deferred_open_identical(struct share_mode_entry *e1,
1294 struct share_mode_entry *e2)
1296 return (procid_equal(&e1->pid, &e2->pid) &&
1297 (e1->op_mid == e2->op_mid) &&
1298 file_id_equal(&e1->id, &e2->id));
1301 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1302 struct share_mode_entry *entry)
1306 for (i=0; i<lck->num_share_modes; i++) {
1307 struct share_mode_entry *e = &lck->share_modes[i];
1308 if (is_valid_share_mode_entry(entry) &&
1309 is_valid_share_mode_entry(e) &&
1310 share_modes_identical(e, entry)) {
1313 if (is_deferred_open_entry(entry) &&
1314 is_deferred_open_entry(e) &&
1315 deferred_open_identical(e, entry)) {
1322 /*******************************************************************
1323 Del the share mode of a file for this process. Return the number of
1325 ********************************************************************/
1327 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
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 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1340 lck->modified = True;
1344 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid,
1345 struct server_id pid)
1347 struct share_mode_entry entry, *e;
1349 fill_deferred_open_entry(&entry, timeval_zero(),
1352 e = find_share_mode_entry(lck, &entry);
1357 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1358 lck->modified = True;
1361 /*******************************************************************
1362 Remove an oplock mid and mode entry from a share mode.
1363 ********************************************************************/
1365 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1367 struct share_mode_entry entry, *e;
1369 /* Don't care about the pid owner being correct here - just a search. */
1370 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1372 e = find_share_mode_entry(lck, &entry);
1377 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1379 * Going from exclusive or batch,
1380 * we always go through FAKE_LEVEL_II
1383 e->op_type = FAKE_LEVEL_II_OPLOCK;
1385 e->op_type = NO_OPLOCK;
1387 lck->modified = True;
1391 /*******************************************************************
1392 Downgrade a oplock type from exclusive to level II.
1393 ********************************************************************/
1395 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1397 struct share_mode_entry entry, *e;
1399 /* Don't care about the pid owner being correct here - just a search. */
1400 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1402 e = find_share_mode_entry(lck, &entry);
1407 e->op_type = LEVEL_II_OPLOCK;
1408 lck->modified = True;
1412 /****************************************************************************
1413 Check if setting delete on close is allowed on this fsp.
1414 ****************************************************************************/
1416 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1419 * Only allow delete on close for writable files.
1422 if ((dosmode & aRONLY) &&
1423 !lp_delete_readonly(SNUM(fsp->conn))) {
1424 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1425 "flag set but file attribute is readonly.\n",
1427 return NT_STATUS_CANNOT_DELETE;
1431 * Only allow delete on close for writable shares.
1434 if (!CAN_WRITE(fsp->conn)) {
1435 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1436 "close flag set but write access denied on share.\n",
1438 return NT_STATUS_ACCESS_DENIED;
1442 * Only allow delete on close for files/directories opened with delete
1446 if (!(fsp->access_mask & DELETE_ACCESS)) {
1447 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1448 "close flag set but delete access denied.\n",
1450 return NT_STATUS_ACCESS_DENIED;
1453 /* Don't allow delete on close for non-empty directories. */
1454 if (fsp->is_directory) {
1455 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1456 return can_delete_directory(fsp->conn,
1457 fsp->fsp_name->base_name);
1460 return NT_STATUS_OK;
1463 /*************************************************************************
1464 Return a talloced copy of a struct security_unix_token. NULL on fail.
1465 (Should this be in locking.c.... ?).
1466 *************************************************************************/
1468 static struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct security_unix_token *tok)
1470 struct security_unix_token *cpy;
1472 cpy = TALLOC_P(ctx, struct security_unix_token);
1477 cpy->uid = tok->uid;
1478 cpy->gid = tok->gid;
1479 cpy->ngroups = tok->ngroups;
1481 /* Make this a talloc child of cpy. */
1482 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1486 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1491 /****************************************************************************
1492 Adds a delete on close token.
1493 ****************************************************************************/
1495 static bool add_delete_on_close_token(struct share_mode_lock *lck,
1497 const struct security_unix_token *tok)
1499 struct delete_token_list *dtl;
1501 dtl = TALLOC_ZERO_P(lck, struct delete_token_list);
1506 dtl->name_hash = name_hash;
1507 dtl->delete_token = copy_unix_token(lck, tok);
1508 if (dtl->delete_token == NULL) {
1512 DLIST_ADD(lck->delete_tokens, dtl);
1513 lck->modified = true;
1517 /****************************************************************************
1518 Sets the delete on close flag over all share modes on this file.
1519 Modify the share mode entry for all files open
1520 on this device and inode to tell other smbds we have
1521 changed the delete on close flag. This will be noticed
1522 in the close code, the last closer will delete the file
1524 This makes a copy of any struct security_unix_token into the
1525 lck entry. This function is used when the lock is already granted.
1526 ****************************************************************************/
1528 void set_delete_on_close_lck(files_struct *fsp,
1529 struct share_mode_lock *lck,
1530 bool delete_on_close,
1531 const struct security_unix_token *tok)
1533 struct delete_token_list *dtl;
1536 if (delete_on_close) {
1537 SMB_ASSERT(tok != NULL);
1539 SMB_ASSERT(tok == NULL);
1542 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1543 if (dtl->name_hash == fsp->name_hash) {
1544 lck->modified = true;
1545 if (delete_on_close == false) {
1546 /* Delete this entry. */
1547 DLIST_REMOVE(lck->delete_tokens, dtl);
1551 /* Replace this token with the
1553 TALLOC_FREE(dtl->delete_token);
1554 dtl->delete_token = copy_unix_token(dtl, tok);
1555 SMB_ASSERT(dtl->delete_token != NULL);
1559 if (!delete_on_close) {
1560 /* Nothing to delete - not found. */
1564 ret = add_delete_on_close_token(lck, fsp->name_hash, tok);
1568 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const struct security_unix_token *tok)
1570 struct share_mode_lock *lck;
1572 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1573 "fnum = %d, file %s\n",
1574 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1577 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1583 set_delete_on_close_lck(fsp, lck, delete_on_close,
1584 delete_on_close ? tok : NULL);
1586 if (fsp->is_directory) {
1587 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1588 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1589 fsp->fsp_name->base_name);
1594 fsp->delete_on_close = delete_on_close;
1599 const struct security_unix_token *get_delete_on_close_token(struct share_mode_lock *lck, uint32_t name_hash)
1601 struct delete_token_list *dtl;
1603 DEBUG(10,("get_delete_on_close_token: name_hash = 0x%x\n",
1604 (unsigned int)name_hash ));
1606 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1607 DEBUG(10,("get_delete_on_close_token: dtl->name_hash = 0x%x\n",
1608 (unsigned int)dtl->name_hash ));
1609 if (dtl->name_hash == name_hash) {
1610 return dtl->delete_token;
1616 bool is_delete_on_close_set(struct share_mode_lock *lck, uint32_t name_hash)
1618 return (get_delete_on_close_token(lck, name_hash) != NULL);
1621 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1623 struct share_mode_lock *lck;
1625 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1626 timestring(talloc_tos(),
1627 convert_timespec_to_time_t(write_time)),
1628 file_id_string_tos(&fileid)));
1630 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1635 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1636 lck->modified = True;
1637 lck->changed_write_time = write_time;
1644 bool set_write_time(struct file_id fileid, struct timespec write_time)
1646 struct share_mode_lock *lck;
1648 DEBUG(5,("set_write_time: %s id=%s\n",
1649 timestring(talloc_tos(),
1650 convert_timespec_to_time_t(write_time)),
1651 file_id_string_tos(&fileid)));
1653 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1658 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1659 lck->modified = True;
1660 lck->old_write_time = write_time;
1668 struct forall_state {
1669 void (*fn)(const struct share_mode_entry *entry,
1670 const char *sharepath,
1672 void *private_data);
1676 static int traverse_fn(struct db_record *rec, void *_state)
1678 struct forall_state *state = (struct forall_state *)_state;
1679 struct locking_data *data;
1680 struct share_mode_entry *shares;
1681 const char *sharepath;
1683 const char *del_tokens;
1684 uint32_t total_del_token_size = 0;
1687 /* Ensure this is a locking_key record. */
1688 if (rec->key.dsize != sizeof(struct file_id))
1691 data = (struct locking_data *)rec->value.dptr;
1692 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1693 del_tokens = (const char *)rec->value.dptr + sizeof(*data) +
1694 data->u.s.num_share_mode_entries*sizeof(*shares);
1696 for (i = 0; i < data->u.s.num_delete_token_entries; i++) {
1697 uint32_t del_token_size;
1698 memcpy(&del_token_size, del_tokens, sizeof(uint32_t));
1699 total_del_token_size += del_token_size;
1700 del_tokens += del_token_size;
1703 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1704 data->u.s.num_share_mode_entries*sizeof(*shares) +
1705 total_del_token_size;
1706 fname = (const char *)rec->value.dptr + sizeof(*data) +
1707 data->u.s.num_share_mode_entries*sizeof(*shares) +
1708 total_del_token_size +
1709 strlen(sharepath) + 1;
1711 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1712 state->fn(&shares[i], sharepath, fname,
1713 state->private_data);
1718 /*******************************************************************
1719 Call the specified function on each entry under management by the
1721 ********************************************************************/
1723 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1724 const char *, void *),
1727 struct forall_state state;
1729 if (lock_db == NULL)
1733 state.private_data = private_data;
1735 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);