xfs_reflink.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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);
  157. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  158. find_end_of_shared);
  159. xfs_btree_del_cursor(cur, error);
  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. {
  294. int nimaps = 1;
  295. if (imap->br_state == XFS_EXT_NORM)
  296. return 0;
  297. xfs_trim_extent(imap, offset_fsb, count_fsb);
  298. trace_xfs_reflink_convert_cow(ip, imap);
  299. if (imap->br_blockcount == 0)
  300. return 0;
  301. return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
  302. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, 0, imap,
  303. &nimaps);
  304. }
  305. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  306. int
  307. xfs_reflink_convert_cow(
  308. struct xfs_inode *ip,
  309. xfs_off_t offset,
  310. xfs_off_t count)
  311. {
  312. struct xfs_mount *mp = ip->i_mount;
  313. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  314. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  315. xfs_filblks_t count_fsb = end_fsb - offset_fsb;
  316. struct xfs_bmbt_irec imap;
  317. int nimaps = 1, error = 0;
  318. ASSERT(count != 0);
  319. xfs_ilock(ip, XFS_ILOCK_EXCL);
  320. error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
  321. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
  322. XFS_BMAPI_CONVERT_ONLY, 0, &imap, &nimaps);
  323. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  324. return error;
  325. }
  326. /*
  327. * Find the extent that maps the given range in the COW fork. Even if the extent
  328. * is not shared we might have a preallocation for it in the COW fork. If so we
  329. * use it that rather than trigger a new allocation.
  330. */
  331. static int
  332. xfs_find_trim_cow_extent(
  333. struct xfs_inode *ip,
  334. struct xfs_bmbt_irec *imap,
  335. bool *shared,
  336. bool *found)
  337. {
  338. xfs_fileoff_t offset_fsb = imap->br_startoff;
  339. xfs_filblks_t count_fsb = imap->br_blockcount;
  340. struct xfs_iext_cursor icur;
  341. struct xfs_bmbt_irec got;
  342. bool trimmed;
  343. *found = false;
  344. /*
  345. * If we don't find an overlapping extent, trim the range we need to
  346. * allocate to fit the hole we found.
  347. */
  348. if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) ||
  349. got.br_startoff > offset_fsb)
  350. return xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  351. *shared = true;
  352. if (isnullstartblock(got.br_startblock)) {
  353. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  354. return 0;
  355. }
  356. /* real extent found - no need to allocate */
  357. xfs_trim_extent(&got, offset_fsb, count_fsb);
  358. *imap = got;
  359. *found = true;
  360. return 0;
  361. }
  362. /* Allocate all CoW reservations covering a range of blocks in a file. */
  363. int
  364. xfs_reflink_allocate_cow(
  365. struct xfs_inode *ip,
  366. struct xfs_bmbt_irec *imap,
  367. bool *shared,
  368. uint *lockmode)
  369. {
  370. struct xfs_mount *mp = ip->i_mount;
  371. xfs_fileoff_t offset_fsb = imap->br_startoff;
  372. xfs_filblks_t count_fsb = imap->br_blockcount;
  373. struct xfs_trans *tp;
  374. int nimaps, error = 0;
  375. bool found;
  376. xfs_filblks_t resaligned;
  377. xfs_extlen_t resblks = 0;
  378. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  379. ASSERT(xfs_is_reflink_inode(ip));
  380. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  381. if (error || !*shared)
  382. return error;
  383. if (found)
  384. goto convert;
  385. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  386. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  387. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  388. xfs_iunlock(ip, *lockmode);
  389. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  390. *lockmode = XFS_ILOCK_EXCL;
  391. xfs_ilock(ip, *lockmode);
  392. if (error)
  393. return error;
  394. error = xfs_qm_dqattach_locked(ip, false);
  395. if (error)
  396. goto out_trans_cancel;
  397. /*
  398. * Check for an overlapping extent again now that we dropped the ilock.
  399. */
  400. error = xfs_find_trim_cow_extent(ip, imap, shared, &found);
  401. if (error || !*shared)
  402. goto out_trans_cancel;
  403. if (found) {
  404. xfs_trans_cancel(tp);
  405. goto convert;
  406. }
  407. error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
  408. XFS_QMOPT_RES_REGBLKS);
  409. if (error)
  410. goto out_trans_cancel;
  411. xfs_trans_ijoin(tp, ip, 0);
  412. /* Allocate the entire reservation as unwritten blocks. */
  413. nimaps = 1;
  414. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  415. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC,
  416. resblks, imap, &nimaps);
  417. if (error)
  418. goto out_unreserve;
  419. xfs_inode_set_cowblocks_tag(ip);
  420. error = xfs_trans_commit(tp);
  421. if (error)
  422. return error;
  423. /*
  424. * Allocation succeeded but the requested range was not even partially
  425. * satisfied? Bail out!
  426. */
  427. if (nimaps == 0)
  428. return -ENOSPC;
  429. convert:
  430. return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
  431. out_unreserve:
  432. xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
  433. XFS_QMOPT_RES_REGBLKS);
  434. out_trans_cancel:
  435. xfs_trans_cancel(tp);
  436. return error;
  437. }
  438. /*
  439. * Cancel CoW reservations for some block range of an inode.
  440. *
  441. * If cancel_real is true this function cancels all COW fork extents for the
  442. * inode; if cancel_real is false, real extents are not cleared.
  443. *
  444. * Caller must have already joined the inode to the current transaction. The
  445. * inode will be joined to the transaction returned to the caller.
  446. */
  447. int
  448. xfs_reflink_cancel_cow_blocks(
  449. struct xfs_inode *ip,
  450. struct xfs_trans **tpp,
  451. xfs_fileoff_t offset_fsb,
  452. xfs_fileoff_t end_fsb,
  453. bool cancel_real)
  454. {
  455. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  456. struct xfs_bmbt_irec got, del;
  457. struct xfs_iext_cursor icur;
  458. int error = 0;
  459. if (!xfs_inode_has_cow_data(ip))
  460. return 0;
  461. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  462. return 0;
  463. /* Walk backwards until we're out of the I/O range... */
  464. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  465. del = got;
  466. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  467. /* Extent delete may have bumped ext forward */
  468. if (!del.br_blockcount) {
  469. xfs_iext_prev(ifp, &icur);
  470. goto next_extent;
  471. }
  472. trace_xfs_reflink_cancel_cow(ip, &del);
  473. if (isnullstartblock(del.br_startblock)) {
  474. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  475. &icur, &got, &del);
  476. if (error)
  477. break;
  478. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  479. ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
  480. /* Free the CoW orphan record. */
  481. error = xfs_refcount_free_cow_extent(*tpp,
  482. del.br_startblock, del.br_blockcount);
  483. if (error)
  484. break;
  485. xfs_bmap_add_free(*tpp, del.br_startblock,
  486. del.br_blockcount, NULL);
  487. /* Roll the transaction */
  488. error = xfs_defer_finish(tpp);
  489. if (error)
  490. break;
  491. /* Remove the mapping from the CoW fork. */
  492. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  493. /* Remove the quota reservation */
  494. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  495. -(long)del.br_blockcount, 0,
  496. XFS_QMOPT_RES_REGBLKS);
  497. if (error)
  498. break;
  499. } else {
  500. /* Didn't do anything, push cursor back. */
  501. xfs_iext_prev(ifp, &icur);
  502. }
  503. next_extent:
  504. if (!xfs_iext_get_extent(ifp, &icur, &got))
  505. break;
  506. }
  507. /* clear tag if cow fork is emptied */
  508. if (!ifp->if_bytes)
  509. xfs_inode_clear_cowblocks_tag(ip);
  510. return error;
  511. }
  512. /*
  513. * Cancel CoW reservations for some byte range of an inode.
  514. *
  515. * If cancel_real is true this function cancels all COW fork extents for the
  516. * inode; if cancel_real is false, real extents are not cleared.
  517. */
  518. int
  519. xfs_reflink_cancel_cow_range(
  520. struct xfs_inode *ip,
  521. xfs_off_t offset,
  522. xfs_off_t count,
  523. bool cancel_real)
  524. {
  525. struct xfs_trans *tp;
  526. xfs_fileoff_t offset_fsb;
  527. xfs_fileoff_t end_fsb;
  528. int error;
  529. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  530. ASSERT(xfs_is_reflink_inode(ip));
  531. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  532. if (count == NULLFILEOFF)
  533. end_fsb = NULLFILEOFF;
  534. else
  535. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  536. /* Start a rolling transaction to remove the mappings */
  537. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  538. 0, 0, XFS_TRANS_NOFS, &tp);
  539. if (error)
  540. goto out;
  541. xfs_ilock(ip, XFS_ILOCK_EXCL);
  542. xfs_trans_ijoin(tp, ip, 0);
  543. /* Scrape out the old CoW reservations */
  544. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  545. cancel_real);
  546. if (error)
  547. goto out_cancel;
  548. error = xfs_trans_commit(tp);
  549. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  550. return error;
  551. out_cancel:
  552. xfs_trans_cancel(tp);
  553. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  554. out:
  555. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  556. return error;
  557. }
  558. /*
  559. * Remap parts of a file's data fork after a successful CoW.
  560. */
  561. int
  562. xfs_reflink_end_cow(
  563. struct xfs_inode *ip,
  564. xfs_off_t offset,
  565. xfs_off_t count)
  566. {
  567. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  568. struct xfs_bmbt_irec got, del;
  569. struct xfs_trans *tp;
  570. xfs_fileoff_t offset_fsb;
  571. xfs_fileoff_t end_fsb;
  572. int error;
  573. unsigned int resblks;
  574. xfs_filblks_t rlen;
  575. struct xfs_iext_cursor icur;
  576. trace_xfs_reflink_end_cow(ip, offset, count);
  577. /* No COW extents? That's easy! */
  578. if (ifp->if_bytes == 0)
  579. return 0;
  580. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  581. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  582. /*
  583. * Start a rolling transaction to switch the mappings. We're
  584. * unlikely ever to have to remap 16T worth of single-block
  585. * extents, so just cap the worst case extent count to 2^32-1.
  586. * Stick a warning in just in case, and avoid 64-bit division.
  587. */
  588. BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
  589. if (end_fsb - offset_fsb > UINT_MAX) {
  590. error = -EFSCORRUPTED;
  591. xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
  592. ASSERT(0);
  593. goto out;
  594. }
  595. resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
  596. (unsigned int)(end_fsb - offset_fsb),
  597. XFS_DATA_FORK);
  598. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  599. resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  600. if (error)
  601. goto out;
  602. xfs_ilock(ip, XFS_ILOCK_EXCL);
  603. xfs_trans_ijoin(tp, ip, 0);
  604. /*
  605. * In case of racing, overlapping AIO writes no COW extents might be
  606. * left by the time I/O completes for the loser of the race. In that
  607. * case we are done.
  608. */
  609. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  610. goto out_cancel;
  611. /* Walk backwards until we're out of the I/O range... */
  612. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  613. del = got;
  614. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  615. /* Extent delete may have bumped ext forward */
  616. if (!del.br_blockcount)
  617. goto prev_extent;
  618. /*
  619. * Only remap real extent that contain data. With AIO
  620. * speculatively preallocations can leak into the range we
  621. * are called upon, and we need to skip them.
  622. */
  623. if (!xfs_bmap_is_real_extent(&got))
  624. goto prev_extent;
  625. /* Unmap the old blocks in the data fork. */
  626. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  627. rlen = del.br_blockcount;
  628. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
  629. if (error)
  630. goto out_cancel;
  631. /* Trim the extent to whatever got unmapped. */
  632. if (rlen) {
  633. xfs_trim_extent(&del, del.br_startoff + rlen,
  634. del.br_blockcount - rlen);
  635. }
  636. trace_xfs_reflink_cow_remap(ip, &del);
  637. /* Free the CoW orphan record. */
  638. error = xfs_refcount_free_cow_extent(tp, del.br_startblock,
  639. del.br_blockcount);
  640. if (error)
  641. goto out_cancel;
  642. /* Map the new blocks into the data fork. */
  643. error = xfs_bmap_map_extent(tp, ip, &del);
  644. if (error)
  645. goto out_cancel;
  646. /* Charge this new data fork mapping to the on-disk quota. */
  647. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
  648. (long)del.br_blockcount);
  649. /* Remove the mapping from the CoW fork. */
  650. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  651. error = xfs_defer_finish(&tp);
  652. if (error)
  653. goto out_cancel;
  654. if (!xfs_iext_get_extent(ifp, &icur, &got))
  655. break;
  656. continue;
  657. prev_extent:
  658. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  659. break;
  660. }
  661. error = xfs_trans_commit(tp);
  662. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  663. if (error)
  664. goto out;
  665. return 0;
  666. out_cancel:
  667. xfs_trans_cancel(tp);
  668. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  669. out:
  670. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  671. return error;
  672. }
  673. /*
  674. * Free leftover CoW reservations that didn't get cleaned out.
  675. */
  676. int
  677. xfs_reflink_recover_cow(
  678. struct xfs_mount *mp)
  679. {
  680. xfs_agnumber_t agno;
  681. int error = 0;
  682. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  683. return 0;
  684. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  685. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  686. if (error)
  687. break;
  688. }
  689. return error;
  690. }
  691. /*
  692. * Reflinking (Block) Ranges of Two Files Together
  693. *
  694. * First, ensure that the reflink flag is set on both inodes. The flag is an
  695. * optimization to avoid unnecessary refcount btree lookups in the write path.
  696. *
  697. * Now we can iteratively remap the range of extents (and holes) in src to the
  698. * corresponding ranges in dest. Let drange and srange denote the ranges of
  699. * logical blocks in dest and src touched by the reflink operation.
  700. *
  701. * While the length of drange is greater than zero,
  702. * - Read src's bmbt at the start of srange ("imap")
  703. * - If imap doesn't exist, make imap appear to start at the end of srange
  704. * with zero length.
  705. * - If imap starts before srange, advance imap to start at srange.
  706. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  707. * - Punch (imap start - srange start + imap len) blocks from dest at
  708. * offset (drange start).
  709. * - If imap points to a real range of pblks,
  710. * > Increase the refcount of the imap's pblks
  711. * > Map imap's pblks into dest at the offset
  712. * (drange start + imap start - srange start)
  713. * - Advance drange and srange by (imap start - srange start + imap len)
  714. *
  715. * Finally, if the reflink made dest longer, update both the in-core and
  716. * on-disk file sizes.
  717. *
  718. * ASCII Art Demonstration:
  719. *
  720. * Let's say we want to reflink this source file:
  721. *
  722. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  723. * <-------------------->
  724. *
  725. * into this destination file:
  726. *
  727. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  728. * <-------------------->
  729. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  730. * Observe that the range has different logical offsets in either file.
  731. *
  732. * Consider that the first extent in the source file doesn't line up with our
  733. * reflink range. Unmapping and remapping are separate operations, so we can
  734. * unmap more blocks from the destination file than we remap.
  735. *
  736. * ----SSSSSSS-SSSSS----SSSSSS
  737. * <------->
  738. * --DDDDD---------DDDDD--DDD
  739. * <------->
  740. *
  741. * Now remap the source extent into the destination file:
  742. *
  743. * ----SSSSSSS-SSSSS----SSSSSS
  744. * <------->
  745. * --DDDDD--SSSSSSSDDDDD--DDD
  746. * <------->
  747. *
  748. * Do likewise with the second hole and extent in our range. Holes in the
  749. * unmap range don't affect our operation.
  750. *
  751. * ----SSSSSSS-SSSSS----SSSSSS
  752. * <---->
  753. * --DDDDD--SSSSSSS-SSSSS-DDD
  754. * <---->
  755. *
  756. * Finally, unmap and remap part of the third extent. This will increase the
  757. * size of the destination file.
  758. *
  759. * ----SSSSSSS-SSSSS----SSSSSS
  760. * <----->
  761. * --DDDDD--SSSSSSS-SSSSS----SSS
  762. * <----->
  763. *
  764. * Once we update the destination file's i_size, we're done.
  765. */
  766. /*
  767. * Ensure the reflink bit is set in both inodes.
  768. */
  769. STATIC int
  770. xfs_reflink_set_inode_flag(
  771. struct xfs_inode *src,
  772. struct xfs_inode *dest)
  773. {
  774. struct xfs_mount *mp = src->i_mount;
  775. int error;
  776. struct xfs_trans *tp;
  777. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  778. return 0;
  779. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  780. if (error)
  781. goto out_error;
  782. /* Lock both files against IO */
  783. if (src->i_ino == dest->i_ino)
  784. xfs_ilock(src, XFS_ILOCK_EXCL);
  785. else
  786. xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
  787. if (!xfs_is_reflink_inode(src)) {
  788. trace_xfs_reflink_set_inode_flag(src);
  789. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  790. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  791. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  792. xfs_ifork_init_cow(src);
  793. } else
  794. xfs_iunlock(src, XFS_ILOCK_EXCL);
  795. if (src->i_ino == dest->i_ino)
  796. goto commit_flags;
  797. if (!xfs_is_reflink_inode(dest)) {
  798. trace_xfs_reflink_set_inode_flag(dest);
  799. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  800. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  801. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  802. xfs_ifork_init_cow(dest);
  803. } else
  804. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  805. commit_flags:
  806. error = xfs_trans_commit(tp);
  807. if (error)
  808. goto out_error;
  809. return error;
  810. out_error:
  811. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  812. return error;
  813. }
  814. /*
  815. * Update destination inode size & cowextsize hint, if necessary.
  816. */
  817. STATIC int
  818. xfs_reflink_update_dest(
  819. struct xfs_inode *dest,
  820. xfs_off_t newlen,
  821. xfs_extlen_t cowextsize,
  822. bool is_dedupe)
  823. {
  824. struct xfs_mount *mp = dest->i_mount;
  825. struct xfs_trans *tp;
  826. int error;
  827. if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  828. return 0;
  829. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  830. if (error)
  831. goto out_error;
  832. xfs_ilock(dest, XFS_ILOCK_EXCL);
  833. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  834. if (newlen > i_size_read(VFS_I(dest))) {
  835. trace_xfs_reflink_update_inode_size(dest, newlen);
  836. i_size_write(VFS_I(dest), newlen);
  837. dest->i_d.di_size = newlen;
  838. }
  839. if (cowextsize) {
  840. dest->i_d.di_cowextsize = cowextsize;
  841. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  842. }
  843. if (!is_dedupe) {
  844. xfs_trans_ichgtime(tp, dest,
  845. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  846. }
  847. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  848. error = xfs_trans_commit(tp);
  849. if (error)
  850. goto out_error;
  851. return error;
  852. out_error:
  853. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  854. return error;
  855. }
  856. /*
  857. * Do we have enough reserve in this AG to handle a reflink? The refcount
  858. * btree already reserved all the space it needs, but the rmap btree can grow
  859. * infinitely, so we won't allow more reflinks when the AG is down to the
  860. * btree reserves.
  861. */
  862. static int
  863. xfs_reflink_ag_has_free_space(
  864. struct xfs_mount *mp,
  865. xfs_agnumber_t agno)
  866. {
  867. struct xfs_perag *pag;
  868. int error = 0;
  869. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  870. return 0;
  871. pag = xfs_perag_get(mp, agno);
  872. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
  873. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  874. error = -ENOSPC;
  875. xfs_perag_put(pag);
  876. return error;
  877. }
  878. /*
  879. * Unmap a range of blocks from a file, then map other blocks into the hole.
  880. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  881. * The extent irec is mapped into dest at irec->br_startoff.
  882. */
  883. STATIC int
  884. xfs_reflink_remap_extent(
  885. struct xfs_inode *ip,
  886. struct xfs_bmbt_irec *irec,
  887. xfs_fileoff_t destoff,
  888. xfs_off_t new_isize)
  889. {
  890. struct xfs_mount *mp = ip->i_mount;
  891. bool real_extent = xfs_bmap_is_real_extent(irec);
  892. struct xfs_trans *tp;
  893. unsigned int resblks;
  894. struct xfs_bmbt_irec uirec;
  895. xfs_filblks_t rlen;
  896. xfs_filblks_t unmap_len;
  897. xfs_off_t newlen;
  898. int error;
  899. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  900. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  901. /* No reflinking if we're low on space */
  902. if (real_extent) {
  903. error = xfs_reflink_ag_has_free_space(mp,
  904. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  905. if (error)
  906. goto out;
  907. }
  908. /* Start a rolling transaction to switch the mappings */
  909. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  910. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  911. if (error)
  912. goto out;
  913. xfs_ilock(ip, XFS_ILOCK_EXCL);
  914. xfs_trans_ijoin(tp, ip, 0);
  915. /* If we're not just clearing space, then do we have enough quota? */
  916. if (real_extent) {
  917. error = xfs_trans_reserve_quota_nblks(tp, ip,
  918. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  919. if (error)
  920. goto out_cancel;
  921. }
  922. trace_xfs_reflink_remap(ip, irec->br_startoff,
  923. irec->br_blockcount, irec->br_startblock);
  924. /* Unmap the old blocks in the data fork. */
  925. rlen = unmap_len;
  926. while (rlen) {
  927. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  928. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
  929. if (error)
  930. goto out_cancel;
  931. /*
  932. * Trim the extent to whatever got unmapped.
  933. * Remember, bunmapi works backwards.
  934. */
  935. uirec.br_startblock = irec->br_startblock + rlen;
  936. uirec.br_startoff = irec->br_startoff + rlen;
  937. uirec.br_blockcount = unmap_len - rlen;
  938. unmap_len = rlen;
  939. /* If this isn't a real mapping, we're done. */
  940. if (!real_extent || uirec.br_blockcount == 0)
  941. goto next_extent;
  942. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  943. uirec.br_blockcount, uirec.br_startblock);
  944. /* Update the refcount tree */
  945. error = xfs_refcount_increase_extent(tp, &uirec);
  946. if (error)
  947. goto out_cancel;
  948. /* Map the new blocks into the data fork. */
  949. error = xfs_bmap_map_extent(tp, ip, &uirec);
  950. if (error)
  951. goto out_cancel;
  952. /* Update quota accounting. */
  953. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  954. uirec.br_blockcount);
  955. /* Update dest isize if needed. */
  956. newlen = XFS_FSB_TO_B(mp,
  957. uirec.br_startoff + uirec.br_blockcount);
  958. newlen = min_t(xfs_off_t, newlen, new_isize);
  959. if (newlen > i_size_read(VFS_I(ip))) {
  960. trace_xfs_reflink_update_inode_size(ip, newlen);
  961. i_size_write(VFS_I(ip), newlen);
  962. ip->i_d.di_size = newlen;
  963. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  964. }
  965. next_extent:
  966. /* Process all the deferred stuff. */
  967. error = xfs_defer_finish(&tp);
  968. if (error)
  969. goto out_cancel;
  970. }
  971. error = xfs_trans_commit(tp);
  972. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  973. if (error)
  974. goto out;
  975. return 0;
  976. out_cancel:
  977. xfs_trans_cancel(tp);
  978. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  979. out:
  980. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  981. return error;
  982. }
  983. /*
  984. * Iteratively remap one file's extents (and holes) to another's.
  985. */
  986. STATIC int
  987. xfs_reflink_remap_blocks(
  988. struct xfs_inode *src,
  989. xfs_fileoff_t srcoff,
  990. struct xfs_inode *dest,
  991. xfs_fileoff_t destoff,
  992. xfs_filblks_t len,
  993. xfs_off_t new_isize)
  994. {
  995. struct xfs_bmbt_irec imap;
  996. int nimaps;
  997. int error = 0;
  998. xfs_filblks_t range_len;
  999. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1000. while (len) {
  1001. uint lock_mode;
  1002. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1003. dest, destoff);
  1004. /* Read extent from the source file */
  1005. nimaps = 1;
  1006. lock_mode = xfs_ilock_data_map_shared(src);
  1007. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1008. xfs_iunlock(src, lock_mode);
  1009. if (error)
  1010. goto err;
  1011. ASSERT(nimaps == 1);
  1012. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1013. &imap);
  1014. /* Translate imap into the destination file. */
  1015. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1016. imap.br_startoff += destoff - srcoff;
  1017. /* Clear dest from destoff to the end of imap and map it in. */
  1018. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1019. new_isize);
  1020. if (error)
  1021. goto err;
  1022. if (fatal_signal_pending(current)) {
  1023. error = -EINTR;
  1024. goto err;
  1025. }
  1026. /* Advance drange/srange */
  1027. srcoff += range_len;
  1028. destoff += range_len;
  1029. len -= range_len;
  1030. }
  1031. return 0;
  1032. err:
  1033. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1034. return error;
  1035. }
  1036. /*
  1037. * Grab the exclusive iolock for a data copy from src to dest, making
  1038. * sure to abide vfs locking order (lowest pointer value goes first) and
  1039. * breaking the pnfs layout leases on dest before proceeding. The loop
  1040. * is needed because we cannot call the blocking break_layout() with the
  1041. * src iolock held, and therefore have to back out both locks.
  1042. */
  1043. static int
  1044. xfs_iolock_two_inodes_and_break_layout(
  1045. struct inode *src,
  1046. struct inode *dest)
  1047. {
  1048. int error;
  1049. retry:
  1050. if (src < dest) {
  1051. inode_lock_shared(src);
  1052. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  1053. } else {
  1054. /* src >= dest */
  1055. inode_lock(dest);
  1056. }
  1057. error = break_layout(dest, false);
  1058. if (error == -EWOULDBLOCK) {
  1059. inode_unlock(dest);
  1060. if (src < dest)
  1061. inode_unlock_shared(src);
  1062. error = break_layout(dest, true);
  1063. if (error)
  1064. return error;
  1065. goto retry;
  1066. }
  1067. if (error) {
  1068. inode_unlock(dest);
  1069. if (src < dest)
  1070. inode_unlock_shared(src);
  1071. return error;
  1072. }
  1073. if (src > dest)
  1074. inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
  1075. return 0;
  1076. }
  1077. /* Unlock both inodes after they've been prepped for a range clone. */
  1078. STATIC void
  1079. xfs_reflink_remap_unlock(
  1080. struct file *file_in,
  1081. struct file *file_out)
  1082. {
  1083. struct inode *inode_in = file_inode(file_in);
  1084. struct xfs_inode *src = XFS_I(inode_in);
  1085. struct inode *inode_out = file_inode(file_out);
  1086. struct xfs_inode *dest = XFS_I(inode_out);
  1087. bool same_inode = (inode_in == inode_out);
  1088. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1089. if (!same_inode)
  1090. xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
  1091. inode_unlock(inode_out);
  1092. if (!same_inode)
  1093. inode_unlock_shared(inode_in);
  1094. }
  1095. /*
  1096. * If we're reflinking to a point past the destination file's EOF, we must
  1097. * zero any speculative post-EOF preallocations that sit between the old EOF
  1098. * and the destination file offset.
  1099. */
  1100. static int
  1101. xfs_reflink_zero_posteof(
  1102. struct xfs_inode *ip,
  1103. loff_t pos)
  1104. {
  1105. loff_t isize = i_size_read(VFS_I(ip));
  1106. if (pos <= isize)
  1107. return 0;
  1108. trace_xfs_zero_eof(ip, isize, pos - isize);
  1109. return iomap_zero_range(VFS_I(ip), isize, pos - isize, NULL,
  1110. &xfs_iomap_ops);
  1111. }
  1112. /*
  1113. * Prepare two files for range cloning. Upon a successful return both inodes
  1114. * will have the iolock and mmaplock held, the page cache of the out file
  1115. * will be truncated, and any leases on the out file will have been broken.
  1116. * This function borrows heavily from xfs_file_aio_write_checks.
  1117. *
  1118. * The VFS allows partial EOF blocks to "match" for dedupe even though it hasn't
  1119. * checked that the bytes beyond EOF physically match. Hence we cannot use the
  1120. * EOF block in the source dedupe range because it's not a complete block match,
  1121. * hence can introduce a corruption into the file that has it's
  1122. * block replaced.
  1123. *
  1124. * Despite this issue, we still need to report that range as successfully
  1125. * deduped to avoid confusing userspace with EINVAL errors on completely
  1126. * matching file data. The only time that an unaligned length will be passed to
  1127. * us is when it spans the EOF block of the source file, so if we simply mask it
  1128. * down to be block aligned here the we will dedupe everything but that partial
  1129. * EOF block.
  1130. */
  1131. STATIC int
  1132. xfs_reflink_remap_prep(
  1133. struct file *file_in,
  1134. loff_t pos_in,
  1135. struct file *file_out,
  1136. loff_t pos_out,
  1137. u64 *len,
  1138. bool is_dedupe)
  1139. {
  1140. struct inode *inode_in = file_inode(file_in);
  1141. struct xfs_inode *src = XFS_I(inode_in);
  1142. struct inode *inode_out = file_inode(file_out);
  1143. struct xfs_inode *dest = XFS_I(inode_out);
  1144. bool same_inode = (inode_in == inode_out);
  1145. ssize_t ret;
  1146. /* Lock both files against IO */
  1147. ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
  1148. if (ret)
  1149. return ret;
  1150. if (same_inode)
  1151. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1152. else
  1153. xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
  1154. XFS_MMAPLOCK_EXCL);
  1155. /* Check file eligibility and prepare for block sharing. */
  1156. ret = -EINVAL;
  1157. /* Don't reflink realtime inodes */
  1158. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1159. goto out_unlock;
  1160. /* Don't share DAX file data for now. */
  1161. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1162. goto out_unlock;
  1163. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1164. len, is_dedupe);
  1165. if (ret <= 0)
  1166. goto out_unlock;
  1167. /*
  1168. * If the dedupe data matches, chop off the partial EOF block
  1169. * from the source file so we don't try to dedupe the partial
  1170. * EOF block.
  1171. */
  1172. if (is_dedupe)
  1173. *len &= ~((u64)i_blocksize(inode_in) - 1);
  1174. /* Attach dquots to dest inode before changing block map */
  1175. ret = xfs_qm_dqattach(dest);
  1176. if (ret)
  1177. goto out_unlock;
  1178. /*
  1179. * Zero existing post-eof speculative preallocations in the destination
  1180. * file.
  1181. */
  1182. ret = xfs_reflink_zero_posteof(dest, pos_out);
  1183. if (ret)
  1184. goto out_unlock;
  1185. /* Set flags and remap blocks. */
  1186. ret = xfs_reflink_set_inode_flag(src, dest);
  1187. if (ret)
  1188. goto out_unlock;
  1189. /* Zap any page cache for the destination file's range. */
  1190. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1191. PAGE_ALIGN(pos_out + *len) - 1);
  1192. /* If we're altering the file contents... */
  1193. if (!is_dedupe) {
  1194. /*
  1195. * ...update the timestamps (which will grab the ilock again
  1196. * from xfs_fs_dirty_inode, so we have to call it before we
  1197. * take the ilock).
  1198. */
  1199. if (!(file_out->f_mode & FMODE_NOCMTIME)) {
  1200. ret = file_update_time(file_out);
  1201. if (ret)
  1202. goto out_unlock;
  1203. }
  1204. /*
  1205. * ...clear the security bits if the process is not being run
  1206. * by root. This keeps people from modifying setuid and setgid
  1207. * binaries.
  1208. */
  1209. ret = file_remove_privs(file_out);
  1210. if (ret)
  1211. goto out_unlock;
  1212. }
  1213. return 1;
  1214. out_unlock:
  1215. xfs_reflink_remap_unlock(file_in, file_out);
  1216. return ret;
  1217. }
  1218. /*
  1219. * Link a range of blocks from one file to another.
  1220. */
  1221. int
  1222. xfs_reflink_remap_range(
  1223. struct file *file_in,
  1224. loff_t pos_in,
  1225. struct file *file_out,
  1226. loff_t pos_out,
  1227. u64 len,
  1228. bool is_dedupe)
  1229. {
  1230. struct inode *inode_in = file_inode(file_in);
  1231. struct xfs_inode *src = XFS_I(inode_in);
  1232. struct inode *inode_out = file_inode(file_out);
  1233. struct xfs_inode *dest = XFS_I(inode_out);
  1234. struct xfs_mount *mp = src->i_mount;
  1235. xfs_fileoff_t sfsbno, dfsbno;
  1236. xfs_filblks_t fsblen;
  1237. xfs_extlen_t cowextsize;
  1238. ssize_t ret;
  1239. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1240. return -EOPNOTSUPP;
  1241. if (XFS_FORCED_SHUTDOWN(mp))
  1242. return -EIO;
  1243. /* Prepare and then clone file data. */
  1244. ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
  1245. &len, is_dedupe);
  1246. if (ret <= 0)
  1247. return ret;
  1248. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1249. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1250. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1251. fsblen = XFS_B_TO_FSB(mp, len);
  1252. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1253. pos_out + len);
  1254. if (ret)
  1255. goto out_unlock;
  1256. /*
  1257. * Carry the cowextsize hint from src to dest if we're sharing the
  1258. * entire source file to the entire destination file, the source file
  1259. * has a cowextsize hint, and the destination file does not.
  1260. */
  1261. cowextsize = 0;
  1262. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1263. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1264. pos_out == 0 && len >= i_size_read(inode_out) &&
  1265. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1266. cowextsize = src->i_d.di_cowextsize;
  1267. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1268. is_dedupe);
  1269. out_unlock:
  1270. xfs_reflink_remap_unlock(file_in, file_out);
  1271. if (ret)
  1272. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1273. return ret;
  1274. }
  1275. /*
  1276. * The user wants to preemptively CoW all shared blocks in this file,
  1277. * which enables us to turn off the reflink flag. Iterate all
  1278. * extents which are not prealloc/delalloc to see which ranges are
  1279. * mentioned in the refcount tree, then read those blocks into the
  1280. * pagecache, dirty them, fsync them back out, and then we can update
  1281. * the inode flag. What happens if we run out of memory? :)
  1282. */
  1283. STATIC int
  1284. xfs_reflink_dirty_extents(
  1285. struct xfs_inode *ip,
  1286. xfs_fileoff_t fbno,
  1287. xfs_filblks_t end,
  1288. xfs_off_t isize)
  1289. {
  1290. struct xfs_mount *mp = ip->i_mount;
  1291. xfs_agnumber_t agno;
  1292. xfs_agblock_t agbno;
  1293. xfs_extlen_t aglen;
  1294. xfs_agblock_t rbno;
  1295. xfs_extlen_t rlen;
  1296. xfs_off_t fpos;
  1297. xfs_off_t flen;
  1298. struct xfs_bmbt_irec map[2];
  1299. int nmaps;
  1300. int error = 0;
  1301. while (end - fbno > 0) {
  1302. nmaps = 1;
  1303. /*
  1304. * Look for extents in the file. Skip holes, delalloc, or
  1305. * unwritten extents; they can't be reflinked.
  1306. */
  1307. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1308. if (error)
  1309. goto out;
  1310. if (nmaps == 0)
  1311. break;
  1312. if (!xfs_bmap_is_real_extent(&map[0]))
  1313. goto next;
  1314. map[1] = map[0];
  1315. while (map[1].br_blockcount) {
  1316. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1317. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1318. aglen = map[1].br_blockcount;
  1319. error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
  1320. aglen, &rbno, &rlen, true);
  1321. if (error)
  1322. goto out;
  1323. if (rbno == NULLAGBLOCK)
  1324. break;
  1325. /* Dirty the pages */
  1326. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1327. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1328. (rbno - agbno));
  1329. flen = XFS_FSB_TO_B(mp, rlen);
  1330. if (fpos + flen > isize)
  1331. flen = isize - fpos;
  1332. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1333. &xfs_iomap_ops);
  1334. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1335. if (error)
  1336. goto out;
  1337. map[1].br_blockcount -= (rbno - agbno + rlen);
  1338. map[1].br_startoff += (rbno - agbno + rlen);
  1339. map[1].br_startblock += (rbno - agbno + rlen);
  1340. }
  1341. next:
  1342. fbno = map[0].br_startoff + map[0].br_blockcount;
  1343. }
  1344. out:
  1345. return error;
  1346. }
  1347. /* Does this inode need the reflink flag? */
  1348. int
  1349. xfs_reflink_inode_has_shared_extents(
  1350. struct xfs_trans *tp,
  1351. struct xfs_inode *ip,
  1352. bool *has_shared)
  1353. {
  1354. struct xfs_bmbt_irec got;
  1355. struct xfs_mount *mp = ip->i_mount;
  1356. struct xfs_ifork *ifp;
  1357. xfs_agnumber_t agno;
  1358. xfs_agblock_t agbno;
  1359. xfs_extlen_t aglen;
  1360. xfs_agblock_t rbno;
  1361. xfs_extlen_t rlen;
  1362. struct xfs_iext_cursor icur;
  1363. bool found;
  1364. int error;
  1365. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1366. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1367. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1368. if (error)
  1369. return error;
  1370. }
  1371. *has_shared = false;
  1372. found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
  1373. while (found) {
  1374. if (isnullstartblock(got.br_startblock) ||
  1375. got.br_state != XFS_EXT_NORM)
  1376. goto next;
  1377. agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
  1378. agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
  1379. aglen = got.br_blockcount;
  1380. error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
  1381. &rbno, &rlen, false);
  1382. if (error)
  1383. return error;
  1384. /* Is there still a shared block here? */
  1385. if (rbno != NULLAGBLOCK) {
  1386. *has_shared = true;
  1387. return 0;
  1388. }
  1389. next:
  1390. found = xfs_iext_next_extent(ifp, &icur, &got);
  1391. }
  1392. return 0;
  1393. }
  1394. /*
  1395. * Clear the inode reflink flag if there are no shared extents.
  1396. *
  1397. * The caller is responsible for joining the inode to the transaction passed in.
  1398. * The inode will be joined to the transaction that is returned to the caller.
  1399. */
  1400. int
  1401. xfs_reflink_clear_inode_flag(
  1402. struct xfs_inode *ip,
  1403. struct xfs_trans **tpp)
  1404. {
  1405. bool needs_flag;
  1406. int error = 0;
  1407. ASSERT(xfs_is_reflink_inode(ip));
  1408. error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
  1409. if (error || needs_flag)
  1410. return error;
  1411. /*
  1412. * We didn't find any shared blocks so turn off the reflink flag.
  1413. * First, get rid of any leftover CoW mappings.
  1414. */
  1415. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1416. if (error)
  1417. return error;
  1418. /* Clear the inode flag. */
  1419. trace_xfs_reflink_unset_inode_flag(ip);
  1420. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1421. xfs_inode_clear_cowblocks_tag(ip);
  1422. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1423. return error;
  1424. }
  1425. /*
  1426. * Clear the inode reflink flag if there are no shared extents and the size
  1427. * hasn't changed.
  1428. */
  1429. STATIC int
  1430. xfs_reflink_try_clear_inode_flag(
  1431. struct xfs_inode *ip)
  1432. {
  1433. struct xfs_mount *mp = ip->i_mount;
  1434. struct xfs_trans *tp;
  1435. int error = 0;
  1436. /* Start a rolling transaction to remove the mappings */
  1437. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1438. if (error)
  1439. return error;
  1440. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1441. xfs_trans_ijoin(tp, ip, 0);
  1442. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1443. if (error)
  1444. goto cancel;
  1445. error = xfs_trans_commit(tp);
  1446. if (error)
  1447. goto out;
  1448. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1449. return 0;
  1450. cancel:
  1451. xfs_trans_cancel(tp);
  1452. out:
  1453. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1454. return error;
  1455. }
  1456. /*
  1457. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1458. * the reflink flag if we unshare all of the file's blocks.
  1459. */
  1460. int
  1461. xfs_reflink_unshare(
  1462. struct xfs_inode *ip,
  1463. xfs_off_t offset,
  1464. xfs_off_t len)
  1465. {
  1466. struct xfs_mount *mp = ip->i_mount;
  1467. xfs_fileoff_t fbno;
  1468. xfs_filblks_t end;
  1469. xfs_off_t isize;
  1470. int error;
  1471. if (!xfs_is_reflink_inode(ip))
  1472. return 0;
  1473. trace_xfs_reflink_unshare(ip, offset, len);
  1474. inode_dio_wait(VFS_I(ip));
  1475. /* Try to CoW the selected ranges */
  1476. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1477. fbno = XFS_B_TO_FSBT(mp, offset);
  1478. isize = i_size_read(VFS_I(ip));
  1479. end = XFS_B_TO_FSB(mp, offset + len);
  1480. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1481. if (error)
  1482. goto out_unlock;
  1483. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1484. /* Wait for the IO to finish */
  1485. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1486. if (error)
  1487. goto out;
  1488. /* Turn off the reflink flag if possible. */
  1489. error = xfs_reflink_try_clear_inode_flag(ip);
  1490. if (error)
  1491. goto out;
  1492. return 0;
  1493. out_unlock:
  1494. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1495. out:
  1496. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1497. return error;
  1498. }