bt_close.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_close.c 8.7 (Berkeley) 8/17/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. #include <sys/param.h>
  40. #include <errno.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include "../include/db.h"
  46. #include "btree.h"
  47. static int bt_meta __P((BTREE *));
  48. /*
  49. * BT_CLOSE -- Close a btree.
  50. *
  51. * Parameters:
  52. * dbp: pointer to access method
  53. *
  54. * Returns:
  55. * RET_ERROR, RET_SUCCESS
  56. */
  57. int
  58. __bt_close(dbp)
  59. DB *dbp;
  60. {
  61. BTREE *t;
  62. int fd;
  63. t = dbp->internal;
  64. /* Toss any page pinned across calls. */
  65. if (t->bt_pinned != NULL) {
  66. mpool_put(t->bt_mp, t->bt_pinned, 0);
  67. t->bt_pinned = NULL;
  68. }
  69. /* Sync the tree. */
  70. if (__bt_sync(dbp, 0) == RET_ERROR)
  71. return (RET_ERROR);
  72. /* Close the memory pool. */
  73. if (mpool_close(t->bt_mp) == RET_ERROR)
  74. return (RET_ERROR);
  75. /* Free random memory. */
  76. if (t->bt_cursor.key.data != NULL) {
  77. free(t->bt_cursor.key.data);
  78. t->bt_cursor.key.size = 0;
  79. t->bt_cursor.key.data = NULL;
  80. }
  81. if (t->bt_rkey.data) {
  82. free(t->bt_rkey.data);
  83. t->bt_rkey.size = 0;
  84. t->bt_rkey.data = NULL;
  85. }
  86. if (t->bt_rdata.data) {
  87. free(t->bt_rdata.data);
  88. t->bt_rdata.size = 0;
  89. t->bt_rdata.data = NULL;
  90. }
  91. fd = t->bt_fd;
  92. free(t);
  93. free(dbp);
  94. return (close(fd) ? RET_ERROR : RET_SUCCESS);
  95. }
  96. /*
  97. * BT_SYNC -- sync the btree to disk.
  98. *
  99. * Parameters:
  100. * dbp: pointer to access method
  101. *
  102. * Returns:
  103. * RET_SUCCESS, RET_ERROR.
  104. */
  105. int
  106. __bt_sync(dbp, flags)
  107. const DB *dbp;
  108. u_int flags;
  109. {
  110. BTREE *t;
  111. int status;
  112. t = dbp->internal;
  113. /* Toss any page pinned across calls. */
  114. if (t->bt_pinned != NULL) {
  115. mpool_put(t->bt_mp, t->bt_pinned, 0);
  116. t->bt_pinned = NULL;
  117. }
  118. /* Sync doesn't currently take any flags. */
  119. if (flags != 0) {
  120. errno = EINVAL;
  121. return (RET_ERROR);
  122. }
  123. if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
  124. return (RET_SUCCESS);
  125. if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
  126. return (RET_ERROR);
  127. if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
  128. F_CLR(t, B_MODIFIED);
  129. return (status);
  130. }
  131. /*
  132. * BT_META -- write the tree meta data to disk.
  133. *
  134. * Parameters:
  135. * t: tree
  136. *
  137. * Returns:
  138. * RET_ERROR, RET_SUCCESS
  139. */
  140. static int
  141. bt_meta(t)
  142. BTREE *t;
  143. {
  144. BTMETA m;
  145. void *p;
  146. if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
  147. return (RET_ERROR);
  148. /* Fill in metadata. */
  149. m.magic = BTREEMAGIC;
  150. m.version = BTREEVERSION;
  151. m.psize = t->bt_psize;
  152. m.free = t->bt_free;
  153. m.nrecs = t->bt_nrecs;
  154. m.flags = F_ISSET(t, SAVEMETA);
  155. memmove(p, &m, sizeof(BTMETA));
  156. mpool_put(t->bt_mp, p, MPOOL_DIRTY);
  157. return (RET_SUCCESS);
  158. }