r21807: modularise the opendb code, so different backends can be
[samba.git] / source4 / ntvfs / common / opendb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2004
5    
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.
10    
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.
15    
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.
19 */
20
21 /*
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.
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 "ntvfs/ntvfs.h"
43 #include "ntvfs/common/ntvfs_common.h"
44 #include "cluster/cluster.h"
45
46 static const struct opendb_ops *ops;
47
48 /*
49   set the odb backend ops
50 */
51 void odb_set_ops(const struct opendb_ops *new_ops)
52 {
53         ops = new_ops;
54 }
55
56 /*
57   Open up the openfiles.tdb database. Close it down using
58   talloc_free(). We need the messaging_ctx to allow for pending open
59   notifications.
60 */
61 _PUBLIC_ struct odb_context *odb_init(TALLOC_CTX *mem_ctx, 
62                                       struct ntvfs_context *ntvfs_ctx)
63 {
64         if (ops == NULL) {
65                 odb_tdb_init_ops();
66         }
67         return ops->odb_init(mem_ctx, ntvfs_ctx);
68 }
69
70 /*
71   get a lock on a entry in the odb. This call returns a lock handle,
72   which the caller should unlock using talloc_free().
73 */
74 _PUBLIC_ struct odb_lock *odb_lock(TALLOC_CTX *mem_ctx,
75                                    struct odb_context *odb, DATA_BLOB *file_key)
76 {
77         return ops->odb_lock(mem_ctx, odb, file_key);
78 }
79
80
81 /*
82   register an open file in the open files database. This implements the share_access
83   rules
84
85   Note that the path is only used by the delete on close logic, not
86   for comparing with other filenames
87 */
88 _PUBLIC_ NTSTATUS odb_open_file(struct odb_lock *lck, void *file_handle,
89                                 uint32_t stream_id, uint32_t share_access, 
90                                 uint32_t access_mask, BOOL delete_on_close,
91                                 const char *path, 
92                                 uint32_t oplock_level, uint32_t *oplock_granted)
93 {
94         return ops->odb_open_file(lck, file_handle, stream_id, share_access,
95                                   access_mask, delete_on_close, path, oplock_level,
96                                   oplock_granted);
97 }
98
99
100 /*
101   register a pending open file in the open files database
102 */
103 _PUBLIC_ NTSTATUS odb_open_file_pending(struct odb_lock *lck, void *private)
104 {
105         return ops->odb_open_file_pending(lck, private);
106 }
107
108
109 /*
110   remove a opendb entry
111 */
112 _PUBLIC_ NTSTATUS odb_close_file(struct odb_lock *lck, void *file_handle)
113 {
114         return ops->odb_close_file(lck, file_handle);
115 }
116
117
118 /*
119   remove a pending opendb entry
120 */
121 _PUBLIC_ NTSTATUS odb_remove_pending(struct odb_lock *lck, void *private)
122 {
123         return ops->odb_remove_pending(lck, private);
124 }
125
126
127 /*
128   rename the path in a open file
129 */
130 _PUBLIC_ NTSTATUS odb_rename(struct odb_lock *lck, const char *path)
131 {
132         return ops->odb_rename(lck, path);
133 }
134
135 /*
136   update delete on close flag on an open file
137 */
138 _PUBLIC_ NTSTATUS odb_set_delete_on_close(struct odb_lock *lck, BOOL del_on_close)
139 {
140         return ops->odb_set_delete_on_close(lck, del_on_close);
141 }
142
143 /*
144   return the current value of the delete_on_close bit, and how many
145   people still have the file open
146 */
147 _PUBLIC_ NTSTATUS odb_get_delete_on_close(struct odb_context *odb, 
148                                           DATA_BLOB *key, BOOL *del_on_close, 
149                                           int *open_count, char **path)
150 {
151         return ops->odb_get_delete_on_close(odb, key, del_on_close, open_count, path);
152 }
153
154
155 /*
156   determine if a file can be opened with the given share_access,
157   create_options and access_mask
158 */
159 _PUBLIC_ NTSTATUS odb_can_open(struct odb_lock *lck,
160                                uint32_t share_access, uint32_t create_options, 
161                                uint32_t access_mask)
162 {
163         return ops->odb_can_open(lck, share_access, create_options, access_mask);
164 }