2 Unix SMB/CIFS implementation.
4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 this is the open files database, ctdb backend. It implements shared
23 storage of what files are open between server instances, and
24 implements the rules 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"
49 #include "ntvfs/common/ntvfs_common.h"
50 #include "cluster/cluster.h"
51 #include "include/ctdb.h"
52 #include "param/param.h"
55 struct ctdb_context *ctdb;
56 struct ctdb_db_context *ctdb_db;
57 struct ntvfs_context *ntvfs_ctx;
62 an odb lock handle. You must obtain one of these using odb_lock() before doing
66 struct odb_context *odb;
67 struct ctdb_record_handle *rec;
73 Open up the openfiles.tdb database. Close it down using
74 talloc_free(). We need the messaging_ctx to allow for pending open
77 static struct odb_context *odb_ctdb_init(TALLOC_CTX *mem_ctx,
78 struct ntvfs_context *ntvfs_ctx)
80 struct odb_context *odb;
81 struct ctdb_context *ctdb = talloc_get_type(cluster_backend_handle(),
84 odb = talloc(mem_ctx, struct odb_context);
90 odb->ctdb_db = ctdb_attach(ctdb, "opendb");
92 DEBUG(0,("Failed to get attached ctdb db handle for opendb\n"));
97 odb->ntvfs_ctx = ntvfs_ctx;
99 /* leave oplocks disabled by default until the code is working */
100 odb->oplocks = lp_parm_bool(ntvfs_ctx->lp_ctx, NULL, "opendb", "oplocks", false);
106 get a lock on a entry in the odb. This call returns a lock handle,
107 which the caller should unlock using talloc_free().
109 static struct odb_lock *odb_ctdb_lock(TALLOC_CTX *mem_ctx,
110 struct odb_context *odb, DATA_BLOB *file_key)
112 struct odb_lock *lck;
114 lck = talloc(mem_ctx, struct odb_lock);
119 lck->odb = talloc_reference(lck, odb);
120 lck->key.dptr = talloc_memdup(lck, file_key->data, file_key->length);
121 lck->key.dsize = file_key->length;
122 if (lck->key.dptr == NULL) {
127 lck->rec = ctdb_fetch_lock(odb->ctdb_db, (TALLOC_CTX *)lck, lck->key, &lck->data);
137 determine if two odb_entry structures conflict
139 return NT_STATUS_OK on no conflict
141 static NTSTATUS share_conflict(struct opendb_entry *e1, struct opendb_entry *e2)
143 /* if either open involves no read.write or delete access then
145 if (!(e1->access_mask & (SEC_FILE_WRITE_DATA |
146 SEC_FILE_APPEND_DATA |
152 if (!(e2->access_mask & (SEC_FILE_WRITE_DATA |
153 SEC_FILE_APPEND_DATA |
160 /* data IO access masks. This is skipped if the two open handles
161 are on different streams (as in that case the masks don't
163 if (e1->stream_id != e2->stream_id) {
167 #define CHECK_MASK(am, right, sa, share) \
168 if (((am) & (right)) && !((sa) & (share))) return NT_STATUS_SHARING_VIOLATION
170 CHECK_MASK(e1->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
171 e2->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
172 CHECK_MASK(e2->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
173 e1->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
175 CHECK_MASK(e1->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
176 e2->share_access, NTCREATEX_SHARE_ACCESS_READ);
177 CHECK_MASK(e2->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
178 e1->share_access, NTCREATEX_SHARE_ACCESS_READ);
180 CHECK_MASK(e1->access_mask, SEC_STD_DELETE,
181 e2->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
182 CHECK_MASK(e2->access_mask, SEC_STD_DELETE,
183 e1->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
189 pull a record, translating from the db format to the opendb_file structure defined
192 static NTSTATUS odb_pull_record(struct odb_lock *lck, struct opendb_file *file)
196 enum ndr_err_code ndr_err;
200 if (dbuf.dsize == 0) {
201 /* empty record in ctdb means the record isn't there */
202 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
205 blob.data = dbuf.dptr;
206 blob.length = dbuf.dsize;
208 ndr_err = ndr_pull_struct_blob(&blob, lck, lp_iconv_convenience(global_loadparm), file, (ndr_pull_flags_fn_t)ndr_pull_opendb_file);
209 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
210 return ndr_map_error2ntstatus(ndr_err);
217 push a record, translating from the opendb_file structure defined in opendb.idl
219 static NTSTATUS odb_push_record(struct odb_lock *lck, struct opendb_file *file)
223 enum ndr_err_code ndr_err;
226 if (!file->num_entries) {
229 ctdb_record_store(lck->rec, dbuf);
230 talloc_free(lck->rec);
234 ndr_err = ndr_push_struct_blob(&blob, lck,
235 lp_iconv_convenience(global_loadparm),
236 file, (ndr_push_flags_fn_t)ndr_push_opendb_file);
237 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
238 return ndr_map_error2ntstatus(ndr_err);
241 dbuf.dptr = blob.data;
242 dbuf.dsize = blob.length;
244 ret = ctdb_record_store(lck->rec, dbuf);
245 talloc_free(lck->rec);
246 data_blob_free(&blob);
248 return NT_STATUS_INTERNAL_DB_CORRUPTION;
255 send an oplock break to a client
257 static NTSTATUS odb_oplock_break_send(struct odb_context *odb, struct opendb_entry *e)
259 /* tell the server handling this open file about the need to send the client
261 return messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, e->server,
262 MSG_NTVFS_OPLOCK_BREAK, e->file_handle);
266 register an open file in the open files database. This implements the share_access
269 Note that the path is only used by the delete on close logic, not
270 for comparing with other filenames
272 static NTSTATUS odb_ctdb_open_file(struct odb_lock *lck, void *file_handle,
273 uint32_t stream_id, uint32_t share_access,
274 uint32_t access_mask, bool delete_on_close,
276 uint32_t oplock_level, uint32_t *oplock_granted)
278 struct odb_context *odb = lck->odb;
279 struct opendb_entry e;
281 struct opendb_file file;
284 if (odb->oplocks == false) {
285 oplock_level = OPLOCK_NONE;
288 status = odb_pull_record(lck, &file);
289 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
290 /* initialise a blank structure */
294 NT_STATUS_NOT_OK_RETURN(status);
297 /* see if it conflicts */
298 e.server = odb->ntvfs_ctx->server_id;
299 e.file_handle = file_handle;
300 e.stream_id = stream_id;
301 e.share_access = share_access;
302 e.access_mask = access_mask;
303 e.delete_on_close = delete_on_close;
304 e.oplock_level = OPLOCK_NONE;
306 /* see if anyone has an oplock, which we need to break */
307 for (i=0;i<file.num_entries;i++) {
308 if (file.entries[i].oplock_level == OPLOCK_BATCH) {
309 /* a batch oplock caches close calls, which
310 means the client application might have
311 already closed the file. We have to allow
312 this close to propogate by sending a oplock
313 break request and suspending this call
314 until the break is acknowledged or the file
316 odb_oplock_break_send(odb, &file.entries[i]);
317 return NT_STATUS_OPLOCK_NOT_GRANTED;
321 if (file.delete_on_close ||
322 (file.num_entries != 0 && delete_on_close)) {
323 /* while delete on close is set, no new opens are allowed */
324 return NT_STATUS_DELETE_PENDING;
327 /* check for sharing violations */
328 for (i=0;i<file.num_entries;i++) {
329 status = share_conflict(&file.entries[i], &e);
330 NT_STATUS_NOT_OK_RETURN(status);
333 /* we now know the open could succeed, but we need to check
334 for any exclusive oplocks. We can't grant a second open
335 till these are broken. Note that we check for batch oplocks
336 before checking for sharing violations, and check for
337 exclusive oplocks afterwards. */
338 for (i=0;i<file.num_entries;i++) {
339 if (file.entries[i].oplock_level == OPLOCK_EXCLUSIVE) {
340 odb_oplock_break_send(odb, &file.entries[i]);
341 return NT_STATUS_OPLOCK_NOT_GRANTED;
346 possibly grant an exclusive or batch oplock if this is the only client
347 with the file open. We don't yet grant levelII oplocks.
349 if (oplock_granted != NULL) {
350 if ((oplock_level == OPLOCK_BATCH ||
351 oplock_level == OPLOCK_EXCLUSIVE) &&
352 file.num_entries == 0) {
353 (*oplock_granted) = oplock_level;
355 (*oplock_granted) = OPLOCK_NONE;
357 e.oplock_level = (*oplock_granted);
360 /* it doesn't conflict, so add it to the end */
361 file.entries = talloc_realloc(lck, file.entries, struct opendb_entry,
363 NT_STATUS_HAVE_NO_MEMORY(file.entries);
365 file.entries[file.num_entries] = e;
368 return odb_push_record(lck, &file);
373 register a pending open file in the open files database
375 static NTSTATUS odb_ctdb_open_file_pending(struct odb_lock *lck, void *private)
377 struct odb_context *odb = lck->odb;
378 struct opendb_file file;
381 status = odb_pull_record(lck, &file);
382 NT_STATUS_NOT_OK_RETURN(status);
384 file.pending = talloc_realloc(lck, file.pending, struct opendb_pending,
386 NT_STATUS_HAVE_NO_MEMORY(file.pending);
388 file.pending[file.num_pending].server = odb->ntvfs_ctx->server_id;
389 file.pending[file.num_pending].notify_ptr = private;
393 return odb_push_record(lck, &file);
398 remove a opendb entry
400 static NTSTATUS odb_ctdb_close_file(struct odb_lock *lck, void *file_handle)
402 struct odb_context *odb = lck->odb;
403 struct opendb_file file;
407 status = odb_pull_record(lck, &file);
408 NT_STATUS_NOT_OK_RETURN(status);
410 /* find the entry, and delete it */
411 for (i=0;i<file.num_entries;i++) {
412 if (file_handle == file.entries[i].file_handle &&
413 cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.entries[i].server)) {
414 if (file.entries[i].delete_on_close) {
415 file.delete_on_close = true;
417 if (i < file.num_entries-1) {
418 memmove(file.entries+i, file.entries+i+1,
419 (file.num_entries - (i+1)) *
420 sizeof(struct opendb_entry));
426 if (i == file.num_entries) {
427 return NT_STATUS_UNSUCCESSFUL;
430 /* send any pending notifications, removing them once sent */
431 for (i=0;i<file.num_pending;i++) {
432 messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, file.pending[i].server,
434 file.pending[i].notify_ptr);
436 file.num_pending = 0;
440 return odb_push_record(lck, &file);
445 remove a pending opendb entry
447 static NTSTATUS odb_ctdb_remove_pending(struct odb_lock *lck, void *private)
449 struct odb_context *odb = lck->odb;
452 struct opendb_file file;
454 status = odb_pull_record(lck, &file);
455 NT_STATUS_NOT_OK_RETURN(status);
457 /* find the entry, and delete it */
458 for (i=0;i<file.num_pending;i++) {
459 if (private == file.pending[i].notify_ptr &&
460 cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.pending[i].server)) {
461 if (i < file.num_pending-1) {
462 memmove(file.pending+i, file.pending+i+1,
463 (file.num_pending - (i+1)) *
464 sizeof(struct opendb_pending));
470 if (i == file.num_pending) {
471 return NT_STATUS_UNSUCCESSFUL;
476 return odb_push_record(lck, &file);
481 rename the path in a open file
483 static NTSTATUS odb_ctdb_rename(struct odb_lock *lck, const char *path)
485 struct opendb_file file;
488 status = odb_pull_record(lck, &file);
489 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
490 /* not having the record at all is OK */
493 NT_STATUS_NOT_OK_RETURN(status);
496 return odb_push_record(lck, &file);
500 update delete on close flag on an open file
502 static NTSTATUS odb_ctdb_set_delete_on_close(struct odb_lock *lck, bool del_on_close)
505 struct opendb_file file;
507 status = odb_pull_record(lck, &file);
508 NT_STATUS_NOT_OK_RETURN(status);
510 file.delete_on_close = del_on_close;
512 return odb_push_record(lck, &file);
516 return the current value of the delete_on_close bit, and how many
517 people still have the file open
519 static NTSTATUS odb_ctdb_get_delete_on_close(struct odb_context *odb,
520 DATA_BLOB *key, bool *del_on_close,
521 int *open_count, char **path)
524 struct opendb_file file;
525 struct odb_lock *lck;
527 lck = odb_lock(odb, odb, key);
528 NT_STATUS_HAVE_NO_MEMORY(lck);
530 status = odb_pull_record(lck, &file);
531 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
533 (*del_on_close) = false;
536 if (!NT_STATUS_IS_OK(status)) {
541 (*del_on_close) = file.delete_on_close;
542 if (open_count != NULL) {
543 (*open_count) = file.num_entries;
546 *path = talloc_strdup(odb, file.path);
547 NT_STATUS_HAVE_NO_MEMORY(*path);
548 if (file.num_entries == 1 && file.entries[0].delete_on_close) {
549 (*del_on_close) = true;
560 determine if a file can be opened with the given share_access,
561 create_options and access_mask
563 static NTSTATUS odb_ctdb_can_open(struct odb_lock *lck,
564 uint32_t share_access, uint32_t create_options,
565 uint32_t access_mask)
567 struct odb_context *odb = lck->odb;
569 struct opendb_file file;
570 struct opendb_entry e;
573 status = odb_pull_record(lck, &file);
574 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
577 NT_STATUS_NOT_OK_RETURN(status);
579 if ((create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) &&
580 file.num_entries != 0) {
581 return NT_STATUS_SHARING_VIOLATION;
584 if (file.delete_on_close) {
585 return NT_STATUS_DELETE_PENDING;
588 e.server = odb->ntvfs_ctx->server_id;
589 e.file_handle = NULL;
591 e.share_access = share_access;
592 e.access_mask = access_mask;
594 for (i=0;i<file.num_entries;i++) {
595 status = share_conflict(&file.entries[i], &e);
596 if (!NT_STATUS_IS_OK(status)) {
597 /* note that we discard the error code
598 here. We do this as unless we are actually
599 doing an open (which comes via a different
600 function), we need to return a sharing
602 return NT_STATUS_SHARING_VIOLATION;
610 static const struct opendb_ops opendb_ctdb_ops = {
611 .odb_init = odb_ctdb_init,
612 .odb_lock = odb_ctdb_lock,
613 .odb_open_file = odb_ctdb_open_file,
614 .odb_open_file_pending = odb_ctdb_open_file_pending,
615 .odb_close_file = odb_ctdb_close_file,
616 .odb_remove_pending = odb_ctdb_remove_pending,
617 .odb_rename = odb_ctdb_rename,
618 .odb_set_delete_on_close = odb_ctdb_set_delete_on_close,
619 .odb_get_delete_on_close = odb_ctdb_get_delete_on_close,
620 .odb_can_open = odb_ctdb_can_open
624 void odb_ctdb_init_ops(void)
626 odb_set_ops(&opendb_ctdb_ops);