xfs_bmap_btree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. /*
  339. * Check extent records, which have just been read, for
  340. * any bit in the extent flag field. ASSERT on debug
  341. * kernels, as this condition should not occur.
  342. * Return an error condition (1) if any flags found,
  343. * otherwise return 0.
  344. */
  345. int
  346. xfs_check_nostate_extents(
  347. xfs_ifork_t *ifp,
  348. xfs_extnum_t idx,
  349. xfs_extnum_t num)
  350. {
  351. for (; num > 0; num--, idx++) {
  352. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  353. if ((ep->l0 >>
  354. (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
  355. ASSERT(0);
  356. return 1;
  357. }
  358. }
  359. return 0;
  360. }
  361. STATIC struct xfs_btree_cur *
  362. xfs_bmbt_dup_cursor(
  363. struct xfs_btree_cur *cur)
  364. {
  365. struct xfs_btree_cur *new;
  366. new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
  367. cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  368. /*
  369. * Copy the firstblock, dfops, and flags values,
  370. * since init cursor doesn't get them.
  371. */
  372. new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
  373. new->bc_private.b.dfops = cur->bc_private.b.dfops;
  374. new->bc_private.b.flags = cur->bc_private.b.flags;
  375. return new;
  376. }
  377. STATIC void
  378. xfs_bmbt_update_cursor(
  379. struct xfs_btree_cur *src,
  380. struct xfs_btree_cur *dst)
  381. {
  382. ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
  383. (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
  384. ASSERT(dst->bc_private.b.dfops == src->bc_private.b.dfops);
  385. dst->bc_private.b.allocated += src->bc_private.b.allocated;
  386. dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
  387. src->bc_private.b.allocated = 0;
  388. }
  389. STATIC int
  390. xfs_bmbt_alloc_block(
  391. struct xfs_btree_cur *cur,
  392. union xfs_btree_ptr *start,
  393. union xfs_btree_ptr *new,
  394. int *stat)
  395. {
  396. xfs_alloc_arg_t args; /* block allocation args */
  397. int error; /* error return value */
  398. memset(&args, 0, sizeof(args));
  399. args.tp = cur->bc_tp;
  400. args.mp = cur->bc_mp;
  401. args.fsbno = cur->bc_private.b.firstblock;
  402. args.firstblock = args.fsbno;
  403. xfs_rmap_ino_bmbt_owner(&args.oinfo, cur->bc_private.b.ip->i_ino,
  404. cur->bc_private.b.whichfork);
  405. if (args.fsbno == NULLFSBLOCK) {
  406. args.fsbno = be64_to_cpu(start->l);
  407. try_another_ag:
  408. args.type = XFS_ALLOCTYPE_START_BNO;
  409. /*
  410. * Make sure there is sufficient room left in the AG to
  411. * complete a full tree split for an extent insert. If
  412. * we are converting the middle part of an extent then
  413. * we may need space for two tree splits.
  414. *
  415. * We are relying on the caller to make the correct block
  416. * reservation for this operation to succeed. If the
  417. * reservation amount is insufficient then we may fail a
  418. * block allocation here and corrupt the filesystem.
  419. */
  420. args.minleft = args.tp->t_blk_res;
  421. } else if (cur->bc_private.b.dfops->dop_low) {
  422. args.type = XFS_ALLOCTYPE_START_BNO;
  423. } else {
  424. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  425. }
  426. args.minlen = args.maxlen = args.prod = 1;
  427. args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
  428. if (!args.wasdel && args.tp->t_blk_res == 0) {
  429. error = -ENOSPC;
  430. goto error0;
  431. }
  432. error = xfs_alloc_vextent(&args);
  433. if (error)
  434. goto error0;
  435. /*
  436. * During a CoW operation, the allocation and bmbt updates occur in
  437. * different transactions. The mapping code tries to put new bmbt
  438. * blocks near extents being mapped, but the only way to guarantee this
  439. * is if the alloc and the mapping happen in a single transaction that
  440. * has a block reservation. That isn't the case here, so if we run out
  441. * of space we'll try again with another AG.
  442. */
  443. if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
  444. args.fsbno == NULLFSBLOCK &&
  445. args.type == XFS_ALLOCTYPE_NEAR_BNO) {
  446. cur->bc_private.b.dfops->dop_low = true;
  447. args.fsbno = cur->bc_private.b.firstblock;
  448. goto try_another_ag;
  449. }
  450. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  451. /*
  452. * Could not find an AG with enough free space to satisfy
  453. * a full btree split. Try again and if
  454. * successful activate the lowspace algorithm.
  455. */
  456. args.fsbno = 0;
  457. args.type = XFS_ALLOCTYPE_FIRST_AG;
  458. error = xfs_alloc_vextent(&args);
  459. if (error)
  460. goto error0;
  461. cur->bc_private.b.dfops->dop_low = true;
  462. }
  463. if (args.fsbno == NULLFSBLOCK) {
  464. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  465. *stat = 0;
  466. return 0;
  467. }
  468. ASSERT(args.len == 1);
  469. cur->bc_private.b.firstblock = args.fsbno;
  470. cur->bc_private.b.allocated++;
  471. cur->bc_private.b.ip->i_d.di_nblocks++;
  472. xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
  473. xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
  474. XFS_TRANS_DQ_BCOUNT, 1L);
  475. new->l = cpu_to_be64(args.fsbno);
  476. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  477. *stat = 1;
  478. return 0;
  479. error0:
  480. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  481. return error;
  482. }
  483. STATIC int
  484. xfs_bmbt_free_block(
  485. struct xfs_btree_cur *cur,
  486. struct xfs_buf *bp)
  487. {
  488. struct xfs_mount *mp = cur->bc_mp;
  489. struct xfs_inode *ip = cur->bc_private.b.ip;
  490. struct xfs_trans *tp = cur->bc_tp;
  491. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  492. struct xfs_owner_info oinfo;
  493. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_private.b.whichfork);
  494. xfs_bmap_add_free(mp, cur->bc_private.b.dfops, fsbno, 1, &oinfo);
  495. ip->i_d.di_nblocks--;
  496. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  497. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  498. return 0;
  499. }
  500. STATIC int
  501. xfs_bmbt_get_minrecs(
  502. struct xfs_btree_cur *cur,
  503. int level)
  504. {
  505. if (level == cur->bc_nlevels - 1) {
  506. struct xfs_ifork *ifp;
  507. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  508. cur->bc_private.b.whichfork);
  509. return xfs_bmbt_maxrecs(cur->bc_mp,
  510. ifp->if_broot_bytes, level == 0) / 2;
  511. }
  512. return cur->bc_mp->m_bmap_dmnr[level != 0];
  513. }
  514. int
  515. xfs_bmbt_get_maxrecs(
  516. struct xfs_btree_cur *cur,
  517. int level)
  518. {
  519. if (level == cur->bc_nlevels - 1) {
  520. struct xfs_ifork *ifp;
  521. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  522. cur->bc_private.b.whichfork);
  523. return xfs_bmbt_maxrecs(cur->bc_mp,
  524. ifp->if_broot_bytes, level == 0);
  525. }
  526. return cur->bc_mp->m_bmap_dmxr[level != 0];
  527. }
  528. /*
  529. * Get the maximum records we could store in the on-disk format.
  530. *
  531. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  532. * for the root node this checks the available space in the dinode fork
  533. * so that we can resize the in-memory buffer to match it. After a
  534. * resize to the maximum size this function returns the same value
  535. * as xfs_bmbt_get_maxrecs for the root node, too.
  536. */
  537. STATIC int
  538. xfs_bmbt_get_dmaxrecs(
  539. struct xfs_btree_cur *cur,
  540. int level)
  541. {
  542. if (level != cur->bc_nlevels - 1)
  543. return cur->bc_mp->m_bmap_dmxr[level != 0];
  544. return xfs_bmdr_maxrecs(cur->bc_private.b.forksize, level == 0);
  545. }
  546. STATIC void
  547. xfs_bmbt_init_key_from_rec(
  548. union xfs_btree_key *key,
  549. union xfs_btree_rec *rec)
  550. {
  551. key->bmbt.br_startoff =
  552. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  553. }
  554. STATIC void
  555. xfs_bmbt_init_rec_from_cur(
  556. struct xfs_btree_cur *cur,
  557. union xfs_btree_rec *rec)
  558. {
  559. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  560. }
  561. STATIC void
  562. xfs_bmbt_init_ptr_from_cur(
  563. struct xfs_btree_cur *cur,
  564. union xfs_btree_ptr *ptr)
  565. {
  566. ptr->l = 0;
  567. }
  568. STATIC __int64_t
  569. xfs_bmbt_key_diff(
  570. struct xfs_btree_cur *cur,
  571. union xfs_btree_key *key)
  572. {
  573. return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  574. cur->bc_rec.b.br_startoff;
  575. }
  576. static bool
  577. xfs_bmbt_verify(
  578. struct xfs_buf *bp)
  579. {
  580. struct xfs_mount *mp = bp->b_target->bt_mount;
  581. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  582. unsigned int level;
  583. switch (block->bb_magic) {
  584. case cpu_to_be32(XFS_BMAP_CRC_MAGIC):
  585. if (!xfs_sb_version_hascrc(&mp->m_sb))
  586. return false;
  587. if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
  588. return false;
  589. if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn)
  590. return false;
  591. /*
  592. * XXX: need a better way of verifying the owner here. Right now
  593. * just make sure there has been one set.
  594. */
  595. if (be64_to_cpu(block->bb_u.l.bb_owner) == 0)
  596. return false;
  597. /* fall through */
  598. case cpu_to_be32(XFS_BMAP_MAGIC):
  599. break;
  600. default:
  601. return false;
  602. }
  603. /*
  604. * numrecs and level verification.
  605. *
  606. * We don't know what fork we belong to, so just verify that the level
  607. * is less than the maximum of the two. Later checks will be more
  608. * precise.
  609. */
  610. level = be16_to_cpu(block->bb_level);
  611. if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
  612. return false;
  613. if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  614. return false;
  615. /* sibling pointer verification */
  616. if (!block->bb_u.l.bb_leftsib ||
  617. (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
  618. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib))))
  619. return false;
  620. if (!block->bb_u.l.bb_rightsib ||
  621. (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
  622. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib))))
  623. return false;
  624. return true;
  625. }
  626. static void
  627. xfs_bmbt_read_verify(
  628. struct xfs_buf *bp)
  629. {
  630. if (!xfs_btree_lblock_verify_crc(bp))
  631. xfs_buf_ioerror(bp, -EFSBADCRC);
  632. else if (!xfs_bmbt_verify(bp))
  633. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  634. if (bp->b_error) {
  635. trace_xfs_btree_corrupt(bp, _RET_IP_);
  636. xfs_verifier_error(bp);
  637. }
  638. }
  639. static void
  640. xfs_bmbt_write_verify(
  641. struct xfs_buf *bp)
  642. {
  643. if (!xfs_bmbt_verify(bp)) {
  644. trace_xfs_btree_corrupt(bp, _RET_IP_);
  645. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  646. xfs_verifier_error(bp);
  647. return;
  648. }
  649. xfs_btree_lblock_calc_crc(bp);
  650. }
  651. const struct xfs_buf_ops xfs_bmbt_buf_ops = {
  652. .name = "xfs_bmbt",
  653. .verify_read = xfs_bmbt_read_verify,
  654. .verify_write = xfs_bmbt_write_verify,
  655. };
  656. #if defined(DEBUG) || defined(XFS_WARN)
  657. STATIC int
  658. xfs_bmbt_keys_inorder(
  659. struct xfs_btree_cur *cur,
  660. union xfs_btree_key *k1,
  661. union xfs_btree_key *k2)
  662. {
  663. return be64_to_cpu(k1->bmbt.br_startoff) <
  664. be64_to_cpu(k2->bmbt.br_startoff);
  665. }
  666. STATIC int
  667. xfs_bmbt_recs_inorder(
  668. struct xfs_btree_cur *cur,
  669. union xfs_btree_rec *r1,
  670. union xfs_btree_rec *r2)
  671. {
  672. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  673. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  674. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  675. }
  676. #endif /* DEBUG */
  677. static const struct xfs_btree_ops xfs_bmbt_ops = {
  678. .rec_len = sizeof(xfs_bmbt_rec_t),
  679. .key_len = sizeof(xfs_bmbt_key_t),
  680. .dup_cursor = xfs_bmbt_dup_cursor,
  681. .update_cursor = xfs_bmbt_update_cursor,
  682. .alloc_block = xfs_bmbt_alloc_block,
  683. .free_block = xfs_bmbt_free_block,
  684. .get_maxrecs = xfs_bmbt_get_maxrecs,
  685. .get_minrecs = xfs_bmbt_get_minrecs,
  686. .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
  687. .init_key_from_rec = xfs_bmbt_init_key_from_rec,
  688. .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
  689. .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
  690. .key_diff = xfs_bmbt_key_diff,
  691. .buf_ops = &xfs_bmbt_buf_ops,
  692. #if defined(DEBUG) || defined(XFS_WARN)
  693. .keys_inorder = xfs_bmbt_keys_inorder,
  694. .recs_inorder = xfs_bmbt_recs_inorder,
  695. #endif
  696. };
  697. /*
  698. * Allocate a new bmap btree cursor.
  699. */
  700. struct xfs_btree_cur * /* new bmap btree cursor */
  701. xfs_bmbt_init_cursor(
  702. struct xfs_mount *mp, /* file system mount point */
  703. struct xfs_trans *tp, /* transaction pointer */
  704. struct xfs_inode *ip, /* inode owning the btree */
  705. int whichfork) /* data or attr fork */
  706. {
  707. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  708. struct xfs_btree_cur *cur;
  709. ASSERT(whichfork != XFS_COW_FORK);
  710. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  711. cur->bc_tp = tp;
  712. cur->bc_mp = mp;
  713. cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  714. cur->bc_btnum = XFS_BTNUM_BMAP;
  715. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  716. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_bmbt_2);
  717. cur->bc_ops = &xfs_bmbt_ops;
  718. cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
  719. if (xfs_sb_version_hascrc(&mp->m_sb))
  720. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  721. cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
  722. cur->bc_private.b.ip = ip;
  723. cur->bc_private.b.firstblock = NULLFSBLOCK;
  724. cur->bc_private.b.dfops = NULL;
  725. cur->bc_private.b.allocated = 0;
  726. cur->bc_private.b.flags = 0;
  727. cur->bc_private.b.whichfork = whichfork;
  728. return cur;
  729. }
  730. /*
  731. * Calculate number of records in a bmap btree block.
  732. */
  733. int
  734. xfs_bmbt_maxrecs(
  735. struct xfs_mount *mp,
  736. int blocklen,
  737. int leaf)
  738. {
  739. blocklen -= XFS_BMBT_BLOCK_LEN(mp);
  740. if (leaf)
  741. return blocklen / sizeof(xfs_bmbt_rec_t);
  742. return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
  743. }
  744. /*
  745. * Calculate number of records in a bmap btree inode root.
  746. */
  747. int
  748. xfs_bmdr_maxrecs(
  749. int blocklen,
  750. int leaf)
  751. {
  752. blocklen -= sizeof(xfs_bmdr_block_t);
  753. if (leaf)
  754. return blocklen / sizeof(xfs_bmdr_rec_t);
  755. return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
  756. }
  757. /*
  758. * Change the owner of a btree format fork fo the inode passed in. Change it to
  759. * the owner of that is passed in so that we can change owners before or after
  760. * we switch forks between inodes. The operation that the caller is doing will
  761. * determine whether is needs to change owner before or after the switch.
  762. *
  763. * For demand paged transactional modification, the fork switch should be done
  764. * after reading in all the blocks, modifying them and pinning them in the
  765. * transaction. For modification when the buffers are already pinned in memory,
  766. * the fork switch can be done before changing the owner as we won't need to
  767. * validate the owner until the btree buffers are unpinned and writes can occur
  768. * again.
  769. *
  770. * For recovery based ownership change, there is no transactional context and
  771. * so a buffer list must be supplied so that we can record the buffers that we
  772. * modified for the caller to issue IO on.
  773. */
  774. int
  775. xfs_bmbt_change_owner(
  776. struct xfs_trans *tp,
  777. struct xfs_inode *ip,
  778. int whichfork,
  779. xfs_ino_t new_owner,
  780. struct list_head *buffer_list)
  781. {
  782. struct xfs_btree_cur *cur;
  783. int error;
  784. ASSERT(tp || buffer_list);
  785. ASSERT(!(tp && buffer_list));
  786. if (whichfork == XFS_DATA_FORK)
  787. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE);
  788. else
  789. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE);
  790. cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
  791. if (!cur)
  792. return -ENOMEM;
  793. error = xfs_btree_change_owner(cur, new_owner, buffer_list);
  794. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  795. return error;
  796. }