xfs_reflink.c 45 KB

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