xfs_bmap_btree.c 21 KB

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