xfs_ialloc_btree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_mount.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_btree.h"
  28. #include "xfs_ialloc.h"
  29. #include "xfs_ialloc_btree.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_cksum.h"
  34. #include "xfs_trans.h"
  35. STATIC int
  36. xfs_inobt_get_minrecs(
  37. struct xfs_btree_cur *cur,
  38. int level)
  39. {
  40. return cur->bc_mp->m_inobt_mnr[level != 0];
  41. }
  42. STATIC struct xfs_btree_cur *
  43. xfs_inobt_dup_cursor(
  44. struct xfs_btree_cur *cur)
  45. {
  46. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  47. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  48. cur->bc_btnum);
  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 void
  63. xfs_finobt_set_root(
  64. struct xfs_btree_cur *cur,
  65. union xfs_btree_ptr *nptr,
  66. int inc) /* level change */
  67. {
  68. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  69. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  70. agi->agi_free_root = nptr->s;
  71. be32_add_cpu(&agi->agi_free_level, inc);
  72. xfs_ialloc_log_agi(cur->bc_tp, agbp,
  73. XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL);
  74. }
  75. STATIC int
  76. xfs_inobt_alloc_block(
  77. struct xfs_btree_cur *cur,
  78. union xfs_btree_ptr *start,
  79. union xfs_btree_ptr *new,
  80. int *stat)
  81. {
  82. xfs_alloc_arg_t args; /* block allocation args */
  83. int error; /* error return value */
  84. xfs_agblock_t sbno = be32_to_cpu(start->s);
  85. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  86. memset(&args, 0, sizeof(args));
  87. args.tp = cur->bc_tp;
  88. args.mp = cur->bc_mp;
  89. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  90. args.minlen = 1;
  91. args.maxlen = 1;
  92. args.prod = 1;
  93. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  94. error = xfs_alloc_vextent(&args);
  95. if (error) {
  96. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  97. return error;
  98. }
  99. if (args.fsbno == NULLFSBLOCK) {
  100. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  101. *stat = 0;
  102. return 0;
  103. }
  104. ASSERT(args.len == 1);
  105. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  106. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  107. *stat = 1;
  108. return 0;
  109. }
  110. STATIC int
  111. xfs_inobt_free_block(
  112. struct xfs_btree_cur *cur,
  113. struct xfs_buf *bp)
  114. {
  115. xfs_fsblock_t fsbno;
  116. int error;
  117. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  118. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  119. if (error)
  120. return error;
  121. xfs_trans_binval(cur->bc_tp, bp);
  122. return error;
  123. }
  124. STATIC int
  125. xfs_inobt_get_maxrecs(
  126. struct xfs_btree_cur *cur,
  127. int level)
  128. {
  129. return cur->bc_mp->m_inobt_mxr[level != 0];
  130. }
  131. STATIC void
  132. xfs_inobt_init_key_from_rec(
  133. union xfs_btree_key *key,
  134. union xfs_btree_rec *rec)
  135. {
  136. key->inobt.ir_startino = rec->inobt.ir_startino;
  137. }
  138. STATIC void
  139. xfs_inobt_init_rec_from_key(
  140. union xfs_btree_key *key,
  141. union xfs_btree_rec *rec)
  142. {
  143. rec->inobt.ir_startino = key->inobt.ir_startino;
  144. }
  145. STATIC void
  146. xfs_inobt_init_rec_from_cur(
  147. struct xfs_btree_cur *cur,
  148. union xfs_btree_rec *rec)
  149. {
  150. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  151. if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
  152. rec->inobt.ir_u.sp.ir_holemask =
  153. cpu_to_be16(cur->bc_rec.i.ir_holemask);
  154. rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count;
  155. rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount;
  156. } else {
  157. /* ir_holemask/ir_count not supported on-disk */
  158. rec->inobt.ir_u.f.ir_freecount =
  159. cpu_to_be32(cur->bc_rec.i.ir_freecount);
  160. }
  161. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  162. }
  163. /*
  164. * initial value of ptr for lookup
  165. */
  166. STATIC void
  167. xfs_inobt_init_ptr_from_cur(
  168. struct xfs_btree_cur *cur,
  169. union xfs_btree_ptr *ptr)
  170. {
  171. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  172. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  173. ptr->s = agi->agi_root;
  174. }
  175. STATIC void
  176. xfs_finobt_init_ptr_from_cur(
  177. struct xfs_btree_cur *cur,
  178. union xfs_btree_ptr *ptr)
  179. {
  180. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  181. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  182. ptr->s = agi->agi_free_root;
  183. }
  184. STATIC __int64_t
  185. xfs_inobt_key_diff(
  186. struct xfs_btree_cur *cur,
  187. union xfs_btree_key *key)
  188. {
  189. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  190. cur->bc_rec.i.ir_startino;
  191. }
  192. static int
  193. xfs_inobt_verify(
  194. struct xfs_buf *bp)
  195. {
  196. struct xfs_mount *mp = bp->b_target->bt_mount;
  197. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  198. struct xfs_perag *pag = bp->b_pag;
  199. unsigned int level;
  200. /*
  201. * During growfs operations, we can't verify the exact owner as the
  202. * perag is not fully initialised and hence not attached to the buffer.
  203. *
  204. * Similarly, during log recovery we will have a perag structure
  205. * attached, but the agi information will not yet have been initialised
  206. * from the on disk AGI. We don't currently use any of this information,
  207. * but beware of the landmine (i.e. need to check pag->pagi_init) if we
  208. * ever do.
  209. */
  210. switch (block->bb_magic) {
  211. case cpu_to_be32(XFS_IBT_CRC_MAGIC):
  212. case cpu_to_be32(XFS_FIBT_CRC_MAGIC):
  213. if (!xfs_sb_version_hascrc(&mp->m_sb))
  214. return false;
  215. if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
  216. return false;
  217. if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
  218. return false;
  219. if (pag &&
  220. be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
  221. return false;
  222. /* fall through */
  223. case cpu_to_be32(XFS_IBT_MAGIC):
  224. case cpu_to_be32(XFS_FIBT_MAGIC):
  225. break;
  226. default:
  227. return 0;
  228. }
  229. /* numrecs and level verification */
  230. level = be16_to_cpu(block->bb_level);
  231. if (level >= mp->m_in_maxlevels)
  232. return false;
  233. if (be16_to_cpu(block->bb_numrecs) > mp->m_inobt_mxr[level != 0])
  234. return false;
  235. /* sibling pointer verification */
  236. if (!block->bb_u.s.bb_leftsib ||
  237. (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
  238. block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
  239. return false;
  240. if (!block->bb_u.s.bb_rightsib ||
  241. (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
  242. block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
  243. return false;
  244. return true;
  245. }
  246. static void
  247. xfs_inobt_read_verify(
  248. struct xfs_buf *bp)
  249. {
  250. if (!xfs_btree_sblock_verify_crc(bp))
  251. xfs_buf_ioerror(bp, -EFSBADCRC);
  252. else if (!xfs_inobt_verify(bp))
  253. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  254. if (bp->b_error) {
  255. trace_xfs_btree_corrupt(bp, _RET_IP_);
  256. xfs_verifier_error(bp);
  257. }
  258. }
  259. static void
  260. xfs_inobt_write_verify(
  261. struct xfs_buf *bp)
  262. {
  263. if (!xfs_inobt_verify(bp)) {
  264. trace_xfs_btree_corrupt(bp, _RET_IP_);
  265. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  266. xfs_verifier_error(bp);
  267. return;
  268. }
  269. xfs_btree_sblock_calc_crc(bp);
  270. }
  271. const struct xfs_buf_ops xfs_inobt_buf_ops = {
  272. .name = "xfs_inobt",
  273. .verify_read = xfs_inobt_read_verify,
  274. .verify_write = xfs_inobt_write_verify,
  275. };
  276. #if defined(DEBUG) || defined(XFS_WARN)
  277. STATIC int
  278. xfs_inobt_keys_inorder(
  279. struct xfs_btree_cur *cur,
  280. union xfs_btree_key *k1,
  281. union xfs_btree_key *k2)
  282. {
  283. return be32_to_cpu(k1->inobt.ir_startino) <
  284. be32_to_cpu(k2->inobt.ir_startino);
  285. }
  286. STATIC int
  287. xfs_inobt_recs_inorder(
  288. struct xfs_btree_cur *cur,
  289. union xfs_btree_rec *r1,
  290. union xfs_btree_rec *r2)
  291. {
  292. return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
  293. be32_to_cpu(r2->inobt.ir_startino);
  294. }
  295. #endif /* DEBUG */
  296. static const struct xfs_btree_ops xfs_inobt_ops = {
  297. .rec_len = sizeof(xfs_inobt_rec_t),
  298. .key_len = sizeof(xfs_inobt_key_t),
  299. .dup_cursor = xfs_inobt_dup_cursor,
  300. .set_root = xfs_inobt_set_root,
  301. .alloc_block = xfs_inobt_alloc_block,
  302. .free_block = xfs_inobt_free_block,
  303. .get_minrecs = xfs_inobt_get_minrecs,
  304. .get_maxrecs = xfs_inobt_get_maxrecs,
  305. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  306. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  307. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  308. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  309. .key_diff = xfs_inobt_key_diff,
  310. .buf_ops = &xfs_inobt_buf_ops,
  311. #if defined(DEBUG) || defined(XFS_WARN)
  312. .keys_inorder = xfs_inobt_keys_inorder,
  313. .recs_inorder = xfs_inobt_recs_inorder,
  314. #endif
  315. };
  316. static const struct xfs_btree_ops xfs_finobt_ops = {
  317. .rec_len = sizeof(xfs_inobt_rec_t),
  318. .key_len = sizeof(xfs_inobt_key_t),
  319. .dup_cursor = xfs_inobt_dup_cursor,
  320. .set_root = xfs_finobt_set_root,
  321. .alloc_block = xfs_inobt_alloc_block,
  322. .free_block = xfs_inobt_free_block,
  323. .get_minrecs = xfs_inobt_get_minrecs,
  324. .get_maxrecs = xfs_inobt_get_maxrecs,
  325. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  326. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  327. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  328. .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur,
  329. .key_diff = xfs_inobt_key_diff,
  330. .buf_ops = &xfs_inobt_buf_ops,
  331. #if defined(DEBUG) || defined(XFS_WARN)
  332. .keys_inorder = xfs_inobt_keys_inorder,
  333. .recs_inorder = xfs_inobt_recs_inorder,
  334. #endif
  335. };
  336. /*
  337. * Allocate a new inode btree cursor.
  338. */
  339. struct xfs_btree_cur * /* new inode btree cursor */
  340. xfs_inobt_init_cursor(
  341. struct xfs_mount *mp, /* file system mount point */
  342. struct xfs_trans *tp, /* transaction pointer */
  343. struct xfs_buf *agbp, /* buffer for agi structure */
  344. xfs_agnumber_t agno, /* allocation group number */
  345. xfs_btnum_t btnum) /* ialloc or free ino btree */
  346. {
  347. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  348. struct xfs_btree_cur *cur;
  349. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  350. cur->bc_tp = tp;
  351. cur->bc_mp = mp;
  352. cur->bc_btnum = btnum;
  353. if (btnum == XFS_BTNUM_INO) {
  354. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  355. cur->bc_ops = &xfs_inobt_ops;
  356. } else {
  357. cur->bc_nlevels = be32_to_cpu(agi->agi_free_level);
  358. cur->bc_ops = &xfs_finobt_ops;
  359. }
  360. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  361. if (xfs_sb_version_hascrc(&mp->m_sb))
  362. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  363. cur->bc_private.a.agbp = agbp;
  364. cur->bc_private.a.agno = agno;
  365. return cur;
  366. }
  367. /*
  368. * Calculate number of records in an inobt btree block.
  369. */
  370. int
  371. xfs_inobt_maxrecs(
  372. struct xfs_mount *mp,
  373. int blocklen,
  374. int leaf)
  375. {
  376. blocklen -= XFS_INOBT_BLOCK_LEN(mp);
  377. if (leaf)
  378. return blocklen / sizeof(xfs_inobt_rec_t);
  379. return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
  380. }
  381. /*
  382. * Convert the inode record holemask to an inode allocation bitmap. The inode
  383. * allocation bitmap is inode granularity and specifies whether an inode is
  384. * physically allocated on disk (not whether the inode is considered allocated
  385. * or free by the fs).
  386. *
  387. * A bit value of 1 means the inode is allocated, a value of 0 means it is free.
  388. */
  389. uint64_t
  390. xfs_inobt_irec_to_allocmask(
  391. struct xfs_inobt_rec_incore *rec)
  392. {
  393. uint64_t bitmap = 0;
  394. uint64_t inodespbit;
  395. int nextbit;
  396. uint allocbitmap;
  397. /*
  398. * The holemask has 16-bits for a 64 inode record. Therefore each
  399. * holemask bit represents multiple inodes. Create a mask of bits to set
  400. * in the allocmask for each holemask bit.
  401. */
  402. inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1;
  403. /*
  404. * Allocated inodes are represented by 0 bits in holemask. Invert the 0
  405. * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask
  406. * anything beyond the 16 holemask bits since this casts to a larger
  407. * type.
  408. */
  409. allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1);
  410. /*
  411. * allocbitmap is the inverted holemask so every set bit represents
  412. * allocated inodes. To expand from 16-bit holemask granularity to
  413. * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target
  414. * bitmap for every holemask bit.
  415. */
  416. nextbit = xfs_next_bit(&allocbitmap, 1, 0);
  417. while (nextbit != -1) {
  418. ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY));
  419. bitmap |= (inodespbit <<
  420. (nextbit * XFS_INODES_PER_HOLEMASK_BIT));
  421. nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1);
  422. }
  423. return bitmap;
  424. }
  425. #if defined(DEBUG) || defined(XFS_WARN)
  426. /*
  427. * Verify that an in-core inode record has a valid inode count.
  428. */
  429. int
  430. xfs_inobt_rec_check_count(
  431. struct xfs_mount *mp,
  432. struct xfs_inobt_rec_incore *rec)
  433. {
  434. int inocount = 0;
  435. int nextbit = 0;
  436. uint64_t allocbmap;
  437. int wordsz;
  438. wordsz = sizeof(allocbmap) / sizeof(unsigned int);
  439. allocbmap = xfs_inobt_irec_to_allocmask(rec);
  440. nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit);
  441. while (nextbit != -1) {
  442. inocount++;
  443. nextbit = xfs_next_bit((uint *) &allocbmap, wordsz,
  444. nextbit + 1);
  445. }
  446. if (inocount != rec->ir_count)
  447. return -EFSCORRUPTED;
  448. return 0;
  449. }
  450. #endif /* DEBUG */