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"
49 #define DBGC_CLASS DBGC_LOCKING
51 #define NO_LOCKING_COUNT (-1)
53 /* the locking database handle */
54 static struct db_context *lock_db;
56 /****************************************************************************
58 ****************************************************************************/
60 const char *lock_type_name(enum brl_type lock_type)
67 case PENDING_READ_LOCK:
68 return "PENDING_READ";
69 case PENDING_WRITE_LOCK:
70 return "PENDING_WRITE";
76 const char *lock_flav_name(enum brl_flavour lock_flav)
78 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
81 /****************************************************************************
82 Utility function called to see if a file region is locked.
83 Called in the read/write codepath.
84 ****************************************************************************/
86 void init_strict_lock_struct(files_struct *fsp,
90 enum brl_type lock_type,
91 struct lock_struct *plock)
93 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
95 plock->context.smblctx = smblctx;
96 plock->context.tid = fsp->conn->cnum;
97 plock->context.pid = sconn_server_id(fsp->conn->sconn);
100 plock->fnum = fsp->fnum;
101 plock->lock_type = lock_type;
102 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
105 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
107 int strict_locking = lp_strict_locking(fsp->conn->params);
110 if (plock->size == 0) {
114 if (!lp_locking(fsp->conn->params) || !strict_locking) {
118 if (strict_locking == Auto) {
119 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
120 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
122 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
123 (plock->lock_type == READ_LOCK)) {
124 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
127 struct byte_range_lock *br_lck;
129 br_lck = brl_get_locks_readonly(fsp);
133 ret = brl_locktest(br_lck,
134 plock->context.smblctx,
142 struct byte_range_lock *br_lck;
144 br_lck = brl_get_locks_readonly(fsp);
148 ret = brl_locktest(br_lck,
149 plock->context.smblctx,
157 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
158 "len=%.0f %s for fnum %d file %s\n",
159 lock_flav_name(plock->lock_flav),
160 (double)plock->start, (double)plock->size,
161 ret ? "unlocked" : "locked",
162 plock->fnum, fsp_str_dbg(fsp)));
167 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
171 /****************************************************************************
172 Find out if a lock could be granted - return who is blocking us if we can't.
173 ****************************************************************************/
175 NTSTATUS query_lock(files_struct *fsp,
179 enum brl_type *plock_type,
180 enum brl_flavour lock_flav)
182 struct byte_range_lock *br_lck = NULL;
184 if (!fsp->can_lock) {
185 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
188 if (!lp_locking(fsp->conn->params)) {
192 br_lck = brl_get_locks_readonly(fsp);
194 return NT_STATUS_NO_MEMORY;
197 return brl_lockquery(br_lck,
199 sconn_server_id(fsp->conn->sconn),
206 static void increment_current_lock_count(files_struct *fsp,
207 enum brl_flavour lock_flav)
209 if (lock_flav == WINDOWS_LOCK &&
210 fsp->current_lock_count != NO_LOCKING_COUNT) {
211 /* blocking ie. pending, locks also count here,
212 * as this is an efficiency counter to avoid checking
213 * the lock db. on close. JRA. */
215 fsp->current_lock_count++;
217 /* Notice that this has had a POSIX lock request.
218 * We can't count locks after this so forget them.
220 fsp->current_lock_count = NO_LOCKING_COUNT;
224 static void decrement_current_lock_count(files_struct *fsp,
225 enum brl_flavour lock_flav)
227 if (lock_flav == WINDOWS_LOCK &&
228 fsp->current_lock_count != NO_LOCKING_COUNT) {
229 SMB_ASSERT(fsp->current_lock_count > 0);
230 fsp->current_lock_count--;
234 /****************************************************************************
235 Utility function called by locking requests.
236 ****************************************************************************/
238 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
243 enum brl_type lock_type,
244 enum brl_flavour lock_flav,
248 struct blocking_lock_record *blr)
250 struct byte_range_lock *br_lck = NULL;
252 /* silently return ok on print files as we don't do locking there */
253 if (fsp->print_file) {
254 *perr = NT_STATUS_OK;
258 if (!fsp->can_lock) {
259 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
263 if (!lp_locking(fsp->conn->params)) {
264 *perr = NT_STATUS_OK;
268 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
270 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
271 "blocking_lock=%s requested for fnum %d file %s\n",
272 lock_flav_name(lock_flav), lock_type_name(lock_type),
273 (double)offset, (double)count, blocking_lock ? "true" :
274 "false", fsp->fnum, fsp_str_dbg(fsp)));
276 br_lck = brl_get_locks(talloc_tos(), fsp);
278 *perr = NT_STATUS_NO_MEMORY;
282 *perr = brl_lock(msg_ctx,
285 sconn_server_id(fsp->conn->sconn),
294 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
296 increment_current_lock_count(fsp, lock_flav);
300 /****************************************************************************
301 Utility function called by unlocking requests.
302 ****************************************************************************/
304 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
309 enum brl_flavour lock_flav)
312 struct byte_range_lock *br_lck = NULL;
314 if (!fsp->can_lock) {
315 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
318 if (!lp_locking(fsp->conn->params)) {
322 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
323 (double)offset, (double)count, fsp->fnum,
326 br_lck = brl_get_locks(talloc_tos(), fsp);
328 return NT_STATUS_NO_MEMORY;
331 ok = brl_unlock(msg_ctx,
334 sconn_server_id(fsp->conn->sconn),
342 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
343 return NT_STATUS_RANGE_NOT_LOCKED;
346 decrement_current_lock_count(fsp, lock_flav);
350 /****************************************************************************
351 Cancel any pending blocked locks.
352 ****************************************************************************/
354 NTSTATUS do_lock_cancel(files_struct *fsp,
358 enum brl_flavour lock_flav,
359 struct blocking_lock_record *blr)
362 struct byte_range_lock *br_lck = NULL;
364 if (!fsp->can_lock) {
365 return fsp->is_directory ?
366 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
369 if (!lp_locking(fsp->conn->params)) {
370 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
373 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
374 (double)offset, (double)count, fsp->fnum,
377 br_lck = brl_get_locks(talloc_tos(), fsp);
379 return NT_STATUS_NO_MEMORY;
382 ok = brl_lock_cancel(br_lck,
384 sconn_server_id(fsp->conn->sconn),
393 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
394 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
397 decrement_current_lock_count(fsp, lock_flav);
401 /****************************************************************************
402 Remove any locks on this fd. Called from file_close().
403 ****************************************************************************/
405 void locking_close_file(struct messaging_context *msg_ctx,
407 enum file_close_type close_type)
409 struct byte_range_lock *br_lck;
411 if (!lp_locking(fsp->conn->params)) {
415 /* If we have not outstanding locks or pending
416 * locks then we don't need to look in the lock db.
419 if (fsp->current_lock_count == 0) {
423 br_lck = brl_get_locks(talloc_tos(),fsp);
426 cancel_pending_lock_requests_by_fid(fsp, br_lck, close_type);
427 brl_close_fnum(msg_ctx, br_lck);
432 /****************************************************************************
433 Initialise the locking functions.
434 ****************************************************************************/
436 static bool locking_init_internal(bool read_only)
443 lock_db = db_open(NULL, lock_path("locking.tdb"),
444 lp_open_files_db_hash_size(),
445 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
446 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
449 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
453 if (!posix_locking_init(read_only))
459 bool locking_init(void)
461 return locking_init_internal(false);
464 bool locking_init_readonly(void)
466 return locking_init_internal(true);
469 /*******************************************************************
470 Deinitialize the share_mode management.
471 ******************************************************************/
473 bool locking_end(void)
476 TALLOC_FREE(lock_db);
480 /*******************************************************************
481 Form a static locking key for a dev/inode pair.
482 ******************************************************************/
484 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
487 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
490 /*******************************************************************
491 Print out a share mode.
492 ********************************************************************/
494 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
496 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
497 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
498 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
499 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
501 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
502 procid_str_static(&e->pid),
503 e->share_access, e->private_options,
504 e->access_mask, (unsigned long long)e->op_mid,
505 e->op_type, e->share_file_id,
506 (unsigned int)e->uid, (unsigned int)e->flags,
507 file_id_string_tos(&e->id),
508 (unsigned int)e->name_hash);
511 /*******************************************************************
512 Print out a share mode table.
513 ********************************************************************/
515 static void print_share_mode_table(struct locking_data *data)
517 int num_share_modes = data->u.s.num_share_mode_entries;
518 struct share_mode_entry *shares =
519 (struct share_mode_entry *)(data + 1);
522 for (i = 0; i < num_share_modes; i++) {
523 struct share_mode_entry entry;
527 * We need to memcpy the entry here due to alignment
528 * restrictions that are not met when directly accessing
532 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
533 str = share_mode_str(talloc_tos(), i, &entry);
535 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
540 static int parse_delete_tokens_list(struct share_mode_lock *lck,
541 struct locking_data *pdata,
544 uint8_t *p = dbuf.dptr + sizeof(struct locking_data) +
545 (lck->num_share_modes *
546 sizeof(struct share_mode_entry));
547 uint8_t *end_ptr = dbuf.dptr + (dbuf.dsize - 2);
548 int delete_tokens_size = 0;
551 lck->delete_tokens = NULL;
553 for (i = 0; i < pdata->u.s.num_delete_token_entries; i++) {
555 struct delete_token_list *pdtl;
557 if (end_ptr - p < (sizeof(uint32_t) + sizeof(uint32_t) +
558 sizeof(uid_t) + sizeof(gid_t))) {
559 DEBUG(0,("parse_delete_tokens_list: "
560 "corrupt token list (%u)",
561 (unsigned int)(end_ptr - p)));
562 smb_panic("corrupt token list");
566 memcpy(&token_len, p, sizeof(token_len));
567 delete_tokens_size += token_len;
569 if (p + token_len > end_ptr || token_len < sizeof(token_len) +
570 sizeof(pdtl->name_hash) +
573 DEBUG(0,("parse_delete_tokens_list: "
574 "invalid token length (%u)\n",
575 (unsigned int)token_len ));
576 smb_panic("invalid token length");
580 p += sizeof(token_len);
582 pdtl = TALLOC_ZERO_P(lck, struct delete_token_list);
584 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
587 /* Copy out the name_hash. */
588 memcpy(&pdtl->name_hash, p, sizeof(pdtl->name_hash));
589 p += sizeof(pdtl->name_hash);
591 pdtl->delete_token = TALLOC_ZERO_P(pdtl, struct security_unix_token);
592 if (pdtl->delete_token == NULL) {
593 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
597 /* Copy out the uid and gid. */
598 memcpy(&pdtl->delete_token->uid, p, sizeof(uid_t));
600 memcpy(&pdtl->delete_token->gid, p, sizeof(gid_t));
603 token_len -= (sizeof(token_len) + sizeof(pdtl->name_hash) +
604 sizeof(uid_t) + sizeof(gid_t));
606 /* Any supplementary groups ? */
610 if (token_len % sizeof(gid_t) != 0) {
611 DEBUG(0,("parse_delete_tokens_list: "
612 "corrupt group list (%u)",
613 (unsigned int)(token_len % sizeof(gid_t)) ));
614 smb_panic("corrupt group list");
618 pdtl->delete_token->ngroups = token_len / sizeof(gid_t);
619 pdtl->delete_token->groups = TALLOC_ARRAY(pdtl->delete_token, gid_t,
620 pdtl->delete_token->ngroups);
621 if (pdtl->delete_token->groups == NULL) {
622 DEBUG(0,("parse_delete_tokens_list: talloc failed"));
626 for (j = 0; j < pdtl->delete_token->ngroups; j++) {
627 memcpy(&pdtl->delete_token->groups[j], p, sizeof(gid_t));
631 /* Add to the list. */
632 DLIST_ADD(lck->delete_tokens, pdtl);
635 return delete_tokens_size;
638 /*******************************************************************
639 Get all share mode entries for a dev/inode pair.
640 ********************************************************************/
642 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
644 struct locking_data data;
645 int delete_tokens_size;
648 if (dbuf.dsize < sizeof(struct locking_data)) {
649 smb_panic("parse_share_modes: buffer too short");
652 memcpy(&data, dbuf.dptr, sizeof(data));
654 lck->old_write_time = data.u.s.old_write_time;
655 lck->changed_write_time = data.u.s.changed_write_time;
656 lck->num_share_modes = data.u.s.num_share_mode_entries;
658 DEBUG(10, ("parse_share_modes: owrt: %s, "
659 "cwrt: %s, ntok: %u, num_share_modes: %d\n",
660 timestring(talloc_tos(),
661 convert_timespec_to_time_t(lck->old_write_time)),
662 timestring(talloc_tos(),
663 convert_timespec_to_time_t(
664 lck->changed_write_time)),
665 (unsigned int)data.u.s.num_delete_token_entries,
666 lck->num_share_modes));
668 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
669 DEBUG(0, ("invalid number of share modes: %d\n",
670 lck->num_share_modes));
671 smb_panic("parse_share_modes: invalid number of share modes");
674 lck->share_modes = NULL;
676 if (lck->num_share_modes != 0) {
678 if (dbuf.dsize < (sizeof(struct locking_data) +
679 (lck->num_share_modes *
680 sizeof(struct share_mode_entry)))) {
681 smb_panic("parse_share_modes: buffer too short");
684 lck->share_modes = (struct share_mode_entry *)
686 dbuf.dptr+sizeof(struct locking_data),
687 lck->num_share_modes *
688 sizeof(struct share_mode_entry));
690 if (lck->share_modes == NULL) {
691 smb_panic("parse_share_modes: talloc failed");
695 /* Get any delete tokens. */
696 delete_tokens_size = parse_delete_tokens_list(lck, &data, dbuf);
697 if (delete_tokens_size < 0) {
698 smb_panic("parse_share_modes: parse_delete_tokens_list failed");
701 /* Save off the associated service path and filename. */
702 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
703 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
706 lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
707 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
709 strlen(lck->servicepath) + 1;
711 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
712 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
714 strlen(lck->servicepath) + 1 +
715 strlen(lck->base_name) + 1;
718 * Ensure that each entry has a real process attached.
721 for (i = 0; i < lck->num_share_modes; i++) {
722 struct share_mode_entry *entry_p = &lck->share_modes[i];
724 if (DEBUGLEVEL >= 10) {
725 str = share_mode_str(NULL, i, entry_p);
727 DEBUG(10,("parse_share_modes: %s\n",
729 if (!serverid_exists(&entry_p->pid)) {
730 DEBUG(10,("parse_share_modes: deleted %s\n",
732 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
733 lck->modified = True;
741 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
746 struct locking_data *data;
748 ssize_t sp_len, bn_len, sn_len;
749 uint32_t delete_tokens_size = 0;
750 struct delete_token_list *pdtl = NULL;
751 uint32_t num_delete_token_entries = 0;
756 for (i=0; i<lck->num_share_modes; i++) {
757 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
762 if (num_valid == 0) {
766 sp_len = strlen(lck->servicepath);
767 bn_len = strlen(lck->base_name);
768 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
770 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
771 num_delete_token_entries++;
772 delete_tokens_size += (sizeof(uint32_t) +
776 pdtl->delete_token->ngroups*sizeof(gid_t));
779 result.dsize = sizeof(*data) +
780 lck->num_share_modes * sizeof(struct share_mode_entry) +
785 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
787 if (result.dptr == NULL) {
788 smb_panic("talloc failed");
791 data = (struct locking_data *)result.dptr;
793 data->u.s.num_share_mode_entries = lck->num_share_modes;
794 data->u.s.old_write_time = lck->old_write_time;
795 data->u.s.changed_write_time = lck->changed_write_time;
796 data->u.s.num_delete_token_entries = num_delete_token_entries;
798 DEBUG(10,("unparse_share_modes: owrt: %s cwrt: %s, ntok: %u, "
800 timestring(talloc_tos(),
801 convert_timespec_to_time_t(lck->old_write_time)),
802 timestring(talloc_tos(),
803 convert_timespec_to_time_t(
804 lck->changed_write_time)),
805 (unsigned int)data->u.s.num_delete_token_entries,
806 data->u.s.num_share_mode_entries));
808 memcpy(result.dptr + sizeof(*data), lck->share_modes,
809 sizeof(struct share_mode_entry)*lck->num_share_modes);
810 offset = sizeof(*data) +
811 sizeof(struct share_mode_entry)*lck->num_share_modes;
813 /* Store any delete on close tokens. */
814 for (pdtl = lck->delete_tokens; pdtl; pdtl = pdtl->next) {
815 struct security_unix_token *pdt = pdtl->delete_token;
816 uint32_t token_size = sizeof(uint32_t) +
820 (pdt->ngroups * sizeof(gid_t));
821 uint8_t *p = result.dptr + offset;
823 memcpy(p, &token_size, sizeof(uint32_t));
824 p += sizeof(uint32_t);
826 memcpy(p, &pdtl->name_hash, sizeof(uint32_t));
827 p += sizeof(uint32_t);
829 memcpy(p, &pdt->uid, sizeof(uid_t));
832 memcpy(p, &pdt->gid, sizeof(gid_t));
835 for (i = 0; i < pdt->ngroups; i++) {
836 memcpy(p, &pdt->groups[i], sizeof(gid_t));
839 offset += token_size;
842 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
843 result.dsize - offset - 1);
844 offset += sp_len + 1;
845 safe_strcpy((char *)result.dptr + offset, lck->base_name,
846 result.dsize - offset - 1);
847 offset += bn_len + 1;
848 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
849 result.dsize - offset - 1);
851 if (DEBUGLEVEL >= 10) {
852 print_share_mode_table(data);
858 static int share_mode_lock_destructor(struct share_mode_lock *lck)
863 if (!lck->modified) {
867 data = unparse_share_modes(lck);
869 if (data.dptr == NULL) {
871 /* There has been an entry before, delete it */
873 status = lck->record->delete_rec(lck->record);
874 if (!NT_STATUS_IS_OK(status)) {
877 DEBUG(0, ("delete_rec returned %s\n",
880 if (asprintf(&errmsg, "could not delete share "
882 nt_errstr(status)) == -1) {
883 smb_panic("could not delete share"
892 status = lck->record->store(lck->record, data, TDB_REPLACE);
893 if (!NT_STATUS_IS_OK(status)) {
896 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
898 if (asprintf(&errmsg, "could not store share mode entry: %s",
899 nt_errstr(status)) == -1) {
900 smb_panic("could not store share mode entry");
910 static bool fill_share_mode_lock(struct share_mode_lock *lck,
912 const char *servicepath,
913 const struct smb_filename *smb_fname,
914 TDB_DATA share_mode_data,
915 const struct timespec *old_write_time)
917 /* Ensure we set every field here as the destructor must be
918 valid even if parse_share_modes fails. */
920 lck->servicepath = NULL;
921 lck->base_name = NULL;
922 lck->stream_name = NULL;
924 lck->num_share_modes = 0;
925 lck->share_modes = NULL;
926 lck->delete_tokens = NULL;
927 ZERO_STRUCT(lck->old_write_time);
928 ZERO_STRUCT(lck->changed_write_time);
930 lck->modified = False;
932 lck->fresh = (share_mode_data.dptr == NULL);
936 if (smb_fname == NULL || servicepath == NULL
937 || old_write_time == NULL) {
941 has_stream = smb_fname->stream_name != NULL;
943 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
944 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
945 lck->servicepath = talloc_strdup(lck, servicepath);
946 if (lck->base_name == NULL ||
947 (has_stream && lck->stream_name == NULL) ||
948 lck->servicepath == NULL) {
949 DEBUG(0, ("talloc failed\n"));
952 lck->old_write_time = *old_write_time;
954 if (!parse_share_modes(share_mode_data, lck)) {
955 DEBUG(0, ("Could not parse share modes\n"));
963 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
964 const struct file_id id,
965 const char *servicepath,
966 const struct smb_filename *smb_fname,
967 const struct timespec *old_write_time)
969 struct share_mode_lock *lck;
971 TDB_DATA key = locking_key(&id, &tmp);
973 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
974 DEBUG(0, ("talloc failed\n"));
978 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
979 DEBUG(3, ("Could not lock share entry\n"));
984 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
985 lck->record->value, old_write_time)) {
986 DEBUG(3, ("fill_share_mode_lock failed\n"));
991 talloc_set_destructor(lck, share_mode_lock_destructor);
996 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
997 const struct file_id id)
999 struct share_mode_lock *lck;
1001 TDB_DATA key = locking_key(&id, &tmp);
1004 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
1005 DEBUG(0, ("talloc failed\n"));
1009 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
1010 DEBUG(3, ("Could not fetch share entry\n"));
1015 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
1016 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
1017 "around (file not open)\n"));
1025 /*******************************************************************
1026 Sets the service name and filename for rename.
1027 At this point we emit "file renamed" messages to all
1028 process id's that have this file open.
1029 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
1030 ********************************************************************/
1032 bool rename_share_filename(struct messaging_context *msg_ctx,
1033 struct share_mode_lock *lck,
1034 const char *servicepath,
1035 uint32_t orig_name_hash,
1036 uint32_t new_name_hash,
1037 const struct smb_filename *smb_fname_dst)
1045 bool strip_two_chars = false;
1046 bool has_stream = smb_fname_dst->stream_name != NULL;
1048 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
1049 servicepath, smb_fname_dst->base_name));
1052 * rename_internal_fsp() and rename_internals() add './' to
1053 * head of newname if newname does not contain a '/'.
1055 if (smb_fname_dst->base_name[0] &&
1056 smb_fname_dst->base_name[1] &&
1057 smb_fname_dst->base_name[0] == '.' &&
1058 smb_fname_dst->base_name[1] == '/') {
1059 strip_two_chars = true;
1062 lck->servicepath = talloc_strdup(lck, servicepath);
1063 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
1064 (strip_two_chars ? 2 : 0));
1065 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
1066 if (lck->base_name == NULL ||
1067 (has_stream && lck->stream_name == NULL) ||
1068 lck->servicepath == NULL) {
1069 DEBUG(0, ("rename_share_filename: talloc failed\n"));
1072 lck->modified = True;
1074 sp_len = strlen(lck->servicepath);
1075 bn_len = strlen(lck->base_name);
1076 sn_len = has_stream ? strlen(lck->stream_name) : 0;
1078 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
1081 /* Set up the name changed message. */
1082 frm = TALLOC_ARRAY(lck, char, msg_len);
1087 push_file_id_24(frm, &lck->id);
1089 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1091 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1092 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1093 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1096 /* Send the messages. */
1097 for (i=0; i<lck->num_share_modes; i++) {
1098 struct share_mode_entry *se = &lck->share_modes[i];
1099 if (!is_valid_share_mode_entry(se)) {
1103 /* If this is a hardlink to the inode
1104 with a different name, skip this. */
1105 if (se->name_hash != orig_name_hash) {
1109 se->name_hash = new_name_hash;
1111 /* But not to ourselves... */
1112 if (procid_is_me(&se->pid)) {
1116 DEBUG(10,("rename_share_filename: sending rename message to "
1117 "pid %s file_id %s sharepath %s base_name %s "
1119 procid_str_static(&se->pid),
1120 file_id_string_tos(&lck->id),
1121 lck->servicepath, lck->base_name,
1122 has_stream ? lck->stream_name : ""));
1124 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1125 (uint8 *)frm, msg_len);
1131 void get_file_infos(struct file_id id,
1133 bool *delete_on_close,
1134 struct timespec *write_time)
1136 struct share_mode_lock *lck;
1138 if (delete_on_close) {
1139 *delete_on_close = false;
1143 ZERO_STRUCTP(write_time);
1146 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1150 if (delete_on_close) {
1151 *delete_on_close = is_delete_on_close_set(lck, name_hash);
1157 wt = lck->changed_write_time;
1158 if (null_timespec(wt)) {
1159 wt = lck->old_write_time;
1168 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1172 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1173 /* cope with dead entries from the process not
1174 existing. These should not be considered valid,
1175 otherwise we end up doing zero timeout sharing
1180 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1181 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1182 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1184 SMB_ASSERT(num_props <= 1);
1185 return (num_props != 0);
1188 bool is_deferred_open_entry(const struct share_mode_entry *e)
1190 return (e->op_type == DEFERRED_OPEN_ENTRY);
1193 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1195 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1198 /*******************************************************************
1199 Fill a share mode entry.
1200 ********************************************************************/
1202 static void fill_share_mode_entry(struct share_mode_entry *e,
1204 uid_t uid, uint64_t mid, uint16 op_type)
1207 e->pid = sconn_server_id(fsp->conn->sconn);
1208 e->share_access = fsp->share_access;
1209 e->private_options = fsp->fh->private_options;
1210 e->access_mask = fsp->access_mask;
1212 e->op_type = op_type;
1213 e->time.tv_sec = fsp->open_time.tv_sec;
1214 e->time.tv_usec = fsp->open_time.tv_usec;
1215 e->id = fsp->file_id;
1216 e->share_file_id = fsp->fh->gen_id;
1217 e->uid = (uint32)uid;
1218 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1219 e->name_hash = fsp->name_hash;
1222 static void fill_deferred_open_entry(struct share_mode_entry *e,
1223 const struct timeval request_time,
1225 struct server_id pid,
1231 e->op_type = DEFERRED_OPEN_ENTRY;
1232 e->time.tv_sec = request_time.tv_sec;
1233 e->time.tv_usec = request_time.tv_usec;
1235 e->uid = (uint32)-1;
1239 static void add_share_mode_entry(struct share_mode_lock *lck,
1240 const struct share_mode_entry *entry)
1244 for (i=0; i<lck->num_share_modes; i++) {
1245 struct share_mode_entry *e = &lck->share_modes[i];
1246 if (is_unused_share_mode_entry(e)) {
1252 if (i == lck->num_share_modes) {
1253 /* No unused entry found */
1254 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1255 &lck->share_modes, &lck->num_share_modes);
1257 lck->modified = True;
1260 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1261 uid_t uid, uint64_t mid, uint16 op_type)
1263 struct share_mode_entry entry;
1264 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1265 add_share_mode_entry(lck, &entry);
1268 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1269 struct timeval request_time,
1270 struct server_id pid, struct file_id id)
1272 struct share_mode_entry entry;
1273 fill_deferred_open_entry(&entry, request_time, id, pid, mid);
1274 add_share_mode_entry(lck, &entry);
1277 /*******************************************************************
1278 Check if two share mode entries are identical, ignoring oplock
1279 and mid info and desired_access. (Removed paranoia test - it's
1280 not automatically a logic error if they are identical. JRA.)
1281 ********************************************************************/
1283 static bool share_modes_identical(struct share_mode_entry *e1,
1284 struct share_mode_entry *e2)
1286 /* We used to check for e1->share_access == e2->share_access here
1287 as well as the other fields but 2 different DOS or FCB opens
1288 sharing the same share mode entry may validly differ in
1289 fsp->share_access field. */
1291 return (procid_equal(&e1->pid, &e2->pid) &&
1292 file_id_equal(&e1->id, &e2->id) &&
1293 e1->share_file_id == e2->share_file_id );
1296 static bool deferred_open_identical(struct share_mode_entry *e1,
1297 struct share_mode_entry *e2)
1299 return (procid_equal(&e1->pid, &e2->pid) &&
1300 (e1->op_mid == e2->op_mid) &&
1301 file_id_equal(&e1->id, &e2->id));
1304 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1305 struct share_mode_entry *entry)
1309 for (i=0; i<lck->num_share_modes; i++) {
1310 struct share_mode_entry *e = &lck->share_modes[i];
1311 if (is_valid_share_mode_entry(entry) &&
1312 is_valid_share_mode_entry(e) &&
1313 share_modes_identical(e, entry)) {
1316 if (is_deferred_open_entry(entry) &&
1317 is_deferred_open_entry(e) &&
1318 deferred_open_identical(e, entry)) {
1325 /*******************************************************************
1326 Del the share mode of a file for this process. Return the number of
1328 ********************************************************************/
1330 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1332 struct share_mode_entry entry, *e;
1334 /* Don't care about the pid owner being correct here - just a search. */
1335 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1337 e = find_share_mode_entry(lck, &entry);
1342 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1343 lck->modified = True;
1347 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid,
1348 struct server_id pid)
1350 struct share_mode_entry entry, *e;
1352 fill_deferred_open_entry(&entry, timeval_zero(),
1355 e = find_share_mode_entry(lck, &entry);
1360 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1361 lck->modified = True;
1364 /*******************************************************************
1365 Remove an oplock mid and mode entry from a share mode.
1366 ********************************************************************/
1368 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1370 struct share_mode_entry entry, *e;
1372 /* Don't care about the pid owner being correct here - just a search. */
1373 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1375 e = find_share_mode_entry(lck, &entry);
1380 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1382 * Going from exclusive or batch,
1383 * we always go through FAKE_LEVEL_II
1386 e->op_type = FAKE_LEVEL_II_OPLOCK;
1388 e->op_type = NO_OPLOCK;
1390 lck->modified = True;
1394 /*******************************************************************
1395 Downgrade a oplock type from exclusive to level II.
1396 ********************************************************************/
1398 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1400 struct share_mode_entry entry, *e;
1402 /* Don't care about the pid owner being correct here - just a search. */
1403 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1405 e = find_share_mode_entry(lck, &entry);
1410 e->op_type = LEVEL_II_OPLOCK;
1411 lck->modified = True;
1415 /****************************************************************************
1416 Check if setting delete on close is allowed on this fsp.
1417 ****************************************************************************/
1419 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1422 * Only allow delete on close for writable files.
1425 if ((dosmode & aRONLY) &&
1426 !lp_delete_readonly(SNUM(fsp->conn))) {
1427 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1428 "flag set but file attribute is readonly.\n",
1430 return NT_STATUS_CANNOT_DELETE;
1434 * Only allow delete on close for writable shares.
1437 if (!CAN_WRITE(fsp->conn)) {
1438 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1439 "close flag set but write access denied on share.\n",
1441 return NT_STATUS_ACCESS_DENIED;
1445 * Only allow delete on close for files/directories opened with delete
1449 if (!(fsp->access_mask & DELETE_ACCESS)) {
1450 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1451 "close flag set but delete access denied.\n",
1453 return NT_STATUS_ACCESS_DENIED;
1456 /* Don't allow delete on close for non-empty directories. */
1457 if (fsp->is_directory) {
1458 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1459 return can_delete_directory(fsp->conn,
1460 fsp->fsp_name->base_name);
1463 return NT_STATUS_OK;
1466 /*************************************************************************
1467 Return a talloced copy of a struct security_unix_token. NULL on fail.
1468 (Should this be in locking.c.... ?).
1469 *************************************************************************/
1471 static struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct security_unix_token *tok)
1473 struct security_unix_token *cpy;
1475 cpy = TALLOC_P(ctx, struct security_unix_token);
1480 cpy->uid = tok->uid;
1481 cpy->gid = tok->gid;
1482 cpy->ngroups = tok->ngroups;
1484 /* Make this a talloc child of cpy. */
1485 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1489 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1494 /****************************************************************************
1495 Adds a delete on close token.
1496 ****************************************************************************/
1498 static bool add_delete_on_close_token(struct share_mode_lock *lck,
1500 const struct security_unix_token *tok)
1502 struct delete_token_list *dtl;
1504 dtl = TALLOC_ZERO_P(lck, struct delete_token_list);
1509 dtl->name_hash = name_hash;
1510 dtl->delete_token = copy_unix_token(lck, tok);
1511 if (dtl->delete_token == NULL) {
1515 DLIST_ADD(lck->delete_tokens, dtl);
1516 lck->modified = true;
1520 /****************************************************************************
1521 Sets the delete on close flag over all share modes on this file.
1522 Modify the share mode entry for all files open
1523 on this device and inode to tell other smbds we have
1524 changed the delete on close flag. This will be noticed
1525 in the close code, the last closer will delete the file
1527 This makes a copy of any struct security_unix_token into the
1528 lck entry. This function is used when the lock is already granted.
1529 ****************************************************************************/
1531 void set_delete_on_close_lck(files_struct *fsp,
1532 struct share_mode_lock *lck,
1533 bool delete_on_close,
1534 const struct security_unix_token *tok)
1536 struct delete_token_list *dtl;
1539 if (delete_on_close) {
1540 SMB_ASSERT(tok != NULL);
1542 SMB_ASSERT(tok == NULL);
1545 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1546 if (dtl->name_hash == fsp->name_hash) {
1547 lck->modified = true;
1548 if (delete_on_close == false) {
1549 /* Delete this entry. */
1550 DLIST_REMOVE(lck->delete_tokens, dtl);
1554 /* Replace this token with the
1556 TALLOC_FREE(dtl->delete_token);
1557 dtl->delete_token = copy_unix_token(dtl, tok);
1558 SMB_ASSERT(dtl->delete_token != NULL);
1562 if (!delete_on_close) {
1563 /* Nothing to delete - not found. */
1567 ret = add_delete_on_close_token(lck, fsp->name_hash, tok);
1571 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const struct security_unix_token *tok)
1573 struct share_mode_lock *lck;
1575 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1576 "fnum = %d, file %s\n",
1577 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1580 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1586 set_delete_on_close_lck(fsp, lck, delete_on_close,
1587 delete_on_close ? tok : NULL);
1589 if (fsp->is_directory) {
1590 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1591 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1592 fsp->fsp_name->base_name);
1597 fsp->delete_on_close = delete_on_close;
1602 const struct security_unix_token *get_delete_on_close_token(struct share_mode_lock *lck, uint32_t name_hash)
1604 struct delete_token_list *dtl;
1606 DEBUG(10,("get_delete_on_close_token: name_hash = 0x%x\n",
1607 (unsigned int)name_hash ));
1609 for (dtl = lck->delete_tokens; dtl; dtl = dtl->next) {
1610 DEBUG(10,("get_delete_on_close_token: dtl->name_hash = 0x%x\n",
1611 (unsigned int)dtl->name_hash ));
1612 if (dtl->name_hash == name_hash) {
1613 return dtl->delete_token;
1619 bool is_delete_on_close_set(struct share_mode_lock *lck, uint32_t name_hash)
1621 return (get_delete_on_close_token(lck, name_hash) != NULL);
1624 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1626 struct share_mode_lock *lck;
1628 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1629 timestring(talloc_tos(),
1630 convert_timespec_to_time_t(write_time)),
1631 file_id_string_tos(&fileid)));
1633 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1638 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1639 lck->modified = True;
1640 lck->changed_write_time = write_time;
1647 bool set_write_time(struct file_id fileid, struct timespec write_time)
1649 struct share_mode_lock *lck;
1651 DEBUG(5,("set_write_time: %s id=%s\n",
1652 timestring(talloc_tos(),
1653 convert_timespec_to_time_t(write_time)),
1654 file_id_string_tos(&fileid)));
1656 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1661 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1662 lck->modified = True;
1663 lck->old_write_time = write_time;
1671 struct forall_state {
1672 void (*fn)(const struct share_mode_entry *entry,
1673 const char *sharepath,
1675 void *private_data);
1679 static int traverse_fn(struct db_record *rec, void *_state)
1681 struct forall_state *state = (struct forall_state *)_state;
1682 struct locking_data *data;
1683 struct share_mode_entry *shares;
1684 const char *sharepath;
1686 const char *del_tokens;
1687 uint32_t total_del_token_size = 0;
1690 /* Ensure this is a locking_key record. */
1691 if (rec->key.dsize != sizeof(struct file_id))
1694 data = (struct locking_data *)rec->value.dptr;
1695 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1696 del_tokens = (const char *)rec->value.dptr + sizeof(*data) +
1697 data->u.s.num_share_mode_entries*sizeof(*shares);
1699 for (i = 0; i < data->u.s.num_delete_token_entries; i++) {
1700 uint32_t del_token_size;
1701 memcpy(&del_token_size, del_tokens, sizeof(uint32_t));
1702 total_del_token_size += del_token_size;
1703 del_tokens += del_token_size;
1706 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1707 data->u.s.num_share_mode_entries*sizeof(*shares) +
1708 total_del_token_size;
1709 fname = (const char *)rec->value.dptr + sizeof(*data) +
1710 data->u.s.num_share_mode_entries*sizeof(*shares) +
1711 total_del_token_size +
1712 strlen(sharepath) + 1;
1714 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1715 state->fn(&shares[i], sharepath, fname,
1716 state->private_data);
1721 /*******************************************************************
1722 Call the specified function on each entry under management by the
1724 ********************************************************************/
1726 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1727 const char *, void *),
1730 struct forall_state state;
1732 if (lock_db == NULL)
1736 state.private_data = private_data;
1738 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);