79efb70ccc67db1f57d4a880cef7238aa7ffc54f
[kamenim/samba-autobuild/.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 #include "source3/lib/dbwrap/dbwrap_watch.h"
50 #include "locking/leases_db.h"
51 #include "../lib/util/memcache.h"
52
53 #undef DBGC_CLASS
54 #define DBGC_CLASS DBGC_LOCKING
55
56 #define NO_LOCKING_COUNT (-1)
57
58 /* the locking database handle */
59 static struct db_context *lock_db;
60
61 static bool locking_init_internal(bool read_only)
62 {
63         char *db_path;
64
65         brl_init(read_only);
66
67         if (lock_db)
68                 return True;
69
70         db_path = lock_path("locking.tdb");
71         if (db_path == NULL) {
72                 return false;
73         }
74
75         lock_db = db_open(NULL, db_path,
76                           SMB_OPEN_DATABASE_TDB_HASH_SIZE,
77                           TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
78                           read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
79                           DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
80         TALLOC_FREE(db_path);
81         if (!lock_db) {
82                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
83                 return False;
84         }
85
86         if (!posix_locking_init(read_only))
87                 return False;
88
89         dbwrap_watch_db(lock_db, server_messaging_context());
90
91         return True;
92 }
93
94 bool locking_init(void)
95 {
96         return locking_init_internal(false);
97 }
98
99 bool locking_init_readonly(void)
100 {
101         return locking_init_internal(true);
102 }
103
104 /*******************************************************************
105  Deinitialize the share_mode management.
106 ******************************************************************/
107
108 bool locking_end(void)
109 {
110         brl_shutdown();
111         TALLOC_FREE(lock_db);
112         return true;
113 }
114
115 /*******************************************************************
116  Form a static locking key for a dev/inode pair.
117 ******************************************************************/
118
119 static TDB_DATA locking_key(const struct file_id *id)
120 {
121         return make_tdb_data((const uint8_t *)id, sizeof(*id));
122 }
123
124 /*******************************************************************
125  Share mode cache utility functions that store/delete/retrieve
126  entries from memcache.
127
128  For now share the statcache (global cache) memory space. If
129  a lock record gets orphaned (which shouldn't happen as we're
130  using the same locking_key data as lookup) it will eventually
131  fall out of the cache via the normal LRU trim mechanism. If
132  necessary we can always make this a separate (smaller) cache.
133 ******************************************************************/
134
135 static const DATA_BLOB memcache_key(const struct file_id *id)
136 {
137         return data_blob_const((const void *)id, sizeof(*id));
138 }
139
140 static void share_mode_memcache_delete(struct share_mode_data *d)
141 {
142         const DATA_BLOB key = memcache_key(&d->id);
143
144         DEBUG(10,("deleting entry for file %s seq 0x%llu key %s\n",
145                 d->base_name,
146                 (unsigned long long) d->sequence_number,
147                 file_id_string(talloc_tos(), &d->id)));
148
149         memcache_delete(NULL,
150                         SHARE_MODE_LOCK_CACHE,
151                         key);
152 }
153
154 static void share_mode_memcache_store(struct share_mode_data *d)
155 {
156         const DATA_BLOB key = memcache_key(&d->id);
157
158         DEBUG(10,("stored entry for file %s seq 0x%llu key %s\n",
159                 d->base_name,
160                 (unsigned long long) d->sequence_number,
161                 file_id_string(talloc_tos(), &d->id)));
162
163         /* Ensure everything stored in the cache is pristine. */
164         d->modified = false;
165         d->fresh = false;
166
167         /*
168          * Ensure the memory going into the cache
169          * doesn't have a destructor so it can be
170          * cleanly freed by share_mode_memcache_delete().
171          */
172         talloc_set_destructor(d, NULL);
173
174         /* Cache will own d after this call. */
175         memcache_add_talloc(NULL,
176                         SHARE_MODE_LOCK_CACHE,
177                         key,
178                         &d);
179 }
180
181 /*
182  * NB. We use ndr_pull_hyper on a stack-created
183  * struct ndr_pull with no talloc allowed, as we
184  * need this to be really fast as an ndr-peek into
185  * the first 8 bytes of the blob.
186  */
187
188 static enum ndr_err_code get_blob_sequence_number(DATA_BLOB *blob,
189                                                 uint64_t *pseq)
190 {
191         struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
192         NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
193         return NDR_ERR_SUCCESS;
194 }
195
196 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
197 {
198         return -1;
199 }
200
201 static struct share_mode_data *share_mode_memcache_fetch(TALLOC_CTX *mem_ctx,
202                                         const TDB_DATA id_key,
203                                         DATA_BLOB *blob)
204 {
205         enum ndr_err_code ndr_err;
206         struct share_mode_data *d;
207         uint64_t sequence_number;
208         void *ptr;
209         struct file_id id;
210         DATA_BLOB key;
211
212         /* Ensure this is a locking_key record. */
213         if (id_key.dsize != sizeof(id)) {
214                 return NULL;
215         }
216
217         memcpy(&id, id_key.dptr, id_key.dsize);
218         key = memcache_key(&id);
219
220         ptr = memcache_lookup_talloc(NULL,
221                         SHARE_MODE_LOCK_CACHE,
222                         key);
223         if (ptr == NULL) {
224                 DEBUG(10,("failed to find entry for key %s\n",
225                         file_id_string(mem_ctx, &id)));
226                 return NULL;
227         }
228         /* sequence number key is at start of blob. */
229         ndr_err = get_blob_sequence_number(blob, &sequence_number);
230         if (ndr_err != NDR_ERR_SUCCESS) {
231                 /* Bad blob. Remove entry. */
232                 DEBUG(10,("bad blob %u key %s\n",
233                         (unsigned int)ndr_err,
234                         file_id_string(mem_ctx, &id)));
235                 memcache_delete(NULL,
236                         SHARE_MODE_LOCK_CACHE,
237                         key);
238                 return NULL;
239         }
240
241         d = (struct share_mode_data *)ptr;
242         if (d->sequence_number != sequence_number) {
243                 DEBUG(10,("seq changed (cached 0x%llu) (new 0x%llu) "
244                         "for key %s\n",
245                         (unsigned long long)d->sequence_number,
246                         (unsigned long long)sequence_number,
247                         file_id_string(mem_ctx, &id)));
248                 /* Cache out of date. Remove entry. */
249                 memcache_delete(NULL,
250                         SHARE_MODE_LOCK_CACHE,
251                         key);
252                 return NULL;
253         }
254
255         /* Move onto mem_ctx. */
256         d = talloc_move(mem_ctx, &ptr);
257
258         /*
259          * Now we own d, prevent the cache from freeing it
260          * when we delete the entry.
261          */
262         talloc_set_destructor(d, share_mode_data_nofree_destructor);
263
264         /* Remove from the cache. We own it now. */
265         memcache_delete(NULL,
266                         SHARE_MODE_LOCK_CACHE,
267                         key);
268
269         /* And reset the destructor to none. */
270         talloc_set_destructor(d, NULL);
271
272         DEBUG(10,("fetched entry for file %s seq 0x%llu key %s\n",
273                 d->base_name,
274                 (unsigned long long)d->sequence_number,
275                 file_id_string(mem_ctx, &id)));
276
277         return d;
278 }
279
280 /*******************************************************************
281  Get all share mode entries for a dev/inode pair.
282 ********************************************************************/
283
284 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
285                                                 const TDB_DATA key,
286                                                 const TDB_DATA dbuf)
287 {
288         struct share_mode_data *d;
289         enum ndr_err_code ndr_err;
290         uint32_t i;
291         DATA_BLOB blob;
292
293         blob.data = dbuf.dptr;
294         blob.length = dbuf.dsize;
295
296         /* See if we already have a cached copy of this key. */
297         d = share_mode_memcache_fetch(mem_ctx, key, &blob);
298         if (d != NULL) {
299                 return d;
300         }
301
302         d = talloc(mem_ctx, struct share_mode_data);
303         if (d == NULL) {
304                 DEBUG(0, ("talloc failed\n"));
305                 goto fail;
306         }
307
308         ndr_err = ndr_pull_struct_blob_all(
309                 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
310         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
311                 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
312                           ndr_errstr(ndr_err)));
313                 goto fail;
314         }
315
316         /*
317          * Initialize the values that are [skip] in the idl. The NDR code does
318          * not initialize them.
319          */
320
321         for (i=0; i<d->num_share_modes; i++) {
322                 struct share_mode_entry *e = &d->share_modes[i];
323
324                 e->stale = false;
325                 e->lease = NULL;
326                 if (e->op_type != LEASE_OPLOCK) {
327                         continue;
328                 }
329                 if (e->lease_idx >= d->num_leases) {
330                         continue;
331                 }
332                 e->lease = &d->leases[e->lease_idx];
333         }
334         d->modified = false;
335         d->fresh = false;
336
337         if (DEBUGLEVEL >= 10) {
338                 DEBUG(10, ("parse_share_modes:\n"));
339                 NDR_PRINT_DEBUG(share_mode_data, d);
340         }
341
342         return d;
343 fail:
344         TALLOC_FREE(d);
345         return NULL;
346 }
347
348 /*******************************************************************
349  Create a storable data blob from a modified share_mode_data struct.
350 ********************************************************************/
351
352 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
353 {
354         DATA_BLOB blob;
355         enum ndr_err_code ndr_err;
356
357         if (DEBUGLEVEL >= 10) {
358                 DEBUG(10, ("unparse_share_modes:\n"));
359                 NDR_PRINT_DEBUG(share_mode_data, d);
360         }
361
362         share_mode_memcache_delete(d);
363
364         /* Update the sequence number. */
365         d->sequence_number += 1;
366
367         remove_stale_share_mode_entries(d);
368
369         if (d->num_share_modes == 0) {
370                 DEBUG(10, ("No used share mode found\n"));
371                 return make_tdb_data(NULL, 0);
372         }
373
374         ndr_err = ndr_push_struct_blob(
375                 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
376         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
377                 smb_panic("ndr_push_share_mode_lock failed");
378         }
379
380         return make_tdb_data(blob.data, blob.length);
381 }
382
383 /*******************************************************************
384  If modified, store the share_mode_data back into the database.
385 ********************************************************************/
386
387 static int share_mode_data_destructor(struct share_mode_data *d)
388 {
389         NTSTATUS status;
390         TDB_DATA data;
391
392         if (!d->modified) {
393                 return 0;
394         }
395
396         data = unparse_share_modes(d);
397
398         if (data.dptr == NULL) {
399                 if (!d->fresh) {
400                         /* There has been an entry before, delete it */
401
402                         status = dbwrap_record_delete(d->record);
403                         if (!NT_STATUS_IS_OK(status)) {
404                                 char *errmsg;
405
406                                 DEBUG(0, ("delete_rec returned %s\n",
407                                           nt_errstr(status)));
408
409                                 if (asprintf(&errmsg, "could not delete share "
410                                              "entry: %s\n",
411                                              nt_errstr(status)) == -1) {
412                                         smb_panic("could not delete share"
413                                                   "entry");
414                                 }
415                                 smb_panic(errmsg);
416                         }
417                 }
418                 /*
419                  * Nothing to store in cache - allow the normal
420                  * release of record lock and memory free.
421                  */
422                 return 0;
423         }
424
425         status = dbwrap_record_store(d->record, data, TDB_REPLACE);
426         if (!NT_STATUS_IS_OK(status)) {
427                 char *errmsg;
428
429                 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
430
431                 if (asprintf(&errmsg, "could not store share mode entry: %s",
432                              nt_errstr(status)) == -1) {
433                         smb_panic("could not store share mode entry");
434                 }
435                 smb_panic(errmsg);
436         }
437
438         /*
439          * Release the record lock before putting in the cache.
440          */
441         TALLOC_FREE(d->record);
442
443         /*
444          * Reparent d into the in-memory cache so it can be reused if the
445          * sequence number matches. See parse_share_modes()
446          * for details.
447          */
448
449         share_mode_memcache_store(d);
450         return -1;
451 }
452
453 /*******************************************************************
454  Allocate a new share_mode_data struct, mark it unmodified.
455  fresh is set to note that currently there is no database entry.
456 ********************************************************************/
457
458 static struct share_mode_data *fresh_share_mode_lock(
459         TALLOC_CTX *mem_ctx, const char *servicepath,
460         const struct smb_filename *smb_fname,
461         const struct timespec *old_write_time)
462 {
463         struct share_mode_data *d;
464
465         if ((servicepath == NULL) || (smb_fname == NULL) ||
466             (old_write_time == NULL)) {
467                 return NULL;
468         }
469
470         d = talloc_zero(mem_ctx, struct share_mode_data);
471         if (d == NULL) {
472                 goto fail;
473         }
474         /* New record - new sequence number. */
475         generate_random_buffer((uint8_t *)&d->sequence_number, 8);
476
477         d->base_name = talloc_strdup(d, smb_fname->base_name);
478         if (d->base_name == NULL) {
479                 goto fail;
480         }
481         if (smb_fname->stream_name != NULL) {
482                 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
483                 if (d->stream_name == NULL) {
484                         goto fail;
485                 }
486         }
487         d->servicepath = talloc_strdup(d, servicepath);
488         if (d->servicepath == NULL) {
489                 goto fail;
490         }
491         d->old_write_time = *old_write_time;
492         d->modified = false;
493         d->fresh = true;
494         return d;
495 fail:
496         DEBUG(0, ("talloc failed\n"));
497         TALLOC_FREE(d);
498         return NULL;
499 }
500
501 /*******************************************************************
502  Either fetch a share mode from the database, or allocate a fresh
503  one if the record doesn't exist.
504 ********************************************************************/
505
506 static struct share_mode_lock *get_share_mode_lock_internal(
507         TALLOC_CTX *mem_ctx, struct file_id id,
508         const char *servicepath, const struct smb_filename *smb_fname,
509         const struct timespec *old_write_time)
510 {
511         struct share_mode_lock *lck;
512         struct share_mode_data *d;
513         struct db_record *rec;
514         TDB_DATA key = locking_key(&id);
515         TDB_DATA value;
516
517         rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
518         if (rec == NULL) {
519                 DEBUG(3, ("Could not lock share entry\n"));
520                 return NULL;
521         }
522
523         value = dbwrap_record_get_value(rec);
524
525         if (value.dptr == NULL) {
526                 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
527                                           old_write_time);
528         } else {
529                 d = parse_share_modes(mem_ctx, key, value);
530         }
531
532         if (d == NULL) {
533                 DEBUG(5, ("get_share_mode_lock_internal: "
534                         "Could not get share mode lock\n"));
535                 TALLOC_FREE(rec);
536                 return NULL;
537         }
538         d->id = id;
539         d->record = talloc_move(d, &rec);
540         talloc_set_destructor(d, share_mode_data_destructor);
541
542         lck = talloc(mem_ctx, struct share_mode_lock);
543         if (lck == NULL) {
544                 DEBUG(1, ("talloc failed\n"));
545                 TALLOC_FREE(d);
546                 return NULL;
547         }
548         lck->data = talloc_move(lck, &d);
549         return lck;
550 }
551
552 /*
553  * We can only ever have one share mode locked. Users of
554  * get_share_mode_lock never see this, it will be refcounted by
555  * talloc_reference.
556  */
557 static struct share_mode_lock *the_lock;
558 static struct file_id the_lock_id;
559
560 static int the_lock_destructor(struct share_mode_lock *l)
561 {
562         the_lock = NULL;
563         ZERO_STRUCT(the_lock_id);
564         return 0;
565 }
566
567 /*******************************************************************
568  Get a share_mode_lock, Reference counted to allow nested calls.
569 ********************************************************************/
570
571 struct share_mode_lock *get_share_mode_lock(
572         TALLOC_CTX *mem_ctx,
573         struct file_id id,
574         const char *servicepath,
575         const struct smb_filename *smb_fname,
576         const struct timespec *old_write_time)
577 {
578         struct share_mode_lock *lck;
579
580         lck = talloc(mem_ctx, struct share_mode_lock);
581         if (lck == NULL) {
582                 DEBUG(1, ("talloc failed\n"));
583                 return NULL;
584         }
585
586         if (the_lock == NULL) {
587                 the_lock = get_share_mode_lock_internal(
588                         lck, id, servicepath, smb_fname, old_write_time);
589                 if (the_lock == NULL) {
590                         goto fail;
591                 }
592                 talloc_set_destructor(the_lock, the_lock_destructor);
593                 the_lock_id = id;
594         } else {
595                 if (!file_id_equal(&the_lock_id, &id)) {
596                         DEBUG(1, ("Can not lock two share modes "
597                                   "simultaneously\n"));
598                         goto fail;
599                 }
600                 if (talloc_reference(lck, the_lock) == NULL) {
601                         DEBUG(1, ("talloc_reference failed\n"));
602                         goto fail;
603                 }
604         }
605         lck->data = the_lock->data;
606         return lck;
607 fail:
608         TALLOC_FREE(lck);
609         return NULL;
610 }
611
612 static void fetch_share_mode_unlocked_parser(
613         TDB_DATA key, TDB_DATA data, void *private_data)
614 {
615         struct share_mode_lock *lck = talloc_get_type_abort(
616                 private_data, struct share_mode_lock);
617
618         lck->data = parse_share_modes(lck, key, data);
619 }
620
621 /*******************************************************************
622  Get a share_mode_lock without locking the database or reference
623  counting. Used by smbstatus to display existing share modes.
624 ********************************************************************/
625
626 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
627                                                   struct file_id id)
628 {
629         struct share_mode_lock *lck;
630         TDB_DATA key = locking_key(&id);
631         NTSTATUS status;
632
633         lck = talloc(mem_ctx, struct share_mode_lock);
634         if (lck == NULL) {
635                 DEBUG(0, ("talloc failed\n"));
636                 return NULL;
637         }
638         status = dbwrap_parse_record(
639                 lock_db, key, fetch_share_mode_unlocked_parser, lck);
640         if (!NT_STATUS_IS_OK(status) ||
641             (lck->data == NULL)) {
642                 TALLOC_FREE(lck);
643                 return NULL;
644         }
645         return lck;
646 }
647
648 struct share_mode_forall_state {
649         int (*fn)(struct file_id fid, const struct share_mode_data *data,
650                   void *private_data);
651         void *private_data;
652 };
653
654 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
655 {
656         struct share_mode_forall_state *state =
657                 (struct share_mode_forall_state *)_state;
658         uint32_t i;
659         TDB_DATA key;
660         TDB_DATA value;
661         DATA_BLOB blob;
662         enum ndr_err_code ndr_err;
663         struct share_mode_data *d;
664         struct file_id fid;
665         int ret;
666
667         key = dbwrap_record_get_key(rec);
668         value = dbwrap_record_get_value(rec);
669
670         /* Ensure this is a locking_key record. */
671         if (key.dsize != sizeof(fid)) {
672                 return 0;
673         }
674         memcpy(&fid, key.dptr, sizeof(fid));
675
676         d = talloc(talloc_tos(), struct share_mode_data);
677         if (d == NULL) {
678                 return 0;
679         }
680
681         blob.data = value.dptr;
682         blob.length = value.dsize;
683
684         ndr_err = ndr_pull_struct_blob_all(
685                 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
686         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
687                 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
688                 return 0;
689         }
690         if (DEBUGLEVEL > 10) {
691                 DEBUG(11, ("parse_share_modes:\n"));
692                 NDR_PRINT_DEBUG(share_mode_data, d);
693         }
694         for (i=0; i<d->num_share_modes; i++) {
695                 d->share_modes[i].stale = false; /* [skip] in idl */
696         }
697
698         ret = state->fn(fid, d, state->private_data);
699
700         TALLOC_FREE(d);
701         return ret;
702 }
703
704 int share_mode_forall(int (*fn)(struct file_id fid,
705                                 const struct share_mode_data *data,
706                                 void *private_data),
707                       void *private_data)
708 {
709         struct share_mode_forall_state state = {
710                 .fn = fn,
711                 .private_data = private_data
712         };
713         NTSTATUS status;
714         int count;
715
716         if (lock_db == NULL) {
717                 return 0;
718         }
719
720         status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
721                                       &state, &count);
722         if (!NT_STATUS_IS_OK(status)) {
723                 return -1;
724         }
725
726         return count;
727 }
728
729 struct share_entry_forall_state {
730         int (*fn)(const struct share_mode_entry *e,
731                   const char *service_path, const char *base_name,
732                   void *private_data);
733         void *private_data;
734 };
735
736 static int share_entry_traverse_fn(struct file_id fid,
737                                    const struct share_mode_data *data,
738                                    void *private_data)
739 {
740         struct share_entry_forall_state *state = private_data;
741         uint32_t i;
742
743         for (i=0; i<data->num_share_modes; i++) {
744                 int ret;
745
746                 ret = state->fn(&data->share_modes[i],
747                                 data->servicepath, data->base_name,
748                                 state->private_data);
749                 if (ret != 0) {
750                         return ret;
751                 }
752         }
753
754         return 0;
755 }
756
757 /*******************************************************************
758  Call the specified function on each entry under management by the
759  share mode system.
760 ********************************************************************/
761
762 int share_entry_forall(int (*fn)(const struct share_mode_entry *,
763                                  const char *, const char *, void *),
764                        void *private_data)
765 {
766         struct share_entry_forall_state state = {
767                 .fn = fn, .private_data = private_data };
768
769         return share_mode_forall(share_entry_traverse_fn, &state);
770 }
771
772 bool share_mode_cleanup_disconnected(struct file_id fid,
773                                      uint64_t open_persistent_id)
774 {
775         bool ret = false;
776         TALLOC_CTX *frame = talloc_stackframe();
777         unsigned n;
778         struct share_mode_data *data;
779         struct share_mode_lock *lck;
780         bool ok;
781
782         lck = get_existing_share_mode_lock(frame, fid);
783         if (lck == NULL) {
784                 DEBUG(5, ("share_mode_cleanup_disconnected: "
785                           "Could not fetch share mode entry for %s\n",
786                           file_id_string(frame, &fid)));
787                 goto done;
788         }
789         data = lck->data;
790
791         for (n=0; n < data->num_share_modes; n++) {
792                 struct share_mode_entry *entry = &data->share_modes[n];
793
794                 if (!server_id_is_disconnected(&entry->pid)) {
795                         DEBUG(5, ("share_mode_cleanup_disconnected: "
796                                   "file (file-id='%s', servicepath='%s', "
797                                   "base_name='%s%s%s') "
798                                   "is used by server %s ==> do not cleanup\n",
799                                   file_id_string(frame, &fid),
800                                   data->servicepath,
801                                   data->base_name,
802                                   (data->stream_name == NULL)
803                                   ? "" : "', stream_name='",
804                                   (data->stream_name == NULL)
805                                   ? "" : data->stream_name,
806                                   server_id_str(frame, &entry->pid)));
807                         goto done;
808                 }
809                 if (open_persistent_id != entry->share_file_id) {
810                         DEBUG(5, ("share_mode_cleanup_disconnected: "
811                                   "entry for file "
812                                   "(file-id='%s', servicepath='%s', "
813                                   "base_name='%s%s%s') "
814                                   "has share_file_id %llu but expected %llu"
815                                   "==> do not cleanup\n",
816                                   file_id_string(frame, &fid),
817                                   data->servicepath,
818                                   data->base_name,
819                                   (data->stream_name == NULL)
820                                   ? "" : "', stream_name='",
821                                   (data->stream_name == NULL)
822                                   ? "" : data->stream_name,
823                                   (unsigned long long)entry->share_file_id,
824                                   (unsigned long long)open_persistent_id));
825                         goto done;
826                 }
827         }
828
829         for (n=0; n < data->num_leases; n++) {
830                 struct share_mode_lease *l = &data->leases[n];
831                 NTSTATUS status;
832
833                 status = leases_db_del(&l->client_guid, &l->lease_key, &fid);
834
835                 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__,
836                            nt_errstr(status)));
837         }
838
839         ok = brl_cleanup_disconnected(fid, open_persistent_id);
840         if (!ok) {
841                 DEBUG(10, ("share_mode_cleanup_disconnected: "
842                            "failed to clean up byte range locks associated "
843                            "with file (file-id='%s', servicepath='%s', "
844                            "base_name='%s%s%s') and open_persistent_id %llu "
845                            "==> do not cleanup\n",
846                            file_id_string(frame, &fid),
847                            data->servicepath,
848                            data->base_name,
849                            (data->stream_name == NULL)
850                            ? "" : "', stream_name='",
851                            (data->stream_name == NULL)
852                            ? "" : data->stream_name,
853                            (unsigned long long)open_persistent_id));
854                 goto done;
855         }
856
857         DEBUG(10, ("share_mode_cleanup_disconnected: "
858                    "cleaning up %u entries for file "
859                    "(file-id='%s', servicepath='%s', "
860                    "base_name='%s%s%s') "
861                    "from open_persistent_id %llu\n",
862                    data->num_share_modes,
863                    file_id_string(frame, &fid),
864                    data->servicepath,
865                    data->base_name,
866                    (data->stream_name == NULL)
867                    ? "" : "', stream_name='",
868                    (data->stream_name == NULL)
869                    ? "" : data->stream_name,
870                    (unsigned long long)open_persistent_id));
871
872         data->num_share_modes = 0;
873         data->num_leases = 0;
874         data->modified = true;
875
876         ret = true;
877 done:
878         talloc_free(frame);
879         return ret;
880 }