s3:locking: no need to make a file_id passed by value a constant
[Samba.git] / source3 / locking / share_mode_lock.c
blob2d7ca39fc78c485680b3865cd3db4ab4ce2be5ed
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"
50 #undef DBGC_CLASS
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)
60 brl_init(read_only);
62 if (lock_db)
63 return True;
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,
69 DBWRAP_LOCK_ORDER_1);
71 if (!lock_db) {
72 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
73 return False;
76 if (!posix_locking_init(read_only))
77 return False;
79 return True;
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)
98 brl_shutdown();
99 TALLOC_FREE(lock_db);
100 return true;
103 /*******************************************************************
104 Form a static locking key for a dev/inode pair.
105 ******************************************************************/
107 static TDB_DATA locking_key(const struct file_id *id)
109 return make_tdb_data((const uint8_t *)id, sizeof(*id));
112 /*******************************************************************
113 Get all share mode entries for a dev/inode pair.
114 ********************************************************************/
116 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
117 const TDB_DATA dbuf)
119 struct share_mode_data *d;
120 enum ndr_err_code ndr_err;
121 DATA_BLOB blob;
123 d = talloc(mem_ctx, struct share_mode_data);
124 if (d == NULL) {
125 DEBUG(0, ("talloc failed\n"));
126 goto fail;
129 blob.data = dbuf.dptr;
130 blob.length = dbuf.dsize;
132 ndr_err = ndr_pull_struct_blob(
133 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
134 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
135 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
136 goto fail;
139 d->modified = false;
140 d->fresh = false;
142 if (DEBUGLEVEL >= 10) {
143 DEBUG(10, ("parse_share_modes:\n"));
144 NDR_PRINT_DEBUG(share_mode_data, d);
147 return d;
148 fail:
149 TALLOC_FREE(d);
150 return NULL;
153 /*******************************************************************
154 Create a storable data blob from a modified share_mode_data struct.
155 ********************************************************************/
157 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
159 DATA_BLOB blob;
160 enum ndr_err_code ndr_err;
162 if (DEBUGLEVEL >= 10) {
163 DEBUG(10, ("unparse_share_modes:\n"));
164 NDR_PRINT_DEBUG(share_mode_data, d);
167 if (d->num_share_modes == 0) {
168 DEBUG(10, ("No used share mode found\n"));
169 return make_tdb_data(NULL, 0);
172 ndr_err = ndr_push_struct_blob(
173 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
174 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
175 smb_panic("ndr_push_share_mode_lock failed");
178 return make_tdb_data(blob.data, blob.length);
181 /*******************************************************************
182 If modified, store the share_mode_data back into the database.
183 ********************************************************************/
185 static int share_mode_data_destructor(struct share_mode_data *d)
187 NTSTATUS status;
188 TDB_DATA data;
190 if (!d->modified) {
191 return 0;
194 data = unparse_share_modes(d);
196 if (data.dptr == NULL) {
197 if (!d->fresh) {
198 /* There has been an entry before, delete it */
200 status = dbwrap_record_delete(d->record);
201 if (!NT_STATUS_IS_OK(status)) {
202 char *errmsg;
204 DEBUG(0, ("delete_rec returned %s\n",
205 nt_errstr(status)));
207 if (asprintf(&errmsg, "could not delete share "
208 "entry: %s\n",
209 nt_errstr(status)) == -1) {
210 smb_panic("could not delete share"
211 "entry");
213 smb_panic(errmsg);
216 goto done;
219 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
220 if (!NT_STATUS_IS_OK(status)) {
221 char *errmsg;
223 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
225 if (asprintf(&errmsg, "could not store share mode entry: %s",
226 nt_errstr(status)) == -1) {
227 smb_panic("could not store share mode entry");
229 smb_panic(errmsg);
232 done:
234 return 0;
237 /*******************************************************************
238 Allocate a new share_mode_data struct, mark it unmodified.
239 fresh is set to note that currently there is no database entry.
240 ********************************************************************/
242 static struct share_mode_data *fresh_share_mode_lock(
243 TALLOC_CTX *mem_ctx, const char *servicepath,
244 const struct smb_filename *smb_fname,
245 const struct timespec *old_write_time)
247 struct share_mode_data *d;
249 if ((servicepath == NULL) || (smb_fname == NULL) ||
250 (old_write_time == NULL)) {
251 return NULL;
254 d = talloc_zero(mem_ctx, struct share_mode_data);
255 if (d == NULL) {
256 goto fail;
258 d->base_name = talloc_strdup(d, smb_fname->base_name);
259 if (d->base_name == NULL) {
260 goto fail;
262 if (smb_fname->stream_name != NULL) {
263 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
264 if (d->stream_name == NULL) {
265 goto fail;
268 d->servicepath = talloc_strdup(d, servicepath);
269 if (d->servicepath == NULL) {
270 goto fail;
272 d->old_write_time = *old_write_time;
273 d->modified = false;
274 d->fresh = true;
275 return d;
276 fail:
277 DEBUG(0, ("talloc failed\n"));
278 TALLOC_FREE(d);
279 return NULL;
282 /*******************************************************************
283 Either fetch a share mode from the database, or allocate a fresh
284 one if the record doesn't exist.
285 ********************************************************************/
287 static struct share_mode_lock *get_share_mode_lock_internal(
288 TALLOC_CTX *mem_ctx, struct file_id id,
289 const char *servicepath, const struct smb_filename *smb_fname,
290 const struct timespec *old_write_time)
292 struct share_mode_lock *lck;
293 struct share_mode_data *d;
294 struct db_record *rec;
295 TDB_DATA key = locking_key(&id);
296 TDB_DATA value;
298 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
299 if (rec == NULL) {
300 DEBUG(3, ("Could not lock share entry\n"));
301 return NULL;
304 value = dbwrap_record_get_value(rec);
306 if (value.dptr == NULL) {
307 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
308 old_write_time);
309 } else {
310 d = parse_share_modes(mem_ctx, value);
313 if (d == NULL) {
314 DEBUG(5, ("get_share_mode_lock_internal: "
315 "Could not get share mode lock\n"));
316 TALLOC_FREE(rec);
317 return NULL;
319 d->id = id;
320 d->record = talloc_move(d, &rec);
321 talloc_set_destructor(d, share_mode_data_destructor);
323 lck = talloc(mem_ctx, struct share_mode_lock);
324 if (lck == NULL) {
325 DEBUG(1, ("talloc failed\n"));
326 TALLOC_FREE(d);
327 return NULL;
329 lck->data = talloc_move(lck, &d);
330 return lck;
334 * We can only ever have one share mode locked. Users of
335 * get_share_mode_lock never see this, it will be refcounted by
336 * talloc_reference.
338 static struct share_mode_lock *the_lock;
340 static int the_lock_destructor(struct share_mode_lock *l)
342 the_lock = NULL;
343 return 0;
346 /*******************************************************************
347 Get a share_mode_lock, Reference counted to allow nested calls.
348 ********************************************************************/
350 struct share_mode_lock *get_share_mode_lock(
351 TALLOC_CTX *mem_ctx,
352 struct file_id id,
353 const char *servicepath,
354 const struct smb_filename *smb_fname,
355 const struct timespec *old_write_time)
357 TALLOC_CTX *frame = talloc_stackframe();
359 struct share_mode_lock *lck;
361 if (the_lock == NULL) {
362 the_lock = get_share_mode_lock_internal(
363 frame, id, servicepath, smb_fname, old_write_time);
364 if (the_lock == NULL) {
365 goto fail;
367 talloc_set_destructor(the_lock, the_lock_destructor);
369 if (!file_id_equal(&the_lock->data->id, &id)) {
370 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
371 goto fail;
373 lck = talloc(mem_ctx, struct share_mode_lock);
374 if (lck == NULL) {
375 DEBUG(1, ("talloc failed\n"));
376 goto fail;
378 if (talloc_reference(lck, the_lock) == NULL) {
379 DEBUG(1, ("talloc_reference failed\n"));
380 goto fail;
382 lck->data = the_lock->data;
383 TALLOC_FREE(frame);
384 return lck;
385 fail:
386 TALLOC_FREE(frame);
387 return NULL;
390 static void fetch_share_mode_unlocked_parser(
391 TDB_DATA key, TDB_DATA data, void *private_data)
393 struct share_mode_lock *lck = talloc_get_type_abort(
394 private_data, struct share_mode_lock);
396 lck->data = parse_share_modes(lck, data);
399 /*******************************************************************
400 Get a share_mode_lock without locking the database or reference
401 counting. Used by smbstatus to display existing share modes.
402 ********************************************************************/
404 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
405 struct file_id id)
407 struct share_mode_lock *lck;
408 TDB_DATA key = locking_key(&id);
409 NTSTATUS status;
411 lck = talloc(mem_ctx, struct share_mode_lock);
412 if (lck == NULL) {
413 DEBUG(0, ("talloc failed\n"));
414 return NULL;
416 status = dbwrap_parse_record(
417 lock_db, key, fetch_share_mode_unlocked_parser, lck);
418 if (!NT_STATUS_IS_OK(status) ||
419 (lck->data == NULL)) {
420 TALLOC_FREE(lck);
421 return NULL;
423 return lck;
426 struct forall_state {
427 void (*fn)(const struct share_mode_entry *entry,
428 const char *sharepath,
429 const char *fname,
430 void *private_data);
431 void *private_data;
434 static int traverse_fn(struct db_record *rec, void *_state)
436 struct forall_state *state = (struct forall_state *)_state;
437 uint32_t i;
438 TDB_DATA key;
439 TDB_DATA value;
440 DATA_BLOB blob;
441 enum ndr_err_code ndr_err;
442 struct share_mode_data *d;
444 key = dbwrap_record_get_key(rec);
445 value = dbwrap_record_get_value(rec);
447 /* Ensure this is a locking_key record. */
448 if (key.dsize != sizeof(struct file_id))
449 return 0;
451 d = talloc(talloc_tos(), struct share_mode_data);
452 if (d == NULL) {
453 return 0;
456 blob.data = value.dptr;
457 blob.length = value.dsize;
459 ndr_err = ndr_pull_struct_blob(
460 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
461 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
462 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
463 return 0;
465 for (i=0; i<d->num_share_modes; i++) {
466 state->fn(&d->share_modes[i],
467 d->servicepath, d->base_name,
468 state->private_data);
470 TALLOC_FREE(d);
472 return 0;
475 /*******************************************************************
476 Call the specified function on each entry under management by the
477 share mode system.
478 ********************************************************************/
480 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
481 const char *, void *),
482 void *private_data)
484 struct forall_state state;
485 NTSTATUS status;
486 int count;
488 if (lock_db == NULL)
489 return 0;
491 state.fn = fn;
492 state.private_data = private_data;
494 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
495 &count);
497 if (!NT_STATUS_IS_OK(status)) {
498 return -1;
499 } else {
500 return count;