2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 this is the open files database. It implements shared storage of
23 what files are open between server instances, and implements the rules
24 of shared access to files.
26 The caller needs to provide a file_key, which specifies what file
27 they are talking about. This needs to be a unique key across all
28 filesystems, and is usually implemented in terms of a device/inode
31 Before any operations can be performed the caller needs to establish
32 a lock on the record associated with file_key. That is done by
33 calling odb_lock(). The caller releases this lock by calling
34 talloc_free() on the returned handle.
36 All other operations on a record are done by passing the odb_lock()
37 handle back to this module. The handle contains internal
38 information about what file_key is being operated on.
42 #include "system/filesys.h"
43 #include "lib/tdb/include/tdb.h"
44 #include "messaging/messaging.h"
46 #include "lib/messaging/irpc.h"
47 #include "librpc/gen_ndr/ndr_opendb.h"
48 #include "ntvfs/ntvfs.h"
52 struct ntvfs_context *ntvfs_ctx;
57 an odb lock handle. You must obtain one of these using odb_lock() before doing
61 struct odb_context *odb;
66 Open up the openfiles.tdb database. Close it down using
67 talloc_free(). We need the messaging_ctx to allow for pending open
70 _PUBLIC_ struct odb_context *odb_init(TALLOC_CTX *mem_ctx,
71 struct ntvfs_context *ntvfs_ctx)
74 struct odb_context *odb;
76 odb = talloc(mem_ctx, struct odb_context);
81 path = smbd_tmp_path(odb, "openfiles.tdb");
82 odb->w = tdb_wrap_open(odb, path, 0,
84 O_RDWR|O_CREAT, 0600);
91 odb->ntvfs_ctx = ntvfs_ctx;
93 /* leave oplocks disabled by default until the code is working */
94 odb->oplocks = lp_parm_bool(-1, "opendb", "oplocks", False);
100 destroy a lock on the database
102 static int odb_lock_destructor(struct odb_lock *lck)
104 tdb_chainunlock(lck->odb->w->tdb, lck->key);
109 get a lock on a entry in the odb. This call returns a lock handle,
110 which the caller should unlock using talloc_free().
112 _PUBLIC_ struct odb_lock *odb_lock(TALLOC_CTX *mem_ctx,
113 struct odb_context *odb, DATA_BLOB *file_key)
115 struct odb_lock *lck;
117 lck = talloc(mem_ctx, struct odb_lock);
122 lck->odb = talloc_reference(lck, odb);
123 lck->key.dptr = talloc_memdup(lck, file_key->data, file_key->length);
124 lck->key.dsize = file_key->length;
125 if (lck->key.dptr == NULL) {
130 if (tdb_chainlock(odb->w->tdb, lck->key) != 0) {
135 talloc_set_destructor(lck, odb_lock_destructor);
141 determine if two odb_entry structures conflict
143 return NT_STATUS_OK on no conflict
145 static NTSTATUS share_conflict(struct opendb_entry *e1, struct opendb_entry *e2)
147 /* if either open involves no read.write or delete access then
149 if (!(e1->access_mask & (SEC_FILE_WRITE_DATA |
150 SEC_FILE_APPEND_DATA |
156 if (!(e2->access_mask & (SEC_FILE_WRITE_DATA |
157 SEC_FILE_APPEND_DATA |
164 /* data IO access masks. This is skipped if the two open handles
165 are on different streams (as in that case the masks don't
167 if (e1->stream_id != e2->stream_id) {
171 #define CHECK_MASK(am, right, sa, share) \
172 if (((am) & (right)) && !((sa) & (share))) return NT_STATUS_SHARING_VIOLATION
174 CHECK_MASK(e1->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
175 e2->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
176 CHECK_MASK(e2->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
177 e1->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
179 CHECK_MASK(e1->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
180 e2->share_access, NTCREATEX_SHARE_ACCESS_READ);
181 CHECK_MASK(e2->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
182 e1->share_access, NTCREATEX_SHARE_ACCESS_READ);
184 CHECK_MASK(e1->access_mask, SEC_STD_DELETE,
185 e2->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
186 CHECK_MASK(e2->access_mask, SEC_STD_DELETE,
187 e1->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
193 pull a record, translating from the db format to the opendb_file structure defined
196 static NTSTATUS odb_pull_record(struct odb_lock *lck, struct opendb_file *file)
198 struct odb_context *odb = lck->odb;
203 dbuf = tdb_fetch(odb->w->tdb, lck->key);
204 if (dbuf.dptr == NULL) {
205 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
208 blob.data = dbuf.dptr;
209 blob.length = dbuf.dsize;
211 status = ndr_pull_struct_blob(&blob, lck, file, (ndr_pull_flags_fn_t)ndr_pull_opendb_file);
219 push a record, translating from the opendb_file structure defined in opendb.idl
221 static NTSTATUS odb_push_record(struct odb_lock *lck, struct opendb_file *file)
223 struct odb_context *odb = lck->odb;
229 if (file->num_entries == 0) {
230 ret = tdb_delete(odb->w->tdb, lck->key);
232 return NT_STATUS_INTERNAL_DB_CORRUPTION;
237 status = ndr_push_struct_blob(&blob, lck, file, (ndr_push_flags_fn_t)ndr_push_opendb_file);
238 NT_STATUS_NOT_OK_RETURN(status);
240 dbuf.dptr = blob.data;
241 dbuf.dsize = blob.length;
243 ret = tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE);
244 data_blob_free(&blob);
246 return NT_STATUS_INTERNAL_DB_CORRUPTION;
253 send an oplock break to a client
255 static NTSTATUS odb_oplock_break_send(struct odb_context *odb, struct opendb_entry *e)
257 /* tell the server handling this open file about the need to send the client
259 return messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, e->server,
260 MSG_NTVFS_OPLOCK_BREAK, e->file_handle);
264 register an open file in the open files database. This implements the share_access
267 Note that the path is only used by the delete on close logic, not
268 for comparing with other filenames
270 _PUBLIC_ NTSTATUS odb_open_file(struct odb_lock *lck, void *file_handle,
271 uint32_t stream_id, uint32_t share_access,
272 uint32_t access_mask, BOOL delete_on_close,
274 uint32_t oplock_level, uint32_t *oplock_granted)
276 struct odb_context *odb = lck->odb;
277 struct opendb_entry e;
279 struct opendb_file file;
282 if (odb->oplocks == False) {
283 oplock_level = OPLOCK_NONE;
286 status = odb_pull_record(lck, &file);
287 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
288 /* initialise a blank structure */
292 NT_STATUS_NOT_OK_RETURN(status);
295 /* see if it conflicts */
296 e.server = odb->ntvfs_ctx->server_id;
297 e.file_handle = file_handle;
298 e.stream_id = stream_id;
299 e.share_access = share_access;
300 e.access_mask = access_mask;
301 e.delete_on_close = delete_on_close;
302 e.oplock_level = OPLOCK_NONE;
304 /* see if anyone has an oplock, which we need to break */
305 for (i=0;i<file.num_entries;i++) {
306 if (file.entries[i].oplock_level == OPLOCK_BATCH) {
307 /* a batch oplock caches close calls, which
308 means the client application might have
309 already closed the file. We have to allow
310 this close to propogate by sending a oplock
311 break request and suspending this call
312 until the break is acknowledged or the file
314 odb_oplock_break_send(odb, &file.entries[i]);
315 return NT_STATUS_OPLOCK_NOT_GRANTED;
319 if (file.delete_on_close ||
320 (file.num_entries != 0 && delete_on_close)) {
321 /* while delete on close is set, no new opens are allowed */
322 return NT_STATUS_DELETE_PENDING;
325 /* check for sharing violations */
326 for (i=0;i<file.num_entries;i++) {
327 status = share_conflict(&file.entries[i], &e);
328 NT_STATUS_NOT_OK_RETURN(status);
331 /* we now know the open could succeed, but we need to check
332 for any exclusive oplocks. We can't grant a second open
333 till these are broken. Note that we check for batch oplocks
334 before checking for sharing violations, and check for
335 exclusive oplocks afterwards. */
336 for (i=0;i<file.num_entries;i++) {
337 if (file.entries[i].oplock_level == OPLOCK_EXCLUSIVE) {
338 odb_oplock_break_send(odb, &file.entries[i]);
339 return NT_STATUS_OPLOCK_NOT_GRANTED;
344 possibly grant an exclusive or batch oplock if this is the only client
345 with the file open. We don't yet grant levelII oplocks.
347 if (oplock_granted != NULL) {
348 if ((oplock_level == OPLOCK_BATCH ||
349 oplock_level == OPLOCK_EXCLUSIVE) &&
350 file.num_entries == 0) {
351 (*oplock_granted) = oplock_level;
353 (*oplock_granted) = OPLOCK_NONE;
355 e.oplock_level = (*oplock_granted);
358 /* it doesn't conflict, so add it to the end */
359 file.entries = talloc_realloc(lck, file.entries, struct opendb_entry,
361 NT_STATUS_HAVE_NO_MEMORY(file.entries);
363 file.entries[file.num_entries] = e;
366 return odb_push_record(lck, &file);
371 register a pending open file in the open files database
373 _PUBLIC_ NTSTATUS odb_open_file_pending(struct odb_lock *lck, void *private)
375 struct odb_context *odb = lck->odb;
376 struct opendb_file file;
379 status = odb_pull_record(lck, &file);
380 NT_STATUS_NOT_OK_RETURN(status);
382 file.pending = talloc_realloc(lck, file.pending, struct opendb_pending,
384 NT_STATUS_HAVE_NO_MEMORY(file.pending);
386 file.pending[file.num_pending].server = odb->ntvfs_ctx->server_id;
387 file.pending[file.num_pending].notify_ptr = private;
391 return odb_push_record(lck, &file);
396 remove a opendb entry
398 _PUBLIC_ NTSTATUS odb_close_file(struct odb_lock *lck, void *file_handle)
400 struct odb_context *odb = lck->odb;
401 struct opendb_file file;
405 status = odb_pull_record(lck, &file);
406 NT_STATUS_NOT_OK_RETURN(status);
408 /* find the entry, and delete it */
409 for (i=0;i<file.num_entries;i++) {
410 if (file_handle == file.entries[i].file_handle &&
411 cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.entries[i].server)) {
412 if (file.entries[i].delete_on_close) {
413 file.delete_on_close = True;
415 if (i < file.num_entries-1) {
416 memmove(file.entries+i, file.entries+i+1,
417 (file.num_entries - (i+1)) *
418 sizeof(struct opendb_entry));
424 if (i == file.num_entries) {
425 return NT_STATUS_UNSUCCESSFUL;
428 /* send any pending notifications, removing them once sent */
429 for (i=0;i<file.num_pending;i++) {
430 messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, file.pending[i].server,
432 file.pending[i].notify_ptr);
434 file.num_pending = 0;
438 return odb_push_record(lck, &file);
443 remove a pending opendb entry
445 _PUBLIC_ NTSTATUS odb_remove_pending(struct odb_lock *lck, void *private)
447 struct odb_context *odb = lck->odb;
450 struct opendb_file file;
452 status = odb_pull_record(lck, &file);
453 NT_STATUS_NOT_OK_RETURN(status);
455 /* find the entry, and delete it */
456 for (i=0;i<file.num_pending;i++) {
457 if (private == file.pending[i].notify_ptr &&
458 cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.pending[i].server)) {
459 if (i < file.num_pending-1) {
460 memmove(file.pending+i, file.pending+i+1,
461 (file.num_pending - (i+1)) *
462 sizeof(struct opendb_pending));
468 if (i == file.num_pending) {
469 return NT_STATUS_UNSUCCESSFUL;
474 return odb_push_record(lck, &file);
479 rename the path in a open file
481 _PUBLIC_ NTSTATUS odb_rename(struct odb_lock *lck, const char *path)
483 struct opendb_file file;
486 status = odb_pull_record(lck, &file);
487 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
488 /* not having the record at all is OK */
491 NT_STATUS_NOT_OK_RETURN(status);
494 return odb_push_record(lck, &file);
498 update delete on close flag on an open file
500 _PUBLIC_ NTSTATUS odb_set_delete_on_close(struct odb_lock *lck, BOOL del_on_close)
503 struct opendb_file file;
505 status = odb_pull_record(lck, &file);
506 NT_STATUS_NOT_OK_RETURN(status);
508 file.delete_on_close = del_on_close;
510 return odb_push_record(lck, &file);
514 return the current value of the delete_on_close bit, and how many
515 people still have the file open
517 _PUBLIC_ NTSTATUS odb_get_delete_on_close(struct odb_context *odb,
518 DATA_BLOB *key, BOOL *del_on_close,
519 int *open_count, char **path)
522 struct opendb_file file;
523 struct odb_lock *lck;
525 lck = odb_lock(odb, odb, key);
526 NT_STATUS_HAVE_NO_MEMORY(lck);
528 status = odb_pull_record(lck, &file);
529 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
531 (*del_on_close) = False;
534 if (!NT_STATUS_IS_OK(status)) {
539 (*del_on_close) = file.delete_on_close;
540 if (open_count != NULL) {
541 (*open_count) = file.num_entries;
544 *path = talloc_strdup(odb, file.path);
545 NT_STATUS_HAVE_NO_MEMORY(*path);
546 if (file.num_entries == 1 && file.entries[0].delete_on_close) {
547 (*del_on_close) = True;
558 determine if a file can be opened with the given share_access,
559 create_options and access_mask
561 _PUBLIC_ NTSTATUS odb_can_open(struct odb_lock *lck,
562 uint32_t share_access, uint32_t create_options,
563 uint32_t access_mask)
565 struct odb_context *odb = lck->odb;
567 struct opendb_file file;
568 struct opendb_entry e;
571 status = odb_pull_record(lck, &file);
572 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
575 NT_STATUS_NOT_OK_RETURN(status);
577 if ((create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) &&
578 file.num_entries != 0) {
579 return NT_STATUS_SHARING_VIOLATION;
582 if (file.delete_on_close) {
583 return NT_STATUS_DELETE_PENDING;
586 e.server = odb->ntvfs_ctx->server_id;
587 e.file_handle = NULL;
589 e.share_access = share_access;
590 e.access_mask = access_mask;
592 for (i=0;i<file.num_entries;i++) {
593 status = share_conflict(&file.entries[i], &e);
594 if (!NT_STATUS_IS_OK(status)) {
595 /* note that we discard the error code
596 here. We do this as unless we are actually
597 doing an open (which comes via a different
598 function), we need to return a sharing
600 return NT_STATUS_SHARING_VIOLATION;