xfs_reflink.c 43 KB

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