]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_reflink.c
xfs: track preallocation separately in xfs_bmapi_reserve_delalloc()
[mirror_ubuntu-zesty-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,
974ae922 298 end_fsb - imap->br_startoff, 0, &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 */
86f12ab0 454void
ef473667
DW
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{
86f12ab0
CH
460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
461 struct xfs_bmbt_irec got;
ef473667
DW
462 xfs_extnum_t idx;
463
464 if (!xfs_is_reflink_inode(ip))
86f12ab0 465 return;
ef473667
DW
466
467 /* Find the extent in the CoW fork. */
86f12ab0
CH
468 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
469 return;
ef473667
DW
470
471 /* This is the extent before; try sliding up one. */
86f12ab0
CH
472 if (got.br_startoff < offset_fsb) {
473 if (!xfs_iext_get_extent(ifp, idx + 1, &got))
474 return;
ef473667
DW
475 }
476
86f12ab0
CH
477 if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
478 return;
ef473667 479
86f12ab0 480 imap->br_blockcount = got.br_startoff - imap->br_startoff;
ef473667 481 trace_xfs_reflink_trim_irec(ip, imap);
ef473667 482}
43caeb18
DW
483
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{
3e0ee78f 494 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
df5ab1b5 495 struct xfs_bmbt_irec got, del;
3e0ee78f 496 xfs_extnum_t idx;
43caeb18
DW
497 xfs_fsblock_t firstfsb;
498 struct xfs_defer_ops dfops;
df5ab1b5 499 int error = 0;
43caeb18
DW
500
501 if (!xfs_is_reflink_inode(ip))
502 return 0;
df5ab1b5 503 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
3e0ee78f 504 return 0;
43caeb18 505
3e0ee78f
CH
506 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);
43caeb18 510
3e0ee78f
CH
511 if (isnullstartblock(del.br_startblock)) {
512 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
513 &idx, &got, &del);
43caeb18
DW
514 if (error)
515 break;
43caeb18
DW
516 } else {
517 xfs_trans_ijoin(*tpp, ip, 0);
518 xfs_defer_init(&dfops, &firstfsb);
519
174edb0e
DW
520 /* Free the CoW orphan record. */
521 error = xfs_refcount_free_cow_extent(ip->i_mount,
3e0ee78f
CH
522 &dfops, del.br_startblock,
523 del.br_blockcount);
174edb0e
DW
524 if (error)
525 break;
526
43caeb18 527 xfs_bmap_add_free(ip->i_mount, &dfops,
3e0ee78f 528 del.br_startblock, del.br_blockcount,
43caeb18
DW
529 NULL);
530
531 /* Update quota accounting */
532 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
3e0ee78f 533 -(long)del.br_blockcount);
43caeb18
DW
534
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. */
3e0ee78f 543 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
43caeb18
DW
544 }
545
df5ab1b5 546 if (!xfs_iext_get_extent(ifp, ++idx, &got))
c17a8ef4 547 break;
43caeb18
DW
548 }
549
c17a8ef4
BF
550 /* clear tag if cow fork is emptied */
551 if (!ifp->if_bytes)
552 xfs_inode_clear_cowblocks_tag(ip);
553
43caeb18
DW
554 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);
63646fc5 572 ASSERT(xfs_is_reflink_inode(ip));
43caeb18
DW
573
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{
c1112b6e 616 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4ab8671c 617 struct xfs_bmbt_irec got, del;
43caeb18
DW
618 struct xfs_trans *tp;
619 xfs_fileoff_t offset_fsb;
620 xfs_fileoff_t end_fsb;
43caeb18
DW
621 xfs_fsblock_t firstfsb;
622 struct xfs_defer_ops dfops;
4ab8671c 623 int error;
43caeb18 624 unsigned int resblks;
43caeb18 625 xfs_filblks_t rlen;
c1112b6e 626 xfs_extnum_t idx;
43caeb18
DW
627
628 trace_xfs_reflink_end_cow(ip, offset, count);
629
c1112b6e
CH
630 /* No COW extents? That's easy! */
631 if (ifp->if_bytes == 0)
632 return 0;
633
43caeb18
DW
634 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
635 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
43caeb18
DW
636
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
c1112b6e 647 /* If there is a hole at end_fsb - 1 go to the previous extent */
4ab8671c
CH
648 if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
649 got.br_startoff > end_fsb) {
c1112b6e 650 ASSERT(idx > 0);
4ab8671c 651 xfs_iext_get_extent(ifp, --idx, &got);
c1112b6e 652 }
43caeb18 653
c1112b6e
CH
654 /* 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--;
43caeb18 662 goto next_extent;
c1112b6e
CH
663 }
664
665 ASSERT(!isnullstartblock(got.br_startblock));
43caeb18
DW
666
667 /* Unmap the old blocks in the data fork. */
c1112b6e
CH
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;
43caeb18 674
c1112b6e
CH
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);
174edb0e 681
c1112b6e
CH
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;
43caeb18 687
c1112b6e
CH
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;
43caeb18 692
c1112b6e
CH
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;
43caeb18 699next_extent:
4ab8671c 700 if (!xfs_iext_get_extent(ifp, idx, &got))
c1112b6e 701 break;
43caeb18
DW
702 }
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);
43caeb18
DW
712 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}
174edb0e
DW
718
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}
862bb360
DW
740
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/*
f7ca3522 875 * Update destination inode size & cowextsize hint, if necessary.
862bb360
DW
876 */
877STATIC int
878xfs_reflink_update_dest(
879 struct xfs_inode *dest,
f7ca3522
DW
880 xfs_off_t newlen,
881 xfs_extlen_t cowextsize)
862bb360
DW
882{
883 struct xfs_mount *mp = dest->i_mount;
884 struct xfs_trans *tp;
885 int error;
886
f7ca3522 887 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
862bb360
DW
888 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
f7ca3522
DW
897 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
862bb360
DW
908 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
6fa164b8
DW
920/*
921 * 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
862bb360
DW
945/*
946 * 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
6fa164b8
DW
977 /* 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
862bb360
DW
985 /* 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
cc714660
DW
1139/*
1140 * 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
862bb360
DW
1244/*
1245 * Link a range of blocks from one file to another.
1246 */
1247int
1248xfs_reflink_remap_range(
5faaf4fa
CH
1249 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)
862bb360 1255{
5faaf4fa
CH
1256 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);
862bb360 1260 struct xfs_mount *mp = src->i_mount;
5faaf4fa
CH
1261 loff_t bs = inode_out->i_sb->s_blocksize;
1262 bool same_inode = (inode_in == inode_out);
862bb360
DW
1263 xfs_fileoff_t sfsbno, dfsbno;
1264 xfs_filblks_t fsblen;
f7ca3522 1265 xfs_extlen_t cowextsize;
5faaf4fa
CH
1266 loff_t isize;
1267 ssize_t ret;
1268 loff_t blen;
862bb360
DW
1269
1270 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1271 return -EOPNOTSUPP;
1272
1273 if (XFS_FORCED_SHUTDOWN(mp))
1274 return -EIO;
1275
5faaf4fa
CH
1276 /* 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
862bb360
DW
1305 /* Don't reflink realtime inodes */
1306 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
5faaf4fa
CH
1307 goto out_unlock;
1308
1309 /* Don't share DAX file data for now. */
1310 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1311 goto out_unlock;
1312
1313 /* 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
fba3e594
DW
1320 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1321 if (len == 0) {
1322 if (is_dedupe) {
1323 ret = 0;
1324 goto out_unlock;
1325 }
5faaf4fa 1326 len = isize - pos_in;
fba3e594 1327 }
5faaf4fa
CH
1328
1329 /* Ensure offsets don't wrap and the input is inside i_size */
1330 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1331 pos_in + len > isize)
1332 goto out_unlock;
862bb360 1333
5faaf4fa
CH
1334 /* Don't allow dedupe past EOF in the dest file */
1335 if (is_dedupe) {
1336 loff_t disize;
cc714660 1337
5faaf4fa
CH
1338 disize = i_size_read(inode_out);
1339 if (pos_out >= disize || pos_out + len > disize)
1340 goto out_unlock;
1341 }
1342
1343 /* If we're linking to EOF, continue to the block boundary. */
1344 if (pos_in + len == isize)
1345 blen = ALIGN(isize, bs) - pos_in;
1346 else
1347 blen = len;
1348
1349 /* Only reflink if we're aligned to block boundaries */
1350 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1351 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1352 goto out_unlock;
1353
1354 /* Don't allow overlapped reflink within the same file */
1355 if (same_inode) {
1356 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1357 goto out_unlock;
1358 }
1359
1360 /* Wait for the completion of any pending IOs on both files */
1361 inode_dio_wait(inode_in);
1362 if (!same_inode)
1363 inode_dio_wait(inode_out);
1364
1365 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1366 pos_in, pos_in + len - 1);
1367 if (ret)
1368 goto out_unlock;
1369
1370 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1371 pos_out, pos_out + len - 1);
1372 if (ret)
1373 goto out_unlock;
1374
1375 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
862bb360 1376
cc714660
DW
1377 /*
1378 * Check that the extents are the same.
1379 */
5faaf4fa
CH
1380 if (is_dedupe) {
1381 bool is_same = false;
1382
1383 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1384 len, &is_same);
1385 if (ret)
1386 goto out_unlock;
cc714660 1387 if (!is_same) {
5faaf4fa
CH
1388 ret = -EBADE;
1389 goto out_unlock;
cc714660
DW
1390 }
1391 }
1392
5faaf4fa
CH
1393 ret = xfs_reflink_set_inode_flag(src, dest);
1394 if (ret)
1395 goto out_unlock;
862bb360
DW
1396
1397 /*
1398 * Invalidate the page cache so that we can clear any CoW mappings
1399 * in the destination file.
1400 */
5faaf4fa
CH
1401 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1402 PAGE_ALIGN(pos_out + len) - 1);
862bb360 1403
5faaf4fa
CH
1404 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1405 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
862bb360 1406 fsblen = XFS_B_TO_FSB(mp, len);
5faaf4fa
CH
1407 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1408 pos_out + len);
1409 if (ret)
1410 goto out_unlock;
862bb360 1411
f7ca3522
DW
1412 /*
1413 * Carry the cowextsize hint from src to dest if we're sharing the
1414 * entire source file to the entire destination file, the source file
1415 * has a cowextsize hint, and the destination file does not.
1416 */
1417 cowextsize = 0;
5faaf4fa 1418 if (pos_in == 0 && len == i_size_read(inode_in) &&
f7ca3522 1419 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
5faaf4fa 1420 pos_out == 0 && len >= i_size_read(inode_out) &&
f7ca3522
DW
1421 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1422 cowextsize = src->i_d.di_cowextsize;
1423
5faaf4fa 1424 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
862bb360 1425
5faaf4fa
CH
1426out_unlock:
1427 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1428 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1429 if (src->i_ino != dest->i_ino) {
1430 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1431 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1432 }
1433 if (ret)
1434 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1435 return ret;
862bb360 1436}
98cc2db5
DW
1437
1438/*
1439 * The user wants to preemptively CoW all shared blocks in this file,
1440 * which enables us to turn off the reflink flag. Iterate all
1441 * extents which are not prealloc/delalloc to see which ranges are
1442 * mentioned in the refcount tree, then read those blocks into the
1443 * pagecache, dirty them, fsync them back out, and then we can update
1444 * the inode flag. What happens if we run out of memory? :)
1445 */
1446STATIC int
1447xfs_reflink_dirty_extents(
1448 struct xfs_inode *ip,
1449 xfs_fileoff_t fbno,
1450 xfs_filblks_t end,
1451 xfs_off_t isize)
1452{
1453 struct xfs_mount *mp = ip->i_mount;
1454 xfs_agnumber_t agno;
1455 xfs_agblock_t agbno;
1456 xfs_extlen_t aglen;
1457 xfs_agblock_t rbno;
1458 xfs_extlen_t rlen;
1459 xfs_off_t fpos;
1460 xfs_off_t flen;
1461 struct xfs_bmbt_irec map[2];
1462 int nmaps;
9780643c 1463 int error = 0;
98cc2db5
DW
1464
1465 while (end - fbno > 0) {
1466 nmaps = 1;
1467 /*
1468 * Look for extents in the file. Skip holes, delalloc, or
1469 * unwritten extents; they can't be reflinked.
1470 */
1471 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1472 if (error)
1473 goto out;
1474 if (nmaps == 0)
1475 break;
1476 if (map[0].br_startblock == HOLESTARTBLOCK ||
1477 map[0].br_startblock == DELAYSTARTBLOCK ||
1478 ISUNWRITTEN(&map[0]))
1479 goto next;
1480
1481 map[1] = map[0];
1482 while (map[1].br_blockcount) {
1483 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1484 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1485 aglen = map[1].br_blockcount;
1486
1487 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1488 &rbno, &rlen, true);
1489 if (error)
1490 goto out;
1491 if (rbno == NULLAGBLOCK)
1492 break;
1493
1494 /* Dirty the pages */
1495 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1496 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1497 (rbno - agbno));
1498 flen = XFS_FSB_TO_B(mp, rlen);
1499 if (fpos + flen > isize)
1500 flen = isize - fpos;
1501 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1502 &xfs_iomap_ops);
1503 xfs_ilock(ip, XFS_ILOCK_EXCL);
1504 if (error)
1505 goto out;
1506
1507 map[1].br_blockcount -= (rbno - agbno + rlen);
1508 map[1].br_startoff += (rbno - agbno + rlen);
1509 map[1].br_startblock += (rbno - agbno + rlen);
1510 }
1511
1512next:
1513 fbno = map[0].br_startoff + map[0].br_blockcount;
1514 }
1515out:
1516 return error;
1517}
1518
1519/* Clear the inode reflink flag if there are no shared extents. */
1520int
1521xfs_reflink_clear_inode_flag(
1522 struct xfs_inode *ip,
1523 struct xfs_trans **tpp)
1524{
1525 struct xfs_mount *mp = ip->i_mount;
1526 xfs_fileoff_t fbno;
1527 xfs_filblks_t end;
1528 xfs_agnumber_t agno;
1529 xfs_agblock_t agbno;
1530 xfs_extlen_t aglen;
1531 xfs_agblock_t rbno;
1532 xfs_extlen_t rlen;
024adf48 1533 struct xfs_bmbt_irec map;
98cc2db5
DW
1534 int nmaps;
1535 int error = 0;
1536
63646fc5 1537 ASSERT(xfs_is_reflink_inode(ip));
98cc2db5
DW
1538
1539 fbno = 0;
1540 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1541 while (end - fbno > 0) {
1542 nmaps = 1;
1543 /*
1544 * Look for extents in the file. Skip holes, delalloc, or
1545 * unwritten extents; they can't be reflinked.
1546 */
024adf48 1547 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
98cc2db5
DW
1548 if (error)
1549 return error;
1550 if (nmaps == 0)
1551 break;
024adf48
DW
1552 if (map.br_startblock == HOLESTARTBLOCK ||
1553 map.br_startblock == DELAYSTARTBLOCK ||
1554 ISUNWRITTEN(&map))
98cc2db5
DW
1555 goto next;
1556
024adf48
DW
1557 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1558 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1559 aglen = map.br_blockcount;
98cc2db5 1560
024adf48
DW
1561 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1562 &rbno, &rlen, false);
1563 if (error)
1564 return error;
1565 /* Is there still a shared block here? */
1566 if (rbno != NULLAGBLOCK)
1567 return 0;
98cc2db5 1568next:
024adf48 1569 fbno = map.br_startoff + map.br_blockcount;
98cc2db5
DW
1570 }
1571
1572 /*
1573 * We didn't find any shared blocks so turn off the reflink flag.
1574 * First, get rid of any leftover CoW mappings.
1575 */
1576 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1577 if (error)
1578 return error;
1579
1580 /* Clear the inode flag. */
1581 trace_xfs_reflink_unset_inode_flag(ip);
1582 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
83104d44 1583 xfs_inode_clear_cowblocks_tag(ip);
98cc2db5
DW
1584 xfs_trans_ijoin(*tpp, ip, 0);
1585 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1586
1587 return error;
1588}
1589
1590/*
1591 * Clear the inode reflink flag if there are no shared extents and the size
1592 * hasn't changed.
1593 */
1594STATIC int
1595xfs_reflink_try_clear_inode_flag(
97a1b87e 1596 struct xfs_inode *ip)
98cc2db5
DW
1597{
1598 struct xfs_mount *mp = ip->i_mount;
1599 struct xfs_trans *tp;
1600 int error = 0;
1601
1602 /* Start a rolling transaction to remove the mappings */
1603 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1604 if (error)
1605 return error;
1606
1607 xfs_ilock(ip, XFS_ILOCK_EXCL);
1608 xfs_trans_ijoin(tp, ip, 0);
1609
98cc2db5
DW
1610 error = xfs_reflink_clear_inode_flag(ip, &tp);
1611 if (error)
1612 goto cancel;
1613
1614 error = xfs_trans_commit(tp);
1615 if (error)
1616 goto out;
1617
1618 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1619 return 0;
1620cancel:
1621 xfs_trans_cancel(tp);
1622out:
1623 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1624 return error;
1625}
1626
1627/*
1628 * Pre-COW all shared blocks within a given byte range of a file and turn off
1629 * the reflink flag if we unshare all of the file's blocks.
1630 */
1631int
1632xfs_reflink_unshare(
1633 struct xfs_inode *ip,
1634 xfs_off_t offset,
1635 xfs_off_t len)
1636{
1637 struct xfs_mount *mp = ip->i_mount;
1638 xfs_fileoff_t fbno;
1639 xfs_filblks_t end;
1640 xfs_off_t isize;
1641 int error;
1642
1643 if (!xfs_is_reflink_inode(ip))
1644 return 0;
1645
1646 trace_xfs_reflink_unshare(ip, offset, len);
1647
1648 inode_dio_wait(VFS_I(ip));
1649
1650 /* Try to CoW the selected ranges */
1651 xfs_ilock(ip, XFS_ILOCK_EXCL);
97a1b87e 1652 fbno = XFS_B_TO_FSBT(mp, offset);
98cc2db5
DW
1653 isize = i_size_read(VFS_I(ip));
1654 end = XFS_B_TO_FSB(mp, offset + len);
1655 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1656 if (error)
1657 goto out_unlock;
1658 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1659
1660 /* Wait for the IO to finish */
1661 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1662 if (error)
1663 goto out;
1664
97a1b87e
DW
1665 /* Turn off the reflink flag if possible. */
1666 error = xfs_reflink_try_clear_inode_flag(ip);
1667 if (error)
1668 goto out;
98cc2db5
DW
1669
1670 return 0;
1671
1672out_unlock:
1673 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1674out:
1675 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1676 return error;
1677}