1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_btree.h"
16 #include "xfs_rmap_btree.h"
17 #include "xfs_trace.h"
19 #include "xfs_alloc.h"
21 #include <linux/fsmap.h>
22 #include "xfs_fsmap.h"
23 #include "xfs_refcount.h"
24 #include "xfs_refcount_btree.h"
25 #include "xfs_alloc_btree.h"
26 #include "xfs_rtalloc.h"
28 /* Convert an xfs_fsmap to an fsmap. */
30 xfs_fsmap_from_internal(
32 struct xfs_fsmap
*src
)
34 dest
->fmr_device
= src
->fmr_device
;
35 dest
->fmr_flags
= src
->fmr_flags
;
36 dest
->fmr_physical
= BBTOB(src
->fmr_physical
);
37 dest
->fmr_owner
= src
->fmr_owner
;
38 dest
->fmr_offset
= BBTOB(src
->fmr_offset
);
39 dest
->fmr_length
= BBTOB(src
->fmr_length
);
40 dest
->fmr_reserved
[0] = 0;
41 dest
->fmr_reserved
[1] = 0;
42 dest
->fmr_reserved
[2] = 0;
45 /* Convert an fsmap to an xfs_fsmap. */
47 xfs_fsmap_to_internal(
48 struct xfs_fsmap
*dest
,
51 dest
->fmr_device
= src
->fmr_device
;
52 dest
->fmr_flags
= src
->fmr_flags
;
53 dest
->fmr_physical
= BTOBBT(src
->fmr_physical
);
54 dest
->fmr_owner
= src
->fmr_owner
;
55 dest
->fmr_offset
= BTOBBT(src
->fmr_offset
);
56 dest
->fmr_length
= BTOBBT(src
->fmr_length
);
59 /* Convert an fsmap owner into an rmapbt owner. */
61 xfs_fsmap_owner_to_rmap(
62 struct xfs_rmap_irec
*dest
,
63 struct xfs_fsmap
*src
)
65 if (!(src
->fmr_flags
& FMR_OF_SPECIAL_OWNER
)) {
66 dest
->rm_owner
= src
->fmr_owner
;
70 switch (src
->fmr_owner
) {
71 case 0: /* "lowest owner id possible" */
72 case -1ULL: /* "highest owner id possible" */
75 case XFS_FMR_OWN_FREE
:
76 dest
->rm_owner
= XFS_RMAP_OWN_NULL
;
78 case XFS_FMR_OWN_UNKNOWN
:
79 dest
->rm_owner
= XFS_RMAP_OWN_UNKNOWN
;
82 dest
->rm_owner
= XFS_RMAP_OWN_FS
;
85 dest
->rm_owner
= XFS_RMAP_OWN_LOG
;
88 dest
->rm_owner
= XFS_RMAP_OWN_AG
;
90 case XFS_FMR_OWN_INOBT
:
91 dest
->rm_owner
= XFS_RMAP_OWN_INOBT
;
93 case XFS_FMR_OWN_INODES
:
94 dest
->rm_owner
= XFS_RMAP_OWN_INODES
;
96 case XFS_FMR_OWN_REFC
:
97 dest
->rm_owner
= XFS_RMAP_OWN_REFC
;
100 dest
->rm_owner
= XFS_RMAP_OWN_COW
;
102 case XFS_FMR_OWN_DEFECTIVE
: /* not implemented */
110 /* Convert an rmapbt owner into an fsmap owner. */
112 xfs_fsmap_owner_from_rmap(
113 struct xfs_fsmap
*dest
,
114 struct xfs_rmap_irec
*src
)
117 if (!XFS_RMAP_NON_INODE_OWNER(src
->rm_owner
)) {
118 dest
->fmr_owner
= src
->rm_owner
;
121 dest
->fmr_flags
|= FMR_OF_SPECIAL_OWNER
;
123 switch (src
->rm_owner
) {
124 case XFS_RMAP_OWN_FS
:
125 dest
->fmr_owner
= XFS_FMR_OWN_FS
;
127 case XFS_RMAP_OWN_LOG
:
128 dest
->fmr_owner
= XFS_FMR_OWN_LOG
;
130 case XFS_RMAP_OWN_AG
:
131 dest
->fmr_owner
= XFS_FMR_OWN_AG
;
133 case XFS_RMAP_OWN_INOBT
:
134 dest
->fmr_owner
= XFS_FMR_OWN_INOBT
;
136 case XFS_RMAP_OWN_INODES
:
137 dest
->fmr_owner
= XFS_FMR_OWN_INODES
;
139 case XFS_RMAP_OWN_REFC
:
140 dest
->fmr_owner
= XFS_FMR_OWN_REFC
;
142 case XFS_RMAP_OWN_COW
:
143 dest
->fmr_owner
= XFS_FMR_OWN_COW
;
145 case XFS_RMAP_OWN_NULL
: /* "free" */
146 dest
->fmr_owner
= XFS_FMR_OWN_FREE
;
149 return -EFSCORRUPTED
;
154 /* getfsmap query state */
155 struct xfs_getfsmap_info
{
156 struct xfs_fsmap_head
*head
;
157 xfs_fsmap_format_t formatter
; /* formatting fn */
158 void *format_arg
; /* format buffer */
159 struct xfs_buf
*agf_bp
; /* AGF, for refcount queries */
160 xfs_daddr_t next_daddr
; /* next daddr we expect */
161 u64 missing_owner
; /* owner of holes */
162 u32 dev
; /* device id */
163 xfs_agnumber_t agno
; /* AG number, if applicable */
164 struct xfs_rmap_irec low
; /* low rmap key */
165 struct xfs_rmap_irec high
; /* high rmap key */
166 bool last
; /* last extent? */
169 /* Associate a device with a getfsmap handler. */
170 struct xfs_getfsmap_dev
{
172 int (*fn
)(struct xfs_trans
*tp
,
173 struct xfs_fsmap
*keys
,
174 struct xfs_getfsmap_info
*info
);
177 /* Compare two getfsmap device handlers. */
179 xfs_getfsmap_dev_compare(
183 const struct xfs_getfsmap_dev
*d1
= p1
;
184 const struct xfs_getfsmap_dev
*d2
= p2
;
186 return d1
->dev
- d2
->dev
;
189 /* Decide if this mapping is shared. */
191 xfs_getfsmap_is_shared(
192 struct xfs_trans
*tp
,
193 struct xfs_getfsmap_info
*info
,
194 struct xfs_rmap_irec
*rec
,
197 struct xfs_mount
*mp
= tp
->t_mountp
;
198 struct xfs_btree_cur
*cur
;
204 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
206 /* rt files will have agno set to NULLAGNUMBER */
207 if (info
->agno
== NULLAGNUMBER
)
210 /* Are there any shared blocks here? */
212 cur
= xfs_refcountbt_init_cursor(mp
, tp
, info
->agf_bp
,
215 error
= xfs_refcount_find_shared(cur
, rec
->rm_startblock
,
216 rec
->rm_blockcount
, &fbno
, &flen
, false);
218 xfs_btree_del_cursor(cur
, error
);
227 * Format a reverse mapping for getfsmap, having translated rm_startblock
228 * into the appropriate daddr units.
232 struct xfs_trans
*tp
,
233 struct xfs_getfsmap_info
*info
,
234 struct xfs_rmap_irec
*rec
,
235 xfs_daddr_t rec_daddr
)
237 struct xfs_fsmap fmr
;
238 struct xfs_mount
*mp
= tp
->t_mountp
;
242 if (fatal_signal_pending(current
))
246 * Filter out records that start before our startpoint, if the
247 * caller requested that.
249 if (xfs_rmap_compare(rec
, &info
->low
) < 0) {
250 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
251 if (info
->next_daddr
< rec_daddr
)
252 info
->next_daddr
= rec_daddr
;
253 return XFS_BTREE_QUERY_RANGE_CONTINUE
;
256 /* Are we just counting mappings? */
257 if (info
->head
->fmh_count
== 0) {
258 if (rec_daddr
> info
->next_daddr
)
259 info
->head
->fmh_entries
++;
262 return XFS_BTREE_QUERY_RANGE_CONTINUE
;
264 info
->head
->fmh_entries
++;
266 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
267 if (info
->next_daddr
< rec_daddr
)
268 info
->next_daddr
= rec_daddr
;
269 return XFS_BTREE_QUERY_RANGE_CONTINUE
;
273 * If the record starts past the last physical block we saw,
274 * then we've found a gap. Report the gap as being owned by
275 * whatever the caller specified is the missing owner.
277 if (rec_daddr
> info
->next_daddr
) {
278 if (info
->head
->fmh_entries
>= info
->head
->fmh_count
)
279 return XFS_BTREE_QUERY_RANGE_ABORT
;
281 fmr
.fmr_device
= info
->dev
;
282 fmr
.fmr_physical
= info
->next_daddr
;
283 fmr
.fmr_owner
= info
->missing_owner
;
285 fmr
.fmr_length
= rec_daddr
- info
->next_daddr
;
286 fmr
.fmr_flags
= FMR_OF_SPECIAL_OWNER
;
287 error
= info
->formatter(&fmr
, info
->format_arg
);
290 info
->head
->fmh_entries
++;
296 /* Fill out the extent we found */
297 if (info
->head
->fmh_entries
>= info
->head
->fmh_count
)
298 return XFS_BTREE_QUERY_RANGE_ABORT
;
300 trace_xfs_fsmap_mapping(mp
, info
->dev
, info
->agno
, rec
);
302 fmr
.fmr_device
= info
->dev
;
303 fmr
.fmr_physical
= rec_daddr
;
304 error
= xfs_fsmap_owner_from_rmap(&fmr
, rec
);
307 fmr
.fmr_offset
= XFS_FSB_TO_BB(mp
, rec
->rm_offset
);
308 fmr
.fmr_length
= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
309 if (rec
->rm_flags
& XFS_RMAP_UNWRITTEN
)
310 fmr
.fmr_flags
|= FMR_OF_PREALLOC
;
311 if (rec
->rm_flags
& XFS_RMAP_ATTR_FORK
)
312 fmr
.fmr_flags
|= FMR_OF_ATTR_FORK
;
313 if (rec
->rm_flags
& XFS_RMAP_BMBT_BLOCK
)
314 fmr
.fmr_flags
|= FMR_OF_EXTENT_MAP
;
315 if (fmr
.fmr_flags
== 0) {
316 error
= xfs_getfsmap_is_shared(tp
, info
, rec
, &shared
);
320 fmr
.fmr_flags
|= FMR_OF_SHARED
;
322 error
= info
->formatter(&fmr
, info
->format_arg
);
325 info
->head
->fmh_entries
++;
328 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
329 if (info
->next_daddr
< rec_daddr
)
330 info
->next_daddr
= rec_daddr
;
331 return XFS_BTREE_QUERY_RANGE_CONTINUE
;
334 /* Transform a rmapbt irec into a fsmap */
336 xfs_getfsmap_datadev_helper(
337 struct xfs_btree_cur
*cur
,
338 struct xfs_rmap_irec
*rec
,
341 struct xfs_mount
*mp
= cur
->bc_mp
;
342 struct xfs_getfsmap_info
*info
= priv
;
344 xfs_daddr_t rec_daddr
;
346 fsb
= XFS_AGB_TO_FSB(mp
, cur
->bc_private
.a
.agno
, rec
->rm_startblock
);
347 rec_daddr
= XFS_FSB_TO_DADDR(mp
, fsb
);
349 return xfs_getfsmap_helper(cur
->bc_tp
, info
, rec
, rec_daddr
);
352 /* Transform a bnobt irec into a fsmap */
354 xfs_getfsmap_datadev_bnobt_helper(
355 struct xfs_btree_cur
*cur
,
356 struct xfs_alloc_rec_incore
*rec
,
359 struct xfs_mount
*mp
= cur
->bc_mp
;
360 struct xfs_getfsmap_info
*info
= priv
;
361 struct xfs_rmap_irec irec
;
362 xfs_daddr_t rec_daddr
;
364 rec_daddr
= XFS_AGB_TO_DADDR(mp
, cur
->bc_private
.a
.agno
,
367 irec
.rm_startblock
= rec
->ar_startblock
;
368 irec
.rm_blockcount
= rec
->ar_blockcount
;
369 irec
.rm_owner
= XFS_RMAP_OWN_NULL
; /* "free" */
373 return xfs_getfsmap_helper(cur
->bc_tp
, info
, &irec
, rec_daddr
);
376 /* Set rmap flags based on the getfsmap flags */
378 xfs_getfsmap_set_irec_flags(
379 struct xfs_rmap_irec
*irec
,
380 struct xfs_fsmap
*fmr
)
383 if (fmr
->fmr_flags
& FMR_OF_ATTR_FORK
)
384 irec
->rm_flags
|= XFS_RMAP_ATTR_FORK
;
385 if (fmr
->fmr_flags
& FMR_OF_EXTENT_MAP
)
386 irec
->rm_flags
|= XFS_RMAP_BMBT_BLOCK
;
387 if (fmr
->fmr_flags
& FMR_OF_PREALLOC
)
388 irec
->rm_flags
|= XFS_RMAP_UNWRITTEN
;
391 /* Execute a getfsmap query against the log device. */
394 struct xfs_trans
*tp
,
395 struct xfs_fsmap
*keys
,
396 struct xfs_getfsmap_info
*info
)
398 struct xfs_mount
*mp
= tp
->t_mountp
;
399 struct xfs_rmap_irec rmap
;
402 /* Set up search keys */
403 info
->low
.rm_startblock
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_physical
);
404 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
405 error
= xfs_fsmap_owner_to_rmap(&info
->low
, keys
);
408 info
->low
.rm_blockcount
= 0;
409 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
411 error
= xfs_fsmap_owner_to_rmap(&info
->high
, keys
+ 1);
414 info
->high
.rm_startblock
= -1U;
415 info
->high
.rm_owner
= ULLONG_MAX
;
416 info
->high
.rm_offset
= ULLONG_MAX
;
417 info
->high
.rm_blockcount
= 0;
418 info
->high
.rm_flags
= XFS_RMAP_KEY_FLAGS
| XFS_RMAP_REC_FLAGS
;
419 info
->missing_owner
= XFS_FMR_OWN_FREE
;
421 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
422 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
, &info
->high
);
424 if (keys
[0].fmr_physical
> 0)
427 /* Fabricate an rmap entry for the external log device. */
428 rmap
.rm_startblock
= 0;
429 rmap
.rm_blockcount
= mp
->m_sb
.sb_logblocks
;
430 rmap
.rm_owner
= XFS_RMAP_OWN_LOG
;
434 return xfs_getfsmap_helper(tp
, info
, &rmap
, 0);
438 /* Transform a rtbitmap "record" into a fsmap */
440 xfs_getfsmap_rtdev_rtbitmap_helper(
441 struct xfs_trans
*tp
,
442 struct xfs_rtalloc_rec
*rec
,
445 struct xfs_mount
*mp
= tp
->t_mountp
;
446 struct xfs_getfsmap_info
*info
= priv
;
447 struct xfs_rmap_irec irec
;
448 xfs_daddr_t rec_daddr
;
450 irec
.rm_startblock
= rec
->ar_startext
* mp
->m_sb
.sb_rextsize
;
451 rec_daddr
= XFS_FSB_TO_BB(mp
, irec
.rm_startblock
);
452 irec
.rm_blockcount
= rec
->ar_extcount
* mp
->m_sb
.sb_rextsize
;
453 irec
.rm_owner
= XFS_RMAP_OWN_NULL
; /* "free" */
457 return xfs_getfsmap_helper(tp
, info
, &irec
, rec_daddr
);
460 /* Execute a getfsmap query against the realtime device. */
462 __xfs_getfsmap_rtdev(
463 struct xfs_trans
*tp
,
464 struct xfs_fsmap
*keys
,
465 int (*query_fn
)(struct xfs_trans
*,
466 struct xfs_getfsmap_info
*),
467 struct xfs_getfsmap_info
*info
)
469 struct xfs_mount
*mp
= tp
->t_mountp
;
470 xfs_fsblock_t start_fsb
;
471 xfs_fsblock_t end_fsb
;
475 eofs
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
476 if (keys
[0].fmr_physical
>= eofs
)
478 if (keys
[1].fmr_physical
>= eofs
)
479 keys
[1].fmr_physical
= eofs
- 1;
480 start_fsb
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_physical
);
481 end_fsb
= XFS_BB_TO_FSB(mp
, keys
[1].fmr_physical
);
483 /* Set up search keys */
484 info
->low
.rm_startblock
= start_fsb
;
485 error
= xfs_fsmap_owner_to_rmap(&info
->low
, &keys
[0]);
488 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
489 info
->low
.rm_blockcount
= 0;
490 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
492 info
->high
.rm_startblock
= end_fsb
;
493 error
= xfs_fsmap_owner_to_rmap(&info
->high
, &keys
[1]);
496 info
->high
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[1].fmr_offset
);
497 info
->high
.rm_blockcount
= 0;
498 xfs_getfsmap_set_irec_flags(&info
->high
, &keys
[1]);
500 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
501 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
, &info
->high
);
503 return query_fn(tp
, info
);
506 /* Actually query the realtime bitmap. */
508 xfs_getfsmap_rtdev_rtbitmap_query(
509 struct xfs_trans
*tp
,
510 struct xfs_getfsmap_info
*info
)
512 struct xfs_rtalloc_rec alow
= { 0 };
513 struct xfs_rtalloc_rec ahigh
= { 0 };
516 xfs_ilock(tp
->t_mountp
->m_rbmip
, XFS_ILOCK_SHARED
);
518 alow
.ar_startext
= info
->low
.rm_startblock
;
519 ahigh
.ar_startext
= info
->high
.rm_startblock
;
520 do_div(alow
.ar_startext
, tp
->t_mountp
->m_sb
.sb_rextsize
);
521 if (do_div(ahigh
.ar_startext
, tp
->t_mountp
->m_sb
.sb_rextsize
))
523 error
= xfs_rtalloc_query_range(tp
, &alow
, &ahigh
,
524 xfs_getfsmap_rtdev_rtbitmap_helper
, info
);
528 /* Report any gaps at the end of the rtbitmap */
530 error
= xfs_getfsmap_rtdev_rtbitmap_helper(tp
, &ahigh
, info
);
534 xfs_iunlock(tp
->t_mountp
->m_rbmip
, XFS_ILOCK_SHARED
);
538 /* Execute a getfsmap query against the realtime device rtbitmap. */
540 xfs_getfsmap_rtdev_rtbitmap(
541 struct xfs_trans
*tp
,
542 struct xfs_fsmap
*keys
,
543 struct xfs_getfsmap_info
*info
)
545 info
->missing_owner
= XFS_FMR_OWN_UNKNOWN
;
546 return __xfs_getfsmap_rtdev(tp
, keys
, xfs_getfsmap_rtdev_rtbitmap_query
,
549 #endif /* CONFIG_XFS_RT */
551 /* Execute a getfsmap query against the regular data device. */
553 __xfs_getfsmap_datadev(
554 struct xfs_trans
*tp
,
555 struct xfs_fsmap
*keys
,
556 struct xfs_getfsmap_info
*info
,
557 int (*query_fn
)(struct xfs_trans
*,
558 struct xfs_getfsmap_info
*,
559 struct xfs_btree_cur
**,
563 struct xfs_mount
*mp
= tp
->t_mountp
;
564 struct xfs_btree_cur
*bt_cur
= NULL
;
565 xfs_fsblock_t start_fsb
;
566 xfs_fsblock_t end_fsb
;
567 xfs_agnumber_t start_ag
;
568 xfs_agnumber_t end_ag
;
572 eofs
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
);
573 if (keys
[0].fmr_physical
>= eofs
)
575 if (keys
[1].fmr_physical
>= eofs
)
576 keys
[1].fmr_physical
= eofs
- 1;
577 start_fsb
= XFS_DADDR_TO_FSB(mp
, keys
[0].fmr_physical
);
578 end_fsb
= XFS_DADDR_TO_FSB(mp
, keys
[1].fmr_physical
);
581 * Convert the fsmap low/high keys to AG based keys. Initialize
582 * low to the fsmap low key and max out the high key to the end
585 info
->low
.rm_startblock
= XFS_FSB_TO_AGBNO(mp
, start_fsb
);
586 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
587 error
= xfs_fsmap_owner_to_rmap(&info
->low
, &keys
[0]);
590 info
->low
.rm_blockcount
= 0;
591 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
593 info
->high
.rm_startblock
= -1U;
594 info
->high
.rm_owner
= ULLONG_MAX
;
595 info
->high
.rm_offset
= ULLONG_MAX
;
596 info
->high
.rm_blockcount
= 0;
597 info
->high
.rm_flags
= XFS_RMAP_KEY_FLAGS
| XFS_RMAP_REC_FLAGS
;
599 start_ag
= XFS_FSB_TO_AGNO(mp
, start_fsb
);
600 end_ag
= XFS_FSB_TO_AGNO(mp
, end_fsb
);
603 for (info
->agno
= start_ag
; info
->agno
<= end_ag
; info
->agno
++) {
605 * Set the AG high key from the fsmap high key if this
606 * is the last AG that we're querying.
608 if (info
->agno
== end_ag
) {
609 info
->high
.rm_startblock
= XFS_FSB_TO_AGBNO(mp
,
611 info
->high
.rm_offset
= XFS_BB_TO_FSBT(mp
,
613 error
= xfs_fsmap_owner_to_rmap(&info
->high
, &keys
[1]);
616 xfs_getfsmap_set_irec_flags(&info
->high
, &keys
[1]);
620 xfs_btree_del_cursor(bt_cur
, XFS_BTREE_NOERROR
);
622 xfs_trans_brelse(tp
, info
->agf_bp
);
626 error
= xfs_alloc_read_agf(mp
, tp
, info
->agno
, 0,
631 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
632 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
,
635 error
= query_fn(tp
, info
, &bt_cur
, priv
);
640 * Set the AG low key to the start of the AG prior to
641 * moving on to the next AG.
643 if (info
->agno
== start_ag
) {
644 info
->low
.rm_startblock
= 0;
645 info
->low
.rm_owner
= 0;
646 info
->low
.rm_offset
= 0;
647 info
->low
.rm_flags
= 0;
651 /* Report any gap at the end of the AG */
653 error
= query_fn(tp
, info
, &bt_cur
, priv
);
659 xfs_btree_del_cursor(bt_cur
, error
< 0 ? XFS_BTREE_ERROR
:
662 xfs_trans_brelse(tp
, info
->agf_bp
);
669 /* Actually query the rmap btree. */
671 xfs_getfsmap_datadev_rmapbt_query(
672 struct xfs_trans
*tp
,
673 struct xfs_getfsmap_info
*info
,
674 struct xfs_btree_cur
**curpp
,
677 /* Report any gap at the end of the last AG. */
679 return xfs_getfsmap_datadev_helper(*curpp
, &info
->high
, info
);
681 /* Allocate cursor for this AG and query_range it. */
682 *curpp
= xfs_rmapbt_init_cursor(tp
->t_mountp
, tp
, info
->agf_bp
,
684 return xfs_rmap_query_range(*curpp
, &info
->low
, &info
->high
,
685 xfs_getfsmap_datadev_helper
, info
);
688 /* Execute a getfsmap query against the regular data device rmapbt. */
690 xfs_getfsmap_datadev_rmapbt(
691 struct xfs_trans
*tp
,
692 struct xfs_fsmap
*keys
,
693 struct xfs_getfsmap_info
*info
)
695 info
->missing_owner
= XFS_FMR_OWN_FREE
;
696 return __xfs_getfsmap_datadev(tp
, keys
, info
,
697 xfs_getfsmap_datadev_rmapbt_query
, NULL
);
700 /* Actually query the bno btree. */
702 xfs_getfsmap_datadev_bnobt_query(
703 struct xfs_trans
*tp
,
704 struct xfs_getfsmap_info
*info
,
705 struct xfs_btree_cur
**curpp
,
708 struct xfs_alloc_rec_incore
*key
= priv
;
710 /* Report any gap at the end of the last AG. */
712 return xfs_getfsmap_datadev_bnobt_helper(*curpp
, &key
[1], info
);
714 /* Allocate cursor for this AG and query_range it. */
715 *curpp
= xfs_allocbt_init_cursor(tp
->t_mountp
, tp
, info
->agf_bp
,
716 info
->agno
, XFS_BTNUM_BNO
);
717 key
->ar_startblock
= info
->low
.rm_startblock
;
718 key
[1].ar_startblock
= info
->high
.rm_startblock
;
719 return xfs_alloc_query_range(*curpp
, key
, &key
[1],
720 xfs_getfsmap_datadev_bnobt_helper
, info
);
723 /* Execute a getfsmap query against the regular data device's bnobt. */
725 xfs_getfsmap_datadev_bnobt(
726 struct xfs_trans
*tp
,
727 struct xfs_fsmap
*keys
,
728 struct xfs_getfsmap_info
*info
)
730 struct xfs_alloc_rec_incore akeys
[2];
732 info
->missing_owner
= XFS_FMR_OWN_UNKNOWN
;
733 return __xfs_getfsmap_datadev(tp
, keys
, info
,
734 xfs_getfsmap_datadev_bnobt_query
, &akeys
[0]);
737 /* Do we recognize the device? */
739 xfs_getfsmap_is_valid_device(
740 struct xfs_mount
*mp
,
741 struct xfs_fsmap
*fm
)
743 if (fm
->fmr_device
== 0 || fm
->fmr_device
== UINT_MAX
||
744 fm
->fmr_device
== new_encode_dev(mp
->m_ddev_targp
->bt_dev
))
746 if (mp
->m_logdev_targp
&&
747 fm
->fmr_device
== new_encode_dev(mp
->m_logdev_targp
->bt_dev
))
749 if (mp
->m_rtdev_targp
&&
750 fm
->fmr_device
== new_encode_dev(mp
->m_rtdev_targp
->bt_dev
))
755 /* Ensure that the low key is less than the high key. */
757 xfs_getfsmap_check_keys(
758 struct xfs_fsmap
*low_key
,
759 struct xfs_fsmap
*high_key
)
761 if (low_key
->fmr_device
> high_key
->fmr_device
)
763 if (low_key
->fmr_device
< high_key
->fmr_device
)
766 if (low_key
->fmr_physical
> high_key
->fmr_physical
)
768 if (low_key
->fmr_physical
< high_key
->fmr_physical
)
771 if (low_key
->fmr_owner
> high_key
->fmr_owner
)
773 if (low_key
->fmr_owner
< high_key
->fmr_owner
)
776 if (low_key
->fmr_offset
> high_key
->fmr_offset
)
778 if (low_key
->fmr_offset
< high_key
->fmr_offset
)
785 * There are only two devices if we didn't configure RT devices at build time.
788 #define XFS_GETFSMAP_DEVS 3
790 #define XFS_GETFSMAP_DEVS 2
791 #endif /* CONFIG_XFS_RT */
794 * Get filesystem's extents as described in head, and format for
795 * output. Calls formatter to fill the user's buffer until all
796 * extents are mapped, until the passed-in head->fmh_count slots have
797 * been filled, or until the formatter short-circuits the loop, if it
798 * is tracking filled-in extents on its own.
802 * There are multiple levels of keys and counters at work here:
803 * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
804 * these reflect fs-wide sector addrs.
805 * dkeys -- fmh_keys used to query each device;
806 * these are fmh_keys but w/ the low key
807 * bumped up by fmr_length.
808 * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
809 * is how we detect gaps in the fsmap
810 records and report them.
811 * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
812 * dkeys; used to query the metadata.
816 struct xfs_mount
*mp
,
817 struct xfs_fsmap_head
*head
,
818 xfs_fsmap_format_t formatter
,
821 struct xfs_trans
*tp
= NULL
;
822 struct xfs_fsmap dkeys
[2]; /* per-dev keys */
823 struct xfs_getfsmap_dev handlers
[XFS_GETFSMAP_DEVS
];
824 struct xfs_getfsmap_info info
= { NULL
};
829 if (head
->fmh_iflags
& ~FMH_IF_VALID
)
831 if (!xfs_getfsmap_is_valid_device(mp
, &head
->fmh_keys
[0]) ||
832 !xfs_getfsmap_is_valid_device(mp
, &head
->fmh_keys
[1]))
835 use_rmap
= capable(CAP_SYS_ADMIN
) &&
836 xfs_sb_version_hasrmapbt(&mp
->m_sb
);
837 head
->fmh_entries
= 0;
839 /* Set up our device handlers. */
840 memset(handlers
, 0, sizeof(handlers
));
841 handlers
[0].dev
= new_encode_dev(mp
->m_ddev_targp
->bt_dev
);
843 handlers
[0].fn
= xfs_getfsmap_datadev_rmapbt
;
845 handlers
[0].fn
= xfs_getfsmap_datadev_bnobt
;
846 if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
) {
847 handlers
[1].dev
= new_encode_dev(mp
->m_logdev_targp
->bt_dev
);
848 handlers
[1].fn
= xfs_getfsmap_logdev
;
851 if (mp
->m_rtdev_targp
) {
852 handlers
[2].dev
= new_encode_dev(mp
->m_rtdev_targp
->bt_dev
);
853 handlers
[2].fn
= xfs_getfsmap_rtdev_rtbitmap
;
855 #endif /* CONFIG_XFS_RT */
857 xfs_sort(handlers
, XFS_GETFSMAP_DEVS
, sizeof(struct xfs_getfsmap_dev
),
858 xfs_getfsmap_dev_compare
);
861 * To continue where we left off, we allow userspace to use the
862 * last mapping from a previous call as the low key of the next.
863 * This is identified by a non-zero length in the low key. We
864 * have to increment the low key in this scenario to ensure we
865 * don't return the same mapping again, and instead return the
868 * If the low key mapping refers to file data, the same physical
869 * blocks could be mapped to several other files/offsets.
870 * According to rmapbt record ordering, the minimal next
871 * possible record for the block range is the next starting
872 * offset in the same inode. Therefore, bump the file offset to
873 * continue the search appropriately. For all other low key
874 * mapping types (attr blocks, metadata), bump the physical
875 * offset as there can be no other mapping for the same physical
878 dkeys
[0] = head
->fmh_keys
[0];
879 if (dkeys
[0].fmr_flags
& (FMR_OF_SPECIAL_OWNER
| FMR_OF_EXTENT_MAP
)) {
880 dkeys
[0].fmr_physical
+= dkeys
[0].fmr_length
;
881 dkeys
[0].fmr_owner
= 0;
882 if (dkeys
[0].fmr_offset
)
885 dkeys
[0].fmr_offset
+= dkeys
[0].fmr_length
;
886 dkeys
[0].fmr_length
= 0;
887 memset(&dkeys
[1], 0xFF, sizeof(struct xfs_fsmap
));
889 if (!xfs_getfsmap_check_keys(dkeys
, &head
->fmh_keys
[1]))
892 info
.next_daddr
= head
->fmh_keys
[0].fmr_physical
+
893 head
->fmh_keys
[0].fmr_length
;
894 info
.formatter
= formatter
;
895 info
.format_arg
= arg
;
898 /* For each device we support... */
899 for (i
= 0; i
< XFS_GETFSMAP_DEVS
; i
++) {
900 /* Is this device within the range the user asked for? */
903 if (head
->fmh_keys
[0].fmr_device
> handlers
[i
].dev
)
905 if (head
->fmh_keys
[1].fmr_device
< handlers
[i
].dev
)
909 * If this device number matches the high key, we have
910 * to pass the high key to the handler to limit the
911 * query results. If the device number exceeds the
912 * low key, zero out the low key so that we get
913 * everything from the beginning.
915 if (handlers
[i
].dev
== head
->fmh_keys
[1].fmr_device
)
916 dkeys
[1] = head
->fmh_keys
[1];
917 if (handlers
[i
].dev
> head
->fmh_keys
[0].fmr_device
)
918 memset(&dkeys
[0], 0, sizeof(struct xfs_fsmap
));
920 error
= xfs_trans_alloc_empty(mp
, &tp
);
924 info
.dev
= handlers
[i
].dev
;
926 info
.agno
= NULLAGNUMBER
;
927 error
= handlers
[i
].fn(tp
, dkeys
, &info
);
930 xfs_trans_cancel(tp
);
936 xfs_trans_cancel(tp
);
937 head
->fmh_oflags
= FMH_OF_DEV_T
;