xfs_reflink.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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;
  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 all pending CoW reservations for some block range of an inode.
  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. {
  505. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  506. struct xfs_bmbt_irec got, del;
  507. xfs_extnum_t idx;
  508. xfs_fsblock_t firstfsb;
  509. struct xfs_defer_ops dfops;
  510. int error = 0;
  511. if (!xfs_is_reflink_inode(ip))
  512. return 0;
  513. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  514. return 0;
  515. while (got.br_startoff < end_fsb) {
  516. del = got;
  517. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  518. trace_xfs_reflink_cancel_cow(ip, &del);
  519. if (isnullstartblock(del.br_startblock)) {
  520. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  521. &idx, &got, &del);
  522. if (error)
  523. break;
  524. } else {
  525. xfs_trans_ijoin(*tpp, ip, 0);
  526. xfs_defer_init(&dfops, &firstfsb);
  527. /* Free the CoW orphan record. */
  528. error = xfs_refcount_free_cow_extent(ip->i_mount,
  529. &dfops, del.br_startblock,
  530. del.br_blockcount);
  531. if (error)
  532. break;
  533. xfs_bmap_add_free(ip->i_mount, &dfops,
  534. del.br_startblock, del.br_blockcount,
  535. NULL);
  536. /* Update quota accounting */
  537. xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
  538. -(long)del.br_blockcount);
  539. /* Roll the transaction */
  540. error = xfs_defer_finish(tpp, &dfops, ip);
  541. if (error) {
  542. xfs_defer_cancel(&dfops);
  543. break;
  544. }
  545. /* Remove the mapping from the CoW fork. */
  546. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  547. }
  548. if (!xfs_iext_get_extent(ifp, ++idx, &got))
  549. break;
  550. }
  551. /* clear tag if cow fork is emptied */
  552. if (!ifp->if_bytes)
  553. xfs_inode_clear_cowblocks_tag(ip);
  554. return error;
  555. }
  556. /*
  557. * Cancel all pending CoW reservations for some byte range of an inode.
  558. */
  559. int
  560. xfs_reflink_cancel_cow_range(
  561. struct xfs_inode *ip,
  562. xfs_off_t offset,
  563. xfs_off_t count)
  564. {
  565. struct xfs_trans *tp;
  566. xfs_fileoff_t offset_fsb;
  567. xfs_fileoff_t end_fsb;
  568. int error;
  569. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  570. ASSERT(xfs_is_reflink_inode(ip));
  571. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  572. if (count == NULLFILEOFF)
  573. end_fsb = NULLFILEOFF;
  574. else
  575. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  576. /* Start a rolling transaction to remove the mappings */
  577. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  578. 0, 0, 0, &tp);
  579. if (error)
  580. goto out;
  581. xfs_ilock(ip, XFS_ILOCK_EXCL);
  582. xfs_trans_ijoin(tp, ip, 0);
  583. /* Scrape out the old CoW reservations */
  584. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
  585. if (error)
  586. goto out_cancel;
  587. error = xfs_trans_commit(tp);
  588. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  589. return error;
  590. out_cancel:
  591. xfs_trans_cancel(tp);
  592. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  593. out:
  594. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  595. return error;
  596. }
  597. /*
  598. * Remap parts of a file's data fork after a successful CoW.
  599. */
  600. int
  601. xfs_reflink_end_cow(
  602. struct xfs_inode *ip,
  603. xfs_off_t offset,
  604. xfs_off_t count)
  605. {
  606. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  607. struct xfs_bmbt_irec got, del;
  608. struct xfs_trans *tp;
  609. xfs_fileoff_t offset_fsb;
  610. xfs_fileoff_t end_fsb;
  611. xfs_fsblock_t firstfsb;
  612. struct xfs_defer_ops dfops;
  613. int error;
  614. unsigned int resblks;
  615. xfs_filblks_t rlen;
  616. xfs_extnum_t idx;
  617. trace_xfs_reflink_end_cow(ip, offset, count);
  618. /* No COW extents? That's easy! */
  619. if (ifp->if_bytes == 0)
  620. return 0;
  621. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  622. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  623. /* Start a rolling transaction to switch the mappings */
  624. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  625. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  626. resblks, 0, 0, &tp);
  627. if (error)
  628. goto out;
  629. xfs_ilock(ip, XFS_ILOCK_EXCL);
  630. xfs_trans_ijoin(tp, ip, 0);
  631. /* If there is a hole at end_fsb - 1 go to the previous extent */
  632. if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
  633. got.br_startoff > end_fsb) {
  634. ASSERT(idx > 0);
  635. xfs_iext_get_extent(ifp, --idx, &got);
  636. }
  637. /* Walk backwards until we're out of the I/O range... */
  638. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  639. del = got;
  640. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  641. /* Extent delete may have bumped idx forward */
  642. if (!del.br_blockcount) {
  643. idx--;
  644. goto next_extent;
  645. }
  646. ASSERT(!isnullstartblock(got.br_startblock));
  647. /*
  648. * Don't remap unwritten extents; these are
  649. * speculatively preallocated CoW extents that have been
  650. * allocated but have not yet been involved in a write.
  651. */
  652. if (got.br_state == XFS_EXT_UNWRITTEN) {
  653. idx--;
  654. goto next_extent;
  655. }
  656. /* Unmap the old blocks in the data fork. */
  657. xfs_defer_init(&dfops, &firstfsb);
  658. rlen = del.br_blockcount;
  659. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
  660. &firstfsb, &dfops);
  661. if (error)
  662. goto out_defer;
  663. /* Trim the extent to whatever got unmapped. */
  664. if (rlen) {
  665. xfs_trim_extent(&del, del.br_startoff + rlen,
  666. del.br_blockcount - rlen);
  667. }
  668. trace_xfs_reflink_cow_remap(ip, &del);
  669. /* Free the CoW orphan record. */
  670. error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
  671. del.br_startblock, del.br_blockcount);
  672. if (error)
  673. goto out_defer;
  674. /* Map the new blocks into the data fork. */
  675. error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
  676. if (error)
  677. goto out_defer;
  678. /* Remove the mapping from the CoW fork. */
  679. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  680. error = xfs_defer_finish(&tp, &dfops, ip);
  681. if (error)
  682. goto out_defer;
  683. next_extent:
  684. if (!xfs_iext_get_extent(ifp, idx, &got))
  685. break;
  686. }
  687. error = xfs_trans_commit(tp);
  688. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  689. if (error)
  690. goto out;
  691. return 0;
  692. out_defer:
  693. xfs_defer_cancel(&dfops);
  694. xfs_trans_cancel(tp);
  695. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  696. out:
  697. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  698. return error;
  699. }
  700. /*
  701. * Free leftover CoW reservations that didn't get cleaned out.
  702. */
  703. int
  704. xfs_reflink_recover_cow(
  705. struct xfs_mount *mp)
  706. {
  707. xfs_agnumber_t agno;
  708. int error = 0;
  709. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  710. return 0;
  711. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  712. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  713. if (error)
  714. break;
  715. }
  716. return error;
  717. }
  718. /*
  719. * Reflinking (Block) Ranges of Two Files Together
  720. *
  721. * First, ensure that the reflink flag is set on both inodes. The flag is an
  722. * optimization to avoid unnecessary refcount btree lookups in the write path.
  723. *
  724. * Now we can iteratively remap the range of extents (and holes) in src to the
  725. * corresponding ranges in dest. Let drange and srange denote the ranges of
  726. * logical blocks in dest and src touched by the reflink operation.
  727. *
  728. * While the length of drange is greater than zero,
  729. * - Read src's bmbt at the start of srange ("imap")
  730. * - If imap doesn't exist, make imap appear to start at the end of srange
  731. * with zero length.
  732. * - If imap starts before srange, advance imap to start at srange.
  733. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  734. * - Punch (imap start - srange start + imap len) blocks from dest at
  735. * offset (drange start).
  736. * - If imap points to a real range of pblks,
  737. * > Increase the refcount of the imap's pblks
  738. * > Map imap's pblks into dest at the offset
  739. * (drange start + imap start - srange start)
  740. * - Advance drange and srange by (imap start - srange start + imap len)
  741. *
  742. * Finally, if the reflink made dest longer, update both the in-core and
  743. * on-disk file sizes.
  744. *
  745. * ASCII Art Demonstration:
  746. *
  747. * Let's say we want to reflink this source file:
  748. *
  749. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  750. * <-------------------->
  751. *
  752. * into this destination file:
  753. *
  754. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  755. * <-------------------->
  756. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  757. * Observe that the range has different logical offsets in either file.
  758. *
  759. * Consider that the first extent in the source file doesn't line up with our
  760. * reflink range. Unmapping and remapping are separate operations, so we can
  761. * unmap more blocks from the destination file than we remap.
  762. *
  763. * ----SSSSSSS-SSSSS----SSSSSS
  764. * <------->
  765. * --DDDDD---------DDDDD--DDD
  766. * <------->
  767. *
  768. * Now remap the source extent into the destination file:
  769. *
  770. * ----SSSSSSS-SSSSS----SSSSSS
  771. * <------->
  772. * --DDDDD--SSSSSSSDDDDD--DDD
  773. * <------->
  774. *
  775. * Do likewise with the second hole and extent in our range. Holes in the
  776. * unmap range don't affect our operation.
  777. *
  778. * ----SSSSSSS-SSSSS----SSSSSS
  779. * <---->
  780. * --DDDDD--SSSSSSS-SSSSS-DDD
  781. * <---->
  782. *
  783. * Finally, unmap and remap part of the third extent. This will increase the
  784. * size of the destination file.
  785. *
  786. * ----SSSSSSS-SSSSS----SSSSSS
  787. * <----->
  788. * --DDDDD--SSSSSSS-SSSSS----SSS
  789. * <----->
  790. *
  791. * Once we update the destination file's i_size, we're done.
  792. */
  793. /*
  794. * Ensure the reflink bit is set in both inodes.
  795. */
  796. STATIC int
  797. xfs_reflink_set_inode_flag(
  798. struct xfs_inode *src,
  799. struct xfs_inode *dest)
  800. {
  801. struct xfs_mount *mp = src->i_mount;
  802. int error;
  803. struct xfs_trans *tp;
  804. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  805. return 0;
  806. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  807. if (error)
  808. goto out_error;
  809. /* Lock both files against IO */
  810. if (src->i_ino == dest->i_ino)
  811. xfs_ilock(src, XFS_ILOCK_EXCL);
  812. else
  813. xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
  814. if (!xfs_is_reflink_inode(src)) {
  815. trace_xfs_reflink_set_inode_flag(src);
  816. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  817. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  818. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  819. xfs_ifork_init_cow(src);
  820. } else
  821. xfs_iunlock(src, XFS_ILOCK_EXCL);
  822. if (src->i_ino == dest->i_ino)
  823. goto commit_flags;
  824. if (!xfs_is_reflink_inode(dest)) {
  825. trace_xfs_reflink_set_inode_flag(dest);
  826. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  827. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  828. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  829. xfs_ifork_init_cow(dest);
  830. } else
  831. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  832. commit_flags:
  833. error = xfs_trans_commit(tp);
  834. if (error)
  835. goto out_error;
  836. return error;
  837. out_error:
  838. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  839. return error;
  840. }
  841. /*
  842. * Update destination inode size & cowextsize hint, if necessary.
  843. */
  844. STATIC int
  845. xfs_reflink_update_dest(
  846. struct xfs_inode *dest,
  847. xfs_off_t newlen,
  848. xfs_extlen_t cowextsize)
  849. {
  850. struct xfs_mount *mp = dest->i_mount;
  851. struct xfs_trans *tp;
  852. int error;
  853. if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  854. return 0;
  855. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  856. if (error)
  857. goto out_error;
  858. xfs_ilock(dest, XFS_ILOCK_EXCL);
  859. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  860. if (newlen > i_size_read(VFS_I(dest))) {
  861. trace_xfs_reflink_update_inode_size(dest, newlen);
  862. i_size_write(VFS_I(dest), newlen);
  863. dest->i_d.di_size = newlen;
  864. }
  865. if (cowextsize) {
  866. dest->i_d.di_cowextsize = cowextsize;
  867. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  868. }
  869. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  870. error = xfs_trans_commit(tp);
  871. if (error)
  872. goto out_error;
  873. return error;
  874. out_error:
  875. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  876. return error;
  877. }
  878. /*
  879. * Do we have enough reserve in this AG to handle a reflink? The refcount
  880. * btree already reserved all the space it needs, but the rmap btree can grow
  881. * infinitely, so we won't allow more reflinks when the AG is down to the
  882. * btree reserves.
  883. */
  884. static int
  885. xfs_reflink_ag_has_free_space(
  886. struct xfs_mount *mp,
  887. xfs_agnumber_t agno)
  888. {
  889. struct xfs_perag *pag;
  890. int error = 0;
  891. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  892. return 0;
  893. pag = xfs_perag_get(mp, agno);
  894. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
  895. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  896. error = -ENOSPC;
  897. xfs_perag_put(pag);
  898. return error;
  899. }
  900. /*
  901. * Unmap a range of blocks from a file, then map other blocks into the hole.
  902. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  903. * The extent irec is mapped into dest at irec->br_startoff.
  904. */
  905. STATIC int
  906. xfs_reflink_remap_extent(
  907. struct xfs_inode *ip,
  908. struct xfs_bmbt_irec *irec,
  909. xfs_fileoff_t destoff,
  910. xfs_off_t new_isize)
  911. {
  912. struct xfs_mount *mp = ip->i_mount;
  913. struct xfs_trans *tp;
  914. xfs_fsblock_t firstfsb;
  915. unsigned int resblks;
  916. struct xfs_defer_ops dfops;
  917. struct xfs_bmbt_irec uirec;
  918. bool real_extent;
  919. xfs_filblks_t rlen;
  920. xfs_filblks_t unmap_len;
  921. xfs_off_t newlen;
  922. int error;
  923. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  924. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  925. /* Only remap normal extents. */
  926. real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
  927. irec->br_startblock != DELAYSTARTBLOCK &&
  928. !ISUNWRITTEN(irec));
  929. /* No reflinking if we're low on space */
  930. if (real_extent) {
  931. error = xfs_reflink_ag_has_free_space(mp,
  932. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  933. if (error)
  934. goto out;
  935. }
  936. /* Start a rolling transaction to switch the mappings */
  937. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  938. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  939. if (error)
  940. goto out;
  941. xfs_ilock(ip, XFS_ILOCK_EXCL);
  942. xfs_trans_ijoin(tp, ip, 0);
  943. /* If we're not just clearing space, then do we have enough quota? */
  944. if (real_extent) {
  945. error = xfs_trans_reserve_quota_nblks(tp, ip,
  946. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  947. if (error)
  948. goto out_cancel;
  949. }
  950. trace_xfs_reflink_remap(ip, irec->br_startoff,
  951. irec->br_blockcount, irec->br_startblock);
  952. /* Unmap the old blocks in the data fork. */
  953. rlen = unmap_len;
  954. while (rlen) {
  955. xfs_defer_init(&dfops, &firstfsb);
  956. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
  957. &firstfsb, &dfops);
  958. if (error)
  959. goto out_defer;
  960. /*
  961. * Trim the extent to whatever got unmapped.
  962. * Remember, bunmapi works backwards.
  963. */
  964. uirec.br_startblock = irec->br_startblock + rlen;
  965. uirec.br_startoff = irec->br_startoff + rlen;
  966. uirec.br_blockcount = unmap_len - rlen;
  967. unmap_len = rlen;
  968. /* If this isn't a real mapping, we're done. */
  969. if (!real_extent || uirec.br_blockcount == 0)
  970. goto next_extent;
  971. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  972. uirec.br_blockcount, uirec.br_startblock);
  973. /* Update the refcount tree */
  974. error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
  975. if (error)
  976. goto out_defer;
  977. /* Map the new blocks into the data fork. */
  978. error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
  979. if (error)
  980. goto out_defer;
  981. /* Update quota accounting. */
  982. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  983. uirec.br_blockcount);
  984. /* Update dest isize if needed. */
  985. newlen = XFS_FSB_TO_B(mp,
  986. uirec.br_startoff + uirec.br_blockcount);
  987. newlen = min_t(xfs_off_t, newlen, new_isize);
  988. if (newlen > i_size_read(VFS_I(ip))) {
  989. trace_xfs_reflink_update_inode_size(ip, newlen);
  990. i_size_write(VFS_I(ip), newlen);
  991. ip->i_d.di_size = newlen;
  992. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  993. }
  994. next_extent:
  995. /* Process all the deferred stuff. */
  996. error = xfs_defer_finish(&tp, &dfops, ip);
  997. if (error)
  998. goto out_defer;
  999. }
  1000. error = xfs_trans_commit(tp);
  1001. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1002. if (error)
  1003. goto out;
  1004. return 0;
  1005. out_defer:
  1006. xfs_defer_cancel(&dfops);
  1007. out_cancel:
  1008. xfs_trans_cancel(tp);
  1009. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1010. out:
  1011. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  1012. return error;
  1013. }
  1014. /*
  1015. * Iteratively remap one file's extents (and holes) to another's.
  1016. */
  1017. STATIC int
  1018. xfs_reflink_remap_blocks(
  1019. struct xfs_inode *src,
  1020. xfs_fileoff_t srcoff,
  1021. struct xfs_inode *dest,
  1022. xfs_fileoff_t destoff,
  1023. xfs_filblks_t len,
  1024. xfs_off_t new_isize)
  1025. {
  1026. struct xfs_bmbt_irec imap;
  1027. int nimaps;
  1028. int error = 0;
  1029. xfs_filblks_t range_len;
  1030. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1031. while (len) {
  1032. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1033. dest, destoff);
  1034. /* Read extent from the source file */
  1035. nimaps = 1;
  1036. xfs_ilock(src, XFS_ILOCK_EXCL);
  1037. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1038. xfs_iunlock(src, XFS_ILOCK_EXCL);
  1039. if (error)
  1040. goto err;
  1041. ASSERT(nimaps == 1);
  1042. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1043. &imap);
  1044. /* Translate imap into the destination file. */
  1045. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1046. imap.br_startoff += destoff - srcoff;
  1047. /* Clear dest from destoff to the end of imap and map it in. */
  1048. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1049. new_isize);
  1050. if (error)
  1051. goto err;
  1052. if (fatal_signal_pending(current)) {
  1053. error = -EINTR;
  1054. goto err;
  1055. }
  1056. /* Advance drange/srange */
  1057. srcoff += range_len;
  1058. destoff += range_len;
  1059. len -= range_len;
  1060. }
  1061. return 0;
  1062. err:
  1063. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1064. return error;
  1065. }
  1066. /*
  1067. * Link a range of blocks from one file to another.
  1068. */
  1069. int
  1070. xfs_reflink_remap_range(
  1071. struct file *file_in,
  1072. loff_t pos_in,
  1073. struct file *file_out,
  1074. loff_t pos_out,
  1075. u64 len,
  1076. bool is_dedupe)
  1077. {
  1078. struct inode *inode_in = file_inode(file_in);
  1079. struct xfs_inode *src = XFS_I(inode_in);
  1080. struct inode *inode_out = file_inode(file_out);
  1081. struct xfs_inode *dest = XFS_I(inode_out);
  1082. struct xfs_mount *mp = src->i_mount;
  1083. bool same_inode = (inode_in == inode_out);
  1084. xfs_fileoff_t sfsbno, dfsbno;
  1085. xfs_filblks_t fsblen;
  1086. xfs_extlen_t cowextsize;
  1087. ssize_t ret;
  1088. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1089. return -EOPNOTSUPP;
  1090. if (XFS_FORCED_SHUTDOWN(mp))
  1091. return -EIO;
  1092. /* Lock both files against IO */
  1093. lock_two_nondirectories(inode_in, inode_out);
  1094. if (same_inode)
  1095. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1096. else
  1097. xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
  1098. /* Check file eligibility and prepare for block sharing. */
  1099. ret = -EINVAL;
  1100. /* Don't reflink realtime inodes */
  1101. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1102. goto out_unlock;
  1103. /* Don't share DAX file data for now. */
  1104. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1105. goto out_unlock;
  1106. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1107. &len, is_dedupe);
  1108. if (ret <= 0)
  1109. goto out_unlock;
  1110. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1111. /* Set flags and remap blocks. */
  1112. ret = xfs_reflink_set_inode_flag(src, dest);
  1113. if (ret)
  1114. goto out_unlock;
  1115. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1116. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1117. fsblen = XFS_B_TO_FSB(mp, len);
  1118. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1119. pos_out + len);
  1120. if (ret)
  1121. goto out_unlock;
  1122. /* Zap any page cache for the destination file's range. */
  1123. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1124. PAGE_ALIGN(pos_out + len) - 1);
  1125. /*
  1126. * Carry the cowextsize hint from src to dest if we're sharing the
  1127. * entire source file to the entire destination file, the source file
  1128. * has a cowextsize hint, and the destination file does not.
  1129. */
  1130. cowextsize = 0;
  1131. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1132. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1133. pos_out == 0 && len >= i_size_read(inode_out) &&
  1134. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1135. cowextsize = src->i_d.di_cowextsize;
  1136. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
  1137. out_unlock:
  1138. xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
  1139. if (!same_inode)
  1140. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1141. unlock_two_nondirectories(inode_in, inode_out);
  1142. if (ret)
  1143. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1144. return ret;
  1145. }
  1146. /*
  1147. * The user wants to preemptively CoW all shared blocks in this file,
  1148. * which enables us to turn off the reflink flag. Iterate all
  1149. * extents which are not prealloc/delalloc to see which ranges are
  1150. * mentioned in the refcount tree, then read those blocks into the
  1151. * pagecache, dirty them, fsync them back out, and then we can update
  1152. * the inode flag. What happens if we run out of memory? :)
  1153. */
  1154. STATIC int
  1155. xfs_reflink_dirty_extents(
  1156. struct xfs_inode *ip,
  1157. xfs_fileoff_t fbno,
  1158. xfs_filblks_t end,
  1159. xfs_off_t isize)
  1160. {
  1161. struct xfs_mount *mp = ip->i_mount;
  1162. xfs_agnumber_t agno;
  1163. xfs_agblock_t agbno;
  1164. xfs_extlen_t aglen;
  1165. xfs_agblock_t rbno;
  1166. xfs_extlen_t rlen;
  1167. xfs_off_t fpos;
  1168. xfs_off_t flen;
  1169. struct xfs_bmbt_irec map[2];
  1170. int nmaps;
  1171. int error = 0;
  1172. while (end - fbno > 0) {
  1173. nmaps = 1;
  1174. /*
  1175. * Look for extents in the file. Skip holes, delalloc, or
  1176. * unwritten extents; they can't be reflinked.
  1177. */
  1178. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1179. if (error)
  1180. goto out;
  1181. if (nmaps == 0)
  1182. break;
  1183. if (map[0].br_startblock == HOLESTARTBLOCK ||
  1184. map[0].br_startblock == DELAYSTARTBLOCK ||
  1185. ISUNWRITTEN(&map[0]))
  1186. goto next;
  1187. map[1] = map[0];
  1188. while (map[1].br_blockcount) {
  1189. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1190. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1191. aglen = map[1].br_blockcount;
  1192. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1193. &rbno, &rlen, true);
  1194. if (error)
  1195. goto out;
  1196. if (rbno == NULLAGBLOCK)
  1197. break;
  1198. /* Dirty the pages */
  1199. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1200. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1201. (rbno - agbno));
  1202. flen = XFS_FSB_TO_B(mp, rlen);
  1203. if (fpos + flen > isize)
  1204. flen = isize - fpos;
  1205. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1206. &xfs_iomap_ops);
  1207. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1208. if (error)
  1209. goto out;
  1210. map[1].br_blockcount -= (rbno - agbno + rlen);
  1211. map[1].br_startoff += (rbno - agbno + rlen);
  1212. map[1].br_startblock += (rbno - agbno + rlen);
  1213. }
  1214. next:
  1215. fbno = map[0].br_startoff + map[0].br_blockcount;
  1216. }
  1217. out:
  1218. return error;
  1219. }
  1220. /* Clear the inode reflink flag if there are no shared extents. */
  1221. int
  1222. xfs_reflink_clear_inode_flag(
  1223. struct xfs_inode *ip,
  1224. struct xfs_trans **tpp)
  1225. {
  1226. struct xfs_mount *mp = ip->i_mount;
  1227. xfs_fileoff_t fbno;
  1228. xfs_filblks_t end;
  1229. xfs_agnumber_t agno;
  1230. xfs_agblock_t agbno;
  1231. xfs_extlen_t aglen;
  1232. xfs_agblock_t rbno;
  1233. xfs_extlen_t rlen;
  1234. struct xfs_bmbt_irec map;
  1235. int nmaps;
  1236. int error = 0;
  1237. ASSERT(xfs_is_reflink_inode(ip));
  1238. fbno = 0;
  1239. end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
  1240. while (end - fbno > 0) {
  1241. nmaps = 1;
  1242. /*
  1243. * Look for extents in the file. Skip holes, delalloc, or
  1244. * unwritten extents; they can't be reflinked.
  1245. */
  1246. error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
  1247. if (error)
  1248. return error;
  1249. if (nmaps == 0)
  1250. break;
  1251. if (map.br_startblock == HOLESTARTBLOCK ||
  1252. map.br_startblock == DELAYSTARTBLOCK ||
  1253. ISUNWRITTEN(&map))
  1254. goto next;
  1255. agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
  1256. agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
  1257. aglen = map.br_blockcount;
  1258. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1259. &rbno, &rlen, false);
  1260. if (error)
  1261. return error;
  1262. /* Is there still a shared block here? */
  1263. if (rbno != NULLAGBLOCK)
  1264. return 0;
  1265. next:
  1266. fbno = map.br_startoff + map.br_blockcount;
  1267. }
  1268. /*
  1269. * We didn't find any shared blocks so turn off the reflink flag.
  1270. * First, get rid of any leftover CoW mappings.
  1271. */
  1272. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
  1273. if (error)
  1274. return error;
  1275. /* Clear the inode flag. */
  1276. trace_xfs_reflink_unset_inode_flag(ip);
  1277. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1278. xfs_inode_clear_cowblocks_tag(ip);
  1279. xfs_trans_ijoin(*tpp, ip, 0);
  1280. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1281. return error;
  1282. }
  1283. /*
  1284. * Clear the inode reflink flag if there are no shared extents and the size
  1285. * hasn't changed.
  1286. */
  1287. STATIC int
  1288. xfs_reflink_try_clear_inode_flag(
  1289. struct xfs_inode *ip)
  1290. {
  1291. struct xfs_mount *mp = ip->i_mount;
  1292. struct xfs_trans *tp;
  1293. int error = 0;
  1294. /* Start a rolling transaction to remove the mappings */
  1295. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1296. if (error)
  1297. return error;
  1298. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1299. xfs_trans_ijoin(tp, ip, 0);
  1300. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1301. if (error)
  1302. goto cancel;
  1303. error = xfs_trans_commit(tp);
  1304. if (error)
  1305. goto out;
  1306. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1307. return 0;
  1308. cancel:
  1309. xfs_trans_cancel(tp);
  1310. out:
  1311. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1312. return error;
  1313. }
  1314. /*
  1315. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1316. * the reflink flag if we unshare all of the file's blocks.
  1317. */
  1318. int
  1319. xfs_reflink_unshare(
  1320. struct xfs_inode *ip,
  1321. xfs_off_t offset,
  1322. xfs_off_t len)
  1323. {
  1324. struct xfs_mount *mp = ip->i_mount;
  1325. xfs_fileoff_t fbno;
  1326. xfs_filblks_t end;
  1327. xfs_off_t isize;
  1328. int error;
  1329. if (!xfs_is_reflink_inode(ip))
  1330. return 0;
  1331. trace_xfs_reflink_unshare(ip, offset, len);
  1332. inode_dio_wait(VFS_I(ip));
  1333. /* Try to CoW the selected ranges */
  1334. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1335. fbno = XFS_B_TO_FSBT(mp, offset);
  1336. isize = i_size_read(VFS_I(ip));
  1337. end = XFS_B_TO_FSB(mp, offset + len);
  1338. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1339. if (error)
  1340. goto out_unlock;
  1341. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1342. /* Wait for the IO to finish */
  1343. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1344. if (error)
  1345. goto out;
  1346. /* Turn off the reflink flag if possible. */
  1347. error = xfs_reflink_try_clear_inode_flag(ip);
  1348. if (error)
  1349. goto out;
  1350. return 0;
  1351. out_unlock:
  1352. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1353. out:
  1354. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1355. return error;
  1356. }