xfs_bmap_btree.c 23 KB

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