tdb2: move file operations into separate structure
[ccan] / ccan / tdb2 / open.c
1 #include "private.h"
2
3 /* all lock info, to detect double-opens (fcntl file don't nest!) */
4 static struct tdb_file *files = NULL;
5
6 static struct tdb_file *find_file(dev_t device, ino_t ino)
7 {
8         struct tdb_file *i;
9
10         for (i = files; i; i = i->next) {
11                 if (i->device == device && i->inode == ino) {
12                         break;
13                 }
14         }
15         return i;
16 }
17
18 static bool read_all(int fd, void *buf, size_t len)
19 {
20         while (len) {
21                 ssize_t ret;
22                 ret = read(fd, buf, len);
23                 if (ret < 0)
24                         return false;
25                 if (ret == 0) {
26                         /* ETOOSHORT? */
27                         errno = EWOULDBLOCK;
28                         return false;
29                 }
30                 buf = (char *)buf + ret;
31                 len -= ret;
32         }
33         return true;
34 }
35
36 static uint64_t random_number(struct tdb_context *tdb)
37 {
38         int fd;
39         uint64_t ret = 0;
40         struct timeval now;
41
42         fd = open("/dev/urandom", O_RDONLY);
43         if (fd >= 0) {
44                 if (read_all(fd, &ret, sizeof(ret))) {
45                         close(fd);
46                         return ret;
47                 }
48                 close(fd);
49         }
50         /* FIXME: Untested!  Based on Wikipedia protocol description! */
51         fd = open("/dev/egd-pool", O_RDWR);
52         if (fd >= 0) {
53                 /* Command is 1, next byte is size we want to read. */
54                 char cmd[2] = { 1, sizeof(uint64_t) };
55                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
56                         char reply[1 + sizeof(uint64_t)];
57                         int r = read(fd, reply, sizeof(reply));
58                         if (r > 1) {
59                                 /* Copy at least some bytes. */
60                                 memcpy(&ret, reply+1, r - 1);
61                                 if (reply[0] == sizeof(uint64_t)
62                                     && r == sizeof(reply)) {
63                                         close(fd);
64                                         return ret;
65                                 }
66                         }
67                 }
68                 close(fd);
69         }
70
71         /* Fallback: pid and time. */
72         gettimeofday(&now, NULL);
73         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
74         tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
75                    "tdb_open: random from getpid and time");
76         return ret;
77 }
78
79 struct new_database {
80         struct tdb_header hdr;
81         struct tdb_freetable ftable;
82 };
83
84 /* initialise a new database */
85 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
86                                        struct tdb_attribute_seed *seed,
87                                        struct tdb_header *hdr)
88 {
89         /* We make it up in memory, then write it out if not internal */
90         struct new_database newdb;
91         unsigned int magic_len;
92         ssize_t rlen;
93         enum TDB_ERROR ecode;
94
95         /* Fill in the header */
96         newdb.hdr.version = TDB_VERSION;
97         if (seed)
98                 newdb.hdr.hash_seed = seed->seed;
99         else
100                 newdb.hdr.hash_seed = random_number(tdb);
101         newdb.hdr.hash_test = TDB_HASH_MAGIC;
102         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
103                                          sizeof(newdb.hdr.hash_test),
104                                          newdb.hdr.hash_seed,
105                                          tdb->hash_priv);
106         newdb.hdr.recovery = 0;
107         newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
108         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
109         /* Initial hashes are empty. */
110         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
111
112         /* Free is empty. */
113         newdb.hdr.free_table = offsetof(struct new_database, ftable);
114         memset(&newdb.ftable, 0, sizeof(newdb.ftable));
115         ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
116                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
117                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
118                            0);
119         if (ecode != TDB_SUCCESS) {
120                 return ecode;
121         }
122
123         /* Magic food */
124         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
125         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
126
127         /* This creates an endian-converted database, as if read from disk */
128         magic_len = sizeof(newdb.hdr.magic_food);
129         tdb_convert(tdb,
130                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
131
132         *hdr = newdb.hdr;
133
134         if (tdb->flags & TDB_INTERNAL) {
135                 tdb->map_size = sizeof(newdb);
136                 tdb->map_ptr = malloc(tdb->map_size);
137                 if (!tdb->map_ptr) {
138                         return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
139                                           "tdb_new_database:"
140                                           " failed to allocate");
141                 }
142                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
143                 return TDB_SUCCESS;
144         }
145         if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
146                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
147                                   "tdb_new_database:"
148                                   " failed to seek: %s", strerror(errno));
149         }
150
151         if (ftruncate(tdb->file->fd, 0) == -1) {
152                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
153                                   "tdb_new_database:"
154                                   " failed to truncate: %s", strerror(errno));
155         }
156
157         rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
158         if (rlen != sizeof(newdb)) {
159                 if (rlen >= 0)
160                         errno = ENOSPC;
161                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
162                                   "tdb_new_database: %zi writing header: %s",
163                                   rlen, strerror(errno));
164         }
165         return TDB_SUCCESS;
166 }
167
168 struct tdb_context *tdb_open(const char *name, int tdb_flags,
169                              int open_flags, mode_t mode,
170                              union tdb_attribute *attr)
171 {
172         struct tdb_context *tdb;
173         struct stat st;
174         int saved_errno = 0;
175         uint64_t hash_test;
176         unsigned v;
177         ssize_t rlen;
178         struct tdb_header hdr;
179         struct tdb_attribute_seed *seed = NULL;
180         tdb_bool_err berr;
181         enum TDB_ERROR ecode;
182
183         tdb = malloc(sizeof(*tdb));
184         if (!tdb) {
185                 /* Can't log this */
186                 errno = ENOMEM;
187                 return NULL;
188         }
189         tdb->name = NULL;
190         tdb->map_ptr = NULL;
191         tdb->direct_access = 0;
192         tdb->map_size = sizeof(struct tdb_header);
193         tdb->flags = tdb_flags;
194         tdb->logfn = NULL;
195         tdb->transaction = NULL;
196         tdb->stats = NULL;
197         tdb->access = NULL;
198         tdb->file = NULL;
199         tdb_hash_init(tdb);
200         tdb_io_init(tdb);
201
202         while (attr) {
203                 switch (attr->base.attr) {
204                 case TDB_ATTRIBUTE_LOG:
205                         tdb->logfn = attr->log.log_fn;
206                         tdb->log_private = attr->log.log_private;
207                         break;
208                 case TDB_ATTRIBUTE_HASH:
209                         tdb->khash = attr->hash.hash_fn;
210                         tdb->hash_priv = attr->hash.hash_private;
211                         break;
212                 case TDB_ATTRIBUTE_SEED:
213                         seed = &attr->seed;
214                         break;
215                 case TDB_ATTRIBUTE_STATS:
216                         tdb->stats = &attr->stats;
217                         /* They have stats we don't know about?  Tell them. */
218                         if (tdb->stats->size > sizeof(attr->stats))
219                                 tdb->stats->size = sizeof(attr->stats);
220                         break;
221                 default:
222                         ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
223                                            TDB_LOG_USE_ERROR,
224                                            "tdb_open:"
225                                            " unknown attribute type %u",
226                                            attr->base.attr);
227                         goto fail;
228                 }
229                 attr = attr->base.next;
230         }
231
232         if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
233                           | TDB_NOSYNC)) {
234                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
235                                    "tdb_open: unknown flags %u", tdb_flags);
236                 goto fail;
237         }
238
239         if ((open_flags & O_ACCMODE) == O_WRONLY) {
240                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
241                                    "tdb_open: can't open tdb %s write-only",
242                                    name);
243                 goto fail;
244         }
245
246         if ((open_flags & O_ACCMODE) == O_RDONLY) {
247                 tdb->read_only = true;
248                 tdb->mmap_flags = PROT_READ;
249         } else {
250                 tdb->read_only = false;
251                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
252         }
253
254         /* internal databases don't need any of the rest. */
255         if (tdb->flags & TDB_INTERNAL) {
256                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
257                 ecode = tdb_new_database(tdb, seed, &hdr);
258                 if (ecode != TDB_SUCCESS) {
259                         goto fail;
260                 }
261                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
262                 tdb->hash_seed = hdr.hash_seed;
263                 tdb_ftable_init(tdb);
264                 return tdb;
265         }
266
267         if (stat(name, &st) != -1)
268                 tdb->file = find_file(st.st_dev, st.st_ino);
269
270         if (!tdb->file) {
271                 int fd;
272
273                 if ((fd = open(name, open_flags, mode)) == -1) {
274                         /* errno set by open(2) */
275                         saved_errno = errno;
276                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
277                                    "tdb_open: could not open file %s: %s",
278                                    name, strerror(errno));
279                         goto fail;
280                 }
281
282                 /* on exec, don't inherit the fd */
283                 v = fcntl(fd, F_GETFD, 0);
284                 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
285
286                 if (fstat(fd, &st) == -1) {
287                         saved_errno = errno;
288                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
289                                    "tdb_open: could not stat open %s: %s",
290                                    name, strerror(errno));
291                         goto fail;
292                 }
293
294                 tdb->file = malloc(sizeof(*tdb->file));
295                 if (!tdb->file) {
296                         saved_errno = ENOMEM;
297                         tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
298                                    "tdb_open: could alloc file");
299                         goto fail;
300                 }
301
302                 tdb->file->next = files;
303                 tdb->file->num_lockrecs = 0;
304                 tdb->file->lockrecs = NULL;
305                 tdb->file->allrecord_lock.count = 0;
306                 tdb->file->fd = fd;
307                 tdb->file->device = st.st_dev;
308                 tdb->file->inode = st.st_ino;
309         } else {
310                 /* FIXME */
311                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
312                                    "tdb_open: %s (%d,%d) is already open in"
313                                    " this process",
314                                    name, (int)st.st_dev, (int)st.st_ino);
315                 goto fail;
316         }
317
318         /* ensure there is only one process initialising at once */
319         ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
320         if (ecode != TDB_SUCCESS) {
321                 goto fail;
322         }
323
324         /* If they used O_TRUNC, read will return 0. */
325         rlen = read(tdb->file->fd, &hdr, sizeof(hdr));
326         if (rlen == 0 && (open_flags & O_CREAT)) {
327                 ecode = tdb_new_database(tdb, seed, &hdr);
328                 if (ecode != TDB_SUCCESS) {
329                         goto fail;
330                 }
331         } else if (rlen < 0) {
332                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
333                                    "tdb_open: error %s reading %s",
334                                    strerror(errno), name);
335                 goto fail;
336         } else if (rlen < sizeof(hdr)
337                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
338                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
339                                    "tdb_open: %s is not a tdb file", name);
340                 goto fail;
341         }
342
343         if (hdr.version != TDB_VERSION) {
344                 if (hdr.version == bswap_64(TDB_VERSION))
345                         tdb->flags |= TDB_CONVERT;
346                 else {
347                         /* wrong version */
348                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
349                                            "tdb_open:"
350                                            " %s is unknown version 0x%llx",
351                                            name, (long long)hdr.version);
352                         goto fail;
353                 }
354         }
355
356         tdb_convert(tdb, &hdr, sizeof(hdr));
357         tdb->hash_seed = hdr.hash_seed;
358         hash_test = TDB_HASH_MAGIC;
359         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
360         if (hdr.hash_test != hash_test) {
361                 /* wrong hash variant */
362                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
363                                    "tdb_open:"
364                                    " %s uses a different hash function",
365                                    name);
366                 goto fail;
367         }
368
369         tdb->name = strdup(name);
370         if (!tdb->name) {
371                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
372                                    "tdb_open: failed to allocate name");
373                 goto fail;
374         }
375
376         /* Clear any features we don't understand. */
377         if ((open_flags & O_ACCMODE) != O_RDONLY) {
378                 hdr.features_used &= TDB_FEATURE_MASK;
379                 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
380                                                     features_used),
381                                       &hdr.features_used,
382                                       sizeof(hdr.features_used)) == -1)
383                         goto fail;
384         }
385
386         tdb_unlock_open(tdb);
387
388         /* This make sure we have current map_size and mmap. */
389         tdb->methods->oob(tdb, tdb->map_size + 1, true);
390
391         /* Now it's fully formed, recover if necessary. */
392         berr = tdb_needs_recovery(tdb);
393         if (unlikely(berr != false)) {
394                 if (berr < 0) {
395                         ecode = berr;
396                         goto fail;
397                 }
398                 ecode = tdb_lock_and_recover(tdb);
399                 if (ecode != TDB_SUCCESS) {
400                         goto fail;
401                 }
402         }
403
404         ecode = tdb_ftable_init(tdb);
405         if (ecode != TDB_SUCCESS) {
406                 goto fail;
407         }
408
409         /* Add to linked list. */
410         files = tdb->file;
411         return tdb;
412
413  fail:
414         /* Map ecode to some logical errno. */
415         if (!saved_errno) {
416                 switch (ecode) {
417                 case TDB_ERR_CORRUPT:
418                 case TDB_ERR_IO:
419                         saved_errno = EIO;
420                         break;
421                 case TDB_ERR_LOCK:
422                         saved_errno = EWOULDBLOCK;
423                         break;
424                 case TDB_ERR_OOM:
425                         saved_errno = ENOMEM;
426                         break;
427                 case TDB_ERR_EINVAL:
428                         saved_errno = EINVAL;
429                         break;
430                 default:
431                         saved_errno = EINVAL;
432                         break;
433                 }
434         }
435
436 #ifdef TDB_TRACE
437         close(tdb->tracefd);
438 #endif
439         if (tdb->map_ptr) {
440                 if (tdb->flags & TDB_INTERNAL) {
441                         free(tdb->map_ptr);
442                 } else
443                         tdb_munmap(tdb);
444         }
445         free((char *)tdb->name);
446         if (tdb->file) {
447                 if (close(tdb->file->fd) != 0)
448                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
449                                    "tdb_open: failed to close tdb fd"
450                                    " on error: %s", strerror(errno));
451                 free(tdb->file->lockrecs);
452                 free(tdb->file);
453         }
454
455         free(tdb);
456         errno = saved_errno;
457         return NULL;
458 }
459
460 int tdb_close(struct tdb_context *tdb)
461 {
462         int ret = 0;
463
464         tdb_trace(tdb, "tdb_close");
465
466         if (tdb->transaction) {
467                 tdb_transaction_cancel(tdb);
468         }
469
470         if (tdb->map_ptr) {
471                 if (tdb->flags & TDB_INTERNAL)
472                         free(tdb->map_ptr);
473                 else
474                         tdb_munmap(tdb);
475         }
476         free((char *)tdb->name);
477         if (tdb->file) {
478                 struct tdb_file **i;
479                 ret = close(tdb->file->fd);
480
481                 /* Remove from files list */
482                 for (i = &files; *i; i = &(*i)->next) {
483                         if (*i == tdb->file) {
484                                 *i = tdb->file->next;
485                                 break;
486                         }
487                 }
488                 free(tdb->file->lockrecs);
489                 free(tdb->file);
490         }
491
492 #ifdef TDB_TRACE
493         close(tdb->tracefd);
494 #endif
495         free(tdb);
496
497         return ret;
498 }