xfs_reflink.c 42 KB

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