1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Cache db contents for parse_record based on seqnum
|
---|
4 | Copyright (C) Volker Lendecke 2012
|
---|
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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "replace.h"
|
---|
21 | #include "lib/param/loadparm.h"
|
---|
22 | #include "lib/dbwrap/dbwrap_cache.h"
|
---|
23 | #include "lib/dbwrap/dbwrap_private.h"
|
---|
24 | #include "lib/dbwrap/dbwrap_rbt.h"
|
---|
25 | #include "lib/util/talloc_stack.h"
|
---|
26 |
|
---|
27 | struct db_cache_ctx {
|
---|
28 | int seqnum;
|
---|
29 | struct db_context *backing;
|
---|
30 | struct db_context *positive;
|
---|
31 | struct db_context *negative;
|
---|
32 | };
|
---|
33 |
|
---|
34 | static bool dbwrap_cache_validate(struct db_cache_ctx *ctx)
|
---|
35 | {
|
---|
36 | int backing_seqnum;
|
---|
37 |
|
---|
38 | backing_seqnum = dbwrap_get_seqnum(ctx->backing);
|
---|
39 | if (backing_seqnum == ctx->seqnum) {
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | TALLOC_FREE(ctx->positive);
|
---|
44 | ctx->positive = db_open_rbt(ctx);
|
---|
45 | if (ctx->positive == NULL) {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | TALLOC_FREE(ctx->negative);
|
---|
50 | ctx->negative = db_open_rbt(ctx);
|
---|
51 | if (ctx->negative == NULL) {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | ctx->seqnum = backing_seqnum;
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | static NTSTATUS dbwrap_cache_parse_record(
|
---|
60 | struct db_context *db, TDB_DATA key,
|
---|
61 | void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
|
---|
62 | void *private_data)
|
---|
63 | {
|
---|
64 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
65 | db->private_data, struct db_cache_ctx);
|
---|
66 | TDB_DATA value;
|
---|
67 | NTSTATUS status;
|
---|
68 |
|
---|
69 | if (!dbwrap_cache_validate(ctx)) {
|
---|
70 | return NT_STATUS_NO_MEMORY;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (dbwrap_exists(ctx->negative, key)) {
|
---|
74 | return NT_STATUS_NOT_FOUND;
|
---|
75 | }
|
---|
76 | status = dbwrap_parse_record(ctx->positive, key, parser, private_data);
|
---|
77 | if (NT_STATUS_IS_OK(status)) {
|
---|
78 | return status;
|
---|
79 | }
|
---|
80 |
|
---|
81 | status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value);
|
---|
82 |
|
---|
83 | if (NT_STATUS_IS_OK(status)) {
|
---|
84 | dbwrap_store(ctx->positive, key, value, 0);
|
---|
85 | parser(key, value, private_data);
|
---|
86 | TALLOC_FREE(value.dptr);
|
---|
87 | return NT_STATUS_OK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
|
---|
91 | char c = '\0';
|
---|
92 | value.dptr = (uint8_t *)&c;
|
---|
93 | value.dsize = sizeof(c);
|
---|
94 | dbwrap_store(ctx->negative, key, value, 0);
|
---|
95 | return NT_STATUS_NOT_FOUND;
|
---|
96 | }
|
---|
97 | return status;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static struct db_record *dbwrap_cache_fetch_locked(
|
---|
101 | struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
|
---|
102 | {
|
---|
103 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
104 | db->private_data, struct db_cache_ctx);
|
---|
105 | return dbwrap_fetch_locked(ctx->backing, mem_ctx, key);
|
---|
106 | }
|
---|
107 |
|
---|
108 | static int dbwrap_cache_traverse(struct db_context *db,
|
---|
109 | int (*f)(struct db_record *rec,
|
---|
110 | void *private_data),
|
---|
111 | void *private_data)
|
---|
112 | {
|
---|
113 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
114 | db->private_data, struct db_cache_ctx);
|
---|
115 | NTSTATUS status;
|
---|
116 | int ret;
|
---|
117 | status = dbwrap_traverse(ctx->backing, f, private_data, &ret);
|
---|
118 | if (!NT_STATUS_IS_OK(status)) {
|
---|
119 | return -1;
|
---|
120 | }
|
---|
121 | return ret;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int dbwrap_cache_traverse_read(struct db_context *db,
|
---|
125 | int (*f)(struct db_record *rec,
|
---|
126 | void *private_data),
|
---|
127 | void *private_data)
|
---|
128 | {
|
---|
129 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
130 | db->private_data, struct db_cache_ctx);
|
---|
131 | NTSTATUS status;
|
---|
132 | int ret;
|
---|
133 | status = dbwrap_traverse_read(ctx->backing, f, private_data, &ret);
|
---|
134 | if (!NT_STATUS_IS_OK(status)) {
|
---|
135 | return -1;
|
---|
136 | }
|
---|
137 | return ret;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static int dbwrap_cache_get_seqnum(struct db_context *db)
|
---|
141 | {
|
---|
142 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
143 | db->private_data, struct db_cache_ctx);
|
---|
144 | return dbwrap_get_seqnum(ctx->backing);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static int dbwrap_cache_transaction_start(struct db_context *db)
|
---|
148 | {
|
---|
149 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
150 | db->private_data, struct db_cache_ctx);
|
---|
151 | return dbwrap_transaction_start(ctx->backing);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static int dbwrap_cache_transaction_commit(struct db_context *db)
|
---|
155 | {
|
---|
156 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
157 | db->private_data, struct db_cache_ctx);
|
---|
158 | return dbwrap_transaction_commit(ctx->backing);
|
---|
159 | }
|
---|
160 |
|
---|
161 | static int dbwrap_cache_transaction_cancel(struct db_context *db)
|
---|
162 | {
|
---|
163 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
164 | db->private_data, struct db_cache_ctx);
|
---|
165 | return dbwrap_transaction_cancel(ctx->backing);
|
---|
166 | }
|
---|
167 |
|
---|
168 | static int dbwrap_cache_exists(struct db_context *db, TDB_DATA key)
|
---|
169 | {
|
---|
170 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
171 | db->private_data, struct db_cache_ctx);
|
---|
172 |
|
---|
173 | if (ctx->positive && dbwrap_exists(ctx->positive, key)) {
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 | if (ctx->negative && dbwrap_exists(ctx->negative, key)) {
|
---|
177 | return false;
|
---|
178 | }
|
---|
179 | return dbwrap_exists(ctx->backing, key);
|
---|
180 | }
|
---|
181 |
|
---|
182 | static size_t dbwrap_cache_id(struct db_context *db, uint8_t *id,
|
---|
183 | size_t idlen)
|
---|
184 | {
|
---|
185 | struct db_cache_ctx *ctx = talloc_get_type_abort(
|
---|
186 | db->private_data, struct db_cache_ctx);
|
---|
187 |
|
---|
188 | return dbwrap_db_id(ctx->backing, id, idlen);
|
---|
189 | }
|
---|
190 |
|
---|
191 | struct db_context *db_open_cache(TALLOC_CTX *mem_ctx,
|
---|
192 | struct db_context *backing)
|
---|
193 | {
|
---|
194 | struct db_context *db;
|
---|
195 | struct db_cache_ctx *ctx;
|
---|
196 |
|
---|
197 | db = talloc_zero(mem_ctx, struct db_context);
|
---|
198 | if (db == NULL) {
|
---|
199 | return NULL;
|
---|
200 | }
|
---|
201 | ctx = talloc_zero(db, struct db_cache_ctx);
|
---|
202 | if (ctx == NULL) {
|
---|
203 | TALLOC_FREE(db);
|
---|
204 | return NULL;
|
---|
205 | }
|
---|
206 |
|
---|
207 | ctx->seqnum = -1;
|
---|
208 | ctx->backing = talloc_move(ctx, &backing);
|
---|
209 | db->private_data = ctx;
|
---|
210 | if (!dbwrap_cache_validate(ctx)) {
|
---|
211 | TALLOC_FREE(db);
|
---|
212 | return NULL;
|
---|
213 | }
|
---|
214 |
|
---|
215 | db->fetch_locked = dbwrap_cache_fetch_locked;
|
---|
216 | db->traverse = dbwrap_cache_traverse;
|
---|
217 | db->traverse_read = dbwrap_cache_traverse_read;
|
---|
218 | db->get_seqnum = dbwrap_cache_get_seqnum;
|
---|
219 | db->transaction_start = dbwrap_cache_transaction_start;
|
---|
220 | db->transaction_commit = dbwrap_cache_transaction_commit;
|
---|
221 | db->transaction_cancel = dbwrap_cache_transaction_cancel;
|
---|
222 | db->parse_record = dbwrap_cache_parse_record;
|
---|
223 | db->exists = dbwrap_cache_exists;
|
---|
224 | db->id = dbwrap_cache_id;
|
---|
225 | db->name = dbwrap_name(ctx->backing);
|
---|
226 | return db;
|
---|
227 | }
|
---|