blob: 29a75ecb24251336d504dd3428ff7e401d3abb38 [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 *
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -080085 * As an optimization, the CoW extent size hint (cowextsz) creates
86 * outsized aligned delalloc reservations in the hope of landing out of
87 * order nearby CoW writes in a single extent on disk, thereby reducing
88 * fragmentation and improving future performance.
89 *
90 * D: --RRRRRRSSSRRRRRRRR--- (data fork)
91 * C: ------DDDDDDD--------- (CoW fork)
92 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -070093 * When dirty pages are being written out (typically in writepage), the
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -080094 * delalloc reservations are converted into unwritten mappings by
95 * allocating blocks and replacing the delalloc mapping with real ones.
96 * A delalloc mapping can be replaced by several unwritten ones if the
97 * free space is fragmented.
98 *
99 * D: --RRRRRRSSSRRRRRRRR---
100 * C: ------UUUUUUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700101 *
102 * We want to adapt the delalloc mechanism for copy-on-write, since the
103 * write paths are similar. The first two steps (creating the reservation
104 * and allocating the blocks) are exactly the same as delalloc except that
105 * the mappings must be stored in a separate CoW fork because we do not want
106 * to disturb the mapping in the data fork until we're sure that the write
107 * succeeded. IO completion in this case is the process of removing the old
108 * mapping from the data fork and moving the new mapping from the CoW fork to
109 * the data fork. This will be discussed shortly.
110 *
111 * For now, unaligned directio writes will be bounced back to the page cache.
112 * Block-aligned directio writes will use the same mechanism as buffered
113 * writes.
114 *
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800115 * Just prior to submitting the actual disk write requests, we convert
116 * the extents representing the range of the file actually being written
117 * (as opposed to extra pieces created for the cowextsize hint) to real
118 * extents. This will become important in the next step:
119 *
120 * D: --RRRRRRSSSRRRRRRRR---
121 * C: ------UUrrUUU---------
122 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700123 * CoW remapping must be done after the data block write completes,
124 * because we don't want to destroy the old data fork map until we're sure
125 * the new block has been written. Since the new mappings are kept in a
126 * separate fork, we can simply iterate these mappings to find the ones
127 * that cover the file blocks that we just CoW'd. For each extent, simply
128 * unmap the corresponding range in the data fork, map the new range into
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800129 * the data fork, and remove the extent from the CoW fork. Because of
130 * the presence of the cowextsize hint, however, we must be careful
131 * only to remap the blocks that we've actually written out -- we must
132 * never remap delalloc reservations nor CoW staging blocks that have
133 * yet to be written. This corresponds exactly to the real extents in
134 * the CoW fork:
135 *
136 * D: --RRRRRRrrSRRRRRRRR---
137 * C: ------UU--UUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700138 *
139 * Since the remapping operation can be applied to an arbitrary file
140 * range, we record the need for the remap step as a flag in the ioend
141 * instead of declaring a new IO type. This is required for direct io
142 * because we only have ioend for the whole dio, and we have to be able to
143 * remember the presence of unwritten blocks and CoW blocks with a single
144 * ioend structure. Better yet, the more ground we can cover with one
145 * ioend, the better.
146 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700147
148/*
149 * Given an AG extent, find the lowest-numbered run of shared blocks
150 * within that range and return the range in fbno/flen. If
151 * find_end_of_shared is true, return the longest contiguous extent of
152 * shared blocks. If there are no shared extents, fbno and flen will
153 * be set to NULLAGBLOCK and 0, respectively.
154 */
155int
156xfs_reflink_find_shared(
157 struct xfs_mount *mp,
158 xfs_agnumber_t agno,
159 xfs_agblock_t agbno,
160 xfs_extlen_t aglen,
161 xfs_agblock_t *fbno,
162 xfs_extlen_t *flen,
163 bool find_end_of_shared)
164{
165 struct xfs_buf *agbp;
166 struct xfs_btree_cur *cur;
167 int error;
168
169 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
170 if (error)
171 return error;
172
173 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
174
175 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
176 find_end_of_shared);
177
178 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
179
180 xfs_buf_relse(agbp);
181 return error;
182}
183
184/*
185 * Trim the mapping to the next block where there's a change in the
186 * shared/unshared status. More specifically, this means that we
187 * find the lowest-numbered extent of shared blocks that coincides with
188 * the given block mapping. If the shared extent overlaps the start of
189 * the mapping, trim the mapping to the end of the shared extent. If
190 * the shared region intersects the mapping, trim the mapping to the
191 * start of the shared extent. If there are no shared regions that
192 * overlap, just return the original extent.
193 */
194int
195xfs_reflink_trim_around_shared(
196 struct xfs_inode *ip,
197 struct xfs_bmbt_irec *irec,
198 bool *shared,
199 bool *trimmed)
200{
201 xfs_agnumber_t agno;
202 xfs_agblock_t agbno;
203 xfs_extlen_t aglen;
204 xfs_agblock_t fbno;
205 xfs_extlen_t flen;
206 int error = 0;
207
208 /* Holes, unwritten, and delalloc extents cannot be shared */
209 if (!xfs_is_reflink_inode(ip) ||
210 ISUNWRITTEN(irec) ||
211 irec->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig62c5ac82016-10-20 15:52:00 +1100212 irec->br_startblock == DELAYSTARTBLOCK ||
213 isnullstartblock(irec->br_startblock)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700214 *shared = false;
215 return 0;
216 }
217
218 trace_xfs_reflink_trim_around_shared(ip, irec);
219
220 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
221 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
222 aglen = irec->br_blockcount;
223
224 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
225 aglen, &fbno, &flen, true);
226 if (error)
227 return error;
228
229 *shared = *trimmed = false;
230 if (fbno == NULLAGBLOCK) {
231 /* No shared blocks at all. */
232 return 0;
233 } else if (fbno == agbno) {
234 /*
235 * The start of this extent is shared. Truncate the
236 * mapping at the end of the shared region so that a
237 * subsequent iteration starts at the start of the
238 * unshared region.
239 */
240 irec->br_blockcount = flen;
241 *shared = true;
242 if (flen != aglen)
243 *trimmed = true;
244 return 0;
245 } else {
246 /*
247 * There's a shared extent midway through this extent.
248 * Truncate the mapping at the start of the shared
249 * extent so that a subsequent iteration starts at the
250 * start of the shared region.
251 */
252 irec->br_blockcount = fbno - agbno;
253 *trimmed = true;
254 return 0;
255 }
256}
257
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100258/*
259 * Trim the passed in imap to the next shared/unshared extent boundary, and
260 * if imap->br_startoff points to a shared extent reserve space for it in the
261 * COW fork. In this case *shared is set to true, else to false.
262 *
263 * Note that imap will always contain the block numbers for the existing blocks
264 * in the data fork, as the upper layers need them for read-modify-write
265 * operations.
266 */
267int
268xfs_reflink_reserve_cow(
Darrick J. Wong2a067052016-10-03 09:11:33 -0700269 struct xfs_inode *ip,
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100270 struct xfs_bmbt_irec *imap,
271 bool *shared)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700272{
Christoph Hellwig4a323332017-01-09 16:38:44 +0100273 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
274 struct xfs_bmbt_irec got;
Christoph Hellwig4a323332017-01-09 16:38:44 +0100275 int error = 0;
276 bool eof = false, trimmed;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700277 xfs_extnum_t idx;
278
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100279 /*
280 * Search the COW fork extent list first. This serves two purposes:
281 * first this implement the speculative preallocation using cowextisze,
282 * so that we also unshared block adjacent to shared blocks instead
283 * of just the shared blocks themselves. Second the lookup in the
284 * extent list is generally faster than going out to the shared extent
285 * tree.
286 */
Christoph Hellwig4a323332017-01-09 16:38:44 +0100287
288 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
289 eof = true;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100290 if (!eof && got.br_startoff <= imap->br_startoff) {
291 trace_xfs_reflink_cow_found(ip, imap);
292 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700293
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100294 *shared = true;
295 return 0;
296 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700297
298 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100299 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700300 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100301 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700302
303 /* Not shared? Just report the (potentially capped) extent. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100304 if (!*shared)
305 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700306
307 /*
308 * Fork all the shared blocks from our write offset until the end of
309 * the extent.
310 */
311 error = xfs_qm_dqattach_locked(ip, 0);
312 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100313 return error;
314
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100315 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
Brian Foster3d6e3b12017-01-09 16:38:45 +0100316 imap->br_blockcount, 0, &got, &idx, eof);
317 if (error == -ENOSPC || error == -EDQUOT)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100318 trace_xfs_reflink_cow_enospc(ip, imap);
Brian Foster3d6e3b12017-01-09 16:38:45 +0100319 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100320 return error;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700321
Darrick J. Wong2a067052016-10-03 09:11:33 -0700322 trace_xfs_reflink_cow_alloc(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100323 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700324}
Darrick J. Wongef473662016-10-03 09:11:34 -0700325
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800326/* Convert part of an unwritten CoW extent to a real one. */
327STATIC int
328xfs_reflink_convert_cow_extent(
329 struct xfs_inode *ip,
330 struct xfs_bmbt_irec *imap,
331 xfs_fileoff_t offset_fsb,
332 xfs_filblks_t count_fsb,
333 struct xfs_defer_ops *dfops)
334{
335 struct xfs_bmbt_irec irec = *imap;
336 xfs_fsblock_t first_block;
337 int nimaps = 1;
338
339 if (imap->br_state == XFS_EXT_NORM)
340 return 0;
341
342 xfs_trim_extent(&irec, offset_fsb, count_fsb);
343 trace_xfs_reflink_convert_cow(ip, &irec);
344 if (irec.br_blockcount == 0)
345 return 0;
346 return xfs_bmapi_write(NULL, ip, irec.br_startoff, irec.br_blockcount,
347 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
348 0, &irec, &nimaps, dfops);
349}
350
351/* Convert all of the unwritten CoW extents in a file's range to real ones. */
352int
353xfs_reflink_convert_cow(
354 struct xfs_inode *ip,
355 xfs_off_t offset,
356 xfs_off_t count)
357{
358 struct xfs_bmbt_irec got;
359 struct xfs_defer_ops dfops;
360 struct xfs_mount *mp = ip->i_mount;
361 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
362 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
363 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
364 xfs_extnum_t idx;
365 bool found;
Darrick J. Wonge5e2e562017-02-13 22:52:27 -0800366 int error = 0;
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800367
368 xfs_ilock(ip, XFS_ILOCK_EXCL);
369
370 /* Convert all the extents to real from unwritten. */
371 for (found = xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
372 found && got.br_startoff < end_fsb;
373 found = xfs_iext_get_extent(ifp, ++idx, &got)) {
374 error = xfs_reflink_convert_cow_extent(ip, &got, offset_fsb,
375 end_fsb - offset_fsb, &dfops);
376 if (error)
377 break;
378 }
379
380 /* Finish up. */
381 xfs_iunlock(ip, XFS_ILOCK_EXCL);
382 return error;
383}
384
Darrick J. Wong0613f162016-10-03 09:11:37 -0700385/* Allocate all CoW reservations covering a range of blocks in a file. */
386static int
387__xfs_reflink_allocate_cow(
388 struct xfs_inode *ip,
389 xfs_fileoff_t *offset_fsb,
390 xfs_fileoff_t end_fsb)
391{
392 struct xfs_mount *mp = ip->i_mount;
393 struct xfs_bmbt_irec imap;
394 struct xfs_defer_ops dfops;
395 struct xfs_trans *tp;
396 xfs_fsblock_t first_block;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700397 int nimaps = 1, error;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100398 bool shared;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700399
400 xfs_defer_init(&dfops, &first_block);
401
402 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
403 XFS_TRANS_RESERVE, &tp);
404 if (error)
405 return error;
406
407 xfs_ilock(ip, XFS_ILOCK_EXCL);
408
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100409 /* Read extent from the source file. */
410 nimaps = 1;
411 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
412 &imap, &nimaps, 0);
413 if (error)
414 goto out_unlock;
415 ASSERT(nimaps == 1);
416
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800417 /* Make sure there's a CoW reservation for it. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100418 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700419 if (error)
420 goto out_trans_cancel;
421
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100422 if (!shared) {
423 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700424 goto out_trans_cancel;
425 }
426
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800427 /* Allocate the entire reservation as unwritten blocks. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700428 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100429 error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800430 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
Darrick J. Wong0613f162016-10-03 09:11:37 -0700431 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
432 &imap, &nimaps, &dfops);
433 if (error)
434 goto out_trans_cancel;
435
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800436 /* Finish up. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700437 error = xfs_defer_finish(&tp, &dfops, NULL);
438 if (error)
439 goto out_trans_cancel;
440
441 error = xfs_trans_commit(tp);
442
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100443 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700444out_unlock:
445 xfs_iunlock(ip, XFS_ILOCK_EXCL);
446 return error;
447out_trans_cancel:
448 xfs_defer_cancel(&dfops);
449 xfs_trans_cancel(tp);
450 goto out_unlock;
451}
452
453/* Allocate all CoW reservations covering a part of a file. */
454int
455xfs_reflink_allocate_cow_range(
456 struct xfs_inode *ip,
457 xfs_off_t offset,
458 xfs_off_t count)
459{
460 struct xfs_mount *mp = ip->i_mount;
461 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
462 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
463 int error;
464
465 ASSERT(xfs_is_reflink_inode(ip));
466
467 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
468
469 /*
470 * Make sure that the dquots are there.
471 */
472 error = xfs_qm_dqattach(ip, 0);
473 if (error)
474 return error;
475
476 while (offset_fsb < end_fsb) {
477 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
478 if (error) {
479 trace_xfs_reflink_allocate_cow_range_error(ip, error,
480 _RET_IP_);
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800481 return error;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700482 }
483 }
484
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800485 /* Convert the CoW extents to regular. */
486 return xfs_reflink_convert_cow(ip, offset, count);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700487}
488
Darrick J. Wongef473662016-10-03 09:11:34 -0700489/*
490 * Find the CoW reservation (and whether or not it needs block allocation)
491 * for a given byte offset of a file.
492 */
493bool
494xfs_reflink_find_cow_mapping(
495 struct xfs_inode *ip,
496 xfs_off_t offset,
497 struct xfs_bmbt_irec *imap,
498 bool *need_alloc)
499{
500 struct xfs_bmbt_irec irec;
501 struct xfs_ifork *ifp;
502 struct xfs_bmbt_rec_host *gotp;
503 xfs_fileoff_t bno;
504 xfs_extnum_t idx;
505
506 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
507 ASSERT(xfs_is_reflink_inode(ip));
508
509 /* Find the extent in the CoW fork. */
510 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
511 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
512 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
513 if (!gotp)
514 return false;
515
516 xfs_bmbt_get_all(gotp, &irec);
517 if (bno >= irec.br_startoff + irec.br_blockcount ||
518 bno < irec.br_startoff)
519 return false;
520
521 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
522 &irec);
523
524 /* If it's still delalloc, we must allocate later. */
525 *imap = irec;
526 *need_alloc = !!(isnullstartblock(irec.br_startblock));
527
528 return true;
529}
530
531/*
532 * Trim an extent to end at the next CoW reservation past offset_fsb.
533 */
534int
535xfs_reflink_trim_irec_to_next_cow(
536 struct xfs_inode *ip,
537 xfs_fileoff_t offset_fsb,
538 struct xfs_bmbt_irec *imap)
539{
540 struct xfs_bmbt_irec irec;
541 struct xfs_ifork *ifp;
542 struct xfs_bmbt_rec_host *gotp;
543 xfs_extnum_t idx;
544
545 if (!xfs_is_reflink_inode(ip))
546 return 0;
547
548 /* Find the extent in the CoW fork. */
549 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
550 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
551 if (!gotp)
552 return 0;
553 xfs_bmbt_get_all(gotp, &irec);
554
555 /* This is the extent before; try sliding up one. */
556 if (irec.br_startoff < offset_fsb) {
557 idx++;
Eric Sandeenf380ee72017-01-09 16:38:36 +0100558 if (idx >= xfs_iext_count(ifp))
Darrick J. Wongef473662016-10-03 09:11:34 -0700559 return 0;
560 gotp = xfs_iext_get_ext(ifp, idx);
561 xfs_bmbt_get_all(gotp, &irec);
562 }
563
564 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
565 return 0;
566
567 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
568 trace_xfs_reflink_trim_irec(ip, imap);
569
570 return 0;
571}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700572
573/*
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800574 * Cancel CoW reservations for some block range of an inode.
575 *
576 * If cancel_real is true this function cancels all COW fork extents for the
577 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700578 */
579int
580xfs_reflink_cancel_cow_blocks(
581 struct xfs_inode *ip,
582 struct xfs_trans **tpp,
583 xfs_fileoff_t offset_fsb,
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800584 xfs_fileoff_t end_fsb,
585 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700586{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100587 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
588 struct xfs_bmbt_irec got, prev, del;
589 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700590 xfs_fsblock_t firstfsb;
591 struct xfs_defer_ops dfops;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100592 int error = 0, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700593
594 if (!xfs_is_reflink_inode(ip))
595 return 0;
596
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100597 xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
598 &got, &prev);
599 if (eof)
600 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700601
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100602 while (got.br_startoff < end_fsb) {
603 del = got;
604 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
605 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700606
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100607 if (isnullstartblock(del.br_startblock)) {
608 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
609 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700610 if (error)
611 break;
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800612 } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700613 xfs_trans_ijoin(*tpp, ip, 0);
614 xfs_defer_init(&dfops, &firstfsb);
615
Darrick J. Wong174edb02016-10-03 09:11:39 -0700616 /* Free the CoW orphan record. */
617 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100618 &dfops, del.br_startblock,
619 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700620 if (error)
621 break;
622
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700623 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100624 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700625 NULL);
626
627 /* Update quota accounting */
628 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100629 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700630
631 /* Roll the transaction */
632 error = xfs_defer_finish(tpp, &dfops, ip);
633 if (error) {
634 xfs_defer_cancel(&dfops);
635 break;
636 }
637
638 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100639 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700640 }
641
Eric Sandeenf380ee72017-01-09 16:38:36 +0100642 if (++idx >= xfs_iext_count(ifp))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100643 break;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100644 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700645 }
646
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100647 /* clear tag if cow fork is emptied */
648 if (!ifp->if_bytes)
649 xfs_inode_clear_cowblocks_tag(ip);
650
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700651 return error;
652}
653
654/*
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800655 * Cancel CoW reservations for some byte range of an inode.
656 *
657 * If cancel_real is true this function cancels all COW fork extents for the
658 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700659 */
660int
661xfs_reflink_cancel_cow_range(
662 struct xfs_inode *ip,
663 xfs_off_t offset,
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800664 xfs_off_t count,
665 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700666{
667 struct xfs_trans *tp;
668 xfs_fileoff_t offset_fsb;
669 xfs_fileoff_t end_fsb;
670 int error;
671
672 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100673 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700674
675 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
676 if (count == NULLFILEOFF)
677 end_fsb = NULLFILEOFF;
678 else
679 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
680
681 /* Start a rolling transaction to remove the mappings */
682 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
683 0, 0, 0, &tp);
684 if (error)
685 goto out;
686
687 xfs_ilock(ip, XFS_ILOCK_EXCL);
688 xfs_trans_ijoin(tp, ip, 0);
689
690 /* Scrape out the old CoW reservations */
Christoph Hellwig3b83a022017-03-07 16:45:58 -0800691 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
692 cancel_real);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700693 if (error)
694 goto out_cancel;
695
696 error = xfs_trans_commit(tp);
697
698 xfs_iunlock(ip, XFS_ILOCK_EXCL);
699 return error;
700
701out_cancel:
702 xfs_trans_cancel(tp);
703 xfs_iunlock(ip, XFS_ILOCK_EXCL);
704out:
705 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
706 return error;
707}
708
709/*
710 * Remap parts of a file's data fork after a successful CoW.
711 */
712int
713xfs_reflink_end_cow(
714 struct xfs_inode *ip,
715 xfs_off_t offset,
716 xfs_off_t count)
717{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100718 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
719 struct xfs_bmbt_irec got, prev, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700720 struct xfs_trans *tp;
721 xfs_fileoff_t offset_fsb;
722 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700723 xfs_fsblock_t firstfsb;
724 struct xfs_defer_ops dfops;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100725 int error, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700726 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700727 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100728 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700729
730 trace_xfs_reflink_end_cow(ip, offset, count);
731
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100732 /* No COW extents? That's easy! */
733 if (ifp->if_bytes == 0)
734 return 0;
735
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700736 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
737 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700738
Darrick J. Wongd457f822017-04-12 12:26:07 -0700739 /*
740 * Start a rolling transaction to switch the mappings. We're
741 * unlikely ever to have to remap 16T worth of single-block
742 * extents, so just cap the worst case extent count to 2^32-1.
743 * Stick a warning in just in case, and avoid 64-bit division.
744 */
745 BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
746 if (end_fsb - offset_fsb > UINT_MAX) {
747 error = -EFSCORRUPTED;
748 xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
749 ASSERT(0);
750 goto out;
751 }
752 resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
753 (unsigned int)(end_fsb - offset_fsb),
754 XFS_DATA_FORK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700755 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
756 resblks, 0, 0, &tp);
757 if (error)
758 goto out;
759
760 xfs_ilock(ip, XFS_ILOCK_EXCL);
761 xfs_trans_ijoin(tp, ip, 0);
762
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100763 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
764 &got, &prev);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700765
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100766 /* If there is a hole at end_fsb - 1 go to the previous extent */
767 if (eof || got.br_startoff > end_fsb) {
768 ASSERT(idx > 0);
769 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
770 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700771
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100772 /* Walk backwards until we're out of the I/O range... */
773 while (got.br_startoff + got.br_blockcount > offset_fsb) {
774 del = got;
775 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
776
777 /* Extent delete may have bumped idx forward */
778 if (!del.br_blockcount) {
779 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700780 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700781 }
782
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100783 ASSERT(!isnullstartblock(got.br_startblock));
784
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800785 /*
786 * Don't remap unwritten extents; these are
787 * speculatively preallocated CoW extents that have been
788 * allocated but have not yet been involved in a write.
789 */
790 if (got.br_state == XFS_EXT_UNWRITTEN) {
791 idx--;
792 goto next_extent;
793 }
794
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100795 /* Unmap the old blocks in the data fork. */
796 xfs_defer_init(&dfops, &firstfsb);
797 rlen = del.br_blockcount;
798 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
799 &firstfsb, &dfops);
800 if (error)
801 goto out_defer;
802
803 /* Trim the extent to whatever got unmapped. */
804 if (rlen) {
805 xfs_trim_extent(&del, del.br_startoff + rlen,
806 del.br_blockcount - rlen);
807 }
808 trace_xfs_reflink_cow_remap(ip, &del);
809
810 /* Free the CoW orphan record. */
811 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
812 del.br_startblock, del.br_blockcount);
813 if (error)
814 goto out_defer;
815
816 /* Map the new blocks into the data fork. */
817 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
818 if (error)
819 goto out_defer;
820
821 /* Remove the mapping from the CoW fork. */
822 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
823
824 error = xfs_defer_finish(&tp, &dfops, ip);
825 if (error)
826 goto out_defer;
827
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700828next_extent:
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100829 if (idx < 0)
830 break;
831 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700832 }
833
834 error = xfs_trans_commit(tp);
835 xfs_iunlock(ip, XFS_ILOCK_EXCL);
836 if (error)
837 goto out;
838 return 0;
839
840out_defer:
841 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700842 xfs_trans_cancel(tp);
843 xfs_iunlock(ip, XFS_ILOCK_EXCL);
844out:
845 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
846 return error;
847}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700848
849/*
850 * Free leftover CoW reservations that didn't get cleaned out.
851 */
852int
853xfs_reflink_recover_cow(
854 struct xfs_mount *mp)
855{
856 xfs_agnumber_t agno;
857 int error = 0;
858
859 if (!xfs_sb_version_hasreflink(&mp->m_sb))
860 return 0;
861
862 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
863 error = xfs_refcount_recover_cow_leftovers(mp, agno);
864 if (error)
865 break;
866 }
867
868 return error;
869}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700870
871/*
872 * Reflinking (Block) Ranges of Two Files Together
873 *
874 * First, ensure that the reflink flag is set on both inodes. The flag is an
875 * optimization to avoid unnecessary refcount btree lookups in the write path.
876 *
877 * Now we can iteratively remap the range of extents (and holes) in src to the
878 * corresponding ranges in dest. Let drange and srange denote the ranges of
879 * logical blocks in dest and src touched by the reflink operation.
880 *
881 * While the length of drange is greater than zero,
882 * - Read src's bmbt at the start of srange ("imap")
883 * - If imap doesn't exist, make imap appear to start at the end of srange
884 * with zero length.
885 * - If imap starts before srange, advance imap to start at srange.
886 * - If imap goes beyond srange, truncate imap to end at the end of srange.
887 * - Punch (imap start - srange start + imap len) blocks from dest at
888 * offset (drange start).
889 * - If imap points to a real range of pblks,
890 * > Increase the refcount of the imap's pblks
891 * > Map imap's pblks into dest at the offset
892 * (drange start + imap start - srange start)
893 * - Advance drange and srange by (imap start - srange start + imap len)
894 *
895 * Finally, if the reflink made dest longer, update both the in-core and
896 * on-disk file sizes.
897 *
898 * ASCII Art Demonstration:
899 *
900 * Let's say we want to reflink this source file:
901 *
902 * ----SSSSSSS-SSSSS----SSSSSS (src file)
903 * <-------------------->
904 *
905 * into this destination file:
906 *
907 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
908 * <-------------------->
909 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
910 * Observe that the range has different logical offsets in either file.
911 *
912 * Consider that the first extent in the source file doesn't line up with our
913 * reflink range. Unmapping and remapping are separate operations, so we can
914 * unmap more blocks from the destination file than we remap.
915 *
916 * ----SSSSSSS-SSSSS----SSSSSS
917 * <------->
918 * --DDDDD---------DDDDD--DDD
919 * <------->
920 *
921 * Now remap the source extent into the destination file:
922 *
923 * ----SSSSSSS-SSSSS----SSSSSS
924 * <------->
925 * --DDDDD--SSSSSSSDDDDD--DDD
926 * <------->
927 *
928 * Do likewise with the second hole and extent in our range. Holes in the
929 * unmap range don't affect our operation.
930 *
931 * ----SSSSSSS-SSSSS----SSSSSS
932 * <---->
933 * --DDDDD--SSSSSSS-SSSSS-DDD
934 * <---->
935 *
936 * Finally, unmap and remap part of the third extent. This will increase the
937 * size of the destination file.
938 *
939 * ----SSSSSSS-SSSSS----SSSSSS
940 * <----->
941 * --DDDDD--SSSSSSS-SSSSS----SSS
942 * <----->
943 *
944 * Once we update the destination file's i_size, we're done.
945 */
946
947/*
948 * Ensure the reflink bit is set in both inodes.
949 */
950STATIC int
951xfs_reflink_set_inode_flag(
952 struct xfs_inode *src,
953 struct xfs_inode *dest)
954{
955 struct xfs_mount *mp = src->i_mount;
956 int error;
957 struct xfs_trans *tp;
958
959 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
960 return 0;
961
962 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
963 if (error)
964 goto out_error;
965
966 /* Lock both files against IO */
967 if (src->i_ino == dest->i_ino)
968 xfs_ilock(src, XFS_ILOCK_EXCL);
969 else
970 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
971
972 if (!xfs_is_reflink_inode(src)) {
973 trace_xfs_reflink_set_inode_flag(src);
974 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
975 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
976 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
977 xfs_ifork_init_cow(src);
978 } else
979 xfs_iunlock(src, XFS_ILOCK_EXCL);
980
981 if (src->i_ino == dest->i_ino)
982 goto commit_flags;
983
984 if (!xfs_is_reflink_inode(dest)) {
985 trace_xfs_reflink_set_inode_flag(dest);
986 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
987 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
988 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
989 xfs_ifork_init_cow(dest);
990 } else
991 xfs_iunlock(dest, XFS_ILOCK_EXCL);
992
993commit_flags:
994 error = xfs_trans_commit(tp);
995 if (error)
996 goto out_error;
997 return error;
998
999out_error:
1000 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
1001 return error;
1002}
1003
1004/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001005 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -07001006 */
1007STATIC int
1008xfs_reflink_update_dest(
1009 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001010 xfs_off_t newlen,
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001011 xfs_extlen_t cowextsize,
1012 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001013{
1014 struct xfs_mount *mp = dest->i_mount;
1015 struct xfs_trans *tp;
1016 int error;
1017
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001018 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001019 return 0;
1020
1021 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1022 if (error)
1023 goto out_error;
1024
1025 xfs_ilock(dest, XFS_ILOCK_EXCL);
1026 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1027
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001028 if (newlen > i_size_read(VFS_I(dest))) {
1029 trace_xfs_reflink_update_inode_size(dest, newlen);
1030 i_size_write(VFS_I(dest), newlen);
1031 dest->i_d.di_size = newlen;
1032 }
1033
1034 if (cowextsize) {
1035 dest->i_d.di_cowextsize = cowextsize;
1036 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1037 }
1038
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001039 if (!is_dedupe) {
1040 xfs_trans_ichgtime(tp, dest,
1041 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1042 }
Darrick J. Wong862bb362016-10-03 09:11:40 -07001043 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1044
1045 error = xfs_trans_commit(tp);
1046 if (error)
1047 goto out_error;
1048 return error;
1049
1050out_error:
1051 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1052 return error;
1053}
1054
1055/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001056 * Do we have enough reserve in this AG to handle a reflink? The refcount
1057 * btree already reserved all the space it needs, but the rmap btree can grow
1058 * infinitely, so we won't allow more reflinks when the AG is down to the
1059 * btree reserves.
1060 */
1061static int
1062xfs_reflink_ag_has_free_space(
1063 struct xfs_mount *mp,
1064 xfs_agnumber_t agno)
1065{
1066 struct xfs_perag *pag;
1067 int error = 0;
1068
1069 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1070 return 0;
1071
1072 pag = xfs_perag_get(mp, agno);
1073 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1074 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1075 error = -ENOSPC;
1076 xfs_perag_put(pag);
1077 return error;
1078}
1079
1080/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001081 * Unmap a range of blocks from a file, then map other blocks into the hole.
1082 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1083 * The extent irec is mapped into dest at irec->br_startoff.
1084 */
1085STATIC int
1086xfs_reflink_remap_extent(
1087 struct xfs_inode *ip,
1088 struct xfs_bmbt_irec *irec,
1089 xfs_fileoff_t destoff,
1090 xfs_off_t new_isize)
1091{
1092 struct xfs_mount *mp = ip->i_mount;
1093 struct xfs_trans *tp;
1094 xfs_fsblock_t firstfsb;
1095 unsigned int resblks;
1096 struct xfs_defer_ops dfops;
1097 struct xfs_bmbt_irec uirec;
1098 bool real_extent;
1099 xfs_filblks_t rlen;
1100 xfs_filblks_t unmap_len;
1101 xfs_off_t newlen;
1102 int error;
1103
1104 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1105 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1106
1107 /* Only remap normal extents. */
1108 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1109 irec->br_startblock != DELAYSTARTBLOCK &&
1110 !ISUNWRITTEN(irec));
1111
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001112 /* No reflinking if we're low on space */
1113 if (real_extent) {
1114 error = xfs_reflink_ag_has_free_space(mp,
1115 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1116 if (error)
1117 goto out;
1118 }
1119
Darrick J. Wong862bb362016-10-03 09:11:40 -07001120 /* Start a rolling transaction to switch the mappings */
1121 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1122 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1123 if (error)
1124 goto out;
1125
1126 xfs_ilock(ip, XFS_ILOCK_EXCL);
1127 xfs_trans_ijoin(tp, ip, 0);
1128
1129 /* If we're not just clearing space, then do we have enough quota? */
1130 if (real_extent) {
1131 error = xfs_trans_reserve_quota_nblks(tp, ip,
1132 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1133 if (error)
1134 goto out_cancel;
1135 }
1136
1137 trace_xfs_reflink_remap(ip, irec->br_startoff,
1138 irec->br_blockcount, irec->br_startblock);
1139
1140 /* Unmap the old blocks in the data fork. */
1141 rlen = unmap_len;
1142 while (rlen) {
1143 xfs_defer_init(&dfops, &firstfsb);
1144 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1145 &firstfsb, &dfops);
1146 if (error)
1147 goto out_defer;
1148
1149 /*
1150 * Trim the extent to whatever got unmapped.
1151 * Remember, bunmapi works backwards.
1152 */
1153 uirec.br_startblock = irec->br_startblock + rlen;
1154 uirec.br_startoff = irec->br_startoff + rlen;
1155 uirec.br_blockcount = unmap_len - rlen;
1156 unmap_len = rlen;
1157
1158 /* If this isn't a real mapping, we're done. */
1159 if (!real_extent || uirec.br_blockcount == 0)
1160 goto next_extent;
1161
1162 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1163 uirec.br_blockcount, uirec.br_startblock);
1164
1165 /* Update the refcount tree */
1166 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1167 if (error)
1168 goto out_defer;
1169
1170 /* Map the new blocks into the data fork. */
1171 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1172 if (error)
1173 goto out_defer;
1174
1175 /* Update quota accounting. */
1176 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1177 uirec.br_blockcount);
1178
1179 /* Update dest isize if needed. */
1180 newlen = XFS_FSB_TO_B(mp,
1181 uirec.br_startoff + uirec.br_blockcount);
1182 newlen = min_t(xfs_off_t, newlen, new_isize);
1183 if (newlen > i_size_read(VFS_I(ip))) {
1184 trace_xfs_reflink_update_inode_size(ip, newlen);
1185 i_size_write(VFS_I(ip), newlen);
1186 ip->i_d.di_size = newlen;
1187 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1188 }
1189
1190next_extent:
1191 /* Process all the deferred stuff. */
1192 error = xfs_defer_finish(&tp, &dfops, ip);
1193 if (error)
1194 goto out_defer;
1195 }
1196
1197 error = xfs_trans_commit(tp);
1198 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1199 if (error)
1200 goto out;
1201 return 0;
1202
1203out_defer:
1204 xfs_defer_cancel(&dfops);
1205out_cancel:
1206 xfs_trans_cancel(tp);
1207 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1208out:
1209 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1210 return error;
1211}
1212
1213/*
1214 * Iteratively remap one file's extents (and holes) to another's.
1215 */
1216STATIC int
1217xfs_reflink_remap_blocks(
1218 struct xfs_inode *src,
1219 xfs_fileoff_t srcoff,
1220 struct xfs_inode *dest,
1221 xfs_fileoff_t destoff,
1222 xfs_filblks_t len,
1223 xfs_off_t new_isize)
1224{
1225 struct xfs_bmbt_irec imap;
1226 int nimaps;
1227 int error = 0;
1228 xfs_filblks_t range_len;
1229
1230 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1231 while (len) {
1232 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1233 dest, destoff);
1234 /* Read extent from the source file */
1235 nimaps = 1;
1236 xfs_ilock(src, XFS_ILOCK_EXCL);
1237 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1238 xfs_iunlock(src, XFS_ILOCK_EXCL);
1239 if (error)
1240 goto err;
1241 ASSERT(nimaps == 1);
1242
1243 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1244 &imap);
1245
1246 /* Translate imap into the destination file. */
1247 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1248 imap.br_startoff += destoff - srcoff;
1249
1250 /* Clear dest from destoff to the end of imap and map it in. */
1251 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1252 new_isize);
1253 if (error)
1254 goto err;
1255
1256 if (fatal_signal_pending(current)) {
1257 error = -EINTR;
1258 goto err;
1259 }
1260
1261 /* Advance drange/srange */
1262 srcoff += range_len;
1263 destoff += range_len;
1264 len -= range_len;
1265 }
1266
1267 return 0;
1268
1269err:
1270 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1271 return error;
1272}
1273
1274/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001275 * Read a page's worth of file data into the page cache. Return the page
1276 * locked.
1277 */
1278static struct page *
1279xfs_get_page(
1280 struct inode *inode,
1281 xfs_off_t offset)
1282{
1283 struct address_space *mapping;
1284 struct page *page;
1285 pgoff_t n;
1286
1287 n = offset >> PAGE_SHIFT;
1288 mapping = inode->i_mapping;
1289 page = read_mapping_page(mapping, n, NULL);
1290 if (IS_ERR(page))
1291 return page;
1292 if (!PageUptodate(page)) {
1293 put_page(page);
1294 return ERR_PTR(-EIO);
1295 }
1296 lock_page(page);
1297 return page;
1298}
1299
1300/*
1301 * Compare extents of two files to see if they are the same.
1302 */
1303static int
1304xfs_compare_extents(
1305 struct inode *src,
1306 xfs_off_t srcoff,
1307 struct inode *dest,
1308 xfs_off_t destoff,
1309 xfs_off_t len,
1310 bool *is_same)
1311{
1312 xfs_off_t src_poff;
1313 xfs_off_t dest_poff;
1314 void *src_addr;
1315 void *dest_addr;
1316 struct page *src_page;
1317 struct page *dest_page;
1318 xfs_off_t cmp_len;
1319 bool same;
1320 int error;
1321
1322 error = -EINVAL;
1323 same = true;
1324 while (len) {
1325 src_poff = srcoff & (PAGE_SIZE - 1);
1326 dest_poff = destoff & (PAGE_SIZE - 1);
1327 cmp_len = min(PAGE_SIZE - src_poff,
1328 PAGE_SIZE - dest_poff);
1329 cmp_len = min(cmp_len, len);
1330 ASSERT(cmp_len > 0);
1331
1332 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1333 XFS_I(dest), destoff);
1334
1335 src_page = xfs_get_page(src, srcoff);
1336 if (IS_ERR(src_page)) {
1337 error = PTR_ERR(src_page);
1338 goto out_error;
1339 }
1340 dest_page = xfs_get_page(dest, destoff);
1341 if (IS_ERR(dest_page)) {
1342 error = PTR_ERR(dest_page);
1343 unlock_page(src_page);
1344 put_page(src_page);
1345 goto out_error;
1346 }
1347 src_addr = kmap_atomic(src_page);
1348 dest_addr = kmap_atomic(dest_page);
1349
1350 flush_dcache_page(src_page);
1351 flush_dcache_page(dest_page);
1352
1353 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1354 same = false;
1355
1356 kunmap_atomic(dest_addr);
1357 kunmap_atomic(src_addr);
1358 unlock_page(dest_page);
1359 unlock_page(src_page);
1360 put_page(dest_page);
1361 put_page(src_page);
1362
1363 if (!same)
1364 break;
1365
1366 srcoff += cmp_len;
1367 destoff += cmp_len;
1368 len -= cmp_len;
1369 }
1370
1371 *is_same = same;
1372 return 0;
1373
1374out_error:
1375 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1376 return error;
1377}
1378
1379/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001380 * Link a range of blocks from one file to another.
1381 */
1382int
1383xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001384 struct file *file_in,
1385 loff_t pos_in,
1386 struct file *file_out,
1387 loff_t pos_out,
1388 u64 len,
1389 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001390{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001391 struct inode *inode_in = file_inode(file_in);
1392 struct xfs_inode *src = XFS_I(inode_in);
1393 struct inode *inode_out = file_inode(file_out);
1394 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001395 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001396 loff_t bs = inode_out->i_sb->s_blocksize;
1397 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001398 xfs_fileoff_t sfsbno, dfsbno;
1399 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001400 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001401 loff_t isize;
1402 ssize_t ret;
1403 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001404
1405 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1406 return -EOPNOTSUPP;
1407
1408 if (XFS_FORCED_SHUTDOWN(mp))
1409 return -EIO;
1410
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001411 /* Lock both files against IO */
1412 if (same_inode) {
1413 xfs_ilock(src, XFS_IOLOCK_EXCL);
1414 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1415 } else {
1416 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1417 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1418 }
1419
1420 /* Don't touch certain kinds of inodes */
1421 ret = -EPERM;
1422 if (IS_IMMUTABLE(inode_out))
1423 goto out_unlock;
1424
1425 ret = -ETXTBSY;
1426 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1427 goto out_unlock;
1428
1429
1430 /* Don't reflink dirs, pipes, sockets... */
1431 ret = -EISDIR;
1432 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1433 goto out_unlock;
1434 ret = -EINVAL;
1435 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1436 goto out_unlock;
1437 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1438 goto out_unlock;
1439
Darrick J. Wong862bb362016-10-03 09:11:40 -07001440 /* Don't reflink realtime inodes */
1441 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001442 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001443
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001444 /* Don't share DAX file data for now. */
1445 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1446 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001447
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001448 /* Are we going all the way to the end? */
1449 isize = i_size_read(inode_in);
1450 if (isize == 0) {
1451 ret = 0;
1452 goto out_unlock;
1453 }
1454
Darrick J. Wong39032572017-01-09 16:38:41 +01001455 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1456 if (len == 0) {
1457 if (is_dedupe) {
1458 ret = 0;
1459 goto out_unlock;
1460 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001461 len = isize - pos_in;
Darrick J. Wong39032572017-01-09 16:38:41 +01001462 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001463
1464 /* Ensure offsets don't wrap and the input is inside i_size */
1465 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1466 pos_in + len > isize)
1467 goto out_unlock;
1468
1469 /* Don't allow dedupe past EOF in the dest file */
1470 if (is_dedupe) {
1471 loff_t disize;
1472
1473 disize = i_size_read(inode_out);
1474 if (pos_out >= disize || pos_out + len > disize)
1475 goto out_unlock;
1476 }
1477
1478 /* If we're linking to EOF, continue to the block boundary. */
1479 if (pos_in + len == isize)
1480 blen = ALIGN(isize, bs) - pos_in;
1481 else
1482 blen = len;
1483
1484 /* Only reflink if we're aligned to block boundaries */
1485 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1486 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1487 goto out_unlock;
1488
1489 /* Don't allow overlapped reflink within the same file */
1490 if (same_inode) {
1491 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1492 goto out_unlock;
1493 }
1494
1495 /* Wait for the completion of any pending IOs on both files */
1496 inode_dio_wait(inode_in);
1497 if (!same_inode)
1498 inode_dio_wait(inode_out);
1499
1500 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1501 pos_in, pos_in + len - 1);
1502 if (ret)
1503 goto out_unlock;
1504
1505 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1506 pos_out, pos_out + len - 1);
1507 if (ret)
1508 goto out_unlock;
1509
1510 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001511
Darrick J. Wongcc714662016-10-03 09:11:41 -07001512 /*
1513 * Check that the extents are the same.
1514 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001515 if (is_dedupe) {
1516 bool is_same = false;
1517
1518 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1519 len, &is_same);
1520 if (ret)
1521 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001522 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001523 ret = -EBADE;
1524 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001525 }
1526 }
1527
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001528 ret = xfs_reflink_set_inode_flag(src, dest);
1529 if (ret)
1530 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001531
1532 /*
1533 * Invalidate the page cache so that we can clear any CoW mappings
1534 * in the destination file.
1535 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001536 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1537 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001538
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001539 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1540 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001541 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001542 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1543 pos_out + len);
1544 if (ret)
1545 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001546
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001547 /*
1548 * Carry the cowextsize hint from src to dest if we're sharing the
1549 * entire source file to the entire destination file, the source file
1550 * has a cowextsize hint, and the destination file does not.
1551 */
1552 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001553 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001554 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001555 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001556 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1557 cowextsize = src->i_d.di_cowextsize;
1558
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001559 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1560 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001561
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001562out_unlock:
1563 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1564 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1565 if (src->i_ino != dest->i_ino) {
1566 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1567 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1568 }
1569 if (ret)
1570 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1571 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001572}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001573
1574/*
1575 * The user wants to preemptively CoW all shared blocks in this file,
1576 * which enables us to turn off the reflink flag. Iterate all
1577 * extents which are not prealloc/delalloc to see which ranges are
1578 * mentioned in the refcount tree, then read those blocks into the
1579 * pagecache, dirty them, fsync them back out, and then we can update
1580 * the inode flag. What happens if we run out of memory? :)
1581 */
1582STATIC int
1583xfs_reflink_dirty_extents(
1584 struct xfs_inode *ip,
1585 xfs_fileoff_t fbno,
1586 xfs_filblks_t end,
1587 xfs_off_t isize)
1588{
1589 struct xfs_mount *mp = ip->i_mount;
1590 xfs_agnumber_t agno;
1591 xfs_agblock_t agbno;
1592 xfs_extlen_t aglen;
1593 xfs_agblock_t rbno;
1594 xfs_extlen_t rlen;
1595 xfs_off_t fpos;
1596 xfs_off_t flen;
1597 struct xfs_bmbt_irec map[2];
1598 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001599 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001600
1601 while (end - fbno > 0) {
1602 nmaps = 1;
1603 /*
1604 * Look for extents in the file. Skip holes, delalloc, or
1605 * unwritten extents; they can't be reflinked.
1606 */
1607 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1608 if (error)
1609 goto out;
1610 if (nmaps == 0)
1611 break;
1612 if (map[0].br_startblock == HOLESTARTBLOCK ||
1613 map[0].br_startblock == DELAYSTARTBLOCK ||
1614 ISUNWRITTEN(&map[0]))
1615 goto next;
1616
1617 map[1] = map[0];
1618 while (map[1].br_blockcount) {
1619 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1620 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1621 aglen = map[1].br_blockcount;
1622
1623 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1624 &rbno, &rlen, true);
1625 if (error)
1626 goto out;
1627 if (rbno == NULLAGBLOCK)
1628 break;
1629
1630 /* Dirty the pages */
1631 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1632 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1633 (rbno - agbno));
1634 flen = XFS_FSB_TO_B(mp, rlen);
1635 if (fpos + flen > isize)
1636 flen = isize - fpos;
1637 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1638 &xfs_iomap_ops);
1639 xfs_ilock(ip, XFS_ILOCK_EXCL);
1640 if (error)
1641 goto out;
1642
1643 map[1].br_blockcount -= (rbno - agbno + rlen);
1644 map[1].br_startoff += (rbno - agbno + rlen);
1645 map[1].br_startblock += (rbno - agbno + rlen);
1646 }
1647
1648next:
1649 fbno = map[0].br_startoff + map[0].br_blockcount;
1650 }
1651out:
1652 return error;
1653}
1654
1655/* Clear the inode reflink flag if there are no shared extents. */
1656int
1657xfs_reflink_clear_inode_flag(
1658 struct xfs_inode *ip,
1659 struct xfs_trans **tpp)
1660{
1661 struct xfs_mount *mp = ip->i_mount;
1662 xfs_fileoff_t fbno;
1663 xfs_filblks_t end;
1664 xfs_agnumber_t agno;
1665 xfs_agblock_t agbno;
1666 xfs_extlen_t aglen;
1667 xfs_agblock_t rbno;
1668 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001669 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001670 int nmaps;
1671 int error = 0;
1672
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001673 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001674
1675 fbno = 0;
1676 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1677 while (end - fbno > 0) {
1678 nmaps = 1;
1679 /*
1680 * Look for extents in the file. Skip holes, delalloc, or
1681 * unwritten extents; they can't be reflinked.
1682 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001683 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001684 if (error)
1685 return error;
1686 if (nmaps == 0)
1687 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001688 if (map.br_startblock == HOLESTARTBLOCK ||
1689 map.br_startblock == DELAYSTARTBLOCK ||
1690 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001691 goto next;
1692
Darrick J. Wong024adf42016-10-10 16:47:40 +11001693 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1694 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1695 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001696
Darrick J. Wong024adf42016-10-10 16:47:40 +11001697 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1698 &rbno, &rlen, false);
1699 if (error)
1700 return error;
1701 /* Is there still a shared block here? */
1702 if (rbno != NULLAGBLOCK)
1703 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001704next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001705 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001706 }
1707
1708 /*
1709 * We didn't find any shared blocks so turn off the reflink flag.
1710 * First, get rid of any leftover CoW mappings.
1711 */
Christoph Hellwig3b83a022017-03-07 16:45:58 -08001712 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001713 if (error)
1714 return error;
1715
1716 /* Clear the inode flag. */
1717 trace_xfs_reflink_unset_inode_flag(ip);
1718 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001719 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001720 xfs_trans_ijoin(*tpp, ip, 0);
1721 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1722
1723 return error;
1724}
1725
1726/*
1727 * Clear the inode reflink flag if there are no shared extents and the size
1728 * hasn't changed.
1729 */
1730STATIC int
1731xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001732 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001733{
1734 struct xfs_mount *mp = ip->i_mount;
1735 struct xfs_trans *tp;
1736 int error = 0;
1737
1738 /* Start a rolling transaction to remove the mappings */
1739 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1740 if (error)
1741 return error;
1742
1743 xfs_ilock(ip, XFS_ILOCK_EXCL);
1744 xfs_trans_ijoin(tp, ip, 0);
1745
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001746 error = xfs_reflink_clear_inode_flag(ip, &tp);
1747 if (error)
1748 goto cancel;
1749
1750 error = xfs_trans_commit(tp);
1751 if (error)
1752 goto out;
1753
1754 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1755 return 0;
1756cancel:
1757 xfs_trans_cancel(tp);
1758out:
1759 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1760 return error;
1761}
1762
1763/*
1764 * Pre-COW all shared blocks within a given byte range of a file and turn off
1765 * the reflink flag if we unshare all of the file's blocks.
1766 */
1767int
1768xfs_reflink_unshare(
1769 struct xfs_inode *ip,
1770 xfs_off_t offset,
1771 xfs_off_t len)
1772{
1773 struct xfs_mount *mp = ip->i_mount;
1774 xfs_fileoff_t fbno;
1775 xfs_filblks_t end;
1776 xfs_off_t isize;
1777 int error;
1778
1779 if (!xfs_is_reflink_inode(ip))
1780 return 0;
1781
1782 trace_xfs_reflink_unshare(ip, offset, len);
1783
1784 inode_dio_wait(VFS_I(ip));
1785
1786 /* Try to CoW the selected ranges */
1787 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001788 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001789 isize = i_size_read(VFS_I(ip));
1790 end = XFS_B_TO_FSB(mp, offset + len);
1791 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1792 if (error)
1793 goto out_unlock;
1794 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1795
1796 /* Wait for the IO to finish */
1797 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1798 if (error)
1799 goto out;
1800
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001801 /* Turn off the reflink flag if possible. */
1802 error = xfs_reflink_try_clear_inode_flag(ip);
1803 if (error)
1804 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001805
1806 return 0;
1807
1808out_unlock:
1809 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1810out:
1811 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1812 return error;
1813}