xfs_reflink.c 43 KB

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