xref: /titanic_52/usr/src/cmd/mdb/common/modules/specfs/specfs.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_modapi.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/time.h>
33 #include <sys/vnode.h>
34 #include <sys/fs/snode.h>
35 
36 typedef struct snode_walk_data {
37 	int sw_stablesz;
38 	uintptr_t sw_stable;
39 } snode_walk_data_t;
40 
41 int
42 snode_walk_init(mdb_walk_state_t *wsp)
43 {
44 	int stablesz;
45 	GElf_Sym sym;
46 	uintptr_t stable;
47 	uintptr_t sp;
48 	snode_walk_data_t *sw;
49 
50 	if (mdb_readvar(&stablesz, "stablesz") == -1) {
51 		mdb_warn("failed to read 'stablesz'");
52 		return (WALK_ERR);
53 	}
54 
55 	if (stablesz == 0)
56 		return (WALK_DONE);
57 
58 	if (mdb_lookup_by_name("stable", &sym) == -1) {
59 		mdb_warn("failed to read 'stable'");
60 		return (WALK_ERR);
61 	}
62 
63 	stable = (uintptr_t)sym.st_value;
64 
65 	if (mdb_vread(&sp, sizeof (sp), stable) == -1) {
66 		mdb_warn("failed to read stable entry at %p", stable);
67 		return (WALK_DONE);
68 	}
69 
70 	sw = mdb_alloc(sizeof (snode_walk_data_t), UM_SLEEP);
71 	sw->sw_stablesz = stablesz;
72 	sw->sw_stable = stable;
73 
74 	wsp->walk_addr = sp;
75 	wsp->walk_data = sw;
76 
77 	return (WALK_NEXT);
78 }
79 
80 int
81 snode_walk_step(mdb_walk_state_t *wsp)
82 {
83 	uintptr_t addr = wsp->walk_addr;
84 	snode_walk_data_t *sw = wsp->walk_data;
85 	struct snode *sp;
86 	struct snode snode;
87 
88 	while (addr == NULL) {
89 		if (--sw->sw_stablesz == 0)
90 			return (WALK_DONE);
91 
92 		sw->sw_stable += sizeof (struct snode *);
93 
94 		if (mdb_vread(&sp, sizeof (sp), sw->sw_stable) == -1) {
95 			mdb_warn("failed to read stable entry at %p",
96 			    sw->sw_stable);
97 			return (WALK_DONE);
98 		}
99 		addr = (uintptr_t)sp;
100 	}
101 
102 	if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
103 		mdb_warn("failed to read snode at %p", addr);
104 		return (WALK_DONE);
105 	}
106 
107 	wsp->walk_addr = (uintptr_t)snode.s_next;
108 
109 	return (wsp->walk_callback(addr, &snode, wsp->walk_cbdata));
110 }
111 
112 void
113 snode_walk_fini(mdb_walk_state_t *wsp)
114 {
115 	mdb_free(wsp->walk_data, sizeof (snode_walk_data_t));
116 }
117 
118 typedef struct snode_cbdata {
119 	int sd_major;
120 	int sd_minor;
121 	int sd_verbose;
122 } snode_cbdata_t;
123 
124 static int
125 snode_cb(uintptr_t addr, const struct snode *snode, snode_cbdata_t *sd)
126 {
127 	static const mdb_bitmask_t s_flag_masks[] = {
128 		{ "UPD",	SUPD,		SUPD		},
129 		{ "ACC",	SACC,		SACC		},
130 		{ "CHG",	SCHG,		SCHG		},
131 		{ "PRIV",	SPRIV,		SPRIV		},
132 		{ "LOFFSET",	SLOFFSET,	SLOFFSET	},
133 		{ "LOCKED",	SLOCKED,	SLOCKED		},
134 		{ "WANT",	SWANT,		SWANT		},
135 		{ "CLONE",	SCLONE,		SCLONE		},
136 		{ "NEEDCLOSE",	SNEEDCLOSE,	SNEEDCLOSE	},
137 		{ "DIPSET",	SDIPSET,	SDIPSET		},
138 		{ "SIZEVALID",	SSIZEVALID,	SSIZEVALID	},
139 		{ NULL,		0,		0		}
140 	};
141 
142 	int major = getmajor(snode->s_dev);
143 	int minor = getminor(snode->s_dev);
144 
145 	if (sd->sd_major != -1 && sd->sd_major != major)
146 		return (WALK_NEXT);
147 
148 	if (sd->sd_minor != -1 && sd->sd_minor != minor)
149 		return (WALK_NEXT);
150 
151 	if (sd->sd_verbose) {
152 		mdb_printf("%0?p %?p %6d %16lx <%b>\n",
153 		    addr, snode->s_vnode, snode->s_count, snode->s_dev,
154 		    snode->s_flag, s_flag_masks);
155 	} else {
156 		mdb_printf("%p\n", addr);
157 	}
158 
159 	return (WALK_NEXT);
160 }
161 
162 /*ARGSUSED*/
163 int
164 snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
165 {
166 	snode_cbdata_t sd;
167 	struct snode snode;
168 	uintptr_t major = 0, dev = 0;
169 
170 	sd.sd_major = -1;
171 	sd.sd_minor = -1;
172 	sd.sd_verbose = !(flags & DCMD_PIPE_OUT);
173 
174 	if (mdb_getopts(argc, argv,
175 	    'm', MDB_OPT_UINTPTR, &major,
176 	    'd', MDB_OPT_UINTPTR, &dev, NULL) != argc)
177 		return (DCMD_USAGE);
178 
179 	if (dev != 0) {
180 		sd.sd_major = getmajor(dev);
181 		sd.sd_minor = getminor(dev);
182 	}
183 
184 	if (major != 0)
185 		sd.sd_major = major;
186 
187 	if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
188 		mdb_printf("%<u>%?s %?s %6s %16s %-15s%</u>\n",
189 		    "ADDR", "VNODE", "COUNT", "DEV", "FLAG");
190 	}
191 
192 	if (!(flags & DCMD_ADDRSPEC)) {
193 		if (mdb_walk("snode", (mdb_walk_cb_t)snode_cb, &sd) == -1) {
194 			mdb_warn("can't walk snodes");
195 			return (DCMD_ERR);
196 		}
197 		return (DCMD_OK);
198 	}
199 
200 	if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
201 		mdb_warn("failed to read snode structure at %p", addr);
202 		return (DCMD_ERR);
203 	}
204 
205 	snode_cb(addr, &snode, &sd);
206 
207 	return (DCMD_OK);
208 }
209 
210 /*ARGSUSED3*/
211 int
212 major2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
213 {
214 	snode_cbdata_t sd;
215 
216 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
217 		return (DCMD_USAGE);
218 
219 	sd.sd_major = addr;
220 	sd.sd_minor = -1;
221 	sd.sd_verbose = 0;
222 
223 	if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
224 		return (DCMD_ERR);
225 
226 	return (DCMD_OK);
227 }
228 
229 /*ARGSUSED3*/
230 int
231 dev2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
232 {
233 	snode_cbdata_t sd;
234 
235 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
236 		return (DCMD_USAGE);
237 
238 	sd.sd_major = getmajor(addr);
239 	sd.sd_minor = getminor(addr);
240 	sd.sd_verbose = 0;
241 
242 	if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
243 		return (DCMD_ERR);
244 
245 	return (DCMD_OK);
246 }
247 
248 void
249 snode_help(void)
250 {
251 	mdb_printf("Options:\n"
252 	    "   -d device  filter snodes of the specified dev_t\n"
253 	    "   -m major   filter snodes of the specified major number\n");
254 }
255 
256 /*
257  * MDB module linkage
258  */
259 static const mdb_dcmd_t dcmds[] = {
260 	{ "dev2snode", ":", "given a dev_t, return the snode", dev2snode },
261 	{ "major2snode", ":", "given a major number, return the snode(s)",
262 	    major2snode },
263 	{ "snode", "?[-d device] [-m major]",
264 		"filter and display snode structures", snode, snode_help },
265 	{ NULL }
266 };
267 
268 static const mdb_walker_t walkers[] = {
269 	{ "snode", "walk global snode lists",
270 		snode_walk_init, snode_walk_step, snode_walk_fini },
271 	{ NULL }
272 };
273 
274 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
275 
276 const mdb_modinfo_t *
277 _mdb_init(void)
278 {
279 	return (&modinfo);
280 }
281