smbd: Remove stale share mode entries while walking the array
[samba.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 "lib/util/server_id.h"
41 #include "locking/proto.h"
42 #include "smbd/globals.h"
43 #include "dbwrap/dbwrap.h"
44 #include "dbwrap/dbwrap_open.h"
45 #include "../libcli/security/security.h"
46 #include "serverid.h"
47 #include "messages.h"
48 #include "util_tdb.h"
49 #include "../librpc/gen_ndr/ndr_open_files.h"
50 #include "source3/lib/dbwrap/dbwrap_watch.h"
51 #include "locking/leases_db.h"
52 #include "../lib/util/memcache.h"
53 #include "lib/util/tevent_ntstatus.h"
54
55 #undef DBGC_CLASS
56 #define DBGC_CLASS DBGC_LOCKING
57
58 #define NO_LOCKING_COUNT (-1)
59
60 /* the locking database handle */
61 static struct db_context *lock_db;
62
63 static bool locking_init_internal(bool read_only)
64 {
65         struct db_context *backend;
66         char *db_path;
67
68         brl_init(read_only);
69
70         if (lock_db)
71                 return True;
72
73         db_path = lock_path(talloc_tos(), "locking.tdb");
74         if (db_path == NULL) {
75                 return false;
76         }
77
78         backend = db_open(NULL, db_path,
79                           SMB_OPEN_DATABASE_TDB_HASH_SIZE,
80                           TDB_DEFAULT|
81                           TDB_VOLATILE|
82                           TDB_CLEAR_IF_FIRST|
83                           TDB_INCOMPATIBLE_HASH|
84                           TDB_SEQNUM,
85                           read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
86                           DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
87         TALLOC_FREE(db_path);
88         if (!backend) {
89                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
90                 return False;
91         }
92
93         lock_db = db_open_watched(NULL, &backend, global_messaging_context());
94         if (lock_db == NULL) {
95                 DBG_ERR("db_open_watched failed\n");
96                 TALLOC_FREE(backend);
97                 return false;
98         }
99
100         if (!posix_locking_init(read_only)) {
101                 TALLOC_FREE(lock_db);
102                 return False;
103         }
104
105         return True;
106 }
107
108 bool locking_init(void)
109 {
110         return locking_init_internal(false);
111 }
112
113 bool locking_init_readonly(void)
114 {
115         return locking_init_internal(true);
116 }
117
118 /*******************************************************************
119  Deinitialize the share_mode management.
120 ******************************************************************/
121
122 bool locking_end(void)
123 {
124         brl_shutdown();
125         TALLOC_FREE(lock_db);
126         return true;
127 }
128
129 /*******************************************************************
130  Form a static locking key for a dev/inode pair.
131 ******************************************************************/
132
133 static TDB_DATA locking_key(const struct file_id *id)
134 {
135         return make_tdb_data((const uint8_t *)id, sizeof(*id));
136 }
137
138 /*******************************************************************
139  Share mode cache utility functions that store/delete/retrieve
140  entries from memcache.
141
142  For now share the statcache (global cache) memory space. If
143  a lock record gets orphaned (which shouldn't happen as we're
144  using the same locking_key data as lookup) it will eventually
145  fall out of the cache via the normal LRU trim mechanism. If
146  necessary we can always make this a separate (smaller) cache.
147 ******************************************************************/
148
149 static DATA_BLOB memcache_key(const struct file_id *id)
150 {
151         return data_blob_const((const void *)id, sizeof(*id));
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         DBG_DEBUG("stored entry for file %s seq %"PRIx64" key %s\n",
159                   d->base_name,
160                   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 evicted by the memcache LRU
171          * mechanism.
172          */
173         talloc_set_destructor(d, NULL);
174
175         /* Cache will own d after this call. */
176         memcache_add_talloc(NULL,
177                         SHARE_MODE_LOCK_CACHE,
178                         key,
179                         &d);
180 }
181
182 /*
183  * NB. We use ndr_pull_hyper on a stack-created
184  * struct ndr_pull with no talloc allowed, as we
185  * need this to be really fast as an ndr-peek into
186  * the first 9 bytes of the blob.
187  */
188
189 static enum ndr_err_code get_share_mode_blob_header(
190         DATA_BLOB *blob, uint64_t *pseq, uint8_t *pflags)
191 {
192         struct ndr_pull ndr = {.data = blob->data, .data_size = blob->length};
193         NDR_CHECK(ndr_pull_hyper(&ndr, NDR_SCALARS, pseq));
194         NDR_CHECK(ndr_pull_uint8(&ndr, NDR_SCALARS, pflags));
195         return NDR_ERR_SUCCESS;
196 }
197
198 struct fsp_update_share_mode_flags_state {
199         enum ndr_err_code ndr_err;
200         uint8_t share_mode_flags;
201 };
202
203 static void fsp_update_share_mode_flags_fn(
204         struct db_record *rec, bool *modified_dependent, void *private_data)
205 {
206         struct fsp_update_share_mode_flags_state *state = private_data;
207         TDB_DATA value = dbwrap_record_get_value(rec);
208         DATA_BLOB blob = { .data = value.dptr, .length = value.dsize };
209         uint64_t seq;
210
211         state->ndr_err = get_share_mode_blob_header(
212                 &blob, &seq, &state->share_mode_flags);
213 }
214
215 static NTSTATUS fsp_update_share_mode_flags(struct files_struct *fsp)
216 {
217         struct fsp_update_share_mode_flags_state state = {0};
218         int seqnum = dbwrap_get_seqnum(lock_db);
219         NTSTATUS status;
220
221         if (seqnum == fsp->share_mode_flags_seqnum) {
222                 return NT_STATUS_OK;
223         }
224
225         status = share_mode_do_locked(
226                 fsp->file_id, fsp_update_share_mode_flags_fn, &state);
227         if (!NT_STATUS_IS_OK(status)) {
228                 DBG_DEBUG("share_mode_do_locked returned %s\n",
229                           nt_errstr(status));
230                 return status;
231         }
232
233         if (!NDR_ERR_CODE_IS_SUCCESS(state.ndr_err)) {
234                 DBG_DEBUG("get_share_mode_blob_header returned %s\n",
235                           ndr_errstr(state.ndr_err));
236                 return ndr_map_error2ntstatus(state.ndr_err);
237         }
238
239         fsp->share_mode_flags_seqnum = seqnum;
240         fsp->share_mode_flags = state.share_mode_flags;
241
242         return NT_STATUS_OK;
243 }
244
245 bool file_has_read_lease(struct files_struct *fsp)
246 {
247         NTSTATUS status;
248
249         status = fsp_update_share_mode_flags(fsp);
250         if (!NT_STATUS_IS_OK(status)) {
251                 /* Safe default for leases */
252                 return true;
253         }
254
255         return (fsp->share_mode_flags & SHARE_MODE_HAS_READ_LEASE) != 0;
256 }
257
258 static int share_mode_data_nofree_destructor(struct share_mode_data *d)
259 {
260         return -1;
261 }
262
263 static struct share_mode_data *share_mode_memcache_fetch(TALLOC_CTX *mem_ctx,
264                                         const TDB_DATA id_key,
265                                         DATA_BLOB *blob)
266 {
267         enum ndr_err_code ndr_err;
268         struct share_mode_data *d;
269         uint64_t sequence_number;
270         uint8_t flags;
271         void *ptr;
272         struct file_id id;
273         DATA_BLOB key;
274
275         /* Ensure this is a locking_key record. */
276         if (id_key.dsize != sizeof(id)) {
277                 return NULL;
278         }
279
280         memcpy(&id, id_key.dptr, id_key.dsize);
281         key = memcache_key(&id);
282
283         ptr = memcache_lookup_talloc(NULL,
284                         SHARE_MODE_LOCK_CACHE,
285                         key);
286         if (ptr == NULL) {
287                 DEBUG(10,("failed to find entry for key %s\n",
288                         file_id_string(mem_ctx, &id)));
289                 return NULL;
290         }
291         /* sequence number key is at start of blob. */
292         ndr_err = get_share_mode_blob_header(blob, &sequence_number, &flags);
293         if (ndr_err != NDR_ERR_SUCCESS) {
294                 /* Bad blob. Remove entry. */
295                 DEBUG(10,("bad blob %u key %s\n",
296                         (unsigned int)ndr_err,
297                         file_id_string(mem_ctx, &id)));
298                 memcache_delete(NULL,
299                         SHARE_MODE_LOCK_CACHE,
300                         key);
301                 return NULL;
302         }
303
304         d = (struct share_mode_data *)ptr;
305         if (d->sequence_number != sequence_number) {
306                 DBG_DEBUG("seq changed (cached %"PRIx64") (new %"PRIx64") "
307                           "for key %s\n",
308                           d->sequence_number,
309                           sequence_number,
310                           file_id_string(mem_ctx, &id));
311                 /* Cache out of date. Remove entry. */
312                 memcache_delete(NULL,
313                         SHARE_MODE_LOCK_CACHE,
314                         key);
315                 return NULL;
316         }
317
318         /* Move onto mem_ctx. */
319         d = talloc_move(mem_ctx, &ptr);
320
321         /*
322          * Now we own d, prevent the cache from freeing it
323          * when we delete the entry.
324          */
325         talloc_set_destructor(d, share_mode_data_nofree_destructor);
326
327         /* Remove from the cache. We own it now. */
328         memcache_delete(NULL,
329                         SHARE_MODE_LOCK_CACHE,
330                         key);
331
332         /* And reset the destructor to none. */
333         talloc_set_destructor(d, NULL);
334
335         DBG_DEBUG("fetched entry for file %s seq %"PRIx64" key %s\n",
336                   d->base_name,
337                   d->sequence_number,
338                   file_id_string(mem_ctx, &id));
339
340         return d;
341 }
342
343 /*******************************************************************
344  Get all share mode entries for a dev/inode pair.
345 ********************************************************************/
346
347 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
348                                                 const TDB_DATA key,
349                                                 const TDB_DATA dbuf)
350 {
351         struct share_mode_data *d;
352         enum ndr_err_code ndr_err;
353         DATA_BLOB blob;
354
355         blob.data = dbuf.dptr;
356         blob.length = dbuf.dsize;
357
358         /* See if we already have a cached copy of this key. */
359         d = share_mode_memcache_fetch(mem_ctx, key, &blob);
360         if (d != NULL) {
361                 return d;
362         }
363
364         d = talloc(mem_ctx, struct share_mode_data);
365         if (d == NULL) {
366                 DEBUG(0, ("talloc failed\n"));
367                 goto fail;
368         }
369
370         ndr_err = ndr_pull_struct_blob_all(
371                 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
372         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
373                 DBG_WARNING("ndr_pull_share_mode_data failed: %s\n",
374                             ndr_errstr(ndr_err));
375                 goto fail;
376         }
377
378         if (DEBUGLEVEL >= 10) {
379                 DEBUG(10, ("parse_share_modes:\n"));
380                 NDR_PRINT_DEBUG(share_mode_data, d);
381         }
382
383         return d;
384 fail:
385         TALLOC_FREE(d);
386         return NULL;
387 }
388
389 /*******************************************************************
390  If modified, store the share_mode_data back into the database.
391 ********************************************************************/
392
393 static NTSTATUS share_mode_data_store(struct share_mode_data *d)
394 {
395         DATA_BLOB blob;
396         enum ndr_err_code ndr_err;
397         NTSTATUS status;
398
399         if (!d->modified) {
400                 DBG_DEBUG("not modified\n");
401                 return NT_STATUS_OK;
402         }
403
404         if (DEBUGLEVEL >= 10) {
405                 DBG_DEBUG("\n");
406                 NDR_PRINT_DEBUG(share_mode_data, d);
407         }
408
409         d->sequence_number += 1;
410
411         if (d->num_share_modes == 0) {
412                 TALLOC_FREE(d->delete_tokens);
413                 d->num_delete_tokens = 0;
414
415                 if (d->fresh) {
416                         DBG_DEBUG("Ignoring fresh empty record\n");
417                         return NT_STATUS_OK;
418                 }
419                 status = dbwrap_record_delete(d->record);
420                 return status;
421         }
422
423         ndr_err = ndr_push_struct_blob(
424                 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
425         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
426                 DBG_DEBUG("ndr_push_share_mode_data failed: %s\n",
427                           ndr_errstr(ndr_err));
428                 return ndr_map_error2ntstatus(ndr_err);
429         }
430
431         status = dbwrap_record_store(
432                 d->record,
433                 (TDB_DATA) { .dptr = blob.data, .dsize = blob.length },
434                 TDB_REPLACE);
435         TALLOC_FREE(blob.data);
436
437         if (!NT_STATUS_IS_OK(status)) {
438                 DBG_DEBUG("dbwrap_record_store failed: %s\n",
439                           nt_errstr(status));
440         }
441
442         return status;
443 }
444
445 /*******************************************************************
446  Allocate a new share_mode_data struct, mark it unmodified.
447  fresh is set to note that currently there is no database entry.
448 ********************************************************************/
449
450 static struct share_mode_data *fresh_share_mode_lock(
451         TALLOC_CTX *mem_ctx, const char *servicepath,
452         const struct smb_filename *smb_fname,
453         const struct timespec *old_write_time)
454 {
455         struct share_mode_data *d;
456
457         if ((servicepath == NULL) || (smb_fname == NULL) ||
458             (old_write_time == NULL)) {
459                 return NULL;
460         }
461
462         d = talloc_zero(mem_ctx, struct share_mode_data);
463         if (d == NULL) {
464                 goto fail;
465         }
466         /* New record - new sequence number. */
467         generate_random_buffer((uint8_t *)&d->sequence_number, 8);
468
469         d->base_name = talloc_strdup(d, smb_fname->base_name);
470         if (d->base_name == NULL) {
471                 goto fail;
472         }
473         if (smb_fname->stream_name != NULL) {
474                 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
475                 if (d->stream_name == NULL) {
476                         goto fail;
477                 }
478         }
479         d->servicepath = talloc_strdup(d, servicepath);
480         if (d->servicepath == NULL) {
481                 goto fail;
482         }
483         d->old_write_time = *old_write_time;
484         d->modified = false;
485         d->fresh = true;
486         return d;
487 fail:
488         DEBUG(0, ("talloc failed\n"));
489         TALLOC_FREE(d);
490         return NULL;
491 }
492
493 /*
494  * We can only ever have one share mode locked. Use a static
495  * share_mode_data pointer that is shared by multiple nested
496  * share_mode_lock structures, explicitly refcounted.
497  */
498 static struct share_mode_data *static_share_mode_data = NULL;
499 static size_t static_share_mode_data_refcount = 0;
500
501 /*
502  * db_record for the above. With dbwrap_do_locked we can get a
503  * db_record on the stack, which we can't TALLOC_FREE but which we
504  * need to share with a nested get_share_mode_lock call.
505  */
506 static struct db_record *static_share_mode_record = NULL;
507 static bool static_share_mode_record_talloced = false;
508
509 /*******************************************************************
510  Either fetch a share mode from the database, or allocate a fresh
511  one if the record doesn't exist.
512 ********************************************************************/
513
514 static NTSTATUS get_static_share_mode_data(
515         struct db_record *rec,
516         struct file_id id,
517         const char *servicepath,
518         const struct smb_filename *smb_fname,
519         const struct timespec *old_write_time)
520 {
521         struct share_mode_data *d;
522         TDB_DATA value = dbwrap_record_get_value(rec);
523
524         SMB_ASSERT(static_share_mode_data == NULL);
525
526         if (value.dptr == NULL) {
527                 d = fresh_share_mode_lock(
528                         lock_db, servicepath, smb_fname, old_write_time);
529                 if (d == NULL) {
530                         return NT_STATUS_NO_MEMORY;
531                 }
532         } else {
533                 TDB_DATA key = locking_key(&id);
534                 d = parse_share_modes(lock_db, key, value);
535                 if (d == NULL) {
536                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
537                 }
538         }
539
540         d->id = id;
541         d->record = rec;
542
543         static_share_mode_data = d;
544
545         return NT_STATUS_OK;
546 }
547
548 /*******************************************************************
549  Get a share_mode_lock, Reference counted to allow nested calls.
550 ********************************************************************/
551
552 static int share_mode_lock_destructor(struct share_mode_lock *lck);
553
554 struct share_mode_lock *get_share_mode_lock(
555         TALLOC_CTX *mem_ctx,
556         struct file_id id,
557         const char *servicepath,
558         const struct smb_filename *smb_fname,
559         const struct timespec *old_write_time)
560 {
561         TDB_DATA key = locking_key(&id);
562         struct share_mode_lock *lck = NULL;
563         NTSTATUS status;
564
565         lck = talloc(mem_ctx, struct share_mode_lock);
566         if (lck == NULL) {
567                 DEBUG(1, ("talloc failed\n"));
568                 return NULL;
569         }
570
571         if (static_share_mode_data != NULL) {
572                 if (!file_id_equal(&static_share_mode_data->id, &id)) {
573                         DEBUG(1, ("Can not lock two share modes "
574                                   "simultaneously\n"));
575                         goto fail;
576                 }
577                 goto done;
578         }
579
580         SMB_ASSERT(static_share_mode_data_refcount == 0);
581
582         if (static_share_mode_record == NULL) {
583                 static_share_mode_record = dbwrap_fetch_locked(
584                         lock_db, lock_db, key);
585                 if (static_share_mode_record == NULL) {
586                         DEBUG(3, ("Could not lock share entry\n"));
587                         goto fail;
588                 }
589                 static_share_mode_record_talloced = true;
590
591                 status = get_static_share_mode_data(
592                         static_share_mode_record,
593                         id,
594                         servicepath,
595                         smb_fname,
596                         old_write_time);
597                 if (!NT_STATUS_IS_OK(status)) {
598                         DBG_DEBUG("get_static_share_mode_data failed: %s\n",
599                                   nt_errstr(status));
600                         TALLOC_FREE(static_share_mode_record);
601                         goto fail;
602                 }
603         } else {
604                 TDB_DATA static_key;
605                 int cmp;
606
607                 static_key = dbwrap_record_get_key(static_share_mode_record);
608
609                 cmp = tdb_data_cmp(static_key, key);
610                 if (cmp != 0) {
611                         DBG_WARNING("Can not lock two share modes "
612                                     "simultaneously\n");
613                         return NULL;
614                 }
615
616                 status = get_static_share_mode_data(
617                         static_share_mode_record,
618                         id,
619                         servicepath,
620                         smb_fname,
621                         old_write_time);
622                 if (!NT_STATUS_IS_OK(status)) {
623                         DBG_WARNING("get_static_share_mode_data failed: %s\n",
624                                     nt_errstr(status));
625                         goto fail;
626                 }
627         }
628
629 done:
630         static_share_mode_data_refcount += 1;
631         lck->data = static_share_mode_data;
632
633         talloc_set_destructor(lck, share_mode_lock_destructor);
634
635         return lck;
636 fail:
637         TALLOC_FREE(lck);
638         return NULL;
639 }
640
641 static int share_mode_lock_destructor(struct share_mode_lock *lck)
642 {
643         NTSTATUS status;
644
645         SMB_ASSERT(static_share_mode_data_refcount > 0);
646         static_share_mode_data_refcount -= 1;
647
648         if (static_share_mode_data_refcount > 0) {
649                 return 0;
650         }
651
652         status = share_mode_data_store(static_share_mode_data);
653         if (!NT_STATUS_IS_OK(status)) {
654                 DBG_ERR("share_mode_data_store failed: %s\n",
655                         nt_errstr(status));
656                 smb_panic("Could not store share mode data\n");
657         }
658
659         /*
660          * Drop the locking.tdb lock before moving the share_mode_data
661          * to memcache
662          */
663         SMB_ASSERT(static_share_mode_data->record == static_share_mode_record);
664         static_share_mode_data->record = NULL;
665
666         if (static_share_mode_record_talloced) {
667                 TALLOC_FREE(static_share_mode_record);
668         }
669
670         if (static_share_mode_data->num_share_modes != 0) {
671                 /*
672                  * This is worth keeping. Without share modes,
673                  * share_mode_data_store above has left nothing in the
674                  * database.
675                  */
676                 share_mode_memcache_store(static_share_mode_data);
677                 static_share_mode_data = NULL;
678         } else {
679                 /*
680                  * The next opener of this file will find an empty
681                  * locking.tdb record. Don't store the share_mode_data
682                  * in the memcache, fresh_share_mode_lock() will
683                  * generate a fresh seqnum anyway, obsoleting the
684                  * cache entry.
685                  */
686                 TALLOC_FREE(static_share_mode_data);
687         }
688
689         return 0;
690 }
691
692 struct share_mode_do_locked_state {
693         void (*fn)(struct db_record *rec,
694                    bool *modified_dependent,
695                    void *private_data);
696         void *private_data;
697 };
698
699 static void share_mode_do_locked_fn(struct db_record *rec,
700                                     void *private_data)
701 {
702         struct share_mode_do_locked_state *state = private_data;
703         bool modified_dependent = false;
704         bool reset_static_share_mode_record = false;
705
706         if (static_share_mode_record == NULL) {
707                 static_share_mode_record = rec;
708                 static_share_mode_record_talloced = false;
709                 reset_static_share_mode_record = true;
710         } else {
711                 SMB_ASSERT(static_share_mode_record == rec);
712         }
713
714         state->fn(rec, &modified_dependent, state->private_data);
715
716         if (modified_dependent) {
717                 dbwrap_watched_wakeup(rec);
718         }
719
720         if (reset_static_share_mode_record) {
721                 static_share_mode_record = NULL;
722         }
723 }
724
725 NTSTATUS share_mode_do_locked(
726         struct file_id id,
727         void (*fn)(struct db_record *rec,
728                    bool *modified_dependent,
729                    void *private_data),
730         void *private_data)
731 {
732         TDB_DATA key = locking_key(&id);
733         size_t refcount = static_share_mode_data_refcount;
734
735         if (static_share_mode_record != NULL) {
736                 bool modified_dependent = false;
737                 TDB_DATA static_key;
738                 int cmp;
739
740                 static_key = dbwrap_record_get_key(static_share_mode_record);
741
742                 cmp = tdb_data_cmp(static_key, key);
743                 if (cmp != 0) {
744                         DBG_WARNING("Can not lock two share modes "
745                                     "simultaneously\n");
746                         return NT_STATUS_INVALID_LOCK_SEQUENCE;
747                 }
748
749                 fn(static_share_mode_record,
750                    &modified_dependent,
751                    private_data);
752
753                 if (modified_dependent) {
754                         dbwrap_watched_wakeup(static_share_mode_record);
755                 }
756         } else {
757                 struct share_mode_do_locked_state state = {
758                         .fn = fn, .private_data = private_data,
759                 };
760                 NTSTATUS status;
761
762                 status = dbwrap_do_locked(
763                         lock_db, key, share_mode_do_locked_fn, &state);
764                 if (!NT_STATUS_IS_OK(status)) {
765                         DBG_WARNING("dbwrap_do_locked failed: %s\n",
766                                     nt_errstr(status));
767                         return status;
768                 }
769         }
770
771         SMB_ASSERT(refcount == static_share_mode_data_refcount);
772
773         return NT_STATUS_OK;
774 }
775
776 static void share_mode_wakeup_waiters_fn(struct db_record *rec,
777                                          bool *modified_dependent,
778                                          void *private_data)
779 {
780         *modified_dependent = true;
781 }
782
783 NTSTATUS share_mode_wakeup_waiters(struct file_id id)
784 {
785         return share_mode_do_locked(id, share_mode_wakeup_waiters_fn, NULL);
786 }
787
788 struct fetch_share_mode_unlocked_state {
789         TALLOC_CTX *mem_ctx;
790         struct share_mode_lock *lck;
791 };
792
793 static void fetch_share_mode_unlocked_parser(
794         TDB_DATA key, TDB_DATA data, void *private_data)
795 {
796         struct fetch_share_mode_unlocked_state *state = private_data;
797
798         if (data.dsize == 0) {
799                 /* Likely a ctdb tombstone record, ignore it */
800                 return;
801         }
802
803         state->lck = talloc(state->mem_ctx, struct share_mode_lock);
804         if (state->lck == NULL) {
805                 DEBUG(0, ("talloc failed\n"));
806                 return;
807         }
808
809         state->lck->data = parse_share_modes(state->lck, key, data);
810 }
811
812 /*******************************************************************
813  Get a share_mode_lock without locking the database or reference
814  counting. Used by smbstatus to display existing share modes.
815 ********************************************************************/
816
817 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
818                                                   struct file_id id)
819 {
820         struct fetch_share_mode_unlocked_state state = { .mem_ctx = mem_ctx };
821         TDB_DATA key = locking_key(&id);
822         NTSTATUS status;
823
824         status = dbwrap_parse_record(
825                 lock_db, key, fetch_share_mode_unlocked_parser, &state);
826         if (!NT_STATUS_IS_OK(status)) {
827                 return NULL;
828         }
829         return state.lck;
830 }
831
832 static void fetch_share_mode_done(struct tevent_req *subreq);
833
834 struct fetch_share_mode_state {
835         struct file_id id;
836         TDB_DATA key;
837         struct fetch_share_mode_unlocked_state parser_state;
838         enum dbwrap_req_state req_state;
839 };
840
841 /**
842  * @brief Get a share_mode_lock without locking or refcounting
843  *
844  * This can be used in a clustered Samba environment where the async dbwrap
845  * request is sent over a socket to the local ctdbd. If the send queue is full
846  * and the caller was issuing multiple async dbwrap requests in a loop, the
847  * caller knows it's probably time to stop sending requests for now and try
848  * again later.
849  *
850  * @param[in]  mem_ctx The talloc memory context to use.
851  *
852  * @param[in]  ev      The event context to work on.
853  *
854  * @param[in]  id      The file id for the locking.tdb key
855  *
856  * @param[out] queued  This boolean out parameter tells the caller whether the
857  *                     async request is blocked in a full send queue:
858  *
859  *                     false := request is dispatched
860  *
861  *                     true  := send queue is full, request waiting to be
862  *                              dispatched
863  *
864  * @return             The new async request, NULL on error.
865  **/
866 struct tevent_req *fetch_share_mode_send(TALLOC_CTX *mem_ctx,
867                                          struct tevent_context *ev,
868                                          struct file_id id,
869                                          bool *queued)
870 {
871         struct tevent_req *req = NULL;
872         struct fetch_share_mode_state *state = NULL;
873         struct tevent_req *subreq = NULL;
874
875         *queued = false;
876
877         req = tevent_req_create(mem_ctx, &state,
878                                 struct fetch_share_mode_state);
879         if (req == NULL) {
880                 return NULL;
881         }
882
883         state->id = id;
884         state->key = locking_key(&state->id);
885         state->parser_state.mem_ctx = state;
886
887         subreq = dbwrap_parse_record_send(state,
888                                           ev,
889                                           lock_db,
890                                           state->key,
891                                           fetch_share_mode_unlocked_parser,
892                                           &state->parser_state,
893                                           &state->req_state);
894         if (tevent_req_nomem(subreq, req)) {
895                 return tevent_req_post(req, ev);
896         }
897         tevent_req_set_callback(subreq, fetch_share_mode_done, req);
898
899         if (state->req_state < DBWRAP_REQ_DISPATCHED) {
900                 *queued = true;
901         }
902         return req;
903 }
904
905 static void fetch_share_mode_done(struct tevent_req *subreq)
906 {
907         struct tevent_req *req = tevent_req_callback_data(
908                 subreq, struct tevent_req);
909         NTSTATUS status;
910
911         status = dbwrap_parse_record_recv(subreq);
912         TALLOC_FREE(subreq);
913         if (tevent_req_nterror(req, status)) {
914                 return;
915         }
916
917         tevent_req_done(req);
918         return;
919 }
920
921 NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
922                                TALLOC_CTX *mem_ctx,
923                                struct share_mode_lock **_lck)
924 {
925         struct fetch_share_mode_state *state = tevent_req_data(
926                 req, struct fetch_share_mode_state);
927         struct share_mode_lock *lck = NULL;
928
929         NTSTATUS status;
930
931         if (tevent_req_is_nterror(req, &status)) {
932                 tevent_req_received(req);
933                 return status;
934         }
935
936         if (state->parser_state.lck->data == NULL) {
937                 tevent_req_received(req);
938                 return NT_STATUS_NOT_FOUND;
939         }
940
941         lck = talloc_move(mem_ctx, &state->parser_state.lck);
942
943         if (DEBUGLEVEL >= 10) {
944                 DBG_DEBUG("share_mode_data:\n");
945                 NDR_PRINT_DEBUG(share_mode_data, lck->data);
946         }
947
948         *_lck = lck;
949         tevent_req_received(req);
950         return NT_STATUS_OK;
951 }
952
953 struct share_mode_forall_state {
954         int (*fn)(struct file_id fid, const struct share_mode_data *data,
955                   void *private_data);
956         void *private_data;
957 };
958
959 static int share_mode_traverse_fn(struct db_record *rec, void *_state)
960 {
961         struct share_mode_forall_state *state =
962                 (struct share_mode_forall_state *)_state;
963         TDB_DATA key;
964         TDB_DATA value;
965         DATA_BLOB blob;
966         enum ndr_err_code ndr_err;
967         struct share_mode_data *d;
968         struct file_id fid;
969         int ret;
970
971         key = dbwrap_record_get_key(rec);
972         value = dbwrap_record_get_value(rec);
973
974         /* Ensure this is a locking_key record. */
975         if (key.dsize != sizeof(fid)) {
976                 return 0;
977         }
978         memcpy(&fid, key.dptr, sizeof(fid));
979
980         d = talloc(talloc_tos(), struct share_mode_data);
981         if (d == NULL) {
982                 return 0;
983         }
984
985         blob.data = value.dptr;
986         blob.length = value.dsize;
987
988         ndr_err = ndr_pull_struct_blob_all(
989                 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
990         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
991                 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
992                 return 0;
993         }
994
995         if (DEBUGLEVEL > 10) {
996                 DEBUG(11, ("parse_share_modes:\n"));
997                 NDR_PRINT_DEBUG(share_mode_data, d);
998         }
999
1000         ret = state->fn(fid, d, state->private_data);
1001
1002         TALLOC_FREE(d);
1003         return ret;
1004 }
1005
1006 int share_mode_forall(int (*fn)(struct file_id fid,
1007                                 const struct share_mode_data *data,
1008                                 void *private_data),
1009                       void *private_data)
1010 {
1011         struct share_mode_forall_state state = {
1012                 .fn = fn,
1013                 .private_data = private_data
1014         };
1015         NTSTATUS status;
1016         int count;
1017
1018         if (lock_db == NULL) {
1019                 return 0;
1020         }
1021
1022         status = dbwrap_traverse_read(lock_db, share_mode_traverse_fn,
1023                                       &state, &count);
1024         if (!NT_STATUS_IS_OK(status)) {
1025                 return -1;
1026         }
1027
1028         return count;
1029 }
1030
1031 struct share_entry_forall_state {
1032         struct file_id fid;
1033         const struct share_mode_data *data;
1034         int (*fn)(struct file_id fid,
1035                   const struct share_mode_data *data,
1036                   const struct share_mode_entry *entry,
1037                   void *private_data);
1038         void *private_data;
1039         int ret;
1040 };
1041
1042 static bool share_entry_traverse_walker(
1043         struct share_mode_entry *e,
1044         bool *modified,
1045         void *private_data)
1046 {
1047         struct share_entry_forall_state *state = private_data;
1048
1049         state->ret = state->fn(
1050                 state->fid, state->data, e, state->private_data);
1051         return (state->ret != 0);
1052 }
1053
1054 static int share_entry_traverse_fn(struct file_id fid,
1055                                    const struct share_mode_data *data,
1056                                    void *private_data)
1057 {
1058         struct share_entry_forall_state *state = private_data;
1059         struct share_mode_lock lck = {
1060                 .data = discard_const_p(struct share_mode_data, data)
1061         };
1062         bool ok;
1063
1064         state->fid = fid;
1065         state->data = data;
1066
1067         ok = share_mode_forall_entries(
1068                 &lck, share_entry_traverse_walker, state);
1069         if (!ok) {
1070                 DBG_DEBUG("share_mode_forall_entries failed\n");
1071                 return false;
1072         }
1073
1074         return state->ret;
1075 }
1076
1077 /*******************************************************************
1078  Call the specified function on each entry under management by the
1079  share mode system.
1080 ********************************************************************/
1081
1082 int share_entry_forall(int (*fn)(struct file_id fid,
1083                                  const struct share_mode_data *data,
1084                                  const struct share_mode_entry *entry,
1085                                  void *private_data),
1086                       void *private_data)
1087 {
1088         struct share_entry_forall_state state = {
1089                 .fn = fn, .private_data = private_data };
1090
1091         return share_mode_forall(share_entry_traverse_fn, &state);
1092 }
1093
1094 struct cleanup_disconnected_state {
1095         struct share_mode_lock *lck;
1096         uint64_t open_persistent_id;
1097         bool found_connected;
1098 };
1099
1100 static bool cleanup_disconnected_lease(struct share_mode_entry *e,
1101                                        void *private_data)
1102 {
1103         struct cleanup_disconnected_state *state = private_data;
1104         NTSTATUS status;
1105
1106         status = leases_db_del(
1107                 &e->client_guid, &e->lease_key, &state->lck->data->id);
1108
1109         if (!NT_STATUS_IS_OK(status)) {
1110                 DBG_DEBUG("leases_db_del failed: %s\n",
1111                           nt_errstr(status));
1112         }
1113
1114         return false;
1115 }
1116
1117 static bool share_mode_cleanup_disconnected_fn(
1118         struct share_mode_entry *e,
1119         bool *modified,
1120         void *private_data)
1121 {
1122         struct cleanup_disconnected_state *state = private_data;
1123         struct share_mode_data *d = state->lck->data;
1124         bool disconnected;
1125
1126         disconnected = server_id_is_disconnected(&e->pid);
1127         if (!disconnected) {
1128                 struct file_id_buf tmp1;
1129                 struct server_id_buf tmp2;
1130                 DBG_INFO("file (file-id='%s', servicepath='%s', "
1131                          "base_name='%s%s%s') "
1132                          "is used by server %s ==> do not cleanup\n",
1133                          file_id_str_buf(d->id, &tmp1),
1134                          d->servicepath,
1135                          d->base_name,
1136                          (d->stream_name == NULL)
1137                          ? "" : "', stream_name='",
1138                          (d->stream_name == NULL)
1139                          ? "" : d->stream_name,
1140                          server_id_str_buf(e->pid, &tmp2));
1141                 state->found_connected = true;
1142                 return true;
1143         }
1144
1145         if (state->open_persistent_id != e->share_file_id) {
1146                 struct file_id_buf tmp;
1147                 DBG_INFO("entry for file "
1148                          "(file-id='%s', servicepath='%s', "
1149                          "base_name='%s%s%s') "
1150                          "has share_file_id %"PRIu64" but expected "
1151                          "%"PRIu64"==> do not cleanup\n",
1152                          file_id_str_buf(d->id, &tmp),
1153                          d->servicepath,
1154                          d->base_name,
1155                          (d->stream_name == NULL)
1156                          ? "" : "', stream_name='",
1157                          (d->stream_name == NULL)
1158                          ? "" : d->stream_name,
1159                          e->share_file_id,
1160                          state->open_persistent_id);
1161                 state->found_connected = true;
1162                 return true;
1163         }
1164
1165         return false;
1166 }
1167
1168 bool share_mode_cleanup_disconnected(struct file_id fid,
1169                                      uint64_t open_persistent_id)
1170 {
1171         struct cleanup_disconnected_state state = {
1172                 .open_persistent_id = open_persistent_id
1173         };
1174         struct share_mode_data *data;
1175         bool ret = false;
1176         TALLOC_CTX *frame = talloc_stackframe();
1177         bool ok;
1178
1179         state.lck = get_existing_share_mode_lock(frame, fid);
1180         if (state.lck == NULL) {
1181                 DEBUG(5, ("share_mode_cleanup_disconnected: "
1182                           "Could not fetch share mode entry for %s\n",
1183                           file_id_string(frame, &fid)));
1184                 goto done;
1185         }
1186         data = state.lck->data;
1187
1188         ok = share_mode_forall_entries(
1189                 state.lck, share_mode_cleanup_disconnected_fn, &state);
1190         if (!ok) {
1191                 DBG_DEBUG("share_mode_forall_entries failed\n");
1192                 goto done;
1193         }
1194         if (state.found_connected) {
1195                 DBG_DEBUG("Found connected entry\n");
1196                 goto done;
1197         }
1198
1199         ok = share_mode_forall_leases(
1200                 state.lck, cleanup_disconnected_lease, &state);
1201         if (!ok) {
1202                 DBG_DEBUG("failed to clean up leases associated "
1203                           "with file (file-id='%s', servicepath='%s', "
1204                           "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1205                           "==> do not cleanup\n",
1206                           file_id_string(frame, &fid),
1207                           data->servicepath,
1208                           data->base_name,
1209                           (data->stream_name == NULL)
1210                           ? "" : "', stream_name='",
1211                           (data->stream_name == NULL)
1212                           ? "" : data->stream_name,
1213                           open_persistent_id);
1214         }
1215
1216         ok = brl_cleanup_disconnected(fid, open_persistent_id);
1217         if (!ok) {
1218                 DBG_DEBUG("failed to clean up byte range locks associated "
1219                           "with file (file-id='%s', servicepath='%s', "
1220                           "base_name='%s%s%s') and open_persistent_id %"PRIu64" "
1221                           "==> do not cleanup\n",
1222                           file_id_string(frame, &fid),
1223                           data->servicepath,
1224                           data->base_name,
1225                           (data->stream_name == NULL)
1226                           ? "" : "', stream_name='",
1227                           (data->stream_name == NULL)
1228                           ? "" : data->stream_name,
1229                           open_persistent_id);
1230                 goto done;
1231         }
1232
1233         DBG_DEBUG("cleaning up %u entries for file "
1234                   "(file-id='%s', servicepath='%s', "
1235                   "base_name='%s%s%s') "
1236                   "from open_persistent_id %"PRIu64"\n",
1237                   data->num_share_modes,
1238                   file_id_string(frame, &fid),
1239                   data->servicepath,
1240                   data->base_name,
1241                   (data->stream_name == NULL)
1242                   ? "" : "', stream_name='",
1243                   (data->stream_name == NULL)
1244                   ? "" : data->stream_name,
1245                   open_persistent_id);
1246
1247         data->num_share_modes = 0;
1248         data->modified = true;
1249
1250         ret = true;
1251 done:
1252         talloc_free(frame);
1253         return ret;
1254 }