xfs_bmap_btree.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. #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(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 *stat)
  478. {
  479. xfs_alloc_arg_t args; /* block allocation args */
  480. int error; /* error return value */
  481. memset(&args, 0, sizeof(args));
  482. args.tp = cur->bc_tp;
  483. args.mp = cur->bc_mp;
  484. args.fsbno = cur->bc_private.b.firstblock;
  485. args.firstblock = args.fsbno;
  486. if (args.fsbno == NULLFSBLOCK) {
  487. args.fsbno = be64_to_cpu(start->l);
  488. args.type = XFS_ALLOCTYPE_START_BNO;
  489. /*
  490. * Make sure there is sufficient room left in the AG to
  491. * complete a full tree split for an extent insert. If
  492. * we are converting the middle part of an extent then
  493. * we may need space for two tree splits.
  494. *
  495. * We are relying on the caller to make the correct block
  496. * reservation for this operation to succeed. If the
  497. * reservation amount is insufficient then we may fail a
  498. * block allocation here and corrupt the filesystem.
  499. */
  500. args.minleft = xfs_trans_get_block_res(args.tp);
  501. } else if (cur->bc_private.b.flist->xbf_low) {
  502. args.type = XFS_ALLOCTYPE_START_BNO;
  503. } else {
  504. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  505. }
  506. args.minlen = args.maxlen = args.prod = 1;
  507. args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
  508. if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
  509. error = XFS_ERROR(ENOSPC);
  510. goto error0;
  511. }
  512. error = xfs_alloc_vextent(&args);
  513. if (error)
  514. goto error0;
  515. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  516. /*
  517. * Could not find an AG with enough free space to satisfy
  518. * a full btree split. Try again without minleft and if
  519. * successful activate the lowspace algorithm.
  520. */
  521. args.fsbno = 0;
  522. args.type = XFS_ALLOCTYPE_FIRST_AG;
  523. args.minleft = 0;
  524. error = xfs_alloc_vextent(&args);
  525. if (error)
  526. goto error0;
  527. cur->bc_private.b.flist->xbf_low = 1;
  528. }
  529. if (args.fsbno == NULLFSBLOCK) {
  530. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  531. *stat = 0;
  532. return 0;
  533. }
  534. ASSERT(args.len == 1);
  535. cur->bc_private.b.firstblock = args.fsbno;
  536. cur->bc_private.b.allocated++;
  537. cur->bc_private.b.ip->i_d.di_nblocks++;
  538. xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
  539. xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
  540. XFS_TRANS_DQ_BCOUNT, 1L);
  541. new->l = cpu_to_be64(args.fsbno);
  542. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  543. *stat = 1;
  544. return 0;
  545. error0:
  546. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  547. return error;
  548. }
  549. STATIC int
  550. xfs_bmbt_free_block(
  551. struct xfs_btree_cur *cur,
  552. struct xfs_buf *bp)
  553. {
  554. struct xfs_mount *mp = cur->bc_mp;
  555. struct xfs_inode *ip = cur->bc_private.b.ip;
  556. struct xfs_trans *tp = cur->bc_tp;
  557. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  558. xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
  559. ip->i_d.di_nblocks--;
  560. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  561. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  562. xfs_trans_binval(tp, bp);
  563. return 0;
  564. }
  565. STATIC int
  566. xfs_bmbt_get_minrecs(
  567. struct xfs_btree_cur *cur,
  568. int level)
  569. {
  570. if (level == cur->bc_nlevels - 1) {
  571. struct xfs_ifork *ifp;
  572. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  573. cur->bc_private.b.whichfork);
  574. return xfs_bmbt_maxrecs(cur->bc_mp,
  575. ifp->if_broot_bytes, level == 0) / 2;
  576. }
  577. return cur->bc_mp->m_bmap_dmnr[level != 0];
  578. }
  579. int
  580. xfs_bmbt_get_maxrecs(
  581. struct xfs_btree_cur *cur,
  582. int level)
  583. {
  584. if (level == cur->bc_nlevels - 1) {
  585. struct xfs_ifork *ifp;
  586. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  587. cur->bc_private.b.whichfork);
  588. return xfs_bmbt_maxrecs(cur->bc_mp,
  589. ifp->if_broot_bytes, level == 0);
  590. }
  591. return cur->bc_mp->m_bmap_dmxr[level != 0];
  592. }
  593. /*
  594. * Get the maximum records we could store in the on-disk format.
  595. *
  596. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  597. * for the root node this checks the available space in the dinode fork
  598. * so that we can resize the in-memory buffer to match it. After a
  599. * resize to the maximum size this function returns the same value
  600. * as xfs_bmbt_get_maxrecs for the root node, too.
  601. */
  602. STATIC int
  603. xfs_bmbt_get_dmaxrecs(
  604. struct xfs_btree_cur *cur,
  605. int level)
  606. {
  607. if (level != cur->bc_nlevels - 1)
  608. return cur->bc_mp->m_bmap_dmxr[level != 0];
  609. return xfs_bmdr_maxrecs(cur->bc_private.b.forksize, level == 0);
  610. }
  611. STATIC void
  612. xfs_bmbt_init_key_from_rec(
  613. union xfs_btree_key *key,
  614. union xfs_btree_rec *rec)
  615. {
  616. key->bmbt.br_startoff =
  617. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  618. }
  619. STATIC void
  620. xfs_bmbt_init_rec_from_key(
  621. union xfs_btree_key *key,
  622. union xfs_btree_rec *rec)
  623. {
  624. ASSERT(key->bmbt.br_startoff != 0);
  625. xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
  626. 0, 0, XFS_EXT_NORM);
  627. }
  628. STATIC void
  629. xfs_bmbt_init_rec_from_cur(
  630. struct xfs_btree_cur *cur,
  631. union xfs_btree_rec *rec)
  632. {
  633. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  634. }
  635. STATIC void
  636. xfs_bmbt_init_ptr_from_cur(
  637. struct xfs_btree_cur *cur,
  638. union xfs_btree_ptr *ptr)
  639. {
  640. ptr->l = 0;
  641. }
  642. STATIC __int64_t
  643. xfs_bmbt_key_diff(
  644. struct xfs_btree_cur *cur,
  645. union xfs_btree_key *key)
  646. {
  647. return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  648. cur->bc_rec.b.br_startoff;
  649. }
  650. static bool
  651. xfs_bmbt_verify(
  652. struct xfs_buf *bp)
  653. {
  654. struct xfs_mount *mp = bp->b_target->bt_mount;
  655. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  656. unsigned int level;
  657. switch (block->bb_magic) {
  658. case cpu_to_be32(XFS_BMAP_CRC_MAGIC):
  659. if (!xfs_sb_version_hascrc(&mp->m_sb))
  660. return false;
  661. if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid))
  662. return false;
  663. if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn)
  664. return false;
  665. /*
  666. * XXX: need a better way of verifying the owner here. Right now
  667. * just make sure there has been one set.
  668. */
  669. if (be64_to_cpu(block->bb_u.l.bb_owner) == 0)
  670. return false;
  671. /* fall through */
  672. case cpu_to_be32(XFS_BMAP_MAGIC):
  673. break;
  674. default:
  675. return false;
  676. }
  677. /*
  678. * numrecs and level verification.
  679. *
  680. * We don't know what fork we belong to, so just verify that the level
  681. * is less than the maximum of the two. Later checks will be more
  682. * precise.
  683. */
  684. level = be16_to_cpu(block->bb_level);
  685. if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
  686. return false;
  687. if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  688. return false;
  689. /* sibling pointer verification */
  690. if (!block->bb_u.l.bb_leftsib ||
  691. (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLDFSBNO) &&
  692. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib))))
  693. return false;
  694. if (!block->bb_u.l.bb_rightsib ||
  695. (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLDFSBNO) &&
  696. !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib))))
  697. return false;
  698. return true;
  699. }
  700. static void
  701. xfs_bmbt_read_verify(
  702. struct xfs_buf *bp)
  703. {
  704. if (!xfs_btree_lblock_verify_crc(bp))
  705. xfs_buf_ioerror(bp, EFSBADCRC);
  706. else if (!xfs_bmbt_verify(bp))
  707. xfs_buf_ioerror(bp, EFSCORRUPTED);
  708. if (bp->b_error) {
  709. trace_xfs_btree_corrupt(bp, _RET_IP_);
  710. xfs_verifier_error(bp);
  711. }
  712. }
  713. static void
  714. xfs_bmbt_write_verify(
  715. struct xfs_buf *bp)
  716. {
  717. if (!xfs_bmbt_verify(bp)) {
  718. trace_xfs_btree_corrupt(bp, _RET_IP_);
  719. xfs_buf_ioerror(bp, EFSCORRUPTED);
  720. xfs_verifier_error(bp);
  721. return;
  722. }
  723. xfs_btree_lblock_calc_crc(bp);
  724. }
  725. const struct xfs_buf_ops xfs_bmbt_buf_ops = {
  726. .verify_read = xfs_bmbt_read_verify,
  727. .verify_write = xfs_bmbt_write_verify,
  728. };
  729. #if defined(DEBUG) || defined(XFS_WARN)
  730. STATIC int
  731. xfs_bmbt_keys_inorder(
  732. struct xfs_btree_cur *cur,
  733. union xfs_btree_key *k1,
  734. union xfs_btree_key *k2)
  735. {
  736. return be64_to_cpu(k1->bmbt.br_startoff) <
  737. be64_to_cpu(k2->bmbt.br_startoff);
  738. }
  739. STATIC int
  740. xfs_bmbt_recs_inorder(
  741. struct xfs_btree_cur *cur,
  742. union xfs_btree_rec *r1,
  743. union xfs_btree_rec *r2)
  744. {
  745. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  746. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  747. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  748. }
  749. #endif /* DEBUG */
  750. static const struct xfs_btree_ops xfs_bmbt_ops = {
  751. .rec_len = sizeof(xfs_bmbt_rec_t),
  752. .key_len = sizeof(xfs_bmbt_key_t),
  753. .dup_cursor = xfs_bmbt_dup_cursor,
  754. .update_cursor = xfs_bmbt_update_cursor,
  755. .alloc_block = xfs_bmbt_alloc_block,
  756. .free_block = xfs_bmbt_free_block,
  757. .get_maxrecs = xfs_bmbt_get_maxrecs,
  758. .get_minrecs = xfs_bmbt_get_minrecs,
  759. .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
  760. .init_key_from_rec = xfs_bmbt_init_key_from_rec,
  761. .init_rec_from_key = xfs_bmbt_init_rec_from_key,
  762. .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
  763. .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
  764. .key_diff = xfs_bmbt_key_diff,
  765. .buf_ops = &xfs_bmbt_buf_ops,
  766. #if defined(DEBUG) || defined(XFS_WARN)
  767. .keys_inorder = xfs_bmbt_keys_inorder,
  768. .recs_inorder = xfs_bmbt_recs_inorder,
  769. #endif
  770. };
  771. /*
  772. * Allocate a new bmap btree cursor.
  773. */
  774. struct xfs_btree_cur * /* new bmap btree cursor */
  775. xfs_bmbt_init_cursor(
  776. struct xfs_mount *mp, /* file system mount point */
  777. struct xfs_trans *tp, /* transaction pointer */
  778. struct xfs_inode *ip, /* inode owning the btree */
  779. int whichfork) /* data or attr fork */
  780. {
  781. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  782. struct xfs_btree_cur *cur;
  783. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  784. cur->bc_tp = tp;
  785. cur->bc_mp = mp;
  786. cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  787. cur->bc_btnum = XFS_BTNUM_BMAP;
  788. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  789. cur->bc_ops = &xfs_bmbt_ops;
  790. cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
  791. if (xfs_sb_version_hascrc(&mp->m_sb))
  792. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  793. cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
  794. cur->bc_private.b.ip = ip;
  795. cur->bc_private.b.firstblock = NULLFSBLOCK;
  796. cur->bc_private.b.flist = NULL;
  797. cur->bc_private.b.allocated = 0;
  798. cur->bc_private.b.flags = 0;
  799. cur->bc_private.b.whichfork = whichfork;
  800. return cur;
  801. }
  802. /*
  803. * Calculate number of records in a bmap btree block.
  804. */
  805. int
  806. xfs_bmbt_maxrecs(
  807. struct xfs_mount *mp,
  808. int blocklen,
  809. int leaf)
  810. {
  811. blocklen -= XFS_BMBT_BLOCK_LEN(mp);
  812. if (leaf)
  813. return blocklen / sizeof(xfs_bmbt_rec_t);
  814. return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
  815. }
  816. /*
  817. * Calculate number of records in a bmap btree inode root.
  818. */
  819. int
  820. xfs_bmdr_maxrecs(
  821. int blocklen,
  822. int leaf)
  823. {
  824. blocklen -= sizeof(xfs_bmdr_block_t);
  825. if (leaf)
  826. return blocklen / sizeof(xfs_bmdr_rec_t);
  827. return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t));
  828. }
  829. /*
  830. * Change the owner of a btree format fork fo the inode passed in. Change it to
  831. * the owner of that is passed in so that we can change owners before or after
  832. * we switch forks between inodes. The operation that the caller is doing will
  833. * determine whether is needs to change owner before or after the switch.
  834. *
  835. * For demand paged transactional modification, the fork switch should be done
  836. * after reading in all the blocks, modifying them and pinning them in the
  837. * transaction. For modification when the buffers are already pinned in memory,
  838. * the fork switch can be done before changing the owner as we won't need to
  839. * validate the owner until the btree buffers are unpinned and writes can occur
  840. * again.
  841. *
  842. * For recovery based ownership change, there is no transactional context and
  843. * so a buffer list must be supplied so that we can record the buffers that we
  844. * modified for the caller to issue IO on.
  845. */
  846. int
  847. xfs_bmbt_change_owner(
  848. struct xfs_trans *tp,
  849. struct xfs_inode *ip,
  850. int whichfork,
  851. xfs_ino_t new_owner,
  852. struct list_head *buffer_list)
  853. {
  854. struct xfs_btree_cur *cur;
  855. int error;
  856. ASSERT(tp || buffer_list);
  857. ASSERT(!(tp && buffer_list));
  858. if (whichfork == XFS_DATA_FORK)
  859. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_BTREE);
  860. else
  861. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE);
  862. cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork);
  863. if (!cur)
  864. return ENOMEM;
  865. error = xfs_btree_change_owner(cur, new_owner, buffer_list);
  866. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  867. return error;
  868. }