xfs_reflink.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. xfs_agnumber_t agno,
  157. xfs_agblock_t agbno,
  158. xfs_extlen_t aglen,
  159. xfs_agblock_t *fbno,
  160. xfs_extlen_t *flen,
  161. bool find_end_of_shared)
  162. {
  163. struct xfs_buf *agbp;
  164. struct xfs_btree_cur *cur;
  165. int error;
  166. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  167. if (error)
  168. return error;
  169. cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
  170. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  171. find_end_of_shared);
  172. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  173. xfs_buf_relse(agbp);
  174. return error;
  175. }
  176. /*
  177. * Trim the mapping to the next block where there's a change in the
  178. * shared/unshared status. More specifically, this means that we
  179. * find the lowest-numbered extent of shared blocks that coincides with
  180. * the given block mapping. If the shared extent overlaps the start of
  181. * the mapping, trim the mapping to the end of the shared extent. If
  182. * the shared region intersects the mapping, trim the mapping to the
  183. * start of the shared extent. If there are no shared regions that
  184. * overlap, just return the original extent.
  185. */
  186. int
  187. xfs_reflink_trim_around_shared(
  188. struct xfs_inode *ip,
  189. struct xfs_bmbt_irec *irec,
  190. bool *shared,
  191. bool *trimmed)
  192. {
  193. xfs_agnumber_t agno;
  194. xfs_agblock_t agbno;
  195. xfs_extlen_t aglen;
  196. xfs_agblock_t fbno;
  197. xfs_extlen_t flen;
  198. int error = 0;
  199. /* Holes, unwritten, and delalloc extents cannot be shared */
  200. if (!xfs_is_reflink_inode(ip) ||
  201. ISUNWRITTEN(irec) ||
  202. irec->br_startblock == HOLESTARTBLOCK ||
  203. irec->br_startblock == DELAYSTARTBLOCK ||
  204. isnullstartblock(irec->br_startblock)) {
  205. *shared = false;
  206. return 0;
  207. }
  208. trace_xfs_reflink_trim_around_shared(ip, irec);
  209. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  210. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  211. aglen = irec->br_blockcount;
  212. error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
  213. aglen, &fbno, &flen, true);
  214. if (error)
  215. return error;
  216. *shared = *trimmed = false;
  217. if (fbno == NULLAGBLOCK) {
  218. /* No shared blocks at all. */
  219. return 0;
  220. } else if (fbno == agbno) {
  221. /*
  222. * The start of this extent is shared. Truncate the
  223. * mapping at the end of the shared region so that a
  224. * subsequent iteration starts at the start of the
  225. * unshared region.
  226. */
  227. irec->br_blockcount = flen;
  228. *shared = true;
  229. if (flen != aglen)
  230. *trimmed = true;
  231. return 0;
  232. } else {
  233. /*
  234. * There's a shared extent midway through this extent.
  235. * Truncate the mapping at the start of the shared
  236. * extent so that a subsequent iteration starts at the
  237. * start of the shared region.
  238. */
  239. irec->br_blockcount = fbno - agbno;
  240. *trimmed = true;
  241. return 0;
  242. }
  243. }
  244. /*
  245. * Trim the passed in imap to the next shared/unshared extent boundary, and
  246. * if imap->br_startoff points to a shared extent reserve space for it in the
  247. * COW fork. In this case *shared is set to true, else to false.
  248. *
  249. * Note that imap will always contain the block numbers for the existing blocks
  250. * in the data fork, as the upper layers need them for read-modify-write
  251. * operations.
  252. */
  253. int
  254. xfs_reflink_reserve_cow(
  255. struct xfs_inode *ip,
  256. struct xfs_bmbt_irec *imap,
  257. bool *shared)
  258. {
  259. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  260. struct xfs_bmbt_irec got;
  261. int error = 0;
  262. bool eof = false, trimmed;
  263. xfs_extnum_t idx;
  264. /*
  265. * Search the COW fork extent list first. This serves two purposes:
  266. * first this implement the speculative preallocation using cowextisze,
  267. * so that we also unshared block adjacent to shared blocks instead
  268. * of just the shared blocks themselves. Second the lookup in the
  269. * extent list is generally faster than going out to the shared extent
  270. * tree.
  271. */
  272. if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
  273. eof = true;
  274. if (!eof && got.br_startoff <= imap->br_startoff) {
  275. trace_xfs_reflink_cow_found(ip, imap);
  276. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  277. *shared = true;
  278. return 0;
  279. }
  280. /* Trim the mapping to the nearest shared extent boundary. */
  281. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  282. if (error)
  283. return error;
  284. /* Not shared? Just report the (potentially capped) extent. */
  285. if (!*shared)
  286. return 0;
  287. /*
  288. * Fork all the shared blocks from our write offset until the end of
  289. * the extent.
  290. */
  291. error = xfs_qm_dqattach_locked(ip, 0);
  292. if (error)
  293. return error;
  294. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
  295. imap->br_blockcount, 0, &got, &idx, eof);
  296. if (error == -ENOSPC || error == -EDQUOT)
  297. trace_xfs_reflink_cow_enospc(ip, imap);
  298. if (error)
  299. return error;
  300. trace_xfs_reflink_cow_alloc(ip, &got);
  301. return 0;
  302. }
  303. /* Convert part of an unwritten CoW extent to a real one. */
  304. STATIC int
  305. xfs_reflink_convert_cow_extent(
  306. struct xfs_inode *ip,
  307. struct xfs_bmbt_irec *imap,
  308. xfs_fileoff_t offset_fsb,
  309. xfs_filblks_t count_fsb,
  310. struct xfs_defer_ops *dfops)
  311. {
  312. xfs_fsblock_t first_block;
  313. int nimaps = 1;
  314. if (imap->br_state == XFS_EXT_NORM)
  315. return 0;
  316. xfs_trim_extent(imap, offset_fsb, count_fsb);
  317. trace_xfs_reflink_convert_cow(ip, imap);
  318. if (imap->br_blockcount == 0)
  319. return 0;
  320. return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
  321. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
  322. 0, imap, &nimaps, dfops);
  323. }
  324. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  325. int
  326. xfs_reflink_convert_cow(
  327. struct xfs_inode *ip,
  328. xfs_off_t offset,
  329. xfs_off_t count)
  330. {
  331. struct xfs_bmbt_irec got;
  332. struct xfs_defer_ops dfops;
  333. struct xfs_mount *mp = ip->i_mount;
  334. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  335. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  336. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  337. xfs_extnum_t idx;
  338. bool found;
  339. int error = 0;
  340. xfs_ilock(ip, XFS_ILOCK_EXCL);
  341. /* Convert all the extents to real from unwritten. */
  342. for (found = xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
  343. found && got.br_startoff < end_fsb;
  344. found = xfs_iext_get_extent(ifp, ++idx, &got)) {
  345. error = xfs_reflink_convert_cow_extent(ip, &got, offset_fsb,
  346. end_fsb - offset_fsb, &dfops);
  347. if (error)
  348. break;
  349. }
  350. /* Finish up. */
  351. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  352. return error;
  353. }
  354. /* Allocate all CoW reservations covering a range of blocks in a file. */
  355. int
  356. xfs_reflink_allocate_cow(
  357. struct xfs_inode *ip,
  358. struct xfs_bmbt_irec *imap,
  359. bool *shared,
  360. uint *lockmode)
  361. {
  362. struct xfs_mount *mp = ip->i_mount;
  363. xfs_fileoff_t offset_fsb = imap->br_startoff;
  364. xfs_filblks_t count_fsb = imap->br_blockcount;
  365. struct xfs_bmbt_irec got;
  366. struct xfs_defer_ops dfops;
  367. struct xfs_trans *tp = NULL;
  368. xfs_fsblock_t first_block;
  369. int nimaps, error = 0;
  370. bool trimmed;
  371. xfs_filblks_t resaligned;
  372. xfs_extlen_t resblks = 0;
  373. xfs_extnum_t idx;
  374. retry:
  375. ASSERT(xfs_is_reflink_inode(ip));
  376. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  377. /*
  378. * Even if the extent is not shared we might have a preallocation for
  379. * it in the COW fork. If so use it.
  380. */
  381. if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &idx, &got) &&
  382. got.br_startoff <= offset_fsb) {
  383. *shared = true;
  384. /* If we have a real allocation in the COW fork we're done. */
  385. if (!isnullstartblock(got.br_startblock)) {
  386. xfs_trim_extent(&got, offset_fsb, count_fsb);
  387. *imap = got;
  388. goto convert;
  389. }
  390. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  391. } else {
  392. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  393. if (error || !*shared)
  394. goto out;
  395. }
  396. if (!tp) {
  397. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  398. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  399. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  400. xfs_iunlock(ip, *lockmode);
  401. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  402. *lockmode = XFS_ILOCK_EXCL;
  403. xfs_ilock(ip, *lockmode);
  404. if (error)
  405. return error;
  406. error = xfs_qm_dqattach_locked(ip, 0);
  407. if (error)
  408. goto out;
  409. goto retry;
  410. }
  411. error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
  412. XFS_QMOPT_RES_REGBLKS);
  413. if (error)
  414. goto out;
  415. xfs_trans_ijoin(tp, ip, 0);
  416. xfs_defer_init(&dfops, &first_block);
  417. nimaps = 1;
  418. /* Allocate the entire reservation as unwritten blocks. */
  419. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  420. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
  421. resblks, imap, &nimaps, &dfops);
  422. if (error)
  423. goto out_bmap_cancel;
  424. /* Finish up. */
  425. error = xfs_defer_finish(&tp, &dfops, NULL);
  426. if (error)
  427. goto out_bmap_cancel;
  428. error = xfs_trans_commit(tp);
  429. if (error)
  430. return error;
  431. convert:
  432. return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb,
  433. &dfops);
  434. out_bmap_cancel:
  435. xfs_defer_cancel(&dfops);
  436. xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
  437. XFS_QMOPT_RES_REGBLKS);
  438. out:
  439. if (tp)
  440. xfs_trans_cancel(tp);
  441. return error;
  442. }
  443. /*
  444. * Find the CoW reservation for a given byte offset of a file.
  445. */
  446. bool
  447. xfs_reflink_find_cow_mapping(
  448. struct xfs_inode *ip,
  449. xfs_off_t offset,
  450. struct xfs_bmbt_irec *imap)
  451. {
  452. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  453. xfs_fileoff_t offset_fsb;
  454. struct xfs_bmbt_irec got;
  455. xfs_extnum_t idx;
  456. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  457. ASSERT(xfs_is_reflink_inode(ip));
  458. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  459. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  460. return false;
  461. if (got.br_startoff > offset_fsb)
  462. return false;
  463. trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
  464. &got);
  465. *imap = got;
  466. return true;
  467. }
  468. /*
  469. * Trim an extent to end at the next CoW reservation past offset_fsb.
  470. */
  471. void
  472. xfs_reflink_trim_irec_to_next_cow(
  473. struct xfs_inode *ip,
  474. xfs_fileoff_t offset_fsb,
  475. struct xfs_bmbt_irec *imap)
  476. {
  477. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  478. struct xfs_bmbt_irec got;
  479. xfs_extnum_t idx;
  480. if (!xfs_is_reflink_inode(ip))
  481. return;
  482. /* Find the extent in the CoW fork. */
  483. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  484. return;
  485. /* This is the extent before; try sliding up one. */
  486. if (got.br_startoff < offset_fsb) {
  487. if (!xfs_iext_get_extent(ifp, idx + 1, &got))
  488. return;
  489. }
  490. if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
  491. return;
  492. imap->br_blockcount = got.br_startoff - imap->br_startoff;
  493. trace_xfs_reflink_trim_irec(ip, imap);
  494. }
  495. /*
  496. * Cancel CoW reservations for some block range of an inode.
  497. *
  498. * If cancel_real is true this function cancels all COW fork extents for the
  499. * inode; if cancel_real is false, real extents are not cleared.
  500. */
  501. int
  502. xfs_reflink_cancel_cow_blocks(
  503. struct xfs_inode *ip,
  504. struct xfs_trans **tpp,
  505. xfs_fileoff_t offset_fsb,
  506. xfs_fileoff_t end_fsb,
  507. bool cancel_real)
  508. {
  509. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  510. struct xfs_bmbt_irec got, del;
  511. xfs_extnum_t idx;
  512. xfs_fsblock_t firstfsb;
  513. struct xfs_defer_ops dfops;
  514. int error = 0;
  515. if (!xfs_is_reflink_inode(ip))
  516. return 0;
  517. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  518. return 0;
  519. while (got.br_startoff < end_fsb) {
  520. del = got;
  521. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  522. trace_xfs_reflink_cancel_cow(ip, &del);
  523. if (isnullstartblock(del.br_startblock)) {
  524. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  525. &idx, &got, &del);
  526. if (error)
  527. break;
  528. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  529. xfs_trans_ijoin(*tpp, ip, 0);
  530. xfs_defer_init(&dfops, &firstfsb);
  531. /* Free the CoW orphan record. */
  532. error = xfs_refcount_free_cow_extent(ip->i_mount,
  533. &dfops, del.br_startblock,
  534. del.br_blockcount);
  535. if (error)
  536. break;
  537. xfs_bmap_add_free(ip->i_mount, &dfops,
  538. del.br_startblock, del.br_blockcount,
  539. NULL);
  540. /* Update quota accounting */
  541. xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
  542. -(long)del.br_blockcount);
  543. /* Roll the transaction */
  544. error = xfs_defer_finish(tpp, &dfops, ip);
  545. if (error) {
  546. xfs_defer_cancel(&dfops);
  547. break;
  548. }
  549. /* Remove the mapping from the CoW fork. */
  550. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  551. }
  552. if (!xfs_iext_get_extent(ifp, ++idx, &got))
  553. break;
  554. }
  555. /* clear tag if cow fork is emptied */
  556. if (!ifp->if_bytes)
  557. xfs_inode_clear_cowblocks_tag(ip);
  558. return error;
  559. }
  560. /*
  561. * Cancel CoW reservations for some byte range of an inode.
  562. *
  563. * If cancel_real is true this function cancels all COW fork extents for the
  564. * inode; if cancel_real is false, real extents are not cleared.
  565. */
  566. int
  567. xfs_reflink_cancel_cow_range(
  568. struct xfs_inode *ip,
  569. xfs_off_t offset,
  570. xfs_off_t count,
  571. bool cancel_real)
  572. {
  573. struct xfs_trans *tp;
  574. xfs_fileoff_t offset_fsb;
  575. xfs_fileoff_t end_fsb;
  576. int error;
  577. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  578. ASSERT(xfs_is_reflink_inode(ip));
  579. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  580. if (count == NULLFILEOFF)
  581. end_fsb = NULLFILEOFF;
  582. else
  583. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  584. /* Start a rolling transaction to remove the mappings */
  585. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  586. 0, 0, 0, &tp);
  587. if (error)
  588. goto out;
  589. xfs_ilock(ip, XFS_ILOCK_EXCL);
  590. xfs_trans_ijoin(tp, ip, 0);
  591. /* Scrape out the old CoW reservations */
  592. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  593. cancel_real);
  594. if (error)
  595. goto out_cancel;
  596. error = xfs_trans_commit(tp);
  597. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  598. return error;
  599. out_cancel:
  600. xfs_trans_cancel(tp);
  601. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  602. out:
  603. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  604. return error;
  605. }
  606. /*
  607. * Remap parts of a file's data fork after a successful CoW.
  608. */
  609. int
  610. xfs_reflink_end_cow(
  611. struct xfs_inode *ip,
  612. xfs_off_t offset,
  613. xfs_off_t count)
  614. {
  615. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  616. struct xfs_bmbt_irec got, del;
  617. struct xfs_trans *tp;
  618. xfs_fileoff_t offset_fsb;
  619. xfs_fileoff_t end_fsb;
  620. xfs_fsblock_t firstfsb;
  621. struct xfs_defer_ops dfops;
  622. int error;
  623. unsigned int resblks;
  624. xfs_filblks_t rlen;
  625. xfs_extnum_t idx;
  626. trace_xfs_reflink_end_cow(ip, offset, count);
  627. /* No COW extents? That's easy! */
  628. if (ifp->if_bytes == 0)
  629. return 0;
  630. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  631. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  632. /* Start a rolling transaction to switch the mappings */
  633. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  634. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  635. resblks, 0, 0, &tp);
  636. if (error)
  637. goto out;
  638. xfs_ilock(ip, XFS_ILOCK_EXCL);
  639. xfs_trans_ijoin(tp, ip, 0);
  640. /* If there is a hole at end_fsb - 1 go to the previous extent */
  641. if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
  642. got.br_startoff > end_fsb) {
  643. ASSERT(idx > 0);
  644. xfs_iext_get_extent(ifp, --idx, &got);
  645. }
  646. /* Walk backwards until we're out of the I/O range... */
  647. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  648. del = got;
  649. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  650. /* Extent delete may have bumped idx forward */
  651. if (!del.br_blockcount) {
  652. idx--;
  653. goto next_extent;
  654. }
  655. ASSERT(!isnullstartblock(got.br_startblock));
  656. /*
  657. * Don't remap unwritten extents; these are
  658. * speculatively preallocated CoW extents that have been
  659. * allocated but have not yet been involved in a write.
  660. */
  661. if (got.br_state == XFS_EXT_UNWRITTEN) {
  662. idx--;
  663. goto next_extent;
  664. }
  665. /* Unmap the old blocks in the data fork. */
  666. xfs_defer_init(&dfops, &firstfsb);
  667. rlen = del.br_blockcount;
  668. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
  669. &firstfsb, &dfops);
  670. if (error)
  671. goto out_defer;
  672. /* Trim the extent to whatever got unmapped. */
  673. if (rlen) {
  674. xfs_trim_extent(&del, del.br_startoff + rlen,
  675. del.br_blockcount - rlen);
  676. }
  677. trace_xfs_reflink_cow_remap(ip, &del);
  678. /* Free the CoW orphan record. */
  679. error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
  680. del.br_startblock, del.br_blockcount);
  681. if (error)
  682. goto out_defer;
  683. /* Map the new blocks into the data fork. */
  684. error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
  685. if (error)
  686. goto out_defer;
  687. /* Remove the mapping from the CoW fork. */
  688. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  689. error = xfs_defer_finish(&tp, &dfops, ip);
  690. if (error)
  691. goto out_defer;
  692. next_extent:
  693. if (!xfs_iext_get_extent(ifp, idx, &got))
  694. break;
  695. }
  696. error = xfs_trans_commit(tp);
  697. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  698. if (error)
  699. goto out;
  700. return 0;
  701. out_defer:
  702. xfs_defer_cancel(&dfops);
  703. xfs_trans_cancel(tp);
  704. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  705. out:
  706. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  707. return error;
  708. }
  709. /*
  710. * Free leftover CoW reservations that didn't get cleaned out.
  711. */
  712. int
  713. xfs_reflink_recover_cow(
  714. struct xfs_mount *mp)
  715. {
  716. xfs_agnumber_t agno;
  717. int error = 0;
  718. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  719. return 0;
  720. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  721. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  722. if (error)
  723. break;
  724. }
  725. return error;
  726. }
  727. /*
  728. * Reflinking (Block) Ranges of Two Files Together
  729. *
  730. * First, ensure that the reflink flag is set on both inodes. The flag is an
  731. * optimization to avoid unnecessary refcount btree lookups in the write path.
  732. *
  733. * Now we can iteratively remap the range of extents (and holes) in src to the
  734. * corresponding ranges in dest. Let drange and srange denote the ranges of
  735. * logical blocks in dest and src touched by the reflink operation.
  736. *
  737. * While the length of drange is greater than zero,
  738. * - Read src's bmbt at the start of srange ("imap")
  739. * - If imap doesn't exist, make imap appear to start at the end of srange
  740. * with zero length.
  741. * - If imap starts before srange, advance imap to start at srange.
  742. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  743. * - Punch (imap start - srange start + imap len) blocks from dest at
  744. * offset (drange start).
  745. * - If imap points to a real range of pblks,
  746. * > Increase the refcount of the imap's pblks
  747. * > Map imap's pblks into dest at the offset
  748. * (drange start + imap start - srange start)
  749. * - Advance drange and srange by (imap start - srange start + imap len)
  750. *
  751. * Finally, if the reflink made dest longer, update both the in-core and
  752. * on-disk file sizes.
  753. *
  754. * ASCII Art Demonstration:
  755. *
  756. * Let's say we want to reflink this source file:
  757. *
  758. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  759. * <-------------------->
  760. *
  761. * into this destination file:
  762. *
  763. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  764. * <-------------------->
  765. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  766. * Observe that the range has different logical offsets in either file.
  767. *
  768. * Consider that the first extent in the source file doesn't line up with our
  769. * reflink range. Unmapping and remapping are separate operations, so we can
  770. * unmap more blocks from the destination file than we remap.
  771. *
  772. * ----SSSSSSS-SSSSS----SSSSSS
  773. * <------->
  774. * --DDDDD---------DDDDD--DDD
  775. * <------->
  776. *
  777. * Now remap the source extent into the destination file:
  778. *
  779. * ----SSSSSSS-SSSSS----SSSSSS
  780. * <------->
  781. * --DDDDD--SSSSSSSDDDDD--DDD
  782. * <------->
  783. *
  784. * Do likewise with the second hole and extent in our range. Holes in the
  785. * unmap range don't affect our operation.
  786. *
  787. * ----SSSSSSS-SSSSS----SSSSSS
  788. * <---->
  789. * --DDDDD--SSSSSSS-SSSSS-DDD
  790. * <---->
  791. *
  792. * Finally, unmap and remap part of the third extent. This will increase the
  793. * size of the destination file.
  794. *
  795. * ----SSSSSSS-SSSSS----SSSSSS
  796. * <----->
  797. * --DDDDD--SSSSSSS-SSSSS----SSS
  798. * <----->
  799. *
  800. * Once we update the destination file's i_size, we're done.
  801. */
  802. /*
  803. * Ensure the reflink bit is set in both inodes.
  804. */
  805. STATIC int
  806. xfs_reflink_set_inode_flag(
  807. struct xfs_inode *src,
  808. struct xfs_inode *dest)
  809. {
  810. struct xfs_mount *mp = src->i_mount;
  811. int error;
  812. struct xfs_trans *tp;
  813. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  814. return 0;
  815. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  816. if (error)
  817. goto out_error;
  818. /* Lock both files against IO */
  819. if (src->i_ino == dest->i_ino)
  820. xfs_ilock(src, XFS_ILOCK_EXCL);
  821. else
  822. xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
  823. if (!xfs_is_reflink_inode(src)) {
  824. trace_xfs_reflink_set_inode_flag(src);
  825. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  826. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  827. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  828. xfs_ifork_init_cow(src);
  829. } else
  830. xfs_iunlock(src, XFS_ILOCK_EXCL);
  831. if (src->i_ino == dest->i_ino)
  832. goto commit_flags;
  833. if (!xfs_is_reflink_inode(dest)) {
  834. trace_xfs_reflink_set_inode_flag(dest);
  835. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  836. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  837. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  838. xfs_ifork_init_cow(dest);
  839. } else
  840. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  841. commit_flags:
  842. error = xfs_trans_commit(tp);
  843. if (error)
  844. goto out_error;
  845. return error;
  846. out_error:
  847. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  848. return error;
  849. }
  850. /*
  851. * Update destination inode size & cowextsize hint, if necessary.
  852. */
  853. STATIC int
  854. xfs_reflink_update_dest(
  855. struct xfs_inode *dest,
  856. xfs_off_t newlen,
  857. xfs_extlen_t cowextsize,
  858. bool is_dedupe)
  859. {
  860. struct xfs_mount *mp = dest->i_mount;
  861. struct xfs_trans *tp;
  862. int error;
  863. if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  864. return 0;
  865. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  866. if (error)
  867. goto out_error;
  868. xfs_ilock(dest, XFS_ILOCK_EXCL);
  869. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  870. if (newlen > i_size_read(VFS_I(dest))) {
  871. trace_xfs_reflink_update_inode_size(dest, newlen);
  872. i_size_write(VFS_I(dest), newlen);
  873. dest->i_d.di_size = newlen;
  874. }
  875. if (cowextsize) {
  876. dest->i_d.di_cowextsize = cowextsize;
  877. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  878. }
  879. if (!is_dedupe) {
  880. xfs_trans_ichgtime(tp, dest,
  881. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  882. }
  883. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  884. error = xfs_trans_commit(tp);
  885. if (error)
  886. goto out_error;
  887. return error;
  888. out_error:
  889. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  890. return error;
  891. }
  892. /*
  893. * Do we have enough reserve in this AG to handle a reflink? The refcount
  894. * btree already reserved all the space it needs, but the rmap btree can grow
  895. * infinitely, so we won't allow more reflinks when the AG is down to the
  896. * btree reserves.
  897. */
  898. static int
  899. xfs_reflink_ag_has_free_space(
  900. struct xfs_mount *mp,
  901. xfs_agnumber_t agno)
  902. {
  903. struct xfs_perag *pag;
  904. int error = 0;
  905. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  906. return 0;
  907. pag = xfs_perag_get(mp, agno);
  908. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
  909. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  910. error = -ENOSPC;
  911. xfs_perag_put(pag);
  912. return error;
  913. }
  914. /*
  915. * Unmap a range of blocks from a file, then map other blocks into the hole.
  916. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  917. * The extent irec is mapped into dest at irec->br_startoff.
  918. */
  919. STATIC int
  920. xfs_reflink_remap_extent(
  921. struct xfs_inode *ip,
  922. struct xfs_bmbt_irec *irec,
  923. xfs_fileoff_t destoff,
  924. xfs_off_t new_isize)
  925. {
  926. struct xfs_mount *mp = ip->i_mount;
  927. struct xfs_trans *tp;
  928. xfs_fsblock_t firstfsb;
  929. unsigned int resblks;
  930. struct xfs_defer_ops dfops;
  931. struct xfs_bmbt_irec uirec;
  932. bool real_extent;
  933. xfs_filblks_t rlen;
  934. xfs_filblks_t unmap_len;
  935. xfs_off_t newlen;
  936. int error;
  937. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  938. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  939. /* Only remap normal extents. */
  940. real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
  941. irec->br_startblock != DELAYSTARTBLOCK &&
  942. !ISUNWRITTEN(irec));
  943. /* No reflinking if we're low on space */
  944. if (real_extent) {
  945. error = xfs_reflink_ag_has_free_space(mp,
  946. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  947. if (error)
  948. goto out;
  949. }
  950. /* Start a rolling transaction to switch the mappings */
  951. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  952. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  953. if (error)
  954. goto out;
  955. xfs_ilock(ip, XFS_ILOCK_EXCL);
  956. xfs_trans_ijoin(tp, ip, 0);
  957. /* If we're not just clearing space, then do we have enough quota? */
  958. if (real_extent) {
  959. error = xfs_trans_reserve_quota_nblks(tp, ip,
  960. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  961. if (error)
  962. goto out_cancel;
  963. }
  964. trace_xfs_reflink_remap(ip, irec->br_startoff,
  965. irec->br_blockcount, irec->br_startblock);
  966. /* Unmap the old blocks in the data fork. */
  967. rlen = unmap_len;
  968. while (rlen) {
  969. xfs_defer_init(&dfops, &firstfsb);
  970. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
  971. &firstfsb, &dfops);
  972. if (error)
  973. goto out_defer;
  974. /*
  975. * Trim the extent to whatever got unmapped.
  976. * Remember, bunmapi works backwards.
  977. */
  978. uirec.br_startblock = irec->br_startblock + rlen;
  979. uirec.br_startoff = irec->br_startoff + rlen;
  980. uirec.br_blockcount = unmap_len - rlen;
  981. unmap_len = rlen;
  982. /* If this isn't a real mapping, we're done. */
  983. if (!real_extent || uirec.br_blockcount == 0)
  984. goto next_extent;
  985. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  986. uirec.br_blockcount, uirec.br_startblock);
  987. /* Update the refcount tree */
  988. error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
  989. if (error)
  990. goto out_defer;
  991. /* Map the new blocks into the data fork. */
  992. error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
  993. if (error)
  994. goto out_defer;
  995. /* Update quota accounting. */
  996. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  997. uirec.br_blockcount);
  998. /* Update dest isize if needed. */
  999. newlen = XFS_FSB_TO_B(mp,
  1000. uirec.br_startoff + uirec.br_blockcount);
  1001. newlen = min_t(xfs_off_t, newlen, new_isize);
  1002. if (newlen > i_size_read(VFS_I(ip))) {
  1003. trace_xfs_reflink_update_inode_size(ip, newlen);
  1004. i_size_write(VFS_I(ip), newlen);
  1005. ip->i_d.di_size = newlen;
  1006. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1007. }
  1008. next_extent:
  1009. /* Process all the deferred stuff. */
  1010. error = xfs_defer_finish(&tp, &dfops, ip);
  1011. if (error)
  1012. goto out_defer;
  1013. }
  1014. error = xfs_trans_commit(tp);
  1015. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1016. if (error)
  1017. goto out;
  1018. return 0;
  1019. out_defer:
  1020. xfs_defer_cancel(&dfops);
  1021. out_cancel:
  1022. xfs_trans_cancel(tp);
  1023. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1024. out:
  1025. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  1026. return error;
  1027. }
  1028. /*
  1029. * Iteratively remap one file's extents (and holes) to another's.
  1030. */
  1031. STATIC int
  1032. xfs_reflink_remap_blocks(
  1033. struct xfs_inode *src,
  1034. xfs_fileoff_t srcoff,
  1035. struct xfs_inode *dest,
  1036. xfs_fileoff_t destoff,
  1037. xfs_filblks_t len,
  1038. xfs_off_t new_isize)
  1039. {
  1040. struct xfs_bmbt_irec imap;
  1041. int nimaps;
  1042. int error = 0;
  1043. xfs_filblks_t range_len;
  1044. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1045. while (len) {
  1046. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1047. dest, destoff);
  1048. /* Read extent from the source file */
  1049. nimaps = 1;
  1050. xfs_ilock(src, XFS_ILOCK_EXCL);
  1051. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1052. xfs_iunlock(src, XFS_ILOCK_EXCL);
  1053. if (error)
  1054. goto err;
  1055. ASSERT(nimaps == 1);
  1056. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1057. &imap);
  1058. /* Translate imap into the destination file. */
  1059. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1060. imap.br_startoff += destoff - srcoff;
  1061. /* Clear dest from destoff to the end of imap and map it in. */
  1062. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1063. new_isize);
  1064. if (error)
  1065. goto err;
  1066. if (fatal_signal_pending(current)) {
  1067. error = -EINTR;
  1068. goto err;
  1069. }
  1070. /* Advance drange/srange */
  1071. srcoff += range_len;
  1072. destoff += range_len;
  1073. len -= range_len;
  1074. }
  1075. return 0;
  1076. err:
  1077. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1078. return error;
  1079. }
  1080. /*
  1081. * Link a range of blocks from one file to another.
  1082. */
  1083. int
  1084. xfs_reflink_remap_range(
  1085. struct file *file_in,
  1086. loff_t pos_in,
  1087. struct file *file_out,
  1088. loff_t pos_out,
  1089. u64 len,
  1090. bool is_dedupe)
  1091. {
  1092. struct inode *inode_in = file_inode(file_in);
  1093. struct xfs_inode *src = XFS_I(inode_in);
  1094. struct inode *inode_out = file_inode(file_out);
  1095. struct xfs_inode *dest = XFS_I(inode_out);
  1096. struct xfs_mount *mp = src->i_mount;
  1097. bool same_inode = (inode_in == inode_out);
  1098. xfs_fileoff_t sfsbno, dfsbno;
  1099. xfs_filblks_t fsblen;
  1100. xfs_extlen_t cowextsize;
  1101. ssize_t ret;
  1102. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1103. return -EOPNOTSUPP;
  1104. if (XFS_FORCED_SHUTDOWN(mp))
  1105. return -EIO;
  1106. /* Lock both files against IO */
  1107. lock_two_nondirectories(inode_in, inode_out);
  1108. if (same_inode)
  1109. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1110. else
  1111. xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
  1112. /* Check file eligibility and prepare for block sharing. */
  1113. ret = -EINVAL;
  1114. /* Don't reflink realtime inodes */
  1115. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1116. goto out_unlock;
  1117. /* Don't share DAX file data for now. */
  1118. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1119. goto out_unlock;
  1120. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1121. &len, is_dedupe);
  1122. if (ret <= 0)
  1123. goto out_unlock;
  1124. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1125. /* Set flags and remap blocks. */
  1126. ret = xfs_reflink_set_inode_flag(src, dest);
  1127. if (ret)
  1128. goto out_unlock;
  1129. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1130. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1131. fsblen = XFS_B_TO_FSB(mp, len);
  1132. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1133. pos_out + len);
  1134. if (ret)
  1135. goto out_unlock;
  1136. /* Zap any page cache for the destination file's range. */
  1137. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1138. PAGE_ALIGN(pos_out + len) - 1);
  1139. /*
  1140. * Carry the cowextsize hint from src to dest if we're sharing the
  1141. * entire source file to the entire destination file, the source file
  1142. * has a cowextsize hint, and the destination file does not.
  1143. */
  1144. cowextsize = 0;
  1145. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1146. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1147. pos_out == 0 && len >= i_size_read(inode_out) &&
  1148. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1149. cowextsize = src->i_d.di_cowextsize;
  1150. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1151. is_dedupe);
  1152. out_unlock:
  1153. xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
  1154. if (!same_inode)
  1155. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1156. unlock_two_nondirectories(inode_in, inode_out);
  1157. if (ret)
  1158. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1159. return ret;
  1160. }
  1161. /*
  1162. * The user wants to preemptively CoW all shared blocks in this file,
  1163. * which enables us to turn off the reflink flag. Iterate all
  1164. * extents which are not prealloc/delalloc to see which ranges are
  1165. * mentioned in the refcount tree, then read those blocks into the
  1166. * pagecache, dirty them, fsync them back out, and then we can update
  1167. * the inode flag. What happens if we run out of memory? :)
  1168. */
  1169. STATIC int
  1170. xfs_reflink_dirty_extents(
  1171. struct xfs_inode *ip,
  1172. xfs_fileoff_t fbno,
  1173. xfs_filblks_t end,
  1174. xfs_off_t isize)
  1175. {
  1176. struct xfs_mount *mp = ip->i_mount;
  1177. xfs_agnumber_t agno;
  1178. xfs_agblock_t agbno;
  1179. xfs_extlen_t aglen;
  1180. xfs_agblock_t rbno;
  1181. xfs_extlen_t rlen;
  1182. xfs_off_t fpos;
  1183. xfs_off_t flen;
  1184. struct xfs_bmbt_irec map[2];
  1185. int nmaps;
  1186. int error = 0;
  1187. while (end - fbno > 0) {
  1188. nmaps = 1;
  1189. /*
  1190. * Look for extents in the file. Skip holes, delalloc, or
  1191. * unwritten extents; they can't be reflinked.
  1192. */
  1193. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1194. if (error)
  1195. goto out;
  1196. if (nmaps == 0)
  1197. break;
  1198. if (map[0].br_startblock == HOLESTARTBLOCK ||
  1199. map[0].br_startblock == DELAYSTARTBLOCK ||
  1200. ISUNWRITTEN(&map[0]))
  1201. goto next;
  1202. map[1] = map[0];
  1203. while (map[1].br_blockcount) {
  1204. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1205. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1206. aglen = map[1].br_blockcount;
  1207. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1208. &rbno, &rlen, true);
  1209. if (error)
  1210. goto out;
  1211. if (rbno == NULLAGBLOCK)
  1212. break;
  1213. /* Dirty the pages */
  1214. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1215. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1216. (rbno - agbno));
  1217. flen = XFS_FSB_TO_B(mp, rlen);
  1218. if (fpos + flen > isize)
  1219. flen = isize - fpos;
  1220. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1221. &xfs_iomap_ops);
  1222. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1223. if (error)
  1224. goto out;
  1225. map[1].br_blockcount -= (rbno - agbno + rlen);
  1226. map[1].br_startoff += (rbno - agbno + rlen);
  1227. map[1].br_startblock += (rbno - agbno + rlen);
  1228. }
  1229. next:
  1230. fbno = map[0].br_startoff + map[0].br_blockcount;
  1231. }
  1232. out:
  1233. return error;
  1234. }
  1235. /* Clear the inode reflink flag if there are no shared extents. */
  1236. int
  1237. xfs_reflink_clear_inode_flag(
  1238. struct xfs_inode *ip,
  1239. struct xfs_trans **tpp)
  1240. {
  1241. struct xfs_mount *mp = ip->i_mount;
  1242. xfs_fileoff_t fbno;
  1243. xfs_filblks_t end;
  1244. xfs_agnumber_t agno;
  1245. xfs_agblock_t agbno;
  1246. xfs_extlen_t aglen;
  1247. xfs_agblock_t rbno;
  1248. xfs_extlen_t rlen;
  1249. struct xfs_bmbt_irec map;
  1250. int nmaps;
  1251. int error = 0;
  1252. ASSERT(xfs_is_reflink_inode(ip));
  1253. fbno = 0;
  1254. end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
  1255. while (end - fbno > 0) {
  1256. nmaps = 1;
  1257. /*
  1258. * Look for extents in the file. Skip holes, delalloc, or
  1259. * unwritten extents; they can't be reflinked.
  1260. */
  1261. error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
  1262. if (error)
  1263. return error;
  1264. if (nmaps == 0)
  1265. break;
  1266. if (map.br_startblock == HOLESTARTBLOCK ||
  1267. map.br_startblock == DELAYSTARTBLOCK ||
  1268. ISUNWRITTEN(&map))
  1269. goto next;
  1270. agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
  1271. agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
  1272. aglen = map.br_blockcount;
  1273. error = xfs_reflink_find_shared(mp, 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. return 0;
  1280. next:
  1281. fbno = map.br_startoff + map.br_blockcount;
  1282. }
  1283. /*
  1284. * We didn't find any shared blocks so turn off the reflink flag.
  1285. * First, get rid of any leftover CoW mappings.
  1286. */
  1287. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1288. if (error)
  1289. return error;
  1290. /* Clear the inode flag. */
  1291. trace_xfs_reflink_unset_inode_flag(ip);
  1292. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1293. xfs_inode_clear_cowblocks_tag(ip);
  1294. xfs_trans_ijoin(*tpp, ip, 0);
  1295. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1296. return error;
  1297. }
  1298. /*
  1299. * Clear the inode reflink flag if there are no shared extents and the size
  1300. * hasn't changed.
  1301. */
  1302. STATIC int
  1303. xfs_reflink_try_clear_inode_flag(
  1304. struct xfs_inode *ip)
  1305. {
  1306. struct xfs_mount *mp = ip->i_mount;
  1307. struct xfs_trans *tp;
  1308. int error = 0;
  1309. /* Start a rolling transaction to remove the mappings */
  1310. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1311. if (error)
  1312. return error;
  1313. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1314. xfs_trans_ijoin(tp, ip, 0);
  1315. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1316. if (error)
  1317. goto cancel;
  1318. error = xfs_trans_commit(tp);
  1319. if (error)
  1320. goto out;
  1321. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1322. return 0;
  1323. cancel:
  1324. xfs_trans_cancel(tp);
  1325. out:
  1326. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1327. return error;
  1328. }
  1329. /*
  1330. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1331. * the reflink flag if we unshare all of the file's blocks.
  1332. */
  1333. int
  1334. xfs_reflink_unshare(
  1335. struct xfs_inode *ip,
  1336. xfs_off_t offset,
  1337. xfs_off_t len)
  1338. {
  1339. struct xfs_mount *mp = ip->i_mount;
  1340. xfs_fileoff_t fbno;
  1341. xfs_filblks_t end;
  1342. xfs_off_t isize;
  1343. int error;
  1344. if (!xfs_is_reflink_inode(ip))
  1345. return 0;
  1346. trace_xfs_reflink_unshare(ip, offset, len);
  1347. inode_dio_wait(VFS_I(ip));
  1348. /* Try to CoW the selected ranges */
  1349. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1350. fbno = XFS_B_TO_FSBT(mp, offset);
  1351. isize = i_size_read(VFS_I(ip));
  1352. end = XFS_B_TO_FSB(mp, offset + len);
  1353. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1354. if (error)
  1355. goto out_unlock;
  1356. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1357. /* Wait for the IO to finish */
  1358. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1359. if (error)
  1360. goto out;
  1361. /* Turn off the reflink flag if possible. */
  1362. error = xfs_reflink_try_clear_inode_flag(ip);
  1363. if (error)
  1364. goto out;
  1365. return 0;
  1366. out_unlock:
  1367. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1368. out:
  1369. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1370. return error;
  1371. }