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"
51 #define DBGC_CLASS DBGC_LOCKING
53 #define NO_LOCKING_COUNT (-1)
55 /* the locking database handle */
56 static struct db_context *lock_db;
58 static bool locking_init_internal(bool read_only)
65 lock_db = db_open(NULL, lock_path("locking.tdb"),
66 lp_open_files_db_hash_size(),
67 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
68 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
72 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
76 if (!posix_locking_init(read_only))
82 bool locking_init(void)
84 return locking_init_internal(false);
87 bool locking_init_readonly(void)
89 return locking_init_internal(true);
92 /*******************************************************************
93 Deinitialize the share_mode management.
94 ******************************************************************/
96 bool locking_end(void)
103 /*******************************************************************
104 Form a static locking key for a dev/inode pair.
105 ******************************************************************/
107 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
110 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
113 /*******************************************************************
114 Get all share mode entries for a dev/inode pair.
115 ********************************************************************/
117 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
120 struct share_mode_data *d;
122 struct server_id *pids;
124 enum ndr_err_code ndr_err;
127 d = talloc(mem_ctx, struct share_mode_data);
129 DEBUG(0, ("talloc failed\n"));
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\n"));
146 if (DEBUGLEVEL >= 10) {
147 DEBUG(10, ("parse_share_modes:\n"));
148 NDR_PRINT_DEBUG(share_mode_data, d);
152 * Ensure that each entry has a real process attached.
155 pids = talloc_array(talloc_tos(), struct server_id,
158 DEBUG(0, ("talloc failed\n"));
161 pid_exists = talloc_array(talloc_tos(), bool, d->num_share_modes);
162 if (pid_exists == NULL) {
163 DEBUG(0, ("talloc failed\n"));
167 for (i=0; i<d->num_share_modes; i++) {
168 pids[i] = d->share_modes[i].pid;
170 if (!serverids_exist(pids, d->num_share_modes, pid_exists)) {
171 DEBUG(0, ("serverid_exists failed\n"));
176 while (i < d->num_share_modes) {
177 struct share_mode_entry *e = &d->share_modes[i];
178 if (!pid_exists[i]) {
179 DEBUG(10, ("wipe non-existent pid %s\n",
180 procid_str_static(&e->pid)));
181 *e = d->share_modes[d->num_share_modes-1];
182 d->num_share_modes -= 1;
188 TALLOC_FREE(pid_exists);
196 /*******************************************************************
197 Create a storable data blob from a modified share_mode_data struct.
198 ********************************************************************/
200 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
203 enum ndr_err_code ndr_err;
205 if (DEBUGLEVEL >= 10) {
206 DEBUG(10, ("unparse_share_modes:\n"));
207 NDR_PRINT_DEBUG(share_mode_data, d);
210 if (d->num_share_modes == 0) {
211 DEBUG(10, ("No used share mode found\n"));
212 return make_tdb_data(NULL, 0);
215 ndr_err = ndr_push_struct_blob(
216 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
217 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
218 smb_panic("ndr_push_share_mode_lock failed");
221 return make_tdb_data(blob.data, blob.length);
224 /*******************************************************************
225 If modified, store the share_mode_data back into the database.
226 ********************************************************************/
228 static int share_mode_data_destructor(struct share_mode_data *d)
237 data = unparse_share_modes(d);
239 if (data.dptr == NULL) {
241 /* There has been an entry before, delete it */
243 status = dbwrap_record_delete(d->record);
244 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(0, ("delete_rec returned %s\n",
250 if (asprintf(&errmsg, "could not delete share "
252 nt_errstr(status)) == -1) {
253 smb_panic("could not delete share"
262 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
263 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
268 if (asprintf(&errmsg, "could not store share mode entry: %s",
269 nt_errstr(status)) == -1) {
270 smb_panic("could not store share mode entry");
280 /*******************************************************************
281 Allocate a new share_mode_data struct, mark it unmodified.
282 fresh is set to note that currently there is no database entry.
283 ********************************************************************/
285 static struct share_mode_data *fresh_share_mode_lock(
286 TALLOC_CTX *mem_ctx, const char *servicepath,
287 const struct smb_filename *smb_fname,
288 const struct timespec *old_write_time)
290 struct share_mode_data *d;
292 if ((servicepath == NULL) || (smb_fname == NULL) ||
293 (old_write_time == NULL)) {
297 d = talloc_zero(mem_ctx, struct share_mode_data);
301 d->base_name = talloc_strdup(d, smb_fname->base_name);
302 if (d->base_name == NULL) {
305 if (smb_fname->stream_name != NULL) {
306 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
307 if (d->stream_name == NULL) {
311 d->servicepath = talloc_strdup(d, servicepath);
312 if (d->servicepath == NULL) {
315 d->old_write_time = *old_write_time;
320 DEBUG(0, ("talloc failed\n"));
325 /*******************************************************************
326 Either fetch a share mode from the database, or allocate a fresh
327 one if the record doesn't exist.
328 ********************************************************************/
330 static struct share_mode_lock *get_share_mode_lock_internal(
331 TALLOC_CTX *mem_ctx, const struct file_id id,
332 const char *servicepath, const struct smb_filename *smb_fname,
333 const struct timespec *old_write_time)
335 struct share_mode_lock *lck;
336 struct share_mode_data *d;
338 struct db_record *rec;
339 TDB_DATA key = locking_key(&id, &tmp);
342 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
344 DEBUG(3, ("Could not lock share entry\n"));
348 value = dbwrap_record_get_value(rec);
350 if (value.dptr == NULL) {
351 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
354 d = parse_share_modes(mem_ctx, value);
358 DEBUG(5, ("get_share_mode_lock_internal: "
359 "Could not get share mode lock\n"));
364 d->record = talloc_move(d, &rec);
365 talloc_set_destructor(d, share_mode_data_destructor);
367 lck = talloc(mem_ctx, struct share_mode_lock);
369 DEBUG(1, ("talloc failed\n"));
373 lck->data = talloc_move(lck, &d);
378 * We can only ever have one share mode locked. Users of
379 * get_share_mode_lock never see this, it will be refcounted by
382 static struct share_mode_lock *the_lock;
384 static int the_lock_destructor(struct share_mode_lock *l)
390 /*******************************************************************
391 Get a share_mode_lock, Reference counted to allow nexted calls.
392 ********************************************************************/
394 struct share_mode_lock *get_share_mode_lock(
396 const struct file_id id,
397 const char *servicepath,
398 const struct smb_filename *smb_fname,
399 const struct timespec *old_write_time)
401 TALLOC_CTX *frame = talloc_stackframe();
403 struct share_mode_lock *lck;
405 if (the_lock == NULL) {
406 the_lock = get_share_mode_lock_internal(
407 frame, id, servicepath, smb_fname, old_write_time);
408 if (the_lock == NULL) {
411 talloc_set_destructor(the_lock, the_lock_destructor);
413 if (!file_id_equal(&the_lock->data->id, &id)) {
414 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
417 lck = talloc(mem_ctx, struct share_mode_lock);
419 DEBUG(1, ("talloc failed\n"));
422 if (talloc_reference(lck, the_lock) == NULL) {
423 DEBUG(1, ("talloc_reference failed\n"));
426 lck->data = the_lock->data;
434 /*******************************************************************
435 Get a share_mode_lock without locking the database or reference
436 counting. Used by smbstatus to display existing share modes.
437 ********************************************************************/
439 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
440 const struct file_id id)
442 struct share_mode_lock *lck;
444 TDB_DATA key = locking_key(&id, &tmp);
448 status = dbwrap_fetch(lock_db, talloc_tos(), key, &data);
449 if (!NT_STATUS_IS_OK(status)) {
450 DEBUG(3, ("Could not fetch share entry\n"));
453 if (data.dptr == NULL) {
456 lck = talloc(mem_ctx, struct share_mode_lock);
458 TALLOC_FREE(data.dptr);
461 lck->data = parse_share_modes(lck, data);
462 TALLOC_FREE(data.dptr);
463 if (lck->data == NULL) {
470 struct forall_state {
471 void (*fn)(const struct share_mode_entry *entry,
472 const char *sharepath,
478 static int traverse_fn(struct db_record *rec, void *_state)
480 struct forall_state *state = (struct forall_state *)_state;
485 enum ndr_err_code ndr_err;
486 struct share_mode_data *d;
488 key = dbwrap_record_get_key(rec);
489 value = dbwrap_record_get_value(rec);
491 /* Ensure this is a locking_key record. */
492 if (key.dsize != sizeof(struct file_id))
495 d = talloc(talloc_tos(), struct share_mode_data);
500 blob.data = value.dptr;
501 blob.length = value.dsize;
503 ndr_err = ndr_pull_struct_blob(
504 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
505 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
506 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
509 for (i=0; i<d->num_share_modes; i++) {
510 state->fn(&d->share_modes[i],
511 d->servicepath, d->base_name,
512 state->private_data);
519 /*******************************************************************
520 Call the specified function on each entry under management by the
522 ********************************************************************/
524 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
525 const char *, void *),
528 struct forall_state state;
536 state.private_data = private_data;
538 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
541 if (!NT_STATUS_IS_OK(status)) {