xfs_reflink.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_log_format.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_bmap_util.h"
  35. #include "xfs_error.h"
  36. #include "xfs_dir2.h"
  37. #include "xfs_dir2_priv.h"
  38. #include "xfs_ioctl.h"
  39. #include "xfs_trace.h"
  40. #include "xfs_log.h"
  41. #include "xfs_icache.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_btree.h"
  44. #include "xfs_refcount_btree.h"
  45. #include "xfs_refcount.h"
  46. #include "xfs_bmap_btree.h"
  47. #include "xfs_trans_space.h"
  48. #include "xfs_bit.h"
  49. #include "xfs_alloc.h"
  50. #include "xfs_quota_defs.h"
  51. #include "xfs_quota.h"
  52. #include "xfs_btree.h"
  53. #include "xfs_bmap_btree.h"
  54. #include "xfs_reflink.h"
  55. #include "xfs_iomap.h"
  56. #include "xfs_rmap_btree.h"
  57. #include "xfs_sb.h"
  58. #include "xfs_ag_resv.h"
  59. /*
  60. * Copy on Write of Shared Blocks
  61. *
  62. * XFS must preserve "the usual" file semantics even when two files share
  63. * the same physical blocks. This means that a write to one file must not
  64. * alter the blocks in a different file; the way that we'll do that is
  65. * through the use of a copy-on-write mechanism. At a high level, that
  66. * means that when we want to write to a shared block, we allocate a new
  67. * block, write the data to the new block, and if that succeeds we map the
  68. * new block into the file.
  69. *
  70. * XFS provides a "delayed allocation" mechanism that defers the allocation
  71. * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
  72. * possible. This reduces fragmentation by enabling the filesystem to ask
  73. * for bigger chunks less often, which is exactly what we want for CoW.
  74. *
  75. * The delalloc mechanism begins when the kernel wants to make a block
  76. * writable (write_begin or page_mkwrite). If the offset is not mapped, we
  77. * create a delalloc mapping, which is a regular in-core extent, but without
  78. * a real startblock. (For delalloc mappings, the startblock encodes both
  79. * a flag that this is a delalloc mapping, and a worst-case estimate of how
  80. * many blocks might be required to put the mapping into the BMBT.) delalloc
  81. * mappings are a reservation against the free space in the filesystem;
  82. * adjacent mappings can also be combined into fewer larger mappings.
  83. *
  84. * When dirty pages are being written out (typically in writepage), the
  85. * delalloc reservations are converted into real mappings by allocating
  86. * blocks and replacing the delalloc mapping with real ones. A delalloc
  87. * mapping can be replaced by several real ones if the free space is
  88. * fragmented.
  89. *
  90. * We want to adapt the delalloc mechanism for copy-on-write, since the
  91. * write paths are similar. The first two steps (creating the reservation
  92. * and allocating the blocks) are exactly the same as delalloc except that
  93. * the mappings must be stored in a separate CoW fork because we do not want
  94. * to disturb the mapping in the data fork until we're sure that the write
  95. * succeeded. IO completion in this case is the process of removing the old
  96. * mapping from the data fork and moving the new mapping from the CoW fork to
  97. * the data fork. This will be discussed shortly.
  98. *
  99. * For now, unaligned directio writes will be bounced back to the page cache.
  100. * Block-aligned directio writes will use the same mechanism as buffered
  101. * writes.
  102. *
  103. * CoW remapping must be done after the data block write completes,
  104. * because we don't want to destroy the old data fork map until we're sure
  105. * the new block has been written. Since the new mappings are kept in a
  106. * separate fork, we can simply iterate these mappings to find the ones
  107. * that cover the file blocks that we just CoW'd. For each extent, simply
  108. * unmap the corresponding range in the data fork, map the new range into
  109. * the data fork, and remove the extent from the CoW fork.
  110. *
  111. * Since the remapping operation can be applied to an arbitrary file
  112. * range, we record the need for the remap step as a flag in the ioend
  113. * instead of declaring a new IO type. This is required for direct io
  114. * because we only have ioend for the whole dio, and we have to be able to
  115. * remember the presence of unwritten blocks and CoW blocks with a single
  116. * ioend structure. Better yet, the more ground we can cover with one
  117. * ioend, the better.
  118. */
  119. /*
  120. * Given an AG extent, find the lowest-numbered run of shared blocks
  121. * within that range and return the range in fbno/flen. If
  122. * find_end_of_shared is true, return the longest contiguous extent of
  123. * shared blocks. If there are no shared extents, fbno and flen will
  124. * be set to NULLAGBLOCK and 0, respectively.
  125. */
  126. int
  127. xfs_reflink_find_shared(
  128. struct xfs_mount *mp,
  129. xfs_agnumber_t agno,
  130. xfs_agblock_t agbno,
  131. xfs_extlen_t aglen,
  132. xfs_agblock_t *fbno,
  133. xfs_extlen_t *flen,
  134. bool find_end_of_shared)
  135. {
  136. struct xfs_buf *agbp;
  137. struct xfs_btree_cur *cur;
  138. int error;
  139. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  140. if (error)
  141. return error;
  142. cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
  143. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  144. find_end_of_shared);
  145. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  146. xfs_buf_relse(agbp);
  147. return error;
  148. }
  149. /*
  150. * Trim the mapping to the next block where there's a change in the
  151. * shared/unshared status. More specifically, this means that we
  152. * find the lowest-numbered extent of shared blocks that coincides with
  153. * the given block mapping. If the shared extent overlaps the start of
  154. * the mapping, trim the mapping to the end of the shared extent. If
  155. * the shared region intersects the mapping, trim the mapping to the
  156. * start of the shared extent. If there are no shared regions that
  157. * overlap, just return the original extent.
  158. */
  159. int
  160. xfs_reflink_trim_around_shared(
  161. struct xfs_inode *ip,
  162. struct xfs_bmbt_irec *irec,
  163. bool *shared,
  164. bool *trimmed)
  165. {
  166. xfs_agnumber_t agno;
  167. xfs_agblock_t agbno;
  168. xfs_extlen_t aglen;
  169. xfs_agblock_t fbno;
  170. xfs_extlen_t flen;
  171. int error = 0;
  172. /* Holes, unwritten, and delalloc extents cannot be shared */
  173. if (!xfs_is_reflink_inode(ip) ||
  174. ISUNWRITTEN(irec) ||
  175. irec->br_startblock == HOLESTARTBLOCK ||
  176. irec->br_startblock == DELAYSTARTBLOCK ||
  177. isnullstartblock(irec->br_startblock)) {
  178. *shared = false;
  179. return 0;
  180. }
  181. trace_xfs_reflink_trim_around_shared(ip, irec);
  182. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  183. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  184. aglen = irec->br_blockcount;
  185. error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
  186. aglen, &fbno, &flen, true);
  187. if (error)
  188. return error;
  189. *shared = *trimmed = false;
  190. if (fbno == NULLAGBLOCK) {
  191. /* No shared blocks at all. */
  192. return 0;
  193. } else if (fbno == agbno) {
  194. /*
  195. * The start of this extent is shared. Truncate the
  196. * mapping at the end of the shared region so that a
  197. * subsequent iteration starts at the start of the
  198. * unshared region.
  199. */
  200. irec->br_blockcount = flen;
  201. *shared = true;
  202. if (flen != aglen)
  203. *trimmed = true;
  204. return 0;
  205. } else {
  206. /*
  207. * There's a shared extent midway through this extent.
  208. * Truncate the mapping at the start of the shared
  209. * extent so that a subsequent iteration starts at the
  210. * start of the shared region.
  211. */
  212. irec->br_blockcount = fbno - agbno;
  213. *trimmed = true;
  214. return 0;
  215. }
  216. }
  217. /*
  218. * Trim the passed in imap to the next shared/unshared extent boundary, and
  219. * if imap->br_startoff points to a shared extent reserve space for it in the
  220. * COW fork. In this case *shared is set to true, else to false.
  221. *
  222. * Note that imap will always contain the block numbers for the existing blocks
  223. * in the data fork, as the upper layers need them for read-modify-write
  224. * operations.
  225. */
  226. int
  227. xfs_reflink_reserve_cow(
  228. struct xfs_inode *ip,
  229. struct xfs_bmbt_irec *imap,
  230. bool *shared)
  231. {
  232. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  233. struct xfs_bmbt_irec got;
  234. int error = 0;
  235. bool eof = false, trimmed;
  236. xfs_extnum_t idx;
  237. /*
  238. * Search the COW fork extent list first. This serves two purposes:
  239. * first this implement the speculative preallocation using cowextisze,
  240. * so that we also unshared block adjacent to shared blocks instead
  241. * of just the shared blocks themselves. Second the lookup in the
  242. * extent list is generally faster than going out to the shared extent
  243. * tree.
  244. */
  245. if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
  246. eof = true;
  247. if (!eof && got.br_startoff <= imap->br_startoff) {
  248. trace_xfs_reflink_cow_found(ip, imap);
  249. xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
  250. *shared = true;
  251. return 0;
  252. }
  253. /* Trim the mapping to the nearest shared extent boundary. */
  254. error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
  255. if (error)
  256. return error;
  257. /* Not shared? Just report the (potentially capped) extent. */
  258. if (!*shared)
  259. return 0;
  260. /*
  261. * Fork all the shared blocks from our write offset until the end of
  262. * the extent.
  263. */
  264. error = xfs_qm_dqattach_locked(ip, 0);
  265. if (error)
  266. return error;
  267. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
  268. imap->br_blockcount, 0, &got, &idx, eof);
  269. if (error == -ENOSPC || error == -EDQUOT)
  270. trace_xfs_reflink_cow_enospc(ip, imap);
  271. if (error)
  272. return error;
  273. trace_xfs_reflink_cow_alloc(ip, &got);
  274. return 0;
  275. }
  276. /* Allocate all CoW reservations covering a range of blocks in a file. */
  277. static int
  278. __xfs_reflink_allocate_cow(
  279. struct xfs_inode *ip,
  280. xfs_fileoff_t *offset_fsb,
  281. xfs_fileoff_t end_fsb)
  282. {
  283. struct xfs_mount *mp = ip->i_mount;
  284. struct xfs_bmbt_irec imap;
  285. struct xfs_defer_ops dfops;
  286. struct xfs_trans *tp;
  287. xfs_fsblock_t first_block;
  288. int nimaps = 1, error;
  289. bool shared;
  290. xfs_defer_init(&dfops, &first_block);
  291. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
  292. XFS_TRANS_RESERVE, &tp);
  293. if (error)
  294. return error;
  295. xfs_ilock(ip, XFS_ILOCK_EXCL);
  296. /* Read extent from the source file. */
  297. nimaps = 1;
  298. error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
  299. &imap, &nimaps, 0);
  300. if (error)
  301. goto out_unlock;
  302. ASSERT(nimaps == 1);
  303. error = xfs_reflink_reserve_cow(ip, &imap, &shared);
  304. if (error)
  305. goto out_trans_cancel;
  306. if (!shared) {
  307. *offset_fsb = imap.br_startoff + imap.br_blockcount;
  308. goto out_trans_cancel;
  309. }
  310. xfs_trans_ijoin(tp, ip, 0);
  311. error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
  312. XFS_BMAPI_COWFORK, &first_block,
  313. XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
  314. &imap, &nimaps, &dfops);
  315. if (error)
  316. goto out_trans_cancel;
  317. error = xfs_defer_finish(&tp, &dfops, NULL);
  318. if (error)
  319. goto out_trans_cancel;
  320. error = xfs_trans_commit(tp);
  321. *offset_fsb = imap.br_startoff + imap.br_blockcount;
  322. out_unlock:
  323. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  324. return error;
  325. out_trans_cancel:
  326. xfs_defer_cancel(&dfops);
  327. xfs_trans_cancel(tp);
  328. goto out_unlock;
  329. }
  330. /* Allocate all CoW reservations covering a part of a file. */
  331. int
  332. xfs_reflink_allocate_cow_range(
  333. struct xfs_inode *ip,
  334. xfs_off_t offset,
  335. xfs_off_t count)
  336. {
  337. struct xfs_mount *mp = ip->i_mount;
  338. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  339. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  340. int error;
  341. ASSERT(xfs_is_reflink_inode(ip));
  342. trace_xfs_reflink_allocate_cow_range(ip, offset, count);
  343. /*
  344. * Make sure that the dquots are there.
  345. */
  346. error = xfs_qm_dqattach(ip, 0);
  347. if (error)
  348. return error;
  349. while (offset_fsb < end_fsb) {
  350. error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
  351. if (error) {
  352. trace_xfs_reflink_allocate_cow_range_error(ip, error,
  353. _RET_IP_);
  354. break;
  355. }
  356. }
  357. return error;
  358. }
  359. /*
  360. * Find the CoW reservation for a given byte offset of a file.
  361. */
  362. bool
  363. xfs_reflink_find_cow_mapping(
  364. struct xfs_inode *ip,
  365. xfs_off_t offset,
  366. struct xfs_bmbt_irec *imap)
  367. {
  368. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  369. xfs_fileoff_t offset_fsb;
  370. struct xfs_bmbt_irec got;
  371. xfs_extnum_t idx;
  372. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  373. ASSERT(xfs_is_reflink_inode(ip));
  374. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  375. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  376. return false;
  377. if (got.br_startoff > offset_fsb)
  378. return false;
  379. trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
  380. &got);
  381. *imap = got;
  382. return true;
  383. }
  384. /*
  385. * Trim an extent to end at the next CoW reservation past offset_fsb.
  386. */
  387. void
  388. xfs_reflink_trim_irec_to_next_cow(
  389. struct xfs_inode *ip,
  390. xfs_fileoff_t offset_fsb,
  391. struct xfs_bmbt_irec *imap)
  392. {
  393. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  394. struct xfs_bmbt_irec got;
  395. xfs_extnum_t idx;
  396. if (!xfs_is_reflink_inode(ip))
  397. return;
  398. /* Find the extent in the CoW fork. */
  399. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  400. return;
  401. /* This is the extent before; try sliding up one. */
  402. if (got.br_startoff < offset_fsb) {
  403. if (!xfs_iext_get_extent(ifp, idx + 1, &got))
  404. return;
  405. }
  406. if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
  407. return;
  408. imap->br_blockcount = got.br_startoff - imap->br_startoff;
  409. trace_xfs_reflink_trim_irec(ip, imap);
  410. }
  411. /*
  412. * Cancel all pending CoW reservations for some block range of an inode.
  413. */
  414. int
  415. xfs_reflink_cancel_cow_blocks(
  416. struct xfs_inode *ip,
  417. struct xfs_trans **tpp,
  418. xfs_fileoff_t offset_fsb,
  419. xfs_fileoff_t end_fsb)
  420. {
  421. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  422. struct xfs_bmbt_irec got, del;
  423. xfs_extnum_t idx;
  424. xfs_fsblock_t firstfsb;
  425. struct xfs_defer_ops dfops;
  426. int error = 0;
  427. if (!xfs_is_reflink_inode(ip))
  428. return 0;
  429. if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
  430. return 0;
  431. while (got.br_startoff < end_fsb) {
  432. del = got;
  433. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  434. trace_xfs_reflink_cancel_cow(ip, &del);
  435. if (isnullstartblock(del.br_startblock)) {
  436. error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
  437. &idx, &got, &del);
  438. if (error)
  439. break;
  440. } else {
  441. xfs_trans_ijoin(*tpp, ip, 0);
  442. xfs_defer_init(&dfops, &firstfsb);
  443. /* Free the CoW orphan record. */
  444. error = xfs_refcount_free_cow_extent(ip->i_mount,
  445. &dfops, del.br_startblock,
  446. del.br_blockcount);
  447. if (error)
  448. break;
  449. xfs_bmap_add_free(ip->i_mount, &dfops,
  450. del.br_startblock, del.br_blockcount,
  451. NULL);
  452. /* Update quota accounting */
  453. xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
  454. -(long)del.br_blockcount);
  455. /* Roll the transaction */
  456. error = xfs_defer_finish(tpp, &dfops, ip);
  457. if (error) {
  458. xfs_defer_cancel(&dfops);
  459. break;
  460. }
  461. /* Remove the mapping from the CoW fork. */
  462. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  463. }
  464. if (!xfs_iext_get_extent(ifp, ++idx, &got))
  465. break;
  466. }
  467. /* clear tag if cow fork is emptied */
  468. if (!ifp->if_bytes)
  469. xfs_inode_clear_cowblocks_tag(ip);
  470. return error;
  471. }
  472. /*
  473. * Cancel all pending CoW reservations for some byte range of an inode.
  474. */
  475. int
  476. xfs_reflink_cancel_cow_range(
  477. struct xfs_inode *ip,
  478. xfs_off_t offset,
  479. xfs_off_t count)
  480. {
  481. struct xfs_trans *tp;
  482. xfs_fileoff_t offset_fsb;
  483. xfs_fileoff_t end_fsb;
  484. int error;
  485. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  486. ASSERT(xfs_is_reflink_inode(ip));
  487. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  488. if (count == NULLFILEOFF)
  489. end_fsb = NULLFILEOFF;
  490. else
  491. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  492. /* Start a rolling transaction to remove the mappings */
  493. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  494. 0, 0, 0, &tp);
  495. if (error)
  496. goto out;
  497. xfs_ilock(ip, XFS_ILOCK_EXCL);
  498. xfs_trans_ijoin(tp, ip, 0);
  499. /* Scrape out the old CoW reservations */
  500. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
  501. if (error)
  502. goto out_cancel;
  503. error = xfs_trans_commit(tp);
  504. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  505. return error;
  506. out_cancel:
  507. xfs_trans_cancel(tp);
  508. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  509. out:
  510. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  511. return error;
  512. }
  513. /*
  514. * Remap parts of a file's data fork after a successful CoW.
  515. */
  516. int
  517. xfs_reflink_end_cow(
  518. struct xfs_inode *ip,
  519. xfs_off_t offset,
  520. xfs_off_t count)
  521. {
  522. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  523. struct xfs_bmbt_irec got, del;
  524. struct xfs_trans *tp;
  525. xfs_fileoff_t offset_fsb;
  526. xfs_fileoff_t end_fsb;
  527. xfs_fsblock_t firstfsb;
  528. struct xfs_defer_ops dfops;
  529. int error;
  530. unsigned int resblks;
  531. xfs_filblks_t rlen;
  532. xfs_extnum_t idx;
  533. trace_xfs_reflink_end_cow(ip, offset, count);
  534. /* No COW extents? That's easy! */
  535. if (ifp->if_bytes == 0)
  536. return 0;
  537. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  538. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  539. /* Start a rolling transaction to switch the mappings */
  540. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  541. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  542. resblks, 0, 0, &tp);
  543. if (error)
  544. goto out;
  545. xfs_ilock(ip, XFS_ILOCK_EXCL);
  546. xfs_trans_ijoin(tp, ip, 0);
  547. /* If there is a hole at end_fsb - 1 go to the previous extent */
  548. if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
  549. got.br_startoff > end_fsb) {
  550. ASSERT(idx > 0);
  551. xfs_iext_get_extent(ifp, --idx, &got);
  552. }
  553. /* Walk backwards until we're out of the I/O range... */
  554. while (got.br_startoff + got.br_blockcount > offset_fsb) {
  555. del = got;
  556. xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
  557. /* Extent delete may have bumped idx forward */
  558. if (!del.br_blockcount) {
  559. idx--;
  560. goto next_extent;
  561. }
  562. ASSERT(!isnullstartblock(got.br_startblock));
  563. /* Unmap the old blocks in the data fork. */
  564. xfs_defer_init(&dfops, &firstfsb);
  565. rlen = del.br_blockcount;
  566. error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
  567. &firstfsb, &dfops);
  568. if (error)
  569. goto out_defer;
  570. /* Trim the extent to whatever got unmapped. */
  571. if (rlen) {
  572. xfs_trim_extent(&del, del.br_startoff + rlen,
  573. del.br_blockcount - rlen);
  574. }
  575. trace_xfs_reflink_cow_remap(ip, &del);
  576. /* Free the CoW orphan record. */
  577. error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
  578. del.br_startblock, del.br_blockcount);
  579. if (error)
  580. goto out_defer;
  581. /* Map the new blocks into the data fork. */
  582. error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
  583. if (error)
  584. goto out_defer;
  585. /* Remove the mapping from the CoW fork. */
  586. xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
  587. error = xfs_defer_finish(&tp, &dfops, ip);
  588. if (error)
  589. goto out_defer;
  590. next_extent:
  591. if (!xfs_iext_get_extent(ifp, idx, &got))
  592. break;
  593. }
  594. error = xfs_trans_commit(tp);
  595. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  596. if (error)
  597. goto out;
  598. return 0;
  599. out_defer:
  600. xfs_defer_cancel(&dfops);
  601. xfs_trans_cancel(tp);
  602. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  603. out:
  604. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  605. return error;
  606. }
  607. /*
  608. * Free leftover CoW reservations that didn't get cleaned out.
  609. */
  610. int
  611. xfs_reflink_recover_cow(
  612. struct xfs_mount *mp)
  613. {
  614. xfs_agnumber_t agno;
  615. int error = 0;
  616. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  617. return 0;
  618. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  619. error = xfs_refcount_recover_cow_leftovers(mp, agno);
  620. if (error)
  621. break;
  622. }
  623. return error;
  624. }
  625. /*
  626. * Reflinking (Block) Ranges of Two Files Together
  627. *
  628. * First, ensure that the reflink flag is set on both inodes. The flag is an
  629. * optimization to avoid unnecessary refcount btree lookups in the write path.
  630. *
  631. * Now we can iteratively remap the range of extents (and holes) in src to the
  632. * corresponding ranges in dest. Let drange and srange denote the ranges of
  633. * logical blocks in dest and src touched by the reflink operation.
  634. *
  635. * While the length of drange is greater than zero,
  636. * - Read src's bmbt at the start of srange ("imap")
  637. * - If imap doesn't exist, make imap appear to start at the end of srange
  638. * with zero length.
  639. * - If imap starts before srange, advance imap to start at srange.
  640. * - If imap goes beyond srange, truncate imap to end at the end of srange.
  641. * - Punch (imap start - srange start + imap len) blocks from dest at
  642. * offset (drange start).
  643. * - If imap points to a real range of pblks,
  644. * > Increase the refcount of the imap's pblks
  645. * > Map imap's pblks into dest at the offset
  646. * (drange start + imap start - srange start)
  647. * - Advance drange and srange by (imap start - srange start + imap len)
  648. *
  649. * Finally, if the reflink made dest longer, update both the in-core and
  650. * on-disk file sizes.
  651. *
  652. * ASCII Art Demonstration:
  653. *
  654. * Let's say we want to reflink this source file:
  655. *
  656. * ----SSSSSSS-SSSSS----SSSSSS (src file)
  657. * <-------------------->
  658. *
  659. * into this destination file:
  660. *
  661. * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
  662. * <-------------------->
  663. * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
  664. * Observe that the range has different logical offsets in either file.
  665. *
  666. * Consider that the first extent in the source file doesn't line up with our
  667. * reflink range. Unmapping and remapping are separate operations, so we can
  668. * unmap more blocks from the destination file than we remap.
  669. *
  670. * ----SSSSSSS-SSSSS----SSSSSS
  671. * <------->
  672. * --DDDDD---------DDDDD--DDD
  673. * <------->
  674. *
  675. * Now remap the source extent into the destination file:
  676. *
  677. * ----SSSSSSS-SSSSS----SSSSSS
  678. * <------->
  679. * --DDDDD--SSSSSSSDDDDD--DDD
  680. * <------->
  681. *
  682. * Do likewise with the second hole and extent in our range. Holes in the
  683. * unmap range don't affect our operation.
  684. *
  685. * ----SSSSSSS-SSSSS----SSSSSS
  686. * <---->
  687. * --DDDDD--SSSSSSS-SSSSS-DDD
  688. * <---->
  689. *
  690. * Finally, unmap and remap part of the third extent. This will increase the
  691. * size of the destination file.
  692. *
  693. * ----SSSSSSS-SSSSS----SSSSSS
  694. * <----->
  695. * --DDDDD--SSSSSSS-SSSSS----SSS
  696. * <----->
  697. *
  698. * Once we update the destination file's i_size, we're done.
  699. */
  700. /*
  701. * Ensure the reflink bit is set in both inodes.
  702. */
  703. STATIC int
  704. xfs_reflink_set_inode_flag(
  705. struct xfs_inode *src,
  706. struct xfs_inode *dest)
  707. {
  708. struct xfs_mount *mp = src->i_mount;
  709. int error;
  710. struct xfs_trans *tp;
  711. if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
  712. return 0;
  713. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  714. if (error)
  715. goto out_error;
  716. /* Lock both files against IO */
  717. if (src->i_ino == dest->i_ino)
  718. xfs_ilock(src, XFS_ILOCK_EXCL);
  719. else
  720. xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
  721. if (!xfs_is_reflink_inode(src)) {
  722. trace_xfs_reflink_set_inode_flag(src);
  723. xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
  724. src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  725. xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
  726. xfs_ifork_init_cow(src);
  727. } else
  728. xfs_iunlock(src, XFS_ILOCK_EXCL);
  729. if (src->i_ino == dest->i_ino)
  730. goto commit_flags;
  731. if (!xfs_is_reflink_inode(dest)) {
  732. trace_xfs_reflink_set_inode_flag(dest);
  733. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  734. dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  735. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  736. xfs_ifork_init_cow(dest);
  737. } else
  738. xfs_iunlock(dest, XFS_ILOCK_EXCL);
  739. commit_flags:
  740. error = xfs_trans_commit(tp);
  741. if (error)
  742. goto out_error;
  743. return error;
  744. out_error:
  745. trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
  746. return error;
  747. }
  748. /*
  749. * Update destination inode size & cowextsize hint, if necessary.
  750. */
  751. STATIC int
  752. xfs_reflink_update_dest(
  753. struct xfs_inode *dest,
  754. xfs_off_t newlen,
  755. xfs_extlen_t cowextsize)
  756. {
  757. struct xfs_mount *mp = dest->i_mount;
  758. struct xfs_trans *tp;
  759. int error;
  760. if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
  761. return 0;
  762. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  763. if (error)
  764. goto out_error;
  765. xfs_ilock(dest, XFS_ILOCK_EXCL);
  766. xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
  767. if (newlen > i_size_read(VFS_I(dest))) {
  768. trace_xfs_reflink_update_inode_size(dest, newlen);
  769. i_size_write(VFS_I(dest), newlen);
  770. dest->i_d.di_size = newlen;
  771. }
  772. if (cowextsize) {
  773. dest->i_d.di_cowextsize = cowextsize;
  774. dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  775. }
  776. xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
  777. error = xfs_trans_commit(tp);
  778. if (error)
  779. goto out_error;
  780. return error;
  781. out_error:
  782. trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
  783. return error;
  784. }
  785. /*
  786. * Do we have enough reserve in this AG to handle a reflink? The refcount
  787. * btree already reserved all the space it needs, but the rmap btree can grow
  788. * infinitely, so we won't allow more reflinks when the AG is down to the
  789. * btree reserves.
  790. */
  791. static int
  792. xfs_reflink_ag_has_free_space(
  793. struct xfs_mount *mp,
  794. xfs_agnumber_t agno)
  795. {
  796. struct xfs_perag *pag;
  797. int error = 0;
  798. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  799. return 0;
  800. pag = xfs_perag_get(mp, agno);
  801. if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
  802. xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
  803. error = -ENOSPC;
  804. xfs_perag_put(pag);
  805. return error;
  806. }
  807. /*
  808. * Unmap a range of blocks from a file, then map other blocks into the hole.
  809. * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
  810. * The extent irec is mapped into dest at irec->br_startoff.
  811. */
  812. STATIC int
  813. xfs_reflink_remap_extent(
  814. struct xfs_inode *ip,
  815. struct xfs_bmbt_irec *irec,
  816. xfs_fileoff_t destoff,
  817. xfs_off_t new_isize)
  818. {
  819. struct xfs_mount *mp = ip->i_mount;
  820. struct xfs_trans *tp;
  821. xfs_fsblock_t firstfsb;
  822. unsigned int resblks;
  823. struct xfs_defer_ops dfops;
  824. struct xfs_bmbt_irec uirec;
  825. bool real_extent;
  826. xfs_filblks_t rlen;
  827. xfs_filblks_t unmap_len;
  828. xfs_off_t newlen;
  829. int error;
  830. unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
  831. trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
  832. /* Only remap normal extents. */
  833. real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
  834. irec->br_startblock != DELAYSTARTBLOCK &&
  835. !ISUNWRITTEN(irec));
  836. /* No reflinking if we're low on space */
  837. if (real_extent) {
  838. error = xfs_reflink_ag_has_free_space(mp,
  839. XFS_FSB_TO_AGNO(mp, irec->br_startblock));
  840. if (error)
  841. goto out;
  842. }
  843. /* Start a rolling transaction to switch the mappings */
  844. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  845. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  846. if (error)
  847. goto out;
  848. xfs_ilock(ip, XFS_ILOCK_EXCL);
  849. xfs_trans_ijoin(tp, ip, 0);
  850. /* If we're not just clearing space, then do we have enough quota? */
  851. if (real_extent) {
  852. error = xfs_trans_reserve_quota_nblks(tp, ip,
  853. irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
  854. if (error)
  855. goto out_cancel;
  856. }
  857. trace_xfs_reflink_remap(ip, irec->br_startoff,
  858. irec->br_blockcount, irec->br_startblock);
  859. /* Unmap the old blocks in the data fork. */
  860. rlen = unmap_len;
  861. while (rlen) {
  862. xfs_defer_init(&dfops, &firstfsb);
  863. error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
  864. &firstfsb, &dfops);
  865. if (error)
  866. goto out_defer;
  867. /*
  868. * Trim the extent to whatever got unmapped.
  869. * Remember, bunmapi works backwards.
  870. */
  871. uirec.br_startblock = irec->br_startblock + rlen;
  872. uirec.br_startoff = irec->br_startoff + rlen;
  873. uirec.br_blockcount = unmap_len - rlen;
  874. unmap_len = rlen;
  875. /* If this isn't a real mapping, we're done. */
  876. if (!real_extent || uirec.br_blockcount == 0)
  877. goto next_extent;
  878. trace_xfs_reflink_remap(ip, uirec.br_startoff,
  879. uirec.br_blockcount, uirec.br_startblock);
  880. /* Update the refcount tree */
  881. error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
  882. if (error)
  883. goto out_defer;
  884. /* Map the new blocks into the data fork. */
  885. error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
  886. if (error)
  887. goto out_defer;
  888. /* Update quota accounting. */
  889. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
  890. uirec.br_blockcount);
  891. /* Update dest isize if needed. */
  892. newlen = XFS_FSB_TO_B(mp,
  893. uirec.br_startoff + uirec.br_blockcount);
  894. newlen = min_t(xfs_off_t, newlen, new_isize);
  895. if (newlen > i_size_read(VFS_I(ip))) {
  896. trace_xfs_reflink_update_inode_size(ip, newlen);
  897. i_size_write(VFS_I(ip), newlen);
  898. ip->i_d.di_size = newlen;
  899. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  900. }
  901. next_extent:
  902. /* Process all the deferred stuff. */
  903. error = xfs_defer_finish(&tp, &dfops, ip);
  904. if (error)
  905. goto out_defer;
  906. }
  907. error = xfs_trans_commit(tp);
  908. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  909. if (error)
  910. goto out;
  911. return 0;
  912. out_defer:
  913. xfs_defer_cancel(&dfops);
  914. out_cancel:
  915. xfs_trans_cancel(tp);
  916. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  917. out:
  918. trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
  919. return error;
  920. }
  921. /*
  922. * Iteratively remap one file's extents (and holes) to another's.
  923. */
  924. STATIC int
  925. xfs_reflink_remap_blocks(
  926. struct xfs_inode *src,
  927. xfs_fileoff_t srcoff,
  928. struct xfs_inode *dest,
  929. xfs_fileoff_t destoff,
  930. xfs_filblks_t len,
  931. xfs_off_t new_isize)
  932. {
  933. struct xfs_bmbt_irec imap;
  934. int nimaps;
  935. int error = 0;
  936. xfs_filblks_t range_len;
  937. /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
  938. while (len) {
  939. trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
  940. dest, destoff);
  941. /* Read extent from the source file */
  942. nimaps = 1;
  943. xfs_ilock(src, XFS_ILOCK_EXCL);
  944. error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
  945. xfs_iunlock(src, XFS_ILOCK_EXCL);
  946. if (error)
  947. goto err;
  948. ASSERT(nimaps == 1);
  949. trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
  950. &imap);
  951. /* Translate imap into the destination file. */
  952. range_len = imap.br_startoff + imap.br_blockcount - srcoff;
  953. imap.br_startoff += destoff - srcoff;
  954. /* Clear dest from destoff to the end of imap and map it in. */
  955. error = xfs_reflink_remap_extent(dest, &imap, destoff,
  956. new_isize);
  957. if (error)
  958. goto err;
  959. if (fatal_signal_pending(current)) {
  960. error = -EINTR;
  961. goto err;
  962. }
  963. /* Advance drange/srange */
  964. srcoff += range_len;
  965. destoff += range_len;
  966. len -= range_len;
  967. }
  968. return 0;
  969. err:
  970. trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
  971. return error;
  972. }
  973. /*
  974. * Link a range of blocks from one file to another.
  975. */
  976. int
  977. xfs_reflink_remap_range(
  978. struct file *file_in,
  979. loff_t pos_in,
  980. struct file *file_out,
  981. loff_t pos_out,
  982. u64 len,
  983. bool is_dedupe)
  984. {
  985. struct inode *inode_in = file_inode(file_in);
  986. struct xfs_inode *src = XFS_I(inode_in);
  987. struct inode *inode_out = file_inode(file_out);
  988. struct xfs_inode *dest = XFS_I(inode_out);
  989. struct xfs_mount *mp = src->i_mount;
  990. bool same_inode = (inode_in == inode_out);
  991. xfs_fileoff_t sfsbno, dfsbno;
  992. xfs_filblks_t fsblen;
  993. xfs_extlen_t cowextsize;
  994. ssize_t ret;
  995. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  996. return -EOPNOTSUPP;
  997. if (XFS_FORCED_SHUTDOWN(mp))
  998. return -EIO;
  999. /* Lock both files against IO */
  1000. lock_two_nondirectories(inode_in, inode_out);
  1001. if (same_inode)
  1002. xfs_ilock(src, XFS_MMAPLOCK_EXCL);
  1003. else
  1004. xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
  1005. /* Check file eligibility and prepare for block sharing. */
  1006. ret = -EINVAL;
  1007. /* Don't reflink realtime inodes */
  1008. if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
  1009. goto out_unlock;
  1010. /* Don't share DAX file data for now. */
  1011. if (IS_DAX(inode_in) || IS_DAX(inode_out))
  1012. goto out_unlock;
  1013. ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
  1014. &len, is_dedupe);
  1015. if (ret <= 0)
  1016. goto out_unlock;
  1017. trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
  1018. /* Set flags and remap blocks. */
  1019. ret = xfs_reflink_set_inode_flag(src, dest);
  1020. if (ret)
  1021. goto out_unlock;
  1022. dfsbno = XFS_B_TO_FSBT(mp, pos_out);
  1023. sfsbno = XFS_B_TO_FSBT(mp, pos_in);
  1024. fsblen = XFS_B_TO_FSB(mp, len);
  1025. ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
  1026. pos_out + len);
  1027. if (ret)
  1028. goto out_unlock;
  1029. /* Zap any page cache for the destination file's range. */
  1030. truncate_inode_pages_range(&inode_out->i_data, pos_out,
  1031. PAGE_ALIGN(pos_out + len) - 1);
  1032. /*
  1033. * Carry the cowextsize hint from src to dest if we're sharing the
  1034. * entire source file to the entire destination file, the source file
  1035. * has a cowextsize hint, and the destination file does not.
  1036. */
  1037. cowextsize = 0;
  1038. if (pos_in == 0 && len == i_size_read(inode_in) &&
  1039. (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
  1040. pos_out == 0 && len >= i_size_read(inode_out) &&
  1041. !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1042. cowextsize = src->i_d.di_cowextsize;
  1043. ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
  1044. out_unlock:
  1045. xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
  1046. if (!same_inode)
  1047. xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
  1048. unlock_two_nondirectories(inode_in, inode_out);
  1049. if (ret)
  1050. trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
  1051. return ret;
  1052. }
  1053. /*
  1054. * The user wants to preemptively CoW all shared blocks in this file,
  1055. * which enables us to turn off the reflink flag. Iterate all
  1056. * extents which are not prealloc/delalloc to see which ranges are
  1057. * mentioned in the refcount tree, then read those blocks into the
  1058. * pagecache, dirty them, fsync them back out, and then we can update
  1059. * the inode flag. What happens if we run out of memory? :)
  1060. */
  1061. STATIC int
  1062. xfs_reflink_dirty_extents(
  1063. struct xfs_inode *ip,
  1064. xfs_fileoff_t fbno,
  1065. xfs_filblks_t end,
  1066. xfs_off_t isize)
  1067. {
  1068. struct xfs_mount *mp = ip->i_mount;
  1069. xfs_agnumber_t agno;
  1070. xfs_agblock_t agbno;
  1071. xfs_extlen_t aglen;
  1072. xfs_agblock_t rbno;
  1073. xfs_extlen_t rlen;
  1074. xfs_off_t fpos;
  1075. xfs_off_t flen;
  1076. struct xfs_bmbt_irec map[2];
  1077. int nmaps;
  1078. int error = 0;
  1079. while (end - fbno > 0) {
  1080. nmaps = 1;
  1081. /*
  1082. * Look for extents in the file. Skip holes, delalloc, or
  1083. * unwritten extents; they can't be reflinked.
  1084. */
  1085. error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
  1086. if (error)
  1087. goto out;
  1088. if (nmaps == 0)
  1089. break;
  1090. if (map[0].br_startblock == HOLESTARTBLOCK ||
  1091. map[0].br_startblock == DELAYSTARTBLOCK ||
  1092. ISUNWRITTEN(&map[0]))
  1093. goto next;
  1094. map[1] = map[0];
  1095. while (map[1].br_blockcount) {
  1096. agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
  1097. agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
  1098. aglen = map[1].br_blockcount;
  1099. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1100. &rbno, &rlen, true);
  1101. if (error)
  1102. goto out;
  1103. if (rbno == NULLAGBLOCK)
  1104. break;
  1105. /* Dirty the pages */
  1106. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1107. fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
  1108. (rbno - agbno));
  1109. flen = XFS_FSB_TO_B(mp, rlen);
  1110. if (fpos + flen > isize)
  1111. flen = isize - fpos;
  1112. error = iomap_file_dirty(VFS_I(ip), fpos, flen,
  1113. &xfs_iomap_ops);
  1114. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1115. if (error)
  1116. goto out;
  1117. map[1].br_blockcount -= (rbno - agbno + rlen);
  1118. map[1].br_startoff += (rbno - agbno + rlen);
  1119. map[1].br_startblock += (rbno - agbno + rlen);
  1120. }
  1121. next:
  1122. fbno = map[0].br_startoff + map[0].br_blockcount;
  1123. }
  1124. out:
  1125. return error;
  1126. }
  1127. /* Clear the inode reflink flag if there are no shared extents. */
  1128. int
  1129. xfs_reflink_clear_inode_flag(
  1130. struct xfs_inode *ip,
  1131. struct xfs_trans **tpp)
  1132. {
  1133. struct xfs_mount *mp = ip->i_mount;
  1134. xfs_fileoff_t fbno;
  1135. xfs_filblks_t end;
  1136. xfs_agnumber_t agno;
  1137. xfs_agblock_t agbno;
  1138. xfs_extlen_t aglen;
  1139. xfs_agblock_t rbno;
  1140. xfs_extlen_t rlen;
  1141. struct xfs_bmbt_irec map;
  1142. int nmaps;
  1143. int error = 0;
  1144. ASSERT(xfs_is_reflink_inode(ip));
  1145. fbno = 0;
  1146. end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
  1147. while (end - fbno > 0) {
  1148. nmaps = 1;
  1149. /*
  1150. * Look for extents in the file. Skip holes, delalloc, or
  1151. * unwritten extents; they can't be reflinked.
  1152. */
  1153. error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
  1154. if (error)
  1155. return error;
  1156. if (nmaps == 0)
  1157. break;
  1158. if (map.br_startblock == HOLESTARTBLOCK ||
  1159. map.br_startblock == DELAYSTARTBLOCK ||
  1160. ISUNWRITTEN(&map))
  1161. goto next;
  1162. agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
  1163. agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
  1164. aglen = map.br_blockcount;
  1165. error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
  1166. &rbno, &rlen, false);
  1167. if (error)
  1168. return error;
  1169. /* Is there still a shared block here? */
  1170. if (rbno != NULLAGBLOCK)
  1171. return 0;
  1172. next:
  1173. fbno = map.br_startoff + map.br_blockcount;
  1174. }
  1175. /*
  1176. * We didn't find any shared blocks so turn off the reflink flag.
  1177. * First, get rid of any leftover CoW mappings.
  1178. */
  1179. error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
  1180. if (error)
  1181. return error;
  1182. /* Clear the inode flag. */
  1183. trace_xfs_reflink_unset_inode_flag(ip);
  1184. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1185. xfs_inode_clear_cowblocks_tag(ip);
  1186. xfs_trans_ijoin(*tpp, ip, 0);
  1187. xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
  1188. return error;
  1189. }
  1190. /*
  1191. * Clear the inode reflink flag if there are no shared extents and the size
  1192. * hasn't changed.
  1193. */
  1194. STATIC int
  1195. xfs_reflink_try_clear_inode_flag(
  1196. struct xfs_inode *ip)
  1197. {
  1198. struct xfs_mount *mp = ip->i_mount;
  1199. struct xfs_trans *tp;
  1200. int error = 0;
  1201. /* Start a rolling transaction to remove the mappings */
  1202. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  1203. if (error)
  1204. return error;
  1205. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1206. xfs_trans_ijoin(tp, ip, 0);
  1207. error = xfs_reflink_clear_inode_flag(ip, &tp);
  1208. if (error)
  1209. goto cancel;
  1210. error = xfs_trans_commit(tp);
  1211. if (error)
  1212. goto out;
  1213. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1214. return 0;
  1215. cancel:
  1216. xfs_trans_cancel(tp);
  1217. out:
  1218. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1219. return error;
  1220. }
  1221. /*
  1222. * Pre-COW all shared blocks within a given byte range of a file and turn off
  1223. * the reflink flag if we unshare all of the file's blocks.
  1224. */
  1225. int
  1226. xfs_reflink_unshare(
  1227. struct xfs_inode *ip,
  1228. xfs_off_t offset,
  1229. xfs_off_t len)
  1230. {
  1231. struct xfs_mount *mp = ip->i_mount;
  1232. xfs_fileoff_t fbno;
  1233. xfs_filblks_t end;
  1234. xfs_off_t isize;
  1235. int error;
  1236. if (!xfs_is_reflink_inode(ip))
  1237. return 0;
  1238. trace_xfs_reflink_unshare(ip, offset, len);
  1239. inode_dio_wait(VFS_I(ip));
  1240. /* Try to CoW the selected ranges */
  1241. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1242. fbno = XFS_B_TO_FSBT(mp, offset);
  1243. isize = i_size_read(VFS_I(ip));
  1244. end = XFS_B_TO_FSB(mp, offset + len);
  1245. error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
  1246. if (error)
  1247. goto out_unlock;
  1248. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1249. /* Wait for the IO to finish */
  1250. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1251. if (error)
  1252. goto out;
  1253. /* Turn off the reflink flag if possible. */
  1254. error = xfs_reflink_try_clear_inode_flag(ip);
  1255. if (error)
  1256. goto out;
  1257. return 0;
  1258. out_unlock:
  1259. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1260. out:
  1261. trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
  1262. return error;
  1263. }