xfs_bmap_btree.c 24 KB

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