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 rewritten 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 "lib/util/server_id.h"
41 #include "locking/proto.h"
42 #include "smbd/globals.h"
43 #include "dbwrap/dbwrap.h"
44 #include "dbwrap/dbwrap_open.h"
45 #include "../libcli/security/security.h"
49 #include "../librpc/gen_ndr/ndr_open_files.h"
50 #include "librpc/gen_ndr/ndr_file_id.h"
51 #include "locking/leases_db.h"
54 #define DBGC_CLASS DBGC_LOCKING
56 #define NO_LOCKING_COUNT (-1)
58 /****************************************************************************
60 ****************************************************************************/
62 const char *lock_type_name(enum brl_type lock_type
)
69 case PENDING_READ_LOCK
:
70 return "PENDING_READ";
71 case PENDING_WRITE_LOCK
:
72 return "PENDING_WRITE";
78 const char *lock_flav_name(enum brl_flavour lock_flav
)
80 return (lock_flav
== WINDOWS_LOCK
) ? "WINDOWS_LOCK" : "POSIX_LOCK";
83 /****************************************************************************
84 Utility function called to see if a file region is locked.
85 Called in the read/write codepath.
86 ****************************************************************************/
88 void init_strict_lock_struct(files_struct
*fsp
,
92 enum brl_type lock_type
,
93 struct lock_struct
*plock
)
95 SMB_ASSERT(lock_type
== READ_LOCK
|| lock_type
== WRITE_LOCK
);
97 plock
->context
.smblctx
= smblctx
;
98 plock
->context
.tid
= fsp
->conn
->cnum
;
99 plock
->context
.pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
100 plock
->start
= start
;
102 plock
->fnum
= fsp
->fnum
;
103 plock
->lock_type
= lock_type
;
104 plock
->lock_flav
= lp_posix_cifsu_locktype(fsp
);
107 bool strict_lock_default(files_struct
*fsp
, struct lock_struct
*plock
)
109 struct byte_range_lock
*br_lck
;
110 int strict_locking
= lp_strict_locking(fsp
->conn
->params
);
113 if (plock
->size
== 0) {
117 if (!lp_locking(fsp
->conn
->params
) || !strict_locking
) {
121 if (strict_locking
== Auto
) {
122 uint32_t lease_type
= fsp_lease_type(fsp
);
124 if ((lease_type
& SMB2_LEASE_READ
) &&
125 (plock
->lock_type
== READ_LOCK
))
127 DBG_DEBUG("optimisation - read lease on file %s\n",
132 if ((lease_type
& SMB2_LEASE_WRITE
) &&
133 (plock
->lock_type
== WRITE_LOCK
))
135 DBG_DEBUG("optimisation - write lease on file %s\n",
141 br_lck
= brl_get_locks_readonly(fsp
);
145 ret
= brl_locktest(br_lck
, plock
);
149 * We got a lock conflict. Retry with rw locks to enable
150 * autocleanup. This is the slow path anyway.
152 br_lck
= brl_get_locks(talloc_tos(), fsp
);
153 ret
= brl_locktest(br_lck
, plock
);
157 DEBUG(10, ("strict_lock_default: flavour = %s brl start=%ju "
158 "len=%ju %s for fnum %ju file %s\n",
159 lock_flav_name(plock
->lock_flav
),
160 (uintmax_t)plock
->start
, (uintmax_t)plock
->size
,
161 ret
? "unlocked" : "locked",
162 (uintmax_t)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 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
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
,
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=%ju len=%ju "
270 "blocking_lock=%s requested for %s file %s\n",
271 lock_flav_name(lock_flav
), lock_type_name(lock_type
),
272 (uintmax_t)offset
, (uintmax_t)count
, blocking_lock
? "true" :
273 "false", fsp_fnum_dbg(fsp
), 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 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
292 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr
)));
294 increment_current_lock_count(fsp
, lock_flav
);
298 /****************************************************************************
299 Utility function called by unlocking requests.
300 ****************************************************************************/
302 NTSTATUS
do_unlock(struct messaging_context
*msg_ctx
,
307 enum brl_flavour lock_flav
)
310 struct byte_range_lock
*br_lck
= NULL
;
312 if (!fsp
->can_lock
) {
313 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
316 if (!lp_locking(fsp
->conn
->params
)) {
320 DEBUG(10, ("do_unlock: unlock start=%ju len=%ju requested for %s file "
321 "%s\n", (uintmax_t)offset
, (uintmax_t)count
,
322 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
324 br_lck
= brl_get_locks(talloc_tos(), fsp
);
326 return NT_STATUS_NO_MEMORY
;
329 ok
= brl_unlock(msg_ctx
,
332 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
340 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
341 return NT_STATUS_RANGE_NOT_LOCKED
;
344 decrement_current_lock_count(fsp
, lock_flav
);
348 /****************************************************************************
349 Cancel any pending blocked locks.
350 ****************************************************************************/
352 NTSTATUS
do_lock_cancel(files_struct
*fsp
,
356 enum brl_flavour lock_flav
)
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=%ju len=%ju requested for "
371 "%s file %s\n", (uintmax_t)offset
, (uintmax_t)count
,
372 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
374 br_lck
= brl_get_locks(talloc_tos(), fsp
);
376 return NT_STATUS_NO_MEMORY
;
379 ok
= brl_lock_cancel(br_lck
,
381 messaging_server_id(fsp
->conn
->sconn
->msg_ctx
),
389 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
390 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
393 decrement_current_lock_count(fsp
, lock_flav
);
397 /****************************************************************************
398 Remove any locks on this fd. Called from file_close().
399 ****************************************************************************/
401 void locking_close_file(struct messaging_context
*msg_ctx
,
403 enum file_close_type close_type
)
405 struct byte_range_lock
*br_lck
;
407 if (!lp_locking(fsp
->conn
->params
)) {
411 /* If we have no outstanding locks or pending
412 * locks then we don't need to look in the lock db.
415 if (fsp
->current_lock_count
== 0) {
419 br_lck
= brl_get_locks(talloc_tos(),fsp
);
422 cancel_pending_lock_requests_by_fid(fsp
, br_lck
, close_type
);
423 brl_close_fnum(msg_ctx
, br_lck
);
428 /*******************************************************************
429 Print out a share mode.
430 ********************************************************************/
432 char *share_mode_str(TALLOC_CTX
*ctx
, int num
, const struct share_mode_entry
*e
)
434 struct server_id_buf tmp
;
436 return talloc_asprintf(ctx
, "share_mode_entry[%d]: "
437 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
438 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %llu, "
439 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
441 server_id_str_buf(e
->pid
, &tmp
),
442 e
->share_access
, e
->private_options
,
443 e
->access_mask
, (unsigned long long)e
->op_mid
,
444 e
->op_type
, (unsigned long long)e
->share_file_id
,
445 (unsigned int)e
->uid
, (unsigned int)e
->flags
,
446 file_id_string_tos(&e
->id
),
447 (unsigned int)e
->name_hash
);
450 /*******************************************************************
451 Fetch a share mode where we know one MUST exist. This call reference
452 counts it internally to allow for nested lock fetches.
453 ********************************************************************/
455 struct share_mode_lock
*get_existing_share_mode_lock(TALLOC_CTX
*mem_ctx
,
456 const struct file_id id
)
458 return get_share_mode_lock(mem_ctx
, id
, NULL
, NULL
, NULL
);
461 /*******************************************************************
462 Sets the service name and filename for rename.
463 At this point we emit "file renamed" messages to all
464 process id's that have this file open.
465 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
466 ********************************************************************/
468 bool rename_share_filename(struct messaging_context
*msg_ctx
,
469 struct share_mode_lock
*lck
,
471 const char *servicepath
,
472 uint32_t orig_name_hash
,
473 uint32_t new_name_hash
,
474 const struct smb_filename
*smb_fname_dst
)
476 struct share_mode_data
*d
= lck
->data
;
483 bool strip_two_chars
= false;
484 bool has_stream
= smb_fname_dst
->stream_name
!= NULL
;
485 struct server_id self_pid
= messaging_server_id(msg_ctx
);
487 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
488 servicepath
, smb_fname_dst
->base_name
));
491 * rename_internal_fsp() and rename_internals() add './' to
492 * head of newname if newname does not contain a '/'.
494 if (smb_fname_dst
->base_name
[0] &&
495 smb_fname_dst
->base_name
[1] &&
496 smb_fname_dst
->base_name
[0] == '.' &&
497 smb_fname_dst
->base_name
[1] == '/') {
498 strip_two_chars
= true;
501 d
->servicepath
= talloc_strdup(d
, servicepath
);
502 d
->base_name
= talloc_strdup(d
, smb_fname_dst
->base_name
+
503 (strip_two_chars
? 2 : 0));
504 d
->stream_name
= talloc_strdup(d
, smb_fname_dst
->stream_name
);
505 if (d
->base_name
== NULL
||
506 (has_stream
&& d
->stream_name
== NULL
) ||
507 d
->servicepath
== NULL
) {
508 DEBUG(0, ("rename_share_filename: talloc failed\n"));
513 sp_len
= strlen(d
->servicepath
);
514 bn_len
= strlen(d
->base_name
);
515 sn_len
= has_stream
? strlen(d
->stream_name
) : 0;
517 msg_len
= MSG_FILE_RENAMED_MIN_SIZE
+ sp_len
+ 1 + bn_len
+ 1 +
520 /* Set up the name changed message. */
521 frm
= talloc_array(d
, char, msg_len
);
526 push_file_id_24(frm
, &id
);
528 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len
));
531 d
->servicepath
? d
->servicepath
: "",
533 strlcpy(&frm
[24 + sp_len
+ 1],
534 d
->base_name
? d
->base_name
: "",
536 strlcpy(&frm
[24 + sp_len
+ 1 + bn_len
+ 1],
537 d
->stream_name
? d
->stream_name
: "",
540 /* Send the messages. */
541 for (i
=0; i
<d
->num_share_modes
; i
++) {
542 struct share_mode_entry
*se
= &d
->share_modes
[i
];
543 struct server_id_buf tmp
;
545 if (!is_valid_share_mode_entry(se
)) {
549 /* If this is a hardlink to the inode
550 with a different name, skip this. */
551 if (se
->name_hash
!= orig_name_hash
) {
555 se
->name_hash
= new_name_hash
;
557 /* But not to ourselves... */
558 if (serverid_equal(&se
->pid
, &self_pid
)) {
562 if (share_mode_stale_pid(d
, i
)) {
566 DEBUG(10,("rename_share_filename: sending rename message to "
567 "pid %s file_id %s sharepath %s base_name %s "
569 server_id_str_buf(se
->pid
, &tmp
),
570 file_id_string_tos(&id
),
571 d
->servicepath
, d
->base_name
,
572 has_stream
? d
->stream_name
: ""));
574 messaging_send_buf(msg_ctx
, se
->pid
, MSG_SMB_FILE_RENAME
,
575 (uint8_t *)frm
, msg_len
);
578 for (i
=0; i
<d
->num_leases
; i
++) {
579 /* Update the filename in leases_db. */
581 struct share_mode_lease
*l
;
585 status
= leases_db_rename(&l
->client_guid
,
591 if (!NT_STATUS_IS_OK(status
)) {
592 /* Any error recovery possible here ? */
593 DEBUG(1,("Failed to rename lease key for "
594 "renamed file %s:%s. %s\n",
605 void get_file_infos(struct file_id id
,
607 bool *delete_on_close
,
608 struct timespec
*write_time
)
610 struct share_mode_lock
*lck
;
612 if (delete_on_close
) {
613 *delete_on_close
= false;
617 ZERO_STRUCTP(write_time
);
620 if (!(lck
= fetch_share_mode_unlocked(talloc_tos(), id
))) {
624 if (delete_on_close
) {
625 *delete_on_close
= is_delete_on_close_set(lck
, name_hash
);
629 *write_time
= get_share_mode_write_time(lck
);
635 bool is_valid_share_mode_entry(const struct share_mode_entry
*e
)
643 num_props
+= ((e
->op_type
== NO_OPLOCK
) ? 1 : 0);
644 num_props
+= (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
645 num_props
+= (LEVEL_II_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
646 num_props
+= (e
->op_type
== LEASE_OPLOCK
);
648 if ((num_props
> 1) && serverid_exists(&e
->pid
)) {
649 smb_panic("Invalid share mode entry");
651 return (num_props
!= 0);
655 * See if we need to remove a lease being referred to by a
656 * share mode that is being marked stale or deleted.
659 static void remove_share_mode_lease(struct share_mode_data
*d
,
660 struct share_mode_entry
*e
)
662 struct GUID client_guid
;
663 struct smb2_lease_key lease_key
;
668 op_type
= e
->op_type
;
669 e
->op_type
= NO_OPLOCK
;
673 if (op_type
!= LEASE_OPLOCK
) {
678 * This used to reference a lease. If there's no other one referencing
682 lease_idx
= e
->lease_idx
;
683 e
->lease_idx
= UINT32_MAX
;
685 for (i
=0; i
<d
->num_share_modes
; i
++) {
686 if (d
->share_modes
[i
].stale
) {
689 if (e
== &d
->share_modes
[i
]) {
693 if (d
->share_modes
[i
].lease_idx
== lease_idx
) {
697 if (i
< d
->num_share_modes
) {
705 &d
->leases
[lease_idx
].client_guid
,
706 sizeof(client_guid
));
707 lease_key
= d
->leases
[lease_idx
].lease_key
;
710 d
->leases
[lease_idx
] = d
->leases
[d
->num_leases
];
713 * We changed the lease array. Fix all references to it.
715 for (i
=0; i
<d
->num_share_modes
; i
++) {
716 if (d
->share_modes
[i
].lease_idx
== d
->num_leases
) {
717 d
->share_modes
[i
].lease_idx
= lease_idx
;
718 d
->share_modes
[i
].lease
= &d
->leases
[lease_idx
];
725 status
= leases_db_del(&client_guid
,
729 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__
,
735 * In case d->share_modes[i] conflicts with something or otherwise is
736 * being used, we need to make sure the corresponding process still
739 bool share_mode_stale_pid(struct share_mode_data
*d
, uint32_t idx
)
741 struct server_id_buf tmp
;
742 struct share_mode_entry
*e
;
744 if (idx
> d
->num_share_modes
) {
745 DEBUG(1, ("Asking for index %u, only %u around\n",
746 idx
, (unsigned)d
->num_share_modes
));
749 e
= &d
->share_modes
[idx
];
756 if (serverid_exists(&e
->pid
)) {
757 DEBUG(10, ("PID %s (index %u out of %u) still exists\n",
758 server_id_str_buf(e
->pid
, &tmp
), idx
,
759 (unsigned)d
->num_share_modes
));
762 DEBUG(10, ("PID %s (index %u out of %u) does not exist anymore\n",
763 server_id_str_buf(e
->pid
, &tmp
), idx
,
764 (unsigned)d
->num_share_modes
));
768 if (d
->num_delete_tokens
!= 0) {
769 uint32_t i
, num_stale
;
772 * We cannot have any delete tokens
773 * if there are no valid share modes.
778 for (i
=0; i
<d
->num_share_modes
; i
++) {
779 if (d
->share_modes
[i
].stale
) {
784 if (num_stale
== d
->num_share_modes
) {
786 * No non-stale share mode found
788 TALLOC_FREE(d
->delete_tokens
);
789 d
->num_delete_tokens
= 0;
793 remove_share_mode_lease(d
, e
);
799 void remove_stale_share_mode_entries(struct share_mode_data
*d
)
804 while (i
< d
->num_share_modes
) {
805 if (d
->share_modes
[i
].stale
) {
806 struct share_mode_entry
*m
= d
->share_modes
;
807 m
[i
] = m
[d
->num_share_modes
-1];
808 d
->num_share_modes
-= 1;
815 bool set_share_mode(struct share_mode_lock
*lck
, struct files_struct
*fsp
,
816 uid_t uid
, uint64_t mid
, uint16_t op_type
,
819 struct share_mode_data
*d
= lck
->data
;
820 struct share_mode_entry
*tmp
, *e
;
821 struct share_mode_lease
*lease
= NULL
;
823 if (lease_idx
== UINT32_MAX
) {
825 } else if (lease_idx
>= d
->num_leases
) {
828 lease
= &d
->leases
[lease_idx
];
831 tmp
= talloc_realloc(d
, d
->share_modes
, struct share_mode_entry
,
832 d
->num_share_modes
+1);
836 d
->share_modes
= tmp
;
837 e
= &d
->share_modes
[d
->num_share_modes
];
838 d
->num_share_modes
+= 1;
842 e
->pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
843 e
->share_access
= fsp
->share_access
;
844 e
->private_options
= fsp
->fh
->private_options
;
845 e
->access_mask
= fsp
->access_mask
;
847 e
->op_type
= op_type
;
848 e
->lease_idx
= lease_idx
;
850 e
->time
.tv_sec
= fsp
->open_time
.tv_sec
;
851 e
->time
.tv_usec
= fsp
->open_time
.tv_usec
;
852 e
->id
= fsp
->file_id
;
853 e
->share_file_id
= fsp
->fh
->gen_id
;
854 e
->uid
= (uint32_t)uid
;
855 e
->flags
= (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) ?
856 SHARE_MODE_FLAG_POSIX_OPEN
: 0;
857 e
->name_hash
= fsp
->name_hash
;
862 struct share_mode_entry
*find_share_mode_entry(
863 struct share_mode_lock
*lck
, files_struct
*fsp
)
865 struct share_mode_data
*d
= lck
->data
;
866 struct server_id pid
;
869 pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
871 for (i
=0; i
<d
->num_share_modes
; i
++) {
872 struct share_mode_entry
*e
= &d
->share_modes
[i
];
874 if (!is_valid_share_mode_entry(e
)) {
877 if (!serverid_equal(&pid
, &e
->pid
)) {
880 if (!file_id_equal(&fsp
->file_id
, &e
->id
)) {
883 if (fsp
->fh
->gen_id
!= e
->share_file_id
) {
891 /*******************************************************************
892 Del the share mode of a file for this process. Return the number of
894 ********************************************************************/
896 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
898 struct share_mode_entry
*e
;
900 e
= find_share_mode_entry(lck
, fsp
);
904 remove_share_mode_lease(lck
->data
, e
);
905 *e
= lck
->data
->share_modes
[lck
->data
->num_share_modes
-1];
906 lck
->data
->num_share_modes
-= 1;
907 lck
->data
->modified
= True
;
911 bool mark_share_mode_disconnected(struct share_mode_lock
*lck
,
912 struct files_struct
*fsp
)
914 struct share_mode_entry
*e
;
916 if (lck
->data
->num_share_modes
!= 1) {
920 if (fsp
->op
== NULL
) {
923 if (!fsp
->op
->global
->durable
) {
927 e
= find_share_mode_entry(lck
, fsp
);
932 DEBUG(10, ("Marking share mode entry disconnected for durable handle\n"));
934 server_id_set_disconnected(&e
->pid
);
937 * On reopen the caller needs to check that
938 * the client comes with the correct handle.
940 e
->share_file_id
= fsp
->op
->global
->open_persistent_id
;
942 lck
->data
->modified
= true;
946 /*******************************************************************
947 Remove an oplock mid and mode entry from a share mode.
948 ********************************************************************/
950 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
952 struct share_mode_data
*d
= lck
->data
;
953 struct share_mode_entry
*e
;
955 e
= find_share_mode_entry(lck
, fsp
);
960 remove_share_mode_lease(d
, e
);
965 /*******************************************************************
966 Downgrade a oplock type from exclusive to level II.
967 ********************************************************************/
969 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
971 struct share_mode_entry
*e
;
973 e
= find_share_mode_entry(lck
, fsp
);
978 e
->op_type
= LEVEL_II_OPLOCK
;
979 lck
->data
->modified
= True
;
983 NTSTATUS
downgrade_share_lease(struct smbd_server_connection
*sconn
,
984 struct share_mode_lock
*lck
,
985 const struct smb2_lease_key
*key
,
986 uint32_t new_lease_state
,
987 struct share_mode_lease
**_l
)
989 struct share_mode_data
*d
= lck
->data
;
990 struct share_mode_lease
*l
;
995 for (i
=0; i
<d
->num_leases
; i
++) {
996 if (smb2_lease_equal(&sconn
->client
->connections
->smb2
.client
.guid
,
998 &d
->leases
[i
].client_guid
,
999 &d
->leases
[i
].lease_key
)) {
1003 if (i
== d
->num_leases
) {
1004 DEBUG(10, ("lease not found\n"));
1005 return NT_STATUS_INVALID_PARAMETER
;
1011 DEBUG(1, ("Attempt to break from %d to %d - but we're not in breaking state\n",
1012 (int)l
->current_state
, (int)new_lease_state
));
1013 return NT_STATUS_UNSUCCESSFUL
;
1017 * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
1018 * must be a strict bitwise superset of new_lease_state
1020 if ((new_lease_state
& l
->breaking_to_requested
) != new_lease_state
) {
1021 DEBUG(1, ("Attempt to upgrade from %d to %d - expected %d\n",
1022 (int)l
->current_state
, (int)new_lease_state
,
1023 (int)l
->breaking_to_requested
));
1024 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
1027 if (l
->current_state
!= new_lease_state
) {
1028 l
->current_state
= new_lease_state
;
1032 if ((new_lease_state
& ~l
->breaking_to_required
) != 0) {
1033 DEBUG(5, ("lease state %d not fully broken from %d to %d\n",
1034 (int)new_lease_state
,
1035 (int)l
->current_state
,
1036 (int)l
->breaking_to_required
));
1037 l
->breaking_to_requested
= l
->breaking_to_required
;
1038 if (l
->current_state
& (~SMB2_LEASE_READ
)) {
1040 * Here we break in steps, as windows does
1041 * see the breaking3 and v2_breaking3 tests.
1043 l
->breaking_to_requested
|= SMB2_LEASE_READ
;
1047 return NT_STATUS_OPLOCK_BREAK_IN_PROGRESS
;
1050 DEBUG(10, ("breaking from %d to %d - expected %d\n",
1051 (int)l
->current_state
, (int)new_lease_state
,
1052 (int)l
->breaking_to_requested
));
1054 l
->breaking_to_requested
= 0;
1055 l
->breaking_to_required
= 0;
1056 l
->breaking
= false;
1060 return NT_STATUS_OK
;
1063 /****************************************************************************
1064 Adds a delete on close token.
1065 ****************************************************************************/
1067 static bool add_delete_on_close_token(struct share_mode_data
*d
,
1069 const struct security_token
*nt_tok
,
1070 const struct security_unix_token
*tok
)
1072 struct delete_token
*tmp
, *dtl
;
1074 tmp
= talloc_realloc(d
, d
->delete_tokens
, struct delete_token
,
1075 d
->num_delete_tokens
+1);
1079 d
->delete_tokens
= tmp
;
1080 dtl
= &d
->delete_tokens
[d
->num_delete_tokens
];
1082 dtl
->name_hash
= name_hash
;
1083 dtl
->delete_nt_token
= dup_nt_token(d
->delete_tokens
, nt_tok
);
1084 if (dtl
->delete_nt_token
== NULL
) {
1087 dtl
->delete_token
= copy_unix_token(d
->delete_tokens
, tok
);
1088 if (dtl
->delete_token
== NULL
) {
1091 d
->num_delete_tokens
+= 1;
1096 void reset_delete_on_close_lck(files_struct
*fsp
,
1097 struct share_mode_lock
*lck
)
1099 struct share_mode_data
*d
= lck
->data
;
1102 for (i
=0; i
<d
->num_delete_tokens
; i
++) {
1103 struct delete_token
*dt
= &d
->delete_tokens
[i
];
1105 if (dt
->name_hash
== fsp
->name_hash
) {
1108 /* Delete this entry. */
1109 TALLOC_FREE(dt
->delete_nt_token
);
1110 TALLOC_FREE(dt
->delete_token
);
1111 *dt
= d
->delete_tokens
[d
->num_delete_tokens
-1];
1112 d
->num_delete_tokens
-= 1;
1117 /****************************************************************************
1118 Sets the delete on close flag over all share modes on this file.
1119 Modify the share mode entry for all files open
1120 on this device and inode to tell other smbds we have
1121 changed the delete on close flag. This will be noticed
1122 in the close code, the last closer will delete the file
1124 This makes a copy of any struct security_unix_token into the
1125 lck entry. This function is used when the lock is already granted.
1126 ****************************************************************************/
1128 void set_delete_on_close_lck(files_struct
*fsp
,
1129 struct share_mode_lock
*lck
,
1130 const struct security_token
*nt_tok
,
1131 const struct security_unix_token
*tok
)
1133 struct messaging_context
*msg_ctx
= fsp
->conn
->sconn
->msg_ctx
;
1134 struct share_mode_data
*d
= lck
->data
;
1137 DATA_BLOB fid_blob
= {};
1138 enum ndr_err_code ndr_err
;
1140 SMB_ASSERT(nt_tok
!= NULL
);
1141 SMB_ASSERT(tok
!= NULL
);
1143 for (i
=0; i
<d
->num_delete_tokens
; i
++) {
1144 struct delete_token
*dt
= &d
->delete_tokens
[i
];
1145 if (dt
->name_hash
== fsp
->name_hash
) {
1148 /* Replace this token with the given tok. */
1149 TALLOC_FREE(dt
->delete_nt_token
);
1150 dt
->delete_nt_token
= dup_nt_token(dt
, nt_tok
);
1151 SMB_ASSERT(dt
->delete_nt_token
!= NULL
);
1152 TALLOC_FREE(dt
->delete_token
);
1153 dt
->delete_token
= copy_unix_token(dt
, tok
);
1154 SMB_ASSERT(dt
->delete_token
!= NULL
);
1160 ret
= add_delete_on_close_token(lck
->data
, fsp
->name_hash
, nt_tok
, tok
);
1163 ndr_err
= ndr_push_struct_blob(&fid_blob
, talloc_tos(), &fsp
->file_id
,
1164 (ndr_push_flags_fn_t
)ndr_push_file_id
);
1165 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1166 DEBUG(10, ("ndr_push_file_id failed: %s\n",
1167 ndr_errstr(ndr_err
)));
1170 for (i
=0; i
<d
->num_share_modes
; i
++) {
1171 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1174 status
= messaging_send(
1175 msg_ctx
, e
->pid
, MSG_SMB_NOTIFY_CANCEL_DELETED
,
1178 if (!NT_STATUS_IS_OK(status
)) {
1179 struct server_id_buf tmp
;
1180 DEBUG(10, ("%s: messaging_send to %s returned %s\n",
1181 __func__
, server_id_str_buf(e
->pid
, &tmp
),
1182 nt_errstr(status
)));
1186 TALLOC_FREE(fid_blob
.data
);
1189 bool set_delete_on_close(files_struct
*fsp
, bool delete_on_close
,
1190 const struct security_token
*nt_tok
,
1191 const struct security_unix_token
*tok
)
1193 struct share_mode_lock
*lck
;
1195 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1197 delete_on_close
? "Adding" : "Removing", fsp_fnum_dbg(fsp
),
1200 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
1205 if (delete_on_close
) {
1206 set_delete_on_close_lck(fsp
, lck
, nt_tok
, tok
);
1208 reset_delete_on_close_lck(fsp
, lck
);
1211 if (fsp
->is_directory
) {
1212 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp
->fsp_name
));
1213 send_stat_cache_delete_message(fsp
->conn
->sconn
->msg_ctx
,
1214 fsp
->fsp_name
->base_name
);
1219 fsp
->delete_on_close
= delete_on_close
;
1224 static struct delete_token
*find_delete_on_close_token(
1225 struct share_mode_data
*d
, uint32_t name_hash
)
1229 DEBUG(10, ("find_delete_on_close_token: name_hash = 0x%x\n",
1230 (unsigned int)name_hash
));
1232 for (i
=0; i
<d
->num_delete_tokens
; i
++) {
1233 struct delete_token
*dt
= &d
->delete_tokens
[i
];
1235 DEBUG(10, ("find__delete_on_close_token: dt->name_hash = 0x%x\n",
1236 (unsigned int)dt
->name_hash
));
1237 if (dt
->name_hash
== name_hash
) {
1244 /****************************************************************************
1245 Return the NT token and UNIX token if there's a match. Return true if
1246 found, false if not.
1247 ****************************************************************************/
1249 bool get_delete_on_close_token(struct share_mode_lock
*lck
,
1251 const struct security_token
**pp_nt_tok
,
1252 const struct security_unix_token
**pp_tok
)
1254 struct delete_token
*dt
;
1256 dt
= find_delete_on_close_token(lck
->data
, name_hash
);
1260 *pp_nt_tok
= dt
->delete_nt_token
;
1261 *pp_tok
= dt
->delete_token
;
1265 bool is_delete_on_close_set(struct share_mode_lock
*lck
, uint32_t name_hash
)
1267 return find_delete_on_close_token(lck
->data
, name_hash
) != NULL
;
1270 bool set_sticky_write_time(struct file_id fileid
, struct timespec write_time
)
1272 struct share_mode_lock
*lck
;
1274 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1275 timestring(talloc_tos(),
1276 convert_timespec_to_time_t(write_time
)),
1277 file_id_string_tos(&fileid
)));
1279 lck
= get_existing_share_mode_lock(talloc_tos(), fileid
);
1284 if (timespec_compare(&lck
->data
->changed_write_time
, &write_time
) != 0) {
1285 lck
->data
->modified
= True
;
1286 lck
->data
->changed_write_time
= write_time
;
1293 bool set_write_time(struct file_id fileid
, struct timespec write_time
)
1295 struct share_mode_lock
*lck
;
1297 DEBUG(5,("set_write_time: %s id=%s\n",
1298 timestring(talloc_tos(),
1299 convert_timespec_to_time_t(write_time
)),
1300 file_id_string_tos(&fileid
)));
1302 lck
= get_existing_share_mode_lock(talloc_tos(), fileid
);
1307 if (timespec_compare(&lck
->data
->old_write_time
, &write_time
) != 0) {
1308 lck
->data
->modified
= True
;
1309 lck
->data
->old_write_time
= write_time
;
1316 struct timespec
get_share_mode_write_time(struct share_mode_lock
*lck
)
1318 struct share_mode_data
*d
= lck
->data
;
1320 if (!null_timespec(d
->changed_write_time
)) {
1321 return d
->changed_write_time
;
1323 return d
->old_write_time
;