summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/modules/smbfs/smbfs.c
blob: 5368aa7fcc0394c7dcc7ae6ffb7d34a6db0df19c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/mdb_modapi.h>

#ifdef	_USER
#include "../genunix/avl.h"
#define	_FAKE_KERNEL
#endif

#include <sys/refstr_impl.h>
#include <sys/vnode.h>
#include <sys/vfs.h>

#include <smbfs/smbfs.h>
#include <smbfs/smbfs_node.h>

#define	OPT_VERBOSE	0x0001	/* Be [-v]erbose in dcmd's */

/*
 * This macro lets us easily use both sizeof (typename)
 * and the string-ified typename for the error message.
 */
#define	SMBFS_OBJ_FETCH(obj_addr, obj_type, dest, err) \
	if (mdb_vread(dest, sizeof (obj_type), ((uintptr_t)obj_addr)) \
	!= sizeof (obj_type)) { \
		mdb_warn("error reading "#obj_type" at %p", obj_addr); \
		return (err); \
	}

/*
 * We need to read in a private copy
 * of every string we want to print out.
 */
void
print_str(uintptr_t addr)
{
	char buf[64];
	int len, mx = sizeof (buf) - 4;

	if ((len = mdb_readstr(buf, sizeof (buf), addr)) <= 0) {
		mdb_printf(" (%p)", addr);
	} else {
		if (len > mx)
			strcpy(&buf[mx], "...");
		mdb_printf(" %s", buf);
	}
}

/*
 * Dcmd (and callback function) to print a summary of
 * all "smbfs" entries in the VFS list.
 */

typedef struct smbfs_vfs_cbdata {
	int flags;
	int printed_header;
	uintptr_t vfsops;	/* filter by vfs ops pointer */
	smbmntinfo_t smi;	/* scratch space for smbfs_vfs_cb */
} smbfs_vfs_cbdata_t;

int
smbfs_vfs_cb(uintptr_t addr, const void *data, void *arg)
{
	const vfs_t *vfs = data;
	smbfs_vfs_cbdata_t *cbd = arg;
	uintptr_t ta;

	/* Filter by matching smbfs ops vector. */
	if (cbd->vfsops && cbd->vfsops != (uintptr_t)vfs->vfs_op) {
		return (WALK_NEXT);
	}

	if (cbd->printed_header == 0) {
		cbd->printed_header = 1;
		mdb_printf("// vfs_t smbmntinfo_t mnt_path\n");
	}

	mdb_printf(" %-p", addr);	/* vfs_t */
	mdb_printf(" %-p", (uintptr_t)vfs->vfs_data);
	/*
	 * Note: vfs_mntpt is a refstr_t.
	 * Advance to string member.
	 */
	ta = (uintptr_t)vfs->vfs_mntpt;
	ta += OFFSETOF(struct refstr, rs_string);
	print_str(ta);
	mdb_printf("\n");

	if (cbd->flags & OPT_VERBOSE) {
		mdb_inc_indent(2);
		/* Don't fail the walk if this fails. */
		if (mdb_vread(&cbd->smi, sizeof (cbd->smi),
		    (uintptr_t)vfs->vfs_data) == -1) {
			mdb_warn("error reading smbmntinfo_t at %p",
			    (uintptr_t)vfs->vfs_data);
		} else {
			/* Interesting parts of smbmntinfo_t */
			mdb_printf("smi_share: %p, smi_root: %p\n",
			    cbd->smi.smi_share, cbd->smi.smi_root);
		}
		mdb_dec_indent(2);
	}

	return (WALK_NEXT);
}

int
smbfs_vfs_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	smbfs_vfs_cbdata_t *cbd;
	vfs_t *vfs;

	cbd = mdb_zalloc(sizeof (*cbd),  UM_SLEEP | UM_GC);

	/*
	 * Get the ops address here, so things work
	 * even if the smbfs module is loaded later
	 * than this mdb module.
	 */
	if (mdb_readvar(&cbd->vfsops, "smbfs_vfsops") == -1) {
		mdb_warn("failed to find 'smbfs_vfsops'\n");
		return (DCMD_ERR);
	}

	if (mdb_getopts(argc, argv,
	    'v', MDB_OPT_SETBITS, OPT_VERBOSE, &cbd->flags,
	    NULL) != argc) {
		return (DCMD_USAGE);
	}

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk("vfs", smbfs_vfs_cb, cbd)
		    == -1) {
			mdb_warn("can't walk smbfs vfs");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	vfs = mdb_alloc(sizeof (*vfs),  UM_SLEEP | UM_GC);
	SMBFS_OBJ_FETCH(addr, vfs_t, vfs, DCMD_ERR);
	smbfs_vfs_cb(addr, vfs, cbd);
	return (DCMD_OK);
}

