]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_open.c
5d0097b2a79ac2de54875414cf8c0485d3d7357e
[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
28 #include "tdb1_private.h"
29
30 /* We use two hashes to double-check they're using the right hash function. */
31 void tdb1_header_hash(struct tdb_context *tdb,
32                      uint32_t *magic1_hash, uint32_t *magic2_hash)
33 {
34         uint32_t tdb1_magic = TDB1_MAGIC;
35
36         *magic1_hash = tdb_hash(tdb, TDB_MAGIC_FOOD, sizeof(TDB_MAGIC_FOOD));
37         *magic2_hash = tdb_hash(tdb, TDB1_CONV(tdb1_magic), sizeof(tdb1_magic));
38
39         /* Make sure at least one hash is non-zero! */
40         if (*magic1_hash == 0 && *magic2_hash == 0)
41                 *magic1_hash = 1;
42 }
43
44 /* initialise a new database with a specified hash size */
45 static int tdb1_new_database(struct tdb_context *tdb, int hash_size)
46 {
47         struct tdb1_header *newdb;
48         size_t size;
49         int ret = -1;
50
51         /* We make it up in memory, then write it out if not internal */
52         size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t);
53         if (!(newdb = (struct tdb1_header *)calloc(size, 1))) {
54                 tdb->last_error = TDB_ERR_OOM;
55                 return -1;
56         }
57
58         /* Fill in the header */
59         newdb->version = TDB1_VERSION;
60         newdb->hash_size = hash_size;
61
62         tdb1_header_hash(tdb, &newdb->magic1_hash, &newdb->magic2_hash);
63
64         /* Make sure older tdbs (which don't check the magic hash fields)
65          * will refuse to open this TDB. */
66         if (tdb->hash_fn == tdb1_incompatible_hash)
67                 newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC;
68
69         if (tdb->flags & TDB_INTERNAL) {
70                 tdb->file->fd = -1;
71                 tdb->file->map_size = size;
72                 tdb->file->map_ptr = (char *)newdb;
73                 memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
74                 /* Convert the `ondisk' version if asked. */
75                 TDB1_CONV(*newdb);
76                 return 0;
77         }
78         if (lseek(tdb->file->fd, 0, SEEK_SET) == -1)
79                 goto fail;
80
81         if (ftruncate(tdb->file->fd, 0) == -1)
82                 goto fail;
83
84         /* This creates an endian-converted header, as if read from disk */
85         TDB1_CONV(*newdb);
86         memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
87         /* Don't endian-convert the magic food! */
88         memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
89         /* we still have "ret == -1" here */
90         if (tdb1_write_all(tdb->file->fd, newdb, size))
91                 ret = 0;
92
93   fail:
94         SAFE_FREE(newdb);
95         return ret;
96 }
97
98
99
100 /* open the database, creating it if necessary
101
102    The open_flags and mode are passed straight to the open call on the
103    database file. A flags value of O_WRONLY is invalid. The hash size
104    is advisory, use zero for a default value.
105
106    Return is NULL on error, in which case errno is also set.  Don't
107    try to call tdb1_error or tdb1_errname, just do strerror(errno).
108
109    @param name may be NULL for internal databases. */
110 struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
111                       int open_flags, mode_t mode)
112 {
113         return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode, NULL, NULL);
114 }
115
116 static bool hash_correct(struct tdb_context *tdb,
117                          uint32_t *m1, uint32_t *m2)
118 {
119         tdb1_header_hash(tdb, m1, m2);
120         return (tdb->tdb1.header.magic1_hash == *m1 &&
121                 tdb->tdb1.header.magic2_hash == *m2);
122 }
123
124 static bool check_header_hash(struct tdb_context *tdb,
125                               uint32_t *m1, uint32_t *m2)
126 {
127         if (hash_correct(tdb, m1, m2))
128                 return true;
129
130         /* If they use one inbuilt, try the other inbuilt hash. */
131         if (tdb->hash_fn == tdb1_old_hash)
132                 tdb->hash_fn = tdb1_incompatible_hash;
133         else if (tdb->hash_fn == tdb1_incompatible_hash)
134                 tdb->hash_fn = tdb1_old_hash;
135         else
136                 return false;
137         return hash_correct(tdb, m1, m2);
138 }
139
140 struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
141                                 int open_flags, mode_t mode,
142                                 const struct tdb1_logging_context *log_ctx,
143                                 tdb1_hash_func hash_fn)
144 {
145         struct tdb_context *tdb;
146         struct stat st;
147         int rev = 0;
148         unsigned v;
149         const char *hash_alg;
150         uint32_t magic1, magic2;
151
152         if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
153                 /* Can't log this */
154                 errno = ENOMEM;
155                 goto fail;
156         }
157         tdb->file = calloc(1, sizeof *tdb->file);
158         if (!tdb->file) {
159                 free(tdb);
160                 errno = ENOMEM;
161                 goto fail;
162         }
163         tdb1_io_init(tdb);
164         tdb->file->fd = -1;
165         tdb->name = NULL;
166         tdb->file->map_ptr = NULL;
167         tdb->flags = tdb1_flags|TDB_VERSION1;
168         tdb->open_flags = open_flags;
169         tdb->lock_fn = tdb_fcntl_lock;
170         tdb->unlock_fn = tdb_fcntl_unlock;
171         if (log_ctx) {
172                 tdb->log_fn = log_ctx->log_fn;
173                 tdb->log_data = log_ctx->log_private;
174         } else
175                 tdb->log_fn = NULL;
176
177         if (name == NULL && (tdb1_flags & TDB_INTERNAL)) {
178                 name = "__TDB1_INTERNAL__";
179         }
180
181         if (name == NULL) {
182                 tdb->name = (char *)"__NULL__";
183                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
184                            "tdb1_open_ex: called with name == NULL");
185                 tdb->name = NULL;
186                 errno = EINVAL;
187                 goto fail;
188         }
189
190         /* now make a copy of the name, as the caller memory might went away */
191         if (!(tdb->name = (char *)strdup(name))) {
192                 /*
193                  * set the name as the given string, so that tdb1_name() will
194                  * work in case of an error.
195                  */
196                 tdb->name = (char *)name;
197                 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
198                            "tdb1_open_ex: can't strdup(%s)", name);
199                 tdb->name = NULL;
200                 errno = ENOMEM;
201                 goto fail;
202         }
203         tdb->hash_seed = 0;
204
205         if (hash_fn) {
206                 tdb->hash_fn = hash_fn;
207                 if (hash_fn == tdb1_incompatible_hash)
208                         hash_alg = "tdb1_incompatible_hash";
209                 else
210                         hash_alg = "the user defined";
211         } else {
212                 tdb->hash_fn = tdb1_old_hash;
213                 hash_alg = "default";
214         }
215
216         /* cache the page size */
217         tdb->tdb1.page_size = getpagesize();
218         if (tdb->tdb1.page_size <= 0) {
219                 tdb->tdb1.page_size = 0x2000;
220         }
221
222         /* FIXME: Used to be 5 for TDB_VOLATILE. */
223         tdb->tdb1.max_dead_records = 0;
224
225         if ((open_flags & O_ACCMODE) == O_WRONLY) {
226                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
227                            "tdb1_open_ex: can't open tdb %s write-only",
228                            name);
229                 errno = EINVAL;
230                 goto fail;
231         }
232
233         if (hash_size == 0)
234                 hash_size = TDB1_DEFAULT_HASH_SIZE;
235         if ((open_flags & O_ACCMODE) == O_RDONLY) {
236                 tdb->flags |= TDB_RDONLY;
237                 /* read only databases don't do locking */
238                 tdb->flags |= TDB_NOLOCK;
239         }
240
241         /* internal databases don't mmap or lock, and start off cleared */
242         if (tdb->flags & TDB_INTERNAL) {
243                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
244                 if (tdb1_new_database(tdb, hash_size) != 0) {
245                         tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
246                                    "tdb1_open_ex: tdb1_new_database failed!");
247                         goto fail;
248                 }
249                 goto internal;
250         }
251
252         if ((tdb->file->fd = open(name, open_flags, mode)) == -1) {
253                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
254                            "tdb1_open_ex: could not open file %s: %s",
255                            name, strerror(errno));
256                 goto fail;      /* errno set by open(2) */
257         }
258
259         /* on exec, don't inherit the fd */
260         v = fcntl(tdb->file->fd, F_GETFD, 0);
261         fcntl(tdb->file->fd, F_SETFD, v | FD_CLOEXEC);
262
263         /* ensure there is only one process initialising at once */
264         if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT) == -1) {
265                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
266                            "tdb1_open_ex: failed to get open lock on %s: %s",
267                            name, strerror(errno));
268                 goto fail;      /* errno set by tdb1_brlock */
269         }
270
271         errno = 0;
272         if (read(tdb->file->fd, &tdb->tdb1.header, sizeof(tdb->tdb1.header)) != sizeof(tdb->tdb1.header)
273             || strcmp(tdb->tdb1.header.magic_food, TDB_MAGIC_FOOD) != 0) {
274                 if (!(open_flags & O_CREAT) || tdb1_new_database(tdb, hash_size) == -1) {
275                         if (errno == 0) {
276                                 errno = EIO; /* ie bad format or something */
277                         }
278                         goto fail;
279                 }
280                 rev = (tdb->flags & TDB_CONVERT);
281         } else if (tdb->tdb1.header.version != TDB1_VERSION
282                    && !(rev = (tdb->tdb1.header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
283                 /* wrong version */
284                 errno = EIO;
285                 goto fail;
286         }
287         if (!rev)
288                 tdb->flags &= ~TDB_CONVERT;
289         else {
290                 tdb->flags |= TDB_CONVERT;
291                 tdb1_convert(&tdb->tdb1.header, sizeof(tdb->tdb1.header));
292         }
293         if (fstat(tdb->file->fd, &st) == -1)
294                 goto fail;
295
296         if (tdb->tdb1.header.rwlocks != 0 &&
297             tdb->tdb1.header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
298                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
299                            "tdb1_open_ex: spinlocks no longer supported");
300                 goto fail;
301         }
302
303         if ((tdb->tdb1.header.magic1_hash == 0) && (tdb->tdb1.header.magic2_hash == 0)) {
304                 /* older TDB without magic hash references */
305                 tdb->hash_fn = tdb1_old_hash;
306         } else if (!check_header_hash(tdb, &magic1, &magic2)) {
307                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_USE_ERROR,
308                            "tdb1_open_ex: "
309                            "%s was not created with %s hash function we are using\n"
310                            "magic1_hash[0x%08X %s 0x%08X] "
311                            "magic2_hash[0x%08X %s 0x%08X]",
312                            name, hash_alg,
313                            tdb->tdb1.header.magic1_hash,
314                            (tdb->tdb1.header.magic1_hash == magic1) ? "==" : "!=",
315                            magic1,
316                            tdb->tdb1.header.magic2_hash,
317                            (tdb->tdb1.header.magic2_hash == magic2) ? "==" : "!=",
318                            magic2);
319                 errno = EINVAL;
320                 goto fail;
321         }
322
323         tdb->file->map_size = st.st_size;
324         tdb->file->device = st.st_dev;
325         tdb->file->inode = st.st_ino;
326         tdb1_mmap(tdb);
327
328         /* if needed, run recovery */
329         if (tdb1_transaction_recover(tdb) == -1) {
330                 goto fail;
331         }
332
333  internal:
334         /* Internal (memory-only) databases skip all the code above to
335          * do with disk files, and resume here by releasing their
336          * open lock and hooking into the active list. */
337         if (tdb1_nest_unlock(tdb, TDB1_OPEN_LOCK, F_WRLCK) == -1) {
338                 goto fail;
339         }
340         return tdb;
341
342  fail:
343         { int save_errno = errno;
344
345         if (!tdb)
346                 return NULL;
347
348         if (tdb->file->map_ptr) {
349                 if (tdb->flags & TDB_INTERNAL)
350                         SAFE_FREE(tdb->file->map_ptr);
351                 else
352                         tdb1_munmap(tdb);
353         }
354         if (tdb->file->fd != -1)
355                 if (close(tdb->file->fd) != 0)
356                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
357                                    "tdb1_open_ex: failed to close tdb->fd on error!");
358         if (tdb->file) {
359                 SAFE_FREE(tdb->file->lockrecs);
360                 SAFE_FREE(tdb->file);
361         }
362         SAFE_FREE(tdb->name);
363         SAFE_FREE(tdb);
364         errno = save_errno;
365         return NULL;
366         }
367 }
368
369 /*
370  * Set the maximum number of dead records per hash chain
371  */
372
373 void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead)
374 {
375         tdb->tdb1.max_dead_records = max_dead;
376 }
377
378 /**
379  * Close a database.
380  *
381  * @returns -1 for error; 0 for success.
382  **/
383 int tdb1_close(struct tdb_context *tdb)
384 {
385         int ret = 0;
386
387         if (tdb->tdb1.transaction) {
388                 tdb1_transaction_cancel(tdb);
389         }
390
391         if (tdb->file->map_ptr) {
392                 if (tdb->flags & TDB_INTERNAL)
393                         SAFE_FREE(tdb->file->map_ptr);
394                 else
395                         tdb1_munmap(tdb);
396         }
397         SAFE_FREE(tdb->name);
398         if (tdb->file->fd != -1) {
399                 ret = close(tdb->file->fd);
400                 tdb->file->fd = -1;
401         }
402         SAFE_FREE(tdb->file->lockrecs);
403         SAFE_FREE(tdb->file);
404
405         memset(tdb, 0, sizeof(*tdb));
406         SAFE_FREE(tdb);
407
408         return ret;
409 }