xfs_refcount.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_refcount_btree.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_errortag.h"
  20. #include "xfs_error.h"
  21. #include "xfs_trace.h"
  22. #include "xfs_cksum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_refcount.h"
  26. #include "xfs_rmap.h"
  27. /* Allowable refcount adjustment amounts. */
  28. enum xfs_refc_adjust_op {
  29. XFS_REFCOUNT_ADJUST_INCREASE = 1,
  30. XFS_REFCOUNT_ADJUST_DECREASE = -1,
  31. XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
  32. XFS_REFCOUNT_ADJUST_COW_FREE = -1,
  33. };
  34. STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
  35. xfs_agblock_t agbno, xfs_extlen_t aglen);
  36. STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
  37. xfs_agblock_t agbno, xfs_extlen_t aglen);
  38. /*
  39. * Look up the first record less than or equal to [bno, len] in the btree
  40. * given by cur.
  41. */
  42. int
  43. xfs_refcount_lookup_le(
  44. struct xfs_btree_cur *cur,
  45. xfs_agblock_t bno,
  46. int *stat)
  47. {
  48. trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
  49. XFS_LOOKUP_LE);
  50. cur->bc_rec.rc.rc_startblock = bno;
  51. cur->bc_rec.rc.rc_blockcount = 0;
  52. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  53. }
  54. /*
  55. * Look up the first record greater than or equal to [bno, len] in the btree
  56. * given by cur.
  57. */
  58. int
  59. xfs_refcount_lookup_ge(
  60. struct xfs_btree_cur *cur,
  61. xfs_agblock_t bno,
  62. int *stat)
  63. {
  64. trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
  65. XFS_LOOKUP_GE);
  66. cur->bc_rec.rc.rc_startblock = bno;
  67. cur->bc_rec.rc.rc_blockcount = 0;
  68. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  69. }
  70. /*
  71. * Look up the first record equal to [bno, len] in the btree
  72. * given by cur.
  73. */
  74. int
  75. xfs_refcount_lookup_eq(
  76. struct xfs_btree_cur *cur,
  77. xfs_agblock_t bno,
  78. int *stat)
  79. {
  80. trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
  81. XFS_LOOKUP_LE);
  82. cur->bc_rec.rc.rc_startblock = bno;
  83. cur->bc_rec.rc.rc_blockcount = 0;
  84. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  85. }
  86. /* Convert on-disk record to in-core format. */
  87. void
  88. xfs_refcount_btrec_to_irec(
  89. union xfs_btree_rec *rec,
  90. struct xfs_refcount_irec *irec)
  91. {
  92. irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock);
  93. irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
  94. irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
  95. }
  96. /*
  97. * Get the data from the pointed-to record.
  98. */
  99. int
  100. xfs_refcount_get_rec(
  101. struct xfs_btree_cur *cur,
  102. struct xfs_refcount_irec *irec,
  103. int *stat)
  104. {
  105. struct xfs_mount *mp = cur->bc_mp;
  106. xfs_agnumber_t agno = cur->bc_private.a.agno;
  107. union xfs_btree_rec *rec;
  108. int error;
  109. xfs_agblock_t realstart;
  110. error = xfs_btree_get_rec(cur, &rec, stat);
  111. if (error || !*stat)
  112. return error;
  113. xfs_refcount_btrec_to_irec(rec, irec);
  114. agno = cur->bc_private.a.agno;
  115. if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
  116. goto out_bad_rec;
  117. /* handle special COW-staging state */
  118. realstart = irec->rc_startblock;
  119. if (realstart & XFS_REFC_COW_START) {
  120. if (irec->rc_refcount != 1)
  121. goto out_bad_rec;
  122. realstart &= ~XFS_REFC_COW_START;
  123. } else if (irec->rc_refcount < 2) {
  124. goto out_bad_rec;
  125. }
  126. /* check for valid extent range, including overflow */
  127. if (!xfs_verify_agbno(mp, agno, realstart))
  128. goto out_bad_rec;
  129. if (realstart > realstart + irec->rc_blockcount)
  130. goto out_bad_rec;
  131. if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1))
  132. goto out_bad_rec;
  133. if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
  134. goto out_bad_rec;
  135. trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno, irec);
  136. return 0;
  137. out_bad_rec:
  138. xfs_warn(mp,
  139. "Refcount BTree record corruption in AG %d detected!", agno);
  140. xfs_warn(mp,
  141. "Start block 0x%x, block count 0x%x, references 0x%x",
  142. irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
  143. return -EFSCORRUPTED;
  144. }
  145. /*
  146. * Update the record referred to by cur to the value given
  147. * by [bno, len, refcount].
  148. * This either works (return 0) or gets an EFSCORRUPTED error.
  149. */
  150. STATIC int
  151. xfs_refcount_update(
  152. struct xfs_btree_cur *cur,
  153. struct xfs_refcount_irec *irec)
  154. {
  155. union xfs_btree_rec rec;
  156. int error;
  157. trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec);
  158. rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock);
  159. rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
  160. rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
  161. error = xfs_btree_update(cur, &rec);
  162. if (error)
  163. trace_xfs_refcount_update_error(cur->bc_mp,
  164. cur->bc_private.a.agno, error, _RET_IP_);
  165. return error;
  166. }
  167. /*
  168. * Insert the record referred to by cur to the value given
  169. * by [bno, len, refcount].
  170. * This either works (return 0) or gets an EFSCORRUPTED error.
  171. */
  172. int
  173. xfs_refcount_insert(
  174. struct xfs_btree_cur *cur,
  175. struct xfs_refcount_irec *irec,
  176. int *i)
  177. {
  178. int error;
  179. trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec);
  180. cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
  181. cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
  182. cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
  183. error = xfs_btree_insert(cur, i);
  184. if (error)
  185. goto out_error;
  186. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
  187. out_error:
  188. if (error)
  189. trace_xfs_refcount_insert_error(cur->bc_mp,
  190. cur->bc_private.a.agno, error, _RET_IP_);
  191. return error;
  192. }
  193. /*
  194. * Remove the record referred to by cur, then set the pointer to the spot
  195. * where the record could be re-inserted, in case we want to increment or
  196. * decrement the cursor.
  197. * This either works (return 0) or gets an EFSCORRUPTED error.
  198. */
  199. STATIC int
  200. xfs_refcount_delete(
  201. struct xfs_btree_cur *cur,
  202. int *i)
  203. {
  204. struct xfs_refcount_irec irec;
  205. int found_rec;
  206. int error;
  207. error = xfs_refcount_get_rec(cur, &irec, &found_rec);
  208. if (error)
  209. goto out_error;
  210. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  211. trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec);
  212. error = xfs_btree_delete(cur, i);
  213. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
  214. if (error)
  215. goto out_error;
  216. error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec);
  217. out_error:
  218. if (error)
  219. trace_xfs_refcount_delete_error(cur->bc_mp,
  220. cur->bc_private.a.agno, error, _RET_IP_);
  221. return error;
  222. }
  223. /*
  224. * Adjusting the Reference Count
  225. *
  226. * As stated elsewhere, the reference count btree (refcbt) stores
  227. * >1 reference counts for extents of physical blocks. In this
  228. * operation, we're either raising or lowering the reference count of
  229. * some subrange stored in the tree:
  230. *
  231. * <------ adjustment range ------>
  232. * ----+ +---+-----+ +--+--------+---------
  233. * 2 | | 3 | 4 | |17| 55 | 10
  234. * ----+ +---+-----+ +--+--------+---------
  235. * X axis is physical blocks number;
  236. * reference counts are the numbers inside the rectangles
  237. *
  238. * The first thing we need to do is to ensure that there are no
  239. * refcount extents crossing either boundary of the range to be
  240. * adjusted. For any extent that does cross a boundary, split it into
  241. * two extents so that we can increment the refcount of one of the
  242. * pieces later:
  243. *
  244. * <------ adjustment range ------>
  245. * ----+ +---+-----+ +--+--------+----+----
  246. * 2 | | 3 | 2 | |17| 55 | 10 | 10
  247. * ----+ +---+-----+ +--+--------+----+----
  248. *
  249. * For this next step, let's assume that all the physical blocks in
  250. * the adjustment range are mapped to a file and are therefore in use
  251. * at least once. Therefore, we can infer that any gap in the
  252. * refcount tree within the adjustment range represents a physical
  253. * extent with refcount == 1:
  254. *
  255. * <------ adjustment range ------>
  256. * ----+---+---+-----+-+--+--------+----+----
  257. * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
  258. * ----+---+---+-----+-+--+--------+----+----
  259. * ^
  260. *
  261. * For each extent that falls within the interval range, figure out
  262. * which extent is to the left or the right of that extent. Now we
  263. * have a left, current, and right extent. If the new reference count
  264. * of the center extent enables us to merge left, center, and right
  265. * into one record covering all three, do so. If the center extent is
  266. * at the left end of the range, abuts the left extent, and its new
  267. * reference count matches the left extent's record, then merge them.
  268. * If the center extent is at the right end of the range, abuts the
  269. * right extent, and the reference counts match, merge those. In the
  270. * example, we can left merge (assuming an increment operation):
  271. *
  272. * <------ adjustment range ------>
  273. * --------+---+-----+-+--+--------+----+----
  274. * 2 | 3 | 2 |1|17| 55 | 10 | 10
  275. * --------+---+-----+-+--+--------+----+----
  276. * ^
  277. *
  278. * For all other extents within the range, adjust the reference count
  279. * or delete it if the refcount falls below 2. If we were
  280. * incrementing, the end result looks like this:
  281. *
  282. * <------ adjustment range ------>
  283. * --------+---+-----+-+--+--------+----+----
  284. * 2 | 4 | 3 |2|18| 56 | 11 | 10
  285. * --------+---+-----+-+--+--------+----+----
  286. *
  287. * The result of a decrement operation looks as such:
  288. *
  289. * <------ adjustment range ------>
  290. * ----+ +---+ +--+--------+----+----
  291. * 2 | | 2 | |16| 54 | 9 | 10
  292. * ----+ +---+ +--+--------+----+----
  293. * DDDD 111111DD
  294. *
  295. * The blocks marked "D" are freed; the blocks marked "1" are only
  296. * referenced once and therefore the record is removed from the
  297. * refcount btree.
  298. */
  299. /* Next block after this extent. */
  300. static inline xfs_agblock_t
  301. xfs_refc_next(
  302. struct xfs_refcount_irec *rc)
  303. {
  304. return rc->rc_startblock + rc->rc_blockcount;
  305. }
  306. /*
  307. * Split a refcount extent that crosses agbno.
  308. */
  309. STATIC int
  310. xfs_refcount_split_extent(
  311. struct xfs_btree_cur *cur,
  312. xfs_agblock_t agbno,
  313. bool *shape_changed)
  314. {
  315. struct xfs_refcount_irec rcext, tmp;
  316. int found_rec;
  317. int error;
  318. *shape_changed = false;
  319. error = xfs_refcount_lookup_le(cur, agbno, &found_rec);
  320. if (error)
  321. goto out_error;
  322. if (!found_rec)
  323. return 0;
  324. error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
  325. if (error)
  326. goto out_error;
  327. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  328. if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
  329. return 0;
  330. *shape_changed = true;
  331. trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_private.a.agno,
  332. &rcext, agbno);
  333. /* Establish the right extent. */
  334. tmp = rcext;
  335. tmp.rc_startblock = agbno;
  336. tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
  337. error = xfs_refcount_update(cur, &tmp);
  338. if (error)
  339. goto out_error;
  340. /* Insert the left extent. */
  341. tmp = rcext;
  342. tmp.rc_blockcount = agbno - rcext.rc_startblock;
  343. error = xfs_refcount_insert(cur, &tmp, &found_rec);
  344. if (error)
  345. goto out_error;
  346. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  347. return error;
  348. out_error:
  349. trace_xfs_refcount_split_extent_error(cur->bc_mp,
  350. cur->bc_private.a.agno, error, _RET_IP_);
  351. return error;
  352. }
  353. /*
  354. * Merge the left, center, and right extents.
  355. */
  356. STATIC int
  357. xfs_refcount_merge_center_extents(
  358. struct xfs_btree_cur *cur,
  359. struct xfs_refcount_irec *left,
  360. struct xfs_refcount_irec *center,
  361. struct xfs_refcount_irec *right,
  362. unsigned long long extlen,
  363. xfs_extlen_t *aglen)
  364. {
  365. int error;
  366. int found_rec;
  367. trace_xfs_refcount_merge_center_extents(cur->bc_mp,
  368. cur->bc_private.a.agno, left, center, right);
  369. /*
  370. * Make sure the center and right extents are not in the btree.
  371. * If the center extent was synthesized, the first delete call
  372. * removes the right extent and we skip the second deletion.
  373. * If center and right were in the btree, then the first delete
  374. * call removes the center and the second one removes the right
  375. * extent.
  376. */
  377. error = xfs_refcount_lookup_ge(cur, center->rc_startblock,
  378. &found_rec);
  379. if (error)
  380. goto out_error;
  381. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  382. error = xfs_refcount_delete(cur, &found_rec);
  383. if (error)
  384. goto out_error;
  385. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  386. if (center->rc_refcount > 1) {
  387. error = xfs_refcount_delete(cur, &found_rec);
  388. if (error)
  389. goto out_error;
  390. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  391. out_error);
  392. }
  393. /* Enlarge the left extent. */
  394. error = xfs_refcount_lookup_le(cur, left->rc_startblock,
  395. &found_rec);
  396. if (error)
  397. goto out_error;
  398. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  399. left->rc_blockcount = extlen;
  400. error = xfs_refcount_update(cur, left);
  401. if (error)
  402. goto out_error;
  403. *aglen = 0;
  404. return error;
  405. out_error:
  406. trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
  407. cur->bc_private.a.agno, error, _RET_IP_);
  408. return error;
  409. }
  410. /*
  411. * Merge with the left extent.
  412. */
  413. STATIC int
  414. xfs_refcount_merge_left_extent(
  415. struct xfs_btree_cur *cur,
  416. struct xfs_refcount_irec *left,
  417. struct xfs_refcount_irec *cleft,
  418. xfs_agblock_t *agbno,
  419. xfs_extlen_t *aglen)
  420. {
  421. int error;
  422. int found_rec;
  423. trace_xfs_refcount_merge_left_extent(cur->bc_mp,
  424. cur->bc_private.a.agno, left, cleft);
  425. /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
  426. if (cleft->rc_refcount > 1) {
  427. error = xfs_refcount_lookup_le(cur, cleft->rc_startblock,
  428. &found_rec);
  429. if (error)
  430. goto out_error;
  431. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  432. out_error);
  433. error = xfs_refcount_delete(cur, &found_rec);
  434. if (error)
  435. goto out_error;
  436. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  437. out_error);
  438. }
  439. /* Enlarge the left extent. */
  440. error = xfs_refcount_lookup_le(cur, left->rc_startblock,
  441. &found_rec);
  442. if (error)
  443. goto out_error;
  444. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  445. left->rc_blockcount += cleft->rc_blockcount;
  446. error = xfs_refcount_update(cur, left);
  447. if (error)
  448. goto out_error;
  449. *agbno += cleft->rc_blockcount;
  450. *aglen -= cleft->rc_blockcount;
  451. return error;
  452. out_error:
  453. trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
  454. cur->bc_private.a.agno, error, _RET_IP_);
  455. return error;
  456. }
  457. /*
  458. * Merge with the right extent.
  459. */
  460. STATIC int
  461. xfs_refcount_merge_right_extent(
  462. struct xfs_btree_cur *cur,
  463. struct xfs_refcount_irec *right,
  464. struct xfs_refcount_irec *cright,
  465. xfs_extlen_t *aglen)
  466. {
  467. int error;
  468. int found_rec;
  469. trace_xfs_refcount_merge_right_extent(cur->bc_mp,
  470. cur->bc_private.a.agno, cright, right);
  471. /*
  472. * If the extent ending at agbno+aglen (cright) wasn't synthesized,
  473. * remove it.
  474. */
  475. if (cright->rc_refcount > 1) {
  476. error = xfs_refcount_lookup_le(cur, cright->rc_startblock,
  477. &found_rec);
  478. if (error)
  479. goto out_error;
  480. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  481. out_error);
  482. error = xfs_refcount_delete(cur, &found_rec);
  483. if (error)
  484. goto out_error;
  485. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  486. out_error);
  487. }
  488. /* Enlarge the right extent. */
  489. error = xfs_refcount_lookup_le(cur, right->rc_startblock,
  490. &found_rec);
  491. if (error)
  492. goto out_error;
  493. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  494. right->rc_startblock -= cright->rc_blockcount;
  495. right->rc_blockcount += cright->rc_blockcount;
  496. error = xfs_refcount_update(cur, right);
  497. if (error)
  498. goto out_error;
  499. *aglen -= cright->rc_blockcount;
  500. return error;
  501. out_error:
  502. trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
  503. cur->bc_private.a.agno, error, _RET_IP_);
  504. return error;
  505. }
  506. #define XFS_FIND_RCEXT_SHARED 1
  507. #define XFS_FIND_RCEXT_COW 2
  508. /*
  509. * Find the left extent and the one after it (cleft). This function assumes
  510. * that we've already split any extent crossing agbno.
  511. */
  512. STATIC int
  513. xfs_refcount_find_left_extents(
  514. struct xfs_btree_cur *cur,
  515. struct xfs_refcount_irec *left,
  516. struct xfs_refcount_irec *cleft,
  517. xfs_agblock_t agbno,
  518. xfs_extlen_t aglen,
  519. int flags)
  520. {
  521. struct xfs_refcount_irec tmp;
  522. int error;
  523. int found_rec;
  524. left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
  525. error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec);
  526. if (error)
  527. goto out_error;
  528. if (!found_rec)
  529. return 0;
  530. error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
  531. if (error)
  532. goto out_error;
  533. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  534. if (xfs_refc_next(&tmp) != agbno)
  535. return 0;
  536. if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
  537. return 0;
  538. if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
  539. return 0;
  540. /* We have a left extent; retrieve (or invent) the next right one */
  541. *left = tmp;
  542. error = xfs_btree_increment(cur, 0, &found_rec);
  543. if (error)
  544. goto out_error;
  545. if (found_rec) {
  546. error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
  547. if (error)
  548. goto out_error;
  549. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  550. out_error);
  551. /* if tmp starts at the end of our range, just use that */
  552. if (tmp.rc_startblock == agbno)
  553. *cleft = tmp;
  554. else {
  555. /*
  556. * There's a gap in the refcntbt at the start of the
  557. * range we're interested in (refcount == 1) so
  558. * synthesize the implied extent and pass it back.
  559. * We assume here that the agbno/aglen range was
  560. * passed in from a data fork extent mapping and
  561. * therefore is allocated to exactly one owner.
  562. */
  563. cleft->rc_startblock = agbno;
  564. cleft->rc_blockcount = min(aglen,
  565. tmp.rc_startblock - agbno);
  566. cleft->rc_refcount = 1;
  567. }
  568. } else {
  569. /*
  570. * No extents, so pretend that there's one covering the whole
  571. * range.
  572. */
  573. cleft->rc_startblock = agbno;
  574. cleft->rc_blockcount = aglen;
  575. cleft->rc_refcount = 1;
  576. }
  577. trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_private.a.agno,
  578. left, cleft, agbno);
  579. return error;
  580. out_error:
  581. trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
  582. cur->bc_private.a.agno, error, _RET_IP_);
  583. return error;
  584. }
  585. /*
  586. * Find the right extent and the one before it (cright). This function
  587. * assumes that we've already split any extents crossing agbno + aglen.
  588. */
  589. STATIC int
  590. xfs_refcount_find_right_extents(
  591. struct xfs_btree_cur *cur,
  592. struct xfs_refcount_irec *right,
  593. struct xfs_refcount_irec *cright,
  594. xfs_agblock_t agbno,
  595. xfs_extlen_t aglen,
  596. int flags)
  597. {
  598. struct xfs_refcount_irec tmp;
  599. int error;
  600. int found_rec;
  601. right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
  602. error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec);
  603. if (error)
  604. goto out_error;
  605. if (!found_rec)
  606. return 0;
  607. error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
  608. if (error)
  609. goto out_error;
  610. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
  611. if (tmp.rc_startblock != agbno + aglen)
  612. return 0;
  613. if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
  614. return 0;
  615. if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
  616. return 0;
  617. /* We have a right extent; retrieve (or invent) the next left one */
  618. *right = tmp;
  619. error = xfs_btree_decrement(cur, 0, &found_rec);
  620. if (error)
  621. goto out_error;
  622. if (found_rec) {
  623. error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
  624. if (error)
  625. goto out_error;
  626. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
  627. out_error);
  628. /* if tmp ends at the end of our range, just use that */
  629. if (xfs_refc_next(&tmp) == agbno + aglen)
  630. *cright = tmp;
  631. else {
  632. /*
  633. * There's a gap in the refcntbt at the end of the
  634. * range we're interested in (refcount == 1) so
  635. * create the implied extent and pass it back.
  636. * We assume here that the agbno/aglen range was
  637. * passed in from a data fork extent mapping and
  638. * therefore is allocated to exactly one owner.
  639. */
  640. cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
  641. cright->rc_blockcount = right->rc_startblock -
  642. cright->rc_startblock;
  643. cright->rc_refcount = 1;
  644. }
  645. } else {
  646. /*
  647. * No extents, so pretend that there's one covering the whole
  648. * range.
  649. */
  650. cright->rc_startblock = agbno;
  651. cright->rc_blockcount = aglen;
  652. cright->rc_refcount = 1;
  653. }
  654. trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_private.a.agno,
  655. cright, right, agbno + aglen);
  656. return error;
  657. out_error:
  658. trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
  659. cur->bc_private.a.agno, error, _RET_IP_);
  660. return error;
  661. }
  662. /* Is this extent valid? */
  663. static inline bool
  664. xfs_refc_valid(
  665. struct xfs_refcount_irec *rc)
  666. {
  667. return rc->rc_startblock != NULLAGBLOCK;
  668. }
  669. /*
  670. * Try to merge with any extents on the boundaries of the adjustment range.
  671. */
  672. STATIC int
  673. xfs_refcount_merge_extents(
  674. struct xfs_btree_cur *cur,
  675. xfs_agblock_t *agbno,
  676. xfs_extlen_t *aglen,
  677. enum xfs_refc_adjust_op adjust,
  678. int flags,
  679. bool *shape_changed)
  680. {
  681. struct xfs_refcount_irec left = {0}, cleft = {0};
  682. struct xfs_refcount_irec cright = {0}, right = {0};
  683. int error;
  684. unsigned long long ulen;
  685. bool cequal;
  686. *shape_changed = false;
  687. /*
  688. * Find the extent just below agbno [left], just above agbno [cleft],
  689. * just below (agbno + aglen) [cright], and just above (agbno + aglen)
  690. * [right].
  691. */
  692. error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno,
  693. *aglen, flags);
  694. if (error)
  695. return error;
  696. error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno,
  697. *aglen, flags);
  698. if (error)
  699. return error;
  700. /* No left or right extent to merge; exit. */
  701. if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
  702. return 0;
  703. cequal = (cleft.rc_startblock == cright.rc_startblock) &&
  704. (cleft.rc_blockcount == cright.rc_blockcount);
  705. /* Try to merge left, cleft, and right. cleft must == cright. */
  706. ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount +
  707. right.rc_blockcount;
  708. if (xfs_refc_valid(&left) && xfs_refc_valid(&right) &&
  709. xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal &&
  710. left.rc_refcount == cleft.rc_refcount + adjust &&
  711. right.rc_refcount == cleft.rc_refcount + adjust &&
  712. ulen < MAXREFCEXTLEN) {
  713. *shape_changed = true;
  714. return xfs_refcount_merge_center_extents(cur, &left, &cleft,
  715. &right, ulen, aglen);
  716. }
  717. /* Try to merge left and cleft. */
  718. ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount;
  719. if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) &&
  720. left.rc_refcount == cleft.rc_refcount + adjust &&
  721. ulen < MAXREFCEXTLEN) {
  722. *shape_changed = true;
  723. error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
  724. agbno, aglen);
  725. if (error)
  726. return error;
  727. /*
  728. * If we just merged left + cleft and cleft == cright,
  729. * we no longer have a cright to merge with right. We're done.
  730. */
  731. if (cequal)
  732. return 0;
  733. }
  734. /* Try to merge cright and right. */
  735. ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount;
  736. if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) &&
  737. right.rc_refcount == cright.rc_refcount + adjust &&
  738. ulen < MAXREFCEXTLEN) {
  739. *shape_changed = true;
  740. return xfs_refcount_merge_right_extent(cur, &right, &cright,
  741. aglen);
  742. }
  743. return error;
  744. }
  745. /*
  746. * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
  747. * true incorrectly is a shutdown FS; the penalty for guessing false
  748. * incorrectly is more transaction rolls than might be necessary.
  749. * Be conservative here.
  750. */
  751. static bool
  752. xfs_refcount_still_have_space(
  753. struct xfs_btree_cur *cur)
  754. {
  755. unsigned long overhead;
  756. overhead = cur->bc_private.a.priv.refc.shape_changes *
  757. xfs_allocfree_log_count(cur->bc_mp, 1);
  758. overhead *= cur->bc_mp->m_sb.sb_blocksize;
  759. /*
  760. * Only allow 2 refcount extent updates per transaction if the
  761. * refcount continue update "error" has been injected.
  762. */
  763. if (cur->bc_private.a.priv.refc.nr_ops > 2 &&
  764. XFS_TEST_ERROR(false, cur->bc_mp,
  765. XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
  766. return false;
  767. if (cur->bc_private.a.priv.refc.nr_ops == 0)
  768. return true;
  769. else if (overhead > cur->bc_tp->t_log_res)
  770. return false;
  771. return cur->bc_tp->t_log_res - overhead >
  772. cur->bc_private.a.priv.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
  773. }
  774. /*
  775. * Adjust the refcounts of middle extents. At this point we should have
  776. * split extents that crossed the adjustment range; merged with adjacent
  777. * extents; and updated agbno/aglen to reflect the merges. Therefore,
  778. * all we have to do is update the extents inside [agbno, agbno + aglen].
  779. */
  780. STATIC int
  781. xfs_refcount_adjust_extents(
  782. struct xfs_btree_cur *cur,
  783. xfs_agblock_t *agbno,
  784. xfs_extlen_t *aglen,
  785. enum xfs_refc_adjust_op adj,
  786. struct xfs_owner_info *oinfo)
  787. {
  788. struct xfs_refcount_irec ext, tmp;
  789. int error;
  790. int found_rec, found_tmp;
  791. xfs_fsblock_t fsbno;
  792. /* Merging did all the work already. */
  793. if (*aglen == 0)
  794. return 0;
  795. error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec);
  796. if (error)
  797. goto out_error;
  798. while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
  799. error = xfs_refcount_get_rec(cur, &ext, &found_rec);
  800. if (error)
  801. goto out_error;
  802. if (!found_rec) {
  803. ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
  804. ext.rc_blockcount = 0;
  805. ext.rc_refcount = 0;
  806. }
  807. /*
  808. * Deal with a hole in the refcount tree; if a file maps to
  809. * these blocks and there's no refcountbt record, pretend that
  810. * there is one with refcount == 1.
  811. */
  812. if (ext.rc_startblock != *agbno) {
  813. tmp.rc_startblock = *agbno;
  814. tmp.rc_blockcount = min(*aglen,
  815. ext.rc_startblock - *agbno);
  816. tmp.rc_refcount = 1 + adj;
  817. trace_xfs_refcount_modify_extent(cur->bc_mp,
  818. cur->bc_private.a.agno, &tmp);
  819. /*
  820. * Either cover the hole (increment) or
  821. * delete the range (decrement).
  822. */
  823. if (tmp.rc_refcount) {
  824. error = xfs_refcount_insert(cur, &tmp,
  825. &found_tmp);
  826. if (error)
  827. goto out_error;
  828. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  829. found_tmp == 1, out_error);
  830. cur->bc_private.a.priv.refc.nr_ops++;
  831. } else {
  832. fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
  833. cur->bc_private.a.agno,
  834. tmp.rc_startblock);
  835. xfs_bmap_add_free(cur->bc_tp, fsbno,
  836. tmp.rc_blockcount, oinfo);
  837. }
  838. (*agbno) += tmp.rc_blockcount;
  839. (*aglen) -= tmp.rc_blockcount;
  840. error = xfs_refcount_lookup_ge(cur, *agbno,
  841. &found_rec);
  842. if (error)
  843. goto out_error;
  844. }
  845. /* Stop if there's nothing left to modify */
  846. if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
  847. break;
  848. /*
  849. * Adjust the reference count and either update the tree
  850. * (incr) or free the blocks (decr).
  851. */
  852. if (ext.rc_refcount == MAXREFCOUNT)
  853. goto skip;
  854. ext.rc_refcount += adj;
  855. trace_xfs_refcount_modify_extent(cur->bc_mp,
  856. cur->bc_private.a.agno, &ext);
  857. if (ext.rc_refcount > 1) {
  858. error = xfs_refcount_update(cur, &ext);
  859. if (error)
  860. goto out_error;
  861. cur->bc_private.a.priv.refc.nr_ops++;
  862. } else if (ext.rc_refcount == 1) {
  863. error = xfs_refcount_delete(cur, &found_rec);
  864. if (error)
  865. goto out_error;
  866. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  867. found_rec == 1, out_error);
  868. cur->bc_private.a.priv.refc.nr_ops++;
  869. goto advloop;
  870. } else {
  871. fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
  872. cur->bc_private.a.agno,
  873. ext.rc_startblock);
  874. xfs_bmap_add_free(cur->bc_tp, fsbno, ext.rc_blockcount,
  875. oinfo);
  876. }
  877. skip:
  878. error = xfs_btree_increment(cur, 0, &found_rec);
  879. if (error)
  880. goto out_error;
  881. advloop:
  882. (*agbno) += ext.rc_blockcount;
  883. (*aglen) -= ext.rc_blockcount;
  884. }
  885. return error;
  886. out_error:
  887. trace_xfs_refcount_modify_extent_error(cur->bc_mp,
  888. cur->bc_private.a.agno, error, _RET_IP_);
  889. return error;
  890. }
  891. /* Adjust the reference count of a range of AG blocks. */
  892. STATIC int
  893. xfs_refcount_adjust(
  894. struct xfs_btree_cur *cur,
  895. xfs_agblock_t agbno,
  896. xfs_extlen_t aglen,
  897. xfs_agblock_t *new_agbno,
  898. xfs_extlen_t *new_aglen,
  899. enum xfs_refc_adjust_op adj,
  900. struct xfs_owner_info *oinfo)
  901. {
  902. bool shape_changed;
  903. int shape_changes = 0;
  904. int error;
  905. *new_agbno = agbno;
  906. *new_aglen = aglen;
  907. if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
  908. trace_xfs_refcount_increase(cur->bc_mp, cur->bc_private.a.agno,
  909. agbno, aglen);
  910. else
  911. trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_private.a.agno,
  912. agbno, aglen);
  913. /*
  914. * Ensure that no rcextents cross the boundary of the adjustment range.
  915. */
  916. error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
  917. if (error)
  918. goto out_error;
  919. if (shape_changed)
  920. shape_changes++;
  921. error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
  922. if (error)
  923. goto out_error;
  924. if (shape_changed)
  925. shape_changes++;
  926. /*
  927. * Try to merge with the left or right extents of the range.
  928. */
  929. error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj,
  930. XFS_FIND_RCEXT_SHARED, &shape_changed);
  931. if (error)
  932. goto out_error;
  933. if (shape_changed)
  934. shape_changes++;
  935. if (shape_changes)
  936. cur->bc_private.a.priv.refc.shape_changes++;
  937. /* Now that we've taken care of the ends, adjust the middle extents */
  938. error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen,
  939. adj, oinfo);
  940. if (error)
  941. goto out_error;
  942. return 0;
  943. out_error:
  944. trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_private.a.agno,
  945. error, _RET_IP_);
  946. return error;
  947. }
  948. /* Clean up after calling xfs_refcount_finish_one. */
  949. void
  950. xfs_refcount_finish_one_cleanup(
  951. struct xfs_trans *tp,
  952. struct xfs_btree_cur *rcur,
  953. int error)
  954. {
  955. struct xfs_buf *agbp;
  956. if (rcur == NULL)
  957. return;
  958. agbp = rcur->bc_private.a.agbp;
  959. xfs_btree_del_cursor(rcur, error);
  960. if (error)
  961. xfs_trans_brelse(tp, agbp);
  962. }
  963. /*
  964. * Process one of the deferred refcount operations. We pass back the
  965. * btree cursor to maintain our lock on the btree between calls.
  966. * This saves time and eliminates a buffer deadlock between the
  967. * superblock and the AGF because we'll always grab them in the same
  968. * order.
  969. */
  970. int
  971. xfs_refcount_finish_one(
  972. struct xfs_trans *tp,
  973. enum xfs_refcount_intent_type type,
  974. xfs_fsblock_t startblock,
  975. xfs_extlen_t blockcount,
  976. xfs_fsblock_t *new_fsb,
  977. xfs_extlen_t *new_len,
  978. struct xfs_btree_cur **pcur)
  979. {
  980. struct xfs_mount *mp = tp->t_mountp;
  981. struct xfs_btree_cur *rcur;
  982. struct xfs_buf *agbp = NULL;
  983. int error = 0;
  984. xfs_agnumber_t agno;
  985. xfs_agblock_t bno;
  986. xfs_agblock_t new_agbno;
  987. unsigned long nr_ops = 0;
  988. int shape_changes = 0;
  989. agno = XFS_FSB_TO_AGNO(mp, startblock);
  990. ASSERT(agno != NULLAGNUMBER);
  991. bno = XFS_FSB_TO_AGBNO(mp, startblock);
  992. trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
  993. type, XFS_FSB_TO_AGBNO(mp, startblock),
  994. blockcount);
  995. if (XFS_TEST_ERROR(false, mp,
  996. XFS_ERRTAG_REFCOUNT_FINISH_ONE))
  997. return -EIO;
  998. /*
  999. * If we haven't gotten a cursor or the cursor AG doesn't match
  1000. * the startblock, get one now.
  1001. */
  1002. rcur = *pcur;
  1003. if (rcur != NULL && rcur->bc_private.a.agno != agno) {
  1004. nr_ops = rcur->bc_private.a.priv.refc.nr_ops;
  1005. shape_changes = rcur->bc_private.a.priv.refc.shape_changes;
  1006. xfs_refcount_finish_one_cleanup(tp, rcur, 0);
  1007. rcur = NULL;
  1008. *pcur = NULL;
  1009. }
  1010. if (rcur == NULL) {
  1011. error = xfs_alloc_read_agf(tp->t_mountp, tp, agno,
  1012. XFS_ALLOC_FLAG_FREEING, &agbp);
  1013. if (error)
  1014. return error;
  1015. if (!agbp)
  1016. return -EFSCORRUPTED;
  1017. rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno);
  1018. if (!rcur) {
  1019. error = -ENOMEM;
  1020. goto out_cur;
  1021. }
  1022. rcur->bc_private.a.priv.refc.nr_ops = nr_ops;
  1023. rcur->bc_private.a.priv.refc.shape_changes = shape_changes;
  1024. }
  1025. *pcur = rcur;
  1026. switch (type) {
  1027. case XFS_REFCOUNT_INCREASE:
  1028. error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
  1029. new_len, XFS_REFCOUNT_ADJUST_INCREASE, NULL);
  1030. *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
  1031. break;
  1032. case XFS_REFCOUNT_DECREASE:
  1033. error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
  1034. new_len, XFS_REFCOUNT_ADJUST_DECREASE, NULL);
  1035. *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
  1036. break;
  1037. case XFS_REFCOUNT_ALLOC_COW:
  1038. *new_fsb = startblock + blockcount;
  1039. *new_len = 0;
  1040. error = __xfs_refcount_cow_alloc(rcur, bno, blockcount);
  1041. break;
  1042. case XFS_REFCOUNT_FREE_COW:
  1043. *new_fsb = startblock + blockcount;
  1044. *new_len = 0;
  1045. error = __xfs_refcount_cow_free(rcur, bno, blockcount);
  1046. break;
  1047. default:
  1048. ASSERT(0);
  1049. error = -EFSCORRUPTED;
  1050. }
  1051. if (!error && *new_len > 0)
  1052. trace_xfs_refcount_finish_one_leftover(mp, agno, type,
  1053. bno, blockcount, new_agbno, *new_len);
  1054. return error;
  1055. out_cur:
  1056. xfs_trans_brelse(tp, agbp);
  1057. return error;
  1058. }
  1059. /*
  1060. * Record a refcount intent for later processing.
  1061. */
  1062. static int
  1063. __xfs_refcount_add(
  1064. struct xfs_trans *tp,
  1065. enum xfs_refcount_intent_type type,
  1066. xfs_fsblock_t startblock,
  1067. xfs_extlen_t blockcount)
  1068. {
  1069. struct xfs_refcount_intent *ri;
  1070. trace_xfs_refcount_defer(tp->t_mountp,
  1071. XFS_FSB_TO_AGNO(tp->t_mountp, startblock),
  1072. type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
  1073. blockcount);
  1074. ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
  1075. KM_SLEEP | KM_NOFS);
  1076. INIT_LIST_HEAD(&ri->ri_list);
  1077. ri->ri_type = type;
  1078. ri->ri_startblock = startblock;
  1079. ri->ri_blockcount = blockcount;
  1080. xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
  1081. return 0;
  1082. }
  1083. /*
  1084. * Increase the reference count of the blocks backing a file's extent.
  1085. */
  1086. int
  1087. xfs_refcount_increase_extent(
  1088. struct xfs_trans *tp,
  1089. struct xfs_bmbt_irec *PREV)
  1090. {
  1091. if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb))
  1092. return 0;
  1093. return __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE,
  1094. PREV->br_startblock, PREV->br_blockcount);
  1095. }
  1096. /*
  1097. * Decrease the reference count of the blocks backing a file's extent.
  1098. */
  1099. int
  1100. xfs_refcount_decrease_extent(
  1101. struct xfs_trans *tp,
  1102. struct xfs_bmbt_irec *PREV)
  1103. {
  1104. if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb))
  1105. return 0;
  1106. return __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE,
  1107. PREV->br_startblock, PREV->br_blockcount);
  1108. }
  1109. /*
  1110. * Given an AG extent, find the lowest-numbered run of shared blocks
  1111. * within that range and return the range in fbno/flen. If
  1112. * find_end_of_shared is set, return the longest contiguous extent of
  1113. * shared blocks; if not, just return the first extent we find. If no
  1114. * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
  1115. * and 0, respectively.
  1116. */
  1117. int
  1118. xfs_refcount_find_shared(
  1119. struct xfs_btree_cur *cur,
  1120. xfs_agblock_t agbno,
  1121. xfs_extlen_t aglen,
  1122. xfs_agblock_t *fbno,
  1123. xfs_extlen_t *flen,
  1124. bool find_end_of_shared)
  1125. {
  1126. struct xfs_refcount_irec tmp;
  1127. int i;
  1128. int have;
  1129. int error;
  1130. trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_private.a.agno,
  1131. agbno, aglen);
  1132. /* By default, skip the whole range */
  1133. *fbno = NULLAGBLOCK;
  1134. *flen = 0;
  1135. /* Try to find a refcount extent that crosses the start */
  1136. error = xfs_refcount_lookup_le(cur, agbno, &have);
  1137. if (error)
  1138. goto out_error;
  1139. if (!have) {
  1140. /* No left extent, look at the next one */
  1141. error = xfs_btree_increment(cur, 0, &have);
  1142. if (error)
  1143. goto out_error;
  1144. if (!have)
  1145. goto done;
  1146. }
  1147. error = xfs_refcount_get_rec(cur, &tmp, &i);
  1148. if (error)
  1149. goto out_error;
  1150. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
  1151. /* If the extent ends before the start, look at the next one */
  1152. if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
  1153. error = xfs_btree_increment(cur, 0, &have);
  1154. if (error)
  1155. goto out_error;
  1156. if (!have)
  1157. goto done;
  1158. error = xfs_refcount_get_rec(cur, &tmp, &i);
  1159. if (error)
  1160. goto out_error;
  1161. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
  1162. }
  1163. /* If the extent starts after the range we want, bail out */
  1164. if (tmp.rc_startblock >= agbno + aglen)
  1165. goto done;
  1166. /* We found the start of a shared extent! */
  1167. if (tmp.rc_startblock < agbno) {
  1168. tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
  1169. tmp.rc_startblock = agbno;
  1170. }
  1171. *fbno = tmp.rc_startblock;
  1172. *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
  1173. if (!find_end_of_shared)
  1174. goto done;
  1175. /* Otherwise, find the end of this shared extent */
  1176. while (*fbno + *flen < agbno + aglen) {
  1177. error = xfs_btree_increment(cur, 0, &have);
  1178. if (error)
  1179. goto out_error;
  1180. if (!have)
  1181. break;
  1182. error = xfs_refcount_get_rec(cur, &tmp, &i);
  1183. if (error)
  1184. goto out_error;
  1185. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
  1186. if (tmp.rc_startblock >= agbno + aglen ||
  1187. tmp.rc_startblock != *fbno + *flen)
  1188. break;
  1189. *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
  1190. }
  1191. done:
  1192. trace_xfs_refcount_find_shared_result(cur->bc_mp,
  1193. cur->bc_private.a.agno, *fbno, *flen);
  1194. out_error:
  1195. if (error)
  1196. trace_xfs_refcount_find_shared_error(cur->bc_mp,
  1197. cur->bc_private.a.agno, error, _RET_IP_);
  1198. return error;
  1199. }
  1200. /*
  1201. * Recovering CoW Blocks After a Crash
  1202. *
  1203. * Due to the way that the copy on write mechanism works, there's a window of
  1204. * opportunity in which we can lose track of allocated blocks during a crash.
  1205. * Because CoW uses delayed allocation in the in-core CoW fork, writeback
  1206. * causes blocks to be allocated and stored in the CoW fork. The blocks are
  1207. * no longer in the free space btree but are not otherwise recorded anywhere
  1208. * until the write completes and the blocks are mapped into the file. A crash
  1209. * in between allocation and remapping results in the replacement blocks being
  1210. * lost. This situation is exacerbated by the CoW extent size hint because
  1211. * allocations can hang around for long time.
  1212. *
  1213. * However, there is a place where we can record these allocations before they
  1214. * become mappings -- the reference count btree. The btree does not record
  1215. * extents with refcount == 1, so we can record allocations with a refcount of
  1216. * 1. Blocks being used for CoW writeout cannot be shared, so there should be
  1217. * no conflict with shared block records. These mappings should be created
  1218. * when we allocate blocks to the CoW fork and deleted when they're removed
  1219. * from the CoW fork.
  1220. *
  1221. * Minor nit: records for in-progress CoW allocations and records for shared
  1222. * extents must never be merged, to preserve the property that (except for CoW
  1223. * allocations) there are no refcount btree entries with refcount == 1. The
  1224. * only time this could potentially happen is when unsharing a block that's
  1225. * adjacent to CoW allocations, so we must be careful to avoid this.
  1226. *
  1227. * At mount time we recover lost CoW allocations by searching the refcount
  1228. * btree for these refcount == 1 mappings. These represent CoW allocations
  1229. * that were in progress at the time the filesystem went down, so we can free
  1230. * them to get the space back.
  1231. *
  1232. * This mechanism is superior to creating EFIs for unmapped CoW extents for
  1233. * several reasons -- first, EFIs pin the tail of the log and would have to be
  1234. * periodically relogged to avoid filling up the log. Second, CoW completions
  1235. * will have to file an EFD and create new EFIs for whatever remains in the
  1236. * CoW fork; this partially takes care of (1) but extent-size reservations
  1237. * will have to periodically relog even if there's no writeout in progress.
  1238. * This can happen if the CoW extent size hint is set, which you really want.
  1239. * Third, EFIs cannot currently be automatically relogged into newer
  1240. * transactions to advance the log tail. Fourth, stuffing the log full of
  1241. * EFIs places an upper bound on the number of CoW allocations that can be
  1242. * held filesystem-wide at any given time. Recording them in the refcount
  1243. * btree doesn't require us to maintain any state in memory and doesn't pin
  1244. * the log.
  1245. */
  1246. /*
  1247. * Adjust the refcounts of CoW allocations. These allocations are "magic"
  1248. * in that they're not referenced anywhere else in the filesystem, so we
  1249. * stash them in the refcount btree with a refcount of 1 until either file
  1250. * remapping (or CoW cancellation) happens.
  1251. */
  1252. STATIC int
  1253. xfs_refcount_adjust_cow_extents(
  1254. struct xfs_btree_cur *cur,
  1255. xfs_agblock_t agbno,
  1256. xfs_extlen_t aglen,
  1257. enum xfs_refc_adjust_op adj)
  1258. {
  1259. struct xfs_refcount_irec ext, tmp;
  1260. int error;
  1261. int found_rec, found_tmp;
  1262. if (aglen == 0)
  1263. return 0;
  1264. /* Find any overlapping refcount records */
  1265. error = xfs_refcount_lookup_ge(cur, agbno, &found_rec);
  1266. if (error)
  1267. goto out_error;
  1268. error = xfs_refcount_get_rec(cur, &ext, &found_rec);
  1269. if (error)
  1270. goto out_error;
  1271. if (!found_rec) {
  1272. ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks +
  1273. XFS_REFC_COW_START;
  1274. ext.rc_blockcount = 0;
  1275. ext.rc_refcount = 0;
  1276. }
  1277. switch (adj) {
  1278. case XFS_REFCOUNT_ADJUST_COW_ALLOC:
  1279. /* Adding a CoW reservation, there should be nothing here. */
  1280. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1281. ext.rc_startblock >= agbno + aglen, out_error);
  1282. tmp.rc_startblock = agbno;
  1283. tmp.rc_blockcount = aglen;
  1284. tmp.rc_refcount = 1;
  1285. trace_xfs_refcount_modify_extent(cur->bc_mp,
  1286. cur->bc_private.a.agno, &tmp);
  1287. error = xfs_refcount_insert(cur, &tmp,
  1288. &found_tmp);
  1289. if (error)
  1290. goto out_error;
  1291. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1292. found_tmp == 1, out_error);
  1293. break;
  1294. case XFS_REFCOUNT_ADJUST_COW_FREE:
  1295. /* Removing a CoW reservation, there should be one extent. */
  1296. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1297. ext.rc_startblock == agbno, out_error);
  1298. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1299. ext.rc_blockcount == aglen, out_error);
  1300. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1301. ext.rc_refcount == 1, out_error);
  1302. ext.rc_refcount = 0;
  1303. trace_xfs_refcount_modify_extent(cur->bc_mp,
  1304. cur->bc_private.a.agno, &ext);
  1305. error = xfs_refcount_delete(cur, &found_rec);
  1306. if (error)
  1307. goto out_error;
  1308. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
  1309. found_rec == 1, out_error);
  1310. break;
  1311. default:
  1312. ASSERT(0);
  1313. }
  1314. return error;
  1315. out_error:
  1316. trace_xfs_refcount_modify_extent_error(cur->bc_mp,
  1317. cur->bc_private.a.agno, error, _RET_IP_);
  1318. return error;
  1319. }
  1320. /*
  1321. * Add or remove refcount btree entries for CoW reservations.
  1322. */
  1323. STATIC int
  1324. xfs_refcount_adjust_cow(
  1325. struct xfs_btree_cur *cur,
  1326. xfs_agblock_t agbno,
  1327. xfs_extlen_t aglen,
  1328. enum xfs_refc_adjust_op adj)
  1329. {
  1330. bool shape_changed;
  1331. int error;
  1332. agbno += XFS_REFC_COW_START;
  1333. /*
  1334. * Ensure that no rcextents cross the boundary of the adjustment range.
  1335. */
  1336. error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
  1337. if (error)
  1338. goto out_error;
  1339. error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
  1340. if (error)
  1341. goto out_error;
  1342. /*
  1343. * Try to merge with the left or right extents of the range.
  1344. */
  1345. error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj,
  1346. XFS_FIND_RCEXT_COW, &shape_changed);
  1347. if (error)
  1348. goto out_error;
  1349. /* Now that we've taken care of the ends, adjust the middle extents */
  1350. error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
  1351. if (error)
  1352. goto out_error;
  1353. return 0;
  1354. out_error:
  1355. trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_private.a.agno,
  1356. error, _RET_IP_);
  1357. return error;
  1358. }
  1359. /*
  1360. * Record a CoW allocation in the refcount btree.
  1361. */
  1362. STATIC int
  1363. __xfs_refcount_cow_alloc(
  1364. struct xfs_btree_cur *rcur,
  1365. xfs_agblock_t agbno,
  1366. xfs_extlen_t aglen)
  1367. {
  1368. trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_private.a.agno,
  1369. agbno, aglen);
  1370. /* Add refcount btree reservation */
  1371. return xfs_refcount_adjust_cow(rcur, agbno, aglen,
  1372. XFS_REFCOUNT_ADJUST_COW_ALLOC);
  1373. }
  1374. /*
  1375. * Remove a CoW allocation from the refcount btree.
  1376. */
  1377. STATIC int
  1378. __xfs_refcount_cow_free(
  1379. struct xfs_btree_cur *rcur,
  1380. xfs_agblock_t agbno,
  1381. xfs_extlen_t aglen)
  1382. {
  1383. trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_private.a.agno,
  1384. agbno, aglen);
  1385. /* Remove refcount btree reservation */
  1386. return xfs_refcount_adjust_cow(rcur, agbno, aglen,
  1387. XFS_REFCOUNT_ADJUST_COW_FREE);
  1388. }
  1389. /* Record a CoW staging extent in the refcount btree. */
  1390. int
  1391. xfs_refcount_alloc_cow_extent(
  1392. struct xfs_trans *tp,
  1393. xfs_fsblock_t fsb,
  1394. xfs_extlen_t len)
  1395. {
  1396. struct xfs_mount *mp = tp->t_mountp;
  1397. int error;
  1398. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1399. return 0;
  1400. error = __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len);
  1401. if (error)
  1402. return error;
  1403. /* Add rmap entry */
  1404. return xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
  1405. XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
  1406. }
  1407. /* Forget a CoW staging event in the refcount btree. */
  1408. int
  1409. xfs_refcount_free_cow_extent(
  1410. struct xfs_trans *tp,
  1411. xfs_fsblock_t fsb,
  1412. xfs_extlen_t len)
  1413. {
  1414. struct xfs_mount *mp = tp->t_mountp;
  1415. int error;
  1416. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1417. return 0;
  1418. /* Remove rmap entry */
  1419. error = xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
  1420. XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
  1421. if (error)
  1422. return error;
  1423. return __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len);
  1424. }
  1425. struct xfs_refcount_recovery {
  1426. struct list_head rr_list;
  1427. struct xfs_refcount_irec rr_rrec;
  1428. };
  1429. /* Stuff an extent on the recovery list. */
  1430. STATIC int
  1431. xfs_refcount_recover_extent(
  1432. struct xfs_btree_cur *cur,
  1433. union xfs_btree_rec *rec,
  1434. void *priv)
  1435. {
  1436. struct list_head *debris = priv;
  1437. struct xfs_refcount_recovery *rr;
  1438. if (be32_to_cpu(rec->refc.rc_refcount) != 1)
  1439. return -EFSCORRUPTED;
  1440. rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), KM_SLEEP);
  1441. xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
  1442. list_add_tail(&rr->rr_list, debris);
  1443. return 0;
  1444. }
  1445. /* Find and remove leftover CoW reservations. */
  1446. int
  1447. xfs_refcount_recover_cow_leftovers(
  1448. struct xfs_mount *mp,
  1449. xfs_agnumber_t agno)
  1450. {
  1451. struct xfs_trans *tp;
  1452. struct xfs_btree_cur *cur;
  1453. struct xfs_buf *agbp;
  1454. struct xfs_refcount_recovery *rr, *n;
  1455. struct list_head debris;
  1456. union xfs_btree_irec low;
  1457. union xfs_btree_irec high;
  1458. xfs_fsblock_t fsb;
  1459. xfs_agblock_t agbno;
  1460. int error;
  1461. if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
  1462. return -EOPNOTSUPP;
  1463. INIT_LIST_HEAD(&debris);
  1464. /*
  1465. * In this first part, we use an empty transaction to gather up
  1466. * all the leftover CoW extents so that we can subsequently
  1467. * delete them. The empty transaction is used to avoid
  1468. * a buffer lock deadlock if there happens to be a loop in the
  1469. * refcountbt because we're allowed to re-grab a buffer that is
  1470. * already attached to our transaction. When we're done
  1471. * recording the CoW debris we cancel the (empty) transaction
  1472. * and everything goes away cleanly.
  1473. */
  1474. error = xfs_trans_alloc_empty(mp, &tp);
  1475. if (error)
  1476. return error;
  1477. error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
  1478. if (error)
  1479. goto out_trans;
  1480. if (!agbp) {
  1481. error = -ENOMEM;
  1482. goto out_trans;
  1483. }
  1484. cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno);
  1485. /* Find all the leftover CoW staging extents. */
  1486. memset(&low, 0, sizeof(low));
  1487. memset(&high, 0, sizeof(high));
  1488. low.rc.rc_startblock = XFS_REFC_COW_START;
  1489. high.rc.rc_startblock = -1U;
  1490. error = xfs_btree_query_range(cur, &low, &high,
  1491. xfs_refcount_recover_extent, &debris);
  1492. xfs_btree_del_cursor(cur, error);
  1493. xfs_trans_brelse(tp, agbp);
  1494. xfs_trans_cancel(tp);
  1495. if (error)
  1496. goto out_free;
  1497. /* Now iterate the list to free the leftovers */
  1498. list_for_each_entry_safe(rr, n, &debris, rr_list) {
  1499. /* Set up transaction. */
  1500. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1501. if (error)
  1502. goto out_free;
  1503. trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec);
  1504. /* Free the orphan record */
  1505. agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START;
  1506. fsb = XFS_AGB_TO_FSB(mp, agno, agbno);
  1507. error = xfs_refcount_free_cow_extent(tp, fsb,
  1508. rr->rr_rrec.rc_blockcount);
  1509. if (error)
  1510. goto out_trans;
  1511. /* Free the block. */
  1512. xfs_bmap_add_free(tp, fsb, rr->rr_rrec.rc_blockcount, NULL);
  1513. error = xfs_trans_commit(tp);
  1514. if (error)
  1515. goto out_free;
  1516. list_del(&rr->rr_list);
  1517. kmem_free(rr);
  1518. }
  1519. return error;
  1520. out_trans:
  1521. xfs_trans_cancel(tp);
  1522. out_free:
  1523. /* Free the leftover list */
  1524. list_for_each_entry_safe(rr, n, &debris, rr_list) {
  1525. list_del(&rr->rr_list);
  1526. kmem_free(rr);
  1527. }
  1528. return error;
  1529. }
  1530. /* Is there a record covering a given extent? */
  1531. int
  1532. xfs_refcount_has_record(
  1533. struct xfs_btree_cur *cur,
  1534. xfs_agblock_t bno,
  1535. xfs_extlen_t len,
  1536. bool *exists)
  1537. {
  1538. union xfs_btree_irec low;
  1539. union xfs_btree_irec high;
  1540. memset(&low, 0, sizeof(low));
  1541. low.rc.rc_startblock = bno;
  1542. memset(&high, 0xFF, sizeof(high));
  1543. high.rc.rc_startblock = bno + len - 1;
  1544. return xfs_btree_has_record(cur, &low, &high, exists);
  1545. }