]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_reflink.c
xfs: cleanup xfs_reflink_find_cow_mapping
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_reflink.c
CommitLineData
3993baeb
DW
1/*
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"
174edb0e 43#include "xfs_btree.h"
3993baeb
DW
44#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"
2a06705c 55#include "xfs_iomap.h"
43caeb18 56#include "xfs_rmap_btree.h"
6fa164b8
DW
57#include "xfs_sb.h"
58#include "xfs_ag_resv.h"
3993baeb
DW
59
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 */
2a06705c
DW
120
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 ||
62c5ac89
CH
185 irec->br_startblock == DELAYSTARTBLOCK ||
186 isnullstartblock(irec->br_startblock)) {
2a06705c
DW
187 *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
3ba020be
CH
231/*
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(
2a06705c 242 struct xfs_inode *ip,
3ba020be
CH
243 struct xfs_bmbt_irec *imap,
244 bool *shared)
2a06705c 245{
2755fc44
CH
246 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
247 struct xfs_bmbt_irec got;
3ba020be 248 xfs_fileoff_t end_fsb, orig_end_fsb;
2755fc44
CH
249 int error = 0;
250 bool eof = false, trimmed;
2a06705c 251 xfs_extnum_t idx;
f7ca3522 252 xfs_extlen_t align;
2a06705c 253
3ba020be
CH
254 /*
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 */
2755fc44
CH
262
263 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
264 eof = true;
3ba020be
CH
265 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);
2a06705c 268
3ba020be
CH
269 *shared = true;
270 return 0;
271 }
2a06705c
DW
272
273 /* Trim the mapping to the nearest shared extent boundary. */
3ba020be 274 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
2a06705c 275 if (error)
3ba020be 276 return error;
2a06705c
DW
277
278 /* Not shared? Just report the (potentially capped) extent. */
3ba020be
CH
279 if (!*shared)
280 return 0;
2a06705c
DW
281
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)
3ba020be
CH
288 return error;
289
290 end_fsb = orig_end_fsb = imap->br_startoff + imap->br_blockcount;
2a06705c 291
f7ca3522
DW
292 align = xfs_eof_alignment(ip, xfs_get_cowextsz_hint(ip));
293 if (align)
294 end_fsb = roundup_64(end_fsb, align);
295
2a06705c 296retry:
3ba020be 297 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
65c5f419 298 end_fsb - imap->br_startoff, &got, &idx, eof);
2a06705c
DW
299 switch (error) {
300 case 0:
301 break;
302 case -ENOSPC:
303 case -EDQUOT:
304 /* retry without any preallocation */
3ba020be 305 trace_xfs_reflink_cow_enospc(ip, imap);
2a06705c
DW
306 if (end_fsb != orig_end_fsb) {
307 end_fsb = orig_end_fsb;
308 goto retry;
309 }
310 /*FALLTHRU*/
311 default:
3ba020be 312 return error;
2a06705c
DW
313 }
314
83104d44
DW
315 if (end_fsb != orig_end_fsb)
316 xfs_inode_set_cowblocks_tag(ip);
317
2a06705c 318 trace_xfs_reflink_cow_alloc(ip, &got);
3ba020be 319 return 0;
2a06705c 320}
ef473667 321
0613f16c
DW
322/* 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;
0613f16c 334 int nimaps = 1, error;
3ba020be 335 bool shared;
0613f16c
DW
336
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
3ba020be
CH
346 /* 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);
0613f16c
DW
355 if (error)
356 goto out_trans_cancel;
357
3ba020be
CH
358 if (!shared) {
359 *offset_fsb = imap.br_startoff + imap.br_blockcount;
0613f16c
DW
360 goto out_trans_cancel;
361 }
362
363 xfs_trans_ijoin(tp, ip, 0);
3ba020be 364 error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
0613f16c
DW
365 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
0613f16c
DW
371 error = xfs_defer_finish(&tp, &dfops, NULL);
372 if (error)
373 goto out_trans_cancel;
374
375 error = xfs_trans_commit(tp);
376
3ba020be 377 *offset_fsb = imap.br_startoff + imap.br_blockcount;
0613f16c
DW
378out_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
ef473667 422/*
092d5d9d 423 * Find the CoW reservation for a given byte offset of a file.
ef473667
DW
424 */
425bool
426xfs_reflink_find_cow_mapping(
427 struct xfs_inode *ip,
428 xfs_off_t offset,
092d5d9d 429 struct xfs_bmbt_irec *imap)
ef473667 430{
092d5d9d
CH
431 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
432 xfs_fileoff_t offset_fsb;
433 struct xfs_bmbt_irec got;
ef473667
DW
434 xfs_extnum_t idx;
435
436 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
437 ASSERT(xfs_is_reflink_inode(ip));
438
092d5d9d
CH
439 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
440 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
ef473667 441 return false;
092d5d9d 442 if (got.br_startoff > offset_fsb)
ef473667
DW
443 return false;
444
445 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
092d5d9d
CH
446 &got);
447 *imap = got;
ef473667
DW
448 return true;
449}
450
451/*
452 * Trim an extent to end at the next CoW reservation past offset_fsb.
453 */
454int
455xfs_reflink_trim_irec_to_next_cow(
456 struct xfs_inode *ip,
457 xfs_fileoff_t offset_fsb,
458 struct xfs_bmbt_irec *imap)
459{
460 struct xfs_bmbt_irec irec;
461 struct xfs_ifork *ifp;
462 struct xfs_bmbt_rec_host *gotp;
463 xfs_extnum_t idx;
464
465 if (!xfs_is_reflink_inode(ip))
466 return 0;
467
468 /* Find the extent in the CoW fork. */
469 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
470 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
471 if (!gotp)
472 return 0;
473 xfs_bmbt_get_all(gotp, &irec);
474
475 /* This is the extent before; try sliding up one. */
476 if (irec.br_startoff < offset_fsb) {
477 idx++;
5d829300 478 if (idx >= xfs_iext_count(ifp))
ef473667
DW
479 return 0;
480 gotp = xfs_iext_get_ext(ifp, idx);
481 xfs_bmbt_get_all(gotp, &irec);
482 }
483
484 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
485 return 0;
486
487 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
488 trace_xfs_reflink_trim_irec(ip, imap);
489
490 return 0;
491}
43caeb18
DW
492
493/*
494 * Cancel all pending CoW reservations for some block range of an inode.
495 */
496int
497xfs_reflink_cancel_cow_blocks(
498 struct xfs_inode *ip,
499 struct xfs_trans **tpp,
500 xfs_fileoff_t offset_fsb,
501 xfs_fileoff_t end_fsb)
502{
3e0ee78f
CH
503 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
504 struct xfs_bmbt_irec got, prev, del;
505 xfs_extnum_t idx;
43caeb18
DW
506 xfs_fsblock_t firstfsb;
507 struct xfs_defer_ops dfops;
3e0ee78f 508 int error = 0, eof = 0;
43caeb18
DW
509
510 if (!xfs_is_reflink_inode(ip))
511 return 0;
512
3e0ee78f
CH
513 xfs_bmap_search_extents(ip, offset_fsb, XFS_COW_FORK, &eof, &idx,
514 &got, &prev);
515 if (eof)
516 return 0;
43caeb18 517
3e0ee78f
CH
518 while (got.br_startoff < end_fsb) {
519 del = got;
520 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
521 trace_xfs_reflink_cancel_cow(ip, &del);
43caeb18 522
3e0ee78f
CH
523 if (isnullstartblock(del.br_startblock)) {
524 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
525 &idx, &got, &del);
43caeb18
DW
526 if (error)
527 break;
43caeb18
DW
528 } else {
529 xfs_trans_ijoin(*tpp, ip, 0);
530 xfs_defer_init(&dfops, &firstfsb);
531
174edb0e
DW
532 /* Free the CoW orphan record. */
533 error = xfs_refcount_free_cow_extent(ip->i_mount,
3e0ee78f
CH
534 &dfops, del.br_startblock,
535 del.br_blockcount);
174edb0e
DW
536 if (error)
537 break;
538
43caeb18 539 xfs_bmap_add_free(ip->i_mount, &dfops,
3e0ee78f 540 del.br_startblock, del.br_blockcount,
43caeb18
DW
541 NULL);
542
543 /* Update quota accounting */
544 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
3e0ee78f 545 -(long)del.br_blockcount);
43caeb18
DW
546
547 /* Roll the transaction */
548 error = xfs_defer_finish(tpp, &dfops, ip);
549 if (error) {
550 xfs_defer_cancel(&dfops);
551 break;
552 }
553
554 /* Remove the mapping from the CoW fork. */
3e0ee78f 555 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
43caeb18
DW
556 }
557
5d829300 558 if (++idx >= xfs_iext_count(ifp))
c17a8ef4 559 break;
3e0ee78f 560 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
43caeb18
DW
561 }
562
c17a8ef4
BF
563 /* clear tag if cow fork is emptied */
564 if (!ifp->if_bytes)
565 xfs_inode_clear_cowblocks_tag(ip);
566
43caeb18
DW
567 return error;
568}
569
570/*
571 * Cancel all pending CoW reservations for some byte range of an inode.
572 */
573int
574xfs_reflink_cancel_cow_range(
575 struct xfs_inode *ip,
576 xfs_off_t offset,
577 xfs_off_t count)
578{
579 struct xfs_trans *tp;
580 xfs_fileoff_t offset_fsb;
581 xfs_fileoff_t end_fsb;
582 int error;
583
584 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
63646fc5 585 ASSERT(xfs_is_reflink_inode(ip));
43caeb18
DW
586
587 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
588 if (count == NULLFILEOFF)
589 end_fsb = NULLFILEOFF;
590 else
591 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
592
593 /* Start a rolling transaction to remove the mappings */
594 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
595 0, 0, 0, &tp);
596 if (error)
597 goto out;
598
599 xfs_ilock(ip, XFS_ILOCK_EXCL);
600 xfs_trans_ijoin(tp, ip, 0);
601
602 /* Scrape out the old CoW reservations */
603 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
604 if (error)
605 goto out_cancel;
606
607 error = xfs_trans_commit(tp);
608
609 xfs_iunlock(ip, XFS_ILOCK_EXCL);
610 return error;
611
612out_cancel:
613 xfs_trans_cancel(tp);
614 xfs_iunlock(ip, XFS_ILOCK_EXCL);
615out:
616 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
617 return error;
618}
619
620/*
621 * Remap parts of a file's data fork after a successful CoW.
622 */
623int
624xfs_reflink_end_cow(
625 struct xfs_inode *ip,
626 xfs_off_t offset,
627 xfs_off_t count)
628{
c1112b6e
CH
629 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
630 struct xfs_bmbt_irec got, prev, del;
43caeb18
DW
631 struct xfs_trans *tp;
632 xfs_fileoff_t offset_fsb;
633 xfs_fileoff_t end_fsb;
43caeb18
DW
634 xfs_fsblock_t firstfsb;
635 struct xfs_defer_ops dfops;
c1112b6e 636 int error, eof = 0;
43caeb18 637 unsigned int resblks;
43caeb18 638 xfs_filblks_t rlen;
c1112b6e 639 xfs_extnum_t idx;
43caeb18
DW
640
641 trace_xfs_reflink_end_cow(ip, offset, count);
642
c1112b6e
CH
643 /* No COW extents? That's easy! */
644 if (ifp->if_bytes == 0)
645 return 0;
646
43caeb18
DW
647 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
648 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
43caeb18
DW
649
650 /* Start a rolling transaction to switch the mappings */
651 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
652 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
653 resblks, 0, 0, &tp);
654 if (error)
655 goto out;
656
657 xfs_ilock(ip, XFS_ILOCK_EXCL);
658 xfs_trans_ijoin(tp, ip, 0);
659
c1112b6e
CH
660 xfs_bmap_search_extents(ip, end_fsb - 1, XFS_COW_FORK, &eof, &idx,
661 &got, &prev);
43caeb18 662
c1112b6e
CH
663 /* If there is a hole at end_fsb - 1 go to the previous extent */
664 if (eof || got.br_startoff > end_fsb) {
665 ASSERT(idx > 0);
666 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, --idx), &got);
667 }
43caeb18 668
c1112b6e
CH
669 /* Walk backwards until we're out of the I/O range... */
670 while (got.br_startoff + got.br_blockcount > offset_fsb) {
671 del = got;
672 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
673
674 /* Extent delete may have bumped idx forward */
675 if (!del.br_blockcount) {
676 idx--;
43caeb18 677 goto next_extent;
c1112b6e
CH
678 }
679
680 ASSERT(!isnullstartblock(got.br_startblock));
43caeb18
DW
681
682 /* Unmap the old blocks in the data fork. */
c1112b6e
CH
683 xfs_defer_init(&dfops, &firstfsb);
684 rlen = del.br_blockcount;
685 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
686 &firstfsb, &dfops);
687 if (error)
688 goto out_defer;
43caeb18 689
c1112b6e
CH
690 /* Trim the extent to whatever got unmapped. */
691 if (rlen) {
692 xfs_trim_extent(&del, del.br_startoff + rlen,
693 del.br_blockcount - rlen);
694 }
695 trace_xfs_reflink_cow_remap(ip, &del);
174edb0e 696
c1112b6e
CH
697 /* Free the CoW orphan record. */
698 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
699 del.br_startblock, del.br_blockcount);
700 if (error)
701 goto out_defer;
43caeb18 702
c1112b6e
CH
703 /* Map the new blocks into the data fork. */
704 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
705 if (error)
706 goto out_defer;
43caeb18 707
c1112b6e
CH
708 /* Remove the mapping from the CoW fork. */
709 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
710
711 error = xfs_defer_finish(&tp, &dfops, ip);
712 if (error)
713 goto out_defer;
43caeb18
DW
714
715next_extent:
c1112b6e
CH
716 if (idx < 0)
717 break;
718 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &got);
43caeb18
DW
719 }
720
721 error = xfs_trans_commit(tp);
722 xfs_iunlock(ip, XFS_ILOCK_EXCL);
723 if (error)
724 goto out;
725 return 0;
726
727out_defer:
728 xfs_defer_cancel(&dfops);
43caeb18
DW
729 xfs_trans_cancel(tp);
730 xfs_iunlock(ip, XFS_ILOCK_EXCL);
731out:
732 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
733 return error;
734}
174edb0e
DW
735
736/*
737 * Free leftover CoW reservations that didn't get cleaned out.
738 */
739int
740xfs_reflink_recover_cow(
741 struct xfs_mount *mp)
742{
743 xfs_agnumber_t agno;
744 int error = 0;
745
746 if (!xfs_sb_version_hasreflink(&mp->m_sb))
747 return 0;
748
749 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
750 error = xfs_refcount_recover_cow_leftovers(mp, agno);
751 if (error)
752 break;
753 }
754
755 return error;
756}
862bb360
DW
757
758/*
759 * Reflinking (Block) Ranges of Two Files Together
760 *
761 * First, ensure that the reflink flag is set on both inodes. The flag is an
762 * optimization to avoid unnecessary refcount btree lookups in the write path.
763 *
764 * Now we can iteratively remap the range of extents (and holes) in src to the
765 * corresponding ranges in dest. Let drange and srange denote the ranges of
766 * logical blocks in dest and src touched by the reflink operation.
767 *
768 * While the length of drange is greater than zero,
769 * - Read src's bmbt at the start of srange ("imap")
770 * - If imap doesn't exist, make imap appear to start at the end of srange
771 * with zero length.
772 * - If imap starts before srange, advance imap to start at srange.
773 * - If imap goes beyond srange, truncate imap to end at the end of srange.
774 * - Punch (imap start - srange start + imap len) blocks from dest at
775 * offset (drange start).
776 * - If imap points to a real range of pblks,
777 * > Increase the refcount of the imap's pblks
778 * > Map imap's pblks into dest at the offset
779 * (drange start + imap start - srange start)
780 * - Advance drange and srange by (imap start - srange start + imap len)
781 *
782 * Finally, if the reflink made dest longer, update both the in-core and
783 * on-disk file sizes.
784 *
785 * ASCII Art Demonstration:
786 *
787 * Let's say we want to reflink this source file:
788 *
789 * ----SSSSSSS-SSSSS----SSSSSS (src file)
790 * <-------------------->
791 *
792 * into this destination file:
793 *
794 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
795 * <-------------------->
796 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
797 * Observe that the range has different logical offsets in either file.
798 *
799 * Consider that the first extent in the source file doesn't line up with our
800 * reflink range. Unmapping and remapping are separate operations, so we can
801 * unmap more blocks from the destination file than we remap.
802 *
803 * ----SSSSSSS-SSSSS----SSSSSS
804 * <------->
805 * --DDDDD---------DDDDD--DDD
806 * <------->
807 *
808 * Now remap the source extent into the destination file:
809 *
810 * ----SSSSSSS-SSSSS----SSSSSS
811 * <------->
812 * --DDDDD--SSSSSSSDDDDD--DDD
813 * <------->
814 *
815 * Do likewise with the second hole and extent in our range. Holes in the
816 * unmap range don't affect our operation.
817 *
818 * ----SSSSSSS-SSSSS----SSSSSS
819 * <---->
820 * --DDDDD--SSSSSSS-SSSSS-DDD
821 * <---->
822 *
823 * Finally, unmap and remap part of the third extent. This will increase the
824 * size of the destination file.
825 *
826 * ----SSSSSSS-SSSSS----SSSSSS
827 * <----->
828 * --DDDDD--SSSSSSS-SSSSS----SSS
829 * <----->
830 *
831 * Once we update the destination file's i_size, we're done.
832 */
833
834/*
835 * Ensure the reflink bit is set in both inodes.
836 */
837STATIC int
838xfs_reflink_set_inode_flag(
839 struct xfs_inode *src,
840 struct xfs_inode *dest)
841{
842 struct xfs_mount *mp = src->i_mount;
843 int error;
844 struct xfs_trans *tp;
845
846 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
847 return 0;
848
849 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
850 if (error)
851 goto out_error;
852
853 /* Lock both files against IO */
854 if (src->i_ino == dest->i_ino)
855 xfs_ilock(src, XFS_ILOCK_EXCL);
856 else
857 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
858
859 if (!xfs_is_reflink_inode(src)) {
860 trace_xfs_reflink_set_inode_flag(src);
861 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
862 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
863 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
864 xfs_ifork_init_cow(src);
865 } else
866 xfs_iunlock(src, XFS_ILOCK_EXCL);
867
868 if (src->i_ino == dest->i_ino)
869 goto commit_flags;
870
871 if (!xfs_is_reflink_inode(dest)) {
872 trace_xfs_reflink_set_inode_flag(dest);
873 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
874 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
875 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
876 xfs_ifork_init_cow(dest);
877 } else
878 xfs_iunlock(dest, XFS_ILOCK_EXCL);
879
880commit_flags:
881 error = xfs_trans_commit(tp);
882 if (error)
883 goto out_error;
884 return error;
885
886out_error:
887 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
888 return error;
889}
890
891/*
f7ca3522 892 * Update destination inode size & cowextsize hint, if necessary.
862bb360
DW
893 */
894STATIC int
895xfs_reflink_update_dest(
896 struct xfs_inode *dest,
f7ca3522
DW
897 xfs_off_t newlen,
898 xfs_extlen_t cowextsize)
862bb360
DW
899{
900 struct xfs_mount *mp = dest->i_mount;
901 struct xfs_trans *tp;
902 int error;
903
f7ca3522 904 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
862bb360
DW
905 return 0;
906
907 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
908 if (error)
909 goto out_error;
910
911 xfs_ilock(dest, XFS_ILOCK_EXCL);
912 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
913
f7ca3522
DW
914 if (newlen > i_size_read(VFS_I(dest))) {
915 trace_xfs_reflink_update_inode_size(dest, newlen);
916 i_size_write(VFS_I(dest), newlen);
917 dest->i_d.di_size = newlen;
918 }
919
920 if (cowextsize) {
921 dest->i_d.di_cowextsize = cowextsize;
922 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
923 }
924
862bb360
DW
925 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
926
927 error = xfs_trans_commit(tp);
928 if (error)
929 goto out_error;
930 return error;
931
932out_error:
933 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
934 return error;
935}
936
6fa164b8
DW
937/*
938 * Do we have enough reserve in this AG to handle a reflink? The refcount
939 * btree already reserved all the space it needs, but the rmap btree can grow
940 * infinitely, so we won't allow more reflinks when the AG is down to the
941 * btree reserves.
942 */
943static int
944xfs_reflink_ag_has_free_space(
945 struct xfs_mount *mp,
946 xfs_agnumber_t agno)
947{
948 struct xfs_perag *pag;
949 int error = 0;
950
951 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
952 return 0;
953
954 pag = xfs_perag_get(mp, agno);
955 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
956 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
957 error = -ENOSPC;
958 xfs_perag_put(pag);
959 return error;
960}
961
862bb360
DW
962/*
963 * Unmap a range of blocks from a file, then map other blocks into the hole.
964 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
965 * The extent irec is mapped into dest at irec->br_startoff.
966 */
967STATIC int
968xfs_reflink_remap_extent(
969 struct xfs_inode *ip,
970 struct xfs_bmbt_irec *irec,
971 xfs_fileoff_t destoff,
972 xfs_off_t new_isize)
973{
974 struct xfs_mount *mp = ip->i_mount;
975 struct xfs_trans *tp;
976 xfs_fsblock_t firstfsb;
977 unsigned int resblks;
978 struct xfs_defer_ops dfops;
979 struct xfs_bmbt_irec uirec;
980 bool real_extent;
981 xfs_filblks_t rlen;
982 xfs_filblks_t unmap_len;
983 xfs_off_t newlen;
984 int error;
985
986 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
987 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
988
989 /* Only remap normal extents. */
990 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
991 irec->br_startblock != DELAYSTARTBLOCK &&
992 !ISUNWRITTEN(irec));
993
6fa164b8
DW
994 /* No reflinking if we're low on space */
995 if (real_extent) {
996 error = xfs_reflink_ag_has_free_space(mp,
997 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
998 if (error)
999 goto out;
1000 }
1001
862bb360
DW
1002 /* Start a rolling transaction to switch the mappings */
1003 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1004 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1005 if (error)
1006 goto out;
1007
1008 xfs_ilock(ip, XFS_ILOCK_EXCL);
1009 xfs_trans_ijoin(tp, ip, 0);
1010
1011 /* If we're not just clearing space, then do we have enough quota? */
1012 if (real_extent) {
1013 error = xfs_trans_reserve_quota_nblks(tp, ip,
1014 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1015 if (error)
1016 goto out_cancel;
1017 }
1018
1019 trace_xfs_reflink_remap(ip, irec->br_startoff,
1020 irec->br_blockcount, irec->br_startblock);
1021
1022 /* Unmap the old blocks in the data fork. */
1023 rlen = unmap_len;
1024 while (rlen) {
1025 xfs_defer_init(&dfops, &firstfsb);
1026 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1027 &firstfsb, &dfops);
1028 if (error)
1029 goto out_defer;
1030
1031 /*
1032 * Trim the extent to whatever got unmapped.
1033 * Remember, bunmapi works backwards.
1034 */
1035 uirec.br_startblock = irec->br_startblock + rlen;
1036 uirec.br_startoff = irec->br_startoff + rlen;
1037 uirec.br_blockcount = unmap_len - rlen;
1038 unmap_len = rlen;
1039
1040 /* If this isn't a real mapping, we're done. */
1041 if (!real_extent || uirec.br_blockcount == 0)
1042 goto next_extent;
1043
1044 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1045 uirec.br_blockcount, uirec.br_startblock);
1046
1047 /* Update the refcount tree */
1048 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1049 if (error)
1050 goto out_defer;
1051
1052 /* Map the new blocks into the data fork. */
1053 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1054 if (error)
1055 goto out_defer;
1056
1057 /* Update quota accounting. */
1058 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1059 uirec.br_blockcount);
1060
1061 /* Update dest isize if needed. */
1062 newlen = XFS_FSB_TO_B(mp,
1063 uirec.br_startoff + uirec.br_blockcount);
1064 newlen = min_t(xfs_off_t, newlen, new_isize);
1065 if (newlen > i_size_read(VFS_I(ip))) {
1066 trace_xfs_reflink_update_inode_size(ip, newlen);
1067 i_size_write(VFS_I(ip), newlen);
1068 ip->i_d.di_size = newlen;
1069 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1070 }
1071
1072next_extent:
1073 /* Process all the deferred stuff. */
1074 error = xfs_defer_finish(&tp, &dfops, ip);
1075 if (error)
1076 goto out_defer;
1077 }
1078
1079 error = xfs_trans_commit(tp);
1080 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1081 if (error)
1082 goto out;
1083 return 0;
1084
1085out_defer:
1086 xfs_defer_cancel(&dfops);
1087out_cancel:
1088 xfs_trans_cancel(tp);
1089 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1090out:
1091 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1092 return error;
1093}
1094
1095/*
1096 * Iteratively remap one file's extents (and holes) to another's.
1097 */
1098STATIC int
1099xfs_reflink_remap_blocks(
1100 struct xfs_inode *src,
1101 xfs_fileoff_t srcoff,
1102 struct xfs_inode *dest,
1103 xfs_fileoff_t destoff,
1104 xfs_filblks_t len,
1105 xfs_off_t new_isize)
1106{
1107 struct xfs_bmbt_irec imap;
1108 int nimaps;
1109 int error = 0;
1110 xfs_filblks_t range_len;
1111
1112 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1113 while (len) {
1114 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1115 dest, destoff);
1116 /* Read extent from the source file */
1117 nimaps = 1;
1118 xfs_ilock(src, XFS_ILOCK_EXCL);
1119 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1120 xfs_iunlock(src, XFS_ILOCK_EXCL);
1121 if (error)
1122 goto err;
1123 ASSERT(nimaps == 1);
1124
1125 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1126 &imap);
1127
1128 /* Translate imap into the destination file. */
1129 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1130 imap.br_startoff += destoff - srcoff;
1131
1132 /* Clear dest from destoff to the end of imap and map it in. */
1133 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1134 new_isize);
1135 if (error)
1136 goto err;
1137
1138 if (fatal_signal_pending(current)) {
1139 error = -EINTR;
1140 goto err;
1141 }
1142
1143 /* Advance drange/srange */
1144 srcoff += range_len;
1145 destoff += range_len;
1146 len -= range_len;
1147 }
1148
1149 return 0;
1150
1151err:
1152 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1153 return error;
1154}
1155
cc714660
DW
1156/*
1157 * Read a page's worth of file data into the page cache. Return the page
1158 * locked.
1159 */
1160static struct page *
1161xfs_get_page(
1162 struct inode *inode,
1163 xfs_off_t offset)
1164{
1165 struct address_space *mapping;
1166 struct page *page;
1167 pgoff_t n;
1168
1169 n = offset >> PAGE_SHIFT;
1170 mapping = inode->i_mapping;
1171 page = read_mapping_page(mapping, n, NULL);
1172 if (IS_ERR(page))
1173 return page;
1174 if (!PageUptodate(page)) {
1175 put_page(page);
1176 return ERR_PTR(-EIO);
1177 }
1178 lock_page(page);
1179 return page;
1180}
1181
1182/*
1183 * Compare extents of two files to see if they are the same.
1184 */
1185static int
1186xfs_compare_extents(
1187 struct inode *src,
1188 xfs_off_t srcoff,
1189 struct inode *dest,
1190 xfs_off_t destoff,
1191 xfs_off_t len,
1192 bool *is_same)
1193{
1194 xfs_off_t src_poff;
1195 xfs_off_t dest_poff;
1196 void *src_addr;
1197 void *dest_addr;
1198 struct page *src_page;
1199 struct page *dest_page;
1200 xfs_off_t cmp_len;
1201 bool same;
1202 int error;
1203
1204 error = -EINVAL;
1205 same = true;
1206 while (len) {
1207 src_poff = srcoff & (PAGE_SIZE - 1);
1208 dest_poff = destoff & (PAGE_SIZE - 1);
1209 cmp_len = min(PAGE_SIZE - src_poff,
1210 PAGE_SIZE - dest_poff);
1211 cmp_len = min(cmp_len, len);
1212 ASSERT(cmp_len > 0);
1213
1214 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1215 XFS_I(dest), destoff);
1216
1217 src_page = xfs_get_page(src, srcoff);
1218 if (IS_ERR(src_page)) {
1219 error = PTR_ERR(src_page);
1220 goto out_error;
1221 }
1222 dest_page = xfs_get_page(dest, destoff);
1223 if (IS_ERR(dest_page)) {
1224 error = PTR_ERR(dest_page);
1225 unlock_page(src_page);
1226 put_page(src_page);
1227 goto out_error;
1228 }
1229 src_addr = kmap_atomic(src_page);
1230 dest_addr = kmap_atomic(dest_page);
1231
1232 flush_dcache_page(src_page);
1233 flush_dcache_page(dest_page);
1234
1235 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1236 same = false;
1237
1238 kunmap_atomic(dest_addr);
1239 kunmap_atomic(src_addr);
1240 unlock_page(dest_page);
1241 unlock_page(src_page);
1242 put_page(dest_page);
1243 put_page(src_page);
1244
1245 if (!same)
1246 break;
1247
1248 srcoff += cmp_len;
1249 destoff += cmp_len;
1250 len -= cmp_len;
1251 }
1252
1253 *is_same = same;
1254 return 0;
1255
1256out_error:
1257 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1258 return error;
1259}
1260
862bb360
DW
1261/*
1262 * Link a range of blocks from one file to another.
1263 */
1264int
1265xfs_reflink_remap_range(
5faaf4fa
CH
1266 struct file *file_in,
1267 loff_t pos_in,
1268 struct file *file_out,
1269 loff_t pos_out,
1270 u64 len,
1271 bool is_dedupe)
862bb360 1272{
5faaf4fa
CH
1273 struct inode *inode_in = file_inode(file_in);
1274 struct xfs_inode *src = XFS_I(inode_in);
1275 struct inode *inode_out = file_inode(file_out);
1276 struct xfs_inode *dest = XFS_I(inode_out);
862bb360 1277 struct xfs_mount *mp = src->i_mount;
5faaf4fa
CH
1278 loff_t bs = inode_out->i_sb->s_blocksize;
1279 bool same_inode = (inode_in == inode_out);
862bb360
DW
1280 xfs_fileoff_t sfsbno, dfsbno;
1281 xfs_filblks_t fsblen;
f7ca3522 1282 xfs_extlen_t cowextsize;
5faaf4fa
CH
1283 loff_t isize;
1284 ssize_t ret;
1285 loff_t blen;
862bb360
DW
1286
1287 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1288 return -EOPNOTSUPP;
1289
1290 if (XFS_FORCED_SHUTDOWN(mp))
1291 return -EIO;
1292
5faaf4fa
CH
1293 /* Lock both files against IO */
1294 if (same_inode) {
1295 xfs_ilock(src, XFS_IOLOCK_EXCL);
1296 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1297 } else {
1298 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1299 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1300 }
1301
1302 /* Don't touch certain kinds of inodes */
1303 ret = -EPERM;
1304 if (IS_IMMUTABLE(inode_out))
1305 goto out_unlock;
1306
1307 ret = -ETXTBSY;
1308 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1309 goto out_unlock;
1310
1311
1312 /* Don't reflink dirs, pipes, sockets... */
1313 ret = -EISDIR;
1314 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1315 goto out_unlock;
1316 ret = -EINVAL;
1317 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1318 goto out_unlock;
1319 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1320 goto out_unlock;
1321
862bb360
DW
1322 /* Don't reflink realtime inodes */
1323 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
5faaf4fa
CH
1324 goto out_unlock;
1325
1326 /* Don't share DAX file data for now. */
1327 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1328 goto out_unlock;
1329
1330 /* Are we going all the way to the end? */
1331 isize = i_size_read(inode_in);
1332 if (isize == 0) {
1333 ret = 0;
1334 goto out_unlock;
1335 }
1336
1337 if (len == 0)
1338 len = isize - pos_in;
1339
1340 /* Ensure offsets don't wrap and the input is inside i_size */
1341 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1342 pos_in + len > isize)
1343 goto out_unlock;
862bb360 1344
5faaf4fa
CH
1345 /* Don't allow dedupe past EOF in the dest file */
1346 if (is_dedupe) {
1347 loff_t disize;
cc714660 1348
5faaf4fa
CH
1349 disize = i_size_read(inode_out);
1350 if (pos_out >= disize || pos_out + len > disize)
1351 goto out_unlock;
1352 }
1353
1354 /* If we're linking to EOF, continue to the block boundary. */
1355 if (pos_in + len == isize)
1356 blen = ALIGN(isize, bs) - pos_in;
1357 else
1358 blen = len;
1359
1360 /* Only reflink if we're aligned to block boundaries */
1361 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1362 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1363 goto out_unlock;
1364
1365 /* Don't allow overlapped reflink within the same file */
1366 if (same_inode) {
1367 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1368 goto out_unlock;
1369 }
1370
1371 /* Wait for the completion of any pending IOs on both files */
1372 inode_dio_wait(inode_in);
1373 if (!same_inode)
1374 inode_dio_wait(inode_out);
1375
1376 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1377 pos_in, pos_in + len - 1);
1378 if (ret)
1379 goto out_unlock;
1380
1381 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1382 pos_out, pos_out + len - 1);
1383 if (ret)
1384 goto out_unlock;
1385
1386 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
862bb360 1387
cc714660
DW
1388 /*
1389 * Check that the extents are the same.
1390 */
5faaf4fa
CH
1391 if (is_dedupe) {
1392 bool is_same = false;
1393
1394 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1395 len, &is_same);
1396 if (ret)
1397 goto out_unlock;
cc714660 1398 if (!is_same) {
5faaf4fa
CH
1399 ret = -EBADE;
1400 goto out_unlock;
cc714660
DW
1401 }
1402 }
1403
5faaf4fa
CH
1404 ret = xfs_reflink_set_inode_flag(src, dest);
1405 if (ret)
1406 goto out_unlock;
862bb360
DW
1407
1408 /*
1409 * Invalidate the page cache so that we can clear any CoW mappings
1410 * in the destination file.
1411 */
5faaf4fa
CH
1412 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1413 PAGE_ALIGN(pos_out + len) - 1);
862bb360 1414
5faaf4fa
CH
1415 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1416 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
862bb360 1417 fsblen = XFS_B_TO_FSB(mp, len);
5faaf4fa
CH
1418 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1419 pos_out + len);
1420 if (ret)
1421 goto out_unlock;
862bb360 1422
f7ca3522
DW
1423 /*
1424 * Carry the cowextsize hint from src to dest if we're sharing the
1425 * entire source file to the entire destination file, the source file
1426 * has a cowextsize hint, and the destination file does not.
1427 */
1428 cowextsize = 0;
5faaf4fa 1429 if (pos_in == 0 && len == i_size_read(inode_in) &&
f7ca3522 1430 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
5faaf4fa 1431 pos_out == 0 && len >= i_size_read(inode_out) &&
f7ca3522
DW
1432 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1433 cowextsize = src->i_d.di_cowextsize;
1434
5faaf4fa 1435 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
862bb360 1436
5faaf4fa
CH
1437out_unlock:
1438 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1439 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1440 if (src->i_ino != dest->i_ino) {
1441 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1442 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1443 }
1444 if (ret)
1445 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1446 return ret;
862bb360 1447}
98cc2db5
DW
1448
1449/*
1450 * The user wants to preemptively CoW all shared blocks in this file,
1451 * which enables us to turn off the reflink flag. Iterate all
1452 * extents which are not prealloc/delalloc to see which ranges are
1453 * mentioned in the refcount tree, then read those blocks into the
1454 * pagecache, dirty them, fsync them back out, and then we can update
1455 * the inode flag. What happens if we run out of memory? :)
1456 */
1457STATIC int
1458xfs_reflink_dirty_extents(
1459 struct xfs_inode *ip,
1460 xfs_fileoff_t fbno,
1461 xfs_filblks_t end,
1462 xfs_off_t isize)
1463{
1464 struct xfs_mount *mp = ip->i_mount;
1465 xfs_agnumber_t agno;
1466 xfs_agblock_t agbno;
1467 xfs_extlen_t aglen;
1468 xfs_agblock_t rbno;
1469 xfs_extlen_t rlen;
1470 xfs_off_t fpos;
1471 xfs_off_t flen;
1472 struct xfs_bmbt_irec map[2];
1473 int nmaps;
9780643c 1474 int error = 0;
98cc2db5
DW
1475
1476 while (end - fbno > 0) {
1477 nmaps = 1;
1478 /*
1479 * Look for extents in the file. Skip holes, delalloc, or
1480 * unwritten extents; they can't be reflinked.
1481 */
1482 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1483 if (error)
1484 goto out;
1485 if (nmaps == 0)
1486 break;
1487 if (map[0].br_startblock == HOLESTARTBLOCK ||
1488 map[0].br_startblock == DELAYSTARTBLOCK ||
1489 ISUNWRITTEN(&map[0]))
1490 goto next;
1491
1492 map[1] = map[0];
1493 while (map[1].br_blockcount) {
1494 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1495 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1496 aglen = map[1].br_blockcount;
1497
1498 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1499 &rbno, &rlen, true);
1500 if (error)
1501 goto out;
1502 if (rbno == NULLAGBLOCK)
1503 break;
1504
1505 /* Dirty the pages */
1506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1507 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1508 (rbno - agbno));
1509 flen = XFS_FSB_TO_B(mp, rlen);
1510 if (fpos + flen > isize)
1511 flen = isize - fpos;
1512 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1513 &xfs_iomap_ops);
1514 xfs_ilock(ip, XFS_ILOCK_EXCL);
1515 if (error)
1516 goto out;
1517
1518 map[1].br_blockcount -= (rbno - agbno + rlen);
1519 map[1].br_startoff += (rbno - agbno + rlen);
1520 map[1].br_startblock += (rbno - agbno + rlen);
1521 }
1522
1523next:
1524 fbno = map[0].br_startoff + map[0].br_blockcount;
1525 }
1526out:
1527 return error;
1528}
1529
1530/* Clear the inode reflink flag if there are no shared extents. */
1531int
1532xfs_reflink_clear_inode_flag(
1533 struct xfs_inode *ip,
1534 struct xfs_trans **tpp)
1535{
1536 struct xfs_mount *mp = ip->i_mount;
1537 xfs_fileoff_t fbno;
1538 xfs_filblks_t end;
1539 xfs_agnumber_t agno;
1540 xfs_agblock_t agbno;
1541 xfs_extlen_t aglen;
1542 xfs_agblock_t rbno;
1543 xfs_extlen_t rlen;
024adf48 1544 struct xfs_bmbt_irec map;
98cc2db5
DW
1545 int nmaps;
1546 int error = 0;
1547
63646fc5 1548 ASSERT(xfs_is_reflink_inode(ip));
98cc2db5
DW
1549
1550 fbno = 0;
1551 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1552 while (end - fbno > 0) {
1553 nmaps = 1;
1554 /*
1555 * Look for extents in the file. Skip holes, delalloc, or
1556 * unwritten extents; they can't be reflinked.
1557 */
024adf48 1558 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
98cc2db5
DW
1559 if (error)
1560 return error;
1561 if (nmaps == 0)
1562 break;
024adf48
DW
1563 if (map.br_startblock == HOLESTARTBLOCK ||
1564 map.br_startblock == DELAYSTARTBLOCK ||
1565 ISUNWRITTEN(&map))
98cc2db5
DW
1566 goto next;
1567
024adf48
DW
1568 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1569 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1570 aglen = map.br_blockcount;
98cc2db5 1571
024adf48
DW
1572 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1573 &rbno, &rlen, false);
1574 if (error)
1575 return error;
1576 /* Is there still a shared block here? */
1577 if (rbno != NULLAGBLOCK)
1578 return 0;
98cc2db5 1579next:
024adf48 1580 fbno = map.br_startoff + map.br_blockcount;
98cc2db5
DW
1581 }
1582
1583 /*
1584 * We didn't find any shared blocks so turn off the reflink flag.
1585 * First, get rid of any leftover CoW mappings.
1586 */
1587 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1588 if (error)
1589 return error;
1590
1591 /* Clear the inode flag. */
1592 trace_xfs_reflink_unset_inode_flag(ip);
1593 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
83104d44 1594 xfs_inode_clear_cowblocks_tag(ip);
98cc2db5
DW
1595 xfs_trans_ijoin(*tpp, ip, 0);
1596 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1597
1598 return error;
1599}
1600
1601/*
1602 * Clear the inode reflink flag if there are no shared extents and the size
1603 * hasn't changed.
1604 */
1605STATIC int
1606xfs_reflink_try_clear_inode_flag(
97a1b87e 1607 struct xfs_inode *ip)
98cc2db5
DW
1608{
1609 struct xfs_mount *mp = ip->i_mount;
1610 struct xfs_trans *tp;
1611 int error = 0;
1612
1613 /* Start a rolling transaction to remove the mappings */
1614 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1615 if (error)
1616 return error;
1617
1618 xfs_ilock(ip, XFS_ILOCK_EXCL);
1619 xfs_trans_ijoin(tp, ip, 0);
1620
98cc2db5
DW
1621 error = xfs_reflink_clear_inode_flag(ip, &tp);
1622 if (error)
1623 goto cancel;
1624
1625 error = xfs_trans_commit(tp);
1626 if (error)
1627 goto out;
1628
1629 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1630 return 0;
1631cancel:
1632 xfs_trans_cancel(tp);
1633out:
1634 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1635 return error;
1636}
1637
1638/*
1639 * Pre-COW all shared blocks within a given byte range of a file and turn off
1640 * the reflink flag if we unshare all of the file's blocks.
1641 */
1642int
1643xfs_reflink_unshare(
1644 struct xfs_inode *ip,
1645 xfs_off_t offset,
1646 xfs_off_t len)
1647{
1648 struct xfs_mount *mp = ip->i_mount;
1649 xfs_fileoff_t fbno;
1650 xfs_filblks_t end;
1651 xfs_off_t isize;
1652 int error;
1653
1654 if (!xfs_is_reflink_inode(ip))
1655 return 0;
1656
1657 trace_xfs_reflink_unshare(ip, offset, len);
1658
1659 inode_dio_wait(VFS_I(ip));
1660
1661 /* Try to CoW the selected ranges */
1662 xfs_ilock(ip, XFS_ILOCK_EXCL);
97a1b87e 1663 fbno = XFS_B_TO_FSBT(mp, offset);
98cc2db5
DW
1664 isize = i_size_read(VFS_I(ip));
1665 end = XFS_B_TO_FSB(mp, offset + len);
1666 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1667 if (error)
1668 goto out_unlock;
1669 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1670
1671 /* Wait for the IO to finish */
1672 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1673 if (error)
1674 goto out;
1675
97a1b87e
DW
1676 /* Turn off the reflink flag if possible. */
1677 error = xfs_reflink_try_clear_inode_flag(ip);
1678 if (error)
1679 goto out;
98cc2db5
DW
1680
1681 return 0;
1682
1683out_unlock:
1684 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1685out:
1686 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1687 return error;
1688}