xref: /titanic_51/usr/src/cmd/mdb/common/modules/specfs/specfs.c (revision e099bf07784b9aadc4cc8655e69d462397e99860)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*e099bf07Scth  * Common Development and Distribution License (the "License").
6*e099bf07Scth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e099bf07Scth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
337c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate typedef struct snode_walk_data {
367c478bd9Sstevel@tonic-gate 	int sw_stablesz;
377c478bd9Sstevel@tonic-gate 	uintptr_t sw_stable;
387c478bd9Sstevel@tonic-gate } snode_walk_data_t;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int
417c478bd9Sstevel@tonic-gate snode_walk_init(mdb_walk_state_t *wsp)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	int stablesz;
447c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
457c478bd9Sstevel@tonic-gate 	uintptr_t stable;
467c478bd9Sstevel@tonic-gate 	uintptr_t sp;
477c478bd9Sstevel@tonic-gate 	snode_walk_data_t *sw;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&stablesz, "stablesz") == -1) {
507c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'stablesz'");
517c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (stablesz == 0)
557c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("stable", &sym) == -1) {
587c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'stable'");
597c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	stable = (uintptr_t)sym.st_value;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (mdb_vread(&sp, sizeof (sp), stable) == -1) {
657c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read stable entry at %p", stable);
667c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	sw = mdb_alloc(sizeof (snode_walk_data_t), UM_SLEEP);
707c478bd9Sstevel@tonic-gate 	sw->sw_stablesz = stablesz;
717c478bd9Sstevel@tonic-gate 	sw->sw_stable = stable;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	wsp->walk_addr = sp;
747c478bd9Sstevel@tonic-gate 	wsp->walk_data = sw;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
807c478bd9Sstevel@tonic-gate snode_walk_step(mdb_walk_state_t *wsp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
837c478bd9Sstevel@tonic-gate 	snode_walk_data_t *sw = wsp->walk_data;
847c478bd9Sstevel@tonic-gate 	struct snode *sp;
857c478bd9Sstevel@tonic-gate 	struct snode snode;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	while (addr == NULL) {
887c478bd9Sstevel@tonic-gate 		if (--sw->sw_stablesz == 0)
897c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		sw->sw_stable += sizeof (struct snode *);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		if (mdb_vread(&sp, sizeof (sp), sw->sw_stable) == -1) {
947c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read stable entry at %p",
957c478bd9Sstevel@tonic-gate 			    sw->sw_stable);
967c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 		addr = (uintptr_t)sp;
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
1027c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read snode at %p", addr);
1037c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)snode.s_next;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &snode, wsp->walk_cbdata));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate void
1127c478bd9Sstevel@tonic-gate snode_walk_fini(mdb_walk_state_t *wsp)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (snode_walk_data_t));
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate typedef struct snode_cbdata {
1187c478bd9Sstevel@tonic-gate 	int sd_major;
1197c478bd9Sstevel@tonic-gate 	int sd_minor;
1207c478bd9Sstevel@tonic-gate 	int sd_verbose;
1217c478bd9Sstevel@tonic-gate } snode_cbdata_t;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static int
1247c478bd9Sstevel@tonic-gate snode_cb(uintptr_t addr, const struct snode *snode, snode_cbdata_t *sd)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	static const mdb_bitmask_t s_flag_masks[] = {
1277c478bd9Sstevel@tonic-gate 		{ "UPD",	SUPD,		SUPD		},
1287c478bd9Sstevel@tonic-gate 		{ "ACC",	SACC,		SACC		},
1297c478bd9Sstevel@tonic-gate 		{ "CHG",	SCHG,		SCHG		},
1307c478bd9Sstevel@tonic-gate 		{ "PRIV",	SPRIV,		SPRIV		},
1317c478bd9Sstevel@tonic-gate 		{ "LOFFSET",	SLOFFSET,	SLOFFSET	},
1327c478bd9Sstevel@tonic-gate 		{ "LOCKED",	SLOCKED,	SLOCKED		},
1337c478bd9Sstevel@tonic-gate 		{ "WANT",	SWANT,		SWANT		},
1347c478bd9Sstevel@tonic-gate 		{ "CLONE",	SCLONE,		SCLONE		},
1357c478bd9Sstevel@tonic-gate 		{ "NEEDCLOSE",	SNEEDCLOSE,	SNEEDCLOSE	},
1367c478bd9Sstevel@tonic-gate 		{ "DIPSET",	SDIPSET,	SDIPSET		},
1377c478bd9Sstevel@tonic-gate 		{ "SIZEVALID",	SSIZEVALID,	SSIZEVALID	},
138*e099bf07Scth 		{ "MUXED",	SMUXED,		SMUXED		},
139*e099bf07Scth 		{ "SELFCLONE",	SSELFCLONE,	SSELFCLONE	},
140*e099bf07Scth 		{ "NOFLUSH",	SNOFLUSH,	SNOFLUSH	},
141*e099bf07Scth 		{ "CLOSING",	SCLOSING,	SCLOSING	},
1427c478bd9Sstevel@tonic-gate 		{ NULL,		0,		0		}
1437c478bd9Sstevel@tonic-gate 	};
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	int major = getmajor(snode->s_dev);
1467c478bd9Sstevel@tonic-gate 	int minor = getminor(snode->s_dev);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (sd->sd_major != -1 && sd->sd_major != major)
1497c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (sd->sd_minor != -1 && sd->sd_minor != minor)
1527c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (sd->sd_verbose) {
1557c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p %?p %6d %16lx <%b>\n",
1567c478bd9Sstevel@tonic-gate 		    addr, snode->s_vnode, snode->s_count, snode->s_dev,
1577c478bd9Sstevel@tonic-gate 		    snode->s_flag, s_flag_masks);
1587c478bd9Sstevel@tonic-gate 	} else {
1597c478bd9Sstevel@tonic-gate 		mdb_printf("%p\n", addr);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1667c478bd9Sstevel@tonic-gate int
1677c478bd9Sstevel@tonic-gate snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	snode_cbdata_t sd;
1707c478bd9Sstevel@tonic-gate 	struct snode snode;
1717c478bd9Sstevel@tonic-gate 	uintptr_t major = 0, dev = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	sd.sd_major = -1;
1747c478bd9Sstevel@tonic-gate 	sd.sd_minor = -1;
1757c478bd9Sstevel@tonic-gate 	sd.sd_verbose = !(flags & DCMD_PIPE_OUT);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
1787c478bd9Sstevel@tonic-gate 	    'm', MDB_OPT_UINTPTR, &major,
1797c478bd9Sstevel@tonic-gate 	    'd', MDB_OPT_UINTPTR, &dev, NULL) != argc)
1807c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (dev != 0) {
1837c478bd9Sstevel@tonic-gate 		sd.sd_major = getmajor(dev);
1847c478bd9Sstevel@tonic-gate 		sd.sd_minor = getminor(dev);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (major != 0)
1887c478bd9Sstevel@tonic-gate 		sd.sd_major = major;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
1917c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %6s %16s %-15s%</u>\n",
1927c478bd9Sstevel@tonic-gate 		    "ADDR", "VNODE", "COUNT", "DEV", "FLAG");
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
1967c478bd9Sstevel@tonic-gate 		if (mdb_walk("snode", (mdb_walk_cb_t)snode_cb, &sd) == -1) {
1977c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk snodes");
1987c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
2047c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read snode structure at %p", addr);
2057c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	snode_cb(addr, &snode, &sd);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
2147c478bd9Sstevel@tonic-gate int
2157c478bd9Sstevel@tonic-gate major2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	snode_cbdata_t sd;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2207c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	sd.sd_major = addr;
2237c478bd9Sstevel@tonic-gate 	sd.sd_minor = -1;
2247c478bd9Sstevel@tonic-gate 	sd.sd_verbose = 0;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
2277c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
2337c478bd9Sstevel@tonic-gate int
2347c478bd9Sstevel@tonic-gate dev2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	snode_cbdata_t sd;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2397c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	sd.sd_major = getmajor(addr);
2427c478bd9Sstevel@tonic-gate 	sd.sd_minor = getminor(addr);
2437c478bd9Sstevel@tonic-gate 	sd.sd_verbose = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
2467c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate void
2527c478bd9Sstevel@tonic-gate snode_help(void)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	mdb_printf("Options:\n"
2557c478bd9Sstevel@tonic-gate 	    "   -d device  filter snodes of the specified dev_t\n"
2567c478bd9Sstevel@tonic-gate 	    "   -m major   filter snodes of the specified major number\n");
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * MDB module linkage
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
2637c478bd9Sstevel@tonic-gate 	{ "dev2snode", ":", "given a dev_t, return the snode", dev2snode },
2647c478bd9Sstevel@tonic-gate 	{ "major2snode", ":", "given a major number, return the snode(s)",
2657c478bd9Sstevel@tonic-gate 	    major2snode },
2667c478bd9Sstevel@tonic-gate 	{ "snode", "?[-d device] [-m major]",
2677c478bd9Sstevel@tonic-gate 		"filter and display snode structures", snode, snode_help },
2687c478bd9Sstevel@tonic-gate 	{ NULL }
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
2727c478bd9Sstevel@tonic-gate 	{ "snode", "walk global snode lists",
2737c478bd9Sstevel@tonic-gate 		snode_walk_init, snode_walk_step, snode_walk_fini },
2747c478bd9Sstevel@tonic-gate 	{ NULL }
2757c478bd9Sstevel@tonic-gate };
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
2807c478bd9Sstevel@tonic-gate _mdb_init(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	return (&modinfo);
2837c478bd9Sstevel@tonic-gate }
284