]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_open.c
tdb2: Make tdb1 share tdb_store flags, struct tdb_data and TDB_MAGIC_FOOD.
[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 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
31 static struct tdb1_context *tdb1s = NULL;
32
33 /* We use two hashes to double-check they're using the right hash function. */
34 void tdb1_header_hash(struct tdb1_context *tdb,
35                      uint32_t *magic1_hash, uint32_t *magic2_hash)
36 {
37         TDB_DATA hash_key;
38         uint32_t tdb1_magic = TDB1_MAGIC;
39
40         hash_key.dptr = (unsigned char *)TDB_MAGIC_FOOD;
41         hash_key.dsize = sizeof(TDB_MAGIC_FOOD);
42         *magic1_hash = tdb->hash_fn(&hash_key);
43
44         hash_key.dptr = (unsigned char *)TDB1_CONV(tdb1_magic);
45         hash_key.dsize = sizeof(tdb1_magic);
46         *magic2_hash = tdb->hash_fn(&hash_key);
47
48         /* Make sure at least one hash is non-zero! */
49         if (*magic1_hash == 0 && *magic2_hash == 0)
50                 *magic1_hash = 1;
51 }
52
53 /* initialise a new database with a specified hash size */
54 static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
55 {
56         struct tdb1_header *newdb;
57         size_t size;
58         int ret = -1;
59
60         /* We make it up in memory, then write it out if not internal */
61         size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t);
62         if (!(newdb = (struct tdb1_header *)calloc(size, 1))) {
63                 tdb->last_error = TDB_ERR_OOM;
64                 return -1;
65         }
66
67         /* Fill in the header */
68         newdb->version = TDB1_VERSION;
69         newdb->hash_size = hash_size;
70
71         tdb1_header_hash(tdb, &newdb->magic1_hash, &newdb->magic2_hash);
72
73         /* Make sure older tdbs (which don't check the magic hash fields)
74          * will refuse to open this TDB. */
75         if (tdb->flags & TDB1_INCOMPATIBLE_HASH)
76                 newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC;
77
78         if (tdb->flags & TDB1_INTERNAL) {
79                 tdb->map_size = size;
80                 tdb->map_ptr = (char *)newdb;
81                 memcpy(&tdb->header, newdb, sizeof(tdb->header));
82                 /* Convert the `ondisk' version if asked. */
83                 TDB1_CONV(*newdb);
84                 return 0;
85         }
86         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
87                 goto fail;
88
89         if (ftruncate(tdb->fd, 0) == -1)
90                 goto fail;
91
92         /* This creates an endian-converted header, as if read from disk */
93         TDB1_CONV(*newdb);
94         memcpy(&tdb->header, newdb, sizeof(tdb->header));
95         /* Don't endian-convert the magic food! */
96         memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
97         /* we still have "ret == -1" here */
98         if (tdb1_write_all(tdb->fd, newdb, size))
99                 ret = 0;
100
101   fail:
102         SAFE_FREE(newdb);
103         return ret;
104 }
105
106
107
108 static int tdb1_already_open(dev_t device,
109                             ino_t ino)
110 {
111         struct tdb1_context *i;
112
113         for (i = tdb1s; i; i = i->next) {
114                 if (i->device == device && i->inode == ino) {
115                         return 1;
116                 }
117         }
118
119         return 0;
120 }
121
122 /* open the database, creating it if necessary
123
124    The open_flags and mode are passed straight to the open call on the
125    database file. A flags value of O_WRONLY is invalid. The hash size
126    is advisory, use zero for a default value.
127
128    Return is NULL on error, in which case errno is also set.  Don't
129    try to call tdb1_error or tdb1_errname, just do strerror(errno).
130
131    @param name may be NULL for internal databases. */
132 struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
133                       int open_flags, mode_t mode)
134 {
135         return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode, NULL, NULL);
136 }
137
138 static bool check_header_hash(struct tdb1_context *tdb,
139                               bool default_hash, uint32_t *m1, uint32_t *m2)
140 {
141         tdb1_header_hash(tdb, m1, m2);
142         if (tdb->header.magic1_hash == *m1 &&
143             tdb->header.magic2_hash == *m2) {
144                 return true;
145         }
146
147         /* If they explicitly set a hash, always respect it. */
148         if (!default_hash)
149                 return false;
150
151         /* Otherwise, try the other inbuilt hash. */
152         if (tdb->hash_fn == tdb1_old_hash)
153                 tdb->hash_fn = tdb1_jenkins_hash;
154         else
155                 tdb->hash_fn = tdb1_old_hash;
156         return check_header_hash(tdb, false, m1, m2);
157 }
158
159 struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
160                                 int open_flags, mode_t mode,
161                                 const struct tdb1_logging_context *log_ctx,
162                                 tdb1_hash_func hash_fn)
163 {
164         struct tdb1_context *tdb;
165         struct stat st;
166         int rev = 0, locked = 0;
167         unsigned char *vp;
168         uint32_t vertest;
169         unsigned v;
170         const char *hash_alg;
171         uint32_t magic1, magic2;
172
173         if (!(tdb = (struct tdb1_context *)calloc(1, sizeof *tdb))) {
174                 /* Can't log this */
175                 errno = ENOMEM;
176                 goto fail;
177         }
178         tdb1_io_init(tdb);
179         tdb->fd = -1;
180         tdb->name = NULL;
181         tdb->map_ptr = NULL;
182         tdb->flags = tdb1_flags;
183         tdb->open_flags = open_flags;
184         if (log_ctx) {
185                 tdb->log_fn = log_ctx->log_fn;
186                 tdb->log_data = log_ctx->log_private;
187         } else
188                 tdb->log_fn = NULL;
189
190         if (name == NULL && (tdb1_flags & TDB1_INTERNAL)) {
191                 name = "__TDB1_INTERNAL__";
192         }
193
194         if (name == NULL) {
195                 tdb->name = (char *)"__NULL__";
196                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
197                            "tdb1_open_ex: called with name == NULL");
198                 tdb->name = NULL;
199                 errno = EINVAL;
200                 goto fail;
201         }
202
203         /* now make a copy of the name, as the caller memory might went away */
204         if (!(tdb->name = (char *)strdup(name))) {
205                 /*
206                  * set the name as the given string, so that tdb1_name() will
207                  * work in case of an error.
208                  */
209                 tdb->name = (char *)name;
210                 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
211                            "tdb1_open_ex: can't strdup(%s)", name);
212                 tdb->name = NULL;
213                 errno = ENOMEM;
214                 goto fail;
215         }
216
217         if (hash_fn) {
218                 tdb->hash_fn = hash_fn;
219                 hash_alg = "the user defined";
220         } else {
221                 /* This controls what we use when creating a tdb. */
222                 if (tdb->flags & TDB1_INCOMPATIBLE_HASH) {
223                         tdb->hash_fn = tdb1_jenkins_hash;
224                 } else {
225                         tdb->hash_fn = tdb1_old_hash;
226                 }
227                 hash_alg = "either default";
228         }
229
230         /* cache the page size */
231         tdb->page_size = getpagesize();
232         if (tdb->page_size <= 0) {
233                 tdb->page_size = 0x2000;
234         }
235
236         tdb->max_dead_records = (tdb1_flags & TDB1_VOLATILE) ? 5 : 0;
237
238         if ((open_flags & O_ACCMODE) == O_WRONLY) {
239                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
240                            "tdb1_open_ex: can't open tdb %s write-only",
241                            name);
242                 errno = EINVAL;
243                 goto fail;
244         }
245
246         if (hash_size == 0)
247                 hash_size = TDB1_DEFAULT_HASH_SIZE;
248         if ((open_flags & O_ACCMODE) == O_RDONLY) {
249                 tdb->read_only = 1;
250                 /* read only databases don't do locking or clear if first */
251                 tdb->flags |= TDB1_NOLOCK;
252                 tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
253         }
254
255         if ((tdb->flags & TDB1_ALLOW_NESTING) &&
256             (tdb->flags & TDB1_DISALLOW_NESTING)) {
257                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
258                            "tdb1_open_ex: "
259                            "allow_nesting and disallow_nesting are not allowed together!");
260                 errno = EINVAL;
261                 goto fail;
262         }
263
264         /*
265          * TDB1_ALLOW_NESTING is the default behavior.
266          * Note: this may change in future versions!
267          */
268         if (!(tdb->flags & TDB1_DISALLOW_NESTING)) {
269                 tdb->flags |= TDB1_ALLOW_NESTING;
270         }
271
272         /* internal databases don't mmap or lock, and start off cleared */
273         if (tdb->flags & TDB1_INTERNAL) {
274                 tdb->flags |= (TDB1_NOLOCK | TDB1_NOMMAP);
275                 tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
276                 if (tdb1_new_database(tdb, hash_size) != 0) {
277                         tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
278                                    "tdb1_open_ex: tdb1_new_database failed!");
279                         goto fail;
280                 }
281                 goto internal;
282         }
283
284         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
285                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
286                            "tdb1_open_ex: could not open file %s: %s",
287                            name, strerror(errno));
288                 goto fail;      /* errno set by open(2) */
289         }
290
291         /* on exec, don't inherit the fd */
292         v = fcntl(tdb->fd, F_GETFD, 0);
293         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
294
295         /* ensure there is only one process initialising at once */
296         if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT) == -1) {
297                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
298                            "tdb1_open_ex: failed to get open lock on %s: %s",
299                            name, strerror(errno));
300                 goto fail;      /* errno set by tdb1_brlock */
301         }
302
303         /* we need to zero database if we are the only one with it open */
304         if ((tdb1_flags & TDB1_CLEAR_IF_FIRST) &&
305             (!tdb->read_only) &&
306             (locked = (tdb1_nest_lock(tdb, TDB1_ACTIVE_LOCK, F_WRLCK, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE) == 0))) {
307                 open_flags |= O_CREAT;
308                 if (ftruncate(tdb->fd, 0) == -1) {
309                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
310                                    "tdb1_open_ex: "
311                                    "failed to truncate %s: %s",
312                                    name, strerror(errno));
313                         goto fail; /* errno set by ftruncate */
314                 }
315         }
316
317         errno = 0;
318         if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
319             || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
320                 if (!(open_flags & O_CREAT) || tdb1_new_database(tdb, hash_size) == -1) {
321                         if (errno == 0) {
322                                 errno = EIO; /* ie bad format or something */
323                         }
324                         goto fail;
325                 }
326                 rev = (tdb->flags & TDB1_CONVERT);
327         } else if (tdb->header.version != TDB1_VERSION
328                    && !(rev = (tdb->header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
329                 /* wrong version */
330                 errno = EIO;
331                 goto fail;
332         }
333         vp = (unsigned char *)&tdb->header.version;
334         vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
335                   (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3];
336         tdb->flags |= (vertest==TDB1_VERSION) ? TDB1_BIGENDIAN : 0;
337         if (!rev)
338                 tdb->flags &= ~TDB1_CONVERT;
339         else {
340                 tdb->flags |= TDB1_CONVERT;
341                 tdb1_convert(&tdb->header, sizeof(tdb->header));
342         }
343         if (fstat(tdb->fd, &st) == -1)
344                 goto fail;
345
346         if (tdb->header.rwlocks != 0 &&
347             tdb->header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
348                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
349                            "tdb1_open_ex: spinlocks no longer supported");
350                 goto fail;
351         }
352
353         if ((tdb->header.magic1_hash == 0) && (tdb->header.magic2_hash == 0)) {
354                 /* older TDB without magic hash references */
355                 tdb->hash_fn = tdb1_old_hash;
356         } else if (!check_header_hash(tdb, !hash_fn, &magic1, &magic2)) {
357                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_USE_ERROR,
358                            "tdb1_open_ex: "
359                            "%s was not created with %s hash function we are using\n"
360                            "magic1_hash[0x%08X %s 0x%08X] "
361                            "magic2_hash[0x%08X %s 0x%08X]",
362                            name, hash_alg,
363                            tdb->header.magic1_hash,
364                            (tdb->header.magic1_hash == magic1) ? "==" : "!=",
365                            magic1,
366                            tdb->header.magic2_hash,
367                            (tdb->header.magic2_hash == magic2) ? "==" : "!=",
368                            magic2);
369                 errno = EINVAL;
370                 goto fail;
371         }
372
373         /* Is it already in the open list?  If so, fail. */
374         if (tdb1_already_open(st.st_dev, st.st_ino)) {
375                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
376                            "tdb1_open_ex: "
377                            "%s (%d,%d) is already open in this process",
378                            name, (int)st.st_dev, (int)st.st_ino);
379                 errno = EBUSY;
380                 goto fail;
381         }
382
383         tdb->map_size = st.st_size;
384         tdb->device = st.st_dev;
385         tdb->inode = st.st_ino;
386         tdb1_mmap(tdb);
387         if (locked) {
388                 if (tdb1_nest_unlock(tdb, TDB1_ACTIVE_LOCK, F_WRLCK) == -1) {
389                         tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
390                                    "tdb1_open_ex: "
391                                    "failed to release ACTIVE_LOCK on %s: %s",
392                                    name, strerror(errno));
393                         goto fail;
394                 }
395
396         }
397
398         /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
399            we didn't get the initial exclusive lock as we need to let all other
400            users know we're using it. */
401
402         if (tdb1_flags & TDB1_CLEAR_IF_FIRST) {
403                 /* leave this lock in place to indicate it's in use */
404                 if (tdb1_nest_lock(tdb, TDB1_ACTIVE_LOCK, F_RDLCK, TDB_LOCK_WAIT) == -1) {
405                         goto fail;
406                 }
407         }
408
409         /* if needed, run recovery */
410         if (tdb1_transaction_recover(tdb) == -1) {
411                 goto fail;
412         }
413
414  internal:
415         /* Internal (memory-only) databases skip all the code above to
416          * do with disk files, and resume here by releasing their
417          * open lock and hooking into the active list. */
418         if (tdb1_nest_unlock(tdb, TDB1_OPEN_LOCK, F_WRLCK) == -1) {
419                 goto fail;
420         }
421         tdb->next = tdb1s;
422         tdb1s = tdb;
423         return tdb;
424
425  fail:
426         { int save_errno = errno;
427
428         if (!tdb)
429                 return NULL;
430
431         if (tdb->map_ptr) {
432                 if (tdb->flags & TDB1_INTERNAL)
433                         SAFE_FREE(tdb->map_ptr);
434                 else
435                         tdb1_munmap(tdb);
436         }
437         if (tdb->fd != -1)
438                 if (close(tdb->fd) != 0)
439                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
440                                    "tdb1_open_ex: failed to close tdb->fd on error!");
441         SAFE_FREE(tdb->lockrecs);
442         SAFE_FREE(tdb->name);
443         SAFE_FREE(tdb);
444         errno = save_errno;
445         return NULL;
446         }
447 }
448
449 /*
450  * Set the maximum number of dead records per hash chain
451  */
452
453 void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead)
454 {
455         tdb->max_dead_records = max_dead;
456 }
457
458 /**
459  * Close a database.
460  *
461  * @returns -1 for error; 0 for success.
462  **/
463 int tdb1_close(struct tdb1_context *tdb)
464 {
465         struct tdb1_context **i;
466         int ret = 0;
467
468         if (tdb->transaction) {
469                 tdb1_transaction_cancel(tdb);
470         }
471
472         if (tdb->map_ptr) {
473                 if (tdb->flags & TDB1_INTERNAL)
474                         SAFE_FREE(tdb->map_ptr);
475                 else
476                         tdb1_munmap(tdb);
477         }
478         SAFE_FREE(tdb->name);
479         if (tdb->fd != -1) {
480                 ret = close(tdb->fd);
481                 tdb->fd = -1;
482         }
483         SAFE_FREE(tdb->lockrecs);
484
485         /* Remove from contexts list */
486         for (i = &tdb1s; *i; i = &(*i)->next) {
487                 if (*i == tdb) {
488                         *i = tdb->next;
489                         break;
490                 }
491         }
492
493         memset(tdb, 0, sizeof(*tdb));
494         SAFE_FREE(tdb);
495
496         return ret;
497 }