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"
53 #define DBGC_CLASS DBGC_LOCKING
55 #define NO_LOCKING_COUNT (-1)
57 /* the locking database handle */
58 static struct db_context
*lock_db
;
60 static bool locking_init_internal(bool read_only
)
67 lock_db
= db_open(NULL
, lock_path("locking.tdb"),
68 SMB_OPEN_DATABASE_TDB_HASH_SIZE
,
69 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
70 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644,
71 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
74 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
78 if (!posix_locking_init(read_only
))
81 dbwrap_watch_db(lock_db
, server_messaging_context());
86 bool locking_init(void)
88 return locking_init_internal(false);
91 bool locking_init_readonly(void)
93 return locking_init_internal(true);
96 /*******************************************************************
97 Deinitialize the share_mode management.
98 ******************************************************************/
100 bool locking_end(void)
103 TALLOC_FREE(lock_db
);
107 /*******************************************************************
108 Form a static locking key for a dev/inode pair.
109 ******************************************************************/
111 static TDB_DATA
locking_key(const struct file_id
*id
)
113 return make_tdb_data((const uint8_t *)id
, sizeof(*id
));
116 /*******************************************************************
117 Get all share mode entries for a dev/inode pair.
118 ********************************************************************/
120 static struct share_mode_data
*parse_share_modes(TALLOC_CTX
*mem_ctx
,
123 struct share_mode_data
*d
;
124 enum ndr_err_code ndr_err
;
128 d
= talloc(mem_ctx
, struct share_mode_data
);
130 DEBUG(0, ("talloc failed\n"));
134 blob
.data
= dbuf
.dptr
;
135 blob
.length
= dbuf
.dsize
;
137 ndr_err
= ndr_pull_struct_blob_all(
138 &blob
, d
, d
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_data
);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
140 DEBUG(1, ("ndr_pull_share_mode_lock failed: %s\n",
141 ndr_errstr(ndr_err
)));
146 * Initialize the values that are [skip] in the idl. The NDR code does
147 * not initialize them.
150 for (i
=0; i
<d
->num_share_modes
; i
++) {
151 struct share_mode_entry
*e
= &d
->share_modes
[i
];
155 if (e
->op_type
!= LEASE_OPLOCK
) {
158 if (e
->lease_idx
>= d
->num_leases
) {
161 e
->lease
= &d
->leases
[e
->lease_idx
];
166 if (DEBUGLEVEL
>= 10) {
167 DEBUG(10, ("parse_share_modes:\n"));
168 NDR_PRINT_DEBUG(share_mode_data
, d
);
177 /*******************************************************************
178 Create a storable data blob from a modified share_mode_data struct.
179 ********************************************************************/
181 static TDB_DATA
unparse_share_modes(struct share_mode_data
*d
)
184 enum ndr_err_code ndr_err
;
186 if (DEBUGLEVEL
>= 10) {
187 DEBUG(10, ("unparse_share_modes:\n"));
188 NDR_PRINT_DEBUG(share_mode_data
, d
);
191 remove_stale_share_mode_entries(d
);
193 if (d
->num_share_modes
== 0) {
194 DEBUG(10, ("No used share mode found\n"));
195 return make_tdb_data(NULL
, 0);
198 ndr_err
= ndr_push_struct_blob(
199 &blob
, d
, d
, (ndr_push_flags_fn_t
)ndr_push_share_mode_data
);
200 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
201 smb_panic("ndr_push_share_mode_lock failed");
204 return make_tdb_data(blob
.data
, blob
.length
);
207 /*******************************************************************
208 If modified, store the share_mode_data back into the database.
209 ********************************************************************/
211 static int share_mode_data_destructor(struct share_mode_data
*d
)
220 data
= unparse_share_modes(d
);
222 if (data
.dptr
== NULL
) {
224 /* There has been an entry before, delete it */
226 status
= dbwrap_record_delete(d
->record
);
227 if (!NT_STATUS_IS_OK(status
)) {
230 DEBUG(0, ("delete_rec returned %s\n",
233 if (asprintf(&errmsg
, "could not delete share "
235 nt_errstr(status
)) == -1) {
236 smb_panic("could not delete share"
245 status
= dbwrap_record_store(d
->record
, data
, TDB_REPLACE
);
246 if (!NT_STATUS_IS_OK(status
)) {
249 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
251 if (asprintf(&errmsg
, "could not store share mode entry: %s",
252 nt_errstr(status
)) == -1) {
253 smb_panic("could not store share mode entry");
263 /*******************************************************************
264 Allocate a new share_mode_data struct, mark it unmodified.
265 fresh is set to note that currently there is no database entry.
266 ********************************************************************/
268 static struct share_mode_data
*fresh_share_mode_lock(
269 TALLOC_CTX
*mem_ctx
, const char *servicepath
,
270 const struct smb_filename
*smb_fname
,
271 const struct timespec
*old_write_time
)
273 struct share_mode_data
*d
;
275 if ((servicepath
== NULL
) || (smb_fname
== NULL
) ||
276 (old_write_time
== NULL
)) {
280 d
= talloc_zero(mem_ctx
, struct share_mode_data
);
284 d
->base_name
= talloc_strdup(d
, smb_fname
->base_name
);
285 if (d
->base_name
== NULL
) {
288 if (smb_fname
->stream_name
!= NULL
) {
289 d
->stream_name
= talloc_strdup(d
, smb_fname
->stream_name
);
290 if (d
->stream_name
== NULL
) {
294 d
->servicepath
= talloc_strdup(d
, servicepath
);
295 if (d
->servicepath
== NULL
) {
298 d
->old_write_time
= *old_write_time
;
303 DEBUG(0, ("talloc failed\n"));
308 /*******************************************************************
309 Either fetch a share mode from the database, or allocate a fresh
310 one if the record doesn't exist.
311 ********************************************************************/
313 static struct share_mode_lock
*get_share_mode_lock_internal(
314 TALLOC_CTX
*mem_ctx
, struct file_id id
,
315 const char *servicepath
, const struct smb_filename
*smb_fname
,
316 const struct timespec
*old_write_time
)
318 struct share_mode_lock
*lck
;
319 struct share_mode_data
*d
;
320 struct db_record
*rec
;
321 TDB_DATA key
= locking_key(&id
);
324 rec
= dbwrap_fetch_locked(lock_db
, mem_ctx
, key
);
326 DEBUG(3, ("Could not lock share entry\n"));
330 value
= dbwrap_record_get_value(rec
);
332 if (value
.dptr
== NULL
) {
333 d
= fresh_share_mode_lock(mem_ctx
, servicepath
, smb_fname
,
336 d
= parse_share_modes(mem_ctx
, value
);
340 DEBUG(5, ("get_share_mode_lock_internal: "
341 "Could not get share mode lock\n"));
345 d
->record
= talloc_move(d
, &rec
);
346 talloc_set_destructor(d
, share_mode_data_destructor
);
348 lck
= talloc(mem_ctx
, struct share_mode_lock
);
350 DEBUG(1, ("talloc failed\n"));
354 lck
->data
= talloc_move(lck
, &d
);
359 * We can only ever have one share mode locked. Users of
360 * get_share_mode_lock never see this, it will be refcounted by
363 static struct share_mode_lock
*the_lock
;
364 static struct file_id the_lock_id
;
366 static int the_lock_destructor(struct share_mode_lock
*l
)
369 ZERO_STRUCT(the_lock_id
);
373 /*******************************************************************
374 Get a share_mode_lock, Reference counted to allow nested calls.
375 ********************************************************************/
377 struct share_mode_lock
*get_share_mode_lock(
380 const char *servicepath
,
381 const struct smb_filename
*smb_fname
,
382 const struct timespec
*old_write_time
)
384 struct share_mode_lock
*lck
;
386 lck
= talloc(mem_ctx
, struct share_mode_lock
);
388 DEBUG(1, ("talloc failed\n"));
392 if (the_lock
== NULL
) {
393 the_lock
= get_share_mode_lock_internal(
394 lck
, id
, servicepath
, smb_fname
, old_write_time
);
395 if (the_lock
== NULL
) {
398 talloc_set_destructor(the_lock
, the_lock_destructor
);
401 if (!file_id_equal(&the_lock_id
, &id
)) {
402 DEBUG(1, ("Can not lock two share modes "
403 "simultaneously\n"));
406 if (talloc_reference(lck
, the_lock
) == NULL
) {
407 DEBUG(1, ("talloc_reference failed\n"));
411 lck
->data
= the_lock
->data
;
418 static void fetch_share_mode_unlocked_parser(
419 TDB_DATA key
, TDB_DATA data
, void *private_data
)
421 struct share_mode_lock
*lck
= talloc_get_type_abort(
422 private_data
, struct share_mode_lock
);
424 lck
->data
= parse_share_modes(lck
, data
);
427 /*******************************************************************
428 Get a share_mode_lock without locking the database or reference
429 counting. Used by smbstatus to display existing share modes.
430 ********************************************************************/
432 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
435 struct share_mode_lock
*lck
;
436 TDB_DATA key
= locking_key(&id
);
439 lck
= talloc(mem_ctx
, struct share_mode_lock
);
441 DEBUG(0, ("talloc failed\n"));
444 status
= dbwrap_parse_record(
445 lock_db
, key
, fetch_share_mode_unlocked_parser
, lck
);
446 if (!NT_STATUS_IS_OK(status
) ||
447 (lck
->data
== NULL
)) {
454 struct share_mode_forall_state
{
455 int (*fn
)(struct file_id fid
, const struct share_mode_data
*data
,
460 static int share_mode_traverse_fn(struct db_record
*rec
, void *_state
)
462 struct share_mode_forall_state
*state
=
463 (struct share_mode_forall_state
*)_state
;
468 enum ndr_err_code ndr_err
;
469 struct share_mode_data
*d
;
473 key
= dbwrap_record_get_key(rec
);
474 value
= dbwrap_record_get_value(rec
);
476 /* Ensure this is a locking_key record. */
477 if (key
.dsize
!= sizeof(fid
)) {
480 memcpy(&fid
, key
.dptr
, sizeof(fid
));
482 d
= talloc(talloc_tos(), struct share_mode_data
);
487 blob
.data
= value
.dptr
;
488 blob
.length
= value
.dsize
;
490 ndr_err
= ndr_pull_struct_blob_all(
491 &blob
, d
, d
, (ndr_pull_flags_fn_t
)ndr_pull_share_mode_data
);
492 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
493 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
496 if (DEBUGLEVEL
> 10) {
497 DEBUG(11, ("parse_share_modes:\n"));
498 NDR_PRINT_DEBUG(share_mode_data
, d
);
500 for (i
=0; i
<d
->num_share_modes
; i
++) {
501 d
->share_modes
[i
].stale
= false; /* [skip] in idl */
504 ret
= state
->fn(fid
, d
, state
->private_data
);
510 int share_mode_forall(int (*fn
)(struct file_id fid
,
511 const struct share_mode_data
*data
,
515 struct share_mode_forall_state state
= {
517 .private_data
= private_data
522 if (lock_db
== NULL
) {
526 status
= dbwrap_traverse_read(lock_db
, share_mode_traverse_fn
,
528 if (!NT_STATUS_IS_OK(status
)) {
535 struct share_entry_forall_state
{
536 int (*fn
)(const struct share_mode_entry
*e
,
537 const char *service_path
, const char *base_name
,
542 static int share_entry_traverse_fn(struct file_id fid
,
543 const struct share_mode_data
*data
,
546 struct share_entry_forall_state
*state
= private_data
;
549 for (i
=0; i
<data
->num_share_modes
; i
++) {
552 ret
= state
->fn(&data
->share_modes
[i
],
553 data
->servicepath
, data
->base_name
,
554 state
->private_data
);
563 /*******************************************************************
564 Call the specified function on each entry under management by the
566 ********************************************************************/
568 int share_entry_forall(int (*fn
)(const struct share_mode_entry
*,
569 const char *, const char *, void *),
572 struct share_entry_forall_state state
= {
573 .fn
= fn
, .private_data
= private_data
};
575 return share_mode_forall(share_entry_traverse_fn
, &state
);
578 bool share_mode_cleanup_disconnected(struct file_id fid
,
579 uint64_t open_persistent_id
)
582 TALLOC_CTX
*frame
= talloc_stackframe();
584 struct share_mode_data
*data
;
585 struct share_mode_lock
*lck
;
588 lck
= get_existing_share_mode_lock(frame
, fid
);
590 DEBUG(5, ("share_mode_cleanup_disconnected: "
591 "Could not fetch share mode entry for %s\n",
592 file_id_string(frame
, &fid
)));
597 for (n
=0; n
< data
->num_share_modes
; n
++) {
598 struct share_mode_entry
*entry
= &data
->share_modes
[n
];
600 if (!server_id_is_disconnected(&entry
->pid
)) {
601 DEBUG(5, ("share_mode_cleanup_disconnected: "
602 "file (file-id='%s', servicepath='%s', "
603 "base_name='%s%s%s') "
604 "is used by server %s ==> do not cleanup\n",
605 file_id_string(frame
, &fid
),
608 (data
->stream_name
== NULL
)
609 ? "" : "', stream_name='",
610 (data
->stream_name
== NULL
)
611 ? "" : data
->stream_name
,
612 server_id_str(frame
, &entry
->pid
)));
615 if (open_persistent_id
!= entry
->share_file_id
) {
616 DEBUG(5, ("share_mode_cleanup_disconnected: "
618 "(file-id='%s', servicepath='%s', "
619 "base_name='%s%s%s') "
620 "has share_file_id %llu but expected %llu"
621 "==> do not cleanup\n",
622 file_id_string(frame
, &fid
),
625 (data
->stream_name
== NULL
)
626 ? "" : "', stream_name='",
627 (data
->stream_name
== NULL
)
628 ? "" : data
->stream_name
,
629 (unsigned long long)entry
->share_file_id
,
630 (unsigned long long)open_persistent_id
));
635 for (n
=0; n
< data
->num_leases
; n
++) {
636 struct share_mode_lease
*l
= &data
->leases
[n
];
639 status
= leases_db_del(&l
->client_guid
, &l
->lease_key
, &fid
);
641 DEBUG(10, ("%s: leases_db_del returned %s\n", __func__
,
645 ok
= brl_cleanup_disconnected(fid
, open_persistent_id
);
647 DEBUG(10, ("share_mode_cleanup_disconnected: "
648 "failed to clean up byte range locks associated "
649 "with file (file-id='%s', servicepath='%s', "
650 "base_name='%s%s%s') and open_persistent_id %llu "
651 "==> do not cleanup\n",
652 file_id_string(frame
, &fid
),
655 (data
->stream_name
== NULL
)
656 ? "" : "', stream_name='",
657 (data
->stream_name
== NULL
)
658 ? "" : data
->stream_name
,
659 (unsigned long long)open_persistent_id
));
663 DEBUG(10, ("share_mode_cleanup_disconnected: "
664 "cleaning up %u entries for file "
665 "(file-id='%s', servicepath='%s', "
666 "base_name='%s%s%s') "
667 "from open_persistent_id %llu\n",
668 data
->num_share_modes
,
669 file_id_string(frame
, &fid
),
672 (data
->stream_name
== NULL
)
673 ? "" : "', stream_name='",
674 (data
->stream_name
== NULL
)
675 ? "" : data
->stream_name
,
676 (unsigned long long)open_persistent_id
));
678 data
->num_share_modes
= 0;
679 data
->num_leases
= 0;
680 data
->modified
= true;