blob: 36c07b12189e0b57a2941facd1c11aa8b1df2a5e [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;
366 int error;
367
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/*
574 * Cancel all pending CoW reservations for some block range of an inode.
575 */
576int
577xfs_reflink_cancel_cow_blocks(
578 struct xfs_inode *ip,
579 struct xfs_trans **tpp,
580 xfs_fileoff_t offset_fsb,
581 xfs_fileoff_t end_fsb)
582{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100583 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
584 struct xfs_bmbt_irec got, prev, del;
585 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700586 xfs_fsblock_t firstfsb;
587 struct xfs_defer_ops dfops;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100588 int error = 0, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700589
590 if (!xfs_is_reflink_inode(ip))
591 return 0;
592
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100593 xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
594 &got, &prev);
595 if (eof)
596 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700597
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100598 while (got.br_startoff < end_fsb) {
599 del = got;
600 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
601 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700602
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100603 if (isnullstartblock(del.br_startblock)) {
604 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
605 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700606 if (error)
607 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700608 } else {
609 xfs_trans_ijoin(*tpp, ip, 0);
610 xfs_defer_init(&dfops, &firstfsb);
611
Darrick J. Wong174edb02016-10-03 09:11:39 -0700612 /* Free the CoW orphan record. */
613 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100614 &dfops, del.br_startblock,
615 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700616 if (error)
617 break;
618
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700619 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100620 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700621 NULL);
622
623 /* Update quota accounting */
624 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100625 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700626
627 /* Roll the transaction */
628 error = xfs_defer_finish(tpp, &dfops, ip);
629 if (error) {
630 xfs_defer_cancel(&dfops);
631 break;
632 }
633
634 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100635 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700636 }
637
Eric Sandeenf380ee72017-01-09 16:38:36 +0100638 if (++idx >= xfs_iext_count(ifp))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100639 break;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100640 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700641 }
642
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100643 /* clear tag if cow fork is emptied */
644 if (!ifp->if_bytes)
645 xfs_inode_clear_cowblocks_tag(ip);
646
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700647 return error;
648}
649
650/*
651 * Cancel all pending CoW reservations for some byte range of an inode.
652 */
653int
654xfs_reflink_cancel_cow_range(
655 struct xfs_inode *ip,
656 xfs_off_t offset,
657 xfs_off_t count)
658{
659 struct xfs_trans *tp;
660 xfs_fileoff_t offset_fsb;
661 xfs_fileoff_t end_fsb;
662 int error;
663
664 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100665 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700666
667 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
668 if (count == NULLFILEOFF)
669 end_fsb = NULLFILEOFF;
670 else
671 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
672
673 /* Start a rolling transaction to remove the mappings */
674 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
675 0, 0, 0, &tp);
676 if (error)
677 goto out;
678
679 xfs_ilock(ip, XFS_ILOCK_EXCL);
680 xfs_trans_ijoin(tp, ip, 0);
681
682 /* Scrape out the old CoW reservations */
683 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
684 if (error)
685 goto out_cancel;
686
687 error = xfs_trans_commit(tp);
688
689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
690 return error;
691
692out_cancel:
693 xfs_trans_cancel(tp);
694 xfs_iunlock(ip, XFS_ILOCK_EXCL);
695out:
696 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
697 return error;
698}
699
700/*
701 * Remap parts of a file's data fork after a successful CoW.
702 */
703int
704xfs_reflink_end_cow(
705 struct xfs_inode *ip,
706 xfs_off_t offset,
707 xfs_off_t count)
708{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100709 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
710 struct xfs_bmbt_irec got, prev, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700711 struct xfs_trans *tp;
712 xfs_fileoff_t offset_fsb;
713 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700714 xfs_fsblock_t firstfsb;
715 struct xfs_defer_ops dfops;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100716 int error, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700717 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700718 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100719 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700720
721 trace_xfs_reflink_end_cow(ip, offset, count);
722
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100723 /* No COW extents? That's easy! */
724 if (ifp->if_bytes == 0)
725 return 0;
726
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700727 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
728 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700729
730 /* Start a rolling transaction to switch the mappings */
731 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
732 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
733 resblks, 0, 0, &tp);
734 if (error)
735 goto out;
736
737 xfs_ilock(ip, XFS_ILOCK_EXCL);
738 xfs_trans_ijoin(tp, ip, 0);
739
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100740 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
741 &got, &prev);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700742
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100743 /* If there is a hole at end_fsb - 1 go to the previous extent */
744 if (eof || got.br_startoff > end_fsb) {
745 ASSERT(idx > 0);
746 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
747 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700748
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100749 /* Walk backwards until we're out of the I/O range... */
750 while (got.br_startoff + got.br_blockcount > offset_fsb) {
751 del = got;
752 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
753
754 /* Extent delete may have bumped idx forward */
755 if (!del.br_blockcount) {
756 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700757 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700758 }
759
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100760 ASSERT(!isnullstartblock(got.br_startblock));
761
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800762 /*
763 * Don't remap unwritten extents; these are
764 * speculatively preallocated CoW extents that have been
765 * allocated but have not yet been involved in a write.
766 */
767 if (got.br_state == XFS_EXT_UNWRITTEN) {
768 idx--;
769 goto next_extent;
770 }
771
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100772 /* Unmap the old blocks in the data fork. */
773 xfs_defer_init(&dfops, &firstfsb);
774 rlen = del.br_blockcount;
775 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
776 &firstfsb, &dfops);
777 if (error)
778 goto out_defer;
779
780 /* Trim the extent to whatever got unmapped. */
781 if (rlen) {
782 xfs_trim_extent(&del, del.br_startoff + rlen,
783 del.br_blockcount - rlen);
784 }
785 trace_xfs_reflink_cow_remap(ip, &del);
786
787 /* Free the CoW orphan record. */
788 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
789 del.br_startblock, del.br_blockcount);
790 if (error)
791 goto out_defer;
792
793 /* Map the new blocks into the data fork. */
794 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
795 if (error)
796 goto out_defer;
797
798 /* Remove the mapping from the CoW fork. */
799 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
800
801 error = xfs_defer_finish(&tp, &dfops, ip);
802 if (error)
803 goto out_defer;
804
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700805next_extent:
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100806 if (idx < 0)
807 break;
808 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700809 }
810
811 error = xfs_trans_commit(tp);
812 xfs_iunlock(ip, XFS_ILOCK_EXCL);
813 if (error)
814 goto out;
815 return 0;
816
817out_defer:
818 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700819 xfs_trans_cancel(tp);
820 xfs_iunlock(ip, XFS_ILOCK_EXCL);
821out:
822 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
823 return error;
824}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700825
826/*
827 * Free leftover CoW reservations that didn't get cleaned out.
828 */
829int
830xfs_reflink_recover_cow(
831 struct xfs_mount *mp)
832{
833 xfs_agnumber_t agno;
834 int error = 0;
835
836 if (!xfs_sb_version_hasreflink(&mp->m_sb))
837 return 0;
838
839 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
840 error = xfs_refcount_recover_cow_leftovers(mp, agno);
841 if (error)
842 break;
843 }
844
845 return error;
846}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700847
848/*
849 * Reflinking (Block) Ranges of Two Files Together
850 *
851 * First, ensure that the reflink flag is set on both inodes. The flag is an
852 * optimization to avoid unnecessary refcount btree lookups in the write path.
853 *
854 * Now we can iteratively remap the range of extents (and holes) in src to the
855 * corresponding ranges in dest. Let drange and srange denote the ranges of
856 * logical blocks in dest and src touched by the reflink operation.
857 *
858 * While the length of drange is greater than zero,
859 * - Read src's bmbt at the start of srange ("imap")
860 * - If imap doesn't exist, make imap appear to start at the end of srange
861 * with zero length.
862 * - If imap starts before srange, advance imap to start at srange.
863 * - If imap goes beyond srange, truncate imap to end at the end of srange.
864 * - Punch (imap start - srange start + imap len) blocks from dest at
865 * offset (drange start).
866 * - If imap points to a real range of pblks,
867 * > Increase the refcount of the imap's pblks
868 * > Map imap's pblks into dest at the offset
869 * (drange start + imap start - srange start)
870 * - Advance drange and srange by (imap start - srange start + imap len)
871 *
872 * Finally, if the reflink made dest longer, update both the in-core and
873 * on-disk file sizes.
874 *
875 * ASCII Art Demonstration:
876 *
877 * Let's say we want to reflink this source file:
878 *
879 * ----SSSSSSS-SSSSS----SSSSSS (src file)
880 * <-------------------->
881 *
882 * into this destination file:
883 *
884 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
885 * <-------------------->
886 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
887 * Observe that the range has different logical offsets in either file.
888 *
889 * Consider that the first extent in the source file doesn't line up with our
890 * reflink range. Unmapping and remapping are separate operations, so we can
891 * unmap more blocks from the destination file than we remap.
892 *
893 * ----SSSSSSS-SSSSS----SSSSSS
894 * <------->
895 * --DDDDD---------DDDDD--DDD
896 * <------->
897 *
898 * Now remap the source extent into the destination file:
899 *
900 * ----SSSSSSS-SSSSS----SSSSSS
901 * <------->
902 * --DDDDD--SSSSSSSDDDDD--DDD
903 * <------->
904 *
905 * Do likewise with the second hole and extent in our range. Holes in the
906 * unmap range don't affect our operation.
907 *
908 * ----SSSSSSS-SSSSS----SSSSSS
909 * <---->
910 * --DDDDD--SSSSSSS-SSSSS-DDD
911 * <---->
912 *
913 * Finally, unmap and remap part of the third extent. This will increase the
914 * size of the destination file.
915 *
916 * ----SSSSSSS-SSSSS----SSSSSS
917 * <----->
918 * --DDDDD--SSSSSSS-SSSSS----SSS
919 * <----->
920 *
921 * Once we update the destination file's i_size, we're done.
922 */
923
924/*
925 * Ensure the reflink bit is set in both inodes.
926 */
927STATIC int
928xfs_reflink_set_inode_flag(
929 struct xfs_inode *src,
930 struct xfs_inode *dest)
931{
932 struct xfs_mount *mp = src->i_mount;
933 int error;
934 struct xfs_trans *tp;
935
936 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
937 return 0;
938
939 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
940 if (error)
941 goto out_error;
942
943 /* Lock both files against IO */
944 if (src->i_ino == dest->i_ino)
945 xfs_ilock(src, XFS_ILOCK_EXCL);
946 else
947 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
948
949 if (!xfs_is_reflink_inode(src)) {
950 trace_xfs_reflink_set_inode_flag(src);
951 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
952 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
953 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
954 xfs_ifork_init_cow(src);
955 } else
956 xfs_iunlock(src, XFS_ILOCK_EXCL);
957
958 if (src->i_ino == dest->i_ino)
959 goto commit_flags;
960
961 if (!xfs_is_reflink_inode(dest)) {
962 trace_xfs_reflink_set_inode_flag(dest);
963 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
964 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
965 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
966 xfs_ifork_init_cow(dest);
967 } else
968 xfs_iunlock(dest, XFS_ILOCK_EXCL);
969
970commit_flags:
971 error = xfs_trans_commit(tp);
972 if (error)
973 goto out_error;
974 return error;
975
976out_error:
977 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
978 return error;
979}
980
981/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700982 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700983 */
984STATIC int
985xfs_reflink_update_dest(
986 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700987 xfs_off_t newlen,
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -0800988 xfs_extlen_t cowextsize,
989 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700990{
991 struct xfs_mount *mp = dest->i_mount;
992 struct xfs_trans *tp;
993 int error;
994
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -0800995 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700996 return 0;
997
998 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
999 if (error)
1000 goto out_error;
1001
1002 xfs_ilock(dest, XFS_ILOCK_EXCL);
1003 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1004
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001005 if (newlen > i_size_read(VFS_I(dest))) {
1006 trace_xfs_reflink_update_inode_size(dest, newlen);
1007 i_size_write(VFS_I(dest), newlen);
1008 dest->i_d.di_size = newlen;
1009 }
1010
1011 if (cowextsize) {
1012 dest->i_d.di_cowextsize = cowextsize;
1013 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1014 }
1015
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001016 if (!is_dedupe) {
1017 xfs_trans_ichgtime(tp, dest,
1018 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1019 }
Darrick J. Wong862bb362016-10-03 09:11:40 -07001020 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1021
1022 error = xfs_trans_commit(tp);
1023 if (error)
1024 goto out_error;
1025 return error;
1026
1027out_error:
1028 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1029 return error;
1030}
1031
1032/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001033 * Do we have enough reserve in this AG to handle a reflink? The refcount
1034 * btree already reserved all the space it needs, but the rmap btree can grow
1035 * infinitely, so we won't allow more reflinks when the AG is down to the
1036 * btree reserves.
1037 */
1038static int
1039xfs_reflink_ag_has_free_space(
1040 struct xfs_mount *mp,
1041 xfs_agnumber_t agno)
1042{
1043 struct xfs_perag *pag;
1044 int error = 0;
1045
1046 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1047 return 0;
1048
1049 pag = xfs_perag_get(mp, agno);
1050 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1051 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1052 error = -ENOSPC;
1053 xfs_perag_put(pag);
1054 return error;
1055}
1056
1057/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001058 * Unmap a range of blocks from a file, then map other blocks into the hole.
1059 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1060 * The extent irec is mapped into dest at irec->br_startoff.
1061 */
1062STATIC int
1063xfs_reflink_remap_extent(
1064 struct xfs_inode *ip,
1065 struct xfs_bmbt_irec *irec,
1066 xfs_fileoff_t destoff,
1067 xfs_off_t new_isize)
1068{
1069 struct xfs_mount *mp = ip->i_mount;
1070 struct xfs_trans *tp;
1071 xfs_fsblock_t firstfsb;
1072 unsigned int resblks;
1073 struct xfs_defer_ops dfops;
1074 struct xfs_bmbt_irec uirec;
1075 bool real_extent;
1076 xfs_filblks_t rlen;
1077 xfs_filblks_t unmap_len;
1078 xfs_off_t newlen;
1079 int error;
1080
1081 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1082 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1083
1084 /* Only remap normal extents. */
1085 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1086 irec->br_startblock != DELAYSTARTBLOCK &&
1087 !ISUNWRITTEN(irec));
1088
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001089 /* No reflinking if we're low on space */
1090 if (real_extent) {
1091 error = xfs_reflink_ag_has_free_space(mp,
1092 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1093 if (error)
1094 goto out;
1095 }
1096
Darrick J. Wong862bb362016-10-03 09:11:40 -07001097 /* Start a rolling transaction to switch the mappings */
1098 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1099 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1100 if (error)
1101 goto out;
1102
1103 xfs_ilock(ip, XFS_ILOCK_EXCL);
1104 xfs_trans_ijoin(tp, ip, 0);
1105
1106 /* If we're not just clearing space, then do we have enough quota? */
1107 if (real_extent) {
1108 error = xfs_trans_reserve_quota_nblks(tp, ip,
1109 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1110 if (error)
1111 goto out_cancel;
1112 }
1113
1114 trace_xfs_reflink_remap(ip, irec->br_startoff,
1115 irec->br_blockcount, irec->br_startblock);
1116
1117 /* Unmap the old blocks in the data fork. */
1118 rlen = unmap_len;
1119 while (rlen) {
1120 xfs_defer_init(&dfops, &firstfsb);
1121 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1122 &firstfsb, &dfops);
1123 if (error)
1124 goto out_defer;
1125
1126 /*
1127 * Trim the extent to whatever got unmapped.
1128 * Remember, bunmapi works backwards.
1129 */
1130 uirec.br_startblock = irec->br_startblock + rlen;
1131 uirec.br_startoff = irec->br_startoff + rlen;
1132 uirec.br_blockcount = unmap_len - rlen;
1133 unmap_len = rlen;
1134
1135 /* If this isn't a real mapping, we're done. */
1136 if (!real_extent || uirec.br_blockcount == 0)
1137 goto next_extent;
1138
1139 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1140 uirec.br_blockcount, uirec.br_startblock);
1141
1142 /* Update the refcount tree */
1143 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1144 if (error)
1145 goto out_defer;
1146
1147 /* Map the new blocks into the data fork. */
1148 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1149 if (error)
1150 goto out_defer;
1151
1152 /* Update quota accounting. */
1153 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1154 uirec.br_blockcount);
1155
1156 /* Update dest isize if needed. */
1157 newlen = XFS_FSB_TO_B(mp,
1158 uirec.br_startoff + uirec.br_blockcount);
1159 newlen = min_t(xfs_off_t, newlen, new_isize);
1160 if (newlen > i_size_read(VFS_I(ip))) {
1161 trace_xfs_reflink_update_inode_size(ip, newlen);
1162 i_size_write(VFS_I(ip), newlen);
1163 ip->i_d.di_size = newlen;
1164 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1165 }
1166
1167next_extent:
1168 /* Process all the deferred stuff. */
1169 error = xfs_defer_finish(&tp, &dfops, ip);
1170 if (error)
1171 goto out_defer;
1172 }
1173
1174 error = xfs_trans_commit(tp);
1175 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1176 if (error)
1177 goto out;
1178 return 0;
1179
1180out_defer:
1181 xfs_defer_cancel(&dfops);
1182out_cancel:
1183 xfs_trans_cancel(tp);
1184 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1185out:
1186 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1187 return error;
1188}
1189
1190/*
1191 * Iteratively remap one file's extents (and holes) to another's.
1192 */
1193STATIC int
1194xfs_reflink_remap_blocks(
1195 struct xfs_inode *src,
1196 xfs_fileoff_t srcoff,
1197 struct xfs_inode *dest,
1198 xfs_fileoff_t destoff,
1199 xfs_filblks_t len,
1200 xfs_off_t new_isize)
1201{
1202 struct xfs_bmbt_irec imap;
1203 int nimaps;
1204 int error = 0;
1205 xfs_filblks_t range_len;
1206
1207 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1208 while (len) {
1209 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1210 dest, destoff);
1211 /* Read extent from the source file */
1212 nimaps = 1;
1213 xfs_ilock(src, XFS_ILOCK_EXCL);
1214 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1215 xfs_iunlock(src, XFS_ILOCK_EXCL);
1216 if (error)
1217 goto err;
1218 ASSERT(nimaps == 1);
1219
1220 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1221 &imap);
1222
1223 /* Translate imap into the destination file. */
1224 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1225 imap.br_startoff += destoff - srcoff;
1226
1227 /* Clear dest from destoff to the end of imap and map it in. */
1228 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1229 new_isize);
1230 if (error)
1231 goto err;
1232
1233 if (fatal_signal_pending(current)) {
1234 error = -EINTR;
1235 goto err;
1236 }
1237
1238 /* Advance drange/srange */
1239 srcoff += range_len;
1240 destoff += range_len;
1241 len -= range_len;
1242 }
1243
1244 return 0;
1245
1246err:
1247 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1248 return error;
1249}
1250
1251/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001252 * Read a page's worth of file data into the page cache. Return the page
1253 * locked.
1254 */
1255static struct page *
1256xfs_get_page(
1257 struct inode *inode,
1258 xfs_off_t offset)
1259{
1260 struct address_space *mapping;
1261 struct page *page;
1262 pgoff_t n;
1263
1264 n = offset >> PAGE_SHIFT;
1265 mapping = inode->i_mapping;
1266 page = read_mapping_page(mapping, n, NULL);
1267 if (IS_ERR(page))
1268 return page;
1269 if (!PageUptodate(page)) {
1270 put_page(page);
1271 return ERR_PTR(-EIO);
1272 }
1273 lock_page(page);
1274 return page;
1275}
1276
1277/*
1278 * Compare extents of two files to see if they are the same.
1279 */
1280static int
1281xfs_compare_extents(
1282 struct inode *src,
1283 xfs_off_t srcoff,
1284 struct inode *dest,
1285 xfs_off_t destoff,
1286 xfs_off_t len,
1287 bool *is_same)
1288{
1289 xfs_off_t src_poff;
1290 xfs_off_t dest_poff;
1291 void *src_addr;
1292 void *dest_addr;
1293 struct page *src_page;
1294 struct page *dest_page;
1295 xfs_off_t cmp_len;
1296 bool same;
1297 int error;
1298
1299 error = -EINVAL;
1300 same = true;
1301 while (len) {
1302 src_poff = srcoff & (PAGE_SIZE - 1);
1303 dest_poff = destoff & (PAGE_SIZE - 1);
1304 cmp_len = min(PAGE_SIZE - src_poff,
1305 PAGE_SIZE - dest_poff);
1306 cmp_len = min(cmp_len, len);
1307 ASSERT(cmp_len > 0);
1308
1309 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1310 XFS_I(dest), destoff);
1311
1312 src_page = xfs_get_page(src, srcoff);
1313 if (IS_ERR(src_page)) {
1314 error = PTR_ERR(src_page);
1315 goto out_error;
1316 }
1317 dest_page = xfs_get_page(dest, destoff);
1318 if (IS_ERR(dest_page)) {
1319 error = PTR_ERR(dest_page);
1320 unlock_page(src_page);
1321 put_page(src_page);
1322 goto out_error;
1323 }
1324 src_addr = kmap_atomic(src_page);
1325 dest_addr = kmap_atomic(dest_page);
1326
1327 flush_dcache_page(src_page);
1328 flush_dcache_page(dest_page);
1329
1330 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1331 same = false;
1332
1333 kunmap_atomic(dest_addr);
1334 kunmap_atomic(src_addr);
1335 unlock_page(dest_page);
1336 unlock_page(src_page);
1337 put_page(dest_page);
1338 put_page(src_page);
1339
1340 if (!same)
1341 break;
1342
1343 srcoff += cmp_len;
1344 destoff += cmp_len;
1345 len -= cmp_len;
1346 }
1347
1348 *is_same = same;
1349 return 0;
1350
1351out_error:
1352 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1353 return error;
1354}
1355
1356/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001357 * Link a range of blocks from one file to another.
1358 */
1359int
1360xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001361 struct file *file_in,
1362 loff_t pos_in,
1363 struct file *file_out,
1364 loff_t pos_out,
1365 u64 len,
1366 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001367{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001368 struct inode *inode_in = file_inode(file_in);
1369 struct xfs_inode *src = XFS_I(inode_in);
1370 struct inode *inode_out = file_inode(file_out);
1371 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001372 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001373 loff_t bs = inode_out->i_sb->s_blocksize;
1374 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001375 xfs_fileoff_t sfsbno, dfsbno;
1376 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001377 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001378 loff_t isize;
1379 ssize_t ret;
1380 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001381
1382 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1383 return -EOPNOTSUPP;
1384
1385 if (XFS_FORCED_SHUTDOWN(mp))
1386 return -EIO;
1387
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001388 /* Lock both files against IO */
1389 if (same_inode) {
1390 xfs_ilock(src, XFS_IOLOCK_EXCL);
1391 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1392 } else {
1393 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1394 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1395 }
1396
1397 /* Don't touch certain kinds of inodes */
1398 ret = -EPERM;
1399 if (IS_IMMUTABLE(inode_out))
1400 goto out_unlock;
1401
1402 ret = -ETXTBSY;
1403 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1404 goto out_unlock;
1405
1406
1407 /* Don't reflink dirs, pipes, sockets... */
1408 ret = -EISDIR;
1409 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1410 goto out_unlock;
1411 ret = -EINVAL;
1412 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1413 goto out_unlock;
1414 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1415 goto out_unlock;
1416
Darrick J. Wong862bb362016-10-03 09:11:40 -07001417 /* Don't reflink realtime inodes */
1418 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001419 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001420
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001421 /* Don't share DAX file data for now. */
1422 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1423 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001424
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001425 /* Are we going all the way to the end? */
1426 isize = i_size_read(inode_in);
1427 if (isize == 0) {
1428 ret = 0;
1429 goto out_unlock;
1430 }
1431
Darrick J. Wong39032572017-01-09 16:38:41 +01001432 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1433 if (len == 0) {
1434 if (is_dedupe) {
1435 ret = 0;
1436 goto out_unlock;
1437 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001438 len = isize - pos_in;
Darrick J. Wong39032572017-01-09 16:38:41 +01001439 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001440
1441 /* Ensure offsets don't wrap and the input is inside i_size */
1442 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1443 pos_in + len > isize)
1444 goto out_unlock;
1445
1446 /* Don't allow dedupe past EOF in the dest file */
1447 if (is_dedupe) {
1448 loff_t disize;
1449
1450 disize = i_size_read(inode_out);
1451 if (pos_out >= disize || pos_out + len > disize)
1452 goto out_unlock;
1453 }
1454
1455 /* If we're linking to EOF, continue to the block boundary. */
1456 if (pos_in + len == isize)
1457 blen = ALIGN(isize, bs) - pos_in;
1458 else
1459 blen = len;
1460
1461 /* Only reflink if we're aligned to block boundaries */
1462 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1463 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1464 goto out_unlock;
1465
1466 /* Don't allow overlapped reflink within the same file */
1467 if (same_inode) {
1468 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1469 goto out_unlock;
1470 }
1471
1472 /* Wait for the completion of any pending IOs on both files */
1473 inode_dio_wait(inode_in);
1474 if (!same_inode)
1475 inode_dio_wait(inode_out);
1476
1477 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1478 pos_in, pos_in + len - 1);
1479 if (ret)
1480 goto out_unlock;
1481
1482 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1483 pos_out, pos_out + len - 1);
1484 if (ret)
1485 goto out_unlock;
1486
1487 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001488
Darrick J. Wongcc714662016-10-03 09:11:41 -07001489 /*
1490 * Check that the extents are the same.
1491 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001492 if (is_dedupe) {
1493 bool is_same = false;
1494
1495 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1496 len, &is_same);
1497 if (ret)
1498 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001499 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001500 ret = -EBADE;
1501 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001502 }
1503 }
1504
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001505 ret = xfs_reflink_set_inode_flag(src, dest);
1506 if (ret)
1507 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001508
1509 /*
1510 * Invalidate the page cache so that we can clear any CoW mappings
1511 * in the destination file.
1512 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001513 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1514 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001515
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001516 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1517 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001518 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001519 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1520 pos_out + len);
1521 if (ret)
1522 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001523
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001524 /*
1525 * Carry the cowextsize hint from src to dest if we're sharing the
1526 * entire source file to the entire destination file, the source file
1527 * has a cowextsize hint, and the destination file does not.
1528 */
1529 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001530 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001531 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001532 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001533 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1534 cowextsize = src->i_d.di_cowextsize;
1535
Christoph Hellwig67eb7bf2017-02-06 17:45:51 -08001536 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1537 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001538
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001539out_unlock:
1540 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1541 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1542 if (src->i_ino != dest->i_ino) {
1543 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1544 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1545 }
1546 if (ret)
1547 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1548 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001549}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001550
1551/*
1552 * The user wants to preemptively CoW all shared blocks in this file,
1553 * which enables us to turn off the reflink flag. Iterate all
1554 * extents which are not prealloc/delalloc to see which ranges are
1555 * mentioned in the refcount tree, then read those blocks into the
1556 * pagecache, dirty them, fsync them back out, and then we can update
1557 * the inode flag. What happens if we run out of memory? :)
1558 */
1559STATIC int
1560xfs_reflink_dirty_extents(
1561 struct xfs_inode *ip,
1562 xfs_fileoff_t fbno,
1563 xfs_filblks_t end,
1564 xfs_off_t isize)
1565{
1566 struct xfs_mount *mp = ip->i_mount;
1567 xfs_agnumber_t agno;
1568 xfs_agblock_t agbno;
1569 xfs_extlen_t aglen;
1570 xfs_agblock_t rbno;
1571 xfs_extlen_t rlen;
1572 xfs_off_t fpos;
1573 xfs_off_t flen;
1574 struct xfs_bmbt_irec map[2];
1575 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001576 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001577
1578 while (end - fbno > 0) {
1579 nmaps = 1;
1580 /*
1581 * Look for extents in the file. Skip holes, delalloc, or
1582 * unwritten extents; they can't be reflinked.
1583 */
1584 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1585 if (error)
1586 goto out;
1587 if (nmaps == 0)
1588 break;
1589 if (map[0].br_startblock == HOLESTARTBLOCK ||
1590 map[0].br_startblock == DELAYSTARTBLOCK ||
1591 ISUNWRITTEN(&map[0]))
1592 goto next;
1593
1594 map[1] = map[0];
1595 while (map[1].br_blockcount) {
1596 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1597 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1598 aglen = map[1].br_blockcount;
1599
1600 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1601 &rbno, &rlen, true);
1602 if (error)
1603 goto out;
1604 if (rbno == NULLAGBLOCK)
1605 break;
1606
1607 /* Dirty the pages */
1608 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1609 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1610 (rbno - agbno));
1611 flen = XFS_FSB_TO_B(mp, rlen);
1612 if (fpos + flen > isize)
1613 flen = isize - fpos;
1614 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1615 &xfs_iomap_ops);
1616 xfs_ilock(ip, XFS_ILOCK_EXCL);
1617 if (error)
1618 goto out;
1619
1620 map[1].br_blockcount -= (rbno - agbno + rlen);
1621 map[1].br_startoff += (rbno - agbno + rlen);
1622 map[1].br_startblock += (rbno - agbno + rlen);
1623 }
1624
1625next:
1626 fbno = map[0].br_startoff + map[0].br_blockcount;
1627 }
1628out:
1629 return error;
1630}
1631
1632/* Clear the inode reflink flag if there are no shared extents. */
1633int
1634xfs_reflink_clear_inode_flag(
1635 struct xfs_inode *ip,
1636 struct xfs_trans **tpp)
1637{
1638 struct xfs_mount *mp = ip->i_mount;
1639 xfs_fileoff_t fbno;
1640 xfs_filblks_t end;
1641 xfs_agnumber_t agno;
1642 xfs_agblock_t agbno;
1643 xfs_extlen_t aglen;
1644 xfs_agblock_t rbno;
1645 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001646 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001647 int nmaps;
1648 int error = 0;
1649
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001650 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001651
1652 fbno = 0;
1653 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1654 while (end - fbno > 0) {
1655 nmaps = 1;
1656 /*
1657 * Look for extents in the file. Skip holes, delalloc, or
1658 * unwritten extents; they can't be reflinked.
1659 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001660 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001661 if (error)
1662 return error;
1663 if (nmaps == 0)
1664 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001665 if (map.br_startblock == HOLESTARTBLOCK ||
1666 map.br_startblock == DELAYSTARTBLOCK ||
1667 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001668 goto next;
1669
Darrick J. Wong024adf42016-10-10 16:47:40 +11001670 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1671 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1672 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001673
Darrick J. Wong024adf42016-10-10 16:47:40 +11001674 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1675 &rbno, &rlen, false);
1676 if (error)
1677 return error;
1678 /* Is there still a shared block here? */
1679 if (rbno != NULLAGBLOCK)
1680 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001681next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001682 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001683 }
1684
1685 /*
1686 * We didn't find any shared blocks so turn off the reflink flag.
1687 * First, get rid of any leftover CoW mappings.
1688 */
1689 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1690 if (error)
1691 return error;
1692
1693 /* Clear the inode flag. */
1694 trace_xfs_reflink_unset_inode_flag(ip);
1695 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001696 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001697 xfs_trans_ijoin(*tpp, ip, 0);
1698 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1699
1700 return error;
1701}
1702
1703/*
1704 * Clear the inode reflink flag if there are no shared extents and the size
1705 * hasn't changed.
1706 */
1707STATIC int
1708xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001709 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001710{
1711 struct xfs_mount *mp = ip->i_mount;
1712 struct xfs_trans *tp;
1713 int error = 0;
1714
1715 /* Start a rolling transaction to remove the mappings */
1716 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1717 if (error)
1718 return error;
1719
1720 xfs_ilock(ip, XFS_ILOCK_EXCL);
1721 xfs_trans_ijoin(tp, ip, 0);
1722
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001723 error = xfs_reflink_clear_inode_flag(ip, &tp);
1724 if (error)
1725 goto cancel;
1726
1727 error = xfs_trans_commit(tp);
1728 if (error)
1729 goto out;
1730
1731 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1732 return 0;
1733cancel:
1734 xfs_trans_cancel(tp);
1735out:
1736 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1737 return error;
1738}
1739
1740/*
1741 * Pre-COW all shared blocks within a given byte range of a file and turn off
1742 * the reflink flag if we unshare all of the file's blocks.
1743 */
1744int
1745xfs_reflink_unshare(
1746 struct xfs_inode *ip,
1747 xfs_off_t offset,
1748 xfs_off_t len)
1749{
1750 struct xfs_mount *mp = ip->i_mount;
1751 xfs_fileoff_t fbno;
1752 xfs_filblks_t end;
1753 xfs_off_t isize;
1754 int error;
1755
1756 if (!xfs_is_reflink_inode(ip))
1757 return 0;
1758
1759 trace_xfs_reflink_unshare(ip, offset, len);
1760
1761 inode_dio_wait(VFS_I(ip));
1762
1763 /* Try to CoW the selected ranges */
1764 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001765 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001766 isize = i_size_read(VFS_I(ip));
1767 end = XFS_B_TO_FSB(mp, offset + len);
1768 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1769 if (error)
1770 goto out_unlock;
1771 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1772
1773 /* Wait for the IO to finish */
1774 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1775 if (error)
1776 goto out;
1777
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001778 /* Turn off the reflink flag if possible. */
1779 error = xfs_reflink_try_clear_inode_flag(ip);
1780 if (error)
1781 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001782
1783 return 0;
1784
1785out_unlock:
1786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1787out:
1788 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1789 return error;
1790}