s3: smbd: VFS: fake_acl module called get_full_smb_filename() with a stream path...
[Samba.git] / source3 / locking / share_mode_lock.c
blob327ac796980f7a550d048efce0c07823d01794be
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 "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"
45 #include "serverid.h"
46 #include "messages.h"
47 #include "util_tdb.h"
48 #include "../librpc/gen_ndr/ndr_open_files.h"
49 #include "source3/lib/dbwrap/dbwrap_watch.h"
50 #include "locking/leases_db.h"
52 #undef DBGC_CLASS
53 #define DBGC_CLASS DBGC_LOCKING
55 #define NO_LOCKING_COUNT (-1)
57 /* the locking database handle */
58 static struct db_context *lock_db;
60 static bool locking_init_internal(bool read_only)
62 brl_init(read_only);
64 if (lock_db)
65 return True;
67 lock_db = db_open(NULL, lock_path("locking.tdb"),
68 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
69 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
70 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
71 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
73 if (!lock_db) {
74 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
75 return False;
78 if (!posix_locking_init(read_only))
79 return False;
81 dbwrap_watch_db(lock_db, server_messaging_context());
83 return True;
86 bool locking_init(void)
88 return locking_init_internal(false);
91 bool locking_init_readonly(void)
93 return locking_init_internal(true);
96 /*******************************************************************
97 Deinitialize the share_mode management.
98 ******************************************************************/
100 bool locking_end(void)
102 brl_shutdown();
103 TALLOC_FREE(lock_db);
104 return true;
107 /*******************************************************************
108 Form a static locking key for a dev/inode pair.
109 ******************************************************************/
111 static TDB_DATA locking_key(const struct file_id *id)
113 return make_tdb_data((const uint8_t *)id, sizeof(*id));
116 /*******************************************************************
117 Get all share mode entries for a dev/inode pair.
118 ********************************************************************/
120 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
121 const TDB_DATA dbuf)
123 struct share_mode_data *d;
124 enum ndr_err_code ndr_err;
125 uint32_t i;
126 DATA_BLOB blob;
128 d = talloc(mem_ctx, struct share_mode_data);
129 if (d == NULL) {
130 DEBUG(0, ("talloc failed\n"));
131 goto fail;
134 blob.data = dbuf.dptr;
135 blob.length = dbuf.dsize;
137 ndr_err = ndr_pull_struct_blob_all(
138 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
141 ndr_errstr(ndr_err)));
142 goto fail;
146 * Initialize the values that are [skip] in the idl. The NDR code does
147 * not initialize them.
150 for (i=0; i<d->num_share_modes; i++) {
151 struct share_mode_entry *e = &d->share_modes[i];
153 e->stale = false;
154 e->lease = NULL;
155 if (e->op_type != LEASE_OPLOCK) {
156 continue;
158 if (e->lease_idx >= d->num_leases) {
159 continue;
161 e->lease = &d->leases[e->lease_idx];
163 d->modified = false;
164 d->fresh = false;
166 if (DEBUGLEVEL >= 10) {
167 DEBUG(10, ("parse_share_modes:\n"));
168 NDR_PRINT_DEBUG(share_mode_data, d);
171 return d;
172 fail:
173 TALLOC_FREE(d);
174 return NULL;
177 /*******************************************************************
178 Create a storable data blob from a modified share_mode_data struct.
179 ********************************************************************/
181 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
183 DATA_BLOB blob;
184 enum ndr_err_code ndr_err;
186 if (DEBUGLEVEL >= 10) {
187 DEBUG(10, ("unparse_share_modes:\n"));
188 NDR_PRINT_DEBUG(share_mode_data, d);
191 remove_stale_share_mode_entries(d);
193 if (d->num_share_modes == 0) {
194 DEBUG(10, ("No used share mode found\n"));
195 return make_tdb_data(NULL, 0);
198 ndr_err = ndr_push_struct_blob(
199 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
200 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
201 smb_panic("ndr_push_share_mode_lock failed");
204 return make_tdb_data(blob.data, blob.length);
207 /*******************************************************************
208 If modified, store the share_mode_data back into the database.
209 ********************************************************************/
211 static int share_mode_data_destructor(struct share_mode_data *d)
213 NTSTATUS status;
214 TDB_DATA data;
216 if (!d->modified) {
217 return 0;
220 data = unparse_share_modes(d);
222 if (data.dptr == NULL) {
223 if (!d->fresh) {
224 /* There has been an entry before, delete it */
226 status = dbwrap_record_delete(d->record);
227 if (!NT_STATUS_IS_OK(status)) {
228 char *errmsg;
230 DEBUG(0, ("delete_rec returned %s\n",
231 nt_errstr(status)));
233 if (asprintf(&errmsg, "could not delete share "
234 "entry: %s\n",
235 nt_errstr(status)) == -1) {
236 smb_panic("could not delete share"
237 "entry");
239 smb_panic(errmsg);
242 goto done;
245 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
246 if (!NT_STATUS_IS_OK(status)) {
247 char *errmsg;
249 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
251 if (asprintf(&errmsg, "could not store share mode entry: %s",
252 nt_errstr(status)) == -1) {
253 smb_panic("could not store share mode entry");
255 smb_panic(errmsg);
258 done:
260 return 0;
263 /*******************************************************************
264 Allocate a new share_mode_data struct, mark it unmodified.
265 fresh is set to note that currently there is no database entry.
266 ********************************************************************/
268 static struct share_mode_data *fresh_share_mode_lock(
269 TALLOC_CTX *mem_ctx, const char *servicepath,
270 const struct smb_filename *smb_fname,
271 const struct timespec *old_write_time)
273 struct share_mode_data *d;
275 if ((servicepath == NULL) || (smb_fname == NULL) ||
276 (old_write_time == NULL)) {
277 return NULL;
280 d = talloc_zero(mem_ctx, struct share_mode_data);
281 if (d == NULL) {
282 goto fail;
284 d->base_name = talloc_strdup(d, smb_fname->base_name);
285 if (d->base_name == NULL) {
286 goto fail;
288 if (smb_fname->stream_name != NULL) {
289 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
290 if (d->stream_name == NULL) {
291 goto fail;
294 d->servicepath = talloc_strdup(d, servicepath);
295 if (d->servicepath == NULL) {
296 goto fail;
298 d->old_write_time = *old_write_time;
299 d->modified = false;
300 d->fresh = true;
301 return d;
302 fail:
303 DEBUG(0, ("talloc failed\n"));
304 TALLOC_FREE(d);
305 return NULL;
308 /*******************************************************************
309 Either fetch a share mode from the database, or allocate a fresh
310 one if the record doesn't exist.
311 ********************************************************************/
313 static struct share_mode_lock *get_share_mode_lock_internal(
314 TALLOC_CTX *mem_ctx, struct file_id id,
315 const char *servicepath, const struct smb_filename *smb_fname,
316 const struct timespec *old_write_time)
318 struct share_mode_lock *lck;
319 struct share_mode_data *d;
320 struct db_record *rec;
321 TDB_DATA key = locking_key(&id);
322 TDB_DATA value;
324 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
325 if (rec == NULL) {
326 DEBUG(3, ("Could not lock share entry\n"));
327 return NULL;
330 value = dbwrap_record_get_value(rec);
332 if (value.dptr == NULL) {
333 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
334 old_write_time);
335 } else {
336 d = parse_share_modes(mem_ctx, value);
339 if (d == NULL) {
340 DEBUG(5, ("get_share_mode_lock_internal: "
341 "Could not get share mode lock\n"));
342 TALLOC_FREE(rec);
343 return NULL;
345 d->record = talloc_move(d, &rec);
346 talloc_set_destructor(d, share_mode_data_destructor);
348 lck = talloc(mem_ctx, struct share_mode_lock);
349 if (lck == NULL) {
350 DEBUG(1, ("talloc failed\n"));
351 TALLOC_FREE(d);
352 return NULL;
354 lck->data = talloc_move(lck, &d);
355 return lck;
359 * We can only ever have one share mode locked. Users of
360 * get_share_mode_lock never see this, it will be refcounted by
361 * talloc_reference.
363 static struct share_mode_lock *the_lock;
364 static struct file_id the_lock_id;
366 static int the_lock_destructor(struct share_mode_lock *l)
368 the_lock = NULL;
369 ZERO_STRUCT(the_lock_id);
370 return 0;
373 /*******************************************************************
374 Get a share_mode_lock, Reference counted to allow nested calls.
375 ********************************************************************/
377 struct share_mode_lock *get_share_mode_lock(
378 TALLOC_CTX *mem_ctx,
379 struct file_id id,
380 const char *servicepath,
381 const struct smb_filename *smb_fname,
382 const struct timespec *old_write_time)
384 struct share_mode_lock *lck;
386 lck = talloc(mem_ctx, struct share_mode_lock);
387 if (lck == NULL) {
388 DEBUG(1, ("talloc failed\n"));
389 return NULL;
392 if (the_lock == NULL) {
393 the_lock = get_share_mode_lock_internal(
394 lck, id, servicepath, smb_fname, old_write_time);
395 if (the_lock == NULL) {
396 goto fail;
398 talloc_set_destructor(the_lock, the_lock_destructor);
399 the_lock_id = id;
400 } else {
401 if (!file_id_equal(&the_lock_id, &id)) {
402 DEBUG(1, ("Can not lock two share modes "
403 "simultaneously\n"));
404 goto fail;
406 if (talloc_reference(lck, the_lock) == NULL) {
407 DEBUG(1, ("talloc_reference failed\n"));
408 goto fail;
411 lck->data = the_lock->data;
412 return lck;
413 fail:
414 TALLOC_FREE(lck);
415 return NULL;
418 static void fetch_share_mode_unlocked_parser(
419 TDB_DATA key, TDB_DATA data, void *private_data)
421 struct share_mode_lock *lck = talloc_get_type_abort(
422 private_data, struct share_mode_lock);
424 lck->data = parse_share_modes(lck, data);
427 /*******************************************************************
428 Get a share_mode_lock without locking the database or reference
429 counting. Used by smbstatus to display existing share modes.
430 ********************************************************************/
432 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
433 struct file_id id)
435 struct share_mode_lock *lck;
436 TDB_DATA key = locking_key(&id);
437 NTSTATUS status;
439 lck = talloc(mem_ctx, struct share_mode_lock);
440 if (lck == NULL) {
441 DEBUG(0, ("talloc failed\n"));
442 return NULL;
444 status = dbwrap_parse_record(
445 lock_db, key, fetch_share_mode_unlocked_parser, lck);
446 if (!NT_STATUS_IS_OK(status) ||
447 (lck->data == NULL)) {
448 TALLOC_FREE(lck);
449 return NULL;
451 return lck;
454 struct share_mode_forall_state {
455 int (*fn)(struct file_id fid, const struct share_mode_data *data,
456 void *private_data);
457 void *private_data;
460 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
462 struct share_mode_forall_state *state =
463 (struct share_mode_forall_state *)_state;
464 uint32_t i;
465 TDB_DATA key;
466 TDB_DATA value;
467 DATA_BLOB blob;
468 enum ndr_err_code ndr_err;
469 struct share_mode_data *d;
470 struct file_id fid;
471 int ret;
473 key = dbwrap_record_get_key(rec);
474 value = dbwrap_record_get_value(rec);
476 /* Ensure this is a locking_key record. */
477 if (key.dsize != sizeof(fid)) {
478 return 0;
480 memcpy(&fid, key.dptr, sizeof(fid));
482 d = talloc(talloc_tos(), struct share_mode_data);
483 if (d == NULL) {
484 return 0;
487 blob.data = value.dptr;
488 blob.length = value.dsize;
490 ndr_err = ndr_pull_struct_blob_all(
491 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
492 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
493 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
494 return 0;
496 if (DEBUGLEVEL > 10) {
497 DEBUG(11, ("parse_share_modes:\n"));
498 NDR_PRINT_DEBUG(share_mode_data, d);
500 for (i=0; i<d->num_share_modes; i++) {
501 d->share_modes[i].stale = false; /* [skip] in idl */
504 ret = state->fn(fid, d, state->private_data);
506 TALLOC_FREE(d);
507 return ret;
510 int share_mode_forall(int (*fn)(struct file_id fid,
511 const struct share_mode_data *data,
512 void *private_data),
513 void *private_data)
515 struct share_mode_forall_state state = {
516 .fn = fn,
517 .private_data = private_data
519 NTSTATUS status;
520 int count;
522 if (lock_db == NULL) {
523 return 0;
526 status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
527 &state, &count);
528 if (!NT_STATUS_IS_OK(status)) {
529 return -1;
532 return count;
535 struct share_entry_forall_state {
536 int (*fn)(const struct share_mode_entry *e,
537 const char *service_path, const char *base_name,
538 void *private_data);
539 void *private_data;
542 static int share_entry_traverse_fn(struct file_id fid,
543 const struct share_mode_data *data,
544 void *private_data)
546 struct share_entry_forall_state *state = private_data;
547 uint32_t i;
549 for (i=0; i<data->num_share_modes; i++) {
550 int ret;
552 ret = state->fn(&data->share_modes[i],
553 data->servicepath, data->base_name,
554 state->private_data);
555 if (ret != 0) {
556 return ret;
560 return 0;
563 /*******************************************************************
564 Call the specified function on each entry under management by the
565 share mode system.
566 ********************************************************************/
568 int share_entry_forall(int (*fn)(const struct share_mode_entry *,
569 const char *, const char *, void *),
570 void *private_data)
572 struct share_entry_forall_state state = {
573 .fn = fn, .private_data = private_data };
575 return share_mode_forall(share_entry_traverse_fn, &state);
578 bool share_mode_cleanup_disconnected(struct file_id fid,
579 uint64_t open_persistent_id)
581 bool ret = false;
582 TALLOC_CTX *frame = talloc_stackframe();
583 unsigned n;
584 struct share_mode_data *data;
585 struct share_mode_lock *lck;
586 bool ok;
588 lck = get_existing_share_mode_lock(frame, fid);
589 if (lck == NULL) {
590 DEBUG(5, ("share_mode_cleanup_disconnected: "
591 "Could not fetch share mode entry for %s\n",
592 file_id_string(frame, &fid)));
593 goto done;
595 data = lck->data;
597 for (n=0; n < data->num_share_modes; n++) {
598 struct share_mode_entry *entry = &data->share_modes[n];
600 if (!server_id_is_disconnected(&entry->pid)) {
601 DEBUG(5, ("share_mode_cleanup_disconnected: "
602 "file (file-id='%s', servicepath='%s', "
603 "base_name='%s%s%s') "
604 "is used by server %s ==> do not cleanup\n",
605 file_id_string(frame, &fid),
606 data->servicepath,
607 data->base_name,
608 (data->stream_name == NULL)
609 ? "" : "', stream_name='",
610 (data->stream_name == NULL)
611 ? "" : data->stream_name,
612 server_id_str(frame, &entry->pid)));
613 goto done;
615 if (open_persistent_id != entry->share_file_id) {
616 DEBUG(5, ("share_mode_cleanup_disconnected: "
617 "entry for file "
618 "(file-id='%s', servicepath='%s', "
619 "base_name='%s%s%s') "
620 "has share_file_id %llu but expected %llu"
621 "==> do not cleanup\n",
622 file_id_string(frame, &fid),
623 data->servicepath,
624 data->base_name,
625 (data->stream_name == NULL)
626 ? "" : "', stream_name='",
627 (data->stream_name == NULL)
628 ? "" : data->stream_name,
629 (unsigned long long)entry->share_file_id,
630 (unsigned long long)open_persistent_id));
631 goto done;
635 for (n=0; n < data->num_leases; n++) {
636 struct share_mode_lease *l = &data->leases[n];
637 NTSTATUS status;
639 status = leases_db_del(&l->client_guid, &l->lease_key, &fid);
641 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__,
642 nt_errstr(status)));
645 ok = brl_cleanup_disconnected(fid, open_persistent_id);
646 if (!ok) {
647 DEBUG(10, ("share_mode_cleanup_disconnected: "
648 "failed to clean up byte range locks associated "
649 "with file (file-id='%s', servicepath='%s', "
650 "base_name='%s%s%s') and open_persistent_id %llu "
651 "==> do not cleanup\n",
652 file_id_string(frame, &fid),
653 data->servicepath,
654 data->base_name,
655 (data->stream_name == NULL)
656 ? "" : "', stream_name='",
657 (data->stream_name == NULL)
658 ? "" : data->stream_name,
659 (unsigned long long)open_persistent_id));
660 goto done;
663 DEBUG(10, ("share_mode_cleanup_disconnected: "
664 "cleaning up %u entries for file "
665 "(file-id='%s', servicepath='%s', "
666 "base_name='%s%s%s') "
667 "from open_persistent_id %llu\n",
668 data->num_share_modes,
669 file_id_string(frame, &fid),
670 data->servicepath,
671 data->base_name,
672 (data->stream_name == NULL)
673 ? "" : "', stream_name='",
674 (data->stream_name == NULL)
675 ? "" : data->stream_name,
676 (unsigned long long)open_persistent_id));
678 data->num_share_modes = 0;
679 data->num_leases = 0;
680 data->modified = true;
682 ret = true;
683 done:
684 talloc_free(frame);
685 return ret;