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 "locking/proto.h"
41 #include "smbd/globals.h"
42 #include "dbwrap/dbwrap.h"
43 #include "dbwrap/dbwrap_open.h"
44 #include "../libcli/security/security.h"
48 #include "../librpc/gen_ndr/ndr_open_files.h"
49 #include "source3/lib/dbwrap/dbwrap_watch.h"
52 #define DBGC_CLASS DBGC_LOCKING
54 #define NO_LOCKING_COUNT (-1)
56 /* the locking database handle */
57 static struct db_context *lock_db;
59 static bool locking_init_internal(bool read_only)
68 db_path = lock_path("locking.tdb");
69 if (db_path == NULL) {
73 lock_db = db_open(NULL, db_path,
74 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
75 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
76 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
77 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
80 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
84 if (!posix_locking_init(read_only))
87 dbwrap_watch_db(lock_db, server_messaging_context());
92 bool locking_init(void)
94 return locking_init_internal(false);
97 bool locking_init_readonly(void)
99 return locking_init_internal(true);
102 /*******************************************************************
103 Deinitialize the share_mode management.
104 ******************************************************************/
106 bool locking_end(void)
109 TALLOC_FREE(lock_db);
113 /*******************************************************************
114 Form a static locking key for a dev/inode pair.
115 ******************************************************************/
117 static TDB_DATA locking_key(const struct file_id *id)
119 return make_tdb_data((const uint8_t *)id, sizeof(*id));
122 /*******************************************************************
123 Get all share mode entries for a dev/inode pair.
124 ********************************************************************/
126 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
129 struct share_mode_data *d;
130 enum ndr_err_code ndr_err;
134 d = talloc(mem_ctx, struct share_mode_data);
136 DEBUG(0, ("talloc failed\n"));
140 blob.data = dbuf.dptr;
141 blob.length = dbuf.dsize;
143 ndr_err = ndr_pull_struct_blob_all(
144 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
145 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
146 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
147 ndr_errstr(ndr_err)));
152 * Initialize the values that are [skip] in the idl. The NDR code does
153 * not initialize them.
156 for (i=0; i<d->num_share_modes; i++) {
157 struct share_mode_entry *e = &d->share_modes[i];
161 if (e->op_type != LEASE_OPLOCK) {
164 if (e->lease_idx >= d->num_leases) {
167 e->lease = &d->leases[e->lease_idx];
172 if (DEBUGLEVEL >= 10) {
173 DEBUG(10, ("parse_share_modes:\n"));
174 NDR_PRINT_DEBUG(share_mode_data, d);
183 /*******************************************************************
184 Create a storable data blob from a modified share_mode_data struct.
185 ********************************************************************/
187 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
190 enum ndr_err_code ndr_err;
192 if (DEBUGLEVEL >= 10) {
193 DEBUG(10, ("unparse_share_modes:\n"));
194 NDR_PRINT_DEBUG(share_mode_data, d);
197 remove_stale_share_mode_entries(d);
199 if (d->num_share_modes == 0) {
200 DEBUG(10, ("No used share mode found\n"));
201 return make_tdb_data(NULL, 0);
204 ndr_err = ndr_push_struct_blob(
205 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
206 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
207 smb_panic("ndr_push_share_mode_lock failed");
210 return make_tdb_data(blob.data, blob.length);
213 /*******************************************************************
214 If modified, store the share_mode_data back into the database.
215 ********************************************************************/
217 static int share_mode_data_destructor(struct share_mode_data *d)
226 data = unparse_share_modes(d);
228 if (data.dptr == NULL) {
230 /* There has been an entry before, delete it */
232 status = dbwrap_record_delete(d->record);
233 if (!NT_STATUS_IS_OK(status)) {
236 DEBUG(0, ("delete_rec returned %s\n",
239 if (asprintf(&errmsg, "could not delete share "
241 nt_errstr(status)) == -1) {
242 smb_panic("could not delete share"
251 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
252 if (!NT_STATUS_IS_OK(status)) {
255 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
257 if (asprintf(&errmsg, "could not store share mode entry: %s",
258 nt_errstr(status)) == -1) {
259 smb_panic("could not store share mode entry");
269 /*******************************************************************
270 Allocate a new share_mode_data struct, mark it unmodified.
271 fresh is set to note that currently there is no database entry.
272 ********************************************************************/
274 static struct share_mode_data *fresh_share_mode_lock(
275 TALLOC_CTX *mem_ctx, const char *servicepath,
276 const struct smb_filename *smb_fname,
277 const struct timespec *old_write_time)
279 struct share_mode_data *d;
281 if ((servicepath == NULL) || (smb_fname == NULL) ||
282 (old_write_time == NULL)) {
286 d = talloc_zero(mem_ctx, struct share_mode_data);
290 d->base_name = talloc_strdup(d, smb_fname->base_name);
291 if (d->base_name == NULL) {
294 if (smb_fname->stream_name != NULL) {
295 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
296 if (d->stream_name == NULL) {
300 d->servicepath = talloc_strdup(d, servicepath);
301 if (d->servicepath == NULL) {
304 d->old_write_time = *old_write_time;
309 DEBUG(0, ("talloc failed\n"));
314 /*******************************************************************
315 Either fetch a share mode from the database, or allocate a fresh
316 one if the record doesn't exist.
317 ********************************************************************/
319 static struct share_mode_lock *get_share_mode_lock_internal(
320 TALLOC_CTX *mem_ctx, struct file_id id,
321 const char *servicepath, const struct smb_filename *smb_fname,
322 const struct timespec *old_write_time)
324 struct share_mode_lock *lck;
325 struct share_mode_data *d;
326 struct db_record *rec;
327 TDB_DATA key = locking_key(&id);
330 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
332 DEBUG(3, ("Could not lock share entry\n"));
336 value = dbwrap_record_get_value(rec);
338 if (value.dptr == NULL) {
339 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
342 d = parse_share_modes(mem_ctx, value);
346 DEBUG(5, ("get_share_mode_lock_internal: "
347 "Could not get share mode lock\n"));
351 d->record = talloc_move(d, &rec);
352 talloc_set_destructor(d, share_mode_data_destructor);
354 lck = talloc(mem_ctx, struct share_mode_lock);
356 DEBUG(1, ("talloc failed\n"));
360 lck->data = talloc_move(lck, &d);
365 * We can only ever have one share mode locked. Users of
366 * get_share_mode_lock never see this, it will be refcounted by
369 static struct share_mode_lock *the_lock;
370 static struct file_id the_lock_id;
372 static int the_lock_destructor(struct share_mode_lock *l)
375 ZERO_STRUCT(the_lock_id);
379 /*******************************************************************
380 Get a share_mode_lock, Reference counted to allow nested calls.
381 ********************************************************************/
383 struct share_mode_lock *get_share_mode_lock(
386 const char *servicepath,
387 const struct smb_filename *smb_fname,
388 const struct timespec *old_write_time)
390 struct share_mode_lock *lck;
392 lck = talloc(mem_ctx, struct share_mode_lock);
394 DEBUG(1, ("talloc failed\n"));
398 if (the_lock == NULL) {
399 the_lock = get_share_mode_lock_internal(
400 lck, id, servicepath, smb_fname, old_write_time);
401 if (the_lock == NULL) {
404 talloc_set_destructor(the_lock, the_lock_destructor);
407 if (!file_id_equal(&the_lock_id, &id)) {
408 DEBUG(1, ("Can not lock two share modes "
409 "simultaneously\n"));
412 if (talloc_reference(lck, the_lock) == NULL) {
413 DEBUG(1, ("talloc_reference failed\n"));
417 lck->data = the_lock->data;
424 static void fetch_share_mode_unlocked_parser(
425 TDB_DATA key, TDB_DATA data, void *private_data)
427 struct share_mode_lock *lck = talloc_get_type_abort(
428 private_data, struct share_mode_lock);
430 lck->data = parse_share_modes(lck, data);
433 /*******************************************************************
434 Get a share_mode_lock without locking the database or reference
435 counting. Used by smbstatus to display existing share modes.
436 ********************************************************************/
438 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
441 struct share_mode_lock *lck;
442 TDB_DATA key = locking_key(&id);
445 lck = talloc(mem_ctx, struct share_mode_lock);
447 DEBUG(0, ("talloc failed\n"));
450 status = dbwrap_parse_record(
451 lock_db, key, fetch_share_mode_unlocked_parser, lck);
452 if (!NT_STATUS_IS_OK(status) ||
453 (lck->data == NULL)) {
460 struct share_mode_forall_state {
461 int (*fn)(struct file_id fid, const struct share_mode_data *data,
466 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
468 struct share_mode_forall_state *state =
469 (struct share_mode_forall_state *)_state;
474 enum ndr_err_code ndr_err;
475 struct share_mode_data *d;
479 key = dbwrap_record_get_key(rec);
480 value = dbwrap_record_get_value(rec);
482 /* Ensure this is a locking_key record. */
483 if (key.dsize != sizeof(fid)) {
486 memcpy(&fid, key.dptr, sizeof(fid));
488 d = talloc(talloc_tos(), struct share_mode_data);
493 blob.data = value.dptr;
494 blob.length = value.dsize;
496 ndr_err = ndr_pull_struct_blob_all(
497 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
498 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
499 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
502 if (DEBUGLEVEL > 10) {
503 DEBUG(11, ("parse_share_modes:\n"));
504 NDR_PRINT_DEBUG(share_mode_data, d);
506 for (i=0; i<d->num_share_modes; i++) {
507 d->share_modes[i].stale = false; /* [skip] in idl */
510 ret = state->fn(fid, d, state->private_data);
516 int share_mode_forall(int (*fn)(struct file_id fid,
517 const struct share_mode_data *data,
521 struct share_mode_forall_state state = {
523 .private_data = private_data
528 if (lock_db == NULL) {
532 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
534 if (!NT_STATUS_IS_OK(status)) {
541 struct share_entry_forall_state {
542 int (*fn)(const struct share_mode_entry *e,
543 const char *service_path, const char *base_name,
548 static int share_entry_traverse_fn(struct file_id fid,
549 const struct share_mode_data *data,
552 struct share_entry_forall_state *state = private_data;
555 for (i=0; i<data->num_share_modes; i++) {
558 ret = state->fn(&data->share_modes[i],
559 data->servicepath, data->base_name,
560 state->private_data);
569 /*******************************************************************
570 Call the specified function on each entry under management by the
572 ********************************************************************/
574 int share_entry_forall(int (*fn)(const struct share_mode_entry *,
575 const char *, const char *, void *),
578 struct share_entry_forall_state state = {
579 .fn = fn, .private_data = private_data };
581 return share_mode_forall(share_entry_traverse_fn, &state);
584 bool share_mode_cleanup_disconnected(struct file_id fid,
585 uint64_t open_persistent_id)
588 TALLOC_CTX *frame = talloc_stackframe();
590 struct share_mode_data *data;
591 struct share_mode_lock *lck;
594 lck = get_existing_share_mode_lock(frame, fid);
596 DEBUG(5, ("share_mode_cleanup_disconnected: "
597 "Could not fetch share mode entry for %s\n",
598 file_id_string(frame, &fid)));
603 for (n=0; n < data->num_share_modes; n++) {
604 struct share_mode_entry *entry = &data->share_modes[n];
606 if (!server_id_is_disconnected(&entry->pid)) {
607 DEBUG(5, ("share_mode_cleanup_disconnected: "
608 "file (file-id='%s', servicepath='%s', "
609 "base_name='%s%s%s') "
610 "is used by server %s ==> do not cleanup\n",
611 file_id_string(frame, &fid),
614 (data->stream_name == NULL)
615 ? "" : "', stream_name='",
616 (data->stream_name == NULL)
617 ? "" : data->stream_name,
618 server_id_str(frame, &entry->pid)));
621 if (open_persistent_id != entry->share_file_id) {
622 DEBUG(5, ("share_mode_cleanup_disconnected: "
624 "(file-id='%s', servicepath='%s', "
625 "base_name='%s%s%s') "
626 "has share_file_id %llu but expected %llu"
627 "==> do not cleanup\n",
628 file_id_string(frame, &fid),
631 (data->stream_name == NULL)
632 ? "" : "', stream_name='",
633 (data->stream_name == NULL)
634 ? "" : data->stream_name,
635 (unsigned long long)entry->share_file_id,
636 (unsigned long long)open_persistent_id));
641 ok = brl_cleanup_disconnected(fid, open_persistent_id);
643 DEBUG(10, ("share_mode_cleanup_disconnected: "
644 "failed to clean up byte range locks associated "
645 "with file (file-id='%s', servicepath='%s', "
646 "base_name='%s%s%s') and open_persistent_id %llu "
647 "==> do not cleanup\n",
648 file_id_string(frame, &fid),
651 (data->stream_name == NULL)
652 ? "" : "', stream_name='",
653 (data->stream_name == NULL)
654 ? "" : data->stream_name,
655 (unsigned long long)open_persistent_id));
659 DEBUG(10, ("share_mode_cleanup_disconnected: "
660 "cleaning up %u entries for file "
661 "(file-id='%s', servicepath='%s', "
662 "base_name='%s%s%s') "
663 "from open_persistent_id %llu\n",
664 data->num_share_modes,
665 file_id_string(frame, &fid),
668 (data->stream_name == NULL)
669 ? "" : "', stream_name='",
670 (data->stream_name == NULL)
671 ? "" : data->stream_name,
672 (unsigned long long)open_persistent_id));
674 data->num_share_modes = 0;
675 data->modified = true;