2d656472219fa8256e44e18bb82cea3d8107cf9f
[abartlet/samba-debian.git] / lib / dbwrap / dbwrap_rbt.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface wrapper around red-black trees
4    Copyright (C) Volker Lendecke 2007, 2008
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 "includes.h"
21 #include "dbwrap/dbwrap.h"
22 #include "dbwrap/dbwrap_private.h"
23 #include "dbwrap/dbwrap_rbt.h"
24 #include "../lib/util/rbtree.h"
25
26 #define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15)
27
28 struct db_rbt_ctx {
29         struct rb_root tree;
30 };
31
32 struct db_rbt_rec {
33         struct db_rbt_node *node;
34 };
35
36 /* The structure that ends up in the tree */
37
38 struct db_rbt_node {
39         struct rb_node rb_node;
40         size_t keysize, valuesize;
41 };
42
43 /*
44  * Hide the ugly pointer calculations in a function
45  */
46
47 static struct db_rbt_node *db_rbt2node(struct rb_node *node)
48 {
49         return (struct db_rbt_node *)
50                 ((char *)node - offsetof(struct db_rbt_node, rb_node));
51 }
52
53 /*
54  * Compare two keys
55  */
56
57 static int db_rbt_compare(TDB_DATA a, TDB_DATA b)
58 {
59         int res;
60
61         res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize));
62
63         if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) {
64                 return -1;
65         }
66         if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) {
67                 return 1;
68         }
69         return 0;
70 }
71
72 /*
73  * dissect a db_rbt_node into its implicit key and value parts
74  */
75
76 static void db_rbt_parse_node(struct db_rbt_node *node,
77                               TDB_DATA *key, TDB_DATA *value)
78 {
79         size_t key_offset, value_offset;
80
81         key_offset = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
82         key->dptr = ((uint8_t *)node) + key_offset;
83         key->dsize = node->keysize;
84
85         value_offset = DBWRAP_RBT_ALIGN(node->keysize);
86         value->dptr = key->dptr + value_offset;
87         value->dsize = node->valuesize;
88 }
89
90 static ssize_t db_rbt_reclen(size_t keylen, size_t valuelen)
91 {
92         size_t len, tmp;
93
94         len = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
95
96         tmp = DBWRAP_RBT_ALIGN(keylen);
97         if (tmp < keylen) {
98                 goto overflow;
99         }
100
101         len += tmp;
102         if (len < tmp) {
103                 goto overflow;
104         }
105
106         len += valuelen;
107         if (len < valuelen) {
108                 goto overflow;
109         }
110
111         return len;
112 overflow:
113         return -1;
114 }
115
116 static NTSTATUS db_rbt_store(struct db_record *rec, TDB_DATA data, int flag)
117 {
118         struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
119                 rec->db->private_data, struct db_rbt_ctx);
120         struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
121         struct db_rbt_node *node;
122
123         struct rb_node ** p;
124         struct rb_node * parent;
125
126         ssize_t reclen;
127         TDB_DATA this_key, this_val;
128
129         if (rec_priv->node != NULL) {
130
131                 /*
132                  * The record was around previously
133                  */
134
135                 db_rbt_parse_node(rec_priv->node, &this_key, &this_val);
136
137                 SMB_ASSERT(this_key.dsize == rec->key.dsize);
138                 SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr,
139                                   this_key.dsize) == 0);
140
141                 if (this_val.dsize >= data.dsize) {
142                         /*
143                          * The new value fits into the old space
144                          */
145                         memcpy(this_val.dptr, data.dptr, data.dsize);
146                         rec_priv->node->valuesize = data.dsize;
147                         return NT_STATUS_OK;
148                 }
149         }
150
151         reclen = db_rbt_reclen(rec->key.dsize, data.dsize);
152         if (reclen == -1) {
153                 return NT_STATUS_INSUFFICIENT_RESOURCES;
154         }
155
156         node = talloc_zero_size(db_ctx, reclen);
157         if (node == NULL) {
158                 return NT_STATUS_NO_MEMORY;
159         }
160
161         if (rec_priv->node != NULL) {
162                 /*
163                  * We need to delete the key from the tree and start fresh,
164                  * there's not enough space in the existing record
165                  */
166
167                 rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
168
169                 /*
170                  * Keep the existing node around for a while: If the record
171                  * existed before, we reference the key data in there.
172                  */
173         }
174
175         node->keysize = rec->key.dsize;
176         node->valuesize = data.dsize;
177
178         db_rbt_parse_node(node, &this_key, &this_val);
179
180         memcpy(this_key.dptr, rec->key.dptr, node->keysize);
181         TALLOC_FREE(rec_priv->node);
182         rec_priv->node = node;
183
184         memcpy(this_val.dptr, data.dptr, node->valuesize);
185
186         parent = NULL;
187         p = &db_ctx->tree.rb_node;
188
189         while (*p) {
190                 struct db_rbt_node *r;
191                 TDB_DATA search_key, search_val;
192                 int res;
193
194                 parent = (*p);
195
196                 r = db_rbt2node(*p);
197
198                 db_rbt_parse_node(r, &search_key, &search_val);
199
200                 res = db_rbt_compare(this_key, search_key);
201
202                 if (res == -1) {
203                         p = &(*p)->rb_left;
204                 }
205                 else if (res == 1) {
206                         p = &(*p)->rb_right;
207                 }
208                 else {
209                         smb_panic("someone messed with the tree");
210                 }
211         }
212
213         rb_link_node(&node->rb_node, parent, p);
214         rb_insert_color(&node->rb_node, &db_ctx->tree);
215
216         return NT_STATUS_OK;
217 }
218
219 static NTSTATUS db_rbt_delete(struct db_record *rec)
220 {
221         struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
222                 rec->db->private_data, struct db_rbt_ctx);
223         struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
224
225         if (rec_priv->node == NULL) {
226                 return NT_STATUS_OK;
227         }
228
229         rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
230         TALLOC_FREE(rec_priv->node);
231
232         return NT_STATUS_OK;
233 }
234
235 static NTSTATUS db_rbt_store_deny(struct db_record *rec, TDB_DATA data, int flag)
236 {
237         return NT_STATUS_MEDIA_WRITE_PROTECTED;
238 }
239
240 static NTSTATUS db_rbt_delete_deny(struct db_record *rec)
241 {
242         return NT_STATUS_MEDIA_WRITE_PROTECTED;
243 }
244
245 struct db_rbt_search_result {
246         TDB_DATA key;
247         TDB_DATA val;
248         struct db_rbt_node* node;
249 };
250
251 static bool db_rbt_search_internal(struct db_context *db, TDB_DATA key,
252                                    struct db_rbt_search_result *result)
253 {
254         struct db_rbt_ctx *ctx = talloc_get_type_abort(
255                 db->private_data, struct db_rbt_ctx);
256
257         struct rb_node *n;
258         bool found = false;
259         struct db_rbt_node *r = NULL;
260         TDB_DATA search_key, search_val;
261
262         n = ctx->tree.rb_node;
263
264         while (n != NULL) {
265                 int res;
266
267                 r = db_rbt2node(n);
268
269                 db_rbt_parse_node(r, &search_key, &search_val);
270
271                 res = db_rbt_compare(key, search_key);
272
273                 if (res == -1) {
274                         n = n->rb_left;
275                 }
276                 else if (res == 1) {
277                         n = n->rb_right;
278                 }
279                 else {
280                         found = true;
281                         break;
282                 }
283         }
284         if (result != NULL) {
285                 if (found) {
286                         result->key = search_key;
287                         result->val = search_val;
288                         result->node = r;
289                 } else {
290                         ZERO_STRUCT(*result);
291                 }
292         }
293         return found;
294 }
295
296 static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx,
297                                              TALLOC_CTX *mem_ctx,
298                                              TDB_DATA key)
299 {
300         struct db_rbt_rec *rec_priv;
301         struct db_record *result;
302         size_t size;
303         bool found;
304         struct db_rbt_search_result res;
305
306         found = db_rbt_search_internal(db_ctx, key, &res);
307
308         /*
309          * In this low-level routine, play tricks to reduce the number of
310          * tallocs to one. Not recommened for general use, but here it pays
311          * off.
312          */
313
314         size = DBWRAP_RBT_ALIGN(sizeof(struct db_record))
315                 + sizeof(struct db_rbt_rec);
316
317         if (!found) {
318                 /*
319                  * We need to keep the key around for later store
320                  */
321                 size += key.dsize;
322         }
323
324         result = (struct db_record *)talloc_size(mem_ctx, size);
325         if (result == NULL) {
326                 return NULL;
327         }
328
329         rec_priv = (struct db_rbt_rec *)
330                 ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record)));
331
332         result->store = db_rbt_store;
333         result->delete_rec = db_rbt_delete;
334         result->private_data = rec_priv;
335
336         rec_priv->node = res.node;
337         result->value  = res.val;
338
339         if (found) {
340                 result->key = res.key;
341         }
342         else {
343                 result->key.dptr = (uint8_t *)
344                         ((char *)rec_priv + sizeof(*rec_priv));
345                 result->key.dsize = key.dsize;
346                 memcpy(result->key.dptr, key.dptr, key.dsize);
347         }
348
349         return result;
350 }
351
352 static int db_rbt_exists(struct db_context *db, TDB_DATA key)
353 {
354         return db_rbt_search_internal(db, key, NULL);
355 }
356
357 static int db_rbt_wipe(struct db_context *db)
358 {
359         struct db_rbt_ctx *old_ctx = talloc_get_type_abort(
360                 db->private_data, struct db_rbt_ctx);
361         struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx);
362         if (new_ctx == NULL) {
363                 return -1;
364         }
365         db->private_data = new_ctx;
366         talloc_free(old_ctx);
367         return 0;
368 }
369
370 static NTSTATUS db_rbt_parse_record(struct db_context *db, TDB_DATA key,
371                                     void (*parser)(TDB_DATA key, TDB_DATA data,
372                                                    void *private_data),
373                                     void *private_data)
374 {
375         struct db_rbt_search_result res;
376         bool found = db_rbt_search_internal(db, key, &res);
377
378         if (!found) {
379                 return NT_STATUS_NOT_FOUND;
380         }
381         parser(res.key, res.val, private_data);
382         return NT_STATUS_OK;
383 }
384
385 static int db_rbt_traverse_internal(struct db_context *db,
386                                     struct rb_node *n,
387                                     int (*f)(struct db_record *db,
388                                              void *private_data),
389                                     void *private_data, uint32_t* count,
390                                     bool rw)
391 {
392         struct rb_node *rb_right;
393         struct rb_node *rb_left;
394         struct db_record rec;
395         struct db_rbt_rec rec_priv;
396         int ret;
397
398         if (n == NULL) {
399                 return 0;
400         }
401
402         rb_left = n->rb_left;
403         rb_right = n->rb_right;
404
405         ret = db_rbt_traverse_internal(db, rb_left, f, private_data, count, rw);
406         if (ret != 0) {
407                 return ret;
408         }
409
410         rec_priv.node = db_rbt2node(n);
411         /* n might be altered by the callback function */
412         n = NULL;
413
414         ZERO_STRUCT(rec);
415         rec.db = db;
416         rec.private_data = &rec_priv;
417         if (rw) {
418                 rec.store = db_rbt_store;
419                 rec.delete_rec = db_rbt_delete;
420         } else {
421                 rec.store = db_rbt_store_deny;
422                 rec.delete_rec = db_rbt_delete_deny;
423         }
424         db_rbt_parse_node(rec_priv.node, &rec.key, &rec.value);
425
426         ret = f(&rec, private_data);
427         (*count) ++;
428         if (ret != 0) {
429                 return ret;
430         }
431
432         if (rec_priv.node != NULL) {
433                 /*
434                  * If the current record is still there
435                  * we should take the current rb_right.
436                  */
437                 rb_right = rec_priv.node->rb_node.rb_right;
438         }
439
440         return db_rbt_traverse_internal(db, rb_right, f, private_data, count, rw);
441 }
442
443 static int db_rbt_traverse(struct db_context *db,
444                            int (*f)(struct db_record *db,
445                                     void *private_data),
446                            void *private_data)
447 {
448         struct db_rbt_ctx *ctx = talloc_get_type_abort(
449                 db->private_data, struct db_rbt_ctx);
450         uint32_t count = 0;
451
452         int ret = db_rbt_traverse_internal(db, ctx->tree.rb_node,
453                                            f, private_data, &count,
454                                            true /* rw */);
455         if (ret != 0) {
456                 return -1;
457         }
458         if (count > INT_MAX) {
459                 return -1;
460         }
461         return count;
462 }
463
464 static int db_rbt_traverse_read(struct db_context *db,
465                                 int (*f)(struct db_record *db,
466                                          void *private_data),
467                                 void *private_data)
468 {
469         struct db_rbt_ctx *ctx = talloc_get_type_abort(
470                 db->private_data, struct db_rbt_ctx);
471         uint32_t count = 0;
472
473         int ret = db_rbt_traverse_internal(db, ctx->tree.rb_node,
474                                            f, private_data, &count,
475                                            false /* rw */);
476         if (ret != 0) {
477                 return -1;
478         }
479         if (count > INT_MAX) {
480                 return -1;
481         }
482         return count;
483 }
484
485 static int db_rbt_get_seqnum(struct db_context *db)
486 {
487         return 0;
488 }
489
490 static int db_rbt_trans_dummy(struct db_context *db)
491 {
492         /*
493          * Transactions are pretty pointless in-memory, just return success.
494          */
495         return 0;
496 }
497
498 static void db_rbt_id(struct db_context *db, const uint8_t **id, size_t *idlen)
499 {
500         *id = (uint8_t *)db;
501         *idlen = sizeof(struct db_context *);
502 }
503
504 struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx)
505 {
506         struct db_context *result;
507
508         result = talloc_zero(mem_ctx, struct db_context);
509
510         if (result == NULL) {
511                 return NULL;
512         }
513
514         result->private_data = talloc_zero(result, struct db_rbt_ctx);
515
516         if (result->private_data == NULL) {
517                 TALLOC_FREE(result);
518                 return NULL;
519         }
520
521         result->fetch_locked = db_rbt_fetch_locked;
522         result->traverse = db_rbt_traverse;
523         result->traverse_read = db_rbt_traverse_read;
524         result->get_seqnum = db_rbt_get_seqnum;
525         result->transaction_start = db_rbt_trans_dummy;
526         result->transaction_commit = db_rbt_trans_dummy;
527         result->transaction_cancel = db_rbt_trans_dummy;
528         result->exists = db_rbt_exists;
529         result->wipe = db_rbt_wipe;
530         result->parse_record = db_rbt_parse_record;
531         result->id = db_rbt_id;
532         result->name = "dbwrap rbt";
533
534         return result;
535 }