xfs_bmap_btree.c 22 KB

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