blob: 539a612a02e5a04dbc47b40528605cf4fab8f5bb [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,
988 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700989{
990 struct xfs_mount *mp = dest->i_mount;
991 struct xfs_trans *tp;
992 int error;
993
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700994 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700995 return 0;
996
997 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
998 if (error)
999 goto out_error;
1000
1001 xfs_ilock(dest, XFS_ILOCK_EXCL);
1002 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1003
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001004 if (newlen > i_size_read(VFS_I(dest))) {
1005 trace_xfs_reflink_update_inode_size(dest, newlen);
1006 i_size_write(VFS_I(dest), newlen);
1007 dest->i_d.di_size = newlen;
1008 }
1009
1010 if (cowextsize) {
1011 dest->i_d.di_cowextsize = cowextsize;
1012 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1013 }
1014
Darrick J. Wong862bb362016-10-03 09:11:40 -07001015 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1016
1017 error = xfs_trans_commit(tp);
1018 if (error)
1019 goto out_error;
1020 return error;
1021
1022out_error:
1023 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1024 return error;
1025}
1026
1027/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001028 * Do we have enough reserve in this AG to handle a reflink? The refcount
1029 * btree already reserved all the space it needs, but the rmap btree can grow
1030 * infinitely, so we won't allow more reflinks when the AG is down to the
1031 * btree reserves.
1032 */
1033static int
1034xfs_reflink_ag_has_free_space(
1035 struct xfs_mount *mp,
1036 xfs_agnumber_t agno)
1037{
1038 struct xfs_perag *pag;
1039 int error = 0;
1040
1041 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1042 return 0;
1043
1044 pag = xfs_perag_get(mp, agno);
1045 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1046 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1047 error = -ENOSPC;
1048 xfs_perag_put(pag);
1049 return error;
1050}
1051
1052/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001053 * Unmap a range of blocks from a file, then map other blocks into the hole.
1054 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1055 * The extent irec is mapped into dest at irec->br_startoff.
1056 */
1057STATIC int
1058xfs_reflink_remap_extent(
1059 struct xfs_inode *ip,
1060 struct xfs_bmbt_irec *irec,
1061 xfs_fileoff_t destoff,
1062 xfs_off_t new_isize)
1063{
1064 struct xfs_mount *mp = ip->i_mount;
1065 struct xfs_trans *tp;
1066 xfs_fsblock_t firstfsb;
1067 unsigned int resblks;
1068 struct xfs_defer_ops dfops;
1069 struct xfs_bmbt_irec uirec;
1070 bool real_extent;
1071 xfs_filblks_t rlen;
1072 xfs_filblks_t unmap_len;
1073 xfs_off_t newlen;
1074 int error;
1075
1076 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1077 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1078
1079 /* Only remap normal extents. */
1080 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1081 irec->br_startblock != DELAYSTARTBLOCK &&
1082 !ISUNWRITTEN(irec));
1083
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001084 /* No reflinking if we're low on space */
1085 if (real_extent) {
1086 error = xfs_reflink_ag_has_free_space(mp,
1087 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1088 if (error)
1089 goto out;
1090 }
1091
Darrick J. Wong862bb362016-10-03 09:11:40 -07001092 /* Start a rolling transaction to switch the mappings */
1093 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1094 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1095 if (error)
1096 goto out;
1097
1098 xfs_ilock(ip, XFS_ILOCK_EXCL);
1099 xfs_trans_ijoin(tp, ip, 0);
1100
1101 /* If we're not just clearing space, then do we have enough quota? */
1102 if (real_extent) {
1103 error = xfs_trans_reserve_quota_nblks(tp, ip,
1104 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1105 if (error)
1106 goto out_cancel;
1107 }
1108
1109 trace_xfs_reflink_remap(ip, irec->br_startoff,
1110 irec->br_blockcount, irec->br_startblock);
1111
1112 /* Unmap the old blocks in the data fork. */
1113 rlen = unmap_len;
1114 while (rlen) {
1115 xfs_defer_init(&dfops, &firstfsb);
1116 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1117 &firstfsb, &dfops);
1118 if (error)
1119 goto out_defer;
1120
1121 /*
1122 * Trim the extent to whatever got unmapped.
1123 * Remember, bunmapi works backwards.
1124 */
1125 uirec.br_startblock = irec->br_startblock + rlen;
1126 uirec.br_startoff = irec->br_startoff + rlen;
1127 uirec.br_blockcount = unmap_len - rlen;
1128 unmap_len = rlen;
1129
1130 /* If this isn't a real mapping, we're done. */
1131 if (!real_extent || uirec.br_blockcount == 0)
1132 goto next_extent;
1133
1134 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1135 uirec.br_blockcount, uirec.br_startblock);
1136
1137 /* Update the refcount tree */
1138 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1139 if (error)
1140 goto out_defer;
1141
1142 /* Map the new blocks into the data fork. */
1143 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1144 if (error)
1145 goto out_defer;
1146
1147 /* Update quota accounting. */
1148 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1149 uirec.br_blockcount);
1150
1151 /* Update dest isize if needed. */
1152 newlen = XFS_FSB_TO_B(mp,
1153 uirec.br_startoff + uirec.br_blockcount);
1154 newlen = min_t(xfs_off_t, newlen, new_isize);
1155 if (newlen > i_size_read(VFS_I(ip))) {
1156 trace_xfs_reflink_update_inode_size(ip, newlen);
1157 i_size_write(VFS_I(ip), newlen);
1158 ip->i_d.di_size = newlen;
1159 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1160 }
1161
1162next_extent:
1163 /* Process all the deferred stuff. */
1164 error = xfs_defer_finish(&tp, &dfops, ip);
1165 if (error)
1166 goto out_defer;
1167 }
1168
1169 error = xfs_trans_commit(tp);
1170 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1171 if (error)
1172 goto out;
1173 return 0;
1174
1175out_defer:
1176 xfs_defer_cancel(&dfops);
1177out_cancel:
1178 xfs_trans_cancel(tp);
1179 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1180out:
1181 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1182 return error;
1183}
1184
1185/*
1186 * Iteratively remap one file's extents (and holes) to another's.
1187 */
1188STATIC int
1189xfs_reflink_remap_blocks(
1190 struct xfs_inode *src,
1191 xfs_fileoff_t srcoff,
1192 struct xfs_inode *dest,
1193 xfs_fileoff_t destoff,
1194 xfs_filblks_t len,
1195 xfs_off_t new_isize)
1196{
1197 struct xfs_bmbt_irec imap;
1198 int nimaps;
1199 int error = 0;
1200 xfs_filblks_t range_len;
1201
1202 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1203 while (len) {
1204 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1205 dest, destoff);
1206 /* Read extent from the source file */
1207 nimaps = 1;
1208 xfs_ilock(src, XFS_ILOCK_EXCL);
1209 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1210 xfs_iunlock(src, XFS_ILOCK_EXCL);
1211 if (error)
1212 goto err;
1213 ASSERT(nimaps == 1);
1214
1215 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1216 &imap);
1217
1218 /* Translate imap into the destination file. */
1219 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1220 imap.br_startoff += destoff - srcoff;
1221
1222 /* Clear dest from destoff to the end of imap and map it in. */
1223 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1224 new_isize);
1225 if (error)
1226 goto err;
1227
1228 if (fatal_signal_pending(current)) {
1229 error = -EINTR;
1230 goto err;
1231 }
1232
1233 /* Advance drange/srange */
1234 srcoff += range_len;
1235 destoff += range_len;
1236 len -= range_len;
1237 }
1238
1239 return 0;
1240
1241err:
1242 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1243 return error;
1244}
1245
1246/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001247 * Read a page's worth of file data into the page cache. Return the page
1248 * locked.
1249 */
1250static struct page *
1251xfs_get_page(
1252 struct inode *inode,
1253 xfs_off_t offset)
1254{
1255 struct address_space *mapping;
1256 struct page *page;
1257 pgoff_t n;
1258
1259 n = offset >> PAGE_SHIFT;
1260 mapping = inode->i_mapping;
1261 page = read_mapping_page(mapping, n, NULL);
1262 if (IS_ERR(page))
1263 return page;
1264 if (!PageUptodate(page)) {
1265 put_page(page);
1266 return ERR_PTR(-EIO);
1267 }
1268 lock_page(page);
1269 return page;
1270}
1271
1272/*
1273 * Compare extents of two files to see if they are the same.
1274 */
1275static int
1276xfs_compare_extents(
1277 struct inode *src,
1278 xfs_off_t srcoff,
1279 struct inode *dest,
1280 xfs_off_t destoff,
1281 xfs_off_t len,
1282 bool *is_same)
1283{
1284 xfs_off_t src_poff;
1285 xfs_off_t dest_poff;
1286 void *src_addr;
1287 void *dest_addr;
1288 struct page *src_page;
1289 struct page *dest_page;
1290 xfs_off_t cmp_len;
1291 bool same;
1292 int error;
1293
1294 error = -EINVAL;
1295 same = true;
1296 while (len) {
1297 src_poff = srcoff & (PAGE_SIZE - 1);
1298 dest_poff = destoff & (PAGE_SIZE - 1);
1299 cmp_len = min(PAGE_SIZE - src_poff,
1300 PAGE_SIZE - dest_poff);
1301 cmp_len = min(cmp_len, len);
1302 ASSERT(cmp_len > 0);
1303
1304 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1305 XFS_I(dest), destoff);
1306
1307 src_page = xfs_get_page(src, srcoff);
1308 if (IS_ERR(src_page)) {
1309 error = PTR_ERR(src_page);
1310 goto out_error;
1311 }
1312 dest_page = xfs_get_page(dest, destoff);
1313 if (IS_ERR(dest_page)) {
1314 error = PTR_ERR(dest_page);
1315 unlock_page(src_page);
1316 put_page(src_page);
1317 goto out_error;
1318 }
1319 src_addr = kmap_atomic(src_page);
1320 dest_addr = kmap_atomic(dest_page);
1321
1322 flush_dcache_page(src_page);
1323 flush_dcache_page(dest_page);
1324
1325 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1326 same = false;
1327
1328 kunmap_atomic(dest_addr);
1329 kunmap_atomic(src_addr);
1330 unlock_page(dest_page);
1331 unlock_page(src_page);
1332 put_page(dest_page);
1333 put_page(src_page);
1334
1335 if (!same)
1336 break;
1337
1338 srcoff += cmp_len;
1339 destoff += cmp_len;
1340 len -= cmp_len;
1341 }
1342
1343 *is_same = same;
1344 return 0;
1345
1346out_error:
1347 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1348 return error;
1349}
1350
1351/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001352 * Link a range of blocks from one file to another.
1353 */
1354int
1355xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001356 struct file *file_in,
1357 loff_t pos_in,
1358 struct file *file_out,
1359 loff_t pos_out,
1360 u64 len,
1361 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001362{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001363 struct inode *inode_in = file_inode(file_in);
1364 struct xfs_inode *src = XFS_I(inode_in);
1365 struct inode *inode_out = file_inode(file_out);
1366 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001367 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001368 loff_t bs = inode_out->i_sb->s_blocksize;
1369 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001370 xfs_fileoff_t sfsbno, dfsbno;
1371 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001372 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001373 loff_t isize;
1374 ssize_t ret;
1375 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001376
1377 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1378 return -EOPNOTSUPP;
1379
1380 if (XFS_FORCED_SHUTDOWN(mp))
1381 return -EIO;
1382
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001383 /* Lock both files against IO */
1384 if (same_inode) {
1385 xfs_ilock(src, XFS_IOLOCK_EXCL);
1386 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1387 } else {
1388 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1389 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1390 }
1391
1392 /* Don't touch certain kinds of inodes */
1393 ret = -EPERM;
1394 if (IS_IMMUTABLE(inode_out))
1395 goto out_unlock;
1396
1397 ret = -ETXTBSY;
1398 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1399 goto out_unlock;
1400
1401
1402 /* Don't reflink dirs, pipes, sockets... */
1403 ret = -EISDIR;
1404 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1405 goto out_unlock;
1406 ret = -EINVAL;
1407 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1408 goto out_unlock;
1409 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1410 goto out_unlock;
1411
Darrick J. Wong862bb362016-10-03 09:11:40 -07001412 /* Don't reflink realtime inodes */
1413 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001414 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001415
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001416 /* Don't share DAX file data for now. */
1417 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1418 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001419
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001420 /* Are we going all the way to the end? */
1421 isize = i_size_read(inode_in);
1422 if (isize == 0) {
1423 ret = 0;
1424 goto out_unlock;
1425 }
1426
Darrick J. Wong39032572017-01-09 16:38:41 +01001427 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1428 if (len == 0) {
1429 if (is_dedupe) {
1430 ret = 0;
1431 goto out_unlock;
1432 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001433 len = isize - pos_in;
Darrick J. Wong39032572017-01-09 16:38:41 +01001434 }
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001435
1436 /* Ensure offsets don't wrap and the input is inside i_size */
1437 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1438 pos_in + len > isize)
1439 goto out_unlock;
1440
1441 /* Don't allow dedupe past EOF in the dest file */
1442 if (is_dedupe) {
1443 loff_t disize;
1444
1445 disize = i_size_read(inode_out);
1446 if (pos_out >= disize || pos_out + len > disize)
1447 goto out_unlock;
1448 }
1449
1450 /* If we're linking to EOF, continue to the block boundary. */
1451 if (pos_in + len == isize)
1452 blen = ALIGN(isize, bs) - pos_in;
1453 else
1454 blen = len;
1455
1456 /* Only reflink if we're aligned to block boundaries */
1457 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1458 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1459 goto out_unlock;
1460
1461 /* Don't allow overlapped reflink within the same file */
1462 if (same_inode) {
1463 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1464 goto out_unlock;
1465 }
1466
1467 /* Wait for the completion of any pending IOs on both files */
1468 inode_dio_wait(inode_in);
1469 if (!same_inode)
1470 inode_dio_wait(inode_out);
1471
1472 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1473 pos_in, pos_in + len - 1);
1474 if (ret)
1475 goto out_unlock;
1476
1477 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1478 pos_out, pos_out + len - 1);
1479 if (ret)
1480 goto out_unlock;
1481
1482 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001483
Darrick J. Wongcc714662016-10-03 09:11:41 -07001484 /*
1485 * Check that the extents are the same.
1486 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001487 if (is_dedupe) {
1488 bool is_same = false;
1489
1490 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1491 len, &is_same);
1492 if (ret)
1493 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001494 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001495 ret = -EBADE;
1496 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001497 }
1498 }
1499
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001500 ret = xfs_reflink_set_inode_flag(src, dest);
1501 if (ret)
1502 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001503
1504 /*
1505 * Invalidate the page cache so that we can clear any CoW mappings
1506 * in the destination file.
1507 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001508 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1509 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001510
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001511 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1512 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001513 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001514 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1515 pos_out + len);
1516 if (ret)
1517 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001518
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001519 /*
1520 * Carry the cowextsize hint from src to dest if we're sharing the
1521 * entire source file to the entire destination file, the source file
1522 * has a cowextsize hint, and the destination file does not.
1523 */
1524 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001525 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001526 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001527 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001528 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1529 cowextsize = src->i_d.di_cowextsize;
1530
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001531 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001532
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001533out_unlock:
1534 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1535 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1536 if (src->i_ino != dest->i_ino) {
1537 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1538 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1539 }
1540 if (ret)
1541 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1542 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001543}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001544
1545/*
1546 * The user wants to preemptively CoW all shared blocks in this file,
1547 * which enables us to turn off the reflink flag. Iterate all
1548 * extents which are not prealloc/delalloc to see which ranges are
1549 * mentioned in the refcount tree, then read those blocks into the
1550 * pagecache, dirty them, fsync them back out, and then we can update
1551 * the inode flag. What happens if we run out of memory? :)
1552 */
1553STATIC int
1554xfs_reflink_dirty_extents(
1555 struct xfs_inode *ip,
1556 xfs_fileoff_t fbno,
1557 xfs_filblks_t end,
1558 xfs_off_t isize)
1559{
1560 struct xfs_mount *mp = ip->i_mount;
1561 xfs_agnumber_t agno;
1562 xfs_agblock_t agbno;
1563 xfs_extlen_t aglen;
1564 xfs_agblock_t rbno;
1565 xfs_extlen_t rlen;
1566 xfs_off_t fpos;
1567 xfs_off_t flen;
1568 struct xfs_bmbt_irec map[2];
1569 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001570 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001571
1572 while (end - fbno > 0) {
1573 nmaps = 1;
1574 /*
1575 * Look for extents in the file. Skip holes, delalloc, or
1576 * unwritten extents; they can't be reflinked.
1577 */
1578 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1579 if (error)
1580 goto out;
1581 if (nmaps == 0)
1582 break;
1583 if (map[0].br_startblock == HOLESTARTBLOCK ||
1584 map[0].br_startblock == DELAYSTARTBLOCK ||
1585 ISUNWRITTEN(&map[0]))
1586 goto next;
1587
1588 map[1] = map[0];
1589 while (map[1].br_blockcount) {
1590 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1591 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1592 aglen = map[1].br_blockcount;
1593
1594 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1595 &rbno, &rlen, true);
1596 if (error)
1597 goto out;
1598 if (rbno == NULLAGBLOCK)
1599 break;
1600
1601 /* Dirty the pages */
1602 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1603 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1604 (rbno - agbno));
1605 flen = XFS_FSB_TO_B(mp, rlen);
1606 if (fpos + flen > isize)
1607 flen = isize - fpos;
1608 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1609 &xfs_iomap_ops);
1610 xfs_ilock(ip, XFS_ILOCK_EXCL);
1611 if (error)
1612 goto out;
1613
1614 map[1].br_blockcount -= (rbno - agbno + rlen);
1615 map[1].br_startoff += (rbno - agbno + rlen);
1616 map[1].br_startblock += (rbno - agbno + rlen);
1617 }
1618
1619next:
1620 fbno = map[0].br_startoff + map[0].br_blockcount;
1621 }
1622out:
1623 return error;
1624}
1625
1626/* Clear the inode reflink flag if there are no shared extents. */
1627int
1628xfs_reflink_clear_inode_flag(
1629 struct xfs_inode *ip,
1630 struct xfs_trans **tpp)
1631{
1632 struct xfs_mount *mp = ip->i_mount;
1633 xfs_fileoff_t fbno;
1634 xfs_filblks_t end;
1635 xfs_agnumber_t agno;
1636 xfs_agblock_t agbno;
1637 xfs_extlen_t aglen;
1638 xfs_agblock_t rbno;
1639 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001640 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001641 int nmaps;
1642 int error = 0;
1643
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001644 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001645
1646 fbno = 0;
1647 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1648 while (end - fbno > 0) {
1649 nmaps = 1;
1650 /*
1651 * Look for extents in the file. Skip holes, delalloc, or
1652 * unwritten extents; they can't be reflinked.
1653 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001654 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001655 if (error)
1656 return error;
1657 if (nmaps == 0)
1658 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001659 if (map.br_startblock == HOLESTARTBLOCK ||
1660 map.br_startblock == DELAYSTARTBLOCK ||
1661 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001662 goto next;
1663
Darrick J. Wong024adf42016-10-10 16:47:40 +11001664 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1665 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1666 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001667
Darrick J. Wong024adf42016-10-10 16:47:40 +11001668 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1669 &rbno, &rlen, false);
1670 if (error)
1671 return error;
1672 /* Is there still a shared block here? */
1673 if (rbno != NULLAGBLOCK)
1674 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001675next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001676 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001677 }
1678
1679 /*
1680 * We didn't find any shared blocks so turn off the reflink flag.
1681 * First, get rid of any leftover CoW mappings.
1682 */
1683 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1684 if (error)
1685 return error;
1686
1687 /* Clear the inode flag. */
1688 trace_xfs_reflink_unset_inode_flag(ip);
1689 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001690 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001691 xfs_trans_ijoin(*tpp, ip, 0);
1692 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1693
1694 return error;
1695}
1696
1697/*
1698 * Clear the inode reflink flag if there are no shared extents and the size
1699 * hasn't changed.
1700 */
1701STATIC int
1702xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001703 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001704{
1705 struct xfs_mount *mp = ip->i_mount;
1706 struct xfs_trans *tp;
1707 int error = 0;
1708
1709 /* Start a rolling transaction to remove the mappings */
1710 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1711 if (error)
1712 return error;
1713
1714 xfs_ilock(ip, XFS_ILOCK_EXCL);
1715 xfs_trans_ijoin(tp, ip, 0);
1716
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001717 error = xfs_reflink_clear_inode_flag(ip, &tp);
1718 if (error)
1719 goto cancel;
1720
1721 error = xfs_trans_commit(tp);
1722 if (error)
1723 goto out;
1724
1725 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1726 return 0;
1727cancel:
1728 xfs_trans_cancel(tp);
1729out:
1730 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1731 return error;
1732}
1733
1734/*
1735 * Pre-COW all shared blocks within a given byte range of a file and turn off
1736 * the reflink flag if we unshare all of the file's blocks.
1737 */
1738int
1739xfs_reflink_unshare(
1740 struct xfs_inode *ip,
1741 xfs_off_t offset,
1742 xfs_off_t len)
1743{
1744 struct xfs_mount *mp = ip->i_mount;
1745 xfs_fileoff_t fbno;
1746 xfs_filblks_t end;
1747 xfs_off_t isize;
1748 int error;
1749
1750 if (!xfs_is_reflink_inode(ip))
1751 return 0;
1752
1753 trace_xfs_reflink_unshare(ip, offset, len);
1754
1755 inode_dio_wait(VFS_I(ip));
1756
1757 /* Try to CoW the selected ranges */
1758 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001759 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001760 isize = i_size_read(VFS_I(ip));
1761 end = XFS_B_TO_FSB(mp, offset + len);
1762 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1763 if (error)
1764 goto out_unlock;
1765 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1766
1767 /* Wait for the IO to finish */
1768 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1769 if (error)
1770 goto out;
1771
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001772 /* Turn off the reflink flag if possible. */
1773 error = xfs_reflink_try_clear_inode_flag(ip);
1774 if (error)
1775 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001776
1777 return 0;
1778
1779out_unlock:
1780 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1781out:
1782 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1783 return error;
1784}