xfs_ialloc_btree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_btree.h"
  34. #include "xfs_ialloc.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_error.h"
  37. STATIC int
  38. xfs_inobt_get_minrecs(
  39. struct xfs_btree_cur *cur,
  40. int level)
  41. {
  42. return cur->bc_mp->m_inobt_mnr[level != 0];
  43. }
  44. STATIC struct xfs_btree_cur *
  45. xfs_inobt_dup_cursor(
  46. struct xfs_btree_cur *cur)
  47. {
  48. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  49. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  50. }
  51. STATIC void
  52. xfs_inobt_set_root(
  53. struct xfs_btree_cur *cur,
  54. union xfs_btree_ptr *nptr,
  55. int inc) /* level change */
  56. {
  57. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  58. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  59. agi->agi_root = nptr->s;
  60. be32_add_cpu(&agi->agi_level, inc);
  61. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  62. }
  63. STATIC int
  64. xfs_inobt_alloc_block(
  65. struct xfs_btree_cur *cur,
  66. union xfs_btree_ptr *start,
  67. union xfs_btree_ptr *new,
  68. int length,
  69. int *stat)
  70. {
  71. xfs_alloc_arg_t args; /* block allocation args */
  72. int error; /* error return value */
  73. xfs_agblock_t sbno = be32_to_cpu(start->s);
  74. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  75. memset(&args, 0, sizeof(args));
  76. args.tp = cur->bc_tp;
  77. args.mp = cur->bc_mp;
  78. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  79. args.minlen = 1;
  80. args.maxlen = 1;
  81. args.prod = 1;
  82. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  83. error = xfs_alloc_vextent(&args);
  84. if (error) {
  85. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  86. return error;
  87. }
  88. if (args.fsbno == NULLFSBLOCK) {
  89. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  90. *stat = 0;
  91. return 0;
  92. }
  93. ASSERT(args.len == 1);
  94. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  95. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  96. *stat = 1;
  97. return 0;
  98. }
  99. STATIC int
  100. xfs_inobt_free_block(
  101. struct xfs_btree_cur *cur,
  102. struct xfs_buf *bp)
  103. {
  104. xfs_fsblock_t fsbno;
  105. int error;
  106. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  107. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  108. if (error)
  109. return error;
  110. xfs_trans_binval(cur->bc_tp, bp);
  111. return error;
  112. }
  113. STATIC int
  114. xfs_inobt_get_maxrecs(
  115. struct xfs_btree_cur *cur,
  116. int level)
  117. {
  118. return cur->bc_mp->m_inobt_mxr[level != 0];
  119. }
  120. STATIC void
  121. xfs_inobt_init_key_from_rec(
  122. union xfs_btree_key *key,
  123. union xfs_btree_rec *rec)
  124. {
  125. key->inobt.ir_startino = rec->inobt.ir_startino;
  126. }
  127. STATIC void
  128. xfs_inobt_init_rec_from_key(
  129. union xfs_btree_key *key,
  130. union xfs_btree_rec *rec)
  131. {
  132. rec->inobt.ir_startino = key->inobt.ir_startino;
  133. }
  134. STATIC void
  135. xfs_inobt_init_rec_from_cur(
  136. struct xfs_btree_cur *cur,
  137. union xfs_btree_rec *rec)
  138. {
  139. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  140. rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  141. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  142. }
  143. /*
  144. * initial value of ptr for lookup
  145. */
  146. STATIC void
  147. xfs_inobt_init_ptr_from_cur(
  148. struct xfs_btree_cur *cur,
  149. union xfs_btree_ptr *ptr)
  150. {
  151. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  152. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  153. ptr->s = agi->agi_root;
  154. }
  155. STATIC __int64_t
  156. xfs_inobt_key_diff(
  157. struct xfs_btree_cur *cur,
  158. union xfs_btree_key *key)
  159. {
  160. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  161. cur->bc_rec.i.ir_startino;
  162. }
  163. #ifdef DEBUG
  164. STATIC int
  165. xfs_inobt_keys_inorder(
  166. struct xfs_btree_cur *cur,
  167. union xfs_btree_key *k1,
  168. union xfs_btree_key *k2)
  169. {
  170. return be32_to_cpu(k1->inobt.ir_startino) <
  171. be32_to_cpu(k2->inobt.ir_startino);
  172. }
  173. STATIC int
  174. xfs_inobt_recs_inorder(
  175. struct xfs_btree_cur *cur,
  176. union xfs_btree_rec *r1,
  177. union xfs_btree_rec *r2)
  178. {
  179. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  180. be32_to_cpu(r2->inobt.ir_startino);
  181. }
  182. #endif /* DEBUG */
  183. static const struct xfs_btree_ops xfs_inobt_ops = {
  184. .rec_len = sizeof(xfs_inobt_rec_t),
  185. .key_len = sizeof(xfs_inobt_key_t),
  186. .dup_cursor = xfs_inobt_dup_cursor,
  187. .set_root = xfs_inobt_set_root,
  188. .alloc_block = xfs_inobt_alloc_block,
  189. .free_block = xfs_inobt_free_block,
  190. .get_minrecs = xfs_inobt_get_minrecs,
  191. .get_maxrecs = xfs_inobt_get_maxrecs,
  192. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  193. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  194. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  195. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  196. .key_diff = xfs_inobt_key_diff,
  197. #ifdef DEBUG
  198. .keys_inorder = xfs_inobt_keys_inorder,
  199. .recs_inorder = xfs_inobt_recs_inorder,
  200. #endif
  201. };
  202. /*
  203. * Allocate a new inode btree cursor.
  204. */
  205. struct xfs_btree_cur * /* new inode btree cursor */
  206. xfs_inobt_init_cursor(
  207. struct xfs_mount *mp, /* file system mount point */
  208. struct xfs_trans *tp, /* transaction pointer */
  209. struct xfs_buf *agbp, /* buffer for agi structure */
  210. xfs_agnumber_t agno) /* allocation group number */
  211. {
  212. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  213. struct xfs_btree_cur *cur;
  214. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  215. cur->bc_tp = tp;
  216. cur->bc_mp = mp;
  217. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  218. cur->bc_btnum = XFS_BTNUM_INO;
  219. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  220. cur->bc_ops = &xfs_inobt_ops;
  221. cur->bc_private.a.agbp = agbp;
  222. cur->bc_private.a.agno = agno;
  223. return cur;
  224. }
  225. /*
  226. * Calculate number of records in an inobt btree block.
  227. */
  228. int
  229. xfs_inobt_maxrecs(
  230. struct xfs_mount *mp,
  231. int blocklen,
  232. int leaf)
  233. {
  234. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  235. if (leaf)
  236. return blocklen / sizeof(xfs_inobt_rec_t);
  237. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  238. }