2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2006
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
32 rewritten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
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"
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"
54 #define DBGC_CLASS DBGC_LOCKING
56 #define NO_LOCKING_COUNT (-1)
58 /* the locking database handle */
59 static struct db_context
*lock_db
;
61 static bool locking_init_internal(bool read_only
)
70 db_path
= lock_path("locking.tdb");
71 if (db_path
== NULL
) {
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
);
82 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
86 if (!posix_locking_init(read_only
))
89 dbwrap_watch_db(lock_db
, server_messaging_context());
94 bool locking_init(void)
96 return locking_init_internal(false);
99 bool locking_init_readonly(void)
101 return locking_init_internal(true);
104 /*******************************************************************
105 Deinitialize the share_mode management.
106 ******************************************************************/
108 bool locking_end(void)
111 TALLOC_FREE(lock_db
);
115 /*******************************************************************
116 Form a static locking key for a dev/inode pair.
117 ******************************************************************/
119 static TDB_DATA
locking_key(const struct file_id
*id
)
121 return make_tdb_data((const uint8_t *)id
, sizeof(*id
));
124 /*******************************************************************
125 Share mode cache utility functions that store/delete/retrieve
126 entries from memcache.
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 ******************************************************************/
135 static const DATA_BLOB
memcache_key(const struct file_id
*id
)
137 return data_blob_const((const void *)id
, sizeof(*id
));
140 static void share_mode_memcache_delete(struct share_mode_data
*d
)
142 const DATA_BLOB key
= memcache_key(&d
->id
);
144 DEBUG(10,("deleting entry for file %s seq 0x%llu key %s\n",
146 (unsigned long long) d
->sequence_number
,
147 file_id_string(talloc_tos(), &d
->id
)));
149 memcache_delete(NULL
,
150 SHARE_MODE_LOCK_CACHE
,
154 static void share_mode_memcache_store(struct share_mode_data
*d
)
156 const DATA_BLOB key
= memcache_key(&d
->id
);
158 DEBUG(10,("stored entry for file %s seq 0x%llu key %s\n",
160 (unsigned long long) d
->sequence_number
,
161 file_id_string(talloc_tos(), &d
->id
)));
163 /* Ensure everything stored in the cache is pristine. */
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().
172 talloc_set_destructor(d
, NULL
);
174 /* Cache will own d after this call. */
175 memcache_add_talloc(NULL
,
176 SHARE_MODE_LOCK_CACHE
,
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.
188 static enum ndr_err_code
get_blob_sequence_number(DATA_BLOB
*blob
,
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
;
196 static int share_mode_data_nofree_destructor(struct share_mode_data
*d
)
201 static struct share_mode_data
*share_mode_memcache_fetch(TALLOC_CTX
*mem_ctx
,
202 const TDB_DATA id_key
,
205 enum ndr_err_code ndr_err
;
206 struct share_mode_data
*d
;
207 uint64_t sequence_number
;
212 /* Ensure this is a locking_key record. */
213 if (id_key
.dsize
!= sizeof(id
)) {
217 memcpy(&id
, id_key
.dptr
, id_key
.dsize
);
218 key
= memcache_key(&id
);
220 ptr
= memcache_lookup_talloc(NULL
,
221 SHARE_MODE_LOCK_CACHE
,
224 DEBUG(10,("failed to find entry for key %s\n",
225 file_id_string(mem_ctx
, &id
)));
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
,
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) "
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
,
255 /* Move onto mem_ctx. */
256 d
= talloc_move(mem_ctx
, &ptr
);
259 * Now we own d, prevent the cache from freeing it
260 * when we delete the entry.
262 talloc_set_destructor(d
, share_mode_data_nofree_destructor
);
264 /* Remove from the cache. We own it now. */
265 memcache_delete(NULL
,
266 SHARE_MODE_LOCK_CACHE
,
269 /* And reset the destructor to none. */
270 talloc_set_destructor(d
, NULL
);
272 DEBUG(10,("fetched entry for file %s seq 0x%llu key %s\n",
274 (unsigned long long)d
->sequence_number
,
275 file_id_string(mem_ctx
, &id
)));
280 /*******************************************************************
281 Get all share mode entries for a dev/inode pair.
282 ********************************************************************/
284 static struct share_mode_data
*parse_share_modes(TALLOC_CTX
*mem_ctx
,
288 struct share_mode_data
*d
;
289 enum ndr_err_code ndr_err
;
293 blob
.data
= dbuf
.dptr
;
294 blob
.length
= dbuf
.dsize
;
296 /* See if we already have a cached copy of this key. */
297 d
= share_mode_memcache_fetch(mem_ctx
, key
, &blob
);
302 d
= talloc(mem_ctx
, struct share_mode_data
);
304 DEBUG(0, ("talloc failed\n"));
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
)));
317 * Initialize the values that are [skip] in the idl. The NDR code does
318 * not initialize them.
321 for (i
=0; i
<d
->num_share_modes
; i
++) {
322 struct share_mode_entry
*e
= &d
->share_modes
[i
];
326 if (e
->op_type
!= LEASE_OPLOCK
) {
329 if (e
->lease_idx
>= d
->num_leases
) {
332 e
->lease
= &d
->leases
[e
->lease_idx
];
337 if (DEBUGLEVEL
>= 10) {
338 DEBUG(10, ("parse_share_modes:\n"));
339 NDR_PRINT_DEBUG(share_mode_data
, d
);
348 /*******************************************************************
349 Create a storable data blob from a modified share_mode_data struct.
350 ********************************************************************/
352 static TDB_DATA
unparse_share_modes(struct share_mode_data
*d
)
355 enum ndr_err_code ndr_err
;
357 if (DEBUGLEVEL
>= 10) {
358 DEBUG(10, ("unparse_share_modes:\n"));
359 NDR_PRINT_DEBUG(share_mode_data
, d
);
362 share_mode_memcache_delete(d
);
364 /* Update the sequence number. */
365 d
->sequence_number
+= 1;
367 remove_stale_share_mode_entries(d
);
369 if (d
->num_share_modes
== 0) {
370 DEBUG(10, ("No used share mode found\n"));
371 return make_tdb_data(NULL
, 0);
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");
380 return make_tdb_data(blob
.data
, blob
.length
);
383 /*******************************************************************
384 If modified, store the share_mode_data back into the database.
385 ********************************************************************/
387 static int share_mode_data_destructor(struct share_mode_data
*d
)
396 data
= unparse_share_modes(d
);
398 if (data
.dptr
== NULL
) {
400 /* There has been an entry before, delete it */
402 status
= dbwrap_record_delete(d
->record
);
403 if (!NT_STATUS_IS_OK(status
)) {
406 DEBUG(0, ("delete_rec returned %s\n",
409 if (asprintf(&errmsg
, "could not delete share "
411 nt_errstr(status
)) == -1) {
412 smb_panic("could not delete share"
419 * Nothing to store in cache - allow the normal
420 * release of record lock and memory free.
425 status
= dbwrap_record_store(d
->record
, data
, TDB_REPLACE
);
426 if (!NT_STATUS_IS_OK(status
)) {
429 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
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");
439 * Release the record lock before putting in the cache.
441 TALLOC_FREE(d
->record
);
444 * Release the dptr as well before reparenting to NULL
445 * (in-memory cache) context.
447 TALLOC_FREE(data
.dptr
);
449 * Reparent d into the in-memory cache so it can be reused if the
450 * sequence number matches. See parse_share_modes()
454 share_mode_memcache_store(d
);
458 /*******************************************************************
459 Allocate a new share_mode_data struct, mark it unmodified.
460 fresh is set to note that currently there is no database entry.
461 ********************************************************************/
463 static struct share_mode_data
*fresh_share_mode_lock(
464 TALLOC_CTX
*mem_ctx
, const char *servicepath
,
465 const struct smb_filename
*smb_fname
,
466 const struct timespec
*old_write_time
)
468 struct share_mode_data
*d
;
470 if ((servicepath
== NULL
) || (smb_fname
== NULL
) ||
471 (old_write_time
== NULL
)) {
475 d
= talloc_zero(mem_ctx
, struct share_mode_data
);
479 /* New record - new sequence number. */
480 generate_random_buffer((uint8_t *)&d
->sequence_number
, 8);
482 d
->base_name
= talloc_strdup(d
, smb_fname
->base_name
);
483 if (d
->base_name
== NULL
) {
486 if (smb_fname
->stream_name
!= NULL
) {
487 d
->stream_name
= talloc_strdup(d
, smb_fname
->stream_name
);
488 if (d
->stream_name
== NULL
) {
492 d
->servicepath
= talloc_strdup(d
, servicepath
);
493 if (d
->servicepath
== NULL
) {
496 d
->old_write_time
= *old_write_time
;
501 DEBUG(0, ("talloc failed\n"));
506 /*******************************************************************
507 Either fetch a share mode from the database, or allocate a fresh
508 one if the record doesn't exist.
509 ********************************************************************/
511 static struct share_mode_lock
*get_share_mode_lock_internal(
512 TALLOC_CTX
*mem_ctx
, struct file_id id
,
513 const char *servicepath
, const struct smb_filename
*smb_fname
,
514 const struct timespec
*old_write_time
)
516 struct share_mode_lock
*lck
;
517 struct share_mode_data
*d
;
518 struct db_record
*rec
;
519 TDB_DATA key
= locking_key(&id
);
522 rec
= dbwrap_fetch_locked(lock_db
, mem_ctx
, key
);
524 DEBUG(3, ("Could not lock share entry\n"));
528 value
= dbwrap_record_get_value(rec
);
530 if (value
.dptr
== NULL
) {
531 d
= fresh_share_mode_lock(mem_ctx
, servicepath
, smb_fname
,
534 d
= parse_share_modes(mem_ctx
, key
, value
);
538 DEBUG(5, ("get_share_mode_lock_internal: "
539 "Could not get share mode lock\n"));
544 d
->record
= talloc_move(d
, &rec
);
545 talloc_set_destructor(d
, share_mode_data_destructor
);
547 lck
= talloc(mem_ctx
, struct share_mode_lock
);
549 DEBUG(1, ("talloc failed\n"));
553 lck
->data
= talloc_move(lck
, &d
);
558 * We can only ever have one share mode locked. Users of
559 * get_share_mode_lock never see this, it will be refcounted by
562 static struct share_mode_lock
*the_lock
;
563 static struct file_id the_lock_id
;
565 static int the_lock_destructor(struct share_mode_lock
*l
)
568 ZERO_STRUCT(the_lock_id
);
572 /*******************************************************************
573 Get a share_mode_lock, Reference counted to allow nested calls.
574 ********************************************************************/
576 struct share_mode_lock
*get_share_mode_lock(
579 const char *servicepath
,
580 const struct smb_filename
*smb_fname
,
581 const struct timespec
*old_write_time
)
583 struct share_mode_lock
*lck
;
585 lck
= talloc(mem_ctx
, struct share_mode_lock
);
587 DEBUG(1, ("talloc failed\n"));
591 if (the_lock
== NULL
) {
592 the_lock
= get_share_mode_lock_internal(
593 lck
, id
, servicepath
, smb_fname
, old_write_time
);
594 if (the_lock
== NULL
) {
597 talloc_set_destructor(the_lock
, the_lock_destructor
);
600 if (!file_id_equal(&the_lock_id
, &id
)) {
601 DEBUG(1, ("Can not lock two share modes "
602 "simultaneously\n"));
605 if (talloc_reference(lck
, the_lock
) == NULL
) {
606 DEBUG(1, ("talloc_reference failed\n"));
610 lck
->data
= the_lock
->data
;
617 static void fetch_share_mode_unlocked_parser(
618 TDB_DATA key
, TDB_DATA data
, void *private_data
)
620 struct share_mode_lock
*lck
= talloc_get_type_abort(
621 private_data
, struct share_mode_lock
);
623 lck
->data
= parse_share_modes(lck
, key
, data
);
626 /*******************************************************************
627 Get a share_mode_lock without locking the database or reference
628 counting. Used by smbstatus to display existing share modes.
629 ********************************************************************/
631 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
634 struct share_mode_lock
*lck
;
635 TDB_DATA key
= locking_key(&id
);
638 lck
= talloc(mem_ctx
, struct share_mode_lock
);
640 DEBUG(0, ("talloc failed\n"));
643 status
= dbwrap_parse_record(
644 lock_db
, key
, fetch_share_mode_unlocked_parser
, lck
);
645 if (!NT_STATUS_IS_OK(status
) ||
646 (lck
->data
== NULL
)) {
653 struct share_mode_forall_state
{
654 int (*fn
)(struct file_id fid
, const struct share_mode_data
*data
,
659 static int share_mode_traverse_fn(struct db_record
*rec
, void *_state
)
661 struct share_mode_forall_state
*state
=
662 (struct share_mode_forall_state
*)_state
;
667 enum ndr_err_code ndr_err
;
668 struct share_mode_data
*d
;
672 key
= dbwrap_record_get_key(rec
);
673 value
= dbwrap_record_get_value(rec
);
675 /* Ensure this is a locking_key record. */
676 if (key
.dsize
!= sizeof(fid
)) {
679 memcpy(&fid
, key
.dptr
, sizeof(fid
));
681 d
= talloc(talloc_tos(), struct share_mode_data
);
686 blob
.data
= value
.dptr
;
687 blob
.length
= value
.dsize
;
689 ndr_err
= ndr_pull_struct_blob_all(
690 &blob
, d
, d
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_data
);
691 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
692 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
696 for (i
=0; i
<d
->num_share_modes
; i
++) {
697 struct share_mode_entry
*entry
= &d
->share_modes
[i
];
698 entry
->stale
= false; /* [skip] in idl */
699 entry
->lease
= &d
->leases
[entry
->lease_idx
];
702 if (DEBUGLEVEL
> 10) {
703 DEBUG(11, ("parse_share_modes:\n"));
704 NDR_PRINT_DEBUG(share_mode_data
, d
);
707 ret
= state
->fn(fid
, d
, state
->private_data
);
713 int share_mode_forall(int (*fn
)(struct file_id fid
,
714 const struct share_mode_data
*data
,
718 struct share_mode_forall_state state
= {
720 .private_data
= private_data
725 if (lock_db
== NULL
) {
729 status
= dbwrap_traverse_read(lock_db
, share_mode_traverse_fn
,
731 if (!NT_STATUS_IS_OK(status
)) {
738 struct share_entry_forall_state
{
739 int (*fn
)(const struct share_mode_entry
*e
,
740 const char *service_path
,
741 const char *base_name
,
742 const char *stream_name
,
747 static int share_entry_traverse_fn(struct file_id fid
,
748 const struct share_mode_data
*data
,
751 struct share_entry_forall_state
*state
= private_data
;
754 for (i
=0; i
<data
->num_share_modes
; i
++) {
757 ret
= state
->fn(&data
->share_modes
[i
],
761 state
->private_data
);
770 /*******************************************************************
771 Call the specified function on each entry under management by the
773 ********************************************************************/
775 int share_entry_forall(int (*fn
)(const struct share_mode_entry
*,
776 const char *, const char *,
777 const char *, void *),
780 struct share_entry_forall_state state
= {
781 .fn
= fn
, .private_data
= private_data
};
783 return share_mode_forall(share_entry_traverse_fn
, &state
);
786 bool share_mode_cleanup_disconnected(struct file_id fid
,
787 uint64_t open_persistent_id
)
790 TALLOC_CTX
*frame
= talloc_stackframe();
792 struct share_mode_data
*data
;
793 struct share_mode_lock
*lck
;
796 lck
= get_existing_share_mode_lock(frame
, fid
);
798 DEBUG(5, ("share_mode_cleanup_disconnected: "
799 "Could not fetch share mode entry for %s\n",
800 file_id_string(frame
, &fid
)));
805 for (n
=0; n
< data
->num_share_modes
; n
++) {
806 struct share_mode_entry
*entry
= &data
->share_modes
[n
];
808 if (!server_id_is_disconnected(&entry
->pid
)) {
809 struct server_id_buf tmp
;
810 DEBUG(5, ("share_mode_cleanup_disconnected: "
811 "file (file-id='%s', servicepath='%s', "
812 "base_name='%s%s%s') "
813 "is used by server %s ==> do not cleanup\n",
814 file_id_string(frame
, &fid
),
817 (data
->stream_name
== NULL
)
818 ? "" : "', stream_name='",
819 (data
->stream_name
== NULL
)
820 ? "" : data
->stream_name
,
821 server_id_str_buf(entry
->pid
, &tmp
)));
824 if (open_persistent_id
!= entry
->share_file_id
) {
825 DEBUG(5, ("share_mode_cleanup_disconnected: "
827 "(file-id='%s', servicepath='%s', "
828 "base_name='%s%s%s') "
829 "has share_file_id %llu but expected %llu"
830 "==> do not cleanup\n",
831 file_id_string(frame
, &fid
),
834 (data
->stream_name
== NULL
)
835 ? "" : "', stream_name='",
836 (data
->stream_name
== NULL
)
837 ? "" : data
->stream_name
,
838 (unsigned long long)entry
->share_file_id
,
839 (unsigned long long)open_persistent_id
));
844 for (n
=0; n
< data
->num_leases
; n
++) {
845 struct share_mode_lease
*l
= &data
->leases
[n
];
848 status
= leases_db_del(&l
->client_guid
, &l
->lease_key
, &fid
);
850 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__
,
854 ok
= brl_cleanup_disconnected(fid
, open_persistent_id
);
856 DEBUG(10, ("share_mode_cleanup_disconnected: "
857 "failed to clean up byte range locks associated "
858 "with file (file-id='%s', servicepath='%s', "
859 "base_name='%s%s%s') and open_persistent_id %llu "
860 "==> do not cleanup\n",
861 file_id_string(frame
, &fid
),
864 (data
->stream_name
== NULL
)
865 ? "" : "', stream_name='",
866 (data
->stream_name
== NULL
)
867 ? "" : data
->stream_name
,
868 (unsigned long long)open_persistent_id
));
872 DEBUG(10, ("share_mode_cleanup_disconnected: "
873 "cleaning up %u entries for file "
874 "(file-id='%s', servicepath='%s', "
875 "base_name='%s%s%s') "
876 "from open_persistent_id %llu\n",
877 data
->num_share_modes
,
878 file_id_string(frame
, &fid
),
881 (data
->stream_name
== NULL
)
882 ? "" : "', stream_name='",
883 (data
->stream_name
== NULL
)
884 ? "" : data
->stream_name
,
885 (unsigned long long)open_persistent_id
));
887 data
->num_share_modes
= 0;
888 data
->num_leases
= 0;
889 data
->modified
= true;