1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012 Red Hat, Inc.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_defer.h"
17 #include "xfs_inode.h"
18 #include "xfs_btree.h"
19 #include "xfs_trans.h"
20 #include "xfs_extfree_item.h"
21 #include "xfs_alloc.h"
23 #include "xfs_bmap_util.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_rtalloc.h"
26 #include "xfs_error.h"
27 #include "xfs_quota.h"
28 #include "xfs_trans_space.h"
29 #include "xfs_trace.h"
30 #include "xfs_icache.h"
32 #include "xfs_rmap_btree.h"
33 #include "xfs_iomap.h"
34 #include "xfs_reflink.h"
35 #include "xfs_refcount.h"
37 /* Kernel only BMAP related definitions and functions */
40 * Convert the given file system block to a disk block. We have to treat it
41 * differently based on whether the file is a real time file or not, because the
45 xfs_fsb_to_db(struct xfs_inode
*ip
, xfs_fsblock_t fsb
)
47 return (XFS_IS_REALTIME_INODE(ip
) ? \
48 (xfs_daddr_t
)XFS_FSB_TO_BB((ip
)->i_mount
, (fsb
)) : \
49 XFS_FSB_TO_DADDR((ip
)->i_mount
, (fsb
)));
53 * Routine to zero an extent on disk allocated to the specific inode.
55 * The VFS functions take a linearised filesystem block offset, so we have to
56 * convert the sparse xfs fsb to the right format first.
57 * VFS types are real funky, too.
62 xfs_fsblock_t start_fsb
,
65 struct xfs_mount
*mp
= ip
->i_mount
;
66 xfs_daddr_t sector
= xfs_fsb_to_db(ip
, start_fsb
);
67 sector_t block
= XFS_BB_TO_FSBT(mp
, sector
);
69 return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip
)),
70 block
<< (mp
->m_super
->s_blocksize_bits
- 9),
71 count_fsb
<< (mp
->m_super
->s_blocksize_bits
- 9),
78 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
80 int error
; /* error return value */
81 xfs_mount_t
*mp
; /* mount point structure */
82 xfs_extlen_t prod
= 0; /* product factor for allocators */
83 xfs_extlen_t mod
= 0; /* product factor for allocators */
84 xfs_extlen_t ralen
= 0; /* realtime allocation length */
85 xfs_extlen_t align
; /* minimum allocation alignment */
89 align
= xfs_get_extsz_hint(ap
->ip
);
90 prod
= align
/ mp
->m_sb
.sb_rextsize
;
91 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
93 ap
->conv
, &ap
->offset
, &ap
->length
);
97 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
100 * If the offset & length are not perfectly aligned
101 * then kill prod, it will just get us in trouble.
103 div_u64_rem(ap
->offset
, align
, &mod
);
104 if (mod
|| ap
->length
% align
)
107 * Set ralen to be the actual requested length in rtextents.
109 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
111 * If the old value was close enough to MAXEXTLEN that
112 * we rounded up to it, cut it back so it's valid again.
113 * Note that if it's a really large request (bigger than
114 * MAXEXTLEN), we don't hear about that number, and can't
115 * adjust the starting point to match it.
117 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
118 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
121 * Lock out modifications to both the RT bitmap and summary inodes
123 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTBITMAP
);
124 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
125 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
|XFS_ILOCK_RTSUM
);
126 xfs_trans_ijoin(ap
->tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
129 * If it's an allocation to an empty file at offset 0,
130 * pick an extent that will space things out in the rt area.
132 if (ap
->eof
&& ap
->offset
== 0) {
133 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
135 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
138 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
143 xfs_bmap_adjacent(ap
);
146 * Realtime allocation, done through xfs_rtallocate_extent.
148 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
151 error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
152 &ralen
, ap
->wasdel
, prod
, &rtb
);
157 if (ap
->blkno
!= NULLFSBLOCK
) {
158 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
159 ralen
*= mp
->m_sb
.sb_rextsize
;
161 ap
->ip
->i_d
.di_nblocks
+= ralen
;
162 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
164 ap
->ip
->i_delayed_blks
-= ralen
;
166 * Adjust the disk quota also. This was reserved
169 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
170 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
171 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
173 /* Zero the extent if we were asked to do so */
174 if (ap
->datatype
& XFS_ALLOC_USERDATA_ZERO
) {
175 error
= xfs_zero_extent(ap
->ip
, ap
->blkno
, ap
->length
);
184 #endif /* CONFIG_XFS_RT */
187 * Check if the endoff is outside the last extent. If so the caller will grow
188 * the allocation to a stripe unit boundary. All offsets are considered outside
189 * the end of file for an empty fork, so 1 is returned in *eof in that case.
193 struct xfs_inode
*ip
,
194 xfs_fileoff_t endoff
,
198 struct xfs_bmbt_irec rec
;
201 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
205 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
210 * Extent tree block counting routines.
214 * Count leaf blocks given a range of extent records. Delayed allocation
215 * extents are not counted towards the totals.
218 xfs_bmap_count_leaves(
219 struct xfs_ifork
*ifp
,
220 xfs_filblks_t
*count
)
222 struct xfs_iext_cursor icur
;
223 struct xfs_bmbt_irec got
;
224 xfs_extnum_t numrecs
= 0;
226 for_each_xfs_iext(ifp
, &icur
, &got
) {
227 if (!isnullstartblock(got
.br_startblock
)) {
228 *count
+= got
.br_blockcount
;
237 * Count leaf blocks given a range of extent records originally
241 xfs_bmap_disk_count_leaves(
242 struct xfs_mount
*mp
,
243 struct xfs_btree_block
*block
,
245 xfs_filblks_t
*count
)
250 for (b
= 1; b
<= numrecs
; b
++) {
251 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
252 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
257 * Recursively walks each level of a btree
258 * to count total fsblocks in use.
262 struct xfs_mount
*mp
,
263 struct xfs_trans
*tp
,
264 struct xfs_ifork
*ifp
,
265 xfs_fsblock_t blockno
,
267 xfs_extnum_t
*nextents
,
268 xfs_filblks_t
*count
)
271 struct xfs_buf
*bp
, *nbp
;
274 xfs_fsblock_t bno
= blockno
;
275 xfs_fsblock_t nextbno
;
276 struct xfs_btree_block
*block
, *nextblock
;
279 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
,
284 block
= XFS_BUF_TO_BLOCK(bp
);
287 /* Not at node above leaves, count this level of nodes */
288 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
289 while (nextbno
!= NULLFSBLOCK
) {
290 error
= xfs_btree_read_bufl(mp
, tp
, nextbno
, 0, &nbp
,
296 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
297 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
298 xfs_trans_brelse(tp
, nbp
);
301 /* Dive to the next level */
302 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
303 bno
= be64_to_cpu(*pp
);
304 error
= xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, nextents
,
307 xfs_trans_brelse(tp
, bp
);
308 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
309 XFS_ERRLEVEL_LOW
, mp
);
310 return -EFSCORRUPTED
;
312 xfs_trans_brelse(tp
, bp
);
314 /* count all level 1 nodes and their leaves */
316 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
317 numrecs
= be16_to_cpu(block
->bb_numrecs
);
318 (*nextents
) += numrecs
;
319 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
320 xfs_trans_brelse(tp
, bp
);
321 if (nextbno
== NULLFSBLOCK
)
324 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
330 block
= XFS_BUF_TO_BLOCK(bp
);
337 * Count fsblocks of the given fork. Delayed allocation extents are
338 * not counted towards the totals.
341 xfs_bmap_count_blocks(
342 struct xfs_trans
*tp
,
343 struct xfs_inode
*ip
,
345 xfs_extnum_t
*nextents
,
346 xfs_filblks_t
*count
)
348 struct xfs_mount
*mp
; /* file system mount structure */
349 __be64
*pp
; /* pointer to block address */
350 struct xfs_btree_block
*block
; /* current btree block */
351 struct xfs_ifork
*ifp
; /* fork structure */
352 xfs_fsblock_t bno
; /* block # of "block" */
353 int level
; /* btree level, for checking */
360 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
364 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
365 case XFS_DINODE_FMT_EXTENTS
:
366 *nextents
= xfs_bmap_count_leaves(ifp
, count
);
368 case XFS_DINODE_FMT_BTREE
:
369 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
370 error
= xfs_iread_extents(tp
, ip
, whichfork
);
376 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
378 block
= ifp
->if_broot
;
379 level
= be16_to_cpu(block
->bb_level
);
381 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
382 bno
= be64_to_cpu(*pp
);
383 ASSERT(bno
!= NULLFSBLOCK
);
384 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
385 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
387 error
= xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
,
390 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)",
391 XFS_ERRLEVEL_LOW
, mp
);
392 return -EFSCORRUPTED
;
401 xfs_getbmap_report_one(
402 struct xfs_inode
*ip
,
403 struct getbmapx
*bmv
,
404 struct kgetbmap
*out
,
406 struct xfs_bmbt_irec
*got
)
408 struct kgetbmap
*p
= out
+ bmv
->bmv_entries
;
409 bool shared
= false, trimmed
= false;
412 error
= xfs_reflink_trim_around_shared(ip
, got
, &shared
, &trimmed
);
416 if (isnullstartblock(got
->br_startblock
) ||
417 got
->br_startblock
== DELAYSTARTBLOCK
) {
419 * Delalloc extents that start beyond EOF can occur due to
420 * speculative EOF allocation when the delalloc extent is larger
421 * than the largest freespace extent at conversion time. These
422 * extents cannot be converted by data writeback, so can exist
423 * here even if we are not supposed to be finding delalloc
426 if (got
->br_startoff
< XFS_B_TO_FSB(ip
->i_mount
, XFS_ISIZE(ip
)))
427 ASSERT((bmv
->bmv_iflags
& BMV_IF_DELALLOC
) != 0);
429 p
->bmv_oflags
|= BMV_OF_DELALLOC
;
432 p
->bmv_block
= xfs_fsb_to_db(ip
, got
->br_startblock
);
435 if (got
->br_state
== XFS_EXT_UNWRITTEN
&&
436 (bmv
->bmv_iflags
& BMV_IF_PREALLOC
))
437 p
->bmv_oflags
|= BMV_OF_PREALLOC
;
440 p
->bmv_oflags
|= BMV_OF_SHARED
;
442 p
->bmv_offset
= XFS_FSB_TO_BB(ip
->i_mount
, got
->br_startoff
);
443 p
->bmv_length
= XFS_FSB_TO_BB(ip
->i_mount
, got
->br_blockcount
);
445 bmv
->bmv_offset
= p
->bmv_offset
+ p
->bmv_length
;
446 bmv
->bmv_length
= max(0LL, bmv_end
- bmv
->bmv_offset
);
452 xfs_getbmap_report_hole(
453 struct xfs_inode
*ip
,
454 struct getbmapx
*bmv
,
455 struct kgetbmap
*out
,
460 struct kgetbmap
*p
= out
+ bmv
->bmv_entries
;
462 if (bmv
->bmv_iflags
& BMV_IF_NO_HOLES
)
466 p
->bmv_offset
= XFS_FSB_TO_BB(ip
->i_mount
, bno
);
467 p
->bmv_length
= XFS_FSB_TO_BB(ip
->i_mount
, end
- bno
);
469 bmv
->bmv_offset
= p
->bmv_offset
+ p
->bmv_length
;
470 bmv
->bmv_length
= max(0LL, bmv_end
- bmv
->bmv_offset
);
476 struct getbmapx
*bmv
)
478 return bmv
->bmv_length
== 0 || bmv
->bmv_entries
>= bmv
->bmv_count
- 1;
482 xfs_getbmap_next_rec(
483 struct xfs_bmbt_irec
*rec
,
484 xfs_fileoff_t total_end
)
486 xfs_fileoff_t end
= rec
->br_startoff
+ rec
->br_blockcount
;
488 if (end
== total_end
)
491 rec
->br_startoff
+= rec
->br_blockcount
;
492 if (!isnullstartblock(rec
->br_startblock
) &&
493 rec
->br_startblock
!= DELAYSTARTBLOCK
)
494 rec
->br_startblock
+= rec
->br_blockcount
;
495 rec
->br_blockcount
= total_end
- end
;
500 * Get inode's extents as described in bmv, and format for output.
501 * Calls formatter to fill the user's buffer until all extents
502 * are mapped, until the passed-in bmv->bmv_count slots have
503 * been filled, or until the formatter short-circuits the loop,
504 * if it is tracking filled-in extents on its own.
508 struct xfs_inode
*ip
,
509 struct getbmapx
*bmv
, /* user bmap structure */
510 struct kgetbmap
*out
)
512 struct xfs_mount
*mp
= ip
->i_mount
;
513 int iflags
= bmv
->bmv_iflags
;
514 int whichfork
, lock
, error
= 0;
515 int64_t bmv_end
, max_len
;
516 xfs_fileoff_t bno
, first_bno
;
517 struct xfs_ifork
*ifp
;
518 struct xfs_bmbt_irec got
, rec
;
520 struct xfs_iext_cursor icur
;
522 if (bmv
->bmv_iflags
& ~BMV_IF_VALID
)
525 /* Only allow CoW fork queries if we're debugging. */
526 if (iflags
& BMV_IF_COWFORK
)
529 if ((iflags
& BMV_IF_ATTRFORK
) && (iflags
& BMV_IF_COWFORK
))
532 if (bmv
->bmv_length
< -1)
534 bmv
->bmv_entries
= 0;
535 if (bmv
->bmv_length
== 0)
538 if (iflags
& BMV_IF_ATTRFORK
)
539 whichfork
= XFS_ATTR_FORK
;
540 else if (iflags
& BMV_IF_COWFORK
)
541 whichfork
= XFS_COW_FORK
;
543 whichfork
= XFS_DATA_FORK
;
544 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
546 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
549 if (!XFS_IFORK_Q(ip
))
550 goto out_unlock_iolock
;
553 lock
= xfs_ilock_attr_map_shared(ip
);
556 /* No CoW fork? Just return */
558 goto out_unlock_iolock
;
560 if (xfs_get_cowextsz_hint(ip
))
561 max_len
= mp
->m_super
->s_maxbytes
;
563 max_len
= XFS_ISIZE(ip
);
565 lock
= XFS_ILOCK_SHARED
;
569 if (!(iflags
& BMV_IF_DELALLOC
) &&
570 (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
)) {
571 error
= filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
573 goto out_unlock_iolock
;
576 * Even after flushing the inode, there can still be
577 * delalloc blocks on the inode beyond EOF due to
578 * speculative preallocation. These are not removed
579 * until the release function is called or the inode
580 * is inactivated. Hence we cannot assert here that
581 * ip->i_delayed_blks == 0.
585 if (xfs_get_extsz_hint(ip
) ||
587 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))
588 max_len
= mp
->m_super
->s_maxbytes
;
590 max_len
= XFS_ISIZE(ip
);
592 lock
= xfs_ilock_data_map_shared(ip
);
596 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
597 case XFS_DINODE_FMT_EXTENTS
:
598 case XFS_DINODE_FMT_BTREE
:
600 case XFS_DINODE_FMT_LOCAL
:
601 /* Local format inode forks report no extents. */
602 goto out_unlock_ilock
;
605 goto out_unlock_ilock
;
608 if (bmv
->bmv_length
== -1) {
609 max_len
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, max_len
));
610 bmv
->bmv_length
= max(0LL, max_len
- bmv
->bmv_offset
);
613 bmv_end
= bmv
->bmv_offset
+ bmv
->bmv_length
;
615 first_bno
= bno
= XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
);
616 len
= XFS_BB_TO_FSB(mp
, bmv
->bmv_length
);
618 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
619 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
621 goto out_unlock_ilock
;
624 if (!xfs_iext_lookup_extent(ip
, ifp
, bno
, &icur
, &got
)) {
626 * Report a whole-file hole if the delalloc flag is set to
627 * stay compatible with the old implementation.
629 if (iflags
& BMV_IF_DELALLOC
)
630 xfs_getbmap_report_hole(ip
, bmv
, out
, bmv_end
, bno
,
631 XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
632 goto out_unlock_ilock
;
635 while (!xfs_getbmap_full(bmv
)) {
636 xfs_trim_extent(&got
, first_bno
, len
);
639 * Report an entry for a hole if this extent doesn't directly
640 * follow the previous one.
642 if (got
.br_startoff
> bno
) {
643 xfs_getbmap_report_hole(ip
, bmv
, out
, bmv_end
, bno
,
645 if (xfs_getbmap_full(bmv
))
650 * In order to report shared extents accurately, we report each
651 * distinct shared / unshared part of a single bmbt record with
652 * an individual getbmapx record.
654 bno
= got
.br_startoff
+ got
.br_blockcount
;
657 error
= xfs_getbmap_report_one(ip
, bmv
, out
, bmv_end
,
659 if (error
|| xfs_getbmap_full(bmv
))
660 goto out_unlock_ilock
;
661 } while (xfs_getbmap_next_rec(&rec
, bno
));
663 if (!xfs_iext_next_extent(ifp
, &icur
, &got
)) {
664 xfs_fileoff_t end
= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
));
666 out
[bmv
->bmv_entries
- 1].bmv_oflags
|= BMV_OF_LAST
;
668 if (whichfork
!= XFS_ATTR_FORK
&& bno
< end
&&
669 !xfs_getbmap_full(bmv
)) {
670 xfs_getbmap_report_hole(ip
, bmv
, out
, bmv_end
,
676 if (bno
>= first_bno
+ len
)
681 xfs_iunlock(ip
, lock
);
683 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
688 * Dead simple method of punching delalyed allocation blocks from a range in
689 * the inode. This will always punch out both the start and end blocks, even
690 * if the ranges only partially overlap them, so it is up to the caller to
691 * ensure that partial blocks are not passed in.
694 xfs_bmap_punch_delalloc_range(
695 struct xfs_inode
*ip
,
696 xfs_fileoff_t start_fsb
,
697 xfs_fileoff_t length
)
699 struct xfs_ifork
*ifp
= &ip
->i_df
;
700 xfs_fileoff_t end_fsb
= start_fsb
+ length
;
701 struct xfs_bmbt_irec got
, del
;
702 struct xfs_iext_cursor icur
;
705 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
707 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
708 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
713 if (!xfs_iext_lookup_extent_before(ip
, ifp
, &end_fsb
, &icur
, &got
))
716 while (got
.br_startoff
+ got
.br_blockcount
> start_fsb
) {
718 xfs_trim_extent(&del
, start_fsb
, length
);
721 * A delete can push the cursor forward. Step back to the
722 * previous extent on non-delalloc or extents outside the
725 if (!del
.br_blockcount
||
726 !isnullstartblock(del
.br_startblock
)) {
727 if (!xfs_iext_prev_extent(ifp
, &icur
, &got
))
732 error
= xfs_bmap_del_extent_delay(ip
, XFS_DATA_FORK
, &icur
,
734 if (error
|| !xfs_iext_get_extent(ifp
, &icur
, &got
))
742 * Test whether it is appropriate to check an inode for and free post EOF
743 * blocks. The 'force' parameter determines whether we should also consider
744 * regular files that are marked preallocated or append-only.
747 xfs_can_free_eofblocks(struct xfs_inode
*ip
, bool force
)
749 /* prealloc/delalloc exists only on regular files */
750 if (!S_ISREG(VFS_I(ip
)->i_mode
))
754 * Zero sized files with no cached pages and delalloc blocks will not
755 * have speculative prealloc/delalloc blocks to remove.
757 if (VFS_I(ip
)->i_size
== 0 &&
758 VFS_I(ip
)->i_mapping
->nrpages
== 0 &&
759 ip
->i_delayed_blks
== 0)
762 /* If we haven't read in the extent list, then don't do it now. */
763 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
))
767 * Do not free real preallocated or append-only files unless the file
768 * has delalloc blocks and we are forced to remove them.
770 if (ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
))
771 if (!force
|| ip
->i_delayed_blks
== 0)
778 * This is called to free any blocks beyond eof. The caller must hold
779 * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
780 * reference to the inode.
784 struct xfs_inode
*ip
)
786 struct xfs_trans
*tp
;
788 xfs_fileoff_t end_fsb
;
789 xfs_fileoff_t last_fsb
;
790 xfs_filblks_t map_len
;
792 struct xfs_bmbt_irec imap
;
793 struct xfs_mount
*mp
= ip
->i_mount
;
796 * Figure out if there are any blocks beyond the end
797 * of the file. If not, then there is nothing to do.
799 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_ISIZE(ip
));
800 last_fsb
= XFS_B_TO_FSB(mp
, mp
->m_super
->s_maxbytes
);
801 if (last_fsb
<= end_fsb
)
803 map_len
= last_fsb
- end_fsb
;
806 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
807 error
= xfs_bmapi_read(ip
, end_fsb
, map_len
, &imap
, &nimaps
, 0);
808 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
811 * If there are blocks after the end of file, truncate the file to its
812 * current size to free them up.
814 if (!error
&& (nimaps
!= 0) &&
815 (imap
.br_startblock
!= HOLESTARTBLOCK
||
816 ip
->i_delayed_blks
)) {
818 * Attach the dquots to the inode up front.
820 error
= xfs_qm_dqattach(ip
);
824 /* wait on dio to ensure i_size has settled */
825 inode_dio_wait(VFS_I(ip
));
827 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
, 0, 0, 0,
830 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
834 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
835 xfs_trans_ijoin(tp
, ip
, 0);
838 * Do not update the on-disk file size. If we update the
839 * on-disk file size and then the system crashes before the
840 * contents of the file are flushed to disk then the files
841 * may be full of holes (ie NULL files bug).
843 error
= xfs_itruncate_extents_flags(&tp
, ip
, XFS_DATA_FORK
,
844 XFS_ISIZE(ip
), XFS_BMAPI_NODISCARD
);
847 * If we get an error at this point we simply don't
848 * bother truncating the file.
850 xfs_trans_cancel(tp
);
852 error
= xfs_trans_commit(tp
);
854 xfs_inode_clear_eofblocks_tag(ip
);
857 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
863 xfs_alloc_file_space(
864 struct xfs_inode
*ip
,
869 xfs_mount_t
*mp
= ip
->i_mount
;
871 xfs_filblks_t allocated_fsb
;
872 xfs_filblks_t allocatesize_fsb
;
873 xfs_extlen_t extsz
, temp
;
874 xfs_fileoff_t startoffset_fsb
;
875 xfs_fsblock_t firstfsb
;
880 xfs_bmbt_irec_t imaps
[1], *imapp
;
881 struct xfs_defer_ops dfops
;
882 uint qblocks
, resblks
, resrtextents
;
885 trace_xfs_alloc_file_space(ip
);
887 if (XFS_FORCED_SHUTDOWN(mp
))
890 error
= xfs_qm_dqattach(ip
);
897 rt
= XFS_IS_REALTIME_INODE(ip
);
898 extsz
= xfs_get_extsz_hint(ip
);
903 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
904 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
907 * Allocate file space until done or until there is an error
909 while (allocatesize_fsb
&& !error
) {
913 * Determine space reservations for data/realtime.
915 if (unlikely(extsz
)) {
919 e
= startoffset_fsb
+ allocatesize_fsb
;
920 div_u64_rem(startoffset_fsb
, extsz
, &temp
);
923 div_u64_rem(e
, extsz
, &temp
);
928 e
= allocatesize_fsb
;
932 * The transaction reservation is limited to a 32-bit block
933 * count, hence we need to limit the number of blocks we are
934 * trying to reserve to avoid an overflow. We can't allocate
935 * more than @nimaps extents, and an extent is limited on disk
936 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
938 resblks
= min_t(xfs_fileoff_t
, (e
- s
), (MAXEXTLEN
* nimaps
));
940 resrtextents
= qblocks
= resblks
;
941 resrtextents
/= mp
->m_sb
.sb_rextsize
;
942 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
943 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
946 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resblks
);
947 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
951 * Allocate and setup the transaction.
953 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
,
954 resrtextents
, 0, &tp
);
957 * Check for running out of space
961 * Free the transaction structure.
963 ASSERT(error
== -ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
966 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
967 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
,
972 xfs_trans_ijoin(tp
, ip
, 0);
974 xfs_defer_init(&dfops
, &firstfsb
);
975 error
= xfs_bmapi_write(tp
, ip
, startoffset_fsb
,
976 allocatesize_fsb
, alloc_type
, &firstfsb
,
977 resblks
, imapp
, &nimaps
, &dfops
);
982 * Complete the transaction
984 error
= xfs_defer_finish(&tp
, &dfops
);
988 error
= xfs_trans_commit(tp
);
989 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
993 allocated_fsb
= imapp
->br_blockcount
;
1000 startoffset_fsb
+= allocated_fsb
;
1001 allocatesize_fsb
-= allocated_fsb
;
1006 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1007 xfs_defer_cancel(&dfops
);
1008 xfs_trans_unreserve_quota_nblks(tp
, ip
, (long)qblocks
, 0, quota_flag
);
1010 error1
: /* Just cancel transaction */
1011 xfs_trans_cancel(tp
);
1012 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1018 struct xfs_inode
*ip
,
1019 xfs_fileoff_t startoffset_fsb
,
1020 xfs_filblks_t len_fsb
,
1023 struct xfs_mount
*mp
= ip
->i_mount
;
1024 struct xfs_trans
*tp
;
1025 struct xfs_defer_ops dfops
;
1026 xfs_fsblock_t firstfsb
;
1027 uint resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1030 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0, &tp
);
1032 ASSERT(error
== -ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
1036 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1037 error
= xfs_trans_reserve_quota(tp
, mp
, ip
->i_udquot
, ip
->i_gdquot
,
1038 ip
->i_pdquot
, resblks
, 0, XFS_QMOPT_RES_REGBLKS
);
1040 goto out_trans_cancel
;
1042 xfs_trans_ijoin(tp
, ip
, 0);
1044 xfs_defer_init(&dfops
, &firstfsb
);
1045 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
, len_fsb
, 0, 2, &firstfsb
,
1048 goto out_bmap_cancel
;
1050 xfs_defer_ijoin(&dfops
, ip
);
1051 error
= xfs_defer_finish(&tp
, &dfops
);
1053 goto out_bmap_cancel
;
1055 error
= xfs_trans_commit(tp
);
1057 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1061 xfs_defer_cancel(&dfops
);
1063 xfs_trans_cancel(tp
);
1068 xfs_adjust_extent_unmap_boundaries(
1069 struct xfs_inode
*ip
,
1070 xfs_fileoff_t
*startoffset_fsb
,
1071 xfs_fileoff_t
*endoffset_fsb
)
1073 struct xfs_mount
*mp
= ip
->i_mount
;
1074 struct xfs_bmbt_irec imap
;
1076 xfs_extlen_t mod
= 0;
1079 error
= xfs_bmapi_read(ip
, *startoffset_fsb
, 1, &imap
, &nimap
, 0);
1083 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1084 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1085 div_u64_rem(imap
.br_startblock
, mp
->m_sb
.sb_rextsize
, &mod
);
1087 *startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
1091 error
= xfs_bmapi_read(ip
, *endoffset_fsb
- 1, 1, &imap
, &nimap
, 0);
1095 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1096 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1098 if (mod
&& mod
!= mp
->m_sb
.sb_rextsize
)
1099 *endoffset_fsb
-= mod
;
1106 xfs_flush_unmap_range(
1107 struct xfs_inode
*ip
,
1111 struct xfs_mount
*mp
= ip
->i_mount
;
1112 struct inode
*inode
= VFS_I(ip
);
1113 xfs_off_t rounding
, start
, end
;
1116 /* wait for the completion of any pending DIOs */
1117 inode_dio_wait(inode
);
1119 rounding
= max_t(xfs_off_t
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_SIZE
);
1120 start
= round_down(offset
, rounding
);
1121 end
= round_up(offset
+ len
, rounding
) - 1;
1123 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
1126 truncate_pagecache_range(inode
, start
, end
);
1131 xfs_free_file_space(
1132 struct xfs_inode
*ip
,
1136 struct xfs_mount
*mp
= ip
->i_mount
;
1137 xfs_fileoff_t startoffset_fsb
;
1138 xfs_fileoff_t endoffset_fsb
;
1139 int done
= 0, error
;
1141 trace_xfs_free_file_space(ip
);
1143 error
= xfs_qm_dqattach(ip
);
1147 if (len
<= 0) /* if nothing being freed */
1150 error
= xfs_flush_unmap_range(ip
, offset
, len
);
1154 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
1155 endoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
+ len
);
1158 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1159 * and we can't use unwritten extents then we actually need to ensure
1160 * to zero the whole extent, otherwise we just need to take of block
1161 * boundaries, and xfs_bunmapi will handle the rest.
1163 if (XFS_IS_REALTIME_INODE(ip
) &&
1164 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
1165 error
= xfs_adjust_extent_unmap_boundaries(ip
, &startoffset_fsb
,
1171 if (endoffset_fsb
> startoffset_fsb
) {
1173 error
= xfs_unmap_extent(ip
, startoffset_fsb
,
1174 endoffset_fsb
- startoffset_fsb
, &done
);
1181 * Now that we've unmap all full blocks we'll have to zero out any
1182 * partial block at the beginning and/or end. iomap_zero_range is smart
1183 * enough to skip any holes, including those we just created, but we
1184 * must take care not to zero beyond EOF and enlarge i_size.
1186 if (offset
>= XFS_ISIZE(ip
))
1188 if (offset
+ len
> XFS_ISIZE(ip
))
1189 len
= XFS_ISIZE(ip
) - offset
;
1190 error
= iomap_zero_range(VFS_I(ip
), offset
, len
, NULL
, &xfs_iomap_ops
);
1195 * If we zeroed right up to EOF and EOF straddles a page boundary we
1196 * must make sure that the post-EOF area is also zeroed because the
1197 * page could be mmap'd and iomap_zero_range doesn't do that for us.
1198 * Writeback of the eof page will do this, albeit clumsily.
1200 if (offset
+ len
>= XFS_ISIZE(ip
) && ((offset
+ len
) & PAGE_MASK
)) {
1201 error
= filemap_write_and_wait_range(VFS_I(ip
)->i_mapping
,
1202 (offset
+ len
) & ~PAGE_MASK
, LLONG_MAX
);
1209 * Preallocate and zero a range of a file. This mechanism has the allocation
1210 * semantics of fallocate and in addition converts data in the range to zeroes.
1213 xfs_zero_file_space(
1214 struct xfs_inode
*ip
,
1218 struct xfs_mount
*mp
= ip
->i_mount
;
1222 trace_xfs_zero_file_space(ip
);
1224 blksize
= 1 << mp
->m_sb
.sb_blocklog
;
1227 * Punch a hole and prealloc the range. We use hole punch rather than
1228 * unwritten extent conversion for two reasons:
1230 * 1.) Hole punch handles partial block zeroing for us.
1232 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1233 * by virtue of the hole punch.
1235 error
= xfs_free_file_space(ip
, offset
, len
);
1239 error
= xfs_alloc_file_space(ip
, round_down(offset
, blksize
),
1240 round_up(offset
+ len
, blksize
) -
1241 round_down(offset
, blksize
),
1242 XFS_BMAPI_PREALLOC
);
1250 struct xfs_inode
*ip
,
1256 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1257 * into the accessible region of the file.
1259 if (xfs_can_free_eofblocks(ip
, true)) {
1260 error
= xfs_free_eofblocks(ip
);
1266 * Writeback and invalidate cache for the remainder of the file as we're
1267 * about to shift down every extent from offset to EOF.
1269 error
= filemap_write_and_wait_range(VFS_I(ip
)->i_mapping
, offset
, -1);
1272 error
= invalidate_inode_pages2_range(VFS_I(ip
)->i_mapping
,
1273 offset
>> PAGE_SHIFT
, -1);
1278 * Clean out anything hanging around in the cow fork now that
1279 * we've flushed all the dirty data out to disk to avoid having
1280 * CoW extents at the wrong offsets.
1282 if (xfs_is_reflink_inode(ip
)) {
1283 error
= xfs_reflink_cancel_cow_range(ip
, offset
, NULLFILEOFF
,
1293 * xfs_collapse_file_space()
1294 * This routine frees disk space and shift extent for the given file.
1295 * The first thing we do is to free data blocks in the specified range
1296 * by calling xfs_free_file_space(). It would also sync dirty data
1297 * and invalidate page cache over the region on which collapse range
1298 * is working. And Shift extent records to the left to cover a hole.
1305 xfs_collapse_file_space(
1306 struct xfs_inode
*ip
,
1310 struct xfs_mount
*mp
= ip
->i_mount
;
1311 struct xfs_trans
*tp
;
1313 struct xfs_defer_ops dfops
;
1314 xfs_fsblock_t first_block
;
1315 xfs_fileoff_t next_fsb
= XFS_B_TO_FSB(mp
, offset
+ len
);
1316 xfs_fileoff_t shift_fsb
= XFS_B_TO_FSB(mp
, len
);
1317 uint resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1320 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1321 ASSERT(xfs_isilocked(ip
, XFS_MMAPLOCK_EXCL
));
1323 trace_xfs_collapse_file_space(ip
);
1325 error
= xfs_free_file_space(ip
, offset
, len
);
1329 error
= xfs_prepare_shift(ip
, offset
);
1333 while (!error
&& !done
) {
1334 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0,
1339 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1340 error
= xfs_trans_reserve_quota(tp
, mp
, ip
->i_udquot
,
1341 ip
->i_gdquot
, ip
->i_pdquot
, resblks
, 0,
1342 XFS_QMOPT_RES_REGBLKS
);
1344 goto out_trans_cancel
;
1345 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1347 xfs_defer_init(&dfops
, &first_block
);
1348 error
= xfs_bmap_collapse_extents(tp
, ip
, &next_fsb
, shift_fsb
,
1349 &done
, &first_block
, &dfops
);
1351 goto out_bmap_cancel
;
1353 error
= xfs_defer_finish(&tp
, &dfops
);
1355 goto out_bmap_cancel
;
1356 error
= xfs_trans_commit(tp
);
1362 xfs_defer_cancel(&dfops
);
1364 xfs_trans_cancel(tp
);
1369 * xfs_insert_file_space()
1370 * This routine create hole space by shifting extents for the given file.
1371 * The first thing we do is to sync dirty data and invalidate page cache
1372 * over the region on which insert range is working. And split an extent
1373 * to two extents at given offset by calling xfs_bmap_split_extent.
1374 * And shift all extent records which are laying between [offset,
1375 * last allocated extent] to the right to reserve hole range.
1381 xfs_insert_file_space(
1382 struct xfs_inode
*ip
,
1386 struct xfs_mount
*mp
= ip
->i_mount
;
1387 struct xfs_trans
*tp
;
1389 struct xfs_defer_ops dfops
;
1390 xfs_fsblock_t first_block
;
1391 xfs_fileoff_t stop_fsb
= XFS_B_TO_FSB(mp
, offset
);
1392 xfs_fileoff_t next_fsb
= NULLFSBLOCK
;
1393 xfs_fileoff_t shift_fsb
= XFS_B_TO_FSB(mp
, len
);
1396 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1397 ASSERT(xfs_isilocked(ip
, XFS_MMAPLOCK_EXCL
));
1399 trace_xfs_insert_file_space(ip
);
1401 error
= xfs_bmap_can_insert_extents(ip
, stop_fsb
, shift_fsb
);
1405 error
= xfs_prepare_shift(ip
, offset
);
1410 * The extent shifting code works on extent granularity. So, if stop_fsb
1411 * is not the starting block of extent, we need to split the extent at
1414 error
= xfs_bmap_split_extent(ip
, stop_fsb
);
1418 while (!error
&& !done
) {
1419 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0, 0,
1424 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1425 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1426 xfs_defer_init(&dfops
, &first_block
);
1427 error
= xfs_bmap_insert_extents(tp
, ip
, &next_fsb
, shift_fsb
,
1428 &done
, stop_fsb
, &first_block
, &dfops
);
1430 goto out_bmap_cancel
;
1432 error
= xfs_defer_finish(&tp
, &dfops
);
1434 goto out_bmap_cancel
;
1435 error
= xfs_trans_commit(tp
);
1441 xfs_defer_cancel(&dfops
);
1442 xfs_trans_cancel(tp
);
1447 * We need to check that the format of the data fork in the temporary inode is
1448 * valid for the target inode before doing the swap. This is not a problem with
1449 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1450 * data fork depending on the space the attribute fork is taking so we can get
1451 * invalid formats on the target inode.
1453 * E.g. target has space for 7 extents in extent format, temp inode only has
1454 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1455 * btree, but when swapped it needs to be in extent format. Hence we can't just
1456 * blindly swap data forks on attr2 filesystems.
1458 * Note that we check the swap in both directions so that we don't end up with
1459 * a corrupt temporary inode, either.
1461 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1462 * inode will prevent this situation from occurring, so all we do here is
1463 * reject and log the attempt. basically we are putting the responsibility on
1464 * userspace to get this right.
1467 xfs_swap_extents_check_format(
1468 struct xfs_inode
*ip
, /* target inode */
1469 struct xfs_inode
*tip
) /* tmp inode */
1472 /* Should never get a local format */
1473 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
||
1474 tip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
1478 * if the target inode has less extents that then temporary inode then
1479 * why did userspace call us?
1481 if (ip
->i_d
.di_nextents
< tip
->i_d
.di_nextents
)
1485 * If we have to use the (expensive) rmap swap method, we can
1486 * handle any number of extents and any format.
1488 if (xfs_sb_version_hasrmapbt(&ip
->i_mount
->m_sb
))
1492 * if the target inode is in extent form and the temp inode is in btree
1493 * form then we will end up with the target inode in the wrong format
1494 * as we already know there are less extents in the temp inode.
1496 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1497 tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
)
1500 /* Check temp in extent form to max in target */
1501 if (tip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1502 XFS_IFORK_NEXTENTS(tip
, XFS_DATA_FORK
) >
1503 XFS_IFORK_MAXEXT(ip
, XFS_DATA_FORK
))
1506 /* Check target in extent form to max in temp */
1507 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1508 XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) >
1509 XFS_IFORK_MAXEXT(tip
, XFS_DATA_FORK
))
1513 * If we are in a btree format, check that the temp root block will fit
1514 * in the target and that it has enough extents to be in btree format
1517 * Note that we have to be careful to allow btree->extent conversions
1518 * (a common defrag case) which will occur when the temp inode is in
1521 if (tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1522 if (XFS_IFORK_Q(ip
) &&
1523 XFS_BMAP_BMDR_SPACE(tip
->i_df
.if_broot
) > XFS_IFORK_BOFF(ip
))
1525 if (XFS_IFORK_NEXTENTS(tip
, XFS_DATA_FORK
) <=
1526 XFS_IFORK_MAXEXT(ip
, XFS_DATA_FORK
))
1530 /* Reciprocal target->temp btree format checks */
1531 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1532 if (XFS_IFORK_Q(tip
) &&
1533 XFS_BMAP_BMDR_SPACE(ip
->i_df
.if_broot
) > XFS_IFORK_BOFF(tip
))
1535 if (XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) <=
1536 XFS_IFORK_MAXEXT(tip
, XFS_DATA_FORK
))
1544 xfs_swap_extent_flush(
1545 struct xfs_inode
*ip
)
1549 error
= filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
1552 truncate_pagecache_range(VFS_I(ip
), 0, -1);
1554 /* Verify O_DIRECT for ftmp */
1555 if (VFS_I(ip
)->i_mapping
->nrpages
)
1561 * Move extents from one file to another, when rmap is enabled.
1564 xfs_swap_extent_rmap(
1565 struct xfs_trans
**tpp
,
1566 struct xfs_inode
*ip
,
1567 struct xfs_inode
*tip
)
1569 struct xfs_bmbt_irec irec
;
1570 struct xfs_bmbt_irec uirec
;
1571 struct xfs_bmbt_irec tirec
;
1572 xfs_fileoff_t offset_fsb
;
1573 xfs_fileoff_t end_fsb
;
1574 xfs_filblks_t count_fsb
;
1575 xfs_fsblock_t firstfsb
;
1576 struct xfs_defer_ops dfops
;
1581 uint64_t tip_flags2
;
1584 * If the source file has shared blocks, we must flag the donor
1585 * file as having shared blocks so that we get the shared-block
1586 * rmap functions when we go to fix up the rmaps. The flags
1587 * will be switch for reals later.
1589 tip_flags2
= tip
->i_d
.di_flags2
;
1590 if (ip
->i_d
.di_flags2
& XFS_DIFLAG2_REFLINK
)
1591 tip
->i_d
.di_flags2
|= XFS_DIFLAG2_REFLINK
;
1594 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, i_size_read(VFS_I(ip
)));
1595 count_fsb
= (xfs_filblks_t
)(end_fsb
- offset_fsb
);
1598 /* Read extent from the donor file */
1600 error
= xfs_bmapi_read(tip
, offset_fsb
, count_fsb
, &tirec
,
1604 ASSERT(nimaps
== 1);
1605 ASSERT(tirec
.br_startblock
!= DELAYSTARTBLOCK
);
1607 trace_xfs_swap_extent_rmap_remap(tip
, &tirec
);
1608 ilen
= tirec
.br_blockcount
;
1610 /* Unmap the old blocks in the source file. */
1611 while (tirec
.br_blockcount
) {
1612 xfs_defer_init(&dfops
, &firstfsb
);
1613 trace_xfs_swap_extent_rmap_remap_piece(tip
, &tirec
);
1615 /* Read extent from the source file */
1617 error
= xfs_bmapi_read(ip
, tirec
.br_startoff
,
1618 tirec
.br_blockcount
, &irec
,
1622 ASSERT(nimaps
== 1);
1623 ASSERT(tirec
.br_startoff
== irec
.br_startoff
);
1624 trace_xfs_swap_extent_rmap_remap_piece(ip
, &irec
);
1626 /* Trim the extent. */
1628 uirec
.br_blockcount
= rlen
= min_t(xfs_filblks_t
,
1629 tirec
.br_blockcount
,
1630 irec
.br_blockcount
);
1631 trace_xfs_swap_extent_rmap_remap_piece(tip
, &uirec
);
1633 /* Remove the mapping from the donor file. */
1634 error
= xfs_bmap_unmap_extent((*tpp
)->t_mountp
, &dfops
,
1639 /* Remove the mapping from the source file. */
1640 error
= xfs_bmap_unmap_extent((*tpp
)->t_mountp
, &dfops
,
1645 /* Map the donor file's blocks into the source file. */
1646 error
= xfs_bmap_map_extent((*tpp
)->t_mountp
, &dfops
,
1651 /* Map the source file's blocks into the donor file. */
1652 error
= xfs_bmap_map_extent((*tpp
)->t_mountp
, &dfops
,
1657 xfs_defer_ijoin(&dfops
, ip
);
1658 error
= xfs_defer_finish(tpp
, &dfops
);
1662 tirec
.br_startoff
+= rlen
;
1663 if (tirec
.br_startblock
!= HOLESTARTBLOCK
&&
1664 tirec
.br_startblock
!= DELAYSTARTBLOCK
)
1665 tirec
.br_startblock
+= rlen
;
1666 tirec
.br_blockcount
-= rlen
;
1674 tip
->i_d
.di_flags2
= tip_flags2
;
1678 xfs_defer_cancel(&dfops
);
1680 trace_xfs_swap_extent_rmap_error(ip
, error
, _RET_IP_
);
1681 tip
->i_d
.di_flags2
= tip_flags2
;
1685 /* Swap the extents of two files by swapping data forks. */
1687 xfs_swap_extent_forks(
1688 struct xfs_trans
*tp
,
1689 struct xfs_inode
*ip
,
1690 struct xfs_inode
*tip
,
1692 int *target_log_flags
)
1694 struct xfs_ifork tempifp
, *ifp
, *tifp
;
1695 xfs_filblks_t aforkblks
= 0;
1696 xfs_filblks_t taforkblks
= 0;
1702 * Count the number of extended attribute blocks
1704 if ( ((XFS_IFORK_Q(ip
) != 0) && (ip
->i_d
.di_anextents
> 0)) &&
1705 (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)) {
1706 error
= xfs_bmap_count_blocks(tp
, ip
, XFS_ATTR_FORK
, &junk
,
1711 if ( ((XFS_IFORK_Q(tip
) != 0) && (tip
->i_d
.di_anextents
> 0)) &&
1712 (tip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)) {
1713 error
= xfs_bmap_count_blocks(tp
, tip
, XFS_ATTR_FORK
, &junk
,
1720 * Btree format (v3) inodes have the inode number stamped in the bmbt
1721 * block headers. We can't start changing the bmbt blocks until the
1722 * inode owner change is logged so recovery does the right thing in the
1723 * event of a crash. Set the owner change log flags now and leave the
1724 * bmbt scan as the last step.
1726 if (ip
->i_d
.di_version
== 3 &&
1727 ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
)
1728 (*target_log_flags
) |= XFS_ILOG_DOWNER
;
1729 if (tip
->i_d
.di_version
== 3 &&
1730 tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
)
1731 (*src_log_flags
) |= XFS_ILOG_DOWNER
;
1734 * Swap the data forks of the inodes
1738 tempifp
= *ifp
; /* struct copy */
1739 *ifp
= *tifp
; /* struct copy */
1740 *tifp
= tempifp
; /* struct copy */
1743 * Fix the on-disk inode values
1745 tmp
= (uint64_t)ip
->i_d
.di_nblocks
;
1746 ip
->i_d
.di_nblocks
= tip
->i_d
.di_nblocks
- taforkblks
+ aforkblks
;
1747 tip
->i_d
.di_nblocks
= tmp
+ taforkblks
- aforkblks
;
1749 tmp
= (uint64_t) ip
->i_d
.di_nextents
;
1750 ip
->i_d
.di_nextents
= tip
->i_d
.di_nextents
;
1751 tip
->i_d
.di_nextents
= tmp
;
1753 tmp
= (uint64_t) ip
->i_d
.di_format
;
1754 ip
->i_d
.di_format
= tip
->i_d
.di_format
;
1755 tip
->i_d
.di_format
= tmp
;
1758 * The extents in the source inode could still contain speculative
1759 * preallocation beyond EOF (e.g. the file is open but not modified
1760 * while defrag is in progress). In that case, we need to copy over the
1761 * number of delalloc blocks the data fork in the source inode is
1762 * tracking beyond EOF so that when the fork is truncated away when the
1763 * temporary inode is unlinked we don't underrun the i_delayed_blks
1764 * counter on that inode.
1766 ASSERT(tip
->i_delayed_blks
== 0);
1767 tip
->i_delayed_blks
= ip
->i_delayed_blks
;
1768 ip
->i_delayed_blks
= 0;
1770 switch (ip
->i_d
.di_format
) {
1771 case XFS_DINODE_FMT_EXTENTS
:
1772 (*src_log_flags
) |= XFS_ILOG_DEXT
;
1774 case XFS_DINODE_FMT_BTREE
:
1775 ASSERT(ip
->i_d
.di_version
< 3 ||
1776 (*src_log_flags
& XFS_ILOG_DOWNER
));
1777 (*src_log_flags
) |= XFS_ILOG_DBROOT
;
1781 switch (tip
->i_d
.di_format
) {
1782 case XFS_DINODE_FMT_EXTENTS
:
1783 (*target_log_flags
) |= XFS_ILOG_DEXT
;
1785 case XFS_DINODE_FMT_BTREE
:
1786 (*target_log_flags
) |= XFS_ILOG_DBROOT
;
1787 ASSERT(tip
->i_d
.di_version
< 3 ||
1788 (*target_log_flags
& XFS_ILOG_DOWNER
));
1796 * Fix up the owners of the bmbt blocks to refer to the current inode. The
1797 * change owner scan attempts to order all modified buffers in the current
1798 * transaction. In the event of ordered buffer failure, the offending buffer is
1799 * physically logged as a fallback and the scan returns -EAGAIN. We must roll
1800 * the transaction in this case to replenish the fallback log reservation and
1801 * restart the scan. This process repeats until the scan completes.
1804 xfs_swap_change_owner(
1805 struct xfs_trans
**tpp
,
1806 struct xfs_inode
*ip
,
1807 struct xfs_inode
*tmpip
)
1810 struct xfs_trans
*tp
= *tpp
;
1813 error
= xfs_bmbt_change_owner(tp
, ip
, XFS_DATA_FORK
, ip
->i_ino
,
1815 /* success or fatal error */
1816 if (error
!= -EAGAIN
)
1819 error
= xfs_trans_roll(tpp
);
1825 * Redirty both inodes so they can relog and keep the log tail
1828 xfs_trans_ijoin(tp
, ip
, 0);
1829 xfs_trans_ijoin(tp
, tmpip
, 0);
1830 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1831 xfs_trans_log_inode(tp
, tmpip
, XFS_ILOG_CORE
);
1839 struct xfs_inode
*ip
, /* target inode */
1840 struct xfs_inode
*tip
, /* tmp inode */
1841 struct xfs_swapext
*sxp
)
1843 struct xfs_mount
*mp
= ip
->i_mount
;
1844 struct xfs_trans
*tp
;
1845 struct xfs_bstat
*sbp
= &sxp
->sx_stat
;
1846 int src_log_flags
, target_log_flags
;
1849 struct xfs_ifork
*cowfp
;
1854 * Lock the inodes against other IO, page faults and truncate to
1855 * begin with. Then we can ensure the inodes are flushed and have no
1856 * page cache safely. Once we have done this we can take the ilocks and
1857 * do the rest of the checks.
1859 lock_two_nondirectories(VFS_I(ip
), VFS_I(tip
));
1860 lock_flags
= XFS_MMAPLOCK_EXCL
;
1861 xfs_lock_two_inodes(ip
, XFS_MMAPLOCK_EXCL
, tip
, XFS_MMAPLOCK_EXCL
);
1863 /* Verify that both files have the same format */
1864 if ((VFS_I(ip
)->i_mode
& S_IFMT
) != (VFS_I(tip
)->i_mode
& S_IFMT
)) {
1869 /* Verify both files are either real-time or non-realtime */
1870 if (XFS_IS_REALTIME_INODE(ip
) != XFS_IS_REALTIME_INODE(tip
)) {
1875 error
= xfs_swap_extent_flush(ip
);
1878 error
= xfs_swap_extent_flush(tip
);
1883 * Extent "swapping" with rmap requires a permanent reservation and
1884 * a block reservation because it's really just a remap operation
1885 * performed with log redo items!
1887 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
)) {
1888 int w
= XFS_DATA_FORK
;
1889 uint32_t ipnext
= XFS_IFORK_NEXTENTS(ip
, w
);
1890 uint32_t tipnext
= XFS_IFORK_NEXTENTS(tip
, w
);
1893 * Conceptually this shouldn't affect the shape of either bmbt,
1894 * but since we atomically move extents one by one, we reserve
1895 * enough space to rebuild both trees.
1897 resblks
= XFS_SWAP_RMAP_SPACE_RES(mp
, ipnext
, w
);
1898 resblks
+= XFS_SWAP_RMAP_SPACE_RES(mp
, tipnext
, w
);
1901 * Handle the corner case where either inode might straddle the
1902 * btree format boundary. If so, the inode could bounce between
1903 * btree <-> extent format on unmap -> remap cycles, freeing and
1904 * allocating a bmapbt block each time.
1906 if (ipnext
== (XFS_IFORK_MAXEXT(ip
, w
) + 1))
1907 resblks
+= XFS_IFORK_MAXEXT(ip
, w
);
1908 if (tipnext
== (XFS_IFORK_MAXEXT(tip
, w
) + 1))
1909 resblks
+= XFS_IFORK_MAXEXT(tip
, w
);
1911 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0, &tp
);
1916 * Lock and join the inodes to the tansaction so that transaction commit
1917 * or cancel will unlock the inodes from this point onwards.
1919 xfs_lock_two_inodes(ip
, XFS_ILOCK_EXCL
, tip
, XFS_ILOCK_EXCL
);
1920 lock_flags
|= XFS_ILOCK_EXCL
;
1921 xfs_trans_ijoin(tp
, ip
, 0);
1922 xfs_trans_ijoin(tp
, tip
, 0);
1925 /* Verify all data are being swapped */
1926 if (sxp
->sx_offset
!= 0 ||
1927 sxp
->sx_length
!= ip
->i_d
.di_size
||
1928 sxp
->sx_length
!= tip
->i_d
.di_size
) {
1930 goto out_trans_cancel
;
1933 trace_xfs_swap_extent_before(ip
, 0);
1934 trace_xfs_swap_extent_before(tip
, 1);
1936 /* check inode formats now that data is flushed */
1937 error
= xfs_swap_extents_check_format(ip
, tip
);
1940 "%s: inode 0x%llx format is incompatible for exchanging.",
1941 __func__
, ip
->i_ino
);
1942 goto out_trans_cancel
;
1946 * Compare the current change & modify times with that
1947 * passed in. If they differ, we abort this swap.
1948 * This is the mechanism used to ensure the calling
1949 * process that the file was not changed out from
1952 if ((sbp
->bs_ctime
.tv_sec
!= VFS_I(ip
)->i_ctime
.tv_sec
) ||
1953 (sbp
->bs_ctime
.tv_nsec
!= VFS_I(ip
)->i_ctime
.tv_nsec
) ||
1954 (sbp
->bs_mtime
.tv_sec
!= VFS_I(ip
)->i_mtime
.tv_sec
) ||
1955 (sbp
->bs_mtime
.tv_nsec
!= VFS_I(ip
)->i_mtime
.tv_nsec
)) {
1957 goto out_trans_cancel
;
1961 * Note the trickiness in setting the log flags - we set the owner log
1962 * flag on the opposite inode (i.e. the inode we are setting the new
1963 * owner to be) because once we swap the forks and log that, log
1964 * recovery is going to see the fork as owned by the swapped inode,
1965 * not the pre-swapped inodes.
1967 src_log_flags
= XFS_ILOG_CORE
;
1968 target_log_flags
= XFS_ILOG_CORE
;
1970 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
1971 error
= xfs_swap_extent_rmap(&tp
, ip
, tip
);
1973 error
= xfs_swap_extent_forks(tp
, ip
, tip
, &src_log_flags
,
1976 goto out_trans_cancel
;
1978 /* Do we have to swap reflink flags? */
1979 if ((ip
->i_d
.di_flags2
& XFS_DIFLAG2_REFLINK
) ^
1980 (tip
->i_d
.di_flags2
& XFS_DIFLAG2_REFLINK
)) {
1981 f
= ip
->i_d
.di_flags2
& XFS_DIFLAG2_REFLINK
;
1982 ip
->i_d
.di_flags2
&= ~XFS_DIFLAG2_REFLINK
;
1983 ip
->i_d
.di_flags2
|= tip
->i_d
.di_flags2
& XFS_DIFLAG2_REFLINK
;
1984 tip
->i_d
.di_flags2
&= ~XFS_DIFLAG2_REFLINK
;
1985 tip
->i_d
.di_flags2
|= f
& XFS_DIFLAG2_REFLINK
;
1988 /* Swap the cow forks. */
1989 if (xfs_sb_version_hasreflink(&mp
->m_sb
)) {
1990 xfs_extnum_t extnum
;
1992 ASSERT(ip
->i_cformat
== XFS_DINODE_FMT_EXTENTS
);
1993 ASSERT(tip
->i_cformat
== XFS_DINODE_FMT_EXTENTS
);
1995 extnum
= ip
->i_cnextents
;
1996 ip
->i_cnextents
= tip
->i_cnextents
;
1997 tip
->i_cnextents
= extnum
;
1999 cowfp
= ip
->i_cowfp
;
2000 ip
->i_cowfp
= tip
->i_cowfp
;
2001 tip
->i_cowfp
= cowfp
;
2003 if (ip
->i_cowfp
&& ip
->i_cowfp
->if_bytes
)
2004 xfs_inode_set_cowblocks_tag(ip
);
2006 xfs_inode_clear_cowblocks_tag(ip
);
2007 if (tip
->i_cowfp
&& tip
->i_cowfp
->if_bytes
)
2008 xfs_inode_set_cowblocks_tag(tip
);
2010 xfs_inode_clear_cowblocks_tag(tip
);
2013 xfs_trans_log_inode(tp
, ip
, src_log_flags
);
2014 xfs_trans_log_inode(tp
, tip
, target_log_flags
);
2017 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
2018 * have inode number owner values in the bmbt blocks that still refer to
2019 * the old inode. Scan each bmbt to fix up the owner values with the
2020 * inode number of the current inode.
2022 if (src_log_flags
& XFS_ILOG_DOWNER
) {
2023 error
= xfs_swap_change_owner(&tp
, ip
, tip
);
2025 goto out_trans_cancel
;
2027 if (target_log_flags
& XFS_ILOG_DOWNER
) {
2028 error
= xfs_swap_change_owner(&tp
, tip
, ip
);
2030 goto out_trans_cancel
;
2034 * If this is a synchronous mount, make sure that the
2035 * transaction goes to disk before returning to the user.
2037 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
2038 xfs_trans_set_sync(tp
);
2040 error
= xfs_trans_commit(tp
);
2042 trace_xfs_swap_extent_after(ip
, 0);
2043 trace_xfs_swap_extent_after(tip
, 1);
2046 xfs_iunlock(ip
, lock_flags
);
2047 xfs_iunlock(tip
, lock_flags
);
2048 unlock_two_nondirectories(VFS_I(ip
), VFS_I(tip
));
2052 xfs_trans_cancel(tp
);