blob: 2252f163c38f8e16789838030b513c22b70a0c89 [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
739 /* Start a rolling transaction to switch the mappings */
740 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
741 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
742 resblks, 0, 0, &tp);
743 if (error)
744 goto out;
745
746 xfs_ilock(ip, XFS_ILOCK_EXCL);
747 xfs_trans_ijoin(tp, ip, 0);
748
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100749 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
750 &got, &prev);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700751
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100752 /* If there is a hole at end_fsb - 1 go to the previous extent */
753 if (eof || got.br_startoff > end_fsb) {
754 ASSERT(idx > 0);
755 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
756 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700757
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100758 /* Walk backwards until we're out of the I/O range... */
759 while (got.br_startoff + got.br_blockcount > offset_fsb) {
760 del = got;
761 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
762
763 /* Extent delete may have bumped idx forward */
764 if (!del.br_blockcount) {
765 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700766 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700767 }
768
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100769 ASSERT(!isnullstartblock(got.br_startblock));
770
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800771 /*
772 * Don't remap unwritten extents; these are
773 * speculatively preallocated CoW extents that have been
774 * allocated but have not yet been involved in a write.
775 */
776 if (got.br_state == XFS_EXT_UNWRITTEN) {
777 idx--;
778 goto next_extent;
779 }
780
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100781 /* Unmap the old blocks in the data fork. */
782 xfs_defer_init(&dfops, &firstfsb);
783 rlen = del.br_blockcount;
784 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
785 &firstfsb, &dfops);
786 if (error)
787 goto out_defer;
788
789 /* Trim the extent to whatever got unmapped. */
790 if (rlen) {
791 xfs_trim_extent(&del, del.br_startoff + rlen,
792 del.br_blockcount - rlen);
793 }
794 trace_xfs_reflink_cow_remap(ip, &del);
795
796 /* Free the CoW orphan record. */
797 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
798 del.br_startblock, del.br_blockcount);
799 if (error)
800 goto out_defer;
801
802 /* Map the new blocks into the data fork. */
803 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
804 if (error)
805 goto out_defer;
806
807 /* Remove the mapping from the CoW fork. */
808 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
809
810 error = xfs_defer_finish(&tp, &dfops, ip);
811 if (error)
812 goto out_defer;
813
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700814next_extent:
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100815 if (idx < 0)
816 break;
817 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700818 }
819
820 error = xfs_trans_commit(tp);
821 xfs_iunlock(ip, XFS_ILOCK_EXCL);
822 if (error)
823 goto out;
824 return 0;
825
826out_defer:
827 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700828 xfs_trans_cancel(tp);
829 xfs_iunlock(ip, XFS_ILOCK_EXCL);
830out:
831 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
832 return error;
833}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700834
835/*
836 * Free leftover CoW reservations that didn't get cleaned out.
837 */
838int
839xfs_reflink_recover_cow(
840 struct xfs_mount *mp)
841{
842 xfs_agnumber_t agno;
843 int error = 0;
844
845 if (!xfs_sb_version_hasreflink(&mp->m_sb))
846 return 0;
847
848 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
849 error = xfs_refcount_recover_cow_leftovers(mp, agno);
850 if (error)
851 break;
852 }
853
854 return error;
855}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700856
857/*
858 * Reflinking (Block) Ranges of Two Files Together
859 *
860 * First, ensure that the reflink flag is set on both inodes. The flag is an
861 * optimization to avoid unnecessary refcount btree lookups in the write path.
862 *
863 * Now we can iteratively remap the range of extents (and holes) in src to the
864 * corresponding ranges in dest. Let drange and srange denote the ranges of
865 * logical blocks in dest and src touched by the reflink operation.
866 *
867 * While the length of drange is greater than zero,
868 * - Read src's bmbt at the start of srange ("imap")
869 * - If imap doesn't exist, make imap appear to start at the end of srange
870 * with zero length.
871 * - If imap starts before srange, advance imap to start at srange.
872 * - If imap goes beyond srange, truncate imap to end at the end of srange.
873 * - Punch (imap start - srange start + imap len) blocks from dest at
874 * offset (drange start).
875 * - If imap points to a real range of pblks,
876 * > Increase the refcount of the imap's pblks
877 * > Map imap's pblks into dest at the offset
878 * (drange start + imap start - srange start)
879 * - Advance drange and srange by (imap start - srange start + imap len)
880 *
881 * Finally, if the reflink made dest longer, update both the in-core and
882 * on-disk file sizes.
883 *
884 * ASCII Art Demonstration:
885 *
886 * Let's say we want to reflink this source file:
887 *
888 * ----SSSSSSS-SSSSS----SSSSSS (src file)
889 * <-------------------->
890 *
891 * into this destination file:
892 *
893 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
894 * <-------------------->
895 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
896 * Observe that the range has different logical offsets in either file.
897 *
898 * Consider that the first extent in the source file doesn't line up with our
899 * reflink range. Unmapping and remapping are separate operations, so we can
900 * unmap more blocks from the destination file than we remap.
901 *
902 * ----SSSSSSS-SSSSS----SSSSSS
903 * <------->
904 * --DDDDD---------DDDDD--DDD
905 * <------->
906 *
907 * Now remap the source extent into the destination file:
908 *
909 * ----SSSSSSS-SSSSS----SSSSSS
910 * <------->
911 * --DDDDD--SSSSSSSDDDDD--DDD
912 * <------->
913 *
914 * Do likewise with the second hole and extent in our range. Holes in the
915 * unmap range don't affect our operation.
916 *
917 * ----SSSSSSS-SSSSS----SSSSSS
918 * <---->
919 * --DDDDD--SSSSSSS-SSSSS-DDD
920 * <---->
921 *
922 * Finally, unmap and remap part of the third extent. This will increase the
923 * size of the destination file.
924 *
925 * ----SSSSSSS-SSSSS----SSSSSS
926 * <----->
927 * --DDDDD--SSSSSSS-SSSSS----SSS
928 * <----->
929 *
930 * Once we update the destination file's i_size, we're done.
931 */
932
933/*
934 * Ensure the reflink bit is set in both inodes.
935 */
936STATIC int
937xfs_reflink_set_inode_flag(
938 struct xfs_inode *src,
939 struct xfs_inode *dest)
940{
941 struct xfs_mount *mp = src->i_mount;
942 int error;
943 struct xfs_trans *tp;
944
945 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
946 return 0;
947
948 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
949 if (error)
950 goto out_error;
951
952 /* Lock both files against IO */
953 if (src->i_ino == dest->i_ino)
954 xfs_ilock(src, XFS_ILOCK_EXCL);
955 else
956 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
957
958 if (!xfs_is_reflink_inode(src)) {
959 trace_xfs_reflink_set_inode_flag(src);
960 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
961 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
962 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
963 xfs_ifork_init_cow(src);
964 } else
965 xfs_iunlock(src, XFS_ILOCK_EXCL);
966
967 if (src->i_ino == dest->i_ino)
968 goto commit_flags;
969
970 if (!xfs_is_reflink_inode(dest)) {
971 trace_xfs_reflink_set_inode_flag(dest);
972 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
973 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
974 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
975 xfs_ifork_init_cow(dest);
976 } else
977 xfs_iunlock(dest, XFS_ILOCK_EXCL);
978
979commit_flags:
980 error = xfs_trans_commit(tp);
981 if (error)
982 goto out_error;
983 return error;
984
985out_error:
986 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
987 return error;
988}
989
990/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700991 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700992 */
993STATIC int
994xfs_reflink_update_dest(
995 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700996 xfs_off_t newlen,
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -0800997 xfs_extlen_t cowextsize,
998 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700999{
1000 struct xfs_mount *mp = dest->i_mount;
1001 struct xfs_trans *tp;
1002 int error;
1003
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001004 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001005 return 0;
1006
1007 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1008 if (error)
1009 goto out_error;
1010
1011 xfs_ilock(dest, XFS_ILOCK_EXCL);
1012 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1013
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001014 if (newlen > i_size_read(VFS_I(dest))) {
1015 trace_xfs_reflink_update_inode_size(dest, newlen);
1016 i_size_write(VFS_I(dest), newlen);
1017 dest->i_d.di_size = newlen;
1018 }
1019
1020 if (cowextsize) {
1021 dest->i_d.di_cowextsize = cowextsize;
1022 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1023 }
1024
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001025 if (!is_dedupe) {
1026 xfs_trans_ichgtime(tp, dest,
1027 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1028 }
Darrick J. Wong862bb362016-10-03 09:11:40 -07001029 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1030
1031 error = xfs_trans_commit(tp);
1032 if (error)
1033 goto out_error;
1034 return error;
1035
1036out_error:
1037 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1038 return error;
1039}
1040
1041/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001042 * Do we have enough reserve in this AG to handle a reflink? The refcount
1043 * btree already reserved all the space it needs, but the rmap btree can grow
1044 * infinitely, so we won't allow more reflinks when the AG is down to the
1045 * btree reserves.
1046 */
1047static int
1048xfs_reflink_ag_has_free_space(
1049 struct xfs_mount *mp,
1050 xfs_agnumber_t agno)
1051{
1052 struct xfs_perag *pag;
1053 int error = 0;
1054
1055 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1056 return 0;
1057
1058 pag = xfs_perag_get(mp, agno);
1059 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1060 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1061 error = -ENOSPC;
1062 xfs_perag_put(pag);
1063 return error;
1064}
1065
1066/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001067 * Unmap a range of blocks from a file, then map other blocks into the hole.
1068 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1069 * The extent irec is mapped into dest at irec->br_startoff.
1070 */
1071STATIC int
1072xfs_reflink_remap_extent(
1073 struct xfs_inode *ip,
1074 struct xfs_bmbt_irec *irec,
1075 xfs_fileoff_t destoff,
1076 xfs_off_t new_isize)
1077{
1078 struct xfs_mount *mp = ip->i_mount;
1079 struct xfs_trans *tp;
1080 xfs_fsblock_t firstfsb;
1081 unsigned int resblks;
1082 struct xfs_defer_ops dfops;
1083 struct xfs_bmbt_irec uirec;
1084 bool real_extent;
1085 xfs_filblks_t rlen;
1086 xfs_filblks_t unmap_len;
1087 xfs_off_t newlen;
1088 int error;
1089
1090 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1091 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1092
1093 /* Only remap normal extents. */
1094 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1095 irec->br_startblock != DELAYSTARTBLOCK &&
1096 !ISUNWRITTEN(irec));
1097
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001098 /* No reflinking if we're low on space */
1099 if (real_extent) {
1100 error = xfs_reflink_ag_has_free_space(mp,
1101 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1102 if (error)
1103 goto out;
1104 }
1105
Darrick J. Wong862bb362016-10-03 09:11:40 -07001106 /* Start a rolling transaction to switch the mappings */
1107 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1108 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1109 if (error)
1110 goto out;
1111
1112 xfs_ilock(ip, XFS_ILOCK_EXCL);
1113 xfs_trans_ijoin(tp, ip, 0);
1114
1115 /* If we're not just clearing space, then do we have enough quota? */
1116 if (real_extent) {
1117 error = xfs_trans_reserve_quota_nblks(tp, ip,
1118 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1119 if (error)
1120 goto out_cancel;
1121 }
1122
1123 trace_xfs_reflink_remap(ip, irec->br_startoff,
1124 irec->br_blockcount, irec->br_startblock);
1125
1126 /* Unmap the old blocks in the data fork. */
1127 rlen = unmap_len;
1128 while (rlen) {
1129 xfs_defer_init(&dfops, &firstfsb);
1130 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1131 &firstfsb, &dfops);
1132 if (error)
1133 goto out_defer;
1134
1135 /*
1136 * Trim the extent to whatever got unmapped.
1137 * Remember, bunmapi works backwards.
1138 */
1139 uirec.br_startblock = irec->br_startblock + rlen;
1140 uirec.br_startoff = irec->br_startoff + rlen;
1141 uirec.br_blockcount = unmap_len - rlen;
1142 unmap_len = rlen;
1143
1144 /* If this isn't a real mapping, we're done. */
1145 if (!real_extent || uirec.br_blockcount == 0)
1146 goto next_extent;
1147
1148 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1149 uirec.br_blockcount, uirec.br_startblock);
1150
1151 /* Update the refcount tree */
1152 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1153 if (error)
1154 goto out_defer;
1155
1156 /* Map the new blocks into the data fork. */
1157 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1158 if (error)
1159 goto out_defer;
1160
1161 /* Update quota accounting. */
1162 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1163 uirec.br_blockcount);
1164
1165 /* Update dest isize if needed. */
1166 newlen = XFS_FSB_TO_B(mp,
1167 uirec.br_startoff + uirec.br_blockcount);
1168 newlen = min_t(xfs_off_t, newlen, new_isize);
1169 if (newlen > i_size_read(VFS_I(ip))) {
1170 trace_xfs_reflink_update_inode_size(ip, newlen);
1171 i_size_write(VFS_I(ip), newlen);
1172 ip->i_d.di_size = newlen;
1173 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1174 }
1175
1176next_extent:
1177 /* Process all the deferred stuff. */
1178 error = xfs_defer_finish(&tp, &dfops, ip);
1179 if (error)
1180 goto out_defer;
1181 }
1182
1183 error = xfs_trans_commit(tp);
1184 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1185 if (error)
1186 goto out;
1187 return 0;
1188
1189out_defer:
1190 xfs_defer_cancel(&dfops);
1191out_cancel:
1192 xfs_trans_cancel(tp);
1193 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1194out:
1195 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1196 return error;
1197}
1198
1199/*
1200 * Iteratively remap one file's extents (and holes) to another's.
1201 */
1202STATIC int
1203xfs_reflink_remap_blocks(
1204 struct xfs_inode *src,
1205 xfs_fileoff_t srcoff,
1206 struct xfs_inode *dest,
1207 xfs_fileoff_t destoff,
1208 xfs_filblks_t len,
1209 xfs_off_t new_isize)
1210{
1211 struct xfs_bmbt_irec imap;
1212 int nimaps;
1213 int error = 0;
1214 xfs_filblks_t range_len;
1215
1216 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1217 while (len) {
1218 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1219 dest, destoff);
1220 /* Read extent from the source file */
1221 nimaps = 1;
1222 xfs_ilock(src, XFS_ILOCK_EXCL);
1223 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1224 xfs_iunlock(src, XFS_ILOCK_EXCL);
1225 if (error)
1226 goto err;
1227 ASSERT(nimaps == 1);
1228
1229 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1230 &imap);
1231
1232 /* Translate imap into the destination file. */
1233 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1234 imap.br_startoff += destoff - srcoff;
1235
1236 /* Clear dest from destoff to the end of imap and map it in. */
1237 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1238 new_isize);
1239 if (error)
1240 goto err;
1241
1242 if (fatal_signal_pending(current)) {
1243 error = -EINTR;
1244 goto err;
1245 }
1246
1247 /* Advance drange/srange */
1248 srcoff += range_len;
1249 destoff += range_len;
1250 len -= range_len;
1251 }
1252
1253 return 0;
1254
1255err:
1256 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1257 return error;
1258}
1259
1260/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001261 * Read a page's worth of file data into the page cache. Return the page
1262 * locked.
1263 */
1264static struct page *
1265xfs_get_page(
1266 struct inode *inode,
1267 xfs_off_t offset)
1268{
1269 struct address_space *mapping;
1270 struct page *page;
1271 pgoff_t n;
1272
1273 n = offset >> PAGE_SHIFT;
1274 mapping = inode->i_mapping;
1275 page = read_mapping_page(mapping, n, NULL);
1276 if (IS_ERR(page))
1277 return page;
1278 if (!PageUptodate(page)) {
1279 put_page(page);
1280 return ERR_PTR(-EIO);
1281 }
1282 lock_page(page);
1283 return page;
1284}
1285
1286/*
1287 * Compare extents of two files to see if they are the same.
1288 */
1289static int
1290xfs_compare_extents(
1291 struct inode *src,
1292 xfs_off_t srcoff,
1293 struct inode *dest,
1294 xfs_off_t destoff,
1295 xfs_off_t len,
1296 bool *is_same)
1297{
1298 xfs_off_t src_poff;
1299 xfs_off_t dest_poff;
1300 void *src_addr;
1301 void *dest_addr;
1302 struct page *src_page;
1303 struct page *dest_page;
1304 xfs_off_t cmp_len;
1305 bool same;
1306 int error;
1307
1308 error = -EINVAL;
1309 same = true;
1310 while (len) {
1311 src_poff = srcoff & (PAGE_SIZE - 1);
1312 dest_poff = destoff & (PAGE_SIZE - 1);
1313 cmp_len = min(PAGE_SIZE - src_poff,
1314 PAGE_SIZE - dest_poff);
1315 cmp_len = min(cmp_len, len);
1316 ASSERT(cmp_len > 0);
1317
1318 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1319 XFS_I(dest), destoff);
1320
1321 src_page = xfs_get_page(src, srcoff);
1322 if (IS_ERR(src_page)) {
1323 error = PTR_ERR(src_page);
1324 goto out_error;
1325 }
1326 dest_page = xfs_get_page(dest, destoff);
1327 if (IS_ERR(dest_page)) {
1328 error = PTR_ERR(dest_page);
1329 unlock_page(src_page);
1330 put_page(src_page);
1331 goto out_error;
1332 }
1333 src_addr = kmap_atomic(src_page);
1334 dest_addr = kmap_atomic(dest_page);
1335
1336 flush_dcache_page(src_page);
1337 flush_dcache_page(dest_page);
1338
1339 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1340 same = false;
1341
1342 kunmap_atomic(dest_addr);
1343 kunmap_atomic(src_addr);
1344 unlock_page(dest_page);
1345 unlock_page(src_page);
1346 put_page(dest_page);
1347 put_page(src_page);
1348
1349 if (!same)
1350 break;
1351
1352 srcoff += cmp_len;
1353 destoff += cmp_len;
1354 len -= cmp_len;
1355 }
1356
1357 *is_same = same;
1358 return 0;
1359
1360out_error:
1361 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1362 return error;
1363}
1364
1365/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001366 * Link a range of blocks from one file to another.
1367 */
1368int
1369xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001370 struct file *file_in,
1371 loff_t pos_in,
1372 struct file *file_out,
1373 loff_t pos_out,
1374 u64 len,
1375 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001376{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001377 struct inode *inode_in = file_inode(file_in);
1378 struct xfs_inode *src = XFS_I(inode_in);
1379 struct inode *inode_out = file_inode(file_out);
1380 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001381 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001382 loff_t bs = inode_out->i_sb->s_blocksize;
1383 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001384 xfs_fileoff_t sfsbno, dfsbno;
1385 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001386 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001387 loff_t isize;
1388 ssize_t ret;
1389 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001390
1391 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1392 return -EOPNOTSUPP;
1393
1394 if (XFS_FORCED_SHUTDOWN(mp))
1395 return -EIO;
1396
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001397 /* Lock both files against IO */
1398 if (same_inode) {
1399 xfs_ilock(src, XFS_IOLOCK_EXCL);
1400 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1401 } else {
1402 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1403 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1404 }
1405
1406 /* Don't touch certain kinds of inodes */
1407 ret = -EPERM;
1408 if (IS_IMMUTABLE(inode_out))
1409 goto out_unlock;
1410
1411 ret = -ETXTBSY;
1412 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1413 goto out_unlock;
1414
1415
1416 /* Don't reflink dirs, pipes, sockets... */
1417 ret = -EISDIR;
1418 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1419 goto out_unlock;
1420 ret = -EINVAL;
1421 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1422 goto out_unlock;
1423 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1424 goto out_unlock;
1425
Darrick J. Wong862bb362016-10-03 09:11:40 -07001426 /* Don't reflink realtime inodes */
1427 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001428 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001429
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001430 /* Don't share DAX file data for now. */
1431 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1432 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001433
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001434 /* Are we going all the way to the end? */
1435 isize = i_size_read(inode_in);
1436 if (isize == 0) {
1437 ret = 0;
1438 goto out_unlock;
1439 }
1440
Darrick J. Wong39032572017-01-09 16:38:41 +01001441 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1442 if (len == 0) {
1443 if (is_dedupe) {
1444 ret = 0;
1445 goto out_unlock;
1446 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001447 len = isize - pos_in;
Darrick J. Wong39032572017-01-09 16:38:41 +01001448 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001449
1450 /* Ensure offsets don't wrap and the input is inside i_size */
1451 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1452 pos_in + len > isize)
1453 goto out_unlock;
1454
1455 /* Don't allow dedupe past EOF in the dest file */
1456 if (is_dedupe) {
1457 loff_t disize;
1458
1459 disize = i_size_read(inode_out);
1460 if (pos_out >= disize || pos_out + len > disize)
1461 goto out_unlock;
1462 }
1463
1464 /* If we're linking to EOF, continue to the block boundary. */
1465 if (pos_in + len == isize)
1466 blen = ALIGN(isize, bs) - pos_in;
1467 else
1468 blen = len;
1469
1470 /* Only reflink if we're aligned to block boundaries */
1471 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1472 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1473 goto out_unlock;
1474
1475 /* Don't allow overlapped reflink within the same file */
1476 if (same_inode) {
1477 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1478 goto out_unlock;
1479 }
1480
1481 /* Wait for the completion of any pending IOs on both files */
1482 inode_dio_wait(inode_in);
1483 if (!same_inode)
1484 inode_dio_wait(inode_out);
1485
1486 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1487 pos_in, pos_in + len - 1);
1488 if (ret)
1489 goto out_unlock;
1490
1491 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1492 pos_out, pos_out + len - 1);
1493 if (ret)
1494 goto out_unlock;
1495
1496 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001497
Darrick J. Wongcc714662016-10-03 09:11:41 -07001498 /*
1499 * Check that the extents are the same.
1500 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001501 if (is_dedupe) {
1502 bool is_same = false;
1503
1504 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1505 len, &is_same);
1506 if (ret)
1507 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001508 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001509 ret = -EBADE;
1510 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001511 }
1512 }
1513
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001514 ret = xfs_reflink_set_inode_flag(src, dest);
1515 if (ret)
1516 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001517
1518 /*
1519 * Invalidate the page cache so that we can clear any CoW mappings
1520 * in the destination file.
1521 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001522 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1523 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001524
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001525 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1526 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001527 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001528 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1529 pos_out + len);
1530 if (ret)
1531 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001532
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001533 /*
1534 * Carry the cowextsize hint from src to dest if we're sharing the
1535 * entire source file to the entire destination file, the source file
1536 * has a cowextsize hint, and the destination file does not.
1537 */
1538 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001539 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001540 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001541 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001542 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1543 cowextsize = src->i_d.di_cowextsize;
1544
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001545 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1546 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001547
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001548out_unlock:
1549 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1550 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1551 if (src->i_ino != dest->i_ino) {
1552 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1553 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1554 }
1555 if (ret)
1556 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1557 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001558}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001559
1560/*
1561 * The user wants to preemptively CoW all shared blocks in this file,
1562 * which enables us to turn off the reflink flag. Iterate all
1563 * extents which are not prealloc/delalloc to see which ranges are
1564 * mentioned in the refcount tree, then read those blocks into the
1565 * pagecache, dirty them, fsync them back out, and then we can update
1566 * the inode flag. What happens if we run out of memory? :)
1567 */
1568STATIC int
1569xfs_reflink_dirty_extents(
1570 struct xfs_inode *ip,
1571 xfs_fileoff_t fbno,
1572 xfs_filblks_t end,
1573 xfs_off_t isize)
1574{
1575 struct xfs_mount *mp = ip->i_mount;
1576 xfs_agnumber_t agno;
1577 xfs_agblock_t agbno;
1578 xfs_extlen_t aglen;
1579 xfs_agblock_t rbno;
1580 xfs_extlen_t rlen;
1581 xfs_off_t fpos;
1582 xfs_off_t flen;
1583 struct xfs_bmbt_irec map[2];
1584 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001585 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001586
1587 while (end - fbno > 0) {
1588 nmaps = 1;
1589 /*
1590 * Look for extents in the file. Skip holes, delalloc, or
1591 * unwritten extents; they can't be reflinked.
1592 */
1593 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1594 if (error)
1595 goto out;
1596 if (nmaps == 0)
1597 break;
1598 if (map[0].br_startblock == HOLESTARTBLOCK ||
1599 map[0].br_startblock == DELAYSTARTBLOCK ||
1600 ISUNWRITTEN(&map[0]))
1601 goto next;
1602
1603 map[1] = map[0];
1604 while (map[1].br_blockcount) {
1605 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1606 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1607 aglen = map[1].br_blockcount;
1608
1609 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1610 &rbno, &rlen, true);
1611 if (error)
1612 goto out;
1613 if (rbno == NULLAGBLOCK)
1614 break;
1615
1616 /* Dirty the pages */
1617 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1618 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1619 (rbno - agbno));
1620 flen = XFS_FSB_TO_B(mp, rlen);
1621 if (fpos + flen > isize)
1622 flen = isize - fpos;
1623 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1624 &xfs_iomap_ops);
1625 xfs_ilock(ip, XFS_ILOCK_EXCL);
1626 if (error)
1627 goto out;
1628
1629 map[1].br_blockcount -= (rbno - agbno + rlen);
1630 map[1].br_startoff += (rbno - agbno + rlen);
1631 map[1].br_startblock += (rbno - agbno + rlen);
1632 }
1633
1634next:
1635 fbno = map[0].br_startoff + map[0].br_blockcount;
1636 }
1637out:
1638 return error;
1639}
1640
1641/* Clear the inode reflink flag if there are no shared extents. */
1642int
1643xfs_reflink_clear_inode_flag(
1644 struct xfs_inode *ip,
1645 struct xfs_trans **tpp)
1646{
1647 struct xfs_mount *mp = ip->i_mount;
1648 xfs_fileoff_t fbno;
1649 xfs_filblks_t end;
1650 xfs_agnumber_t agno;
1651 xfs_agblock_t agbno;
1652 xfs_extlen_t aglen;
1653 xfs_agblock_t rbno;
1654 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001655 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001656 int nmaps;
1657 int error = 0;
1658
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001659 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001660
1661 fbno = 0;
1662 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1663 while (end - fbno > 0) {
1664 nmaps = 1;
1665 /*
1666 * Look for extents in the file. Skip holes, delalloc, or
1667 * unwritten extents; they can't be reflinked.
1668 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001669 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001670 if (error)
1671 return error;
1672 if (nmaps == 0)
1673 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001674 if (map.br_startblock == HOLESTARTBLOCK ||
1675 map.br_startblock == DELAYSTARTBLOCK ||
1676 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001677 goto next;
1678
Darrick J. Wong024adf42016-10-10 16:47:40 +11001679 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1680 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1681 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001682
Darrick J. Wong024adf42016-10-10 16:47:40 +11001683 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1684 &rbno, &rlen, false);
1685 if (error)
1686 return error;
1687 /* Is there still a shared block here? */
1688 if (rbno != NULLAGBLOCK)
1689 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001690next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001691 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001692 }
1693
1694 /*
1695 * We didn't find any shared blocks so turn off the reflink flag.
1696 * First, get rid of any leftover CoW mappings.
1697 */
Christoph Hellwig3b83a022017-03-07 16:45:58 -08001698 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001699 if (error)
1700 return error;
1701
1702 /* Clear the inode flag. */
1703 trace_xfs_reflink_unset_inode_flag(ip);
1704 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001705 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001706 xfs_trans_ijoin(*tpp, ip, 0);
1707 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1708
1709 return error;
1710}
1711
1712/*
1713 * Clear the inode reflink flag if there are no shared extents and the size
1714 * hasn't changed.
1715 */
1716STATIC int
1717xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001718 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001719{
1720 struct xfs_mount *mp = ip->i_mount;
1721 struct xfs_trans *tp;
1722 int error = 0;
1723
1724 /* Start a rolling transaction to remove the mappings */
1725 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1726 if (error)
1727 return error;
1728
1729 xfs_ilock(ip, XFS_ILOCK_EXCL);
1730 xfs_trans_ijoin(tp, ip, 0);
1731
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001732 error = xfs_reflink_clear_inode_flag(ip, &tp);
1733 if (error)
1734 goto cancel;
1735
1736 error = xfs_trans_commit(tp);
1737 if (error)
1738 goto out;
1739
1740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1741 return 0;
1742cancel:
1743 xfs_trans_cancel(tp);
1744out:
1745 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1746 return error;
1747}
1748
1749/*
1750 * Pre-COW all shared blocks within a given byte range of a file and turn off
1751 * the reflink flag if we unshare all of the file's blocks.
1752 */
1753int
1754xfs_reflink_unshare(
1755 struct xfs_inode *ip,
1756 xfs_off_t offset,
1757 xfs_off_t len)
1758{
1759 struct xfs_mount *mp = ip->i_mount;
1760 xfs_fileoff_t fbno;
1761 xfs_filblks_t end;
1762 xfs_off_t isize;
1763 int error;
1764
1765 if (!xfs_is_reflink_inode(ip))
1766 return 0;
1767
1768 trace_xfs_reflink_unshare(ip, offset, len);
1769
1770 inode_dio_wait(VFS_I(ip));
1771
1772 /* Try to CoW the selected ranges */
1773 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001774 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001775 isize = i_size_read(VFS_I(ip));
1776 end = XFS_B_TO_FSB(mp, offset + len);
1777 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1778 if (error)
1779 goto out_unlock;
1780 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1781
1782 /* Wait for the IO to finish */
1783 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1784 if (error)
1785 goto out;
1786
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001787 /* Turn off the reflink flag if possible. */
1788 error = xfs_reflink_try_clear_inode_flag(ip);
1789 if (error)
1790 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001791
1792 return 0;
1793
1794out_unlock:
1795 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1796out:
1797 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1798 return error;
1799}