bt_open.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Mike Olson.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)bt_open.c 8.10 (Berkeley) 8/17/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. /*
  40. * Implementation of btree access method for 4.4BSD.
  41. *
  42. * The design here was originally based on that of the btree access method
  43. * used in the Postgres database system at UC Berkeley. This implementation
  44. * is wholly independent of the Postgres code.
  45. */
  46. #include <sys/param.h>
  47. #include <sys/stat.h>
  48. #include <errno.h>
  49. #include <fcntl.h>
  50. #include <limits.h>
  51. #include <signal.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <db.h>
  57. #include "btree.h"
  58. #ifdef DEBUG
  59. #undef MINPSIZE
  60. #define MINPSIZE 128
  61. #endif
  62. static int byteorder __P((void));
  63. static int nroot __P((BTREE *));
  64. static int tmp __P((void));
  65. /*
  66. * __BT_OPEN -- Open a btree.
  67. *
  68. * Creates and fills a DB struct, and calls the routine that actually
  69. * opens the btree.
  70. *
  71. * Parameters:
  72. * fname: filename (NULL for in-memory trees)
  73. * flags: open flag bits
  74. * mode: open permission bits
  75. * b: BTREEINFO pointer
  76. *
  77. * Returns:
  78. * NULL on failure, pointer to DB on success.
  79. *
  80. */
  81. DB *
  82. __bt_open(fname, flags, mode, openinfo, dflags)
  83. const char *fname;
  84. int flags, mode, dflags;
  85. const BTREEINFO *openinfo;
  86. {
  87. struct stat sb;
  88. BTMETA m;
  89. BTREE *t;
  90. BTREEINFO b;
  91. DB *dbp;
  92. pgno_t ncache;
  93. ssize_t nr;
  94. int machine_lorder;
  95. t = NULL;
  96. /*
  97. * Intention is to make sure all of the user's selections are okay
  98. * here and then use them without checking. Can't be complete, since
  99. * we don't know the right page size, lorder or flags until the backing
  100. * file is opened. Also, the file's page size can cause the cachesize
  101. * to change.
  102. */
  103. machine_lorder = byteorder();
  104. if (openinfo) {
  105. b = *openinfo;
  106. /* Flags: R_DUP. */
  107. if (b.flags & ~(R_DUP))
  108. goto einval;
  109. /*
  110. * Page size must be indx_t aligned and >= MINPSIZE. Default
  111. * page size is set farther on, based on the underlying file
  112. * transfer size.
  113. */
  114. if (b.psize &&
  115. (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
  116. b.psize & (sizeof(indx_t) - 1)))
  117. goto einval;
  118. /* Minimum number of keys per page; absolute minimum is 2. */
  119. if (b.minkeypage) {
  120. if (b.minkeypage < 2)
  121. goto einval;
  122. } else
  123. b.minkeypage = DEFMINKEYPAGE;
  124. /* If no comparison, use default comparison and prefix. */
  125. if (b.compare == NULL) {
  126. b.compare = __bt_defcmp;
  127. if (b.prefix == NULL)
  128. b.prefix = __bt_defpfx;
  129. }
  130. if (b.lorder == 0)
  131. b.lorder = machine_lorder;
  132. } else {
  133. b.compare = __bt_defcmp;
  134. b.cachesize = 0;
  135. b.flags = 0;
  136. b.lorder = machine_lorder;
  137. b.minkeypage = DEFMINKEYPAGE;
  138. b.prefix = __bt_defpfx;
  139. b.psize = 0;
  140. }
  141. /* Check for the ubiquitous PDP-11. */
  142. if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
  143. goto einval;
  144. /* Allocate and initialize DB and BTREE structures. */
  145. if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
  146. goto err;
  147. memset(t, 0, sizeof(BTREE));
  148. t->bt_fd = -1; /* Don't close unopened fd on error. */
  149. t->bt_lorder = b.lorder;
  150. t->bt_order = NOT;
  151. t->bt_cmp = b.compare;
  152. t->bt_pfx = b.prefix;
  153. t->bt_rfd = -1;
  154. if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
  155. goto err;
  156. memset(t->bt_dbp, 0, sizeof(DB));
  157. if (t->bt_lorder != machine_lorder)
  158. F_SET(t, B_NEEDSWAP);
  159. dbp->type = DB_BTREE;
  160. dbp->internal = t;
  161. dbp->close = __bt_close;
  162. dbp->del = __bt_delete;
  163. dbp->fd = __bt_fd;
  164. dbp->get = __bt_get;
  165. dbp->put = __bt_put;
  166. dbp->seq = __bt_seq;
  167. dbp->sync = __bt_sync;
  168. /*
  169. * If no file name was supplied, this is an in-memory btree and we
  170. * open a backing temporary file. Otherwise, it's a disk-based tree.
  171. */
  172. if (fname) {
  173. switch (flags & O_ACCMODE) {
  174. case O_RDONLY:
  175. F_SET(t, B_RDONLY);
  176. break;
  177. case O_RDWR:
  178. break;
  179. case O_WRONLY:
  180. default:
  181. goto einval;
  182. }
  183. if ((t->bt_fd = open(fname, flags, mode)) < 0)
  184. goto err;
  185. } else {
  186. if ((flags & O_ACCMODE) != O_RDWR)
  187. goto einval;
  188. if ((t->bt_fd = tmp()) == -1)
  189. goto err;
  190. F_SET(t, B_INMEM);
  191. }
  192. if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
  193. goto err;
  194. if (fstat(t->bt_fd, &sb))
  195. goto err;
  196. if (sb.st_size) {
  197. if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
  198. goto err;
  199. if (nr != sizeof(BTMETA))
  200. goto eftype;
  201. /*
  202. * Read in the meta-data. This can change the notion of what
  203. * the lorder, page size and flags are, and, when the page size
  204. * changes, the cachesize value can change too. If the user
  205. * specified the wrong byte order for an existing database, we
  206. * don't bother to return an error, we just clear the NEEDSWAP
  207. * bit.
  208. */
  209. if (m.magic == BTREEMAGIC)
  210. F_CLR(t, B_NEEDSWAP);
  211. else {
  212. F_SET(t, B_NEEDSWAP);
  213. M_32_SWAP(m.magic);
  214. M_32_SWAP(m.version);
  215. M_32_SWAP(m.psize);
  216. M_32_SWAP(m.free);
  217. M_32_SWAP(m.nrecs);
  218. M_32_SWAP(m.flags);
  219. }
  220. if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
  221. goto eftype;
  222. if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
  223. m.psize & (sizeof(indx_t) - 1))
  224. goto eftype;
  225. if (m.flags & ~SAVEMETA)
  226. goto eftype;
  227. b.psize = m.psize;
  228. F_SET(t, m.flags);
  229. t->bt_free = m.free;
  230. t->bt_nrecs = m.nrecs;
  231. } else {
  232. /*
  233. * Set the page size to the best value for I/O to this file.
  234. * Don't overflow the page offset type.
  235. */
  236. if (b.psize == 0) {
  237. #ifdef _STATBUF_ST_BLKSIZE
  238. b.psize = sb.st_blksize;
  239. #endif
  240. if (b.psize < MINPSIZE)
  241. b.psize = MINPSIZE;
  242. if (b.psize > MAX_PAGE_OFFSET + 1)
  243. b.psize = MAX_PAGE_OFFSET + 1;
  244. }
  245. /* Set flag if duplicates permitted. */
  246. if (!(b.flags & R_DUP))
  247. F_SET(t, B_NODUPS);
  248. t->bt_free = P_INVALID;
  249. t->bt_nrecs = 0;
  250. F_SET(t, B_METADIRTY);
  251. }
  252. t->bt_psize = b.psize;
  253. /* Set the cache size; must be a multiple of the page size. */
  254. if (b.cachesize && b.cachesize & (b.psize - 1))
  255. b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
  256. if (b.cachesize < b.psize * MINCACHE)
  257. b.cachesize = b.psize * MINCACHE;
  258. /* Calculate number of pages to cache. */
  259. ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
  260. /*
  261. * The btree data structure requires that at least two keys can fit on
  262. * a page, but other than that there's no fixed requirement. The user
  263. * specified a minimum number per page, and we translated that into the
  264. * number of bytes a key/data pair can use before being placed on an
  265. * overflow page. This calculation includes the page header, the size
  266. * of the index referencing the leaf item and the size of the leaf item
  267. * structure. Also, don't let the user specify a minkeypage such that
  268. * a key/data pair won't fit even if both key and data are on overflow
  269. * pages.
  270. */
  271. t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
  272. (sizeof(indx_t) + NBLEAFDBT(0, 0));
  273. if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
  274. t->bt_ovflsize =
  275. NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
  276. /* Initialize the buffer pool. */
  277. if ((t->bt_mp =
  278. mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
  279. goto err;
  280. if (!F_ISSET(t, B_INMEM))
  281. mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
  282. /* Create a root page if new tree. */
  283. if (nroot(t) == RET_ERROR)
  284. goto err;
  285. /* Global flags. */
  286. if (dflags & DB_LOCK)
  287. F_SET(t, B_DB_LOCK);
  288. if (dflags & DB_SHMEM)
  289. F_SET(t, B_DB_SHMEM);
  290. if (dflags & DB_TXN)
  291. F_SET(t, B_DB_TXN);
  292. return (dbp);
  293. einval: errno = EINVAL;
  294. goto err;
  295. eftype: errno = EFTYPE;
  296. goto err;
  297. err: if (t) {
  298. if (t->bt_dbp)
  299. free(t->bt_dbp);
  300. if (t->bt_fd != -1)
  301. (void)close(t->bt_fd);
  302. free(t);
  303. }
  304. return (NULL);
  305. }
  306. /*
  307. * NROOT -- Create the root of a new tree.
  308. *
  309. * Parameters:
  310. * t: tree
  311. *
  312. * Returns:
  313. * RET_ERROR, RET_SUCCESS
  314. */
  315. static int
  316. nroot(t)
  317. BTREE *t;
  318. {
  319. PAGE *meta, *root;
  320. pgno_t npg;
  321. if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
  322. mpool_put(t->bt_mp, meta, 0);
  323. return (RET_SUCCESS);
  324. }
  325. if (errno != EINVAL) /* It's OK to not exist. */
  326. return (RET_ERROR);
  327. errno = 0;
  328. if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
  329. return (RET_ERROR);
  330. if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
  331. return (RET_ERROR);
  332. if (npg != P_ROOT)
  333. return (RET_ERROR);
  334. root->pgno = npg;
  335. root->prevpg = root->nextpg = P_INVALID;
  336. root->lower = BTDATAOFF;
  337. root->upper = t->bt_psize;
  338. root->flags = P_BLEAF;
  339. memset(meta, 0, t->bt_psize);
  340. mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
  341. mpool_put(t->bt_mp, root, MPOOL_DIRTY);
  342. return (RET_SUCCESS);
  343. }
  344. static int
  345. tmp()
  346. {
  347. sigset_t set, oset;
  348. int fd;
  349. const char *envtmp;
  350. char *path;
  351. static const char fmt[] = "%s/bt.XXXXXX";
  352. size_t n;
  353. envtmp = getenv("TMPDIR");
  354. if (!envtmp)
  355. envtmp = "/tmp";
  356. n = strlen (envtmp) + sizeof fmt;
  357. #ifdef __GNUC__
  358. path = __builtin_alloca(n);
  359. #else
  360. path = malloc(n);
  361. #endif
  362. (void)snprintf(path, n, fmt, envtmp ? envtmp : "/tmp");
  363. (void)sigfillset(&set);
  364. (void)sigprocmask(SIG_BLOCK, &set, &oset);
  365. if ((fd = mkstemp(path)) != -1)
  366. (void)unlink(path);
  367. (void)sigprocmask(SIG_SETMASK, &oset, NULL);
  368. #ifndef __GNUC__
  369. free(path);
  370. #endif
  371. return(fd);
  372. }
  373. static int
  374. byteorder()
  375. {
  376. u_int32_t x;
  377. u_char *p;
  378. x = 0x01020304;
  379. p = (u_char *)&x;
  380. switch (*p) {
  381. case 1:
  382. return (BIG_ENDIAN);
  383. case 4:
  384. return (LITTLE_ENDIAN);
  385. default:
  386. return (0);
  387. }
  388. }
  389. int
  390. __bt_fd(dbp)
  391. const DB *dbp;
  392. {
  393. BTREE *t;
  394. t = dbp->internal;
  395. /* Toss any page pinned across calls. */
  396. if (t->bt_pinned != NULL) {
  397. mpool_put(t->bt_mp, t->bt_pinned, 0);
  398. t->bt_pinned = NULL;
  399. }
  400. /* In-memory database can't have a file descriptor. */
  401. if (F_ISSET(t, B_INMEM)) {
  402. errno = ENOENT;
  403. return (-1);
  404. }
  405. return (t->bt_fd);
  406. }