void
smbfs_vfs_help(void)
{
	mdb_printf(
	    "Display addresses of the mounted smbfs structures\n"
	    "and the pathname of the mountpoint\n"
	    "\nOptions:\n"
	    "  -v    display details of the smbmntinfo\n");
}

/*
 * Dcmd (and callback function) to print a summary of
 * all smbnodes in the node "hash" (cache) AVL tree.
 */

typedef struct smbfs_node_cbdata {
	int flags;
	int printed_header;
	vnode_t vn;
} smbfs_node_cbdata_t;

int
smbfs_node_cb(uintptr_t addr, const void *data, void *arg)
{
	const smbnode_t *np = data;
	smbfs_node_cbdata_t *cbd = arg;

	if (cbd->printed_header == 0) {
		cbd->printed_header = 1;
		mdb_printf("// vnode smbnode rpath\n");
	}

	mdb_printf(" %-p", (uintptr_t)np->r_vnode);
	mdb_printf(" %-p", addr);	/* smbnode */
	print_str((uintptr_t)np->n_rpath);
	mdb_printf("\n");

	if (cbd->flags & OPT_VERBOSE) {
		mdb_inc_indent(2);
		/* Don't fail the walk if this fails. */
		if (mdb_vread(&cbd->vn, sizeof (cbd->vn),
		    (uintptr_t)np->r_vnode) == -1) {
			mdb_warn("error reading vnode_t at %p",
			    (uintptr_t)np->r_vnode);
		} else {
			/* Interesting parts of vnode_t */
			mdb_printf("v_type=%d v_count=%d",
			    cbd->vn.v_type, cbd->vn.v_count);
			mdb_printf("\n");
		}
		mdb_dec_indent(2);
	}

	return (WALK_NEXT);
}

int
smbfs_node_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	smbfs_node_cbdata_t *cbd;

	cbd = mdb_zalloc(sizeof (*cbd), UM_SLEEP | UM_GC);

	if (mdb_getopts(argc, argv,
	    'v', MDB_OPT_SETBITS, OPT_VERBOSE, &cbd->flags,
	    NULL) != argc) {
		return (DCMD_USAGE);
	}

	if (!(flags & DCMD_ADDRSPEC)) {
		mdb_warn("expect an smbmntinfo_t addr");
		return (DCMD_USAGE);
	}
	addr += OFFSETOF(smbmntinfo_t, smi_hash_avl);

	if (mdb_pwalk("avl", smbfs_node_cb, cbd, addr) == -1) {
		mdb_warn("cannot walk smbfs nodes");
		return (DCMD_ERR);
	}

	return (DCMD_OK);
}

void
smbfs_node_help(void)
{
	mdb_printf("Options:\n"
	    "  -v           be verbose when displaying smbnodes\n");
}

static const mdb_dcmd_t dcmds[] = {
	{
		"smbfs_vfs", "?[-v]",
		"show smbfs-mounted vfs structs",
		smbfs_vfs_dcmd, smbfs_vfs_help
	},
	{
		"smbfs_node", "?[-v]",
		"given an smbmntinfo_t, list smbnodes",
		smbfs_node_dcmd, smbfs_node_help
	},
	{NULL}
};

#ifdef _USER
/*
 * Sadly, can't just compile ../genunix/vfs.c with this since
 * it has become a catch-all for FS-specific headers etc.
 */
int
vfs_walk_init(mdb_walk_state_t *wsp)
{
	if (wsp->walk_addr == 0 &&
	    mdb_readvar(&wsp->walk_addr, "rootvfs") == -1) {
		mdb_warn("failed to read 'rootvfs'");
		return (WALK_ERR);
	}

	wsp->walk_data = (void *)wsp->walk_addr;
	return (WALK_NEXT);
}

int
vfs_walk_step(mdb_walk_state_t *wsp)
{
	vfs_t vfs;
	int status;

	if (mdb_vread(&vfs, sizeof (vfs), wsp->walk_addr) == -1) {
		mdb_warn("failed to read vfs_t at %p", wsp->walk_addr);
		return (WALK_DONE);
	}

	status = wsp->walk_callback(wsp->walk_addr, &vfs, wsp->walk_cbdata);

	if (vfs.vfs_next == wsp->walk_data)
		return (WALK_DONE);

	wsp->walk_addr = (uintptr_t)vfs.vfs_next;

	return (status);
}
#endif	// _USER

static const mdb_walker_t walkers[] = {
#ifdef	_USER
	/* from avl.c */
	{ AVL_WALK_NAME, AVL_WALK_DESC,
		avl_walk_init, avl_walk_step, avl_walk_fini },
	/* from vfs.c */
	{ "vfs", "walk file system list",
		vfs_walk_init, vfs_walk_step },
#endif	// _USER
	{NULL}
};


static const mdb_modinfo_t modinfo = {
	MDB_API_VERSION,
	dcmds,
	walkers
};

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}