xfs_reflink.c 45 KB

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