blob: c1e98a43a9379d4696d8391e21c477c4f3db99d0 [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;
241
242 /* Already reserved? Skip the refcount btree access. */
243 xfs_bmap_search_extents(ip, *offset_fsb, XFS_COW_FORK, &eof, &idx,
244 &got, &prev);
245 if (!eof && got.br_startoff <= *offset_fsb) {
246 end_fsb = orig_end_fsb = got.br_startoff + got.br_blockcount;
247 trace_xfs_reflink_cow_found(ip, &got);
248 goto done;
249 }
250
251 /* Read extent from the source file. */
252 nimaps = 1;
253 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
254 &imap, &nimaps, 0);
255 if (error)
256 goto out_unlock;
257 ASSERT(nimaps == 1);
258
259 /* Trim the mapping to the nearest shared extent boundary. */
260 error = xfs_reflink_trim_around_shared(ip, &imap, &shared, &trimmed);
261 if (error)
262 goto out_unlock;
263
264 end_fsb = orig_end_fsb = imap.br_startoff + imap.br_blockcount;
265
266 /* Not shared? Just report the (potentially capped) extent. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700267 if (!shared) {
268 *skipped = true;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700269 goto done;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700270 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700271
272 /*
273 * Fork all the shared blocks from our write offset until the end of
274 * the extent.
275 */
276 error = xfs_qm_dqattach_locked(ip, 0);
277 if (error)
278 goto out_unlock;
279
280retry:
281 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, *offset_fsb,
282 end_fsb - *offset_fsb, &got,
283 &prev, &idx, eof);
284 switch (error) {
285 case 0:
286 break;
287 case -ENOSPC:
288 case -EDQUOT:
289 /* retry without any preallocation */
290 trace_xfs_reflink_cow_enospc(ip, &imap);
291 if (end_fsb != orig_end_fsb) {
292 end_fsb = orig_end_fsb;
293 goto retry;
294 }
295 /*FALLTHRU*/
296 default:
297 goto out_unlock;
298 }
299
300 trace_xfs_reflink_cow_alloc(ip, &got);
301done:
302 *offset_fsb = end_fsb;
303out_unlock:
304 return error;
305}
306
307/* Create a CoW reservation for part of a file. */
308int
309xfs_reflink_reserve_cow_range(
310 struct xfs_inode *ip,
311 xfs_off_t offset,
312 xfs_off_t count)
313{
314 struct xfs_mount *mp = ip->i_mount;
315 xfs_fileoff_t offset_fsb, end_fsb;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700316 bool skipped = false;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700317 int error;
318
319 trace_xfs_reflink_reserve_cow_range(ip, offset, count);
320
321 offset_fsb = XFS_B_TO_FSBT(mp, offset);
322 end_fsb = XFS_B_TO_FSB(mp, offset + count);
323
324 xfs_ilock(ip, XFS_ILOCK_EXCL);
325 while (offset_fsb < end_fsb) {
Darrick J. Wong0613f162016-10-03 09:11:37 -0700326 error = __xfs_reflink_reserve_cow(ip, &offset_fsb, end_fsb,
327 &skipped);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700328 if (error) {
329 trace_xfs_reflink_reserve_cow_range_error(ip, error,
330 _RET_IP_);
331 break;
332 }
333 }
334 xfs_iunlock(ip, XFS_ILOCK_EXCL);
335
336 return error;
337}
Darrick J. Wongef473662016-10-03 09:11:34 -0700338
Darrick J. Wong0613f162016-10-03 09:11:37 -0700339/* Allocate all CoW reservations covering a range of blocks in a file. */
340static int
341__xfs_reflink_allocate_cow(
342 struct xfs_inode *ip,
343 xfs_fileoff_t *offset_fsb,
344 xfs_fileoff_t end_fsb)
345{
346 struct xfs_mount *mp = ip->i_mount;
347 struct xfs_bmbt_irec imap;
348 struct xfs_defer_ops dfops;
349 struct xfs_trans *tp;
350 xfs_fsblock_t first_block;
351 xfs_fileoff_t next_fsb;
352 int nimaps = 1, error;
353 bool skipped = false;
354
355 xfs_defer_init(&dfops, &first_block);
356
357 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
358 XFS_TRANS_RESERVE, &tp);
359 if (error)
360 return error;
361
362 xfs_ilock(ip, XFS_ILOCK_EXCL);
363
364 next_fsb = *offset_fsb;
365 error = __xfs_reflink_reserve_cow(ip, &next_fsb, end_fsb, &skipped);
366 if (error)
367 goto out_trans_cancel;
368
369 if (skipped) {
370 *offset_fsb = next_fsb;
371 goto out_trans_cancel;
372 }
373
374 xfs_trans_ijoin(tp, ip, 0);
375 error = xfs_bmapi_write(tp, ip, *offset_fsb, next_fsb - *offset_fsb,
376 XFS_BMAPI_COWFORK, &first_block,
377 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
378 &imap, &nimaps, &dfops);
379 if (error)
380 goto out_trans_cancel;
381
382 /* We might not have been able to map the whole delalloc extent */
383 *offset_fsb = min(*offset_fsb + imap.br_blockcount, next_fsb);
384
385 error = xfs_defer_finish(&tp, &dfops, NULL);
386 if (error)
387 goto out_trans_cancel;
388
389 error = xfs_trans_commit(tp);
390
391out_unlock:
392 xfs_iunlock(ip, XFS_ILOCK_EXCL);
393 return error;
394out_trans_cancel:
395 xfs_defer_cancel(&dfops);
396 xfs_trans_cancel(tp);
397 goto out_unlock;
398}
399
400/* Allocate all CoW reservations covering a part of a file. */
401int
402xfs_reflink_allocate_cow_range(
403 struct xfs_inode *ip,
404 xfs_off_t offset,
405 xfs_off_t count)
406{
407 struct xfs_mount *mp = ip->i_mount;
408 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
409 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
410 int error;
411
412 ASSERT(xfs_is_reflink_inode(ip));
413
414 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
415
416 /*
417 * Make sure that the dquots are there.
418 */
419 error = xfs_qm_dqattach(ip, 0);
420 if (error)
421 return error;
422
423 while (offset_fsb < end_fsb) {
424 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
425 if (error) {
426 trace_xfs_reflink_allocate_cow_range_error(ip, error,
427 _RET_IP_);
428 break;
429 }
430 }
431
432 return error;
433}
434
Darrick J. Wongef473662016-10-03 09:11:34 -0700435/*
436 * Find the CoW reservation (and whether or not it needs block allocation)
437 * for a given byte offset of a file.
438 */
439bool
440xfs_reflink_find_cow_mapping(
441 struct xfs_inode *ip,
442 xfs_off_t offset,
443 struct xfs_bmbt_irec *imap,
444 bool *need_alloc)
445{
446 struct xfs_bmbt_irec irec;
447 struct xfs_ifork *ifp;
448 struct xfs_bmbt_rec_host *gotp;
449 xfs_fileoff_t bno;
450 xfs_extnum_t idx;
451
452 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
453 ASSERT(xfs_is_reflink_inode(ip));
454
455 /* Find the extent in the CoW fork. */
456 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
457 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
458 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
459 if (!gotp)
460 return false;
461
462 xfs_bmbt_get_all(gotp, &irec);
463 if (bno >= irec.br_startoff + irec.br_blockcount ||
464 bno < irec.br_startoff)
465 return false;
466
467 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
468 &irec);
469
470 /* If it's still delalloc, we must allocate later. */
471 *imap = irec;
472 *need_alloc = !!(isnullstartblock(irec.br_startblock));
473
474 return true;
475}
476
477/*
478 * Trim an extent to end at the next CoW reservation past offset_fsb.
479 */
480int
481xfs_reflink_trim_irec_to_next_cow(
482 struct xfs_inode *ip,
483 xfs_fileoff_t offset_fsb,
484 struct xfs_bmbt_irec *imap)
485{
486 struct xfs_bmbt_irec irec;
487 struct xfs_ifork *ifp;
488 struct xfs_bmbt_rec_host *gotp;
489 xfs_extnum_t idx;
490
491 if (!xfs_is_reflink_inode(ip))
492 return 0;
493
494 /* Find the extent in the CoW fork. */
495 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
496 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
497 if (!gotp)
498 return 0;
499 xfs_bmbt_get_all(gotp, &irec);
500
501 /* This is the extent before; try sliding up one. */
502 if (irec.br_startoff < offset_fsb) {
503 idx++;
504 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
505 return 0;
506 gotp = xfs_iext_get_ext(ifp, idx);
507 xfs_bmbt_get_all(gotp, &irec);
508 }
509
510 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
511 return 0;
512
513 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
514 trace_xfs_reflink_trim_irec(ip, imap);
515
516 return 0;
517}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700518
519/*
520 * Cancel all pending CoW reservations for some block range of an inode.
521 */
522int
523xfs_reflink_cancel_cow_blocks(
524 struct xfs_inode *ip,
525 struct xfs_trans **tpp,
526 xfs_fileoff_t offset_fsb,
527 xfs_fileoff_t end_fsb)
528{
529 struct xfs_bmbt_irec irec;
530 xfs_filblks_t count_fsb;
531 xfs_fsblock_t firstfsb;
532 struct xfs_defer_ops dfops;
533 int error = 0;
534 int nimaps;
535
536 if (!xfs_is_reflink_inode(ip))
537 return 0;
538
539 /* Go find the old extent in the CoW fork. */
540 while (offset_fsb < end_fsb) {
541 nimaps = 1;
542 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
543 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
544 &nimaps, XFS_BMAPI_COWFORK);
545 if (error)
546 break;
547 ASSERT(nimaps == 1);
548
549 trace_xfs_reflink_cancel_cow(ip, &irec);
550
551 if (irec.br_startblock == DELAYSTARTBLOCK) {
552 /* Free a delayed allocation. */
553 xfs_mod_fdblocks(ip->i_mount, irec.br_blockcount,
554 false);
555 ip->i_delayed_blks -= irec.br_blockcount;
556
557 /* Remove the mapping from the CoW fork. */
558 error = xfs_bunmapi_cow(ip, &irec);
559 if (error)
560 break;
561 } else if (irec.br_startblock == HOLESTARTBLOCK) {
562 /* empty */
563 } else {
564 xfs_trans_ijoin(*tpp, ip, 0);
565 xfs_defer_init(&dfops, &firstfsb);
566
Darrick J. Wong174edb02016-10-03 09:11:39 -0700567 /* Free the CoW orphan record. */
568 error = xfs_refcount_free_cow_extent(ip->i_mount,
569 &dfops, irec.br_startblock,
570 irec.br_blockcount);
571 if (error)
572 break;
573
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700574 xfs_bmap_add_free(ip->i_mount, &dfops,
575 irec.br_startblock, irec.br_blockcount,
576 NULL);
577
578 /* Update quota accounting */
579 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
580 -(long)irec.br_blockcount);
581
582 /* Roll the transaction */
583 error = xfs_defer_finish(tpp, &dfops, ip);
584 if (error) {
585 xfs_defer_cancel(&dfops);
586 break;
587 }
588
589 /* Remove the mapping from the CoW fork. */
590 error = xfs_bunmapi_cow(ip, &irec);
591 if (error)
592 break;
593 }
594
595 /* Roll on... */
596 offset_fsb = irec.br_startoff + irec.br_blockcount;
597 }
598
599 return error;
600}
601
602/*
603 * Cancel all pending CoW reservations for some byte range of an inode.
604 */
605int
606xfs_reflink_cancel_cow_range(
607 struct xfs_inode *ip,
608 xfs_off_t offset,
609 xfs_off_t count)
610{
611 struct xfs_trans *tp;
612 xfs_fileoff_t offset_fsb;
613 xfs_fileoff_t end_fsb;
614 int error;
615
616 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
617
618 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
619 if (count == NULLFILEOFF)
620 end_fsb = NULLFILEOFF;
621 else
622 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
623
624 /* Start a rolling transaction to remove the mappings */
625 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
626 0, 0, 0, &tp);
627 if (error)
628 goto out;
629
630 xfs_ilock(ip, XFS_ILOCK_EXCL);
631 xfs_trans_ijoin(tp, ip, 0);
632
633 /* Scrape out the old CoW reservations */
634 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
635 if (error)
636 goto out_cancel;
637
638 error = xfs_trans_commit(tp);
639
640 xfs_iunlock(ip, XFS_ILOCK_EXCL);
641 return error;
642
643out_cancel:
644 xfs_trans_cancel(tp);
645 xfs_iunlock(ip, XFS_ILOCK_EXCL);
646out:
647 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
648 return error;
649}
650
651/*
652 * Remap parts of a file's data fork after a successful CoW.
653 */
654int
655xfs_reflink_end_cow(
656 struct xfs_inode *ip,
657 xfs_off_t offset,
658 xfs_off_t count)
659{
660 struct xfs_bmbt_irec irec;
661 struct xfs_bmbt_irec uirec;
662 struct xfs_trans *tp;
663 xfs_fileoff_t offset_fsb;
664 xfs_fileoff_t end_fsb;
665 xfs_filblks_t count_fsb;
666 xfs_fsblock_t firstfsb;
667 struct xfs_defer_ops dfops;
668 int error;
669 unsigned int resblks;
670 xfs_filblks_t ilen;
671 xfs_filblks_t rlen;
672 int nimaps;
673
674 trace_xfs_reflink_end_cow(ip, offset, count);
675
676 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
677 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
678 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
679
680 /* Start a rolling transaction to switch the mappings */
681 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
682 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
683 resblks, 0, 0, &tp);
684 if (error)
685 goto out;
686
687 xfs_ilock(ip, XFS_ILOCK_EXCL);
688 xfs_trans_ijoin(tp, ip, 0);
689
690 /* Go find the old extent in the CoW fork. */
691 while (offset_fsb < end_fsb) {
692 /* Read extent from the source file */
693 nimaps = 1;
694 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
695 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
696 &nimaps, XFS_BMAPI_COWFORK);
697 if (error)
698 goto out_cancel;
699 ASSERT(nimaps == 1);
700
701 ASSERT(irec.br_startblock != DELAYSTARTBLOCK);
702 trace_xfs_reflink_cow_remap(ip, &irec);
703
704 /*
705 * We can have a hole in the CoW fork if part of a directio
706 * write is CoW but part of it isn't.
707 */
708 rlen = ilen = irec.br_blockcount;
709 if (irec.br_startblock == HOLESTARTBLOCK)
710 goto next_extent;
711
712 /* Unmap the old blocks in the data fork. */
713 while (rlen) {
714 xfs_defer_init(&dfops, &firstfsb);
715 error = __xfs_bunmapi(tp, ip, irec.br_startoff,
716 &rlen, 0, 1, &firstfsb, &dfops);
717 if (error)
718 goto out_defer;
719
720 /*
721 * Trim the extent to whatever got unmapped.
722 * Remember, bunmapi works backwards.
723 */
724 uirec.br_startblock = irec.br_startblock + rlen;
725 uirec.br_startoff = irec.br_startoff + rlen;
726 uirec.br_blockcount = irec.br_blockcount - rlen;
727 irec.br_blockcount = rlen;
728 trace_xfs_reflink_cow_remap_piece(ip, &uirec);
729
Darrick J. Wong174edb02016-10-03 09:11:39 -0700730 /* Free the CoW orphan record. */
731 error = xfs_refcount_free_cow_extent(tp->t_mountp,
732 &dfops, uirec.br_startblock,
733 uirec.br_blockcount);
734 if (error)
735 goto out_defer;
736
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700737 /* Map the new blocks into the data fork. */
738 error = xfs_bmap_map_extent(tp->t_mountp, &dfops,
739 ip, &uirec);
740 if (error)
741 goto out_defer;
742
743 /* Remove the mapping from the CoW fork. */
744 error = xfs_bunmapi_cow(ip, &uirec);
745 if (error)
746 goto out_defer;
747
748 error = xfs_defer_finish(&tp, &dfops, ip);
749 if (error)
750 goto out_defer;
751 }
752
753next_extent:
754 /* Roll on... */
755 offset_fsb = irec.br_startoff + ilen;
756 }
757
758 error = xfs_trans_commit(tp);
759 xfs_iunlock(ip, XFS_ILOCK_EXCL);
760 if (error)
761 goto out;
762 return 0;
763
764out_defer:
765 xfs_defer_cancel(&dfops);
766out_cancel:
767 xfs_trans_cancel(tp);
768 xfs_iunlock(ip, XFS_ILOCK_EXCL);
769out:
770 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
771 return error;
772}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700773
774/*
775 * Free leftover CoW reservations that didn't get cleaned out.
776 */
777int
778xfs_reflink_recover_cow(
779 struct xfs_mount *mp)
780{
781 xfs_agnumber_t agno;
782 int error = 0;
783
784 if (!xfs_sb_version_hasreflink(&mp->m_sb))
785 return 0;
786
787 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
788 error = xfs_refcount_recover_cow_leftovers(mp, agno);
789 if (error)
790 break;
791 }
792
793 return error;
794}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700795
796/*
797 * Reflinking (Block) Ranges of Two Files Together
798 *
799 * First, ensure that the reflink flag is set on both inodes. The flag is an
800 * optimization to avoid unnecessary refcount btree lookups in the write path.
801 *
802 * Now we can iteratively remap the range of extents (and holes) in src to the
803 * corresponding ranges in dest. Let drange and srange denote the ranges of
804 * logical blocks in dest and src touched by the reflink operation.
805 *
806 * While the length of drange is greater than zero,
807 * - Read src's bmbt at the start of srange ("imap")
808 * - If imap doesn't exist, make imap appear to start at the end of srange
809 * with zero length.
810 * - If imap starts before srange, advance imap to start at srange.
811 * - If imap goes beyond srange, truncate imap to end at the end of srange.
812 * - Punch (imap start - srange start + imap len) blocks from dest at
813 * offset (drange start).
814 * - If imap points to a real range of pblks,
815 * > Increase the refcount of the imap's pblks
816 * > Map imap's pblks into dest at the offset
817 * (drange start + imap start - srange start)
818 * - Advance drange and srange by (imap start - srange start + imap len)
819 *
820 * Finally, if the reflink made dest longer, update both the in-core and
821 * on-disk file sizes.
822 *
823 * ASCII Art Demonstration:
824 *
825 * Let's say we want to reflink this source file:
826 *
827 * ----SSSSSSS-SSSSS----SSSSSS (src file)
828 * <-------------------->
829 *
830 * into this destination file:
831 *
832 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
833 * <-------------------->
834 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
835 * Observe that the range has different logical offsets in either file.
836 *
837 * Consider that the first extent in the source file doesn't line up with our
838 * reflink range. Unmapping and remapping are separate operations, so we can
839 * unmap more blocks from the destination file than we remap.
840 *
841 * ----SSSSSSS-SSSSS----SSSSSS
842 * <------->
843 * --DDDDD---------DDDDD--DDD
844 * <------->
845 *
846 * Now remap the source extent into the destination file:
847 *
848 * ----SSSSSSS-SSSSS----SSSSSS
849 * <------->
850 * --DDDDD--SSSSSSSDDDDD--DDD
851 * <------->
852 *
853 * Do likewise with the second hole and extent in our range. Holes in the
854 * unmap range don't affect our operation.
855 *
856 * ----SSSSSSS-SSSSS----SSSSSS
857 * <---->
858 * --DDDDD--SSSSSSS-SSSSS-DDD
859 * <---->
860 *
861 * Finally, unmap and remap part of the third extent. This will increase the
862 * size of the destination file.
863 *
864 * ----SSSSSSS-SSSSS----SSSSSS
865 * <----->
866 * --DDDDD--SSSSSSS-SSSSS----SSS
867 * <----->
868 *
869 * Once we update the destination file's i_size, we're done.
870 */
871
872/*
873 * Ensure the reflink bit is set in both inodes.
874 */
875STATIC int
876xfs_reflink_set_inode_flag(
877 struct xfs_inode *src,
878 struct xfs_inode *dest)
879{
880 struct xfs_mount *mp = src->i_mount;
881 int error;
882 struct xfs_trans *tp;
883
884 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
885 return 0;
886
887 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
888 if (error)
889 goto out_error;
890
891 /* Lock both files against IO */
892 if (src->i_ino == dest->i_ino)
893 xfs_ilock(src, XFS_ILOCK_EXCL);
894 else
895 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
896
897 if (!xfs_is_reflink_inode(src)) {
898 trace_xfs_reflink_set_inode_flag(src);
899 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
900 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
901 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
902 xfs_ifork_init_cow(src);
903 } else
904 xfs_iunlock(src, XFS_ILOCK_EXCL);
905
906 if (src->i_ino == dest->i_ino)
907 goto commit_flags;
908
909 if (!xfs_is_reflink_inode(dest)) {
910 trace_xfs_reflink_set_inode_flag(dest);
911 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
912 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
913 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
914 xfs_ifork_init_cow(dest);
915 } else
916 xfs_iunlock(dest, XFS_ILOCK_EXCL);
917
918commit_flags:
919 error = xfs_trans_commit(tp);
920 if (error)
921 goto out_error;
922 return error;
923
924out_error:
925 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
926 return error;
927}
928
929/*
930 * Update destination inode size, if necessary.
931 */
932STATIC int
933xfs_reflink_update_dest(
934 struct xfs_inode *dest,
935 xfs_off_t newlen)
936{
937 struct xfs_mount *mp = dest->i_mount;
938 struct xfs_trans *tp;
939 int error;
940
941 if (newlen <= i_size_read(VFS_I(dest)))
942 return 0;
943
944 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
945 if (error)
946 goto out_error;
947
948 xfs_ilock(dest, XFS_ILOCK_EXCL);
949 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
950
951 trace_xfs_reflink_update_inode_size(dest, newlen);
952 i_size_write(VFS_I(dest), newlen);
953 dest->i_d.di_size = newlen;
954 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
955
956 error = xfs_trans_commit(tp);
957 if (error)
958 goto out_error;
959 return error;
960
961out_error:
962 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
963 return error;
964}
965
966/*
967 * Unmap a range of blocks from a file, then map other blocks into the hole.
968 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
969 * The extent irec is mapped into dest at irec->br_startoff.
970 */
971STATIC int
972xfs_reflink_remap_extent(
973 struct xfs_inode *ip,
974 struct xfs_bmbt_irec *irec,
975 xfs_fileoff_t destoff,
976 xfs_off_t new_isize)
977{
978 struct xfs_mount *mp = ip->i_mount;
979 struct xfs_trans *tp;
980 xfs_fsblock_t firstfsb;
981 unsigned int resblks;
982 struct xfs_defer_ops dfops;
983 struct xfs_bmbt_irec uirec;
984 bool real_extent;
985 xfs_filblks_t rlen;
986 xfs_filblks_t unmap_len;
987 xfs_off_t newlen;
988 int error;
989
990 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
991 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
992
993 /* Only remap normal extents. */
994 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
995 irec->br_startblock != DELAYSTARTBLOCK &&
996 !ISUNWRITTEN(irec));
997
998 /* Start a rolling transaction to switch the mappings */
999 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1000 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1001 if (error)
1002 goto out;
1003
1004 xfs_ilock(ip, XFS_ILOCK_EXCL);
1005 xfs_trans_ijoin(tp, ip, 0);
1006
1007 /* If we're not just clearing space, then do we have enough quota? */
1008 if (real_extent) {
1009 error = xfs_trans_reserve_quota_nblks(tp, ip,
1010 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1011 if (error)
1012 goto out_cancel;
1013 }
1014
1015 trace_xfs_reflink_remap(ip, irec->br_startoff,
1016 irec->br_blockcount, irec->br_startblock);
1017
1018 /* Unmap the old blocks in the data fork. */
1019 rlen = unmap_len;
1020 while (rlen) {
1021 xfs_defer_init(&dfops, &firstfsb);
1022 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1023 &firstfsb, &dfops);
1024 if (error)
1025 goto out_defer;
1026
1027 /*
1028 * Trim the extent to whatever got unmapped.
1029 * Remember, bunmapi works backwards.
1030 */
1031 uirec.br_startblock = irec->br_startblock + rlen;
1032 uirec.br_startoff = irec->br_startoff + rlen;
1033 uirec.br_blockcount = unmap_len - rlen;
1034 unmap_len = rlen;
1035
1036 /* If this isn't a real mapping, we're done. */
1037 if (!real_extent || uirec.br_blockcount == 0)
1038 goto next_extent;
1039
1040 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1041 uirec.br_blockcount, uirec.br_startblock);
1042
1043 /* Update the refcount tree */
1044 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1045 if (error)
1046 goto out_defer;
1047
1048 /* Map the new blocks into the data fork. */
1049 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1050 if (error)
1051 goto out_defer;
1052
1053 /* Update quota accounting. */
1054 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1055 uirec.br_blockcount);
1056
1057 /* Update dest isize if needed. */
1058 newlen = XFS_FSB_TO_B(mp,
1059 uirec.br_startoff + uirec.br_blockcount);
1060 newlen = min_t(xfs_off_t, newlen, new_isize);
1061 if (newlen > i_size_read(VFS_I(ip))) {
1062 trace_xfs_reflink_update_inode_size(ip, newlen);
1063 i_size_write(VFS_I(ip), newlen);
1064 ip->i_d.di_size = newlen;
1065 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1066 }
1067
1068next_extent:
1069 /* Process all the deferred stuff. */
1070 error = xfs_defer_finish(&tp, &dfops, ip);
1071 if (error)
1072 goto out_defer;
1073 }
1074
1075 error = xfs_trans_commit(tp);
1076 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1077 if (error)
1078 goto out;
1079 return 0;
1080
1081out_defer:
1082 xfs_defer_cancel(&dfops);
1083out_cancel:
1084 xfs_trans_cancel(tp);
1085 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1086out:
1087 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1088 return error;
1089}
1090
1091/*
1092 * Iteratively remap one file's extents (and holes) to another's.
1093 */
1094STATIC int
1095xfs_reflink_remap_blocks(
1096 struct xfs_inode *src,
1097 xfs_fileoff_t srcoff,
1098 struct xfs_inode *dest,
1099 xfs_fileoff_t destoff,
1100 xfs_filblks_t len,
1101 xfs_off_t new_isize)
1102{
1103 struct xfs_bmbt_irec imap;
1104 int nimaps;
1105 int error = 0;
1106 xfs_filblks_t range_len;
1107
1108 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1109 while (len) {
1110 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1111 dest, destoff);
1112 /* Read extent from the source file */
1113 nimaps = 1;
1114 xfs_ilock(src, XFS_ILOCK_EXCL);
1115 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1116 xfs_iunlock(src, XFS_ILOCK_EXCL);
1117 if (error)
1118 goto err;
1119 ASSERT(nimaps == 1);
1120
1121 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1122 &imap);
1123
1124 /* Translate imap into the destination file. */
1125 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1126 imap.br_startoff += destoff - srcoff;
1127
1128 /* Clear dest from destoff to the end of imap and map it in. */
1129 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1130 new_isize);
1131 if (error)
1132 goto err;
1133
1134 if (fatal_signal_pending(current)) {
1135 error = -EINTR;
1136 goto err;
1137 }
1138
1139 /* Advance drange/srange */
1140 srcoff += range_len;
1141 destoff += range_len;
1142 len -= range_len;
1143 }
1144
1145 return 0;
1146
1147err:
1148 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1149 return error;
1150}
1151
1152/*
1153 * Link a range of blocks from one file to another.
1154 */
1155int
1156xfs_reflink_remap_range(
1157 struct xfs_inode *src,
1158 xfs_off_t srcoff,
1159 struct xfs_inode *dest,
1160 xfs_off_t destoff,
1161 xfs_off_t len)
1162{
1163 struct xfs_mount *mp = src->i_mount;
1164 xfs_fileoff_t sfsbno, dfsbno;
1165 xfs_filblks_t fsblen;
1166 int error;
1167
1168 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1169 return -EOPNOTSUPP;
1170
1171 if (XFS_FORCED_SHUTDOWN(mp))
1172 return -EIO;
1173
1174 /* Don't reflink realtime inodes */
1175 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1176 return -EINVAL;
1177
1178 trace_xfs_reflink_remap_range(src, srcoff, len, dest, destoff);
1179
1180 /* Lock both files against IO */
1181 if (src->i_ino == dest->i_ino) {
1182 xfs_ilock(src, XFS_IOLOCK_EXCL);
1183 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1184 } else {
1185 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1186 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1187 }
1188
1189 error = xfs_reflink_set_inode_flag(src, dest);
1190 if (error)
1191 goto out_error;
1192
1193 /*
1194 * Invalidate the page cache so that we can clear any CoW mappings
1195 * in the destination file.
1196 */
1197 truncate_inode_pages_range(&VFS_I(dest)->i_data, destoff,
1198 PAGE_ALIGN(destoff + len) - 1);
1199
1200 dfsbno = XFS_B_TO_FSBT(mp, destoff);
1201 sfsbno = XFS_B_TO_FSBT(mp, srcoff);
1202 fsblen = XFS_B_TO_FSB(mp, len);
1203 error = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1204 destoff + len);
1205 if (error)
1206 goto out_error;
1207
1208 error = xfs_reflink_update_dest(dest, destoff + len);
1209 if (error)
1210 goto out_error;
1211
1212out_error:
1213 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1214 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1215 if (src->i_ino != dest->i_ino) {
1216 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1217 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1218 }
1219 if (error)
1220 trace_xfs_reflink_remap_range_error(dest, error, _RET_IP_);
1221 return error;
1222}