xfs_reflink.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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. xfs_agnumber_t agno,
  157. xfs_agblock_t agbno,
  158. xfs_extlen_t aglen,
  159. xfs_agblock_t *fbno,
  160. xfs_extlen_t *flen,
  161. bool find_end_of_shared)
  162. {
  163. struct xfs_buf *agbp;
  164. struct xfs_btree_cur *cur;
  165. int error;
  166. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  167. if (error)
  168. return error;
  169. if (!agbp)
  170. return -ENOMEM;
  171. cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
  172. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  173. find_end_of_shared);
  174. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  175. xfs_buf_relse(agbp);
  176. return error;
  177. }
  178. /*
  179. * Trim the mapping to the next block where there's a change in the
  180. * shared/unshared status. More specifically, this means that we
  181. * find the lowest-numbered extent of shared blocks that coincides with
  182. * the given block mapping. If the shared extent overlaps the start of
  183. * the mapping, trim the mapping to the end of the shared extent. If
  184. * the shared region intersects the mapping, trim the mapping to the
  185. * start of the shared extent. If there are no shared regions that
  186. * overlap, just return the original extent.
  187. */
  188. int
  189. xfs_reflink_trim_around_shared(
  190. struct xfs_inode *ip,
  191. struct xfs_bmbt_irec *irec,
  192. bool *shared,
  193. bool *trimmed)
  194. {
  195. xfs_agnumber_t agno;
  196. xfs_agblock_t agbno;
  197. xfs_extlen_t aglen;
  198. xfs_agblock_t fbno;
  199. xfs_extlen_t flen;
  200. int error = 0;
  201. /* Holes, unwritten, and delalloc extents cannot be shared */
  202. if (!xfs_is_reflink_inode(ip) ||
  203. ISUNWRITTEN(irec) ||
  204. irec->br_startblock == HOLESTARTBLOCK ||
  205. irec->br_startblock == DELAYSTARTBLOCK ||
  206. isnullstartblock(irec->br_startblock)) {
  207. *shared = false;
  208. return 0;
  209. }
  210. trace_xfs_reflink_trim_around_shared(ip, irec);
  211. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  212. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  213. aglen = irec->br_blockcount;
  214. error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
  215. aglen, &fbno, &flen, true);
  216. if (error)
  217. return error;
  218. *shared = *trimmed = false;
  219. if (fbno == NULLAGBLOCK) {
  220. /* No shared blocks at all. */
  221. return 0;
  222. } else if (fbno == agbno) {
  223. /*
  224. * The start of this extent is shared. Truncate the
  225. * mapping at the end of the shared region so that a
  226. * subsequent iteration starts at the start of the
  227. * unshared region.
  228. */
  229. irec->br_blockcount = flen;
  230. *shared = true;
  231. if (flen != aglen)
  232. *trimmed = true;
  233. return 0;
  234. } else {
  235. /*
  236. * There's a shared extent midway through this extent.
  237. * Truncate the mapping at the start of the shared
  238. * extent so that a subsequent iteration starts at the
  239. * start of the shared region.
  240. */
  241. irec->br_blockcount = fbno - agbno;
  242. *trimmed = true;
  243. return 0;
  244. }
  245. }
  246. /*
  247. * Trim the passed in imap to the next shared/unshared extent boundary, and
  248. * if imap->br_startoff points to a shared extent reserve space for it in the
  249. * COW fork. In this case *shared is set to true, else to false.
  250. *
  251. * Note that imap will always contain the block numbers for the existing blocks
  252. * in the data fork, as the upper layers need them for read-modify-write
  253. * operations.
  254. */
  255. int
  256. xfs_reflink_reserve_cow(
  257. struct xfs_inode *ip,
  258. struct xfs_bmbt_irec *imap,
  259. bool *shared)
  260. {
  261. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  262. struct xfs_bmbt_irec got;
  263. int error = 0;
  264. bool eof = false, trimmed;
  265. xfs_extnum_t idx;
  266. /*
  267. * Search the COW fork extent list first. This serves two purposes:
  268. * first this implement the speculative preallocation using cowextisze,
  269. * so that we also unshared block adjacent to shared blocks instead
  270. * of just the shared blocks themselves. Second the lookup in the
  271. * extent list is generally faster than going out to the shared extent
  272. * tree.
  273. */
  274. if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
  275. eof = true;
  276. if (!eof && got.br_startoff <= imap->br_startoff) {
  277. trace_xfs_reflink_cow_found(ip, imap);
  278. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  279. *shared = true;
  280. return 0;
  281. }
  282. /* Trim the mapping to the nearest shared extent boundary. */
  283. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  284. if (error)
  285. return error;
  286. /* Not shared? Just report the (potentially capped) extent. */
  287. if (!*shared)
  288. return 0;
  289. /*
  290. * Fork all the shared blocks from our write offset until the end of
  291. * the extent.
  292. */
  293. error = xfs_qm_dqattach_locked(ip, 0);
  294. if (error)
  295. return error;
  296. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
  297. imap->br_blockcount, 0, &got, &idx, eof);
  298. if (error == -ENOSPC || error == -EDQUOT)
  299. trace_xfs_reflink_cow_enospc(ip, imap);
  300. if (error)
  301. return error;
  302. trace_xfs_reflink_cow_alloc(ip, &got);
  303. return 0;
  304. }
  305. /* Convert part of an unwritten CoW extent to a real one. */
  306. STATIC int
  307. xfs_reflink_convert_cow_extent(
  308. struct xfs_inode *ip,
  309. struct xfs_bmbt_irec *imap,
  310. xfs_fileoff_t offset_fsb,
  311. xfs_filblks_t count_fsb,
  312. struct xfs_defer_ops *dfops)
  313. {
  314. struct xfs_bmbt_irec irec = *imap;
  315. xfs_fsblock_t first_block = NULLFSBLOCK;
  316. int nimaps = 1;
  317. if (imap->br_state == XFS_EXT_NORM)
  318. return 0;
  319. xfs_trim_extent(&irec, offset_fsb, count_fsb);
  320. trace_xfs_reflink_convert_cow(ip, &irec);
  321. if (irec.br_blockcount == 0)
  322. return 0;
  323. return xfs_bmapi_write(NULL, ip, irec.br_startoff, irec.br_blockcount,
  324. XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
  325. 0, &irec, &nimaps, dfops);
  326. }
  327. /* Convert all of the unwritten CoW extents in a file's range to real ones. */
  328. int
  329. xfs_reflink_convert_cow(
  330. struct xfs_inode *ip,
  331. xfs_off_t offset,
  332. xfs_off_t count)
  333. {
  334. struct xfs_bmbt_irec got;
  335. struct xfs_defer_ops dfops;
  336. struct xfs_mount *mp = ip->i_mount;
  337. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  338. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  339. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  340. xfs_extnum_t idx;
  341. bool found;
  342. int error = 0;
  343. xfs_ilock(ip, XFS_ILOCK_EXCL);
  344. /* Convert all the extents to real from unwritten. */
  345. for (found = xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
  346. found && got.br_startoff < end_fsb;
  347. found = xfs_iext_get_extent(ifp, ++idx, &got)) {
  348. error = xfs_reflink_convert_cow_extent(ip, &got, offset_fsb,
  349. end_fsb - offset_fsb, &dfops);
  350. if (error)
  351. break;
  352. }
  353. /* Finish up. */
  354. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  355. return error;
  356. }
  357. /* Allocate all CoW reservations covering a range of blocks in a file. */
  358. static int
  359. __xfs_reflink_allocate_cow(
  360. struct xfs_inode *ip,
  361. xfs_fileoff_t *offset_fsb,
  362. xfs_fileoff_t end_fsb)
  363. {
  364. struct xfs_mount *mp = ip->i_mount;
  365. struct xfs_bmbt_irec imap;
  366. struct xfs_defer_ops dfops;
  367. struct xfs_trans *tp;
  368. xfs_fsblock_t first_block;
  369. int nimaps = 1, error;
  370. bool shared;
  371. xfs_defer_init(&dfops, &first_block);
  372. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
  373. XFS_TRANS_RESERVE, &tp);
  374. if (error)
  375. return error;
  376. xfs_ilock(ip, XFS_ILOCK_EXCL);
  377. /* Read extent from the source file. */
  378. nimaps = 1;
  379. error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
  380. &imap, &nimaps, 0);
  381. if (error)
  382. goto out_unlock;
  383. ASSERT(nimaps == 1);
  384. /* Make sure there's a CoW reservation for it. */
  385. error = xfs_reflink_reserve_cow(ip, &imap, &shared);
  386. if (error)
  387. goto out_trans_cancel;
  388. if (!shared) {
  389. *offset_fsb = imap.br_startoff + imap.br_blockcount;
  390. goto out_trans_cancel;
  391. }
  392. /* Allocate the entire reservation as unwritten blocks. */
  393. xfs_trans_ijoin(tp, ip, 0);
  394. error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
  395. XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
  396. XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
  397. &imap, &nimaps, &dfops);
  398. if (error)
  399. goto out_trans_cancel;
  400. /* Finish up. */
  401. error = xfs_defer_finish(&tp, &dfops, NULL);
  402. if (error)
  403. goto out_trans_cancel;
  404. error = xfs_trans_commit(tp);
  405. *offset_fsb = imap.br_startoff + imap.br_blockcount;
  406. out_unlock:
  407. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  408. return error;
  409. out_trans_cancel:
  410. xfs_defer_cancel(&dfops);
  411. xfs_trans_cancel(tp);
  412. goto out_unlock;
  413. }
  414. /* Allocate all CoW reservations covering a part of a file. */
  415. int
  416. xfs_reflink_allocate_cow_range(
  417. struct xfs_inode *ip,
  418. xfs_off_t offset,
  419. xfs_off_t count)
  420. {
  421. struct xfs_mount *mp = ip->i_mount;
  422. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  423. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  424. int error;
  425. ASSERT(xfs_is_reflink_inode(ip));
  426. trace_xfs_reflink_allocate_cow_range(ip, offset, count);
  427. /*
  428. * Make sure that the dquots are there.
  429. */
  430. error = xfs_qm_dqattach(ip, 0);
  431. if (error)
  432. return error;
  433. while (offset_fsb < end_fsb) {
  434. error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
  435. if (error) {
  436. trace_xfs_reflink_allocate_cow_range_error(ip, error,
  437. _RET_IP_);
  438. return error;
  439. }
  440. }
  441. /* Convert the CoW extents to regular. */
  442. return xfs_reflink_convert_cow(ip, offset, count);
  443. }
  444. /*
  445. * Find the CoW reservation (and whether or not it needs block allocation)
  446. * for a given byte offset of a file.
  447. */
  448. bool
  449. xfs_reflink_find_cow_mapping(
  450. struct xfs_inode *ip,
  451. xfs_off_t offset,
  452. struct xfs_bmbt_irec *imap,
  453. bool *need_alloc)
  454. {
  455. struct xfs_bmbt_irec irec;
  456. struct xfs_ifork *ifp;
  457. struct xfs_bmbt_rec_host *gotp;
  458. xfs_fileoff_t bno;
  459. xfs_extnum_t idx;
  460. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  461. ASSERT(xfs_is_reflink_inode(ip));
  462. /* Find the extent in the CoW fork. */
  463. ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  464. bno = XFS_B_TO_FSBT(ip->i_mount, offset);
  465. gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
  466. if (!gotp)
  467. return false;
  468. xfs_bmbt_get_all(gotp, &irec);
  469. if (bno >= irec.br_startoff + irec.br_blockcount ||
  470. bno < irec.br_startoff)
  471. return false;
  472. trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
  473. &irec);
  474. /* If it's still delalloc, we must allocate later. */
  475. *imap = irec;
  476. *need_alloc = !!(isnullstartblock(irec.br_startblock));
  477. return true;
  478. }
  479. /*
  480. * Trim an extent to end at the next CoW reservation past offset_fsb.
  481. */
  482. int
  483. xfs_reflink_trim_irec_to_next_cow(
  484. struct xfs_inode *ip,
  485. xfs_fileoff_t offset_fsb,
  486. struct xfs_bmbt_irec *imap)
  487. {
  488. struct xfs_bmbt_irec irec;
  489. struct xfs_ifork *ifp;
  490. struct xfs_bmbt_rec_host *gotp;
  491. xfs_extnum_t idx;
  492. if (!xfs_is_reflink_inode(ip))
  493. return 0;
  494. /* Find the extent in the CoW fork. */
  495. ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  496. gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
  497. if (!gotp)
  498. return 0;
  499. xfs_bmbt_get_all(gotp, &irec);
  500. /* This is the extent before; try sliding up one. */
  501. if (irec.br_startoff < offset_fsb) {
  502. idx++;
  503. if (idx >= xfs_iext_count(ifp))
  504. return 0;
  505. gotp = xfs_iext_get_ext(ifp, idx);
  506. xfs_bmbt_get_all(gotp, &irec);
  507. }
  508. if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
  509. return 0;
  510. imap->br_blockcount = irec.br_startoff - imap->br_startoff;
  511. trace_xfs_reflink_trim_irec(ip, imap);
  512. return 0;
  513. }
  514. /*
  515. * Cancel CoW reservations for some block range of an inode.
  516. *
  517. * If cancel_real is true this function cancels all COW fork extents for the
  518. * inode; if cancel_real is false, real extents are not cleared.
  519. */
  520. int
  521. xfs_reflink_cancel_cow_blocks(
  522. struct xfs_inode *ip,
  523. struct xfs_trans **tpp,
  524. xfs_fileoff_t offset_fsb,
  525. xfs_fileoff_t end_fsb,
  526. bool cancel_real)
  527. {
  528. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  529. struct xfs_bmbt_irec got, prev, del;
  530. xfs_extnum_t idx;
  531. xfs_fsblock_t firstfsb;
  532. struct xfs_defer_ops dfops;
  533. int error = 0, eof = 0;
  534. if (!xfs_is_reflink_inode(ip))
  535. return 0;
  536. xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
  537. &got, &prev);
  538. if (eof)
  539. return 0;
  540. while (got.br_startoff < end_fsb) {
  541. del = got;
  542. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  543. trace_xfs_reflink_cancel_cow(ip, &del);
  544. if (isnullstartblock(del.br_startblock)) {
  545. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  546. &idx, &got, &del);
  547. if (error)
  548. break;
  549. } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
  550. xfs_trans_ijoin(*tpp, ip, 0);
  551. xfs_defer_init(&dfops, &firstfsb);
  552. /* Free the CoW orphan record. */
  553. error = xfs_refcount_free_cow_extent(ip->i_mount,
  554. &dfops, del.br_startblock,
  555. del.br_blockcount);
  556. if (error)
  557. break;
  558. xfs_bmap_add_free(ip->i_mount, &dfops,
  559. del.br_startblock, del.br_blockcount,
  560. NULL);
  561. /* Update quota accounting */
  562. xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
  563. -(long)del.br_blockcount);
  564. /* Roll the transaction */
  565. error = xfs_defer_finish(tpp, &dfops, ip);
  566. if (error) {
  567. xfs_defer_cancel(&dfops);
  568. break;
  569. }
  570. /* Remove the mapping from the CoW fork. */
  571. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  572. }
  573. if (++idx >= xfs_iext_count(ifp))
  574. break;
  575. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
  576. }
  577. /* clear tag if cow fork is emptied */
  578. if (!ifp->if_bytes)
  579. xfs_inode_clear_cowblocks_tag(ip);
  580. return error;
  581. }
  582. /*
  583. * Cancel CoW reservations for some byte range of an inode.
  584. *
  585. * If cancel_real is true this function cancels all COW fork extents for the
  586. * inode; if cancel_real is false, real extents are not cleared.
  587. */
  588. int
  589. xfs_reflink_cancel_cow_range(
  590. struct xfs_inode *ip,
  591. xfs_off_t offset,
  592. xfs_off_t count,
  593. bool cancel_real)
  594. {
  595. struct xfs_trans *tp;
  596. xfs_fileoff_t offset_fsb;
  597. xfs_fileoff_t end_fsb;
  598. int error;
  599. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  600. ASSERT(xfs_is_reflink_inode(ip));
  601. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  602. if (count == NULLFILEOFF)
  603. end_fsb = NULLFILEOFF;
  604. else
  605. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  606. /* Start a rolling transaction to remove the mappings */
  607. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  608. 0, 0, 0, &tp);
  609. if (error)
  610. goto out;
  611. xfs_ilock(ip, XFS_ILOCK_EXCL);
  612. xfs_trans_ijoin(tp, ip, 0);
  613. /* Scrape out the old CoW reservations */
  614. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
  615. cancel_real);
  616. if (error)
  617. goto out_cancel;
  618. error = xfs_trans_commit(tp);
  619. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  620. return error;
  621. out_cancel:
  622. xfs_trans_cancel(tp);
  623. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  624. out:
  625. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  626. return error;
  627. }
  628. /*
  629. * Remap parts of a file's data fork after a successful CoW.
  630. */
  631. int
  632. xfs_reflink_end_cow(
  633. struct xfs_inode *ip,
  634. xfs_off_t offset,
  635. xfs_off_t count)
  636. {
  637. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  638. struct xfs_bmbt_irec got, prev, del;
  639. struct xfs_trans *tp;
  640. xfs_fileoff_t offset_fsb;
  641. xfs_fileoff_t end_fsb;
  642. xfs_fsblock_t firstfsb;
  643. struct xfs_defer_ops dfops;
  644. int error, eof = 0;
  645. unsigned int resblks;
  646. xfs_filblks_t rlen;
  647. xfs_extnum_t idx;
  648. trace_xfs_reflink_end_cow(ip, offset, count);
  649. /* No COW extents? That's easy! */
  650. if (ifp->if_bytes == 0)
  651. return 0;
  652. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  653. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  654. /*
  655. * Start a rolling transaction to switch the mappings. We're
  656. * unlikely ever to have to remap 16T worth of single-block
  657. * extents, so just cap the worst case extent count to 2^32-1.
  658. * Stick a warning in just in case, and avoid 64-bit division.
  659. */
  660. BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
  661. if (end_fsb - offset_fsb > UINT_MAX) {
  662. error = -EFSCORRUPTED;
  663. xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
  664. ASSERT(0);
  665. goto out;
  666. }
  667. resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
  668. (unsigned int)(end_fsb - offset_fsb),
  669. XFS_DATA_FORK);
  670. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  671. resblks, 0, 0, &tp);
  672. if (error)
  673. goto out;
  674. xfs_ilock(ip, XFS_ILOCK_EXCL);
  675. xfs_trans_ijoin(tp, ip, 0);
  676. xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
  677. &got, &prev);
  678. /* If there is a hole at end_fsb - 1 go to the previous extent */
  679. if (eof || got.br_startoff > end_fsb) {
  680. /*
  681. * In case of racing, overlapping AIO writes no COW extents
  682. * might be left by the time I/O completes for the loser of
  683. * the race. In that case we are done.
  684. */
  685. if (idx <= 0)
  686. goto out_cancel;
  687. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
  688. }
  689. /* Walk backwards until we're out of the I/O range... */
  690. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  691. del = got;
  692. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  693. /* Extent delete may have bumped idx forward */
  694. if (!del.br_blockcount) {
  695. idx--;
  696. goto next_extent;
  697. }
  698. ASSERT(!isnullstartblock(got.br_startblock));
  699. /*
  700. * Don't remap unwritten extents; these are
  701. * speculatively preallocated CoW extents that have been
  702. * allocated but have not yet been involved in a write.
  703. */
  704. if (got.br_state == XFS_EXT_UNWRITTEN) {
  705. idx--;
  706. goto next_extent;
  707. }
  708. /* Unmap the old blocks in the data fork. */
  709. xfs_defer_init(&dfops, &firstfsb);
  710. rlen = del.br_blockcount;
  711. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
  712. &firstfsb, &dfops);
  713. if (error)
  714. goto out_defer;
  715. /* Trim the extent to whatever got unmapped. */
  716. if (rlen) {
  717. xfs_trim_extent(&del, del.br_startoff + rlen,
  718. del.br_blockcount - rlen);
  719. }
  720. trace_xfs_reflink_cow_remap(ip, &del);
  721. /* Free the CoW orphan record. */
  722. error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
  723. del.br_startblock, del.br_blockcount);
  724. if (error)
  725. goto out_defer;
  726. /* Map the new blocks into the data fork. */
  727. error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
  728. if (error)
  729. goto out_defer;
  730. /* Remove the mapping from the CoW fork. */
  731. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  732. error = xfs_defer_finish(&tp, &dfops, ip);
  733. if (error)
  734. goto out_defer;
  735. next_extent:
  736. if (idx < 0)
  737. break;
  738. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
  739. }
  740. error = xfs_trans_commit(tp);
  741. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  742. if (error)
  743. goto out;
  744. return 0;
  745. out_defer:
  746. xfs_defer_cancel(&dfops);
  747. out_cancel:
  748. xfs_trans_cancel(tp);
  749. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  750. out:
  751. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  752. return error;
  753. }
  754. /*
  755. * Free leftover CoW reservations that didn't get cleaned out.
  756. */
  757. int
  758. xfs_reflink_recover_cow(
  759. struct xfs_mount *mp)
  760. {
  761. xfs_agnumber_t agno;
  762. int error = 0;
  763. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  764. return 0;
  765. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  766. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  767. if (error)
  768. break;
  769. }
  770. return error;
  771. }
  772. /*
  773. * Reflinking (Block) Ranges of Two Files Together
  774. *
  775. * First, ensure that the reflink flag is set on both inodes. The flag is an
  776. * optimization to avoid unnecessary refcount btree lookups in the write path.
  777. *
  778. * Now we can iteratively remap the range of extents (and holes) in src to the
  779. * corresponding ranges in dest. Let drange and srange denote the ranges of
  780. * logical blocks in dest and src touched by the reflink operation.
  781. *
  782. * While the length of drange is greater than zero,
  783. * - Read src's bmbt at the start of srange ("imap")
  784. * - If imap doesn't exist, make imap appear to start at the end of srange
  785. * with zero length.
  786. * - If imap starts before srange, advance imap to start at srange.
  787. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  788. * - Punch (imap start - srange start + imap len) blocks from dest at
  789. * offset (drange start).
  790. * - If imap points to a real range of pblks,
  791. * > Increase the refcount of the imap's pblks
  792. * > Map imap's pblks into dest at the offset
  793. * (drange start + imap start - srange start)
  794. * - Advance drange and srange by (imap start - srange start + imap len)
  795. *
  796. * Finally, if the reflink made dest longer, update both the in-core and
  797. * on-disk file sizes.
  798. *
  799. * ASCII Art Demonstration:
  800. *
  801. * Let's say we want to reflink this source file:
  802. *
  803. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  804. * <-------------------->
  805. *
  806. * into this destination file:
  807. *
  808. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  809. * <-------------------->
  810. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  811. * Observe that the range has different logical offsets in either file.
  812. *
  813. * Consider that the first extent in the source file doesn't line up with our
  814. * reflink range. Unmapping and remapping are separate operations, so we can
  815. * unmap more blocks from the destination file than we remap.
  816. *
  817. * ----SSSSSSS-SSSSS----SSSSSS
  818. * <------->
  819. * --DDDDD---------DDDDD--DDD
  820. * <------->
  821. *
  822. * Now remap the source extent into the destination file:
  823. *
  824. * ----SSSSSSS-SSSSS----SSSSSS
  825. * <------->
  826. * --DDDDD--SSSSSSSDDDDD--DDD
  827. * <------->
  828. *
  829. * Do likewise with the second hole and extent in our range. Holes in the
  830. * unmap range don't affect our operation.
  831. *
  832. * ----SSSSSSS-SSSSS----SSSSSS
  833. * <---->
  834. * --DDDDD--SSSSSSS-SSSSS-DDD
  835. * <---->
  836. *
  837. * Finally, unmap and remap part of the third extent. This will increase the
  838. * size of the destination file.
  839. *
  840. * ----SSSSSSS-SSSSS----SSSSSS
  841. * <----->
  842. * --DDDDD--SSSSSSS-SSSSS----SSS
  843. * <----->
  844. *
  845. * Once we update the destination file's i_size, we're done.
  846. */
  847. /*
  848. * Ensure the reflink bit is set in both inodes.
  849. */
  850. STATIC int
  851. xfs_reflink_set_inode_flag(
  852. struct xfs_inode *src,
  853. struct xfs_inode *dest)
  854. {
  855. struct xfs_mount *mp = src->i_mount;
  856. int error;
  857. struct xfs_trans *tp;
  858. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  859. return 0;
  860. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  861. if (error)
  862. goto out_error;
  863. /* Lock both files against IO */
  864. if (src->i_ino == dest->i_ino)
  865. xfs_ilock(src, XFS_ILOCK_EXCL);
  866. else
  867. xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
  868. if (!xfs_is_reflink_inode(src)) {
  869. trace_xfs_reflink_set_inode_flag(src);
  870. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  871. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  872. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  873. xfs_ifork_init_cow(src);
  874. } else
  875. xfs_iunlock(src, XFS_ILOCK_EXCL);
  876. if (src->i_ino == dest->i_ino)
  877. goto commit_flags;
  878. if (!xfs_is_reflink_inode(dest)) {
  879. trace_xfs_reflink_set_inode_flag(dest);
  880. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  881. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  882. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  883. xfs_ifork_init_cow(dest);
  884. } else
  885. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  886. commit_flags:
  887. error = xfs_trans_commit(tp);
  888. if (error)
  889. goto out_error;
  890. return error;
  891. out_error:
  892. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  893. return error;
  894. }
  895. /*
  896. * Update destination inode size & cowextsize hint, if necessary.
  897. */
  898. STATIC int
  899. xfs_reflink_update_dest(
  900. struct xfs_inode *dest,
  901. xfs_off_t newlen,
  902. xfs_extlen_t cowextsize,
  903. bool is_dedupe)
  904. {
  905. struct xfs_mount *mp = dest->i_mount;
  906. struct xfs_trans *tp;
  907. int error;
  908. if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  909. return 0;
  910. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  911. if (error)
  912. goto out_error;
  913. xfs_ilock(dest, XFS_ILOCK_EXCL);
  914. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  915. if (newlen > i_size_read(VFS_I(dest))) {
  916. trace_xfs_reflink_update_inode_size(dest, newlen);
  917. i_size_write(VFS_I(dest), newlen);
  918. dest->i_d.di_size = newlen;
  919. }
  920. if (cowextsize) {
  921. dest->i_d.di_cowextsize = cowextsize;
  922. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  923. }
  924. if (!is_dedupe) {
  925. xfs_trans_ichgtime(tp, dest,
  926. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  927. }
  928. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  929. error = xfs_trans_commit(tp);
  930. if (error)
  931. goto out_error;
  932. return error;
  933. out_error:
  934. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  935. return error;
  936. }
  937. /*
  938. * Do we have enough reserve in this AG to handle a reflink? The refcount
  939. * btree already reserved all the space it needs, but the rmap btree can grow
  940. * infinitely, so we won't allow more reflinks when the AG is down to the
  941. * btree reserves.
  942. */
  943. static int
  944. xfs_reflink_ag_has_free_space(
  945. struct xfs_mount *mp,
  946. xfs_agnumber_t agno)
  947. {
  948. struct xfs_perag *pag;
  949. int error = 0;
  950. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  951. return 0;
  952. pag = xfs_perag_get(mp, agno);
  953. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
  954. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  955. error = -ENOSPC;
  956. xfs_perag_put(pag);
  957. return error;
  958. }
  959. /*
  960. * Unmap a range of blocks from a file, then map other blocks into the hole.
  961. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  962. * The extent irec is mapped into dest at irec->br_startoff.
  963. */
  964. STATIC int
  965. xfs_reflink_remap_extent(
  966. struct xfs_inode *ip,
  967. struct xfs_bmbt_irec *irec,
  968. xfs_fileoff_t destoff,
  969. xfs_off_t new_isize)
  970. {
  971. struct xfs_mount *mp = ip->i_mount;
  972. struct xfs_trans *tp;
  973. xfs_fsblock_t firstfsb;
  974. unsigned int resblks;
  975. struct xfs_defer_ops dfops;
  976. struct xfs_bmbt_irec uirec;
  977. bool real_extent;
  978. xfs_filblks_t rlen;
  979. xfs_filblks_t unmap_len;
  980. xfs_off_t newlen;
  981. int error;
  982. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  983. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  984. /* Only remap normal extents. */
  985. real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
  986. irec->br_startblock != DELAYSTARTBLOCK &&
  987. !ISUNWRITTEN(irec));
  988. /* No reflinking if we're low on space */
  989. if (real_extent) {
  990. error = xfs_reflink_ag_has_free_space(mp,
  991. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  992. if (error)
  993. goto out;
  994. }
  995. /* Start a rolling transaction to switch the mappings */
  996. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  997. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  998. if (error)
  999. goto out;
  1000. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1001. xfs_trans_ijoin(tp, ip, 0);
  1002. /* If we're not just clearing space, then do we have enough quota? */
  1003. if (real_extent) {
  1004. error = xfs_trans_reserve_quota_nblks(tp, ip,
  1005. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  1006. if (error)
  1007. goto out_cancel;
  1008. }
  1009. trace_xfs_reflink_remap(ip, irec->br_startoff,
  1010. irec->br_blockcount, irec->br_startblock);
  1011. /* Unmap the old blocks in the data fork. */
  1012. rlen = unmap_len;
  1013. while (rlen) {
  1014. xfs_defer_init(&dfops, &firstfsb);
  1015. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
  1016. &firstfsb, &dfops);
  1017. if (error)
  1018. goto out_defer;
  1019. /*
  1020. * Trim the extent to whatever got unmapped.
  1021. * Remember, bunmapi works backwards.
  1022. */
  1023. uirec.br_startblock = irec->br_startblock + rlen;
  1024. uirec.br_startoff = irec->br_startoff + rlen;
  1025. uirec.br_blockcount = unmap_len - rlen;
  1026. unmap_len = rlen;
  1027. /* If this isn't a real mapping, we're done. */
  1028. if (!real_extent || uirec.br_blockcount == 0)
  1029. goto next_extent;
  1030. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  1031. uirec.br_blockcount, uirec.br_startblock);
  1032. /* Update the refcount tree */
  1033. error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
  1034. if (error)
  1035. goto out_defer;
  1036. /* Map the new blocks into the data fork. */
  1037. error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
  1038. if (error)
  1039. goto out_defer;
  1040. /* Update quota accounting. */
  1041. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  1042. uirec.br_blockcount);
  1043. /* Update dest isize if needed. */
  1044. newlen = XFS_FSB_TO_B(mp,
  1045. uirec.br_startoff + uirec.br_blockcount);
  1046. newlen = min_t(xfs_off_t, newlen, new_isize);
  1047. if (newlen > i_size_read(VFS_I(ip))) {
  1048. trace_xfs_reflink_update_inode_size(ip, newlen);
  1049. i_size_write(VFS_I(ip), newlen);
  1050. ip->i_d.di_size = newlen;
  1051. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1052. }
  1053. next_extent:
  1054. /* Process all the deferred stuff. */
  1055. error = xfs_defer_finish(&tp, &dfops, ip);
  1056. if (error)
  1057. goto out_defer;
  1058. }
  1059. error = xfs_trans_commit(tp);
  1060. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1061. if (error)
  1062. goto out;
  1063. return 0;
  1064. out_defer:
  1065. xfs_defer_cancel(&dfops);
  1066. out_cancel:
  1067. xfs_trans_cancel(tp);
  1068. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1069. out:
  1070. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  1071. return error;
  1072. }
  1073. /*
  1074. * Iteratively remap one file's extents (and holes) to another's.
  1075. */
  1076. STATIC int
  1077. xfs_reflink_remap_blocks(
  1078. struct xfs_inode *src,
  1079. xfs_fileoff_t srcoff,
  1080. struct xfs_inode *dest,
  1081. xfs_fileoff_t destoff,
  1082. xfs_filblks_t len,
  1083. xfs_off_t new_isize)
  1084. {
  1085. struct xfs_bmbt_irec imap;
  1086. int nimaps;
  1087. int error = 0;
  1088. xfs_filblks_t range_len;
  1089. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  1090. while (len) {
  1091. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  1092. dest, destoff);
  1093. /* Read extent from the source file */
  1094. nimaps = 1;
  1095. xfs_ilock(src, XFS_ILOCK_EXCL);
  1096. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  1097. xfs_iunlock(src, XFS_ILOCK_EXCL);
  1098. if (error)
  1099. goto err;
  1100. ASSERT(nimaps == 1);
  1101. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  1102. &imap);
  1103. /* Translate imap into the destination file. */
  1104. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  1105. imap.br_startoff += destoff - srcoff;
  1106. /* Clear dest from destoff to the end of imap and map it in. */
  1107. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  1108. new_isize);
  1109. if (error)
  1110. goto err;
  1111. if (fatal_signal_pending(current)) {
  1112. error = -EINTR;
  1113. goto err;
  1114. }
  1115. /* Advance drange/srange */
  1116. srcoff += range_len;
  1117. destoff += range_len;
  1118. len -= range_len;
  1119. }
  1120. return 0;
  1121. err:
  1122. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  1123. return error;
  1124. }
  1125. /*
  1126. * Read a page's worth of file data into the page cache. Return the page
  1127. * locked.
  1128. */
  1129. static struct page *
  1130. xfs_get_page(
  1131. struct inode *inode,
  1132. xfs_off_t offset)
  1133. {
  1134. struct address_space *mapping;
  1135. struct page *page;
  1136. pgoff_t n;
  1137. n = offset >> PAGE_SHIFT;
  1138. mapping = inode->i_mapping;
  1139. page = read_mapping_page(mapping, n, NULL);
  1140. if (IS_ERR(page))
  1141. return page;
  1142. if (!PageUptodate(page)) {
  1143. put_page(page);
  1144. return ERR_PTR(-EIO);
  1145. }
  1146. lock_page(page);
  1147. return page;
  1148. }
  1149. /*
  1150. * Compare extents of two files to see if they are the same.
  1151. */
  1152. static int
  1153. xfs_compare_extents(
  1154. struct inode *src,
  1155. xfs_off_t srcoff,
  1156. struct inode *dest,
  1157. xfs_off_t destoff,
  1158. xfs_off_t len,
  1159. bool *is_same)
  1160. {
  1161. xfs_off_t src_poff;
  1162. xfs_off_t dest_poff;
  1163. void *src_addr;
  1164. void *dest_addr;
  1165. struct page *src_page;
  1166. struct page *dest_page;
  1167. xfs_off_t cmp_len;
  1168. bool same;
  1169. int error;
  1170. error = -EINVAL;
  1171. same = true;
  1172. while (len) {
  1173. src_poff = srcoff & (PAGE_SIZE - 1);
  1174. dest_poff = destoff & (PAGE_SIZE - 1);
  1175. cmp_len = min(PAGE_SIZE - src_poff,
  1176. PAGE_SIZE - dest_poff);
  1177. cmp_len = min(cmp_len, len);
  1178. ASSERT(cmp_len > 0);
  1179. trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
  1180. XFS_I(dest), destoff);
  1181. src_page = xfs_get_page(src, srcoff);
  1182. if (IS_ERR(src_page)) {
  1183. error = PTR_ERR(src_page);
  1184. goto out_error;
  1185. }
  1186. dest_page = xfs_get_page(dest, destoff);
  1187. if (IS_ERR(dest_page)) {
  1188. error = PTR_ERR(dest_page);
  1189. unlock_page(src_page);
  1190. put_page(src_page);
  1191. goto out_error;
  1192. }
  1193. src_addr = kmap_atomic(src_page);
  1194. dest_addr = kmap_atomic(dest_page);
  1195. flush_dcache_page(src_page);
  1196. flush_dcache_page(dest_page);
  1197. if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
  1198. same = false;
  1199. kunmap_atomic(dest_addr);
  1200. kunmap_atomic(src_addr);
  1201. unlock_page(dest_page);
  1202. unlock_page(src_page);
  1203. put_page(dest_page);
  1204. put_page(src_page);
  1205. if (!same)
  1206. break;
  1207. srcoff += cmp_len;
  1208. destoff += cmp_len;
  1209. len -= cmp_len;
  1210. }
  1211. *is_same = same;
  1212. return 0;
  1213. out_error:
  1214. trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
  1215. return error;
  1216. }
  1217. /*
  1218. * Link a range of blocks from one file to another.
  1219. */
  1220. int
  1221. xfs_reflink_remap_range(
  1222. struct file *file_in,
  1223. loff_t pos_in,
  1224. struct file *file_out,
  1225. loff_t pos_out,
  1226. u64 len,
  1227. bool is_dedupe)
  1228. {
  1229. struct inode *inode_in = file_inode(file_in);
  1230. struct xfs_inode *src = XFS_I(inode_in);
  1231. struct inode *inode_out = file_inode(file_out);
  1232. struct xfs_inode *dest = XFS_I(inode_out);
  1233. struct xfs_mount *mp = src->i_mount;
  1234. loff_t bs = inode_out->i_sb->s_blocksize;
  1235. bool same_inode = (inode_in == inode_out);
  1236. xfs_fileoff_t sfsbno, dfsbno;
  1237. xfs_filblks_t fsblen;
  1238. xfs_extlen_t cowextsize;
  1239. loff_t isize;
  1240. ssize_t ret;
  1241. loff_t blen;
  1242. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  1243. return -EOPNOTSUPP;
  1244. if (XFS_FORCED_SHUTDOWN(mp))
  1245. return -EIO;
  1246. /* Lock both files against IO */
  1247. if (same_inode) {
  1248. xfs_ilock(src, XFS_IOLOCK_EXCL);
  1249. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1250. } else {
  1251. xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
  1252. xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
  1253. }
  1254. /* Don't touch certain kinds of inodes */
  1255. ret = -EPERM;
  1256. if (IS_IMMUTABLE(inode_out))
  1257. goto out_unlock;
  1258. ret = -ETXTBSY;
  1259. if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
  1260. goto out_unlock;
  1261. /* Don't reflink dirs, pipes, sockets... */
  1262. ret = -EISDIR;
  1263. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1264. goto out_unlock;
  1265. ret = -EINVAL;
  1266. if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
  1267. goto out_unlock;
  1268. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1269. goto out_unlock;
  1270. /* Don't reflink realtime inodes */
  1271. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1272. goto out_unlock;
  1273. /* Don't share DAX file data for now. */
  1274. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1275. goto out_unlock;
  1276. /* Are we going all the way to the end? */
  1277. isize = i_size_read(inode_in);
  1278. if (isize == 0) {
  1279. ret = 0;
  1280. goto out_unlock;
  1281. }
  1282. /* Zero length dedupe exits immediately; reflink goes to EOF. */
  1283. if (len == 0) {
  1284. if (is_dedupe) {
  1285. ret = 0;
  1286. goto out_unlock;
  1287. }
  1288. len = isize - pos_in;
  1289. }
  1290. /* Ensure offsets don't wrap and the input is inside i_size */
  1291. if (pos_in + len < pos_in || pos_out + len < pos_out ||
  1292. pos_in + len > isize)
  1293. goto out_unlock;
  1294. /* Don't allow dedupe past EOF in the dest file */
  1295. if (is_dedupe) {
  1296. loff_t disize;
  1297. disize = i_size_read(inode_out);
  1298. if (pos_out >= disize || pos_out + len > disize)
  1299. goto out_unlock;
  1300. }
  1301. /* If we're linking to EOF, continue to the block boundary. */
  1302. if (pos_in + len == isize)
  1303. blen = ALIGN(isize, bs) - pos_in;
  1304. else
  1305. blen = len;
  1306. /* Only reflink if we're aligned to block boundaries */
  1307. if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
  1308. !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
  1309. goto out_unlock;
  1310. /* Don't allow overlapped reflink within the same file */
  1311. if (same_inode) {
  1312. if (pos_out + blen > pos_in && pos_out < pos_in + blen)
  1313. goto out_unlock;
  1314. }
  1315. /* Wait for the completion of any pending IOs on both files */
  1316. inode_dio_wait(inode_in);
  1317. if (!same_inode)
  1318. inode_dio_wait(inode_out);
  1319. ret = filemap_write_and_wait_range(inode_in->i_mapping,
  1320. pos_in, pos_in + len - 1);
  1321. if (ret)
  1322. goto out_unlock;
  1323. ret = filemap_write_and_wait_range(inode_out->i_mapping,
  1324. pos_out, pos_out + len - 1);
  1325. if (ret)
  1326. goto out_unlock;
  1327. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1328. /*
  1329. * Check that the extents are the same.
  1330. */
  1331. if (is_dedupe) {
  1332. bool is_same = false;
  1333. ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
  1334. len, &is_same);
  1335. if (ret)
  1336. goto out_unlock;
  1337. if (!is_same) {
  1338. ret = -EBADE;
  1339. goto out_unlock;
  1340. }
  1341. }
  1342. ret = xfs_reflink_set_inode_flag(src, dest);
  1343. if (ret)
  1344. goto out_unlock;
  1345. /*
  1346. * Invalidate the page cache so that we can clear any CoW mappings
  1347. * in the destination file.
  1348. */
  1349. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1350. PAGE_ALIGN(pos_out + len) - 1);
  1351. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1352. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1353. fsblen = XFS_B_TO_FSB(mp, len);
  1354. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1355. pos_out + len);
  1356. if (ret)
  1357. goto out_unlock;
  1358. /*
  1359. * Carry the cowextsize hint from src to dest if we're sharing the
  1360. * entire source file to the entire destination file, the source file
  1361. * has a cowextsize hint, and the destination file does not.
  1362. */
  1363. cowextsize = 0;
  1364. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1365. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1366. pos_out == 0 && len >= i_size_read(inode_out) &&
  1367. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1368. cowextsize = src->i_d.di_cowextsize;
  1369. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
  1370. is_dedupe);
  1371. out_unlock:
  1372. xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
  1373. xfs_iunlock(src, XFS_IOLOCK_EXCL);
  1374. if (src->i_ino != dest->i_ino) {
  1375. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1376. xfs_iunlock(dest, XFS_IOLOCK_EXCL);
  1377. }
  1378. if (ret)
  1379. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1380. return ret;
  1381. }
  1382. /*
  1383. * The user wants to preemptively CoW all shared blocks in this file,
  1384. * which enables us to turn off the reflink flag. Iterate all
  1385. * extents which are not prealloc/delalloc to see which ranges are
  1386. * mentioned in the refcount tree, then read those blocks into the
  1387. * pagecache, dirty them, fsync them back out, and then we can update
  1388. * the inode flag. What happens if we run out of memory? :)
  1389. */
  1390. STATIC int
  1391. xfs_reflink_dirty_extents(
  1392. struct xfs_inode *ip,
  1393. xfs_fileoff_t fbno,
  1394. xfs_filblks_t end,
  1395. xfs_off_t isize)
  1396. {
  1397. struct xfs_mount *mp = ip->i_mount;
  1398. xfs_agnumber_t agno;
  1399. xfs_agblock_t agbno;
  1400. xfs_extlen_t aglen;
  1401. xfs_agblock_t rbno;
  1402. xfs_extlen_t rlen;
  1403. xfs_off_t fpos;
  1404. xfs_off_t flen;
  1405. struct xfs_bmbt_irec map[2];
  1406. int nmaps;
  1407. int error = 0;
  1408. while (end - fbno > 0) {
  1409. nmaps = 1;
  1410. /*
  1411. * Look for extents in the file. Skip holes, delalloc, or
  1412. * unwritten extents; they can't be reflinked.
  1413. */
  1414. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1415. if (error)
  1416. goto out;
  1417. if (nmaps == 0)
  1418. break;
  1419. if (map[0].br_startblock == HOLESTARTBLOCK ||
  1420. map[0].br_startblock == DELAYSTARTBLOCK ||
  1421. ISUNWRITTEN(&map[0]))
  1422. goto next;
  1423. map[1] = map[0];
  1424. while (map[1].br_blockcount) {
  1425. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1426. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1427. aglen = map[1].br_blockcount;
  1428. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1429. &rbno, &rlen, true);
  1430. if (error)
  1431. goto out;
  1432. if (rbno == NULLAGBLOCK)
  1433. break;
  1434. /* Dirty the pages */
  1435. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1436. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1437. (rbno - agbno));
  1438. flen = XFS_FSB_TO_B(mp, rlen);
  1439. if (fpos + flen > isize)
  1440. flen = isize - fpos;
  1441. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1442. &xfs_iomap_ops);
  1443. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1444. if (error)
  1445. goto out;
  1446. map[1].br_blockcount -= (rbno - agbno + rlen);
  1447. map[1].br_startoff += (rbno - agbno + rlen);
  1448. map[1].br_startblock += (rbno - agbno + rlen);
  1449. }
  1450. next:
  1451. fbno = map[0].br_startoff + map[0].br_blockcount;
  1452. }
  1453. out:
  1454. return error;
  1455. }
  1456. /* Clear the inode reflink flag if there are no shared extents. */
  1457. int
  1458. xfs_reflink_clear_inode_flag(
  1459. struct xfs_inode *ip,
  1460. struct xfs_trans **tpp)
  1461. {
  1462. struct xfs_mount *mp = ip->i_mount;
  1463. xfs_fileoff_t fbno;
  1464. xfs_filblks_t end;
  1465. xfs_agnumber_t agno;
  1466. xfs_agblock_t agbno;
  1467. xfs_extlen_t aglen;
  1468. xfs_agblock_t rbno;
  1469. xfs_extlen_t rlen;
  1470. struct xfs_bmbt_irec map;
  1471. int nmaps;
  1472. int error = 0;
  1473. ASSERT(xfs_is_reflink_inode(ip));
  1474. fbno = 0;
  1475. end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
  1476. while (end - fbno > 0) {
  1477. nmaps = 1;
  1478. /*
  1479. * Look for extents in the file. Skip holes, delalloc, or
  1480. * unwritten extents; they can't be reflinked.
  1481. */
  1482. error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
  1483. if (error)
  1484. return error;
  1485. if (nmaps == 0)
  1486. break;
  1487. if (map.br_startblock == HOLESTARTBLOCK ||
  1488. map.br_startblock == DELAYSTARTBLOCK ||
  1489. ISUNWRITTEN(&map))
  1490. goto next;
  1491. agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
  1492. agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
  1493. aglen = map.br_blockcount;
  1494. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1495. &rbno, &rlen, false);
  1496. if (error)
  1497. return error;
  1498. /* Is there still a shared block here? */
  1499. if (rbno != NULLAGBLOCK)
  1500. return 0;
  1501. next:
  1502. fbno = map.br_startoff + map.br_blockcount;
  1503. }
  1504. /*
  1505. * We didn't find any shared blocks so turn off the reflink flag.
  1506. * First, get rid of any leftover CoW mappings.
  1507. */
  1508. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
  1509. if (error)
  1510. return error;
  1511. /* Clear the inode flag. */
  1512. trace_xfs_reflink_unset_inode_flag(ip);
  1513. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1514. xfs_inode_clear_cowblocks_tag(ip);
  1515. xfs_trans_ijoin(*tpp, ip, 0);
  1516. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1517. return error;
  1518. }
  1519. /*
  1520. * Clear the inode reflink flag if there are no shared extents and the size
  1521. * hasn't changed.
  1522. */
  1523. STATIC int
  1524. xfs_reflink_try_clear_inode_flag(
  1525. struct xfs_inode *ip)
  1526. {
  1527. struct xfs_mount *mp = ip->i_mount;
  1528. struct xfs_trans *tp;
  1529. int error = 0;
  1530. /* Start a rolling transaction to remove the mappings */
  1531. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1532. if (error)
  1533. return error;
  1534. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1535. xfs_trans_ijoin(tp, ip, 0);
  1536. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1537. if (error)
  1538. goto cancel;
  1539. error = xfs_trans_commit(tp);
  1540. if (error)
  1541. goto out;
  1542. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1543. return 0;
  1544. cancel:
  1545. xfs_trans_cancel(tp);
  1546. out:
  1547. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1548. return error;
  1549. }
  1550. /*
  1551. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1552. * the reflink flag if we unshare all of the file's blocks.
  1553. */
  1554. int
  1555. xfs_reflink_unshare(
  1556. struct xfs_inode *ip,
  1557. xfs_off_t offset,
  1558. xfs_off_t len)
  1559. {
  1560. struct xfs_mount *mp = ip->i_mount;
  1561. xfs_fileoff_t fbno;
  1562. xfs_filblks_t end;
  1563. xfs_off_t isize;
  1564. int error;
  1565. if (!xfs_is_reflink_inode(ip))
  1566. return 0;
  1567. trace_xfs_reflink_unshare(ip, offset, len);
  1568. inode_dio_wait(VFS_I(ip));
  1569. /* Try to CoW the selected ranges */
  1570. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1571. fbno = XFS_B_TO_FSBT(mp, offset);
  1572. isize = i_size_read(VFS_I(ip));
  1573. end = XFS_B_TO_FSB(mp, offset + len);
  1574. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1575. if (error)
  1576. goto out_unlock;
  1577. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1578. /* Wait for the IO to finish */
  1579. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1580. if (error)
  1581. goto out;
  1582. /* Turn off the reflink flag if possible. */
  1583. error = xfs_reflink_try_clear_inode_flag(ip);
  1584. if (error)
  1585. goto out;
  1586. return 0;
  1587. out_unlock:
  1588. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1589. out:
  1590. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1591. return error;
  1592. }