xfs_reflink.c 48 KB

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