wafsamba: remove unused variable from copy_and_fix_python_path
[Samba.git] / source3 / locking / share_mode_lock.c
blob4f049bd68a9cab6e37bd6cd57aed356b54e2b22a
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 lp_open_files_db_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);
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;
174 uint32_t i;
176 if (DEBUGLEVEL >= 10) {
177 DEBUG(10, ("unparse_share_modes:\n"));
178 NDR_PRINT_DEBUG(share_mode_data, d);
181 i = 0;
182 while (i < d->num_share_modes) {
183 if (d->share_modes[i].stale) {
185 * Remove the stale entries before storing
187 struct share_mode_entry *m = d->share_modes;
188 m[i] = m[d->num_share_modes-1];
189 d->num_share_modes -= 1;
190 } else {
191 i += 1;
195 if (d->num_share_modes == 0) {
196 DEBUG(10, ("No used share mode found\n"));
197 return make_tdb_data(NULL, 0);
200 ndr_err = ndr_push_struct_blob(
201 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
202 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
203 smb_panic("ndr_push_share_mode_lock failed");
206 return make_tdb_data(blob.data, blob.length);
209 /*******************************************************************
210 If modified, store the share_mode_data back into the database.
211 ********************************************************************/
213 static int share_mode_data_destructor(struct share_mode_data *d)
215 NTSTATUS status;
216 TDB_DATA data;
218 if (!d->modified) {
219 return 0;
222 data = unparse_share_modes(d);
224 if (data.dptr == NULL) {
225 if (!d->fresh) {
226 /* There has been an entry before, delete it */
228 status = dbwrap_record_delete(d->record);
229 if (!NT_STATUS_IS_OK(status)) {
230 char *errmsg;
232 DEBUG(0, ("delete_rec returned %s\n",
233 nt_errstr(status)));
235 if (asprintf(&errmsg, "could not delete share "
236 "entry: %s\n",
237 nt_errstr(status)) == -1) {
238 smb_panic("could not delete share"
239 "entry");
241 smb_panic(errmsg);
244 goto done;
247 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
248 if (!NT_STATUS_IS_OK(status)) {
249 char *errmsg;
251 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
253 if (asprintf(&errmsg, "could not store share mode entry: %s",
254 nt_errstr(status)) == -1) {
255 smb_panic("could not store share mode entry");
257 smb_panic(errmsg);
260 done:
262 return 0;
265 /*******************************************************************
266 Allocate a new share_mode_data struct, mark it unmodified.
267 fresh is set to note that currently there is no database entry.
268 ********************************************************************/
270 static struct share_mode_data *fresh_share_mode_lock(
271 TALLOC_CTX *mem_ctx, const char *servicepath,
272 const struct smb_filename *smb_fname,
273 const struct timespec *old_write_time)
275 struct share_mode_data *d;
277 if ((servicepath == NULL) || (smb_fname == NULL) ||
278 (old_write_time == NULL)) {
279 return NULL;
282 d = talloc_zero(mem_ctx, struct share_mode_data);
283 if (d == NULL) {
284 goto fail;
286 d->base_name = talloc_strdup(d, smb_fname->base_name);
287 if (d->base_name == NULL) {
288 goto fail;
290 if (smb_fname->stream_name != NULL) {
291 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
292 if (d->stream_name == NULL) {
293 goto fail;
296 d->servicepath = talloc_strdup(d, servicepath);
297 if (d->servicepath == NULL) {
298 goto fail;
300 d->old_write_time = *old_write_time;
301 d->modified = false;
302 d->fresh = true;
303 return d;
304 fail:
305 DEBUG(0, ("talloc failed\n"));
306 TALLOC_FREE(d);
307 return NULL;
310 /*******************************************************************
311 Either fetch a share mode from the database, or allocate a fresh
312 one if the record doesn't exist.
313 ********************************************************************/
315 static struct share_mode_lock *get_share_mode_lock_internal(
316 TALLOC_CTX *mem_ctx, struct file_id id,
317 const char *servicepath, const struct smb_filename *smb_fname,
318 const struct timespec *old_write_time)
320 struct share_mode_lock *lck;
321 struct share_mode_data *d;
322 struct db_record *rec;
323 TDB_DATA key = locking_key(&id);
324 TDB_DATA value;
326 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
327 if (rec == NULL) {
328 DEBUG(3, ("Could not lock share entry\n"));
329 return NULL;
332 value = dbwrap_record_get_value(rec);
334 if (value.dptr == NULL) {
335 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
336 old_write_time);
337 } else {
338 d = parse_share_modes(mem_ctx, value);
341 if (d == NULL) {
342 DEBUG(5, ("get_share_mode_lock_internal: "
343 "Could not get share mode lock\n"));
344 TALLOC_FREE(rec);
345 return NULL;
347 d->id = id;
348 d->record = talloc_move(d, &rec);
349 talloc_set_destructor(d, share_mode_data_destructor);
351 lck = talloc(mem_ctx, struct share_mode_lock);
352 if (lck == NULL) {
353 DEBUG(1, ("talloc failed\n"));
354 TALLOC_FREE(d);
355 return NULL;
357 lck->data = talloc_move(lck, &d);
358 return lck;
362 * We can only ever have one share mode locked. Users of
363 * get_share_mode_lock never see this, it will be refcounted by
364 * talloc_reference.
366 static struct share_mode_lock *the_lock;
368 static int the_lock_destructor(struct share_mode_lock *l)
370 the_lock = NULL;
371 return 0;
374 /*******************************************************************
375 Get a share_mode_lock, Reference counted to allow nested calls.
376 ********************************************************************/
378 struct share_mode_lock *get_share_mode_lock(
379 TALLOC_CTX *mem_ctx,
380 struct file_id id,
381 const char *servicepath,
382 const struct smb_filename *smb_fname,
383 const struct timespec *old_write_time)
385 TALLOC_CTX *frame = talloc_stackframe();
387 struct share_mode_lock *lck;
389 if (the_lock == NULL) {
390 the_lock = get_share_mode_lock_internal(
391 frame, id, servicepath, smb_fname, old_write_time);
392 if (the_lock == NULL) {
393 goto fail;
395 talloc_set_destructor(the_lock, the_lock_destructor);
397 if (!file_id_equal(&the_lock->data->id, &id)) {
398 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
399 goto fail;
401 lck = talloc(mem_ctx, struct share_mode_lock);
402 if (lck == NULL) {
403 DEBUG(1, ("talloc failed\n"));
404 goto fail;
406 if (talloc_reference(lck, the_lock) == NULL) {
407 DEBUG(1, ("talloc_reference failed\n"));
408 goto fail;
410 lck->data = the_lock->data;
411 TALLOC_FREE(frame);
412 return lck;
413 fail:
414 TALLOC_FREE(frame);
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 forall_state {
455 void (*fn)(const struct share_mode_entry *entry,
456 const char *sharepath,
457 const char *fname,
458 void *private_data);
459 void *private_data;
462 static int traverse_fn(struct db_record *rec, void *_state)
464 struct forall_state *state = (struct forall_state *)_state;
465 uint32_t i;
466 TDB_DATA key;
467 TDB_DATA value;
468 DATA_BLOB blob;
469 enum ndr_err_code ndr_err;
470 struct share_mode_data *d;
472 key = dbwrap_record_get_key(rec);
473 value = dbwrap_record_get_value(rec);
475 /* Ensure this is a locking_key record. */
476 if (key.dsize != sizeof(struct file_id))
477 return 0;
479 d = talloc(talloc_tos(), struct share_mode_data);
480 if (d == NULL) {
481 return 0;
484 blob.data = value.dptr;
485 blob.length = value.dsize;
487 ndr_err = ndr_pull_struct_blob(
488 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
489 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
490 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
491 return 0;
493 if (DEBUGLEVEL > 10) {
494 DEBUG(11, ("parse_share_modes:\n"));
495 NDR_PRINT_DEBUG(share_mode_data, d);
497 for (i=0; i<d->num_share_modes; i++) {
498 d->share_modes[i].stale = false; /* [skip] in idl */
499 state->fn(&d->share_modes[i],
500 d->servicepath, d->base_name,
501 state->private_data);
503 TALLOC_FREE(d);
505 return 0;
508 /*******************************************************************
509 Call the specified function on each entry under management by the
510 share mode system.
511 ********************************************************************/
513 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
514 const char *, void *),
515 void *private_data)
517 struct forall_state state;
518 NTSTATUS status;
519 int count;
521 if (lock_db == NULL)
522 return 0;
524 state.fn = fn;
525 state.private_data = private_data;
527 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
528 &count);
530 if (!NT_STATUS_IS_OK(status)) {
531 return -1;
532 } else {
533 return count;
537 bool share_mode_cleanup_disconnected(struct file_id fid,
538 uint64_t open_persistent_id)
540 bool ret = false;
541 TALLOC_CTX *frame = talloc_stackframe();
542 unsigned n;
543 struct share_mode_data *data;
544 struct share_mode_lock *lck;
545 bool ok;
547 lck = get_existing_share_mode_lock(frame, fid);
548 if (lck == NULL) {
549 DEBUG(5, ("share_mode_cleanup_disconnected: "
550 "Could not fetch share mode entry for %s\n",
551 file_id_string(frame, &fid)));
552 goto done;
554 data = lck->data;
556 for (n=0; n < data->num_share_modes; n++) {
557 struct share_mode_entry *entry = &data->share_modes[n];
559 if (!server_id_is_disconnected(&entry->pid)) {
560 DEBUG(5, ("share_mode_cleanup_disconnected: "
561 "file (file-id='%s', servicepath='%s', "
562 "base_name='%s%s%s') "
563 "is used by server %s ==> do not cleanup\n",
564 file_id_string(frame, &fid),
565 data->servicepath,
566 data->base_name,
567 (data->stream_name == NULL)
568 ? "" : "', stream_name='",
569 (data->stream_name == NULL)
570 ? "" : data->stream_name,
571 server_id_str(frame, &entry->pid)));
572 goto done;
574 if (open_persistent_id != entry->share_file_id) {
575 DEBUG(5, ("share_mode_cleanup_disconnected: "
576 "entry for file "
577 "(file-id='%s', servicepath='%s', "
578 "base_name='%s%s%s') "
579 "has share_file_id %llu but expected %llu"
580 "==> do not cleanup\n",
581 file_id_string(frame, &fid),
582 data->servicepath,
583 data->base_name,
584 (data->stream_name == NULL)
585 ? "" : "', stream_name='",
586 (data->stream_name == NULL)
587 ? "" : data->stream_name,
588 (unsigned long long)entry->share_file_id,
589 (unsigned long long)open_persistent_id));
590 goto done;
594 ok = brl_cleanup_disconnected(fid, open_persistent_id);
595 if (!ok) {
596 DEBUG(10, ("share_mode_cleanup_disconnected: "
597 "failed to clean up byte range locks associated "
598 "with file (file-id='%s', servicepath='%s', "
599 "base_name='%s%s%s') and open_persistent_id %llu "
600 "==> do not cleanup\n",
601 file_id_string(frame, &fid),
602 data->servicepath,
603 data->base_name,
604 (data->stream_name == NULL)
605 ? "" : "', stream_name='",
606 (data->stream_name == NULL)
607 ? "" : data->stream_name,
608 (unsigned long long)open_persistent_id));
609 goto done;
612 DEBUG(10, ("share_mode_cleanup_disconnected: "
613 "cleaning up %u entries for file "
614 "(file-id='%s', servicepath='%s', "
615 "base_name='%s%s%s') "
616 "from open_persistent_id %llu\n",
617 data->num_share_modes,
618 file_id_string(frame, &fid),
619 data->servicepath,
620 data->base_name,
621 (data->stream_name == NULL)
622 ? "" : "', stream_name='",
623 (data->stream_name == NULL)
624 ? "" : data->stream_name,
625 (unsigned long long)open_persistent_id));
627 data->num_share_modes = 0;
628 data->modified = true;
630 ret = true;
631 done:
632 talloc_free(frame);
633 return ret;