xfs_bmap_btree.c 22 KB

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