bt_open.c 12 KB

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