xfs_bmap_btree.c 21 KB

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