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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
297c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_node.h>
307c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate typedef struct lnode_walk {
357c478bd9Sstevel@tonic-gate 	struct lobucket *lw_table;	/* Snapshot of hash table */
367c478bd9Sstevel@tonic-gate 	uint_t lw_tabsz;		/* Size of hash table */
377c478bd9Sstevel@tonic-gate 	uint_t lw_tabi;			/* Current table index */
387c478bd9Sstevel@tonic-gate 	lnode_t *lw_lnode;		/* Current buffer */
397c478bd9Sstevel@tonic-gate } lnode_walk_t;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate int
lnode_walk_init(mdb_walk_state_t * wsp)427c478bd9Sstevel@tonic-gate lnode_walk_init(mdb_walk_state_t *wsp)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	lnode_walk_t *lwp;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	int lofsfstype;
477c478bd9Sstevel@tonic-gate 	struct vfs vfs;
487c478bd9Sstevel@tonic-gate 	struct loinfo loinfo;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&lofsfstype, "lofsfstype") == -1) {
517c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'lofsfstype' symbol\n");
527c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 
55*892ad162SToomas Soome 	if (wsp->walk_addr == 0) {
567c478bd9Sstevel@tonic-gate 		uintptr_t rootvfsp, vfsp;
577c478bd9Sstevel@tonic-gate 		uint_t htsize;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 		lwp = mdb_alloc(sizeof (lnode_walk_t), UM_SLEEP);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate retry:
627c478bd9Sstevel@tonic-gate 		lwp->lw_tabsz = 0;
637c478bd9Sstevel@tonic-gate 		if (mdb_readvar(&rootvfsp, "rootvfs") == -1) {
647c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read 'rootvfs' symbol\n");
657c478bd9Sstevel@tonic-gate 			mdb_free(lwp, sizeof (lnode_walk_t));
667c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
677c478bd9Sstevel@tonic-gate 		}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		vfsp = rootvfsp;
707c478bd9Sstevel@tonic-gate 		do {
717c478bd9Sstevel@tonic-gate 			(void) mdb_vread(&vfs, sizeof (vfs), vfsp);
727c478bd9Sstevel@tonic-gate 			if (lofsfstype != vfs.vfs_fstype) {
737c478bd9Sstevel@tonic-gate 				vfsp = (uintptr_t)vfs.vfs_next;
747c478bd9Sstevel@tonic-gate 				continue;
757c478bd9Sstevel@tonic-gate 			}
767c478bd9Sstevel@tonic-gate 			(void) mdb_vread(&loinfo, sizeof (struct loinfo),
777c478bd9Sstevel@tonic-gate 			    (uintptr_t)vfs.vfs_data);
787c478bd9Sstevel@tonic-gate 			lwp->lw_tabsz += loinfo.li_htsize;
797c478bd9Sstevel@tonic-gate 			vfsp = (uintptr_t)vfs.vfs_next;
807c478bd9Sstevel@tonic-gate 		} while (vfsp != rootvfsp);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		if (lwp->lw_tabsz == 0) {
837c478bd9Sstevel@tonic-gate 			/*
847c478bd9Sstevel@tonic-gate 			 * No lofs filesystems mounted.
857c478bd9Sstevel@tonic-gate 			 */
867c478bd9Sstevel@tonic-gate 			mdb_free(lwp, sizeof (lnode_walk_t));
877c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 		lwp->lw_table = mdb_alloc(lwp->lw_tabsz *
907c478bd9Sstevel@tonic-gate 		    sizeof (struct lobucket), UM_SLEEP);
917c478bd9Sstevel@tonic-gate 		htsize = 0;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		vfsp = rootvfsp;
947c478bd9Sstevel@tonic-gate 		do {
957c478bd9Sstevel@tonic-gate 			(void) mdb_vread(&vfs, sizeof (vfs), vfsp);
967c478bd9Sstevel@tonic-gate 			if (lofsfstype != vfs.vfs_fstype) {
977c478bd9Sstevel@tonic-gate 				vfsp = (uintptr_t)vfs.vfs_next;
987c478bd9Sstevel@tonic-gate 				continue;
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 			(void) mdb_vread(&loinfo, sizeof (struct loinfo),
1017c478bd9Sstevel@tonic-gate 			    (uintptr_t)vfs.vfs_data);
1027c478bd9Sstevel@tonic-gate 			if (htsize + loinfo.li_htsize > lwp->lw_tabsz) {
1037c478bd9Sstevel@tonic-gate 				/*
1047c478bd9Sstevel@tonic-gate 				 * Something must have resized.
1057c478bd9Sstevel@tonic-gate 				 */
1067c478bd9Sstevel@tonic-gate 				mdb_free(lwp->lw_table,
1077c478bd9Sstevel@tonic-gate 				    lwp->lw_tabsz * sizeof (struct lobucket));
1087c478bd9Sstevel@tonic-gate 				goto retry;
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 			(void) mdb_vread(lwp->lw_table + htsize,
1117c478bd9Sstevel@tonic-gate 			    loinfo.li_htsize * sizeof (struct lobucket),
1127c478bd9Sstevel@tonic-gate 			    (uintptr_t)loinfo.li_hashtable);
1137c478bd9Sstevel@tonic-gate 			htsize += loinfo.li_htsize;
1147c478bd9Sstevel@tonic-gate 			vfsp = (uintptr_t)vfs.vfs_next;
1157c478bd9Sstevel@tonic-gate 		} while (vfsp != rootvfsp);
1167c478bd9Sstevel@tonic-gate 	} else {
1177c478bd9Sstevel@tonic-gate 		if (mdb_vread(&vfs, sizeof (vfs_t), wsp->walk_addr) == -1) {
1187c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read from '%p'\n", wsp->walk_addr);
1197c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		if (lofsfstype != vfs.vfs_fstype) {
1227c478bd9Sstevel@tonic-gate 			mdb_warn("%p does not point to a lofs mount vfs\n",
1237c478bd9Sstevel@tonic-gate 			    wsp->walk_addr);
1247c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		if (mdb_vread(&loinfo, sizeof (loinfo),
1277c478bd9Sstevel@tonic-gate 		    (uintptr_t)vfs.vfs_data) == -1) {
1287c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read struct loinfo from '%p'\n",
1297c478bd9Sstevel@tonic-gate 			    vfs.vfs_data);
1307c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		lwp = mdb_alloc(sizeof (lnode_walk_t), UM_SLEEP);
1347c478bd9Sstevel@tonic-gate 		lwp->lw_tabsz = loinfo.li_htsize;
1357c478bd9Sstevel@tonic-gate 		lwp->lw_table = mdb_alloc(lwp->lw_tabsz *
1367c478bd9Sstevel@tonic-gate 		    sizeof (struct lobucket), UM_SLEEP);
1377c478bd9Sstevel@tonic-gate 		(void) mdb_vread(lwp->lw_table,
1387c478bd9Sstevel@tonic-gate 		    lwp->lw_tabsz * sizeof (struct lobucket),
1397c478bd9Sstevel@tonic-gate 		    (uintptr_t)loinfo.li_hashtable);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	lwp->lw_tabi = 0;
1427c478bd9Sstevel@tonic-gate 	lwp->lw_lnode = mdb_alloc(sizeof (lnode_t), UM_SLEEP);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)lwp->lw_table[0].lh_chain;
1457c478bd9Sstevel@tonic-gate 	wsp->walk_data = lwp;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate int
lnode_walk_step(mdb_walk_state_t * wsp)1517c478bd9Sstevel@tonic-gate lnode_walk_step(mdb_walk_state_t *wsp)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	lnode_walk_t *lwp = wsp->walk_data;
1547c478bd9Sstevel@tonic-gate 	uintptr_t addr;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * If the next lnode_t address we want is NULL, advance to the next
1587c478bd9Sstevel@tonic-gate 	 * hash bucket.  When we reach lw_tabsz, we're done.
1597c478bd9Sstevel@tonic-gate 	 */
160*892ad162SToomas Soome 	while (wsp->walk_addr == 0) {
1617c478bd9Sstevel@tonic-gate 		if (++lwp->lw_tabi < lwp->lw_tabsz)
1627c478bd9Sstevel@tonic-gate 			wsp->walk_addr =
1637c478bd9Sstevel@tonic-gate 			    (uintptr_t)lwp->lw_table[lwp->lw_tabi].lh_chain;
1647c478bd9Sstevel@tonic-gate 		else
1657c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * When we have an lnode_t address, read the lnode and invoke the
1707c478bd9Sstevel@tonic-gate 	 * walk callback.  Keep the next lnode_t address in wsp->walk_addr.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	addr = wsp->walk_addr;
1737c478bd9Sstevel@tonic-gate 	(void) mdb_vread(lwp->lw_lnode, sizeof (lnode_t), addr);
1747c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)lwp->lw_lnode->lo_next;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, lwp->lw_lnode, wsp->walk_cbdata));
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate void
lnode_walk_fini(mdb_walk_state_t * wsp)1807c478bd9Sstevel@tonic-gate lnode_walk_fini(mdb_walk_state_t *wsp)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	lnode_walk_t *lwp = wsp->walk_data;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	mdb_free(lwp->lw_table, lwp->lw_tabsz * sizeof (struct lobucket));
1857c478bd9Sstevel@tonic-gate 	mdb_free(lwp->lw_lnode, sizeof (lnode_t));
1867c478bd9Sstevel@tonic-gate 	mdb_free(lwp, sizeof (lnode_walk_t));
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1907c478bd9Sstevel@tonic-gate static int
lnode_format(uintptr_t addr,const void * data,void * private)1917c478bd9Sstevel@tonic-gate lnode_format(uintptr_t addr, const void *data, void *private)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	const lnode_t *lop = data;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	mdb_printf("%?p %?p %?p\n",
1967c478bd9Sstevel@tonic-gate 	    addr, lop->lo_vnode, lop->lo_vp);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2027c478bd9Sstevel@tonic-gate int
lnode(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2037c478bd9Sstevel@tonic-gate lnode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	if (argc != 0)
2067c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
2097c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %?s%</u>\n",
2107c478bd9Sstevel@tonic-gate 		    "LNODE", "VNODE", "REALVP");
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
2147c478bd9Sstevel@tonic-gate 		lnode_t lo;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		(void) mdb_vread(&lo, sizeof (lo), addr);
2177c478bd9Sstevel@tonic-gate 		return (lnode_format(addr, &lo, NULL));
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (mdb_walk("lnode", lnode_format, NULL) == -1)
2217c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2277c478bd9Sstevel@tonic-gate int
lnode2dev(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2287c478bd9Sstevel@tonic-gate lnode2dev(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	lnode_t lo;
2317c478bd9Sstevel@tonic-gate 	vnode_t	vno;
2327c478bd9Sstevel@tonic-gate 	vfs_t vfs;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (argc != 0)
2357c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	(void) mdb_vread(&lo, sizeof (lo), addr);
2387c478bd9Sstevel@tonic-gate 	(void) mdb_vread(&vno, sizeof (vno), (uintptr_t)lo.lo_vnode);
2397c478bd9Sstevel@tonic-gate 	(void) mdb_vread(&vfs, sizeof (vfs), (uintptr_t)vno.v_vfsp);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	mdb_printf("lnode %p vfs_dev %0?lx\n", addr, vfs.vfs_dev);
2427c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
2467c478bd9Sstevel@tonic-gate 	{ "lnode", NULL, "print lnode structure(s)", lnode },
2477c478bd9Sstevel@tonic-gate 	{ "lnode2dev", ":", "print vfs_dev given lnode", lnode2dev },
2487c478bd9Sstevel@tonic-gate 	{ NULL }
2497c478bd9Sstevel@tonic-gate };
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
2527c478bd9Sstevel@tonic-gate 	{ "lnode", "hash of lnode structures",
2537c478bd9Sstevel@tonic-gate 		lnode_walk_init, lnode_walk_step, lnode_walk_fini },
2547c478bd9Sstevel@tonic-gate 	{ NULL }
2557c478bd9Sstevel@tonic-gate };
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
2587c478bd9Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, walkers
2597c478bd9Sstevel@tonic-gate };
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)2627c478bd9Sstevel@tonic-gate _mdb_init(void)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	return (&modinfo);
2657c478bd9Sstevel@tonic-gate }
266