de8cc3ce8bde1824030c36a1aeffdc204237880c
[metze/samba/wip.git] / source3 / locking / share_mode_lock.c
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
7
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.
12
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.
17
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/>.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27    locking to deal with multiple share modes per open file.
28
29    September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30    support.
31
32    rewritten completely to use new tdb code. Tridge, Dec '99
33
34    Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35    Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
36 */
37
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
50 #undef DBGC_CLASS
51 #define DBGC_CLASS DBGC_LOCKING
52
53 #define NO_LOCKING_COUNT (-1)
54
55 /* the locking database handle */
56 static struct db_context *lock_db;
57
58 static bool locking_init_internal(bool read_only)
59 {
60         brl_init(read_only);
61
62         if (lock_db)
63                 return True;
64
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);
70
71         if (!lock_db) {
72                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
73                 return False;
74         }
75
76         if (!posix_locking_init(read_only))
77                 return False;
78
79         return True;
80 }
81
82 bool locking_init(void)
83 {
84         return locking_init_internal(false);
85 }
86
87 bool locking_init_readonly(void)
88 {
89         return locking_init_internal(true);
90 }
91
92 /*******************************************************************
93  Deinitialize the share_mode management.
94 ******************************************************************/
95
96 bool locking_end(void)
97 {
98         brl_shutdown();
99         TALLOC_FREE(lock_db);
100         return true;
101 }
102
103 /*******************************************************************
104  Form a static locking key for a dev/inode pair.
105 ******************************************************************/
106
107 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
108 {
109         *tmp = *id;
110         return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
111 }
112
113 /*******************************************************************
114  Get all share mode entries for a dev/inode pair.
115 ********************************************************************/
116
117 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
118                                                  const TDB_DATA dbuf)
119 {
120         struct share_mode_data *d;
121         int i;
122         struct server_id *pids;
123         bool *pid_exists;
124         enum ndr_err_code ndr_err;
125         DATA_BLOB blob;
126
127         d = talloc_zero(mem_ctx, struct share_mode_data);
128         if (d == NULL) {
129                 DEBUG(0, ("talloc failed\n"));
130                 goto fail;
131         }
132
133         blob.data = dbuf.dptr;
134         blob.length = dbuf.dsize;
135
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"));
140                 goto fail;
141         }
142
143         d->modified = false;
144         d->fresh = false;
145
146         if (DEBUGLEVEL >= 10) {
147                 DEBUG(10, ("parse_share_modes:\n"));
148                 NDR_PRINT_DEBUG(share_mode_data, d);
149         }
150
151         /*
152          * Ensure that each entry has a real process attached.
153          */
154
155         pids = talloc_array(talloc_tos(), struct server_id,
156                             d->num_share_modes);
157         if (pids == NULL) {
158                 DEBUG(0, ("talloc failed\n"));
159                 goto fail;
160         }
161         pid_exists = talloc_array(talloc_tos(), bool, d->num_share_modes);
162         if (pid_exists == NULL) {
163                 DEBUG(0, ("talloc failed\n"));
164                 goto fail;
165         }
166
167         for (i=0; i<d->num_share_modes; i++) {
168                 pids[i] = d->share_modes[i].pid;
169         }
170         if (!serverids_exist(pids, d->num_share_modes, pid_exists)) {
171                 DEBUG(0, ("serverid_exists failed\n"));
172                 goto fail;
173         }
174
175         i = 0;
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;
183                         d->modified = True;
184                         continue;
185                 }
186                 i += 1;
187         }
188         TALLOC_FREE(pid_exists);
189         TALLOC_FREE(pids);
190         return d;
191 fail:
192         TALLOC_FREE(d);
193         return NULL;
194 }
195
196 /*******************************************************************
197  Create a storable data blob from a modified share_mode_data struct.
198 ********************************************************************/
199
200 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
201 {
202         DATA_BLOB blob;
203         enum ndr_err_code ndr_err;
204
205         if (DEBUGLEVEL >= 10) {
206                 DEBUG(10, ("unparse_share_modes:\n"));
207                 NDR_PRINT_DEBUG(share_mode_data, d);
208         }
209
210         if (d->num_share_modes == 0) {
211                 DEBUG(10, ("No used share mode found\n"));
212                 return make_tdb_data(NULL, 0);
213         }
214
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");
219         }
220
221         return make_tdb_data(blob.data, blob.length);
222 }
223
224 /*******************************************************************
225  If modified, store the share_mode_data back into the database.
226 ********************************************************************/
227
228 static int share_mode_data_destructor(struct share_mode_data *d)
229 {
230         NTSTATUS status;
231         TDB_DATA data;
232
233         if (!d->modified) {
234                 return 0;
235         }
236
237         data = unparse_share_modes(d);
238
239         if (data.dptr == NULL) {
240                 if (!d->fresh) {
241                         /* There has been an entry before, delete it */
242
243                         status = dbwrap_record_delete(d->record);
244                         if (!NT_STATUS_IS_OK(status)) {
245                                 char *errmsg;
246
247                                 DEBUG(0, ("delete_rec returned %s\n",
248                                           nt_errstr(status)));
249
250                                 if (asprintf(&errmsg, "could not delete share "
251                                              "entry: %s\n",
252                                              nt_errstr(status)) == -1) {
253                                         smb_panic("could not delete share"
254                                                   "entry");
255                                 }
256                                 smb_panic(errmsg);
257                         }
258                 }
259                 goto done;
260         }
261
262         status = dbwrap_record_store(d->record, data, TDB_REPLACE);
263         if (!NT_STATUS_IS_OK(status)) {
264                 char *errmsg;
265
266                 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
267
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");
271                 }
272                 smb_panic(errmsg);
273         }
274
275  done:
276
277         return 0;
278 }
279
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 ********************************************************************/
284
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)
289 {
290         struct share_mode_data *d;
291
292         if ((servicepath == NULL) || (smb_fname == NULL) ||
293             (old_write_time == NULL)) {
294                 return NULL;
295         }
296
297         d = talloc_zero(mem_ctx, struct share_mode_data);
298         if (d == NULL) {
299                 goto fail;
300         }
301         d->base_name = talloc_strdup(d, smb_fname->base_name);
302         if (d->base_name == NULL) {
303                 goto fail;
304         }
305         if (smb_fname->stream_name != NULL) {
306                 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
307                 if (d->stream_name == NULL) {
308                         goto fail;
309                 }
310         }
311         d->servicepath = talloc_strdup(d, servicepath);
312         if (d->servicepath == NULL) {
313                 goto fail;
314         }
315         d->old_write_time = *old_write_time;
316         d->modified = false;
317         d->fresh = true;
318         return d;
319 fail:
320         DEBUG(0, ("talloc failed\n"));
321         TALLOC_FREE(d);
322         return NULL;
323 }
324
325 /*******************************************************************
326  Either fetch a share mode from the database, or allocate a fresh
327  one if the record doesn't exist.
328 ********************************************************************/
329
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)
334 {
335         struct share_mode_lock *lck;
336         struct share_mode_data *d;
337         struct file_id tmp;
338         struct db_record *rec;
339         TDB_DATA key = locking_key(&id, &tmp);
340         TDB_DATA value;
341
342         rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
343         if (rec == NULL) {
344                 DEBUG(3, ("Could not lock share entry\n"));
345                 return NULL;
346         }
347
348         value = dbwrap_record_get_value(rec);
349
350         if (value.dptr == NULL) {
351                 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
352                                           old_write_time);
353         } else {
354                 d = parse_share_modes(mem_ctx, value);
355         }
356
357         if (d == NULL) {
358                 DEBUG(1, ("Could not get share mode lock\n"));
359                 TALLOC_FREE(rec);
360                 return NULL;
361         }
362         d->id = id;
363         d->record = talloc_move(d, &rec);
364         talloc_set_destructor(d, share_mode_data_destructor);
365
366         lck = talloc(mem_ctx, struct share_mode_lock);
367         if (lck == NULL) {
368                 DEBUG(1, ("talloc failed\n"));
369                 TALLOC_FREE(d);
370                 return NULL;
371         }
372         lck->data = talloc_move(lck, &d);
373         return lck;
374 }
375
376 /*
377  * We can only ever have one share mode locked. Users of
378  * get_share_mode_lock never see this, it will be refcounted by
379  * talloc_reference.
380  */
381 static struct share_mode_lock *the_lock;
382
383 static int the_lock_destructor(struct share_mode_lock *l)
384 {
385         the_lock = NULL;
386         return 0;
387 }
388
389 /*******************************************************************
390  Get a share_mode_lock, Reference counted to allow nexted calls.
391 ********************************************************************/
392
393 struct share_mode_lock *get_share_mode_lock(
394         TALLOC_CTX *mem_ctx,
395         const struct file_id id,
396         const char *servicepath,
397         const struct smb_filename *smb_fname,
398         const struct timespec *old_write_time)
399 {
400         TALLOC_CTX *frame = talloc_stackframe();
401
402         struct share_mode_lock *lck;
403
404         if (the_lock == NULL) {
405                 the_lock = get_share_mode_lock_internal(
406                         frame, id, servicepath, smb_fname, old_write_time);
407                 if (the_lock == NULL) {
408                         goto fail;
409                 }
410                 talloc_set_destructor(the_lock, the_lock_destructor);
411         }
412         if (!file_id_equal(&the_lock->data->id, &id)) {
413                 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
414                 goto fail;
415         }
416         lck = talloc(mem_ctx, struct share_mode_lock);
417         if (lck == NULL) {
418                 DEBUG(1, ("talloc failed\n"));
419                 goto fail;
420         }
421         if (talloc_reference(lck, the_lock) == NULL) {
422                 DEBUG(1, ("talloc_reference failed\n"));
423                 goto fail;
424         }
425         lck->data = the_lock->data;
426         TALLOC_FREE(frame);
427         return lck;
428 fail:
429         TALLOC_FREE(frame);
430         return NULL;
431 }
432
433 /*******************************************************************
434  Get a share_mode_lock without locking the database or reference
435  counting. Used by smbstatus to display existing share modes.
436 ********************************************************************/
437
438 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
439                                                   const struct file_id id)
440 {
441         struct share_mode_lock *lck;
442         struct file_id tmp;
443         TDB_DATA key = locking_key(&id, &tmp);
444         TDB_DATA data;
445         NTSTATUS status;
446
447         status = dbwrap_fetch(lock_db, talloc_tos(), key, &data);
448         if (!NT_STATUS_IS_OK(status)) {
449                 DEBUG(3, ("Could not fetch share entry\n"));
450                 return NULL;
451         }
452         if (data.dptr == NULL) {
453                 return NULL;
454         }
455         lck = talloc(mem_ctx, struct share_mode_lock);
456         if (lck == NULL) {
457                 TALLOC_FREE(data.dptr);
458                 return NULL;
459         }
460         lck->data = parse_share_modes(lck, data);
461         TALLOC_FREE(data.dptr);
462         if (lck->data == NULL) {
463                 TALLOC_FREE(lck);
464                 return NULL;
465         }
466         return lck;
467 }
468
469 struct forall_state {
470         void (*fn)(const struct share_mode_entry *entry,
471                    const char *sharepath,
472                    const char *fname,
473                    void *private_data);
474         void *private_data;
475 };
476
477 static int traverse_fn(struct db_record *rec, void *_state)
478 {
479         struct forall_state *state = (struct forall_state *)_state;
480         uint32_t i;
481         TDB_DATA key;
482         TDB_DATA value;
483         DATA_BLOB blob;
484         enum ndr_err_code ndr_err;
485         struct share_mode_data *d;
486
487         key = dbwrap_record_get_key(rec);
488         value = dbwrap_record_get_value(rec);
489
490         /* Ensure this is a locking_key record. */
491         if (key.dsize != sizeof(struct file_id))
492                 return 0;
493
494         d = talloc(talloc_tos(), struct share_mode_data);
495         if (d == NULL) {
496                 return 0;
497         }
498
499         blob.data = value.dptr;
500         blob.length = value.dsize;
501
502         ndr_err = ndr_pull_struct_blob(
503                 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
504         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
505                 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
506                 return 0;
507         }
508         for (i=0; i<d->num_share_modes; i++) {
509                 state->fn(&d->share_modes[i],
510                           d->servicepath, d->base_name,
511                           state->private_data);
512         }
513         TALLOC_FREE(d);
514
515         return 0;
516 }
517
518 /*******************************************************************
519  Call the specified function on each entry under management by the
520  share mode system.
521 ********************************************************************/
522
523 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
524                                  const char *, void *),
525                       void *private_data)
526 {
527         struct forall_state state;
528         NTSTATUS status;
529         int count;
530
531         if (lock_db == NULL)
532                 return 0;
533
534         state.fn = fn;
535         state.private_data = private_data;
536
537         status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
538                                       &count);
539
540         if (!NT_STATUS_IS_OK(status)) {
541                 return -1;
542         } else {
543                 return count;
544         }
545 }