xfs_refcount.c 46 KB

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