xfs_reflink.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_defer.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_inode_item.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_bmap_util.h"
  21. #include "xfs_error.h"
  22. #include "xfs_dir2.h"
  23. #include "xfs_dir2_priv.h"
  24. #include "xfs_ioctl.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_log.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_pnfs.h"
  29. #include "xfs_btree.h"
  30. #include "xfs_refcount_btree.h"
  31. #include "xfs_refcount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_trans_space.h"
  34. #include "xfs_bit.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_quota_defs.h"
  37. #include "xfs_quota.h"
  38. #include "xfs_reflink.h"
  39. #include "xfs_iomap.h"
  40. #include "xfs_rmap_btree.h"
  41. #include "xfs_sb.h"
  42. #include "xfs_ag_resv.h"
  43. /*
  44. * Copy on Write of Shared Blocks
  45. *
  46. * XFS must preserve "the usual" file semantics even when two files share
  47. * the same physical blocks. This means that a write to one file must not
  48. * alter the blocks in a different file; the way that we'll do that is
  49. * through the use of a copy-on-write mechanism. At a high level, that
  50. * means that when we want to write to a shared block, we allocate a new
  51. * block, write the data to the new block, and if that succeeds we map the
  52. * new block into the file.
  53. *
  54. * XFS provides a "delayed allocation" mechanism that defers the allocation
  55. * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
  56. * possible. This reduces fragmentation by enabling the filesystem to ask
  57. * for bigger chunks less often, which is exactly what we want for CoW.
  58. *
  59. * The delalloc mechanism begins when the kernel wants to make a block
  60. * writable (write_begin or page_mkwrite). If the offset is not mapped, we
  61. * create a delalloc mapping, which is a regular in-core extent, but without
  62. * a real startblock. (For delalloc mappings, the startblock encodes both
  63. * a flag that this is a delalloc mapping, and a worst-case estimate of how
  64. * many blocks might be required to put the mapping into the BMBT.) delalloc
  65. * mappings are a reservation against the free space in the filesystem;
  66. * adjacent mappings can also be combined into fewer larger mappings.
  67. *
  68. * As an optimization, the CoW extent size hint (cowextsz) creates
  69. * outsized aligned delalloc reservations in the hope of landing out of
  70. * order nearby CoW writes in a single extent on disk, thereby reducing
  71. * fragmentation and improving future performance.
  72. *
  73. * D: --RRRRRRSSSRRRRRRRR--- (data fork)
  74. * C: ------DDDDDDD--------- (CoW fork)
  75. *
  76. * When dirty pages are being written out (typically in writepage), the
  77. * delalloc reservations are converted into unwritten mappings by
  78. * allocating blocks and replacing the delalloc mapping with real ones.
  79. * A delalloc mapping can be replaced by several unwritten ones if the
  80. * free space is fragmented.
  81. *
  82. * D: --RRRRRRSSSRRRRRRRR---
  83. * C: ------UUUUUUU---------
  84. *
  85. * We want to adapt the delalloc mechanism for copy-on-write, since the
  86. * write paths are similar. The first two steps (creating the reservation
  87. * and allocating the blocks) are exactly the same as delalloc except that
  88. * the mappings must be stored in a separate CoW fork because we do not want
  89. * to disturb the mapping in the data fork until we're sure that the write
  90. * succeeded. IO completion in this case is the process of removing the old
  91. * mapping from the data fork and moving the new mapping from the CoW fork to
  92. * the data fork. This will be discussed shortly.
  93. *
  94. * For now, unaligned directio writes will be bounced back to the page cache.
  95. * Block-aligned directio writes will use the same mechanism as buffered
  96. * writes.
  97. *
  98. * Just prior to submitting the actual disk write requests, we convert
  99. * the extents representing the range of the file actually being written
  100. * (as opposed to extra pieces created for the cowextsize hint) to real
  101. * extents. This will become important in the next step:
  102. *
  103. * D: --RRRRRRSSSRRRRRRRR---
  104. * C: ------UUrrUUU---------
  105. *
  106. * CoW remapping must be done after the data block write completes,
  107. * because we don't want to destroy the old data fork map until we're sure
  108. * the new block has been written. Since the new mappings are kept in a
  109. * separate fork, we can simply iterate these mappings to find the ones
  110. * that cover the file blocks that we just CoW'd. For each extent, simply
  111. * unmap the corresponding range in the data fork, map the new range into
  112. * the data fork, and remove the extent from the CoW fork. Because of
  113. * the presence of the cowextsize hint, however, we must be careful
  114. * only to remap the blocks that we've actually written out -- we must
  115. * never remap delalloc reservations nor CoW staging blocks that have
  116. * yet to be written. This corresponds exactly to the real extents in
  117. * the CoW fork:
  118. *
  119. * D: --RRRRRRrrSRRRRRRRR---
  120. * C: ------UU--UUU---------
  121. *
  122. * Since the remapping operation can be applied to an arbitrary file
  123. * range, we record the need for the remap step as a flag in the ioend
  124. * instead of declaring a new IO type. This is required for direct io
  125. * because we only have ioend for the whole dio, and we have to be able to
  126. * remember the presence of unwritten blocks and CoW blocks with a single
  127. * ioend structure. Better yet, the more ground we can cover with one
  128. * ioend, the better.
  129. */
  130. /*
  131. * Given an AG extent, find the lowest-numbered run of shared blocks
  132. * within that range and return the range in fbno/flen. If
  133. * find_end_of_shared is true, return the longest contiguous extent of
  134. * shared blocks. If there are no shared extents, fbno and flen will
  135. * be set to NULLAGBLOCK and 0, respectively.
  136. */
  137. int
  138. xfs_reflink_find_shared(
  139. struct xfs_mount *mp,
  140. struct xfs_trans *tp,
  141. xfs_agnumber_t agno,
  142. xfs_agblock_t agbno,
  143. xfs_extlen_t aglen,
  144. xfs_agblock_t *fbno,
  145. xfs_extlen_t *flen,
  146. bool find_end_of_shared)
  147. {
  148. struct xfs_buf *agbp;
  149. struct xfs_btree_cur *cur;
  150. int error;
  151. error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
  152. if (error)
  153. return error;
  154. if (!agbp)
  155. return -ENOMEM;
  156. cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
  157. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  158. find_end_of_shared);
  159. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  160. xfs_trans_brelse(tp, agbp);
  161. return error;
  162. }
  163. /*
  164. * Trim the mapping to the next block where there's a change in the
  165. * shared/unshared status. More specifically, this means that we
  166. * find the lowest-numbered extent of shared blocks that coincides with
  167. * the given block mapping. If the shared extent overlaps the start of
  168. * the mapping, trim the mapping to the end of the shared extent. If
  169. * the shared region intersects the mapping, trim the mapping to the
  170. * start of the shared extent. If there are no shared regions that
  171. * overlap, just return the original extent.
  172. */
  173. int
  174. xfs_reflink_trim_around_shared(
  175. struct xfs_inode *ip,
  176. struct xfs_bmbt_irec *irec,
  177. bool *shared,
  178. bool *trimmed)
  179. {
  180. xfs_agnumber_t agno;
  181. xfs_agblock_t agbno;
  182. xfs_extlen_t aglen;
  183. xfs_agblock_t fbno;
  184. xfs_extlen_t flen;
  185. int error = 0;
  186. /* Holes, unwritten, and delalloc extents cannot be shared */
  187. if (!xfs_is_reflink_inode(ip) || !xfs_bmap_is_real_extent(irec)) {
  188. *shared = false;
  189. return 0;
  190. }
  191. trace_xfs_reflink_trim_around_shared(ip, irec);
  192. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  193. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  194. aglen = irec->br_blockcount;
  195. error = xfs_reflink_find_shared(ip->i_mount, NULL, agno, agbno,
  196. aglen, &fbno, &flen, true);
  197. if (error)
  198. return error;
  199. *shared = *trimmed = false;
  200. if (fbno == NULLAGBLOCK) {
  201. /* No shared blocks at all. */
  202. return 0;
  203. } else if (fbno == agbno) {
  204. /*
  205. * The start of this extent is shared. Truncate the
  206. * mapping at the end of the shared region so that a
  207. * subsequent iteration starts at the start of the
  208. * unshared region.
  209. */
  210. irec->br_blockcount = flen;
  211. *shared = true;
  212. if (flen != aglen)
  213. *trimmed = true;
  214. return 0;
  215. } else {
  216. /*
  217. * There's a shared extent midway through this extent.
  218. * Truncate the mapping at the start of the shared
  219. * extent so that a subsequent iteration starts at the
  220. * start of the shared region.
  221. */
  222. irec->br_blockcount = fbno - agbno;
  223. *trimmed = true;
  224. return 0;
  225. }
  226. }
  227. /*
  228. * Trim the passed in imap to the next shared/unshared extent boundary, and
  229. * if imap->br_startoff points to a shared extent reserve space for it in the
  230. * COW fork. In this case *shared is set to true, else to false.
  231. *
  232. * Note that imap will always contain the block numbers for the existing blocks
  233. * in the data fork, as the upper layers need them for read-modify-write
  234. * operations.
  235. */
  236. int
  237. xfs_reflink_reserve_cow(
  238. struct xfs_inode *ip,
  239. struct xfs_bmbt_irec *imap,
  240. bool *shared)
  241. {
  242. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  243. struct xfs_bmbt_irec got;
  244. int error = 0;
  245. bool eof = false, trimmed;
  246. struct xfs_iext_cursor icur;
  247. /*
  248. * Search the COW fork extent list first. This serves two purposes:
  249. * first this implement the speculative preallocation using cowextisze,
  250. * so that we also unshared block adjacent to shared blocks instead
  251. * of just the shared blocks themselves. Second the lookup in the
  252. * extent list is generally faster than going out to the shared extent
  253. * tree.
  254. */
  255. if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &icur, &got))
  256. eof = true;
  257. if (!eof && got.br_startoff <= imap->br_startoff) {
  258. trace_xfs_reflink_cow_found(ip, imap);
  259. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  260. *shared = true;
  261. return 0;
  262. }
  263. /* Trim the mapping to the nearest shared extent boundary. */
  264. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  265. if (error)
  266. return error;
  267. /* Not shared? Just report the (potentially capped) extent. */
  268. if (!*shared)
  269. return 0;
  270. /*
  271. * Fork all the shared blocks from our write offset until the end of
  272. * the extent.
  273. */
  274. error = xfs_qm_dqattach_locked(ip, false);
  275. if (error)
  276. return error;
  277. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
  278. imap->br_blockcount, 0, &got, &icur, eof);
  279. if (error == -ENOSPC || error == -EDQUOT)
  280. trace_xfs_reflink_cow_enospc(ip, imap);
  281. if (error)
  282. return error;
  283. trace_xfs_reflink_cow_alloc(ip, &got);
  284. return 0;
  285. }
  286. /* Convert part of an unwritten CoW extent to a real one. */
  287. STATIC int
  288. xfs_reflink_convert_cow_extent(
  289. struct xfs_inode *ip,
  290. struct xfs_bmbt_irec *imap,
  291. xfs_fileoff_t offset_fsb,
  292. xfs_filblks_t count_fsb,
  293. struct xfs_defer_ops *dfops)
  294. {
  295. xfs_fsblock_t first_block = NULLFSBLOCK;
  296. int nimaps = 1;
  297. if (imap->br_state == XFS_EXT_NORM)
  298. return 0;
  299. xfs_trim_extent(imap, offset_fsb, count_fsb);
  300. trace_xfs_reflink_convert_cow(ip, imap);
  301. if (imap->br_blockcount == 0)
  302. return 0;
  303. return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
  304. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
  305. 0, imap, &nimaps, dfops);
  306. }
  307. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  308. int
  309. xfs_reflink_convert_cow(
  310. struct xfs_inode *ip,
  311. xfs_off_t offset,
  312. xfs_off_t count)
  313. {
  314. struct xfs_mount *mp = ip->i_mount;
  315. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  316. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  317. xfs_filblks_t count_fsb = end_fsb - offset_fsb;
  318. struct xfs_bmbt_irec imap;
  319. struct xfs_defer_ops dfops;
  320. xfs_fsblock_t first_block = NULLFSBLOCK;
  321. int nimaps = 1, error = 0;
  322. ASSERT(count != 0);
  323. xfs_ilock(ip, XFS_ILOCK_EXCL);
  324. error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
  325. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
  326. XFS_BMAPI_CONVERT_ONLY, &first_block, 0, &imap, &nimaps,
  327. &dfops);
  328. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  329. return error;
  330. }
  331. /* Allocate all CoW reservations covering a range of blocks in a file. */
  332. int
  333. xfs_reflink_allocate_cow(
  334. struct xfs_inode *ip,
  335. struct xfs_bmbt_irec *imap,
  336. bool *shared,
  337. uint *lockmode)
  338. {
  339. struct xfs_mount *mp = ip->i_mount;
  340. xfs_fileoff_t offset_fsb = imap->br_startoff;
  341. xfs_filblks_t count_fsb = imap->br_blockcount;
  342. struct xfs_bmbt_irec got;
  343. struct xfs_defer_ops dfops;
  344. struct xfs_trans *tp = NULL;
  345. xfs_fsblock_t first_block;
  346. int nimaps, error = 0;
  347. bool trimmed;
  348. xfs_filblks_t resaligned;
  349. xfs_extlen_t resblks = 0;
  350. struct xfs_iext_cursor icur;
  351. retry:
  352. ASSERT(xfs_is_reflink_inode(ip));
  353. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  354. /*
  355. * Even if the extent is not shared we might have a preallocation for
  356. * it in the COW fork. If so use it.
  357. */
  358. if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) &&
  359. got.br_startoff <= offset_fsb) {
  360. *shared = true;
  361. /* If we have a real allocation in the COW fork we're done. */
  362. if (!isnullstartblock(got.br_startblock)) {
  363. xfs_trim_extent(&got, offset_fsb, count_fsb);
  364. *imap = got;
  365. goto convert;
  366. }
  367. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  368. } else {
  369. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  370. if (error || !*shared)
  371. goto out;
  372. }
  373. if (!tp) {
  374. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  375. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  376. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  377. xfs_iunlock(ip, *lockmode);
  378. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  379. *lockmode = XFS_ILOCK_EXCL;
  380. xfs_ilock(ip, *lockmode);
  381. if (error)
  382. return error;
  383. error = xfs_qm_dqattach_locked(ip, false);
  384. if (error)
  385. goto out;
  386. goto retry;
  387. }
  388. error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
  389. XFS_QMOPT_RES_REGBLKS);
  390. if (error)
  391. goto out;
  392. xfs_trans_ijoin(tp, ip, 0);
  393. xfs_defer_init(&dfops, &first_block);
  394. nimaps = 1;
  395. /* Allocate the entire reservation as unwritten blocks. */
  396. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  397. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
  398. resblks, imap, &nimaps, &dfops);
  399. if (error)
  400. goto out_bmap_cancel;
  401. xfs_inode_set_cowblocks_tag(ip);
  402. /* Finish up. */
  403. error = xfs_defer_finish(&tp, &dfops);
  404. if (error)
  405. goto out_bmap_cancel;
  406. error = xfs_trans_commit(tp);
  407. if (error)
  408. return error;
  409. /*
  410. * Allocation succeeded but the requested range was not even partially
  411. * satisfied? Bail out!
  412. */
  413. if (nimaps == 0)
  414. return -ENOSPC;
  415. convert:
  416. return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb,
  417. &dfops);
  418. out_bmap_cancel:
  419. xfs_defer_cancel(&dfops);
  420. xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
  421. XFS_QMOPT_RES_REGBLKS);
  422. out:
  423. if (tp)
  424. xfs_trans_cancel(tp);
  425. return error;
  426. }
  427. /*
  428. * Find the CoW reservation for a given byte offset of a file.
  429. */
  430. bool
  431. xfs_reflink_find_cow_mapping(
  432. struct xfs_inode *ip,
  433. xfs_off_t offset,
  434. struct xfs_bmbt_irec *imap)
  435. {
  436. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  437. xfs_fileoff_t offset_fsb;
  438. struct xfs_bmbt_irec got;
  439. struct xfs_iext_cursor icur;
  440. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  441. if (!xfs_is_reflink_inode(ip))
  442. return false;
  443. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  444. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got))
  445. return false;
  446. if (got.br_startoff > offset_fsb)
  447. return false;
  448. trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
  449. &got);
  450. *imap = got;
  451. return true;
  452. }
  453. /*
  454. * Trim an extent to end at the next CoW reservation past offset_fsb.
  455. */
  456. void
  457. xfs_reflink_trim_irec_to_next_cow(
  458. struct xfs_inode *ip,
  459. xfs_fileoff_t offset_fsb,
  460. struct xfs_bmbt_irec *imap)
  461. {
  462. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  463. struct xfs_bmbt_irec got;
  464. struct xfs_iext_cursor icur;
  465. if (!xfs_is_reflink_inode(ip))
  466. return;
  467. /* Find the extent in the CoW fork. */
  468. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got))
  469. return;
  470. /* This is the extent before; try sliding up one. */
  471. if (got.br_startoff < offset_fsb) {
  472. if (!xfs_iext_next_extent(ifp, &icur, &got))
  473. return;
  474. }
  475. if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
  476. return;
  477. imap->br_blockcount = got.br_startoff - imap->br_startoff;
  478. trace_xfs_reflink_trim_irec(ip, imap);
  479. }
  480. /*
  481. * Cancel CoW reservations for some block range of an inode.
  482. *
  483. * If cancel_real is true this function cancels all COW fork extents for the
  484. * inode; if cancel_real is false, real extents are not cleared.
  485. *
  486. * Caller must have already joined the inode to the current transaction. The
  487. * inode will be joined to the transaction returned to the caller.
  488. */
  489. int
  490. xfs_reflink_cancel_cow_blocks(
  491. struct xfs_inode *ip,
  492. struct xfs_trans **tpp,
  493. xfs_fileoff_t offset_fsb,
  494. xfs_fileoff_t end_fsb,
  495. bool cancel_real)
  496. {
  497. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  498. struct xfs_bmbt_irec got, del;
  499. struct xfs_iext_cursor icur;
  500. xfs_fsblock_t firstfsb;
  501. struct xfs_defer_ops dfops;
  502. int error = 0;
  503. if (!xfs_is_reflink_inode(ip))
  504. return 0;
  505. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  506. return 0;
  507. /* Walk backwards until we're out of the I/O range... */
  508. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  509. del = got;
  510. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  511. /* Extent delete may have bumped ext forward */
  512. if (!del.br_blockcount) {
  513. xfs_iext_prev(ifp, &icur);
  514. goto next_extent;
  515. }
  516. trace_xfs_reflink_cancel_cow(ip, &del);
  517. if (isnullstartblock(del.br_startblock)) {
  518. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  519. &icur, &got, &del);
  520. if (error)
  521. break;
  522. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  523. xfs_defer_init(&dfops, &firstfsb);
  524. /* Free the CoW orphan record. */
  525. error = xfs_refcount_free_cow_extent(ip->i_mount,
  526. &dfops, del.br_startblock,
  527. del.br_blockcount);
  528. if (error)
  529. break;
  530. xfs_bmap_add_free(ip->i_mount, &dfops,
  531. del.br_startblock, del.br_blockcount,
  532. NULL);
  533. /* Roll the transaction */
  534. xfs_defer_ijoin(&dfops, ip);
  535. error = xfs_defer_finish(tpp, &dfops);
  536. if (error) {
  537. xfs_defer_cancel(&dfops);
  538. break;
  539. }
  540. /* Remove the mapping from the CoW fork. */
  541. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  542. /* Remove the quota reservation */
  543. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  544. -(long)del.br_blockcount, 0,
  545. XFS_QMOPT_RES_REGBLKS);
  546. if (error)
  547. break;
  548. } else {
  549. /* Didn't do anything, push cursor back. */
  550. xfs_iext_prev(ifp, &icur);
  551. }
  552. next_extent:
  553. if (!xfs_iext_get_extent(ifp, &icur, &got))
  554. break;
  555. }
  556. /* clear tag if cow fork is emptied */
  557. if (!ifp->if_bytes)
  558. xfs_inode_clear_cowblocks_tag(ip);
  559. return error;
  560. }
  561. /*
  562. * Cancel CoW reservations for some byte range of an inode.
  563. *
  564. * If cancel_real is true this function cancels all COW fork extents for the
  565. * inode; if cancel_real is false, real extents are not cleared.
  566. */
  567. int
  568. xfs_reflink_cancel_cow_range(
  569. struct xfs_inode *ip,
  570. xfs_off_t offset,
  571. xfs_off_t count,
  572. bool cancel_real)
  573. {
  574. struct xfs_trans *tp;
  575. xfs_fileoff_t offset_fsb;
  576. xfs_fileoff_t end_fsb;
  577. int error;
  578. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  579. ASSERT(xfs_is_reflink_inode(ip));
  580. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  581. if (count == NULLFILEOFF)
  582. end_fsb = NULLFILEOFF;
  583. else
  584. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  585. /* Start a rolling transaction to remove the mappings */
  586. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  587. 0, 0, XFS_TRANS_NOFS, &tp);
  588. if (error)
  589. goto out;
  590. xfs_ilock(ip, XFS_ILOCK_EXCL);
  591. xfs_trans_ijoin(tp, ip, 0);
  592. /* Scrape out the old CoW reservations */
  593. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  594. cancel_real);
  595. if (error)
  596. goto out_cancel;
  597. error = xfs_trans_commit(tp);
  598. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  599. return error;
  600. out_cancel:
  601. xfs_trans_cancel(tp);
  602. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  603. out:
  604. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  605. return error;
  606. }
  607. /*
  608. * Remap parts of a file's data fork after a successful CoW.
  609. */
  610. int
  611. xfs_reflink_end_cow(
  612. struct xfs_inode *ip,
  613. xfs_off_t offset,
  614. xfs_off_t count)
  615. {
  616. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  617. struct xfs_bmbt_irec got, del;
  618. struct xfs_trans *tp;
  619. xfs_fileoff_t offset_fsb;
  620. xfs_fileoff_t end_fsb;
  621. xfs_fsblock_t firstfsb;
  622. struct xfs_defer_ops dfops;
  623. int error;
  624. unsigned int resblks;
  625. xfs_filblks_t rlen;
  626. struct xfs_iext_cursor icur;
  627. trace_xfs_reflink_end_cow(ip, offset, count);
  628. /* No COW extents? That's easy! */
  629. if (ifp->if_bytes == 0)
  630. return 0;
  631. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  632. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  633. /*
  634. * Start a rolling transaction to switch the mappings. We're
  635. * unlikely ever to have to remap 16T worth of single-block
  636. * extents, so just cap the worst case extent count to 2^32-1.
  637. * Stick a warning in just in case, and avoid 64-bit division.
  638. */
  639. BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
  640. if (end_fsb - offset_fsb > UINT_MAX) {
  641. error = -EFSCORRUPTED;
  642. xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
  643. ASSERT(0);
  644. goto out;
  645. }
  646. resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
  647. (unsigned int)(end_fsb - offset_fsb),
  648. XFS_DATA_FORK);
  649. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  650. resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  651. if (error)
  652. goto out;
  653. xfs_ilock(ip, XFS_ILOCK_EXCL);
  654. xfs_trans_ijoin(tp, ip, 0);
  655. /*
  656. * In case of racing, overlapping AIO writes no COW extents might be
  657. * left by the time I/O completes for the loser of the race. In that
  658. * case we are done.
  659. */
  660. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  661. goto out_cancel;
  662. /* Walk backwards until we're out of the I/O range... */
  663. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  664. del = got;
  665. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  666. /* Extent delete may have bumped ext forward */
  667. if (!del.br_blockcount)
  668. goto prev_extent;
  669. ASSERT(!isnullstartblock(got.br_startblock));
  670. /*
  671. * Don't remap unwritten extents; these are
  672. * speculatively preallocated CoW extents that have been
  673. * allocated but have not yet been involved in a write.
  674. */
  675. if (got.br_state == XFS_EXT_UNWRITTEN)
  676. goto prev_extent;
  677. /* Unmap the old blocks in the data fork. */
  678. xfs_defer_init(&dfops, &firstfsb);
  679. rlen = del.br_blockcount;
  680. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
  681. &firstfsb, &dfops);
  682. if (error)
  683. goto out_defer;
  684. /* Trim the extent to whatever got unmapped. */
  685. if (rlen) {
  686. xfs_trim_extent(&del, del.br_startoff + rlen,
  687. del.br_blockcount - rlen);
  688. }
  689. trace_xfs_reflink_cow_remap(ip, &del);
  690. /* Free the CoW orphan record. */
  691. error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
  692. del.br_startblock, del.br_blockcount);
  693. if (error)
  694. goto out_defer;
  695. /* Map the new blocks into the data fork. */
  696. error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
  697. if (error)
  698. goto out_defer;
  699. /* Charge this new data fork mapping to the on-disk quota. */
  700. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
  701. (long)del.br_blockcount);
  702. /* Remove the mapping from the CoW fork. */
  703. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  704. xfs_defer_ijoin(&dfops, ip);
  705. error = xfs_defer_finish(&tp, &dfops);
  706. if (error)
  707. goto out_defer;
  708. if (!xfs_iext_get_extent(ifp, &icur, &got))
  709. break;
  710. continue;
  711. prev_extent:
  712. if (!xfs_iext_prev_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, XFS_ILOCK_EXCL, 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_RMAPBT) ||
  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. uint lock_mode;
  1064. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1065. dest, destoff);
  1066. /* Read extent from the source file */
  1067. nimaps = 1;
  1068. lock_mode = xfs_ilock_data_map_shared(src);
  1069. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1070. xfs_iunlock(src, lock_mode);
  1071. if (error)
  1072. goto err;
  1073. ASSERT(nimaps == 1);
  1074. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1075. &imap);
  1076. /* Translate imap into the destination file. */
  1077. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1078. imap.br_startoff += destoff - srcoff;
  1079. /* Clear dest from destoff to the end of imap and map it in. */
  1080. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1081. new_isize);
  1082. if (error)
  1083. goto err;
  1084. if (fatal_signal_pending(current)) {
  1085. error = -EINTR;
  1086. goto err;
  1087. }
  1088. /* Advance drange/srange */
  1089. srcoff += range_len;
  1090. destoff += range_len;
  1091. len -= range_len;
  1092. }
  1093. return 0;
  1094. err:
  1095. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1096. return error;
  1097. }
  1098. /*
  1099. * Grab the exclusive iolock for a data copy from src to dest, making
  1100. * sure to abide vfs locking order (lowest pointer value goes first) and
  1101. * breaking the pnfs layout leases on dest before proceeding. The loop
  1102. * is needed because we cannot call the blocking break_layout() with the
  1103. * src iolock held, and therefore have to back out both locks.
  1104. */
  1105. static int
  1106. xfs_iolock_two_inodes_and_break_layout(
  1107. struct inode *src,
  1108. struct inode *dest)
  1109. {
  1110. int error;
  1111. retry:
  1112. if (src < dest) {
  1113. inode_lock_shared(src);
  1114. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  1115. } else {
  1116. /* src >= dest */
  1117. inode_lock(dest);
  1118. }
  1119. error = break_layout(dest, false);
  1120. if (error == -EWOULDBLOCK) {
  1121. inode_unlock(dest);
  1122. if (src < dest)
  1123. inode_unlock_shared(src);
  1124. error = break_layout(dest, true);
  1125. if (error)
  1126. return error;
  1127. goto retry;
  1128. }
  1129. if (error) {
  1130. inode_unlock(dest);
  1131. if (src < dest)
  1132. inode_unlock_shared(src);
  1133. return error;
  1134. }
  1135. if (src > dest)
  1136. inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
  1137. return 0;
  1138. }
  1139. /*
  1140. * Link a range of blocks from one file to another.
  1141. */
  1142. int
  1143. xfs_reflink_remap_range(
  1144. struct file *file_in,
  1145. loff_t pos_in,
  1146. struct file *file_out,
  1147. loff_t pos_out,
  1148. u64 len,
  1149. bool is_dedupe)
  1150. {
  1151. struct inode *inode_in = file_inode(file_in);
  1152. struct xfs_inode *src = XFS_I(inode_in);
  1153. struct inode *inode_out = file_inode(file_out);
  1154. struct xfs_inode *dest = XFS_I(inode_out);
  1155. struct xfs_mount *mp = src->i_mount;
  1156. bool same_inode = (inode_in == inode_out);
  1157. xfs_fileoff_t sfsbno, dfsbno;
  1158. xfs_filblks_t fsblen;
  1159. xfs_extlen_t cowextsize;
  1160. ssize_t ret;
  1161. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1162. return -EOPNOTSUPP;
  1163. if (XFS_FORCED_SHUTDOWN(mp))
  1164. return -EIO;
  1165. /* Lock both files against IO */
  1166. ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
  1167. if (ret)
  1168. return ret;
  1169. if (same_inode)
  1170. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1171. else
  1172. xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
  1173. XFS_MMAPLOCK_EXCL);
  1174. /* Check file eligibility and prepare for block sharing. */
  1175. ret = -EINVAL;
  1176. /* Don't reflink realtime inodes */
  1177. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1178. goto out_unlock;
  1179. /* Don't share DAX file data for now. */
  1180. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1181. goto out_unlock;
  1182. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1183. &len, is_dedupe);
  1184. if (ret <= 0)
  1185. goto out_unlock;
  1186. /* Attach dquots to dest inode before changing block map */
  1187. ret = xfs_qm_dqattach(dest);
  1188. if (ret)
  1189. goto out_unlock;
  1190. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1191. /*
  1192. * Clear out post-eof preallocations because we don't have page cache
  1193. * backing the delayed allocations and they'll never get freed on
  1194. * their own.
  1195. */
  1196. if (xfs_can_free_eofblocks(dest, true)) {
  1197. ret = xfs_free_eofblocks(dest);
  1198. if (ret)
  1199. goto out_unlock;
  1200. }
  1201. /* Set flags and remap blocks. */
  1202. ret = xfs_reflink_set_inode_flag(src, dest);
  1203. if (ret)
  1204. goto out_unlock;
  1205. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1206. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1207. fsblen = XFS_B_TO_FSB(mp, len);
  1208. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1209. pos_out + len);
  1210. if (ret)
  1211. goto out_unlock;
  1212. /* Zap any page cache for the destination file's range. */
  1213. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1214. PAGE_ALIGN(pos_out + len) - 1);
  1215. /*
  1216. * Carry the cowextsize hint from src to dest if we're sharing the
  1217. * entire source file to the entire destination file, the source file
  1218. * has a cowextsize hint, and the destination file does not.
  1219. */
  1220. cowextsize = 0;
  1221. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1222. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1223. pos_out == 0 && len >= i_size_read(inode_out) &&
  1224. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1225. cowextsize = src->i_d.di_cowextsize;
  1226. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1227. is_dedupe);
  1228. out_unlock:
  1229. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1230. if (!same_inode)
  1231. xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
  1232. inode_unlock(inode_out);
  1233. if (!same_inode)
  1234. inode_unlock_shared(inode_in);
  1235. if (ret)
  1236. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1237. return ret;
  1238. }
  1239. /*
  1240. * The user wants to preemptively CoW all shared blocks in this file,
  1241. * which enables us to turn off the reflink flag. Iterate all
  1242. * extents which are not prealloc/delalloc to see which ranges are
  1243. * mentioned in the refcount tree, then read those blocks into the
  1244. * pagecache, dirty them, fsync them back out, and then we can update
  1245. * the inode flag. What happens if we run out of memory? :)
  1246. */
  1247. STATIC int
  1248. xfs_reflink_dirty_extents(
  1249. struct xfs_inode *ip,
  1250. xfs_fileoff_t fbno,
  1251. xfs_filblks_t end,
  1252. xfs_off_t isize)
  1253. {
  1254. struct xfs_mount *mp = ip->i_mount;
  1255. xfs_agnumber_t agno;
  1256. xfs_agblock_t agbno;
  1257. xfs_extlen_t aglen;
  1258. xfs_agblock_t rbno;
  1259. xfs_extlen_t rlen;
  1260. xfs_off_t fpos;
  1261. xfs_off_t flen;
  1262. struct xfs_bmbt_irec map[2];
  1263. int nmaps;
  1264. int error = 0;
  1265. while (end - fbno > 0) {
  1266. nmaps = 1;
  1267. /*
  1268. * Look for extents in the file. Skip holes, delalloc, or
  1269. * unwritten extents; they can't be reflinked.
  1270. */
  1271. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1272. if (error)
  1273. goto out;
  1274. if (nmaps == 0)
  1275. break;
  1276. if (!xfs_bmap_is_real_extent(&map[0]))
  1277. goto next;
  1278. map[1] = map[0];
  1279. while (map[1].br_blockcount) {
  1280. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1281. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1282. aglen = map[1].br_blockcount;
  1283. error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
  1284. aglen, &rbno, &rlen, true);
  1285. if (error)
  1286. goto out;
  1287. if (rbno == NULLAGBLOCK)
  1288. break;
  1289. /* Dirty the pages */
  1290. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1291. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1292. (rbno - agbno));
  1293. flen = XFS_FSB_TO_B(mp, rlen);
  1294. if (fpos + flen > isize)
  1295. flen = isize - fpos;
  1296. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1297. &xfs_iomap_ops);
  1298. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1299. if (error)
  1300. goto out;
  1301. map[1].br_blockcount -= (rbno - agbno + rlen);
  1302. map[1].br_startoff += (rbno - agbno + rlen);
  1303. map[1].br_startblock += (rbno - agbno + rlen);
  1304. }
  1305. next:
  1306. fbno = map[0].br_startoff + map[0].br_blockcount;
  1307. }
  1308. out:
  1309. return error;
  1310. }
  1311. /* Does this inode need the reflink flag? */
  1312. int
  1313. xfs_reflink_inode_has_shared_extents(
  1314. struct xfs_trans *tp,
  1315. struct xfs_inode *ip,
  1316. bool *has_shared)
  1317. {
  1318. struct xfs_bmbt_irec got;
  1319. struct xfs_mount *mp = ip->i_mount;
  1320. struct xfs_ifork *ifp;
  1321. xfs_agnumber_t agno;
  1322. xfs_agblock_t agbno;
  1323. xfs_extlen_t aglen;
  1324. xfs_agblock_t rbno;
  1325. xfs_extlen_t rlen;
  1326. struct xfs_iext_cursor icur;
  1327. bool found;
  1328. int error;
  1329. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1330. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1331. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1332. if (error)
  1333. return error;
  1334. }
  1335. *has_shared = false;
  1336. found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
  1337. while (found) {
  1338. if (isnullstartblock(got.br_startblock) ||
  1339. got.br_state != XFS_EXT_NORM)
  1340. goto next;
  1341. agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
  1342. agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
  1343. aglen = got.br_blockcount;
  1344. error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
  1345. &rbno, &rlen, false);
  1346. if (error)
  1347. return error;
  1348. /* Is there still a shared block here? */
  1349. if (rbno != NULLAGBLOCK) {
  1350. *has_shared = true;
  1351. return 0;
  1352. }
  1353. next:
  1354. found = xfs_iext_next_extent(ifp, &icur, &got);
  1355. }
  1356. return 0;
  1357. }
  1358. /*
  1359. * Clear the inode reflink flag if there are no shared extents.
  1360. *
  1361. * The caller is responsible for joining the inode to the transaction passed in.
  1362. * The inode will be joined to the transaction that is returned to the caller.
  1363. */
  1364. int
  1365. xfs_reflink_clear_inode_flag(
  1366. struct xfs_inode *ip,
  1367. struct xfs_trans **tpp)
  1368. {
  1369. bool needs_flag;
  1370. int error = 0;
  1371. ASSERT(xfs_is_reflink_inode(ip));
  1372. error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
  1373. if (error || needs_flag)
  1374. return error;
  1375. /*
  1376. * We didn't find any shared blocks so turn off the reflink flag.
  1377. * First, get rid of any leftover CoW mappings.
  1378. */
  1379. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1380. if (error)
  1381. return error;
  1382. /* Clear the inode flag. */
  1383. trace_xfs_reflink_unset_inode_flag(ip);
  1384. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1385. xfs_inode_clear_cowblocks_tag(ip);
  1386. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1387. return error;
  1388. }
  1389. /*
  1390. * Clear the inode reflink flag if there are no shared extents and the size
  1391. * hasn't changed.
  1392. */
  1393. STATIC int
  1394. xfs_reflink_try_clear_inode_flag(
  1395. struct xfs_inode *ip)
  1396. {
  1397. struct xfs_mount *mp = ip->i_mount;
  1398. struct xfs_trans *tp;
  1399. int error = 0;
  1400. /* Start a rolling transaction to remove the mappings */
  1401. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1402. if (error)
  1403. return error;
  1404. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1405. xfs_trans_ijoin(tp, ip, 0);
  1406. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1407. if (error)
  1408. goto cancel;
  1409. error = xfs_trans_commit(tp);
  1410. if (error)
  1411. goto out;
  1412. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1413. return 0;
  1414. cancel:
  1415. xfs_trans_cancel(tp);
  1416. out:
  1417. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1418. return error;
  1419. }
  1420. /*
  1421. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1422. * the reflink flag if we unshare all of the file's blocks.
  1423. */
  1424. int
  1425. xfs_reflink_unshare(
  1426. struct xfs_inode *ip,
  1427. xfs_off_t offset,
  1428. xfs_off_t len)
  1429. {
  1430. struct xfs_mount *mp = ip->i_mount;
  1431. xfs_fileoff_t fbno;
  1432. xfs_filblks_t end;
  1433. xfs_off_t isize;
  1434. int error;
  1435. if (!xfs_is_reflink_inode(ip))
  1436. return 0;
  1437. trace_xfs_reflink_unshare(ip, offset, len);
  1438. inode_dio_wait(VFS_I(ip));
  1439. /* Try to CoW the selected ranges */
  1440. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1441. fbno = XFS_B_TO_FSBT(mp, offset);
  1442. isize = i_size_read(VFS_I(ip));
  1443. end = XFS_B_TO_FSB(mp, offset + len);
  1444. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1445. if (error)
  1446. goto out_unlock;
  1447. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1448. /* Wait for the IO to finish */
  1449. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1450. if (error)
  1451. goto out;
  1452. /* Turn off the reflink flag if possible. */
  1453. error = xfs_reflink_try_clear_inode_flag(ip);
  1454. if (error)
  1455. goto out;
  1456. return 0;
  1457. out_unlock:
  1458. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1459. out:
  1460. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1461. return error;
  1462. }