smbd: Remove unused "msg_ctx" from do_unlock()
[Samba.git] / source3 / locking / locking.c
bloba31c6f7d3af5fb746241843d2ce6e3ea6e495ac2
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
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/>.
21 Revision History:
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
30 support.
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.
38 #include "includes.h"
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"
46 #include "serverid.h"
47 #include "messages.h"
48 #include "util_tdb.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"
53 #undef DBGC_CLASS
54 #define DBGC_CLASS DBGC_LOCKING
56 #define NO_LOCKING_COUNT (-1)
58 /****************************************************************************
59 Debugging aids :-).
60 ****************************************************************************/
62 const char *lock_type_name(enum brl_type lock_type)
64 switch (lock_type) {
65 case READ_LOCK:
66 return "READ";
67 case WRITE_LOCK:
68 return "WRITE";
69 default:
70 return "other";
74 const char *lock_flav_name(enum brl_flavour lock_flav)
76 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
79 /****************************************************************************
80 Utility function called to see if a file region is locked.
81 Called in the read/write codepath.
82 ****************************************************************************/
84 void init_strict_lock_struct(files_struct *fsp,
85 uint64_t smblctx,
86 br_off start,
87 br_off size,
88 enum brl_type lock_type,
89 struct lock_struct *plock)
91 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
93 plock->context.smblctx = smblctx;
94 plock->context.tid = fsp->conn->cnum;
95 plock->context.pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
96 plock->start = start;
97 plock->size = size;
98 plock->fnum = fsp->fnum;
99 plock->lock_type = lock_type;
100 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
103 bool strict_lock_check_default(files_struct *fsp, struct lock_struct *plock)
105 struct byte_range_lock *br_lck;
106 int strict_locking = lp_strict_locking(fsp->conn->params);
107 bool ret = False;
109 if (plock->size == 0) {
110 return True;
113 if (!lp_locking(fsp->conn->params) || !strict_locking) {
114 return True;
117 if (strict_locking == Auto) {
118 uint32_t lease_type = fsp_lease_type(fsp);
120 if ((lease_type & SMB2_LEASE_READ) &&
121 (plock->lock_type == READ_LOCK))
123 DBG_DEBUG("optimisation - read lease on file %s\n",
124 fsp_str_dbg(fsp));
125 return true;
128 if ((lease_type & SMB2_LEASE_WRITE) &&
129 (plock->lock_type == WRITE_LOCK))
131 DBG_DEBUG("optimisation - write lease on file %s\n",
132 fsp_str_dbg(fsp));
133 return true;
137 br_lck = brl_get_locks_readonly(fsp);
138 if (!br_lck) {
139 return true;
141 ret = brl_locktest(br_lck, plock);
143 if (!ret) {
145 * We got a lock conflict. Retry with rw locks to enable
146 * autocleanup. This is the slow path anyway.
148 br_lck = brl_get_locks(talloc_tos(), fsp);
149 if (br_lck == NULL) {
150 return true;
152 ret = brl_locktest(br_lck, plock);
153 TALLOC_FREE(br_lck);
156 DEBUG(10, ("strict_lock_default: flavour = %s brl start=%ju "
157 "len=%ju %s for fnum %ju file %s\n",
158 lock_flav_name(plock->lock_flav),
159 (uintmax_t)plock->start, (uintmax_t)plock->size,
160 ret ? "unlocked" : "locked",
161 (uintmax_t)plock->fnum, fsp_str_dbg(fsp)));
163 return ret;
166 /****************************************************************************
167 Find out if a lock could be granted - return who is blocking us if we can't.
168 ****************************************************************************/
170 NTSTATUS query_lock(files_struct *fsp,
171 uint64_t *psmblctx,
172 uint64_t *pcount,
173 uint64_t *poffset,
174 enum brl_type *plock_type,
175 enum brl_flavour lock_flav)
177 struct byte_range_lock *br_lck = NULL;
179 if (!fsp->can_lock) {
180 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
183 if (!lp_locking(fsp->conn->params)) {
184 return NT_STATUS_OK;
187 br_lck = brl_get_locks_readonly(fsp);
188 if (!br_lck) {
189 return NT_STATUS_NO_MEMORY;
192 return brl_lockquery(br_lck,
193 psmblctx,
194 messaging_server_id(fsp->conn->sconn->msg_ctx),
195 poffset,
196 pcount,
197 plock_type,
198 lock_flav);
201 static void increment_current_lock_count(files_struct *fsp,
202 enum brl_flavour lock_flav)
204 if (lock_flav == WINDOWS_LOCK &&
205 fsp->current_lock_count != NO_LOCKING_COUNT) {
206 /* blocking ie. pending, locks also count here,
207 * as this is an efficiency counter to avoid checking
208 * the lock db. on close. JRA. */
210 fsp->current_lock_count++;
211 } else {
212 /* Notice that this has had a POSIX lock request.
213 * We can't count locks after this so forget them.
215 fsp->current_lock_count = NO_LOCKING_COUNT;
219 static void decrement_current_lock_count(files_struct *fsp,
220 enum brl_flavour lock_flav)
222 if (lock_flav == WINDOWS_LOCK &&
223 fsp->current_lock_count != NO_LOCKING_COUNT) {
224 SMB_ASSERT(fsp->current_lock_count > 0);
225 fsp->current_lock_count--;
229 /****************************************************************************
230 Utility function called by locking requests.
231 ****************************************************************************/
233 NTSTATUS do_lock(files_struct *fsp,
234 uint64_t smblctx,
235 uint64_t count,
236 uint64_t offset,
237 enum brl_type lock_type,
238 enum brl_flavour lock_flav,
239 struct server_id *pblocker_pid,
240 uint64_t *psmblctx)
242 struct byte_range_lock *br_lck = NULL;
243 struct server_id blocker_pid = { 0 };
244 uint64_t blocker_smblctx = 0;
245 NTSTATUS status;
247 /* silently return ok on print files as we don't do locking there */
248 if (fsp->print_file) {
249 return NT_STATUS_OK;
252 if (!fsp->can_lock) {
253 if (fsp->is_directory) {
254 return NT_STATUS_INVALID_DEVICE_REQUEST;
256 return NT_STATUS_INVALID_HANDLE;
259 if (!lp_locking(fsp->conn->params)) {
260 return NT_STATUS_OK;
263 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
265 DBG_DEBUG("lock flavour %s lock type %s start=%"PRIu64" len=%"PRIu64" "
266 "requested for %s file %s\n",
267 lock_flav_name(lock_flav),
268 lock_type_name(lock_type),
269 offset,
270 count,
271 fsp_fnum_dbg(fsp),
272 fsp_str_dbg(fsp));
274 br_lck = brl_get_locks(talloc_tos(), fsp);
275 if (!br_lck) {
276 return NT_STATUS_NO_MEMORY;
279 status = brl_lock(
280 br_lck,
281 smblctx,
282 messaging_server_id(fsp->conn->sconn->msg_ctx),
283 offset,
284 count,
285 lock_type,
286 lock_flav,
287 &blocker_pid,
288 &blocker_smblctx);
290 TALLOC_FREE(br_lck);
292 if (psmblctx != NULL) {
293 *psmblctx = blocker_smblctx;
295 if (pblocker_pid != NULL) {
296 *pblocker_pid = blocker_pid;
299 DBG_DEBUG("returning status=%s\n", nt_errstr(status));
301 increment_current_lock_count(fsp, lock_flav);
303 return status;
306 /****************************************************************************
307 Utility function called by unlocking requests.
308 ****************************************************************************/
310 NTSTATUS do_unlock(files_struct *fsp,
311 uint64_t smblctx,
312 uint64_t count,
313 uint64_t offset,
314 enum brl_flavour lock_flav)
316 bool ok = False;
317 struct byte_range_lock *br_lck = NULL;
319 if (!fsp->can_lock) {
320 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
323 if (!lp_locking(fsp->conn->params)) {
324 return NT_STATUS_OK;
327 DBG_DEBUG("unlock start=%"PRIu64" len=%"PRIu64" requested for %s file "
328 "%s\n",
329 offset,
330 count,
331 fsp_fnum_dbg(fsp),
332 fsp_str_dbg(fsp));
334 br_lck = brl_get_locks(talloc_tos(), fsp);
335 if (!br_lck) {
336 return NT_STATUS_NO_MEMORY;
339 ok = brl_unlock(br_lck,
340 smblctx,
341 messaging_server_id(fsp->conn->sconn->msg_ctx),
342 offset,
343 count,
344 lock_flav);
346 TALLOC_FREE(br_lck);
348 if (!ok) {
349 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
350 return NT_STATUS_RANGE_NOT_LOCKED;
353 decrement_current_lock_count(fsp, lock_flav);
354 return NT_STATUS_OK;
357 /****************************************************************************
358 Remove any locks on this fd. Called from file_close().
359 ****************************************************************************/
361 void locking_close_file(struct messaging_context *msg_ctx,
362 files_struct *fsp,
363 enum file_close_type close_type)
365 struct byte_range_lock *br_lck;
367 if (!lp_locking(fsp->conn->params)) {
368 return;
371 /* If we have no outstanding locks or pending
372 * locks then we don't need to look in the lock db.
375 if (fsp->current_lock_count == 0) {
376 return;
379 br_lck = brl_get_locks(talloc_tos(),fsp);
381 if (br_lck) {
383 * Unlocks must trigger dbwrap_watch watchers,
384 * normally in smbd_do_unlocking. Here it's done
385 * implictly, we're closing the file and thus remove a
386 * share mode. This will wake the waiters.
388 brl_close_fnum(br_lck);
389 TALLOC_FREE(br_lck);
393 /*******************************************************************
394 Print out a share mode.
395 ********************************************************************/
397 char *share_mode_str(TALLOC_CTX *ctx, int num,
398 const struct file_id *id,
399 const struct share_mode_entry *e)
401 struct server_id_buf tmp;
403 return talloc_asprintf(ctx, "share_mode_entry[%d]: "
404 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
405 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %llu, "
406 "uid = %u, flags = %u, file_id %s, name_hash = 0x%x",
407 num,
408 server_id_str_buf(e->pid, &tmp),
409 e->share_access, e->private_options,
410 e->access_mask, (unsigned long long)e->op_mid,
411 e->op_type, (unsigned long long)e->share_file_id,
412 (unsigned int)e->uid, (unsigned int)e->flags,
413 file_id_string_tos(id),
414 (unsigned int)e->name_hash);
417 /*******************************************************************
418 Fetch a share mode where we know one MUST exist. This call reference
419 counts it internally to allow for nested lock fetches.
420 ********************************************************************/
422 struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
423 const struct file_id id)
425 return get_share_mode_lock(mem_ctx, id, NULL, NULL, NULL);
428 static bool rename_lease_fn(struct share_mode_lock *lck,
429 struct share_mode_entry *e,
430 void *private_data)
432 struct share_mode_data *d = lck->data;
433 NTSTATUS status;
435 status = leases_db_rename(&e->client_guid,
436 &e->lease_key,
437 &d->id,
438 d->servicepath,
439 d->base_name,
440 d->stream_name);
442 if (!NT_STATUS_IS_OK(status)) {
443 /* Any error recovery possible here ? */
444 DBG_WARNING("Failed to rename lease key for "
445 "renamed file %s:%s. %s\n",
446 d->base_name,
447 d->stream_name,
448 nt_errstr(status));
451 return false;
454 /*******************************************************************
455 Sets the service name and filename for rename.
456 At this point we emit "file renamed" messages to all
457 process id's that have this file open.
458 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
459 ********************************************************************/
461 bool rename_share_filename(struct messaging_context *msg_ctx,
462 struct share_mode_lock *lck,
463 struct file_id id,
464 const char *servicepath,
465 uint32_t orig_name_hash,
466 uint32_t new_name_hash,
467 const struct smb_filename *smb_fname_dst)
469 struct share_mode_data *d = lck->data;
470 struct file_rename_message msg = {
471 .id = id,
472 .servicepath = servicepath,
473 .base_name = smb_fname_dst->base_name,
474 .stream_name = smb_fname_dst->stream_name,
476 uint32_t i;
477 struct server_id self_pid = messaging_server_id(msg_ctx);
478 bool ok;
480 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
481 servicepath, smb_fname_dst->base_name));
484 * rename_internal_fsp() and rename_internals() add './' to
485 * head of newname if newname does not contain a '/'.
488 if (strncmp(msg.base_name, "./", 2) == 0) {
489 msg.base_name += 2;
492 d->servicepath = talloc_strdup(d, msg.servicepath);
493 d->base_name = talloc_strdup(d, msg.base_name);
494 d->stream_name = talloc_strdup(d, msg.stream_name);
495 if ((d->servicepath == NULL) ||
496 (d->base_name == NULL) ||
497 ((msg.stream_name != NULL) && (d->stream_name == NULL))) {
498 DBG_WARNING("talloc failed\n");
499 return false;
501 d->modified = True;
503 /* Send the messages. */
504 for (i=0; i<d->num_share_modes; i++) {
505 struct share_mode_entry *se = &d->share_modes[i];
506 DATA_BLOB blob;
507 enum ndr_err_code ndr_err;
509 if (!is_valid_share_mode_entry(se)) {
510 continue;
513 /* If this is a hardlink to the inode
514 with a different name, skip this. */
515 if (se->name_hash != orig_name_hash) {
516 continue;
519 se->name_hash = new_name_hash;
521 /* But not to ourselves... */
522 if (serverid_equal(&se->pid, &self_pid)) {
523 continue;
526 if (share_mode_stale_pid(d, i)) {
527 continue;
530 msg.share_file_id = se->share_file_id;
532 ndr_err = ndr_push_struct_blob(
533 &blob,
534 talloc_tos(),
535 &msg,
536 (ndr_push_flags_fn_t)ndr_push_file_rename_message);
537 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
538 DBG_DEBUG("ndr_push_file_rename_message failed: %s\n",
539 ndr_errstr(ndr_err));
540 return false;
542 if (DEBUGLEVEL >= 10) {
543 struct server_id_buf tmp;
544 DBG_DEBUG("sending rename message to %s\n",
545 server_id_str_buf(se->pid, &tmp));
546 NDR_PRINT_DEBUG(file_rename_message, &msg);
549 messaging_send(msg_ctx, se->pid, MSG_SMB_FILE_RENAME, &blob);
551 TALLOC_FREE(blob.data);
554 ok = share_mode_forall_leases(lck, rename_lease_fn, NULL);
555 if (!ok) {
557 * Ignore error here. Not sure what to do..
559 DBG_WARNING("share_mode_forall_leases failed\n");
562 return True;
565 void get_file_infos(struct file_id id,
566 uint32_t name_hash,
567 bool *delete_on_close,
568 struct timespec *write_time)
570 struct share_mode_lock *lck;
572 if (delete_on_close) {
573 *delete_on_close = false;
576 if (write_time) {
577 ZERO_STRUCTP(write_time);
580 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
581 return;
584 if (delete_on_close) {
585 *delete_on_close = is_delete_on_close_set(lck, name_hash);
588 if (write_time) {
589 *write_time = get_share_mode_write_time(lck);
592 TALLOC_FREE(lck);
595 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
597 int num_props = 0;
599 if (e->stale) {
600 return false;
603 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
604 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
605 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
606 num_props += (e->op_type == LEASE_OPLOCK);
608 if ((num_props > 1) && serverid_exists(&e->pid)) {
609 smb_panic("Invalid share mode entry");
611 return (num_props != 0);
615 * See if we need to remove a lease being referred to by a
616 * share mode that is being marked stale or deleted.
619 static void remove_share_mode_lease(struct share_mode_data *d,
620 struct share_mode_entry *e)
622 uint16_t op_type;
623 uint32_t i;
625 op_type = e->op_type;
626 e->op_type = NO_OPLOCK;
628 d->modified = true;
630 if (op_type != LEASE_OPLOCK) {
631 return;
635 * This used to reference a lease. If there's no other one referencing
636 * it, remove it.
639 for (i=0; i<d->num_share_modes; i++) {
640 struct share_mode_entry *e2 = &d->share_modes[i];
642 if (e2->stale) {
643 continue;
645 if (e == e2) {
646 /* Not ourselves. */
647 continue;
649 if (smb2_lease_equal(&e->client_guid,
650 &e->lease_key,
651 &e2->client_guid,
652 &e2->lease_key)) {
653 break;
656 if (i < d->num_share_modes) {
658 * Found another one
660 return;
664 NTSTATUS status;
666 status = leases_db_del(&e->client_guid,
667 &e->lease_key,
668 &d->id);
670 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__,
671 nt_errstr(status)));
676 * In case d->share_modes[i] conflicts with something or otherwise is
677 * being used, we need to make sure the corresponding process still
678 * exists.
680 bool share_mode_stale_pid(struct share_mode_data *d, uint32_t idx)
682 struct server_id_buf tmp;
683 struct share_mode_entry *e;
685 if (idx > d->num_share_modes) {
686 DBG_WARNING("Asking for index %"PRIu32", "
687 "only %"PRIu32" around\n",
688 idx,
689 d->num_share_modes);
690 return false;
692 e = &d->share_modes[idx];
693 if (e->stale) {
695 * Checked before
697 return true;
699 if (serverid_exists(&e->pid)) {
700 DBG_DEBUG("PID %s (index %"PRIu32" out of %"PRIu32") "
701 "still exists\n",
702 server_id_str_buf(e->pid, &tmp),
703 idx,
704 d->num_share_modes);
705 return false;
707 DBG_DEBUG("PID %s (index %"PRIu32" out of %"PRIu32") "
708 "does not exist anymore\n",
709 server_id_str_buf(e->pid, &tmp),
710 idx,
711 d->num_share_modes);
713 e->stale = true;
715 if (d->num_delete_tokens != 0) {
716 uint32_t i;
718 for (i=0; i<d->num_share_modes; i++) {
719 bool valid = !d->share_modes[i].stale;
720 if (valid) {
721 break;
725 if (i == d->num_share_modes) {
727 * No valid (non-stale) share mode found, all
728 * who might have set the delete token are
729 * gone.
731 TALLOC_FREE(d->delete_tokens);
732 d->num_delete_tokens = 0;
736 remove_share_mode_lease(d, e);
738 d->modified = true;
739 return true;
742 void remove_stale_share_mode_entries(struct share_mode_data *d)
744 uint32_t i;
746 i = 0;
747 while (i < d->num_share_modes) {
748 if (d->share_modes[i].stale) {
749 struct share_mode_entry *m = d->share_modes;
750 m[i] = m[d->num_share_modes-1];
751 d->num_share_modes -= 1;
752 continue;
754 i += 1;
758 bool set_share_mode(struct share_mode_lock *lck,
759 struct files_struct *fsp,
760 uid_t uid,
761 uint64_t mid,
762 uint16_t op_type,
763 const struct GUID *client_guid,
764 const struct smb2_lease_key *lease_key)
766 struct share_mode_data *d = lck->data;
767 struct share_mode_entry *tmp, *e;
769 tmp = talloc_realloc(d, d->share_modes, struct share_mode_entry,
770 d->num_share_modes+1);
771 if (tmp == NULL) {
772 return false;
774 d->share_modes = tmp;
775 e = &d->share_modes[d->num_share_modes];
776 d->num_share_modes += 1;
777 d->modified = true;
779 ZERO_STRUCTP(e);
780 e->pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
781 e->share_access = fsp->share_access;
782 e->private_options = fsp->fh->private_options;
783 e->access_mask = fsp->access_mask;
784 e->op_mid = mid;
785 e->op_type = op_type;
787 if (op_type == LEASE_OPLOCK) {
788 e->client_guid = *client_guid;
789 e->lease_key = *lease_key;
792 e->time.tv_sec = fsp->open_time.tv_sec;
793 e->time.tv_usec = fsp->open_time.tv_usec;
794 e->share_file_id = fsp->fh->gen_id;
795 e->uid = (uint32_t)uid;
796 e->flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
797 SHARE_MODE_FLAG_POSIX_OPEN : 0;
798 e->name_hash = fsp->name_hash;
800 return true;
803 struct share_mode_entry *find_share_mode_entry(
804 struct share_mode_lock *lck, files_struct *fsp)
806 struct share_mode_data *d = lck->data;
807 struct server_id pid;
808 uint32_t i;
810 pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
812 for (i=0; i<d->num_share_modes; i++) {
813 struct share_mode_entry *e = &d->share_modes[i];
815 if (!is_valid_share_mode_entry(e)) {
816 continue;
818 if (!serverid_equal(&pid, &e->pid)) {
819 continue;
821 if (fsp->fh->gen_id != e->share_file_id) {
822 continue;
824 return e;
826 return NULL;
829 /*******************************************************************
830 Del the share mode of a file for this process.
831 ********************************************************************/
833 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
835 struct share_mode_entry *e;
837 e = find_share_mode_entry(lck, fsp);
838 if (e == NULL) {
839 return False;
841 remove_share_mode_lease(lck->data, e);
842 *e = lck->data->share_modes[lck->data->num_share_modes-1];
843 lck->data->num_share_modes -= 1;
844 lck->data->modified = True;
845 return True;
848 bool mark_share_mode_disconnected(struct share_mode_lock *lck,
849 struct files_struct *fsp)
851 struct share_mode_entry *e;
853 if (lck->data->num_share_modes != 1) {
854 return false;
857 if (fsp->op == NULL) {
858 return false;
860 if (!fsp->op->global->durable) {
861 return false;
864 e = find_share_mode_entry(lck, fsp);
865 if (e == NULL) {
866 return false;
869 DEBUG(10, ("Marking share mode entry disconnected for durable handle\n"));
871 server_id_set_disconnected(&e->pid);
874 * On reopen the caller needs to check that
875 * the client comes with the correct handle.
877 e->share_file_id = fsp->op->global->open_persistent_id;
879 lck->data->modified = true;
880 return true;
883 /*******************************************************************
884 Remove an oplock mid and mode entry from a share mode.
885 ********************************************************************/
887 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
889 struct share_mode_data *d = lck->data;
890 struct share_mode_entry *e;
892 e = find_share_mode_entry(lck, fsp);
893 if (e == NULL) {
894 return False;
897 remove_share_mode_lease(d, e);
898 d->modified = True;
899 return true;
902 /*******************************************************************
903 Downgrade a oplock type from exclusive to level II.
904 ********************************************************************/
906 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
908 struct share_mode_entry *e;
910 e = find_share_mode_entry(lck, fsp);
911 if (e == NULL) {
912 return False;
915 e->op_type = LEVEL_II_OPLOCK;
916 lck->data->modified = True;
917 return True;
920 /****************************************************************************
921 Adds a delete on close token.
922 ****************************************************************************/
924 static bool add_delete_on_close_token(struct share_mode_data *d,
925 uint32_t name_hash,
926 const struct security_token *nt_tok,
927 const struct security_unix_token *tok)
929 struct delete_token *tmp, *dtl;
931 tmp = talloc_realloc(d, d->delete_tokens, struct delete_token,
932 d->num_delete_tokens+1);
933 if (tmp == NULL) {
934 return false;
936 d->delete_tokens = tmp;
937 dtl = &d->delete_tokens[d->num_delete_tokens];
939 dtl->name_hash = name_hash;
940 dtl->delete_nt_token = dup_nt_token(d->delete_tokens, nt_tok);
941 if (dtl->delete_nt_token == NULL) {
942 return false;
944 dtl->delete_token = copy_unix_token(d->delete_tokens, tok);
945 if (dtl->delete_token == NULL) {
946 return false;
948 d->num_delete_tokens += 1;
949 d->modified = true;
950 return true;
953 void reset_delete_on_close_lck(files_struct *fsp,
954 struct share_mode_lock *lck)
956 struct share_mode_data *d = lck->data;
957 uint32_t i;
959 for (i=0; i<d->num_delete_tokens; i++) {
960 struct delete_token *dt = &d->delete_tokens[i];
962 if (dt->name_hash == fsp->name_hash) {
963 d->modified = true;
965 /* Delete this entry. */
966 TALLOC_FREE(dt->delete_nt_token);
967 TALLOC_FREE(dt->delete_token);
968 *dt = d->delete_tokens[d->num_delete_tokens-1];
969 d->num_delete_tokens -= 1;
974 /****************************************************************************
975 Sets the delete on close flag over all share modes on this file.
976 Modify the share mode entry for all files open
977 on this device and inode to tell other smbds we have
978 changed the delete on close flag. This will be noticed
979 in the close code, the last closer will delete the file
980 if flag is set.
981 This makes a copy of any struct security_unix_token into the
982 lck entry. This function is used when the lock is already granted.
983 ****************************************************************************/
985 void set_delete_on_close_lck(files_struct *fsp,
986 struct share_mode_lock *lck,
987 const struct security_token *nt_tok,
988 const struct security_unix_token *tok)
990 struct messaging_context *msg_ctx = fsp->conn->sconn->msg_ctx;
991 struct share_mode_data *d = lck->data;
992 uint32_t i;
993 bool ret;
994 DATA_BLOB fid_blob = {};
995 enum ndr_err_code ndr_err;
997 SMB_ASSERT(nt_tok != NULL);
998 SMB_ASSERT(tok != NULL);
1000 for (i=0; i<d->num_delete_tokens; i++) {
1001 struct delete_token *dt = &d->delete_tokens[i];
1002 if (dt->name_hash == fsp->name_hash) {
1003 d->modified = true;
1005 /* Replace this token with the given tok. */
1006 TALLOC_FREE(dt->delete_nt_token);
1007 dt->delete_nt_token = dup_nt_token(dt, nt_tok);
1008 SMB_ASSERT(dt->delete_nt_token != NULL);
1009 TALLOC_FREE(dt->delete_token);
1010 dt->delete_token = copy_unix_token(dt, tok);
1011 SMB_ASSERT(dt->delete_token != NULL);
1013 return;
1017 ret = add_delete_on_close_token(lck->data, fsp->name_hash, nt_tok, tok);
1018 SMB_ASSERT(ret);
1020 ndr_err = ndr_push_struct_blob(&fid_blob, talloc_tos(), &fsp->file_id,
1021 (ndr_push_flags_fn_t)ndr_push_file_id);
1022 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1023 DEBUG(10, ("ndr_push_file_id failed: %s\n",
1024 ndr_errstr(ndr_err)));
1027 for (i=0; i<d->num_share_modes; i++) {
1028 struct share_mode_entry *e = &d->share_modes[i];
1029 NTSTATUS status;
1031 status = messaging_send(
1032 msg_ctx, e->pid, MSG_SMB_NOTIFY_CANCEL_DELETED,
1033 &fid_blob);
1035 if (!NT_STATUS_IS_OK(status)) {
1036 struct server_id_buf tmp;
1037 DEBUG(10, ("%s: messaging_send to %s returned %s\n",
1038 __func__, server_id_str_buf(e->pid, &tmp),
1039 nt_errstr(status)));
1043 TALLOC_FREE(fid_blob.data);
1046 bool set_delete_on_close(files_struct *fsp, bool delete_on_close,
1047 const struct security_token *nt_tok,
1048 const struct security_unix_token *tok)
1050 struct share_mode_lock *lck;
1052 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1053 "%s, file %s\n",
1054 delete_on_close ? "Adding" : "Removing", fsp_fnum_dbg(fsp),
1055 fsp_str_dbg(fsp)));
1057 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1058 if (lck == NULL) {
1059 return False;
1062 if (delete_on_close) {
1063 set_delete_on_close_lck(fsp, lck, nt_tok, tok);
1064 } else {
1065 reset_delete_on_close_lck(fsp, lck);
1068 if (fsp->is_directory) {
1069 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1070 send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx,
1071 fsp->fsp_name->base_name);
1074 TALLOC_FREE(lck);
1076 fsp->delete_on_close = delete_on_close;
1078 return True;
1081 static struct delete_token *find_delete_on_close_token(
1082 struct share_mode_data *d, uint32_t name_hash)
1084 uint32_t i;
1086 DEBUG(10, ("find_delete_on_close_token: name_hash = 0x%x\n",
1087 (unsigned int)name_hash));
1089 for (i=0; i<d->num_delete_tokens; i++) {
1090 struct delete_token *dt = &d->delete_tokens[i];
1092 DEBUG(10, ("find__delete_on_close_token: dt->name_hash = 0x%x\n",
1093 (unsigned int)dt->name_hash ));
1094 if (dt->name_hash == name_hash) {
1095 return dt;
1098 return NULL;
1101 /****************************************************************************
1102 Return the NT token and UNIX token if there's a match. Return true if
1103 found, false if not.
1104 ****************************************************************************/
1106 bool get_delete_on_close_token(struct share_mode_lock *lck,
1107 uint32_t name_hash,
1108 const struct security_token **pp_nt_tok,
1109 const struct security_unix_token **pp_tok)
1111 struct delete_token *dt;
1113 dt = find_delete_on_close_token(lck->data, name_hash);
1114 if (dt == NULL) {
1115 return false;
1117 *pp_nt_tok = dt->delete_nt_token;
1118 *pp_tok = dt->delete_token;
1119 return true;
1122 bool is_delete_on_close_set(struct share_mode_lock *lck, uint32_t name_hash)
1124 return find_delete_on_close_token(lck->data, name_hash) != NULL;
1127 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1129 struct share_mode_lock *lck;
1131 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1132 timestring(talloc_tos(),
1133 convert_timespec_to_time_t(write_time)),
1134 file_id_string_tos(&fileid)));
1136 lck = get_existing_share_mode_lock(talloc_tos(), fileid);
1137 if (lck == NULL) {
1138 return False;
1141 if (timespec_compare(&lck->data->changed_write_time, &write_time) != 0) {
1142 lck->data->modified = True;
1143 lck->data->changed_write_time = write_time;
1146 TALLOC_FREE(lck);
1147 return True;
1150 bool set_write_time(struct file_id fileid, struct timespec write_time)
1152 struct share_mode_lock *lck;
1154 DEBUG(5,("set_write_time: %s id=%s\n",
1155 timestring(talloc_tos(),
1156 convert_timespec_to_time_t(write_time)),
1157 file_id_string_tos(&fileid)));
1159 lck = get_existing_share_mode_lock(talloc_tos(), fileid);
1160 if (lck == NULL) {
1161 return False;
1164 if (timespec_compare(&lck->data->old_write_time, &write_time) != 0) {
1165 lck->data->modified = True;
1166 lck->data->old_write_time = write_time;
1169 TALLOC_FREE(lck);
1170 return True;
1173 struct timespec get_share_mode_write_time(struct share_mode_lock *lck)
1175 struct share_mode_data *d = lck->data;
1177 if (!null_timespec(d->changed_write_time)) {
1178 return d->changed_write_time;
1180 return d->old_write_time;
1183 bool file_has_open_streams(files_struct *fsp)
1185 struct share_mode_lock *lock = NULL;
1186 struct share_mode_data *d = NULL;
1187 uint32_t i;
1189 lock = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1190 if (lock == NULL) {
1191 return false;
1193 d = lock->data;
1195 for (i = 0; i < d->num_share_modes; i++) {
1196 struct share_mode_entry *e = &d->share_modes[i];
1198 if (share_mode_stale_pid(d, i)) {
1199 continue;
1202 if (e->private_options &
1203 NTCREATEX_OPTIONS_PRIVATE_STREAM_BASEOPEN)
1205 TALLOC_FREE(lock);
1206 return true;
1210 TALLOC_FREE(lock);
1211 return false;
1215 * Walk share mode entries, looking at every lease only once
1218 bool share_mode_forall_leases(
1219 struct share_mode_lock *lck,
1220 bool (*fn)(struct share_mode_lock *lck,
1221 struct share_mode_entry *e,
1222 void *private_data),
1223 void *private_data)
1225 struct share_mode_data *d = lck->data;
1226 uint32_t *leases = NULL;
1227 uint32_t num_leases = 0;
1228 uint32_t i;
1230 leases = talloc_array(talloc_tos(), uint32_t, d->num_share_modes);
1231 if (leases == NULL) {
1232 return false;
1235 for (i=0; i<d->num_share_modes; i++) {
1236 struct share_mode_entry *e = &d->share_modes[i];
1237 uint32_t j;
1238 bool ok, stop;
1240 ok = is_valid_share_mode_entry(e);
1241 if (!ok) {
1242 continue;
1245 if (e->op_type != LEASE_OPLOCK) {
1246 continue;
1250 * See if we have already seen "e"'s lease. This is
1251 * O(n^2). If we sort "leases", we can get this down
1252 * to O(n).
1255 for (j=0; j<num_leases; j++) {
1256 uint32_t idx = leases[j];
1257 struct share_mode_entry *l = &d->share_modes[idx];
1259 if (smb2_lease_equal(&e->client_guid,
1260 &e->lease_key,
1261 &l->client_guid,
1262 &l->lease_key)) {
1263 break;
1266 if (j < num_leases) {
1268 * Don't look at "e"'s lease, we've already
1269 * seen it.
1271 continue;
1274 stop = fn(lck, e, private_data);
1275 if (stop) {
1276 TALLOC_FREE(leases);
1277 return true;
1280 leases[num_leases] = i;
1281 num_leases += 1;
1284 TALLOC_FREE(leases);
1285 return true;