1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/vfs.h>
31*0Sstevel@tonic-gate #include <sys/fs/lofs_node.h>
32*0Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate typedef struct lnode_walk {
37*0Sstevel@tonic-gate struct lobucket *lw_table; /* Snapshot of hash table */
38*0Sstevel@tonic-gate uint_t lw_tabsz; /* Size of hash table */
39*0Sstevel@tonic-gate uint_t lw_tabi; /* Current table index */
40*0Sstevel@tonic-gate lnode_t *lw_lnode; /* Current buffer */
41*0Sstevel@tonic-gate } lnode_walk_t;
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate int
lnode_walk_init(mdb_walk_state_t * wsp)44*0Sstevel@tonic-gate lnode_walk_init(mdb_walk_state_t *wsp)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate lnode_walk_t *lwp;
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate int lofsfstype;
49*0Sstevel@tonic-gate struct vfs vfs;
50*0Sstevel@tonic-gate struct loinfo loinfo;
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate if (mdb_readvar(&lofsfstype, "lofsfstype") == -1) {
53*0Sstevel@tonic-gate mdb_warn("failed to read 'lofsfstype' symbol\n");
54*0Sstevel@tonic-gate return (WALK_ERR);
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate if (wsp->walk_addr == NULL) {
58*0Sstevel@tonic-gate uintptr_t rootvfsp, vfsp;
59*0Sstevel@tonic-gate uint_t htsize;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate lwp = mdb_alloc(sizeof (lnode_walk_t), UM_SLEEP);
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate retry:
64*0Sstevel@tonic-gate lwp->lw_tabsz = 0;
65*0Sstevel@tonic-gate if (mdb_readvar(&rootvfsp, "rootvfs") == -1) {
66*0Sstevel@tonic-gate mdb_warn("failed to read 'rootvfs' symbol\n");
67*0Sstevel@tonic-gate mdb_free(lwp, sizeof (lnode_walk_t));
68*0Sstevel@tonic-gate return (WALK_ERR);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate vfsp = rootvfsp;
72*0Sstevel@tonic-gate do {
73*0Sstevel@tonic-gate (void) mdb_vread(&vfs, sizeof (vfs), vfsp);
74*0Sstevel@tonic-gate if (lofsfstype != vfs.vfs_fstype) {
75*0Sstevel@tonic-gate vfsp = (uintptr_t)vfs.vfs_next;
76*0Sstevel@tonic-gate continue;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate (void) mdb_vread(&loinfo, sizeof (struct loinfo),
79*0Sstevel@tonic-gate (uintptr_t)vfs.vfs_data);
80*0Sstevel@tonic-gate lwp->lw_tabsz += loinfo.li_htsize;
81*0Sstevel@tonic-gate vfsp = (uintptr_t)vfs.vfs_next;
82*0Sstevel@tonic-gate } while (vfsp != rootvfsp);
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate if (lwp->lw_tabsz == 0) {
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate * No lofs filesystems mounted.
87*0Sstevel@tonic-gate */
88*0Sstevel@tonic-gate mdb_free(lwp, sizeof (lnode_walk_t));
89*0Sstevel@tonic-gate return (WALK_DONE);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate lwp->lw_table = mdb_alloc(lwp->lw_tabsz *
92*0Sstevel@tonic-gate sizeof (struct lobucket), UM_SLEEP);
93*0Sstevel@tonic-gate htsize = 0;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate vfsp = rootvfsp;
96*0Sstevel@tonic-gate do {
97*0Sstevel@tonic-gate (void) mdb_vread(&vfs, sizeof (vfs), vfsp);
98*0Sstevel@tonic-gate if (lofsfstype != vfs.vfs_fstype) {
99*0Sstevel@tonic-gate vfsp = (uintptr_t)vfs.vfs_next;
100*0Sstevel@tonic-gate continue;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate (void) mdb_vread(&loinfo, sizeof (struct loinfo),
103*0Sstevel@tonic-gate (uintptr_t)vfs.vfs_data);
104*0Sstevel@tonic-gate if (htsize + loinfo.li_htsize > lwp->lw_tabsz) {
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate * Something must have resized.
107*0Sstevel@tonic-gate */
108*0Sstevel@tonic-gate mdb_free(lwp->lw_table,
109*0Sstevel@tonic-gate lwp->lw_tabsz * sizeof (struct lobucket));
110*0Sstevel@tonic-gate goto retry;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate (void) mdb_vread(lwp->lw_table + htsize,
113*0Sstevel@tonic-gate loinfo.li_htsize * sizeof (struct lobucket),
114*0Sstevel@tonic-gate (uintptr_t)loinfo.li_hashtable);
115*0Sstevel@tonic-gate htsize += loinfo.li_htsize;
116*0Sstevel@tonic-gate vfsp = (uintptr_t)vfs.vfs_next;
117*0Sstevel@tonic-gate } while (vfsp != rootvfsp);
118*0Sstevel@tonic-gate } else {
119*0Sstevel@tonic-gate if (mdb_vread(&vfs, sizeof (vfs_t), wsp->walk_addr) == -1) {
120*0Sstevel@tonic-gate mdb_warn("failed to read from '%p'\n", wsp->walk_addr);
121*0Sstevel@tonic-gate return (WALK_ERR);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate if (lofsfstype != vfs.vfs_fstype) {
124*0Sstevel@tonic-gate mdb_warn("%p does not point to a lofs mount vfs\n",
125*0Sstevel@tonic-gate wsp->walk_addr);
126*0Sstevel@tonic-gate return (WALK_ERR);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate if (mdb_vread(&loinfo, sizeof (loinfo),
129*0Sstevel@tonic-gate (uintptr_t)vfs.vfs_data) == -1) {
130*0Sstevel@tonic-gate mdb_warn("failed to read struct loinfo from '%p'\n",
131*0Sstevel@tonic-gate vfs.vfs_data);
132*0Sstevel@tonic-gate return (WALK_ERR);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate lwp = mdb_alloc(sizeof (lnode_walk_t), UM_SLEEP);
136*0Sstevel@tonic-gate lwp->lw_tabsz = loinfo.li_htsize;
137*0Sstevel@tonic-gate lwp->lw_table = mdb_alloc(lwp->lw_tabsz *
138*0Sstevel@tonic-gate sizeof (struct lobucket), UM_SLEEP);
139*0Sstevel@tonic-gate (void) mdb_vread(lwp->lw_table,
140*0Sstevel@tonic-gate lwp->lw_tabsz * sizeof (struct lobucket),
141*0Sstevel@tonic-gate (uintptr_t)loinfo.li_hashtable);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate lwp->lw_tabi = 0;
144*0Sstevel@tonic-gate lwp->lw_lnode = mdb_alloc(sizeof (lnode_t), UM_SLEEP);
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)lwp->lw_table[0].lh_chain;
147*0Sstevel@tonic-gate wsp->walk_data = lwp;
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate return (WALK_NEXT);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate int
lnode_walk_step(mdb_walk_state_t * wsp)153*0Sstevel@tonic-gate lnode_walk_step(mdb_walk_state_t *wsp)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate lnode_walk_t *lwp = wsp->walk_data;
156*0Sstevel@tonic-gate uintptr_t addr;
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate * If the next lnode_t address we want is NULL, advance to the next
160*0Sstevel@tonic-gate * hash bucket. When we reach lw_tabsz, we're done.
161*0Sstevel@tonic-gate */
162*0Sstevel@tonic-gate while (wsp->walk_addr == NULL) {
163*0Sstevel@tonic-gate if (++lwp->lw_tabi < lwp->lw_tabsz)
164*0Sstevel@tonic-gate wsp->walk_addr =
165*0Sstevel@tonic-gate (uintptr_t)lwp->lw_table[lwp->lw_tabi].lh_chain;
166*0Sstevel@tonic-gate else
167*0Sstevel@tonic-gate return (WALK_DONE);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate * When we have an lnode_t address, read the lnode and invoke the
172*0Sstevel@tonic-gate * walk callback. Keep the next lnode_t address in wsp->walk_addr.
173*0Sstevel@tonic-gate */
174*0Sstevel@tonic-gate addr = wsp->walk_addr;
175*0Sstevel@tonic-gate (void) mdb_vread(lwp->lw_lnode, sizeof (lnode_t), addr);
176*0Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)lwp->lw_lnode->lo_next;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate return (wsp->walk_callback(addr, lwp->lw_lnode, wsp->walk_cbdata));
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate void
lnode_walk_fini(mdb_walk_state_t * wsp)182*0Sstevel@tonic-gate lnode_walk_fini(mdb_walk_state_t *wsp)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate lnode_walk_t *lwp = wsp->walk_data;
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate mdb_free(lwp->lw_table, lwp->lw_tabsz * sizeof (struct lobucket));
187*0Sstevel@tonic-gate mdb_free(lwp->lw_lnode, sizeof (lnode_t));
188*0Sstevel@tonic-gate mdb_free(lwp, sizeof (lnode_walk_t));
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*ARGSUSED*/
192*0Sstevel@tonic-gate static int
lnode_format(uintptr_t addr,const void * data,void * private)193*0Sstevel@tonic-gate lnode_format(uintptr_t addr, const void *data, void *private)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate const lnode_t *lop = data;
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate mdb_printf("%?p %?p %?p\n",
198*0Sstevel@tonic-gate addr, lop->lo_vnode, lop->lo_vp);
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate return (DCMD_OK);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate /*ARGSUSED*/
204*0Sstevel@tonic-gate int
lnode(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)205*0Sstevel@tonic-gate lnode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate if (argc != 0)
208*0Sstevel@tonic-gate return (DCMD_USAGE);
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate if (DCMD_HDRSPEC(flags)) {
211*0Sstevel@tonic-gate mdb_printf("%<u>%?s %?s %?s%</u>\n",
212*0Sstevel@tonic-gate "LNODE", "VNODE", "REALVP");
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate if (flags & DCMD_ADDRSPEC) {
216*0Sstevel@tonic-gate lnode_t lo;
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate (void) mdb_vread(&lo, sizeof (lo), addr);
219*0Sstevel@tonic-gate return (lnode_format(addr, &lo, NULL));
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate if (mdb_walk("lnode", lnode_format, NULL) == -1)
223*0Sstevel@tonic-gate return (DCMD_ERR);
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate return (DCMD_OK);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate /*ARGSUSED*/
229*0Sstevel@tonic-gate int
lnode2dev(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)230*0Sstevel@tonic-gate lnode2dev(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate lnode_t lo;
233*0Sstevel@tonic-gate vnode_t vno;
234*0Sstevel@tonic-gate vfs_t vfs;
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate if (argc != 0)
237*0Sstevel@tonic-gate return (DCMD_ERR);
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate (void) mdb_vread(&lo, sizeof (lo), addr);
240*0Sstevel@tonic-gate (void) mdb_vread(&vno, sizeof (vno), (uintptr_t)lo.lo_vnode);
241*0Sstevel@tonic-gate (void) mdb_vread(&vfs, sizeof (vfs), (uintptr_t)vno.v_vfsp);
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate mdb_printf("lnode %p vfs_dev %0?lx\n", addr, vfs.vfs_dev);
244*0Sstevel@tonic-gate return (DCMD_OK);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
248*0Sstevel@tonic-gate { "lnode", NULL, "print lnode structure(s)", lnode },
249*0Sstevel@tonic-gate { "lnode2dev", ":", "print vfs_dev given lnode", lnode2dev },
250*0Sstevel@tonic-gate { NULL }
251*0Sstevel@tonic-gate };
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
254*0Sstevel@tonic-gate { "lnode", "hash of lnode structures",
255*0Sstevel@tonic-gate lnode_walk_init, lnode_walk_step, lnode_walk_fini },
256*0Sstevel@tonic-gate { NULL }
257*0Sstevel@tonic-gate };
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
260*0Sstevel@tonic-gate MDB_API_VERSION, dcmds, walkers
261*0Sstevel@tonic-gate };
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)264*0Sstevel@tonic-gate _mdb_init(void)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate return (&modinfo);
267*0Sstevel@tonic-gate }
268