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. /*
  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. 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. ASSERT(be32_to_cpu(rblock->bb_magic) == XFS_BMAP_MAGIC);
  401. ASSERT(be64_to_cpu(rblock->bb_u.l.bb_leftsib) == NULLDFSBNO);
  402. ASSERT(be64_to_cpu(rblock->bb_u.l.bb_rightsib) == NULLDFSBNO);
  403. ASSERT(be16_to_cpu(rblock->bb_level) > 0);
  404. dblock->bb_level = rblock->bb_level;
  405. dblock->bb_numrecs = rblock->bb_numrecs;
  406. dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0);
  407. fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1);
  408. tkp = XFS_BMDR_KEY_ADDR(dblock, 1);
  409. fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen);
  410. tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr);
  411. dmxr = be16_to_cpu(dblock->bb_numrecs);
  412. memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
  413. memcpy(tpp, fpp, sizeof(*fpp) * dmxr);
  414. }
  415. /*
  416. * Check extent records, which have just been read, for
  417. * any bit in the extent flag field. ASSERT on debug
  418. * kernels, as this condition should not occur.
  419. * Return an error condition (1) if any flags found,
  420. * otherwise return 0.
  421. */
  422. int
  423. xfs_check_nostate_extents(
  424. xfs_ifork_t *ifp,
  425. xfs_extnum_t idx,
  426. xfs_extnum_t num)
  427. {
  428. for (; num > 0; num--, idx++) {
  429. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  430. if ((ep->l0 >>
  431. (64 - BMBT_EXNTFLAG_BITLEN)) != 0) {
  432. ASSERT(0);
  433. return 1;
  434. }
  435. }
  436. return 0;
  437. }
  438. STATIC struct xfs_btree_cur *
  439. xfs_bmbt_dup_cursor(
  440. struct xfs_btree_cur *cur)
  441. {
  442. struct xfs_btree_cur *new;
  443. new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp,
  444. cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  445. /*
  446. * Copy the firstblock, flist, and flags values,
  447. * since init cursor doesn't get them.
  448. */
  449. new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
  450. new->bc_private.b.flist = cur->bc_private.b.flist;
  451. new->bc_private.b.flags = cur->bc_private.b.flags;
  452. return new;
  453. }
  454. STATIC void
  455. xfs_bmbt_update_cursor(
  456. struct xfs_btree_cur *src,
  457. struct xfs_btree_cur *dst)
  458. {
  459. ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) ||
  460. (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME));
  461. ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist);
  462. dst->bc_private.b.allocated += src->bc_private.b.allocated;
  463. dst->bc_private.b.firstblock = src->bc_private.b.firstblock;
  464. src->bc_private.b.allocated = 0;
  465. }
  466. STATIC int
  467. xfs_bmbt_alloc_block(
  468. struct xfs_btree_cur *cur,
  469. union xfs_btree_ptr *start,
  470. union xfs_btree_ptr *new,
  471. int length,
  472. int *stat)
  473. {
  474. xfs_alloc_arg_t args; /* block allocation args */
  475. int error; /* error return value */
  476. memset(&args, 0, sizeof(args));
  477. args.tp = cur->bc_tp;
  478. args.mp = cur->bc_mp;
  479. args.fsbno = cur->bc_private.b.firstblock;
  480. args.firstblock = args.fsbno;
  481. if (args.fsbno == NULLFSBLOCK) {
  482. args.fsbno = be64_to_cpu(start->l);
  483. args.type = XFS_ALLOCTYPE_START_BNO;
  484. /*
  485. * Make sure there is sufficient room left in the AG to
  486. * complete a full tree split for an extent insert. If
  487. * we are converting the middle part of an extent then
  488. * we may need space for two tree splits.
  489. *
  490. * We are relying on the caller to make the correct block
  491. * reservation for this operation to succeed. If the
  492. * reservation amount is insufficient then we may fail a
  493. * block allocation here and corrupt the filesystem.
  494. */
  495. args.minleft = xfs_trans_get_block_res(args.tp);
  496. } else if (cur->bc_private.b.flist->xbf_low) {
  497. args.type = XFS_ALLOCTYPE_START_BNO;
  498. } else {
  499. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  500. }
  501. args.minlen = args.maxlen = args.prod = 1;
  502. args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL;
  503. if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) {
  504. error = XFS_ERROR(ENOSPC);
  505. goto error0;
  506. }
  507. error = xfs_alloc_vextent(&args);
  508. if (error)
  509. goto error0;
  510. if (args.fsbno == NULLFSBLOCK && args.minleft) {
  511. /*
  512. * Could not find an AG with enough free space to satisfy
  513. * a full btree split. Try again without minleft and if
  514. * successful activate the lowspace algorithm.
  515. */
  516. args.fsbno = 0;
  517. args.type = XFS_ALLOCTYPE_FIRST_AG;
  518. args.minleft = 0;
  519. error = xfs_alloc_vextent(&args);
  520. if (error)
  521. goto error0;
  522. cur->bc_private.b.flist->xbf_low = 1;
  523. }
  524. if (args.fsbno == NULLFSBLOCK) {
  525. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  526. *stat = 0;
  527. return 0;
  528. }
  529. ASSERT(args.len == 1);
  530. cur->bc_private.b.firstblock = args.fsbno;
  531. cur->bc_private.b.allocated++;
  532. cur->bc_private.b.ip->i_d.di_nblocks++;
  533. xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE);
  534. xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip,
  535. XFS_TRANS_DQ_BCOUNT, 1L);
  536. new->l = cpu_to_be64(args.fsbno);
  537. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  538. *stat = 1;
  539. return 0;
  540. error0:
  541. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  542. return error;
  543. }
  544. STATIC int
  545. xfs_bmbt_free_block(
  546. struct xfs_btree_cur *cur,
  547. struct xfs_buf *bp)
  548. {
  549. struct xfs_mount *mp = cur->bc_mp;
  550. struct xfs_inode *ip = cur->bc_private.b.ip;
  551. struct xfs_trans *tp = cur->bc_tp;
  552. xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp));
  553. xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp);
  554. ip->i_d.di_nblocks--;
  555. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  556. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  557. xfs_trans_binval(tp, bp);
  558. return 0;
  559. }
  560. STATIC int
  561. xfs_bmbt_get_minrecs(
  562. struct xfs_btree_cur *cur,
  563. int level)
  564. {
  565. if (level == cur->bc_nlevels - 1) {
  566. struct xfs_ifork *ifp;
  567. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  568. cur->bc_private.b.whichfork);
  569. return xfs_bmbt_maxrecs(cur->bc_mp,
  570. ifp->if_broot_bytes, level == 0) / 2;
  571. }
  572. return cur->bc_mp->m_bmap_dmnr[level != 0];
  573. }
  574. int
  575. xfs_bmbt_get_maxrecs(
  576. struct xfs_btree_cur *cur,
  577. int level)
  578. {
  579. if (level == cur->bc_nlevels - 1) {
  580. struct xfs_ifork *ifp;
  581. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip,
  582. cur->bc_private.b.whichfork);
  583. return xfs_bmbt_maxrecs(cur->bc_mp,
  584. ifp->if_broot_bytes, level == 0);
  585. }
  586. return cur->bc_mp->m_bmap_dmxr[level != 0];
  587. }
  588. /*
  589. * Get the maximum records we could store in the on-disk format.
  590. *
  591. * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but
  592. * for the root node this checks the available space in the dinode fork
  593. * so that we can resize the in-memory buffer to match it. After a
  594. * resize to the maximum size this function returns the same value
  595. * as xfs_bmbt_get_maxrecs for the root node, too.
  596. */
  597. STATIC int
  598. xfs_bmbt_get_dmaxrecs(
  599. struct xfs_btree_cur *cur,
  600. int level)
  601. {
  602. if (level != cur->bc_nlevels - 1)
  603. return cur->bc_mp->m_bmap_dmxr[level != 0];
  604. return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize,
  605. level == 0);
  606. }
  607. STATIC void
  608. xfs_bmbt_init_key_from_rec(
  609. union xfs_btree_key *key,
  610. union xfs_btree_rec *rec)
  611. {
  612. key->bmbt.br_startoff =
  613. cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt));
  614. }
  615. STATIC void
  616. xfs_bmbt_init_rec_from_key(
  617. union xfs_btree_key *key,
  618. union xfs_btree_rec *rec)
  619. {
  620. ASSERT(key->bmbt.br_startoff != 0);
  621. xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff),
  622. 0, 0, XFS_EXT_NORM);
  623. }
  624. STATIC void
  625. xfs_bmbt_init_rec_from_cur(
  626. struct xfs_btree_cur *cur,
  627. union xfs_btree_rec *rec)
  628. {
  629. xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b);
  630. }
  631. STATIC void
  632. xfs_bmbt_init_ptr_from_cur(
  633. struct xfs_btree_cur *cur,
  634. union xfs_btree_ptr *ptr)
  635. {
  636. ptr->l = 0;
  637. }
  638. STATIC __int64_t
  639. xfs_bmbt_key_diff(
  640. struct xfs_btree_cur *cur,
  641. union xfs_btree_key *key)
  642. {
  643. return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) -
  644. cur->bc_rec.b.br_startoff;
  645. }
  646. #ifdef DEBUG
  647. STATIC int
  648. xfs_bmbt_keys_inorder(
  649. struct xfs_btree_cur *cur,
  650. union xfs_btree_key *k1,
  651. union xfs_btree_key *k2)
  652. {
  653. return be64_to_cpu(k1->bmbt.br_startoff) <
  654. be64_to_cpu(k2->bmbt.br_startoff);
  655. }
  656. STATIC int
  657. xfs_bmbt_recs_inorder(
  658. struct xfs_btree_cur *cur,
  659. union xfs_btree_rec *r1,
  660. union xfs_btree_rec *r2)
  661. {
  662. return xfs_bmbt_disk_get_startoff(&r1->bmbt) +
  663. xfs_bmbt_disk_get_blockcount(&r1->bmbt) <=
  664. xfs_bmbt_disk_get_startoff(&r2->bmbt);
  665. }
  666. #endif /* DEBUG */
  667. #ifdef XFS_BTREE_TRACE
  668. ktrace_t *xfs_bmbt_trace_buf;
  669. STATIC void
  670. xfs_bmbt_trace_enter(
  671. struct xfs_btree_cur *cur,
  672. const char *func,
  673. char *s,
  674. int type,
  675. int line,
  676. __psunsigned_t a0,
  677. __psunsigned_t a1,
  678. __psunsigned_t a2,
  679. __psunsigned_t a3,
  680. __psunsigned_t a4,
  681. __psunsigned_t a5,
  682. __psunsigned_t a6,
  683. __psunsigned_t a7,
  684. __psunsigned_t a8,
  685. __psunsigned_t a9,
  686. __psunsigned_t a10)
  687. {
  688. struct xfs_inode *ip = cur->bc_private.b.ip;
  689. int whichfork = cur->bc_private.b.whichfork;
  690. ktrace_enter(xfs_bmbt_trace_buf,
  691. (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
  692. (void *)func, (void *)s, (void *)ip, (void *)cur,
  693. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  694. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  695. (void *)a8, (void *)a9, (void *)a10);
  696. ktrace_enter(ip->i_btrace,
  697. (void *)((__psint_t)type | (whichfork << 8) | (line << 16)),
  698. (void *)func, (void *)s, (void *)ip, (void *)cur,
  699. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  700. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  701. (void *)a8, (void *)a9, (void *)a10);
  702. }
  703. STATIC void
  704. xfs_bmbt_trace_cursor(
  705. struct xfs_btree_cur *cur,
  706. __uint32_t *s0,
  707. __uint64_t *l0,
  708. __uint64_t *l1)
  709. {
  710. struct xfs_bmbt_rec_host r;
  711. xfs_bmbt_set_all(&r, &cur->bc_rec.b);
  712. *s0 = (cur->bc_nlevels << 24) |
  713. (cur->bc_private.b.flags << 16) |
  714. cur->bc_private.b.allocated;
  715. *l0 = r.l0;
  716. *l1 = r.l1;
  717. }
  718. STATIC void
  719. xfs_bmbt_trace_key(
  720. struct xfs_btree_cur *cur,
  721. union xfs_btree_key *key,
  722. __uint64_t *l0,
  723. __uint64_t *l1)
  724. {
  725. *l0 = be64_to_cpu(key->bmbt.br_startoff);
  726. *l1 = 0;
  727. }
  728. /* Endian flipping versions of the bmbt extraction functions */
  729. STATIC void
  730. xfs_bmbt_disk_get_all(
  731. xfs_bmbt_rec_t *r,
  732. xfs_bmbt_irec_t *s)
  733. {
  734. __xfs_bmbt_get_all(get_unaligned_be64(&r->l0),
  735. get_unaligned_be64(&r->l1), s);
  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. }