s3: Move the share_mode_lock handling to its own file
[Samba/gebeck_regimport.git] / source3 / locking / share_mode_lock.c
blob1fb96998d704c0c62371573ca63ecb4557b329b1
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);
70 if (!lock_db) {
71 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
72 return False;
75 if (!posix_locking_init(read_only))
76 return False;
78 return True;
81 bool locking_init(void)
83 return locking_init_internal(false);
86 bool locking_init_readonly(void)
88 return locking_init_internal(true);
91 /*******************************************************************
92 Deinitialize the share_mode management.
93 ******************************************************************/
95 bool locking_end(void)
97 brl_shutdown();
98 TALLOC_FREE(lock_db);
99 return true;
102 /*******************************************************************
103 Form a static locking key for a dev/inode pair.
104 ******************************************************************/
106 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
108 *tmp = *id;
109 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
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 int i;
121 struct server_id *pids;
122 bool *pid_exists;
123 enum ndr_err_code ndr_err;
124 DATA_BLOB blob;
126 d = talloc_zero(mem_ctx, struct share_mode_data);
127 if (d == NULL) {
128 DEBUG(0, ("talloc failed\n"));
129 goto fail;
132 blob.data = dbuf.dptr;
133 blob.length = dbuf.dsize;
135 ndr_err = ndr_pull_struct_blob(
136 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
137 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
138 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
139 goto fail;
142 d->modified = false;
143 d->fresh = false;
145 if (DEBUGLEVEL >= 10) {
146 DEBUG(10, ("parse_share_modes:\n"));
147 NDR_PRINT_DEBUG(share_mode_data, d);
151 * Ensure that each entry has a real process attached.
154 pids = talloc_array(talloc_tos(), struct server_id,
155 d->num_share_modes);
156 if (pids == NULL) {
157 DEBUG(0, ("talloc failed\n"));
158 goto fail;
160 pid_exists = talloc_array(talloc_tos(), bool, d->num_share_modes);
161 if (pid_exists == NULL) {
162 DEBUG(0, ("talloc failed\n"));
163 goto fail;
166 for (i=0; i<d->num_share_modes; i++) {
167 pids[i] = d->share_modes[i].pid;
169 if (!serverids_exist(pids, d->num_share_modes, pid_exists)) {
170 DEBUG(0, ("serverid_exists failed\n"));
171 goto fail;
174 i = 0;
175 while (i < d->num_share_modes) {
176 struct share_mode_entry *e = &d->share_modes[i];
177 if (!pid_exists[i]) {
178 *e = d->share_modes[d->num_share_modes-1];
179 d->num_share_modes -= 1;
180 d->modified = True;
181 continue;
183 i += 1;
185 TALLOC_FREE(pid_exists);
186 TALLOC_FREE(pids);
187 return d;
188 fail:
189 TALLOC_FREE(d);
190 return NULL;
193 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
195 DATA_BLOB blob;
196 enum ndr_err_code ndr_err;
198 if (DEBUGLEVEL >= 10) {
199 DEBUG(10, ("unparse_share_modes:\n"));
200 NDR_PRINT_DEBUG(share_mode_data, d);
203 if (d->num_share_modes == 0) {
204 DEBUG(10, ("No used share mode found\n"));
205 return make_tdb_data(NULL, 0);
208 ndr_err = ndr_push_struct_blob(
209 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
210 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
211 smb_panic("ndr_push_share_mode_lock failed");
214 return make_tdb_data(blob.data, blob.length);
217 static int share_mode_data_destructor(struct share_mode_data *d)
219 NTSTATUS status;
220 TDB_DATA data;
222 if (!d->modified) {
223 return 0;
226 data = unparse_share_modes(d);
228 if (data.dptr == NULL) {
229 if (!d->fresh) {
230 /* There has been an entry before, delete it */
232 status = dbwrap_record_delete(d->record);
233 if (!NT_STATUS_IS_OK(status)) {
234 char *errmsg;
236 DEBUG(0, ("delete_rec returned %s\n",
237 nt_errstr(status)));
239 if (asprintf(&errmsg, "could not delete share "
240 "entry: %s\n",
241 nt_errstr(status)) == -1) {
242 smb_panic("could not delete share"
243 "entry");
245 smb_panic(errmsg);
248 goto done;
251 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
252 if (!NT_STATUS_IS_OK(status)) {
253 char *errmsg;
255 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
257 if (asprintf(&errmsg, "could not store share mode entry: %s",
258 nt_errstr(status)) == -1) {
259 smb_panic("could not store share mode entry");
261 smb_panic(errmsg);
264 done:
266 return 0;
269 static struct share_mode_data *fresh_share_mode_lock(
270 TALLOC_CTX *mem_ctx, const char *servicepath,
271 const struct smb_filename *smb_fname,
272 const struct timespec *old_write_time)
274 struct share_mode_data *d;
276 if ((servicepath == NULL) || (smb_fname == NULL) ||
277 (old_write_time == NULL)) {
278 return NULL;
281 d = talloc_zero(mem_ctx, struct share_mode_data);
282 if (d == NULL) {
283 goto fail;
285 d->base_name = talloc_strdup(d, smb_fname->base_name);
286 if (d->base_name == NULL) {
287 goto fail;
289 if (smb_fname->stream_name != NULL) {
290 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
291 if (d->stream_name == NULL) {
292 goto fail;
295 d->servicepath = talloc_strdup(d, servicepath);
296 if (d->servicepath == NULL) {
297 goto fail;
299 d->old_write_time = *old_write_time;
300 d->modified = false;
301 d->fresh = true;
302 return d;
303 fail:
304 DEBUG(0, ("talloc failed\n"));
305 TALLOC_FREE(d);
306 return NULL;
309 struct share_mode_lock *get_share_mode_lock_fresh(TALLOC_CTX *mem_ctx,
310 const struct file_id id,
311 const char *servicepath,
312 const struct smb_filename *smb_fname,
313 const struct timespec *old_write_time)
315 struct share_mode_lock *lck;
316 struct share_mode_data *d;
317 struct file_id tmp;
318 struct db_record *rec;
319 TDB_DATA key = locking_key(&id, &tmp);
320 TDB_DATA value;
322 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
323 if (rec == NULL) {
324 DEBUG(3, ("Could not lock share entry\n"));
325 return NULL;
328 value = dbwrap_record_get_value(rec);
330 if (value.dptr == NULL) {
331 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
332 old_write_time);
333 } else {
334 d = parse_share_modes(mem_ctx, value);
337 if (d == NULL) {
338 DEBUG(1, ("Could not get share mode lock\n"));
339 TALLOC_FREE(rec);
340 return NULL;
342 d->id = id;
343 d->record = talloc_move(d, &rec);
344 talloc_set_destructor(d, share_mode_data_destructor);
346 lck = talloc(mem_ctx, struct share_mode_lock);
347 if (lck == NULL) {
348 DEBUG(1, ("talloc failed\n"));
349 TALLOC_FREE(d);
350 return NULL;
352 lck->data = talloc_move(lck, &d);
353 return lck;
356 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
357 const struct file_id id)
359 struct share_mode_lock *lck;
360 struct file_id tmp;
361 TDB_DATA key = locking_key(&id, &tmp);
362 TDB_DATA data;
363 NTSTATUS status;
365 status = dbwrap_fetch(lock_db, talloc_tos(), key, &data);
366 if (!NT_STATUS_IS_OK(status)) {
367 DEBUG(3, ("Could not fetch share entry\n"));
368 return NULL;
370 if (data.dptr == NULL) {
371 return NULL;
373 lck = talloc(mem_ctx, struct share_mode_lock);
374 if (lck == NULL) {
375 TALLOC_FREE(data.dptr);
376 return NULL;
378 lck->data = parse_share_modes(mem_ctx, data);
379 TALLOC_FREE(data.dptr);
380 if (lck->data == NULL) {
381 TALLOC_FREE(lck);
382 return NULL;
384 return lck;
387 struct forall_state {
388 void (*fn)(const struct share_mode_entry *entry,
389 const char *sharepath,
390 const char *fname,
391 void *private_data);
392 void *private_data;
395 static int traverse_fn(struct db_record *rec, void *_state)
397 struct forall_state *state = (struct forall_state *)_state;
398 uint32_t i;
399 TDB_DATA key;
400 TDB_DATA value;
401 DATA_BLOB blob;
402 enum ndr_err_code ndr_err;
403 struct share_mode_data *d;
405 key = dbwrap_record_get_key(rec);
406 value = dbwrap_record_get_value(rec);
408 /* Ensure this is a locking_key record. */
409 if (key.dsize != sizeof(struct file_id))
410 return 0;
412 d = talloc(talloc_tos(), struct share_mode_data);
413 if (d == NULL) {
414 return 0;
417 blob.data = value.dptr;
418 blob.length = value.dsize;
420 ndr_err = ndr_pull_struct_blob(
421 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
422 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
423 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
424 return 0;
426 for (i=0; i<d->num_share_modes; i++) {
427 state->fn(&d->share_modes[i],
428 d->servicepath, d->base_name,
429 state->private_data);
431 TALLOC_FREE(d);
433 return 0;
436 /*******************************************************************
437 Call the specified function on each entry under management by the
438 share mode system.
439 ********************************************************************/
441 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
442 const char *, void *),
443 void *private_data)
445 struct forall_state state;
446 NTSTATUS status;
447 int count;
449 if (lock_db == NULL)
450 return 0;
452 state.fn = fn;
453 state.private_data = private_data;
455 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
456 &count);
458 if (!NT_STATUS_IS_OK(status)) {
459 return -1;
460 } else {
461 return count;