]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
86bb49e4 DW |
2 | /* |
3 | * Copyright (C) 2016 Oracle. All Rights Reserved. | |
86bb49e4 | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
86bb49e4 DW |
5 | */ |
6 | #include "libxfs.h" | |
7 | #include "command.h" | |
8 | #include "fsmap.h" | |
9 | #include "output.h" | |
10 | #include "init.h" | |
11 | ||
12 | struct fsmap_info { | |
13 | unsigned long long nr; | |
14 | xfs_agnumber_t agno; | |
15 | }; | |
16 | ||
17 | static int | |
18 | fsmap_fn( | |
19 | struct xfs_btree_cur *cur, | |
20 | struct xfs_rmap_irec *rec, | |
21 | void *priv) | |
22 | { | |
23 | struct fsmap_info *info = priv; | |
24 | ||
25 | dbprintf(_("%llu: %u/%u len %u owner %lld offset %llu bmbt %d attrfork %d extflag %d\n"), | |
26 | info->nr, info->agno, rec->rm_startblock, | |
27 | rec->rm_blockcount, rec->rm_owner, rec->rm_offset, | |
28 | !!(rec->rm_flags & XFS_RMAP_BMBT_BLOCK), | |
29 | !!(rec->rm_flags & XFS_RMAP_ATTR_FORK), | |
30 | !!(rec->rm_flags & XFS_RMAP_UNWRITTEN)); | |
31 | info->nr++; | |
32 | ||
33 | return 0; | |
34 | } | |
35 | ||
36 | static void | |
37 | fsmap( | |
38 | xfs_fsblock_t start_fsb, | |
39 | xfs_fsblock_t end_fsb) | |
40 | { | |
41 | struct fsmap_info info; | |
42 | xfs_agnumber_t start_ag; | |
43 | xfs_agnumber_t end_ag; | |
44 | xfs_agnumber_t agno; | |
45 | xfs_daddr_t eofs; | |
138ce9ff DW |
46 | struct xfs_rmap_irec low = {0}; |
47 | struct xfs_rmap_irec high = {0}; | |
86bb49e4 DW |
48 | struct xfs_btree_cur *bt_cur; |
49 | struct xfs_buf *agbp; | |
50 | int error; | |
51 | ||
52 | eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); | |
53 | if (XFS_FSB_TO_DADDR(mp, end_fsb) >= eofs) | |
54 | end_fsb = XFS_DADDR_TO_FSB(mp, eofs - 1); | |
55 | ||
56 | low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb); | |
86bb49e4 DW |
57 | high.rm_startblock = -1U; |
58 | high.rm_owner = ULLONG_MAX; | |
59 | high.rm_offset = ULLONG_MAX; | |
60 | high.rm_flags = XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK | XFS_RMAP_UNWRITTEN; | |
61 | ||
62 | start_ag = XFS_FSB_TO_AGNO(mp, start_fsb); | |
63 | end_ag = XFS_FSB_TO_AGNO(mp, end_fsb); | |
64 | ||
65 | info.nr = 0; | |
66 | for (agno = start_ag; agno <= end_ag; agno++) { | |
67 | if (agno == end_ag) | |
68 | high.rm_startblock = XFS_FSB_TO_AGBNO(mp, end_fsb); | |
69 | ||
e2f60652 | 70 | error = -libxfs_alloc_read_agf(mp, NULL, agno, 0, &agbp); |
86bb49e4 DW |
71 | if (error) { |
72 | dbprintf(_("Error %d while reading AGF.\n"), error); | |
73 | return; | |
74 | } | |
75 | ||
e2f60652 | 76 | bt_cur = libxfs_rmapbt_init_cursor(mp, NULL, agbp, agno); |
86bb49e4 DW |
77 | if (!bt_cur) { |
78 | libxfs_putbuf(agbp); | |
79 | dbprintf(_("Not enough memory.\n")); | |
80 | return; | |
81 | } | |
82 | ||
83 | info.agno = agno; | |
84 | error = -libxfs_rmap_query_range(bt_cur, &low, &high, | |
85 | fsmap_fn, &info); | |
86 | if (error) { | |
e2f60652 | 87 | libxfs_btree_del_cursor(bt_cur, XFS_BTREE_ERROR); |
86bb49e4 DW |
88 | libxfs_putbuf(agbp); |
89 | dbprintf(_("Error %d while querying fsmap btree.\n"), | |
90 | error); | |
91 | return; | |
92 | } | |
93 | ||
e2f60652 | 94 | libxfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR); |
86bb49e4 DW |
95 | libxfs_putbuf(agbp); |
96 | ||
97 | if (agno == start_ag) | |
98 | low.rm_startblock = 0; | |
99 | } | |
100 | } | |
101 | ||
00ff2b10 | 102 | static int |
86bb49e4 DW |
103 | fsmap_f( |
104 | int argc, | |
105 | char **argv) | |
106 | { | |
107 | char *p; | |
108 | int c; | |
109 | xfs_fsblock_t start_fsb = 0; | |
110 | xfs_fsblock_t end_fsb = NULLFSBLOCK; | |
111 | ||
112 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) { | |
113 | dbprintf(_("Filesystem does not support reverse mapping btree.\n")); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | while ((c = getopt(argc, argv, "")) != EOF) { | |
118 | switch (c) { | |
119 | default: | |
120 | dbprintf(_("Bad option for fsmap command.\n")); | |
121 | return 0; | |
122 | } | |
123 | } | |
124 | ||
125 | if (argc > optind) { | |
126 | start_fsb = strtoull(argv[optind], &p, 0); | |
127 | if (*p != '\0' || start_fsb >= mp->m_sb.sb_dblocks) { | |
128 | dbprintf(_("Bad fsmap start_fsb %s.\n"), argv[optind]); | |
129 | return 0; | |
130 | } | |
131 | } | |
132 | ||
133 | if (argc > optind + 1) { | |
134 | end_fsb = strtoull(argv[optind + 1], &p, 0); | |
135 | if (*p != '\0') { | |
136 | dbprintf(_("Bad fsmap end_fsb %s.\n"), argv[optind + 1]); | |
137 | return 0; | |
138 | } | |
139 | } | |
140 | ||
141 | fsmap(start_fsb, end_fsb); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static const cmdinfo_t fsmap_cmd = | |
147 | { "fsmap", NULL, fsmap_f, 0, 2, 0, | |
148 | N_("[start_fsb] [end_fsb]"), | |
149 | N_("display reverse mapping(s)"), NULL }; | |
150 | ||
151 | void | |
152 | fsmap_init(void) | |
153 | { | |
154 | add_command(&fsmap_cmd); | |
155 | } |