blob: becf2465dd2312d01b6a01accdf8e5bd9b6de163 [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 Hellwig2755fc442016-11-24 11:39:49 +1100246 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
247 struct xfs_bmbt_irec got;
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100248 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 Hellwig2755fc442016-11-24 11:39:49 +1100260
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 Foster0260d8f2016-11-28 14:57:42 +1100289 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 Foster0260d8f2016-11-28 14:57:42 +1100292 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/*
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100400 * Find the CoW reservation for a given byte offset of a file.
Darrick J. Wongef473662016-10-03 09:11:34 -0700401 */
402bool
403xfs_reflink_find_cow_mapping(
404 struct xfs_inode *ip,
405 xfs_off_t offset,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100406 struct xfs_bmbt_irec *imap)
Darrick J. Wongef473662016-10-03 09:11:34 -0700407{
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100408 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
409 xfs_fileoff_t offset_fsb;
410 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700411 xfs_extnum_t idx;
412
413 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
414 ASSERT(xfs_is_reflink_inode(ip));
415
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100416 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
417 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Darrick J. Wongef473662016-10-03 09:11:34 -0700418 return false;
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100419 if (got.br_startoff > offset_fsb)
Darrick J. Wongef473662016-10-03 09:11:34 -0700420 return false;
421
422 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100423 &got);
424 *imap = got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700425 return true;
426}
427
428/*
429 * Trim an extent to end at the next CoW reservation past offset_fsb.
430 */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100431void
Darrick J. Wongef473662016-10-03 09:11:34 -0700432xfs_reflink_trim_irec_to_next_cow(
433 struct xfs_inode *ip,
434 xfs_fileoff_t offset_fsb,
435 struct xfs_bmbt_irec *imap)
436{
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100437 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
438 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700439 xfs_extnum_t idx;
440
441 if (!xfs_is_reflink_inode(ip))
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100442 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700443
444 /* Find the extent in the CoW fork. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100445 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
446 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700447
448 /* This is the extent before; try sliding up one. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100449 if (got.br_startoff < offset_fsb) {
450 if (!xfs_iext_get_extent(ifp, idx + 1, &got))
451 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700452 }
453
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100454 if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
455 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700456
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100457 imap->br_blockcount = got.br_startoff - imap->br_startoff;
Darrick J. Wongef473662016-10-03 09:11:34 -0700458 trace_xfs_reflink_trim_irec(ip, imap);
Darrick J. Wongef473662016-10-03 09:11:34 -0700459}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700460
461/*
462 * Cancel all pending CoW reservations for some block range of an inode.
463 */
464int
465xfs_reflink_cancel_cow_blocks(
466 struct xfs_inode *ip,
467 struct xfs_trans **tpp,
468 xfs_fileoff_t offset_fsb,
469 xfs_fileoff_t end_fsb)
470{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100471 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100472 struct xfs_bmbt_irec got, del;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100473 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700474 xfs_fsblock_t firstfsb;
475 struct xfs_defer_ops dfops;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100476 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700477
478 if (!xfs_is_reflink_inode(ip))
479 return 0;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100480 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100481 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700482
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100483 while (got.br_startoff < end_fsb) {
484 del = got;
485 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
486 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700487
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100488 if (isnullstartblock(del.br_startblock)) {
489 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
490 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700491 if (error)
492 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700493 } else {
494 xfs_trans_ijoin(*tpp, ip, 0);
495 xfs_defer_init(&dfops, &firstfsb);
496
Darrick J. Wong174edb02016-10-03 09:11:39 -0700497 /* Free the CoW orphan record. */
498 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100499 &dfops, del.br_startblock,
500 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700501 if (error)
502 break;
503
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700504 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100505 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700506 NULL);
507
508 /* Update quota accounting */
509 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100510 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700511
512 /* Roll the transaction */
513 error = xfs_defer_finish(tpp, &dfops, ip);
514 if (error) {
515 xfs_defer_cancel(&dfops);
516 break;
517 }
518
519 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100520 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700521 }
522
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100523 if (!xfs_iext_get_extent(ifp, ++idx, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100524 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700525 }
526
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100527 /* clear tag if cow fork is emptied */
528 if (!ifp->if_bytes)
529 xfs_inode_clear_cowblocks_tag(ip);
530
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700531 return error;
532}
533
534/*
535 * Cancel all pending CoW reservations for some byte range of an inode.
536 */
537int
538xfs_reflink_cancel_cow_range(
539 struct xfs_inode *ip,
540 xfs_off_t offset,
541 xfs_off_t count)
542{
543 struct xfs_trans *tp;
544 xfs_fileoff_t offset_fsb;
545 xfs_fileoff_t end_fsb;
546 int error;
547
548 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100549 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700550
551 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
552 if (count == NULLFILEOFF)
553 end_fsb = NULLFILEOFF;
554 else
555 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
556
557 /* Start a rolling transaction to remove the mappings */
558 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
559 0, 0, 0, &tp);
560 if (error)
561 goto out;
562
563 xfs_ilock(ip, XFS_ILOCK_EXCL);
564 xfs_trans_ijoin(tp, ip, 0);
565
566 /* Scrape out the old CoW reservations */
567 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
568 if (error)
569 goto out_cancel;
570
571 error = xfs_trans_commit(tp);
572
573 xfs_iunlock(ip, XFS_ILOCK_EXCL);
574 return error;
575
576out_cancel:
577 xfs_trans_cancel(tp);
578 xfs_iunlock(ip, XFS_ILOCK_EXCL);
579out:
580 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
581 return error;
582}
583
584/*
585 * Remap parts of a file's data fork after a successful CoW.
586 */
587int
588xfs_reflink_end_cow(
589 struct xfs_inode *ip,
590 xfs_off_t offset,
591 xfs_off_t count)
592{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100593 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100594 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700595 struct xfs_trans *tp;
596 xfs_fileoff_t offset_fsb;
597 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700598 xfs_fsblock_t firstfsb;
599 struct xfs_defer_ops dfops;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100600 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700601 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700602 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100603 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700604
605 trace_xfs_reflink_end_cow(ip, offset, count);
606
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100607 /* No COW extents? That's easy! */
608 if (ifp->if_bytes == 0)
609 return 0;
610
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700611 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
612 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700613
614 /* Start a rolling transaction to switch the mappings */
615 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
616 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
617 resblks, 0, 0, &tp);
618 if (error)
619 goto out;
620
621 xfs_ilock(ip, XFS_ILOCK_EXCL);
622 xfs_trans_ijoin(tp, ip, 0);
623
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100624 /* If there is a hole at end_fsb - 1 go to the previous extent */
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100625 if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
626 got.br_startoff > end_fsb) {
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100627 ASSERT(idx > 0);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100628 xfs_iext_get_extent(ifp, --idx, &got);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100629 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700630
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100631 /* Walk backwards until we're out of the I/O range... */
632 while (got.br_startoff + got.br_blockcount > offset_fsb) {
633 del = got;
634 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
635
636 /* Extent delete may have bumped idx forward */
637 if (!del.br_blockcount) {
638 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700639 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700640 }
641
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100642 ASSERT(!isnullstartblock(got.br_startblock));
643
644 /* Unmap the old blocks in the data fork. */
645 xfs_defer_init(&dfops, &firstfsb);
646 rlen = del.br_blockcount;
647 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
648 &firstfsb, &dfops);
649 if (error)
650 goto out_defer;
651
652 /* Trim the extent to whatever got unmapped. */
653 if (rlen) {
654 xfs_trim_extent(&del, del.br_startoff + rlen,
655 del.br_blockcount - rlen);
656 }
657 trace_xfs_reflink_cow_remap(ip, &del);
658
659 /* Free the CoW orphan record. */
660 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
661 del.br_startblock, del.br_blockcount);
662 if (error)
663 goto out_defer;
664
665 /* Map the new blocks into the data fork. */
666 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
667 if (error)
668 goto out_defer;
669
670 /* Remove the mapping from the CoW fork. */
671 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
672
673 error = xfs_defer_finish(&tp, &dfops, ip);
674 if (error)
675 goto out_defer;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700676next_extent:
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100677 if (!xfs_iext_get_extent(ifp, idx, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100678 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700679 }
680
681 error = xfs_trans_commit(tp);
682 xfs_iunlock(ip, XFS_ILOCK_EXCL);
683 if (error)
684 goto out;
685 return 0;
686
687out_defer:
688 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700689 xfs_trans_cancel(tp);
690 xfs_iunlock(ip, XFS_ILOCK_EXCL);
691out:
692 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
693 return error;
694}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700695
696/*
697 * Free leftover CoW reservations that didn't get cleaned out.
698 */
699int
700xfs_reflink_recover_cow(
701 struct xfs_mount *mp)
702{
703 xfs_agnumber_t agno;
704 int error = 0;
705
706 if (!xfs_sb_version_hasreflink(&mp->m_sb))
707 return 0;
708
709 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
710 error = xfs_refcount_recover_cow_leftovers(mp, agno);
711 if (error)
712 break;
713 }
714
715 return error;
716}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700717
718/*
719 * Reflinking (Block) Ranges of Two Files Together
720 *
721 * First, ensure that the reflink flag is set on both inodes. The flag is an
722 * optimization to avoid unnecessary refcount btree lookups in the write path.
723 *
724 * Now we can iteratively remap the range of extents (and holes) in src to the
725 * corresponding ranges in dest. Let drange and srange denote the ranges of
726 * logical blocks in dest and src touched by the reflink operation.
727 *
728 * While the length of drange is greater than zero,
729 * - Read src's bmbt at the start of srange ("imap")
730 * - If imap doesn't exist, make imap appear to start at the end of srange
731 * with zero length.
732 * - If imap starts before srange, advance imap to start at srange.
733 * - If imap goes beyond srange, truncate imap to end at the end of srange.
734 * - Punch (imap start - srange start + imap len) blocks from dest at
735 * offset (drange start).
736 * - If imap points to a real range of pblks,
737 * > Increase the refcount of the imap's pblks
738 * > Map imap's pblks into dest at the offset
739 * (drange start + imap start - srange start)
740 * - Advance drange and srange by (imap start - srange start + imap len)
741 *
742 * Finally, if the reflink made dest longer, update both the in-core and
743 * on-disk file sizes.
744 *
745 * ASCII Art Demonstration:
746 *
747 * Let's say we want to reflink this source file:
748 *
749 * ----SSSSSSS-SSSSS----SSSSSS (src file)
750 * <-------------------->
751 *
752 * into this destination file:
753 *
754 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
755 * <-------------------->
756 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
757 * Observe that the range has different logical offsets in either file.
758 *
759 * Consider that the first extent in the source file doesn't line up with our
760 * reflink range. Unmapping and remapping are separate operations, so we can
761 * unmap more blocks from the destination file than we remap.
762 *
763 * ----SSSSSSS-SSSSS----SSSSSS
764 * <------->
765 * --DDDDD---------DDDDD--DDD
766 * <------->
767 *
768 * Now remap the source extent into the destination file:
769 *
770 * ----SSSSSSS-SSSSS----SSSSSS
771 * <------->
772 * --DDDDD--SSSSSSSDDDDD--DDD
773 * <------->
774 *
775 * Do likewise with the second hole and extent in our range. Holes in the
776 * unmap range don't affect our operation.
777 *
778 * ----SSSSSSS-SSSSS----SSSSSS
779 * <---->
780 * --DDDDD--SSSSSSS-SSSSS-DDD
781 * <---->
782 *
783 * Finally, unmap and remap part of the third extent. This will increase the
784 * size of the destination file.
785 *
786 * ----SSSSSSS-SSSSS----SSSSSS
787 * <----->
788 * --DDDDD--SSSSSSS-SSSSS----SSS
789 * <----->
790 *
791 * Once we update the destination file's i_size, we're done.
792 */
793
794/*
795 * Ensure the reflink bit is set in both inodes.
796 */
797STATIC int
798xfs_reflink_set_inode_flag(
799 struct xfs_inode *src,
800 struct xfs_inode *dest)
801{
802 struct xfs_mount *mp = src->i_mount;
803 int error;
804 struct xfs_trans *tp;
805
806 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
807 return 0;
808
809 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
810 if (error)
811 goto out_error;
812
813 /* Lock both files against IO */
814 if (src->i_ino == dest->i_ino)
815 xfs_ilock(src, XFS_ILOCK_EXCL);
816 else
817 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
818
819 if (!xfs_is_reflink_inode(src)) {
820 trace_xfs_reflink_set_inode_flag(src);
821 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
822 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
823 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
824 xfs_ifork_init_cow(src);
825 } else
826 xfs_iunlock(src, XFS_ILOCK_EXCL);
827
828 if (src->i_ino == dest->i_ino)
829 goto commit_flags;
830
831 if (!xfs_is_reflink_inode(dest)) {
832 trace_xfs_reflink_set_inode_flag(dest);
833 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
834 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
835 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
836 xfs_ifork_init_cow(dest);
837 } else
838 xfs_iunlock(dest, XFS_ILOCK_EXCL);
839
840commit_flags:
841 error = xfs_trans_commit(tp);
842 if (error)
843 goto out_error;
844 return error;
845
846out_error:
847 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
848 return error;
849}
850
851/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700852 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700853 */
854STATIC int
855xfs_reflink_update_dest(
856 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700857 xfs_off_t newlen,
858 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700859{
860 struct xfs_mount *mp = dest->i_mount;
861 struct xfs_trans *tp;
862 int error;
863
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700864 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700865 return 0;
866
867 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
868 if (error)
869 goto out_error;
870
871 xfs_ilock(dest, XFS_ILOCK_EXCL);
872 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
873
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700874 if (newlen > i_size_read(VFS_I(dest))) {
875 trace_xfs_reflink_update_inode_size(dest, newlen);
876 i_size_write(VFS_I(dest), newlen);
877 dest->i_d.di_size = newlen;
878 }
879
880 if (cowextsize) {
881 dest->i_d.di_cowextsize = cowextsize;
882 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
883 }
884
Darrick J. Wong862bb362016-10-03 09:11:40 -0700885 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
886
887 error = xfs_trans_commit(tp);
888 if (error)
889 goto out_error;
890 return error;
891
892out_error:
893 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
894 return error;
895}
896
897/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700898 * Do we have enough reserve in this AG to handle a reflink? The refcount
899 * btree already reserved all the space it needs, but the rmap btree can grow
900 * infinitely, so we won't allow more reflinks when the AG is down to the
901 * btree reserves.
902 */
903static int
904xfs_reflink_ag_has_free_space(
905 struct xfs_mount *mp,
906 xfs_agnumber_t agno)
907{
908 struct xfs_perag *pag;
909 int error = 0;
910
911 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
912 return 0;
913
914 pag = xfs_perag_get(mp, agno);
915 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
916 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
917 error = -ENOSPC;
918 xfs_perag_put(pag);
919 return error;
920}
921
922/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700923 * Unmap a range of blocks from a file, then map other blocks into the hole.
924 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
925 * The extent irec is mapped into dest at irec->br_startoff.
926 */
927STATIC int
928xfs_reflink_remap_extent(
929 struct xfs_inode *ip,
930 struct xfs_bmbt_irec *irec,
931 xfs_fileoff_t destoff,
932 xfs_off_t new_isize)
933{
934 struct xfs_mount *mp = ip->i_mount;
935 struct xfs_trans *tp;
936 xfs_fsblock_t firstfsb;
937 unsigned int resblks;
938 struct xfs_defer_ops dfops;
939 struct xfs_bmbt_irec uirec;
940 bool real_extent;
941 xfs_filblks_t rlen;
942 xfs_filblks_t unmap_len;
943 xfs_off_t newlen;
944 int error;
945
946 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
947 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
948
949 /* Only remap normal extents. */
950 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
951 irec->br_startblock != DELAYSTARTBLOCK &&
952 !ISUNWRITTEN(irec));
953
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700954 /* No reflinking if we're low on space */
955 if (real_extent) {
956 error = xfs_reflink_ag_has_free_space(mp,
957 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
958 if (error)
959 goto out;
960 }
961
Darrick J. Wong862bb362016-10-03 09:11:40 -0700962 /* Start a rolling transaction to switch the mappings */
963 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
964 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
965 if (error)
966 goto out;
967
968 xfs_ilock(ip, XFS_ILOCK_EXCL);
969 xfs_trans_ijoin(tp, ip, 0);
970
971 /* If we're not just clearing space, then do we have enough quota? */
972 if (real_extent) {
973 error = xfs_trans_reserve_quota_nblks(tp, ip,
974 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
975 if (error)
976 goto out_cancel;
977 }
978
979 trace_xfs_reflink_remap(ip, irec->br_startoff,
980 irec->br_blockcount, irec->br_startblock);
981
982 /* Unmap the old blocks in the data fork. */
983 rlen = unmap_len;
984 while (rlen) {
985 xfs_defer_init(&dfops, &firstfsb);
986 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
987 &firstfsb, &dfops);
988 if (error)
989 goto out_defer;
990
991 /*
992 * Trim the extent to whatever got unmapped.
993 * Remember, bunmapi works backwards.
994 */
995 uirec.br_startblock = irec->br_startblock + rlen;
996 uirec.br_startoff = irec->br_startoff + rlen;
997 uirec.br_blockcount = unmap_len - rlen;
998 unmap_len = rlen;
999
1000 /* If this isn't a real mapping, we're done. */
1001 if (!real_extent || uirec.br_blockcount == 0)
1002 goto next_extent;
1003
1004 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1005 uirec.br_blockcount, uirec.br_startblock);
1006
1007 /* Update the refcount tree */
1008 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1009 if (error)
1010 goto out_defer;
1011
1012 /* Map the new blocks into the data fork. */
1013 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1014 if (error)
1015 goto out_defer;
1016
1017 /* Update quota accounting. */
1018 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1019 uirec.br_blockcount);
1020
1021 /* Update dest isize if needed. */
1022 newlen = XFS_FSB_TO_B(mp,
1023 uirec.br_startoff + uirec.br_blockcount);
1024 newlen = min_t(xfs_off_t, newlen, new_isize);
1025 if (newlen > i_size_read(VFS_I(ip))) {
1026 trace_xfs_reflink_update_inode_size(ip, newlen);
1027 i_size_write(VFS_I(ip), newlen);
1028 ip->i_d.di_size = newlen;
1029 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1030 }
1031
1032next_extent:
1033 /* Process all the deferred stuff. */
1034 error = xfs_defer_finish(&tp, &dfops, ip);
1035 if (error)
1036 goto out_defer;
1037 }
1038
1039 error = xfs_trans_commit(tp);
1040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1041 if (error)
1042 goto out;
1043 return 0;
1044
1045out_defer:
1046 xfs_defer_cancel(&dfops);
1047out_cancel:
1048 xfs_trans_cancel(tp);
1049 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1050out:
1051 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1052 return error;
1053}
1054
1055/*
1056 * Iteratively remap one file's extents (and holes) to another's.
1057 */
1058STATIC int
1059xfs_reflink_remap_blocks(
1060 struct xfs_inode *src,
1061 xfs_fileoff_t srcoff,
1062 struct xfs_inode *dest,
1063 xfs_fileoff_t destoff,
1064 xfs_filblks_t len,
1065 xfs_off_t new_isize)
1066{
1067 struct xfs_bmbt_irec imap;
1068 int nimaps;
1069 int error = 0;
1070 xfs_filblks_t range_len;
1071
1072 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1073 while (len) {
1074 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1075 dest, destoff);
1076 /* Read extent from the source file */
1077 nimaps = 1;
1078 xfs_ilock(src, XFS_ILOCK_EXCL);
1079 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1080 xfs_iunlock(src, XFS_ILOCK_EXCL);
1081 if (error)
1082 goto err;
1083 ASSERT(nimaps == 1);
1084
1085 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1086 &imap);
1087
1088 /* Translate imap into the destination file. */
1089 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1090 imap.br_startoff += destoff - srcoff;
1091
1092 /* Clear dest from destoff to the end of imap and map it in. */
1093 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1094 new_isize);
1095 if (error)
1096 goto err;
1097
1098 if (fatal_signal_pending(current)) {
1099 error = -EINTR;
1100 goto err;
1101 }
1102
1103 /* Advance drange/srange */
1104 srcoff += range_len;
1105 destoff += range_len;
1106 len -= range_len;
1107 }
1108
1109 return 0;
1110
1111err:
1112 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1113 return error;
1114}
1115
1116/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001117 * Read a page's worth of file data into the page cache. Return the page
1118 * locked.
1119 */
1120static struct page *
1121xfs_get_page(
1122 struct inode *inode,
1123 xfs_off_t offset)
1124{
1125 struct address_space *mapping;
1126 struct page *page;
1127 pgoff_t n;
1128
1129 n = offset >> PAGE_SHIFT;
1130 mapping = inode->i_mapping;
1131 page = read_mapping_page(mapping, n, NULL);
1132 if (IS_ERR(page))
1133 return page;
1134 if (!PageUptodate(page)) {
1135 put_page(page);
1136 return ERR_PTR(-EIO);
1137 }
1138 lock_page(page);
1139 return page;
1140}
1141
1142/*
1143 * Compare extents of two files to see if they are the same.
1144 */
1145static int
1146xfs_compare_extents(
1147 struct inode *src,
1148 xfs_off_t srcoff,
1149 struct inode *dest,
1150 xfs_off_t destoff,
1151 xfs_off_t len,
1152 bool *is_same)
1153{
1154 xfs_off_t src_poff;
1155 xfs_off_t dest_poff;
1156 void *src_addr;
1157 void *dest_addr;
1158 struct page *src_page;
1159 struct page *dest_page;
1160 xfs_off_t cmp_len;
1161 bool same;
1162 int error;
1163
1164 error = -EINVAL;
1165 same = true;
1166 while (len) {
1167 src_poff = srcoff & (PAGE_SIZE - 1);
1168 dest_poff = destoff & (PAGE_SIZE - 1);
1169 cmp_len = min(PAGE_SIZE - src_poff,
1170 PAGE_SIZE - dest_poff);
1171 cmp_len = min(cmp_len, len);
1172 ASSERT(cmp_len > 0);
1173
1174 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1175 XFS_I(dest), destoff);
1176
1177 src_page = xfs_get_page(src, srcoff);
1178 if (IS_ERR(src_page)) {
1179 error = PTR_ERR(src_page);
1180 goto out_error;
1181 }
1182 dest_page = xfs_get_page(dest, destoff);
1183 if (IS_ERR(dest_page)) {
1184 error = PTR_ERR(dest_page);
1185 unlock_page(src_page);
1186 put_page(src_page);
1187 goto out_error;
1188 }
1189 src_addr = kmap_atomic(src_page);
1190 dest_addr = kmap_atomic(dest_page);
1191
1192 flush_dcache_page(src_page);
1193 flush_dcache_page(dest_page);
1194
1195 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1196 same = false;
1197
1198 kunmap_atomic(dest_addr);
1199 kunmap_atomic(src_addr);
1200 unlock_page(dest_page);
1201 unlock_page(src_page);
1202 put_page(dest_page);
1203 put_page(src_page);
1204
1205 if (!same)
1206 break;
1207
1208 srcoff += cmp_len;
1209 destoff += cmp_len;
1210 len -= cmp_len;
1211 }
1212
1213 *is_same = same;
1214 return 0;
1215
1216out_error:
1217 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1218 return error;
1219}
1220
1221/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001222 * Link a range of blocks from one file to another.
1223 */
1224int
1225xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001226 struct file *file_in,
1227 loff_t pos_in,
1228 struct file *file_out,
1229 loff_t pos_out,
1230 u64 len,
1231 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001232{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001233 struct inode *inode_in = file_inode(file_in);
1234 struct xfs_inode *src = XFS_I(inode_in);
1235 struct inode *inode_out = file_inode(file_out);
1236 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001237 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001238 loff_t bs = inode_out->i_sb->s_blocksize;
1239 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001240 xfs_fileoff_t sfsbno, dfsbno;
1241 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001242 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001243 loff_t isize;
1244 ssize_t ret;
1245 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001246
1247 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1248 return -EOPNOTSUPP;
1249
1250 if (XFS_FORCED_SHUTDOWN(mp))
1251 return -EIO;
1252
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001253 /* Lock both files against IO */
1254 if (same_inode) {
1255 xfs_ilock(src, XFS_IOLOCK_EXCL);
1256 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1257 } else {
1258 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1259 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1260 }
1261
1262 /* Don't touch certain kinds of inodes */
1263 ret = -EPERM;
1264 if (IS_IMMUTABLE(inode_out))
1265 goto out_unlock;
1266
1267 ret = -ETXTBSY;
1268 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1269 goto out_unlock;
1270
1271
1272 /* Don't reflink dirs, pipes, sockets... */
1273 ret = -EISDIR;
1274 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1275 goto out_unlock;
1276 ret = -EINVAL;
1277 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1278 goto out_unlock;
1279 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1280 goto out_unlock;
1281
Darrick J. Wong862bb362016-10-03 09:11:40 -07001282 /* Don't reflink realtime inodes */
1283 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001284 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001285
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001286 /* Don't share DAX file data for now. */
1287 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1288 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001289
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001290 /* Are we going all the way to the end? */
1291 isize = i_size_read(inode_in);
1292 if (isize == 0) {
1293 ret = 0;
1294 goto out_unlock;
1295 }
1296
Darrick J. Wongfba3e592016-11-28 14:57:42 +11001297 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1298 if (len == 0) {
1299 if (is_dedupe) {
1300 ret = 0;
1301 goto out_unlock;
1302 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001303 len = isize - pos_in;
Darrick J. Wongfba3e592016-11-28 14:57:42 +11001304 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001305
1306 /* Ensure offsets don't wrap and the input is inside i_size */
1307 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1308 pos_in + len > isize)
1309 goto out_unlock;
1310
1311 /* Don't allow dedupe past EOF in the dest file */
1312 if (is_dedupe) {
1313 loff_t disize;
1314
1315 disize = i_size_read(inode_out);
1316 if (pos_out >= disize || pos_out + len > disize)
1317 goto out_unlock;
1318 }
1319
1320 /* If we're linking to EOF, continue to the block boundary. */
1321 if (pos_in + len == isize)
1322 blen = ALIGN(isize, bs) - pos_in;
1323 else
1324 blen = len;
1325
1326 /* Only reflink if we're aligned to block boundaries */
1327 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1328 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1329 goto out_unlock;
1330
1331 /* Don't allow overlapped reflink within the same file */
1332 if (same_inode) {
1333 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1334 goto out_unlock;
1335 }
1336
1337 /* Wait for the completion of any pending IOs on both files */
1338 inode_dio_wait(inode_in);
1339 if (!same_inode)
1340 inode_dio_wait(inode_out);
1341
1342 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1343 pos_in, pos_in + len - 1);
1344 if (ret)
1345 goto out_unlock;
1346
1347 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1348 pos_out, pos_out + len - 1);
1349 if (ret)
1350 goto out_unlock;
1351
1352 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001353
Darrick J. Wongcc714662016-10-03 09:11:41 -07001354 /*
1355 * Check that the extents are the same.
1356 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001357 if (is_dedupe) {
1358 bool is_same = false;
1359
1360 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1361 len, &is_same);
1362 if (ret)
1363 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001364 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001365 ret = -EBADE;
1366 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001367 }
1368 }
1369
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001370 ret = xfs_reflink_set_inode_flag(src, dest);
1371 if (ret)
1372 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001373
1374 /*
1375 * Invalidate the page cache so that we can clear any CoW mappings
1376 * in the destination file.
1377 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001378 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1379 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001380
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001381 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1382 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001383 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001384 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1385 pos_out + len);
1386 if (ret)
1387 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001388
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001389 /*
1390 * Carry the cowextsize hint from src to dest if we're sharing the
1391 * entire source file to the entire destination file, the source file
1392 * has a cowextsize hint, and the destination file does not.
1393 */
1394 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001395 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001396 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001397 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001398 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1399 cowextsize = src->i_d.di_cowextsize;
1400
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001401 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001402
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001403out_unlock:
1404 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1405 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1406 if (src->i_ino != dest->i_ino) {
1407 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1408 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1409 }
1410 if (ret)
1411 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1412 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001413}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001414
1415/*
1416 * The user wants to preemptively CoW all shared blocks in this file,
1417 * which enables us to turn off the reflink flag. Iterate all
1418 * extents which are not prealloc/delalloc to see which ranges are
1419 * mentioned in the refcount tree, then read those blocks into the
1420 * pagecache, dirty them, fsync them back out, and then we can update
1421 * the inode flag. What happens if we run out of memory? :)
1422 */
1423STATIC int
1424xfs_reflink_dirty_extents(
1425 struct xfs_inode *ip,
1426 xfs_fileoff_t fbno,
1427 xfs_filblks_t end,
1428 xfs_off_t isize)
1429{
1430 struct xfs_mount *mp = ip->i_mount;
1431 xfs_agnumber_t agno;
1432 xfs_agblock_t agbno;
1433 xfs_extlen_t aglen;
1434 xfs_agblock_t rbno;
1435 xfs_extlen_t rlen;
1436 xfs_off_t fpos;
1437 xfs_off_t flen;
1438 struct xfs_bmbt_irec map[2];
1439 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001440 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001441
1442 while (end - fbno > 0) {
1443 nmaps = 1;
1444 /*
1445 * Look for extents in the file. Skip holes, delalloc, or
1446 * unwritten extents; they can't be reflinked.
1447 */
1448 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1449 if (error)
1450 goto out;
1451 if (nmaps == 0)
1452 break;
1453 if (map[0].br_startblock == HOLESTARTBLOCK ||
1454 map[0].br_startblock == DELAYSTARTBLOCK ||
1455 ISUNWRITTEN(&map[0]))
1456 goto next;
1457
1458 map[1] = map[0];
1459 while (map[1].br_blockcount) {
1460 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1461 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1462 aglen = map[1].br_blockcount;
1463
1464 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1465 &rbno, &rlen, true);
1466 if (error)
1467 goto out;
1468 if (rbno == NULLAGBLOCK)
1469 break;
1470
1471 /* Dirty the pages */
1472 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1473 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1474 (rbno - agbno));
1475 flen = XFS_FSB_TO_B(mp, rlen);
1476 if (fpos + flen > isize)
1477 flen = isize - fpos;
1478 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1479 &xfs_iomap_ops);
1480 xfs_ilock(ip, XFS_ILOCK_EXCL);
1481 if (error)
1482 goto out;
1483
1484 map[1].br_blockcount -= (rbno - agbno + rlen);
1485 map[1].br_startoff += (rbno - agbno + rlen);
1486 map[1].br_startblock += (rbno - agbno + rlen);
1487 }
1488
1489next:
1490 fbno = map[0].br_startoff + map[0].br_blockcount;
1491 }
1492out:
1493 return error;
1494}
1495
1496/* Clear the inode reflink flag if there are no shared extents. */
1497int
1498xfs_reflink_clear_inode_flag(
1499 struct xfs_inode *ip,
1500 struct xfs_trans **tpp)
1501{
1502 struct xfs_mount *mp = ip->i_mount;
1503 xfs_fileoff_t fbno;
1504 xfs_filblks_t end;
1505 xfs_agnumber_t agno;
1506 xfs_agblock_t agbno;
1507 xfs_extlen_t aglen;
1508 xfs_agblock_t rbno;
1509 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001510 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001511 int nmaps;
1512 int error = 0;
1513
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001514 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001515
1516 fbno = 0;
1517 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1518 while (end - fbno > 0) {
1519 nmaps = 1;
1520 /*
1521 * Look for extents in the file. Skip holes, delalloc, or
1522 * unwritten extents; they can't be reflinked.
1523 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001524 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001525 if (error)
1526 return error;
1527 if (nmaps == 0)
1528 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001529 if (map.br_startblock == HOLESTARTBLOCK ||
1530 map.br_startblock == DELAYSTARTBLOCK ||
1531 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001532 goto next;
1533
Darrick J. Wong024adf42016-10-10 16:47:40 +11001534 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1535 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1536 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001537
Darrick J. Wong024adf42016-10-10 16:47:40 +11001538 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1539 &rbno, &rlen, false);
1540 if (error)
1541 return error;
1542 /* Is there still a shared block here? */
1543 if (rbno != NULLAGBLOCK)
1544 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001545next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001546 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001547 }
1548
1549 /*
1550 * We didn't find any shared blocks so turn off the reflink flag.
1551 * First, get rid of any leftover CoW mappings.
1552 */
1553 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1554 if (error)
1555 return error;
1556
1557 /* Clear the inode flag. */
1558 trace_xfs_reflink_unset_inode_flag(ip);
1559 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001560 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001561 xfs_trans_ijoin(*tpp, ip, 0);
1562 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1563
1564 return error;
1565}
1566
1567/*
1568 * Clear the inode reflink flag if there are no shared extents and the size
1569 * hasn't changed.
1570 */
1571STATIC int
1572xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001573 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001574{
1575 struct xfs_mount *mp = ip->i_mount;
1576 struct xfs_trans *tp;
1577 int error = 0;
1578
1579 /* Start a rolling transaction to remove the mappings */
1580 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1581 if (error)
1582 return error;
1583
1584 xfs_ilock(ip, XFS_ILOCK_EXCL);
1585 xfs_trans_ijoin(tp, ip, 0);
1586
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001587 error = xfs_reflink_clear_inode_flag(ip, &tp);
1588 if (error)
1589 goto cancel;
1590
1591 error = xfs_trans_commit(tp);
1592 if (error)
1593 goto out;
1594
1595 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1596 return 0;
1597cancel:
1598 xfs_trans_cancel(tp);
1599out:
1600 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1601 return error;
1602}
1603
1604/*
1605 * Pre-COW all shared blocks within a given byte range of a file and turn off
1606 * the reflink flag if we unshare all of the file's blocks.
1607 */
1608int
1609xfs_reflink_unshare(
1610 struct xfs_inode *ip,
1611 xfs_off_t offset,
1612 xfs_off_t len)
1613{
1614 struct xfs_mount *mp = ip->i_mount;
1615 xfs_fileoff_t fbno;
1616 xfs_filblks_t end;
1617 xfs_off_t isize;
1618 int error;
1619
1620 if (!xfs_is_reflink_inode(ip))
1621 return 0;
1622
1623 trace_xfs_reflink_unshare(ip, offset, len);
1624
1625 inode_dio_wait(VFS_I(ip));
1626
1627 /* Try to CoW the selected ranges */
1628 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001629 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001630 isize = i_size_read(VFS_I(ip));
1631 end = XFS_B_TO_FSB(mp, offset + len);
1632 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1633 if (error)
1634 goto out_unlock;
1635 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1636
1637 /* Wait for the IO to finish */
1638 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1639 if (error)
1640 goto out;
1641
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001642 /* Turn off the reflink flag if possible. */
1643 error = xfs_reflink_try_clear_inode_flag(ip);
1644 if (error)
1645 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001646
1647 return 0;
1648
1649out_unlock:
1650 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1651out:
1652 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1653 return error;
1654}