xfs_reflink.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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. trace_xfs_reflink_cow_alloc(ip, &got);
  284. return 0;
  285. }
  286. /* Convert part of an unwritten CoW extent to a real one. */
  287. STATIC int
  288. xfs_reflink_convert_cow_extent(
  289. struct xfs_inode *ip,
  290. struct xfs_bmbt_irec *imap,
  291. xfs_fileoff_t offset_fsb,
  292. xfs_filblks_t count_fsb)
  293. {
  294. int nimaps = 1;
  295. if (imap->br_state == XFS_EXT_NORM)
  296. return 0;
  297. xfs_trim_extent(imap, offset_fsb, count_fsb);
  298. trace_xfs_reflink_convert_cow(ip, imap);
  299. if (imap->br_blockcount == 0)
  300. return 0;
  301. return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
  302. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, 0, imap,
  303. &nimaps);
  304. }
  305. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  306. int
  307. xfs_reflink_convert_cow(
  308. struct xfs_inode *ip,
  309. xfs_off_t offset,
  310. xfs_off_t count)
  311. {
  312. struct xfs_mount *mp = ip->i_mount;
  313. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  314. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  315. xfs_filblks_t count_fsb = end_fsb - offset_fsb;
  316. struct xfs_bmbt_irec imap;
  317. int nimaps = 1, error = 0;
  318. ASSERT(count != 0);
  319. xfs_ilock(ip, XFS_ILOCK_EXCL);
  320. error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
  321. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
  322. XFS_BMAPI_CONVERT_ONLY, 0, &imap, &nimaps);
  323. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  324. return error;
  325. }
  326. /*
  327. * Find the extent that maps the given range in the COW fork. Even if the extent
  328. * is not shared we might have a preallocation for it in the COW fork. If so we
  329. * use it that rather than trigger a new allocation.
  330. */
  331. static int
  332. xfs_find_trim_cow_extent(
  333. struct xfs_inode *ip,
  334. struct xfs_bmbt_irec *imap,
  335. bool *shared,
  336. bool *found)
  337. {
  338. xfs_fileoff_t offset_fsb = imap->br_startoff;
  339. xfs_filblks_t count_fsb = imap->br_blockcount;
  340. struct xfs_iext_cursor icur;
  341. struct xfs_bmbt_irec got;
  342. bool trimmed;
  343. *found = false;
  344. /*
  345. * If we don't find an overlapping extent, trim the range we need to
  346. * allocate to fit the hole we found.
  347. */
  348. if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) ||
  349. got.br_startoff > offset_fsb)
  350. return xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  351. *shared = true;
  352. if (isnullstartblock(got.br_startblock)) {
  353. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  354. return 0;
  355. }
  356. /* real extent found - no need to allocate */
  357. xfs_trim_extent(&got, offset_fsb, count_fsb);
  358. *imap = got;
  359. *found = true;
  360. return 0;
  361. }
  362. /* Allocate all CoW reservations covering a range of blocks in a file. */
  363. int
  364. xfs_reflink_allocate_cow(
  365. struct xfs_inode *ip,
  366. struct xfs_bmbt_irec *imap,
  367. bool *shared,
  368. uint *lockmode)
  369. {
  370. struct xfs_mount *mp = ip->i_mount;
  371. xfs_fileoff_t offset_fsb = imap->br_startoff;
  372. xfs_filblks_t count_fsb = imap->br_blockcount;
  373. struct xfs_trans *tp;
  374. int nimaps, error = 0;
  375. bool found;
  376. xfs_filblks_t resaligned;
  377. xfs_extlen_t resblks = 0;
  378. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  379. ASSERT(xfs_is_reflink_inode(ip));
  380. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  381. if (error || !*shared)
  382. return error;
  383. if (found)
  384. goto convert;
  385. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  386. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  387. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  388. xfs_iunlock(ip, *lockmode);
  389. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  390. *lockmode = XFS_ILOCK_EXCL;
  391. xfs_ilock(ip, *lockmode);
  392. if (error)
  393. return error;
  394. error = xfs_qm_dqattach_locked(ip, false);
  395. if (error)
  396. goto out_trans_cancel;
  397. /*
  398. * Check for an overlapping extent again now that we dropped the ilock.
  399. */
  400. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  401. if (error || !*shared)
  402. goto out_trans_cancel;
  403. if (found) {
  404. xfs_trans_cancel(tp);
  405. goto convert;
  406. }
  407. error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
  408. XFS_QMOPT_RES_REGBLKS);
  409. if (error)
  410. goto out_trans_cancel;
  411. xfs_trans_ijoin(tp, ip, 0);
  412. /* Allocate the entire reservation as unwritten blocks. */
  413. nimaps = 1;
  414. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  415. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC,
  416. resblks, imap, &nimaps);
  417. if (error)
  418. goto out_unreserve;
  419. xfs_inode_set_cowblocks_tag(ip);
  420. error = xfs_trans_commit(tp);
  421. if (error)
  422. return error;
  423. /*
  424. * Allocation succeeded but the requested range was not even partially
  425. * satisfied? Bail out!
  426. */
  427. if (nimaps == 0)
  428. return -ENOSPC;
  429. convert:
  430. return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
  431. out_unreserve:
  432. xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
  433. XFS_QMOPT_RES_REGBLKS);
  434. out_trans_cancel:
  435. xfs_trans_cancel(tp);
  436. return error;
  437. }
  438. /*
  439. * Cancel CoW reservations for some block range of an inode.
  440. *
  441. * If cancel_real is true this function cancels all COW fork extents for the
  442. * inode; if cancel_real is false, real extents are not cleared.
  443. *
  444. * Caller must have already joined the inode to the current transaction. The
  445. * inode will be joined to the transaction returned to the caller.
  446. */
  447. int
  448. xfs_reflink_cancel_cow_blocks(
  449. struct xfs_inode *ip,
  450. struct xfs_trans **tpp,
  451. xfs_fileoff_t offset_fsb,
  452. xfs_fileoff_t end_fsb,
  453. bool cancel_real)
  454. {
  455. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  456. struct xfs_bmbt_irec got, del;
  457. struct xfs_iext_cursor icur;
  458. int error = 0;
  459. if (!xfs_inode_has_cow_data(ip))
  460. return 0;
  461. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  462. return 0;
  463. /* Walk backwards until we're out of the I/O range... */
  464. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  465. del = got;
  466. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  467. /* Extent delete may have bumped ext forward */
  468. if (!del.br_blockcount) {
  469. xfs_iext_prev(ifp, &icur);
  470. goto next_extent;
  471. }
  472. trace_xfs_reflink_cancel_cow(ip, &del);
  473. if (isnullstartblock(del.br_startblock)) {
  474. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  475. &icur, &got, &del);
  476. if (error)
  477. break;
  478. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  479. ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
  480. /* Free the CoW orphan record. */
  481. error = xfs_refcount_free_cow_extent(*tpp,
  482. del.br_startblock, del.br_blockcount);
  483. if (error)
  484. break;
  485. xfs_bmap_add_free(*tpp, del.br_startblock,
  486. del.br_blockcount, NULL);
  487. /* Roll the transaction */
  488. error = xfs_defer_finish(tpp);
  489. if (error)
  490. break;
  491. /* Remove the mapping from the CoW fork. */
  492. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  493. /* Remove the quota reservation */
  494. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  495. -(long)del.br_blockcount, 0,
  496. XFS_QMOPT_RES_REGBLKS);
  497. if (error)
  498. break;
  499. } else {
  500. /* Didn't do anything, push cursor back. */
  501. xfs_iext_prev(ifp, &icur);
  502. }
  503. next_extent:
  504. if (!xfs_iext_get_extent(ifp, &icur, &got))
  505. break;
  506. }
  507. /* clear tag if cow fork is emptied */
  508. if (!ifp->if_bytes)
  509. xfs_inode_clear_cowblocks_tag(ip);
  510. return error;
  511. }
  512. /*
  513. * Cancel CoW reservations for some byte range of an inode.
  514. *
  515. * If cancel_real is true this function cancels all COW fork extents for the
  516. * inode; if cancel_real is false, real extents are not cleared.
  517. */
  518. int
  519. xfs_reflink_cancel_cow_range(
  520. struct xfs_inode *ip,
  521. xfs_off_t offset,
  522. xfs_off_t count,
  523. bool cancel_real)
  524. {
  525. struct xfs_trans *tp;
  526. xfs_fileoff_t offset_fsb;
  527. xfs_fileoff_t end_fsb;
  528. int error;
  529. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  530. ASSERT(xfs_is_reflink_inode(ip));
  531. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  532. if (count == NULLFILEOFF)
  533. end_fsb = NULLFILEOFF;
  534. else
  535. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  536. /* Start a rolling transaction to remove the mappings */
  537. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  538. 0, 0, XFS_TRANS_NOFS, &tp);
  539. if (error)
  540. goto out;
  541. xfs_ilock(ip, XFS_ILOCK_EXCL);
  542. xfs_trans_ijoin(tp, ip, 0);
  543. /* Scrape out the old CoW reservations */
  544. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  545. cancel_real);
  546. if (error)
  547. goto out_cancel;
  548. error = xfs_trans_commit(tp);
  549. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  550. return error;
  551. out_cancel:
  552. xfs_trans_cancel(tp);
  553. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  554. out:
  555. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  556. return error;
  557. }
  558. /*
  559. * Remap parts of a file's data fork after a successful CoW.
  560. */
  561. int
  562. xfs_reflink_end_cow(
  563. struct xfs_inode *ip,
  564. xfs_off_t offset,
  565. xfs_off_t count)
  566. {
  567. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  568. struct xfs_bmbt_irec got, del;
  569. struct xfs_trans *tp;
  570. xfs_fileoff_t offset_fsb;
  571. xfs_fileoff_t end_fsb;
  572. int error;
  573. unsigned int resblks;
  574. xfs_filblks_t rlen;
  575. struct xfs_iext_cursor icur;
  576. trace_xfs_reflink_end_cow(ip, offset, count);
  577. /* No COW extents? That's easy! */
  578. if (ifp->if_bytes == 0)
  579. return 0;
  580. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  581. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  582. /*
  583. * Start a rolling transaction to switch the mappings. We're
  584. * unlikely ever to have to remap 16T worth of single-block
  585. * extents, so just cap the worst case extent count to 2^32-1.
  586. * Stick a warning in just in case, and avoid 64-bit division.
  587. */
  588. BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
  589. if (end_fsb - offset_fsb > UINT_MAX) {
  590. error = -EFSCORRUPTED;
  591. xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
  592. ASSERT(0);
  593. goto out;
  594. }
  595. resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
  596. (unsigned int)(end_fsb - offset_fsb),
  597. XFS_DATA_FORK);
  598. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  599. resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  600. if (error)
  601. goto out;
  602. xfs_ilock(ip, XFS_ILOCK_EXCL);
  603. xfs_trans_ijoin(tp, ip, 0);
  604. /*
  605. * In case of racing, overlapping AIO writes no COW extents might be
  606. * left by the time I/O completes for the loser of the race. In that
  607. * case we are done.
  608. */
  609. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  610. goto out_cancel;
  611. /* Walk backwards until we're out of the I/O range... */
  612. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  613. del = got;
  614. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  615. /* Extent delete may have bumped ext forward */
  616. if (!del.br_blockcount)
  617. goto prev_extent;
  618. /*
  619. * Only remap real extent that contain data. With AIO
  620. * speculatively preallocations can leak into the range we
  621. * are called upon, and we need to skip them.
  622. */
  623. if (!xfs_bmap_is_real_extent(&got))
  624. goto prev_extent;
  625. /* Unmap the old blocks in the data fork. */
  626. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  627. rlen = del.br_blockcount;
  628. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
  629. if (error)
  630. goto out_cancel;
  631. /* Trim the extent to whatever got unmapped. */
  632. if (rlen) {
  633. xfs_trim_extent(&del, del.br_startoff + rlen,
  634. del.br_blockcount - rlen);
  635. }
  636. trace_xfs_reflink_cow_remap(ip, &del);
  637. /* Free the CoW orphan record. */
  638. error = xfs_refcount_free_cow_extent(tp, del.br_startblock,
  639. del.br_blockcount);
  640. if (error)
  641. goto out_cancel;
  642. /* Map the new blocks into the data fork. */
  643. error = xfs_bmap_map_extent(tp, ip, &del);
  644. if (error)
  645. goto out_cancel;
  646. /* Charge this new data fork mapping to the on-disk quota. */
  647. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
  648. (long)del.br_blockcount);
  649. /* Remove the mapping from the CoW fork. */
  650. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  651. error = xfs_defer_finish(&tp);
  652. if (error)
  653. goto out_cancel;
  654. if (!xfs_iext_get_extent(ifp, &icur, &got))
  655. break;
  656. continue;
  657. prev_extent:
  658. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  659. break;
  660. }
  661. error = xfs_trans_commit(tp);
  662. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  663. if (error)
  664. goto out;
  665. return 0;
  666. out_cancel:
  667. xfs_trans_cancel(tp);
  668. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  669. out:
  670. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  671. return error;
  672. }
  673. /*
  674. * Free leftover CoW reservations that didn't get cleaned out.
  675. */
  676. int
  677. xfs_reflink_recover_cow(
  678. struct xfs_mount *mp)
  679. {
  680. xfs_agnumber_t agno;
  681. int error = 0;
  682. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  683. return 0;
  684. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  685. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  686. if (error)
  687. break;
  688. }
  689. return error;
  690. }
  691. /*
  692. * Reflinking (Block) Ranges of Two Files Together
  693. *
  694. * First, ensure that the reflink flag is set on both inodes. The flag is an
  695. * optimization to avoid unnecessary refcount btree lookups in the write path.
  696. *
  697. * Now we can iteratively remap the range of extents (and holes) in src to the
  698. * corresponding ranges in dest. Let drange and srange denote the ranges of
  699. * logical blocks in dest and src touched by the reflink operation.
  700. *
  701. * While the length of drange is greater than zero,
  702. * - Read src's bmbt at the start of srange ("imap")
  703. * - If imap doesn't exist, make imap appear to start at the end of srange
  704. * with zero length.
  705. * - If imap starts before srange, advance imap to start at srange.
  706. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  707. * - Punch (imap start - srange start + imap len) blocks from dest at
  708. * offset (drange start).
  709. * - If imap points to a real range of pblks,
  710. * > Increase the refcount of the imap's pblks
  711. * > Map imap's pblks into dest at the offset
  712. * (drange start + imap start - srange start)
  713. * - Advance drange and srange by (imap start - srange start + imap len)
  714. *
  715. * Finally, if the reflink made dest longer, update both the in-core and
  716. * on-disk file sizes.
  717. *
  718. * ASCII Art Demonstration:
  719. *
  720. * Let's say we want to reflink this source file:
  721. *
  722. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  723. * <-------------------->
  724. *
  725. * into this destination file:
  726. *
  727. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  728. * <-------------------->
  729. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  730. * Observe that the range has different logical offsets in either file.
  731. *
  732. * Consider that the first extent in the source file doesn't line up with our
  733. * reflink range. Unmapping and remapping are separate operations, so we can
  734. * unmap more blocks from the destination file than we remap.
  735. *
  736. * ----SSSSSSS-SSSSS----SSSSSS
  737. * <------->
  738. * --DDDDD---------DDDDD--DDD
  739. * <------->
  740. *
  741. * Now remap the source extent into the destination file:
  742. *
  743. * ----SSSSSSS-SSSSS----SSSSSS
  744. * <------->
  745. * --DDDDD--SSSSSSSDDDDD--DDD
  746. * <------->
  747. *
  748. * Do likewise with the second hole and extent in our range. Holes in the
  749. * unmap range don't affect our operation.
  750. *
  751. * ----SSSSSSS-SSSSS----SSSSSS
  752. * <---->
  753. * --DDDDD--SSSSSSS-SSSSS-DDD
  754. * <---->
  755. *
  756. * Finally, unmap and remap part of the third extent. This will increase the
  757. * size of the destination file.
  758. *
  759. * ----SSSSSSS-SSSSS----SSSSSS
  760. * <----->
  761. * --DDDDD--SSSSSSS-SSSSS----SSS
  762. * <----->
  763. *
  764. * Once we update the destination file's i_size, we're done.
  765. */
  766. /*
  767. * Ensure the reflink bit is set in both inodes.
  768. */
  769. STATIC int
  770. xfs_reflink_set_inode_flag(
  771. struct xfs_inode *src,
  772. struct xfs_inode *dest)
  773. {
  774. struct xfs_mount *mp = src->i_mount;
  775. int error;
  776. struct xfs_trans *tp;
  777. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  778. return 0;
  779. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  780. if (error)
  781. goto out_error;
  782. /* Lock both files against IO */
  783. if (src->i_ino == dest->i_ino)
  784. xfs_ilock(src, XFS_ILOCK_EXCL);
  785. else
  786. xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
  787. if (!xfs_is_reflink_inode(src)) {
  788. trace_xfs_reflink_set_inode_flag(src);
  789. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  790. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  791. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  792. xfs_ifork_init_cow(src);
  793. } else
  794. xfs_iunlock(src, XFS_ILOCK_EXCL);
  795. if (src->i_ino == dest->i_ino)
  796. goto commit_flags;
  797. if (!xfs_is_reflink_inode(dest)) {
  798. trace_xfs_reflink_set_inode_flag(dest);
  799. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  800. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  801. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  802. xfs_ifork_init_cow(dest);
  803. } else
  804. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  805. commit_flags:
  806. error = xfs_trans_commit(tp);
  807. if (error)
  808. goto out_error;
  809. return error;
  810. out_error:
  811. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  812. return error;
  813. }
  814. /*
  815. * Update destination inode size & cowextsize hint, if necessary.
  816. */
  817. STATIC int
  818. xfs_reflink_update_dest(
  819. struct xfs_inode *dest,
  820. xfs_off_t newlen,
  821. xfs_extlen_t cowextsize,
  822. unsigned int remap_flags)
  823. {
  824. struct xfs_mount *mp = dest->i_mount;
  825. struct xfs_trans *tp;
  826. int error;
  827. if ((remap_flags & REMAP_FILE_DEDUP) &&
  828. 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 (!(remap_flags & REMAP_FILE_DEDUP)) {
  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. int error;
  900. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  901. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  902. /* No reflinking if we're low on space */
  903. if (real_extent) {
  904. error = xfs_reflink_ag_has_free_space(mp,
  905. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  906. if (error)
  907. goto out;
  908. }
  909. /* Start a rolling transaction to switch the mappings */
  910. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  911. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  912. if (error)
  913. goto out;
  914. xfs_ilock(ip, XFS_ILOCK_EXCL);
  915. xfs_trans_ijoin(tp, ip, 0);
  916. /* If we're not just clearing space, then do we have enough quota? */
  917. if (real_extent) {
  918. error = xfs_trans_reserve_quota_nblks(tp, ip,
  919. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  920. if (error)
  921. goto out_cancel;
  922. }
  923. trace_xfs_reflink_remap(ip, irec->br_startoff,
  924. irec->br_blockcount, irec->br_startblock);
  925. /* Unmap the old blocks in the data fork. */
  926. rlen = unmap_len;
  927. while (rlen) {
  928. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  929. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
  930. if (error)
  931. goto out_cancel;
  932. /*
  933. * Trim the extent to whatever got unmapped.
  934. * Remember, bunmapi works backwards.
  935. */
  936. uirec.br_startblock = irec->br_startblock + rlen;
  937. uirec.br_startoff = irec->br_startoff + rlen;
  938. uirec.br_blockcount = unmap_len - rlen;
  939. unmap_len = rlen;
  940. /* If this isn't a real mapping, we're done. */
  941. if (!real_extent || uirec.br_blockcount == 0)
  942. goto next_extent;
  943. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  944. uirec.br_blockcount, uirec.br_startblock);
  945. /* Update the refcount tree */
  946. error = xfs_refcount_increase_extent(tp, &uirec);
  947. if (error)
  948. goto out_cancel;
  949. /* Map the new blocks into the data fork. */
  950. error = xfs_bmap_map_extent(tp, ip, &uirec);
  951. if (error)
  952. goto out_cancel;
  953. /* Update quota accounting. */
  954. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  955. uirec.br_blockcount);
  956. /* Update dest isize if needed. */
  957. newlen = XFS_FSB_TO_B(mp,
  958. uirec.br_startoff + uirec.br_blockcount);
  959. newlen = min_t(xfs_off_t, newlen, new_isize);
  960. if (newlen > i_size_read(VFS_I(ip))) {
  961. trace_xfs_reflink_update_inode_size(ip, newlen);
  962. i_size_write(VFS_I(ip), newlen);
  963. ip->i_d.di_size = newlen;
  964. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  965. }
  966. next_extent:
  967. /* Process all the deferred stuff. */
  968. error = xfs_defer_finish(&tp);
  969. if (error)
  970. goto out_cancel;
  971. }
  972. error = xfs_trans_commit(tp);
  973. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  974. if (error)
  975. goto out;
  976. return 0;
  977. out_cancel:
  978. xfs_trans_cancel(tp);
  979. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  980. out:
  981. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  982. return error;
  983. }
  984. /*
  985. * Iteratively remap one file's extents (and holes) to another's.
  986. */
  987. STATIC int
  988. xfs_reflink_remap_blocks(
  989. struct xfs_inode *src,
  990. xfs_fileoff_t srcoff,
  991. struct xfs_inode *dest,
  992. xfs_fileoff_t destoff,
  993. xfs_filblks_t len,
  994. xfs_off_t new_isize)
  995. {
  996. struct xfs_bmbt_irec imap;
  997. int nimaps;
  998. int error = 0;
  999. xfs_filblks_t range_len;
  1000. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1001. while (len) {
  1002. uint lock_mode;
  1003. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1004. dest, destoff);
  1005. /* Read extent from the source file */
  1006. nimaps = 1;
  1007. lock_mode = xfs_ilock_data_map_shared(src);
  1008. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1009. xfs_iunlock(src, lock_mode);
  1010. if (error)
  1011. goto err;
  1012. ASSERT(nimaps == 1);
  1013. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1014. &imap);
  1015. /* Translate imap into the destination file. */
  1016. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1017. imap.br_startoff += destoff - srcoff;
  1018. /* Clear dest from destoff to the end of imap and map it in. */
  1019. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1020. new_isize);
  1021. if (error)
  1022. goto err;
  1023. if (fatal_signal_pending(current)) {
  1024. error = -EINTR;
  1025. goto err;
  1026. }
  1027. /* Advance drange/srange */
  1028. srcoff += range_len;
  1029. destoff += range_len;
  1030. len -= range_len;
  1031. }
  1032. return 0;
  1033. err:
  1034. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1035. return error;
  1036. }
  1037. /*
  1038. * Grab the exclusive iolock for a data copy from src to dest, making
  1039. * sure to abide vfs locking order (lowest pointer value goes first) and
  1040. * breaking the pnfs layout leases on dest before proceeding. The loop
  1041. * is needed because we cannot call the blocking break_layout() with the
  1042. * src iolock held, and therefore have to back out both locks.
  1043. */
  1044. static int
  1045. xfs_iolock_two_inodes_and_break_layout(
  1046. struct inode *src,
  1047. struct inode *dest)
  1048. {
  1049. int error;
  1050. retry:
  1051. if (src < dest) {
  1052. inode_lock_shared(src);
  1053. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  1054. } else {
  1055. /* src >= dest */
  1056. inode_lock(dest);
  1057. }
  1058. error = break_layout(dest, false);
  1059. if (error == -EWOULDBLOCK) {
  1060. inode_unlock(dest);
  1061. if (src < dest)
  1062. inode_unlock_shared(src);
  1063. error = break_layout(dest, true);
  1064. if (error)
  1065. return error;
  1066. goto retry;
  1067. }
  1068. if (error) {
  1069. inode_unlock(dest);
  1070. if (src < dest)
  1071. inode_unlock_shared(src);
  1072. return error;
  1073. }
  1074. if (src > dest)
  1075. inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
  1076. return 0;
  1077. }
  1078. /* Unlock both inodes after they've been prepped for a range clone. */
  1079. STATIC void
  1080. xfs_reflink_remap_unlock(
  1081. struct file *file_in,
  1082. struct file *file_out)
  1083. {
  1084. struct inode *inode_in = file_inode(file_in);
  1085. struct xfs_inode *src = XFS_I(inode_in);
  1086. struct inode *inode_out = file_inode(file_out);
  1087. struct xfs_inode *dest = XFS_I(inode_out);
  1088. bool same_inode = (inode_in == inode_out);
  1089. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1090. if (!same_inode)
  1091. xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
  1092. inode_unlock(inode_out);
  1093. if (!same_inode)
  1094. inode_unlock_shared(inode_in);
  1095. }
  1096. /*
  1097. * If we're reflinking to a point past the destination file's EOF, we must
  1098. * zero any speculative post-EOF preallocations that sit between the old EOF
  1099. * and the destination file offset.
  1100. */
  1101. static int
  1102. xfs_reflink_zero_posteof(
  1103. struct xfs_inode *ip,
  1104. loff_t pos)
  1105. {
  1106. loff_t isize = i_size_read(VFS_I(ip));
  1107. if (pos <= isize)
  1108. return 0;
  1109. trace_xfs_zero_eof(ip, isize, pos - isize);
  1110. return iomap_zero_range(VFS_I(ip), isize, pos - isize, NULL,
  1111. &xfs_iomap_ops);
  1112. }
  1113. /*
  1114. * Prepare two files for range cloning. Upon a successful return both inodes
  1115. * will have the iolock and mmaplock held, the page cache of the out file will
  1116. * be truncated, and any leases on the out file will have been broken. This
  1117. * function borrows heavily from xfs_file_aio_write_checks.
  1118. *
  1119. * The VFS allows partial EOF blocks to "match" for dedupe even though it hasn't
  1120. * checked that the bytes beyond EOF physically match. Hence we cannot use the
  1121. * EOF block in the source dedupe range because it's not a complete block match,
  1122. * hence can introduce a corruption into the file that has it's block replaced.
  1123. *
  1124. * In similar fashion, the VFS file cloning also allows partial EOF blocks to be
  1125. * "block aligned" for the purposes of cloning entire files. However, if the
  1126. * source file range includes the EOF block and it lands within the existing EOF
  1127. * of the destination file, then we can expose stale data from beyond the source
  1128. * file EOF in the destination file.
  1129. *
  1130. * XFS doesn't support partial block sharing, so in both cases we have check
  1131. * these cases ourselves. For dedupe, we can simply round the length to dedupe
  1132. * down to the previous whole block and ignore the partial EOF block. While this
  1133. * means we can't dedupe the last block of a file, this is an acceptible
  1134. * tradeoff for simplicity on implementation.
  1135. *
  1136. * For cloning, we want to share the partial EOF block if it is also the new EOF
  1137. * block of the destination file. If the partial EOF block lies inside the
  1138. * existing destination EOF, then we have to abort the clone to avoid exposing
  1139. * stale data in the destination file. Hence we reject these clone attempts with
  1140. * -EINVAL in this case.
  1141. */
  1142. STATIC int
  1143. xfs_reflink_remap_prep(
  1144. struct file *file_in,
  1145. loff_t pos_in,
  1146. struct file *file_out,
  1147. loff_t pos_out,
  1148. u64 *len,
  1149. unsigned int remap_flags)
  1150. {
  1151. struct inode *inode_in = file_inode(file_in);
  1152. struct xfs_inode *src = XFS_I(inode_in);
  1153. struct inode *inode_out = file_inode(file_out);
  1154. struct xfs_inode *dest = XFS_I(inode_out);
  1155. bool same_inode = (inode_in == inode_out);
  1156. u64 blkmask = i_blocksize(inode_in) - 1;
  1157. ssize_t ret;
  1158. /* Lock both files against IO */
  1159. ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
  1160. if (ret)
  1161. return ret;
  1162. if (same_inode)
  1163. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1164. else
  1165. xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
  1166. XFS_MMAPLOCK_EXCL);
  1167. /* Check file eligibility and prepare for block sharing. */
  1168. ret = -EINVAL;
  1169. /* Don't reflink realtime inodes */
  1170. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1171. goto out_unlock;
  1172. /* Don't share DAX file data for now. */
  1173. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1174. goto out_unlock;
  1175. ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
  1176. len, remap_flags);
  1177. if (ret <= 0)
  1178. goto out_unlock;
  1179. /*
  1180. * If the dedupe data matches, chop off the partial EOF block
  1181. * from the source file so we don't try to dedupe the partial
  1182. * EOF block.
  1183. */
  1184. if (remap_flags & REMAP_FILE_DEDUP) {
  1185. *len &= ~blkmask;
  1186. } else if (*len & blkmask) {
  1187. /*
  1188. * The user is attempting to share a partial EOF block,
  1189. * if it's inside the destination EOF then reject it.
  1190. */
  1191. if (pos_out + *len < i_size_read(inode_out)) {
  1192. ret = -EINVAL;
  1193. goto out_unlock;
  1194. }
  1195. }
  1196. /* Attach dquots to dest inode before changing block map */
  1197. ret = xfs_qm_dqattach(dest);
  1198. if (ret)
  1199. goto out_unlock;
  1200. /*
  1201. * Zero existing post-eof speculative preallocations in the destination
  1202. * file.
  1203. */
  1204. ret = xfs_reflink_zero_posteof(dest, pos_out);
  1205. if (ret)
  1206. goto out_unlock;
  1207. /* Set flags and remap blocks. */
  1208. ret = xfs_reflink_set_inode_flag(src, dest);
  1209. if (ret)
  1210. goto out_unlock;
  1211. /* Zap any page cache for the destination file's range. */
  1212. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1213. PAGE_ALIGN(pos_out + *len) - 1);
  1214. /* If we're altering the file contents... */
  1215. if (!(remap_flags & REMAP_FILE_DEDUP)) {
  1216. /*
  1217. * ...update the timestamps (which will grab the ilock again
  1218. * from xfs_fs_dirty_inode, so we have to call it before we
  1219. * take the ilock).
  1220. */
  1221. if (!(file_out->f_mode & FMODE_NOCMTIME)) {
  1222. ret = file_update_time(file_out);
  1223. if (ret)
  1224. goto out_unlock;
  1225. }
  1226. /*
  1227. * ...clear the security bits if the process is not being run
  1228. * by root. This keeps people from modifying setuid and setgid
  1229. * binaries.
  1230. */
  1231. ret = file_remove_privs(file_out);
  1232. if (ret)
  1233. goto out_unlock;
  1234. }
  1235. return 1;
  1236. out_unlock:
  1237. xfs_reflink_remap_unlock(file_in, file_out);
  1238. return ret;
  1239. }
  1240. /*
  1241. * Link a range of blocks from one file to another.
  1242. */
  1243. int
  1244. xfs_reflink_remap_range(
  1245. struct file *file_in,
  1246. loff_t pos_in,
  1247. struct file *file_out,
  1248. loff_t pos_out,
  1249. u64 len,
  1250. unsigned int remap_flags)
  1251. {
  1252. struct inode *inode_in = file_inode(file_in);
  1253. struct xfs_inode *src = XFS_I(inode_in);
  1254. struct inode *inode_out = file_inode(file_out);
  1255. struct xfs_inode *dest = XFS_I(inode_out);
  1256. struct xfs_mount *mp = src->i_mount;
  1257. xfs_fileoff_t sfsbno, dfsbno;
  1258. xfs_filblks_t fsblen;
  1259. xfs_extlen_t cowextsize;
  1260. ssize_t ret;
  1261. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1262. return -EOPNOTSUPP;
  1263. if (XFS_FORCED_SHUTDOWN(mp))
  1264. return -EIO;
  1265. /* Prepare and then clone file data. */
  1266. ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
  1267. &len, remap_flags);
  1268. if (ret <= 0)
  1269. return ret;
  1270. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1271. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1272. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1273. fsblen = XFS_B_TO_FSB(mp, len);
  1274. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1275. pos_out + len);
  1276. if (ret)
  1277. goto out_unlock;
  1278. /*
  1279. * Carry the cowextsize hint from src to dest if we're sharing the
  1280. * entire source file to the entire destination file, the source file
  1281. * has a cowextsize hint, and the destination file does not.
  1282. */
  1283. cowextsize = 0;
  1284. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1285. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1286. pos_out == 0 && len >= i_size_read(inode_out) &&
  1287. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1288. cowextsize = src->i_d.di_cowextsize;
  1289. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1290. remap_flags);
  1291. out_unlock:
  1292. xfs_reflink_remap_unlock(file_in, file_out);
  1293. if (ret)
  1294. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1295. return ret;
  1296. }
  1297. /*
  1298. * The user wants to preemptively CoW all shared blocks in this file,
  1299. * which enables us to turn off the reflink flag. Iterate all
  1300. * extents which are not prealloc/delalloc to see which ranges are
  1301. * mentioned in the refcount tree, then read those blocks into the
  1302. * pagecache, dirty them, fsync them back out, and then we can update
  1303. * the inode flag. What happens if we run out of memory? :)
  1304. */
  1305. STATIC int
  1306. xfs_reflink_dirty_extents(
  1307. struct xfs_inode *ip,
  1308. xfs_fileoff_t fbno,
  1309. xfs_filblks_t end,
  1310. xfs_off_t isize)
  1311. {
  1312. struct xfs_mount *mp = ip->i_mount;
  1313. xfs_agnumber_t agno;
  1314. xfs_agblock_t agbno;
  1315. xfs_extlen_t aglen;
  1316. xfs_agblock_t rbno;
  1317. xfs_extlen_t rlen;
  1318. xfs_off_t fpos;
  1319. xfs_off_t flen;
  1320. struct xfs_bmbt_irec map[2];
  1321. int nmaps;
  1322. int error = 0;
  1323. while (end - fbno > 0) {
  1324. nmaps = 1;
  1325. /*
  1326. * Look for extents in the file. Skip holes, delalloc, or
  1327. * unwritten extents; they can't be reflinked.
  1328. */
  1329. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1330. if (error)
  1331. goto out;
  1332. if (nmaps == 0)
  1333. break;
  1334. if (!xfs_bmap_is_real_extent(&map[0]))
  1335. goto next;
  1336. map[1] = map[0];
  1337. while (map[1].br_blockcount) {
  1338. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1339. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1340. aglen = map[1].br_blockcount;
  1341. error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
  1342. aglen, &rbno, &rlen, true);
  1343. if (error)
  1344. goto out;
  1345. if (rbno == NULLAGBLOCK)
  1346. break;
  1347. /* Dirty the pages */
  1348. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1349. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1350. (rbno - agbno));
  1351. flen = XFS_FSB_TO_B(mp, rlen);
  1352. if (fpos + flen > isize)
  1353. flen = isize - fpos;
  1354. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1355. &xfs_iomap_ops);
  1356. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1357. if (error)
  1358. goto out;
  1359. map[1].br_blockcount -= (rbno - agbno + rlen);
  1360. map[1].br_startoff += (rbno - agbno + rlen);
  1361. map[1].br_startblock += (rbno - agbno + rlen);
  1362. }
  1363. next:
  1364. fbno = map[0].br_startoff + map[0].br_blockcount;
  1365. }
  1366. out:
  1367. return error;
  1368. }
  1369. /* Does this inode need the reflink flag? */
  1370. int
  1371. xfs_reflink_inode_has_shared_extents(
  1372. struct xfs_trans *tp,
  1373. struct xfs_inode *ip,
  1374. bool *has_shared)
  1375. {
  1376. struct xfs_bmbt_irec got;
  1377. struct xfs_mount *mp = ip->i_mount;
  1378. struct xfs_ifork *ifp;
  1379. xfs_agnumber_t agno;
  1380. xfs_agblock_t agbno;
  1381. xfs_extlen_t aglen;
  1382. xfs_agblock_t rbno;
  1383. xfs_extlen_t rlen;
  1384. struct xfs_iext_cursor icur;
  1385. bool found;
  1386. int error;
  1387. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1388. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1389. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1390. if (error)
  1391. return error;
  1392. }
  1393. *has_shared = false;
  1394. found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
  1395. while (found) {
  1396. if (isnullstartblock(got.br_startblock) ||
  1397. got.br_state != XFS_EXT_NORM)
  1398. goto next;
  1399. agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
  1400. agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
  1401. aglen = got.br_blockcount;
  1402. error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
  1403. &rbno, &rlen, false);
  1404. if (error)
  1405. return error;
  1406. /* Is there still a shared block here? */
  1407. if (rbno != NULLAGBLOCK) {
  1408. *has_shared = true;
  1409. return 0;
  1410. }
  1411. next:
  1412. found = xfs_iext_next_extent(ifp, &icur, &got);
  1413. }
  1414. return 0;
  1415. }
  1416. /*
  1417. * Clear the inode reflink flag if there are no shared extents.
  1418. *
  1419. * The caller is responsible for joining the inode to the transaction passed in.
  1420. * The inode will be joined to the transaction that is returned to the caller.
  1421. */
  1422. int
  1423. xfs_reflink_clear_inode_flag(
  1424. struct xfs_inode *ip,
  1425. struct xfs_trans **tpp)
  1426. {
  1427. bool needs_flag;
  1428. int error = 0;
  1429. ASSERT(xfs_is_reflink_inode(ip));
  1430. error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
  1431. if (error || needs_flag)
  1432. return error;
  1433. /*
  1434. * We didn't find any shared blocks so turn off the reflink flag.
  1435. * First, get rid of any leftover CoW mappings.
  1436. */
  1437. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1438. if (error)
  1439. return error;
  1440. /* Clear the inode flag. */
  1441. trace_xfs_reflink_unset_inode_flag(ip);
  1442. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1443. xfs_inode_clear_cowblocks_tag(ip);
  1444. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1445. return error;
  1446. }
  1447. /*
  1448. * Clear the inode reflink flag if there are no shared extents and the size
  1449. * hasn't changed.
  1450. */
  1451. STATIC int
  1452. xfs_reflink_try_clear_inode_flag(
  1453. struct xfs_inode *ip)
  1454. {
  1455. struct xfs_mount *mp = ip->i_mount;
  1456. struct xfs_trans *tp;
  1457. int error = 0;
  1458. /* Start a rolling transaction to remove the mappings */
  1459. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1460. if (error)
  1461. return error;
  1462. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1463. xfs_trans_ijoin(tp, ip, 0);
  1464. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1465. if (error)
  1466. goto cancel;
  1467. error = xfs_trans_commit(tp);
  1468. if (error)
  1469. goto out;
  1470. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1471. return 0;
  1472. cancel:
  1473. xfs_trans_cancel(tp);
  1474. out:
  1475. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1476. return error;
  1477. }
  1478. /*
  1479. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1480. * the reflink flag if we unshare all of the file's blocks.
  1481. */
  1482. int
  1483. xfs_reflink_unshare(
  1484. struct xfs_inode *ip,
  1485. xfs_off_t offset,
  1486. xfs_off_t len)
  1487. {
  1488. struct xfs_mount *mp = ip->i_mount;
  1489. xfs_fileoff_t fbno;
  1490. xfs_filblks_t end;
  1491. xfs_off_t isize;
  1492. int error;
  1493. if (!xfs_is_reflink_inode(ip))
  1494. return 0;
  1495. trace_xfs_reflink_unshare(ip, offset, len);
  1496. inode_dio_wait(VFS_I(ip));
  1497. /* Try to CoW the selected ranges */
  1498. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1499. fbno = XFS_B_TO_FSBT(mp, offset);
  1500. isize = i_size_read(VFS_I(ip));
  1501. end = XFS_B_TO_FSB(mp, offset + len);
  1502. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1503. if (error)
  1504. goto out_unlock;
  1505. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1506. /* Wait for the IO to finish */
  1507. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1508. if (error)
  1509. goto out;
  1510. /* Turn off the reflink flag if possible. */
  1511. error = xfs_reflink_try_clear_inode_flag(ip);
  1512. if (error)
  1513. goto out;
  1514. return 0;
  1515. out_unlock:
  1516. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1517. out:
  1518. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1519. return error;
  1520. }