s3:smb2_notify: make use of smbd_smb2_generate_outbody()
[Samba.git] / source3 / locking / share_mode_lock.c
blob5d0874c3be1301e86abb10ba70371775e4af9c82
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"
51 #undef DBGC_CLASS
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)
61 brl_init(read_only);
63 if (lock_db)
64 return True;
66 lock_db = db_open(NULL, lock_path("locking.tdb"),
67 SMB_OPEN_DATABASE_TDB_HASH_SIZE,
68 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
69 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
70 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
72 if (!lock_db) {
73 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
74 return False;
77 if (!posix_locking_init(read_only))
78 return False;
80 dbwrap_watch_db(lock_db, server_messaging_context());
82 return True;
85 bool locking_init(void)
87 return locking_init_internal(false);
90 bool locking_init_readonly(void)
92 return locking_init_internal(true);
95 /*******************************************************************
96 Deinitialize the share_mode management.
97 ******************************************************************/
99 bool locking_end(void)
101 brl_shutdown();
102 TALLOC_FREE(lock_db);
103 return true;
106 /*******************************************************************
107 Form a static locking key for a dev/inode pair.
108 ******************************************************************/
110 static TDB_DATA locking_key(const struct file_id *id)
112 return make_tdb_data((const uint8_t *)id, sizeof(*id));
115 /*******************************************************************
116 Get all share mode entries for a dev/inode pair.
117 ********************************************************************/
119 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
120 const TDB_DATA dbuf)
122 struct share_mode_data *d;
123 enum ndr_err_code ndr_err;
124 uint32_t i;
125 DATA_BLOB blob;
127 d = talloc(mem_ctx, struct share_mode_data);
128 if (d == NULL) {
129 DEBUG(0, ("talloc failed\n"));
130 goto fail;
133 blob.data = dbuf.dptr;
134 blob.length = dbuf.dsize;
136 ndr_err = ndr_pull_struct_blob(
137 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
138 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
139 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
140 ndr_errstr(ndr_err)));
141 goto fail;
145 * Initialize the values that are [skip] in the idl. The NDR code does
146 * not initialize them.
149 for (i=0; i<d->num_share_modes; i++) {
150 d->share_modes[i].stale = false;
152 d->modified = false;
153 d->fresh = false;
155 if (DEBUGLEVEL >= 10) {
156 DEBUG(10, ("parse_share_modes:\n"));
157 NDR_PRINT_DEBUG(share_mode_data, d);
160 return d;
161 fail:
162 TALLOC_FREE(d);
163 return NULL;
166 /*******************************************************************
167 Create a storable data blob from a modified share_mode_data struct.
168 ********************************************************************/
170 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
172 DATA_BLOB blob;
173 enum ndr_err_code ndr_err;
175 if (DEBUGLEVEL >= 10) {
176 DEBUG(10, ("unparse_share_modes:\n"));
177 NDR_PRINT_DEBUG(share_mode_data, d);
180 remove_stale_share_mode_entries(d);
182 if (d->num_share_modes == 0) {
183 DEBUG(10, ("No used share mode found\n"));
184 return make_tdb_data(NULL, 0);
187 ndr_err = ndr_push_struct_blob(
188 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
189 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
190 smb_panic("ndr_push_share_mode_lock failed");
193 return make_tdb_data(blob.data, blob.length);
196 /*******************************************************************
197 If modified, store the share_mode_data back into the database.
198 ********************************************************************/
200 static int share_mode_data_destructor(struct share_mode_data *d)
202 NTSTATUS status;
203 TDB_DATA data;
205 if (!d->modified) {
206 return 0;
209 data = unparse_share_modes(d);
211 if (data.dptr == NULL) {
212 if (!d->fresh) {
213 /* There has been an entry before, delete it */
215 status = dbwrap_record_delete(d->record);
216 if (!NT_STATUS_IS_OK(status)) {
217 char *errmsg;
219 DEBUG(0, ("delete_rec returned %s\n",
220 nt_errstr(status)));
222 if (asprintf(&errmsg, "could not delete share "
223 "entry: %s\n",
224 nt_errstr(status)) == -1) {
225 smb_panic("could not delete share"
226 "entry");
228 smb_panic(errmsg);
231 goto done;
234 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
235 if (!NT_STATUS_IS_OK(status)) {
236 char *errmsg;
238 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
240 if (asprintf(&errmsg, "could not store share mode entry: %s",
241 nt_errstr(status)) == -1) {
242 smb_panic("could not store share mode entry");
244 smb_panic(errmsg);
247 done:
249 return 0;
252 /*******************************************************************
253 Allocate a new share_mode_data struct, mark it unmodified.
254 fresh is set to note that currently there is no database entry.
255 ********************************************************************/
257 static struct share_mode_data *fresh_share_mode_lock(
258 TALLOC_CTX *mem_ctx, const char *servicepath,
259 const struct smb_filename *smb_fname,
260 const struct timespec *old_write_time)
262 struct share_mode_data *d;
264 if ((servicepath == NULL) || (smb_fname == NULL) ||
265 (old_write_time == NULL)) {
266 return NULL;
269 d = talloc_zero(mem_ctx, struct share_mode_data);
270 if (d == NULL) {
271 goto fail;
273 d->base_name = talloc_strdup(d, smb_fname->base_name);
274 if (d->base_name == NULL) {
275 goto fail;
277 if (smb_fname->stream_name != NULL) {
278 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
279 if (d->stream_name == NULL) {
280 goto fail;
283 d->servicepath = talloc_strdup(d, servicepath);
284 if (d->servicepath == NULL) {
285 goto fail;
287 d->old_write_time = *old_write_time;
288 d->modified = false;
289 d->fresh = true;
290 return d;
291 fail:
292 DEBUG(0, ("talloc failed\n"));
293 TALLOC_FREE(d);
294 return NULL;
297 /*******************************************************************
298 Either fetch a share mode from the database, or allocate a fresh
299 one if the record doesn't exist.
300 ********************************************************************/
302 static struct share_mode_lock *get_share_mode_lock_internal(
303 TALLOC_CTX *mem_ctx, struct file_id id,
304 const char *servicepath, const struct smb_filename *smb_fname,
305 const struct timespec *old_write_time)
307 struct share_mode_lock *lck;
308 struct share_mode_data *d;
309 struct db_record *rec;
310 TDB_DATA key = locking_key(&id);
311 TDB_DATA value;
313 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
314 if (rec == NULL) {
315 DEBUG(3, ("Could not lock share entry\n"));
316 return NULL;
319 value = dbwrap_record_get_value(rec);
321 if (value.dptr == NULL) {
322 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
323 old_write_time);
324 } else {
325 d = parse_share_modes(mem_ctx, value);
328 if (d == NULL) {
329 DEBUG(5, ("get_share_mode_lock_internal: "
330 "Could not get share mode lock\n"));
331 TALLOC_FREE(rec);
332 return NULL;
334 d->id = id;
335 d->record = talloc_move(d, &rec);
336 talloc_set_destructor(d, share_mode_data_destructor);
338 lck = talloc(mem_ctx, struct share_mode_lock);
339 if (lck == NULL) {
340 DEBUG(1, ("talloc failed\n"));
341 TALLOC_FREE(d);
342 return NULL;
344 lck->data = talloc_move(lck, &d);
345 return lck;
349 * We can only ever have one share mode locked. Users of
350 * get_share_mode_lock never see this, it will be refcounted by
351 * talloc_reference.
353 static struct share_mode_lock *the_lock;
355 static int the_lock_destructor(struct share_mode_lock *l)
357 the_lock = NULL;
358 return 0;
361 /*******************************************************************
362 Get a share_mode_lock, Reference counted to allow nested calls.
363 ********************************************************************/
365 struct share_mode_lock *get_share_mode_lock(
366 TALLOC_CTX *mem_ctx,
367 struct file_id id,
368 const char *servicepath,
369 const struct smb_filename *smb_fname,
370 const struct timespec *old_write_time)
372 struct share_mode_lock *lck;
374 lck = talloc(mem_ctx, struct share_mode_lock);
375 if (lck == NULL) {
376 DEBUG(1, ("talloc failed\n"));
377 return NULL;
380 if (the_lock == NULL) {
381 the_lock = get_share_mode_lock_internal(
382 lck, id, servicepath, smb_fname, old_write_time);
383 if (the_lock == NULL) {
384 goto fail;
386 talloc_set_destructor(the_lock, the_lock_destructor);
387 } else {
388 if (talloc_reference(lck, the_lock) == NULL) {
389 DEBUG(1, ("talloc_reference failed\n"));
390 goto fail;
393 if (!file_id_equal(&the_lock->data->id, &id)) {
394 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
395 goto fail;
397 lck->data = the_lock->data;
398 return lck;
399 fail:
400 TALLOC_FREE(lck);
401 return NULL;
404 static void fetch_share_mode_unlocked_parser(
405 TDB_DATA key, TDB_DATA data, void *private_data)
407 struct share_mode_lock *lck = talloc_get_type_abort(
408 private_data, struct share_mode_lock);
410 lck->data = parse_share_modes(lck, data);
413 /*******************************************************************
414 Get a share_mode_lock without locking the database or reference
415 counting. Used by smbstatus to display existing share modes.
416 ********************************************************************/
418 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
419 struct file_id id)
421 struct share_mode_lock *lck;
422 TDB_DATA key = locking_key(&id);
423 NTSTATUS status;
425 lck = talloc(mem_ctx, struct share_mode_lock);
426 if (lck == NULL) {
427 DEBUG(0, ("talloc failed\n"));
428 return NULL;
430 status = dbwrap_parse_record(
431 lock_db, key, fetch_share_mode_unlocked_parser, lck);
432 if (!NT_STATUS_IS_OK(status) ||
433 (lck->data == NULL)) {
434 TALLOC_FREE(lck);
435 return NULL;
437 return lck;
440 struct forall_state {
441 void (*fn)(const struct share_mode_entry *entry,
442 const char *sharepath,
443 const char *fname,
444 void *private_data);
445 void *private_data;
448 static int traverse_fn(struct db_record *rec, void *_state)
450 struct forall_state *state = (struct forall_state *)_state;
451 uint32_t i;
452 TDB_DATA key;
453 TDB_DATA value;
454 DATA_BLOB blob;
455 enum ndr_err_code ndr_err;
456 struct share_mode_data *d;
458 key = dbwrap_record_get_key(rec);
459 value = dbwrap_record_get_value(rec);
461 /* Ensure this is a locking_key record. */
462 if (key.dsize != sizeof(struct file_id))
463 return 0;
465 d = talloc(talloc_tos(), struct share_mode_data);
466 if (d == NULL) {
467 return 0;
470 blob.data = value.dptr;
471 blob.length = value.dsize;
473 ndr_err = ndr_pull_struct_blob(
474 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
475 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
476 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
477 return 0;
479 if (DEBUGLEVEL > 10) {
480 DEBUG(11, ("parse_share_modes:\n"));
481 NDR_PRINT_DEBUG(share_mode_data, d);
483 for (i=0; i<d->num_share_modes; i++) {
484 state->fn(&d->share_modes[i],
485 d->servicepath, d->base_name,
486 state->private_data);
488 TALLOC_FREE(d);
490 return 0;
493 /*******************************************************************
494 Call the specified function on each entry under management by the
495 share mode system.
496 ********************************************************************/
498 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
499 const char *, void *),
500 void *private_data)
502 struct forall_state state;
503 NTSTATUS status;
504 int count;
506 if (lock_db == NULL)
507 return 0;
509 state.fn = fn;
510 state.private_data = private_data;
512 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
513 &count);
515 if (!NT_STATUS_IS_OK(status)) {
516 return -1;
517 } else {
518 return count;
522 bool share_mode_cleanup_disconnected(struct file_id fid,
523 uint64_t open_persistent_id)
525 bool ret = false;
526 TALLOC_CTX *frame = talloc_stackframe();
527 unsigned n;
528 struct share_mode_data *data;
529 struct share_mode_lock *lck;
530 bool ok;
532 lck = get_existing_share_mode_lock(frame, fid);
533 if (lck == NULL) {
534 DEBUG(5, ("share_mode_cleanup_disconnected: "
535 "Could not fetch share mode entry for %s\n",
536 file_id_string(frame, &fid)));
537 goto done;
539 data = lck->data;
541 for (n=0; n < data->num_share_modes; n++) {
542 struct share_mode_entry *entry = &data->share_modes[n];
544 if (!server_id_is_disconnected(&entry->pid)) {
545 DEBUG(5, ("share_mode_cleanup_disconnected: "
546 "file (file-id='%s', servicepath='%s', "
547 "base_name='%s%s%s') "
548 "is used by server %s ==> do not cleanup\n",
549 file_id_string(frame, &fid),
550 data->servicepath,
551 data->base_name,
552 (data->stream_name == NULL)
553 ? "" : "', stream_name='",
554 (data->stream_name == NULL)
555 ? "" : data->stream_name,
556 server_id_str(frame, &entry->pid)));
557 goto done;
559 if (open_persistent_id != entry->share_file_id) {
560 DEBUG(5, ("share_mode_cleanup_disconnected: "
561 "entry for file "
562 "(file-id='%s', servicepath='%s', "
563 "base_name='%s%s%s') "
564 "has share_file_id %llu but expected %llu"
565 "==> do not cleanup\n",
566 file_id_string(frame, &fid),
567 data->servicepath,
568 data->base_name,
569 (data->stream_name == NULL)
570 ? "" : "', stream_name='",
571 (data->stream_name == NULL)
572 ? "" : data->stream_name,
573 (unsigned long long)entry->share_file_id,
574 (unsigned long long)open_persistent_id));
575 goto done;
579 ok = brl_cleanup_disconnected(fid, open_persistent_id);
580 if (!ok) {
581 DEBUG(10, ("share_mode_cleanup_disconnected: "
582 "failed to clean up byte range locks associated "
583 "with file (file-id='%s', servicepath='%s', "
584 "base_name='%s%s%s') and open_persistent_id %llu "
585 "==> do not cleanup\n",
586 file_id_string(frame, &fid),
587 data->servicepath,
588 data->base_name,
589 (data->stream_name == NULL)
590 ? "" : "', stream_name='",
591 (data->stream_name == NULL)
592 ? "" : data->stream_name,
593 (unsigned long long)open_persistent_id));
594 goto done;
597 DEBUG(10, ("share_mode_cleanup_disconnected: "
598 "cleaning up %u entries for file "
599 "(file-id='%s', servicepath='%s', "
600 "base_name='%s%s%s') "
601 "from open_persistent_id %llu\n",
602 data->num_share_modes,
603 file_id_string(frame, &fid),
604 data->servicepath,
605 data->base_name,
606 (data->stream_name == NULL)
607 ? "" : "', stream_name='",
608 (data->stream_name == NULL)
609 ? "" : data->stream_name,
610 (unsigned long long)open_persistent_id));
612 data->num_share_modes = 0;
613 data->modified = true;
615 ret = true;
616 done:
617 talloc_free(frame);
618 return ret;