xfs_reflink.c 48 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_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_bmap_util.h"
  18. #include "xfs_trace.h"
  19. #include "xfs_icache.h"
  20. #include "xfs_btree.h"
  21. #include "xfs_refcount_btree.h"
  22. #include "xfs_refcount.h"
  23. #include "xfs_bmap_btree.h"
  24. #include "xfs_trans_space.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_reflink.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_ag.h"
  31. #include "xfs_ag_resv.h"
  32. #include "xfs_health.h"
  33. /*
  34. * Copy on Write of Shared Blocks
  35. *
  36. * XFS must preserve "the usual" file semantics even when two files share
  37. * the same physical blocks. This means that a write to one file must not
  38. * alter the blocks in a different file; the way that we'll do that is
  39. * through the use of a copy-on-write mechanism. At a high level, that
  40. * means that when we want to write to a shared block, we allocate a new
  41. * block, write the data to the new block, and if that succeeds we map the
  42. * new block into the file.
  43. *
  44. * XFS provides a "delayed allocation" mechanism that defers the allocation
  45. * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
  46. * possible. This reduces fragmentation by enabling the filesystem to ask
  47. * for bigger chunks less often, which is exactly what we want for CoW.
  48. *
  49. * The delalloc mechanism begins when the kernel wants to make a block
  50. * writable (write_begin or page_mkwrite). If the offset is not mapped, we
  51. * create a delalloc mapping, which is a regular in-core extent, but without
  52. * a real startblock. (For delalloc mappings, the startblock encodes both
  53. * a flag that this is a delalloc mapping, and a worst-case estimate of how
  54. * many blocks might be required to put the mapping into the BMBT.) delalloc
  55. * mappings are a reservation against the free space in the filesystem;
  56. * adjacent mappings can also be combined into fewer larger mappings.
  57. *
  58. * As an optimization, the CoW extent size hint (cowextsz) creates
  59. * outsized aligned delalloc reservations in the hope of landing out of
  60. * order nearby CoW writes in a single extent on disk, thereby reducing
  61. * fragmentation and improving future performance.
  62. *
  63. * D: --RRRRRRSSSRRRRRRRR--- (data fork)
  64. * C: ------DDDDDDD--------- (CoW fork)
  65. *
  66. * When dirty pages are being written out (typically in writepage), the
  67. * delalloc reservations are converted into unwritten mappings by
  68. * allocating blocks and replacing the delalloc mapping with real ones.
  69. * A delalloc mapping can be replaced by several unwritten ones if the
  70. * free space is fragmented.
  71. *
  72. * D: --RRRRRRSSSRRRRRRRR---
  73. * C: ------UUUUUUU---------
  74. *
  75. * We want to adapt the delalloc mechanism for copy-on-write, since the
  76. * write paths are similar. The first two steps (creating the reservation
  77. * and allocating the blocks) are exactly the same as delalloc except that
  78. * the mappings must be stored in a separate CoW fork because we do not want
  79. * to disturb the mapping in the data fork until we're sure that the write
  80. * succeeded. IO completion in this case is the process of removing the old
  81. * mapping from the data fork and moving the new mapping from the CoW fork to
  82. * the data fork. This will be discussed shortly.
  83. *
  84. * For now, unaligned directio writes will be bounced back to the page cache.
  85. * Block-aligned directio writes will use the same mechanism as buffered
  86. * writes.
  87. *
  88. * Just prior to submitting the actual disk write requests, we convert
  89. * the extents representing the range of the file actually being written
  90. * (as opposed to extra pieces created for the cowextsize hint) to real
  91. * extents. This will become important in the next step:
  92. *
  93. * D: --RRRRRRSSSRRRRRRRR---
  94. * C: ------UUrrUUU---------
  95. *
  96. * CoW remapping must be done after the data block write completes,
  97. * because we don't want to destroy the old data fork map until we're sure
  98. * the new block has been written. Since the new mappings are kept in a
  99. * separate fork, we can simply iterate these mappings to find the ones
  100. * that cover the file blocks that we just CoW'd. For each extent, simply
  101. * unmap the corresponding range in the data fork, map the new range into
  102. * the data fork, and remove the extent from the CoW fork. Because of
  103. * the presence of the cowextsize hint, however, we must be careful
  104. * only to remap the blocks that we've actually written out -- we must
  105. * never remap delalloc reservations nor CoW staging blocks that have
  106. * yet to be written. This corresponds exactly to the real extents in
  107. * the CoW fork:
  108. *
  109. * D: --RRRRRRrrSRRRRRRRR---
  110. * C: ------UU--UUU---------
  111. *
  112. * Since the remapping operation can be applied to an arbitrary file
  113. * range, we record the need for the remap step as a flag in the ioend
  114. * instead of declaring a new IO type. This is required for direct io
  115. * because we only have ioend for the whole dio, and we have to be able to
  116. * remember the presence of unwritten blocks and CoW blocks with a single
  117. * ioend structure. Better yet, the more ground we can cover with one
  118. * ioend, the better.
  119. */
  120. /*
  121. * Given an AG extent, find the lowest-numbered run of shared blocks
  122. * within that range and return the range in fbno/flen. If
  123. * find_end_of_shared is true, return the longest contiguous extent of
  124. * shared blocks. If there are no shared extents, fbno and flen will
  125. * be set to NULLAGBLOCK and 0, respectively.
  126. */
  127. static int
  128. xfs_reflink_find_shared(
  129. struct xfs_perag *pag,
  130. struct xfs_trans *tp,
  131. xfs_agblock_t agbno,
  132. xfs_extlen_t aglen,
  133. xfs_agblock_t *fbno,
  134. xfs_extlen_t *flen,
  135. bool find_end_of_shared)
  136. {
  137. struct xfs_buf *agbp;
  138. struct xfs_btree_cur *cur;
  139. int error;
  140. error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
  141. if (error)
  142. return error;
  143. cur = xfs_refcountbt_init_cursor(pag->pag_mount, tp, agbp, pag);
  144. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  145. find_end_of_shared);
  146. xfs_btree_del_cursor(cur, error);
  147. xfs_trans_brelse(tp, agbp);
  148. return error;
  149. }
  150. /*
  151. * Trim the mapping to the next block where there's a change in the
  152. * shared/unshared status. More specifically, this means that we
  153. * find the lowest-numbered extent of shared blocks that coincides with
  154. * the given block mapping. If the shared extent overlaps the start of
  155. * the mapping, trim the mapping to the end of the shared extent. If
  156. * the shared region intersects the mapping, trim the mapping to the
  157. * start of the shared extent. If there are no shared regions that
  158. * overlap, just return the original extent.
  159. */
  160. int
  161. xfs_reflink_trim_around_shared(
  162. struct xfs_inode *ip,
  163. struct xfs_bmbt_irec *irec,
  164. bool *shared)
  165. {
  166. struct xfs_mount *mp = ip->i_mount;
  167. struct xfs_perag *pag;
  168. xfs_agblock_t agbno;
  169. xfs_extlen_t aglen;
  170. xfs_agblock_t fbno;
  171. xfs_extlen_t flen;
  172. int error = 0;
  173. /* Holes, unwritten, and delalloc extents cannot be shared */
  174. if (!xfs_is_cow_inode(ip) || !xfs_bmap_is_written_extent(irec)) {
  175. *shared = false;
  176. return 0;
  177. }
  178. trace_xfs_reflink_trim_around_shared(ip, irec);
  179. pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  180. agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
  181. aglen = irec->br_blockcount;
  182. error = xfs_reflink_find_shared(pag, NULL, agbno, aglen, &fbno, &flen,
  183. true);
  184. xfs_perag_put(pag);
  185. if (error)
  186. return error;
  187. *shared = false;
  188. if (fbno == NULLAGBLOCK) {
  189. /* No shared blocks at all. */
  190. return 0;
  191. }
  192. if (fbno == agbno) {
  193. /*
  194. * The start of this extent is shared. Truncate the
  195. * mapping at the end of the shared region so that a
  196. * subsequent iteration starts at the start of the
  197. * unshared region.
  198. */
  199. irec->br_blockcount = flen;
  200. *shared = true;
  201. return 0;
  202. }
  203. /*
  204. * There's a shared extent midway through this extent.
  205. * Truncate the mapping at the start of the shared
  206. * extent so that a subsequent iteration starts at the
  207. * start of the shared region.
  208. */
  209. irec->br_blockcount = fbno - agbno;
  210. return 0;
  211. }
  212. int
  213. xfs_bmap_trim_cow(
  214. struct xfs_inode *ip,
  215. struct xfs_bmbt_irec *imap,
  216. bool *shared)
  217. {
  218. /* We can't update any real extents in always COW mode. */
  219. if (xfs_is_always_cow_inode(ip) &&
  220. !isnullstartblock(imap->br_startblock)) {
  221. *shared = true;
  222. return 0;
  223. }
  224. /* Trim the mapping to the nearest shared extent boundary. */
  225. return xfs_reflink_trim_around_shared(ip, imap, shared);
  226. }
  227. static int
  228. xfs_reflink_convert_cow_locked(
  229. struct xfs_inode *ip,
  230. xfs_fileoff_t offset_fsb,
  231. xfs_filblks_t count_fsb)
  232. {
  233. struct xfs_iext_cursor icur;
  234. struct xfs_bmbt_irec got;
  235. struct xfs_btree_cur *dummy_cur = NULL;
  236. int dummy_logflags;
  237. int error = 0;
  238. if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got))
  239. return 0;
  240. do {
  241. if (got.br_startoff >= offset_fsb + count_fsb)
  242. break;
  243. if (got.br_state == XFS_EXT_NORM)
  244. continue;
  245. if (WARN_ON_ONCE(isnullstartblock(got.br_startblock)))
  246. return -EIO;
  247. xfs_trim_extent(&got, offset_fsb, count_fsb);
  248. if (!got.br_blockcount)
  249. continue;
  250. got.br_state = XFS_EXT_NORM;
  251. error = xfs_bmap_add_extent_unwritten_real(NULL, ip,
  252. XFS_COW_FORK, &icur, &dummy_cur, &got,
  253. &dummy_logflags);
  254. if (error)
  255. return error;
  256. } while (xfs_iext_next_extent(ip->i_cowfp, &icur, &got));
  257. return error;
  258. }
  259. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  260. int
  261. xfs_reflink_convert_cow(
  262. struct xfs_inode *ip,
  263. xfs_off_t offset,
  264. xfs_off_t count)
  265. {
  266. struct xfs_mount *mp = ip->i_mount;
  267. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  268. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  269. xfs_filblks_t count_fsb = end_fsb - offset_fsb;
  270. int error;
  271. ASSERT(count != 0);
  272. xfs_ilock(ip, XFS_ILOCK_EXCL);
  273. error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
  274. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  275. return error;
  276. }
  277. /*
  278. * Find the extent that maps the given range in the COW fork. Even if the extent
  279. * is not shared we might have a preallocation for it in the COW fork. If so we
  280. * use it that rather than trigger a new allocation.
  281. */
  282. static int
  283. xfs_find_trim_cow_extent(
  284. struct xfs_inode *ip,
  285. struct xfs_bmbt_irec *imap,
  286. struct xfs_bmbt_irec *cmap,
  287. bool *shared,
  288. bool *found)
  289. {
  290. xfs_fileoff_t offset_fsb = imap->br_startoff;
  291. xfs_filblks_t count_fsb = imap->br_blockcount;
  292. struct xfs_iext_cursor icur;
  293. *found = false;
  294. /*
  295. * If we don't find an overlapping extent, trim the range we need to
  296. * allocate to fit the hole we found.
  297. */
  298. if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, cmap))
  299. cmap->br_startoff = offset_fsb + count_fsb;
  300. if (cmap->br_startoff > offset_fsb) {
  301. xfs_trim_extent(imap, imap->br_startoff,
  302. cmap->br_startoff - imap->br_startoff);
  303. return xfs_bmap_trim_cow(ip, imap, shared);
  304. }
  305. *shared = true;
  306. if (isnullstartblock(cmap->br_startblock)) {
  307. xfs_trim_extent(imap, cmap->br_startoff, cmap->br_blockcount);
  308. return 0;
  309. }
  310. /* real extent found - no need to allocate */
  311. xfs_trim_extent(cmap, offset_fsb, count_fsb);
  312. *found = true;
  313. return 0;
  314. }
  315. static int
  316. xfs_reflink_convert_unwritten(
  317. struct xfs_inode *ip,
  318. struct xfs_bmbt_irec *imap,
  319. struct xfs_bmbt_irec *cmap,
  320. bool convert_now)
  321. {
  322. xfs_fileoff_t offset_fsb = imap->br_startoff;
  323. xfs_filblks_t count_fsb = imap->br_blockcount;
  324. int error;
  325. /*
  326. * cmap might larger than imap due to cowextsize hint.
  327. */
  328. xfs_trim_extent(cmap, offset_fsb, count_fsb);
  329. /*
  330. * COW fork extents are supposed to remain unwritten until we're ready
  331. * to initiate a disk write. For direct I/O we are going to write the
  332. * data and need the conversion, but for buffered writes we're done.
  333. */
  334. if (!convert_now || cmap->br_state == XFS_EXT_NORM)
  335. return 0;
  336. trace_xfs_reflink_convert_cow(ip, cmap);
  337. error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
  338. if (!error)
  339. cmap->br_state = XFS_EXT_NORM;
  340. return error;
  341. }
  342. static int
  343. xfs_reflink_fill_cow_hole(
  344. struct xfs_inode *ip,
  345. struct xfs_bmbt_irec *imap,
  346. struct xfs_bmbt_irec *cmap,
  347. bool *shared,
  348. uint *lockmode,
  349. bool convert_now)
  350. {
  351. struct xfs_mount *mp = ip->i_mount;
  352. struct xfs_trans *tp;
  353. xfs_filblks_t resaligned;
  354. xfs_extlen_t resblks;
  355. int nimaps;
  356. int error;
  357. bool found;
  358. resaligned = xfs_aligned_fsb_count(imap->br_startoff,
  359. imap->br_blockcount, xfs_get_cowextsz_hint(ip));
  360. resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  361. xfs_iunlock(ip, *lockmode);
  362. *lockmode = 0;
  363. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
  364. false, &tp);
  365. if (error)
  366. return error;
  367. *lockmode = XFS_ILOCK_EXCL;
  368. error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
  369. if (error || !*shared)
  370. goto out_trans_cancel;
  371. if (found) {
  372. xfs_trans_cancel(tp);
  373. goto convert;
  374. }
  375. /* Allocate the entire reservation as unwritten blocks. */
  376. nimaps = 1;
  377. error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
  378. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, 0, cmap,
  379. &nimaps);
  380. if (error)
  381. goto out_trans_cancel;
  382. xfs_inode_set_cowblocks_tag(ip);
  383. error = xfs_trans_commit(tp);
  384. if (error)
  385. return error;
  386. convert:
  387. return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
  388. out_trans_cancel:
  389. xfs_trans_cancel(tp);
  390. return error;
  391. }
  392. static int
  393. xfs_reflink_fill_delalloc(
  394. struct xfs_inode *ip,
  395. struct xfs_bmbt_irec *imap,
  396. struct xfs_bmbt_irec *cmap,
  397. bool *shared,
  398. uint *lockmode,
  399. bool convert_now)
  400. {
  401. struct xfs_mount *mp = ip->i_mount;
  402. struct xfs_trans *tp;
  403. int nimaps;
  404. int error;
  405. bool found;
  406. do {
  407. xfs_iunlock(ip, *lockmode);
  408. *lockmode = 0;
  409. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0,
  410. false, &tp);
  411. if (error)
  412. return error;
  413. *lockmode = XFS_ILOCK_EXCL;
  414. error = xfs_find_trim_cow_extent(ip, imap, cmap, shared,
  415. &found);
  416. if (error || !*shared)
  417. goto out_trans_cancel;
  418. if (found) {
  419. xfs_trans_cancel(tp);
  420. break;
  421. }
  422. ASSERT(isnullstartblock(cmap->br_startblock) ||
  423. cmap->br_startblock == DELAYSTARTBLOCK);
  424. /*
  425. * Replace delalloc reservation with an unwritten extent.
  426. */
  427. nimaps = 1;
  428. error = xfs_bmapi_write(tp, ip, cmap->br_startoff,
  429. cmap->br_blockcount,
  430. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, 0,
  431. cmap, &nimaps);
  432. if (error)
  433. goto out_trans_cancel;
  434. xfs_inode_set_cowblocks_tag(ip);
  435. error = xfs_trans_commit(tp);
  436. if (error)
  437. return error;
  438. } while (cmap->br_startoff + cmap->br_blockcount <= imap->br_startoff);
  439. return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
  440. out_trans_cancel:
  441. xfs_trans_cancel(tp);
  442. return error;
  443. }
  444. /* Allocate all CoW reservations covering a range of blocks in a file. */
  445. int
  446. xfs_reflink_allocate_cow(
  447. struct xfs_inode *ip,
  448. struct xfs_bmbt_irec *imap,
  449. struct xfs_bmbt_irec *cmap,
  450. bool *shared,
  451. uint *lockmode,
  452. bool convert_now)
  453. {
  454. int error;
  455. bool found;
  456. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  457. if (!ip->i_cowfp) {
  458. ASSERT(!xfs_is_reflink_inode(ip));
  459. xfs_ifork_init_cow(ip);
  460. }
  461. error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
  462. if (error || !*shared)
  463. return error;
  464. /* CoW fork has a real extent */
  465. if (found)
  466. return xfs_reflink_convert_unwritten(ip, imap, cmap,
  467. convert_now);
  468. /*
  469. * CoW fork does not have an extent and data extent is shared.
  470. * Allocate a real extent in the CoW fork.
  471. */
  472. if (cmap->br_startoff > imap->br_startoff)
  473. return xfs_reflink_fill_cow_hole(ip, imap, cmap, shared,
  474. lockmode, convert_now);
  475. /*
  476. * CoW fork has a delalloc reservation. Replace it with a real extent.
  477. * There may or may not be a data fork mapping.
  478. */
  479. if (isnullstartblock(cmap->br_startblock) ||
  480. cmap->br_startblock == DELAYSTARTBLOCK)
  481. return xfs_reflink_fill_delalloc(ip, imap, cmap, shared,
  482. lockmode, convert_now);
  483. /* Shouldn't get here. */
  484. ASSERT(0);
  485. return -EFSCORRUPTED;
  486. }
  487. /*
  488. * Cancel CoW reservations for some block range of an inode.
  489. *
  490. * If cancel_real is true this function cancels all COW fork extents for the
  491. * inode; if cancel_real is false, real extents are not cleared.
  492. *
  493. * Caller must have already joined the inode to the current transaction. The
  494. * inode will be joined to the transaction returned to the caller.
  495. */
  496. int
  497. xfs_reflink_cancel_cow_blocks(
  498. struct xfs_inode *ip,
  499. struct xfs_trans **tpp,
  500. xfs_fileoff_t offset_fsb,
  501. xfs_fileoff_t end_fsb,
  502. bool cancel_real)
  503. {
  504. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
  505. struct xfs_bmbt_irec got, del;
  506. struct xfs_iext_cursor icur;
  507. int error = 0;
  508. if (!xfs_inode_has_cow_data(ip))
  509. return 0;
  510. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  511. return 0;
  512. /* Walk backwards until we're out of the I/O range... */
  513. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  514. del = got;
  515. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  516. /* Extent delete may have bumped ext forward */
  517. if (!del.br_blockcount) {
  518. xfs_iext_prev(ifp, &icur);
  519. goto next_extent;
  520. }
  521. trace_xfs_reflink_cancel_cow(ip, &del);
  522. if (isnullstartblock(del.br_startblock)) {
  523. xfs_bmap_del_extent_delay(ip, XFS_COW_FORK, &icur, &got,
  524. &del);
  525. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  526. ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
  527. /* Free the CoW orphan record. */
  528. xfs_refcount_free_cow_extent(*tpp, del.br_startblock,
  529. del.br_blockcount);
  530. error = xfs_free_extent_later(*tpp, del.br_startblock,
  531. del.br_blockcount, NULL,
  532. XFS_AG_RESV_NONE, 0);
  533. if (error)
  534. break;
  535. /* Roll the transaction */
  536. error = xfs_defer_finish(tpp);
  537. if (error)
  538. break;
  539. /* Remove the mapping from the CoW fork. */
  540. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  541. /* Remove the quota reservation */
  542. xfs_quota_unreserve_blkres(ip, del.br_blockcount);
  543. } else {
  544. /* Didn't do anything, push cursor back. */
  545. xfs_iext_prev(ifp, &icur);
  546. }
  547. next_extent:
  548. if (!xfs_iext_get_extent(ifp, &icur, &got))
  549. break;
  550. }
  551. /* clear tag if cow fork is emptied */
  552. if (!ifp->if_bytes)
  553. xfs_inode_clear_cowblocks_tag(ip);
  554. return error;
  555. }
  556. /*
  557. * Cancel CoW reservations for some byte range of an inode.
  558. *
  559. * If cancel_real is true this function cancels all COW fork extents for the
  560. * inode; if cancel_real is false, real extents are not cleared.
  561. */
  562. int
  563. xfs_reflink_cancel_cow_range(
  564. struct xfs_inode *ip,
  565. xfs_off_t offset,
  566. xfs_off_t count,
  567. bool cancel_real)
  568. {
  569. struct xfs_trans *tp;
  570. xfs_fileoff_t offset_fsb;
  571. xfs_fileoff_t end_fsb;
  572. int error;
  573. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  574. ASSERT(ip->i_cowfp);
  575. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  576. if (count == NULLFILEOFF)
  577. end_fsb = NULLFILEOFF;
  578. else
  579. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  580. /* Start a rolling transaction to remove the mappings */
  581. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  582. 0, 0, 0, &tp);
  583. if (error)
  584. goto out;
  585. xfs_ilock(ip, XFS_ILOCK_EXCL);
  586. xfs_trans_ijoin(tp, ip, 0);
  587. /* Scrape out the old CoW reservations */
  588. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  589. cancel_real);
  590. if (error)
  591. goto out_cancel;
  592. error = xfs_trans_commit(tp);
  593. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  594. return error;
  595. out_cancel:
  596. xfs_trans_cancel(tp);
  597. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  598. out:
  599. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  600. return error;
  601. }
  602. /*
  603. * Remap part of the CoW fork into the data fork.
  604. *
  605. * We aim to remap the range starting at @offset_fsb and ending at @end_fsb
  606. * into the data fork; this function will remap what it can (at the end of the
  607. * range) and update @end_fsb appropriately. Each remap gets its own
  608. * transaction because we can end up merging and splitting bmbt blocks for
  609. * every remap operation and we'd like to keep the block reservation
  610. * requirements as low as possible.
  611. */
  612. STATIC int
  613. xfs_reflink_end_cow_extent(
  614. struct xfs_inode *ip,
  615. xfs_fileoff_t *offset_fsb,
  616. xfs_fileoff_t end_fsb)
  617. {
  618. struct xfs_iext_cursor icur;
  619. struct xfs_bmbt_irec got, del, data;
  620. struct xfs_mount *mp = ip->i_mount;
  621. struct xfs_trans *tp;
  622. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
  623. unsigned int resblks;
  624. int nmaps;
  625. int error;
  626. resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  627. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
  628. XFS_TRANS_RESERVE, &tp);
  629. if (error)
  630. return error;
  631. /*
  632. * Lock the inode. We have to ijoin without automatic unlock because
  633. * the lead transaction is the refcountbt record deletion; the data
  634. * fork update follows as a deferred log item.
  635. */
  636. xfs_ilock(ip, XFS_ILOCK_EXCL);
  637. xfs_trans_ijoin(tp, ip, 0);
  638. /*
  639. * In case of racing, overlapping AIO writes no COW extents might be
  640. * left by the time I/O completes for the loser of the race. In that
  641. * case we are done.
  642. */
  643. if (!xfs_iext_lookup_extent(ip, ifp, *offset_fsb, &icur, &got) ||
  644. got.br_startoff >= end_fsb) {
  645. *offset_fsb = end_fsb;
  646. goto out_cancel;
  647. }
  648. /*
  649. * Only remap real extents that contain data. With AIO, speculative
  650. * preallocations can leak into the range we are called upon, and we
  651. * need to skip them. Preserve @got for the eventual CoW fork
  652. * deletion; from now on @del represents the mapping that we're
  653. * actually remapping.
  654. */
  655. while (!xfs_bmap_is_written_extent(&got)) {
  656. if (!xfs_iext_next_extent(ifp, &icur, &got) ||
  657. got.br_startoff >= end_fsb) {
  658. *offset_fsb = end_fsb;
  659. goto out_cancel;
  660. }
  661. }
  662. del = got;
  663. xfs_trim_extent(&del, *offset_fsb, end_fsb - *offset_fsb);
  664. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  665. XFS_IEXT_REFLINK_END_COW_CNT);
  666. if (error)
  667. goto out_cancel;
  668. /* Grab the corresponding mapping in the data fork. */
  669. nmaps = 1;
  670. error = xfs_bmapi_read(ip, del.br_startoff, del.br_blockcount, &data,
  671. &nmaps, 0);
  672. if (error)
  673. goto out_cancel;
  674. /* We can only remap the smaller of the two extent sizes. */
  675. data.br_blockcount = min(data.br_blockcount, del.br_blockcount);
  676. del.br_blockcount = data.br_blockcount;
  677. trace_xfs_reflink_cow_remap_from(ip, &del);
  678. trace_xfs_reflink_cow_remap_to(ip, &data);
  679. if (xfs_bmap_is_real_extent(&data)) {
  680. /*
  681. * If the extent we're remapping is backed by storage (written
  682. * or not), unmap the extent and drop its refcount.
  683. */
  684. xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &data);
  685. xfs_refcount_decrease_extent(tp, &data);
  686. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  687. -data.br_blockcount);
  688. } else if (data.br_startblock == DELAYSTARTBLOCK) {
  689. int done;
  690. /*
  691. * If the extent we're remapping is a delalloc reservation,
  692. * we can use the regular bunmapi function to release the
  693. * incore state. Dropping the delalloc reservation takes care
  694. * of the quota reservation for us.
  695. */
  696. error = xfs_bunmapi(NULL, ip, data.br_startoff,
  697. data.br_blockcount, 0, 1, &done);
  698. if (error)
  699. goto out_cancel;
  700. ASSERT(done);
  701. }
  702. /* Free the CoW orphan record. */
  703. xfs_refcount_free_cow_extent(tp, del.br_startblock, del.br_blockcount);
  704. /* Map the new blocks into the data fork. */
  705. xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, &del);
  706. /* Charge this new data fork mapping to the on-disk quota. */
  707. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
  708. (long)del.br_blockcount);
  709. /* Remove the mapping from the CoW fork. */
  710. xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
  711. error = xfs_trans_commit(tp);
  712. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  713. if (error)
  714. return error;
  715. /* Update the caller about how much progress we made. */
  716. *offset_fsb = del.br_startoff + del.br_blockcount;
  717. return 0;
  718. out_cancel:
  719. xfs_trans_cancel(tp);
  720. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  721. return error;
  722. }
  723. /*
  724. * Remap parts of a file's data fork after a successful CoW.
  725. */
  726. int
  727. xfs_reflink_end_cow(
  728. struct xfs_inode *ip,
  729. xfs_off_t offset,
  730. xfs_off_t count)
  731. {
  732. xfs_fileoff_t offset_fsb;
  733. xfs_fileoff_t end_fsb;
  734. int error = 0;
  735. trace_xfs_reflink_end_cow(ip, offset, count);
  736. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  737. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  738. /*
  739. * Walk forwards until we've remapped the I/O range. The loop function
  740. * repeatedly cycles the ILOCK to allocate one transaction per remapped
  741. * extent.
  742. *
  743. * If we're being called by writeback then the pages will still
  744. * have PageWriteback set, which prevents races with reflink remapping
  745. * and truncate. Reflink remapping prevents races with writeback by
  746. * taking the iolock and mmaplock before flushing the pages and
  747. * remapping, which means there won't be any further writeback or page
  748. * cache dirtying until the reflink completes.
  749. *
  750. * We should never have two threads issuing writeback for the same file
  751. * region. There are also have post-eof checks in the writeback
  752. * preparation code so that we don't bother writing out pages that are
  753. * about to be truncated.
  754. *
  755. * If we're being called as part of directio write completion, the dio
  756. * count is still elevated, which reflink and truncate will wait for.
  757. * Reflink remapping takes the iolock and mmaplock and waits for
  758. * pending dio to finish, which should prevent any directio until the
  759. * remap completes. Multiple concurrent directio writes to the same
  760. * region are handled by end_cow processing only occurring for the
  761. * threads which succeed; the outcome of multiple overlapping direct
  762. * writes is not well defined anyway.
  763. *
  764. * It's possible that a buffered write and a direct write could collide
  765. * here (the buffered write stumbles in after the dio flushes and
  766. * invalidates the page cache and immediately queues writeback), but we
  767. * have never supported this 100%. If either disk write succeeds the
  768. * blocks will be remapped.
  769. */
  770. while (end_fsb > offset_fsb && !error)
  771. error = xfs_reflink_end_cow_extent(ip, &offset_fsb, end_fsb);
  772. if (error)
  773. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  774. return error;
  775. }
  776. /*
  777. * Free all CoW staging blocks that are still referenced by the ondisk refcount
  778. * metadata. The ondisk metadata does not track which inode created the
  779. * staging extent, so callers must ensure that there are no cached inodes with
  780. * live CoW staging extents.
  781. */
  782. int
  783. xfs_reflink_recover_cow(
  784. struct xfs_mount *mp)
  785. {
  786. struct xfs_perag *pag;
  787. xfs_agnumber_t agno;
  788. int error = 0;
  789. if (!xfs_has_reflink(mp))
  790. return 0;
  791. for_each_perag(mp, agno, pag) {
  792. error = xfs_refcount_recover_cow_leftovers(mp, pag);
  793. if (error) {
  794. xfs_perag_rele(pag);
  795. break;
  796. }
  797. }
  798. return error;
  799. }
  800. /*
  801. * Reflinking (Block) Ranges of Two Files Together
  802. *
  803. * First, ensure that the reflink flag is set on both inodes. The flag is an
  804. * optimization to avoid unnecessary refcount btree lookups in the write path.
  805. *
  806. * Now we can iteratively remap the range of extents (and holes) in src to the
  807. * corresponding ranges in dest. Let drange and srange denote the ranges of
  808. * logical blocks in dest and src touched by the reflink operation.
  809. *
  810. * While the length of drange is greater than zero,
  811. * - Read src's bmbt at the start of srange ("imap")
  812. * - If imap doesn't exist, make imap appear to start at the end of srange
  813. * with zero length.
  814. * - If imap starts before srange, advance imap to start at srange.
  815. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  816. * - Punch (imap start - srange start + imap len) blocks from dest at
  817. * offset (drange start).
  818. * - If imap points to a real range of pblks,
  819. * > Increase the refcount of the imap's pblks
  820. * > Map imap's pblks into dest at the offset
  821. * (drange start + imap start - srange start)
  822. * - Advance drange and srange by (imap start - srange start + imap len)
  823. *
  824. * Finally, if the reflink made dest longer, update both the in-core and
  825. * on-disk file sizes.
  826. *
  827. * ASCII Art Demonstration:
  828. *
  829. * Let's say we want to reflink this source file:
  830. *
  831. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  832. * <-------------------->
  833. *
  834. * into this destination file:
  835. *
  836. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  837. * <-------------------->
  838. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  839. * Observe that the range has different logical offsets in either file.
  840. *
  841. * Consider that the first extent in the source file doesn't line up with our
  842. * reflink range. Unmapping and remapping are separate operations, so we can
  843. * unmap more blocks from the destination file than we remap.
  844. *
  845. * ----SSSSSSS-SSSSS----SSSSSS
  846. * <------->
  847. * --DDDDD---------DDDDD--DDD
  848. * <------->
  849. *
  850. * Now remap the source extent into the destination file:
  851. *
  852. * ----SSSSSSS-SSSSS----SSSSSS
  853. * <------->
  854. * --DDDDD--SSSSSSSDDDDD--DDD
  855. * <------->
  856. *
  857. * Do likewise with the second hole and extent in our range. Holes in the
  858. * unmap range don't affect our operation.
  859. *
  860. * ----SSSSSSS-SSSSS----SSSSSS
  861. * <---->
  862. * --DDDDD--SSSSSSS-SSSSS-DDD
  863. * <---->
  864. *
  865. * Finally, unmap and remap part of the third extent. This will increase the
  866. * size of the destination file.
  867. *
  868. * ----SSSSSSS-SSSSS----SSSSSS
  869. * <----->
  870. * --DDDDD--SSSSSSS-SSSSS----SSS
  871. * <----->
  872. *
  873. * Once we update the destination file's i_size, we're done.
  874. */
  875. /*
  876. * Ensure the reflink bit is set in both inodes.
  877. */
  878. STATIC int
  879. xfs_reflink_set_inode_flag(
  880. struct xfs_inode *src,
  881. struct xfs_inode *dest)
  882. {
  883. struct xfs_mount *mp = src->i_mount;
  884. int error;
  885. struct xfs_trans *tp;
  886. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  887. return 0;
  888. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  889. if (error)
  890. goto out_error;
  891. /* Lock both files against IO */
  892. if (src->i_ino == dest->i_ino)
  893. xfs_ilock(src, XFS_ILOCK_EXCL);
  894. else
  895. xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
  896. if (!xfs_is_reflink_inode(src)) {
  897. trace_xfs_reflink_set_inode_flag(src);
  898. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  899. src->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  900. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  901. xfs_ifork_init_cow(src);
  902. } else
  903. xfs_iunlock(src, XFS_ILOCK_EXCL);
  904. if (src->i_ino == dest->i_ino)
  905. goto commit_flags;
  906. if (!xfs_is_reflink_inode(dest)) {
  907. trace_xfs_reflink_set_inode_flag(dest);
  908. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  909. dest->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  910. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  911. xfs_ifork_init_cow(dest);
  912. } else
  913. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  914. commit_flags:
  915. error = xfs_trans_commit(tp);
  916. if (error)
  917. goto out_error;
  918. return error;
  919. out_error:
  920. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  921. return error;
  922. }
  923. /*
  924. * Update destination inode size & cowextsize hint, if necessary.
  925. */
  926. int
  927. xfs_reflink_update_dest(
  928. struct xfs_inode *dest,
  929. xfs_off_t newlen,
  930. xfs_extlen_t cowextsize,
  931. unsigned int remap_flags)
  932. {
  933. struct xfs_mount *mp = dest->i_mount;
  934. struct xfs_trans *tp;
  935. int error;
  936. if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  937. return 0;
  938. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  939. if (error)
  940. goto out_error;
  941. xfs_ilock(dest, XFS_ILOCK_EXCL);
  942. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  943. if (newlen > i_size_read(VFS_I(dest))) {
  944. trace_xfs_reflink_update_inode_size(dest, newlen);
  945. i_size_write(VFS_I(dest), newlen);
  946. dest->i_disk_size = newlen;
  947. }
  948. if (cowextsize) {
  949. dest->i_cowextsize = cowextsize;
  950. dest->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
  951. }
  952. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  953. error = xfs_trans_commit(tp);
  954. if (error)
  955. goto out_error;
  956. return error;
  957. out_error:
  958. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  959. return error;
  960. }
  961. /*
  962. * Do we have enough reserve in this AG to handle a reflink? The refcount
  963. * btree already reserved all the space it needs, but the rmap btree can grow
  964. * infinitely, so we won't allow more reflinks when the AG is down to the
  965. * btree reserves.
  966. */
  967. static int
  968. xfs_reflink_ag_has_free_space(
  969. struct xfs_mount *mp,
  970. xfs_agnumber_t agno)
  971. {
  972. struct xfs_perag *pag;
  973. int error = 0;
  974. if (!xfs_has_rmapbt(mp))
  975. return 0;
  976. pag = xfs_perag_get(mp, agno);
  977. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
  978. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  979. error = -ENOSPC;
  980. xfs_perag_put(pag);
  981. return error;
  982. }
  983. /*
  984. * Remap the given extent into the file. The dmap blockcount will be set to
  985. * the number of blocks that were actually remapped.
  986. */
  987. STATIC int
  988. xfs_reflink_remap_extent(
  989. struct xfs_inode *ip,
  990. struct xfs_bmbt_irec *dmap,
  991. xfs_off_t new_isize)
  992. {
  993. struct xfs_bmbt_irec smap;
  994. struct xfs_mount *mp = ip->i_mount;
  995. struct xfs_trans *tp;
  996. xfs_off_t newlen;
  997. int64_t qdelta = 0;
  998. unsigned int resblks;
  999. bool quota_reserved = true;
  1000. bool smap_real;
  1001. bool dmap_written = xfs_bmap_is_written_extent(dmap);
  1002. int iext_delta = 0;
  1003. int nimaps;
  1004. int error;
  1005. /*
  1006. * Start a rolling transaction to switch the mappings.
  1007. *
  1008. * Adding a written extent to the extent map can cause a bmbt split,
  1009. * and removing a mapped extent from the extent can cause a bmbt split.
  1010. * The two operations cannot both cause a split since they operate on
  1011. * the same index in the bmap btree, so we only need a reservation for
  1012. * one bmbt split if either thing is happening. However, we haven't
  1013. * locked the inode yet, so we reserve assuming this is the case.
  1014. *
  1015. * The first allocation call tries to reserve enough space to handle
  1016. * mapping dmap into a sparse part of the file plus the bmbt split. We
  1017. * haven't locked the inode or read the existing mapping yet, so we do
  1018. * not know for sure that we need the space. This should succeed most
  1019. * of the time.
  1020. *
  1021. * If the first attempt fails, try again but reserving only enough
  1022. * space to handle a bmbt split. This is the hard minimum requirement,
  1023. * and we revisit quota reservations later when we know more about what
  1024. * we're remapping.
  1025. */
  1026. resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  1027. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
  1028. resblks + dmap->br_blockcount, 0, false, &tp);
  1029. if (error == -EDQUOT || error == -ENOSPC) {
  1030. quota_reserved = false;
  1031. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
  1032. resblks, 0, false, &tp);
  1033. }
  1034. if (error)
  1035. goto out;
  1036. /*
  1037. * Read what's currently mapped in the destination file into smap.
  1038. * If smap isn't a hole, we will have to remove it before we can add
  1039. * dmap to the destination file.
  1040. */
  1041. nimaps = 1;
  1042. error = xfs_bmapi_read(ip, dmap->br_startoff, dmap->br_blockcount,
  1043. &smap, &nimaps, 0);
  1044. if (error)
  1045. goto out_cancel;
  1046. ASSERT(nimaps == 1 && smap.br_startoff == dmap->br_startoff);
  1047. smap_real = xfs_bmap_is_real_extent(&smap);
  1048. /*
  1049. * We can only remap as many blocks as the smaller of the two extent
  1050. * maps, because we can only remap one extent at a time.
  1051. */
  1052. dmap->br_blockcount = min(dmap->br_blockcount, smap.br_blockcount);
  1053. ASSERT(dmap->br_blockcount == smap.br_blockcount);
  1054. trace_xfs_reflink_remap_extent_dest(ip, &smap);
  1055. /*
  1056. * Two extents mapped to the same physical block must not have
  1057. * different states; that's filesystem corruption. Move on to the next
  1058. * extent if they're both holes or both the same physical extent.
  1059. */
  1060. if (dmap->br_startblock == smap.br_startblock) {
  1061. if (dmap->br_state != smap.br_state) {
  1062. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  1063. error = -EFSCORRUPTED;
  1064. }
  1065. goto out_cancel;
  1066. }
  1067. /* If both extents are unwritten, leave them alone. */
  1068. if (dmap->br_state == XFS_EXT_UNWRITTEN &&
  1069. smap.br_state == XFS_EXT_UNWRITTEN)
  1070. goto out_cancel;
  1071. /* No reflinking if the AG of the dest mapping is low on space. */
  1072. if (dmap_written) {
  1073. error = xfs_reflink_ag_has_free_space(mp,
  1074. XFS_FSB_TO_AGNO(mp, dmap->br_startblock));
  1075. if (error)
  1076. goto out_cancel;
  1077. }
  1078. /*
  1079. * Increase quota reservation if we think the quota block counter for
  1080. * this file could increase.
  1081. *
  1082. * If we are mapping a written extent into the file, we need to have
  1083. * enough quota block count reservation to handle the blocks in that
  1084. * extent. We log only the delta to the quota block counts, so if the
  1085. * extent we're unmapping also has blocks allocated to it, we don't
  1086. * need a quota reservation for the extent itself.
  1087. *
  1088. * Note that if we're replacing a delalloc reservation with a written
  1089. * extent, we have to take the full quota reservation because removing
  1090. * the delalloc reservation gives the block count back to the quota
  1091. * count. This is suboptimal, but the VFS flushed the dest range
  1092. * before we started. That should have removed all the delalloc
  1093. * reservations, but we code defensively.
  1094. *
  1095. * xfs_trans_alloc_inode above already tried to grab an even larger
  1096. * quota reservation, and kicked off a blockgc scan if it couldn't.
  1097. * If we can't get a potentially smaller quota reservation now, we're
  1098. * done.
  1099. */
  1100. if (!quota_reserved && !smap_real && dmap_written) {
  1101. error = xfs_trans_reserve_quota_nblks(tp, ip,
  1102. dmap->br_blockcount, 0, false);
  1103. if (error)
  1104. goto out_cancel;
  1105. }
  1106. if (smap_real)
  1107. ++iext_delta;
  1108. if (dmap_written)
  1109. ++iext_delta;
  1110. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, iext_delta);
  1111. if (error)
  1112. goto out_cancel;
  1113. if (smap_real) {
  1114. /*
  1115. * If the extent we're unmapping is backed by storage (written
  1116. * or not), unmap the extent and drop its refcount.
  1117. */
  1118. xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &smap);
  1119. xfs_refcount_decrease_extent(tp, &smap);
  1120. qdelta -= smap.br_blockcount;
  1121. } else if (smap.br_startblock == DELAYSTARTBLOCK) {
  1122. int done;
  1123. /*
  1124. * If the extent we're unmapping is a delalloc reservation,
  1125. * we can use the regular bunmapi function to release the
  1126. * incore state. Dropping the delalloc reservation takes care
  1127. * of the quota reservation for us.
  1128. */
  1129. error = xfs_bunmapi(NULL, ip, smap.br_startoff,
  1130. smap.br_blockcount, 0, 1, &done);
  1131. if (error)
  1132. goto out_cancel;
  1133. ASSERT(done);
  1134. }
  1135. /*
  1136. * If the extent we're sharing is backed by written storage, increase
  1137. * its refcount and map it into the file.
  1138. */
  1139. if (dmap_written) {
  1140. xfs_refcount_increase_extent(tp, dmap);
  1141. xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, dmap);
  1142. qdelta += dmap->br_blockcount;
  1143. }
  1144. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, qdelta);
  1145. /* Update dest isize if needed. */
  1146. newlen = XFS_FSB_TO_B(mp, dmap->br_startoff + dmap->br_blockcount);
  1147. newlen = min_t(xfs_off_t, newlen, new_isize);
  1148. if (newlen > i_size_read(VFS_I(ip))) {
  1149. trace_xfs_reflink_update_inode_size(ip, newlen);
  1150. i_size_write(VFS_I(ip), newlen);
  1151. ip->i_disk_size = newlen;
  1152. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1153. }
  1154. /* Commit everything and unlock. */
  1155. error = xfs_trans_commit(tp);
  1156. goto out_unlock;
  1157. out_cancel:
  1158. xfs_trans_cancel(tp);
  1159. out_unlock:
  1160. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1161. out:
  1162. if (error)
  1163. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  1164. return error;
  1165. }
  1166. /* Remap a range of one file to the other. */
  1167. int
  1168. xfs_reflink_remap_blocks(
  1169. struct xfs_inode *src,
  1170. loff_t pos_in,
  1171. struct xfs_inode *dest,
  1172. loff_t pos_out,
  1173. loff_t remap_len,
  1174. loff_t *remapped)
  1175. {
  1176. struct xfs_bmbt_irec imap;
  1177. struct xfs_mount *mp = src->i_mount;
  1178. xfs_fileoff_t srcoff = XFS_B_TO_FSBT(mp, pos_in);
  1179. xfs_fileoff_t destoff = XFS_B_TO_FSBT(mp, pos_out);
  1180. xfs_filblks_t len;
  1181. xfs_filblks_t remapped_len = 0;
  1182. xfs_off_t new_isize = pos_out + remap_len;
  1183. int nimaps;
  1184. int error = 0;
  1185. len = min_t(xfs_filblks_t, XFS_B_TO_FSB(mp, remap_len),
  1186. XFS_MAX_FILEOFF);
  1187. trace_xfs_reflink_remap_blocks(src, srcoff, len, dest, destoff);
  1188. while (len > 0) {
  1189. unsigned int lock_mode;
  1190. /* Read extent from the source file */
  1191. nimaps = 1;
  1192. lock_mode = xfs_ilock_data_map_shared(src);
  1193. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1194. xfs_iunlock(src, lock_mode);
  1195. if (error)
  1196. break;
  1197. /*
  1198. * The caller supposedly flushed all dirty pages in the source
  1199. * file range, which means that writeback should have allocated
  1200. * or deleted all delalloc reservations in that range. If we
  1201. * find one, that's a good sign that something is seriously
  1202. * wrong here.
  1203. */
  1204. ASSERT(nimaps == 1 && imap.br_startoff == srcoff);
  1205. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1206. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1207. xfs_bmap_mark_sick(src, XFS_DATA_FORK);
  1208. error = -EFSCORRUPTED;
  1209. break;
  1210. }
  1211. trace_xfs_reflink_remap_extent_src(src, &imap);
  1212. /* Remap into the destination file at the given offset. */
  1213. imap.br_startoff = destoff;
  1214. error = xfs_reflink_remap_extent(dest, &imap, new_isize);
  1215. if (error)
  1216. break;
  1217. if (fatal_signal_pending(current)) {
  1218. error = -EINTR;
  1219. break;
  1220. }
  1221. /* Advance drange/srange */
  1222. srcoff += imap.br_blockcount;
  1223. destoff += imap.br_blockcount;
  1224. len -= imap.br_blockcount;
  1225. remapped_len += imap.br_blockcount;
  1226. cond_resched();
  1227. }
  1228. if (error)
  1229. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1230. *remapped = min_t(loff_t, remap_len,
  1231. XFS_FSB_TO_B(src->i_mount, remapped_len));
  1232. return error;
  1233. }
  1234. /*
  1235. * If we're reflinking to a point past the destination file's EOF, we must
  1236. * zero any speculative post-EOF preallocations that sit between the old EOF
  1237. * and the destination file offset.
  1238. */
  1239. static int
  1240. xfs_reflink_zero_posteof(
  1241. struct xfs_inode *ip,
  1242. loff_t pos)
  1243. {
  1244. loff_t isize = i_size_read(VFS_I(ip));
  1245. if (pos <= isize)
  1246. return 0;
  1247. trace_xfs_zero_eof(ip, isize, pos - isize);
  1248. return xfs_zero_range(ip, isize, pos - isize, NULL);
  1249. }
  1250. /*
  1251. * Prepare two files for range cloning. Upon a successful return both inodes
  1252. * will have the iolock and mmaplock held, the page cache of the out file will
  1253. * be truncated, and any leases on the out file will have been broken. This
  1254. * function borrows heavily from xfs_file_aio_write_checks.
  1255. *
  1256. * The VFS allows partial EOF blocks to "match" for dedupe even though it hasn't
  1257. * checked that the bytes beyond EOF physically match. Hence we cannot use the
  1258. * EOF block in the source dedupe range because it's not a complete block match,
  1259. * hence can introduce a corruption into the file that has it's block replaced.
  1260. *
  1261. * In similar fashion, the VFS file cloning also allows partial EOF blocks to be
  1262. * "block aligned" for the purposes of cloning entire files. However, if the
  1263. * source file range includes the EOF block and it lands within the existing EOF
  1264. * of the destination file, then we can expose stale data from beyond the source
  1265. * file EOF in the destination file.
  1266. *
  1267. * XFS doesn't support partial block sharing, so in both cases we have check
  1268. * these cases ourselves. For dedupe, we can simply round the length to dedupe
  1269. * down to the previous whole block and ignore the partial EOF block. While this
  1270. * means we can't dedupe the last block of a file, this is an acceptible
  1271. * tradeoff for simplicity on implementation.
  1272. *
  1273. * For cloning, we want to share the partial EOF block if it is also the new EOF
  1274. * block of the destination file. If the partial EOF block lies inside the
  1275. * existing destination EOF, then we have to abort the clone to avoid exposing
  1276. * stale data in the destination file. Hence we reject these clone attempts with
  1277. * -EINVAL in this case.
  1278. */
  1279. int
  1280. xfs_reflink_remap_prep(
  1281. struct file *file_in,
  1282. loff_t pos_in,
  1283. struct file *file_out,
  1284. loff_t pos_out,
  1285. loff_t *len,
  1286. unsigned int remap_flags)
  1287. {
  1288. struct inode *inode_in = file_inode(file_in);
  1289. struct xfs_inode *src = XFS_I(inode_in);
  1290. struct inode *inode_out = file_inode(file_out);
  1291. struct xfs_inode *dest = XFS_I(inode_out);
  1292. int ret;
  1293. /* Lock both files against IO */
  1294. ret = xfs_ilock2_io_mmap(src, dest);
  1295. if (ret)
  1296. return ret;
  1297. /* Check file eligibility and prepare for block sharing. */
  1298. ret = -EINVAL;
  1299. /* Don't reflink realtime inodes */
  1300. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1301. goto out_unlock;
  1302. /* Don't share DAX file data with non-DAX file. */
  1303. if (IS_DAX(inode_in) != IS_DAX(inode_out))
  1304. goto out_unlock;
  1305. if (!IS_DAX(inode_in))
  1306. ret = generic_remap_file_range_prep(file_in, pos_in, file_out,
  1307. pos_out, len, remap_flags);
  1308. else
  1309. ret = dax_remap_file_range_prep(file_in, pos_in, file_out,
  1310. pos_out, len, remap_flags, &xfs_read_iomap_ops);
  1311. if (ret || *len == 0)
  1312. goto out_unlock;
  1313. /* Attach dquots to dest inode before changing block map */
  1314. ret = xfs_qm_dqattach(dest);
  1315. if (ret)
  1316. goto out_unlock;
  1317. /*
  1318. * Zero existing post-eof speculative preallocations in the destination
  1319. * file.
  1320. */
  1321. ret = xfs_reflink_zero_posteof(dest, pos_out);
  1322. if (ret)
  1323. goto out_unlock;
  1324. /* Set flags and remap blocks. */
  1325. ret = xfs_reflink_set_inode_flag(src, dest);
  1326. if (ret)
  1327. goto out_unlock;
  1328. /*
  1329. * If pos_out > EOF, we may have dirtied blocks between EOF and
  1330. * pos_out. In that case, we need to extend the flush and unmap to cover
  1331. * from EOF to the end of the copy length.
  1332. */
  1333. if (pos_out > XFS_ISIZE(dest)) {
  1334. loff_t flen = *len + (pos_out - XFS_ISIZE(dest));
  1335. ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
  1336. } else {
  1337. ret = xfs_flush_unmap_range(dest, pos_out, *len);
  1338. }
  1339. if (ret)
  1340. goto out_unlock;
  1341. xfs_iflags_set(src, XFS_IREMAPPING);
  1342. if (inode_in != inode_out)
  1343. xfs_ilock_demote(src, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  1344. return 0;
  1345. out_unlock:
  1346. xfs_iunlock2_io_mmap(src, dest);
  1347. return ret;
  1348. }
  1349. /* Does this inode need the reflink flag? */
  1350. int
  1351. xfs_reflink_inode_has_shared_extents(
  1352. struct xfs_trans *tp,
  1353. struct xfs_inode *ip,
  1354. bool *has_shared)
  1355. {
  1356. struct xfs_bmbt_irec got;
  1357. struct xfs_mount *mp = ip->i_mount;
  1358. struct xfs_ifork *ifp;
  1359. struct xfs_iext_cursor icur;
  1360. bool found;
  1361. int error;
  1362. ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  1363. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1364. if (error)
  1365. return error;
  1366. *has_shared = false;
  1367. found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
  1368. while (found) {
  1369. struct xfs_perag *pag;
  1370. xfs_agblock_t agbno;
  1371. xfs_extlen_t aglen;
  1372. xfs_agblock_t rbno;
  1373. xfs_extlen_t rlen;
  1374. if (isnullstartblock(got.br_startblock) ||
  1375. got.br_state != XFS_EXT_NORM)
  1376. goto next;
  1377. pag = xfs_perag_get(mp, 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(pag, tp, agbno, aglen,
  1381. &rbno, &rlen, false);
  1382. xfs_perag_put(pag);
  1383. if (error)
  1384. return error;
  1385. /* Is there still a shared block here? */
  1386. if (rbno != NULLAGBLOCK) {
  1387. *has_shared = true;
  1388. return 0;
  1389. }
  1390. next:
  1391. found = xfs_iext_next_extent(ifp, &icur, &got);
  1392. }
  1393. return 0;
  1394. }
  1395. /*
  1396. * Clear the inode reflink flag if there are no shared extents.
  1397. *
  1398. * The caller is responsible for joining the inode to the transaction passed in.
  1399. * The inode will be joined to the transaction that is returned to the caller.
  1400. */
  1401. int
  1402. xfs_reflink_clear_inode_flag(
  1403. struct xfs_inode *ip,
  1404. struct xfs_trans **tpp)
  1405. {
  1406. bool needs_flag;
  1407. int error = 0;
  1408. ASSERT(xfs_is_reflink_inode(ip));
  1409. if (!xfs_can_free_cowblocks(ip))
  1410. return 0;
  1411. error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
  1412. if (error || needs_flag)
  1413. return error;
  1414. /*
  1415. * We didn't find any shared blocks so turn off the reflink flag.
  1416. * First, get rid of any leftover CoW mappings.
  1417. */
  1418. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, XFS_MAX_FILEOFF,
  1419. true);
  1420. if (error)
  1421. return error;
  1422. /* Clear the inode flag. */
  1423. trace_xfs_reflink_unset_inode_flag(ip);
  1424. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  1425. xfs_inode_clear_cowblocks_tag(ip);
  1426. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1427. return error;
  1428. }
  1429. /*
  1430. * Clear the inode reflink flag if there are no shared extents and the size
  1431. * hasn't changed.
  1432. */
  1433. STATIC int
  1434. xfs_reflink_try_clear_inode_flag(
  1435. struct xfs_inode *ip)
  1436. {
  1437. struct xfs_mount *mp = ip->i_mount;
  1438. struct xfs_trans *tp;
  1439. int error = 0;
  1440. /* Start a rolling transaction to remove the mappings */
  1441. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1442. if (error)
  1443. return error;
  1444. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1445. xfs_trans_ijoin(tp, ip, 0);
  1446. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1447. if (error)
  1448. goto cancel;
  1449. error = xfs_trans_commit(tp);
  1450. if (error)
  1451. goto out;
  1452. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1453. return 0;
  1454. cancel:
  1455. xfs_trans_cancel(tp);
  1456. out:
  1457. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1458. return error;
  1459. }
  1460. /*
  1461. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1462. * the reflink flag if we unshare all of the file's blocks.
  1463. */
  1464. int
  1465. xfs_reflink_unshare(
  1466. struct xfs_inode *ip,
  1467. xfs_off_t offset,
  1468. xfs_off_t len)
  1469. {
  1470. struct inode *inode = VFS_I(ip);
  1471. int error;
  1472. if (!xfs_is_reflink_inode(ip))
  1473. return 0;
  1474. trace_xfs_reflink_unshare(ip, offset, len);
  1475. inode_dio_wait(inode);
  1476. if (IS_DAX(inode))
  1477. error = dax_file_unshare(inode, offset, len,
  1478. &xfs_dax_write_iomap_ops);
  1479. else
  1480. error = iomap_file_unshare(inode, offset, len,
  1481. &xfs_buffered_write_iomap_ops);
  1482. if (error)
  1483. goto out;
  1484. error = filemap_write_and_wait_range(inode->i_mapping, offset,
  1485. offset + len - 1);
  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:
  1494. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1495. return error;
  1496. }