xfs_reflink.c 44 KB

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