xfs_bmap_btree.c 25 KB

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