xfs_reflink.c 43 KB

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