xfs_reflink.c 43 KB

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