xfs_reflink.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_defer.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_inode_item.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_bmap_util.h"
  21. #include "xfs_error.h"
  22. #include "xfs_dir2.h"
  23. #include "xfs_dir2_priv.h"
  24. #include "xfs_ioctl.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_log.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_pnfs.h"
  29. #include "xfs_btree.h"
  30. #include "xfs_refcount_btree.h"
  31. #include "xfs_refcount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_trans_space.h"
  34. #include "xfs_bit.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_quota_defs.h"
  37. #include "xfs_quota.h"
  38. #include "xfs_reflink.h"
  39. #include "xfs_iomap.h"
  40. #include "xfs_rmap_btree.h"
  41. #include "xfs_sb.h"
  42. #include "xfs_ag_resv.h"
  43. /*
  44. * Copy on Write of Shared Blocks
  45. *
  46. * XFS must preserve "the usual" file semantics even when two files share
  47. * the same physical blocks. This means that a write to one file must not
  48. * alter the blocks in a different file; the way that we'll do that is
  49. * through the use of a copy-on-write mechanism. At a high level, that
  50. * means that when we want to write to a shared block, we allocate a new
  51. * block, write the data to the new block, and if that succeeds we map the
  52. * new block into the file.
  53. *
  54. * XFS provides a "delayed allocation" mechanism that defers the allocation
  55. * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
  56. * possible. This reduces fragmentation by enabling the filesystem to ask
  57. * for bigger chunks less often, which is exactly what we want for CoW.
  58. *
  59. * The delalloc mechanism begins when the kernel wants to make a block
  60. * writable (write_begin or page_mkwrite). If the offset is not mapped, we
  61. * create a delalloc mapping, which is a regular in-core extent, but without
  62. * a real startblock. (For delalloc mappings, the startblock encodes both
  63. * a flag that this is a delalloc mapping, and a worst-case estimate of how
  64. * many blocks might be required to put the mapping into the BMBT.) delalloc
  65. * mappings are a reservation against the free space in the filesystem;
  66. * adjacent mappings can also be combined into fewer larger mappings.
  67. *
  68. * As an optimization, the CoW extent size hint (cowextsz) creates
  69. * outsized aligned delalloc reservations in the hope of landing out of
  70. * order nearby CoW writes in a single extent on disk, thereby reducing
  71. * fragmentation and improving future performance.
  72. *
  73. * D: --RRRRRRSSSRRRRRRRR--- (data fork)
  74. * C: ------DDDDDDD--------- (CoW fork)
  75. *
  76. * When dirty pages are being written out (typically in writepage), the
  77. * delalloc reservations are converted into unwritten mappings by
  78. * allocating blocks and replacing the delalloc mapping with real ones.
  79. * A delalloc mapping can be replaced by several unwritten ones if the
  80. * free space is fragmented.
  81. *
  82. * D: --RRRRRRSSSRRRRRRRR---
  83. * C: ------UUUUUUU---------
  84. *
  85. * We want to adapt the delalloc mechanism for copy-on-write, since the
  86. * write paths are similar. The first two steps (creating the reservation
  87. * and allocating the blocks) are exactly the same as delalloc except that
  88. * the mappings must be stored in a separate CoW fork because we do not want
  89. * to disturb the mapping in the data fork until we're sure that the write
  90. * succeeded. IO completion in this case is the process of removing the old
  91. * mapping from the data fork and moving the new mapping from the CoW fork to
  92. * the data fork. This will be discussed shortly.
  93. *
  94. * For now, unaligned directio writes will be bounced back to the page cache.
  95. * Block-aligned directio writes will use the same mechanism as buffered
  96. * writes.
  97. *
  98. * Just prior to submitting the actual disk write requests, we convert
  99. * the extents representing the range of the file actually being written
  100. * (as opposed to extra pieces created for the cowextsize hint) to real
  101. * extents. This will become important in the next step:
  102. *
  103. * D: --RRRRRRSSSRRRRRRRR---
  104. * C: ------UUrrUUU---------
  105. *
  106. * CoW remapping must be done after the data block write completes,
  107. * because we don't want to destroy the old data fork map until we're sure
  108. * the new block has been written. Since the new mappings are kept in a
  109. * separate fork, we can simply iterate these mappings to find the ones
  110. * that cover the file blocks that we just CoW'd. For each extent, simply
  111. * unmap the corresponding range in the data fork, map the new range into
  112. * the data fork, and remove the extent from the CoW fork. Because of
  113. * the presence of the cowextsize hint, however, we must be careful
  114. * only to remap the blocks that we've actually written out -- we must
  115. * never remap delalloc reservations nor CoW staging blocks that have
  116. * yet to be written. This corresponds exactly to the real extents in
  117. * the CoW fork:
  118. *
  119. * D: --RRRRRRrrSRRRRRRRR---
  120. * C: ------UU--UUU---------
  121. *
  122. * Since the remapping operation can be applied to an arbitrary file
  123. * range, we record the need for the remap step as a flag in the ioend
  124. * instead of declaring a new IO type. This is required for direct io
  125. * because we only have ioend for the whole dio, and we have to be able to
  126. * remember the presence of unwritten blocks and CoW blocks with a single
  127. * ioend structure. Better yet, the more ground we can cover with one
  128. * ioend, the better.
  129. */
  130. /*
  131. * Given an AG extent, find the lowest-numbered run of shared blocks
  132. * within that range and return the range in fbno/flen. If
  133. * find_end_of_shared is true, return the longest contiguous extent of
  134. * shared blocks. If there are no shared extents, fbno and flen will
  135. * be set to NULLAGBLOCK and 0, respectively.
  136. */
  137. int
  138. xfs_reflink_find_shared(
  139. struct xfs_mount *mp,
  140. struct xfs_trans *tp,
  141. xfs_agnumber_t agno,
  142. xfs_agblock_t agbno,
  143. xfs_extlen_t aglen,
  144. xfs_agblock_t *fbno,
  145. xfs_extlen_t *flen,
  146. bool find_end_of_shared)
  147. {
  148. struct xfs_buf *agbp;
  149. struct xfs_btree_cur *cur;
  150. int error;
  151. error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
  152. if (error)
  153. return error;
  154. if (!agbp)
  155. return -ENOMEM;
  156. cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno);
  157. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  158. find_end_of_shared);
  159. xfs_btree_del_cursor(cur, error);
  160. xfs_trans_brelse(tp, agbp);
  161. return error;
  162. }
  163. /*
  164. * Trim the mapping to the next block where there's a change in the
  165. * shared/unshared status. More specifically, this means that we
  166. * find the lowest-numbered extent of shared blocks that coincides with
  167. * the given block mapping. If the shared extent overlaps the start of
  168. * the mapping, trim the mapping to the end of the shared extent. If
  169. * the shared region intersects the mapping, trim the mapping to the
  170. * start of the shared extent. If there are no shared regions that
  171. * overlap, just return the original extent.
  172. */
  173. int
  174. xfs_reflink_trim_around_shared(
  175. struct xfs_inode *ip,
  176. struct xfs_bmbt_irec *irec,
  177. bool *shared,
  178. bool *trimmed)
  179. {
  180. xfs_agnumber_t agno;
  181. xfs_agblock_t agbno;
  182. xfs_extlen_t aglen;
  183. xfs_agblock_t fbno;
  184. xfs_extlen_t flen;
  185. int error = 0;
  186. /* Holes, unwritten, and delalloc extents cannot be shared */
  187. if (!xfs_is_reflink_inode(ip) || !xfs_bmap_is_real_extent(irec)) {
  188. *shared = false;
  189. return 0;
  190. }
  191. trace_xfs_reflink_trim_around_shared(ip, irec);
  192. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  193. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  194. aglen = irec->br_blockcount;
  195. error = xfs_reflink_find_shared(ip->i_mount, NULL, agno, agbno,
  196. aglen, &fbno, &flen, true);
  197. if (error)
  198. return error;
  199. *shared = *trimmed = false;
  200. if (fbno == NULLAGBLOCK) {
  201. /* No shared blocks at all. */
  202. return 0;
  203. } else if (fbno == agbno) {
  204. /*
  205. * The start of this extent is shared. Truncate the
  206. * mapping at the end of the shared region so that a
  207. * subsequent iteration starts at the start of the
  208. * unshared region.
  209. */
  210. irec->br_blockcount = flen;
  211. *shared = true;
  212. if (flen != aglen)
  213. *trimmed = true;
  214. return 0;
  215. } else {
  216. /*
  217. * There's a shared extent midway through this extent.
  218. * Truncate the mapping at the start of the shared
  219. * extent so that a subsequent iteration starts at the
  220. * start of the shared region.
  221. */
  222. irec->br_blockcount = fbno - agbno;
  223. *trimmed = true;
  224. return 0;
  225. }
  226. }
  227. /*
  228. * Trim the passed in imap to the next shared/unshared extent boundary, and
  229. * if imap->br_startoff points to a shared extent reserve space for it in the
  230. * COW fork. In this case *shared is set to true, else to false.
  231. *
  232. * Note that imap will always contain the block numbers for the existing blocks
  233. * in the data fork, as the upper layers need them for read-modify-write
  234. * operations.
  235. */
  236. int
  237. xfs_reflink_reserve_cow(
  238. struct xfs_inode *ip,
  239. struct xfs_bmbt_irec *imap,
  240. bool *shared)
  241. {
  242. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  243. struct xfs_bmbt_irec got;
  244. int error = 0;
  245. bool eof = false, trimmed;
  246. struct xfs_iext_cursor icur;
  247. /*
  248. * Search the COW fork extent list first. This serves two purposes:
  249. * first this implement the speculative preallocation using cowextisze,
  250. * so that we also unshared block adjacent to shared blocks instead
  251. * of just the shared blocks themselves. Second the lookup in the
  252. * extent list is generally faster than going out to the shared extent
  253. * tree.
  254. */
  255. if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &icur, &got))
  256. eof = true;
  257. if (!eof && got.br_startoff <= imap->br_startoff) {
  258. trace_xfs_reflink_cow_found(ip, imap);
  259. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  260. *shared = true;
  261. return 0;
  262. }
  263. /* Trim the mapping to the nearest shared extent boundary. */
  264. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  265. if (error)
  266. return error;
  267. /* Not shared? Just report the (potentially capped) extent. */
  268. if (!*shared)
  269. return 0;
  270. /*
  271. * Fork all the shared blocks from our write offset until the end of
  272. * the extent.
  273. */
  274. error = xfs_qm_dqattach_locked(ip, false);
  275. if (error)
  276. return error;
  277. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
  278. imap->br_blockcount, 0, &got, &icur, eof);
  279. if (error == -ENOSPC || error == -EDQUOT)
  280. trace_xfs_reflink_cow_enospc(ip, imap);
  281. if (error)
  282. return error;
  283. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  284. trace_xfs_reflink_cow_alloc(ip, &got);
  285. return 0;
  286. }
  287. /* Convert part of an unwritten CoW extent to a real one. */
  288. STATIC int
  289. xfs_reflink_convert_cow_extent(
  290. struct xfs_inode *ip,
  291. struct xfs_bmbt_irec *imap,
  292. xfs_fileoff_t offset_fsb,
  293. xfs_filblks_t count_fsb)
  294. {
  295. int nimaps = 1;
  296. if (imap->br_state == XFS_EXT_NORM)
  297. return 0;
  298. xfs_trim_extent(imap, offset_fsb, count_fsb);
  299. trace_xfs_reflink_convert_cow(ip, imap);
  300. if (imap->br_blockcount == 0)
  301. return 0;
  302. return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
  303. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, 0, imap,
  304. &nimaps);
  305. }
  306. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  307. int
  308. xfs_reflink_convert_cow(
  309. struct xfs_inode *ip,
  310. xfs_off_t offset,
  311. xfs_off_t count)
  312. {
  313. struct xfs_mount *mp = ip->i_mount;
  314. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  315. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  316. xfs_filblks_t count_fsb = end_fsb - offset_fsb;
  317. struct xfs_bmbt_irec imap;
  318. int nimaps = 1, error = 0;
  319. ASSERT(count != 0);
  320. xfs_ilock(ip, XFS_ILOCK_EXCL);
  321. error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
  322. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
  323. XFS_BMAPI_CONVERT_ONLY, 0, &imap, &nimaps);
  324. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  325. return error;
  326. }
  327. /*
  328. * Find the extent that maps the given range in the COW fork. Even if the extent
  329. * is not shared we might have a preallocation for it in the COW fork. If so we
  330. * use it that rather than trigger a new allocation.
  331. */
  332. static int
  333. xfs_find_trim_cow_extent(
  334. struct xfs_inode *ip,
  335. struct xfs_bmbt_irec *imap,
  336. bool *shared,
  337. bool *found)
  338. {
  339. xfs_fileoff_t offset_fsb = imap->br_startoff;
  340. xfs_filblks_t count_fsb = imap->br_blockcount;
  341. struct xfs_iext_cursor icur;
  342. struct xfs_bmbt_irec got;
  343. bool trimmed;
  344. *found = false;
  345. /*
  346. * If we don't find an overlapping extent, trim the range we need to
  347. * allocate to fit the hole we found.
  348. */
  349. if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) ||
  350. got.br_startoff > offset_fsb)
  351. return xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  352. *shared = true;
  353. if (isnullstartblock(got.br_startblock)) {
  354. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  355. return 0;
  356. }
  357. /* real extent found - no need to allocate */
  358. xfs_trim_extent(&got, offset_fsb, count_fsb);
  359. *imap = got;
  360. *found = true;
  361. return 0;
  362. }
  363. /* Allocate all CoW reservations covering a range of blocks in a file. */
  364. int
  365. xfs_reflink_allocate_cow(
  366. struct xfs_inode *ip,
  367. struct xfs_bmbt_irec *imap,
  368. bool *shared,
  369. uint *lockmode)
  370. {
  371. struct xfs_mount *mp = ip->i_mount;
  372. xfs_fileoff_t offset_fsb = imap->br_startoff;
  373. xfs_filblks_t count_fsb = imap->br_blockcount;
  374. struct xfs_trans *tp;
  375. int nimaps, error = 0;
  376. bool found;
  377. xfs_filblks_t resaligned;
  378. xfs_extlen_t resblks = 0;
  379. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  380. ASSERT(xfs_is_reflink_inode(ip));
  381. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  382. if (error || !*shared)
  383. return error;
  384. if (found)
  385. goto convert;
  386. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  387. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  388. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  389. xfs_iunlock(ip, *lockmode);
  390. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  391. *lockmode = XFS_ILOCK_EXCL;
  392. xfs_ilock(ip, *lockmode);
  393. if (error)
  394. return error;
  395. error = xfs_qm_dqattach_locked(ip, false);
  396. if (error)
  397. goto out_trans_cancel;
  398. /*
  399. * Check for an overlapping extent again now that we dropped the ilock.
  400. */
  401. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  402. if (error || !*shared)
  403. goto out_trans_cancel;
  404. if (found) {
  405. xfs_trans_cancel(tp);
  406. goto convert;
  407. }
  408. error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
  409. XFS_QMOPT_RES_REGBLKS);
  410. if (error)
  411. goto out_trans_cancel;
  412. xfs_trans_ijoin(tp, ip, 0);
  413. /* Allocate the entire reservation as unwritten blocks. */
  414. nimaps = 1;
  415. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  416. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC,
  417. resblks, imap, &nimaps);
  418. if (error)
  419. goto out_unreserve;
  420. xfs_inode_set_cowblocks_tag(ip);
  421. error = xfs_trans_commit(tp);
  422. if (error)
  423. return error;
  424. /*
  425. * Allocation succeeded but the requested range was not even partially
  426. * satisfied? Bail out!
  427. */
  428. if (nimaps == 0)
  429. return -ENOSPC;
  430. convert:
  431. return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
  432. out_unreserve:
  433. xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
  434. XFS_QMOPT_RES_REGBLKS);
  435. out_trans_cancel:
  436. xfs_trans_cancel(tp);
  437. return error;
  438. }
  439. /*
  440. * Cancel CoW reservations for some block range of an inode.
  441. *
  442. * If cancel_real is true this function cancels all COW fork extents for the
  443. * inode; if cancel_real is false, real extents are not cleared.
  444. *
  445. * Caller must have already joined the inode to the current transaction. The
  446. * inode will be joined to the transaction returned to the caller.
  447. */
  448. int
  449. xfs_reflink_cancel_cow_blocks(
  450. struct xfs_inode *ip,
  451. struct xfs_trans **tpp,
  452. xfs_fileoff_t offset_fsb,
  453. xfs_fileoff_t end_fsb,
  454. bool cancel_real)
  455. {
  456. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  457. struct xfs_bmbt_irec got, del;
  458. struct xfs_iext_cursor icur;
  459. int error = 0;
  460. if (!xfs_inode_has_cow_data(ip))
  461. return 0;
  462. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  463. return 0;
  464. /* Walk backwards until we're out of the I/O range... */
  465. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  466. del = got;
  467. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  468. /* Extent delete may have bumped ext forward */
  469. if (!del.br_blockcount) {
  470. xfs_iext_prev(ifp, &icur);
  471. goto next_extent;
  472. }
  473. trace_xfs_reflink_cancel_cow(ip, &del);
  474. if (isnullstartblock(del.br_startblock)) {
  475. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  476. &icur, &got, &del);
  477. if (error)
  478. break;
  479. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  480. ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
  481. /* Free the CoW orphan record. */
  482. error = xfs_refcount_free_cow_extent(*tpp,
  483. del.br_startblock, del.br_blockcount);
  484. if (error)
  485. break;
  486. xfs_bmap_add_free(*tpp, del.br_startblock,
  487. del.br_blockcount, NULL);
  488. /* Roll the transaction */
  489. error = xfs_defer_finish(tpp);
  490. if (error)
  491. break;
  492. /* Remove the mapping from the CoW fork. */
  493. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  494. /* Remove the quota reservation */
  495. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  496. -(long)del.br_blockcount, 0,
  497. XFS_QMOPT_RES_REGBLKS);
  498. if (error)
  499. break;
  500. } else {
  501. /* Didn't do anything, push cursor back. */
  502. xfs_iext_prev(ifp, &icur);
  503. }
  504. next_extent:
  505. if (!xfs_iext_get_extent(ifp, &icur, &got))
  506. break;
  507. }
  508. /* clear tag if cow fork is emptied */
  509. if (!ifp->if_bytes)
  510. xfs_inode_clear_cowblocks_tag(ip);
  511. return error;
  512. }
  513. /*
  514. * Cancel CoW reservations for some byte range of an inode.
  515. *
  516. * If cancel_real is true this function cancels all COW fork extents for the
  517. * inode; if cancel_real is false, real extents are not cleared.
  518. */
  519. int
  520. xfs_reflink_cancel_cow_range(
  521. struct xfs_inode *ip,
  522. xfs_off_t offset,
  523. xfs_off_t count,
  524. bool cancel_real)
  525. {
  526. struct xfs_trans *tp;
  527. xfs_fileoff_t offset_fsb;
  528. xfs_fileoff_t end_fsb;
  529. int error;
  530. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  531. ASSERT(xfs_is_reflink_inode(ip));
  532. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  533. if (count == NULLFILEOFF)
  534. end_fsb = NULLFILEOFF;
  535. else
  536. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  537. /* Start a rolling transaction to remove the mappings */
  538. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  539. 0, 0, XFS_TRANS_NOFS, &tp);
  540. if (error)
  541. goto out;
  542. xfs_ilock(ip, XFS_ILOCK_EXCL);
  543. xfs_trans_ijoin(tp, ip, 0);
  544. /* Scrape out the old CoW reservations */
  545. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  546. cancel_real);
  547. if (error)
  548. goto out_cancel;
  549. error = xfs_trans_commit(tp);
  550. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  551. return error;
  552. out_cancel:
  553. xfs_trans_cancel(tp);
  554. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  555. out:
  556. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  557. return error;
  558. }
  559. /*
  560. * Remap parts of a file's data fork after a successful CoW.
  561. */
  562. int
  563. xfs_reflink_end_cow(
  564. struct xfs_inode *ip,
  565. xfs_off_t offset,
  566. xfs_off_t count)
  567. {
  568. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  569. struct xfs_bmbt_irec got, del;
  570. struct xfs_trans *tp;
  571. xfs_fileoff_t offset_fsb;
  572. xfs_fileoff_t end_fsb;
  573. int error;
  574. unsigned int resblks;
  575. xfs_filblks_t rlen;
  576. struct xfs_iext_cursor icur;
  577. trace_xfs_reflink_end_cow(ip, offset, count);
  578. /* No COW extents? That's easy! */
  579. if (ifp->if_bytes == 0)
  580. return 0;
  581. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  582. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  583. /*
  584. * Start a rolling transaction to switch the mappings. We're
  585. * unlikely ever to have to remap 16T worth of single-block
  586. * extents, so just cap the worst case extent count to 2^32-1.
  587. * Stick a warning in just in case, and avoid 64-bit division.
  588. */
  589. BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
  590. if (end_fsb - offset_fsb > UINT_MAX) {
  591. error = -EFSCORRUPTED;
  592. xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
  593. ASSERT(0);
  594. goto out;
  595. }
  596. resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
  597. (unsigned int)(end_fsb - offset_fsb),
  598. XFS_DATA_FORK);
  599. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  600. resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  601. if (error)
  602. goto out;
  603. xfs_ilock(ip, XFS_ILOCK_EXCL);
  604. xfs_trans_ijoin(tp, ip, 0);
  605. /*
  606. * In case of racing, overlapping AIO writes no COW extents might be
  607. * left by the time I/O completes for the loser of the race. In that
  608. * case we are done.
  609. */
  610. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  611. goto out_cancel;
  612. /* Walk backwards until we're out of the I/O range... */
  613. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  614. del = got;
  615. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  616. /* Extent delete may have bumped ext forward */
  617. if (!del.br_blockcount)
  618. goto prev_extent;
  619. /*
  620. * Only remap real extent that contain data. With AIO
  621. * speculatively preallocations can leak into the range we
  622. * are called upon, and we need to skip them.
  623. */
  624. if (!xfs_bmap_is_real_extent(&got))
  625. goto prev_extent;
  626. /* Unmap the old blocks in the data fork. */
  627. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  628. rlen = del.br_blockcount;
  629. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
  630. if (error)
  631. goto out_cancel;
  632. /* Trim the extent to whatever got unmapped. */
  633. if (rlen) {
  634. xfs_trim_extent(&del, del.br_startoff + rlen,
  635. del.br_blockcount - rlen);
  636. }
  637. trace_xfs_reflink_cow_remap(ip, &del);
  638. /* Free the CoW orphan record. */
  639. error = xfs_refcount_free_cow_extent(tp, del.br_startblock,
  640. del.br_blockcount);
  641. if (error)
  642. goto out_cancel;
  643. /* Map the new blocks into the data fork. */
  644. error = xfs_bmap_map_extent(tp, ip, &del);
  645. if (error)
  646. goto out_cancel;
  647. /* Charge this new data fork mapping to the on-disk quota. */
  648. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
  649. (long)del.br_blockcount);
  650. /* Remove the mapping from the CoW fork. */
  651. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  652. error = xfs_defer_finish(&tp);
  653. if (error)
  654. goto out_cancel;
  655. if (!xfs_iext_get_extent(ifp, &icur, &got))
  656. break;
  657. continue;
  658. prev_extent:
  659. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  660. break;
  661. }
  662. error = xfs_trans_commit(tp);
  663. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  664. if (error)
  665. goto out;
  666. return 0;
  667. out_cancel:
  668. xfs_trans_cancel(tp);
  669. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  670. out:
  671. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  672. return error;
  673. }
  674. /*
  675. * Free leftover CoW reservations that didn't get cleaned out.
  676. */
  677. int
  678. xfs_reflink_recover_cow(
  679. struct xfs_mount *mp)
  680. {
  681. xfs_agnumber_t agno;
  682. int error = 0;
  683. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  684. return 0;
  685. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  686. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  687. if (error)
  688. break;
  689. }
  690. return error;
  691. }
  692. /*
  693. * Reflinking (Block) Ranges of Two Files Together
  694. *
  695. * First, ensure that the reflink flag is set on both inodes. The flag is an
  696. * optimization to avoid unnecessary refcount btree lookups in the write path.
  697. *
  698. * Now we can iteratively remap the range of extents (and holes) in src to the
  699. * corresponding ranges in dest. Let drange and srange denote the ranges of
  700. * logical blocks in dest and src touched by the reflink operation.
  701. *
  702. * While the length of drange is greater than zero,
  703. * - Read src's bmbt at the start of srange ("imap")
  704. * - If imap doesn't exist, make imap appear to start at the end of srange
  705. * with zero length.
  706. * - If imap starts before srange, advance imap to start at srange.
  707. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  708. * - Punch (imap start - srange start + imap len) blocks from dest at
  709. * offset (drange start).
  710. * - If imap points to a real range of pblks,
  711. * > Increase the refcount of the imap's pblks
  712. * > Map imap's pblks into dest at the offset
  713. * (drange start + imap start - srange start)
  714. * - Advance drange and srange by (imap start - srange start + imap len)
  715. *
  716. * Finally, if the reflink made dest longer, update both the in-core and
  717. * on-disk file sizes.
  718. *
  719. * ASCII Art Demonstration:
  720. *
  721. * Let's say we want to reflink this source file:
  722. *
  723. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  724. * <-------------------->
  725. *
  726. * into this destination file:
  727. *
  728. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  729. * <-------------------->
  730. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  731. * Observe that the range has different logical offsets in either file.
  732. *
  733. * Consider that the first extent in the source file doesn't line up with our
  734. * reflink range. Unmapping and remapping are separate operations, so we can
  735. * unmap more blocks from the destination file than we remap.
  736. *
  737. * ----SSSSSSS-SSSSS----SSSSSS
  738. * <------->
  739. * --DDDDD---------DDDDD--DDD
  740. * <------->
  741. *
  742. * Now remap the source extent into the destination file:
  743. *
  744. * ----SSSSSSS-SSSSS----SSSSSS
  745. * <------->
  746. * --DDDDD--SSSSSSSDDDDD--DDD
  747. * <------->
  748. *
  749. * Do likewise with the second hole and extent in our range. Holes in the
  750. * unmap range don't affect our operation.
  751. *
  752. * ----SSSSSSS-SSSSS----SSSSSS
  753. * <---->
  754. * --DDDDD--SSSSSSS-SSSSS-DDD
  755. * <---->
  756. *
  757. * Finally, unmap and remap part of the third extent. This will increase the
  758. * size of the destination file.
  759. *
  760. * ----SSSSSSS-SSSSS----SSSSSS
  761. * <----->
  762. * --DDDDD--SSSSSSS-SSSSS----SSS
  763. * <----->
  764. *
  765. * Once we update the destination file's i_size, we're done.
  766. */
  767. /*
  768. * Ensure the reflink bit is set in both inodes.
  769. */
  770. STATIC int
  771. xfs_reflink_set_inode_flag(
  772. struct xfs_inode *src,
  773. struct xfs_inode *dest)
  774. {
  775. struct xfs_mount *mp = src->i_mount;
  776. int error;
  777. struct xfs_trans *tp;
  778. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  779. return 0;
  780. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  781. if (error)
  782. goto out_error;
  783. /* Lock both files against IO */
  784. if (src->i_ino == dest->i_ino)
  785. xfs_ilock(src, XFS_ILOCK_EXCL);
  786. else
  787. xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
  788. if (!xfs_is_reflink_inode(src)) {
  789. trace_xfs_reflink_set_inode_flag(src);
  790. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  791. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  792. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  793. xfs_ifork_init_cow(src);
  794. } else
  795. xfs_iunlock(src, XFS_ILOCK_EXCL);
  796. if (src->i_ino == dest->i_ino)
  797. goto commit_flags;
  798. if (!xfs_is_reflink_inode(dest)) {
  799. trace_xfs_reflink_set_inode_flag(dest);
  800. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  801. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  802. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  803. xfs_ifork_init_cow(dest);
  804. } else
  805. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  806. commit_flags:
  807. error = xfs_trans_commit(tp);
  808. if (error)
  809. goto out_error;
  810. return error;
  811. out_error:
  812. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  813. return error;
  814. }
  815. /*
  816. * Update destination inode size & cowextsize hint, if necessary.
  817. */
  818. STATIC int
  819. xfs_reflink_update_dest(
  820. struct xfs_inode *dest,
  821. xfs_off_t newlen,
  822. xfs_extlen_t cowextsize,
  823. bool is_dedupe)
  824. {
  825. struct xfs_mount *mp = dest->i_mount;
  826. struct xfs_trans *tp;
  827. int error;
  828. if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  829. return 0;
  830. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  831. if (error)
  832. goto out_error;
  833. xfs_ilock(dest, XFS_ILOCK_EXCL);
  834. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  835. if (newlen > i_size_read(VFS_I(dest))) {
  836. trace_xfs_reflink_update_inode_size(dest, newlen);
  837. i_size_write(VFS_I(dest), newlen);
  838. dest->i_d.di_size = newlen;
  839. }
  840. if (cowextsize) {
  841. dest->i_d.di_cowextsize = cowextsize;
  842. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  843. }
  844. if (!is_dedupe) {
  845. xfs_trans_ichgtime(tp, dest,
  846. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  847. }
  848. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  849. error = xfs_trans_commit(tp);
  850. if (error)
  851. goto out_error;
  852. return error;
  853. out_error:
  854. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  855. return error;
  856. }
  857. /*
  858. * Do we have enough reserve in this AG to handle a reflink? The refcount
  859. * btree already reserved all the space it needs, but the rmap btree can grow
  860. * infinitely, so we won't allow more reflinks when the AG is down to the
  861. * btree reserves.
  862. */
  863. static int
  864. xfs_reflink_ag_has_free_space(
  865. struct xfs_mount *mp,
  866. xfs_agnumber_t agno)
  867. {
  868. struct xfs_perag *pag;
  869. int error = 0;
  870. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  871. return 0;
  872. pag = xfs_perag_get(mp, agno);
  873. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
  874. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  875. error = -ENOSPC;
  876. xfs_perag_put(pag);
  877. return error;
  878. }
  879. /*
  880. * Unmap a range of blocks from a file, then map other blocks into the hole.
  881. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  882. * The extent irec is mapped into dest at irec->br_startoff.
  883. */
  884. STATIC int
  885. xfs_reflink_remap_extent(
  886. struct xfs_inode *ip,
  887. struct xfs_bmbt_irec *irec,
  888. xfs_fileoff_t destoff,
  889. xfs_off_t new_isize)
  890. {
  891. struct xfs_mount *mp = ip->i_mount;
  892. bool real_extent = xfs_bmap_is_real_extent(irec);
  893. struct xfs_trans *tp;
  894. unsigned int resblks;
  895. struct xfs_bmbt_irec uirec;
  896. xfs_filblks_t rlen;
  897. xfs_filblks_t unmap_len;
  898. xfs_off_t newlen;
  899. int64_t qres;
  900. int error;
  901. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  902. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  903. /* No reflinking if we're low on space */
  904. if (real_extent) {
  905. error = xfs_reflink_ag_has_free_space(mp,
  906. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  907. if (error)
  908. goto out;
  909. }
  910. /* Start a rolling transaction to switch the mappings */
  911. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  912. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  913. if (error)
  914. goto out;
  915. xfs_ilock(ip, XFS_ILOCK_EXCL);
  916. xfs_trans_ijoin(tp, ip, 0);
  917. /*
  918. * Reserve quota for this operation. We don't know if the first unmap
  919. * in the dest file will cause a bmap btree split, so we always reserve
  920. * at least enough blocks for that split. If the extent being mapped
  921. * in is written, we need to reserve quota for that too.
  922. */
  923. qres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  924. if (real_extent)
  925. qres += irec->br_blockcount;
  926. error = xfs_trans_reserve_quota_nblks(tp, ip, qres, 0,
  927. XFS_QMOPT_RES_REGBLKS);
  928. if (error)
  929. goto out_cancel;
  930. trace_xfs_reflink_remap(ip, irec->br_startoff,
  931. irec->br_blockcount, irec->br_startblock);
  932. /* Unmap the old blocks in the data fork. */
  933. rlen = unmap_len;
  934. while (rlen) {
  935. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  936. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
  937. if (error)
  938. goto out_cancel;
  939. /*
  940. * Trim the extent to whatever got unmapped.
  941. * Remember, bunmapi works backwards.
  942. */
  943. uirec.br_startblock = irec->br_startblock + rlen;
  944. uirec.br_startoff = irec->br_startoff + rlen;
  945. uirec.br_blockcount = unmap_len - rlen;
  946. uirec.br_state = irec->br_state;
  947. unmap_len = rlen;
  948. /* If this isn't a real mapping, we're done. */
  949. if (!real_extent || uirec.br_blockcount == 0)
  950. goto next_extent;
  951. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  952. uirec.br_blockcount, uirec.br_startblock);
  953. /* Update the refcount tree */
  954. error = xfs_refcount_increase_extent(tp, &uirec);
  955. if (error)
  956. goto out_cancel;
  957. /* Map the new blocks into the data fork. */
  958. error = xfs_bmap_map_extent(tp, ip, &uirec);
  959. if (error)
  960. goto out_cancel;
  961. /* Update quota accounting. */
  962. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  963. uirec.br_blockcount);
  964. /* Update dest isize if needed. */
  965. newlen = XFS_FSB_TO_B(mp,
  966. uirec.br_startoff + uirec.br_blockcount);
  967. newlen = min_t(xfs_off_t, newlen, new_isize);
  968. if (newlen > i_size_read(VFS_I(ip))) {
  969. trace_xfs_reflink_update_inode_size(ip, newlen);
  970. i_size_write(VFS_I(ip), newlen);
  971. ip->i_d.di_size = newlen;
  972. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  973. }
  974. next_extent:
  975. /* Process all the deferred stuff. */
  976. error = xfs_defer_finish(&tp);
  977. if (error)
  978. goto out_cancel;
  979. }
  980. error = xfs_trans_commit(tp);
  981. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  982. if (error)
  983. goto out;
  984. return 0;
  985. out_cancel:
  986. xfs_trans_cancel(tp);
  987. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  988. out:
  989. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  990. return error;
  991. }
  992. /*
  993. * Iteratively remap one file's extents (and holes) to another's.
  994. */
  995. STATIC int
  996. xfs_reflink_remap_blocks(
  997. struct xfs_inode *src,
  998. xfs_fileoff_t srcoff,
  999. struct xfs_inode *dest,
  1000. xfs_fileoff_t destoff,
  1001. xfs_filblks_t len,
  1002. xfs_off_t new_isize)
  1003. {
  1004. struct xfs_bmbt_irec imap;
  1005. int nimaps;
  1006. int error = 0;
  1007. xfs_filblks_t range_len;
  1008. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1009. while (len) {
  1010. uint lock_mode;
  1011. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1012. dest, destoff);
  1013. /* Read extent from the source file */
  1014. nimaps = 1;
  1015. lock_mode = xfs_ilock_data_map_shared(src);
  1016. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1017. xfs_iunlock(src, lock_mode);
  1018. if (error)
  1019. goto err;
  1020. ASSERT(nimaps == 1);
  1021. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1022. &imap);
  1023. /* Translate imap into the destination file. */
  1024. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1025. imap.br_startoff += destoff - srcoff;
  1026. /* Clear dest from destoff to the end of imap and map it in. */
  1027. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1028. new_isize);
  1029. if (error)
  1030. goto err;
  1031. if (fatal_signal_pending(current)) {
  1032. error = -EINTR;
  1033. goto err;
  1034. }
  1035. /* Advance drange/srange */
  1036. srcoff += range_len;
  1037. destoff += range_len;
  1038. len -= range_len;
  1039. }
  1040. return 0;
  1041. err:
  1042. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1043. return error;
  1044. }
  1045. /*
  1046. * Grab the exclusive iolock for a data copy from src to dest, making
  1047. * sure to abide vfs locking order (lowest pointer value goes first) and
  1048. * breaking the pnfs layout leases on dest before proceeding. The loop
  1049. * is needed because we cannot call the blocking break_layout() with the
  1050. * src iolock held, and therefore have to back out both locks.
  1051. */
  1052. static int
  1053. xfs_iolock_two_inodes_and_break_layout(
  1054. struct inode *src,
  1055. struct inode *dest)
  1056. {
  1057. int error;
  1058. retry:
  1059. if (src < dest) {
  1060. inode_lock_shared(src);
  1061. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  1062. } else {
  1063. /* src >= dest */
  1064. inode_lock(dest);
  1065. }
  1066. error = break_layout(dest, false);
  1067. if (error == -EWOULDBLOCK) {
  1068. inode_unlock(dest);
  1069. if (src < dest)
  1070. inode_unlock_shared(src);
  1071. error = break_layout(dest, true);
  1072. if (error)
  1073. return error;
  1074. goto retry;
  1075. }
  1076. if (error) {
  1077. inode_unlock(dest);
  1078. if (src < dest)
  1079. inode_unlock_shared(src);
  1080. return error;
  1081. }
  1082. if (src > dest)
  1083. inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
  1084. return 0;
  1085. }
  1086. /* Unlock both inodes after they've been prepped for a range clone. */
  1087. STATIC void
  1088. xfs_reflink_remap_unlock(
  1089. struct file *file_in,
  1090. struct file *file_out)
  1091. {
  1092. struct inode *inode_in = file_inode(file_in);
  1093. struct xfs_inode *src = XFS_I(inode_in);
  1094. struct inode *inode_out = file_inode(file_out);
  1095. struct xfs_inode *dest = XFS_I(inode_out);
  1096. bool same_inode = (inode_in == inode_out);
  1097. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1098. if (!same_inode)
  1099. xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
  1100. inode_unlock(inode_out);
  1101. if (!same_inode)
  1102. inode_unlock_shared(inode_in);
  1103. }
  1104. /*
  1105. * If we're reflinking to a point past the destination file's EOF, we must
  1106. * zero any speculative post-EOF preallocations that sit between the old EOF
  1107. * and the destination file offset.
  1108. */
  1109. static int
  1110. xfs_reflink_zero_posteof(
  1111. struct xfs_inode *ip,
  1112. loff_t pos)
  1113. {
  1114. loff_t isize = i_size_read(VFS_I(ip));
  1115. if (pos <= isize)
  1116. return 0;
  1117. trace_xfs_zero_eof(ip, isize, pos - isize);
  1118. return iomap_zero_range(VFS_I(ip), isize, pos - isize, NULL,
  1119. &xfs_iomap_ops);
  1120. }
  1121. /*
  1122. * Prepare two files for range cloning. Upon a successful return both inodes
  1123. * will have the iolock and mmaplock held, the page cache of the out file will
  1124. * be truncated, and any leases on the out file will have been broken. This
  1125. * function borrows heavily from xfs_file_aio_write_checks.
  1126. *
  1127. * The VFS allows partial EOF blocks to "match" for dedupe even though it hasn't
  1128. * checked that the bytes beyond EOF physically match. Hence we cannot use the
  1129. * EOF block in the source dedupe range because it's not a complete block match,
  1130. * hence can introduce a corruption into the file that has it's block replaced.
  1131. *
  1132. * In similar fashion, the VFS file cloning also allows partial EOF blocks to be
  1133. * "block aligned" for the purposes of cloning entire files. However, if the
  1134. * source file range includes the EOF block and it lands within the existing EOF
  1135. * of the destination file, then we can expose stale data from beyond the source
  1136. * file EOF in the destination file.
  1137. *
  1138. * XFS doesn't support partial block sharing, so in both cases we have check
  1139. * these cases ourselves. For dedupe, we can simply round the length to dedupe
  1140. * down to the previous whole block and ignore the partial EOF block. While this
  1141. * means we can't dedupe the last block of a file, this is an acceptible
  1142. * tradeoff for simplicity on implementation.
  1143. *
  1144. * For cloning, we want to share the partial EOF block if it is also the new EOF
  1145. * block of the destination file. If the partial EOF block lies inside the
  1146. * existing destination EOF, then we have to abort the clone to avoid exposing
  1147. * stale data in the destination file. Hence we reject these clone attempts with
  1148. * -EINVAL in this case.
  1149. */
  1150. STATIC int
  1151. xfs_reflink_remap_prep(
  1152. struct file *file_in,
  1153. loff_t pos_in,
  1154. struct file *file_out,
  1155. loff_t pos_out,
  1156. u64 *len,
  1157. bool is_dedupe)
  1158. {
  1159. struct inode *inode_in = file_inode(file_in);
  1160. struct xfs_inode *src = XFS_I(inode_in);
  1161. struct inode *inode_out = file_inode(file_out);
  1162. struct xfs_inode *dest = XFS_I(inode_out);
  1163. bool same_inode = (inode_in == inode_out);
  1164. u64 blkmask = i_blocksize(inode_in) - 1;
  1165. ssize_t ret;
  1166. /* Lock both files against IO */
  1167. ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
  1168. if (ret)
  1169. return ret;
  1170. if (same_inode)
  1171. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1172. else
  1173. xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
  1174. XFS_MMAPLOCK_EXCL);
  1175. /* Check file eligibility and prepare for block sharing. */
  1176. ret = -EINVAL;
  1177. /* Don't reflink realtime inodes */
  1178. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1179. goto out_unlock;
  1180. /* Don't share DAX file data for now. */
  1181. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1182. goto out_unlock;
  1183. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1184. len, is_dedupe);
  1185. if (ret <= 0)
  1186. goto out_unlock;
  1187. /*
  1188. * If the dedupe data matches, chop off the partial EOF block
  1189. * from the source file so we don't try to dedupe the partial
  1190. * EOF block.
  1191. */
  1192. if (is_dedupe) {
  1193. *len &= ~blkmask;
  1194. } else if (*len & blkmask) {
  1195. /*
  1196. * The user is attempting to share a partial EOF block,
  1197. * if it's inside the destination EOF then reject it.
  1198. */
  1199. if (pos_out + *len < i_size_read(inode_out)) {
  1200. ret = -EINVAL;
  1201. goto out_unlock;
  1202. }
  1203. }
  1204. /* Attach dquots to dest inode before changing block map */
  1205. ret = xfs_qm_dqattach(dest);
  1206. if (ret)
  1207. goto out_unlock;
  1208. /*
  1209. * Zero existing post-eof speculative preallocations in the destination
  1210. * file.
  1211. */
  1212. ret = xfs_reflink_zero_posteof(dest, pos_out);
  1213. if (ret)
  1214. goto out_unlock;
  1215. /* Set flags and remap blocks. */
  1216. ret = xfs_reflink_set_inode_flag(src, dest);
  1217. if (ret)
  1218. goto out_unlock;
  1219. /*
  1220. * If pos_out > EOF, we may have dirtied blocks between EOF and
  1221. * pos_out. In that case, we need to extend the flush and unmap to cover
  1222. * from EOF to the end of the copy length.
  1223. */
  1224. if (pos_out > XFS_ISIZE(dest)) {
  1225. loff_t flen = *len + (pos_out - XFS_ISIZE(dest));
  1226. ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
  1227. } else {
  1228. ret = xfs_flush_unmap_range(dest, pos_out, *len);
  1229. }
  1230. if (ret)
  1231. goto out_unlock;
  1232. /* If we're altering the file contents... */
  1233. if (!is_dedupe) {
  1234. /*
  1235. * ...update the timestamps (which will grab the ilock again
  1236. * from xfs_fs_dirty_inode, so we have to call it before we
  1237. * take the ilock).
  1238. */
  1239. if (!(file_out->f_mode & FMODE_NOCMTIME)) {
  1240. ret = file_update_time(file_out);
  1241. if (ret)
  1242. goto out_unlock;
  1243. }
  1244. /*
  1245. * ...clear the security bits if the process is not being run
  1246. * by root. This keeps people from modifying setuid and setgid
  1247. * binaries.
  1248. */
  1249. ret = file_remove_privs(file_out);
  1250. if (ret)
  1251. goto out_unlock;
  1252. }
  1253. return 1;
  1254. out_unlock:
  1255. xfs_reflink_remap_unlock(file_in, file_out);
  1256. return ret;
  1257. }
  1258. /*
  1259. * Link a range of blocks from one file to another.
  1260. */
  1261. int
  1262. xfs_reflink_remap_range(
  1263. struct file *file_in,
  1264. loff_t pos_in,
  1265. struct file *file_out,
  1266. loff_t pos_out,
  1267. u64 len,
  1268. bool is_dedupe)
  1269. {
  1270. struct inode *inode_in = file_inode(file_in);
  1271. struct xfs_inode *src = XFS_I(inode_in);
  1272. struct inode *inode_out = file_inode(file_out);
  1273. struct xfs_inode *dest = XFS_I(inode_out);
  1274. struct xfs_mount *mp = src->i_mount;
  1275. xfs_fileoff_t sfsbno, dfsbno;
  1276. xfs_filblks_t fsblen;
  1277. xfs_extlen_t cowextsize;
  1278. ssize_t ret;
  1279. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1280. return -EOPNOTSUPP;
  1281. if (XFS_FORCED_SHUTDOWN(mp))
  1282. return -EIO;
  1283. /* Prepare and then clone file data. */
  1284. ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
  1285. &len, is_dedupe);
  1286. if (ret <= 0)
  1287. return ret;
  1288. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1289. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1290. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1291. fsblen = XFS_B_TO_FSB(mp, len);
  1292. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1293. pos_out + len);
  1294. if (ret)
  1295. goto out_unlock;
  1296. /*
  1297. * Carry the cowextsize hint from src to dest if we're sharing the
  1298. * entire source file to the entire destination file, the source file
  1299. * has a cowextsize hint, and the destination file does not.
  1300. */
  1301. cowextsize = 0;
  1302. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1303. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1304. pos_out == 0 && len >= i_size_read(inode_out) &&
  1305. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1306. cowextsize = src->i_d.di_cowextsize;
  1307. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1308. is_dedupe);
  1309. out_unlock:
  1310. xfs_reflink_remap_unlock(file_in, file_out);
  1311. if (ret)
  1312. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1313. return ret;
  1314. }
  1315. /*
  1316. * The user wants to preemptively CoW all shared blocks in this file,
  1317. * which enables us to turn off the reflink flag. Iterate all
  1318. * extents which are not prealloc/delalloc to see which ranges are
  1319. * mentioned in the refcount tree, then read those blocks into the
  1320. * pagecache, dirty them, fsync them back out, and then we can update
  1321. * the inode flag. What happens if we run out of memory? :)
  1322. */
  1323. STATIC int
  1324. xfs_reflink_dirty_extents(
  1325. struct xfs_inode *ip,
  1326. xfs_fileoff_t fbno,
  1327. xfs_filblks_t end,
  1328. xfs_off_t isize)
  1329. {
  1330. struct xfs_mount *mp = ip->i_mount;
  1331. xfs_agnumber_t agno;
  1332. xfs_agblock_t agbno;
  1333. xfs_extlen_t aglen;
  1334. xfs_agblock_t rbno;
  1335. xfs_extlen_t rlen;
  1336. xfs_off_t fpos;
  1337. xfs_off_t flen;
  1338. struct xfs_bmbt_irec map[2];
  1339. int nmaps;
  1340. int error = 0;
  1341. while (end - fbno > 0) {
  1342. nmaps = 1;
  1343. /*
  1344. * Look for extents in the file. Skip holes, delalloc, or
  1345. * unwritten extents; they can't be reflinked.
  1346. */
  1347. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1348. if (error)
  1349. goto out;
  1350. if (nmaps == 0)
  1351. break;
  1352. if (!xfs_bmap_is_real_extent(&map[0]))
  1353. goto next;
  1354. map[1] = map[0];
  1355. while (map[1].br_blockcount) {
  1356. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1357. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1358. aglen = map[1].br_blockcount;
  1359. error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
  1360. aglen, &rbno, &rlen, true);
  1361. if (error)
  1362. goto out;
  1363. if (rbno == NULLAGBLOCK)
  1364. break;
  1365. /* Dirty the pages */
  1366. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1367. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1368. (rbno - agbno));
  1369. flen = XFS_FSB_TO_B(mp, rlen);
  1370. if (fpos + flen > isize)
  1371. flen = isize - fpos;
  1372. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1373. &xfs_iomap_ops);
  1374. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1375. if (error)
  1376. goto out;
  1377. map[1].br_blockcount -= (rbno - agbno + rlen);
  1378. map[1].br_startoff += (rbno - agbno + rlen);
  1379. map[1].br_startblock += (rbno - agbno + rlen);
  1380. }
  1381. next:
  1382. fbno = map[0].br_startoff + map[0].br_blockcount;
  1383. }
  1384. out:
  1385. return error;
  1386. }
  1387. /* Does this inode need the reflink flag? */
  1388. int
  1389. xfs_reflink_inode_has_shared_extents(
  1390. struct xfs_trans *tp,
  1391. struct xfs_inode *ip,
  1392. bool *has_shared)
  1393. {
  1394. struct xfs_bmbt_irec got;
  1395. struct xfs_mount *mp = ip->i_mount;
  1396. struct xfs_ifork *ifp;
  1397. xfs_agnumber_t agno;
  1398. xfs_agblock_t agbno;
  1399. xfs_extlen_t aglen;
  1400. xfs_agblock_t rbno;
  1401. xfs_extlen_t rlen;
  1402. struct xfs_iext_cursor icur;
  1403. bool found;
  1404. int error;
  1405. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1406. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1407. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1408. if (error)
  1409. return error;
  1410. }
  1411. *has_shared = false;
  1412. found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
  1413. while (found) {
  1414. if (isnullstartblock(got.br_startblock) ||
  1415. got.br_state != XFS_EXT_NORM)
  1416. goto next;
  1417. agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
  1418. agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
  1419. aglen = got.br_blockcount;
  1420. error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
  1421. &rbno, &rlen, false);
  1422. if (error)
  1423. return error;
  1424. /* Is there still a shared block here? */
  1425. if (rbno != NULLAGBLOCK) {
  1426. *has_shared = true;
  1427. return 0;
  1428. }
  1429. next:
  1430. found = xfs_iext_next_extent(ifp, &icur, &got);
  1431. }
  1432. return 0;
  1433. }
  1434. /*
  1435. * Clear the inode reflink flag if there are no shared extents.
  1436. *
  1437. * The caller is responsible for joining the inode to the transaction passed in.
  1438. * The inode will be joined to the transaction that is returned to the caller.
  1439. */
  1440. int
  1441. xfs_reflink_clear_inode_flag(
  1442. struct xfs_inode *ip,
  1443. struct xfs_trans **tpp)
  1444. {
  1445. bool needs_flag;
  1446. int error = 0;
  1447. ASSERT(xfs_is_reflink_inode(ip));
  1448. error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
  1449. if (error || needs_flag)
  1450. return error;
  1451. /*
  1452. * We didn't find any shared blocks so turn off the reflink flag.
  1453. * First, get rid of any leftover CoW mappings.
  1454. */
  1455. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1456. if (error)
  1457. return error;
  1458. /* Clear the inode flag. */
  1459. trace_xfs_reflink_unset_inode_flag(ip);
  1460. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1461. xfs_inode_clear_cowblocks_tag(ip);
  1462. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1463. return error;
  1464. }
  1465. /*
  1466. * Clear the inode reflink flag if there are no shared extents and the size
  1467. * hasn't changed.
  1468. */
  1469. STATIC int
  1470. xfs_reflink_try_clear_inode_flag(
  1471. struct xfs_inode *ip)
  1472. {
  1473. struct xfs_mount *mp = ip->i_mount;
  1474. struct xfs_trans *tp;
  1475. int error = 0;
  1476. /* Start a rolling transaction to remove the mappings */
  1477. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1478. if (error)
  1479. return error;
  1480. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1481. xfs_trans_ijoin(tp, ip, 0);
  1482. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1483. if (error)
  1484. goto cancel;
  1485. error = xfs_trans_commit(tp);
  1486. if (error)
  1487. goto out;
  1488. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1489. return 0;
  1490. cancel:
  1491. xfs_trans_cancel(tp);
  1492. out:
  1493. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1494. return error;
  1495. }
  1496. /*
  1497. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1498. * the reflink flag if we unshare all of the file's blocks.
  1499. */
  1500. int
  1501. xfs_reflink_unshare(
  1502. struct xfs_inode *ip,
  1503. xfs_off_t offset,
  1504. xfs_off_t len)
  1505. {
  1506. struct xfs_mount *mp = ip->i_mount;
  1507. xfs_fileoff_t fbno;
  1508. xfs_filblks_t end;
  1509. xfs_off_t isize;
  1510. int error;
  1511. if (!xfs_is_reflink_inode(ip))
  1512. return 0;
  1513. trace_xfs_reflink_unshare(ip, offset, len);
  1514. inode_dio_wait(VFS_I(ip));
  1515. /* Try to CoW the selected ranges */
  1516. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1517. fbno = XFS_B_TO_FSBT(mp, offset);
  1518. isize = i_size_read(VFS_I(ip));
  1519. end = XFS_B_TO_FSB(mp, offset + len);
  1520. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1521. if (error)
  1522. goto out_unlock;
  1523. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1524. /* Wait for the IO to finish */
  1525. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1526. if (error)
  1527. goto out;
  1528. /* Turn off the reflink flag if possible. */
  1529. error = xfs_reflink_try_clear_inode_flag(ip);
  1530. if (error)
  1531. goto out;
  1532. return 0;
  1533. out_unlock:
  1534. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1535. out:
  1536. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1537. return error;
  1538. }