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
;
150 return -EFSCORRUPTED
;
155 /* getfsmap query state */
156 struct xfs_getfsmap_info
{
157 struct xfs_fsmap_head
*head
;
158 xfs_fsmap_format_t formatter
; /* formatting fn */
159 void *format_arg
; /* format buffer */
160 struct xfs_buf
*agf_bp
; /* AGF, for refcount queries */
161 xfs_daddr_t next_daddr
; /* next daddr we expect */
162 u64 missing_owner
; /* owner of holes */
163 u32 dev
; /* device id */
164 xfs_agnumber_t agno
; /* AG number, if applicable */
165 struct xfs_rmap_irec low
; /* low rmap key */
166 struct xfs_rmap_irec high
; /* high rmap key */
167 bool last
; /* last extent? */
170 /* Associate a device with a getfsmap handler. */
171 struct xfs_getfsmap_dev
{
173 int (*fn
)(struct xfs_trans
*tp
,
174 struct xfs_fsmap
*keys
,
175 struct xfs_getfsmap_info
*info
);
178 /* Compare two getfsmap device handlers. */
180 xfs_getfsmap_dev_compare(
184 const struct xfs_getfsmap_dev
*d1
= p1
;
185 const struct xfs_getfsmap_dev
*d2
= p2
;
187 return d1
->dev
- d2
->dev
;
190 /* Decide if this mapping is shared. */
192 xfs_getfsmap_is_shared(
193 struct xfs_trans
*tp
,
194 struct xfs_getfsmap_info
*info
,
195 struct xfs_rmap_irec
*rec
,
198 struct xfs_mount
*mp
= tp
->t_mountp
;
199 struct xfs_btree_cur
*cur
;
205 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
207 /* rt files will have agno set to NULLAGNUMBER */
208 if (info
->agno
== NULLAGNUMBER
)
211 /* Are there any shared blocks here? */
213 cur
= xfs_refcountbt_init_cursor(mp
, tp
, info
->agf_bp
,
216 error
= xfs_refcount_find_shared(cur
, rec
->rm_startblock
,
217 rec
->rm_blockcount
, &fbno
, &flen
, false);
219 xfs_btree_del_cursor(cur
, error
);
228 * Format a reverse mapping for getfsmap, having translated rm_startblock
229 * into the appropriate daddr units.
233 struct xfs_trans
*tp
,
234 struct xfs_getfsmap_info
*info
,
235 struct xfs_rmap_irec
*rec
,
236 xfs_daddr_t rec_daddr
)
238 struct xfs_fsmap fmr
;
239 struct xfs_mount
*mp
= tp
->t_mountp
;
243 if (fatal_signal_pending(current
))
247 * Filter out records that start before our startpoint, if the
248 * caller requested that.
250 if (xfs_rmap_compare(rec
, &info
->low
) < 0) {
251 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
252 if (info
->next_daddr
< rec_daddr
)
253 info
->next_daddr
= rec_daddr
;
257 /* Are we just counting mappings? */
258 if (info
->head
->fmh_count
== 0) {
259 if (rec_daddr
> info
->next_daddr
)
260 info
->head
->fmh_entries
++;
265 info
->head
->fmh_entries
++;
267 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
268 if (info
->next_daddr
< rec_daddr
)
269 info
->next_daddr
= rec_daddr
;
274 * If the record starts past the last physical block we saw,
275 * then we've found a gap. Report the gap as being owned by
276 * whatever the caller specified is the missing owner.
278 if (rec_daddr
> info
->next_daddr
) {
279 if (info
->head
->fmh_entries
>= info
->head
->fmh_count
)
282 fmr
.fmr_device
= info
->dev
;
283 fmr
.fmr_physical
= info
->next_daddr
;
284 fmr
.fmr_owner
= info
->missing_owner
;
286 fmr
.fmr_length
= rec_daddr
- info
->next_daddr
;
287 fmr
.fmr_flags
= FMR_OF_SPECIAL_OWNER
;
288 error
= info
->formatter(&fmr
, info
->format_arg
);
291 info
->head
->fmh_entries
++;
297 /* Fill out the extent we found */
298 if (info
->head
->fmh_entries
>= info
->head
->fmh_count
)
301 trace_xfs_fsmap_mapping(mp
, info
->dev
, info
->agno
, rec
);
303 fmr
.fmr_device
= info
->dev
;
304 fmr
.fmr_physical
= rec_daddr
;
305 error
= xfs_fsmap_owner_from_rmap(&fmr
, rec
);
308 fmr
.fmr_offset
= XFS_FSB_TO_BB(mp
, rec
->rm_offset
);
309 fmr
.fmr_length
= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
310 if (rec
->rm_flags
& XFS_RMAP_UNWRITTEN
)
311 fmr
.fmr_flags
|= FMR_OF_PREALLOC
;
312 if (rec
->rm_flags
& XFS_RMAP_ATTR_FORK
)
313 fmr
.fmr_flags
|= FMR_OF_ATTR_FORK
;
314 if (rec
->rm_flags
& XFS_RMAP_BMBT_BLOCK
)
315 fmr
.fmr_flags
|= FMR_OF_EXTENT_MAP
;
316 if (fmr
.fmr_flags
== 0) {
317 error
= xfs_getfsmap_is_shared(tp
, info
, rec
, &shared
);
321 fmr
.fmr_flags
|= FMR_OF_SHARED
;
323 error
= info
->formatter(&fmr
, info
->format_arg
);
326 info
->head
->fmh_entries
++;
329 rec_daddr
+= XFS_FSB_TO_BB(mp
, rec
->rm_blockcount
);
330 if (info
->next_daddr
< rec_daddr
)
331 info
->next_daddr
= rec_daddr
;
335 /* Transform a rmapbt irec into a fsmap */
337 xfs_getfsmap_datadev_helper(
338 struct xfs_btree_cur
*cur
,
339 struct xfs_rmap_irec
*rec
,
342 struct xfs_mount
*mp
= cur
->bc_mp
;
343 struct xfs_getfsmap_info
*info
= priv
;
345 xfs_daddr_t rec_daddr
;
347 fsb
= XFS_AGB_TO_FSB(mp
, cur
->bc_ag
.agno
, rec
->rm_startblock
);
348 rec_daddr
= XFS_FSB_TO_DADDR(mp
, fsb
);
350 return xfs_getfsmap_helper(cur
->bc_tp
, info
, rec
, rec_daddr
);
353 /* Transform a bnobt irec into a fsmap */
355 xfs_getfsmap_datadev_bnobt_helper(
356 struct xfs_btree_cur
*cur
,
357 struct xfs_alloc_rec_incore
*rec
,
360 struct xfs_mount
*mp
= cur
->bc_mp
;
361 struct xfs_getfsmap_info
*info
= priv
;
362 struct xfs_rmap_irec irec
;
363 xfs_daddr_t rec_daddr
;
365 rec_daddr
= XFS_AGB_TO_DADDR(mp
, cur
->bc_ag
.agno
,
368 irec
.rm_startblock
= rec
->ar_startblock
;
369 irec
.rm_blockcount
= rec
->ar_blockcount
;
370 irec
.rm_owner
= XFS_RMAP_OWN_NULL
; /* "free" */
374 return xfs_getfsmap_helper(cur
->bc_tp
, info
, &irec
, rec_daddr
);
377 /* Set rmap flags based on the getfsmap flags */
379 xfs_getfsmap_set_irec_flags(
380 struct xfs_rmap_irec
*irec
,
381 struct xfs_fsmap
*fmr
)
384 if (fmr
->fmr_flags
& FMR_OF_ATTR_FORK
)
385 irec
->rm_flags
|= XFS_RMAP_ATTR_FORK
;
386 if (fmr
->fmr_flags
& FMR_OF_EXTENT_MAP
)
387 irec
->rm_flags
|= XFS_RMAP_BMBT_BLOCK
;
388 if (fmr
->fmr_flags
& FMR_OF_PREALLOC
)
389 irec
->rm_flags
|= XFS_RMAP_UNWRITTEN
;
392 /* Execute a getfsmap query against the log device. */
395 struct xfs_trans
*tp
,
396 struct xfs_fsmap
*keys
,
397 struct xfs_getfsmap_info
*info
)
399 struct xfs_mount
*mp
= tp
->t_mountp
;
400 struct xfs_rmap_irec rmap
;
403 /* Set up search keys */
404 info
->low
.rm_startblock
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_physical
);
405 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
406 error
= xfs_fsmap_owner_to_rmap(&info
->low
, keys
);
409 info
->low
.rm_blockcount
= 0;
410 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
412 error
= xfs_fsmap_owner_to_rmap(&info
->high
, keys
+ 1);
415 info
->high
.rm_startblock
= -1U;
416 info
->high
.rm_owner
= ULLONG_MAX
;
417 info
->high
.rm_offset
= ULLONG_MAX
;
418 info
->high
.rm_blockcount
= 0;
419 info
->high
.rm_flags
= XFS_RMAP_KEY_FLAGS
| XFS_RMAP_REC_FLAGS
;
420 info
->missing_owner
= XFS_FMR_OWN_FREE
;
422 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
423 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
, &info
->high
);
425 if (keys
[0].fmr_physical
> 0)
428 /* Fabricate an rmap entry for the external log device. */
429 rmap
.rm_startblock
= 0;
430 rmap
.rm_blockcount
= mp
->m_sb
.sb_logblocks
;
431 rmap
.rm_owner
= XFS_RMAP_OWN_LOG
;
435 return xfs_getfsmap_helper(tp
, info
, &rmap
, 0);
439 /* Transform a rtbitmap "record" into a fsmap */
441 xfs_getfsmap_rtdev_rtbitmap_helper(
442 struct xfs_trans
*tp
,
443 struct xfs_rtalloc_rec
*rec
,
446 struct xfs_mount
*mp
= tp
->t_mountp
;
447 struct xfs_getfsmap_info
*info
= priv
;
448 struct xfs_rmap_irec irec
;
449 xfs_daddr_t rec_daddr
;
451 irec
.rm_startblock
= rec
->ar_startext
* mp
->m_sb
.sb_rextsize
;
452 rec_daddr
= XFS_FSB_TO_BB(mp
, irec
.rm_startblock
);
453 irec
.rm_blockcount
= rec
->ar_extcount
* mp
->m_sb
.sb_rextsize
;
454 irec
.rm_owner
= XFS_RMAP_OWN_NULL
; /* "free" */
458 return xfs_getfsmap_helper(tp
, info
, &irec
, rec_daddr
);
461 /* Execute a getfsmap query against the realtime device. */
463 __xfs_getfsmap_rtdev(
464 struct xfs_trans
*tp
,
465 struct xfs_fsmap
*keys
,
466 int (*query_fn
)(struct xfs_trans
*,
467 struct xfs_getfsmap_info
*),
468 struct xfs_getfsmap_info
*info
)
470 struct xfs_mount
*mp
= tp
->t_mountp
;
471 xfs_fsblock_t start_fsb
;
472 xfs_fsblock_t end_fsb
;
476 eofs
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
477 if (keys
[0].fmr_physical
>= eofs
)
479 if (keys
[1].fmr_physical
>= eofs
)
480 keys
[1].fmr_physical
= eofs
- 1;
481 start_fsb
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_physical
);
482 end_fsb
= XFS_BB_TO_FSB(mp
, keys
[1].fmr_physical
);
484 /* Set up search keys */
485 info
->low
.rm_startblock
= start_fsb
;
486 error
= xfs_fsmap_owner_to_rmap(&info
->low
, &keys
[0]);
489 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
490 info
->low
.rm_blockcount
= 0;
491 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
493 info
->high
.rm_startblock
= end_fsb
;
494 error
= xfs_fsmap_owner_to_rmap(&info
->high
, &keys
[1]);
497 info
->high
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[1].fmr_offset
);
498 info
->high
.rm_blockcount
= 0;
499 xfs_getfsmap_set_irec_flags(&info
->high
, &keys
[1]);
501 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
502 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
, &info
->high
);
504 return query_fn(tp
, info
);
507 /* Actually query the realtime bitmap. */
509 xfs_getfsmap_rtdev_rtbitmap_query(
510 struct xfs_trans
*tp
,
511 struct xfs_getfsmap_info
*info
)
513 struct xfs_rtalloc_rec alow
= { 0 };
514 struct xfs_rtalloc_rec ahigh
= { 0 };
517 xfs_ilock(tp
->t_mountp
->m_rbmip
, XFS_ILOCK_SHARED
);
519 alow
.ar_startext
= info
->low
.rm_startblock
;
520 ahigh
.ar_startext
= info
->high
.rm_startblock
;
521 do_div(alow
.ar_startext
, tp
->t_mountp
->m_sb
.sb_rextsize
);
522 if (do_div(ahigh
.ar_startext
, tp
->t_mountp
->m_sb
.sb_rextsize
))
524 error
= xfs_rtalloc_query_range(tp
, &alow
, &ahigh
,
525 xfs_getfsmap_rtdev_rtbitmap_helper
, info
);
529 /* Report any gaps at the end of the rtbitmap */
531 error
= xfs_getfsmap_rtdev_rtbitmap_helper(tp
, &ahigh
, info
);
535 xfs_iunlock(tp
->t_mountp
->m_rbmip
, XFS_ILOCK_SHARED
);
539 /* Execute a getfsmap query against the realtime device rtbitmap. */
541 xfs_getfsmap_rtdev_rtbitmap(
542 struct xfs_trans
*tp
,
543 struct xfs_fsmap
*keys
,
544 struct xfs_getfsmap_info
*info
)
546 info
->missing_owner
= XFS_FMR_OWN_UNKNOWN
;
547 return __xfs_getfsmap_rtdev(tp
, keys
, xfs_getfsmap_rtdev_rtbitmap_query
,
550 #endif /* CONFIG_XFS_RT */
552 /* Execute a getfsmap query against the regular data device. */
554 __xfs_getfsmap_datadev(
555 struct xfs_trans
*tp
,
556 struct xfs_fsmap
*keys
,
557 struct xfs_getfsmap_info
*info
,
558 int (*query_fn
)(struct xfs_trans
*,
559 struct xfs_getfsmap_info
*,
560 struct xfs_btree_cur
**,
564 struct xfs_mount
*mp
= tp
->t_mountp
;
565 struct xfs_btree_cur
*bt_cur
= NULL
;
566 xfs_fsblock_t start_fsb
;
567 xfs_fsblock_t end_fsb
;
568 xfs_agnumber_t start_ag
;
569 xfs_agnumber_t end_ag
;
573 eofs
= XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
);
574 if (keys
[0].fmr_physical
>= eofs
)
576 if (keys
[1].fmr_physical
>= eofs
)
577 keys
[1].fmr_physical
= eofs
- 1;
578 start_fsb
= XFS_DADDR_TO_FSB(mp
, keys
[0].fmr_physical
);
579 end_fsb
= XFS_DADDR_TO_FSB(mp
, keys
[1].fmr_physical
);
582 * Convert the fsmap low/high keys to AG based keys. Initialize
583 * low to the fsmap low key and max out the high key to the end
586 info
->low
.rm_startblock
= XFS_FSB_TO_AGBNO(mp
, start_fsb
);
587 info
->low
.rm_offset
= XFS_BB_TO_FSBT(mp
, keys
[0].fmr_offset
);
588 error
= xfs_fsmap_owner_to_rmap(&info
->low
, &keys
[0]);
591 info
->low
.rm_blockcount
= 0;
592 xfs_getfsmap_set_irec_flags(&info
->low
, &keys
[0]);
594 info
->high
.rm_startblock
= -1U;
595 info
->high
.rm_owner
= ULLONG_MAX
;
596 info
->high
.rm_offset
= ULLONG_MAX
;
597 info
->high
.rm_blockcount
= 0;
598 info
->high
.rm_flags
= XFS_RMAP_KEY_FLAGS
| XFS_RMAP_REC_FLAGS
;
600 start_ag
= XFS_FSB_TO_AGNO(mp
, start_fsb
);
601 end_ag
= XFS_FSB_TO_AGNO(mp
, end_fsb
);
604 for (info
->agno
= start_ag
; info
->agno
<= end_ag
; info
->agno
++) {
606 * Set the AG high key from the fsmap high key if this
607 * is the last AG that we're querying.
609 if (info
->agno
== end_ag
) {
610 info
->high
.rm_startblock
= XFS_FSB_TO_AGBNO(mp
,
612 info
->high
.rm_offset
= XFS_BB_TO_FSBT(mp
,
614 error
= xfs_fsmap_owner_to_rmap(&info
->high
, &keys
[1]);
617 xfs_getfsmap_set_irec_flags(&info
->high
, &keys
[1]);
621 xfs_btree_del_cursor(bt_cur
, XFS_BTREE_NOERROR
);
623 xfs_trans_brelse(tp
, info
->agf_bp
);
627 error
= xfs_alloc_read_agf(mp
, tp
, info
->agno
, 0,
632 trace_xfs_fsmap_low_key(mp
, info
->dev
, info
->agno
, &info
->low
);
633 trace_xfs_fsmap_high_key(mp
, info
->dev
, info
->agno
,
636 error
= query_fn(tp
, info
, &bt_cur
, priv
);
641 * Set the AG low key to the start of the AG prior to
642 * moving on to the next AG.
644 if (info
->agno
== start_ag
) {
645 info
->low
.rm_startblock
= 0;
646 info
->low
.rm_owner
= 0;
647 info
->low
.rm_offset
= 0;
648 info
->low
.rm_flags
= 0;
652 /* Report any gap at the end of the AG */
654 error
= query_fn(tp
, info
, &bt_cur
, priv
);
660 xfs_btree_del_cursor(bt_cur
, error
< 0 ? XFS_BTREE_ERROR
:
663 xfs_trans_brelse(tp
, info
->agf_bp
);
670 /* Actually query the rmap btree. */
672 xfs_getfsmap_datadev_rmapbt_query(
673 struct xfs_trans
*tp
,
674 struct xfs_getfsmap_info
*info
,
675 struct xfs_btree_cur
**curpp
,
678 /* Report any gap at the end of the last AG. */
680 return xfs_getfsmap_datadev_helper(*curpp
, &info
->high
, info
);
682 /* Allocate cursor for this AG and query_range it. */
683 *curpp
= xfs_rmapbt_init_cursor(tp
->t_mountp
, tp
, info
->agf_bp
,
685 return xfs_rmap_query_range(*curpp
, &info
->low
, &info
->high
,
686 xfs_getfsmap_datadev_helper
, info
);
689 /* Execute a getfsmap query against the regular data device rmapbt. */
691 xfs_getfsmap_datadev_rmapbt(
692 struct xfs_trans
*tp
,
693 struct xfs_fsmap
*keys
,
694 struct xfs_getfsmap_info
*info
)
696 info
->missing_owner
= XFS_FMR_OWN_FREE
;
697 return __xfs_getfsmap_datadev(tp
, keys
, info
,
698 xfs_getfsmap_datadev_rmapbt_query
, NULL
);
701 /* Actually query the bno btree. */
703 xfs_getfsmap_datadev_bnobt_query(
704 struct xfs_trans
*tp
,
705 struct xfs_getfsmap_info
*info
,
706 struct xfs_btree_cur
**curpp
,
709 struct xfs_alloc_rec_incore
*key
= priv
;
711 /* Report any gap at the end of the last AG. */
713 return xfs_getfsmap_datadev_bnobt_helper(*curpp
, &key
[1], info
);
715 /* Allocate cursor for this AG and query_range it. */
716 *curpp
= xfs_allocbt_init_cursor(tp
->t_mountp
, tp
, info
->agf_bp
,
717 info
->agno
, XFS_BTNUM_BNO
);
718 key
->ar_startblock
= info
->low
.rm_startblock
;
719 key
[1].ar_startblock
= info
->high
.rm_startblock
;
720 return xfs_alloc_query_range(*curpp
, key
, &key
[1],
721 xfs_getfsmap_datadev_bnobt_helper
, info
);
724 /* Execute a getfsmap query against the regular data device's bnobt. */
726 xfs_getfsmap_datadev_bnobt(
727 struct xfs_trans
*tp
,
728 struct xfs_fsmap
*keys
,
729 struct xfs_getfsmap_info
*info
)
731 struct xfs_alloc_rec_incore akeys
[2];
733 info
->missing_owner
= XFS_FMR_OWN_UNKNOWN
;
734 return __xfs_getfsmap_datadev(tp
, keys
, info
,
735 xfs_getfsmap_datadev_bnobt_query
, &akeys
[0]);
738 /* Do we recognize the device? */
740 xfs_getfsmap_is_valid_device(
741 struct xfs_mount
*mp
,
742 struct xfs_fsmap
*fm
)
744 if (fm
->fmr_device
== 0 || fm
->fmr_device
== UINT_MAX
||
745 fm
->fmr_device
== new_encode_dev(mp
->m_ddev_targp
->bt_dev
))
747 if (mp
->m_logdev_targp
&&
748 fm
->fmr_device
== new_encode_dev(mp
->m_logdev_targp
->bt_dev
))
750 if (mp
->m_rtdev_targp
&&
751 fm
->fmr_device
== new_encode_dev(mp
->m_rtdev_targp
->bt_dev
))
756 /* Ensure that the low key is less than the high key. */
758 xfs_getfsmap_check_keys(
759 struct xfs_fsmap
*low_key
,
760 struct xfs_fsmap
*high_key
)
762 if (low_key
->fmr_device
> high_key
->fmr_device
)
764 if (low_key
->fmr_device
< high_key
->fmr_device
)
767 if (low_key
->fmr_physical
> high_key
->fmr_physical
)
769 if (low_key
->fmr_physical
< high_key
->fmr_physical
)
772 if (low_key
->fmr_owner
> high_key
->fmr_owner
)
774 if (low_key
->fmr_owner
< high_key
->fmr_owner
)
777 if (low_key
->fmr_offset
> high_key
->fmr_offset
)
779 if (low_key
->fmr_offset
< high_key
->fmr_offset
)
786 * There are only two devices if we didn't configure RT devices at build time.
789 #define XFS_GETFSMAP_DEVS 3
791 #define XFS_GETFSMAP_DEVS 2
792 #endif /* CONFIG_XFS_RT */
795 * Get filesystem's extents as described in head, and format for
796 * output. Calls formatter to fill the user's buffer until all
797 * extents are mapped, until the passed-in head->fmh_count slots have
798 * been filled, or until the formatter short-circuits the loop, if it
799 * is tracking filled-in extents on its own.
803 * There are multiple levels of keys and counters at work here:
804 * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
805 * these reflect fs-wide sector addrs.
806 * dkeys -- fmh_keys used to query each device;
807 * these are fmh_keys but w/ the low key
808 * bumped up by fmr_length.
809 * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
810 * is how we detect gaps in the fsmap
811 records and report them.
812 * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
813 * dkeys; used to query the metadata.
817 struct xfs_mount
*mp
,
818 struct xfs_fsmap_head
*head
,
819 xfs_fsmap_format_t formatter
,
822 struct xfs_trans
*tp
= NULL
;
823 struct xfs_fsmap dkeys
[2]; /* per-dev keys */
824 struct xfs_getfsmap_dev handlers
[XFS_GETFSMAP_DEVS
];
825 struct xfs_getfsmap_info info
= { NULL
};
830 if (head
->fmh_iflags
& ~FMH_IF_VALID
)
832 if (!xfs_getfsmap_is_valid_device(mp
, &head
->fmh_keys
[0]) ||
833 !xfs_getfsmap_is_valid_device(mp
, &head
->fmh_keys
[1]))
836 use_rmap
= capable(CAP_SYS_ADMIN
) &&
837 xfs_sb_version_hasrmapbt(&mp
->m_sb
);
838 head
->fmh_entries
= 0;
840 /* Set up our device handlers. */
841 memset(handlers
, 0, sizeof(handlers
));
842 handlers
[0].dev
= new_encode_dev(mp
->m_ddev_targp
->bt_dev
);
844 handlers
[0].fn
= xfs_getfsmap_datadev_rmapbt
;
846 handlers
[0].fn
= xfs_getfsmap_datadev_bnobt
;
847 if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
) {
848 handlers
[1].dev
= new_encode_dev(mp
->m_logdev_targp
->bt_dev
);
849 handlers
[1].fn
= xfs_getfsmap_logdev
;
852 if (mp
->m_rtdev_targp
) {
853 handlers
[2].dev
= new_encode_dev(mp
->m_rtdev_targp
->bt_dev
);
854 handlers
[2].fn
= xfs_getfsmap_rtdev_rtbitmap
;
856 #endif /* CONFIG_XFS_RT */
858 xfs_sort(handlers
, XFS_GETFSMAP_DEVS
, sizeof(struct xfs_getfsmap_dev
),
859 xfs_getfsmap_dev_compare
);
862 * To continue where we left off, we allow userspace to use the
863 * last mapping from a previous call as the low key of the next.
864 * This is identified by a non-zero length in the low key. We
865 * have to increment the low key in this scenario to ensure we
866 * don't return the same mapping again, and instead return the
869 * If the low key mapping refers to file data, the same physical
870 * blocks could be mapped to several other files/offsets.
871 * According to rmapbt record ordering, the minimal next
872 * possible record for the block range is the next starting
873 * offset in the same inode. Therefore, bump the file offset to
874 * continue the search appropriately. For all other low key
875 * mapping types (attr blocks, metadata), bump the physical
876 * offset as there can be no other mapping for the same physical
879 dkeys
[0] = head
->fmh_keys
[0];
880 if (dkeys
[0].fmr_flags
& (FMR_OF_SPECIAL_OWNER
| FMR_OF_EXTENT_MAP
)) {
881 dkeys
[0].fmr_physical
+= dkeys
[0].fmr_length
;
882 dkeys
[0].fmr_owner
= 0;
883 if (dkeys
[0].fmr_offset
)
886 dkeys
[0].fmr_offset
+= dkeys
[0].fmr_length
;
887 dkeys
[0].fmr_length
= 0;
888 memset(&dkeys
[1], 0xFF, sizeof(struct xfs_fsmap
));
890 if (!xfs_getfsmap_check_keys(dkeys
, &head
->fmh_keys
[1]))
893 info
.next_daddr
= head
->fmh_keys
[0].fmr_physical
+
894 head
->fmh_keys
[0].fmr_length
;
895 info
.formatter
= formatter
;
896 info
.format_arg
= arg
;
900 * If fsmap runs concurrently with a scrub, the freeze can be delayed
901 * indefinitely as we walk the rmapbt and iterate over metadata
902 * buffers. Freeze quiesces the log (which waits for the buffer LRU to
903 * be emptied) and that won't happen while we're reading buffers.
905 sb_start_write(mp
->m_super
);
907 /* For each device we support... */
908 for (i
= 0; i
< XFS_GETFSMAP_DEVS
; i
++) {
909 /* Is this device within the range the user asked for? */
912 if (head
->fmh_keys
[0].fmr_device
> handlers
[i
].dev
)
914 if (head
->fmh_keys
[1].fmr_device
< handlers
[i
].dev
)
918 * If this device number matches the high key, we have
919 * to pass the high key to the handler to limit the
920 * query results. If the device number exceeds the
921 * low key, zero out the low key so that we get
922 * everything from the beginning.
924 if (handlers
[i
].dev
== head
->fmh_keys
[1].fmr_device
)
925 dkeys
[1] = head
->fmh_keys
[1];
926 if (handlers
[i
].dev
> head
->fmh_keys
[0].fmr_device
)
927 memset(&dkeys
[0], 0, sizeof(struct xfs_fsmap
));
929 error
= xfs_trans_alloc_empty(mp
, &tp
);
933 info
.dev
= handlers
[i
].dev
;
935 info
.agno
= NULLAGNUMBER
;
936 error
= handlers
[i
].fn(tp
, dkeys
, &info
);
939 xfs_trans_cancel(tp
);
945 xfs_trans_cancel(tp
);
946 sb_end_write(mp
->m_super
);
947 head
->fmh_oflags
= FMH_OF_DEV_T
;