blob: 8e894118295fec9154adc520abe814d931c7b74a [file] [log] [blame]
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_mount.h"
27#include "xfs_defer.h"
28#include "xfs_da_format.h"
29#include "xfs_da_btree.h"
30#include "xfs_inode.h"
31#include "xfs_trans.h"
32#include "xfs_inode_item.h"
33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
35#include "xfs_error.h"
36#include "xfs_dir2.h"
37#include "xfs_dir2_priv.h"
38#include "xfs_ioctl.h"
39#include "xfs_trace.h"
40#include "xfs_log.h"
41#include "xfs_icache.h"
42#include "xfs_pnfs.h"
Darrick J. Wong174edb02016-10-03 09:11:39 -070043#include "xfs_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070044#include "xfs_refcount_btree.h"
45#include "xfs_refcount.h"
46#include "xfs_bmap_btree.h"
47#include "xfs_trans_space.h"
48#include "xfs_bit.h"
49#include "xfs_alloc.h"
50#include "xfs_quota_defs.h"
51#include "xfs_quota.h"
52#include "xfs_btree.h"
53#include "xfs_bmap_btree.h"
54#include "xfs_reflink.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070055#include "xfs_iomap.h"
Darrick J. Wong43caeb12016-10-03 09:11:35 -070056#include "xfs_rmap_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070057
58/*
59 * Copy on Write of Shared Blocks
60 *
61 * XFS must preserve "the usual" file semantics even when two files share
62 * the same physical blocks. This means that a write to one file must not
63 * alter the blocks in a different file; the way that we'll do that is
64 * through the use of a copy-on-write mechanism. At a high level, that
65 * means that when we want to write to a shared block, we allocate a new
66 * block, write the data to the new block, and if that succeeds we map the
67 * new block into the file.
68 *
69 * XFS provides a "delayed allocation" mechanism that defers the allocation
70 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
71 * possible. This reduces fragmentation by enabling the filesystem to ask
72 * for bigger chunks less often, which is exactly what we want for CoW.
73 *
74 * The delalloc mechanism begins when the kernel wants to make a block
75 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
76 * create a delalloc mapping, which is a regular in-core extent, but without
77 * a real startblock. (For delalloc mappings, the startblock encodes both
78 * a flag that this is a delalloc mapping, and a worst-case estimate of how
79 * many blocks might be required to put the mapping into the BMBT.) delalloc
80 * mappings are a reservation against the free space in the filesystem;
81 * adjacent mappings can also be combined into fewer larger mappings.
82 *
83 * When dirty pages are being written out (typically in writepage), the
84 * delalloc reservations are converted into real mappings by allocating
85 * blocks and replacing the delalloc mapping with real ones. A delalloc
86 * mapping can be replaced by several real ones if the free space is
87 * fragmented.
88 *
89 * We want to adapt the delalloc mechanism for copy-on-write, since the
90 * write paths are similar. The first two steps (creating the reservation
91 * and allocating the blocks) are exactly the same as delalloc except that
92 * the mappings must be stored in a separate CoW fork because we do not want
93 * to disturb the mapping in the data fork until we're sure that the write
94 * succeeded. IO completion in this case is the process of removing the old
95 * mapping from the data fork and moving the new mapping from the CoW fork to
96 * the data fork. This will be discussed shortly.
97 *
98 * For now, unaligned directio writes will be bounced back to the page cache.
99 * Block-aligned directio writes will use the same mechanism as buffered
100 * writes.
101 *
102 * CoW remapping must be done after the data block write completes,
103 * because we don't want to destroy the old data fork map until we're sure
104 * the new block has been written. Since the new mappings are kept in a
105 * separate fork, we can simply iterate these mappings to find the ones
106 * that cover the file blocks that we just CoW'd. For each extent, simply
107 * unmap the corresponding range in the data fork, map the new range into
108 * the data fork, and remove the extent from the CoW fork.
109 *
110 * Since the remapping operation can be applied to an arbitrary file
111 * range, we record the need for the remap step as a flag in the ioend
112 * instead of declaring a new IO type. This is required for direct io
113 * because we only have ioend for the whole dio, and we have to be able to
114 * remember the presence of unwritten blocks and CoW blocks with a single
115 * ioend structure. Better yet, the more ground we can cover with one
116 * ioend, the better.
117 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700118
119/*
120 * Given an AG extent, find the lowest-numbered run of shared blocks
121 * within that range and return the range in fbno/flen. If
122 * find_end_of_shared is true, return the longest contiguous extent of
123 * shared blocks. If there are no shared extents, fbno and flen will
124 * be set to NULLAGBLOCK and 0, respectively.
125 */
126int
127xfs_reflink_find_shared(
128 struct xfs_mount *mp,
129 xfs_agnumber_t agno,
130 xfs_agblock_t agbno,
131 xfs_extlen_t aglen,
132 xfs_agblock_t *fbno,
133 xfs_extlen_t *flen,
134 bool find_end_of_shared)
135{
136 struct xfs_buf *agbp;
137 struct xfs_btree_cur *cur;
138 int error;
139
140 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
141 if (error)
142 return error;
143
144 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
145
146 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
147 find_end_of_shared);
148
149 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
150
151 xfs_buf_relse(agbp);
152 return error;
153}
154
155/*
156 * Trim the mapping to the next block where there's a change in the
157 * shared/unshared status. More specifically, this means that we
158 * find the lowest-numbered extent of shared blocks that coincides with
159 * the given block mapping. If the shared extent overlaps the start of
160 * the mapping, trim the mapping to the end of the shared extent. If
161 * the shared region intersects the mapping, trim the mapping to the
162 * start of the shared extent. If there are no shared regions that
163 * overlap, just return the original extent.
164 */
165int
166xfs_reflink_trim_around_shared(
167 struct xfs_inode *ip,
168 struct xfs_bmbt_irec *irec,
169 bool *shared,
170 bool *trimmed)
171{
172 xfs_agnumber_t agno;
173 xfs_agblock_t agbno;
174 xfs_extlen_t aglen;
175 xfs_agblock_t fbno;
176 xfs_extlen_t flen;
177 int error = 0;
178
179 /* Holes, unwritten, and delalloc extents cannot be shared */
180 if (!xfs_is_reflink_inode(ip) ||
181 ISUNWRITTEN(irec) ||
182 irec->br_startblock == HOLESTARTBLOCK ||
183 irec->br_startblock == DELAYSTARTBLOCK) {
184 *shared = false;
185 return 0;
186 }
187
188 trace_xfs_reflink_trim_around_shared(ip, irec);
189
190 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
191 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
192 aglen = irec->br_blockcount;
193
194 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
195 aglen, &fbno, &flen, true);
196 if (error)
197 return error;
198
199 *shared = *trimmed = false;
200 if (fbno == NULLAGBLOCK) {
201 /* No shared blocks at all. */
202 return 0;
203 } else if (fbno == agbno) {
204 /*
205 * The start of this extent is shared. Truncate the
206 * mapping at the end of the shared region so that a
207 * subsequent iteration starts at the start of the
208 * unshared region.
209 */
210 irec->br_blockcount = flen;
211 *shared = true;
212 if (flen != aglen)
213 *trimmed = true;
214 return 0;
215 } else {
216 /*
217 * There's a shared extent midway through this extent.
218 * Truncate the mapping at the start of the shared
219 * extent so that a subsequent iteration starts at the
220 * start of the shared region.
221 */
222 irec->br_blockcount = fbno - agbno;
223 *trimmed = true;
224 return 0;
225 }
226}
227
228/* Create a CoW reservation for a range of blocks within a file. */
229static int
230__xfs_reflink_reserve_cow(
231 struct xfs_inode *ip,
232 xfs_fileoff_t *offset_fsb,
Darrick J. Wong0613f162016-10-03 09:11:37 -0700233 xfs_fileoff_t end_fsb,
234 bool *skipped)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700235{
236 struct xfs_bmbt_irec got, prev, imap;
237 xfs_fileoff_t orig_end_fsb;
238 int nimaps, eof = 0, error = 0;
239 bool shared = false, trimmed = false;
240 xfs_extnum_t idx;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700241 xfs_extlen_t align;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700242
243 /* Already reserved? Skip the refcount btree access. */
244 xfs_bmap_search_extents(ip, *offset_fsb, XFS_COW_FORK, &eof, &idx,
245 &got, &prev);
246 if (!eof && got.br_startoff <= *offset_fsb) {
247 end_fsb = orig_end_fsb = got.br_startoff + got.br_blockcount;
248 trace_xfs_reflink_cow_found(ip, &got);
249 goto done;
250 }
251
252 /* Read extent from the source file. */
253 nimaps = 1;
254 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
255 &imap, &nimaps, 0);
256 if (error)
257 goto out_unlock;
258 ASSERT(nimaps == 1);
259
260 /* Trim the mapping to the nearest shared extent boundary. */
261 error = xfs_reflink_trim_around_shared(ip, &imap, &shared, &trimmed);
262 if (error)
263 goto out_unlock;
264
265 end_fsb = orig_end_fsb = imap.br_startoff + imap.br_blockcount;
266
267 /* Not shared? Just report the (potentially capped) extent. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700268 if (!shared) {
269 *skipped = true;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700270 goto done;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700271 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700272
273 /*
274 * Fork all the shared blocks from our write offset until the end of
275 * the extent.
276 */
277 error = xfs_qm_dqattach_locked(ip, 0);
278 if (error)
279 goto out_unlock;
280
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700281 align = xfs_eof_alignment(ip, xfs_get_cowextsz_hint(ip));
282 if (align)
283 end_fsb = roundup_64(end_fsb, align);
284
Darrick J. Wong2a067052016-10-03 09:11:33 -0700285retry:
286 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, *offset_fsb,
287 end_fsb - *offset_fsb, &got,
288 &prev, &idx, eof);
289 switch (error) {
290 case 0:
291 break;
292 case -ENOSPC:
293 case -EDQUOT:
294 /* retry without any preallocation */
295 trace_xfs_reflink_cow_enospc(ip, &imap);
296 if (end_fsb != orig_end_fsb) {
297 end_fsb = orig_end_fsb;
298 goto retry;
299 }
300 /*FALLTHRU*/
301 default:
302 goto out_unlock;
303 }
304
305 trace_xfs_reflink_cow_alloc(ip, &got);
306done:
307 *offset_fsb = end_fsb;
308out_unlock:
309 return error;
310}
311
312/* Create a CoW reservation for part of a file. */
313int
314xfs_reflink_reserve_cow_range(
315 struct xfs_inode *ip,
316 xfs_off_t offset,
317 xfs_off_t count)
318{
319 struct xfs_mount *mp = ip->i_mount;
320 xfs_fileoff_t offset_fsb, end_fsb;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700321 bool skipped = false;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700322 int error;
323
324 trace_xfs_reflink_reserve_cow_range(ip, offset, count);
325
326 offset_fsb = XFS_B_TO_FSBT(mp, offset);
327 end_fsb = XFS_B_TO_FSB(mp, offset + count);
328
329 xfs_ilock(ip, XFS_ILOCK_EXCL);
330 while (offset_fsb < end_fsb) {
Darrick J. Wong0613f162016-10-03 09:11:37 -0700331 error = __xfs_reflink_reserve_cow(ip, &offset_fsb, end_fsb,
332 &skipped);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700333 if (error) {
334 trace_xfs_reflink_reserve_cow_range_error(ip, error,
335 _RET_IP_);
336 break;
337 }
338 }
339 xfs_iunlock(ip, XFS_ILOCK_EXCL);
340
341 return error;
342}
Darrick J. Wongef473662016-10-03 09:11:34 -0700343
Darrick J. Wong0613f162016-10-03 09:11:37 -0700344/* Allocate all CoW reservations covering a range of blocks in a file. */
345static int
346__xfs_reflink_allocate_cow(
347 struct xfs_inode *ip,
348 xfs_fileoff_t *offset_fsb,
349 xfs_fileoff_t end_fsb)
350{
351 struct xfs_mount *mp = ip->i_mount;
352 struct xfs_bmbt_irec imap;
353 struct xfs_defer_ops dfops;
354 struct xfs_trans *tp;
355 xfs_fsblock_t first_block;
356 xfs_fileoff_t next_fsb;
357 int nimaps = 1, error;
358 bool skipped = false;
359
360 xfs_defer_init(&dfops, &first_block);
361
362 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
363 XFS_TRANS_RESERVE, &tp);
364 if (error)
365 return error;
366
367 xfs_ilock(ip, XFS_ILOCK_EXCL);
368
369 next_fsb = *offset_fsb;
370 error = __xfs_reflink_reserve_cow(ip, &next_fsb, end_fsb, &skipped);
371 if (error)
372 goto out_trans_cancel;
373
374 if (skipped) {
375 *offset_fsb = next_fsb;
376 goto out_trans_cancel;
377 }
378
379 xfs_trans_ijoin(tp, ip, 0);
380 error = xfs_bmapi_write(tp, ip, *offset_fsb, next_fsb - *offset_fsb,
381 XFS_BMAPI_COWFORK, &first_block,
382 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
383 &imap, &nimaps, &dfops);
384 if (error)
385 goto out_trans_cancel;
386
387 /* We might not have been able to map the whole delalloc extent */
388 *offset_fsb = min(*offset_fsb + imap.br_blockcount, next_fsb);
389
390 error = xfs_defer_finish(&tp, &dfops, NULL);
391 if (error)
392 goto out_trans_cancel;
393
394 error = xfs_trans_commit(tp);
395
396out_unlock:
397 xfs_iunlock(ip, XFS_ILOCK_EXCL);
398 return error;
399out_trans_cancel:
400 xfs_defer_cancel(&dfops);
401 xfs_trans_cancel(tp);
402 goto out_unlock;
403}
404
405/* Allocate all CoW reservations covering a part of a file. */
406int
407xfs_reflink_allocate_cow_range(
408 struct xfs_inode *ip,
409 xfs_off_t offset,
410 xfs_off_t count)
411{
412 struct xfs_mount *mp = ip->i_mount;
413 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
414 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
415 int error;
416
417 ASSERT(xfs_is_reflink_inode(ip));
418
419 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
420
421 /*
422 * Make sure that the dquots are there.
423 */
424 error = xfs_qm_dqattach(ip, 0);
425 if (error)
426 return error;
427
428 while (offset_fsb < end_fsb) {
429 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
430 if (error) {
431 trace_xfs_reflink_allocate_cow_range_error(ip, error,
432 _RET_IP_);
433 break;
434 }
435 }
436
437 return error;
438}
439
Darrick J. Wongef473662016-10-03 09:11:34 -0700440/*
441 * Find the CoW reservation (and whether or not it needs block allocation)
442 * for a given byte offset of a file.
443 */
444bool
445xfs_reflink_find_cow_mapping(
446 struct xfs_inode *ip,
447 xfs_off_t offset,
448 struct xfs_bmbt_irec *imap,
449 bool *need_alloc)
450{
451 struct xfs_bmbt_irec irec;
452 struct xfs_ifork *ifp;
453 struct xfs_bmbt_rec_host *gotp;
454 xfs_fileoff_t bno;
455 xfs_extnum_t idx;
456
457 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
458 ASSERT(xfs_is_reflink_inode(ip));
459
460 /* Find the extent in the CoW fork. */
461 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
462 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
463 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
464 if (!gotp)
465 return false;
466
467 xfs_bmbt_get_all(gotp, &irec);
468 if (bno >= irec.br_startoff + irec.br_blockcount ||
469 bno < irec.br_startoff)
470 return false;
471
472 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
473 &irec);
474
475 /* If it's still delalloc, we must allocate later. */
476 *imap = irec;
477 *need_alloc = !!(isnullstartblock(irec.br_startblock));
478
479 return true;
480}
481
482/*
483 * Trim an extent to end at the next CoW reservation past offset_fsb.
484 */
485int
486xfs_reflink_trim_irec_to_next_cow(
487 struct xfs_inode *ip,
488 xfs_fileoff_t offset_fsb,
489 struct xfs_bmbt_irec *imap)
490{
491 struct xfs_bmbt_irec irec;
492 struct xfs_ifork *ifp;
493 struct xfs_bmbt_rec_host *gotp;
494 xfs_extnum_t idx;
495
496 if (!xfs_is_reflink_inode(ip))
497 return 0;
498
499 /* Find the extent in the CoW fork. */
500 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
501 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
502 if (!gotp)
503 return 0;
504 xfs_bmbt_get_all(gotp, &irec);
505
506 /* This is the extent before; try sliding up one. */
507 if (irec.br_startoff < offset_fsb) {
508 idx++;
509 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
510 return 0;
511 gotp = xfs_iext_get_ext(ifp, idx);
512 xfs_bmbt_get_all(gotp, &irec);
513 }
514
515 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
516 return 0;
517
518 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
519 trace_xfs_reflink_trim_irec(ip, imap);
520
521 return 0;
522}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700523
524/*
525 * Cancel all pending CoW reservations for some block range of an inode.
526 */
527int
528xfs_reflink_cancel_cow_blocks(
529 struct xfs_inode *ip,
530 struct xfs_trans **tpp,
531 xfs_fileoff_t offset_fsb,
532 xfs_fileoff_t end_fsb)
533{
534 struct xfs_bmbt_irec irec;
535 xfs_filblks_t count_fsb;
536 xfs_fsblock_t firstfsb;
537 struct xfs_defer_ops dfops;
538 int error = 0;
539 int nimaps;
540
541 if (!xfs_is_reflink_inode(ip))
542 return 0;
543
544 /* Go find the old extent in the CoW fork. */
545 while (offset_fsb < end_fsb) {
546 nimaps = 1;
547 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
548 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
549 &nimaps, XFS_BMAPI_COWFORK);
550 if (error)
551 break;
552 ASSERT(nimaps == 1);
553
554 trace_xfs_reflink_cancel_cow(ip, &irec);
555
556 if (irec.br_startblock == DELAYSTARTBLOCK) {
557 /* Free a delayed allocation. */
558 xfs_mod_fdblocks(ip->i_mount, irec.br_blockcount,
559 false);
560 ip->i_delayed_blks -= irec.br_blockcount;
561
562 /* Remove the mapping from the CoW fork. */
563 error = xfs_bunmapi_cow(ip, &irec);
564 if (error)
565 break;
566 } else if (irec.br_startblock == HOLESTARTBLOCK) {
567 /* empty */
568 } else {
569 xfs_trans_ijoin(*tpp, ip, 0);
570 xfs_defer_init(&dfops, &firstfsb);
571
Darrick J. Wong174edb02016-10-03 09:11:39 -0700572 /* Free the CoW orphan record. */
573 error = xfs_refcount_free_cow_extent(ip->i_mount,
574 &dfops, irec.br_startblock,
575 irec.br_blockcount);
576 if (error)
577 break;
578
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700579 xfs_bmap_add_free(ip->i_mount, &dfops,
580 irec.br_startblock, irec.br_blockcount,
581 NULL);
582
583 /* Update quota accounting */
584 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
585 -(long)irec.br_blockcount);
586
587 /* Roll the transaction */
588 error = xfs_defer_finish(tpp, &dfops, ip);
589 if (error) {
590 xfs_defer_cancel(&dfops);
591 break;
592 }
593
594 /* Remove the mapping from the CoW fork. */
595 error = xfs_bunmapi_cow(ip, &irec);
596 if (error)
597 break;
598 }
599
600 /* Roll on... */
601 offset_fsb = irec.br_startoff + irec.br_blockcount;
602 }
603
604 return error;
605}
606
607/*
608 * Cancel all pending CoW reservations for some byte range of an inode.
609 */
610int
611xfs_reflink_cancel_cow_range(
612 struct xfs_inode *ip,
613 xfs_off_t offset,
614 xfs_off_t count)
615{
616 struct xfs_trans *tp;
617 xfs_fileoff_t offset_fsb;
618 xfs_fileoff_t end_fsb;
619 int error;
620
621 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
622
623 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
624 if (count == NULLFILEOFF)
625 end_fsb = NULLFILEOFF;
626 else
627 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
628
629 /* Start a rolling transaction to remove the mappings */
630 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
631 0, 0, 0, &tp);
632 if (error)
633 goto out;
634
635 xfs_ilock(ip, XFS_ILOCK_EXCL);
636 xfs_trans_ijoin(tp, ip, 0);
637
638 /* Scrape out the old CoW reservations */
639 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
640 if (error)
641 goto out_cancel;
642
643 error = xfs_trans_commit(tp);
644
645 xfs_iunlock(ip, XFS_ILOCK_EXCL);
646 return error;
647
648out_cancel:
649 xfs_trans_cancel(tp);
650 xfs_iunlock(ip, XFS_ILOCK_EXCL);
651out:
652 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
653 return error;
654}
655
656/*
657 * Remap parts of a file's data fork after a successful CoW.
658 */
659int
660xfs_reflink_end_cow(
661 struct xfs_inode *ip,
662 xfs_off_t offset,
663 xfs_off_t count)
664{
665 struct xfs_bmbt_irec irec;
666 struct xfs_bmbt_irec uirec;
667 struct xfs_trans *tp;
668 xfs_fileoff_t offset_fsb;
669 xfs_fileoff_t end_fsb;
670 xfs_filblks_t count_fsb;
671 xfs_fsblock_t firstfsb;
672 struct xfs_defer_ops dfops;
673 int error;
674 unsigned int resblks;
675 xfs_filblks_t ilen;
676 xfs_filblks_t rlen;
677 int nimaps;
678
679 trace_xfs_reflink_end_cow(ip, offset, count);
680
681 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
682 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
683 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
684
685 /* Start a rolling transaction to switch the mappings */
686 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
687 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
688 resblks, 0, 0, &tp);
689 if (error)
690 goto out;
691
692 xfs_ilock(ip, XFS_ILOCK_EXCL);
693 xfs_trans_ijoin(tp, ip, 0);
694
695 /* Go find the old extent in the CoW fork. */
696 while (offset_fsb < end_fsb) {
697 /* Read extent from the source file */
698 nimaps = 1;
699 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
700 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
701 &nimaps, XFS_BMAPI_COWFORK);
702 if (error)
703 goto out_cancel;
704 ASSERT(nimaps == 1);
705
706 ASSERT(irec.br_startblock != DELAYSTARTBLOCK);
707 trace_xfs_reflink_cow_remap(ip, &irec);
708
709 /*
710 * We can have a hole in the CoW fork if part of a directio
711 * write is CoW but part of it isn't.
712 */
713 rlen = ilen = irec.br_blockcount;
714 if (irec.br_startblock == HOLESTARTBLOCK)
715 goto next_extent;
716
717 /* Unmap the old blocks in the data fork. */
718 while (rlen) {
719 xfs_defer_init(&dfops, &firstfsb);
720 error = __xfs_bunmapi(tp, ip, irec.br_startoff,
721 &rlen, 0, 1, &firstfsb, &dfops);
722 if (error)
723 goto out_defer;
724
725 /*
726 * Trim the extent to whatever got unmapped.
727 * Remember, bunmapi works backwards.
728 */
729 uirec.br_startblock = irec.br_startblock + rlen;
730 uirec.br_startoff = irec.br_startoff + rlen;
731 uirec.br_blockcount = irec.br_blockcount - rlen;
732 irec.br_blockcount = rlen;
733 trace_xfs_reflink_cow_remap_piece(ip, &uirec);
734
Darrick J. Wong174edb02016-10-03 09:11:39 -0700735 /* Free the CoW orphan record. */
736 error = xfs_refcount_free_cow_extent(tp->t_mountp,
737 &dfops, uirec.br_startblock,
738 uirec.br_blockcount);
739 if (error)
740 goto out_defer;
741
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700742 /* Map the new blocks into the data fork. */
743 error = xfs_bmap_map_extent(tp->t_mountp, &dfops,
744 ip, &uirec);
745 if (error)
746 goto out_defer;
747
748 /* Remove the mapping from the CoW fork. */
749 error = xfs_bunmapi_cow(ip, &uirec);
750 if (error)
751 goto out_defer;
752
753 error = xfs_defer_finish(&tp, &dfops, ip);
754 if (error)
755 goto out_defer;
756 }
757
758next_extent:
759 /* Roll on... */
760 offset_fsb = irec.br_startoff + ilen;
761 }
762
763 error = xfs_trans_commit(tp);
764 xfs_iunlock(ip, XFS_ILOCK_EXCL);
765 if (error)
766 goto out;
767 return 0;
768
769out_defer:
770 xfs_defer_cancel(&dfops);
771out_cancel:
772 xfs_trans_cancel(tp);
773 xfs_iunlock(ip, XFS_ILOCK_EXCL);
774out:
775 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
776 return error;
777}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700778
779/*
780 * Free leftover CoW reservations that didn't get cleaned out.
781 */
782int
783xfs_reflink_recover_cow(
784 struct xfs_mount *mp)
785{
786 xfs_agnumber_t agno;
787 int error = 0;
788
789 if (!xfs_sb_version_hasreflink(&mp->m_sb))
790 return 0;
791
792 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
793 error = xfs_refcount_recover_cow_leftovers(mp, agno);
794 if (error)
795 break;
796 }
797
798 return error;
799}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700800
801/*
802 * Reflinking (Block) Ranges of Two Files Together
803 *
804 * First, ensure that the reflink flag is set on both inodes. The flag is an
805 * optimization to avoid unnecessary refcount btree lookups in the write path.
806 *
807 * Now we can iteratively remap the range of extents (and holes) in src to the
808 * corresponding ranges in dest. Let drange and srange denote the ranges of
809 * logical blocks in dest and src touched by the reflink operation.
810 *
811 * While the length of drange is greater than zero,
812 * - Read src's bmbt at the start of srange ("imap")
813 * - If imap doesn't exist, make imap appear to start at the end of srange
814 * with zero length.
815 * - If imap starts before srange, advance imap to start at srange.
816 * - If imap goes beyond srange, truncate imap to end at the end of srange.
817 * - Punch (imap start - srange start + imap len) blocks from dest at
818 * offset (drange start).
819 * - If imap points to a real range of pblks,
820 * > Increase the refcount of the imap's pblks
821 * > Map imap's pblks into dest at the offset
822 * (drange start + imap start - srange start)
823 * - Advance drange and srange by (imap start - srange start + imap len)
824 *
825 * Finally, if the reflink made dest longer, update both the in-core and
826 * on-disk file sizes.
827 *
828 * ASCII Art Demonstration:
829 *
830 * Let's say we want to reflink this source file:
831 *
832 * ----SSSSSSS-SSSSS----SSSSSS (src file)
833 * <-------------------->
834 *
835 * into this destination file:
836 *
837 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
838 * <-------------------->
839 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
840 * Observe that the range has different logical offsets in either file.
841 *
842 * Consider that the first extent in the source file doesn't line up with our
843 * reflink range. Unmapping and remapping are separate operations, so we can
844 * unmap more blocks from the destination file than we remap.
845 *
846 * ----SSSSSSS-SSSSS----SSSSSS
847 * <------->
848 * --DDDDD---------DDDDD--DDD
849 * <------->
850 *
851 * Now remap the source extent into the destination file:
852 *
853 * ----SSSSSSS-SSSSS----SSSSSS
854 * <------->
855 * --DDDDD--SSSSSSSDDDDD--DDD
856 * <------->
857 *
858 * Do likewise with the second hole and extent in our range. Holes in the
859 * unmap range don't affect our operation.
860 *
861 * ----SSSSSSS-SSSSS----SSSSSS
862 * <---->
863 * --DDDDD--SSSSSSS-SSSSS-DDD
864 * <---->
865 *
866 * Finally, unmap and remap part of the third extent. This will increase the
867 * size of the destination file.
868 *
869 * ----SSSSSSS-SSSSS----SSSSSS
870 * <----->
871 * --DDDDD--SSSSSSS-SSSSS----SSS
872 * <----->
873 *
874 * Once we update the destination file's i_size, we're done.
875 */
876
877/*
878 * Ensure the reflink bit is set in both inodes.
879 */
880STATIC int
881xfs_reflink_set_inode_flag(
882 struct xfs_inode *src,
883 struct xfs_inode *dest)
884{
885 struct xfs_mount *mp = src->i_mount;
886 int error;
887 struct xfs_trans *tp;
888
889 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
890 return 0;
891
892 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
893 if (error)
894 goto out_error;
895
896 /* Lock both files against IO */
897 if (src->i_ino == dest->i_ino)
898 xfs_ilock(src, XFS_ILOCK_EXCL);
899 else
900 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
901
902 if (!xfs_is_reflink_inode(src)) {
903 trace_xfs_reflink_set_inode_flag(src);
904 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
905 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
906 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
907 xfs_ifork_init_cow(src);
908 } else
909 xfs_iunlock(src, XFS_ILOCK_EXCL);
910
911 if (src->i_ino == dest->i_ino)
912 goto commit_flags;
913
914 if (!xfs_is_reflink_inode(dest)) {
915 trace_xfs_reflink_set_inode_flag(dest);
916 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
917 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
918 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
919 xfs_ifork_init_cow(dest);
920 } else
921 xfs_iunlock(dest, XFS_ILOCK_EXCL);
922
923commit_flags:
924 error = xfs_trans_commit(tp);
925 if (error)
926 goto out_error;
927 return error;
928
929out_error:
930 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
931 return error;
932}
933
934/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700935 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700936 */
937STATIC int
938xfs_reflink_update_dest(
939 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700940 xfs_off_t newlen,
941 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700942{
943 struct xfs_mount *mp = dest->i_mount;
944 struct xfs_trans *tp;
945 int error;
946
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700947 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700948 return 0;
949
950 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
951 if (error)
952 goto out_error;
953
954 xfs_ilock(dest, XFS_ILOCK_EXCL);
955 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
956
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700957 if (newlen > i_size_read(VFS_I(dest))) {
958 trace_xfs_reflink_update_inode_size(dest, newlen);
959 i_size_write(VFS_I(dest), newlen);
960 dest->i_d.di_size = newlen;
961 }
962
963 if (cowextsize) {
964 dest->i_d.di_cowextsize = cowextsize;
965 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
966 }
967
Darrick J. Wong862bb362016-10-03 09:11:40 -0700968 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
969
970 error = xfs_trans_commit(tp);
971 if (error)
972 goto out_error;
973 return error;
974
975out_error:
976 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
977 return error;
978}
979
980/*
981 * Unmap a range of blocks from a file, then map other blocks into the hole.
982 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
983 * The extent irec is mapped into dest at irec->br_startoff.
984 */
985STATIC int
986xfs_reflink_remap_extent(
987 struct xfs_inode *ip,
988 struct xfs_bmbt_irec *irec,
989 xfs_fileoff_t destoff,
990 xfs_off_t new_isize)
991{
992 struct xfs_mount *mp = ip->i_mount;
993 struct xfs_trans *tp;
994 xfs_fsblock_t firstfsb;
995 unsigned int resblks;
996 struct xfs_defer_ops dfops;
997 struct xfs_bmbt_irec uirec;
998 bool real_extent;
999 xfs_filblks_t rlen;
1000 xfs_filblks_t unmap_len;
1001 xfs_off_t newlen;
1002 int error;
1003
1004 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1005 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1006
1007 /* Only remap normal extents. */
1008 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1009 irec->br_startblock != DELAYSTARTBLOCK &&
1010 !ISUNWRITTEN(irec));
1011
1012 /* Start a rolling transaction to switch the mappings */
1013 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1014 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1015 if (error)
1016 goto out;
1017
1018 xfs_ilock(ip, XFS_ILOCK_EXCL);
1019 xfs_trans_ijoin(tp, ip, 0);
1020
1021 /* If we're not just clearing space, then do we have enough quota? */
1022 if (real_extent) {
1023 error = xfs_trans_reserve_quota_nblks(tp, ip,
1024 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1025 if (error)
1026 goto out_cancel;
1027 }
1028
1029 trace_xfs_reflink_remap(ip, irec->br_startoff,
1030 irec->br_blockcount, irec->br_startblock);
1031
1032 /* Unmap the old blocks in the data fork. */
1033 rlen = unmap_len;
1034 while (rlen) {
1035 xfs_defer_init(&dfops, &firstfsb);
1036 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1037 &firstfsb, &dfops);
1038 if (error)
1039 goto out_defer;
1040
1041 /*
1042 * Trim the extent to whatever got unmapped.
1043 * Remember, bunmapi works backwards.
1044 */
1045 uirec.br_startblock = irec->br_startblock + rlen;
1046 uirec.br_startoff = irec->br_startoff + rlen;
1047 uirec.br_blockcount = unmap_len - rlen;
1048 unmap_len = rlen;
1049
1050 /* If this isn't a real mapping, we're done. */
1051 if (!real_extent || uirec.br_blockcount == 0)
1052 goto next_extent;
1053
1054 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1055 uirec.br_blockcount, uirec.br_startblock);
1056
1057 /* Update the refcount tree */
1058 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1059 if (error)
1060 goto out_defer;
1061
1062 /* Map the new blocks into the data fork. */
1063 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1064 if (error)
1065 goto out_defer;
1066
1067 /* Update quota accounting. */
1068 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1069 uirec.br_blockcount);
1070
1071 /* Update dest isize if needed. */
1072 newlen = XFS_FSB_TO_B(mp,
1073 uirec.br_startoff + uirec.br_blockcount);
1074 newlen = min_t(xfs_off_t, newlen, new_isize);
1075 if (newlen > i_size_read(VFS_I(ip))) {
1076 trace_xfs_reflink_update_inode_size(ip, newlen);
1077 i_size_write(VFS_I(ip), newlen);
1078 ip->i_d.di_size = newlen;
1079 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1080 }
1081
1082next_extent:
1083 /* Process all the deferred stuff. */
1084 error = xfs_defer_finish(&tp, &dfops, ip);
1085 if (error)
1086 goto out_defer;
1087 }
1088
1089 error = xfs_trans_commit(tp);
1090 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1091 if (error)
1092 goto out;
1093 return 0;
1094
1095out_defer:
1096 xfs_defer_cancel(&dfops);
1097out_cancel:
1098 xfs_trans_cancel(tp);
1099 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1100out:
1101 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1102 return error;
1103}
1104
1105/*
1106 * Iteratively remap one file's extents (and holes) to another's.
1107 */
1108STATIC int
1109xfs_reflink_remap_blocks(
1110 struct xfs_inode *src,
1111 xfs_fileoff_t srcoff,
1112 struct xfs_inode *dest,
1113 xfs_fileoff_t destoff,
1114 xfs_filblks_t len,
1115 xfs_off_t new_isize)
1116{
1117 struct xfs_bmbt_irec imap;
1118 int nimaps;
1119 int error = 0;
1120 xfs_filblks_t range_len;
1121
1122 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1123 while (len) {
1124 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1125 dest, destoff);
1126 /* Read extent from the source file */
1127 nimaps = 1;
1128 xfs_ilock(src, XFS_ILOCK_EXCL);
1129 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1130 xfs_iunlock(src, XFS_ILOCK_EXCL);
1131 if (error)
1132 goto err;
1133 ASSERT(nimaps == 1);
1134
1135 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1136 &imap);
1137
1138 /* Translate imap into the destination file. */
1139 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1140 imap.br_startoff += destoff - srcoff;
1141
1142 /* Clear dest from destoff to the end of imap and map it in. */
1143 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1144 new_isize);
1145 if (error)
1146 goto err;
1147
1148 if (fatal_signal_pending(current)) {
1149 error = -EINTR;
1150 goto err;
1151 }
1152
1153 /* Advance drange/srange */
1154 srcoff += range_len;
1155 destoff += range_len;
1156 len -= range_len;
1157 }
1158
1159 return 0;
1160
1161err:
1162 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1163 return error;
1164}
1165
1166/*
Darrick J. Wongcc714662016-10-03 09:11:41 -07001167 * Read a page's worth of file data into the page cache. Return the page
1168 * locked.
1169 */
1170static struct page *
1171xfs_get_page(
1172 struct inode *inode,
1173 xfs_off_t offset)
1174{
1175 struct address_space *mapping;
1176 struct page *page;
1177 pgoff_t n;
1178
1179 n = offset >> PAGE_SHIFT;
1180 mapping = inode->i_mapping;
1181 page = read_mapping_page(mapping, n, NULL);
1182 if (IS_ERR(page))
1183 return page;
1184 if (!PageUptodate(page)) {
1185 put_page(page);
1186 return ERR_PTR(-EIO);
1187 }
1188 lock_page(page);
1189 return page;
1190}
1191
1192/*
1193 * Compare extents of two files to see if they are the same.
1194 */
1195static int
1196xfs_compare_extents(
1197 struct inode *src,
1198 xfs_off_t srcoff,
1199 struct inode *dest,
1200 xfs_off_t destoff,
1201 xfs_off_t len,
1202 bool *is_same)
1203{
1204 xfs_off_t src_poff;
1205 xfs_off_t dest_poff;
1206 void *src_addr;
1207 void *dest_addr;
1208 struct page *src_page;
1209 struct page *dest_page;
1210 xfs_off_t cmp_len;
1211 bool same;
1212 int error;
1213
1214 error = -EINVAL;
1215 same = true;
1216 while (len) {
1217 src_poff = srcoff & (PAGE_SIZE - 1);
1218 dest_poff = destoff & (PAGE_SIZE - 1);
1219 cmp_len = min(PAGE_SIZE - src_poff,
1220 PAGE_SIZE - dest_poff);
1221 cmp_len = min(cmp_len, len);
1222 ASSERT(cmp_len > 0);
1223
1224 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1225 XFS_I(dest), destoff);
1226
1227 src_page = xfs_get_page(src, srcoff);
1228 if (IS_ERR(src_page)) {
1229 error = PTR_ERR(src_page);
1230 goto out_error;
1231 }
1232 dest_page = xfs_get_page(dest, destoff);
1233 if (IS_ERR(dest_page)) {
1234 error = PTR_ERR(dest_page);
1235 unlock_page(src_page);
1236 put_page(src_page);
1237 goto out_error;
1238 }
1239 src_addr = kmap_atomic(src_page);
1240 dest_addr = kmap_atomic(dest_page);
1241
1242 flush_dcache_page(src_page);
1243 flush_dcache_page(dest_page);
1244
1245 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1246 same = false;
1247
1248 kunmap_atomic(dest_addr);
1249 kunmap_atomic(src_addr);
1250 unlock_page(dest_page);
1251 unlock_page(src_page);
1252 put_page(dest_page);
1253 put_page(src_page);
1254
1255 if (!same)
1256 break;
1257
1258 srcoff += cmp_len;
1259 destoff += cmp_len;
1260 len -= cmp_len;
1261 }
1262
1263 *is_same = same;
1264 return 0;
1265
1266out_error:
1267 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1268 return error;
1269}
1270
1271/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001272 * Link a range of blocks from one file to another.
1273 */
1274int
1275xfs_reflink_remap_range(
1276 struct xfs_inode *src,
1277 xfs_off_t srcoff,
1278 struct xfs_inode *dest,
1279 xfs_off_t destoff,
Darrick J. Wongcc714662016-10-03 09:11:41 -07001280 xfs_off_t len,
1281 unsigned int flags)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001282{
1283 struct xfs_mount *mp = src->i_mount;
1284 xfs_fileoff_t sfsbno, dfsbno;
1285 xfs_filblks_t fsblen;
1286 int error;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001287 xfs_extlen_t cowextsize;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001288 bool is_same;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001289
1290 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1291 return -EOPNOTSUPP;
1292
1293 if (XFS_FORCED_SHUTDOWN(mp))
1294 return -EIO;
1295
1296 /* Don't reflink realtime inodes */
1297 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1298 return -EINVAL;
1299
Darrick J. Wongcc714662016-10-03 09:11:41 -07001300 if (flags & ~XFS_REFLINK_ALL)
1301 return -EINVAL;
1302
Darrick J. Wong862bb362016-10-03 09:11:40 -07001303 trace_xfs_reflink_remap_range(src, srcoff, len, dest, destoff);
1304
1305 /* Lock both files against IO */
1306 if (src->i_ino == dest->i_ino) {
1307 xfs_ilock(src, XFS_IOLOCK_EXCL);
1308 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1309 } else {
1310 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1311 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1312 }
1313
Darrick J. Wongcc714662016-10-03 09:11:41 -07001314 /*
1315 * Check that the extents are the same.
1316 */
1317 if (flags & XFS_REFLINK_DEDUPE) {
1318 is_same = false;
1319 error = xfs_compare_extents(VFS_I(src), srcoff, VFS_I(dest),
1320 destoff, len, &is_same);
1321 if (error)
1322 goto out_error;
1323 if (!is_same) {
1324 error = -EBADE;
1325 goto out_error;
1326 }
1327 }
1328
Darrick J. Wong862bb362016-10-03 09:11:40 -07001329 error = xfs_reflink_set_inode_flag(src, dest);
1330 if (error)
1331 goto out_error;
1332
1333 /*
1334 * Invalidate the page cache so that we can clear any CoW mappings
1335 * in the destination file.
1336 */
1337 truncate_inode_pages_range(&VFS_I(dest)->i_data, destoff,
1338 PAGE_ALIGN(destoff + len) - 1);
1339
1340 dfsbno = XFS_B_TO_FSBT(mp, destoff);
1341 sfsbno = XFS_B_TO_FSBT(mp, srcoff);
1342 fsblen = XFS_B_TO_FSB(mp, len);
1343 error = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1344 destoff + len);
1345 if (error)
1346 goto out_error;
1347
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001348 /*
1349 * Carry the cowextsize hint from src to dest if we're sharing the
1350 * entire source file to the entire destination file, the source file
1351 * has a cowextsize hint, and the destination file does not.
1352 */
1353 cowextsize = 0;
1354 if (srcoff == 0 && len == i_size_read(VFS_I(src)) &&
1355 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1356 destoff == 0 && len >= i_size_read(VFS_I(dest)) &&
1357 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1358 cowextsize = src->i_d.di_cowextsize;
1359
1360 error = xfs_reflink_update_dest(dest, destoff + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001361 if (error)
1362 goto out_error;
1363
1364out_error:
1365 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1366 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1367 if (src->i_ino != dest->i_ino) {
1368 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1369 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1370 }
1371 if (error)
1372 trace_xfs_reflink_remap_range_error(dest, error, _RET_IP_);
1373 return error;
1374}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001375
1376/*
1377 * The user wants to preemptively CoW all shared blocks in this file,
1378 * which enables us to turn off the reflink flag. Iterate all
1379 * extents which are not prealloc/delalloc to see which ranges are
1380 * mentioned in the refcount tree, then read those blocks into the
1381 * pagecache, dirty them, fsync them back out, and then we can update
1382 * the inode flag. What happens if we run out of memory? :)
1383 */
1384STATIC int
1385xfs_reflink_dirty_extents(
1386 struct xfs_inode *ip,
1387 xfs_fileoff_t fbno,
1388 xfs_filblks_t end,
1389 xfs_off_t isize)
1390{
1391 struct xfs_mount *mp = ip->i_mount;
1392 xfs_agnumber_t agno;
1393 xfs_agblock_t agbno;
1394 xfs_extlen_t aglen;
1395 xfs_agblock_t rbno;
1396 xfs_extlen_t rlen;
1397 xfs_off_t fpos;
1398 xfs_off_t flen;
1399 struct xfs_bmbt_irec map[2];
1400 int nmaps;
1401 int error;
1402
1403 while (end - fbno > 0) {
1404 nmaps = 1;
1405 /*
1406 * Look for extents in the file. Skip holes, delalloc, or
1407 * unwritten extents; they can't be reflinked.
1408 */
1409 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1410 if (error)
1411 goto out;
1412 if (nmaps == 0)
1413 break;
1414 if (map[0].br_startblock == HOLESTARTBLOCK ||
1415 map[0].br_startblock == DELAYSTARTBLOCK ||
1416 ISUNWRITTEN(&map[0]))
1417 goto next;
1418
1419 map[1] = map[0];
1420 while (map[1].br_blockcount) {
1421 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1422 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1423 aglen = map[1].br_blockcount;
1424
1425 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1426 &rbno, &rlen, true);
1427 if (error)
1428 goto out;
1429 if (rbno == NULLAGBLOCK)
1430 break;
1431
1432 /* Dirty the pages */
1433 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1434 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1435 (rbno - agbno));
1436 flen = XFS_FSB_TO_B(mp, rlen);
1437 if (fpos + flen > isize)
1438 flen = isize - fpos;
1439 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1440 &xfs_iomap_ops);
1441 xfs_ilock(ip, XFS_ILOCK_EXCL);
1442 if (error)
1443 goto out;
1444
1445 map[1].br_blockcount -= (rbno - agbno + rlen);
1446 map[1].br_startoff += (rbno - agbno + rlen);
1447 map[1].br_startblock += (rbno - agbno + rlen);
1448 }
1449
1450next:
1451 fbno = map[0].br_startoff + map[0].br_blockcount;
1452 }
1453out:
1454 return error;
1455}
1456
1457/* Clear the inode reflink flag if there are no shared extents. */
1458int
1459xfs_reflink_clear_inode_flag(
1460 struct xfs_inode *ip,
1461 struct xfs_trans **tpp)
1462{
1463 struct xfs_mount *mp = ip->i_mount;
1464 xfs_fileoff_t fbno;
1465 xfs_filblks_t end;
1466 xfs_agnumber_t agno;
1467 xfs_agblock_t agbno;
1468 xfs_extlen_t aglen;
1469 xfs_agblock_t rbno;
1470 xfs_extlen_t rlen;
1471 struct xfs_bmbt_irec map[2];
1472 int nmaps;
1473 int error = 0;
1474
1475 if (!(ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK))
1476 return 0;
1477
1478 fbno = 0;
1479 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1480 while (end - fbno > 0) {
1481 nmaps = 1;
1482 /*
1483 * Look for extents in the file. Skip holes, delalloc, or
1484 * unwritten extents; they can't be reflinked.
1485 */
1486 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1487 if (error)
1488 return error;
1489 if (nmaps == 0)
1490 break;
1491 if (map[0].br_startblock == HOLESTARTBLOCK ||
1492 map[0].br_startblock == DELAYSTARTBLOCK ||
1493 ISUNWRITTEN(&map[0]))
1494 goto next;
1495
1496 map[1] = map[0];
1497 while (map[1].br_blockcount) {
1498 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1499 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1500 aglen = map[1].br_blockcount;
1501
1502 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1503 &rbno, &rlen, false);
1504 if (error)
1505 return error;
1506 /* Is there still a shared block here? */
1507 if (rbno != NULLAGBLOCK)
1508 return 0;
1509
1510 map[1].br_blockcount -= aglen;
1511 map[1].br_startoff += aglen;
1512 map[1].br_startblock += aglen;
1513 }
1514
1515next:
1516 fbno = map[0].br_startoff + map[0].br_blockcount;
1517 }
1518
1519 /*
1520 * We didn't find any shared blocks so turn off the reflink flag.
1521 * First, get rid of any leftover CoW mappings.
1522 */
1523 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1524 if (error)
1525 return error;
1526
1527 /* Clear the inode flag. */
1528 trace_xfs_reflink_unset_inode_flag(ip);
1529 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1530 xfs_trans_ijoin(*tpp, ip, 0);
1531 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1532
1533 return error;
1534}
1535
1536/*
1537 * Clear the inode reflink flag if there are no shared extents and the size
1538 * hasn't changed.
1539 */
1540STATIC int
1541xfs_reflink_try_clear_inode_flag(
1542 struct xfs_inode *ip,
1543 xfs_off_t old_isize)
1544{
1545 struct xfs_mount *mp = ip->i_mount;
1546 struct xfs_trans *tp;
1547 int error = 0;
1548
1549 /* Start a rolling transaction to remove the mappings */
1550 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1551 if (error)
1552 return error;
1553
1554 xfs_ilock(ip, XFS_ILOCK_EXCL);
1555 xfs_trans_ijoin(tp, ip, 0);
1556
1557 if (old_isize != i_size_read(VFS_I(ip)))
1558 goto cancel;
1559
1560 error = xfs_reflink_clear_inode_flag(ip, &tp);
1561 if (error)
1562 goto cancel;
1563
1564 error = xfs_trans_commit(tp);
1565 if (error)
1566 goto out;
1567
1568 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1569 return 0;
1570cancel:
1571 xfs_trans_cancel(tp);
1572out:
1573 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1574 return error;
1575}
1576
1577/*
1578 * Pre-COW all shared blocks within a given byte range of a file and turn off
1579 * the reflink flag if we unshare all of the file's blocks.
1580 */
1581int
1582xfs_reflink_unshare(
1583 struct xfs_inode *ip,
1584 xfs_off_t offset,
1585 xfs_off_t len)
1586{
1587 struct xfs_mount *mp = ip->i_mount;
1588 xfs_fileoff_t fbno;
1589 xfs_filblks_t end;
1590 xfs_off_t isize;
1591 int error;
1592
1593 if (!xfs_is_reflink_inode(ip))
1594 return 0;
1595
1596 trace_xfs_reflink_unshare(ip, offset, len);
1597
1598 inode_dio_wait(VFS_I(ip));
1599
1600 /* Try to CoW the selected ranges */
1601 xfs_ilock(ip, XFS_ILOCK_EXCL);
1602 fbno = XFS_B_TO_FSB(mp, offset);
1603 isize = i_size_read(VFS_I(ip));
1604 end = XFS_B_TO_FSB(mp, offset + len);
1605 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1606 if (error)
1607 goto out_unlock;
1608 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1609
1610 /* Wait for the IO to finish */
1611 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1612 if (error)
1613 goto out;
1614
1615 /* Turn off the reflink flag if we unshared the whole file */
1616 if (offset == 0 && len == isize) {
1617 error = xfs_reflink_try_clear_inode_flag(ip, isize);
1618 if (error)
1619 goto out;
1620 }
1621
1622 return 0;
1623
1624out_unlock:
1625 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1626out:
1627 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1628 return error;
1629}