xfs_bmap_btree.c 21 KB

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