xfs_bmap_btree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (c) 2000-2003,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_defer.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_inode_item.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_btree.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_error.h"
  35. #include "xfs_quota.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_cksum.h"
  38. #include "xfs_rmap.h"
  39. /*
  40. * Determine the extent state.
  41. */
  42. /* ARGSUSED */
  43. STATIC xfs_exntst_t
  44. xfs_extent_state(
  45. xfs_filblks_t blks,
  46. int extent_flag)
  47. {
  48. if (extent_flag) {
  49. ASSERT(blks != 0); /* saved for DMIG */
  50. return XFS_EXT_UNWRITTEN;
  51. }
  52. return XFS_EXT_NORM;
  53. }
  54. /*
  55. * Convert on-disk form of btree root to in-memory form.
  56. */
  57. void
  58. xfs_bmdr_to_bmbt(
  59. struct xfs_inode *ip,
  60. xfs_bmdr_block_t *dblock,
  61. int dblocklen,
  62. struct xfs_btree_block *rblock,
  63. int rblocklen)
  64. {
  65. struct xfs_mount *mp = ip->i_mount;
  66. int dmxr;
  67. xfs_bmbt_key_t *fkp;
  68. __be64 *fpp;
  69. xfs_bmbt_key_t *tkp;
  70. __be64 *tpp;
  71. xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL,
  72. XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
  73. XFS_BTREE_LONG_PTRS);
  74. rblock->bb_level = dblock->bb_level;
  75. ASSERT(be16_to_cpu(rblock->bb_level) > 0);
  76. rblock->bb_numrecs = dblock->bb_numrecs;
  77. dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
  78. fkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  79. tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  80. fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  81. tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  82. dmxr = be16_to_cpu(dblock->bb_numrecs);
  83. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  84. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  85. }
  86. /*
  87. * Convert a compressed bmap extent record to an uncompressed form.
  88. * This code must be in sync with the routines xfs_bmbt_get_startoff,
  89. * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state.
  90. */
  91. STATIC void
  92. __xfs_bmbt_get_all(
  93. uint64_t l0,
  94. uint64_t l1,
  95. xfs_bmbt_irec_t *s)
  96. {
  97. int ext_flag;
  98. xfs_exntst_t st;
  99. ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN));
  100. s->br_startoff = ((xfs_fileoff_t)l0 &
  101. xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  102. s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) |
  103. (((xfs_fsblock_t)l1) >> 21);
  104. s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21));
  105. /* This is xfs_extent_state() in-line */
  106. if (ext_flag) {
  107. ASSERT(s->br_blockcount != 0); /* saved for DMIG */
  108. st = XFS_EXT_UNWRITTEN;
  109. } else
  110. st = XFS_EXT_NORM;
  111. s->br_state = st;
  112. }
  113. void
  114. xfs_bmbt_get_all(
  115. xfs_bmbt_rec_host_t *r,
  116. xfs_bmbt_irec_t *s)
  117. {
  118. __xfs_bmbt_get_all(r->l0, r->l1, s);
  119. }
  120. /*
  121. * Extract the blockcount field from an in memory bmap extent record.
  122. */
  123. xfs_filblks_t
  124. xfs_bmbt_get_blockcount(
  125. xfs_bmbt_rec_host_t *r)
  126. {
  127. return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21));
  128. }
  129. /*
  130. * Extract the startblock field from an in memory bmap extent record.
  131. */
  132. xfs_fsblock_t
  133. xfs_bmbt_get_startblock(
  134. xfs_bmbt_rec_host_t *r)
  135. {
  136. return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) |
  137. (((xfs_fsblock_t)r->l1) >> 21);
  138. }
  139. /*
  140. * Extract the startoff field from an in memory bmap extent record.
  141. */
  142. xfs_fileoff_t
  143. xfs_bmbt_get_startoff(
  144. xfs_bmbt_rec_host_t *r)
  145. {
  146. return ((xfs_fileoff_t)r->l0 &
  147. xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  148. }
  149. xfs_exntst_t
  150. xfs_bmbt_get_state(
  151. xfs_bmbt_rec_host_t *r)
  152. {
  153. int ext_flag;
  154. ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN));
  155. return xfs_extent_state(xfs_bmbt_get_blockcount(r),
  156. ext_flag);
  157. }
  158. /*
  159. * Extract the blockcount field from an on disk bmap extent record.
  160. */
  161. xfs_filblks_t
  162. xfs_bmbt_disk_get_blockcount(
  163. xfs_bmbt_rec_t *r)
  164. {
  165. return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21));
  166. }
  167. /*
  168. * Extract the startoff field from a disk format bmap extent record.
  169. */
  170. xfs_fileoff_t
  171. xfs_bmbt_disk_get_startoff(
  172. xfs_bmbt_rec_t *r)
  173. {
  174. return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
  175. xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  176. }
  177. /*
  178. * Set all the fields in a bmap extent record from the arguments.
  179. */
  180. void
  181. xfs_bmbt_set_allf(
  182. xfs_bmbt_rec_host_t *r,
  183. xfs_fileoff_t startoff,
  184. xfs_fsblock_t startblock,
  185. xfs_filblks_t blockcount,
  186. xfs_exntst_t state)
  187. {
  188. int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
  189. ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
  190. ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
  191. ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
  192. ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
  193. r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  194. ((xfs_bmbt_rec_base_t)startoff << 9) |
  195. ((xfs_bmbt_rec_base_t)startblock >> 43);
  196. r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
  197. ((xfs_bmbt_rec_base_t)blockcount &
  198. (xfs_bmbt_rec_base_t)xfs_mask64lo(21));
  199. }
  200. /*
  201. * Set all the fields in a bmap extent record from the uncompressed form.
  202. */
  203. void
  204. xfs_bmbt_set_all(
  205. xfs_bmbt_rec_host_t *r,
  206. xfs_bmbt_irec_t *s)
  207. {
  208. xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
  209. s->br_blockcount, s->br_state);
  210. }
  211. /*
  212. * Set all the fields in a disk format bmap extent record from the arguments.
  213. */
  214. void
  215. xfs_bmbt_disk_set_allf(
  216. xfs_bmbt_rec_t *r,
  217. xfs_fileoff_t startoff,
  218. xfs_fsblock_t startblock,
  219. xfs_filblks_t blockcount,
  220. xfs_exntst_t state)
  221. {
  222. int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
  223. ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
  224. ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0);
  225. ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
  226. ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0);
  227. r->l0 = cpu_to_be64(
  228. ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  229. ((xfs_bmbt_rec_base_t)startoff << 9) |
  230. ((xfs_bmbt_rec_base_t)startblock >> 43));
  231. r->l1 = cpu_to_be64(
  232. ((xfs_bmbt_rec_base_t)startblock << 21) |
  233. ((xfs_bmbt_rec_base_t)blockcount &
  234. (xfs_bmbt_rec_base_t)xfs_mask64lo(21)));
  235. }
  236. /*
  237. * Set all the fields in a bmap extent record from the uncompressed form.
  238. */
  239. STATIC void
  240. xfs_bmbt_disk_set_all(
  241. xfs_bmbt_rec_t *r,
  242. xfs_bmbt_irec_t *s)
  243. {
  244. xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
  245. s->br_blockcount, s->br_state);
  246. }
  247. /*
  248. * Set the blockcount field in a bmap extent record.
  249. */
  250. void
  251. xfs_bmbt_set_blockcount(
  252. xfs_bmbt_rec_host_t *r,
  253. xfs_filblks_t v)
  254. {
  255. ASSERT((v & xfs_mask64hi(43)) == 0);
  256. r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64hi(43)) |
  257. (xfs_bmbt_rec_base_t)(v & xfs_mask64lo(21));
  258. }
  259. /*
  260. * Set the startblock field in a bmap extent record.
  261. */
  262. void
  263. xfs_bmbt_set_startblock(
  264. xfs_bmbt_rec_host_t *r,
  265. xfs_fsblock_t v)
  266. {
  267. ASSERT((v & xfs_mask64hi(12)) == 0);
  268. r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) |
  269. (xfs_bmbt_rec_base_t)(v >> 43);
  270. r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) |
  271. (xfs_bmbt_rec_base_t)(v << 21);
  272. }
  273. /*
  274. * Set the startoff field in a bmap extent record.
  275. */
  276. void
  277. xfs_bmbt_set_startoff(
  278. xfs_bmbt_rec_host_t *r,
  279. xfs_fileoff_t v)
  280. {
  281. ASSERT((v & xfs_mask64hi(9)) == 0);
  282. r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) |
  283. ((xfs_bmbt_rec_base_t)v << 9) |
  284. (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9));
  285. }
  286. /*
  287. * Set the extent state field in a bmap extent record.
  288. */
  289. void
  290. xfs_bmbt_set_state(
  291. xfs_bmbt_rec_host_t *r,
  292. xfs_exntst_t v)
  293. {
  294. ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
  295. if (v == XFS_EXT_NORM)
  296. r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN);
  297. else
  298. r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN);
  299. }
  300. /*
  301. * Convert in-memory form of btree root to on-disk form.
  302. */
  303. void
  304. xfs_bmbt_to_bmdr(
  305. struct xfs_mount *mp,
  306. struct xfs_btree_block *rblock,
  307. int rblocklen,
  308. xfs_bmdr_block_t *dblock,
  309. int dblocklen)
  310. {
  311. int dmxr;
  312. xfs_bmbt_key_t *fkp;
  313. __be64 *fpp;
  314. xfs_bmbt_key_t *tkp;
  315. __be64 *tpp;
  316. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  317. ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC));
  318. ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid,
  319. &mp->m_sb.sb_meta_uuid));
  320. ASSERT(rblock->bb_u.l.bb_blkno ==
  321. cpu_to_be64(XFS_BUF_DADDR_NULL));
  322. } else
  323. ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC));
  324. ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
  325. ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
  326. ASSERT(rblock->bb_level != 0);
  327. dblock->bb_level = rblock->bb_level;
  328. dblock->bb_numrecs = rblock->bb_numrecs;
  329. dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
  330. fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  331. tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  332. fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  333. tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  334. dmxr = be16_to_cpu(dblock->bb_numrecs);
  335. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  336. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  337. }
  338. STATIC struct xfs_btree_cur *
  339. xfs_bmbt_dup_cursor(
  340. struct xfs_btree_cur *cur)
  341. {
  342. struct xfs_btree_cur *new;
  343. new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
  344. cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  345. /*
  346. * Copy the firstblock, dfops, and flags values,
  347. * since init cursor doesn't get them.
  348. */
  349. new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
  350. new->bc_private.b.dfops = cur->bc_private.b.dfops;
  351. new->bc_private.b.flags = cur->bc_private.b.flags;
  352. return new;
  353. }
  354. STATIC void
  355. xfs_bmbt_update_cursor(
  356. struct xfs_btree_cur *src,
  357. struct xfs_btree_cur *dst)
  358. {
  359. ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
  360. (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
  361. ASSERT(dst->bc_private.b.dfops == src->bc_private.b.dfops);
  362. dst->bc_private.b.allocated += src->bc_private.b.allocated;
  363. dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
  364. src->bc_private.b.allocated = 0;
  365. }
  366. STATIC int
  367. xfs_bmbt_alloc_block(
  368. struct xfs_btree_cur *cur,
  369. union xfs_btree_ptr *start,
  370. union xfs_btree_ptr *new,
  371. int *stat)
  372. {
  373. xfs_alloc_arg_t args; /* block allocation args */
  374. int error; /* error return value */
  375. memset(&args, 0, sizeof(args));
  376. args.tp = cur->bc_tp;
  377. args.mp = cur->bc_mp;
  378. args.fsbno = cur->bc_private.b.firstblock;
  379. args.firstblock = args.fsbno;
  380. xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_private.b.ip->i_ino,
  381. cur->bc_private.b.whichfork);
  382. if (args.fsbno == NULLFSBLOCK) {
  383. args.fsbno = be64_to_cpu(start->l);
  384. args.type = XFS_ALLOCTYPE_START_BNO;
  385. /*
  386. * Make sure there is sufficient room left in the AG to
  387. * complete a full tree split for an extent insert. If
  388. * we are converting the middle part of an extent then
  389. * we may need space for two tree splits.
  390. *
  391. * We are relying on the caller to make the correct block
  392. * reservation for this operation to succeed. If the
  393. * reservation amount is insufficient then we may fail a
  394. * block allocation here and corrupt the filesystem.
  395. */
  396. args.minleft = args.tp->t_blk_res;
  397. } else if (cur->bc_private.b.dfops->dop_low) {
  398. args.type = XFS_ALLOCTYPE_START_BNO;
  399. } else {
  400. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  401. }
  402. args.minlen = args.maxlen = args.prod = 1;
  403. args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
  404. if (!args.wasdel && args.tp->t_blk_res == 0) {
  405. error = -ENOSPC;
  406. goto error0;
  407. }
  408. error = xfs_alloc_vextent(&args);
  409. if (error)
  410. goto error0;
  411. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  412. /*
  413. * Could not find an AG with enough free space to satisfy
  414. * a full btree split. Try again and if
  415. * successful activate the lowspace algorithm.
  416. */
  417. args.fsbno = 0;
  418. args.type = XFS_ALLOCTYPE_FIRST_AG;
  419. error = xfs_alloc_vextent(&args);
  420. if (error)
  421. goto error0;
  422. cur->bc_private.b.dfops->dop_low = true;
  423. }
  424. if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
  425. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  426. *stat = 0;
  427. return 0;
  428. }
  429. ASSERT(args.len == 1);
  430. cur->bc_private.b.firstblock = args.fsbno;
  431. cur->bc_private.b.allocated++;
  432. cur->bc_private.b.ip->i_d.di_nblocks++;
  433. xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
  434. xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
  435. XFS_TRANS_DQ_BCOUNT, 1L);
  436. new->l = cpu_to_be64(args.fsbno);
  437. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  438. *stat = 1;
  439. return 0;
  440. error0:
  441. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  442. return error;
  443. }
  444. STATIC int
  445. xfs_bmbt_free_block(
  446. struct xfs_btree_cur *cur,
  447. struct xfs_buf *bp)
  448. {
  449. struct xfs_mount *mp = cur->bc_mp;
  450. struct xfs_inode *ip = cur->bc_private.b.ip;
  451. struct xfs_trans *tp = cur->bc_tp;
  452. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  453. struct xfs_owner_info oinfo;
  454. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_private.b.whichfork);
  455. xfs_bmap_add_free(mp, cur->bc_private.b.dfops, fsbno, 1, &oinfo);
  456. ip->i_d.di_nblocks--;
  457. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  458. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  459. return 0;
  460. }
  461. STATIC int
  462. xfs_bmbt_get_minrecs(
  463. struct xfs_btree_cur *cur,
  464. int level)
  465. {
  466. if (level == cur->bc_nlevels - 1) {
  467. struct xfs_ifork *ifp;
  468. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  469. cur->bc_private.b.whichfork);
  470. return xfs_bmbt_maxrecs(cur->bc_mp,
  471. ifp->if_broot_bytes, level == 0) / 2;
  472. }
  473. return cur->bc_mp->m_bmap_dmnr[level != 0];
  474. }
  475. int
  476. xfs_bmbt_get_maxrecs(
  477. struct xfs_btree_cur *cur,
  478. int level)
  479. {
  480. if (level == cur->bc_nlevels - 1) {
  481. struct xfs_ifork *ifp;
  482. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  483. cur->bc_private.b.whichfork);
  484. return xfs_bmbt_maxrecs(cur->bc_mp,
  485. ifp->if_broot_bytes, level == 0);
  486. }
  487. return cur->bc_mp->m_bmap_dmxr[level != 0];
  488. }
  489. /*
  490. * Get the maximum records we could store in the on-disk format.
  491. *
  492. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  493. * for the root node this checks the available space in the dinode fork
  494. * so that we can resize the in-memory buffer to match it. After a
  495. * resize to the maximum size this function returns the same value
  496. * as xfs_bmbt_get_maxrecs for the root node, too.
  497. */
  498. STATIC int
  499. xfs_bmbt_get_dmaxrecs(
  500. struct xfs_btree_cur *cur,
  501. int level)
  502. {
  503. if (level != cur->bc_nlevels - 1)
  504. return cur->bc_mp->m_bmap_dmxr[level != 0];
  505. return xfs_bmdr_maxrecs(cur->bc_private.b.forksize, level == 0);
  506. }
  507. STATIC void
  508. xfs_bmbt_init_key_from_rec(
  509. union xfs_btree_key *key,
  510. union xfs_btree_rec *rec)
  511. {
  512. key->bmbt.br_startoff =
  513. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  514. }
  515. STATIC void
  516. xfs_bmbt_init_high_key_from_rec(
  517. union xfs_btree_key *key,
  518. union xfs_btree_rec *rec)
  519. {
  520. key->bmbt.br_startoff = cpu_to_be64(
  521. xfs_bmbt_disk_get_startoff(&rec->bmbt) +
  522. xfs_bmbt_disk_get_blockcount(&rec->bmbt) - 1);
  523. }
  524. STATIC void
  525. xfs_bmbt_init_rec_from_cur(
  526. struct xfs_btree_cur *cur,
  527. union xfs_btree_rec *rec)
  528. {
  529. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  530. }
  531. STATIC void
  532. xfs_bmbt_init_ptr_from_cur(
  533. struct xfs_btree_cur *cur,
  534. union xfs_btree_ptr *ptr)
  535. {
  536. ptr->l = 0;
  537. }
  538. STATIC int64_t
  539. xfs_bmbt_key_diff(
  540. struct xfs_btree_cur *cur,
  541. union xfs_btree_key *key)
  542. {
  543. return (int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  544. cur->bc_rec.b.br_startoff;
  545. }
  546. STATIC int64_t
  547. xfs_bmbt_diff_two_keys(
  548. struct xfs_btree_cur *cur,
  549. union xfs_btree_key *k1,
  550. union xfs_btree_key *k2)
  551. {
  552. return (int64_t)be64_to_cpu(k1->bmbt.br_startoff) -
  553. be64_to_cpu(k2->bmbt.br_startoff);
  554. }
  555. static bool
  556. xfs_bmbt_verify(
  557. struct xfs_buf *bp)
  558. {
  559. struct xfs_mount *mp = bp->b_target->bt_mount;
  560. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  561. unsigned int level;
  562. switch (block->bb_magic) {
  563. case cpu_to_be32(XFS_BMAP_CRC_MAGIC):
  564. if (!xfs_sb_version_hascrc(&mp->m_sb))
  565. return false;
  566. if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
  567. return false;
  568. if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn)
  569. return false;
  570. /*
  571. * XXX: need a better way of verifying the owner here. Right now
  572. * just make sure there has been one set.
  573. */
  574. if (be64_to_cpu(block->bb_u.l.bb_owner) == 0)
  575. return false;
  576. /* fall through */
  577. case cpu_to_be32(XFS_BMAP_MAGIC):
  578. break;
  579. default:
  580. return false;
  581. }
  582. /*
  583. * numrecs and level verification.
  584. *
  585. * We don't know what fork we belong to, so just verify that the level
  586. * is less than the maximum of the two. Later checks will be more
  587. * precise.
  588. */
  589. level = be16_to_cpu(block->bb_level);
  590. if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
  591. return false;
  592. if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  593. return false;
  594. /* sibling pointer verification */
  595. if (!block->bb_u.l.bb_leftsib ||
  596. (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
  597. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib))))
  598. return false;
  599. if (!block->bb_u.l.bb_rightsib ||
  600. (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
  601. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib))))
  602. return false;
  603. return true;
  604. }
  605. static void
  606. xfs_bmbt_read_verify(
  607. struct xfs_buf *bp)
  608. {
  609. if (!xfs_btree_lblock_verify_crc(bp))
  610. xfs_buf_ioerror(bp, -EFSBADCRC);
  611. else if (!xfs_bmbt_verify(bp))
  612. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  613. if (bp->b_error) {
  614. trace_xfs_btree_corrupt(bp, _RET_IP_);
  615. xfs_verifier_error(bp);
  616. }
  617. }
  618. static void
  619. xfs_bmbt_write_verify(
  620. struct xfs_buf *bp)
  621. {
  622. if (!xfs_bmbt_verify(bp)) {
  623. trace_xfs_btree_corrupt(bp, _RET_IP_);
  624. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  625. xfs_verifier_error(bp);
  626. return;
  627. }
  628. xfs_btree_lblock_calc_crc(bp);
  629. }
  630. const struct xfs_buf_ops xfs_bmbt_buf_ops = {
  631. .name = "xfs_bmbt",
  632. .verify_read = xfs_bmbt_read_verify,
  633. .verify_write = xfs_bmbt_write_verify,
  634. };
  635. STATIC int
  636. xfs_bmbt_keys_inorder(
  637. struct xfs_btree_cur *cur,
  638. union xfs_btree_key *k1,
  639. union xfs_btree_key *k2)
  640. {
  641. return be64_to_cpu(k1->bmbt.br_startoff) <
  642. be64_to_cpu(k2->bmbt.br_startoff);
  643. }
  644. STATIC int
  645. xfs_bmbt_recs_inorder(
  646. struct xfs_btree_cur *cur,
  647. union xfs_btree_rec *r1,
  648. union xfs_btree_rec *r2)
  649. {
  650. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  651. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  652. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  653. }
  654. static const struct xfs_btree_ops xfs_bmbt_ops = {
  655. .rec_len = sizeof(xfs_bmbt_rec_t),
  656. .key_len = sizeof(xfs_bmbt_key_t),
  657. .dup_cursor = xfs_bmbt_dup_cursor,
  658. .update_cursor = xfs_bmbt_update_cursor,
  659. .alloc_block = xfs_bmbt_alloc_block,
  660. .free_block = xfs_bmbt_free_block,
  661. .get_maxrecs = xfs_bmbt_get_maxrecs,
  662. .get_minrecs = xfs_bmbt_get_minrecs,
  663. .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
  664. .init_key_from_rec = xfs_bmbt_init_key_from_rec,
  665. .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec,
  666. .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
  667. .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
  668. .key_diff = xfs_bmbt_key_diff,
  669. .diff_two_keys = xfs_bmbt_diff_two_keys,
  670. .buf_ops = &xfs_bmbt_buf_ops,
  671. .keys_inorder = xfs_bmbt_keys_inorder,
  672. .recs_inorder = xfs_bmbt_recs_inorder,
  673. };
  674. /*
  675. * Allocate a new bmap btree cursor.
  676. */
  677. struct xfs_btree_cur * /* new bmap btree cursor */
  678. xfs_bmbt_init_cursor(
  679. struct xfs_mount *mp, /* file system mount point */
  680. struct xfs_trans *tp, /* transaction pointer */
  681. struct xfs_inode *ip, /* inode owning the btree */
  682. int whichfork) /* data or attr fork */
  683. {
  684. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  685. struct xfs_btree_cur *cur;
  686. ASSERT(whichfork != XFS_COW_FORK);
  687. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  688. cur->bc_tp = tp;
  689. cur->bc_mp = mp;
  690. cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  691. cur->bc_btnum = XFS_BTNUM_BMAP;
  692. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  693. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2);
  694. cur->bc_ops = &xfs_bmbt_ops;
  695. cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
  696. if (xfs_sb_version_hascrc(&mp->m_sb))
  697. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  698. cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
  699. cur->bc_private.b.ip = ip;
  700. cur->bc_private.b.firstblock = NULLFSBLOCK;
  701. cur->bc_private.b.dfops = NULL;
  702. cur->bc_private.b.allocated = 0;
  703. cur->bc_private.b.flags = 0;
  704. cur->bc_private.b.whichfork = whichfork;
  705. return cur;
  706. }
  707. /*
  708. * Calculate number of records in a bmap btree block.
  709. */
  710. int
  711. xfs_bmbt_maxrecs(
  712. struct xfs_mount *mp,
  713. int blocklen,
  714. int leaf)
  715. {
  716. blocklen -= XFS_BMBT_BLOCK_LEN(mp);
  717. if (leaf)
  718. return blocklen / sizeof(xfs_bmbt_rec_t);
  719. return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
  720. }
  721. /*
  722. * Calculate number of records in a bmap btree inode root.
  723. */
  724. int
  725. xfs_bmdr_maxrecs(
  726. int blocklen,
  727. int leaf)
  728. {
  729. blocklen -= sizeof(xfs_bmdr_block_t);
  730. if (leaf)
  731. return blocklen / sizeof(xfs_bmdr_rec_t);
  732. return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
  733. }
  734. /*
  735. * Change the owner of a btree format fork fo the inode passed in. Change it to
  736. * the owner of that is passed in so that we can change owners before or after
  737. * we switch forks between inodes. The operation that the caller is doing will
  738. * determine whether is needs to change owner before or after the switch.
  739. *
  740. * For demand paged transactional modification, the fork switch should be done
  741. * after reading in all the blocks, modifying them and pinning them in the
  742. * transaction. For modification when the buffers are already pinned in memory,
  743. * the fork switch can be done before changing the owner as we won't need to
  744. * validate the owner until the btree buffers are unpinned and writes can occur
  745. * again.
  746. *
  747. * For recovery based ownership change, there is no transactional context and
  748. * so a buffer list must be supplied so that we can record the buffers that we
  749. * modified for the caller to issue IO on.
  750. */
  751. int
  752. xfs_bmbt_change_owner(
  753. struct xfs_trans *tp,
  754. struct xfs_inode *ip,
  755. int whichfork,
  756. xfs_ino_t new_owner,
  757. struct list_head *buffer_list)
  758. {
  759. struct xfs_btree_cur *cur;
  760. int error;
  761. ASSERT(tp || buffer_list);
  762. ASSERT(!(tp && buffer_list));
  763. if (whichfork == XFS_DATA_FORK)
  764. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE);
  765. else
  766. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE);
  767. cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
  768. if (!cur)
  769. return -ENOMEM;
  770. cur->bc_private.b.flags |= XFS_BTCUR_BPRV_INVALID_OWNER;
  771. error = xfs_btree_change_owner(cur, new_owner, buffer_list);
  772. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  773. return error;
  774. }