xfs_bmap_btree.c 23 KB

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