blob: 56372bee08c534cfcae7aeb7b58437a6fc4372bf [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);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100495 struct xfs_bmbt_irec got, del;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100496 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700497 xfs_fsblock_t firstfsb;
498 struct xfs_defer_ops dfops;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100499 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700500
501 if (!xfs_is_reflink_inode(ip))
502 return 0;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100503 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100504 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700505
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100506 while (got.br_startoff < end_fsb) {
507 del = got;
508 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
509 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700510
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100511 if (isnullstartblock(del.br_startblock)) {
512 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
513 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700514 if (error)
515 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700516 } else {
517 xfs_trans_ijoin(*tpp, ip, 0);
518 xfs_defer_init(&dfops, &firstfsb);
519
Darrick J. Wong174edb02016-10-03 09:11:39 -0700520 /* Free the CoW orphan record. */
521 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100522 &dfops, del.br_startblock,
523 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700524 if (error)
525 break;
526
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700527 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100528 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700529 NULL);
530
531 /* Update quota accounting */
532 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100533 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700534
535 /* Roll the transaction */
536 error = xfs_defer_finish(tpp, &dfops, ip);
537 if (error) {
538 xfs_defer_cancel(&dfops);
539 break;
540 }
541
542 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100543 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700544 }
545
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100546 if (!xfs_iext_get_extent(ifp, ++idx, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100547 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700548 }
549
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100550 /* clear tag if cow fork is emptied */
551 if (!ifp->if_bytes)
552 xfs_inode_clear_cowblocks_tag(ip);
553
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700554 return error;
555}
556
557/*
558 * Cancel all pending CoW reservations for some byte range of an inode.
559 */
560int
561xfs_reflink_cancel_cow_range(
562 struct xfs_inode *ip,
563 xfs_off_t offset,
564 xfs_off_t count)
565{
566 struct xfs_trans *tp;
567 xfs_fileoff_t offset_fsb;
568 xfs_fileoff_t end_fsb;
569 int error;
570
571 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100572 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700573
574 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
575 if (count == NULLFILEOFF)
576 end_fsb = NULLFILEOFF;
577 else
578 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
579
580 /* Start a rolling transaction to remove the mappings */
581 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
582 0, 0, 0, &tp);
583 if (error)
584 goto out;
585
586 xfs_ilock(ip, XFS_ILOCK_EXCL);
587 xfs_trans_ijoin(tp, ip, 0);
588
589 /* Scrape out the old CoW reservations */
590 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
591 if (error)
592 goto out_cancel;
593
594 error = xfs_trans_commit(tp);
595
596 xfs_iunlock(ip, XFS_ILOCK_EXCL);
597 return error;
598
599out_cancel:
600 xfs_trans_cancel(tp);
601 xfs_iunlock(ip, XFS_ILOCK_EXCL);
602out:
603 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
604 return error;
605}
606
607/*
608 * Remap parts of a file's data fork after a successful CoW.
609 */
610int
611xfs_reflink_end_cow(
612 struct xfs_inode *ip,
613 xfs_off_t offset,
614 xfs_off_t count)
615{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100616 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100617 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700618 struct xfs_trans *tp;
619 xfs_fileoff_t offset_fsb;
620 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700621 xfs_fsblock_t firstfsb;
622 struct xfs_defer_ops dfops;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100623 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700624 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700625 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100626 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700627
628 trace_xfs_reflink_end_cow(ip, offset, count);
629
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100630 /* No COW extents? That's easy! */
631 if (ifp->if_bytes == 0)
632 return 0;
633
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700634 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
635 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700636
637 /* Start a rolling transaction to switch the mappings */
638 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
639 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
640 resblks, 0, 0, &tp);
641 if (error)
642 goto out;
643
644 xfs_ilock(ip, XFS_ILOCK_EXCL);
645 xfs_trans_ijoin(tp, ip, 0);
646
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100647 /* If there is a hole at end_fsb - 1 go to the previous extent */
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100648 if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
649 got.br_startoff > end_fsb) {
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100650 ASSERT(idx > 0);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100651 xfs_iext_get_extent(ifp, --idx, &got);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100652 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700653
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100654 /* Walk backwards until we're out of the I/O range... */
655 while (got.br_startoff + got.br_blockcount > offset_fsb) {
656 del = got;
657 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
658
659 /* Extent delete may have bumped idx forward */
660 if (!del.br_blockcount) {
661 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700662 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700663 }
664
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100665 ASSERT(!isnullstartblock(got.br_startblock));
666
667 /* Unmap the old blocks in the data fork. */
668 xfs_defer_init(&dfops, &firstfsb);
669 rlen = del.br_blockcount;
670 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
671 &firstfsb, &dfops);
672 if (error)
673 goto out_defer;
674
675 /* Trim the extent to whatever got unmapped. */
676 if (rlen) {
677 xfs_trim_extent(&del, del.br_startoff + rlen,
678 del.br_blockcount - rlen);
679 }
680 trace_xfs_reflink_cow_remap(ip, &del);
681
682 /* Free the CoW orphan record. */
683 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
684 del.br_startblock, del.br_blockcount);
685 if (error)
686 goto out_defer;
687
688 /* Map the new blocks into the data fork. */
689 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
690 if (error)
691 goto out_defer;
692
693 /* Remove the mapping from the CoW fork. */
694 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
695
696 error = xfs_defer_finish(&tp, &dfops, ip);
697 if (error)
698 goto out_defer;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700699next_extent:
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100700 if (!xfs_iext_get_extent(ifp, idx, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100701 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700702 }
703
704 error = xfs_trans_commit(tp);
705 xfs_iunlock(ip, XFS_ILOCK_EXCL);
706 if (error)
707 goto out;
708 return 0;
709
710out_defer:
711 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700712 xfs_trans_cancel(tp);
713 xfs_iunlock(ip, XFS_ILOCK_EXCL);
714out:
715 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
716 return error;
717}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700718
719/*
720 * Free leftover CoW reservations that didn't get cleaned out.
721 */
722int
723xfs_reflink_recover_cow(
724 struct xfs_mount *mp)
725{
726 xfs_agnumber_t agno;
727 int error = 0;
728
729 if (!xfs_sb_version_hasreflink(&mp->m_sb))
730 return 0;
731
732 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
733 error = xfs_refcount_recover_cow_leftovers(mp, agno);
734 if (error)
735 break;
736 }
737
738 return error;
739}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700740
741/*
742 * Reflinking (Block) Ranges of Two Files Together
743 *
744 * First, ensure that the reflink flag is set on both inodes. The flag is an
745 * optimization to avoid unnecessary refcount btree lookups in the write path.
746 *
747 * Now we can iteratively remap the range of extents (and holes) in src to the
748 * corresponding ranges in dest. Let drange and srange denote the ranges of
749 * logical blocks in dest and src touched by the reflink operation.
750 *
751 * While the length of drange is greater than zero,
752 * - Read src's bmbt at the start of srange ("imap")
753 * - If imap doesn't exist, make imap appear to start at the end of srange
754 * with zero length.
755 * - If imap starts before srange, advance imap to start at srange.
756 * - If imap goes beyond srange, truncate imap to end at the end of srange.
757 * - Punch (imap start - srange start + imap len) blocks from dest at
758 * offset (drange start).
759 * - If imap points to a real range of pblks,
760 * > Increase the refcount of the imap's pblks
761 * > Map imap's pblks into dest at the offset
762 * (drange start + imap start - srange start)
763 * - Advance drange and srange by (imap start - srange start + imap len)
764 *
765 * Finally, if the reflink made dest longer, update both the in-core and
766 * on-disk file sizes.
767 *
768 * ASCII Art Demonstration:
769 *
770 * Let's say we want to reflink this source file:
771 *
772 * ----SSSSSSS-SSSSS----SSSSSS (src file)
773 * <-------------------->
774 *
775 * into this destination file:
776 *
777 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
778 * <-------------------->
779 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
780 * Observe that the range has different logical offsets in either file.
781 *
782 * Consider that the first extent in the source file doesn't line up with our
783 * reflink range. Unmapping and remapping are separate operations, so we can
784 * unmap more blocks from the destination file than we remap.
785 *
786 * ----SSSSSSS-SSSSS----SSSSSS
787 * <------->
788 * --DDDDD---------DDDDD--DDD
789 * <------->
790 *
791 * Now remap the source extent into the destination file:
792 *
793 * ----SSSSSSS-SSSSS----SSSSSS
794 * <------->
795 * --DDDDD--SSSSSSSDDDDD--DDD
796 * <------->
797 *
798 * Do likewise with the second hole and extent in our range. Holes in the
799 * unmap range don't affect our operation.
800 *
801 * ----SSSSSSS-SSSSS----SSSSSS
802 * <---->
803 * --DDDDD--SSSSSSS-SSSSS-DDD
804 * <---->
805 *
806 * Finally, unmap and remap part of the third extent. This will increase the
807 * size of the destination file.
808 *
809 * ----SSSSSSS-SSSSS----SSSSSS
810 * <----->
811 * --DDDDD--SSSSSSS-SSSSS----SSS
812 * <----->
813 *
814 * Once we update the destination file's i_size, we're done.
815 */
816
817/*
818 * Ensure the reflink bit is set in both inodes.
819 */
820STATIC int
821xfs_reflink_set_inode_flag(
822 struct xfs_inode *src,
823 struct xfs_inode *dest)
824{
825 struct xfs_mount *mp = src->i_mount;
826 int error;
827 struct xfs_trans *tp;
828
829 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
830 return 0;
831
832 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
833 if (error)
834 goto out_error;
835
836 /* Lock both files against IO */
837 if (src->i_ino == dest->i_ino)
838 xfs_ilock(src, XFS_ILOCK_EXCL);
839 else
840 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
841
842 if (!xfs_is_reflink_inode(src)) {
843 trace_xfs_reflink_set_inode_flag(src);
844 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
845 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
846 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
847 xfs_ifork_init_cow(src);
848 } else
849 xfs_iunlock(src, XFS_ILOCK_EXCL);
850
851 if (src->i_ino == dest->i_ino)
852 goto commit_flags;
853
854 if (!xfs_is_reflink_inode(dest)) {
855 trace_xfs_reflink_set_inode_flag(dest);
856 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
857 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
858 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
859 xfs_ifork_init_cow(dest);
860 } else
861 xfs_iunlock(dest, XFS_ILOCK_EXCL);
862
863commit_flags:
864 error = xfs_trans_commit(tp);
865 if (error)
866 goto out_error;
867 return error;
868
869out_error:
870 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
871 return error;
872}
873
874/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700875 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700876 */
877STATIC int
878xfs_reflink_update_dest(
879 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700880 xfs_off_t newlen,
881 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700882{
883 struct xfs_mount *mp = dest->i_mount;
884 struct xfs_trans *tp;
885 int error;
886
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700887 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700888 return 0;
889
890 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
891 if (error)
892 goto out_error;
893
894 xfs_ilock(dest, XFS_ILOCK_EXCL);
895 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
896
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700897 if (newlen > i_size_read(VFS_I(dest))) {
898 trace_xfs_reflink_update_inode_size(dest, newlen);
899 i_size_write(VFS_I(dest), newlen);
900 dest->i_d.di_size = newlen;
901 }
902
903 if (cowextsize) {
904 dest->i_d.di_cowextsize = cowextsize;
905 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
906 }
907
Darrick J. Wong862bb362016-10-03 09:11:40 -0700908 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
909
910 error = xfs_trans_commit(tp);
911 if (error)
912 goto out_error;
913 return error;
914
915out_error:
916 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
917 return error;
918}
919
920/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700921 * Do we have enough reserve in this AG to handle a reflink? The refcount
922 * btree already reserved all the space it needs, but the rmap btree can grow
923 * infinitely, so we won't allow more reflinks when the AG is down to the
924 * btree reserves.
925 */
926static int
927xfs_reflink_ag_has_free_space(
928 struct xfs_mount *mp,
929 xfs_agnumber_t agno)
930{
931 struct xfs_perag *pag;
932 int error = 0;
933
934 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
935 return 0;
936
937 pag = xfs_perag_get(mp, agno);
938 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
939 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
940 error = -ENOSPC;
941 xfs_perag_put(pag);
942 return error;
943}
944
945/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700946 * Unmap a range of blocks from a file, then map other blocks into the hole.
947 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
948 * The extent irec is mapped into dest at irec->br_startoff.
949 */
950STATIC int
951xfs_reflink_remap_extent(
952 struct xfs_inode *ip,
953 struct xfs_bmbt_irec *irec,
954 xfs_fileoff_t destoff,
955 xfs_off_t new_isize)
956{
957 struct xfs_mount *mp = ip->i_mount;
958 struct xfs_trans *tp;
959 xfs_fsblock_t firstfsb;
960 unsigned int resblks;
961 struct xfs_defer_ops dfops;
962 struct xfs_bmbt_irec uirec;
963 bool real_extent;
964 xfs_filblks_t rlen;
965 xfs_filblks_t unmap_len;
966 xfs_off_t newlen;
967 int error;
968
969 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
970 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
971
972 /* Only remap normal extents. */
973 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
974 irec->br_startblock != DELAYSTARTBLOCK &&
975 !ISUNWRITTEN(irec));
976
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700977 /* No reflinking if we're low on space */
978 if (real_extent) {
979 error = xfs_reflink_ag_has_free_space(mp,
980 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
981 if (error)
982 goto out;
983 }
984
Darrick J. Wong862bb362016-10-03 09:11:40 -0700985 /* Start a rolling transaction to switch the mappings */
986 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
987 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
988 if (error)
989 goto out;
990
991 xfs_ilock(ip, XFS_ILOCK_EXCL);
992 xfs_trans_ijoin(tp, ip, 0);
993
994 /* If we're not just clearing space, then do we have enough quota? */
995 if (real_extent) {
996 error = xfs_trans_reserve_quota_nblks(tp, ip,
997 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
998 if (error)
999 goto out_cancel;
1000 }
1001
1002 trace_xfs_reflink_remap(ip, irec->br_startoff,
1003 irec->br_blockcount, irec->br_startblock);
1004
1005 /* Unmap the old blocks in the data fork. */
1006 rlen = unmap_len;
1007 while (rlen) {
1008 xfs_defer_init(&dfops, &firstfsb);
1009 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1010 &firstfsb, &dfops);
1011 if (error)
1012 goto out_defer;
1013
1014 /*
1015 * Trim the extent to whatever got unmapped.
1016 * Remember, bunmapi works backwards.
1017 */
1018 uirec.br_startblock = irec->br_startblock + rlen;
1019 uirec.br_startoff = irec->br_startoff + rlen;
1020 uirec.br_blockcount = unmap_len - rlen;
1021 unmap_len = rlen;
1022
1023 /* If this isn't a real mapping, we're done. */
1024 if (!real_extent || uirec.br_blockcount == 0)
1025 goto next_extent;
1026
1027 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1028 uirec.br_blockcount, uirec.br_startblock);
1029
1030 /* Update the refcount tree */
1031 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1032 if (error)
1033 goto out_defer;
1034
1035 /* Map the new blocks into the data fork. */
1036 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1037 if (error)
1038 goto out_defer;
1039
1040 /* Update quota accounting. */
1041 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1042 uirec.br_blockcount);
1043
1044 /* Update dest isize if needed. */
1045 newlen = XFS_FSB_TO_B(mp,
1046 uirec.br_startoff + uirec.br_blockcount);
1047 newlen = min_t(xfs_off_t, newlen, new_isize);
1048 if (newlen > i_size_read(VFS_I(ip))) {
1049 trace_xfs_reflink_update_inode_size(ip, newlen);
1050 i_size_write(VFS_I(ip), newlen);
1051 ip->i_d.di_size = newlen;
1052 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1053 }
1054
1055next_extent:
1056 /* Process all the deferred stuff. */
1057 error = xfs_defer_finish(&tp, &dfops, ip);
1058 if (error)
1059 goto out_defer;
1060 }
1061
1062 error = xfs_trans_commit(tp);
1063 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1064 if (error)
1065 goto out;
1066 return 0;
1067
1068out_defer:
1069 xfs_defer_cancel(&dfops);
1070out_cancel:
1071 xfs_trans_cancel(tp);
1072 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1073out:
1074 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1075 return error;
1076}
1077
1078/*
1079 * Iteratively remap one file's extents (and holes) to another's.
1080 */
1081STATIC int
1082xfs_reflink_remap_blocks(
1083 struct xfs_inode *src,
1084 xfs_fileoff_t srcoff,
1085 struct xfs_inode *dest,
1086 xfs_fileoff_t destoff,
1087 xfs_filblks_t len,
1088 xfs_off_t new_isize)
1089{
1090 struct xfs_bmbt_irec imap;
1091 int nimaps;
1092 int error = 0;
1093 xfs_filblks_t range_len;
1094
1095 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1096 while (len) {
1097 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1098 dest, destoff);
1099 /* Read extent from the source file */
1100 nimaps = 1;
1101 xfs_ilock(src, XFS_ILOCK_EXCL);
1102 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1103 xfs_iunlock(src, XFS_ILOCK_EXCL);
1104 if (error)
1105 goto err;
1106 ASSERT(nimaps == 1);
1107
1108 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1109 &imap);
1110
1111 /* Translate imap into the destination file. */
1112 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1113 imap.br_startoff += destoff - srcoff;
1114
1115 /* Clear dest from destoff to the end of imap and map it in. */
1116 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1117 new_isize);
1118 if (error)
1119 goto err;
1120
1121 if (fatal_signal_pending(current)) {
1122 error = -EINTR;
1123 goto err;
1124 }
1125
1126 /* Advance drange/srange */
1127 srcoff += range_len;
1128 destoff += range_len;
1129 len -= range_len;
1130 }
1131
1132 return 0;
1133
1134err:
1135 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1136 return error;
1137}
1138
1139/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001140 * Read a page's worth of file data into the page cache. Return the page
1141 * locked.
1142 */
1143static struct page *
1144xfs_get_page(
1145 struct inode *inode,
1146 xfs_off_t offset)
1147{
1148 struct address_space *mapping;
1149 struct page *page;
1150 pgoff_t n;
1151
1152 n = offset >> PAGE_SHIFT;
1153 mapping = inode->i_mapping;
1154 page = read_mapping_page(mapping, n, NULL);
1155 if (IS_ERR(page))
1156 return page;
1157 if (!PageUptodate(page)) {
1158 put_page(page);
1159 return ERR_PTR(-EIO);
1160 }
1161 lock_page(page);
1162 return page;
1163}
1164
1165/*
1166 * Compare extents of two files to see if they are the same.
1167 */
1168static int
1169xfs_compare_extents(
1170 struct inode *src,
1171 xfs_off_t srcoff,
1172 struct inode *dest,
1173 xfs_off_t destoff,
1174 xfs_off_t len,
1175 bool *is_same)
1176{
1177 xfs_off_t src_poff;
1178 xfs_off_t dest_poff;
1179 void *src_addr;
1180 void *dest_addr;
1181 struct page *src_page;
1182 struct page *dest_page;
1183 xfs_off_t cmp_len;
1184 bool same;
1185 int error;
1186
1187 error = -EINVAL;
1188 same = true;
1189 while (len) {
1190 src_poff = srcoff & (PAGE_SIZE - 1);
1191 dest_poff = destoff & (PAGE_SIZE - 1);
1192 cmp_len = min(PAGE_SIZE - src_poff,
1193 PAGE_SIZE - dest_poff);
1194 cmp_len = min(cmp_len, len);
1195 ASSERT(cmp_len > 0);
1196
1197 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1198 XFS_I(dest), destoff);
1199
1200 src_page = xfs_get_page(src, srcoff);
1201 if (IS_ERR(src_page)) {
1202 error = PTR_ERR(src_page);
1203 goto out_error;
1204 }
1205 dest_page = xfs_get_page(dest, destoff);
1206 if (IS_ERR(dest_page)) {
1207 error = PTR_ERR(dest_page);
1208 unlock_page(src_page);
1209 put_page(src_page);
1210 goto out_error;
1211 }
1212 src_addr = kmap_atomic(src_page);
1213 dest_addr = kmap_atomic(dest_page);
1214
1215 flush_dcache_page(src_page);
1216 flush_dcache_page(dest_page);
1217
1218 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1219 same = false;
1220
1221 kunmap_atomic(dest_addr);
1222 kunmap_atomic(src_addr);
1223 unlock_page(dest_page);
1224 unlock_page(src_page);
1225 put_page(dest_page);
1226 put_page(src_page);
1227
1228 if (!same)
1229 break;
1230
1231 srcoff += cmp_len;
1232 destoff += cmp_len;
1233 len -= cmp_len;
1234 }
1235
1236 *is_same = same;
1237 return 0;
1238
1239out_error:
1240 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1241 return error;
1242}
1243
1244/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001245 * Link a range of blocks from one file to another.
1246 */
1247int
1248xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001249 struct file *file_in,
1250 loff_t pos_in,
1251 struct file *file_out,
1252 loff_t pos_out,
1253 u64 len,
1254 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001255{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001256 struct inode *inode_in = file_inode(file_in);
1257 struct xfs_inode *src = XFS_I(inode_in);
1258 struct inode *inode_out = file_inode(file_out);
1259 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001260 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001261 loff_t bs = inode_out->i_sb->s_blocksize;
1262 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001263 xfs_fileoff_t sfsbno, dfsbno;
1264 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001265 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001266 loff_t isize;
1267 ssize_t ret;
1268 loff_t blen;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001269
1270 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1271 return -EOPNOTSUPP;
1272
1273 if (XFS_FORCED_SHUTDOWN(mp))
1274 return -EIO;
1275
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001276 /* Lock both files against IO */
1277 if (same_inode) {
1278 xfs_ilock(src, XFS_IOLOCK_EXCL);
1279 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1280 } else {
1281 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1282 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1283 }
1284
1285 /* Don't touch certain kinds of inodes */
1286 ret = -EPERM;
1287 if (IS_IMMUTABLE(inode_out))
1288 goto out_unlock;
1289
1290 ret = -ETXTBSY;
1291 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1292 goto out_unlock;
1293
1294
1295 /* Don't reflink dirs, pipes, sockets... */
1296 ret = -EISDIR;
1297 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1298 goto out_unlock;
1299 ret = -EINVAL;
1300 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1301 goto out_unlock;
1302 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1303 goto out_unlock;
1304
Darrick J. Wong862bb362016-10-03 09:11:40 -07001305 /* Don't reflink realtime inodes */
1306 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001307 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001308
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001309 /* Don't share DAX file data for now. */
1310 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1311 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001312
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001313 /* Are we going all the way to the end? */
1314 isize = i_size_read(inode_in);
1315 if (isize == 0) {
1316 ret = 0;
1317 goto out_unlock;
1318 }
1319
1320 if (len == 0)
1321 len = isize - pos_in;
1322
1323 /* Ensure offsets don't wrap and the input is inside i_size */
1324 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1325 pos_in + len > isize)
1326 goto out_unlock;
1327
1328 /* Don't allow dedupe past EOF in the dest file */
1329 if (is_dedupe) {
1330 loff_t disize;
1331
1332 disize = i_size_read(inode_out);
1333 if (pos_out >= disize || pos_out + len > disize)
1334 goto out_unlock;
1335 }
1336
1337 /* If we're linking to EOF, continue to the block boundary. */
1338 if (pos_in + len == isize)
1339 blen = ALIGN(isize, bs) - pos_in;
1340 else
1341 blen = len;
1342
1343 /* Only reflink if we're aligned to block boundaries */
1344 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1345 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1346 goto out_unlock;
1347
1348 /* Don't allow overlapped reflink within the same file */
1349 if (same_inode) {
1350 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1351 goto out_unlock;
1352 }
1353
1354 /* Wait for the completion of any pending IOs on both files */
1355 inode_dio_wait(inode_in);
1356 if (!same_inode)
1357 inode_dio_wait(inode_out);
1358
1359 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1360 pos_in, pos_in + len - 1);
1361 if (ret)
1362 goto out_unlock;
1363
1364 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1365 pos_out, pos_out + len - 1);
1366 if (ret)
1367 goto out_unlock;
1368
1369 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001370
Darrick J. Wongcc714662016-10-03 09:11:41 -07001371 /*
1372 * Check that the extents are the same.
1373 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001374 if (is_dedupe) {
1375 bool is_same = false;
1376
1377 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1378 len, &is_same);
1379 if (ret)
1380 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001381 if (!is_same) {
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001382 ret = -EBADE;
1383 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001384 }
1385 }
1386
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001387 ret = xfs_reflink_set_inode_flag(src, dest);
1388 if (ret)
1389 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001390
1391 /*
1392 * Invalidate the page cache so that we can clear any CoW mappings
1393 * in the destination file.
1394 */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001395 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1396 PAGE_ALIGN(pos_out + len) - 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001397
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001398 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1399 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001400 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001401 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1402 pos_out + len);
1403 if (ret)
1404 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001405
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001406 /*
1407 * Carry the cowextsize hint from src to dest if we're sharing the
1408 * entire source file to the entire destination file, the source file
1409 * has a cowextsize hint, and the destination file does not.
1410 */
1411 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001412 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001413 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001414 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001415 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1416 cowextsize = src->i_d.di_cowextsize;
1417
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001418 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001419
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001420out_unlock:
1421 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1422 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1423 if (src->i_ino != dest->i_ino) {
1424 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1425 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1426 }
1427 if (ret)
1428 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1429 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001430}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001431
1432/*
1433 * The user wants to preemptively CoW all shared blocks in this file,
1434 * which enables us to turn off the reflink flag. Iterate all
1435 * extents which are not prealloc/delalloc to see which ranges are
1436 * mentioned in the refcount tree, then read those blocks into the
1437 * pagecache, dirty them, fsync them back out, and then we can update
1438 * the inode flag. What happens if we run out of memory? :)
1439 */
1440STATIC int
1441xfs_reflink_dirty_extents(
1442 struct xfs_inode *ip,
1443 xfs_fileoff_t fbno,
1444 xfs_filblks_t end,
1445 xfs_off_t isize)
1446{
1447 struct xfs_mount *mp = ip->i_mount;
1448 xfs_agnumber_t agno;
1449 xfs_agblock_t agbno;
1450 xfs_extlen_t aglen;
1451 xfs_agblock_t rbno;
1452 xfs_extlen_t rlen;
1453 xfs_off_t fpos;
1454 xfs_off_t flen;
1455 struct xfs_bmbt_irec map[2];
1456 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001457 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001458
1459 while (end - fbno > 0) {
1460 nmaps = 1;
1461 /*
1462 * Look for extents in the file. Skip holes, delalloc, or
1463 * unwritten extents; they can't be reflinked.
1464 */
1465 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1466 if (error)
1467 goto out;
1468 if (nmaps == 0)
1469 break;
1470 if (map[0].br_startblock == HOLESTARTBLOCK ||
1471 map[0].br_startblock == DELAYSTARTBLOCK ||
1472 ISUNWRITTEN(&map[0]))
1473 goto next;
1474
1475 map[1] = map[0];
1476 while (map[1].br_blockcount) {
1477 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1478 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1479 aglen = map[1].br_blockcount;
1480
1481 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1482 &rbno, &rlen, true);
1483 if (error)
1484 goto out;
1485 if (rbno == NULLAGBLOCK)
1486 break;
1487
1488 /* Dirty the pages */
1489 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1490 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1491 (rbno - agbno));
1492 flen = XFS_FSB_TO_B(mp, rlen);
1493 if (fpos + flen > isize)
1494 flen = isize - fpos;
1495 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1496 &xfs_iomap_ops);
1497 xfs_ilock(ip, XFS_ILOCK_EXCL);
1498 if (error)
1499 goto out;
1500
1501 map[1].br_blockcount -= (rbno - agbno + rlen);
1502 map[1].br_startoff += (rbno - agbno + rlen);
1503 map[1].br_startblock += (rbno - agbno + rlen);
1504 }
1505
1506next:
1507 fbno = map[0].br_startoff + map[0].br_blockcount;
1508 }
1509out:
1510 return error;
1511}
1512
1513/* Clear the inode reflink flag if there are no shared extents. */
1514int
1515xfs_reflink_clear_inode_flag(
1516 struct xfs_inode *ip,
1517 struct xfs_trans **tpp)
1518{
1519 struct xfs_mount *mp = ip->i_mount;
1520 xfs_fileoff_t fbno;
1521 xfs_filblks_t end;
1522 xfs_agnumber_t agno;
1523 xfs_agblock_t agbno;
1524 xfs_extlen_t aglen;
1525 xfs_agblock_t rbno;
1526 xfs_extlen_t rlen;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001527 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001528 int nmaps;
1529 int error = 0;
1530
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001531 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001532
1533 fbno = 0;
1534 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1535 while (end - fbno > 0) {
1536 nmaps = 1;
1537 /*
1538 * Look for extents in the file. Skip holes, delalloc, or
1539 * unwritten extents; they can't be reflinked.
1540 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001541 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001542 if (error)
1543 return error;
1544 if (nmaps == 0)
1545 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001546 if (map.br_startblock == HOLESTARTBLOCK ||
1547 map.br_startblock == DELAYSTARTBLOCK ||
1548 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001549 goto next;
1550
Darrick J. Wong024adf42016-10-10 16:47:40 +11001551 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1552 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1553 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001554
Darrick J. Wong024adf42016-10-10 16:47:40 +11001555 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1556 &rbno, &rlen, false);
1557 if (error)
1558 return error;
1559 /* Is there still a shared block here? */
1560 if (rbno != NULLAGBLOCK)
1561 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001562next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001563 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001564 }
1565
1566 /*
1567 * We didn't find any shared blocks so turn off the reflink flag.
1568 * First, get rid of any leftover CoW mappings.
1569 */
1570 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1571 if (error)
1572 return error;
1573
1574 /* Clear the inode flag. */
1575 trace_xfs_reflink_unset_inode_flag(ip);
1576 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001577 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001578 xfs_trans_ijoin(*tpp, ip, 0);
1579 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1580
1581 return error;
1582}
1583
1584/*
1585 * Clear the inode reflink flag if there are no shared extents and the size
1586 * hasn't changed.
1587 */
1588STATIC int
1589xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001590 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001591{
1592 struct xfs_mount *mp = ip->i_mount;
1593 struct xfs_trans *tp;
1594 int error = 0;
1595
1596 /* Start a rolling transaction to remove the mappings */
1597 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1598 if (error)
1599 return error;
1600
1601 xfs_ilock(ip, XFS_ILOCK_EXCL);
1602 xfs_trans_ijoin(tp, ip, 0);
1603
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001604 error = xfs_reflink_clear_inode_flag(ip, &tp);
1605 if (error)
1606 goto cancel;
1607
1608 error = xfs_trans_commit(tp);
1609 if (error)
1610 goto out;
1611
1612 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1613 return 0;
1614cancel:
1615 xfs_trans_cancel(tp);
1616out:
1617 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1618 return error;
1619}
1620
1621/*
1622 * Pre-COW all shared blocks within a given byte range of a file and turn off
1623 * the reflink flag if we unshare all of the file's blocks.
1624 */
1625int
1626xfs_reflink_unshare(
1627 struct xfs_inode *ip,
1628 xfs_off_t offset,
1629 xfs_off_t len)
1630{
1631 struct xfs_mount *mp = ip->i_mount;
1632 xfs_fileoff_t fbno;
1633 xfs_filblks_t end;
1634 xfs_off_t isize;
1635 int error;
1636
1637 if (!xfs_is_reflink_inode(ip))
1638 return 0;
1639
1640 trace_xfs_reflink_unshare(ip, offset, len);
1641
1642 inode_dio_wait(VFS_I(ip));
1643
1644 /* Try to CoW the selected ranges */
1645 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001646 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001647 isize = i_size_read(VFS_I(ip));
1648 end = XFS_B_TO_FSB(mp, offset + len);
1649 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1650 if (error)
1651 goto out_unlock;
1652 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1653
1654 /* Wait for the IO to finish */
1655 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1656 if (error)
1657 goto out;
1658
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001659 /* Turn off the reflink flag if possible. */
1660 error = xfs_reflink_try_clear_inode_flag(ip);
1661 if (error)
1662 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001663
1664 return 0;
1665
1666out_unlock:
1667 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1668out:
1669 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1670 return error;
1671}