xref: /illumos-gate/usr/src/cmd/mdb/common/modules/smbfs/smbfs.c (revision cc3780e66ce1eea52e650b27b7dc5ad62d24eec2)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * CDDL HEADER START
34bff34e3Sthurlow  *
44bff34e3Sthurlow  * The contents of this file are subject to the terms of the
54bff34e3Sthurlow  * Common Development and Distribution License (the "License").
64bff34e3Sthurlow  * You may not use this file except in compliance with the License.
74bff34e3Sthurlow  *
84bff34e3Sthurlow  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94bff34e3Sthurlow  * or http://www.opensolaris.org/os/licensing.
104bff34e3Sthurlow  * See the License for the specific language governing permissions
114bff34e3Sthurlow  * and limitations under the License.
124bff34e3Sthurlow  *
134bff34e3Sthurlow  * When distributing Covered Code, include this CDDL HEADER in each
144bff34e3Sthurlow  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154bff34e3Sthurlow  * If applicable, add the following below this CDDL HEADER, with the
164bff34e3Sthurlow  * fields enclosed by brackets "[]" replaced with your own identifying
174bff34e3Sthurlow  * information: Portions Copyright [yyyy] [name of copyright owner]
184bff34e3Sthurlow  *
194bff34e3Sthurlow  * CDDL HEADER END
204bff34e3Sthurlow  */
214bff34e3Sthurlow 
224bff34e3Sthurlow /*
238329232eSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
2402d09e03SGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
254bff34e3Sthurlow  * Use is subject to license terms.
264bff34e3Sthurlow  */
274bff34e3Sthurlow 
284bff34e3Sthurlow #include <sys/types.h>
298329232eSGordon Ross #include <sys/mdb_modapi.h>
308329232eSGordon Ross 
318329232eSGordon Ross #ifdef	_USER
328329232eSGordon Ross #include "../genunix/avl.h"
338329232eSGordon Ross #define	_FAKE_KERNEL
348329232eSGordon Ross #endif
358329232eSGordon Ross 
364bff34e3Sthurlow #include <sys/refstr_impl.h>
374bff34e3Sthurlow #include <sys/vnode.h>
384bff34e3Sthurlow #include <sys/vfs.h>
394bff34e3Sthurlow 
40430b4c46SGordon Ross #include <smbfs/smbfs.h>
41430b4c46SGordon Ross #include <smbfs/smbfs_node.h>
424bff34e3Sthurlow 
434bff34e3Sthurlow #define	OPT_VERBOSE	0x0001	/* Be [-v]erbose in dcmd's */
444bff34e3Sthurlow 
454bff34e3Sthurlow /*
464bff34e3Sthurlow  * This macro lets us easily use both sizeof (typename)
474bff34e3Sthurlow  * and the string-ified typename for the error message.
484bff34e3Sthurlow  */
494bff34e3Sthurlow #define	SMBFS_OBJ_FETCH(obj_addr, obj_type, dest, err) \
504bff34e3Sthurlow 	if (mdb_vread(dest, sizeof (obj_type), ((uintptr_t)obj_addr)) \
514bff34e3Sthurlow 	!= sizeof (obj_type)) { \
524bff34e3Sthurlow 		mdb_warn("error reading "#obj_type" at %p", obj_addr); \
534bff34e3Sthurlow 		return (err); \
544bff34e3Sthurlow 	}
554bff34e3Sthurlow 
564bff34e3Sthurlow /*
574bff34e3Sthurlow  * We need to read in a private copy
584bff34e3Sthurlow  * of every string we want to print out.
594bff34e3Sthurlow  */
604bff34e3Sthurlow void
print_str(uintptr_t addr)614bff34e3Sthurlow print_str(uintptr_t addr)
624bff34e3Sthurlow {
634bff34e3Sthurlow 	char buf[64];
644bff34e3Sthurlow 	int len, mx = sizeof (buf) - 4;
654bff34e3Sthurlow 
664bff34e3Sthurlow 	if ((len = mdb_readstr(buf, sizeof (buf), addr)) <= 0) {
674bff34e3Sthurlow 		mdb_printf(" (%p)", addr);
684bff34e3Sthurlow 	} else {
694bff34e3Sthurlow 		if (len > mx)
704bff34e3Sthurlow 			strcpy(&buf[mx], "...");
714bff34e3Sthurlow 		mdb_printf(" %s", buf);
724bff34e3Sthurlow 	}
734bff34e3Sthurlow }
744bff34e3Sthurlow 
754bff34e3Sthurlow /*
764bff34e3Sthurlow  * Dcmd (and callback function) to print a summary of
774bff34e3Sthurlow  * all "smbfs" entries in the VFS list.
784bff34e3Sthurlow  */
794bff34e3Sthurlow 
804bff34e3Sthurlow typedef struct smbfs_vfs_cbdata {
814bff34e3Sthurlow 	int flags;
824bff34e3Sthurlow 	int printed_header;
834bff34e3Sthurlow 	uintptr_t vfsops;	/* filter by vfs ops pointer */
844bff34e3Sthurlow 	smbmntinfo_t smi;	/* scratch space for smbfs_vfs_cb */
854bff34e3Sthurlow } smbfs_vfs_cbdata_t;
864bff34e3Sthurlow 
874bff34e3Sthurlow int
smbfs_vfs_cb(uintptr_t addr,const void * data,void * arg)884bff34e3Sthurlow smbfs_vfs_cb(uintptr_t addr, const void *data, void *arg)
894bff34e3Sthurlow {
904bff34e3Sthurlow 	const vfs_t *vfs = data;
914bff34e3Sthurlow 	smbfs_vfs_cbdata_t *cbd = arg;
924bff34e3Sthurlow 	uintptr_t ta;
934bff34e3Sthurlow 
944bff34e3Sthurlow 	/* Filter by matching smbfs ops vector. */
954bff34e3Sthurlow 	if (cbd->vfsops && cbd->vfsops != (uintptr_t)vfs->vfs_op) {
964bff34e3Sthurlow 		return (WALK_NEXT);
974bff34e3Sthurlow 	}
984bff34e3Sthurlow 
994bff34e3Sthurlow 	if (cbd->printed_header == 0) {
1004bff34e3Sthurlow 		cbd->printed_header = 1;
1014bff34e3Sthurlow 		mdb_printf("// vfs_t smbmntinfo_t mnt_path\n");
1024bff34e3Sthurlow 	}
1034bff34e3Sthurlow 
1044bff34e3Sthurlow 	mdb_printf(" %-p", addr);	/* vfs_t */
1054bff34e3Sthurlow 	mdb_printf(" %-p", (uintptr_t)vfs->vfs_data);
1064bff34e3Sthurlow 	/*
1074bff34e3Sthurlow 	 * Note: vfs_mntpt is a refstr_t.
1084bff34e3Sthurlow 	 * Advance to string member.
1094bff34e3Sthurlow 	 */
1104bff34e3Sthurlow 	ta = (uintptr_t)vfs->vfs_mntpt;
1114bff34e3Sthurlow 	ta += OFFSETOF(struct refstr, rs_string);
1124bff34e3Sthurlow 	print_str(ta);
1134bff34e3Sthurlow 	mdb_printf("\n");
1144bff34e3Sthurlow 
1154bff34e3Sthurlow 	if (cbd->flags & OPT_VERBOSE) {
1164bff34e3Sthurlow 		mdb_inc_indent(2);
1174bff34e3Sthurlow 		/* Don't fail the walk if this fails. */
1184bff34e3Sthurlow 		if (mdb_vread(&cbd->smi, sizeof (cbd->smi),
1194bff34e3Sthurlow 		    (uintptr_t)vfs->vfs_data) == -1) {
1204bff34e3Sthurlow 			mdb_warn("error reading smbmntinfo_t at %p",
1214bff34e3Sthurlow 			    (uintptr_t)vfs->vfs_data);
1224bff34e3Sthurlow 		} else {
1234bff34e3Sthurlow 			/* Interesting parts of smbmntinfo_t */
1244bff34e3Sthurlow 			mdb_printf("smi_share: %p, smi_root: %p\n",
1254bff34e3Sthurlow 			    cbd->smi.smi_share, cbd->smi.smi_root);
1264bff34e3Sthurlow 		}
1274bff34e3Sthurlow 		mdb_dec_indent(2);
1284bff34e3Sthurlow 	}
1294bff34e3Sthurlow 
1304bff34e3Sthurlow 	return (WALK_NEXT);
1314bff34e3Sthurlow }
1324bff34e3Sthurlow 
1334bff34e3Sthurlow int
smbfs_vfs_dcmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1344bff34e3Sthurlow smbfs_vfs_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1354bff34e3Sthurlow {
1364bff34e3Sthurlow 	smbfs_vfs_cbdata_t *cbd;
1374bff34e3Sthurlow 	vfs_t *vfs;
1384bff34e3Sthurlow 
1394bff34e3Sthurlow 	cbd = mdb_zalloc(sizeof (*cbd),  UM_SLEEP | UM_GC);
1404bff34e3Sthurlow 
1414bff34e3Sthurlow 	/*
1424bff34e3Sthurlow 	 * Get the ops address here, so things work
1434bff34e3Sthurlow 	 * even if the smbfs module is loaded later
1444bff34e3Sthurlow 	 * than this mdb module.
1454bff34e3Sthurlow 	 */
1464bff34e3Sthurlow 	if (mdb_readvar(&cbd->vfsops, "smbfs_vfsops") == -1) {
1474bff34e3Sthurlow 		mdb_warn("failed to find 'smbfs_vfsops'\n");
1484bff34e3Sthurlow 		return (DCMD_ERR);
1494bff34e3Sthurlow 	}
1504bff34e3Sthurlow 
1514bff34e3Sthurlow 	if (mdb_getopts(argc, argv,
1524bff34e3Sthurlow 	    'v', MDB_OPT_SETBITS, OPT_VERBOSE, &cbd->flags,
1534bff34e3Sthurlow 	    NULL) != argc) {
1544bff34e3Sthurlow 		return (DCMD_USAGE);
1554bff34e3Sthurlow 	}
1564bff34e3Sthurlow 
1574bff34e3Sthurlow 	if (!(flags & DCMD_ADDRSPEC)) {
1588329232eSGordon Ross 		if (mdb_walk("vfs", smbfs_vfs_cb, cbd)
1594bff34e3Sthurlow 		    == -1) {
1604bff34e3Sthurlow 			mdb_warn("can't walk smbfs vfs");
1614bff34e3Sthurlow 			return (DCMD_ERR);
1624bff34e3Sthurlow 		}
1634bff34e3Sthurlow 		return (DCMD_OK);
1644bff34e3Sthurlow 	}
1654bff34e3Sthurlow 
1664bff34e3Sthurlow 	vfs = mdb_alloc(sizeof (*vfs),  UM_SLEEP | UM_GC);
1674bff34e3Sthurlow 	SMBFS_OBJ_FETCH(addr, vfs_t, vfs, DCMD_ERR);
1684bff34e3Sthurlow 	smbfs_vfs_cb(addr, vfs, cbd);
1694bff34e3Sthurlow 	return (DCMD_OK);
1704bff34e3Sthurlow }
1714bff34e3Sthurlow 
1724bff34e3Sthurlow void
smbfs_vfs_help(void)1734bff34e3Sthurlow smbfs_vfs_help(void)
1744bff34e3Sthurlow {
1754bff34e3Sthurlow 	mdb_printf(
1764bff34e3Sthurlow 	    "Display addresses of the mounted smbfs structures\n"
1774bff34e3Sthurlow 	    "and the pathname of the mountpoint\n"
1784bff34e3Sthurlow 	    "\nOptions:\n"
1794bff34e3Sthurlow 	    "  -v    display details of the smbmntinfo\n");
1804bff34e3Sthurlow }
1814bff34e3Sthurlow 
1824bff34e3Sthurlow /*
1834bff34e3Sthurlow  * Dcmd (and callback function) to print a summary of
18402d09e03SGordon Ross  * all smbnodes in the node "hash" (cache) AVL tree.
1854bff34e3Sthurlow  */
1864bff34e3Sthurlow 
18702d09e03SGordon Ross typedef struct smbfs_node_cbdata {
1884bff34e3Sthurlow 	int flags;
1894bff34e3Sthurlow 	int printed_header;
19002d09e03SGordon Ross 	vnode_t vn;
19102d09e03SGordon Ross } smbfs_node_cbdata_t;
1924bff34e3Sthurlow 
1934bff34e3Sthurlow int
smbfs_node_cb(uintptr_t addr,const void * data,void * arg)19402d09e03SGordon Ross smbfs_node_cb(uintptr_t addr, const void *data, void *arg)
1954bff34e3Sthurlow {
1964bff34e3Sthurlow 	const smbnode_t *np = data;
19702d09e03SGordon Ross 	smbfs_node_cbdata_t *cbd = arg;
1984bff34e3Sthurlow 
1994bff34e3Sthurlow 	if (cbd->printed_header == 0) {
2004bff34e3Sthurlow 		cbd->printed_header = 1;
20102d09e03SGordon Ross 		mdb_printf("// vnode smbnode rpath\n");
2024bff34e3Sthurlow 	}
2034bff34e3Sthurlow 
2044bff34e3Sthurlow 	mdb_printf(" %-p", (uintptr_t)np->r_vnode);
20502d09e03SGordon Ross 	mdb_printf(" %-p", addr);	/* smbnode */
2064bff34e3Sthurlow 	print_str((uintptr_t)np->n_rpath);
2074bff34e3Sthurlow 	mdb_printf("\n");
2084bff34e3Sthurlow 
2094bff34e3Sthurlow 	if (cbd->flags & OPT_VERBOSE) {
2104bff34e3Sthurlow 		mdb_inc_indent(2);
2114bff34e3Sthurlow 		/* Don't fail the walk if this fails. */
2124bff34e3Sthurlow 		if (mdb_vread(&cbd->vn, sizeof (cbd->vn),
2134bff34e3Sthurlow 		    (uintptr_t)np->r_vnode) == -1) {
2144bff34e3Sthurlow 			mdb_warn("error reading vnode_t at %p",
2154bff34e3Sthurlow 			    (uintptr_t)np->r_vnode);
2164bff34e3Sthurlow 		} else {
2174bff34e3Sthurlow 			/* Interesting parts of vnode_t */
21802d09e03SGordon Ross 			mdb_printf("v_type=%d v_count=%d",
21902d09e03SGordon Ross 			    cbd->vn.v_type, cbd->vn.v_count);
2204bff34e3Sthurlow 			mdb_printf("\n");
2214bff34e3Sthurlow 		}
2224bff34e3Sthurlow 		mdb_dec_indent(2);
2234bff34e3Sthurlow 	}
2244bff34e3Sthurlow 
2254bff34e3Sthurlow 	return (WALK_NEXT);
2264bff34e3Sthurlow }
2274bff34e3Sthurlow 
2284bff34e3Sthurlow int
smbfs_node_dcmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)22902d09e03SGordon Ross smbfs_node_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2304bff34e3Sthurlow {
23102d09e03SGordon Ross 	smbfs_node_cbdata_t *cbd;
2324bff34e3Sthurlow 
2334bff34e3Sthurlow 	cbd = mdb_zalloc(sizeof (*cbd), UM_SLEEP | UM_GC);
2344bff34e3Sthurlow 
2354bff34e3Sthurlow 	if (mdb_getopts(argc, argv,
2364bff34e3Sthurlow 	    'v', MDB_OPT_SETBITS, OPT_VERBOSE, &cbd->flags,
23702d09e03SGordon Ross 	    NULL) != argc) {
2384bff34e3Sthurlow 		return (DCMD_USAGE);
2394bff34e3Sthurlow 	}
2404bff34e3Sthurlow 
2414bff34e3Sthurlow 	if (!(flags & DCMD_ADDRSPEC)) {
24202d09e03SGordon Ross 		mdb_warn("expect an smbmntinfo_t addr");
24302d09e03SGordon Ross 		return (DCMD_USAGE);
24402d09e03SGordon Ross 	}
24502d09e03SGordon Ross 	addr += OFFSETOF(smbmntinfo_t, smi_hash_avl);
24602d09e03SGordon Ross 
2478329232eSGordon Ross 	if (mdb_pwalk("avl", smbfs_node_cb, cbd, addr) == -1) {
24802d09e03SGordon Ross 		mdb_warn("cannot walk smbfs nodes");
2494bff34e3Sthurlow 		return (DCMD_ERR);
2504bff34e3Sthurlow 	}
2514bff34e3Sthurlow 
2524bff34e3Sthurlow 	return (DCMD_OK);
2534bff34e3Sthurlow }
2544bff34e3Sthurlow 
2554bff34e3Sthurlow void
smbfs_node_help(void)25602d09e03SGordon Ross smbfs_node_help(void)
2574bff34e3Sthurlow {
2584bff34e3Sthurlow 	mdb_printf("Options:\n"
2594bff34e3Sthurlow 	    "  -v           be verbose when displaying smbnodes\n");
2604bff34e3Sthurlow }
2614bff34e3Sthurlow 
2624bff34e3Sthurlow static const mdb_dcmd_t dcmds[] = {
26302d09e03SGordon Ross 	{
26402d09e03SGordon Ross 		"smbfs_vfs", "?[-v]",
2654bff34e3Sthurlow 		"show smbfs-mounted vfs structs",
26602d09e03SGordon Ross 		smbfs_vfs_dcmd, smbfs_vfs_help
26702d09e03SGordon Ross 	},
26802d09e03SGordon Ross 	{
26902d09e03SGordon Ross 		"smbfs_node", "?[-v]",
27002d09e03SGordon Ross 		"given an smbmntinfo_t, list smbnodes",
27102d09e03SGordon Ross 		smbfs_node_dcmd, smbfs_node_help
27202d09e03SGordon Ross 	},
2734bff34e3Sthurlow 	{NULL}
2744bff34e3Sthurlow };
2754bff34e3Sthurlow 
2768329232eSGordon Ross #ifdef _USER
2778329232eSGordon Ross /*
2788329232eSGordon Ross  * Sadly, can't just compile ../genunix/vfs.c with this since
2798329232eSGordon Ross  * it has become a catch-all for FS-specific headers etc.
2808329232eSGordon Ross  */
2818329232eSGordon Ross int
vfs_walk_init(mdb_walk_state_t * wsp)2828329232eSGordon Ross vfs_walk_init(mdb_walk_state_t *wsp)
2838329232eSGordon Ross {
284*cc3780e6SGordon Ross 	if (wsp->walk_addr == 0 &&
2858329232eSGordon Ross 	    mdb_readvar(&wsp->walk_addr, "rootvfs") == -1) {
2868329232eSGordon Ross 		mdb_warn("failed to read 'rootvfs'");
2878329232eSGordon Ross 		return (WALK_ERR);
2888329232eSGordon Ross 	}
2898329232eSGordon Ross 
2908329232eSGordon Ross 	wsp->walk_data = (void *)wsp->walk_addr;
2918329232eSGordon Ross 	return (WALK_NEXT);
2928329232eSGordon Ross }
2938329232eSGordon Ross 
2948329232eSGordon Ross int
vfs_walk_step(mdb_walk_state_t * wsp)2958329232eSGordon Ross vfs_walk_step(mdb_walk_state_t *wsp)
2968329232eSGordon Ross {
2978329232eSGordon Ross 	vfs_t vfs;
2988329232eSGordon Ross 	int status;
2998329232eSGordon Ross 
3008329232eSGordon Ross 	if (mdb_vread(&vfs, sizeof (vfs), wsp->walk_addr) == -1) {
3018329232eSGordon Ross 		mdb_warn("failed to read vfs_t at %p", wsp->walk_addr);
3028329232eSGordon Ross 		return (WALK_DONE);
3038329232eSGordon Ross 	}
3048329232eSGordon Ross 
3058329232eSGordon Ross 	status = wsp->walk_callback(wsp->walk_addr, &vfs, wsp->walk_cbdata);
3068329232eSGordon Ross 
3078329232eSGordon Ross 	if (vfs.vfs_next == wsp->walk_data)
3088329232eSGordon Ross 		return (WALK_DONE);
3098329232eSGordon Ross 
3108329232eSGordon Ross 	wsp->walk_addr = (uintptr_t)vfs.vfs_next;
3118329232eSGordon Ross 
3128329232eSGordon Ross 	return (status);
3138329232eSGordon Ross }
3148329232eSGordon Ross #endif	// _USER
3158329232eSGordon Ross 
3164bff34e3Sthurlow static const mdb_walker_t walkers[] = {
3178329232eSGordon Ross #ifdef	_USER
3188329232eSGordon Ross 	/* from avl.c */
3198329232eSGordon Ross 	{ AVL_WALK_NAME, AVL_WALK_DESC,
3208329232eSGordon Ross 		avl_walk_init, avl_walk_step, avl_walk_fini },
3218329232eSGordon Ross 	/* from vfs.c */
3228329232eSGordon Ross 	{ "vfs", "walk file system list",
3238329232eSGordon Ross 		vfs_walk_init, vfs_walk_step },
3248329232eSGordon Ross #endif	// _USER
3254bff34e3Sthurlow 	{NULL}
3264bff34e3Sthurlow };
3274bff34e3Sthurlow 
3288329232eSGordon Ross 
3294bff34e3Sthurlow static const mdb_modinfo_t modinfo = {
3304bff34e3Sthurlow 	MDB_API_VERSION,
3314bff34e3Sthurlow 	dcmds,
3324bff34e3Sthurlow 	walkers
3334bff34e3Sthurlow };
3344bff34e3Sthurlow 
3354bff34e3Sthurlow const mdb_modinfo_t *
_mdb_init(void)3364bff34e3Sthurlow _mdb_init(void)
3374bff34e3Sthurlow {
3384bff34e3Sthurlow 	return (&modinfo);
3394bff34e3Sthurlow }
340