xfs_ialloc_btree.c 11 KB

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