xfs_refcount.c 46 KB

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