xfs_ialloc_btree.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include "xfs_trace.h"
  37. #include "xfs_cksum.h"
  38. STATIC int
  39. xfs_inobt_get_minrecs(
  40. struct xfs_btree_cur *cur,
  41. int level)
  42. {
  43. return cur->bc_mp->m_inobt_mnr[level != 0];
  44. }
  45. STATIC struct xfs_btree_cur *
  46. xfs_inobt_dup_cursor(
  47. struct xfs_btree_cur *cur)
  48. {
  49. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  50. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  51. }
  52. STATIC void
  53. xfs_inobt_set_root(
  54. struct xfs_btree_cur *cur,
  55. union xfs_btree_ptr *nptr,
  56. int inc) /* level change */
  57. {
  58. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  59. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  60. agi->agi_root = nptr->s;
  61. be32_add_cpu(&agi->agi_level, inc);
  62. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  63. }
  64. STATIC int
  65. xfs_inobt_alloc_block(
  66. struct xfs_btree_cur *cur,
  67. union xfs_btree_ptr *start,
  68. union xfs_btree_ptr *new,
  69. int length,
  70. int *stat)
  71. {
  72. xfs_alloc_arg_t args; /* block allocation args */
  73. int error; /* error return value */
  74. xfs_agblock_t sbno = be32_to_cpu(start->s);
  75. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  76. memset(&args, 0, sizeof(args));
  77. args.tp = cur->bc_tp;
  78. args.mp = cur->bc_mp;
  79. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  80. args.minlen = 1;
  81. args.maxlen = 1;
  82. args.prod = 1;
  83. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  84. error = xfs_alloc_vextent(&args);
  85. if (error) {
  86. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  87. return error;
  88. }
  89. if (args.fsbno == NULLFSBLOCK) {
  90. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  91. *stat = 0;
  92. return 0;
  93. }
  94. ASSERT(args.len == 1);
  95. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  96. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  97. *stat = 1;
  98. return 0;
  99. }
  100. STATIC int
  101. xfs_inobt_free_block(
  102. struct xfs_btree_cur *cur,
  103. struct xfs_buf *bp)
  104. {
  105. xfs_fsblock_t fsbno;
  106. int error;
  107. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  108. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  109. if (error)
  110. return error;
  111. xfs_trans_binval(cur->bc_tp, bp);
  112. return error;
  113. }
  114. STATIC int
  115. xfs_inobt_get_maxrecs(
  116. struct xfs_btree_cur *cur,
  117. int level)
  118. {
  119. return cur->bc_mp->m_inobt_mxr[level != 0];
  120. }
  121. STATIC void
  122. xfs_inobt_init_key_from_rec(
  123. union xfs_btree_key *key,
  124. union xfs_btree_rec *rec)
  125. {
  126. key->inobt.ir_startino = rec->inobt.ir_startino;
  127. }
  128. STATIC void
  129. xfs_inobt_init_rec_from_key(
  130. union xfs_btree_key *key,
  131. union xfs_btree_rec *rec)
  132. {
  133. rec->inobt.ir_startino = key->inobt.ir_startino;
  134. }
  135. STATIC void
  136. xfs_inobt_init_rec_from_cur(
  137. struct xfs_btree_cur *cur,
  138. union xfs_btree_rec *rec)
  139. {
  140. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  141. rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  142. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  143. }
  144. /*
  145. * initial value of ptr for lookup
  146. */
  147. STATIC void
  148. xfs_inobt_init_ptr_from_cur(
  149. struct xfs_btree_cur *cur,
  150. union xfs_btree_ptr *ptr)
  151. {
  152. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  153. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  154. ptr->s = agi->agi_root;
  155. }
  156. STATIC __int64_t
  157. xfs_inobt_key_diff(
  158. struct xfs_btree_cur *cur,
  159. union xfs_btree_key *key)
  160. {
  161. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  162. cur->bc_rec.i.ir_startino;
  163. }
  164. static int
  165. xfs_inobt_verify(
  166. struct xfs_buf *bp)
  167. {
  168. struct xfs_mount *mp = bp->b_target->bt_mount;
  169. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  170. struct xfs_perag *pag = bp->b_pag;
  171. unsigned int level;
  172. /*
  173. * During growfs operations, we can't verify the exact owner as the
  174. * perag is not fully initialised and hence not attached to the buffer.
  175. *
  176. * Similarly, during log recovery we will have a perag structure
  177. * attached, but the agi information will not yet have been initialised
  178. * from the on disk AGI. We don't currently use any of this information,
  179. * but beware of the landmine (i.e. need to check pag->pagi_init) if we
  180. * ever do.
  181. */
  182. switch (block->bb_magic) {
  183. case cpu_to_be32(XFS_IBT_CRC_MAGIC):
  184. if (!xfs_sb_version_hascrc(&mp->m_sb))
  185. return false;
  186. if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
  187. return false;
  188. if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
  189. return false;
  190. if (pag &&
  191. be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
  192. return false;
  193. /* fall through */
  194. case cpu_to_be32(XFS_IBT_MAGIC):
  195. break;
  196. default:
  197. return 0;
  198. }
  199. /* numrecs and level verification */
  200. level = be16_to_cpu(block->bb_level);
  201. if (level >= mp->m_in_maxlevels)
  202. return false;
  203. if (be16_to_cpu(block->bb_numrecs) > mp->m_inobt_mxr[level != 0])
  204. return false;
  205. /* sibling pointer verification */
  206. if (!block->bb_u.s.bb_leftsib ||
  207. (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
  208. block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
  209. return false;
  210. if (!block->bb_u.s.bb_rightsib ||
  211. (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
  212. block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
  213. return false;
  214. return true;
  215. }
  216. static void
  217. xfs_inobt_read_verify(
  218. struct xfs_buf *bp)
  219. {
  220. if (!(xfs_btree_sblock_verify_crc(bp) &&
  221. xfs_inobt_verify(bp))) {
  222. trace_xfs_btree_corrupt(bp, _RET_IP_);
  223. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
  224. bp->b_target->bt_mount, bp->b_addr);
  225. xfs_buf_ioerror(bp, EFSCORRUPTED);
  226. }
  227. }
  228. static void
  229. xfs_inobt_write_verify(
  230. struct xfs_buf *bp)
  231. {
  232. if (!xfs_inobt_verify(bp)) {
  233. trace_xfs_btree_corrupt(bp, _RET_IP_);
  234. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
  235. bp->b_target->bt_mount, bp->b_addr);
  236. xfs_buf_ioerror(bp, EFSCORRUPTED);
  237. }
  238. xfs_btree_sblock_calc_crc(bp);
  239. }
  240. const struct xfs_buf_ops xfs_inobt_buf_ops = {
  241. .verify_read = xfs_inobt_read_verify,
  242. .verify_write = xfs_inobt_write_verify,
  243. };
  244. #ifdef DEBUG
  245. STATIC int
  246. xfs_inobt_keys_inorder(
  247. struct xfs_btree_cur *cur,
  248. union xfs_btree_key *k1,
  249. union xfs_btree_key *k2)
  250. {
  251. return be32_to_cpu(k1->inobt.ir_startino) <
  252. be32_to_cpu(k2->inobt.ir_startino);
  253. }
  254. STATIC int
  255. xfs_inobt_recs_inorder(
  256. struct xfs_btree_cur *cur,
  257. union xfs_btree_rec *r1,
  258. union xfs_btree_rec *r2)
  259. {
  260. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  261. be32_to_cpu(r2->inobt.ir_startino);
  262. }
  263. #endif /* DEBUG */
  264. static const struct xfs_btree_ops xfs_inobt_ops = {
  265. .rec_len = sizeof(xfs_inobt_rec_t),
  266. .key_len = sizeof(xfs_inobt_key_t),
  267. .dup_cursor = xfs_inobt_dup_cursor,
  268. .set_root = xfs_inobt_set_root,
  269. .alloc_block = xfs_inobt_alloc_block,
  270. .free_block = xfs_inobt_free_block,
  271. .get_minrecs = xfs_inobt_get_minrecs,
  272. .get_maxrecs = xfs_inobt_get_maxrecs,
  273. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  274. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  275. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  276. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  277. .key_diff = xfs_inobt_key_diff,
  278. .buf_ops = &xfs_inobt_buf_ops,
  279. #ifdef DEBUG
  280. .keys_inorder = xfs_inobt_keys_inorder,
  281. .recs_inorder = xfs_inobt_recs_inorder,
  282. #endif
  283. };
  284. /*
  285. * Allocate a new inode btree cursor.
  286. */
  287. struct xfs_btree_cur * /* new inode btree cursor */
  288. xfs_inobt_init_cursor(
  289. struct xfs_mount *mp, /* file system mount point */
  290. struct xfs_trans *tp, /* transaction pointer */
  291. struct xfs_buf *agbp, /* buffer for agi structure */
  292. xfs_agnumber_t agno) /* allocation group number */
  293. {
  294. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  295. struct xfs_btree_cur *cur;
  296. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  297. cur->bc_tp = tp;
  298. cur->bc_mp = mp;
  299. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  300. cur->bc_btnum = XFS_BTNUM_INO;
  301. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  302. cur->bc_ops = &xfs_inobt_ops;
  303. if (xfs_sb_version_hascrc(&mp->m_sb))
  304. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  305. cur->bc_private.a.agbp = agbp;
  306. cur->bc_private.a.agno = agno;
  307. return cur;
  308. }
  309. /*
  310. * Calculate number of records in an inobt btree block.
  311. */
  312. int
  313. xfs_inobt_maxrecs(
  314. struct xfs_mount *mp,
  315. int blocklen,
  316. int leaf)
  317. {
  318. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  319. if (leaf)
  320. return blocklen / sizeof(xfs_inobt_rec_t);
  321. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  322. }