opendb_tdb: only file->delete_on_close == true should give DELETE_PENDING
[samba.git] / source4 / ntvfs / common / opendb_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2004
5    Copyright (C) Stefan Metzmacher 2008
6
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.
11    
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.
16    
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/>.
19 */
20
21 /*
22   this is the open files database, tdb backend. It implements shared
23   storage of what files are open between server instances, and
24   implements the rules of shared access to files.
25
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
29   pair.
30
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.
35
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.
39 */
40
41 #include "includes.h"
42 #include "system/filesys.h"
43 #include "lib/tdb/include/tdb.h"
44 #include "messaging/messaging.h"
45 #include "tdb_wrap.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 "param/param.h"
52
53 struct odb_context {
54         struct tdb_wrap *w;
55         struct ntvfs_context *ntvfs_ctx;
56         bool oplocks;
57 };
58
59 /*
60   an odb lock handle. You must obtain one of these using odb_lock() before doing
61   any other operations. 
62 */
63 struct odb_lock {
64         struct odb_context *odb;
65         TDB_DATA key;
66 };
67
68 /*
69   Open up the openfiles.tdb database. Close it down using
70   talloc_free(). We need the messaging_ctx to allow for pending open
71   notifications.
72 */
73 static struct odb_context *odb_tdb_init(TALLOC_CTX *mem_ctx, 
74                                         struct ntvfs_context *ntvfs_ctx)
75 {
76         struct odb_context *odb;
77
78         odb = talloc(mem_ctx, struct odb_context);
79         if (odb == NULL) {
80                 return NULL;
81         }
82
83         odb->w = cluster_tdb_tmp_open(odb, ntvfs_ctx->lp_ctx, "openfiles.tdb", TDB_DEFAULT);
84         if (odb->w == NULL) {
85                 talloc_free(odb);
86                 return NULL;
87         }
88
89         odb->ntvfs_ctx = ntvfs_ctx;
90
91         /* leave oplocks disabled by default until the code is working */
92         odb->oplocks = lp_parm_bool(ntvfs_ctx->lp_ctx, NULL, "opendb", "oplocks", false);
93
94         return odb;
95 }
96
97 /*
98   destroy a lock on the database
99 */
100 static int odb_lock_destructor(struct odb_lock *lck)
101 {
102         tdb_chainunlock(lck->odb->w->tdb, lck->key);
103         return 0;
104 }
105
106 /*
107   get a lock on a entry in the odb. This call returns a lock handle,
108   which the caller should unlock using talloc_free().
109 */
110 static struct odb_lock *odb_tdb_lock(TALLOC_CTX *mem_ctx,
111                                      struct odb_context *odb, DATA_BLOB *file_key)
112 {
113         struct odb_lock *lck;
114
115         lck = talloc(mem_ctx, struct odb_lock);
116         if (lck == NULL) {
117                 return NULL;
118         }
119
120         lck->odb = talloc_reference(lck, odb);
121         lck->key.dptr = talloc_memdup(lck, file_key->data, file_key->length);
122         lck->key.dsize = file_key->length;
123         if (lck->key.dptr == NULL) {
124                 talloc_free(lck);
125                 return NULL;
126         }
127
128         if (tdb_chainlock(odb->w->tdb, lck->key) != 0) {
129                 talloc_free(lck);
130                 return NULL;
131         }
132
133         talloc_set_destructor(lck, odb_lock_destructor);
134         
135         return lck;
136 }
137
138 static DATA_BLOB odb_tdb_get_key(TALLOC_CTX *mem_ctx, struct odb_lock *lck)
139 {
140         return data_blob_talloc(mem_ctx, lck->key.dptr, lck->key.dsize);
141 }
142
143
144 /*
145   determine if two odb_entry structures conflict
146
147   return NT_STATUS_OK on no conflict
148 */
149 static NTSTATUS share_conflict(struct opendb_entry *e1,
150                                uint32_t stream_id,
151                                uint32_t share_access,
152                                uint32_t access_mask)
153 {
154         /* if either open involves no read.write or delete access then
155            it can't conflict */
156         if (!(e1->access_mask & (SEC_FILE_WRITE_DATA |
157                                  SEC_FILE_APPEND_DATA |
158                                  SEC_FILE_READ_DATA |
159                                  SEC_FILE_EXECUTE |
160                                  SEC_STD_DELETE))) {
161                 return NT_STATUS_OK;
162         }
163         if (!(access_mask & (SEC_FILE_WRITE_DATA |
164                              SEC_FILE_APPEND_DATA |
165                              SEC_FILE_READ_DATA |
166                              SEC_FILE_EXECUTE |
167                              SEC_STD_DELETE))) {
168                 return NT_STATUS_OK;
169         }
170
171         /* data IO access masks. This is skipped if the two open handles
172            are on different streams (as in that case the masks don't
173            interact) */
174         if (e1->stream_id != stream_id) {
175                 return NT_STATUS_OK;
176         }
177
178 #define CHECK_MASK(am, right, sa, share) \
179         if (((am) & (right)) && !((sa) & (share))) return NT_STATUS_SHARING_VIOLATION
180
181         CHECK_MASK(e1->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
182                    share_access, NTCREATEX_SHARE_ACCESS_WRITE);
183         CHECK_MASK(access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
184                    e1->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
185         
186         CHECK_MASK(e1->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
187                    share_access, NTCREATEX_SHARE_ACCESS_READ);
188         CHECK_MASK(access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
189                    e1->share_access, NTCREATEX_SHARE_ACCESS_READ);
190
191         CHECK_MASK(e1->access_mask, SEC_STD_DELETE,
192                    share_access, NTCREATEX_SHARE_ACCESS_DELETE);
193         CHECK_MASK(access_mask, SEC_STD_DELETE,
194                    e1->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
195 #undef CHECK_MASK
196         return NT_STATUS_OK;
197 }
198
199 /*
200   pull a record, translating from the db format to the opendb_file structure defined
201   in opendb.idl
202 */
203 static NTSTATUS odb_pull_record(struct odb_lock *lck, struct opendb_file *file)
204 {
205         struct odb_context *odb = lck->odb;
206         TDB_DATA dbuf;
207         DATA_BLOB blob;
208         enum ndr_err_code ndr_err;
209
210         dbuf = tdb_fetch(odb->w->tdb, lck->key);
211         if (dbuf.dptr == NULL) {
212                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
213         }
214
215         blob.data = dbuf.dptr;
216         blob.length = dbuf.dsize;
217
218         ndr_err = ndr_pull_struct_blob(&blob, lck, lp_iconv_convenience(lck->odb->ntvfs_ctx->lp_ctx), file, (ndr_pull_flags_fn_t)ndr_pull_opendb_file);
219         free(dbuf.dptr);
220         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
221                 return ndr_map_error2ntstatus(ndr_err);
222         }
223
224         return NT_STATUS_OK;
225 }
226
227 /*
228   push a record, translating from the opendb_file structure defined in opendb.idl
229 */
230 static NTSTATUS odb_push_record(struct odb_lock *lck, struct opendb_file *file)
231 {
232         struct odb_context *odb = lck->odb;
233         TDB_DATA dbuf;
234         DATA_BLOB blob;
235         enum ndr_err_code ndr_err;
236         int ret;
237
238         if (file->num_entries == 0) {
239                 ret = tdb_delete(odb->w->tdb, lck->key);
240                 if (ret != 0) {
241                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
242                 }
243                 return NT_STATUS_OK;
244         }
245
246         ndr_err = ndr_push_struct_blob(&blob, lck, lp_iconv_convenience(lck->odb->ntvfs_ctx->lp_ctx), file, (ndr_push_flags_fn_t)ndr_push_opendb_file);
247         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
248                 return ndr_map_error2ntstatus(ndr_err);
249         }
250
251         dbuf.dptr = blob.data;
252         dbuf.dsize = blob.length;
253                 
254         ret = tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE);
255         data_blob_free(&blob);
256         if (ret != 0) {
257                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
258         }
259
260         return NT_STATUS_OK;
261 }
262
263 /*
264   send an oplock break to a client
265 */
266 static NTSTATUS odb_oplock_break_send(struct odb_context *odb,
267                                       struct opendb_entry *e,
268                                       uint8_t level)
269 {
270         NTSTATUS status;
271         struct opendb_oplock_break op_break;
272         DATA_BLOB blob;
273
274         ZERO_STRUCT(op_break);
275
276         /* tell the server handling this open file about the need to send the client
277            a break */
278         op_break.file_handle    = e->file_handle;
279         op_break.level          = level;
280
281         blob = data_blob_const(&op_break, sizeof(op_break));
282
283         status = messaging_send(odb->ntvfs_ctx->msg_ctx, e->server,
284                                 MSG_NTVFS_OPLOCK_BREAK, &blob);
285         NT_STATUS_NOT_OK_RETURN(status);
286
287         return NT_STATUS_OK;
288 }
289
290 static bool access_attributes_only(uint32_t access_mask,
291                                    uint32_t open_disposition)
292 {
293         switch (open_disposition) {
294         case NTCREATEX_DISP_SUPERSEDE:
295         case NTCREATEX_DISP_OVERWRITE_IF:
296         case NTCREATEX_DISP_OVERWRITE:
297                 return false;
298         default:
299                 break;
300         }
301 #define CHECK_MASK(m,g) ((m) && (((m) & ~(g))==0) && (((m) & (g)) != 0))
302         return CHECK_MASK(access_mask,
303                           SEC_STD_SYNCHRONIZE |
304                           SEC_FILE_READ_ATTRIBUTE |
305                           SEC_FILE_WRITE_ATTRIBUTE);
306 #undef CHECK_MASK
307 }
308
309 static NTSTATUS odb_tdb_open_can_internal(struct odb_context *odb,
310                                           const struct opendb_file *file,
311                                           uint32_t stream_id, uint32_t share_access,
312                                           uint32_t access_mask, bool delete_on_close,
313                                           uint32_t open_disposition, bool break_to_none,
314                                           bool *_attrs_only)
315 {
316         NTSTATUS status;
317         uint32_t i;
318         bool attrs_only = false;
319
320         /* see if anyone has an oplock, which we need to break */
321         for (i=0;i<file->num_entries;i++) {
322                 if (file->entries[i].oplock_level == OPLOCK_BATCH) {
323                         bool oplock_return = OPLOCK_BREAK_TO_LEVEL_II;
324                         /* if this is an attribute only access
325                          * it doesn't conflict with a BACTCH oplock
326                          * but we'll not grant the oplock below
327                          */
328                         attrs_only = access_attributes_only(access_mask,
329                                                             open_disposition);
330                         if (attrs_only) {
331                                 break;
332                         }
333                         /* a batch oplock caches close calls, which
334                            means the client application might have
335                            already closed the file. We have to allow
336                            this close to propogate by sending a oplock
337                            break request and suspending this call
338                            until the break is acknowledged or the file
339                            is closed */
340                         if (break_to_none) {
341                                 oplock_return = OPLOCK_BREAK_TO_NONE;
342                         }
343                         odb_oplock_break_send(odb, &file->entries[i],
344                                               oplock_return);
345                         return NT_STATUS_OPLOCK_NOT_GRANTED;
346                 }
347         }
348
349         if (file->delete_on_close) {
350                 /* while delete on close is set, no new opens are allowed */
351                 return NT_STATUS_DELETE_PENDING;
352         }
353
354         if (file->num_entries != 0 && delete_on_close) {
355                 return NT_STATUS_SHARING_VIOLATION;
356         }
357
358         /* check for sharing violations */
359         for (i=0;i<file->num_entries;i++) {
360                 status = share_conflict(&file->entries[i], stream_id,
361                                         share_access, access_mask);
362                 NT_STATUS_NOT_OK_RETURN(status);
363         }
364
365         /* we now know the open could succeed, but we need to check
366            for any exclusive oplocks. We can't grant a second open
367            till these are broken. Note that we check for batch oplocks
368            before checking for sharing violations, and check for
369            exclusive oplocks afterwards. */
370         for (i=0;i<file->num_entries;i++) {
371                 if (file->entries[i].oplock_level == OPLOCK_EXCLUSIVE) {
372                         odb_oplock_break_send(odb, &file->entries[i],
373                                               OPLOCK_BREAK_TO_NONE);
374                         return NT_STATUS_OPLOCK_NOT_GRANTED;
375                 }
376         }
377
378         if (_attrs_only) {
379                 *_attrs_only = attrs_only;
380         }
381         return NT_STATUS_OK;
382 }
383
384 /*
385   register an open file in the open files database. This implements the share_access
386   rules
387
388   Note that the path is only used by the delete on close logic, not
389   for comparing with other filenames
390 */
391 static NTSTATUS odb_tdb_open_file(struct odb_lock *lck, void *file_handle,
392                                   uint32_t stream_id, uint32_t share_access,
393                                   uint32_t access_mask, bool delete_on_close,
394                                   const char *path,
395                                   uint32_t oplock_level, uint32_t *oplock_granted)
396 {
397         struct odb_context *odb = lck->odb;
398         struct opendb_entry e;
399         struct opendb_file file;
400         NTSTATUS status;
401         uint32_t open_disposition = 0;
402         bool break_to_none = false;
403         bool attrs_only = false;
404
405         if (odb->oplocks == false) {
406                 oplock_level = OPLOCK_NONE;
407         }
408
409         status = odb_pull_record(lck, &file);
410         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
411                 /* initialise a blank structure */
412                 ZERO_STRUCT(file);
413                 file.path = path;
414         } else {
415                 NT_STATUS_NOT_OK_RETURN(status);
416         }
417
418         /* see if it conflicts */
419         status = odb_tdb_open_can_internal(odb, &file, stream_id,
420                                            share_access, access_mask,
421                                            delete_on_close, open_disposition,
422                                            break_to_none, &attrs_only);
423         NT_STATUS_NOT_OK_RETURN(status);
424
425         /* see if it conflicts */
426         e.server          = odb->ntvfs_ctx->server_id;
427         e.file_handle     = file_handle;
428         e.stream_id       = stream_id;
429         e.share_access    = share_access;
430         e.access_mask     = access_mask;
431         e.delete_on_close = delete_on_close;
432         e.oplock_level    = OPLOCK_NONE;
433
434         /*
435           possibly grant an exclusive, batch or level2 oplock
436         */
437         if (oplock_granted) {
438                 if (attrs_only) {
439                         e.oplock_level  = OPLOCK_NONE;
440                         *oplock_granted = NO_OPLOCK_RETURN;
441                 } else if (oplock_level == OPLOCK_EXCLUSIVE) {
442                         if (file.num_entries == 0) {
443                                 e.oplock_level  = OPLOCK_EXCLUSIVE;
444                                 *oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
445                         } else {
446                                 e.oplock_level  = OPLOCK_NONE;
447                                 *oplock_granted = NO_OPLOCK_RETURN;
448                         }
449                 } else if (oplock_level == OPLOCK_BATCH) {
450                         if (file.num_entries == 0) {
451                                 e.oplock_level  = OPLOCK_BATCH;
452                                 *oplock_granted = BATCH_OPLOCK_RETURN;
453                         } else {
454                                 e.oplock_level  = OPLOCK_LEVEL_II;
455                                 *oplock_granted = LEVEL_II_OPLOCK_RETURN;
456                         }
457                 } else if (oplock_level == OPLOCK_LEVEL_II) {
458                         e.oplock_level  = OPLOCK_LEVEL_II;
459                         *oplock_granted = LEVEL_II_OPLOCK_RETURN;
460                 } else {
461                         e.oplock_level  = OPLOCK_NONE;
462                         *oplock_granted = NO_OPLOCK_RETURN;
463                 }
464         }
465
466         /* it doesn't conflict, so add it to the end */
467         file.entries = talloc_realloc(lck, file.entries, struct opendb_entry, 
468                                       file.num_entries+1);
469         NT_STATUS_HAVE_NO_MEMORY(file.entries);
470
471         file.entries[file.num_entries] = e;
472         file.num_entries++;
473
474         return odb_push_record(lck, &file);
475 }
476
477
478 /*
479   register a pending open file in the open files database
480 */
481 static NTSTATUS odb_tdb_open_file_pending(struct odb_lock *lck, void *private)
482 {
483         struct odb_context *odb = lck->odb;
484         struct opendb_file file;
485         NTSTATUS status;
486                 
487         status = odb_pull_record(lck, &file);
488         NT_STATUS_NOT_OK_RETURN(status);
489
490         file.pending = talloc_realloc(lck, file.pending, struct opendb_pending, 
491                                       file.num_pending+1);
492         NT_STATUS_HAVE_NO_MEMORY(file.pending);
493
494         file.pending[file.num_pending].server = odb->ntvfs_ctx->server_id;
495         file.pending[file.num_pending].notify_ptr = private;
496
497         file.num_pending++;
498
499         return odb_push_record(lck, &file);
500 }
501
502
503 /*
504   remove a opendb entry
505 */
506 static NTSTATUS odb_tdb_close_file(struct odb_lock *lck, void *file_handle)
507 {
508         struct odb_context *odb = lck->odb;
509         struct opendb_file file;
510         int i;
511         NTSTATUS status;
512
513         status = odb_pull_record(lck, &file);
514         NT_STATUS_NOT_OK_RETURN(status);
515
516         /* find the entry, and delete it */
517         for (i=0;i<file.num_entries;i++) {
518                 if (file_handle == file.entries[i].file_handle &&
519                     cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.entries[i].server)) {
520                         if (file.entries[i].delete_on_close) {
521                                 file.delete_on_close = true;
522                         }
523                         if (i < file.num_entries-1) {
524                                 memmove(file.entries+i, file.entries+i+1, 
525                                         (file.num_entries - (i+1)) * 
526                                         sizeof(struct opendb_entry));
527                         }
528                         break;
529                 }
530         }
531
532         if (i == file.num_entries) {
533                 return NT_STATUS_UNSUCCESSFUL;
534         }
535
536         /* send any pending notifications, removing them once sent */
537         for (i=0;i<file.num_pending;i++) {
538                 messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, file.pending[i].server, 
539                                    MSG_PVFS_RETRY_OPEN, 
540                                    file.pending[i].notify_ptr);
541         }
542         file.num_pending = 0;
543
544         file.num_entries--;
545         
546         return odb_push_record(lck, &file);
547 }
548
549 /*
550   update the oplock level of the client
551 */
552 static NTSTATUS odb_tdb_update_oplock(struct odb_lock *lck, void *file_handle,
553                                       uint32_t oplock_level)
554 {
555         struct odb_context *odb = lck->odb;
556         struct opendb_file file;
557         int i;
558         NTSTATUS status;
559
560         status = odb_pull_record(lck, &file);
561         NT_STATUS_NOT_OK_RETURN(status);
562
563         /* find the entry, and update it */
564         for (i=0;i<file.num_entries;i++) {
565                 if (file_handle == file.entries[i].file_handle &&
566                     cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.entries[i].server)) {
567                         file.entries[i].oplock_level = oplock_level;
568                         break;
569                 }
570         }
571
572         if (i == file.num_entries) {
573                 return NT_STATUS_UNSUCCESSFUL;
574         }
575
576         /* send any pending notifications, removing them once sent */
577         for (i=0;i<file.num_pending;i++) {
578                 messaging_send_ptr(odb->ntvfs_ctx->msg_ctx,
579                                    file.pending[i].server,
580                                    MSG_PVFS_RETRY_OPEN,
581                                    file.pending[i].notify_ptr);
582         }
583         file.num_pending = 0;
584
585         return odb_push_record(lck, &file);
586 }
587
588 /*
589   send oplocks breaks to none to all level2 holders
590 */
591 static NTSTATUS odb_tdb_break_oplocks(struct odb_lock *lck)
592 {
593         struct odb_context *odb = lck->odb;
594         NTSTATUS status;
595         struct opendb_file file;
596         int i;
597         bool modified = true;
598
599         status = odb_pull_record(lck, &file);
600         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
601                 return NT_STATUS_OK;
602         }
603         NT_STATUS_NOT_OK_RETURN(status);
604
605         /* see if anyone has an oplock, which we need to break */
606         for (i=0;i<file.num_entries;i++) {
607                 if (file.entries[i].oplock_level == OPLOCK_LEVEL_II) {
608                         /*
609                          * there could be multiple level2 oplocks
610                          * and we just send a break to none to all of them
611                          * without waiting for a release
612                          */
613                         odb_oplock_break_send(odb, &file.entries[i],
614                                               OPLOCK_BREAK_TO_NONE);
615                         file.entries[i].oplock_level = OPLOCK_NONE;
616                         modified = true;
617                 }
618         }
619
620         if (modified) {
621                 return odb_push_record(lck, &file);
622         }
623         return NT_STATUS_OK;
624 }
625
626 /*
627   remove a pending opendb entry
628 */
629 static NTSTATUS odb_tdb_remove_pending(struct odb_lock *lck, void *private)
630 {
631         struct odb_context *odb = lck->odb;
632         int i;
633         NTSTATUS status;
634         struct opendb_file file;
635
636         status = odb_pull_record(lck, &file);
637         NT_STATUS_NOT_OK_RETURN(status);
638
639         /* find the entry, and delete it */
640         for (i=0;i<file.num_pending;i++) {
641                 if (private == file.pending[i].notify_ptr &&
642                     cluster_id_equal(&odb->ntvfs_ctx->server_id, &file.pending[i].server)) {
643                         if (i < file.num_pending-1) {
644                                 memmove(file.pending+i, file.pending+i+1, 
645                                         (file.num_pending - (i+1)) * 
646                                         sizeof(struct opendb_pending));
647                         }
648                         break;
649                 }
650         }
651
652         if (i == file.num_pending) {
653                 return NT_STATUS_UNSUCCESSFUL;
654         }
655
656         file.num_pending--;
657         
658         return odb_push_record(lck, &file);
659 }
660
661
662 /*
663   rename the path in a open file
664 */
665 static NTSTATUS odb_tdb_rename(struct odb_lock *lck, const char *path)
666 {
667         struct opendb_file file;
668         NTSTATUS status;
669
670         status = odb_pull_record(lck, &file);
671         if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
672                 /* not having the record at all is OK */
673                 return NT_STATUS_OK;
674         }
675         NT_STATUS_NOT_OK_RETURN(status);
676
677         file.path = path;
678         return odb_push_record(lck, &file);
679 }
680
681 /*
682   update delete on close flag on an open file
683 */
684 static NTSTATUS odb_tdb_set_delete_on_close(struct odb_lock *lck, bool del_on_close)
685 {
686         NTSTATUS status;
687         struct opendb_file file;
688
689         status = odb_pull_record(lck, &file);
690         NT_STATUS_NOT_OK_RETURN(status);
691
692         file.delete_on_close = del_on_close;
693
694         return odb_push_record(lck, &file);
695 }
696
697 /*
698   return the current value of the delete_on_close bit, and how many
699   people still have the file open
700 */
701 static NTSTATUS odb_tdb_get_delete_on_close(struct odb_context *odb, 
702                                             DATA_BLOB *key, bool *del_on_close, 
703                                             int *open_count, char **path)
704 {
705         NTSTATUS status;
706         struct opendb_file file;
707         struct odb_lock *lck;
708
709         lck = odb_lock(odb, odb, key);
710         NT_STATUS_HAVE_NO_MEMORY(lck);
711
712         status = odb_pull_record(lck, &file);
713         if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
714                 talloc_free(lck);
715                 (*del_on_close) = false;
716                 return NT_STATUS_OK;
717         }
718         if (!NT_STATUS_IS_OK(status)) {
719                 talloc_free(lck);
720                 return status;
721         }
722
723         (*del_on_close) = file.delete_on_close;
724         if (open_count != NULL) {
725                 (*open_count) = file.num_entries;
726         }
727         if (path != NULL) {
728                 *path = talloc_strdup(odb, file.path);
729                 NT_STATUS_HAVE_NO_MEMORY(*path);
730                 if (file.num_entries == 1 && file.entries[0].delete_on_close) {
731                         (*del_on_close) = true;
732                 }
733         }
734
735         talloc_free(lck);
736
737         return NT_STATUS_OK;
738 }
739
740
741 /*
742   determine if a file can be opened with the given share_access,
743   create_options and access_mask
744 */
745 static NTSTATUS odb_tdb_can_open(struct odb_lock *lck,
746                                  uint32_t share_access, uint32_t create_options, 
747                                  uint32_t access_mask)
748 {
749         struct odb_context *odb = lck->odb;
750         NTSTATUS status;
751         struct opendb_file file;
752         uint32_t stream_id = 0;
753         uint32_t open_disposition = 0;
754         bool delete_on_close = false;
755         bool break_to_none = false;
756         bool attrs_only = false;
757
758         status = odb_pull_record(lck, &file);
759         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
760                 return NT_STATUS_OK;
761         }
762         NT_STATUS_NOT_OK_RETURN(status);
763
764         if (create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
765                 delete_on_close = true;
766         }
767
768         status = odb_tdb_open_can_internal(odb, &file, stream_id,
769                                            share_access, access_mask,
770                                            delete_on_close, open_disposition,
771                                            break_to_none, &attrs_only);
772         NT_STATUS_NOT_OK_RETURN(status);
773
774         return NT_STATUS_OK;
775 }
776
777
778 static const struct opendb_ops opendb_tdb_ops = {
779         .odb_init                = odb_tdb_init,
780         .odb_lock                = odb_tdb_lock,
781         .odb_get_key             = odb_tdb_get_key,
782         .odb_open_file           = odb_tdb_open_file,
783         .odb_open_file_pending   = odb_tdb_open_file_pending,
784         .odb_close_file          = odb_tdb_close_file,
785         .odb_remove_pending      = odb_tdb_remove_pending,
786         .odb_rename              = odb_tdb_rename,
787         .odb_set_delete_on_close = odb_tdb_set_delete_on_close,
788         .odb_get_delete_on_close = odb_tdb_get_delete_on_close,
789         .odb_can_open            = odb_tdb_can_open,
790         .odb_update_oplock       = odb_tdb_update_oplock,
791         .odb_break_oplocks       = odb_tdb_break_oplocks
792 };
793
794
795 void odb_tdb_init_ops(void)
796 {
797         odb_set_ops(&opendb_tdb_ops);
798 }