xfs_ialloc_btree.c 12 KB

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