xfs_reflink.c 43 KB

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