xfs_bmap_btree.c 23 KB

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