2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
30 #include "xfs_trans.h"
31 #include "xfs_extfree_item.h"
32 #include "xfs_alloc.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_rtalloc.h"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
39 #include "xfs_trans_space.h"
40 #include "xfs_trace.h"
41 #include "xfs_icache.h"
44 /* Kernel only BMAP related definitions and functions */
47 * Convert the given file system block to a disk block. We have to treat it
48 * differently based on whether the file is a real time file or not, because the
52 xfs_fsb_to_db(struct xfs_inode
*ip
, xfs_fsblock_t fsb
)
54 return (XFS_IS_REALTIME_INODE(ip
) ? \
55 (xfs_daddr_t
)XFS_FSB_TO_BB((ip
)->i_mount
, (fsb
)) : \
56 XFS_FSB_TO_DADDR((ip
)->i_mount
, (fsb
)));
60 * Routine to zero an extent on disk allocated to the specific inode.
62 * The VFS functions take a linearised filesystem block offset, so we have to
63 * convert the sparse xfs fsb to the right format first.
64 * VFS types are real funky, too.
69 xfs_fsblock_t start_fsb
,
72 struct xfs_mount
*mp
= ip
->i_mount
;
73 xfs_daddr_t sector
= xfs_fsb_to_db(ip
, start_fsb
);
74 sector_t block
= XFS_BB_TO_FSBT(mp
, sector
);
76 return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip
)),
77 block
<< (mp
->m_super
->s_blocksize_bits
- 9),
78 count_fsb
<< (mp
->m_super
->s_blocksize_bits
- 9),
82 /* Sort bmap items by AG. */
84 xfs_bmap_free_list_cmp(
89 struct xfs_mount
*mp
= priv
;
90 struct xfs_bmap_free_item
*ra
;
91 struct xfs_bmap_free_item
*rb
;
93 ra
= container_of(a
, struct xfs_bmap_free_item
, xbfi_list
);
94 rb
= container_of(b
, struct xfs_bmap_free_item
, xbfi_list
);
95 return XFS_FSB_TO_AGNO(mp
, ra
->xbfi_startblock
) -
96 XFS_FSB_TO_AGNO(mp
, rb
->xbfi_startblock
);
100 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
101 * caller. Frees all the extents that need freeing, which must be done
102 * last due to locking considerations. We never free any extents in
103 * the first transaction.
105 * If an inode *ip is provided, rejoin it to the transaction if
106 * the transaction was committed.
110 struct xfs_trans
**tp
, /* transaction pointer addr */
111 struct xfs_bmap_free
*flist
, /* i/o: list extents to free */
112 struct xfs_inode
*ip
)
114 struct xfs_efd_log_item
*efd
; /* extent free data */
115 struct xfs_efi_log_item
*efi
; /* extent free intention */
116 int error
; /* error return value */
117 int committed
;/* xact committed or not */
118 struct xfs_bmap_free_item
*free
; /* free extent item */
120 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
121 if (flist
->xbf_count
== 0)
124 list_sort((*tp
)->t_mountp
, &flist
->xbf_flist
, xfs_bmap_free_list_cmp
);
126 efi
= xfs_trans_get_efi(*tp
, flist
->xbf_count
);
127 list_for_each_entry(free
, &flist
->xbf_flist
, xbfi_list
)
128 xfs_trans_log_efi_extent(*tp
, efi
, free
->xbfi_startblock
,
129 free
->xbfi_blockcount
);
131 error
= __xfs_trans_roll(tp
, ip
, &committed
);
134 * If the transaction was committed, drop the EFD reference
135 * since we're bailing out of here. The other reference is
136 * dropped when the EFI hits the AIL.
138 * If the transaction was not committed, the EFI is freed by the
139 * EFI item unlock handler on abort. Also, we have a new
140 * transaction so we should return committed=1 even though we're
141 * returning an error.
144 xfs_efi_release(efi
);
145 xfs_force_shutdown((*tp
)->t_mountp
,
146 SHUTDOWN_META_IO_ERROR
);
152 * Get an EFD and free each extent in the list, logging to the EFD in
153 * the process. The remaining bmap free list is cleaned up by the caller
156 efd
= xfs_trans_get_efd(*tp
, efi
, flist
->xbf_count
);
157 while (!list_empty(&flist
->xbf_flist
)) {
158 free
= list_first_entry(&flist
->xbf_flist
,
159 struct xfs_bmap_free_item
, xbfi_list
);
160 error
= xfs_trans_free_extent(*tp
, efd
, free
->xbfi_startblock
,
161 free
->xbfi_blockcount
);
165 xfs_bmap_del_free(flist
, free
);
173 struct xfs_bmalloca
*ap
) /* bmap alloc argument struct */
175 xfs_alloctype_t atype
= 0; /* type for allocation routines */
176 int error
; /* error return value */
177 xfs_mount_t
*mp
; /* mount point structure */
178 xfs_extlen_t prod
= 0; /* product factor for allocators */
179 xfs_extlen_t ralen
= 0; /* realtime allocation length */
180 xfs_extlen_t align
; /* minimum allocation alignment */
183 mp
= ap
->ip
->i_mount
;
184 align
= xfs_get_extsz_hint(ap
->ip
);
185 prod
= align
/ mp
->m_sb
.sb_rextsize
;
186 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
187 align
, 1, ap
->eof
, 0,
188 ap
->conv
, &ap
->offset
, &ap
->length
);
192 ASSERT(ap
->length
% mp
->m_sb
.sb_rextsize
== 0);
195 * If the offset & length are not perfectly aligned
196 * then kill prod, it will just get us in trouble.
198 if (do_mod(ap
->offset
, align
) || ap
->length
% align
)
201 * Set ralen to be the actual requested length in rtextents.
203 ralen
= ap
->length
/ mp
->m_sb
.sb_rextsize
;
205 * If the old value was close enough to MAXEXTLEN that
206 * we rounded up to it, cut it back so it's valid again.
207 * Note that if it's a really large request (bigger than
208 * MAXEXTLEN), we don't hear about that number, and can't
209 * adjust the starting point to match it.
211 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
212 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
215 * Lock out modifications to both the RT bitmap and summary inodes
217 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
218 xfs_trans_ijoin(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
219 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
);
220 xfs_trans_ijoin(ap
->tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
223 * If it's an allocation to an empty file at offset 0,
224 * pick an extent that will space things out in the rt area.
226 if (ap
->eof
&& ap
->offset
== 0) {
227 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
229 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
232 ap
->blkno
= rtx
* mp
->m_sb
.sb_rextsize
;
237 xfs_bmap_adjacent(ap
);
240 * Realtime allocation, done through xfs_rtallocate_extent.
242 atype
= ap
->blkno
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
243 do_div(ap
->blkno
, mp
->m_sb
.sb_rextsize
);
246 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1, ap
->length
,
247 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
249 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
250 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->blkno
, 1,
251 ap
->length
, &ralen
, atype
,
252 ap
->wasdel
, 1, &rtb
)))
255 if (ap
->blkno
!= NULLFSBLOCK
) {
256 ap
->blkno
*= mp
->m_sb
.sb_rextsize
;
257 ralen
*= mp
->m_sb
.sb_rextsize
;
259 ap
->ip
->i_d
.di_nblocks
+= ralen
;
260 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
262 ap
->ip
->i_delayed_blks
-= ralen
;
264 * Adjust the disk quota also. This was reserved
267 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
268 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
269 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
271 /* Zero the extent if we were asked to do so */
272 if (ap
->userdata
& XFS_ALLOC_USERDATA_ZERO
) {
273 error
= xfs_zero_extent(ap
->ip
, ap
->blkno
, ap
->length
);
284 * Check if the endoff is outside the last extent. If so the caller will grow
285 * the allocation to a stripe unit boundary. All offsets are considered outside
286 * the end of file for an empty fork, so 1 is returned in *eof in that case.
290 struct xfs_inode
*ip
,
291 xfs_fileoff_t endoff
,
295 struct xfs_bmbt_irec rec
;
298 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
302 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
307 * Extent tree block counting routines.
311 * Count leaf blocks given a range of extent records.
314 xfs_bmap_count_leaves(
322 for (b
= 0; b
< numrecs
; b
++) {
323 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
324 *count
+= xfs_bmbt_get_blockcount(frp
);
329 * Count leaf blocks given a range of extent records originally
333 xfs_bmap_disk_count_leaves(
334 struct xfs_mount
*mp
,
335 struct xfs_btree_block
*block
,
342 for (b
= 1; b
<= numrecs
; b
++) {
343 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
344 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
349 * Recursively walks each level of a btree
350 * to count total fsblocks in use.
352 STATIC
int /* error */
354 xfs_mount_t
*mp
, /* file system mount point */
355 xfs_trans_t
*tp
, /* transaction pointer */
356 xfs_ifork_t
*ifp
, /* inode fork pointer */
357 xfs_fsblock_t blockno
, /* file system block number */
358 int levelin
, /* level in btree */
359 int *count
) /* Count of blocks */
365 xfs_fsblock_t bno
= blockno
;
366 xfs_fsblock_t nextbno
;
367 struct xfs_btree_block
*block
, *nextblock
;
370 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
,
375 block
= XFS_BUF_TO_BLOCK(bp
);
378 /* Not at node above leaves, count this level of nodes */
379 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
380 while (nextbno
!= NULLFSBLOCK
) {
381 error
= xfs_btree_read_bufl(mp
, tp
, nextbno
, 0, &nbp
,
387 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
388 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
389 xfs_trans_brelse(tp
, nbp
);
392 /* Dive to the next level */
393 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
394 bno
= be64_to_cpu(*pp
);
395 if (unlikely((error
=
396 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
397 xfs_trans_brelse(tp
, bp
);
398 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
399 XFS_ERRLEVEL_LOW
, mp
);
400 return -EFSCORRUPTED
;
402 xfs_trans_brelse(tp
, bp
);
404 /* count all level 1 nodes and their leaves */
406 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
407 numrecs
= be16_to_cpu(block
->bb_numrecs
);
408 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
409 xfs_trans_brelse(tp
, bp
);
410 if (nextbno
== NULLFSBLOCK
)
413 error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
419 block
= XFS_BUF_TO_BLOCK(bp
);
426 * Count fsblocks of the given fork.
428 static int /* error */
429 xfs_bmap_count_blocks(
430 xfs_trans_t
*tp
, /* transaction pointer */
431 xfs_inode_t
*ip
, /* incore inode */
432 int whichfork
, /* data or attr fork */
433 int *count
) /* out: count of blocks */
435 struct xfs_btree_block
*block
; /* current btree block */
436 xfs_fsblock_t bno
; /* block # of "block" */
437 xfs_ifork_t
*ifp
; /* fork structure */
438 int level
; /* btree level, for checking */
439 xfs_mount_t
*mp
; /* file system mount structure */
440 __be64
*pp
; /* pointer to block address */
444 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
445 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
446 xfs_bmap_count_leaves(ifp
, 0,
447 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
453 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
455 block
= ifp
->if_broot
;
456 level
= be16_to_cpu(block
->bb_level
);
458 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
459 bno
= be64_to_cpu(*pp
);
460 ASSERT(bno
!= NULLFSBLOCK
);
461 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
462 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
464 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
465 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
467 return -EFSCORRUPTED
;
474 * returns 1 for success, 0 if we failed to map the extent.
477 xfs_getbmapx_fix_eof_hole(
478 xfs_inode_t
*ip
, /* xfs incore inode pointer */
479 struct getbmapx
*out
, /* output structure */
480 int prealloced
, /* this is a file with
481 * preallocated data space */
482 __int64_t end
, /* last block requested */
483 xfs_fsblock_t startblock
)
486 xfs_mount_t
*mp
; /* file system mount point */
487 xfs_ifork_t
*ifp
; /* inode fork pointer */
488 xfs_extnum_t lastx
; /* last extent pointer */
489 xfs_fileoff_t fileblock
;
491 if (startblock
== HOLESTARTBLOCK
) {
494 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)));
495 fixlen
-= out
->bmv_offset
;
496 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
497 /* Came to hole at EOF. Trim it. */
500 out
->bmv_length
= fixlen
;
503 if (startblock
== DELAYSTARTBLOCK
)
506 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
507 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
508 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
509 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
510 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
511 out
->bmv_oflags
|= BMV_OF_LAST
;
518 * Get inode's extents as described in bmv, and format for output.
519 * Calls formatter to fill the user's buffer until all extents
520 * are mapped, until the passed-in bmv->bmv_count slots have
521 * been filled, or until the formatter short-circuits the loop,
522 * if it is tracking filled-in extents on its own.
527 struct getbmapx
*bmv
, /* user bmap structure */
528 xfs_bmap_format_t formatter
, /* format to user */
529 void *arg
) /* formatter arg */
531 __int64_t bmvend
; /* last block requested */
532 int error
= 0; /* return value */
533 __int64_t fixlen
; /* length for -1 case */
534 int i
; /* extent number */
535 int lock
; /* lock state */
536 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
537 xfs_mount_t
*mp
; /* file system mount point */
538 int nex
; /* # of user extents can do */
539 int nexleft
; /* # of user extents left */
540 int subnex
; /* # of bmapi's can do */
541 int nmap
; /* number of map entries */
542 struct getbmapx
*out
; /* output structure */
543 int whichfork
; /* data or attr fork */
544 int prealloced
; /* this is a file with
545 * preallocated data space */
546 int iflags
; /* interface flags */
547 int bmapi_flags
; /* flags for xfs_bmapi */
551 iflags
= bmv
->bmv_iflags
;
552 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
554 if (whichfork
== XFS_ATTR_FORK
) {
555 if (XFS_IFORK_Q(ip
)) {
556 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
557 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
558 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
561 ip
->i_d
.di_aformat
!= 0 &&
562 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
563 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
565 return -EFSCORRUPTED
;
571 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
572 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
573 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
576 if (xfs_get_extsz_hint(ip
) ||
577 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
579 fixlen
= mp
->m_super
->s_maxbytes
;
582 fixlen
= XFS_ISIZE(ip
);
586 if (bmv
->bmv_length
== -1) {
587 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
589 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
590 } else if (bmv
->bmv_length
== 0) {
591 bmv
->bmv_entries
= 0;
593 } else if (bmv
->bmv_length
< 0) {
597 nex
= bmv
->bmv_count
- 1;
600 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
603 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
605 out
= kmem_zalloc_large(bmv
->bmv_count
* sizeof(struct getbmapx
), 0);
609 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
610 if (whichfork
== XFS_DATA_FORK
) {
611 if (!(iflags
& BMV_IF_DELALLOC
) &&
612 (ip
->i_delayed_blks
|| XFS_ISIZE(ip
) > ip
->i_d
.di_size
)) {
613 error
= filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
615 goto out_unlock_iolock
;
618 * Even after flushing the inode, there can still be
619 * delalloc blocks on the inode beyond EOF due to
620 * speculative preallocation. These are not removed
621 * until the release function is called or the inode
622 * is inactivated. Hence we cannot assert here that
623 * ip->i_delayed_blks == 0.
627 lock
= xfs_ilock_data_map_shared(ip
);
629 lock
= xfs_ilock_attr_map_shared(ip
);
633 * Don't let nex be bigger than the number of extents
634 * we can have assuming alternating holes and real extents.
636 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
637 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
639 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
640 if (!(iflags
& BMV_IF_PREALLOC
))
641 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
644 * Allocate enough space to handle "subnex" maps at a time.
648 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
650 goto out_unlock_ilock
;
652 bmv
->bmv_entries
= 0;
654 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
655 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
663 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
664 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
665 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
666 map
, &nmap
, bmapi_flags
);
669 ASSERT(nmap
<= subnex
);
671 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
672 out
[cur_ext
].bmv_oflags
= 0;
673 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
674 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
675 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
676 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
677 out
[cur_ext
].bmv_offset
=
678 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
679 out
[cur_ext
].bmv_length
=
680 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
681 out
[cur_ext
].bmv_unused1
= 0;
682 out
[cur_ext
].bmv_unused2
= 0;
685 * delayed allocation extents that start beyond EOF can
686 * occur due to speculative EOF allocation when the
687 * delalloc extent is larger than the largest freespace
688 * extent at conversion time. These extents cannot be
689 * converted by data writeback, so can exist here even
690 * if we are not supposed to be finding delalloc
693 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
&&
694 map
[i
].br_startoff
<= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
)))
695 ASSERT((iflags
& BMV_IF_DELALLOC
) != 0);
697 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
698 whichfork
== XFS_ATTR_FORK
) {
699 /* came to the end of attribute fork */
700 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
704 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
706 map
[i
].br_startblock
))
710 out
[cur_ext
].bmv_offset
+
711 out
[cur_ext
].bmv_length
;
713 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
716 * In case we don't want to return the hole,
717 * don't increase cur_ext so that we can reuse
718 * it in the next loop.
720 if ((iflags
& BMV_IF_NO_HOLES
) &&
721 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
722 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
730 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
735 xfs_iunlock(ip
, lock
);
737 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
739 for (i
= 0; i
< cur_ext
; i
++) {
740 int full
= 0; /* user array is full */
742 /* format results & advance arg */
743 error
= formatter(&arg
, &out
[i
], &full
);
753 * dead simple method of punching delalyed allocation blocks from a range in
754 * the inode. Walks a block at a time so will be slow, but is only executed in
755 * rare error cases so the overhead is not critical. This will always punch out
756 * both the start and end blocks, even if the ranges only partially overlap
757 * them, so it is up to the caller to ensure that partial blocks are not
761 xfs_bmap_punch_delalloc_range(
762 struct xfs_inode
*ip
,
763 xfs_fileoff_t start_fsb
,
764 xfs_fileoff_t length
)
766 xfs_fileoff_t remaining
= length
;
769 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
773 xfs_bmbt_irec_t imap
;
775 xfs_fsblock_t firstblock
;
776 xfs_bmap_free_t flist
;
779 * Map the range first and check that it is a delalloc extent
780 * before trying to unmap the range. Otherwise we will be
781 * trying to remove a real extent (which requires a
782 * transaction) or a hole, which is probably a bad idea...
784 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
788 /* something screwed, just bail */
789 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
790 xfs_alert(ip
->i_mount
,
791 "Failed delalloc mapping lookup ino %lld fsb %lld.",
792 ip
->i_ino
, start_fsb
);
800 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
801 /* been converted, ignore */
804 WARN_ON(imap
.br_blockcount
== 0);
807 * Note: while we initialise the firstblock/flist pair, they
808 * should never be used because blocks should never be
809 * allocated or freed for a delalloc extent and hence we need
810 * don't cancel or finish them after the xfs_bunmapi() call.
812 xfs_bmap_init(&flist
, &firstblock
);
813 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
818 ASSERT(!flist
.xbf_count
&& list_empty(&flist
.xbf_flist
));
822 } while(remaining
> 0);
828 * Test whether it is appropriate to check an inode for and free post EOF
829 * blocks. The 'force' parameter determines whether we should also consider
830 * regular files that are marked preallocated or append-only.
833 xfs_can_free_eofblocks(struct xfs_inode
*ip
, bool force
)
835 /* prealloc/delalloc exists only on regular files */
836 if (!S_ISREG(VFS_I(ip
)->i_mode
))
840 * Zero sized files with no cached pages and delalloc blocks will not
841 * have speculative prealloc/delalloc blocks to remove.
843 if (VFS_I(ip
)->i_size
== 0 &&
844 VFS_I(ip
)->i_mapping
->nrpages
== 0 &&
845 ip
->i_delayed_blks
== 0)
848 /* If we haven't read in the extent list, then don't do it now. */
849 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
))
853 * Do not free real preallocated or append-only files unless the file
854 * has delalloc blocks and we are forced to remove them.
856 if (ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
))
857 if (!force
|| ip
->i_delayed_blks
== 0)
864 * This is called by xfs_inactive to free any blocks beyond eof
865 * when the link count isn't zero and by xfs_dm_punch_hole() when
866 * punching a hole to EOF.
876 xfs_fileoff_t end_fsb
;
877 xfs_fileoff_t last_fsb
;
878 xfs_filblks_t map_len
;
880 xfs_bmbt_irec_t imap
;
883 * Figure out if there are any blocks beyond the end
884 * of the file. If not, then there is nothing to do.
886 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_ISIZE(ip
));
887 last_fsb
= XFS_B_TO_FSB(mp
, mp
->m_super
->s_maxbytes
);
888 if (last_fsb
<= end_fsb
)
890 map_len
= last_fsb
- end_fsb
;
893 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
894 error
= xfs_bmapi_read(ip
, end_fsb
, map_len
, &imap
, &nimaps
, 0);
895 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
897 if (!error
&& (nimaps
!= 0) &&
898 (imap
.br_startblock
!= HOLESTARTBLOCK
||
899 ip
->i_delayed_blks
)) {
901 * Attach the dquots to the inode up front.
903 error
= xfs_qm_dqattach(ip
, 0);
908 * There are blocks after the end of file.
909 * Free them up now by truncating the file to
913 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_EXCL
))
917 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
, 0, 0, 0,
920 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
922 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
926 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
927 xfs_trans_ijoin(tp
, ip
, 0);
930 * Do not update the on-disk file size. If we update the
931 * on-disk file size and then the system crashes before the
932 * contents of the file are flushed to disk then the files
933 * may be full of holes (ie NULL files bug).
935 error
= xfs_itruncate_extents(&tp
, ip
, XFS_DATA_FORK
,
939 * If we get an error at this point we simply don't
940 * bother truncating the file.
942 xfs_trans_cancel(tp
);
944 error
= xfs_trans_commit(tp
);
946 xfs_inode_clear_eofblocks_tag(ip
);
949 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
951 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
957 xfs_alloc_file_space(
958 struct xfs_inode
*ip
,
963 xfs_mount_t
*mp
= ip
->i_mount
;
965 xfs_filblks_t allocated_fsb
;
966 xfs_filblks_t allocatesize_fsb
;
967 xfs_extlen_t extsz
, temp
;
968 xfs_fileoff_t startoffset_fsb
;
969 xfs_fsblock_t firstfsb
;
974 xfs_bmbt_irec_t imaps
[1], *imapp
;
975 xfs_bmap_free_t free_list
;
976 uint qblocks
, resblks
, resrtextents
;
979 trace_xfs_alloc_file_space(ip
);
981 if (XFS_FORCED_SHUTDOWN(mp
))
984 error
= xfs_qm_dqattach(ip
, 0);
991 rt
= XFS_IS_REALTIME_INODE(ip
);
992 extsz
= xfs_get_extsz_hint(ip
);
997 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
998 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
1001 * Allocate file space until done or until there is an error
1003 while (allocatesize_fsb
&& !error
) {
1007 * Determine space reservations for data/realtime.
1009 if (unlikely(extsz
)) {
1010 s
= startoffset_fsb
;
1013 e
= startoffset_fsb
+ allocatesize_fsb
;
1014 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
1016 if ((temp
= do_mod(e
, extsz
)))
1020 e
= allocatesize_fsb
;
1024 * The transaction reservation is limited to a 32-bit block
1025 * count, hence we need to limit the number of blocks we are
1026 * trying to reserve to avoid an overflow. We can't allocate
1027 * more than @nimaps extents, and an extent is limited on disk
1028 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1030 resblks
= min_t(xfs_fileoff_t
, (e
- s
), (MAXEXTLEN
* nimaps
));
1032 resrtextents
= qblocks
= resblks
;
1033 resrtextents
/= mp
->m_sb
.sb_rextsize
;
1034 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1035 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
1038 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resblks
);
1039 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
1043 * Allocate and setup the transaction.
1045 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
,
1046 resrtextents
, 0, &tp
);
1049 * Check for running out of space
1053 * Free the transaction structure.
1055 ASSERT(error
== -ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
1058 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1059 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
,
1064 xfs_trans_ijoin(tp
, ip
, 0);
1066 xfs_bmap_init(&free_list
, &firstfsb
);
1067 error
= xfs_bmapi_write(tp
, ip
, startoffset_fsb
,
1068 allocatesize_fsb
, alloc_type
, &firstfsb
,
1069 resblks
, imapp
, &nimaps
, &free_list
);
1074 * Complete the transaction
1076 error
= xfs_bmap_finish(&tp
, &free_list
, NULL
);
1080 error
= xfs_trans_commit(tp
);
1081 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1085 allocated_fsb
= imapp
->br_blockcount
;
1092 startoffset_fsb
+= allocated_fsb
;
1093 allocatesize_fsb
-= allocated_fsb
;
1098 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1099 xfs_bmap_cancel(&free_list
);
1100 xfs_trans_unreserve_quota_nblks(tp
, ip
, (long)qblocks
, 0, quota_flag
);
1102 error1
: /* Just cancel transaction */
1103 xfs_trans_cancel(tp
);
1104 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1110 struct xfs_inode
*ip
,
1111 xfs_fileoff_t startoffset_fsb
,
1112 xfs_filblks_t len_fsb
,
1115 struct xfs_mount
*mp
= ip
->i_mount
;
1116 struct xfs_trans
*tp
;
1117 struct xfs_bmap_free free_list
;
1118 xfs_fsblock_t firstfsb
;
1119 uint resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1122 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0, &tp
);
1124 ASSERT(error
== -ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
1128 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1129 error
= xfs_trans_reserve_quota(tp
, mp
, ip
->i_udquot
, ip
->i_gdquot
,
1130 ip
->i_pdquot
, resblks
, 0, XFS_QMOPT_RES_REGBLKS
);
1132 goto out_trans_cancel
;
1134 xfs_trans_ijoin(tp
, ip
, 0);
1136 xfs_bmap_init(&free_list
, &firstfsb
);
1137 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
, len_fsb
, 0, 2, &firstfsb
,
1140 goto out_bmap_cancel
;
1142 error
= xfs_bmap_finish(&tp
, &free_list
, NULL
);
1144 goto out_bmap_cancel
;
1146 error
= xfs_trans_commit(tp
);
1148 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1152 xfs_bmap_cancel(&free_list
);
1154 xfs_trans_cancel(tp
);
1159 xfs_adjust_extent_unmap_boundaries(
1160 struct xfs_inode
*ip
,
1161 xfs_fileoff_t
*startoffset_fsb
,
1162 xfs_fileoff_t
*endoffset_fsb
)
1164 struct xfs_mount
*mp
= ip
->i_mount
;
1165 struct xfs_bmbt_irec imap
;
1167 xfs_extlen_t mod
= 0;
1170 error
= xfs_bmapi_read(ip
, *startoffset_fsb
, 1, &imap
, &nimap
, 0);
1174 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1177 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1178 block
= imap
.br_startblock
;
1179 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
1181 *startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
1185 error
= xfs_bmapi_read(ip
, *endoffset_fsb
- 1, 1, &imap
, &nimap
, 0);
1189 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1190 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1192 if (mod
&& mod
!= mp
->m_sb
.sb_rextsize
)
1193 *endoffset_fsb
-= mod
;
1200 xfs_flush_unmap_range(
1201 struct xfs_inode
*ip
,
1205 struct xfs_mount
*mp
= ip
->i_mount
;
1206 struct inode
*inode
= VFS_I(ip
);
1207 xfs_off_t rounding
, start
, end
;
1210 /* wait for the completion of any pending DIOs */
1211 inode_dio_wait(inode
);
1213 rounding
= max_t(xfs_off_t
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_SIZE
);
1214 start
= round_down(offset
, rounding
);
1215 end
= round_up(offset
+ len
, rounding
) - 1;
1217 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
1220 truncate_pagecache_range(inode
, start
, end
);
1225 xfs_free_file_space(
1226 struct xfs_inode
*ip
,
1230 struct xfs_mount
*mp
= ip
->i_mount
;
1231 xfs_fileoff_t startoffset_fsb
;
1232 xfs_fileoff_t endoffset_fsb
;
1233 int done
= 0, error
;
1235 trace_xfs_free_file_space(ip
);
1237 error
= xfs_qm_dqattach(ip
, 0);
1241 if (len
<= 0) /* if nothing being freed */
1244 error
= xfs_flush_unmap_range(ip
, offset
, len
);
1248 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
1249 endoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
+ len
);
1252 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1253 * and we can't use unwritten extents then we actually need to ensure
1254 * to zero the whole extent, otherwise we just need to take of block
1255 * boundaries, and xfs_bunmapi will handle the rest.
1257 if (XFS_IS_REALTIME_INODE(ip
) &&
1258 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
1259 error
= xfs_adjust_extent_unmap_boundaries(ip
, &startoffset_fsb
,
1265 if (endoffset_fsb
> startoffset_fsb
) {
1267 error
= xfs_unmap_extent(ip
, startoffset_fsb
,
1268 endoffset_fsb
- startoffset_fsb
, &done
);
1275 * Now that we've unmap all full blocks we'll have to zero out any
1276 * partial block at the beginning and/or end. xfs_zero_range is
1277 * smart enough to skip any holes, including those we just created.
1279 return xfs_zero_range(ip
, offset
, len
, NULL
);
1283 * Preallocate and zero a range of a file. This mechanism has the allocation
1284 * semantics of fallocate and in addition converts data in the range to zeroes.
1287 xfs_zero_file_space(
1288 struct xfs_inode
*ip
,
1292 struct xfs_mount
*mp
= ip
->i_mount
;
1296 trace_xfs_zero_file_space(ip
);
1298 blksize
= 1 << mp
->m_sb
.sb_blocklog
;
1301 * Punch a hole and prealloc the range. We use hole punch rather than
1302 * unwritten extent conversion for two reasons:
1304 * 1.) Hole punch handles partial block zeroing for us.
1306 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1307 * by virtue of the hole punch.
1309 error
= xfs_free_file_space(ip
, offset
, len
);
1313 error
= xfs_alloc_file_space(ip
, round_down(offset
, blksize
),
1314 round_up(offset
+ len
, blksize
) -
1315 round_down(offset
, blksize
),
1316 XFS_BMAPI_PREALLOC
);
1323 * @next_fsb will keep track of the extent currently undergoing shift.
1324 * @stop_fsb will keep track of the extent at which we have to stop.
1325 * If we are shifting left, we will start with block (offset + len) and
1326 * shift each extent till last extent.
1327 * If we are shifting right, we will start with last extent inside file space
1328 * and continue until we reach the block corresponding to offset.
1331 xfs_shift_file_space(
1332 struct xfs_inode
*ip
,
1335 enum shift_direction direction
)
1338 struct xfs_mount
*mp
= ip
->i_mount
;
1339 struct xfs_trans
*tp
;
1341 struct xfs_bmap_free free_list
;
1342 xfs_fsblock_t first_block
;
1343 xfs_fileoff_t stop_fsb
;
1344 xfs_fileoff_t next_fsb
;
1345 xfs_fileoff_t shift_fsb
;
1347 ASSERT(direction
== SHIFT_LEFT
|| direction
== SHIFT_RIGHT
);
1349 if (direction
== SHIFT_LEFT
) {
1350 next_fsb
= XFS_B_TO_FSB(mp
, offset
+ len
);
1351 stop_fsb
= XFS_B_TO_FSB(mp
, VFS_I(ip
)->i_size
);
1354 * If right shift, delegate the work of initialization of
1355 * next_fsb to xfs_bmap_shift_extent as it has ilock held.
1357 next_fsb
= NULLFSBLOCK
;
1358 stop_fsb
= XFS_B_TO_FSB(mp
, offset
);
1361 shift_fsb
= XFS_B_TO_FSB(mp
, len
);
1364 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1365 * into the accessible region of the file.
1367 if (xfs_can_free_eofblocks(ip
, true)) {
1368 error
= xfs_free_eofblocks(mp
, ip
, false);
1374 * Writeback and invalidate cache for the remainder of the file as we're
1375 * about to shift down every extent from offset to EOF.
1377 error
= filemap_write_and_wait_range(VFS_I(ip
)->i_mapping
,
1381 error
= invalidate_inode_pages2_range(VFS_I(ip
)->i_mapping
,
1382 offset
>> PAGE_SHIFT
, -1);
1387 * The extent shiting code works on extent granularity. So, if
1388 * stop_fsb is not the starting block of extent, we need to split
1389 * the extent at stop_fsb.
1391 if (direction
== SHIFT_RIGHT
) {
1392 error
= xfs_bmap_split_extent(ip
, stop_fsb
);
1397 while (!error
&& !done
) {
1399 * We would need to reserve permanent block for transaction.
1400 * This will come into picture when after shifting extent into
1401 * hole we found that adjacent extents can be merged which
1402 * may lead to freeing of a block during record update.
1404 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
,
1405 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0, 0, &tp
);
1409 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1410 error
= xfs_trans_reserve_quota(tp
, mp
, ip
->i_udquot
,
1411 ip
->i_gdquot
, ip
->i_pdquot
,
1412 XFS_DIOSTRAT_SPACE_RES(mp
, 0), 0,
1413 XFS_QMOPT_RES_REGBLKS
);
1415 goto out_trans_cancel
;
1417 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1419 xfs_bmap_init(&free_list
, &first_block
);
1422 * We are using the write transaction in which max 2 bmbt
1423 * updates are allowed
1425 error
= xfs_bmap_shift_extents(tp
, ip
, &next_fsb
, shift_fsb
,
1426 &done
, stop_fsb
, &first_block
, &free_list
,
1427 direction
, XFS_BMAP_MAX_SHIFT_EXTENTS
);
1429 goto out_bmap_cancel
;
1431 error
= xfs_bmap_finish(&tp
, &free_list
, NULL
);
1433 goto out_bmap_cancel
;
1435 error
= xfs_trans_commit(tp
);
1441 xfs_bmap_cancel(&free_list
);
1443 xfs_trans_cancel(tp
);
1448 * xfs_collapse_file_space()
1449 * This routine frees disk space and shift extent for the given file.
1450 * The first thing we do is to free data blocks in the specified range
1451 * by calling xfs_free_file_space(). It would also sync dirty data
1452 * and invalidate page cache over the region on which collapse range
1453 * is working. And Shift extent records to the left to cover a hole.
1460 xfs_collapse_file_space(
1461 struct xfs_inode
*ip
,
1467 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1468 trace_xfs_collapse_file_space(ip
);
1470 error
= xfs_free_file_space(ip
, offset
, len
);
1474 return xfs_shift_file_space(ip
, offset
, len
, SHIFT_LEFT
);
1478 * xfs_insert_file_space()
1479 * This routine create hole space by shifting extents for the given file.
1480 * The first thing we do is to sync dirty data and invalidate page cache
1481 * over the region on which insert range is working. And split an extent
1482 * to two extents at given offset by calling xfs_bmap_split_extent.
1483 * And shift all extent records which are laying between [offset,
1484 * last allocated extent] to the right to reserve hole range.
1490 xfs_insert_file_space(
1491 struct xfs_inode
*ip
,
1495 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1496 trace_xfs_insert_file_space(ip
);
1498 return xfs_shift_file_space(ip
, offset
, len
, SHIFT_RIGHT
);
1502 * We need to check that the format of the data fork in the temporary inode is
1503 * valid for the target inode before doing the swap. This is not a problem with
1504 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1505 * data fork depending on the space the attribute fork is taking so we can get
1506 * invalid formats on the target inode.
1508 * E.g. target has space for 7 extents in extent format, temp inode only has
1509 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1510 * btree, but when swapped it needs to be in extent format. Hence we can't just
1511 * blindly swap data forks on attr2 filesystems.
1513 * Note that we check the swap in both directions so that we don't end up with
1514 * a corrupt temporary inode, either.
1516 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1517 * inode will prevent this situation from occurring, so all we do here is
1518 * reject and log the attempt. basically we are putting the responsibility on
1519 * userspace to get this right.
1522 xfs_swap_extents_check_format(
1523 xfs_inode_t
*ip
, /* target inode */
1524 xfs_inode_t
*tip
) /* tmp inode */
1527 /* Should never get a local format */
1528 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
||
1529 tip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
)
1533 * if the target inode has less extents that then temporary inode then
1534 * why did userspace call us?
1536 if (ip
->i_d
.di_nextents
< tip
->i_d
.di_nextents
)
1540 * if the target inode is in extent form and the temp inode is in btree
1541 * form then we will end up with the target inode in the wrong format
1542 * as we already know there are less extents in the temp inode.
1544 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1545 tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
)
1548 /* Check temp in extent form to max in target */
1549 if (tip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1550 XFS_IFORK_NEXTENTS(tip
, XFS_DATA_FORK
) >
1551 XFS_IFORK_MAXEXT(ip
, XFS_DATA_FORK
))
1554 /* Check target in extent form to max in temp */
1555 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1556 XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) >
1557 XFS_IFORK_MAXEXT(tip
, XFS_DATA_FORK
))
1561 * If we are in a btree format, check that the temp root block will fit
1562 * in the target and that it has enough extents to be in btree format
1565 * Note that we have to be careful to allow btree->extent conversions
1566 * (a common defrag case) which will occur when the temp inode is in
1569 if (tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1570 if (XFS_IFORK_BOFF(ip
) &&
1571 XFS_BMAP_BMDR_SPACE(tip
->i_df
.if_broot
) > XFS_IFORK_BOFF(ip
))
1573 if (XFS_IFORK_NEXTENTS(tip
, XFS_DATA_FORK
) <=
1574 XFS_IFORK_MAXEXT(ip
, XFS_DATA_FORK
))
1578 /* Reciprocal target->temp btree format checks */
1579 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1580 if (XFS_IFORK_BOFF(tip
) &&
1581 XFS_BMAP_BMDR_SPACE(ip
->i_df
.if_broot
) > XFS_IFORK_BOFF(tip
))
1583 if (XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) <=
1584 XFS_IFORK_MAXEXT(tip
, XFS_DATA_FORK
))
1592 xfs_swap_extent_flush(
1593 struct xfs_inode
*ip
)
1597 error
= filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
1600 truncate_pagecache_range(VFS_I(ip
), 0, -1);
1602 /* Verify O_DIRECT for ftmp */
1603 if (VFS_I(ip
)->i_mapping
->nrpages
)
1610 xfs_inode_t
*ip
, /* target inode */
1611 xfs_inode_t
*tip
, /* tmp inode */
1614 xfs_mount_t
*mp
= ip
->i_mount
;
1616 xfs_bstat_t
*sbp
= &sxp
->sx_stat
;
1617 xfs_ifork_t
*tempifp
, *ifp
, *tifp
;
1618 int src_log_flags
, target_log_flags
;
1625 tempifp
= kmem_alloc(sizeof(xfs_ifork_t
), KM_MAYFAIL
);
1632 * Lock the inodes against other IO, page faults and truncate to
1633 * begin with. Then we can ensure the inodes are flushed and have no
1634 * page cache safely. Once we have done this we can take the ilocks and
1635 * do the rest of the checks.
1637 lock_flags
= XFS_IOLOCK_EXCL
| XFS_MMAPLOCK_EXCL
;
1638 xfs_lock_two_inodes(ip
, tip
, XFS_IOLOCK_EXCL
);
1639 xfs_lock_two_inodes(ip
, tip
, XFS_MMAPLOCK_EXCL
);
1641 /* Verify that both files have the same format */
1642 if ((VFS_I(ip
)->i_mode
& S_IFMT
) != (VFS_I(tip
)->i_mode
& S_IFMT
)) {
1647 /* Verify both files are either real-time or non-realtime */
1648 if (XFS_IS_REALTIME_INODE(ip
) != XFS_IS_REALTIME_INODE(tip
)) {
1653 error
= xfs_swap_extent_flush(ip
);
1656 error
= xfs_swap_extent_flush(tip
);
1660 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 0, 0, 0, &tp
);
1665 * Lock and join the inodes to the tansaction so that transaction commit
1666 * or cancel will unlock the inodes from this point onwards.
1668 xfs_lock_two_inodes(ip
, tip
, XFS_ILOCK_EXCL
);
1669 lock_flags
|= XFS_ILOCK_EXCL
;
1670 xfs_trans_ijoin(tp
, ip
, lock_flags
);
1671 xfs_trans_ijoin(tp
, tip
, lock_flags
);
1674 /* Verify all data are being swapped */
1675 if (sxp
->sx_offset
!= 0 ||
1676 sxp
->sx_length
!= ip
->i_d
.di_size
||
1677 sxp
->sx_length
!= tip
->i_d
.di_size
) {
1679 goto out_trans_cancel
;
1682 trace_xfs_swap_extent_before(ip
, 0);
1683 trace_xfs_swap_extent_before(tip
, 1);
1685 /* check inode formats now that data is flushed */
1686 error
= xfs_swap_extents_check_format(ip
, tip
);
1689 "%s: inode 0x%llx format is incompatible for exchanging.",
1690 __func__
, ip
->i_ino
);
1691 goto out_trans_cancel
;
1695 * Compare the current change & modify times with that
1696 * passed in. If they differ, we abort this swap.
1697 * This is the mechanism used to ensure the calling
1698 * process that the file was not changed out from
1701 if ((sbp
->bs_ctime
.tv_sec
!= VFS_I(ip
)->i_ctime
.tv_sec
) ||
1702 (sbp
->bs_ctime
.tv_nsec
!= VFS_I(ip
)->i_ctime
.tv_nsec
) ||
1703 (sbp
->bs_mtime
.tv_sec
!= VFS_I(ip
)->i_mtime
.tv_sec
) ||
1704 (sbp
->bs_mtime
.tv_nsec
!= VFS_I(ip
)->i_mtime
.tv_nsec
)) {
1706 goto out_trans_cancel
;
1709 * Count the number of extended attribute blocks
1711 if ( ((XFS_IFORK_Q(ip
) != 0) && (ip
->i_d
.di_anextents
> 0)) &&
1712 (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)) {
1713 error
= xfs_bmap_count_blocks(tp
, ip
, XFS_ATTR_FORK
, &aforkblks
);
1715 goto out_trans_cancel
;
1717 if ( ((XFS_IFORK_Q(tip
) != 0) && (tip
->i_d
.di_anextents
> 0)) &&
1718 (tip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)) {
1719 error
= xfs_bmap_count_blocks(tp
, tip
, XFS_ATTR_FORK
,
1722 goto out_trans_cancel
;
1726 * Before we've swapped the forks, lets set the owners of the forks
1727 * appropriately. We have to do this as we are demand paging the btree
1728 * buffers, and so the validation done on read will expect the owner
1729 * field to be correctly set. Once we change the owners, we can swap the
1732 * Note the trickiness in setting the log flags - we set the owner log
1733 * flag on the opposite inode (i.e. the inode we are setting the new
1734 * owner to be) because once we swap the forks and log that, log
1735 * recovery is going to see the fork as owned by the swapped inode,
1736 * not the pre-swapped inodes.
1738 src_log_flags
= XFS_ILOG_CORE
;
1739 target_log_flags
= XFS_ILOG_CORE
;
1740 if (ip
->i_d
.di_version
== 3 &&
1741 ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1742 target_log_flags
|= XFS_ILOG_DOWNER
;
1743 error
= xfs_bmbt_change_owner(tp
, ip
, XFS_DATA_FORK
,
1746 goto out_trans_cancel
;
1749 if (tip
->i_d
.di_version
== 3 &&
1750 tip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
1751 src_log_flags
|= XFS_ILOG_DOWNER
;
1752 error
= xfs_bmbt_change_owner(tp
, tip
, XFS_DATA_FORK
,
1755 goto out_trans_cancel
;
1759 * Swap the data forks of the inodes
1763 *tempifp
= *ifp
; /* struct copy */
1764 *ifp
= *tifp
; /* struct copy */
1765 *tifp
= *tempifp
; /* struct copy */
1768 * Fix the on-disk inode values
1770 tmp
= (__uint64_t
)ip
->i_d
.di_nblocks
;
1771 ip
->i_d
.di_nblocks
= tip
->i_d
.di_nblocks
- taforkblks
+ aforkblks
;
1772 tip
->i_d
.di_nblocks
= tmp
+ taforkblks
- aforkblks
;
1774 tmp
= (__uint64_t
) ip
->i_d
.di_nextents
;
1775 ip
->i_d
.di_nextents
= tip
->i_d
.di_nextents
;
1776 tip
->i_d
.di_nextents
= tmp
;
1778 tmp
= (__uint64_t
) ip
->i_d
.di_format
;
1779 ip
->i_d
.di_format
= tip
->i_d
.di_format
;
1780 tip
->i_d
.di_format
= tmp
;
1783 * The extents in the source inode could still contain speculative
1784 * preallocation beyond EOF (e.g. the file is open but not modified
1785 * while defrag is in progress). In that case, we need to copy over the
1786 * number of delalloc blocks the data fork in the source inode is
1787 * tracking beyond EOF so that when the fork is truncated away when the
1788 * temporary inode is unlinked we don't underrun the i_delayed_blks
1789 * counter on that inode.
1791 ASSERT(tip
->i_delayed_blks
== 0);
1792 tip
->i_delayed_blks
= ip
->i_delayed_blks
;
1793 ip
->i_delayed_blks
= 0;
1795 switch (ip
->i_d
.di_format
) {
1796 case XFS_DINODE_FMT_EXTENTS
:
1797 /* If the extents fit in the inode, fix the
1798 * pointer. Otherwise it's already NULL or
1799 * pointing to the extent.
1801 if (ip
->i_d
.di_nextents
<= XFS_INLINE_EXTS
) {
1802 ifp
->if_u1
.if_extents
=
1803 ifp
->if_u2
.if_inline_ext
;
1805 src_log_flags
|= XFS_ILOG_DEXT
;
1807 case XFS_DINODE_FMT_BTREE
:
1808 ASSERT(ip
->i_d
.di_version
< 3 ||
1809 (src_log_flags
& XFS_ILOG_DOWNER
));
1810 src_log_flags
|= XFS_ILOG_DBROOT
;
1814 switch (tip
->i_d
.di_format
) {
1815 case XFS_DINODE_FMT_EXTENTS
:
1816 /* If the extents fit in the inode, fix the
1817 * pointer. Otherwise it's already NULL or
1818 * pointing to the extent.
1820 if (tip
->i_d
.di_nextents
<= XFS_INLINE_EXTS
) {
1821 tifp
->if_u1
.if_extents
=
1822 tifp
->if_u2
.if_inline_ext
;
1824 target_log_flags
|= XFS_ILOG_DEXT
;
1826 case XFS_DINODE_FMT_BTREE
:
1827 target_log_flags
|= XFS_ILOG_DBROOT
;
1828 ASSERT(tip
->i_d
.di_version
< 3 ||
1829 (target_log_flags
& XFS_ILOG_DOWNER
));
1833 xfs_trans_log_inode(tp
, ip
, src_log_flags
);
1834 xfs_trans_log_inode(tp
, tip
, target_log_flags
);
1837 * If this is a synchronous mount, make sure that the
1838 * transaction goes to disk before returning to the user.
1840 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
1841 xfs_trans_set_sync(tp
);
1843 error
= xfs_trans_commit(tp
);
1845 trace_xfs_swap_extent_after(ip
, 0);
1846 trace_xfs_swap_extent_after(tip
, 1);
1852 xfs_iunlock(ip
, lock_flags
);
1853 xfs_iunlock(tip
, lock_flags
);
1857 xfs_trans_cancel(tp
);