10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4452Scth * Common Development and Distribution License (the "License").
6*4452Scth * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*4452Scth * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/time.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
330Sstevel@tonic-gate #include <sys/fs/snode.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate typedef struct snode_walk_data {
360Sstevel@tonic-gate int sw_stablesz;
370Sstevel@tonic-gate uintptr_t sw_stable;
380Sstevel@tonic-gate } snode_walk_data_t;
390Sstevel@tonic-gate
400Sstevel@tonic-gate int
snode_walk_init(mdb_walk_state_t * wsp)410Sstevel@tonic-gate snode_walk_init(mdb_walk_state_t *wsp)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate int stablesz;
440Sstevel@tonic-gate GElf_Sym sym;
450Sstevel@tonic-gate uintptr_t stable;
460Sstevel@tonic-gate uintptr_t sp;
470Sstevel@tonic-gate snode_walk_data_t *sw;
480Sstevel@tonic-gate
490Sstevel@tonic-gate if (mdb_readvar(&stablesz, "stablesz") == -1) {
500Sstevel@tonic-gate mdb_warn("failed to read 'stablesz'");
510Sstevel@tonic-gate return (WALK_ERR);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (stablesz == 0)
550Sstevel@tonic-gate return (WALK_DONE);
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (mdb_lookup_by_name("stable", &sym) == -1) {
580Sstevel@tonic-gate mdb_warn("failed to read 'stable'");
590Sstevel@tonic-gate return (WALK_ERR);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate
620Sstevel@tonic-gate stable = (uintptr_t)sym.st_value;
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (mdb_vread(&sp, sizeof (sp), stable) == -1) {
650Sstevel@tonic-gate mdb_warn("failed to read stable entry at %p", stable);
660Sstevel@tonic-gate return (WALK_DONE);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate sw = mdb_alloc(sizeof (snode_walk_data_t), UM_SLEEP);
700Sstevel@tonic-gate sw->sw_stablesz = stablesz;
710Sstevel@tonic-gate sw->sw_stable = stable;
720Sstevel@tonic-gate
730Sstevel@tonic-gate wsp->walk_addr = sp;
740Sstevel@tonic-gate wsp->walk_data = sw;
750Sstevel@tonic-gate
760Sstevel@tonic-gate return (WALK_NEXT);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate int
snode_walk_step(mdb_walk_state_t * wsp)800Sstevel@tonic-gate snode_walk_step(mdb_walk_state_t *wsp)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate uintptr_t addr = wsp->walk_addr;
830Sstevel@tonic-gate snode_walk_data_t *sw = wsp->walk_data;
840Sstevel@tonic-gate struct snode *sp;
850Sstevel@tonic-gate struct snode snode;
860Sstevel@tonic-gate
870Sstevel@tonic-gate while (addr == NULL) {
880Sstevel@tonic-gate if (--sw->sw_stablesz == 0)
890Sstevel@tonic-gate return (WALK_DONE);
900Sstevel@tonic-gate
910Sstevel@tonic-gate sw->sw_stable += sizeof (struct snode *);
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (mdb_vread(&sp, sizeof (sp), sw->sw_stable) == -1) {
940Sstevel@tonic-gate mdb_warn("failed to read stable entry at %p",
950Sstevel@tonic-gate sw->sw_stable);
960Sstevel@tonic-gate return (WALK_DONE);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate addr = (uintptr_t)sp;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
1020Sstevel@tonic-gate mdb_warn("failed to read snode at %p", addr);
1030Sstevel@tonic-gate return (WALK_DONE);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)snode.s_next;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate return (wsp->walk_callback(addr, &snode, wsp->walk_cbdata));
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate void
snode_walk_fini(mdb_walk_state_t * wsp)1120Sstevel@tonic-gate snode_walk_fini(mdb_walk_state_t *wsp)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate mdb_free(wsp->walk_data, sizeof (snode_walk_data_t));
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate typedef struct snode_cbdata {
1180Sstevel@tonic-gate int sd_major;
1190Sstevel@tonic-gate int sd_minor;
1200Sstevel@tonic-gate int sd_verbose;
1210Sstevel@tonic-gate } snode_cbdata_t;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static int
snode_cb(uintptr_t addr,const struct snode * snode,snode_cbdata_t * sd)1240Sstevel@tonic-gate snode_cb(uintptr_t addr, const struct snode *snode, snode_cbdata_t *sd)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate static const mdb_bitmask_t s_flag_masks[] = {
1270Sstevel@tonic-gate { "UPD", SUPD, SUPD },
1280Sstevel@tonic-gate { "ACC", SACC, SACC },
1290Sstevel@tonic-gate { "CHG", SCHG, SCHG },
1300Sstevel@tonic-gate { "PRIV", SPRIV, SPRIV },
1310Sstevel@tonic-gate { "LOFFSET", SLOFFSET, SLOFFSET },
1320Sstevel@tonic-gate { "LOCKED", SLOCKED, SLOCKED },
1330Sstevel@tonic-gate { "WANT", SWANT, SWANT },
1340Sstevel@tonic-gate { "CLONE", SCLONE, SCLONE },
1350Sstevel@tonic-gate { "NEEDCLOSE", SNEEDCLOSE, SNEEDCLOSE },
1360Sstevel@tonic-gate { "DIPSET", SDIPSET, SDIPSET },
1370Sstevel@tonic-gate { "SIZEVALID", SSIZEVALID, SSIZEVALID },
138*4452Scth { "MUXED", SMUXED, SMUXED },
139*4452Scth { "SELFCLONE", SSELFCLONE, SSELFCLONE },
140*4452Scth { "NOFLUSH", SNOFLUSH, SNOFLUSH },
141*4452Scth { "CLOSING", SCLOSING, SCLOSING },
1420Sstevel@tonic-gate { NULL, 0, 0 }
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int major = getmajor(snode->s_dev);
1460Sstevel@tonic-gate int minor = getminor(snode->s_dev);
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (sd->sd_major != -1 && sd->sd_major != major)
1490Sstevel@tonic-gate return (WALK_NEXT);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if (sd->sd_minor != -1 && sd->sd_minor != minor)
1520Sstevel@tonic-gate return (WALK_NEXT);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if (sd->sd_verbose) {
1550Sstevel@tonic-gate mdb_printf("%0?p %?p %6d %16lx <%b>\n",
1560Sstevel@tonic-gate addr, snode->s_vnode, snode->s_count, snode->s_dev,
1570Sstevel@tonic-gate snode->s_flag, s_flag_masks);
1580Sstevel@tonic-gate } else {
1590Sstevel@tonic-gate mdb_printf("%p\n", addr);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate return (WALK_NEXT);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*ARGSUSED*/
1660Sstevel@tonic-gate int
snode(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1670Sstevel@tonic-gate snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate snode_cbdata_t sd;
1700Sstevel@tonic-gate struct snode snode;
1710Sstevel@tonic-gate uintptr_t major = 0, dev = 0;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate sd.sd_major = -1;
1740Sstevel@tonic-gate sd.sd_minor = -1;
1750Sstevel@tonic-gate sd.sd_verbose = !(flags & DCMD_PIPE_OUT);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (mdb_getopts(argc, argv,
1780Sstevel@tonic-gate 'm', MDB_OPT_UINTPTR, &major,
1790Sstevel@tonic-gate 'd', MDB_OPT_UINTPTR, &dev, NULL) != argc)
1800Sstevel@tonic-gate return (DCMD_USAGE);
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (dev != 0) {
1830Sstevel@tonic-gate sd.sd_major = getmajor(dev);
1840Sstevel@tonic-gate sd.sd_minor = getminor(dev);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if (major != 0)
1880Sstevel@tonic-gate sd.sd_major = major;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
1910Sstevel@tonic-gate mdb_printf("%<u>%?s %?s %6s %16s %-15s%</u>\n",
1920Sstevel@tonic-gate "ADDR", "VNODE", "COUNT", "DEV", "FLAG");
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
1960Sstevel@tonic-gate if (mdb_walk("snode", (mdb_walk_cb_t)snode_cb, &sd) == -1) {
1970Sstevel@tonic-gate mdb_warn("can't walk snodes");
1980Sstevel@tonic-gate return (DCMD_ERR);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate return (DCMD_OK);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (mdb_vread(&snode, sizeof (snode), addr) == -1) {
2040Sstevel@tonic-gate mdb_warn("failed to read snode structure at %p", addr);
2050Sstevel@tonic-gate return (DCMD_ERR);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate snode_cb(addr, &snode, &sd);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate return (DCMD_OK);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /*ARGSUSED3*/
2140Sstevel@tonic-gate int
major2snode(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2150Sstevel@tonic-gate major2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate snode_cbdata_t sd;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2200Sstevel@tonic-gate return (DCMD_USAGE);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate sd.sd_major = addr;
2230Sstevel@tonic-gate sd.sd_minor = -1;
2240Sstevel@tonic-gate sd.sd_verbose = 0;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
2270Sstevel@tonic-gate return (DCMD_ERR);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate return (DCMD_OK);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*ARGSUSED3*/
2330Sstevel@tonic-gate int
dev2snode(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2340Sstevel@tonic-gate dev2snode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate snode_cbdata_t sd;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2390Sstevel@tonic-gate return (DCMD_USAGE);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate sd.sd_major = getmajor(addr);
2420Sstevel@tonic-gate sd.sd_minor = getminor(addr);
2430Sstevel@tonic-gate sd.sd_verbose = 0;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (mdb_pwalk("snode", (mdb_walk_cb_t)snode_cb, &sd, 0) != 0)
2460Sstevel@tonic-gate return (DCMD_ERR);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate return (DCMD_OK);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate void
snode_help(void)2520Sstevel@tonic-gate snode_help(void)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate mdb_printf("Options:\n"
2550Sstevel@tonic-gate " -d device filter snodes of the specified dev_t\n"
2560Sstevel@tonic-gate " -m major filter snodes of the specified major number\n");
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * MDB module linkage
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
2630Sstevel@tonic-gate { "dev2snode", ":", "given a dev_t, return the snode", dev2snode },
2640Sstevel@tonic-gate { "major2snode", ":", "given a major number, return the snode(s)",
2650Sstevel@tonic-gate major2snode },
2660Sstevel@tonic-gate { "snode", "?[-d device] [-m major]",
2670Sstevel@tonic-gate "filter and display snode structures", snode, snode_help },
2680Sstevel@tonic-gate { NULL }
2690Sstevel@tonic-gate };
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
2720Sstevel@tonic-gate { "snode", "walk global snode lists",
2730Sstevel@tonic-gate snode_walk_init, snode_walk_step, snode_walk_fini },
2740Sstevel@tonic-gate { NULL }
2750Sstevel@tonic-gate };
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)2800Sstevel@tonic-gate _mdb_init(void)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate return (&modinfo);
2830Sstevel@tonic-gate }
284