blob: 4d3f74e3c5e1d0bcf145c96b359d9d01bd06295d [file] [log] [blame]
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001/*
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"
Darrick J. Wong174edb02016-10-03 09:11:39 -070043#include "xfs_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070044#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"
Darrick J. Wong2a067052016-10-03 09:11:33 -070055#include "xfs_iomap.h"
Darrick J. Wong43caeb12016-10-03 09:11:35 -070056#include "xfs_rmap_btree.h"
Darrick J. Wong6fa164b2016-10-03 09:11:45 -070057#include "xfs_sb.h"
58#include "xfs_ag_resv.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070059
60/*
61 * Copy on Write of Shared Blocks
62 *
63 * XFS must preserve "the usual" file semantics even when two files share
64 * the same physical blocks. This means that a write to one file must not
65 * alter the blocks in a different file; the way that we'll do that is
66 * through the use of a copy-on-write mechanism. At a high level, that
67 * means that when we want to write to a shared block, we allocate a new
68 * block, write the data to the new block, and if that succeeds we map the
69 * new block into the file.
70 *
71 * XFS provides a "delayed allocation" mechanism that defers the allocation
72 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
73 * possible. This reduces fragmentation by enabling the filesystem to ask
74 * for bigger chunks less often, which is exactly what we want for CoW.
75 *
76 * The delalloc mechanism begins when the kernel wants to make a block
77 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
78 * create a delalloc mapping, which is a regular in-core extent, but without
79 * a real startblock. (For delalloc mappings, the startblock encodes both
80 * a flag that this is a delalloc mapping, and a worst-case estimate of how
81 * many blocks might be required to put the mapping into the BMBT.) delalloc
82 * mappings are a reservation against the free space in the filesystem;
83 * adjacent mappings can also be combined into fewer larger mappings.
84 *
85 * When dirty pages are being written out (typically in writepage), the
86 * delalloc reservations are converted into real mappings by allocating
87 * blocks and replacing the delalloc mapping with real ones. A delalloc
88 * mapping can be replaced by several real ones if the free space is
89 * fragmented.
90 *
91 * We want to adapt the delalloc mechanism for copy-on-write, since the
92 * write paths are similar. The first two steps (creating the reservation
93 * and allocating the blocks) are exactly the same as delalloc except that
94 * the mappings must be stored in a separate CoW fork because we do not want
95 * to disturb the mapping in the data fork until we're sure that the write
96 * succeeded. IO completion in this case is the process of removing the old
97 * mapping from the data fork and moving the new mapping from the CoW fork to
98 * the data fork. This will be discussed shortly.
99 *
100 * For now, unaligned directio writes will be bounced back to the page cache.
101 * Block-aligned directio writes will use the same mechanism as buffered
102 * writes.
103 *
104 * CoW remapping must be done after the data block write completes,
105 * because we don't want to destroy the old data fork map until we're sure
106 * the new block has been written. Since the new mappings are kept in a
107 * separate fork, we can simply iterate these mappings to find the ones
108 * that cover the file blocks that we just CoW'd. For each extent, simply
109 * unmap the corresponding range in the data fork, map the new range into
110 * the data fork, and remove the extent from the CoW fork.
111 *
112 * Since the remapping operation can be applied to an arbitrary file
113 * range, we record the need for the remap step as a flag in the ioend
114 * instead of declaring a new IO type. This is required for direct io
115 * because we only have ioend for the whole dio, and we have to be able to
116 * remember the presence of unwritten blocks and CoW blocks with a single
117 * ioend structure. Better yet, the more ground we can cover with one
118 * ioend, the better.
119 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700120
121/*
122 * Given an AG extent, find the lowest-numbered run of shared blocks
123 * within that range and return the range in fbno/flen. If
124 * find_end_of_shared is true, return the longest contiguous extent of
125 * shared blocks. If there are no shared extents, fbno and flen will
126 * be set to NULLAGBLOCK and 0, respectively.
127 */
128int
129xfs_reflink_find_shared(
130 struct xfs_mount *mp,
131 xfs_agnumber_t agno,
132 xfs_agblock_t agbno,
133 xfs_extlen_t aglen,
134 xfs_agblock_t *fbno,
135 xfs_extlen_t *flen,
136 bool find_end_of_shared)
137{
138 struct xfs_buf *agbp;
139 struct xfs_btree_cur *cur;
140 int error;
141
142 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
143 if (error)
144 return error;
145
146 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
147
148 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
149 find_end_of_shared);
150
151 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
152
153 xfs_buf_relse(agbp);
154 return error;
155}
156
157/*
158 * Trim the mapping to the next block where there's a change in the
159 * shared/unshared status. More specifically, this means that we
160 * find the lowest-numbered extent of shared blocks that coincides with
161 * the given block mapping. If the shared extent overlaps the start of
162 * the mapping, trim the mapping to the end of the shared extent. If
163 * the shared region intersects the mapping, trim the mapping to the
164 * start of the shared extent. If there are no shared regions that
165 * overlap, just return the original extent.
166 */
167int
168xfs_reflink_trim_around_shared(
169 struct xfs_inode *ip,
170 struct xfs_bmbt_irec *irec,
171 bool *shared,
172 bool *trimmed)
173{
174 xfs_agnumber_t agno;
175 xfs_agblock_t agbno;
176 xfs_extlen_t aglen;
177 xfs_agblock_t fbno;
178 xfs_extlen_t flen;
179 int error = 0;
180
181 /* Holes, unwritten, and delalloc extents cannot be shared */
182 if (!xfs_is_reflink_inode(ip) ||
183 ISUNWRITTEN(irec) ||
184 irec->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig62c5ac82016-10-20 15:52:00 +1100185 irec->br_startblock == DELAYSTARTBLOCK ||
186 isnullstartblock(irec->br_startblock)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700187 *shared = false;
188 return 0;
189 }
190
191 trace_xfs_reflink_trim_around_shared(ip, irec);
192
193 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
194 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
195 aglen = irec->br_blockcount;
196
197 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
198 aglen, &fbno, &flen, true);
199 if (error)
200 return error;
201
202 *shared = *trimmed = false;
203 if (fbno == NULLAGBLOCK) {
204 /* No shared blocks at all. */
205 return 0;
206 } else if (fbno == agbno) {
207 /*
208 * The start of this extent is shared. Truncate the
209 * mapping at the end of the shared region so that a
210 * subsequent iteration starts at the start of the
211 * unshared region.
212 */
213 irec->br_blockcount = flen;
214 *shared = true;
215 if (flen != aglen)
216 *trimmed = true;
217 return 0;
218 } else {
219 /*
220 * There's a shared extent midway through this extent.
221 * Truncate the mapping at the start of the shared
222 * extent so that a subsequent iteration starts at the
223 * start of the shared region.
224 */
225 irec->br_blockcount = fbno - agbno;
226 *trimmed = true;
227 return 0;
228 }
229}
230
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100231/*
232 * Trim the passed in imap to the next shared/unshared extent boundary, and
233 * if imap->br_startoff points to a shared extent reserve space for it in the
234 * COW fork. In this case *shared is set to true, else to false.
235 *
236 * Note that imap will always contain the block numbers for the existing blocks
237 * in the data fork, as the upper layers need them for read-modify-write
238 * operations.
239 */
240int
241xfs_reflink_reserve_cow(
Darrick J. Wong2a067052016-10-03 09:11:33 -0700242 struct xfs_inode *ip,
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100243 struct xfs_bmbt_irec *imap,
244 bool *shared)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700245{
Christoph Hellwig4a323332017-01-09 16:38:44 +0100246 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
247 struct xfs_bmbt_irec got;
Christoph Hellwig4a323332017-01-09 16:38:44 +0100248 int error = 0;
249 bool eof = false, trimmed;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700250 xfs_extnum_t idx;
251
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100252 /*
253 * Search the COW fork extent list first. This serves two purposes:
254 * first this implement the speculative preallocation using cowextisze,
255 * so that we also unshared block adjacent to shared blocks instead
256 * of just the shared blocks themselves. Second the lookup in the
257 * extent list is generally faster than going out to the shared extent
258 * tree.
259 */
Christoph Hellwig4a323332017-01-09 16:38:44 +0100260
261 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
262 eof = true;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100263 if (!eof && got.br_startoff <= imap->br_startoff) {
264 trace_xfs_reflink_cow_found(ip, imap);
265 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700266
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100267 *shared = true;
268 return 0;
269 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700270
271 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100272 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700273 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100274 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700275
276 /* Not shared? Just report the (potentially capped) extent. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100277 if (!*shared)
278 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700279
280 /*
281 * Fork all the shared blocks from our write offset until the end of
282 * the extent.
283 */
284 error = xfs_qm_dqattach_locked(ip, 0);
285 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100286 return error;
287
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100288 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
Brian Foster3d6e3b12017-01-09 16:38:45 +0100289 imap->br_blockcount, 0, &got, &idx, eof);
290 if (error == -ENOSPC || error == -EDQUOT)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100291 trace_xfs_reflink_cow_enospc(ip, imap);
Brian Foster3d6e3b12017-01-09 16:38:45 +0100292 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100293 return error;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700294
Darrick J. Wong2a067052016-10-03 09:11:33 -0700295 trace_xfs_reflink_cow_alloc(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100296 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700297}
Darrick J. Wongef473662016-10-03 09:11:34 -0700298
Darrick J. Wong0613f162016-10-03 09:11:37 -0700299/* Allocate all CoW reservations covering a range of blocks in a file. */
300static int
301__xfs_reflink_allocate_cow(
302 struct xfs_inode *ip,
303 xfs_fileoff_t *offset_fsb,
304 xfs_fileoff_t end_fsb)
305{
306 struct xfs_mount *mp = ip->i_mount;
307 struct xfs_bmbt_irec imap;
308 struct xfs_defer_ops dfops;
309 struct xfs_trans *tp;
310 xfs_fsblock_t first_block;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700311 int nimaps = 1, error;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100312 bool shared;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700313
314 xfs_defer_init(&dfops, &first_block);
315
316 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
317 XFS_TRANS_RESERVE, &tp);
318 if (error)
319 return error;
320
321 xfs_ilock(ip, XFS_ILOCK_EXCL);
322
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100323 /* Read extent from the source file. */
324 nimaps = 1;
325 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
326 &imap, &nimaps, 0);
327 if (error)
328 goto out_unlock;
329 ASSERT(nimaps == 1);
330
331 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700332 if (error)
333 goto out_trans_cancel;
334
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100335 if (!shared) {
336 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700337 goto out_trans_cancel;
338 }
339
340 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100341 error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
Darrick J. Wong0613f162016-10-03 09:11:37 -0700342 XFS_BMAPI_COWFORK, &first_block,
343 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
344 &imap, &nimaps, &dfops);
345 if (error)
346 goto out_trans_cancel;
347
Darrick J. Wong0613f162016-10-03 09:11:37 -0700348 error = xfs_defer_finish(&tp, &dfops, NULL);
349 if (error)
350 goto out_trans_cancel;
351
352 error = xfs_trans_commit(tp);
353
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100354 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700355out_unlock:
356 xfs_iunlock(ip, XFS_ILOCK_EXCL);
357 return error;
358out_trans_cancel:
359 xfs_defer_cancel(&dfops);
360 xfs_trans_cancel(tp);
361 goto out_unlock;
362}
363
364/* Allocate all CoW reservations covering a part of a file. */
365int
366xfs_reflink_allocate_cow_range(
367 struct xfs_inode *ip,
368 xfs_off_t offset,
369 xfs_off_t count)
370{
371 struct xfs_mount *mp = ip->i_mount;
372 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
373 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
374 int error;
375
376 ASSERT(xfs_is_reflink_inode(ip));
377
378 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
379
380 /*
381 * Make sure that the dquots are there.
382 */
383 error = xfs_qm_dqattach(ip, 0);
384 if (error)
385 return error;
386
387 while (offset_fsb < end_fsb) {
388 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
389 if (error) {
390 trace_xfs_reflink_allocate_cow_range_error(ip, error,
391 _RET_IP_);
392 break;
393 }
394 }
395
396 return error;
397}
398
Darrick J. Wongef473662016-10-03 09:11:34 -0700399/*
400 * Find the CoW reservation (and whether or not it needs block allocation)
401 * for a given byte offset of a file.
402 */
403bool
404xfs_reflink_find_cow_mapping(
405 struct xfs_inode *ip,
406 xfs_off_t offset,
407 struct xfs_bmbt_irec *imap,
408 bool *need_alloc)
409{
410 struct xfs_bmbt_irec irec;
411 struct xfs_ifork *ifp;
412 struct xfs_bmbt_rec_host *gotp;
413 xfs_fileoff_t bno;
414 xfs_extnum_t idx;
415
416 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
417 ASSERT(xfs_is_reflink_inode(ip));
418
419 /* Find the extent in the CoW fork. */
420 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
421 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
422 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
423 if (!gotp)
424 return false;
425
426 xfs_bmbt_get_all(gotp, &irec);
427 if (bno >= irec.br_startoff + irec.br_blockcount ||
428 bno < irec.br_startoff)
429 return false;
430
431 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
432 &irec);
433
434 /* If it's still delalloc, we must allocate later. */
435 *imap = irec;
436 *need_alloc = !!(isnullstartblock(irec.br_startblock));
437
438 return true;
439}
440
441/*
442 * Trim an extent to end at the next CoW reservation past offset_fsb.
443 */
444int
445xfs_reflink_trim_irec_to_next_cow(
446 struct xfs_inode *ip,
447 xfs_fileoff_t offset_fsb,
448 struct xfs_bmbt_irec *imap)
449{
450 struct xfs_bmbt_irec irec;
451 struct xfs_ifork *ifp;
452 struct xfs_bmbt_rec_host *gotp;
453 xfs_extnum_t idx;
454
455 if (!xfs_is_reflink_inode(ip))
456 return 0;
457
458 /* Find the extent in the CoW fork. */
459 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
460 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
461 if (!gotp)
462 return 0;
463 xfs_bmbt_get_all(gotp, &irec);
464
465 /* This is the extent before; try sliding up one. */
466 if (irec.br_startoff < offset_fsb) {
467 idx++;
Eric Sandeenf380ee72017-01-09 16:38:36 +0100468 if (idx >= xfs_iext_count(ifp))
Darrick J. Wongef473662016-10-03 09:11:34 -0700469 return 0;
470 gotp = xfs_iext_get_ext(ifp, idx);
471 xfs_bmbt_get_all(gotp, &irec);
472 }
473
474 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
475 return 0;
476
477 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
478 trace_xfs_reflink_trim_irec(ip, imap);
479
480 return 0;
481}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700482
483/*
484 * Cancel all pending CoW reservations for some block range of an inode.
485 */
486int
487xfs_reflink_cancel_cow_blocks(
488 struct xfs_inode *ip,
489 struct xfs_trans **tpp,
490 xfs_fileoff_t offset_fsb,
491 xfs_fileoff_t end_fsb)
492{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100493 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
494 struct xfs_bmbt_irec got, prev, del;
495 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700496 xfs_fsblock_t firstfsb;
497 struct xfs_defer_ops dfops;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100498 int error = 0, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700499
500 if (!xfs_is_reflink_inode(ip))
501 return 0;
502
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100503 xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
504 &got, &prev);
505 if (eof)
506 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700507
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100508 while (got.br_startoff < end_fsb) {
509 del = got;
510 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
511 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700512
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100513 if (isnullstartblock(del.br_startblock)) {
514 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
515 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700516 if (error)
517 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700518 } else {
519 xfs_trans_ijoin(*tpp, ip, 0);
520 xfs_defer_init(&dfops, &firstfsb);
521
Darrick J. Wong174edb02016-10-03 09:11:39 -0700522 /* Free the CoW orphan record. */
523 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100524 &dfops, del.br_startblock,
525 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700526 if (error)
527 break;
528
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700529 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100530 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700531 NULL);
532
533 /* Update quota accounting */
534 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100535 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700536
537 /* Roll the transaction */
538 error = xfs_defer_finish(tpp, &dfops, ip);
539 if (error) {
540 xfs_defer_cancel(&dfops);
541 break;
542 }
543
544 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100545 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700546 }
547
Eric Sandeenf380ee72017-01-09 16:38:36 +0100548 if (++idx >= xfs_iext_count(ifp))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100549 break;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100550 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700551 }
552
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100553 /* clear tag if cow fork is emptied */
554 if (!ifp->if_bytes)
555 xfs_inode_clear_cowblocks_tag(ip);
556
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700557 return error;
558}
559
560/*
561 * Cancel all pending CoW reservations for some byte range of an inode.
562 */
563int
564xfs_reflink_cancel_cow_range(
565 struct xfs_inode *ip,
566 xfs_off_t offset,
567 xfs_off_t count)
568{
569 struct xfs_trans *tp;
570 xfs_fileoff_t offset_fsb;
571 xfs_fileoff_t end_fsb;
572 int error;
573
574 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100575 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700576
577 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
578 if (count == NULLFILEOFF)
579 end_fsb = NULLFILEOFF;
580 else
581 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
582
583 /* Start a rolling transaction to remove the mappings */
584 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
585 0, 0, 0, &tp);
586 if (error)
587 goto out;
588
589 xfs_ilock(ip, XFS_ILOCK_EXCL);
590 xfs_trans_ijoin(tp, ip, 0);
591
592 /* Scrape out the old CoW reservations */
593 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
594 if (error)
595 goto out_cancel;
596
597 error = xfs_trans_commit(tp);
598
599 xfs_iunlock(ip, XFS_ILOCK_EXCL);
600 return error;
601
602out_cancel:
603 xfs_trans_cancel(tp);
604 xfs_iunlock(ip, XFS_ILOCK_EXCL);
605out:
606 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
607 return error;
608}
609
610/*
611 * Remap parts of a file's data fork after a successful CoW.
612 */
613int
614xfs_reflink_end_cow(
615 struct xfs_inode *ip,
616 xfs_off_t offset,
617 xfs_off_t count)
618{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100619 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
620 struct xfs_bmbt_irec got, prev, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700621 struct xfs_trans *tp;
622 xfs_fileoff_t offset_fsb;
623 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700624 xfs_fsblock_t firstfsb;
625 struct xfs_defer_ops dfops;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100626 int error, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700627 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700628 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100629 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700630
631 trace_xfs_reflink_end_cow(ip, offset, count);
632
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100633 /* No COW extents? That's easy! */
634 if (ifp->if_bytes == 0)
635 return 0;
636
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700637 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
638 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700639
640 /* Start a rolling transaction to switch the mappings */
641 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
642 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
643 resblks, 0, 0, &tp);
644 if (error)
645 goto out;
646
647 xfs_ilock(ip, XFS_ILOCK_EXCL);
648 xfs_trans_ijoin(tp, ip, 0);
649
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100650 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
651 &got, &prev);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700652
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100653 /* If there is a hole at end_fsb - 1 go to the previous extent */
654 if (eof || got.br_startoff > end_fsb) {
655 ASSERT(idx > 0);
656 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
657 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700658
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100659 /* Walk backwards until we're out of the I/O range... */
660 while (got.br_startoff + got.br_blockcount > offset_fsb) {
661 del = got;
662 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
663
664 /* Extent delete may have bumped idx forward */
665 if (!del.br_blockcount) {
666 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700667 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700668 }
669
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100670 ASSERT(!isnullstartblock(got.br_startblock));
671
672 /* Unmap the old blocks in the data fork. */
673 xfs_defer_init(&dfops, &firstfsb);
674 rlen = del.br_blockcount;
675 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
676 &firstfsb, &dfops);
677 if (error)
678 goto out_defer;
679
680 /* Trim the extent to whatever got unmapped. */
681 if (rlen) {
682 xfs_trim_extent(&del, del.br_startoff + rlen,
683 del.br_blockcount - rlen);
684 }
685 trace_xfs_reflink_cow_remap(ip, &del);
686
687 /* Free the CoW orphan record. */
688 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
689 del.br_startblock, del.br_blockcount);
690 if (error)
691 goto out_defer;
692
693 /* Map the new blocks into the data fork. */
694 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
695 if (error)
696 goto out_defer;
697
698 /* Remove the mapping from the CoW fork. */
699 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
700
701 error = xfs_defer_finish(&tp, &dfops, ip);
702 if (error)
703 goto out_defer;
704
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700705next_extent:
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100706 if (idx < 0)
707 break;
708 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700709 }
710
711 error = xfs_trans_commit(tp);
712 xfs_iunlock(ip, XFS_ILOCK_EXCL);
713 if (error)
714 goto out;
715 return 0;
716
717out_defer:
718 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700719 xfs_trans_cancel(tp);
720 xfs_iunlock(ip, XFS_ILOCK_EXCL);
721out:
722 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
723 return error;
724}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700725
726/*
727 * Free leftover CoW reservations that didn't get cleaned out.
728 */
729int
730xfs_reflink_recover_cow(
731 struct xfs_mount *mp)
732{
733 xfs_agnumber_t agno;
734 int error = 0;
735
736 if (!xfs_sb_version_hasreflink(&mp->m_sb))
737 return 0;
738
739 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
740 error = xfs_refcount_recover_cow_leftovers(mp, agno);
741 if (error)
742 break;
743 }
744
745 return error;
746}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700747
748/*
749 * Reflinking (Block) Ranges of Two Files Together
750 *
751 * First, ensure that the reflink flag is set on both inodes. The flag is an
752 * optimization to avoid unnecessary refcount btree lookups in the write path.
753 *
754 * Now we can iteratively remap the range of extents (and holes) in src to the
755 * corresponding ranges in dest. Let drange and srange denote the ranges of
756 * logical blocks in dest and src touched by the reflink operation.
757 *
758 * While the length of drange is greater than zero,
759 * - Read src's bmbt at the start of srange ("imap")
760 * - If imap doesn't exist, make imap appear to start at the end of srange
761 * with zero length.
762 * - If imap starts before srange, advance imap to start at srange.
763 * - If imap goes beyond srange, truncate imap to end at the end of srange.
764 * - Punch (imap start - srange start + imap len) blocks from dest at
765 * offset (drange start).
766 * - If imap points to a real range of pblks,
767 * > Increase the refcount of the imap's pblks
768 * > Map imap's pblks into dest at the offset
769 * (drange start + imap start - srange start)
770 * - Advance drange and srange by (imap start - srange start + imap len)
771 *
772 * Finally, if the reflink made dest longer, update both the in-core and
773 * on-disk file sizes.
774 *
775 * ASCII Art Demonstration:
776 *
777 * Let's say we want to reflink this source file:
778 *
779 * ----SSSSSSS-SSSSS----SSSSSS (src file)
780 * <-------------------->
781 *
782 * into this destination file:
783 *
784 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
785 * <-------------------->
786 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
787 * Observe that the range has different logical offsets in either file.
788 *
789 * Consider that the first extent in the source file doesn't line up with our
790 * reflink range. Unmapping and remapping are separate operations, so we can
791 * unmap more blocks from the destination file than we remap.
792 *
793 * ----SSSSSSS-SSSSS----SSSSSS
794 * <------->
795 * --DDDDD---------DDDDD--DDD
796 * <------->
797 *
798 * Now remap the source extent into the destination file:
799 *
800 * ----SSSSSSS-SSSSS----SSSSSS
801 * <------->
802 * --DDDDD--SSSSSSSDDDDD--DDD
803 * <------->
804 *
805 * Do likewise with the second hole and extent in our range. Holes in the
806 * unmap range don't affect our operation.
807 *
808 * ----SSSSSSS-SSSSS----SSSSSS
809 * <---->
810 * --DDDDD--SSSSSSS-SSSSS-DDD
811 * <---->
812 *
813 * Finally, unmap and remap part of the third extent. This will increase the
814 * size of the destination file.
815 *
816 * ----SSSSSSS-SSSSS----SSSSSS
817 * <----->
818 * --DDDDD--SSSSSSS-SSSSS----SSS
819 * <----->
820 *
821 * Once we update the destination file's i_size, we're done.
822 */
823
824/*
825 * Ensure the reflink bit is set in both inodes.
826 */
827STATIC int
828xfs_reflink_set_inode_flag(
829 struct xfs_inode *src,
830 struct xfs_inode *dest)
831{
832 struct xfs_mount *mp = src->i_mount;
833 int error;
834 struct xfs_trans *tp;
835
836 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
837 return 0;
838
839 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
840 if (error)
841 goto out_error;
842
843 /* Lock both files against IO */
844 if (src->i_ino == dest->i_ino)
845 xfs_ilock(src, XFS_ILOCK_EXCL);
846 else
847 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
848
849 if (!xfs_is_reflink_inode(src)) {
850 trace_xfs_reflink_set_inode_flag(src);
851 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
852 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
853 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
854 xfs_ifork_init_cow(src);
855 } else
856 xfs_iunlock(src, XFS_ILOCK_EXCL);
857
858 if (src->i_ino == dest->i_ino)
859 goto commit_flags;
860
861 if (!xfs_is_reflink_inode(dest)) {
862 trace_xfs_reflink_set_inode_flag(dest);
863 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
864 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
865 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
866 xfs_ifork_init_cow(dest);
867 } else
868 xfs_iunlock(dest, XFS_ILOCK_EXCL);
869
870commit_flags:
871 error = xfs_trans_commit(tp);
872 if (error)
873 goto out_error;
874 return error;
875
876out_error:
877 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
878 return error;
879}
880
881/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700882 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700883 */
884STATIC int
885xfs_reflink_update_dest(
886 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700887 xfs_off_t newlen,
888 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700889{
890 struct xfs_mount *mp = dest->i_mount;
891 struct xfs_trans *tp;
892 int error;
893
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700894 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700895 return 0;
896
897 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
898 if (error)
899 goto out_error;
900
901 xfs_ilock(dest, XFS_ILOCK_EXCL);
902 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
903
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700904 if (newlen > i_size_read(VFS_I(dest))) {
905 trace_xfs_reflink_update_inode_size(dest, newlen);
906 i_size_write(VFS_I(dest), newlen);
907 dest->i_d.di_size = newlen;
908 }
909
910 if (cowextsize) {
911 dest->i_d.di_cowextsize = cowextsize;
912 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
913 }
914
Darrick J. Wong862bb362016-10-03 09:11:40 -0700915 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
916
917 error = xfs_trans_commit(tp);
918 if (error)
919 goto out_error;
920 return error;
921
922out_error:
923 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
924 return error;
925}
926
927/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700928 * Do we have enough reserve in this AG to handle a reflink? The refcount
929 * btree already reserved all the space it needs, but the rmap btree can grow
930 * infinitely, so we won't allow more reflinks when the AG is down to the
931 * btree reserves.
932 */
933static int
934xfs_reflink_ag_has_free_space(
935 struct xfs_mount *mp,
936 xfs_agnumber_t agno)
937{
938 struct xfs_perag *pag;
939 int error = 0;
940
941 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
942 return 0;
943
944 pag = xfs_perag_get(mp, agno);
945 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
946 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
947 error = -ENOSPC;
948 xfs_perag_put(pag);
949 return error;
950}
951
952/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700953 * Unmap a range of blocks from a file, then map other blocks into the hole.
954 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
955 * The extent irec is mapped into dest at irec->br_startoff.
956 */
957STATIC int
958xfs_reflink_remap_extent(
959 struct xfs_inode *ip,
960 struct xfs_bmbt_irec *irec,
961 xfs_fileoff_t destoff,
962 xfs_off_t new_isize)
963{
964 struct xfs_mount *mp = ip->i_mount;
965 struct xfs_trans *tp;
966 xfs_fsblock_t firstfsb;
967 unsigned int resblks;
968 struct xfs_defer_ops dfops;
969 struct xfs_bmbt_irec uirec;
970 bool real_extent;
971 xfs_filblks_t rlen;
972 xfs_filblks_t unmap_len;
973 xfs_off_t newlen;
974 int error;
975
976 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
977 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
978
979 /* Only remap normal extents. */
980 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
981 irec->br_startblock != DELAYSTARTBLOCK &&
982 !ISUNWRITTEN(irec));
983
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700984 /* No reflinking if we're low on space */
985 if (real_extent) {
986 error = xfs_reflink_ag_has_free_space(mp,
987 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
988 if (error)
989 goto out;
990 }
991
Darrick J. Wong862bb362016-10-03 09:11:40 -0700992 /* Start a rolling transaction to switch the mappings */
993 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
994 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
995 if (error)
996 goto out;
997
998 xfs_ilock(ip, XFS_ILOCK_EXCL);
999 xfs_trans_ijoin(tp, ip, 0);
1000
1001 /* If we're not just clearing space, then do we have enough quota? */
1002 if (real_extent) {
1003 error = xfs_trans_reserve_quota_nblks(tp, ip,
1004 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1005 if (error)
1006 goto out_cancel;
1007 }
1008
1009 trace_xfs_reflink_remap(ip, irec->br_startoff,
1010 irec->br_blockcount, irec->br_startblock);
1011
1012 /* Unmap the old blocks in the data fork. */
1013 rlen = unmap_len;
1014 while (rlen) {
1015 xfs_defer_init(&dfops, &firstfsb);
1016 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1017 &firstfsb, &dfops);
1018 if (error)
1019 goto out_defer;
1020
1021 /*
1022 * Trim the extent to whatever got unmapped.
1023 * Remember, bunmapi works backwards.
1024 */
1025 uirec.br_startblock = irec->br_startblock + rlen;
1026 uirec.br_startoff = irec->br_startoff + rlen;
1027 uirec.br_blockcount = unmap_len - rlen;
1028 unmap_len = rlen;
1029
1030 /* If this isn't a real mapping, we're done. */
1031 if (!real_extent || uirec.br_blockcount == 0)
1032 goto next_extent;
1033
1034 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1035 uirec.br_blockcount, uirec.br_startblock);
1036
1037 /* Update the refcount tree */
1038 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1039 if (error)
1040 goto out_defer;
1041
1042 /* Map the new blocks into the data fork. */
1043 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1044 if (error)
1045 goto out_defer;
1046
1047 /* Update quota accounting. */
1048 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1049 uirec.br_blockcount);
1050
1051 /* Update dest isize if needed. */
1052 newlen = XFS_FSB_TO_B(mp,
1053 uirec.br_startoff + uirec.br_blockcount);
1054 newlen = min_t(xfs_off_t, newlen, new_isize);
1055 if (newlen > i_size_read(VFS_I(ip))) {
1056 trace_xfs_reflink_update_inode_size(ip, newlen);
1057 i_size_write(VFS_I(ip), newlen);
1058 ip->i_d.di_size = newlen;
1059 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1060 }
1061
1062next_extent:
1063 /* Process all the deferred stuff. */
1064 error = xfs_defer_finish(&tp, &dfops, ip);
1065 if (error)
1066 goto out_defer;
1067 }
1068
1069 error = xfs_trans_commit(tp);
1070 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1071 if (error)
1072 goto out;
1073 return 0;
1074
1075out_defer:
1076 xfs_defer_cancel(&dfops);
1077out_cancel:
1078 xfs_trans_cancel(tp);
1079 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1080out:
1081 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1082 return error;
1083}
1084
1085/*
1086 * Iteratively remap one file's extents (and holes) to another's.
1087 */
1088STATIC int
1089xfs_reflink_remap_blocks(
1090 struct xfs_inode *src,
1091 xfs_fileoff_t srcoff,
1092 struct xfs_inode *dest,
1093 xfs_fileoff_t destoff,
1094 xfs_filblks_t len,
1095 xfs_off_t new_isize)
1096{
1097 struct xfs_bmbt_irec imap;
1098 int nimaps;
1099 int error = 0;
1100 xfs_filblks_t range_len;
1101
1102 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1103 while (len) {
1104 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1105 dest, destoff);
1106 /* Read extent from the source file */
1107 nimaps = 1;
1108 xfs_ilock(src, XFS_ILOCK_EXCL);
1109 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1110 xfs_iunlock(src, XFS_ILOCK_EXCL);
1111 if (error)
1112 goto err;
1113 ASSERT(nimaps == 1);
1114
1115 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1116 &imap);
1117
1118 /* Translate imap into the destination file. */
1119 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1120 imap.br_startoff += destoff - srcoff;
1121
1122 /* Clear dest from destoff to the end of imap and map it in. */
1123 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1124 new_isize);
1125 if (error)
1126 goto err;
1127
1128 if (fatal_signal_pending(current)) {
1129 error = -EINTR;
1130 goto err;
1131 }
1132
1133 /* Advance drange/srange */
1134 srcoff += range_len;
1135 destoff += range_len;
1136 len -= range_len;
1137 }
1138
1139 return 0;
1140
1141err:
1142 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1143 return error;
1144}
1145
1146/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001147 * Read a page's worth of file data into the page cache. Return the page
1148 * locked.
1149 */
1150static struct page *
1151xfs_get_page(
1152 struct inode *inode,
1153 xfs_off_t offset)
1154{
1155 struct address_space *mapping;
1156 struct page *page;
1157 pgoff_t n;
1158
1159 n = offset >> PAGE_SHIFT;
1160 mapping = inode->i_mapping;
1161 page = read_mapping_page(mapping, n, NULL);
1162 if (IS_ERR(page))
1163 return page;
1164 if (!PageUptodate(page)) {
1165 put_page(page);
1166 return ERR_PTR(-EIO);
1167 }
1168 lock_page(page);
1169 return page;
1170}
1171
1172/*
1173 * Compare extents of two files to see if they are the same.
1174 */
1175static int
1176xfs_compare_extents(
1177 struct inode *src,
1178 xfs_off_t srcoff,
1179 struct inode *dest,
1180 xfs_off_t destoff,
1181 xfs_off_t len,
1182 bool *is_same)
1183{
1184 xfs_off_t src_poff;
1185 xfs_off_t dest_poff;
1186 void *src_addr;
1187 void *dest_addr;
1188 struct page *src_page;
1189 struct page *dest_page;
1190 xfs_off_t cmp_len;
1191 bool same;
1192 int error;
1193
1194 error = -EINVAL;
1195 same = true;
1196 while (len) {
1197 src_poff = srcoff & (PAGE_SIZE - 1);
1198 dest_poff = destoff & (PAGE_SIZE - 1);
1199 cmp_len = min(PAGE_SIZE - src_poff,
1200 PAGE_SIZE - dest_poff);
1201 cmp_len = min(cmp_len, len);
1202 ASSERT(cmp_len > 0);
1203
1204 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1205 XFS_I(dest), destoff);
1206
1207 src_page = xfs_get_page(src, srcoff);
1208 if (IS_ERR(src_page)) {
1209 error = PTR_ERR(src_page);
1210 goto out_error;
1211 }
1212 dest_page = xfs_get_page(dest, destoff);
1213 if (IS_ERR(dest_page)) {
1214 error = PTR_ERR(dest_page);
1215 unlock_page(src_page);
1216 put_page(src_page);
1217 goto out_error;
1218 }
1219 src_addr = kmap_atomic(src_page);
1220 dest_addr = kmap_atomic(dest_page);
1221
1222 flush_dcache_page(src_page);
1223 flush_dcache_page(dest_page);
1224
1225 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1226 same = false;
1227
1228 kunmap_atomic(dest_addr);
1229 kunmap_atomic(src_addr);
1230 unlock_page(dest_page);
1231 unlock_page(src_page);
1232 put_page(dest_page);
1233 put_page(src_page);
1234
1235 if (!same)
1236 break;
1237
1238 srcoff += cmp_len;
1239 destoff += cmp_len;
1240 len -= cmp_len;
1241 }
1242
1243 *is_same = same;
1244 return 0;
1245
1246out_error:
1247 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1248 return error;
1249}
1250
1251/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001252 * Link a range of blocks from one file to another.
1253 */
1254int
1255xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001256 struct file *file_in,
1257 loff_t pos_in,
1258 struct file *file_out,
1259 loff_t pos_out,
1260 u64 len,
1261 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001262{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001263 struct inode *inode_in = file_inode(file_in);
1264 struct xfs_inode *src = XFS_I(inode_in);
1265 struct inode *inode_out = file_inode(file_out);
1266 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001267 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001268 loff_t bs = inode_out->i_sb->s_blocksize;
1269 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001270 xfs_fileoff_t sfsbno, dfsbno;
1271 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001272 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001273 loff_t isize;
1274 ssize_t ret;
1275 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001276
1277 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1278 return -EOPNOTSUPP;
1279
1280 if (XFS_FORCED_SHUTDOWN(mp))
1281 return -EIO;
1282
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001283 /* Lock both files against IO */
1284 if (same_inode) {
1285 xfs_ilock(src, XFS_IOLOCK_EXCL);
1286 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1287 } else {
1288 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1289 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1290 }
1291
1292 /* Don't touch certain kinds of inodes */
1293 ret = -EPERM;
1294 if (IS_IMMUTABLE(inode_out))
1295 goto out_unlock;
1296
1297 ret = -ETXTBSY;
1298 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1299 goto out_unlock;
1300
1301
1302 /* Don't reflink dirs, pipes, sockets... */
1303 ret = -EISDIR;
1304 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1305 goto out_unlock;
1306 ret = -EINVAL;
1307 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1308 goto out_unlock;
1309 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1310 goto out_unlock;
1311
Darrick J. Wong862bb362016-10-03 09:11:40 -07001312 /* Don't reflink realtime inodes */
1313 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001314 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001315
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001316 /* Don't share DAX file data for now. */
1317 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1318 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001319
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001320 /* Are we going all the way to the end? */
1321 isize = i_size_read(inode_in);
1322 if (isize == 0) {
1323 ret = 0;
1324 goto out_unlock;
1325 }
1326
Darrick J. Wong39032572017-01-09 16:38:41 +01001327 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1328 if (len == 0) {
1329 if (is_dedupe) {
1330 ret = 0;
1331 goto out_unlock;
1332 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001333 len = isize - pos_in;
Darrick J. Wong39032572017-01-09 16:38:41 +01001334 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001335
1336 /* Ensure offsets don't wrap and the input is inside i_size */
1337 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1338 pos_in + len > isize)
1339 goto out_unlock;
1340
1341 /* Don't allow dedupe past EOF in the dest file */
1342 if (is_dedupe) {
1343 loff_t disize;
1344
1345 disize = i_size_read(inode_out);
1346 if (pos_out >= disize || pos_out + len > disize)
1347 goto out_unlock;
1348 }
1349
1350 /* If we're linking to EOF, continue to the block boundary. */
1351 if (pos_in + len == isize)
1352 blen = ALIGN(isize, bs) - pos_in;
1353 else
1354 blen = len;
1355
1356 /* Only reflink if we're aligned to block boundaries */
1357 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1358 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1359 goto out_unlock;
1360
1361 /* Don't allow overlapped reflink within the same file */
1362 if (same_inode) {
1363 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1364 goto out_unlock;
1365 }
1366
1367 /* Wait for the completion of any pending IOs on both files */
1368 inode_dio_wait(inode_in);
1369 if (!same_inode)
1370 inode_dio_wait(inode_out);
1371
1372 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1373 pos_in, pos_in + len - 1);
1374 if (ret)
1375 goto out_unlock;
1376
1377 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1378 pos_out, pos_out + len - 1);
1379 if (ret)
1380 goto out_unlock;
1381
1382 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001383
Darrick J. Wongcc714662016-10-03 09:11:41 -07001384 /*
1385 * Check that the extents are the same.
1386 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001387 if (is_dedupe) {
1388 bool is_same = false;
1389
1390 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1391 len, &is_same);
1392 if (ret)
1393 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001394 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001395 ret = -EBADE;
1396 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001397 }
1398 }
1399
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001400 ret = xfs_reflink_set_inode_flag(src, dest);
1401 if (ret)
1402 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001403
1404 /*
1405 * Invalidate the page cache so that we can clear any CoW mappings
1406 * in the destination file.
1407 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001408 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1409 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001410
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001411 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1412 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001413 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001414 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1415 pos_out + len);
1416 if (ret)
1417 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001418
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001419 /*
1420 * Carry the cowextsize hint from src to dest if we're sharing the
1421 * entire source file to the entire destination file, the source file
1422 * has a cowextsize hint, and the destination file does not.
1423 */
1424 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001425 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001426 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001427 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001428 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1429 cowextsize = src->i_d.di_cowextsize;
1430
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001431 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001432
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001433out_unlock:
1434 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1435 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1436 if (src->i_ino != dest->i_ino) {
1437 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1438 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1439 }
1440 if (ret)
1441 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1442 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001443}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001444
1445/*
1446 * The user wants to preemptively CoW all shared blocks in this file,
1447 * which enables us to turn off the reflink flag. Iterate all
1448 * extents which are not prealloc/delalloc to see which ranges are
1449 * mentioned in the refcount tree, then read those blocks into the
1450 * pagecache, dirty them, fsync them back out, and then we can update
1451 * the inode flag. What happens if we run out of memory? :)
1452 */
1453STATIC int
1454xfs_reflink_dirty_extents(
1455 struct xfs_inode *ip,
1456 xfs_fileoff_t fbno,
1457 xfs_filblks_t end,
1458 xfs_off_t isize)
1459{
1460 struct xfs_mount *mp = ip->i_mount;
1461 xfs_agnumber_t agno;
1462 xfs_agblock_t agbno;
1463 xfs_extlen_t aglen;
1464 xfs_agblock_t rbno;
1465 xfs_extlen_t rlen;
1466 xfs_off_t fpos;
1467 xfs_off_t flen;
1468 struct xfs_bmbt_irec map[2];
1469 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001470 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001471
1472 while (end - fbno > 0) {
1473 nmaps = 1;
1474 /*
1475 * Look for extents in the file. Skip holes, delalloc, or
1476 * unwritten extents; they can't be reflinked.
1477 */
1478 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1479 if (error)
1480 goto out;
1481 if (nmaps == 0)
1482 break;
1483 if (map[0].br_startblock == HOLESTARTBLOCK ||
1484 map[0].br_startblock == DELAYSTARTBLOCK ||
1485 ISUNWRITTEN(&map[0]))
1486 goto next;
1487
1488 map[1] = map[0];
1489 while (map[1].br_blockcount) {
1490 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1491 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1492 aglen = map[1].br_blockcount;
1493
1494 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1495 &rbno, &rlen, true);
1496 if (error)
1497 goto out;
1498 if (rbno == NULLAGBLOCK)
1499 break;
1500
1501 /* Dirty the pages */
1502 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1503 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1504 (rbno - agbno));
1505 flen = XFS_FSB_TO_B(mp, rlen);
1506 if (fpos + flen > isize)
1507 flen = isize - fpos;
1508 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1509 &xfs_iomap_ops);
1510 xfs_ilock(ip, XFS_ILOCK_EXCL);
1511 if (error)
1512 goto out;
1513
1514 map[1].br_blockcount -= (rbno - agbno + rlen);
1515 map[1].br_startoff += (rbno - agbno + rlen);
1516 map[1].br_startblock += (rbno - agbno + rlen);
1517 }
1518
1519next:
1520 fbno = map[0].br_startoff + map[0].br_blockcount;
1521 }
1522out:
1523 return error;
1524}
1525
1526/* Clear the inode reflink flag if there are no shared extents. */
1527int
1528xfs_reflink_clear_inode_flag(
1529 struct xfs_inode *ip,
1530 struct xfs_trans **tpp)
1531{
1532 struct xfs_mount *mp = ip->i_mount;
1533 xfs_fileoff_t fbno;
1534 xfs_filblks_t end;
1535 xfs_agnumber_t agno;
1536 xfs_agblock_t agbno;
1537 xfs_extlen_t aglen;
1538 xfs_agblock_t rbno;
1539 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001540 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001541 int nmaps;
1542 int error = 0;
1543
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001544 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001545
1546 fbno = 0;
1547 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1548 while (end - fbno > 0) {
1549 nmaps = 1;
1550 /*
1551 * Look for extents in the file. Skip holes, delalloc, or
1552 * unwritten extents; they can't be reflinked.
1553 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001554 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001555 if (error)
1556 return error;
1557 if (nmaps == 0)
1558 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001559 if (map.br_startblock == HOLESTARTBLOCK ||
1560 map.br_startblock == DELAYSTARTBLOCK ||
1561 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001562 goto next;
1563
Darrick J. Wong024adf42016-10-10 16:47:40 +11001564 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1565 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1566 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001567
Darrick J. Wong024adf42016-10-10 16:47:40 +11001568 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1569 &rbno, &rlen, false);
1570 if (error)
1571 return error;
1572 /* Is there still a shared block here? */
1573 if (rbno != NULLAGBLOCK)
1574 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001575next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001576 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001577 }
1578
1579 /*
1580 * We didn't find any shared blocks so turn off the reflink flag.
1581 * First, get rid of any leftover CoW mappings.
1582 */
1583 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1584 if (error)
1585 return error;
1586
1587 /* Clear the inode flag. */
1588 trace_xfs_reflink_unset_inode_flag(ip);
1589 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001590 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001591 xfs_trans_ijoin(*tpp, ip, 0);
1592 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1593
1594 return error;
1595}
1596
1597/*
1598 * Clear the inode reflink flag if there are no shared extents and the size
1599 * hasn't changed.
1600 */
1601STATIC int
1602xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001603 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001604{
1605 struct xfs_mount *mp = ip->i_mount;
1606 struct xfs_trans *tp;
1607 int error = 0;
1608
1609 /* Start a rolling transaction to remove the mappings */
1610 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1611 if (error)
1612 return error;
1613
1614 xfs_ilock(ip, XFS_ILOCK_EXCL);
1615 xfs_trans_ijoin(tp, ip, 0);
1616
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001617 error = xfs_reflink_clear_inode_flag(ip, &tp);
1618 if (error)
1619 goto cancel;
1620
1621 error = xfs_trans_commit(tp);
1622 if (error)
1623 goto out;
1624
1625 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1626 return 0;
1627cancel:
1628 xfs_trans_cancel(tp);
1629out:
1630 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1631 return error;
1632}
1633
1634/*
1635 * Pre-COW all shared blocks within a given byte range of a file and turn off
1636 * the reflink flag if we unshare all of the file's blocks.
1637 */
1638int
1639xfs_reflink_unshare(
1640 struct xfs_inode *ip,
1641 xfs_off_t offset,
1642 xfs_off_t len)
1643{
1644 struct xfs_mount *mp = ip->i_mount;
1645 xfs_fileoff_t fbno;
1646 xfs_filblks_t end;
1647 xfs_off_t isize;
1648 int error;
1649
1650 if (!xfs_is_reflink_inode(ip))
1651 return 0;
1652
1653 trace_xfs_reflink_unshare(ip, offset, len);
1654
1655 inode_dio_wait(VFS_I(ip));
1656
1657 /* Try to CoW the selected ranges */
1658 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001659 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001660 isize = i_size_read(VFS_I(ip));
1661 end = XFS_B_TO_FSB(mp, offset + len);
1662 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1663 if (error)
1664 goto out_unlock;
1665 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1666
1667 /* Wait for the IO to finish */
1668 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1669 if (error)
1670 goto out;
1671
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001672 /* Turn off the reflink flag if possible. */
1673 error = xfs_reflink_try_clear_inode_flag(ip);
1674 if (error)
1675 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001676
1677 return 0;
1678
1679out_unlock:
1680 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1681out:
1682 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1683 return error;
1684}