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 "system/filesys.h"
40 #include "locking/proto.h"
41 #include "librpc/gen_ndr/messaging.h"
42 #include "smbd/globals.h"
44 #include "../libcli/security/security.h"
48 #define DBGC_CLASS DBGC_LOCKING
50 #define NO_LOCKING_COUNT (-1)
52 /* the locking database handle */
53 static struct db_context *lock_db;
55 /****************************************************************************
57 ****************************************************************************/
59 const char *lock_type_name(enum brl_type lock_type)
66 case PENDING_READ_LOCK:
67 return "PENDING_READ";
68 case PENDING_WRITE_LOCK:
69 return "PENDING_WRITE";
75 const char *lock_flav_name(enum brl_flavour lock_flav)
77 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
80 /****************************************************************************
81 Utility function called to see if a file region is locked.
82 Called in the read/write codepath.
83 ****************************************************************************/
85 void init_strict_lock_struct(files_struct *fsp,
89 enum brl_type lock_type,
90 struct lock_struct *plock)
92 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
94 plock->context.smblctx = smblctx;
95 plock->context.tid = fsp->conn->cnum;
96 plock->context.pid = sconn_server_id(fsp->conn->sconn);
99 plock->fnum = fsp->fnum;
100 plock->lock_type = lock_type;
101 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
104 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
106 int strict_locking = lp_strict_locking(fsp->conn->params);
109 if (plock->size == 0) {
113 if (!lp_locking(fsp->conn->params) || !strict_locking) {
117 if (strict_locking == Auto) {
118 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
119 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
121 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
122 (plock->lock_type == READ_LOCK)) {
123 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
126 struct byte_range_lock *br_lck;
128 br_lck = brl_get_locks_readonly(fsp);
132 ret = brl_locktest(br_lck,
133 plock->context.smblctx,
141 struct byte_range_lock *br_lck;
143 br_lck = brl_get_locks_readonly(fsp);
147 ret = brl_locktest(br_lck,
148 plock->context.smblctx,
156 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
157 "len=%.0f %s for fnum %d file %s\n",
158 lock_flav_name(plock->lock_flav),
159 (double)plock->start, (double)plock->size,
160 ret ? "unlocked" : "locked",
161 plock->fnum, fsp_str_dbg(fsp)));
166 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
170 /****************************************************************************
171 Find out if a lock could be granted - return who is blocking us if we can't.
172 ****************************************************************************/
174 NTSTATUS query_lock(files_struct *fsp,
178 enum brl_type *plock_type,
179 enum brl_flavour lock_flav)
181 struct byte_range_lock *br_lck = NULL;
183 if (!fsp->can_lock) {
184 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
187 if (!lp_locking(fsp->conn->params)) {
191 br_lck = brl_get_locks_readonly(fsp);
193 return NT_STATUS_NO_MEMORY;
196 return brl_lockquery(br_lck,
198 sconn_server_id(fsp->conn->sconn),
205 static void increment_current_lock_count(files_struct *fsp,
206 enum brl_flavour lock_flav)
208 if (lock_flav == WINDOWS_LOCK &&
209 fsp->current_lock_count != NO_LOCKING_COUNT) {
210 /* blocking ie. pending, locks also count here,
211 * as this is an efficiency counter to avoid checking
212 * the lock db. on close. JRA. */
214 fsp->current_lock_count++;
216 /* Notice that this has had a POSIX lock request.
217 * We can't count locks after this so forget them.
219 fsp->current_lock_count = NO_LOCKING_COUNT;
223 static void decrement_current_lock_count(files_struct *fsp,
224 enum brl_flavour lock_flav)
226 if (lock_flav == WINDOWS_LOCK &&
227 fsp->current_lock_count != NO_LOCKING_COUNT) {
228 SMB_ASSERT(fsp->current_lock_count > 0);
229 fsp->current_lock_count--;
233 /****************************************************************************
234 Utility function called by locking requests.
235 ****************************************************************************/
237 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
242 enum brl_type lock_type,
243 enum brl_flavour lock_flav,
247 struct blocking_lock_record *blr)
249 struct byte_range_lock *br_lck = NULL;
251 /* silently return ok on print files as we don't do locking there */
252 if (fsp->print_file) {
253 *perr = NT_STATUS_OK;
257 if (!fsp->can_lock) {
258 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
262 if (!lp_locking(fsp->conn->params)) {
263 *perr = NT_STATUS_OK;
267 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
269 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
270 "blocking_lock=%s requested for fnum %d file %s\n",
271 lock_flav_name(lock_flav), lock_type_name(lock_type),
272 (double)offset, (double)count, blocking_lock ? "true" :
273 "false", fsp->fnum, fsp_str_dbg(fsp)));
275 br_lck = brl_get_locks(talloc_tos(), fsp);
277 *perr = NT_STATUS_NO_MEMORY;
281 *perr = brl_lock(msg_ctx,
284 sconn_server_id(fsp->conn->sconn),
293 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
295 increment_current_lock_count(fsp, lock_flav);
299 /****************************************************************************
300 Utility function called by unlocking requests.
301 ****************************************************************************/
303 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
308 enum brl_flavour lock_flav)
311 struct byte_range_lock *br_lck = NULL;
313 if (!fsp->can_lock) {
314 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
317 if (!lp_locking(fsp->conn->params)) {
321 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
322 (double)offset, (double)count, fsp->fnum,
325 br_lck = brl_get_locks(talloc_tos(), fsp);
327 return NT_STATUS_NO_MEMORY;
330 ok = brl_unlock(msg_ctx,
333 sconn_server_id(fsp->conn->sconn),
341 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
342 return NT_STATUS_RANGE_NOT_LOCKED;
345 decrement_current_lock_count(fsp, lock_flav);
349 /****************************************************************************
350 Cancel any pending blocked locks.
351 ****************************************************************************/
353 NTSTATUS do_lock_cancel(files_struct *fsp,
357 enum brl_flavour lock_flav,
358 struct blocking_lock_record *blr)
361 struct byte_range_lock *br_lck = NULL;
363 if (!fsp->can_lock) {
364 return fsp->is_directory ?
365 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
368 if (!lp_locking(fsp->conn->params)) {
369 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
372 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
373 (double)offset, (double)count, fsp->fnum,
376 br_lck = brl_get_locks(talloc_tos(), fsp);
378 return NT_STATUS_NO_MEMORY;
381 ok = brl_lock_cancel(br_lck,
383 sconn_server_id(fsp->conn->sconn),
392 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
393 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
396 decrement_current_lock_count(fsp, lock_flav);
400 /****************************************************************************
401 Remove any locks on this fd. Called from file_close().
402 ****************************************************************************/
404 void locking_close_file(struct messaging_context *msg_ctx,
406 enum file_close_type close_type)
408 struct byte_range_lock *br_lck;
410 if (!lp_locking(fsp->conn->params)) {
414 /* If we have not outstanding locks or pending
415 * locks then we don't need to look in the lock db.
418 if (fsp->current_lock_count == 0) {
422 br_lck = brl_get_locks(talloc_tos(),fsp);
425 cancel_pending_lock_requests_by_fid(fsp, br_lck, close_type);
426 brl_close_fnum(msg_ctx, br_lck);
431 /****************************************************************************
432 Initialise the locking functions.
433 ****************************************************************************/
435 static bool locking_init_internal(bool read_only)
442 lock_db = db_open(NULL, lock_path("locking.tdb"),
443 lp_open_files_db_hash_size(),
444 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
445 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
448 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
452 if (!posix_locking_init(read_only))
458 bool locking_init(void)
460 return locking_init_internal(false);
463 bool locking_init_readonly(void)
465 return locking_init_internal(true);
468 /*******************************************************************
469 Deinitialize the share_mode management.
470 ******************************************************************/
472 bool locking_end(void)
475 TALLOC_FREE(lock_db);
479 /*******************************************************************
480 Form a static locking key for a dev/inode pair.
481 ******************************************************************/
483 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
486 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
489 /*******************************************************************
490 Print out a share mode.
491 ********************************************************************/
493 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
495 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
496 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
497 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
498 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
500 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
501 procid_str_static(&e->pid),
502 e->share_access, e->private_options,
503 e->access_mask, (unsigned long long)e->op_mid,
504 e->op_type, e->share_file_id,
505 (unsigned int)e->uid, (unsigned int)e->flags,
506 file_id_string_tos(&e->id),
507 (unsigned int)e->name_hash);
510 /*******************************************************************
511 Print out a share mode table.
512 ********************************************************************/
514 static void print_share_mode_table(struct locking_data *data)
516 int num_share_modes = data->u.s.num_share_mode_entries;
517 struct share_mode_entry *shares =
518 (struct share_mode_entry *)(data + 1);
521 for (i = 0; i < num_share_modes; i++) {
522 struct share_mode_entry entry;
526 * We need to memcpy the entry here due to alignment
527 * restrictions that are not met when directly accessing
531 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
532 str = share_mode_str(talloc_tos(), i, &entry);
534 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
539 static int parse_delete_tokens_list(struct share_mode_lock *lck,
540 struct locking_data *pdata,
543 uint8_t *p = dbuf.dptr + sizeof(struct locking_data) +
544 (lck->num_share_modes *
545 sizeof(struct share_mode_entry));
546 uint8_t *end_ptr = dbuf.dptr + (dbuf.dsize - 2);
547 int delete_tokens_size = 0;
550 lck->delete_tokens = NULL;
552 for (i = 0; i < pdata->u.s.num_delete_token_entries; i++) {
554 struct delete_token_list *pdtl;
556 if (end_ptr - p < (sizeof(uint32_t) + sizeof(uint32_t) +
557 sizeof(uid_t) + sizeof(gid_t))) {
558 DEBUG(0,("parse_delete_tokens_list: "
559 "corrupt token list (%u)",
560 (unsigned int)(end_ptr - p)));
561 smb_panic("corrupt token list");
565 memcpy(&token_len, p, sizeof(token_len));
566 delete_tokens_size += token_len;
568 if (p + token_len > end_ptr || token_len < sizeof(token_len) +
569 sizeof(pdtl->name_hash) +
572 DEBUG(0,("parse_delete_tokens_list: "
573 "invalid token length (%u)\n",
574 (unsigned int)token_len ));
575 smb_panic("invalid token length");
579 p += sizeof(token_len);
581 pdtl = TALLOC_ZERO_P(lck, struct delete_token_list);
583 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
586 /* Copy out the name_hash. */
587 memcpy(&pdtl->name_hash, p, sizeof(pdtl->name_hash));
588 p += sizeof(pdtl->name_hash);
590 pdtl->delete_token = TALLOC_ZERO_P(pdtl, struct security_unix_token);
591 if (pdtl->delete_token == NULL) {
592 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
596 /* Copy out the uid and gid. */
597 memcpy(&pdtl->delete_token->uid, p, sizeof(uid_t));
599 memcpy(&pdtl->delete_token->gid, p, sizeof(gid_t));
602 token_len -= (sizeof(token_len) + sizeof(pdtl->name_hash) +
603 sizeof(uid_t) + sizeof(gid_t));
605 /* Any supplementary groups ? */
609 if (token_len % sizeof(gid_t) != 0) {
610 DEBUG(0,("parse_delete_tokens_list: "
611 "corrupt group list (%u)",
612 (unsigned int)(token_len % sizeof(gid_t)) ));
613 smb_panic("corrupt group list");
617 pdtl->delete_token->ngroups = token_len / sizeof(gid_t);
618 pdtl->delete_token->groups = TALLOC_ARRAY(pdtl->delete_token, gid_t,
619 pdtl->delete_token->ngroups);
620 if (pdtl->delete_token->groups == NULL) {
621 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
625 for (j = 0; j < pdtl->delete_token->ngroups; j++) {
626 memcpy(&pdtl->delete_token->groups[j], p, sizeof(gid_t));
630 /* Add to the list. */
631 DLIST_ADD(lck->delete_tokens, pdtl);
634 return delete_tokens_size;
637 /*******************************************************************
638 Get all share mode entries for a dev/inode pair.
639 ********************************************************************/
641 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
643 struct locking_data data;
644 int delete_tokens_size;
647 if (dbuf.dsize < sizeof(struct locking_data)) {
648 smb_panic("parse_share_modes: buffer too short");
651 memcpy(&data, dbuf.dptr, sizeof(data));
653 lck->old_write_time = data.u.s.old_write_time;
654 lck->changed_write_time = data.u.s.changed_write_time;
655 lck->num_share_modes = data.u.s.num_share_mode_entries;
657 DEBUG(10, ("parse_share_modes: owrt: %s, "
658 "cwrt: %s, ntok: %u, num_share_modes: %d\n",
659 timestring(talloc_tos(),
660 convert_timespec_to_time_t(lck->old_write_time)),
661 timestring(talloc_tos(),
662 convert_timespec_to_time_t(
663 lck->changed_write_time)),
664 (unsigned int)data.u.s.num_delete_token_entries,
665 lck->num_share_modes));
667 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
668 DEBUG(0, ("invalid number of share modes: %d\n",
669 lck->num_share_modes));
670 smb_panic("parse_share_modes: invalid number of share modes");
673 lck->share_modes = NULL;
675 if (lck->num_share_modes != 0) {
677 if (dbuf.dsize < (sizeof(struct locking_data) +
678 (lck->num_share_modes *
679 sizeof(struct share_mode_entry)))) {
680 smb_panic("parse_share_modes: buffer too short");
683 lck->share_modes = (struct share_mode_entry *)
685 dbuf.dptr+sizeof(struct locking_data),
686 lck->num_share_modes *
687 sizeof(struct share_mode_entry));
689 if (lck->share_modes == NULL) {
690 smb_panic("parse_share_modes: talloc failed");
694 /* Get any delete tokens. */
695 delete_tokens_size = parse_delete_tokens_list(lck, &data, dbuf);
696 if (delete_tokens_size < 0) {
697 smb_panic("parse_share_modes: parse_delete_tokens_list failed");
700 /* Save off the associated service path and filename. */
701 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
702 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
705 lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
706 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
708 strlen(lck->servicepath) + 1;
710 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
711 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
713 strlen(lck->servicepath) + 1 +
714 strlen(lck->base_name) + 1;
717 * Ensure that each entry has a real process attached.
720 for (i = 0; i < lck->num_share_modes; i++) {
721 struct share_mode_entry *entry_p = &lck->share_modes[i];
723 if (DEBUGLEVEL >= 10) {
724 str = share_mode_str(NULL, i, entry_p);
726 DEBUG(10,("parse_share_modes: %s\n",
728 if (!serverid_exists(&entry_p->pid)) {
729 DEBUG(10,("parse_share_modes: deleted %s\n",
731 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
732 lck->modified = True;
740 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
745 struct locking_data *data;
747 ssize_t sp_len, bn_len, sn_len;
748 uint32_t delete_tokens_size = 0;
749 struct delete_token_list *pdtl = NULL;
750 uint32_t num_delete_token_entries = 0;
755 for (i=0; i<lck->num_share_modes; i++) {
756 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
761 if (num_valid == 0) {
765 sp_len = strlen(lck->servicepath);
766 bn_len = strlen(lck->base_name);
767 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
769 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
770 num_delete_token_entries++;
771 delete_tokens_size += (sizeof(uint32_t) +
775 pdtl->delete_token->ngroups*sizeof(gid_t));
778 result.dsize = sizeof(*data) +
779 lck->num_share_modes * sizeof(struct share_mode_entry) +
784 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
786 if (result.dptr == NULL) {
787 smb_panic("talloc failed");
790 data = (struct locking_data *)result.dptr;
792 data->u.s.num_share_mode_entries = lck->num_share_modes;
793 data->u.s.old_write_time = lck->old_write_time;
794 data->u.s.changed_write_time = lck->changed_write_time;
795 data->u.s.num_delete_token_entries = num_delete_token_entries;
797 DEBUG(10,("unparse_share_modes: owrt: %s cwrt: %s, ntok: %u, "
799 timestring(talloc_tos(),
800 convert_timespec_to_time_t(lck->old_write_time)),
801 timestring(talloc_tos(),
802 convert_timespec_to_time_t(
803 lck->changed_write_time)),
804 (unsigned int)data->u.s.num_delete_token_entries,
805 data->u.s.num_share_mode_entries));
807 memcpy(result.dptr + sizeof(*data), lck->share_modes,
808 sizeof(struct share_mode_entry)*lck->num_share_modes);
809 offset = sizeof(*data) +
810 sizeof(struct share_mode_entry)*lck->num_share_modes;
812 /* Store any delete on close tokens. */
813 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
814 struct security_unix_token *pdt = pdtl->delete_token;
815 uint32_t token_size = sizeof(uint32_t) +
819 (pdt->ngroups * sizeof(gid_t));
820 uint8_t *p = result.dptr + offset;
822 memcpy(p, &token_size, sizeof(uint32_t));
823 p += sizeof(uint32_t);
825 memcpy(p, &pdtl->name_hash, sizeof(uint32_t));
826 p += sizeof(uint32_t);
828 memcpy(p, &pdt->uid, sizeof(uid_t));
831 memcpy(p, &pdt->gid, sizeof(gid_t));
834 for (i = 0; i < pdt->ngroups; i++) {
835 memcpy(p, &pdt->groups[i], sizeof(gid_t));
838 offset += token_size;
841 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
842 result.dsize - offset - 1);
843 offset += sp_len + 1;
844 safe_strcpy((char *)result.dptr + offset, lck->base_name,
845 result.dsize - offset - 1);
846 offset += bn_len + 1;
847 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
848 result.dsize - offset - 1);
850 if (DEBUGLEVEL >= 10) {
851 print_share_mode_table(data);
857 static int share_mode_lock_destructor(struct share_mode_lock *lck)
862 if (!lck->modified) {
866 data = unparse_share_modes(lck);
868 if (data.dptr == NULL) {
870 /* There has been an entry before, delete it */
872 status = lck->record->delete_rec(lck->record);
873 if (!NT_STATUS_IS_OK(status)) {
876 DEBUG(0, ("delete_rec returned %s\n",
879 if (asprintf(&errmsg, "could not delete share "
881 nt_errstr(status)) == -1) {
882 smb_panic("could not delete share"
891 status = lck->record->store(lck->record, data, TDB_REPLACE);
892 if (!NT_STATUS_IS_OK(status)) {
895 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
897 if (asprintf(&errmsg, "could not store share mode entry: %s",
898 nt_errstr(status)) == -1) {
899 smb_panic("could not store share mode entry");
909 static bool fill_share_mode_lock(struct share_mode_lock *lck,
911 const char *servicepath,
912 const struct smb_filename *smb_fname,
913 TDB_DATA share_mode_data,
914 const struct timespec *old_write_time)
916 /* Ensure we set every field here as the destructor must be
917 valid even if parse_share_modes fails. */
919 lck->servicepath = NULL;
920 lck->base_name = NULL;
921 lck->stream_name = NULL;
923 lck->num_share_modes = 0;
924 lck->share_modes = NULL;
925 lck->delete_tokens = NULL;
926 ZERO_STRUCT(lck->old_write_time);
927 ZERO_STRUCT(lck->changed_write_time);
929 lck->modified = False;
931 lck->fresh = (share_mode_data.dptr == NULL);
935 if (smb_fname == NULL || servicepath == NULL
936 || old_write_time == NULL) {
940 has_stream = smb_fname->stream_name != NULL;
942 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
943 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
944 lck->servicepath = talloc_strdup(lck, servicepath);
945 if (lck->base_name == NULL ||
946 (has_stream && lck->stream_name == NULL) ||
947 lck->servicepath == NULL) {
948 DEBUG(0, ("talloc failed\n"));
951 lck->old_write_time = *old_write_time;
953 if (!parse_share_modes(share_mode_data, lck)) {
954 DEBUG(0, ("Could not parse share modes\n"));
962 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
963 const struct file_id id,
964 const char *servicepath,
965 const struct smb_filename *smb_fname,
966 const struct timespec *old_write_time)
968 struct share_mode_lock *lck;
970 TDB_DATA key = locking_key(&id, &tmp);
972 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
973 DEBUG(0, ("talloc failed\n"));
977 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
978 DEBUG(3, ("Could not lock share entry\n"));
983 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
984 lck->record->value, old_write_time)) {
985 DEBUG(3, ("fill_share_mode_lock failed\n"));
990 talloc_set_destructor(lck, share_mode_lock_destructor);
995 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
996 const struct file_id id)
998 struct share_mode_lock *lck;
1000 TDB_DATA key = locking_key(&id, &tmp);
1003 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
1004 DEBUG(0, ("talloc failed\n"));
1008 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
1009 DEBUG(3, ("Could not fetch share entry\n"));
1014 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
1015 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
1016 "around (file not open)\n"));
1024 /*******************************************************************
1025 Sets the service name and filename for rename.
1026 At this point we emit "file renamed" messages to all
1027 process id's that have this file open.
1028 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
1029 ********************************************************************/
1031 bool rename_share_filename(struct messaging_context *msg_ctx,
1032 struct share_mode_lock *lck,
1033 const char *servicepath,
1034 uint32_t orig_name_hash,
1035 uint32_t new_name_hash,
1036 const struct smb_filename *smb_fname_dst)
1044 bool strip_two_chars = false;
1045 bool has_stream = smb_fname_dst->stream_name != NULL;
1047 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
1048 servicepath, smb_fname_dst->base_name));
1051 * rename_internal_fsp() and rename_internals() add './' to
1052 * head of newname if newname does not contain a '/'.
1054 if (smb_fname_dst->base_name[0] &&
1055 smb_fname_dst->base_name[1] &&
1056 smb_fname_dst->base_name[0] == '.' &&
1057 smb_fname_dst->base_name[1] == '/') {
1058 strip_two_chars = true;
1061 lck->servicepath = talloc_strdup(lck, servicepath);
1062 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
1063 (strip_two_chars ? 2 : 0));
1064 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
1065 if (lck->base_name == NULL ||
1066 (has_stream && lck->stream_name == NULL) ||
1067 lck->servicepath == NULL) {
1068 DEBUG(0, ("rename_share_filename: talloc failed\n"));
1071 lck->modified = True;
1073 sp_len = strlen(lck->servicepath);
1074 bn_len = strlen(lck->base_name);
1075 sn_len = has_stream ? strlen(lck->stream_name) : 0;
1077 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
1080 /* Set up the name changed message. */
1081 frm = TALLOC_ARRAY(lck, char, msg_len);
1086 push_file_id_24(frm, &lck->id);
1088 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1090 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1091 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1092 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1095 /* Send the messages. */
1096 for (i=0; i<lck->num_share_modes; i++) {
1097 struct share_mode_entry *se = &lck->share_modes[i];
1098 if (!is_valid_share_mode_entry(se)) {
1102 /* If this is a hardlink to the inode
1103 with a different name, skip this. */
1104 if (se->name_hash != orig_name_hash) {
1108 se->name_hash = new_name_hash;
1110 /* But not to ourselves... */
1111 if (procid_is_me(&se->pid)) {
1115 DEBUG(10,("rename_share_filename: sending rename message to "
1116 "pid %s file_id %s sharepath %s base_name %s "
1118 procid_str_static(&se->pid),
1119 file_id_string_tos(&lck->id),
1120 lck->servicepath, lck->base_name,
1121 has_stream ? lck->stream_name : ""));
1123 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1124 (uint8 *)frm, msg_len);
1130 void get_file_infos(struct file_id id,
1132 bool *delete_on_close,
1133 struct timespec *write_time)
1135 struct share_mode_lock *lck;
1137 if (delete_on_close) {
1138 *delete_on_close = false;
1142 ZERO_STRUCTP(write_time);
1145 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1149 if (delete_on_close) {
1150 *delete_on_close = is_delete_on_close_set(lck, name_hash);
1156 wt = lck->changed_write_time;
1157 if (null_timespec(wt)) {
1158 wt = lck->old_write_time;
1167 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1171 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1172 /* cope with dead entries from the process not
1173 existing. These should not be considered valid,
1174 otherwise we end up doing zero timeout sharing
1179 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1180 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1181 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1183 SMB_ASSERT(num_props <= 1);
1184 return (num_props != 0);
1187 bool is_deferred_open_entry(const struct share_mode_entry *e)
1189 return (e->op_type == DEFERRED_OPEN_ENTRY);
1192 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1194 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1197 /*******************************************************************
1198 Fill a share mode entry.
1199 ********************************************************************/
1201 static void fill_share_mode_entry(struct share_mode_entry *e,
1203 uid_t uid, uint64_t mid, uint16 op_type)
1206 e->pid = sconn_server_id(fsp->conn->sconn);
1207 e->share_access = fsp->share_access;
1208 e->private_options = fsp->fh->private_options;
1209 e->access_mask = fsp->access_mask;
1211 e->op_type = op_type;
1212 e->time.tv_sec = fsp->open_time.tv_sec;
1213 e->time.tv_usec = fsp->open_time.tv_usec;
1214 e->id = fsp->file_id;
1215 e->share_file_id = fsp->fh->gen_id;
1216 e->uid = (uint32)uid;
1217 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1218 e->name_hash = fsp->name_hash;
1221 static void fill_deferred_open_entry(struct share_mode_entry *e,
1222 const struct timeval request_time,
1224 struct server_id pid,
1230 e->op_type = DEFERRED_OPEN_ENTRY;
1231 e->time.tv_sec = request_time.tv_sec;
1232 e->time.tv_usec = request_time.tv_usec;
1234 e->uid = (uint32)-1;
1238 static void add_share_mode_entry(struct share_mode_lock *lck,
1239 const struct share_mode_entry *entry)
1243 for (i=0; i<lck->num_share_modes; i++) {
1244 struct share_mode_entry *e = &lck->share_modes[i];
1245 if (is_unused_share_mode_entry(e)) {
1251 if (i == lck->num_share_modes) {
1252 /* No unused entry found */
1253 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1254 &lck->share_modes, &lck->num_share_modes);
1256 lck->modified = True;
1259 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1260 uid_t uid, uint64_t mid, uint16 op_type)
1262 struct share_mode_entry entry;
1263 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1264 add_share_mode_entry(lck, &entry);
1267 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1268 struct timeval request_time,
1269 struct server_id pid, struct file_id id)
1271 struct share_mode_entry entry;
1272 fill_deferred_open_entry(&entry, request_time, id, pid, mid);
1273 add_share_mode_entry(lck, &entry);
1276 /*******************************************************************
1277 Check if two share mode entries are identical, ignoring oplock
1278 and mid info and desired_access. (Removed paranoia test - it's
1279 not automatically a logic error if they are identical. JRA.)
1280 ********************************************************************/
1282 static bool share_modes_identical(struct share_mode_entry *e1,
1283 struct share_mode_entry *e2)
1285 /* We used to check for e1->share_access == e2->share_access here
1286 as well as the other fields but 2 different DOS or FCB opens
1287 sharing the same share mode entry may validly differ in
1288 fsp->share_access field. */
1290 return (procid_equal(&e1->pid, &e2->pid) &&
1291 file_id_equal(&e1->id, &e2->id) &&
1292 e1->share_file_id == e2->share_file_id );
1295 static bool deferred_open_identical(struct share_mode_entry *e1,
1296 struct share_mode_entry *e2)
1298 return (procid_equal(&e1->pid, &e2->pid) &&
1299 (e1->op_mid == e2->op_mid) &&
1300 file_id_equal(&e1->id, &e2->id));
1303 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1304 struct share_mode_entry *entry)
1308 for (i=0; i<lck->num_share_modes; i++) {
1309 struct share_mode_entry *e = &lck->share_modes[i];
1310 if (is_valid_share_mode_entry(entry) &&
1311 is_valid_share_mode_entry(e) &&
1312 share_modes_identical(e, entry)) {
1315 if (is_deferred_open_entry(entry) &&
1316 is_deferred_open_entry(e) &&
1317 deferred_open_identical(e, entry)) {
1324 /*******************************************************************
1325 Del the share mode of a file for this process. Return the number of
1327 ********************************************************************/
1329 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1331 struct share_mode_entry entry, *e;
1333 /* Don't care about the pid owner being correct here - just a search. */
1334 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1336 e = find_share_mode_entry(lck, &entry);
1341 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1342 lck->modified = True;
1346 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid,
1347 struct server_id pid)
1349 struct share_mode_entry entry, *e;
1351 fill_deferred_open_entry(&entry, timeval_zero(),
1354 e = find_share_mode_entry(lck, &entry);
1359 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1360 lck->modified = True;
1363 /*******************************************************************
1364 Remove an oplock mid and mode entry from a share mode.
1365 ********************************************************************/
1367 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1369 struct share_mode_entry entry, *e;
1371 /* Don't care about the pid owner being correct here - just a search. */
1372 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1374 e = find_share_mode_entry(lck, &entry);
1379 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1381 * Going from exclusive or batch,
1382 * we always go through FAKE_LEVEL_II
1385 e->op_type = FAKE_LEVEL_II_OPLOCK;
1387 e->op_type = NO_OPLOCK;
1389 lck->modified = True;
1393 /*******************************************************************
1394 Downgrade a oplock type from exclusive to level II.
1395 ********************************************************************/
1397 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1399 struct share_mode_entry entry, *e;
1401 /* Don't care about the pid owner being correct here - just a search. */
1402 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1404 e = find_share_mode_entry(lck, &entry);
1409 e->op_type = LEVEL_II_OPLOCK;
1410 lck->modified = True;
1414 /****************************************************************************
1415 Check if setting delete on close is allowed on this fsp.
1416 ****************************************************************************/
1418 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1421 * Only allow delete on close for writable files.
1424 if ((dosmode & aRONLY) &&
1425 !lp_delete_readonly(SNUM(fsp->conn))) {
1426 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1427 "flag set but file attribute is readonly.\n",
1429 return NT_STATUS_CANNOT_DELETE;
1433 * Only allow delete on close for writable shares.
1436 if (!CAN_WRITE(fsp->conn)) {
1437 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1438 "close flag set but write access denied on share.\n",
1440 return NT_STATUS_ACCESS_DENIED;
1444 * Only allow delete on close for files/directories opened with delete
1448 if (!(fsp->access_mask & DELETE_ACCESS)) {
1449 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1450 "close flag set but delete access denied.\n",
1452 return NT_STATUS_ACCESS_DENIED;
1455 /* Don't allow delete on close for non-empty directories. */
1456 if (fsp->is_directory) {
1457 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1458 return can_delete_directory(fsp->conn,
1459 fsp->fsp_name->base_name);
1462 return NT_STATUS_OK;
1465 /*************************************************************************
1466 Return a talloced copy of a struct security_unix_token. NULL on fail.
1467 (Should this be in locking.c.... ?).
1468 *************************************************************************/
1470 static struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct security_unix_token *tok)
1472 struct security_unix_token *cpy;
1474 cpy = TALLOC_P(ctx, struct security_unix_token);
1479 cpy->uid = tok->uid;
1480 cpy->gid = tok->gid;
1481 cpy->ngroups = tok->ngroups;
1483 /* Make this a talloc child of cpy. */
1484 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1488 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1493 /****************************************************************************
1494 Adds a delete on close token.
1495 ****************************************************************************/
1497 static bool add_delete_on_close_token(struct share_mode_lock *lck,
1499 const struct security_unix_token *tok)
1501 struct delete_token_list *dtl;
1503 dtl = TALLOC_ZERO_P(lck, struct delete_token_list);
1508 dtl->name_hash = name_hash;
1509 dtl->delete_token = copy_unix_token(lck, tok);
1510 if (dtl->delete_token == NULL) {
1514 DLIST_ADD(lck->delete_tokens, dtl);
1515 lck->modified = true;
1519 /****************************************************************************
1520 Sets the delete on close flag over all share modes on this file.
1521 Modify the share mode entry for all files open
1522 on this device and inode to tell other smbds we have
1523 changed the delete on close flag. This will be noticed
1524 in the close code, the last closer will delete the file
1526 This makes a copy of any struct security_unix_token into the
1527 lck entry. This function is used when the lock is already granted.
1528 ****************************************************************************/
1530 void set_delete_on_close_lck(files_struct *fsp,
1531 struct share_mode_lock *lck,
1532 bool delete_on_close,
1533 const struct security_unix_token *tok)
1535 struct delete_token_list *dtl;
1538 if (delete_on_close) {
1539 SMB_ASSERT(tok != NULL);
1541 SMB_ASSERT(tok == NULL);
1544 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1545 if (dtl->name_hash == fsp->name_hash) {
1546 lck->modified = true;
1547 if (delete_on_close == false) {
1548 /* Delete this entry. */
1549 DLIST_REMOVE(lck->delete_tokens, dtl);
1553 /* Replace this token with the
1555 TALLOC_FREE(dtl->delete_token);
1556 dtl->delete_token = copy_unix_token(dtl, tok);
1557 SMB_ASSERT(dtl->delete_token != NULL);
1561 if (!delete_on_close) {
1562 /* Nothing to delete - not found. */
1566 ret = add_delete_on_close_token(lck, fsp->name_hash, tok);
1570 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const struct security_unix_token *tok)
1572 struct share_mode_lock *lck;
1574 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1575 "fnum = %d, file %s\n",
1576 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1579 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1585 set_delete_on_close_lck(fsp, lck, delete_on_close,
1586 delete_on_close ? tok : NULL);
1588 if (fsp->is_directory) {
1589 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1590 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1591 fsp->fsp_name->base_name);
1596 fsp->delete_on_close = delete_on_close;
1601 const struct security_unix_token *get_delete_on_close_token(struct share_mode_lock *lck, uint32_t name_hash)
1603 struct delete_token_list *dtl;
1605 DEBUG(10,("get_delete_on_close_token: name_hash = 0x%x\n",
1606 (unsigned int)name_hash ));
1608 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1609 DEBUG(10,("get_delete_on_close_token: dtl->name_hash = 0x%x\n",
1610 (unsigned int)dtl->name_hash ));
1611 if (dtl->name_hash == name_hash) {
1612 return dtl->delete_token;
1618 bool is_delete_on_close_set(struct share_mode_lock *lck, uint32_t name_hash)
1620 return (get_delete_on_close_token(lck, name_hash) != NULL);
1623 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1625 struct share_mode_lock *lck;
1627 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1628 timestring(talloc_tos(),
1629 convert_timespec_to_time_t(write_time)),
1630 file_id_string_tos(&fileid)));
1632 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1637 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1638 lck->modified = True;
1639 lck->changed_write_time = write_time;
1646 bool set_write_time(struct file_id fileid, struct timespec write_time)
1648 struct share_mode_lock *lck;
1650 DEBUG(5,("set_write_time: %s id=%s\n",
1651 timestring(talloc_tos(),
1652 convert_timespec_to_time_t(write_time)),
1653 file_id_string_tos(&fileid)));
1655 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1660 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1661 lck->modified = True;
1662 lck->old_write_time = write_time;
1670 struct forall_state {
1671 void (*fn)(const struct share_mode_entry *entry,
1672 const char *sharepath,
1674 void *private_data);
1678 static int traverse_fn(struct db_record *rec, void *_state)
1680 struct forall_state *state = (struct forall_state *)_state;
1681 struct locking_data *data;
1682 struct share_mode_entry *shares;
1683 const char *sharepath;
1685 const char *del_tokens;
1686 uint32_t total_del_token_size = 0;
1689 /* Ensure this is a locking_key record. */
1690 if (rec->key.dsize != sizeof(struct file_id))
1693 data = (struct locking_data *)rec->value.dptr;
1694 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1695 del_tokens = (const char *)rec->value.dptr + sizeof(*data) +
1696 data->u.s.num_share_mode_entries*sizeof(*shares);
1698 for (i = 0; i < data->u.s.num_delete_token_entries; i++) {
1699 uint32_t del_token_size;
1700 memcpy(&del_token_size, del_tokens, sizeof(uint32_t));
1701 total_del_token_size += del_token_size;
1702 del_tokens += del_token_size;
1705 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1706 data->u.s.num_share_mode_entries*sizeof(*shares) +
1707 total_del_token_size;
1708 fname = (const char *)rec->value.dptr + sizeof(*data) +
1709 data->u.s.num_share_mode_entries*sizeof(*shares) +
1710 total_del_token_size +
1711 strlen(sharepath) + 1;
1713 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1714 state->fn(&shares[i], sharepath, fname,
1715 state->private_data);
1720 /*******************************************************************
1721 Call the specified function on each entry under management by the
1723 ********************************************************************/
1725 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1726 const char *, void *),
1729 struct forall_state state;
1731 if (lock_db == NULL)
1735 state.private_data = private_data;
1737 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);