File: fsmap.c

package info (click to toggle)
xfsprogs 4.9.0+nmu1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 8,012 kB
  • ctags: 10,574
  • sloc: ansic: 110,850; sh: 3,804; makefile: 863; python: 126
file content (169 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2016 Oracle.  All Rights Reserved.
 *
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "libxfs.h"
#include "command.h"
#include "fsmap.h"
#include "output.h"
#include "init.h"

struct fsmap_info {
	unsigned long long	nr;
	xfs_agnumber_t		agno;
};

static int
fsmap_fn(
	struct xfs_btree_cur	*cur,
	struct xfs_rmap_irec	*rec,
	void			*priv)
{
	struct fsmap_info	*info = priv;

	dbprintf(_("%llu: %u/%u len %u owner %lld offset %llu bmbt %d attrfork %d extflag %d\n"),
		info->nr, info->agno, rec->rm_startblock,
		rec->rm_blockcount, rec->rm_owner, rec->rm_offset,
		!!(rec->rm_flags & XFS_RMAP_BMBT_BLOCK),
		!!(rec->rm_flags & XFS_RMAP_ATTR_FORK),
		!!(rec->rm_flags & XFS_RMAP_UNWRITTEN));
	info->nr++;

	return 0;
}

static void
fsmap(
	xfs_fsblock_t		start_fsb,
	xfs_fsblock_t		end_fsb)
{
	struct fsmap_info	info;
	xfs_agnumber_t		start_ag;
	xfs_agnumber_t		end_ag;
	xfs_agnumber_t		agno;
	xfs_daddr_t		eofs;
	struct xfs_rmap_irec	low = {0};
	struct xfs_rmap_irec	high = {0};
	struct xfs_btree_cur	*bt_cur;
	struct xfs_buf		*agbp;
	int			error;

	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
	if (XFS_FSB_TO_DADDR(mp, end_fsb) >= eofs)
		end_fsb = XFS_DADDR_TO_FSB(mp, eofs - 1);

	low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
	high.rm_startblock = -1U;
	high.rm_owner = ULLONG_MAX;
	high.rm_offset = ULLONG_MAX;
	high.rm_flags = XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK | XFS_RMAP_UNWRITTEN;

	start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
	end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);

	info.nr = 0;
	for (agno = start_ag; agno <= end_ag; agno++) {
		if (agno == end_ag)
			high.rm_startblock = XFS_FSB_TO_AGBNO(mp, end_fsb);

		error = -libxfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
		if (error) {
			dbprintf(_("Error %d while reading AGF.\n"), error);
			return;
		}

		bt_cur = libxfs_rmapbt_init_cursor(mp, NULL, agbp, agno);
		if (!bt_cur) {
			libxfs_putbuf(agbp);
			dbprintf(_("Not enough memory.\n"));
			return;
		}

		info.agno = agno;
		error = -libxfs_rmap_query_range(bt_cur, &low, &high,
				fsmap_fn, &info);
		if (error) {
			libxfs_btree_del_cursor(bt_cur, XFS_BTREE_ERROR);
			libxfs_putbuf(agbp);
			dbprintf(_("Error %d while querying fsmap btree.\n"),
				error);
			return;
		}

		libxfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
		libxfs_putbuf(agbp);

		if (agno == start_ag)
			low.rm_startblock = 0;
	}
}

int
fsmap_f(
	int			argc,
	char			**argv)
{
	char			*p;
	int			c;
	xfs_fsblock_t		start_fsb = 0;
	xfs_fsblock_t		end_fsb = NULLFSBLOCK;

	if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) {
		dbprintf(_("Filesystem does not support reverse mapping btree.\n"));
		return 0;
	}

	while ((c = getopt(argc, argv, "")) != EOF) {
		switch (c) {
		default:
			dbprintf(_("Bad option for fsmap command.\n"));
			return 0;
		}
	}

	if (argc > optind) {
		start_fsb = strtoull(argv[optind], &p, 0);
		if (*p != '\0' || start_fsb >= mp->m_sb.sb_dblocks) {
			dbprintf(_("Bad fsmap start_fsb %s.\n"), argv[optind]);
			return 0;
		}
	}

	if (argc > optind + 1) {
		end_fsb = strtoull(argv[optind + 1], &p, 0);
		if (*p != '\0') {
			dbprintf(_("Bad fsmap end_fsb %s.\n"), argv[optind + 1]);
			return 0;
		}
	}

	fsmap(start_fsb, end_fsb);

	return 0;
}

static const cmdinfo_t	fsmap_cmd =
	{ "fsmap", NULL, fsmap_f, 0, 2, 0,
	  N_("[start_fsb] [end_fsb]"),
	  N_("display reverse mapping(s)"), NULL };

void
fsmap_init(void)
{
	add_command(&fsmap_cmd);
}