xfs_refcount.c 54 KB

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