blob: d3cfae842237a0e62358e006d63ffa769ba20741 [file] [log] [blame]
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_mount.h"
27#include "xfs_defer.h"
28#include "xfs_da_format.h"
29#include "xfs_da_btree.h"
30#include "xfs_inode.h"
31#include "xfs_trans.h"
32#include "xfs_inode_item.h"
33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
35#include "xfs_error.h"
36#include "xfs_dir2.h"
37#include "xfs_dir2_priv.h"
38#include "xfs_ioctl.h"
39#include "xfs_trace.h"
40#include "xfs_log.h"
41#include "xfs_icache.h"
42#include "xfs_pnfs.h"
Darrick J. Wong174edb02016-10-03 09:11:39 -070043#include "xfs_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070044#include "xfs_refcount_btree.h"
45#include "xfs_refcount.h"
46#include "xfs_bmap_btree.h"
47#include "xfs_trans_space.h"
48#include "xfs_bit.h"
49#include "xfs_alloc.h"
50#include "xfs_quota_defs.h"
51#include "xfs_quota.h"
52#include "xfs_btree.h"
53#include "xfs_bmap_btree.h"
54#include "xfs_reflink.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070055#include "xfs_iomap.h"
Darrick J. Wong43caeb12016-10-03 09:11:35 -070056#include "xfs_rmap_btree.h"
Darrick J. Wong6fa164b2016-10-03 09:11:45 -070057#include "xfs_sb.h"
58#include "xfs_ag_resv.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070059
60/*
61 * Copy on Write of Shared Blocks
62 *
63 * XFS must preserve "the usual" file semantics even when two files share
64 * the same physical blocks. This means that a write to one file must not
65 * alter the blocks in a different file; the way that we'll do that is
66 * through the use of a copy-on-write mechanism. At a high level, that
67 * means that when we want to write to a shared block, we allocate a new
68 * block, write the data to the new block, and if that succeeds we map the
69 * new block into the file.
70 *
71 * XFS provides a "delayed allocation" mechanism that defers the allocation
72 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
73 * possible. This reduces fragmentation by enabling the filesystem to ask
74 * for bigger chunks less often, which is exactly what we want for CoW.
75 *
76 * The delalloc mechanism begins when the kernel wants to make a block
77 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
78 * create a delalloc mapping, which is a regular in-core extent, but without
79 * a real startblock. (For delalloc mappings, the startblock encodes both
80 * a flag that this is a delalloc mapping, and a worst-case estimate of how
81 * many blocks might be required to put the mapping into the BMBT.) delalloc
82 * mappings are a reservation against the free space in the filesystem;
83 * adjacent mappings can also be combined into fewer larger mappings.
84 *
85 * When dirty pages are being written out (typically in writepage), the
86 * delalloc reservations are converted into real mappings by allocating
87 * blocks and replacing the delalloc mapping with real ones. A delalloc
88 * mapping can be replaced by several real ones if the free space is
89 * fragmented.
90 *
91 * We want to adapt the delalloc mechanism for copy-on-write, since the
92 * write paths are similar. The first two steps (creating the reservation
93 * and allocating the blocks) are exactly the same as delalloc except that
94 * the mappings must be stored in a separate CoW fork because we do not want
95 * to disturb the mapping in the data fork until we're sure that the write
96 * succeeded. IO completion in this case is the process of removing the old
97 * mapping from the data fork and moving the new mapping from the CoW fork to
98 * the data fork. This will be discussed shortly.
99 *
100 * For now, unaligned directio writes will be bounced back to the page cache.
101 * Block-aligned directio writes will use the same mechanism as buffered
102 * writes.
103 *
104 * CoW remapping must be done after the data block write completes,
105 * because we don't want to destroy the old data fork map until we're sure
106 * the new block has been written. Since the new mappings are kept in a
107 * separate fork, we can simply iterate these mappings to find the ones
108 * that cover the file blocks that we just CoW'd. For each extent, simply
109 * unmap the corresponding range in the data fork, map the new range into
110 * the data fork, and remove the extent from the CoW fork.
111 *
112 * Since the remapping operation can be applied to an arbitrary file
113 * range, we record the need for the remap step as a flag in the ioend
114 * instead of declaring a new IO type. This is required for direct io
115 * because we only have ioend for the whole dio, and we have to be able to
116 * remember the presence of unwritten blocks and CoW blocks with a single
117 * ioend structure. Better yet, the more ground we can cover with one
118 * ioend, the better.
119 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700120
121/*
122 * Given an AG extent, find the lowest-numbered run of shared blocks
123 * within that range and return the range in fbno/flen. If
124 * find_end_of_shared is true, return the longest contiguous extent of
125 * shared blocks. If there are no shared extents, fbno and flen will
126 * be set to NULLAGBLOCK and 0, respectively.
127 */
128int
129xfs_reflink_find_shared(
130 struct xfs_mount *mp,
131 xfs_agnumber_t agno,
132 xfs_agblock_t agbno,
133 xfs_extlen_t aglen,
134 xfs_agblock_t *fbno,
135 xfs_extlen_t *flen,
136 bool find_end_of_shared)
137{
138 struct xfs_buf *agbp;
139 struct xfs_btree_cur *cur;
140 int error;
141
142 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
143 if (error)
144 return error;
145
146 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
147
148 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
149 find_end_of_shared);
150
151 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
152
153 xfs_buf_relse(agbp);
154 return error;
155}
156
157/*
158 * Trim the mapping to the next block where there's a change in the
159 * shared/unshared status. More specifically, this means that we
160 * find the lowest-numbered extent of shared blocks that coincides with
161 * the given block mapping. If the shared extent overlaps the start of
162 * the mapping, trim the mapping to the end of the shared extent. If
163 * the shared region intersects the mapping, trim the mapping to the
164 * start of the shared extent. If there are no shared regions that
165 * overlap, just return the original extent.
166 */
167int
168xfs_reflink_trim_around_shared(
169 struct xfs_inode *ip,
170 struct xfs_bmbt_irec *irec,
171 bool *shared,
172 bool *trimmed)
173{
174 xfs_agnumber_t agno;
175 xfs_agblock_t agbno;
176 xfs_extlen_t aglen;
177 xfs_agblock_t fbno;
178 xfs_extlen_t flen;
179 int error = 0;
180
181 /* Holes, unwritten, and delalloc extents cannot be shared */
182 if (!xfs_is_reflink_inode(ip) ||
183 ISUNWRITTEN(irec) ||
184 irec->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig62c5ac82016-10-20 15:52:00 +1100185 irec->br_startblock == DELAYSTARTBLOCK ||
186 isnullstartblock(irec->br_startblock)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700187 *shared = false;
188 return 0;
189 }
190
191 trace_xfs_reflink_trim_around_shared(ip, irec);
192
193 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
194 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
195 aglen = irec->br_blockcount;
196
197 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
198 aglen, &fbno, &flen, true);
199 if (error)
200 return error;
201
202 *shared = *trimmed = false;
203 if (fbno == NULLAGBLOCK) {
204 /* No shared blocks at all. */
205 return 0;
206 } else if (fbno == agbno) {
207 /*
208 * The start of this extent is shared. Truncate the
209 * mapping at the end of the shared region so that a
210 * subsequent iteration starts at the start of the
211 * unshared region.
212 */
213 irec->br_blockcount = flen;
214 *shared = true;
215 if (flen != aglen)
216 *trimmed = true;
217 return 0;
218 } else {
219 /*
220 * There's a shared extent midway through this extent.
221 * Truncate the mapping at the start of the shared
222 * extent so that a subsequent iteration starts at the
223 * start of the shared region.
224 */
225 irec->br_blockcount = fbno - agbno;
226 *trimmed = true;
227 return 0;
228 }
229}
230
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100231/*
232 * Trim the passed in imap to the next shared/unshared extent boundary, and
233 * if imap->br_startoff points to a shared extent reserve space for it in the
234 * COW fork. In this case *shared is set to true, else to false.
235 *
236 * Note that imap will always contain the block numbers for the existing blocks
237 * in the data fork, as the upper layers need them for read-modify-write
238 * operations.
239 */
240int
241xfs_reflink_reserve_cow(
Darrick J. Wong2a067052016-10-03 09:11:33 -0700242 struct xfs_inode *ip,
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100243 struct xfs_bmbt_irec *imap,
244 bool *shared)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700245{
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100246 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
247 struct xfs_bmbt_irec got;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100248 xfs_fileoff_t end_fsb, orig_end_fsb;
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100249 int error = 0;
250 bool eof = false, trimmed;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700251 xfs_extnum_t idx;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700252 xfs_extlen_t align;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700253
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100254 /*
255 * Search the COW fork extent list first. This serves two purposes:
256 * first this implement the speculative preallocation using cowextisze,
257 * so that we also unshared block adjacent to shared blocks instead
258 * of just the shared blocks themselves. Second the lookup in the
259 * extent list is generally faster than going out to the shared extent
260 * tree.
261 */
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100262
263 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
264 eof = true;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100265 if (!eof && got.br_startoff <= imap->br_startoff) {
266 trace_xfs_reflink_cow_found(ip, imap);
267 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700268
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100269 *shared = true;
270 return 0;
271 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700272
273 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100274 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700275 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100276 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700277
278 /* Not shared? Just report the (potentially capped) extent. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100279 if (!*shared)
280 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700281
282 /*
283 * Fork all the shared blocks from our write offset until the end of
284 * the extent.
285 */
286 error = xfs_qm_dqattach_locked(ip, 0);
287 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100288 return error;
289
290 end_fsb = orig_end_fsb = imap->br_startoff + imap->br_blockcount;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700291
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700292 align = xfs_eof_alignment(ip, xfs_get_cowextsz_hint(ip));
293 if (align)
294 end_fsb = roundup_64(end_fsb, align);
295
Darrick J. Wong2a067052016-10-03 09:11:33 -0700296retry:
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100297 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
Christoph Hellwig65c5f412016-11-24 11:39:44 +1100298 end_fsb - imap->br_startoff, &got, &idx, eof);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700299 switch (error) {
300 case 0:
301 break;
302 case -ENOSPC:
303 case -EDQUOT:
304 /* retry without any preallocation */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100305 trace_xfs_reflink_cow_enospc(ip, imap);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700306 if (end_fsb != orig_end_fsb) {
307 end_fsb = orig_end_fsb;
308 goto retry;
309 }
310 /*FALLTHRU*/
311 default:
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100312 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700313 }
314
Darrick J. Wong83104d42016-10-03 09:11:46 -0700315 if (end_fsb != orig_end_fsb)
316 xfs_inode_set_cowblocks_tag(ip);
317
Darrick J. Wong2a067052016-10-03 09:11:33 -0700318 trace_xfs_reflink_cow_alloc(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100319 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700320}
Darrick J. Wongef473662016-10-03 09:11:34 -0700321
Darrick J. Wong0613f162016-10-03 09:11:37 -0700322/* Allocate all CoW reservations covering a range of blocks in a file. */
323static int
324__xfs_reflink_allocate_cow(
325 struct xfs_inode *ip,
326 xfs_fileoff_t *offset_fsb,
327 xfs_fileoff_t end_fsb)
328{
329 struct xfs_mount *mp = ip->i_mount;
330 struct xfs_bmbt_irec imap;
331 struct xfs_defer_ops dfops;
332 struct xfs_trans *tp;
333 xfs_fsblock_t first_block;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700334 int nimaps = 1, error;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100335 bool shared;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700336
337 xfs_defer_init(&dfops, &first_block);
338
339 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
340 XFS_TRANS_RESERVE, &tp);
341 if (error)
342 return error;
343
344 xfs_ilock(ip, XFS_ILOCK_EXCL);
345
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100346 /* Read extent from the source file. */
347 nimaps = 1;
348 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
349 &imap, &nimaps, 0);
350 if (error)
351 goto out_unlock;
352 ASSERT(nimaps == 1);
353
354 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700355 if (error)
356 goto out_trans_cancel;
357
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100358 if (!shared) {
359 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700360 goto out_trans_cancel;
361 }
362
363 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100364 error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
Darrick J. Wong0613f162016-10-03 09:11:37 -0700365 XFS_BMAPI_COWFORK, &first_block,
366 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
367 &imap, &nimaps, &dfops);
368 if (error)
369 goto out_trans_cancel;
370
Darrick J. Wong0613f162016-10-03 09:11:37 -0700371 error = xfs_defer_finish(&tp, &dfops, NULL);
372 if (error)
373 goto out_trans_cancel;
374
375 error = xfs_trans_commit(tp);
376
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100377 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700378out_unlock:
379 xfs_iunlock(ip, XFS_ILOCK_EXCL);
380 return error;
381out_trans_cancel:
382 xfs_defer_cancel(&dfops);
383 xfs_trans_cancel(tp);
384 goto out_unlock;
385}
386
387/* Allocate all CoW reservations covering a part of a file. */
388int
389xfs_reflink_allocate_cow_range(
390 struct xfs_inode *ip,
391 xfs_off_t offset,
392 xfs_off_t count)
393{
394 struct xfs_mount *mp = ip->i_mount;
395 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
396 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
397 int error;
398
399 ASSERT(xfs_is_reflink_inode(ip));
400
401 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
402
403 /*
404 * Make sure that the dquots are there.
405 */
406 error = xfs_qm_dqattach(ip, 0);
407 if (error)
408 return error;
409
410 while (offset_fsb < end_fsb) {
411 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
412 if (error) {
413 trace_xfs_reflink_allocate_cow_range_error(ip, error,
414 _RET_IP_);
415 break;
416 }
417 }
418
419 return error;
420}
421
Darrick J. Wongef473662016-10-03 09:11:34 -0700422/*
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100423 * Find the CoW reservation for a given byte offset of a file.
Darrick J. Wongef473662016-10-03 09:11:34 -0700424 */
425bool
426xfs_reflink_find_cow_mapping(
427 struct xfs_inode *ip,
428 xfs_off_t offset,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100429 struct xfs_bmbt_irec *imap)
Darrick J. Wongef473662016-10-03 09:11:34 -0700430{
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100431 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
432 xfs_fileoff_t offset_fsb;
433 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700434 xfs_extnum_t idx;
435
436 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
437 ASSERT(xfs_is_reflink_inode(ip));
438
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100439 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
440 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Darrick J. Wongef473662016-10-03 09:11:34 -0700441 return false;
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100442 if (got.br_startoff > offset_fsb)
Darrick J. Wongef473662016-10-03 09:11:34 -0700443 return false;
444
445 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100446 &got);
447 *imap = got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700448 return true;
449}
450
451/*
452 * Trim an extent to end at the next CoW reservation past offset_fsb.
453 */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100454void
Darrick J. Wongef473662016-10-03 09:11:34 -0700455xfs_reflink_trim_irec_to_next_cow(
456 struct xfs_inode *ip,
457 xfs_fileoff_t offset_fsb,
458 struct xfs_bmbt_irec *imap)
459{
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
461 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700462 xfs_extnum_t idx;
463
464 if (!xfs_is_reflink_inode(ip))
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100465 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700466
467 /* Find the extent in the CoW fork. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100468 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
469 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700470
471 /* This is the extent before; try sliding up one. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100472 if (got.br_startoff < offset_fsb) {
473 if (!xfs_iext_get_extent(ifp, idx + 1, &got))
474 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700475 }
476
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100477 if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
478 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700479
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100480 imap->br_blockcount = got.br_startoff - imap->br_startoff;
Darrick J. Wongef473662016-10-03 09:11:34 -0700481 trace_xfs_reflink_trim_irec(ip, imap);
Darrick J. Wongef473662016-10-03 09:11:34 -0700482}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700483
484/*
485 * Cancel all pending CoW reservations for some block range of an inode.
486 */
487int
488xfs_reflink_cancel_cow_blocks(
489 struct xfs_inode *ip,
490 struct xfs_trans **tpp,
491 xfs_fileoff_t offset_fsb,
492 xfs_fileoff_t end_fsb)
493{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100494 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
495 struct xfs_bmbt_irec got, prev, del;
496 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700497 xfs_fsblock_t firstfsb;
498 struct xfs_defer_ops dfops;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100499 int error = 0, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700500
501 if (!xfs_is_reflink_inode(ip))
502 return 0;
503
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100504 xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
505 &got, &prev);
506 if (eof)
507 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700508
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100509 while (got.br_startoff < end_fsb) {
510 del = got;
511 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
512 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700513
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100514 if (isnullstartblock(del.br_startblock)) {
515 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
516 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700517 if (error)
518 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700519 } else {
520 xfs_trans_ijoin(*tpp, ip, 0);
521 xfs_defer_init(&dfops, &firstfsb);
522
Darrick J. Wong174edb02016-10-03 09:11:39 -0700523 /* Free the CoW orphan record. */
524 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100525 &dfops, del.br_startblock,
526 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700527 if (error)
528 break;
529
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700530 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100531 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700532 NULL);
533
534 /* Update quota accounting */
535 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100536 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700537
538 /* Roll the transaction */
539 error = xfs_defer_finish(tpp, &dfops, ip);
540 if (error) {
541 xfs_defer_cancel(&dfops);
542 break;
543 }
544
545 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100546 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700547 }
548
Eric Sandeen5d829302016-11-08 12:59:42 +1100549 if (++idx >= xfs_iext_count(ifp))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100550 break;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100551 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700552 }
553
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100554 /* clear tag if cow fork is emptied */
555 if (!ifp->if_bytes)
556 xfs_inode_clear_cowblocks_tag(ip);
557
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700558 return error;
559}
560
561/*
562 * Cancel all pending CoW reservations for some byte range of an inode.
563 */
564int
565xfs_reflink_cancel_cow_range(
566 struct xfs_inode *ip,
567 xfs_off_t offset,
568 xfs_off_t count)
569{
570 struct xfs_trans *tp;
571 xfs_fileoff_t offset_fsb;
572 xfs_fileoff_t end_fsb;
573 int error;
574
575 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100576 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700577
578 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
579 if (count == NULLFILEOFF)
580 end_fsb = NULLFILEOFF;
581 else
582 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
583
584 /* Start a rolling transaction to remove the mappings */
585 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
586 0, 0, 0, &tp);
587 if (error)
588 goto out;
589
590 xfs_ilock(ip, XFS_ILOCK_EXCL);
591 xfs_trans_ijoin(tp, ip, 0);
592
593 /* Scrape out the old CoW reservations */
594 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
595 if (error)
596 goto out_cancel;
597
598 error = xfs_trans_commit(tp);
599
600 xfs_iunlock(ip, XFS_ILOCK_EXCL);
601 return error;
602
603out_cancel:
604 xfs_trans_cancel(tp);
605 xfs_iunlock(ip, XFS_ILOCK_EXCL);
606out:
607 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
608 return error;
609}
610
611/*
612 * Remap parts of a file's data fork after a successful CoW.
613 */
614int
615xfs_reflink_end_cow(
616 struct xfs_inode *ip,
617 xfs_off_t offset,
618 xfs_off_t count)
619{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100620 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
621 struct xfs_bmbt_irec got, prev, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700622 struct xfs_trans *tp;
623 xfs_fileoff_t offset_fsb;
624 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700625 xfs_fsblock_t firstfsb;
626 struct xfs_defer_ops dfops;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100627 int error, eof = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700628 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700629 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100630 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700631
632 trace_xfs_reflink_end_cow(ip, offset, count);
633
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100634 /* No COW extents? That's easy! */
635 if (ifp->if_bytes == 0)
636 return 0;
637
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700638 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
639 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700640
641 /* Start a rolling transaction to switch the mappings */
642 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
643 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
644 resblks, 0, 0, &tp);
645 if (error)
646 goto out;
647
648 xfs_ilock(ip, XFS_ILOCK_EXCL);
649 xfs_trans_ijoin(tp, ip, 0);
650
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100651 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
652 &got, &prev);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700653
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100654 /* If there is a hole at end_fsb - 1 go to the previous extent */
655 if (eof || got.br_startoff > end_fsb) {
656 ASSERT(idx > 0);
657 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
658 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700659
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100660 /* Walk backwards until we're out of the I/O range... */
661 while (got.br_startoff + got.br_blockcount > offset_fsb) {
662 del = got;
663 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
664
665 /* Extent delete may have bumped idx forward */
666 if (!del.br_blockcount) {
667 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700668 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700669 }
670
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100671 ASSERT(!isnullstartblock(got.br_startblock));
672
673 /* Unmap the old blocks in the data fork. */
674 xfs_defer_init(&dfops, &firstfsb);
675 rlen = del.br_blockcount;
676 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
677 &firstfsb, &dfops);
678 if (error)
679 goto out_defer;
680
681 /* Trim the extent to whatever got unmapped. */
682 if (rlen) {
683 xfs_trim_extent(&del, del.br_startoff + rlen,
684 del.br_blockcount - rlen);
685 }
686 trace_xfs_reflink_cow_remap(ip, &del);
687
688 /* Free the CoW orphan record. */
689 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
690 del.br_startblock, del.br_blockcount);
691 if (error)
692 goto out_defer;
693
694 /* Map the new blocks into the data fork. */
695 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
696 if (error)
697 goto out_defer;
698
699 /* Remove the mapping from the CoW fork. */
700 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
701
702 error = xfs_defer_finish(&tp, &dfops, ip);
703 if (error)
704 goto out_defer;
705
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700706next_extent:
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100707 if (idx < 0)
708 break;
709 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700710 }
711
712 error = xfs_trans_commit(tp);
713 xfs_iunlock(ip, XFS_ILOCK_EXCL);
714 if (error)
715 goto out;
716 return 0;
717
718out_defer:
719 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700720 xfs_trans_cancel(tp);
721 xfs_iunlock(ip, XFS_ILOCK_EXCL);
722out:
723 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
724 return error;
725}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700726
727/*
728 * Free leftover CoW reservations that didn't get cleaned out.
729 */
730int
731xfs_reflink_recover_cow(
732 struct xfs_mount *mp)
733{
734 xfs_agnumber_t agno;
735 int error = 0;
736
737 if (!xfs_sb_version_hasreflink(&mp->m_sb))
738 return 0;
739
740 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
741 error = xfs_refcount_recover_cow_leftovers(mp, agno);
742 if (error)
743 break;
744 }
745
746 return error;
747}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700748
749/*
750 * Reflinking (Block) Ranges of Two Files Together
751 *
752 * First, ensure that the reflink flag is set on both inodes. The flag is an
753 * optimization to avoid unnecessary refcount btree lookups in the write path.
754 *
755 * Now we can iteratively remap the range of extents (and holes) in src to the
756 * corresponding ranges in dest. Let drange and srange denote the ranges of
757 * logical blocks in dest and src touched by the reflink operation.
758 *
759 * While the length of drange is greater than zero,
760 * - Read src's bmbt at the start of srange ("imap")
761 * - If imap doesn't exist, make imap appear to start at the end of srange
762 * with zero length.
763 * - If imap starts before srange, advance imap to start at srange.
764 * - If imap goes beyond srange, truncate imap to end at the end of srange.
765 * - Punch (imap start - srange start + imap len) blocks from dest at
766 * offset (drange start).
767 * - If imap points to a real range of pblks,
768 * > Increase the refcount of the imap's pblks
769 * > Map imap's pblks into dest at the offset
770 * (drange start + imap start - srange start)
771 * - Advance drange and srange by (imap start - srange start + imap len)
772 *
773 * Finally, if the reflink made dest longer, update both the in-core and
774 * on-disk file sizes.
775 *
776 * ASCII Art Demonstration:
777 *
778 * Let's say we want to reflink this source file:
779 *
780 * ----SSSSSSS-SSSSS----SSSSSS (src file)
781 * <-------------------->
782 *
783 * into this destination file:
784 *
785 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
786 * <-------------------->
787 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
788 * Observe that the range has different logical offsets in either file.
789 *
790 * Consider that the first extent in the source file doesn't line up with our
791 * reflink range. Unmapping and remapping are separate operations, so we can
792 * unmap more blocks from the destination file than we remap.
793 *
794 * ----SSSSSSS-SSSSS----SSSSSS
795 * <------->
796 * --DDDDD---------DDDDD--DDD
797 * <------->
798 *
799 * Now remap the source extent into the destination file:
800 *
801 * ----SSSSSSS-SSSSS----SSSSSS
802 * <------->
803 * --DDDDD--SSSSSSSDDDDD--DDD
804 * <------->
805 *
806 * Do likewise with the second hole and extent in our range. Holes in the
807 * unmap range don't affect our operation.
808 *
809 * ----SSSSSSS-SSSSS----SSSSSS
810 * <---->
811 * --DDDDD--SSSSSSS-SSSSS-DDD
812 * <---->
813 *
814 * Finally, unmap and remap part of the third extent. This will increase the
815 * size of the destination file.
816 *
817 * ----SSSSSSS-SSSSS----SSSSSS
818 * <----->
819 * --DDDDD--SSSSSSS-SSSSS----SSS
820 * <----->
821 *
822 * Once we update the destination file's i_size, we're done.
823 */
824
825/*
826 * Ensure the reflink bit is set in both inodes.
827 */
828STATIC int
829xfs_reflink_set_inode_flag(
830 struct xfs_inode *src,
831 struct xfs_inode *dest)
832{
833 struct xfs_mount *mp = src->i_mount;
834 int error;
835 struct xfs_trans *tp;
836
837 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
838 return 0;
839
840 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
841 if (error)
842 goto out_error;
843
844 /* Lock both files against IO */
845 if (src->i_ino == dest->i_ino)
846 xfs_ilock(src, XFS_ILOCK_EXCL);
847 else
848 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
849
850 if (!xfs_is_reflink_inode(src)) {
851 trace_xfs_reflink_set_inode_flag(src);
852 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
853 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
854 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
855 xfs_ifork_init_cow(src);
856 } else
857 xfs_iunlock(src, XFS_ILOCK_EXCL);
858
859 if (src->i_ino == dest->i_ino)
860 goto commit_flags;
861
862 if (!xfs_is_reflink_inode(dest)) {
863 trace_xfs_reflink_set_inode_flag(dest);
864 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
865 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
866 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
867 xfs_ifork_init_cow(dest);
868 } else
869 xfs_iunlock(dest, XFS_ILOCK_EXCL);
870
871commit_flags:
872 error = xfs_trans_commit(tp);
873 if (error)
874 goto out_error;
875 return error;
876
877out_error:
878 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
879 return error;
880}
881
882/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700883 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700884 */
885STATIC int
886xfs_reflink_update_dest(
887 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700888 xfs_off_t newlen,
889 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700890{
891 struct xfs_mount *mp = dest->i_mount;
892 struct xfs_trans *tp;
893 int error;
894
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700895 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700896 return 0;
897
898 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
899 if (error)
900 goto out_error;
901
902 xfs_ilock(dest, XFS_ILOCK_EXCL);
903 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
904
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700905 if (newlen > i_size_read(VFS_I(dest))) {
906 trace_xfs_reflink_update_inode_size(dest, newlen);
907 i_size_write(VFS_I(dest), newlen);
908 dest->i_d.di_size = newlen;
909 }
910
911 if (cowextsize) {
912 dest->i_d.di_cowextsize = cowextsize;
913 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
914 }
915
Darrick J. Wong862bb362016-10-03 09:11:40 -0700916 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
917
918 error = xfs_trans_commit(tp);
919 if (error)
920 goto out_error;
921 return error;
922
923out_error:
924 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
925 return error;
926}
927
928/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700929 * Do we have enough reserve in this AG to handle a reflink? The refcount
930 * btree already reserved all the space it needs, but the rmap btree can grow
931 * infinitely, so we won't allow more reflinks when the AG is down to the
932 * btree reserves.
933 */
934static int
935xfs_reflink_ag_has_free_space(
936 struct xfs_mount *mp,
937 xfs_agnumber_t agno)
938{
939 struct xfs_perag *pag;
940 int error = 0;
941
942 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
943 return 0;
944
945 pag = xfs_perag_get(mp, agno);
946 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
947 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
948 error = -ENOSPC;
949 xfs_perag_put(pag);
950 return error;
951}
952
953/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700954 * Unmap a range of blocks from a file, then map other blocks into the hole.
955 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
956 * The extent irec is mapped into dest at irec->br_startoff.
957 */
958STATIC int
959xfs_reflink_remap_extent(
960 struct xfs_inode *ip,
961 struct xfs_bmbt_irec *irec,
962 xfs_fileoff_t destoff,
963 xfs_off_t new_isize)
964{
965 struct xfs_mount *mp = ip->i_mount;
966 struct xfs_trans *tp;
967 xfs_fsblock_t firstfsb;
968 unsigned int resblks;
969 struct xfs_defer_ops dfops;
970 struct xfs_bmbt_irec uirec;
971 bool real_extent;
972 xfs_filblks_t rlen;
973 xfs_filblks_t unmap_len;
974 xfs_off_t newlen;
975 int error;
976
977 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
978 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
979
980 /* Only remap normal extents. */
981 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
982 irec->br_startblock != DELAYSTARTBLOCK &&
983 !ISUNWRITTEN(irec));
984
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700985 /* No reflinking if we're low on space */
986 if (real_extent) {
987 error = xfs_reflink_ag_has_free_space(mp,
988 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
989 if (error)
990 goto out;
991 }
992
Darrick J. Wong862bb362016-10-03 09:11:40 -0700993 /* Start a rolling transaction to switch the mappings */
994 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
995 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
996 if (error)
997 goto out;
998
999 xfs_ilock(ip, XFS_ILOCK_EXCL);
1000 xfs_trans_ijoin(tp, ip, 0);
1001
1002 /* If we're not just clearing space, then do we have enough quota? */
1003 if (real_extent) {
1004 error = xfs_trans_reserve_quota_nblks(tp, ip,
1005 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1006 if (error)
1007 goto out_cancel;
1008 }
1009
1010 trace_xfs_reflink_remap(ip, irec->br_startoff,
1011 irec->br_blockcount, irec->br_startblock);
1012
1013 /* Unmap the old blocks in the data fork. */
1014 rlen = unmap_len;
1015 while (rlen) {
1016 xfs_defer_init(&dfops, &firstfsb);
1017 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1018 &firstfsb, &dfops);
1019 if (error)
1020 goto out_defer;
1021
1022 /*
1023 * Trim the extent to whatever got unmapped.
1024 * Remember, bunmapi works backwards.
1025 */
1026 uirec.br_startblock = irec->br_startblock + rlen;
1027 uirec.br_startoff = irec->br_startoff + rlen;
1028 uirec.br_blockcount = unmap_len - rlen;
1029 unmap_len = rlen;
1030
1031 /* If this isn't a real mapping, we're done. */
1032 if (!real_extent || uirec.br_blockcount == 0)
1033 goto next_extent;
1034
1035 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1036 uirec.br_blockcount, uirec.br_startblock);
1037
1038 /* Update the refcount tree */
1039 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1040 if (error)
1041 goto out_defer;
1042
1043 /* Map the new blocks into the data fork. */
1044 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1045 if (error)
1046 goto out_defer;
1047
1048 /* Update quota accounting. */
1049 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1050 uirec.br_blockcount);
1051
1052 /* Update dest isize if needed. */
1053 newlen = XFS_FSB_TO_B(mp,
1054 uirec.br_startoff + uirec.br_blockcount);
1055 newlen = min_t(xfs_off_t, newlen, new_isize);
1056 if (newlen > i_size_read(VFS_I(ip))) {
1057 trace_xfs_reflink_update_inode_size(ip, newlen);
1058 i_size_write(VFS_I(ip), newlen);
1059 ip->i_d.di_size = newlen;
1060 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1061 }
1062
1063next_extent:
1064 /* Process all the deferred stuff. */
1065 error = xfs_defer_finish(&tp, &dfops, ip);
1066 if (error)
1067 goto out_defer;
1068 }
1069
1070 error = xfs_trans_commit(tp);
1071 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1072 if (error)
1073 goto out;
1074 return 0;
1075
1076out_defer:
1077 xfs_defer_cancel(&dfops);
1078out_cancel:
1079 xfs_trans_cancel(tp);
1080 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1081out:
1082 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1083 return error;
1084}
1085
1086/*
1087 * Iteratively remap one file's extents (and holes) to another's.
1088 */
1089STATIC int
1090xfs_reflink_remap_blocks(
1091 struct xfs_inode *src,
1092 xfs_fileoff_t srcoff,
1093 struct xfs_inode *dest,
1094 xfs_fileoff_t destoff,
1095 xfs_filblks_t len,
1096 xfs_off_t new_isize)
1097{
1098 struct xfs_bmbt_irec imap;
1099 int nimaps;
1100 int error = 0;
1101 xfs_filblks_t range_len;
1102
1103 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1104 while (len) {
1105 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1106 dest, destoff);
1107 /* Read extent from the source file */
1108 nimaps = 1;
1109 xfs_ilock(src, XFS_ILOCK_EXCL);
1110 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1111 xfs_iunlock(src, XFS_ILOCK_EXCL);
1112 if (error)
1113 goto err;
1114 ASSERT(nimaps == 1);
1115
1116 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1117 &imap);
1118
1119 /* Translate imap into the destination file. */
1120 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1121 imap.br_startoff += destoff - srcoff;
1122
1123 /* Clear dest from destoff to the end of imap and map it in. */
1124 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1125 new_isize);
1126 if (error)
1127 goto err;
1128
1129 if (fatal_signal_pending(current)) {
1130 error = -EINTR;
1131 goto err;
1132 }
1133
1134 /* Advance drange/srange */
1135 srcoff += range_len;
1136 destoff += range_len;
1137 len -= range_len;
1138 }
1139
1140 return 0;
1141
1142err:
1143 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1144 return error;
1145}
1146
1147/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001148 * Read a page's worth of file data into the page cache. Return the page
1149 * locked.
1150 */
1151static struct page *
1152xfs_get_page(
1153 struct inode *inode,
1154 xfs_off_t offset)
1155{
1156 struct address_space *mapping;
1157 struct page *page;
1158 pgoff_t n;
1159
1160 n = offset >> PAGE_SHIFT;
1161 mapping = inode->i_mapping;
1162 page = read_mapping_page(mapping, n, NULL);
1163 if (IS_ERR(page))
1164 return page;
1165 if (!PageUptodate(page)) {
1166 put_page(page);
1167 return ERR_PTR(-EIO);
1168 }
1169 lock_page(page);
1170 return page;
1171}
1172
1173/*
1174 * Compare extents of two files to see if they are the same.
1175 */
1176static int
1177xfs_compare_extents(
1178 struct inode *src,
1179 xfs_off_t srcoff,
1180 struct inode *dest,
1181 xfs_off_t destoff,
1182 xfs_off_t len,
1183 bool *is_same)
1184{
1185 xfs_off_t src_poff;
1186 xfs_off_t dest_poff;
1187 void *src_addr;
1188 void *dest_addr;
1189 struct page *src_page;
1190 struct page *dest_page;
1191 xfs_off_t cmp_len;
1192 bool same;
1193 int error;
1194
1195 error = -EINVAL;
1196 same = true;
1197 while (len) {
1198 src_poff = srcoff & (PAGE_SIZE - 1);
1199 dest_poff = destoff & (PAGE_SIZE - 1);
1200 cmp_len = min(PAGE_SIZE - src_poff,
1201 PAGE_SIZE - dest_poff);
1202 cmp_len = min(cmp_len, len);
1203 ASSERT(cmp_len > 0);
1204
1205 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1206 XFS_I(dest), destoff);
1207
1208 src_page = xfs_get_page(src, srcoff);
1209 if (IS_ERR(src_page)) {
1210 error = PTR_ERR(src_page);
1211 goto out_error;
1212 }
1213 dest_page = xfs_get_page(dest, destoff);
1214 if (IS_ERR(dest_page)) {
1215 error = PTR_ERR(dest_page);
1216 unlock_page(src_page);
1217 put_page(src_page);
1218 goto out_error;
1219 }
1220 src_addr = kmap_atomic(src_page);
1221 dest_addr = kmap_atomic(dest_page);
1222
1223 flush_dcache_page(src_page);
1224 flush_dcache_page(dest_page);
1225
1226 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1227 same = false;
1228
1229 kunmap_atomic(dest_addr);
1230 kunmap_atomic(src_addr);
1231 unlock_page(dest_page);
1232 unlock_page(src_page);
1233 put_page(dest_page);
1234 put_page(src_page);
1235
1236 if (!same)
1237 break;
1238
1239 srcoff += cmp_len;
1240 destoff += cmp_len;
1241 len -= cmp_len;
1242 }
1243
1244 *is_same = same;
1245 return 0;
1246
1247out_error:
1248 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1249 return error;
1250}
1251
1252/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001253 * Link a range of blocks from one file to another.
1254 */
1255int
1256xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001257 struct file *file_in,
1258 loff_t pos_in,
1259 struct file *file_out,
1260 loff_t pos_out,
1261 u64 len,
1262 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001263{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001264 struct inode *inode_in = file_inode(file_in);
1265 struct xfs_inode *src = XFS_I(inode_in);
1266 struct inode *inode_out = file_inode(file_out);
1267 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001268 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001269 loff_t bs = inode_out->i_sb->s_blocksize;
1270 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001271 xfs_fileoff_t sfsbno, dfsbno;
1272 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001273 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001274 loff_t isize;
1275 ssize_t ret;
1276 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001277
1278 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1279 return -EOPNOTSUPP;
1280
1281 if (XFS_FORCED_SHUTDOWN(mp))
1282 return -EIO;
1283
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001284 /* Lock both files against IO */
1285 if (same_inode) {
1286 xfs_ilock(src, XFS_IOLOCK_EXCL);
1287 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1288 } else {
1289 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1290 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1291 }
1292
1293 /* Don't touch certain kinds of inodes */
1294 ret = -EPERM;
1295 if (IS_IMMUTABLE(inode_out))
1296 goto out_unlock;
1297
1298 ret = -ETXTBSY;
1299 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1300 goto out_unlock;
1301
1302
1303 /* Don't reflink dirs, pipes, sockets... */
1304 ret = -EISDIR;
1305 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1306 goto out_unlock;
1307 ret = -EINVAL;
1308 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1309 goto out_unlock;
1310 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1311 goto out_unlock;
1312
Darrick J. Wong862bb362016-10-03 09:11:40 -07001313 /* Don't reflink realtime inodes */
1314 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001315 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001316
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001317 /* Don't share DAX file data for now. */
1318 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1319 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001320
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001321 /* Are we going all the way to the end? */
1322 isize = i_size_read(inode_in);
1323 if (isize == 0) {
1324 ret = 0;
1325 goto out_unlock;
1326 }
1327
1328 if (len == 0)
1329 len = isize - pos_in;
1330
1331 /* Ensure offsets don't wrap and the input is inside i_size */
1332 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1333 pos_in + len > isize)
1334 goto out_unlock;
1335
1336 /* Don't allow dedupe past EOF in the dest file */
1337 if (is_dedupe) {
1338 loff_t disize;
1339
1340 disize = i_size_read(inode_out);
1341 if (pos_out >= disize || pos_out + len > disize)
1342 goto out_unlock;
1343 }
1344
1345 /* If we're linking to EOF, continue to the block boundary. */
1346 if (pos_in + len == isize)
1347 blen = ALIGN(isize, bs) - pos_in;
1348 else
1349 blen = len;
1350
1351 /* Only reflink if we're aligned to block boundaries */
1352 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1353 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1354 goto out_unlock;
1355
1356 /* Don't allow overlapped reflink within the same file */
1357 if (same_inode) {
1358 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1359 goto out_unlock;
1360 }
1361
1362 /* Wait for the completion of any pending IOs on both files */
1363 inode_dio_wait(inode_in);
1364 if (!same_inode)
1365 inode_dio_wait(inode_out);
1366
1367 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1368 pos_in, pos_in + len - 1);
1369 if (ret)
1370 goto out_unlock;
1371
1372 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1373 pos_out, pos_out + len - 1);
1374 if (ret)
1375 goto out_unlock;
1376
1377 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001378
Darrick J. Wongcc714662016-10-03 09:11:41 -07001379 /*
1380 * Check that the extents are the same.
1381 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001382 if (is_dedupe) {
1383 bool is_same = false;
1384
1385 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1386 len, &is_same);
1387 if (ret)
1388 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001389 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001390 ret = -EBADE;
1391 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001392 }
1393 }
1394
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001395 ret = xfs_reflink_set_inode_flag(src, dest);
1396 if (ret)
1397 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001398
1399 /*
1400 * Invalidate the page cache so that we can clear any CoW mappings
1401 * in the destination file.
1402 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001403 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1404 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001405
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001406 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1407 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001408 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001409 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1410 pos_out + len);
1411 if (ret)
1412 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001413
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001414 /*
1415 * Carry the cowextsize hint from src to dest if we're sharing the
1416 * entire source file to the entire destination file, the source file
1417 * has a cowextsize hint, and the destination file does not.
1418 */
1419 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001420 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001421 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001422 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001423 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1424 cowextsize = src->i_d.di_cowextsize;
1425
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001426 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001427
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001428out_unlock:
1429 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1430 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1431 if (src->i_ino != dest->i_ino) {
1432 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1433 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1434 }
1435 if (ret)
1436 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1437 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001438}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001439
1440/*
1441 * The user wants to preemptively CoW all shared blocks in this file,
1442 * which enables us to turn off the reflink flag. Iterate all
1443 * extents which are not prealloc/delalloc to see which ranges are
1444 * mentioned in the refcount tree, then read those blocks into the
1445 * pagecache, dirty them, fsync them back out, and then we can update
1446 * the inode flag. What happens if we run out of memory? :)
1447 */
1448STATIC int
1449xfs_reflink_dirty_extents(
1450 struct xfs_inode *ip,
1451 xfs_fileoff_t fbno,
1452 xfs_filblks_t end,
1453 xfs_off_t isize)
1454{
1455 struct xfs_mount *mp = ip->i_mount;
1456 xfs_agnumber_t agno;
1457 xfs_agblock_t agbno;
1458 xfs_extlen_t aglen;
1459 xfs_agblock_t rbno;
1460 xfs_extlen_t rlen;
1461 xfs_off_t fpos;
1462 xfs_off_t flen;
1463 struct xfs_bmbt_irec map[2];
1464 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001465 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001466
1467 while (end - fbno > 0) {
1468 nmaps = 1;
1469 /*
1470 * Look for extents in the file. Skip holes, delalloc, or
1471 * unwritten extents; they can't be reflinked.
1472 */
1473 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1474 if (error)
1475 goto out;
1476 if (nmaps == 0)
1477 break;
1478 if (map[0].br_startblock == HOLESTARTBLOCK ||
1479 map[0].br_startblock == DELAYSTARTBLOCK ||
1480 ISUNWRITTEN(&map[0]))
1481 goto next;
1482
1483 map[1] = map[0];
1484 while (map[1].br_blockcount) {
1485 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1486 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1487 aglen = map[1].br_blockcount;
1488
1489 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1490 &rbno, &rlen, true);
1491 if (error)
1492 goto out;
1493 if (rbno == NULLAGBLOCK)
1494 break;
1495
1496 /* Dirty the pages */
1497 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1498 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1499 (rbno - agbno));
1500 flen = XFS_FSB_TO_B(mp, rlen);
1501 if (fpos + flen > isize)
1502 flen = isize - fpos;
1503 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1504 &xfs_iomap_ops);
1505 xfs_ilock(ip, XFS_ILOCK_EXCL);
1506 if (error)
1507 goto out;
1508
1509 map[1].br_blockcount -= (rbno - agbno + rlen);
1510 map[1].br_startoff += (rbno - agbno + rlen);
1511 map[1].br_startblock += (rbno - agbno + rlen);
1512 }
1513
1514next:
1515 fbno = map[0].br_startoff + map[0].br_blockcount;
1516 }
1517out:
1518 return error;
1519}
1520
1521/* Clear the inode reflink flag if there are no shared extents. */
1522int
1523xfs_reflink_clear_inode_flag(
1524 struct xfs_inode *ip,
1525 struct xfs_trans **tpp)
1526{
1527 struct xfs_mount *mp = ip->i_mount;
1528 xfs_fileoff_t fbno;
1529 xfs_filblks_t end;
1530 xfs_agnumber_t agno;
1531 xfs_agblock_t agbno;
1532 xfs_extlen_t aglen;
1533 xfs_agblock_t rbno;
1534 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001535 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001536 int nmaps;
1537 int error = 0;
1538
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001539 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001540
1541 fbno = 0;
1542 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1543 while (end - fbno > 0) {
1544 nmaps = 1;
1545 /*
1546 * Look for extents in the file. Skip holes, delalloc, or
1547 * unwritten extents; they can't be reflinked.
1548 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001549 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001550 if (error)
1551 return error;
1552 if (nmaps == 0)
1553 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001554 if (map.br_startblock == HOLESTARTBLOCK ||
1555 map.br_startblock == DELAYSTARTBLOCK ||
1556 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001557 goto next;
1558
Darrick J. Wong024adf42016-10-10 16:47:40 +11001559 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1560 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1561 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001562
Darrick J. Wong024adf42016-10-10 16:47:40 +11001563 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1564 &rbno, &rlen, false);
1565 if (error)
1566 return error;
1567 /* Is there still a shared block here? */
1568 if (rbno != NULLAGBLOCK)
1569 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001570next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001571 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001572 }
1573
1574 /*
1575 * We didn't find any shared blocks so turn off the reflink flag.
1576 * First, get rid of any leftover CoW mappings.
1577 */
1578 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1579 if (error)
1580 return error;
1581
1582 /* Clear the inode flag. */
1583 trace_xfs_reflink_unset_inode_flag(ip);
1584 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001585 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001586 xfs_trans_ijoin(*tpp, ip, 0);
1587 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1588
1589 return error;
1590}
1591
1592/*
1593 * Clear the inode reflink flag if there are no shared extents and the size
1594 * hasn't changed.
1595 */
1596STATIC int
1597xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001598 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001599{
1600 struct xfs_mount *mp = ip->i_mount;
1601 struct xfs_trans *tp;
1602 int error = 0;
1603
1604 /* Start a rolling transaction to remove the mappings */
1605 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1606 if (error)
1607 return error;
1608
1609 xfs_ilock(ip, XFS_ILOCK_EXCL);
1610 xfs_trans_ijoin(tp, ip, 0);
1611
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001612 error = xfs_reflink_clear_inode_flag(ip, &tp);
1613 if (error)
1614 goto cancel;
1615
1616 error = xfs_trans_commit(tp);
1617 if (error)
1618 goto out;
1619
1620 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1621 return 0;
1622cancel:
1623 xfs_trans_cancel(tp);
1624out:
1625 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1626 return error;
1627}
1628
1629/*
1630 * Pre-COW all shared blocks within a given byte range of a file and turn off
1631 * the reflink flag if we unshare all of the file's blocks.
1632 */
1633int
1634xfs_reflink_unshare(
1635 struct xfs_inode *ip,
1636 xfs_off_t offset,
1637 xfs_off_t len)
1638{
1639 struct xfs_mount *mp = ip->i_mount;
1640 xfs_fileoff_t fbno;
1641 xfs_filblks_t end;
1642 xfs_off_t isize;
1643 int error;
1644
1645 if (!xfs_is_reflink_inode(ip))
1646 return 0;
1647
1648 trace_xfs_reflink_unshare(ip, offset, len);
1649
1650 inode_dio_wait(VFS_I(ip));
1651
1652 /* Try to CoW the selected ranges */
1653 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001654 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001655 isize = i_size_read(VFS_I(ip));
1656 end = XFS_B_TO_FSB(mp, offset + len);
1657 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1658 if (error)
1659 goto out_unlock;
1660 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1661
1662 /* Wait for the IO to finish */
1663 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1664 if (error)
1665 goto out;
1666
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001667 /* Turn off the reflink flag if possible. */
1668 error = xfs_reflink_try_clear_inode_flag(ip);
1669 if (error)
1670 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001671
1672 return 0;
1673
1674out_unlock:
1675 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1676out:
1677 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1678 return error;
1679}