xfs_reflink.c 43 KB

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