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 d->share_modes[i].stale = false;
162 if (DEBUGLEVEL >= 10) {
163 DEBUG(10, ("parse_share_modes:\n"));
164 NDR_PRINT_DEBUG(share_mode_data, d);
173 /*******************************************************************
174 Create a storable data blob from a modified share_mode_data struct.
175 ********************************************************************/
177 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
180 enum ndr_err_code ndr_err;
182 if (DEBUGLEVEL >= 10) {
183 DEBUG(10, ("unparse_share_modes:\n"));
184 NDR_PRINT_DEBUG(share_mode_data, d);
187 remove_stale_share_mode_entries(d);
189 if (d->num_share_modes == 0) {
190 DEBUG(10, ("No used share mode found\n"));
191 return make_tdb_data(NULL, 0);
194 ndr_err = ndr_push_struct_blob(
195 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
196 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
197 smb_panic("ndr_push_share_mode_lock failed");
200 return make_tdb_data(blob.data, blob.length);
203 /*******************************************************************
204 If modified, store the share_mode_data back into the database.
205 ********************************************************************/
207 static int share_mode_data_destructor(struct share_mode_data *d)
216 data = unparse_share_modes(d);
218 if (data.dptr == NULL) {
220 /* There has been an entry before, delete it */
222 status = dbwrap_record_delete(d->record);
223 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(0, ("delete_rec returned %s\n",
229 if (asprintf(&errmsg, "could not delete share "
231 nt_errstr(status)) == -1) {
232 smb_panic("could not delete share"
241 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
242 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
247 if (asprintf(&errmsg, "could not store share mode entry: %s",
248 nt_errstr(status)) == -1) {
249 smb_panic("could not store share mode entry");
259 /*******************************************************************
260 Allocate a new share_mode_data struct, mark it unmodified.
261 fresh is set to note that currently there is no database entry.
262 ********************************************************************/
264 static struct share_mode_data *fresh_share_mode_lock(
265 TALLOC_CTX *mem_ctx, const char *servicepath,
266 const struct smb_filename *smb_fname,
267 const struct timespec *old_write_time)
269 struct share_mode_data *d;
271 if ((servicepath == NULL) || (smb_fname == NULL) ||
272 (old_write_time == NULL)) {
276 d = talloc_zero(mem_ctx, struct share_mode_data);
280 d->base_name = talloc_strdup(d, smb_fname->base_name);
281 if (d->base_name == NULL) {
284 if (smb_fname->stream_name != NULL) {
285 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
286 if (d->stream_name == NULL) {
290 d->servicepath = talloc_strdup(d, servicepath);
291 if (d->servicepath == NULL) {
294 d->old_write_time = *old_write_time;
299 DEBUG(0, ("talloc failed\n"));
304 /*******************************************************************
305 Either fetch a share mode from the database, or allocate a fresh
306 one if the record doesn't exist.
307 ********************************************************************/
309 static struct share_mode_lock *get_share_mode_lock_internal(
310 TALLOC_CTX *mem_ctx, struct file_id id,
311 const char *servicepath, const struct smb_filename *smb_fname,
312 const struct timespec *old_write_time)
314 struct share_mode_lock *lck;
315 struct share_mode_data *d;
316 struct db_record *rec;
317 TDB_DATA key = locking_key(&id);
320 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
322 DEBUG(3, ("Could not lock share entry\n"));
326 value = dbwrap_record_get_value(rec);
328 if (value.dptr == NULL) {
329 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
332 d = parse_share_modes(mem_ctx, value);
336 DEBUG(5, ("get_share_mode_lock_internal: "
337 "Could not get share mode lock\n"));
341 d->record = talloc_move(d, &rec);
342 talloc_set_destructor(d, share_mode_data_destructor);
344 lck = talloc(mem_ctx, struct share_mode_lock);
346 DEBUG(1, ("talloc failed\n"));
350 lck->data = talloc_move(lck, &d);
355 * We can only ever have one share mode locked. Users of
356 * get_share_mode_lock never see this, it will be refcounted by
359 static struct share_mode_lock *the_lock;
360 static struct file_id the_lock_id;
362 static int the_lock_destructor(struct share_mode_lock *l)
365 ZERO_STRUCT(the_lock_id);
369 /*******************************************************************
370 Get a share_mode_lock, Reference counted to allow nested calls.
371 ********************************************************************/
373 struct share_mode_lock *get_share_mode_lock(
376 const char *servicepath,
377 const struct smb_filename *smb_fname,
378 const struct timespec *old_write_time)
380 struct share_mode_lock *lck;
382 lck = talloc(mem_ctx, struct share_mode_lock);
384 DEBUG(1, ("talloc failed\n"));
388 if (the_lock == NULL) {
389 the_lock = get_share_mode_lock_internal(
390 lck, id, servicepath, smb_fname, old_write_time);
391 if (the_lock == NULL) {
394 talloc_set_destructor(the_lock, the_lock_destructor);
397 if (!file_id_equal(&the_lock_id, &id)) {
398 DEBUG(1, ("Can not lock two share modes "
399 "simultaneously\n"));
402 if (talloc_reference(lck, the_lock) == NULL) {
403 DEBUG(1, ("talloc_reference failed\n"));
407 lck->data = the_lock->data;
414 static void fetch_share_mode_unlocked_parser(
415 TDB_DATA key, TDB_DATA data, void *private_data)
417 struct share_mode_lock *lck = talloc_get_type_abort(
418 private_data, struct share_mode_lock);
420 lck->data = parse_share_modes(lck, data);
423 /*******************************************************************
424 Get a share_mode_lock without locking the database or reference
425 counting. Used by smbstatus to display existing share modes.
426 ********************************************************************/
428 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
431 struct share_mode_lock *lck;
432 TDB_DATA key = locking_key(&id);
435 lck = talloc(mem_ctx, struct share_mode_lock);
437 DEBUG(0, ("talloc failed\n"));
440 status = dbwrap_parse_record(
441 lock_db, key, fetch_share_mode_unlocked_parser, lck);
442 if (!NT_STATUS_IS_OK(status) ||
443 (lck->data == NULL)) {
450 struct share_mode_forall_state {
451 int (*fn)(struct file_id fid, const struct share_mode_data *data,
456 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
458 struct share_mode_forall_state *state =
459 (struct share_mode_forall_state *)_state;
464 enum ndr_err_code ndr_err;
465 struct share_mode_data *d;
469 key = dbwrap_record_get_key(rec);
470 value = dbwrap_record_get_value(rec);
472 /* Ensure this is a locking_key record. */
473 if (key.dsize != sizeof(fid)) {
476 memcpy(&fid, key.dptr, sizeof(fid));
478 d = talloc(talloc_tos(), struct share_mode_data);
483 blob.data = value.dptr;
484 blob.length = value.dsize;
486 ndr_err = ndr_pull_struct_blob_all(
487 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
488 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
489 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
492 if (DEBUGLEVEL > 10) {
493 DEBUG(11, ("parse_share_modes:\n"));
494 NDR_PRINT_DEBUG(share_mode_data, d);
496 for (i=0; i<d->num_share_modes; i++) {
497 d->share_modes[i].stale = false; /* [skip] in idl */
500 ret = state->fn(fid, d, state->private_data);
506 int share_mode_forall(int (*fn)(struct file_id fid,
507 const struct share_mode_data *data,
511 struct share_mode_forall_state state = {
513 .private_data = private_data
518 if (lock_db == NULL) {
522 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
524 if (!NT_STATUS_IS_OK(status)) {
531 struct share_entry_forall_state {
532 int (*fn)(const struct share_mode_entry *e,
533 const char *service_path, const char *base_name,
538 static int share_entry_traverse_fn(struct file_id fid,
539 const struct share_mode_data *data,
542 struct share_entry_forall_state *state = private_data;
545 for (i=0; i<data->num_share_modes; i++) {
548 ret = state->fn(&data->share_modes[i],
549 data->servicepath, data->base_name,
550 state->private_data);
559 /*******************************************************************
560 Call the specified function on each entry under management by the
562 ********************************************************************/
564 int share_entry_forall(int (*fn)(const struct share_mode_entry *,
565 const char *, const char *, void *),
568 struct share_entry_forall_state state = {
569 .fn = fn, .private_data = private_data };
571 return share_mode_forall(share_entry_traverse_fn, &state);
574 bool share_mode_cleanup_disconnected(struct file_id fid,
575 uint64_t open_persistent_id)
578 TALLOC_CTX *frame = talloc_stackframe();
580 struct share_mode_data *data;
581 struct share_mode_lock *lck;
584 lck = get_existing_share_mode_lock(frame, fid);
586 DEBUG(5, ("share_mode_cleanup_disconnected: "
587 "Could not fetch share mode entry for %s\n",
588 file_id_string(frame, &fid)));
593 for (n=0; n < data->num_share_modes; n++) {
594 struct share_mode_entry *entry = &data->share_modes[n];
596 if (!server_id_is_disconnected(&entry->pid)) {
597 DEBUG(5, ("share_mode_cleanup_disconnected: "
598 "file (file-id='%s', servicepath='%s', "
599 "base_name='%s%s%s') "
600 "is used by server %s ==> do not cleanup\n",
601 file_id_string(frame, &fid),
604 (data->stream_name == NULL)
605 ? "" : "', stream_name='",
606 (data->stream_name == NULL)
607 ? "" : data->stream_name,
608 server_id_str(frame, &entry->pid)));
611 if (open_persistent_id != entry->share_file_id) {
612 DEBUG(5, ("share_mode_cleanup_disconnected: "
614 "(file-id='%s', servicepath='%s', "
615 "base_name='%s%s%s') "
616 "has share_file_id %llu but expected %llu"
617 "==> do not cleanup\n",
618 file_id_string(frame, &fid),
621 (data->stream_name == NULL)
622 ? "" : "', stream_name='",
623 (data->stream_name == NULL)
624 ? "" : data->stream_name,
625 (unsigned long long)entry->share_file_id,
626 (unsigned long long)open_persistent_id));
631 ok = brl_cleanup_disconnected(fid, open_persistent_id);
633 DEBUG(10, ("share_mode_cleanup_disconnected: "
634 "failed to clean up byte range locks associated "
635 "with file (file-id='%s', servicepath='%s', "
636 "base_name='%s%s%s') and open_persistent_id %llu "
637 "==> do not cleanup\n",
638 file_id_string(frame, &fid),
641 (data->stream_name == NULL)
642 ? "" : "', stream_name='",
643 (data->stream_name == NULL)
644 ? "" : data->stream_name,
645 (unsigned long long)open_persistent_id));
649 DEBUG(10, ("share_mode_cleanup_disconnected: "
650 "cleaning up %u entries for file "
651 "(file-id='%s', servicepath='%s', "
652 "base_name='%s%s%s') "
653 "from open_persistent_id %llu\n",
654 data->num_share_modes,
655 file_id_string(frame, &fid),
658 (data->stream_name == NULL)
659 ? "" : "', stream_name='",
660 (data->stream_name == NULL)
661 ? "" : data->stream_name,
662 (unsigned long long)open_persistent_id));
664 data->num_share_modes = 0;
665 data->modified = true;