xfs_ialloc_btree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_alloc_btree.h"
  29. #include "xfs_ialloc_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_ialloc.h"
  34. #include "xfs_alloc.h"
  35. #include "xfs_error.h"
  36. STATIC int
  37. xfs_inobt_get_minrecs(
  38. struct xfs_btree_cur *cur,
  39. int level)
  40. {
  41. return cur->bc_mp->m_inobt_mnr[level != 0];
  42. }
  43. STATIC struct xfs_btree_cur *
  44. xfs_inobt_dup_cursor(
  45. struct xfs_btree_cur *cur)
  46. {
  47. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  48. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  49. }
  50. STATIC void
  51. xfs_inobt_set_root(
  52. struct xfs_btree_cur *cur,
  53. union xfs_btree_ptr *nptr,
  54. int inc) /* level change */
  55. {
  56. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  57. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  58. agi->agi_root = nptr->s;
  59. be32_add_cpu(&agi->agi_level, inc);
  60. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  61. }
  62. STATIC int
  63. xfs_inobt_alloc_block(
  64. struct xfs_btree_cur *cur,
  65. union xfs_btree_ptr *start,
  66. union xfs_btree_ptr *new,
  67. int length,
  68. int *stat)
  69. {
  70. xfs_alloc_arg_t args; /* block allocation args */
  71. int error; /* error return value */
  72. xfs_agblock_t sbno = be32_to_cpu(start->s);
  73. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  74. memset(&args, 0, sizeof(args));
  75. args.tp = cur->bc_tp;
  76. args.mp = cur->bc_mp;
  77. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  78. args.minlen = 1;
  79. args.maxlen = 1;
  80. args.prod = 1;
  81. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  82. error = xfs_alloc_vextent(&args);
  83. if (error) {
  84. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  85. return error;
  86. }
  87. if (args.fsbno == NULLFSBLOCK) {
  88. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  89. *stat = 0;
  90. return 0;
  91. }
  92. ASSERT(args.len == 1);
  93. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  94. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  95. *stat = 1;
  96. return 0;
  97. }
  98. STATIC int
  99. xfs_inobt_free_block(
  100. struct xfs_btree_cur *cur,
  101. struct xfs_buf *bp)
  102. {
  103. xfs_fsblock_t fsbno;
  104. int error;
  105. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  106. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  107. if (error)
  108. return error;
  109. xfs_trans_binval(cur->bc_tp, bp);
  110. return error;
  111. }
  112. STATIC int
  113. xfs_inobt_get_maxrecs(
  114. struct xfs_btree_cur *cur,
  115. int level)
  116. {
  117. return cur->bc_mp->m_inobt_mxr[level != 0];
  118. }
  119. STATIC void
  120. xfs_inobt_init_key_from_rec(
  121. union xfs_btree_key *key,
  122. union xfs_btree_rec *rec)
  123. {
  124. key->inobt.ir_startino = rec->inobt.ir_startino;
  125. }
  126. STATIC void
  127. xfs_inobt_init_rec_from_key(
  128. union xfs_btree_key *key,
  129. union xfs_btree_rec *rec)
  130. {
  131. rec->inobt.ir_startino = key->inobt.ir_startino;
  132. }
  133. STATIC void
  134. xfs_inobt_init_rec_from_cur(
  135. struct xfs_btree_cur *cur,
  136. union xfs_btree_rec *rec)
  137. {
  138. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  139. rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  140. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  141. }
  142. /*
  143. * initial value of ptr for lookup
  144. */
  145. STATIC void
  146. xfs_inobt_init_ptr_from_cur(
  147. struct xfs_btree_cur *cur,
  148. union xfs_btree_ptr *ptr)
  149. {
  150. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  151. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  152. ptr->s = agi->agi_root;
  153. }
  154. STATIC __int64_t
  155. xfs_inobt_key_diff(
  156. struct xfs_btree_cur *cur,
  157. union xfs_btree_key *key)
  158. {
  159. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  160. cur->bc_rec.i.ir_startino;
  161. }
  162. #ifdef DEBUG
  163. STATIC int
  164. xfs_inobt_keys_inorder(
  165. struct xfs_btree_cur *cur,
  166. union xfs_btree_key *k1,
  167. union xfs_btree_key *k2)
  168. {
  169. return be32_to_cpu(k1->inobt.ir_startino) <
  170. be32_to_cpu(k2->inobt.ir_startino);
  171. }
  172. STATIC int
  173. xfs_inobt_recs_inorder(
  174. struct xfs_btree_cur *cur,
  175. union xfs_btree_rec *r1,
  176. union xfs_btree_rec *r2)
  177. {
  178. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  179. be32_to_cpu(r2->inobt.ir_startino);
  180. }
  181. #endif /* DEBUG */
  182. static const struct xfs_btree_ops xfs_inobt_ops = {
  183. .rec_len = sizeof(xfs_inobt_rec_t),
  184. .key_len = sizeof(xfs_inobt_key_t),
  185. .dup_cursor = xfs_inobt_dup_cursor,
  186. .set_root = xfs_inobt_set_root,
  187. .alloc_block = xfs_inobt_alloc_block,
  188. .free_block = xfs_inobt_free_block,
  189. .get_minrecs = xfs_inobt_get_minrecs,
  190. .get_maxrecs = xfs_inobt_get_maxrecs,
  191. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  192. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  193. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  194. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  195. .key_diff = xfs_inobt_key_diff,
  196. #ifdef DEBUG
  197. .keys_inorder = xfs_inobt_keys_inorder,
  198. .recs_inorder = xfs_inobt_recs_inorder,
  199. #endif
  200. };
  201. /*
  202. * Allocate a new inode btree cursor.
  203. */
  204. struct xfs_btree_cur * /* new inode btree cursor */
  205. xfs_inobt_init_cursor(
  206. struct xfs_mount *mp, /* file system mount point */
  207. struct xfs_trans *tp, /* transaction pointer */
  208. struct xfs_buf *agbp, /* buffer for agi structure */
  209. xfs_agnumber_t agno) /* allocation group number */
  210. {
  211. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  212. struct xfs_btree_cur *cur;
  213. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  214. cur->bc_tp = tp;
  215. cur->bc_mp = mp;
  216. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  217. cur->bc_btnum = XFS_BTNUM_INO;
  218. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  219. cur->bc_ops = &xfs_inobt_ops;
  220. cur->bc_private.a.agbp = agbp;
  221. cur->bc_private.a.agno = agno;
  222. return cur;
  223. }
  224. /*
  225. * Calculate number of records in an inobt btree block.
  226. */
  227. int
  228. xfs_inobt_maxrecs(
  229. struct xfs_mount *mp,
  230. int blocklen,
  231. int leaf)
  232. {
  233. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  234. if (leaf)
  235. return blocklen / sizeof(xfs_inobt_rec_t);
  236. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  237. }