c8f1fd03c49b61466b54c484214cc3de5bac2c88
[kai/samba-autobuild/.git] / lib / ntdb / test / helprun-layout.c
1 /* NTDB tools to create various canned database layouts. */
2 #include "layout.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <ccan/err/err.h>
7 #include "logging.h"
8
9 struct ntdb_layout *new_ntdb_layout(void)
10 {
11         struct ntdb_layout *layout = malloc(sizeof(*layout));
12         layout->num_elems = 0;
13         layout->elem = NULL;
14         return layout;
15 }
16
17 static void add(struct ntdb_layout *layout, union ntdb_layout_elem elem)
18 {
19         layout->elem = realloc(layout->elem,
20                                sizeof(layout->elem[0])
21                                * (layout->num_elems+1));
22         layout->elem[layout->num_elems++] = elem;
23 }
24
25 void ntdb_layout_add_freetable(struct ntdb_layout *layout)
26 {
27         union ntdb_layout_elem elem;
28         elem.base.type = FREETABLE;
29         add(layout, elem);
30 }
31
32 void ntdb_layout_add_free(struct ntdb_layout *layout, ntdb_len_t len,
33                          unsigned ftable)
34 {
35         union ntdb_layout_elem elem;
36         elem.base.type = FREE;
37         elem.free.len = len;
38         elem.free.ftable_num = ftable;
39         add(layout, elem);
40 }
41
42 void ntdb_layout_add_capability(struct ntdb_layout *layout,
43                                uint64_t type,
44                                bool write_breaks,
45                                bool check_breaks,
46                                bool open_breaks,
47                                ntdb_len_t extra)
48 {
49         union ntdb_layout_elem elem;
50         elem.base.type = CAPABILITY;
51         elem.capability.type = type;
52         if (write_breaks)
53                 elem.capability.type |= NTDB_CAP_NOWRITE;
54         if (open_breaks)
55                 elem.capability.type |= NTDB_CAP_NOOPEN;
56         if (check_breaks)
57                 elem.capability.type |= NTDB_CAP_NOCHECK;
58         elem.capability.extra = extra;
59         add(layout, elem);
60 }
61
62 static NTDB_DATA dup_key(NTDB_DATA key)
63 {
64         NTDB_DATA ret;
65         ret.dsize = key.dsize;
66         ret.dptr = malloc(ret.dsize);
67         memcpy(ret.dptr, key.dptr, ret.dsize);
68         return ret;
69 }
70
71 void ntdb_layout_add_used(struct ntdb_layout *layout,
72                          NTDB_DATA key, NTDB_DATA data,
73                          ntdb_len_t extra)
74 {
75         union ntdb_layout_elem elem;
76         elem.base.type = DATA;
77         elem.used.key = dup_key(key);
78         elem.used.data = dup_key(data);
79         elem.used.extra = extra;
80         add(layout, elem);
81 }
82
83 static ntdb_len_t free_record_len(ntdb_len_t len)
84 {
85         return sizeof(struct ntdb_used_record) + len;
86 }
87
88 static ntdb_len_t data_record_len(struct tle_used *used)
89 {
90         ntdb_len_t len;
91         len = sizeof(struct ntdb_used_record)
92                 + used->key.dsize + used->data.dsize + used->extra;
93         assert(len >= sizeof(struct ntdb_free_record));
94         return len;
95 }
96
97 static ntdb_len_t hashtable_len(struct tle_hashtable *htable)
98 {
99         return sizeof(struct ntdb_used_record)
100                 + (sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS)
101                 + htable->extra;
102 }
103
104 static ntdb_len_t capability_len(struct tle_capability *cap)
105 {
106         return sizeof(struct ntdb_capability) + cap->extra;
107 }
108
109 static ntdb_len_t freetable_len(struct tle_freetable *ftable)
110 {
111         return sizeof(struct ntdb_freetable);
112 }
113
114 static void set_free_record(void *mem, ntdb_len_t len)
115 {
116         /* We do all the work in add_to_freetable */
117 }
118
119 static void add_zero_pad(struct ntdb_used_record *u, size_t len, size_t extra)
120 {
121         if (extra)
122                 ((char *)(u + 1))[len] = '\0';
123 }
124
125 static void set_data_record(void *mem, struct ntdb_context *ntdb,
126                             struct tle_used *used)
127 {
128         struct ntdb_used_record *u = mem;
129
130         set_header(ntdb, u, NTDB_USED_MAGIC, used->key.dsize, used->data.dsize,
131                    used->key.dsize + used->data.dsize + used->extra,
132                    ntdb_hash(ntdb, used->key.dptr, used->key.dsize));
133         memcpy(u + 1, used->key.dptr, used->key.dsize);
134         memcpy((char *)(u + 1) + used->key.dsize,
135                used->data.dptr, used->data.dsize);
136         add_zero_pad(u, used->key.dsize + used->data.dsize, used->extra);
137 }
138
139 static void set_hashtable(void *mem, struct ntdb_context *ntdb,
140                           struct tle_hashtable *htable)
141 {
142         struct ntdb_used_record *u = mem;
143         ntdb_len_t len = sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS;
144
145         set_header(ntdb, u, NTDB_HTABLE_MAGIC, 0, len, len + htable->extra, 0);
146         memset(u + 1, 0, len);
147         add_zero_pad(u, len, htable->extra);
148 }
149
150 static void set_capability(void *mem, struct ntdb_context *ntdb,
151                            struct tle_capability *cap, struct ntdb_header *hdr,
152                            ntdb_off_t last_cap)
153 {
154         struct ntdb_capability *c = mem;
155         ntdb_len_t len = sizeof(*c) - sizeof(struct ntdb_used_record) + cap->extra;
156
157         c->type = cap->type;
158         c->next = 0;
159         set_header(ntdb, &c->hdr, NTDB_CAP_MAGIC, 0, len, len, 0);
160
161         /* Append to capability list. */
162         if (!last_cap) {
163                 hdr->capabilities = cap->base.off;
164         } else {
165                 c = (struct ntdb_capability *)((char *)hdr + last_cap);
166                 c->next = cap->base.off;
167         }
168 }
169
170 static void set_freetable(void *mem, struct ntdb_context *ntdb,
171                          struct tle_freetable *freetable, struct ntdb_header *hdr,
172                          ntdb_off_t last_ftable)
173 {
174         struct ntdb_freetable *ftable = mem;
175         memset(ftable, 0, sizeof(*ftable));
176         set_header(ntdb, &ftable->hdr, NTDB_FTABLE_MAGIC, 0,
177                         sizeof(*ftable) - sizeof(ftable->hdr),
178                         sizeof(*ftable) - sizeof(ftable->hdr), 0);
179
180         if (last_ftable) {
181                 ftable = (struct ntdb_freetable *)((char *)hdr + last_ftable);
182                 ftable->next = freetable->base.off;
183         } else {
184                 hdr->free_table = freetable->base.off;
185         }
186 }
187
188 static void add_to_freetable(struct ntdb_context *ntdb,
189                              ntdb_off_t eoff,
190                              ntdb_off_t elen,
191                              unsigned ftable,
192                              struct tle_freetable *freetable)
193 {
194         ntdb->ftable_off = freetable->base.off;
195         ntdb->ftable = ftable;
196         add_free_record(ntdb, eoff, sizeof(struct ntdb_used_record) + elen,
197                         NTDB_LOCK_WAIT, false);
198 }
199
200 static ntdb_off_t hbucket_off(ntdb_off_t group_start, unsigned ingroup)
201 {
202         return group_start
203                 + (ingroup % (1 << NTDB_HASH_GROUP_BITS)) * sizeof(ntdb_off_t);
204 }
205
206 /* Get bits from a value. */
207 static uint32_t bits(uint64_t val, unsigned start, unsigned num)
208 {
209         assert(num <= 32);
210         return (val >> start) & ((1U << num) - 1);
211 }
212
213 /* We take bits from the top: that way we can lock whole sections of the hash
214  * by using lock ranges. */
215 static uint32_t use_bits(uint64_t h, unsigned num, unsigned *used)
216 {
217         *used += num;
218         return bits(h, 64 - *used, num);
219 }
220
221 static ntdb_off_t encode_offset(ntdb_off_t new_off, unsigned bucket,
222                                uint64_t h)
223 {
224         return bucket
225                 | new_off
226                 | ((uint64_t)bits(h, 64 - NTDB_OFF_UPPER_STEAL_EXTRA,
227                                   NTDB_OFF_UPPER_STEAL_EXTRA)
228                    << NTDB_OFF_HASH_EXTRA_BIT);
229 }
230
231 /* FIXME: Our hash table handling here is primitive: we don't expand! */
232 static void add_to_hashtable(struct ntdb_context *ntdb,
233                              ntdb_off_t eoff,
234                              NTDB_DATA key)
235 {
236         uint64_t h = ntdb_hash(ntdb, key.dptr, key.dsize);
237         ntdb_off_t b_off, group_start;
238         unsigned i, group, in_group;
239         unsigned used = 0;
240
241         group = use_bits(h, NTDB_TOPLEVEL_HASH_BITS-NTDB_HASH_GROUP_BITS, &used);
242         in_group = use_bits(h, NTDB_HASH_GROUP_BITS, &used);
243
244         group_start = offsetof(struct ntdb_header, hashtable)
245                 + group * (sizeof(ntdb_off_t) << NTDB_HASH_GROUP_BITS);
246
247         for (i = 0; i < (1 << NTDB_HASH_GROUP_BITS); i++) {
248                 unsigned bucket = (in_group + i) % (1 << NTDB_HASH_GROUP_BITS);
249
250                 b_off = hbucket_off(group_start, bucket);
251                 if (ntdb_read_off(ntdb, b_off) == 0) {
252                         ntdb_write_off(ntdb, b_off,
253                                       encode_offset(eoff, in_group, h));
254                         return;
255                 }
256         }
257         abort();
258 }
259
260 static struct tle_freetable *find_ftable(struct ntdb_layout *layout, unsigned num)
261 {
262         unsigned i;
263
264         for (i = 0; i < layout->num_elems; i++) {
265                 if (layout->elem[i].base.type != FREETABLE)
266                         continue;
267                 if (num == 0)
268                         return &layout->elem[i].ftable;
269                 num--;
270         }
271         abort();
272 }
273
274 /* FIXME: Support NTDB_CONVERT */
275 struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
276                                    void (*freefn)(void *),
277                                    union ntdb_attribute *attr)
278 {
279         unsigned int i;
280         ntdb_off_t off, len, last_ftable, last_cap;
281         char *mem;
282         struct ntdb_context *ntdb;
283
284         off = sizeof(struct ntdb_header);
285
286         /* First pass of layout: calc lengths */
287         for (i = 0; i < layout->num_elems; i++) {
288                 union ntdb_layout_elem *e = &layout->elem[i];
289                 e->base.off = off;
290                 switch (e->base.type) {
291                 case FREETABLE:
292                         len = freetable_len(&e->ftable);
293                         break;
294                 case FREE:
295                         len = free_record_len(e->free.len);
296                         break;
297                 case DATA:
298                         len = data_record_len(&e->used);
299                         break;
300                 case HASHTABLE:
301                         len = hashtable_len(&e->hashtable);
302                         break;
303                 case CAPABILITY:
304                         len = capability_len(&e->capability);
305                         break;
306                 default:
307                         abort();
308                 }
309                 off += len;
310         }
311
312         mem = malloc(off);
313         /* Fill with some weird pattern. */
314         memset(mem, 0x99, off);
315         /* Now populate our header, cribbing from a real NTDB header. */
316         ntdb = ntdb_open(NULL, NTDB_INTERNAL, O_RDWR, 0, attr);
317         memcpy(mem, ntdb->file->map_ptr, sizeof(struct ntdb_header));
318
319         /* Mug the ntdb we have to make it use this. */
320         freefn(ntdb->file->map_ptr);
321         ntdb->file->map_ptr = mem;
322         ntdb->file->map_size = off;
323
324         last_ftable = 0;
325         last_cap = 0;
326         for (i = 0; i < layout->num_elems; i++) {
327                 union ntdb_layout_elem *e = &layout->elem[i];
328                 switch (e->base.type) {
329                 case FREETABLE:
330                         set_freetable(mem + e->base.off, ntdb, &e->ftable,
331                                      (struct ntdb_header *)mem, last_ftable);
332                         last_ftable = e->base.off;
333                         break;
334                 case FREE:
335                         set_free_record(mem + e->base.off, e->free.len);
336                         break;
337                 case DATA:
338                         set_data_record(mem + e->base.off, ntdb, &e->used);
339                         break;
340                 case HASHTABLE:
341                         set_hashtable(mem + e->base.off, ntdb, &e->hashtable);
342                         break;
343                 case CAPABILITY:
344                         set_capability(mem + e->base.off, ntdb, &e->capability,
345                                        (struct ntdb_header *)mem, last_cap);
346                         last_cap = e->base.off;
347                         break;
348                 }
349         }
350         /* Must have a free table! */
351         assert(last_ftable);
352
353         /* Now fill the free and hash tables. */
354         for (i = 0; i < layout->num_elems; i++) {
355                 union ntdb_layout_elem *e = &layout->elem[i];
356                 switch (e->base.type) {
357                 case FREE:
358                         add_to_freetable(ntdb, e->base.off, e->free.len,
359                                          e->free.ftable_num,
360                                          find_ftable(layout, e->free.ftable_num));
361                         break;
362                 case DATA:
363                         add_to_hashtable(ntdb, e->base.off, e->used.key);
364                         break;
365                 default:
366                         break;
367                 }
368         }
369
370         ntdb->ftable_off = find_ftable(layout, 0)->base.off;
371         return ntdb;
372 }
373
374 void ntdb_layout_write(struct ntdb_layout *layout, void (*freefn)(void *),
375                        union ntdb_attribute *attr, const char *filename)
376 {
377         struct ntdb_context *ntdb = ntdb_layout_get(layout, freefn, attr);
378         int fd;
379
380         fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT,  0600);
381         if (fd < 0)
382                 err(1, "opening %s for writing", filename);
383         if (write(fd, ntdb->file->map_ptr, ntdb->file->map_size)
384             != ntdb->file->map_size)
385                 err(1, "writing %s", filename);
386         close(fd);
387         ntdb_close(ntdb);
388 }
389
390 void ntdb_layout_free(struct ntdb_layout *layout)
391 {
392         unsigned int i;
393
394         for (i = 0; i < layout->num_elems; i++) {
395                 if (layout->elem[i].base.type == DATA) {
396                         free(layout->elem[i].used.key.dptr);
397                         free(layout->elem[i].used.data.dptr);
398                 }
399         }
400         free(layout->elem);
401         free(layout);
402 }