open.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 1999-2005
  5. Copyright (C) Paul `Rusty' Russell 2000
  6. Copyright (C) Jeremy Allison 2000-2003
  7. ** NOTE! The following LGPL license applies to the tdb
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "tdb_private.h"
  22. /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
  23. static struct tdb_context *tdbs = NULL;
  24. /* This is based on the hash algorithm from gdbm */
  25. static unsigned int default_tdb_hash(TDB_DATA *key)
  26. {
  27. uint32_t value; /* Used to compute the hash value. */
  28. uint32_t i; /* Used to cycle through random values. */
  29. /* Set the initial value from the key size. */
  30. for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
  31. value = (value + (key->dptr[i] << (i*5 % 24)));
  32. return (1103515243 * value + 12345);
  33. }
  34. /* initialise a new database with a specified hash size */
  35. static int tdb_new_database(struct tdb_context *tdb, int hash_size)
  36. {
  37. struct tdb_header *newdb;
  38. size_t size;
  39. int ret = -1;
  40. ssize_t written;
  41. /* We make it up in memory, then write it out if not internal */
  42. size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
  43. if (!(newdb = (struct tdb_header *)calloc(size, 1))) {
  44. tdb->ecode = TDB_ERR_OOM;
  45. return -1;
  46. }
  47. /* Fill in the header */
  48. newdb->version = TDB_VERSION;
  49. newdb->hash_size = hash_size;
  50. if (tdb->flags & TDB_INTERNAL) {
  51. tdb->map_size = size;
  52. tdb->map_ptr = (char *)newdb;
  53. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  54. /* Convert the `ondisk' version if asked. */
  55. CONVERT(*newdb);
  56. return 0;
  57. }
  58. if (lseek(tdb->fd, 0, SEEK_SET) == -1)
  59. goto fail;
  60. if (ftruncate(tdb->fd, 0) == -1)
  61. goto fail;
  62. /* This creates an endian-converted header, as if read from disk */
  63. CONVERT(*newdb);
  64. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  65. /* Don't endian-convert the magic food! */
  66. memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
  67. /* we still have "ret == -1" here */
  68. written = write(tdb->fd, newdb, size);
  69. if (written == size) {
  70. ret = 0;
  71. } else if (written != -1) {
  72. /* call write once again, this usually should return -1 and
  73. * set errno appropriately */
  74. size -= written;
  75. written = write(tdb->fd, newdb+written, size);
  76. if (written == size) {
  77. ret = 0;
  78. } else if (written >= 0) {
  79. /* a second incomplete write - we give up.
  80. * guessing the errno... */
  81. errno = ENOSPC;
  82. }
  83. }
  84. fail:
  85. SAFE_FREE(newdb);
  86. return ret;
  87. }
  88. static int tdb_already_open(dev_t device,
  89. ino_t ino)
  90. {
  91. struct tdb_context *i;
  92. for (i = tdbs; i; i = i->next) {
  93. if (i->device == device && i->inode == ino) {
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /* open the database, creating it if necessary
  100. The open_flags and mode are passed straight to the open call on the
  101. database file. A flags value of O_WRONLY is invalid. The hash size
  102. is advisory, use zero for a default value.
  103. Return is NULL on error, in which case errno is also set. Don't
  104. try to call tdb_error or tdb_errname, just do strerror(errno).
  105. @param name may be NULL for internal databases. */
  106. struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
  107. int open_flags, mode_t mode)
  108. {
  109. return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
  110. }
  111. /* a default logging function */
  112. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
  113. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
  114. {
  115. }
  116. struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
  117. int open_flags, mode_t mode,
  118. const struct tdb_logging_context *log_ctx,
  119. tdb_hash_func hash_fn)
  120. {
  121. struct tdb_context *tdb;
  122. struct stat st;
  123. int rev = 0, locked = 0;
  124. unsigned char *vp;
  125. uint32_t vertest;
  126. unsigned v;
  127. if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
  128. /* Can't log this */
  129. errno = ENOMEM;
  130. goto fail;
  131. }
  132. tdb_io_init(tdb);
  133. tdb->fd = -1;
  134. #ifdef TDB_TRACE
  135. tdb->tracefd = -1;
  136. #endif
  137. tdb->name = NULL;
  138. tdb->map_ptr = NULL;
  139. tdb->flags = tdb_flags;
  140. tdb->open_flags = open_flags;
  141. if (log_ctx) {
  142. tdb->log = *log_ctx;
  143. } else {
  144. tdb->log.log_fn = null_log_fn;
  145. tdb->log.log_private = NULL;
  146. }
  147. tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
  148. /* cache the page size */
  149. tdb->page_size = getpagesize();
  150. if (tdb->page_size <= 0) {
  151. tdb->page_size = 0x2000;
  152. }
  153. tdb->max_dead_records = (tdb_flags & TDB_VOLATILE) ? 5 : 0;
  154. if ((open_flags & O_ACCMODE) == O_WRONLY) {
  155. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
  156. name));
  157. errno = EINVAL;
  158. goto fail;
  159. }
  160. if (hash_size == 0)
  161. hash_size = DEFAULT_HASH_SIZE;
  162. if ((open_flags & O_ACCMODE) == O_RDONLY) {
  163. tdb->read_only = 1;
  164. /* read only databases don't do locking or clear if first */
  165. tdb->flags |= TDB_NOLOCK;
  166. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  167. }
  168. if ((tdb->flags & TDB_ALLOW_NESTING) &&
  169. (tdb->flags & TDB_DISALLOW_NESTING)) {
  170. tdb->ecode = TDB_ERR_NESTING;
  171. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
  172. "allow_nesting and disallow_nesting are not allowed together!"));
  173. errno = EINVAL;
  174. goto fail;
  175. }
  176. /*
  177. * TDB_ALLOW_NESTING is the default behavior.
  178. * Note: this may change in future versions!
  179. */
  180. if (!(tdb->flags & TDB_DISALLOW_NESTING)) {
  181. tdb->flags |= TDB_ALLOW_NESTING;
  182. }
  183. /* internal databases don't mmap or lock, and start off cleared */
  184. if (tdb->flags & TDB_INTERNAL) {
  185. tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
  186. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  187. if (tdb_new_database(tdb, hash_size) != 0) {
  188. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
  189. goto fail;
  190. }
  191. goto internal;
  192. }
  193. if ((tdb->fd = open(name, open_flags, mode)) == -1) {
  194. TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
  195. name, strerror(errno)));
  196. goto fail; /* errno set by open(2) */
  197. }
  198. /* on exec, don't inherit the fd */
  199. v = fcntl(tdb->fd, F_GETFD, 0);
  200. fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
  201. /* ensure there is only one process initialising at once */
  202. if (tdb->methods->brlock(tdb, F_WRLCK, GLOBAL_LOCK, 1, TDB_LOCK_WAIT) == -1) {
  203. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
  204. name, strerror(errno)));
  205. goto fail; /* errno set by tdb_brlock */
  206. }
  207. /* we need to zero database if we are the only one with it open */
  208. if ((tdb_flags & TDB_CLEAR_IF_FIRST) &&
  209. (!tdb->read_only) &&
  210. (locked = (tdb->methods->brlock(tdb, F_WRLCK, ACTIVE_LOCK, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE) == 0))) {
  211. open_flags |= O_CREAT;
  212. if (ftruncate(tdb->fd, 0) == -1) {
  213. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
  214. "failed to truncate %s: %s\n",
  215. name, strerror(errno)));
  216. goto fail; /* errno set by ftruncate */
  217. }
  218. }
  219. errno = 0;
  220. if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
  221. || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
  222. if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
  223. if (errno == 0) {
  224. errno = EIO; /* ie bad format or something */
  225. }
  226. goto fail;
  227. }
  228. rev = (tdb->flags & TDB_CONVERT);
  229. } else if (tdb->header.version != TDB_VERSION
  230. && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION)))) {
  231. /* wrong version */
  232. errno = EIO;
  233. goto fail;
  234. }
  235. vp = (unsigned char *)&tdb->header.version;
  236. vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
  237. (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3];
  238. tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
  239. if (!rev)
  240. tdb->flags &= ~TDB_CONVERT;
  241. else {
  242. tdb->flags |= TDB_CONVERT;
  243. tdb_convert(&tdb->header, sizeof(tdb->header));
  244. }
  245. if (fstat(tdb->fd, &st) == -1)
  246. goto fail;
  247. if (tdb->header.rwlocks != 0) {
  248. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n"));
  249. goto fail;
  250. }
  251. /* Is it already in the open list? If so, fail. */
  252. if (tdb_already_open(st.st_dev, st.st_ino)) {
  253. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  254. "%s (%d,%d) is already open in this process\n",
  255. name, (int)st.st_dev, (int)st.st_ino));
  256. errno = EBUSY;
  257. goto fail;
  258. }
  259. if (!(tdb->name = (char *)strdup(name))) {
  260. errno = ENOMEM;
  261. goto fail;
  262. }
  263. tdb->map_size = st.st_size;
  264. tdb->device = st.st_dev;
  265. tdb->inode = st.st_ino;
  266. tdb_mmap(tdb);
  267. if (locked) {
  268. if (tdb->methods->brunlock(tdb, F_WRLCK, ACTIVE_LOCK, 1) == -1) {
  269. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  270. "failed to take ACTIVE_LOCK on %s: %s\n",
  271. name, strerror(errno)));
  272. goto fail;
  273. }
  274. }
  275. /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
  276. we didn't get the initial exclusive lock as we need to let all other
  277. users know we're using it. */
  278. if (tdb_flags & TDB_CLEAR_IF_FIRST) {
  279. /* leave this lock in place to indicate it's in use */
  280. if (tdb->methods->brlock(tdb, F_RDLCK, ACTIVE_LOCK, 1, TDB_LOCK_WAIT) == -1)
  281. goto fail;
  282. }
  283. /* if needed, run recovery */
  284. if (tdb_transaction_recover(tdb) == -1) {
  285. goto fail;
  286. }
  287. #ifdef TDB_TRACE
  288. {
  289. char tracefile[strlen(name) + 32];
  290. sprintf(tracefile, "%s.trace.%u", name, getpid());
  291. tdb->tracefd = open(tracefile, O_WRONLY|O_CREAT|O_EXCL, 0600);
  292. if (tdb->tracefd >= 0) {
  293. tdb_enable_seqnum(tdb);
  294. tdb_trace_open(tdb, "tdb_open", hash_size, tdb_flags,
  295. open_flags);
  296. } else
  297. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to open trace file %s!\n", tracefile));
  298. }
  299. #endif
  300. internal:
  301. /* Internal (memory-only) databases skip all the code above to
  302. * do with disk files, and resume here by releasing their
  303. * global lock and hooking into the active list. */
  304. if (tdb->methods->brunlock(tdb, F_WRLCK, GLOBAL_LOCK, 1) == -1)
  305. goto fail;
  306. tdb->next = tdbs;
  307. tdbs = tdb;
  308. return tdb;
  309. fail:
  310. { int save_errno = errno;
  311. if (!tdb)
  312. return NULL;
  313. #ifdef TDB_TRACE
  314. close(tdb->tracefd);
  315. #endif
  316. if (tdb->map_ptr) {
  317. if (tdb->flags & TDB_INTERNAL)
  318. SAFE_FREE(tdb->map_ptr);
  319. else
  320. tdb_munmap(tdb);
  321. }
  322. SAFE_FREE(tdb->name);
  323. if (tdb->fd != -1)
  324. if (close(tdb->fd) != 0)
  325. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
  326. SAFE_FREE(tdb);
  327. errno = save_errno;
  328. return NULL;
  329. }
  330. }
  331. /*
  332. * Set the maximum number of dead records per hash chain
  333. */
  334. void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
  335. {
  336. tdb->max_dead_records = max_dead;
  337. }
  338. /**
  339. * Close a database.
  340. *
  341. * @returns -1 for error; 0 for success.
  342. **/
  343. int tdb_close(struct tdb_context *tdb)
  344. {
  345. struct tdb_context **i;
  346. int ret = 0;
  347. if (tdb->transaction) {
  348. tdb_transaction_cancel(tdb);
  349. }
  350. tdb_trace(tdb, "tdb_close");
  351. if (tdb->map_ptr) {
  352. if (tdb->flags & TDB_INTERNAL)
  353. SAFE_FREE(tdb->map_ptr);
  354. else
  355. tdb_munmap(tdb);
  356. }
  357. SAFE_FREE(tdb->name);
  358. if (tdb->fd != -1) {
  359. ret = close(tdb->fd);
  360. tdb->fd = -1;
  361. }
  362. SAFE_FREE(tdb->lockrecs);
  363. /* Remove from contexts list */
  364. for (i = &tdbs; *i; i = &(*i)->next) {
  365. if (*i == tdb) {
  366. *i = tdb->next;
  367. break;
  368. }
  369. }
  370. #ifdef TDB_TRACE
  371. close(tdb->tracefd);
  372. #endif
  373. memset(tdb, 0, sizeof(*tdb));
  374. SAFE_FREE(tdb);
  375. return ret;
  376. }
  377. /* register a loging function */
  378. void tdb_set_logging_function(struct tdb_context *tdb,
  379. const struct tdb_logging_context *log_ctx)
  380. {
  381. tdb->log = *log_ctx;
  382. }
  383. void *tdb_get_logging_private(struct tdb_context *tdb)
  384. {
  385. return tdb->log.log_private;
  386. }
  387. /* reopen a tdb - this can be used after a fork to ensure that we have an independent
  388. seek pointer from our parent and to re-establish locks */
  389. int tdb_reopen(struct tdb_context *tdb)
  390. {
  391. struct stat st;
  392. if (tdb->flags & TDB_INTERNAL) {
  393. return 0; /* Nothing to do. */
  394. }
  395. if (tdb->num_locks != 0 || tdb->global_lock.count) {
  396. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
  397. goto fail;
  398. }
  399. if (tdb->transaction != 0) {
  400. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
  401. goto fail;
  402. }
  403. if (tdb_munmap(tdb) != 0) {
  404. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
  405. goto fail;
  406. }
  407. if (close(tdb->fd) != 0)
  408. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
  409. tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
  410. if (tdb->fd == -1) {
  411. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
  412. goto fail;
  413. }
  414. if ((tdb->flags & TDB_CLEAR_IF_FIRST) &&
  415. (tdb->methods->brlock(tdb, F_RDLCK, ACTIVE_LOCK, 1, TDB_LOCK_WAIT) == -1)) {
  416. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
  417. goto fail;
  418. }
  419. if (fstat(tdb->fd, &st) != 0) {
  420. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
  421. goto fail;
  422. }
  423. if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
  424. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
  425. goto fail;
  426. }
  427. tdb_mmap(tdb);
  428. return 0;
  429. fail:
  430. tdb_close(tdb);
  431. return -1;
  432. }
  433. /* reopen all tdb's */
  434. int tdb_reopen_all(int parent_longlived)
  435. {
  436. struct tdb_context *tdb;
  437. for (tdb=tdbs; tdb; tdb = tdb->next) {
  438. /*
  439. * If the parent is longlived (ie. a
  440. * parent daemon architecture), we know
  441. * it will keep it's active lock on a
  442. * tdb opened with CLEAR_IF_FIRST. Thus
  443. * for child processes we don't have to
  444. * add an active lock. This is essential
  445. * to improve performance on systems that
  446. * keep POSIX locks as a non-scalable data
  447. * structure in the kernel.
  448. */
  449. if (parent_longlived) {
  450. /* Ensure no clear-if-first. */
  451. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  452. }
  453. if (tdb_reopen(tdb) != 0)
  454. return -1;
  455. }
  456. return 0;
  457. }