]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_open.c
tdb2: unify tdb1_parse_record into tdb_parse_record
[ccan] / ccan / tdb2 / tdb1_open.c
1  /*
2    Unix SMB/CIFS implementation.
3
4    trivial database library
5
6    Copyright (C) Andrew Tridgell              1999-2005
7    Copyright (C) Paul `Rusty' Russell              2000
8    Copyright (C) Jeremy Allison                    2000-2003
9
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27 #include <assert.h>
28 #include "tdb1_private.h"
29 #include <assert.h>
30
31 /* We use two hashes to double-check they're using the right hash function. */
32 void tdb1_header_hash(struct tdb_context *tdb,
33                      uint32_t *magic1_hash, uint32_t *magic2_hash)
34 {
35         uint32_t tdb1_magic = TDB1_MAGIC;
36
37         *magic1_hash = tdb_hash(tdb, TDB_MAGIC_FOOD, sizeof(TDB_MAGIC_FOOD));
38         *magic2_hash = tdb_hash(tdb, TDB1_CONV(tdb1_magic), sizeof(tdb1_magic));
39
40         /* Make sure at least one hash is non-zero! */
41         if (*magic1_hash == 0 && *magic2_hash == 0)
42                 *magic1_hash = 1;
43 }
44
45 static void tdb_context_init(struct tdb_context *tdb)
46 {
47         assert(tdb->flags & TDB_VERSION1);
48
49         tdb1_io_init(tdb);
50
51         tdb->tdb1.traverse_read = tdb->tdb1.traverse_write = 0;
52         memset(&tdb->tdb1.travlocks, 0, sizeof(tdb->tdb1.travlocks));
53         tdb->tdb1.transaction = NULL;
54
55         /* cache the page size */
56         tdb->tdb1.page_size = getpagesize();
57         if (tdb->tdb1.page_size <= 0) {
58                 tdb->tdb1.page_size = 0x2000;
59         }
60
61         /* FIXME: Used to be 5 for TDB_VOLATILE. */
62         tdb->tdb1.max_dead_records = 0;
63 }
64
65 /* initialise a new database */
66 enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb,
67                                  struct tdb_attribute_tdb1_hashsize *hashsize)
68 {
69         struct tdb1_header *newdb;
70         size_t size;
71         int hash_size = TDB1_DEFAULT_HASH_SIZE;
72         enum TDB_ERROR ret = TDB_ERR_IO;
73
74         tdb_context_init(tdb);
75
76         /* Default TDB2 hash becomes default TDB1 hash. */
77         if (tdb->hash_fn == tdb_jenkins_hash)
78                 tdb->hash_fn = tdb1_old_hash;
79
80         if (hashsize)
81                 hash_size = hashsize->hsize;
82
83         /* We make it up in memory, then write it out if not internal */
84         size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t);
85         if (!(newdb = (struct tdb1_header *)calloc(size, 1))) {
86                 return TDB_ERR_OOM;
87         }
88
89         /* Fill in the header */
90         newdb->version = TDB1_VERSION;
91         newdb->hash_size = hash_size;
92
93         tdb1_header_hash(tdb, &newdb->magic1_hash, &newdb->magic2_hash);
94
95         /* Make sure older tdbs (which don't check the magic hash fields)
96          * will refuse to open this TDB. */
97         if (tdb->hash_fn == tdb1_incompatible_hash)
98                 newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC;
99
100         memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
101         /* This creates an endian-converted db. */
102         TDB1_CONV(*newdb);
103         /* Don't endian-convert the magic food! */
104         memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
105
106         if (tdb->flags & TDB_INTERNAL) {
107                 tdb->file->map_size = size;
108                 tdb->file->map_ptr = (char *)newdb;
109                 return TDB_SUCCESS;
110         }
111         if (lseek(tdb->file->fd, 0, SEEK_SET) == -1)
112                 goto fail;
113
114         if (ftruncate(tdb->file->fd, 0) == -1)
115                 goto fail;
116
117         /* we still have "ret == TDB_ERR_IO" here */
118         if (tdb1_write_all(tdb->file->fd, newdb, size))
119                 ret = TDB_SUCCESS;
120
121   fail:
122         SAFE_FREE(newdb);
123         return ret;
124 }
125
126 typedef void (*tdb1_log_func)(struct tdb_context *, enum tdb_log_level, enum TDB_ERROR,
127                               const char *, void *);
128 typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
129                                    void *data);
130
131 struct tdb1_logging_context {
132         tdb1_log_func log_fn;
133         void *log_private;
134 };
135
136 static bool hash_correct(struct tdb_context *tdb,
137                          uint32_t *m1, uint32_t *m2)
138 {
139         /* older TDB without magic hash references */
140         if (tdb->tdb1.header.magic1_hash == 0
141             && tdb->tdb1.header.magic2_hash == 0) {
142                 return true;
143         }
144
145         tdb1_header_hash(tdb, m1, m2);
146         return (tdb->tdb1.header.magic1_hash == *m1 &&
147                 tdb->tdb1.header.magic2_hash == *m2);
148 }
149
150 static bool check_header_hash(struct tdb_context *tdb,
151                               uint32_t *m1, uint32_t *m2)
152 {
153         if (hash_correct(tdb, m1, m2))
154                 return true;
155
156         /* If they use one inbuilt, try the other inbuilt hash. */
157         if (tdb->hash_fn == tdb1_old_hash)
158                 tdb->hash_fn = tdb1_incompatible_hash;
159         else if (tdb->hash_fn == tdb1_incompatible_hash)
160                 tdb->hash_fn = tdb1_old_hash;
161         else
162                 return false;
163         return hash_correct(tdb, m1, m2);
164 }
165
166 /* We are hold the TDB open lock on tdb->fd. */
167 enum TDB_ERROR tdb1_open(struct tdb_context *tdb)
168 {
169         const char *hash_alg;
170         uint32_t magic1, magic2;
171
172         tdb->flags |= TDB_VERSION1;
173
174         tdb_context_init(tdb);
175
176         /* Default TDB2 hash becomes default TDB1 hash. */
177         if (tdb->hash_fn == tdb_jenkins_hash) {
178                 tdb->hash_fn = tdb1_old_hash;
179                 hash_alg = "default";
180         } else if (tdb->hash_fn == tdb1_incompatible_hash)
181                 hash_alg = "tdb1_incompatible_hash";
182         else
183                 hash_alg = "the user defined";
184
185         if (tdb->tdb1.header.version != TDB1_BYTEREV(TDB1_VERSION)) {
186                 if (tdb->flags & TDB_CONVERT) {
187                         return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
188                                           "tdb1_open:"
189                                           " %s does not need TDB_CONVERT",
190                                           tdb->name);
191                 }
192         } else {
193                 tdb->flags |= TDB_CONVERT;
194                 tdb1_convert(&tdb->tdb1.header, sizeof(tdb->tdb1.header));
195         }
196
197         if (tdb->tdb1.header.rwlocks != 0 &&
198             tdb->tdb1.header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
199                 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
200                                   "tdb1_open: spinlocks no longer supported");
201         }
202
203         if (!check_header_hash(tdb, &magic1, &magic2)) {
204                 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_USE_ERROR,
205                            "tdb1_open: "
206                            "%s was not created with %s hash function we are using\n"
207                            "magic1_hash[0x%08X %s 0x%08X] "
208                            "magic2_hash[0x%08X %s 0x%08X]",
209                            tdb->name, hash_alg,
210                            tdb->tdb1.header.magic1_hash,
211                            (tdb->tdb1.header.magic1_hash == magic1) ? "==" : "!=",
212                            magic1,
213                            tdb->tdb1.header.magic2_hash,
214                            (tdb->tdb1.header.magic2_hash == magic2) ? "==" : "!=",
215                            magic2);
216         }
217         return TDB_SUCCESS;
218 }
219
220 /*
221  * Set the maximum number of dead records per hash chain
222  */
223
224 void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead)
225 {
226         tdb->tdb1.max_dead_records = max_dead;
227 }