xfs_bmap_btree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_btree_trace.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_itable.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_error.h"
  45. #include "xfs_quota.h"
  46. /*
  47. * Determine the extent state.
  48. */
  49. /* ARGSUSED */
  50. STATIC xfs_exntst_t
  51. xfs_extent_state(
  52. xfs_filblks_t blks,
  53. int extent_flag)
  54. {
  55. if (extent_flag) {
  56. ASSERT(blks != 0); /* saved for DMIG */
  57. return XFS_EXT_UNWRITTEN;
  58. }
  59. return XFS_EXT_NORM;
  60. }
  61. /*
  62. * Convert on-disk form of btree root to in-memory form.
  63. */
  64. void
  65. xfs_bmdr_to_bmbt(
  66. struct xfs_mount *mp,
  67. xfs_bmdr_block_t *dblock,
  68. int dblocklen,
  69. struct xfs_btree_block *rblock,
  70. int rblocklen)
  71. {
  72. int dmxr;
  73. xfs_bmbt_key_t *fkp;
  74. __be64 *fpp;
  75. xfs_bmbt_key_t *tkp;
  76. __be64 *tpp;
  77. rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  78. rblock->bb_level = dblock->bb_level;
  79. ASSERT(be16_to_cpu(rblock->bb_level) > 0);
  80. rblock->bb_numrecs = dblock->bb_numrecs;
  81. rblock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  82. rblock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  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_INLINE 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. /* Endian flipping versions of the bmbt extraction functions */
  191. void
  192. xfs_bmbt_disk_get_all(
  193. xfs_bmbt_rec_t *r,
  194. xfs_bmbt_irec_t *s)
  195. {
  196. __xfs_bmbt_get_all(be64_to_cpu(r->l0), be64_to_cpu(r->l1), s);
  197. }
  198. /*
  199. * Extract the blockcount field from an on disk bmap extent record.
  200. */
  201. xfs_filblks_t
  202. xfs_bmbt_disk_get_blockcount(
  203. xfs_bmbt_rec_t *r)
  204. {
  205. return (xfs_filblks_t)(be64_to_cpu(r->l1) & XFS_MASK64LO(21));
  206. }
  207. /*
  208. * Extract the startoff field from a disk format bmap extent record.
  209. */
  210. xfs_fileoff_t
  211. xfs_bmbt_disk_get_startoff(
  212. xfs_bmbt_rec_t *r)
  213. {
  214. return ((xfs_fileoff_t)be64_to_cpu(r->l0) &
  215. XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN)) >> 9;
  216. }
  217. /*
  218. * Set all the fields in a bmap extent record from the arguments.
  219. */
  220. void
  221. xfs_bmbt_set_allf(
  222. xfs_bmbt_rec_host_t *r,
  223. xfs_fileoff_t startoff,
  224. xfs_fsblock_t startblock,
  225. xfs_filblks_t blockcount,
  226. xfs_exntst_t state)
  227. {
  228. int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
  229. ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
  230. ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
  231. ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
  232. #if XFS_BIG_BLKNOS
  233. ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
  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)startblock >> 43);
  237. r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
  238. ((xfs_bmbt_rec_base_t)blockcount &
  239. (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
  240. #else /* !XFS_BIG_BLKNOS */
  241. if (ISNULLSTARTBLOCK(startblock)) {
  242. r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  243. ((xfs_bmbt_rec_base_t)startoff << 9) |
  244. (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
  245. r->l1 = XFS_MASK64HI(11) |
  246. ((xfs_bmbt_rec_base_t)startblock << 21) |
  247. ((xfs_bmbt_rec_base_t)blockcount &
  248. (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
  249. } else {
  250. r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  251. ((xfs_bmbt_rec_base_t)startoff << 9);
  252. r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) |
  253. ((xfs_bmbt_rec_base_t)blockcount &
  254. (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
  255. }
  256. #endif /* XFS_BIG_BLKNOS */
  257. }
  258. /*
  259. * Set all the fields in a bmap extent record from the uncompressed form.
  260. */
  261. void
  262. xfs_bmbt_set_all(
  263. xfs_bmbt_rec_host_t *r,
  264. xfs_bmbt_irec_t *s)
  265. {
  266. xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock,
  267. s->br_blockcount, s->br_state);
  268. }
  269. /*
  270. * Set all the fields in a disk format bmap extent record from the arguments.
  271. */
  272. void
  273. xfs_bmbt_disk_set_allf(
  274. xfs_bmbt_rec_t *r,
  275. xfs_fileoff_t startoff,
  276. xfs_fsblock_t startblock,
  277. xfs_filblks_t blockcount,
  278. xfs_exntst_t state)
  279. {
  280. int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1;
  281. ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN);
  282. ASSERT((startoff & XFS_MASK64HI(64-BMBT_STARTOFF_BITLEN)) == 0);
  283. ASSERT((blockcount & XFS_MASK64HI(64-BMBT_BLOCKCOUNT_BITLEN)) == 0);
  284. #if XFS_BIG_BLKNOS
  285. ASSERT((startblock & XFS_MASK64HI(64-BMBT_STARTBLOCK_BITLEN)) == 0);
  286. r->l0 = cpu_to_be64(
  287. ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  288. ((xfs_bmbt_rec_base_t)startoff << 9) |
  289. ((xfs_bmbt_rec_base_t)startblock >> 43));
  290. r->l1 = cpu_to_be64(
  291. ((xfs_bmbt_rec_base_t)startblock << 21) |
  292. ((xfs_bmbt_rec_base_t)blockcount &
  293. (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
  294. #else /* !XFS_BIG_BLKNOS */
  295. if (ISNULLSTARTBLOCK(startblock)) {
  296. r->l0 = cpu_to_be64(
  297. ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  298. ((xfs_bmbt_rec_base_t)startoff << 9) |
  299. (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
  300. r->l1 = cpu_to_be64(XFS_MASK64HI(11) |
  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. } else {
  305. r->l0 = cpu_to_be64(
  306. ((xfs_bmbt_rec_base_t)extent_flag << 63) |
  307. ((xfs_bmbt_rec_base_t)startoff << 9));
  308. r->l1 = cpu_to_be64(
  309. ((xfs_bmbt_rec_base_t)startblock << 21) |
  310. ((xfs_bmbt_rec_base_t)blockcount &
  311. (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)));
  312. }
  313. #endif /* XFS_BIG_BLKNOS */
  314. }
  315. /*
  316. * Set all the fields in a bmap extent record from the uncompressed form.
  317. */
  318. void
  319. xfs_bmbt_disk_set_all(
  320. xfs_bmbt_rec_t *r,
  321. xfs_bmbt_irec_t *s)
  322. {
  323. xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock,
  324. s->br_blockcount, s->br_state);
  325. }
  326. /*
  327. * Set the blockcount field in a bmap extent record.
  328. */
  329. void
  330. xfs_bmbt_set_blockcount(
  331. xfs_bmbt_rec_host_t *r,
  332. xfs_filblks_t v)
  333. {
  334. ASSERT((v & XFS_MASK64HI(43)) == 0);
  335. r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(43)) |
  336. (xfs_bmbt_rec_base_t)(v & XFS_MASK64LO(21));
  337. }
  338. /*
  339. * Set the startblock field in a bmap extent record.
  340. */
  341. void
  342. xfs_bmbt_set_startblock(
  343. xfs_bmbt_rec_host_t *r,
  344. xfs_fsblock_t v)
  345. {
  346. #if XFS_BIG_BLKNOS
  347. ASSERT((v & XFS_MASK64HI(12)) == 0);
  348. r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64HI(55)) |
  349. (xfs_bmbt_rec_base_t)(v >> 43);
  350. r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21)) |
  351. (xfs_bmbt_rec_base_t)(v << 21);
  352. #else /* !XFS_BIG_BLKNOS */
  353. if (ISNULLSTARTBLOCK(v)) {
  354. r->l0 |= (xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
  355. r->l1 = (xfs_bmbt_rec_base_t)XFS_MASK64HI(11) |
  356. ((xfs_bmbt_rec_base_t)v << 21) |
  357. (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
  358. } else {
  359. r->l0 &= ~(xfs_bmbt_rec_base_t)XFS_MASK64LO(9);
  360. r->l1 = ((xfs_bmbt_rec_base_t)v << 21) |
  361. (r->l1 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(21));
  362. }
  363. #endif /* XFS_BIG_BLKNOS */
  364. }
  365. /*
  366. * Set the startoff field in a bmap extent record.
  367. */
  368. void
  369. xfs_bmbt_set_startoff(
  370. xfs_bmbt_rec_host_t *r,
  371. xfs_fileoff_t v)
  372. {
  373. ASSERT((v & XFS_MASK64HI(9)) == 0);
  374. r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) XFS_MASK64HI(1)) |
  375. ((xfs_bmbt_rec_base_t)v << 9) |
  376. (r->l0 & (xfs_bmbt_rec_base_t)XFS_MASK64LO(9));
  377. }
  378. /*
  379. * Set the extent state field in a bmap extent record.
  380. */
  381. void
  382. xfs_bmbt_set_state(
  383. xfs_bmbt_rec_host_t *r,
  384. xfs_exntst_t v)
  385. {
  386. ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN);
  387. if (v == XFS_EXT_NORM)
  388. r->l0 &= XFS_MASK64LO(64 - BMBT_EXNTFLAG_BITLEN);
  389. else
  390. r->l0 |= XFS_MASK64HI(BMBT_EXNTFLAG_BITLEN);
  391. }
  392. /*
  393. * Convert in-memory form of btree root to on-disk form.
  394. */
  395. void
  396. xfs_bmbt_to_bmdr(
  397. struct xfs_mount *mp,
  398. struct xfs_btree_block *rblock,
  399. int rblocklen,
  400. xfs_bmdr_block_t *dblock,
  401. int dblocklen)
  402. {
  403. int dmxr;
  404. xfs_bmbt_key_t *fkp;
  405. __be64 *fpp;
  406. xfs_bmbt_key_t *tkp;
  407. __be64 *tpp;
  408. ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
  409. ASSERT(be64_to_cpu(rblock->bb_u.l.bb_leftsib) == NULLDFSBNO);
  410. ASSERT(be64_to_cpu(rblock->bb_u.l.bb_rightsib) == NULLDFSBNO);
  411. ASSERT(be16_to_cpu(rblock->bb_level) > 0);
  412. dblock->bb_level = rblock->bb_level;
  413. dblock->bb_numrecs = rblock->bb_numrecs;
  414. dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
  415. fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  416. tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  417. fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  418. tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  419. dmxr = be16_to_cpu(dblock->bb_numrecs);
  420. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  421. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  422. }
  423. /*
  424. * Check extent records, which have just been read, for
  425. * any bit in the extent flag field. ASSERT on debug
  426. * kernels, as this condition should not occur.
  427. * Return an error condition (1) if any flags found,
  428. * otherwise return 0.
  429. */
  430. int
  431. xfs_check_nostate_extents(
  432. xfs_ifork_t *ifp,
  433. xfs_extnum_t idx,
  434. xfs_extnum_t num)
  435. {
  436. for (; num > 0; num--, idx++) {
  437. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  438. if ((ep->l0 >>
  439. (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
  440. ASSERT(0);
  441. return 1;
  442. }
  443. }
  444. return 0;
  445. }
  446. STATIC struct xfs_btree_cur *
  447. xfs_bmbt_dup_cursor(
  448. struct xfs_btree_cur *cur)
  449. {
  450. struct xfs_btree_cur *new;
  451. new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
  452. cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  453. /*
  454. * Copy the firstblock, flist, and flags values,
  455. * since init cursor doesn't get them.
  456. */
  457. new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
  458. new->bc_private.b.flist = cur->bc_private.b.flist;
  459. new->bc_private.b.flags = cur->bc_private.b.flags;
  460. return new;
  461. }
  462. STATIC void
  463. xfs_bmbt_update_cursor(
  464. struct xfs_btree_cur *src,
  465. struct xfs_btree_cur *dst)
  466. {
  467. ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
  468. (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
  469. ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
  470. dst->bc_private.b.allocated += src->bc_private.b.allocated;
  471. dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
  472. src->bc_private.b.allocated = 0;
  473. }
  474. STATIC int
  475. xfs_bmbt_alloc_block(
  476. struct xfs_btree_cur *cur,
  477. union xfs_btree_ptr *start,
  478. union xfs_btree_ptr *new,
  479. int length,
  480. int *stat)
  481. {
  482. xfs_alloc_arg_t args; /* block allocation args */
  483. int error; /* error return value */
  484. memset(&args, 0, sizeof(args));
  485. args.tp = cur->bc_tp;
  486. args.mp = cur->bc_mp;
  487. args.fsbno = cur->bc_private.b.firstblock;
  488. args.firstblock = args.fsbno;
  489. if (args.fsbno == NULLFSBLOCK) {
  490. args.fsbno = be64_to_cpu(start->l);
  491. args.type = XFS_ALLOCTYPE_START_BNO;
  492. /*
  493. * Make sure there is sufficient room left in the AG to
  494. * complete a full tree split for an extent insert. If
  495. * we are converting the middle part of an extent then
  496. * we may need space for two tree splits.
  497. *
  498. * We are relying on the caller to make the correct block
  499. * reservation for this operation to succeed. If the
  500. * reservation amount is insufficient then we may fail a
  501. * block allocation here and corrupt the filesystem.
  502. */
  503. args.minleft = xfs_trans_get_block_res(args.tp);
  504. } else if (cur->bc_private.b.flist->xbf_low) {
  505. args.type = XFS_ALLOCTYPE_START_BNO;
  506. } else {
  507. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  508. }
  509. args.minlen = args.maxlen = args.prod = 1;
  510. args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
  511. if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
  512. error = XFS_ERROR(ENOSPC);
  513. goto error0;
  514. }
  515. error = xfs_alloc_vextent(&args);
  516. if (error)
  517. goto error0;
  518. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  519. /*
  520. * Could not find an AG with enough free space to satisfy
  521. * a full btree split. Try again without minleft and if
  522. * successful activate the lowspace algorithm.
  523. */
  524. args.fsbno = 0;
  525. args.type = XFS_ALLOCTYPE_FIRST_AG;
  526. args.minleft = 0;
  527. error = xfs_alloc_vextent(&args);
  528. if (error)
  529. goto error0;
  530. cur->bc_private.b.flist->xbf_low = 1;
  531. }
  532. if (args.fsbno == NULLFSBLOCK) {
  533. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  534. *stat = 0;
  535. return 0;
  536. }
  537. ASSERT(args.len == 1);
  538. cur->bc_private.b.firstblock = args.fsbno;
  539. cur->bc_private.b.allocated++;
  540. cur->bc_private.b.ip->i_d.di_nblocks++;
  541. xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
  542. XFS_TRANS_MOD_DQUOT_BYINO(args.mp, args.tp, cur->bc_private.b.ip,
  543. XFS_TRANS_DQ_BCOUNT, 1L);
  544. new->l = cpu_to_be64(args.fsbno);
  545. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  546. *stat = 1;
  547. return 0;
  548. error0:
  549. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  550. return error;
  551. }
  552. STATIC int
  553. xfs_bmbt_free_block(
  554. struct xfs_btree_cur *cur,
  555. struct xfs_buf *bp)
  556. {
  557. struct xfs_mount *mp = cur->bc_mp;
  558. struct xfs_inode *ip = cur->bc_private.b.ip;
  559. struct xfs_trans *tp = cur->bc_tp;
  560. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  561. xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
  562. ip->i_d.di_nblocks--;
  563. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  564. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  565. xfs_trans_binval(tp, bp);
  566. return 0;
  567. }
  568. STATIC int
  569. xfs_bmbt_get_minrecs(
  570. struct xfs_btree_cur *cur,
  571. int level)
  572. {
  573. if (level == cur->bc_nlevels - 1) {
  574. struct xfs_ifork *ifp;
  575. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  576. cur->bc_private.b.whichfork);
  577. return xfs_bmbt_maxrecs(cur->bc_mp,
  578. ifp->if_broot_bytes, level == 0) / 2;
  579. }
  580. return cur->bc_mp->m_bmap_dmnr[level != 0];
  581. }
  582. int
  583. xfs_bmbt_get_maxrecs(
  584. struct xfs_btree_cur *cur,
  585. int level)
  586. {
  587. if (level == cur->bc_nlevels - 1) {
  588. struct xfs_ifork *ifp;
  589. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  590. cur->bc_private.b.whichfork);
  591. return xfs_bmbt_maxrecs(cur->bc_mp,
  592. ifp->if_broot_bytes, level == 0);
  593. }
  594. return cur->bc_mp->m_bmap_dmxr[level != 0];
  595. }
  596. /*
  597. * Get the maximum records we could store in the on-disk format.
  598. *
  599. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  600. * for the root node this checks the available space in the dinode fork
  601. * so that we can resize the in-memory buffer to match it. After a
  602. * resize to the maximum size this function returns the same value
  603. * as xfs_bmbt_get_maxrecs for the root node, too.
  604. */
  605. STATIC int
  606. xfs_bmbt_get_dmaxrecs(
  607. struct xfs_btree_cur *cur,
  608. int level)
  609. {
  610. if (level != cur->bc_nlevels - 1)
  611. return cur->bc_mp->m_bmap_dmxr[level != 0];
  612. return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize,
  613. level == 0);
  614. }
  615. STATIC void
  616. xfs_bmbt_init_key_from_rec(
  617. union xfs_btree_key *key,
  618. union xfs_btree_rec *rec)
  619. {
  620. key->bmbt.br_startoff =
  621. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  622. }
  623. STATIC void
  624. xfs_bmbt_init_rec_from_key(
  625. union xfs_btree_key *key,
  626. union xfs_btree_rec *rec)
  627. {
  628. ASSERT(key->bmbt.br_startoff != 0);
  629. xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
  630. 0, 0, XFS_EXT_NORM);
  631. }
  632. STATIC void
  633. xfs_bmbt_init_rec_from_cur(
  634. struct xfs_btree_cur *cur,
  635. union xfs_btree_rec *rec)
  636. {
  637. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  638. }
  639. STATIC void
  640. xfs_bmbt_init_ptr_from_cur(
  641. struct xfs_btree_cur *cur,
  642. union xfs_btree_ptr *ptr)
  643. {
  644. ptr->l = 0;
  645. }
  646. STATIC __int64_t
  647. xfs_bmbt_key_diff(
  648. struct xfs_btree_cur *cur,
  649. union xfs_btree_key *key)
  650. {
  651. return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  652. cur->bc_rec.b.br_startoff;
  653. }
  654. #ifdef DEBUG
  655. STATIC int
  656. xfs_bmbt_keys_inorder(
  657. struct xfs_btree_cur *cur,
  658. union xfs_btree_key *k1,
  659. union xfs_btree_key *k2)
  660. {
  661. return be64_to_cpu(k1->bmbt.br_startoff) <
  662. be64_to_cpu(k2->bmbt.br_startoff);
  663. }
  664. STATIC int
  665. xfs_bmbt_recs_inorder(
  666. struct xfs_btree_cur *cur,
  667. union xfs_btree_rec *r1,
  668. union xfs_btree_rec *r2)
  669. {
  670. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  671. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  672. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  673. }
  674. #endif /* DEBUG */
  675. #ifdef XFS_BTREE_TRACE
  676. ktrace_t *xfs_bmbt_trace_buf;
  677. STATIC void
  678. xfs_bmbt_trace_enter(
  679. struct xfs_btree_cur *cur,
  680. const char *func,
  681. char *s,
  682. int type,
  683. int line,
  684. __psunsigned_t a0,
  685. __psunsigned_t a1,
  686. __psunsigned_t a2,
  687. __psunsigned_t a3,
  688. __psunsigned_t a4,
  689. __psunsigned_t a5,
  690. __psunsigned_t a6,
  691. __psunsigned_t a7,
  692. __psunsigned_t a8,
  693. __psunsigned_t a9,
  694. __psunsigned_t a10)
  695. {
  696. struct xfs_inode *ip = cur->bc_private.b.ip;
  697. int whichfork = cur->bc_private.b.whichfork;
  698. ktrace_enter(xfs_bmbt_trace_buf,
  699. (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
  700. (void *)func, (void *)s, (void *)ip, (void *)cur,
  701. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  702. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  703. (void *)a8, (void *)a9, (void *)a10);
  704. ktrace_enter(ip->i_btrace,
  705. (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
  706. (void *)func, (void *)s, (void *)ip, (void *)cur,
  707. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  708. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  709. (void *)a8, (void *)a9, (void *)a10);
  710. }
  711. STATIC void
  712. xfs_bmbt_trace_cursor(
  713. struct xfs_btree_cur *cur,
  714. __uint32_t *s0,
  715. __uint64_t *l0,
  716. __uint64_t *l1)
  717. {
  718. struct xfs_bmbt_rec_host r;
  719. xfs_bmbt_set_all(&r, &cur->bc_rec.b);
  720. *s0 = (cur->bc_nlevels << 24) |
  721. (cur->bc_private.b.flags << 16) |
  722. cur->bc_private.b.allocated;
  723. *l0 = r.l0;
  724. *l1 = r.l1;
  725. }
  726. STATIC void
  727. xfs_bmbt_trace_key(
  728. struct xfs_btree_cur *cur,
  729. union xfs_btree_key *key,
  730. __uint64_t *l0,
  731. __uint64_t *l1)
  732. {
  733. *l0 = be64_to_cpu(key->bmbt.br_startoff);
  734. *l1 = 0;
  735. }
  736. STATIC void
  737. xfs_bmbt_trace_record(
  738. struct xfs_btree_cur *cur,
  739. union xfs_btree_rec *rec,
  740. __uint64_t *l0,
  741. __uint64_t *l1,
  742. __uint64_t *l2)
  743. {
  744. struct xfs_bmbt_irec irec;
  745. xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
  746. *l0 = irec.br_startoff;
  747. *l1 = irec.br_startblock;
  748. *l2 = irec.br_blockcount;
  749. }
  750. #endif /* XFS_BTREE_TRACE */
  751. static const struct xfs_btree_ops xfs_bmbt_ops = {
  752. .rec_len = sizeof(xfs_bmbt_rec_t),
  753. .key_len = sizeof(xfs_bmbt_key_t),
  754. .dup_cursor = xfs_bmbt_dup_cursor,
  755. .update_cursor = xfs_bmbt_update_cursor,
  756. .alloc_block = xfs_bmbt_alloc_block,
  757. .free_block = xfs_bmbt_free_block,
  758. .get_maxrecs = xfs_bmbt_get_maxrecs,
  759. .get_minrecs = xfs_bmbt_get_minrecs,
  760. .get_dmaxrecs = xfs_bmbt_get_dmaxrecs,
  761. .init_key_from_rec = xfs_bmbt_init_key_from_rec,
  762. .init_rec_from_key = xfs_bmbt_init_rec_from_key,
  763. .init_rec_from_cur = xfs_bmbt_init_rec_from_cur,
  764. .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur,
  765. .key_diff = xfs_bmbt_key_diff,
  766. #ifdef DEBUG
  767. .keys_inorder = xfs_bmbt_keys_inorder,
  768. .recs_inorder = xfs_bmbt_recs_inorder,
  769. #endif
  770. #ifdef XFS_BTREE_TRACE
  771. .trace_enter = xfs_bmbt_trace_enter,
  772. .trace_cursor = xfs_bmbt_trace_cursor,
  773. .trace_key = xfs_bmbt_trace_key,
  774. .trace_record = xfs_bmbt_trace_record,
  775. #endif
  776. };
  777. /*
  778. * Allocate a new bmap btree cursor.
  779. */
  780. struct xfs_btree_cur * /* new bmap btree cursor */
  781. xfs_bmbt_init_cursor(
  782. struct xfs_mount *mp, /* file system mount point */
  783. struct xfs_trans *tp, /* transaction pointer */
  784. struct xfs_inode *ip, /* inode owning the btree */
  785. int whichfork) /* data or attr fork */
  786. {
  787. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  788. struct xfs_btree_cur *cur;
  789. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  790. cur->bc_tp = tp;
  791. cur->bc_mp = mp;
  792. cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  793. cur->bc_btnum = XFS_BTNUM_BMAP;
  794. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  795. cur->bc_ops = &xfs_bmbt_ops;
  796. cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE;
  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. }