xref: /illumos-gate/usr/src/cmd/mdb/common/modules/smbsrv/smbsrv.c (revision 2bbdd445a21f9d61f4a0ca0faf05d5ceb2bd91f3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25  */
26 
27 #include <mdb/mdb_modapi.h>
28 #include <mdb/mdb_ks.h>
29 #include <sys/thread.h>
30 #include <sys/taskq_impl.h>
31 #include <smbsrv/smb_vops.h>
32 #include <smbsrv/smb.h>
33 #include <smbsrv/smb_ktypes.h>
34 
35 #define	SMB_DCMD_INDENT		2
36 #define	ACE_TYPE_TABLEN		(ACE_ALL_TYPES + 1)
37 #define	ACE_TYPE_ENTRY(_v_)	{_v_, #_v_}
38 #define	SMB_COM_ENTRY(_v_, _x_)	{#_v_, _x_}
39 
40 #define	SMB_MDB_MAX_OPTS	9
41 
42 #define	SMB_OPT_SERVER		0x00000001
43 #define	SMB_OPT_SESSION		0x00000002
44 #define	SMB_OPT_REQUEST		0x00000004
45 #define	SMB_OPT_USER		0x00000008
46 #define	SMB_OPT_TREE		0x00000010
47 #define	SMB_OPT_OFILE		0x00000020
48 #define	SMB_OPT_ODIR		0x00000040
49 #define	SMB_OPT_WALK		0x00000100
50 #define	SMB_OPT_VERBOSE		0x00000200
51 #define	SMB_OPT_ALL_OBJ		0x000000FF
52 
53 /*
54  * Structure associating an ACE type to a string.
55  */
56 typedef struct {
57 	uint8_t		ace_type_value;
58 	const char	*ace_type_sting;
59 } ace_type_entry_t;
60 
61 /*
62  * Structure containing strings describing an SMB command.
63  */
64 typedef struct {
65 	const char	*smb_com;
66 	const char	*smb_andx;
67 } smb_com_entry_t;
68 
69 /*
70  * Structure describing an object to be expanded (displayed).
71  */
72 typedef struct {
73 	uint_t		ex_mask;
74 	size_t		ex_offset;
75 	const char	*ex_dcmd;
76 	const char	*ex_name;
77 } smb_exp_t;
78 
79 /*
80  * List of supported options. Ther order has the match the bits SMB_OPT_xxx.
81  */
82 typedef struct smb_mdb_opts {
83 	char		*o_name;
84 	uint32_t	o_value;
85 } smb_mdb_opts_t;
86 
87 static smb_mdb_opts_t smb_opts[SMB_MDB_MAX_OPTS] =
88 {
89 	{ "-s", SMB_OPT_SERVER	},
90 	{ "-e", SMB_OPT_SESSION	},
91 	{ "-r", SMB_OPT_REQUEST	},
92 	{ "-u", SMB_OPT_USER	},
93 	{ "-t", SMB_OPT_TREE	},
94 	{ "-f", SMB_OPT_OFILE	},
95 	{ "-d", SMB_OPT_ODIR	},
96 	{ "-w", SMB_OPT_WALK	},
97 	{ "-v", SMB_OPT_VERBOSE	}
98 };
99 
100 static smb_com_entry_t	smb_com[256] =
101 {
102 	SMB_COM_ENTRY(SMB_COM_CREATE_DIRECTORY, "No"),
103 	SMB_COM_ENTRY(SMB_COM_DELETE_DIRECTORY, "No"),
104 	SMB_COM_ENTRY(SMB_COM_OPEN, "No"),
105 	SMB_COM_ENTRY(SMB_COM_CREATE, "No"),
106 	SMB_COM_ENTRY(SMB_COM_CLOSE, "No"),
107 	SMB_COM_ENTRY(SMB_COM_FLUSH, "No"),
108 	SMB_COM_ENTRY(SMB_COM_DELETE, "No"),
109 	SMB_COM_ENTRY(SMB_COM_RENAME, "No"),
110 	SMB_COM_ENTRY(SMB_COM_QUERY_INFORMATION, "No"),
111 	SMB_COM_ENTRY(SMB_COM_SET_INFORMATION, "No"),
112 	SMB_COM_ENTRY(SMB_COM_READ, "No"),
113 	SMB_COM_ENTRY(SMB_COM_WRITE, "No"),
114 	SMB_COM_ENTRY(SMB_COM_LOCK_BYTE_RANGE, "No"),
115 	SMB_COM_ENTRY(SMB_COM_UNLOCK_BYTE_RANGE, "No"),
116 	SMB_COM_ENTRY(SMB_COM_CREATE_TEMPORARY, "No"),
117 	SMB_COM_ENTRY(SMB_COM_CREATE_NEW, "No"),
118 	SMB_COM_ENTRY(SMB_COM_CHECK_DIRECTORY, "No"),
119 	SMB_COM_ENTRY(SMB_COM_PROCESS_EXIT, "No"),
120 	SMB_COM_ENTRY(SMB_COM_SEEK, "No"),
121 	SMB_COM_ENTRY(SMB_COM_LOCK_AND_READ, "No"),
122 	SMB_COM_ENTRY(SMB_COM_WRITE_AND_UNLOCK, "No"),
123 	SMB_COM_ENTRY(0x15, "?"),
124 	SMB_COM_ENTRY(0x16, "?"),
125 	SMB_COM_ENTRY(0x17, "?"),
126 	SMB_COM_ENTRY(0x18, "?"),
127 	SMB_COM_ENTRY(0x19, "?"),
128 	SMB_COM_ENTRY(SMB_COM_READ_RAW, "No"),
129 	SMB_COM_ENTRY(SMB_COM_READ_MPX, "No"),
130 	SMB_COM_ENTRY(SMB_COM_READ_MPX_SECONDARY, "No"),
131 	SMB_COM_ENTRY(SMB_COM_WRITE_RAW, "No"),
132 	SMB_COM_ENTRY(SMB_COM_WRITE_MPX, "No"),
133 	SMB_COM_ENTRY(SMB_COM_WRITE_MPX_SECONDARY, "No"),
134 	SMB_COM_ENTRY(SMB_COM_WRITE_COMPLETE, "No"),
135 	SMB_COM_ENTRY(SMB_COM_QUERY_SERVER, "No"),
136 	SMB_COM_ENTRY(SMB_COM_SET_INFORMATION2, "No"),
137 	SMB_COM_ENTRY(SMB_COM_QUERY_INFORMATION2, "No"),
138 	SMB_COM_ENTRY(SMB_COM_LOCKING_ANDX, "No"),
139 	SMB_COM_ENTRY(SMB_COM_TRANSACTION, "No"),
140 	SMB_COM_ENTRY(SMB_COM_TRANSACTION_SECONDARY, "No"),
141 	SMB_COM_ENTRY(SMB_COM_IOCTL, "No"),
142 	SMB_COM_ENTRY(SMB_COM_IOCTL_SECONDARY, "No"),
143 	SMB_COM_ENTRY(SMB_COM_COPY, "No"),
144 	SMB_COM_ENTRY(SMB_COM_MOVE, "No"),
145 	SMB_COM_ENTRY(SMB_COM_ECHO, "No"),
146 	SMB_COM_ENTRY(SMB_COM_WRITE_AND_CLOSE, "No"),
147 	SMB_COM_ENTRY(SMB_COM_OPEN_ANDX, "No"),
148 	SMB_COM_ENTRY(SMB_COM_READ_ANDX, "No"),
149 	SMB_COM_ENTRY(SMB_COM_WRITE_ANDX, "No"),
150 	SMB_COM_ENTRY(SMB_COM_NEW_FILE_SIZE, "No"),
151 	SMB_COM_ENTRY(SMB_COM_CLOSE_AND_TREE_DISC, "No"),
152 	SMB_COM_ENTRY(SMB_COM_TRANSACTION2, "No"),
153 	SMB_COM_ENTRY(SMB_COM_TRANSACTION2_SECONDARY, "No"),
154 	SMB_COM_ENTRY(SMB_COM_FIND_CLOSE2, "No"),
155 	SMB_COM_ENTRY(SMB_COM_FIND_NOTIFY_CLOSE, "No"),
156 	SMB_COM_ENTRY(0x36, "?"),
157 	SMB_COM_ENTRY(0x37, "?"),
158 	SMB_COM_ENTRY(0x38, "?"),
159 	SMB_COM_ENTRY(0x39, "?"),
160 	SMB_COM_ENTRY(0x3A, "?"),
161 	SMB_COM_ENTRY(0x3B, "?"),
162 	SMB_COM_ENTRY(0x3C, "?"),
163 	SMB_COM_ENTRY(0x3D, "?"),
164 	SMB_COM_ENTRY(0x3E, "?"),
165 	SMB_COM_ENTRY(0x3F, "?"),
166 	SMB_COM_ENTRY(0x40, "?"),
167 	SMB_COM_ENTRY(0x41, "?"),
168 	SMB_COM_ENTRY(0x42, "?"),
169 	SMB_COM_ENTRY(0x43, "?"),
170 	SMB_COM_ENTRY(0x44, "?"),
171 	SMB_COM_ENTRY(0x45, "?"),
172 	SMB_COM_ENTRY(0x46, "?"),
173 	SMB_COM_ENTRY(0x47, "?"),
174 	SMB_COM_ENTRY(0x48, "?"),
175 	SMB_COM_ENTRY(0x49, "?"),
176 	SMB_COM_ENTRY(0x4A, "?"),
177 	SMB_COM_ENTRY(0x4B, "?"),
178 	SMB_COM_ENTRY(0x4C, "?"),
179 	SMB_COM_ENTRY(0x4D, "?"),
180 	SMB_COM_ENTRY(0x4E, "?"),
181 	SMB_COM_ENTRY(0x4F, "?"),
182 	SMB_COM_ENTRY(0x50, "?"),
183 	SMB_COM_ENTRY(0x51, "?"),
184 	SMB_COM_ENTRY(0x52, "?"),
185 	SMB_COM_ENTRY(0x53, "?"),
186 	SMB_COM_ENTRY(0x54, "?"),
187 	SMB_COM_ENTRY(0x55, "?"),
188 	SMB_COM_ENTRY(0x56, "?"),
189 	SMB_COM_ENTRY(0x57, "?"),
190 	SMB_COM_ENTRY(0x58, "?"),
191 	SMB_COM_ENTRY(0x59, "?"),
192 	SMB_COM_ENTRY(0x5A, "?"),
193 	SMB_COM_ENTRY(0x5B, "?"),
194 	SMB_COM_ENTRY(0x5C, "?"),
195 	SMB_COM_ENTRY(0x5D, "?"),
196 	SMB_COM_ENTRY(0x5E, "?"),
197 	SMB_COM_ENTRY(0x5F, "?"),
198 	SMB_COM_ENTRY(0x60, "?"),
199 	SMB_COM_ENTRY(0x61, "?"),
200 	SMB_COM_ENTRY(0x62, "?"),
201 	SMB_COM_ENTRY(0x63, "?"),
202 	SMB_COM_ENTRY(0x64, "?"),
203 	SMB_COM_ENTRY(0x65, "?"),
204 	SMB_COM_ENTRY(0x66, "?"),
205 	SMB_COM_ENTRY(0x67, "?"),
206 	SMB_COM_ENTRY(0x68, "?"),
207 	SMB_COM_ENTRY(0x69, "?"),
208 	SMB_COM_ENTRY(0x6A, "?"),
209 	SMB_COM_ENTRY(0x6B, "?"),
210 	SMB_COM_ENTRY(0x6C, "?"),
211 	SMB_COM_ENTRY(0x6D, "?"),
212 	SMB_COM_ENTRY(0x6E, "?"),
213 	SMB_COM_ENTRY(0x6F, "?"),
214 	SMB_COM_ENTRY(SMB_COM_TREE_CONNECT, "No"),
215 	SMB_COM_ENTRY(SMB_COM_TREE_DISCONNECT, "No"),
216 	SMB_COM_ENTRY(SMB_COM_NEGOTIATE, "No"),
217 	SMB_COM_ENTRY(SMB_COM_SESSION_SETUP_ANDX, "No"),
218 	SMB_COM_ENTRY(SMB_COM_LOGOFF_ANDX, "No"),
219 	SMB_COM_ENTRY(SMB_COM_TREE_CONNECT_ANDX, "No"),
220 	SMB_COM_ENTRY(0x76, "?"),
221 	SMB_COM_ENTRY(0x77, "?"),
222 	SMB_COM_ENTRY(0x78, "?"),
223 	SMB_COM_ENTRY(0x79, "?"),
224 	SMB_COM_ENTRY(0x7A, "?"),
225 	SMB_COM_ENTRY(0x7B, "?"),
226 	SMB_COM_ENTRY(0x7C, "?"),
227 	SMB_COM_ENTRY(0x7D, "?"),
228 	SMB_COM_ENTRY(0x7E, "?"),
229 	SMB_COM_ENTRY(0x7F, "?"),
230 	SMB_COM_ENTRY(SMB_COM_QUERY_INFORMATION_DISK, "No"),
231 	SMB_COM_ENTRY(SMB_COM_SEARCH, "No"),
232 	SMB_COM_ENTRY(SMB_COM_FIND, "No"),
233 	SMB_COM_ENTRY(SMB_COM_FIND_UNIQUE, "No"),
234 	SMB_COM_ENTRY(SMB_COM_FIND_CLOSE, "No"),
235 	SMB_COM_ENTRY(0x85, "?"),
236 	SMB_COM_ENTRY(0x86, "?"),
237 	SMB_COM_ENTRY(0x87, "?"),
238 	SMB_COM_ENTRY(0x88, "?"),
239 	SMB_COM_ENTRY(0x89, "?"),
240 	SMB_COM_ENTRY(0x8A, "?"),
241 	SMB_COM_ENTRY(0x8B, "?"),
242 	SMB_COM_ENTRY(0x8C, "?"),
243 	SMB_COM_ENTRY(0x8D, "?"),
244 	SMB_COM_ENTRY(0x8E, "?"),
245 	SMB_COM_ENTRY(0x8F, "?"),
246 	SMB_COM_ENTRY(0x90, "?"),
247 	SMB_COM_ENTRY(0x91, "?"),
248 	SMB_COM_ENTRY(0x92, "?"),
249 	SMB_COM_ENTRY(0x93, "?"),
250 	SMB_COM_ENTRY(0x94, "?"),
251 	SMB_COM_ENTRY(0x95, "?"),
252 	SMB_COM_ENTRY(0x96, "?"),
253 	SMB_COM_ENTRY(0x97, "?"),
254 	SMB_COM_ENTRY(0x98, "?"),
255 	SMB_COM_ENTRY(0x99, "?"),
256 	SMB_COM_ENTRY(0x9A, "?"),
257 	SMB_COM_ENTRY(0x9B, "?"),
258 	SMB_COM_ENTRY(0x9C, "?"),
259 	SMB_COM_ENTRY(0x9D, "?"),
260 	SMB_COM_ENTRY(0x9E, "?"),
261 	SMB_COM_ENTRY(0x9F, "?"),
262 	SMB_COM_ENTRY(SMB_COM_NT_TRANSACT, "No"),
263 	SMB_COM_ENTRY(SMB_COM_NT_TRANSACT_SECONDARY, "No"),
264 	SMB_COM_ENTRY(SMB_COM_NT_CREATE_ANDX, "No"),
265 	SMB_COM_ENTRY(0xA3, "?"),
266 	SMB_COM_ENTRY(SMB_COM_NT_CANCEL, "No"),
267 	SMB_COM_ENTRY(SMB_COM_NT_RENAME, "No"),
268 	SMB_COM_ENTRY(0xA6, "?"),
269 	SMB_COM_ENTRY(0xA7, "?"),
270 	SMB_COM_ENTRY(0xA8, "?"),
271 	SMB_COM_ENTRY(0xA9, "?"),
272 	SMB_COM_ENTRY(0xAA, "?"),
273 	SMB_COM_ENTRY(0xAB, "?"),
274 	SMB_COM_ENTRY(0xAC, "?"),
275 	SMB_COM_ENTRY(0xAD, "?"),
276 	SMB_COM_ENTRY(0xAE, "?"),
277 	SMB_COM_ENTRY(0xAF, "?"),
278 	SMB_COM_ENTRY(0xB0, "?"),
279 	SMB_COM_ENTRY(0xB1, "?"),
280 	SMB_COM_ENTRY(0xB2, "?"),
281 	SMB_COM_ENTRY(0xB3, "?"),
282 	SMB_COM_ENTRY(0xB4, "?"),
283 	SMB_COM_ENTRY(0xB5, "?"),
284 	SMB_COM_ENTRY(0xB6, "?"),
285 	SMB_COM_ENTRY(0xB7, "?"),
286 	SMB_COM_ENTRY(0xB8, "?"),
287 	SMB_COM_ENTRY(0xB9, "?"),
288 	SMB_COM_ENTRY(0xBA, "?"),
289 	SMB_COM_ENTRY(0xBB, "?"),
290 	SMB_COM_ENTRY(0xBC, "?"),
291 	SMB_COM_ENTRY(0xBD, "?"),
292 	SMB_COM_ENTRY(0xBE, "?"),
293 	SMB_COM_ENTRY(0xBF, "?"),
294 	SMB_COM_ENTRY(SMB_COM_OPEN_PRINT_FILE, "No"),
295 	SMB_COM_ENTRY(SMB_COM_WRITE_PRINT_FILE, "No"),
296 	SMB_COM_ENTRY(SMB_COM_CLOSE_PRINT_FILE, "No"),
297 	SMB_COM_ENTRY(SMB_COM_GET_PRINT_QUEUE, "No"),
298 	SMB_COM_ENTRY(0xC4, "?"),
299 	SMB_COM_ENTRY(0xC5, "?"),
300 	SMB_COM_ENTRY(0xC6, "?"),
301 	SMB_COM_ENTRY(0xC7, "?"),
302 	SMB_COM_ENTRY(0xC8, "?"),
303 	SMB_COM_ENTRY(0xC9, "?"),
304 	SMB_COM_ENTRY(0xCA, "?"),
305 	SMB_COM_ENTRY(0xCB, "?"),
306 	SMB_COM_ENTRY(0xCC, "?"),
307 	SMB_COM_ENTRY(0xCD, "?"),
308 	SMB_COM_ENTRY(0xCE, "?"),
309 	SMB_COM_ENTRY(0xCF, "?"),
310 	SMB_COM_ENTRY(0xD0, "?"),
311 	SMB_COM_ENTRY(0xD1, "?"),
312 	SMB_COM_ENTRY(0xD2, "?"),
313 	SMB_COM_ENTRY(0xD3, "?"),
314 	SMB_COM_ENTRY(0xD4, "?"),
315 	SMB_COM_ENTRY(0xD5, "?"),
316 	SMB_COM_ENTRY(0xD6, "?"),
317 	SMB_COM_ENTRY(0xD7, "?"),
318 	SMB_COM_ENTRY(SMB_COM_READ_BULK, "No"),
319 	SMB_COM_ENTRY(SMB_COM_WRITE_BULK, "No"),
320 	SMB_COM_ENTRY(SMB_COM_WRITE_BULK_DATA, "No"),
321 	SMB_COM_ENTRY(0xDB, "?"),
322 	SMB_COM_ENTRY(0xDC, "?"),
323 	SMB_COM_ENTRY(0xDD, "?"),
324 	SMB_COM_ENTRY(0xDE, "?"),
325 	SMB_COM_ENTRY(0xDF, "?"),
326 	SMB_COM_ENTRY(0xE0, "?"),
327 	SMB_COM_ENTRY(0xE1, "?"),
328 	SMB_COM_ENTRY(0xE2, "?"),
329 	SMB_COM_ENTRY(0xE3, "?"),
330 	SMB_COM_ENTRY(0xE4, "?"),
331 	SMB_COM_ENTRY(0xE5, "?"),
332 	SMB_COM_ENTRY(0xE6, "?"),
333 	SMB_COM_ENTRY(0xE7, "?"),
334 	SMB_COM_ENTRY(0xE8, "?"),
335 	SMB_COM_ENTRY(0xE9, "?"),
336 	SMB_COM_ENTRY(0xEA, "?"),
337 	SMB_COM_ENTRY(0xEB, "?"),
338 	SMB_COM_ENTRY(0xEC, "?"),
339 	SMB_COM_ENTRY(0xED, "?"),
340 	SMB_COM_ENTRY(0xEE, "?"),
341 	SMB_COM_ENTRY(0xEF, "?"),
342 	SMB_COM_ENTRY(0xF0, "?"),
343 	SMB_COM_ENTRY(0xF1, "?"),
344 	SMB_COM_ENTRY(0xF2, "?"),
345 	SMB_COM_ENTRY(0xF3, "?"),
346 	SMB_COM_ENTRY(0xF4, "?"),
347 	SMB_COM_ENTRY(0xF5, "?"),
348 	SMB_COM_ENTRY(0xF6, "?"),
349 	SMB_COM_ENTRY(0xF7, "?"),
350 	SMB_COM_ENTRY(0xF8, "?"),
351 	SMB_COM_ENTRY(0xF9, "?"),
352 	SMB_COM_ENTRY(0xFA, "?"),
353 	SMB_COM_ENTRY(0xFB, "?"),
354 	SMB_COM_ENTRY(0xFC, "?"),
355 	SMB_COM_ENTRY(0xFD, "?"),
356 	SMB_COM_ENTRY(0xFE, "?"),
357 	SMB_COM_ENTRY(0xFF, "?")
358 };
359 
360 static int smb_dcmd_list(uintptr_t, uint_t, int, const mdb_arg_t *);
361 static void smb_dcmd_list_help(void);
362 static int smb_dcmd_server(uintptr_t, uint_t, int, const mdb_arg_t *);
363 static void smb_dcmd_session_help(void);
364 static int smb_dcmd_session(uintptr_t, uint_t, int, const mdb_arg_t *);
365 static int smb_dcmd_request(uintptr_t, uint_t, int, const mdb_arg_t *);
366 static void smb_dcmd_user_help(void);
367 static int smb_dcmd_user(uintptr_t, uint_t, int, const mdb_arg_t *);
368 static void smb_dcmd_tree_help(void);
369 static int smb_dcmd_tree(uintptr_t, uint_t, int, const mdb_arg_t *);
370 static int smb_dcmd_odir(uintptr_t, uint_t, int, const mdb_arg_t *);
371 static int smb_dcmd_ofile(uintptr_t, uint_t, int, const mdb_arg_t *);
372 static int smb_dcmd_kshare(uintptr_t, uint_t, int, const mdb_arg_t *);
373 static int smb_dcmd_vfs(uintptr_t, uint_t, int, const mdb_arg_t *);
374 static int smb_vfs_walk_init(mdb_walk_state_t *);
375 static int smb_vfs_walk_step(mdb_walk_state_t *);
376 static void smb_node_help(void);
377 static int smb_dcmd_node(uintptr_t, uint_t, int, const mdb_arg_t *);
378 static int smb_node_walk_init(mdb_walk_state_t *);
379 static int smb_node_walk_step(mdb_walk_state_t *);
380 static int smb_lock(uintptr_t, uint_t, int, const mdb_arg_t *);
381 static int smb_oplock(uintptr_t, uint_t, int, const mdb_arg_t *);
382 static int smb_oplock_grant(uintptr_t, uint_t, int, const mdb_arg_t *);
383 static int smb_ace(uintptr_t, uint_t, int, const mdb_arg_t *);
384 static int smb_ace_walk_init(mdb_walk_state_t *);
385 static int smb_ace_walk_step(mdb_walk_state_t *);
386 static int smb_acl(uintptr_t, uint_t, int, const mdb_arg_t *);
387 static int smb_sd(uintptr_t, uint_t, int, const mdb_arg_t *);
388 static int smb_sid(uintptr_t, uint_t, int, const mdb_arg_t *);
389 static int smb_sid_print(uintptr_t);
390 static int smb_fssd(uintptr_t, uint_t, int, const mdb_arg_t *);
391 static int smb_dcmd_getopt(uint_t *, int, const mdb_arg_t *);
392 static int smb_dcmd_setopt(uint_t, int, mdb_arg_t *);
393 static int smb_obj_expand(uintptr_t, uint_t, const smb_exp_t *, ulong_t);
394 static int smb_obj_list(const char *, uint_t, uint_t);
395 static int smb_worker_findstack(uintptr_t);
396 static int smb_stats(uintptr_t, uint_t, int, const mdb_arg_t *);
397 
398 /*
399  * MDB module linkage information:
400  *
401  * We declare a list of structures describing our dcmds, a list of structures
402  * describing our walkers and a function named _mdb_init to return a pointer
403  * to our module information.
404  */
405 static const mdb_dcmd_t dcmds[] = {
406 	{   "smblist",
407 	    "[-seutfdwv]",
408 	    "print tree of SMB objects",
409 	    smb_dcmd_list,
410 	    smb_dcmd_list_help },
411 	{   "smbsrv",
412 	    "[-seutfdwv]",
413 	    "print smb_server information",
414 	    smb_dcmd_server },
415 	{   "smbshares",
416 	    "[-v]",
417 	    "print smb_kshare_t information",
418 	    smb_dcmd_kshare },
419 	{   "smbvfs",
420 	    "[-v]",
421 	    "print smb_vfs information",
422 	    smb_dcmd_vfs },
423 	{   "smbnode",
424 	    "?[-vps]",
425 	    "print smb_node_t information",
426 	    smb_dcmd_node,
427 	    smb_node_help },
428 	{   "smbsess",
429 	    "[-utfdwv]",
430 	    "print smb_session_t information",
431 	    smb_dcmd_session,
432 	    smb_dcmd_session_help},
433 	{   "smbreq",
434 	    ":[-v]",
435 	    "print smb_request_t information",
436 	    smb_dcmd_request },
437 	{   "smblock", ":[-v]",
438 	    "print smb_lock_t information", smb_lock },
439 	{   "smbuser",
440 	    ":[-vdftq]",
441 	    "print smb_user_t information",
442 	    smb_dcmd_user,
443 	    smb_dcmd_user_help },
444 	{   "smbtree",
445 	    ":[-vdf]",
446 	    "print smb_tree_t information",
447 	    smb_dcmd_tree,
448 	    smb_dcmd_tree_help },
449 	{   "smbodir",
450 	    ":[-v]",
451 	    "print smb_odir_t information",
452 	    smb_dcmd_odir },
453 	{   "smbofile",
454 	    "[-v]",
455 	    "print smb_file_t information",
456 	    smb_dcmd_ofile },
457 	{   "smboplock", NULL,
458 	    "print smb_oplock_t information", smb_oplock },
459 	{   "smboplockgrant", NULL,
460 	    "print smb_oplock_grant_t information", smb_oplock_grant },
461 	{   "smbstat", NULL,
462 	    "print all smb dispatched requests statistics",
463 	    smb_stats },
464 	{   "smbace", "[-v]",
465 	    "print smb_ace_t information", smb_ace },
466 	{   "smbacl", "[-v]",
467 	    "print smb_acl_t information", smb_acl },
468 	{   "smbsid", "[-v]",
469 	    "print smb_sid_t information", smb_sid },
470 	{   "smbsd", "[-v]",
471 	    "print smb_sd_t information", smb_sd },
472 	{   "smbfssd", "[-v]",
473 	    "print smb_fssd_t information", smb_fssd },
474 	{ NULL }
475 };
476 
477 static const mdb_walker_t walkers[] = {
478 	{   "smbnode_walker",
479 	    "walk list of smb_node_t structures",
480 	    smb_node_walk_init,
481 	    smb_node_walk_step,
482 	    NULL,
483 	    NULL },
484 	{   "smbvfs_walker",
485 	    "walk list of smb_vfs_t structures",
486 	    smb_vfs_walk_init,
487 	    smb_vfs_walk_step,
488 	    NULL,
489 	    NULL },
490 	{   "smbace_walker",
491 	    "walk list of smb_ace_t structures",
492 	    smb_ace_walk_init,
493 	    smb_ace_walk_step,
494 	    NULL,
495 	    NULL },
496 	{ NULL }
497 };
498 
499 static const mdb_modinfo_t modinfo = {
500 	MDB_API_VERSION, dcmds, walkers
501 };
502 
503 const mdb_modinfo_t *
504 _mdb_init(void)
505 {
506 	return (&modinfo);
507 }
508 
509 /*
510  * *****************************************************************************
511  * ****************************** Top level DCMD *******************************
512  * *****************************************************************************
513  */
514 
515 static void
516 smb_dcmd_list_help(void)
517 {
518 	mdb_printf(
519 	    "Displays the list of objects using an indented tree format.\n"
520 	    "If no option is specified the entire tree is displayed\n\n");
521 	(void) mdb_dec_indent(2);
522 	mdb_printf("%<b>OPTIONS%</b>\n");
523 	(void) mdb_inc_indent(2);
524 	mdb_printf(
525 	    "-v\tDisplay verbose information\n"
526 	    "-s\tDisplay the list of servers\n"
527 	    "-e\tDisplay the list of sessions\n"
528 	    "-r\tDisplay the list of smb requests\n"
529 	    "-u\tDisplay the list of users\n"
530 	    "-t\tDisplay the list of trees\n"
531 	    "-f\tDisplay the list of open files\n"
532 	    "-d\tDisplay the list of open searches\n");
533 }
534 
535 /*
536  * ::smblist
537  *
538  * This function lists the objects specified on the command line. If no object
539  * is specified the entire tree (server through ofile and odir) is displayed.
540  *
541  */
542 /*ARGSUSED*/
543 static int
544 smb_dcmd_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
545 {
546 	GElf_Sym	sym;
547 	uint_t		opts = 0;
548 	int		new_argc;
549 	mdb_arg_t	new_argv[SMB_MDB_MAX_OPTS];
550 
551 	if (smb_dcmd_getopt(&opts, argc, argv))
552 		return (DCMD_USAGE);
553 
554 	if (!(opts & ~(SMB_OPT_WALK | SMB_OPT_VERBOSE)))
555 		opts |= SMB_OPT_ALL_OBJ;
556 
557 	opts |= SMB_OPT_WALK;
558 
559 	new_argc = smb_dcmd_setopt(opts, SMB_MDB_MAX_OPTS, new_argv);
560 
561 	if (mdb_lookup_by_name("smb_servers", &sym) == -1) {
562 		mdb_warn("failed to find symbol smb_servers");
563 		return (DCMD_ERR);
564 	}
565 
566 	addr = (uintptr_t)sym.st_value + offsetof(smb_llist_t, ll_list);
567 
568 	if (mdb_pwalk_dcmd("list", "smbsrv", new_argc, new_argv, addr))
569 		return (DCMD_ERR);
570 	return (DCMD_OK);
571 }
572 
573 /*
574  * *****************************************************************************
575  * ***************************** smb_server_t **********************************
576  * *****************************************************************************
577  */
578 
579 static const char *smb_server_state[SMB_SERVER_STATE_SENTINEL] =
580 {
581 	"CREATED",
582 	"CONFIGURED",
583 	"RUNNING",
584 	"STOPPING",
585 	"DELETING"
586 };
587 
588 /*
589  * List of objects that can be expanded under a server structure.
590  */
591 static const smb_exp_t smb_server_exp[] =
592 {
593 	{ SMB_OPT_ALL_OBJ,
594 	    offsetof(smb_server_t, sv_nbt_daemon.ld_session_list.ll_list),
595 	    "smbsess", "smb_session"},
596 	{ SMB_OPT_ALL_OBJ,
597 	    offsetof(smb_server_t, sv_tcp_daemon.ld_session_list.ll_list),
598 	    "smbsess", "smb_session"},
599 	{ 0, 0, NULL, NULL }
600 };
601 
602 /*
603  * ::smbsrv
604  *
605  * smbsrv dcmd - Print out smb_server structures.
606  */
607 /*ARGSUSED*/
608 static int
609 smb_dcmd_server(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
610 {
611 	uint_t		opts;
612 	ulong_t		indent = 0;
613 
614 	if (smb_dcmd_getopt(&opts, argc, argv))
615 		return (DCMD_USAGE);
616 
617 	if (!(flags & DCMD_ADDRSPEC))
618 		return (smb_obj_list("smb_server", opts | SMB_OPT_SERVER,
619 		    flags));
620 
621 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_SERVER)) ||
622 	    !(opts & SMB_OPT_WALK)) {
623 		smb_server_t	*sv;
624 		const char	*state;
625 
626 		sv = mdb_alloc(sizeof (smb_server_t), UM_SLEEP | UM_GC);
627 		if (mdb_vread(sv, sizeof (smb_server_t), addr) == -1) {
628 			mdb_warn("failed to read smb_server at %p", addr);
629 			return (DCMD_ERR);
630 		}
631 
632 		indent = SMB_DCMD_INDENT;
633 
634 		if (opts & SMB_OPT_VERBOSE) {
635 			mdb_arg_t	argv;
636 
637 			argv.a_type = MDB_TYPE_STRING;
638 			argv.a_un.a_str = "smb_server_t";
639 			if (mdb_call_dcmd("print", addr, flags, 1, &argv))
640 				return (DCMD_ERR);
641 		} else {
642 			if (DCMD_HDRSPEC(flags))
643 				mdb_printf(
644 				    "%<b>%<u>%-?s% "
645 				    "%-4s% "
646 				    "%-32s% "
647 				    "%</u>%</b>\n",
648 				    "SERVER", "ZONE", "STATE");
649 
650 			if (sv->sv_state >= SMB_SERVER_STATE_SENTINEL)
651 				state = "UNKNOWN";
652 			else
653 				state = smb_server_state[sv->sv_state];
654 
655 			mdb_printf("%-?p %-4d %-32s \n",
656 			    addr, sv->sv_zid, state);
657 		}
658 	}
659 	if (smb_obj_expand(addr, opts, smb_server_exp, indent))
660 		return (DCMD_ERR);
661 	return (DCMD_OK);
662 }
663 
664 /*
665  * *****************************************************************************
666  * ***************************** smb_session_t *********************************
667  * *****************************************************************************
668  */
669 
670 static const char *smb_session_state[SMB_SESSION_STATE_SENTINEL] =
671 {
672 	"INITIALIZED",
673 	"DISCONNECTED",
674 	"CONNECTED",
675 	"ESTABLISHED",
676 	"NEGOTIATED",
677 	"OPLOCK_BREAKING",
678 	"WRITE_RAW_ACTIVE",
679 	"READ_RAW_ACTIVE",
680 	"TERMINATED"
681 };
682 
683 /*
684  * List of objects that can be expanded under a session structure.
685  */
686 static const smb_exp_t smb_session_exp[] =
687 {
688 	{ SMB_OPT_REQUEST,
689 	    offsetof(smb_session_t, s_req_list.sl_list),
690 	    "smbreq", "smb_request"},
691 	{ SMB_OPT_USER,
692 	    offsetof(smb_session_t, s_user_list.ll_list),
693 	    "smbuser", "smb_user"},
694 	{ SMB_OPT_TREE | SMB_OPT_OFILE | SMB_OPT_ODIR,
695 	    offsetof(smb_session_t, s_tree_list.ll_list),
696 	    "smbtree", "smb_tree"},
697 	{ 0, 0, NULL, NULL}
698 };
699 
700 static void
701 smb_dcmd_session_help(void)
702 {
703 	mdb_printf(
704 	    "Display the contents of smb_session_t, with optional"
705 	    " filtering.\n\n");
706 	(void) mdb_dec_indent(2);
707 	mdb_printf("%<b>OPTIONS%</b>\n");
708 	(void) mdb_inc_indent(2);
709 	mdb_printf(
710 	    "-v\tDisplay verbose smb_session information\n"
711 	    "-r\tDisplay the list of smb requests attached\n"
712 	    "-u\tDisplay the list of users attached\n");
713 }
714 
715 /*
716  * ::smbsess
717  *
718  * smbsess dcmd - Print out the smb_session structure.
719  */
720 static int
721 smb_dcmd_session(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
722 {
723 	uint_t		opts;
724 	ulong_t		indent = 0;
725 
726 	if (smb_dcmd_getopt(&opts, argc, argv))
727 		return (DCMD_USAGE);
728 
729 	if (!(flags & DCMD_ADDRSPEC)) {
730 		opts |= SMB_OPT_SESSION;
731 		opts &= ~SMB_OPT_SERVER;
732 		return (smb_obj_list("smb_session", opts, flags));
733 	}
734 
735 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_SESSION)) ||
736 	    !(opts & SMB_OPT_WALK)) {
737 		smb_session_t	*se;
738 		const char	*state;
739 
740 		indent = SMB_DCMD_INDENT;
741 
742 		se = mdb_alloc(sizeof (*se), UM_SLEEP | UM_GC);
743 		if (mdb_vread(se, sizeof (*se), addr) == -1) {
744 			mdb_warn("failed to read smb_session at %p", addr);
745 			return (DCMD_ERR);
746 		}
747 		if (se->s_state >= SMB_SESSION_STATE_SENTINEL)
748 			state = "INVALID";
749 		else
750 			state = smb_session_state[se->s_state];
751 
752 		if (opts & SMB_OPT_VERBOSE) {
753 			mdb_printf("%<b>%<u>SMB session information "
754 			    "(%p): %</u>%</b>\n", addr);
755 			switch (se->ipaddr.a_family) {
756 			case AF_INET:
757 				mdb_printf("Client IP address: %I\n",
758 				    se->ipaddr.a_ipv4);
759 				mdb_printf("Local IP Address: %I\n",
760 				    se->local_ipaddr.a_ipv4);
761 				break;
762 			case AF_INET6:
763 				mdb_printf("Client IP address: %N\n",
764 				    &(se->ipaddr.a_ipv6));
765 				mdb_printf("Local IP Address: %N\n",
766 				    &(se->local_ipaddr.a_ipv6));
767 				break;
768 			default:
769 				mdb_printf("Client IP address: unknown\n");
770 				mdb_printf("Local IP Address: unknown\n");
771 			}
772 			mdb_printf("Session KID: %u\n", se->s_kid);
773 			mdb_printf("Workstation Name: %s\n",
774 			    se->workstation);
775 			mdb_printf("Session state: %u (%s)\n", se->s_state,
776 			    state);
777 			mdb_printf("Number of Users: %u\n",
778 			    se->s_user_list.ll_count);
779 			mdb_printf("Number of Trees: %u\n", se->s_tree_cnt);
780 			mdb_printf("Number of Files: %u\n", se->s_file_cnt);
781 			mdb_printf("Number of Shares: %u\n", se->s_dir_cnt);
782 			mdb_printf("Number of active Transact.: %u\n\n",
783 			    se->s_xa_list.ll_count);
784 		} else {
785 			char	cipaddr[INET6_ADDRSTRLEN];
786 			char	lipaddr[INET6_ADDRSTRLEN];
787 			int	ipaddrstrlen;
788 
789 			switch (se->ipaddr.a_family) {
790 			case AF_INET:
791 				ipaddrstrlen = INET_ADDRSTRLEN;
792 				(void) mdb_snprintf(cipaddr, sizeof (cipaddr),
793 				    "%I", se->ipaddr.a_ipv4);
794 				(void) mdb_snprintf(lipaddr, sizeof (lipaddr),
795 				    "%I", se->local_ipaddr.a_ipv4);
796 				break;
797 			case AF_INET6:
798 				ipaddrstrlen = INET6_ADDRSTRLEN;
799 				(void) mdb_snprintf(cipaddr, sizeof (cipaddr),
800 				    "%N", &(se->ipaddr.a_ipv6));
801 				(void) mdb_snprintf(lipaddr, sizeof (lipaddr),
802 				    "%N", &(se->local_ipaddr.a_ipv6));
803 				break;
804 			default:
805 				ipaddrstrlen = INET_ADDRSTRLEN;
806 				(void) mdb_snprintf(cipaddr, sizeof (cipaddr),
807 				    "unknown");
808 				(void) mdb_snprintf(lipaddr, sizeof (lipaddr),
809 				    "unknown");
810 			}
811 
812 			if (DCMD_HDRSPEC(flags)) {
813 				mdb_printf(
814 				    "%<b>%<u>%-?s %-*s %-*s %-16s%</u>%</b>\n",
815 				    "SESSION", ipaddrstrlen, "CLIENT_IP_ADDR",
816 				    ipaddrstrlen, "LOCAL_IP_ADDR", "STATE");
817 			}
818 			mdb_printf("%-?p %-*s %-*s %s\n", addr, ipaddrstrlen,
819 			    cipaddr, ipaddrstrlen, lipaddr, state);
820 		}
821 	}
822 	if (smb_obj_expand(addr, opts, smb_session_exp, indent))
823 		return (DCMD_ERR);
824 	return (DCMD_OK);
825 }
826 
827 /*
828  * *****************************************************************************
829  * **************************** smb_request_t **********************************
830  * *****************************************************************************
831  */
832 
833 static const char *smb_request_state[SMB_REQ_STATE_SENTINEL] =
834 {
835 	"FREE",
836 	"INITIALIZING",
837 	"SUBMITTED",
838 	"ACTIVE",
839 	"WAITING_EVENT",
840 	"EVENT_OCCURRED",
841 	"WAITING_LOCK",
842 	"COMPLETED",
843 	"CANCELED",
844 	"CLEANED_UP"
845 };
846 
847 #define	SMB_REQUEST_BANNER	\
848 	"%<b>%<u>%-?s %-?s %-14s %-14s %-16s %-32s%</u>%</b>\n"
849 #define	SMB_REQUEST_FORMAT	\
850 	"%-?p %-?p %-14lld %-14lld %-16s %s\n"
851 
852 static int
853 smb_dcmd_request(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
854 {
855 	uint_t		opts;
856 
857 	if (smb_dcmd_getopt(&opts, argc, argv))
858 		return (DCMD_USAGE);
859 
860 	if (!(flags & DCMD_ADDRSPEC)) {
861 		opts |= SMB_OPT_REQUEST;
862 		opts &= ~(SMB_OPT_SERVER | SMB_OPT_SESSION | SMB_OPT_USER);
863 		return (smb_obj_list("smb_request", opts, flags));
864 	}
865 
866 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_REQUEST)) ||
867 	    !(opts & SMB_OPT_WALK)) {
868 		smb_request_t	*sr;
869 		const char	*state;
870 		uint64_t	waiting;
871 		uint64_t	running;
872 
873 		sr = mdb_alloc(sizeof (*sr), UM_SLEEP | UM_GC);
874 		if (mdb_vread(sr, sizeof (*sr), addr) == -1) {
875 			mdb_warn("failed to read smb_request at %p", addr);
876 			return (DCMD_ERR);
877 		}
878 		if (sr->sr_magic != SMB_REQ_MAGIC) {
879 			mdb_warn("not an smb_request_t (%p)>", addr);
880 			return (DCMD_ERR);
881 		}
882 		waiting = 0;
883 		running = 0;
884 		if (sr->sr_time_submitted != 0) {
885 			if (sr->sr_time_active != 0) {
886 				waiting = sr->sr_time_active -
887 				    sr->sr_time_submitted;
888 				running = mdb_gethrtime() -
889 				    sr->sr_time_active;
890 			} else {
891 				waiting = mdb_gethrtime() -
892 				    sr->sr_time_submitted;
893 			}
894 		}
895 		waiting /= NANOSEC;
896 		running /= NANOSEC;
897 
898 		if (sr->sr_state >= SMB_REQ_STATE_SENTINEL)
899 			state = "INVALID";
900 		else
901 			state = smb_request_state[sr->sr_state];
902 
903 		if (opts & SMB_OPT_VERBOSE) {
904 			mdb_printf(
905 			    "%</b>%</u>SMB request information (%p):"
906 			    "%</u>%</b>\n\n", addr);
907 
908 			mdb_printf(
909 			    "first SMB COM: %u (%s)\n"
910 			    "current SMB COM: %u (%s)\n"
911 			    "state: %u (%s)\n"
912 			    "TID(tree): %u (%p)\n"
913 			    "UID(user): %u (%p)\n"
914 			    "FID(file): %u (%p)\n"
915 			    "PID: %u\n"
916 			    "MID: %u\n\n"
917 			    "waiting time: %lld\n"
918 			    "running time: %lld\n",
919 			    sr->first_smb_com,
920 			    smb_com[sr->first_smb_com].smb_com,
921 			    sr->smb_com,
922 			    smb_com[sr->smb_com].smb_com,
923 			    sr->sr_state, state,
924 			    sr->smb_tid, sr->tid_tree,
925 			    sr->smb_uid, sr->uid_user,
926 			    sr->smb_fid, sr->fid_ofile,
927 			    sr->smb_pid,
928 			    sr->smb_mid,
929 			    waiting,
930 			    running);
931 
932 			smb_worker_findstack((uintptr_t)sr->sr_worker);
933 		} else {
934 			if (DCMD_HDRSPEC(flags))
935 				mdb_printf(
936 				    SMB_REQUEST_BANNER,
937 				    "ADDR",
938 				    "WORKER",
939 				    "WAITING(s)",
940 				    "RUNNING(s)",
941 				    "STATE",
942 				    "COMMAND");
943 
944 			mdb_printf(SMB_REQUEST_FORMAT,
945 			    addr,
946 			    sr->sr_worker,
947 			    waiting,
948 			    running,
949 			    state,
950 			    smb_com[sr->smb_com].smb_com);
951 		}
952 	}
953 	return (DCMD_OK);
954 }
955 
956 /*
957  * *****************************************************************************
958  * ****************************** smb_user_t ***********************************
959  * *****************************************************************************
960  */
961 
962 static const char *smb_user_state[SMB_USER_STATE_SENTINEL] =
963 {
964 	"LOGGED_IN",
965 	"LOGGING_OFF",
966 	"LOGGED_OFF"
967 };
968 
969 static void
970 smb_dcmd_user_help(void)
971 {
972 	mdb_printf(
973 	    "Display the contents of smb_user_t, with optional filtering.\n\n");
974 	(void) mdb_dec_indent(2);
975 	mdb_printf("%<b>OPTIONS%</b>\n");
976 	(void) mdb_inc_indent(2);
977 	mdb_printf(
978 	    "-v\tDisplay verbose smb_user information\n");
979 }
980 
981 static int
982 smb_dcmd_user(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
983 {
984 	uint_t		opts;
985 
986 	if (smb_dcmd_getopt(&opts, argc, argv))
987 		return (DCMD_USAGE);
988 
989 	if (!(flags & DCMD_ADDRSPEC)) {
990 		opts |= SMB_OPT_USER;
991 		opts &= ~(SMB_OPT_SERVER | SMB_OPT_SESSION | SMB_OPT_REQUEST);
992 		return (smb_obj_list("smb_user", opts, flags));
993 	}
994 
995 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_USER)) ||
996 	    !(opts & SMB_OPT_WALK)) {
997 		smb_user_t	*user;
998 		char		*account;
999 
1000 		user = mdb_alloc(sizeof (*user), UM_SLEEP | UM_GC);
1001 		if (mdb_vread(user, sizeof (*user), addr) == -1) {
1002 			mdb_warn("failed to read smb_user at %p", addr);
1003 			return (DCMD_ERR);
1004 		}
1005 		account = mdb_zalloc(user->u_domain_len + user->u_name_len + 2,
1006 		    UM_SLEEP | UM_GC);
1007 
1008 		if (user->u_domain_len)
1009 			(void) mdb_vread(account, user->u_domain_len,
1010 			    (uintptr_t)user->u_domain);
1011 
1012 		strcat(account, "\\");
1013 
1014 		if (user->u_name_len)
1015 			(void) mdb_vread(account + strlen(account),
1016 			    user->u_name_len, (uintptr_t)user->u_name);
1017 
1018 		if (opts & SMB_OPT_VERBOSE) {
1019 			const char	*state;
1020 
1021 			if (user->u_state >= SMB_USER_STATE_SENTINEL)
1022 				state = "INVALID";
1023 			else
1024 				state = smb_user_state[user->u_state];
1025 
1026 			mdb_printf("%<b>%<u>SMB user information (%p):"
1027 			    "%</u>%</b>\n", addr);
1028 			mdb_printf("UID: %u\n", user->u_uid);
1029 			mdb_printf("State: %d (%s)\n", user->u_state, state);
1030 			mdb_printf("Flags: 0x%08x\n", user->u_flags);
1031 			mdb_printf("Privileges: 0x%08x\n", user->u_privileges);
1032 			mdb_printf("Credential: %p\n", user->u_cred);
1033 			mdb_printf("Reference Count: %d\n", user->u_refcnt);
1034 			mdb_printf("User Account: %s\n\n", account);
1035 		} else {
1036 			if (DCMD_HDRSPEC(flags))
1037 				mdb_printf(
1038 				    "%<b>%<u>%?-s "
1039 				    "%-5s "
1040 				    "%-32s%</u>%</b>\n",
1041 				    "USER", "UID", "ACCOUNT");
1042 
1043 			mdb_printf("%-?p %-5u %-32s\n", addr, user->u_uid,
1044 			    account);
1045 		}
1046 	}
1047 	return (DCMD_OK);
1048 }
1049 
1050 /*
1051  * *****************************************************************************
1052  * ****************************** smb_tree_t ***********************************
1053  * *****************************************************************************
1054  */
1055 
1056 static const char *smb_tree_state[SMB_TREE_STATE_SENTINEL] =
1057 {
1058 	"CONNECTED",
1059 	"DISCONNECTING",
1060 	"DISCONNECTED"
1061 };
1062 
1063 /*
1064  * List of objects that can be expanded under a tree structure.
1065  */
1066 static const smb_exp_t smb_tree_exp[] =
1067 {
1068 	{ SMB_OPT_OFILE,
1069 	    offsetof(smb_tree_t, t_ofile_list.ll_list),
1070 	    "smbofile", "smb_ofile"},
1071 	{ SMB_OPT_ODIR,
1072 	    offsetof(smb_tree_t, t_odir_list.ll_list),
1073 	    "smbodir", "smb_odir"},
1074 	{ 0, 0, NULL, NULL}
1075 };
1076 
1077 static void
1078 smb_dcmd_tree_help(void)
1079 {
1080 	mdb_printf(
1081 	    "Display the contents of smb_tree_t, with optional filtering.\n\n");
1082 	(void) mdb_dec_indent(2);
1083 	mdb_printf("%<b>OPTIONS%</b>\n");
1084 	(void) mdb_inc_indent(2);
1085 	mdb_printf(
1086 	    "-v\tDisplay verbose smb_tree information\n"
1087 	    "-d\tDisplay the list of smb_odirs attached\n"
1088 	    "-f\tDisplay the list of smb_ofiles attached\n");
1089 }
1090 
1091 static int
1092 smb_dcmd_tree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1093 {
1094 	uint_t		opts;
1095 	ulong_t		indent = 0;
1096 
1097 	if (smb_dcmd_getopt(&opts, argc, argv))
1098 		return (DCMD_USAGE);
1099 
1100 	if (!(flags & DCMD_ADDRSPEC)) {
1101 		opts |= SMB_OPT_TREE;
1102 		opts &= ~(SMB_OPT_SERVER | SMB_OPT_SESSION | SMB_OPT_REQUEST |
1103 		    SMB_OPT_USER);
1104 		return (smb_obj_list("smb_tree", opts, flags));
1105 	}
1106 
1107 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_TREE)) ||
1108 	    !(opts & SMB_OPT_WALK)) {
1109 		smb_tree_t	*tree;
1110 
1111 		indent = SMB_DCMD_INDENT;
1112 
1113 		tree = mdb_alloc(sizeof (*tree), UM_SLEEP | UM_GC);
1114 		if (mdb_vread(tree, sizeof (*tree), addr) == -1) {
1115 			mdb_warn("failed to read smb_tree at %p", addr);
1116 			return (DCMD_ERR);
1117 		}
1118 		if (opts & SMB_OPT_VERBOSE) {
1119 			const char	*state;
1120 
1121 			if (tree->t_state >= SMB_TREE_STATE_SENTINEL)
1122 				state = "INVALID";
1123 			else
1124 				state = smb_tree_state[tree->t_state];
1125 
1126 			mdb_printf("%<b>%<u>SMB tree information (%p):"
1127 			    "%</u>%</b>\n\n", addr);
1128 			mdb_printf("TID: %04x\n", tree->t_tid);
1129 			mdb_printf("State: %d (%s)\n", tree->t_state, state);
1130 			mdb_printf("Share: %s\n", tree->t_sharename);
1131 			mdb_printf("Resource: %s\n", tree->t_resource);
1132 			mdb_printf("Type: %s\n", tree->t_typename);
1133 			mdb_printf("Volume: %s\n", tree->t_volume);
1134 			mdb_printf("Umask: %04x\n", tree->t_umask);
1135 			mdb_printf("Flags: %08x\n", tree->t_flags);
1136 			mdb_printf("SMB Node: %llx\n", tree->t_snode);
1137 			mdb_printf("Reference Count: %d\n\n", tree->t_refcnt);
1138 		} else {
1139 			if (DCMD_HDRSPEC(flags))
1140 				mdb_printf(
1141 				    "%<b>%<u>%-?s %-5s %-16s %-32s%</u>%</b>\n",
1142 				    "TREE", "TID", "SHARE NAME", "RESOURCE");
1143 
1144 			mdb_printf("%-?p %-5u %-16s %-32s\n", addr,
1145 			    tree->t_tid, tree->t_sharename, tree->t_resource);
1146 		}
1147 	}
1148 	if (smb_obj_expand(addr, opts, smb_tree_exp, indent))
1149 		return (DCMD_ERR);
1150 	return (DCMD_OK);
1151 }
1152 
1153 /*
1154  * *****************************************************************************
1155  * ****************************** smb_odir_t ***********************************
1156  * *****************************************************************************
1157  */
1158 
1159 static const char *smb_odir_state[SMB_ODIR_STATE_SENTINEL] =
1160 {
1161 	"OPEN",
1162 	"IN_USE",
1163 	"CLOSING",
1164 	"CLOSED"
1165 };
1166 
1167 static int
1168 smb_dcmd_odir(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1169 {
1170 	uint_t		opts;
1171 
1172 	if (smb_dcmd_getopt(&opts, argc, argv))
1173 		return (DCMD_USAGE);
1174 
1175 	if (!(flags & DCMD_ADDRSPEC)) {
1176 		opts |= SMB_OPT_ODIR;
1177 		opts &= ~(SMB_OPT_SERVER | SMB_OPT_SESSION | SMB_OPT_REQUEST |
1178 		    SMB_OPT_USER | SMB_OPT_TREE | SMB_OPT_OFILE);
1179 		return (smb_obj_list("smb_odir", opts, flags));
1180 	}
1181 
1182 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_ODIR)) ||
1183 	    !(opts & SMB_OPT_WALK)) {
1184 		smb_odir_t	*od;
1185 
1186 		od = mdb_alloc(sizeof (*od), UM_SLEEP | UM_GC);
1187 		if (mdb_vread(od, sizeof (*od), addr) == -1) {
1188 			mdb_warn("failed to read smb_odir at %p", addr);
1189 			return (DCMD_ERR);
1190 		}
1191 		if (opts & SMB_OPT_VERBOSE) {
1192 			const char	*state;
1193 
1194 			if (od->d_state >= SMB_ODIR_STATE_SENTINEL)
1195 				state = "INVALID";
1196 			else
1197 				state = smb_odir_state[od->d_state];
1198 
1199 			mdb_printf(
1200 			    "%<b>%<u>SMB odir information (%p):%</u>%</b>\n\n",
1201 			    addr);
1202 			mdb_printf("State: %d (%s)\n", od->d_state, state);
1203 			mdb_printf("SID: %u\n", od->d_odid);
1204 			mdb_printf("User: %p\n", od->d_user);
1205 			mdb_printf("Tree: %p\n", od->d_tree);
1206 			mdb_printf("Reference Count: %d\n", od->d_refcnt);
1207 			mdb_printf("Pattern: %s\n", od->d_pattern);
1208 			mdb_printf("SMB Node: %p\n\n", od->d_dnode);
1209 		} else {
1210 			if (DCMD_HDRSPEC(flags))
1211 				mdb_printf(
1212 				    "%<b>%<u>%-?s "
1213 				    "%-5s "
1214 				    "%-?s "
1215 				    "%-16s%</u>%</b>\n",
1216 				    "ODIR", "SID", "VNODE", "PATTERN");
1217 
1218 			mdb_printf("%?p %-5u %-16p %s\n",
1219 			    addr, od->d_odid, od->d_dnode, od->d_pattern);
1220 		}
1221 	}
1222 	return (DCMD_OK);
1223 }
1224 
1225 /*
1226  * *****************************************************************************
1227  * ****************************** smb_ofile_t **********************************
1228  * *****************************************************************************
1229  */
1230 
1231 static const char *smb_ofile_state[SMB_OFILE_STATE_SENTINEL] =
1232 {
1233 	"OPEN",
1234 	"CLOSING",
1235 	"CLOSED"
1236 };
1237 
1238 static int
1239 smb_dcmd_ofile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1240 {
1241 	uint_t		opts;
1242 
1243 	if (smb_dcmd_getopt(&opts, argc, argv))
1244 		return (DCMD_USAGE);
1245 
1246 	if (!(flags & DCMD_ADDRSPEC)) {
1247 		opts |= SMB_OPT_OFILE;
1248 		opts &= ~(SMB_OPT_SERVER | SMB_OPT_SESSION | SMB_OPT_REQUEST |
1249 		    SMB_OPT_USER | SMB_OPT_TREE | SMB_OPT_ODIR);
1250 		return (smb_obj_list("smb_ofile", opts, flags));
1251 	}
1252 
1253 	if (((opts & SMB_OPT_WALK) && (opts & SMB_OPT_OFILE)) ||
1254 	    !(opts & SMB_OPT_WALK)) {
1255 		smb_ofile_t	*of;
1256 
1257 		of = mdb_alloc(sizeof (*of), UM_SLEEP | UM_GC);
1258 		if (mdb_vread(of, sizeof (*of), addr) == -1) {
1259 			mdb_warn("failed to read smb_ofile at %p", addr);
1260 			return (DCMD_ERR);
1261 		}
1262 		if (opts & SMB_OPT_VERBOSE) {
1263 			const char	*state;
1264 
1265 			if (of->f_state >= SMB_OFILE_STATE_SENTINEL)
1266 				state = "INVALID";
1267 			else
1268 				state = smb_ofile_state[of->f_state];
1269 
1270 			mdb_printf(
1271 			    "%<b>%<u>SMB ofile information (%p):%</u>%</b>\n\n",
1272 			    addr);
1273 			mdb_printf("FID: %u\n", of->f_fid);
1274 			mdb_printf("State: %d (%s)\n", of->f_state, state);
1275 			mdb_printf("SMB Node: %p\n", of->f_node);
1276 			mdb_printf("LLF Offset: 0x%llx (%s)\n",
1277 			    of->f_llf_pos,
1278 			    ((of->f_flags & SMB_OFLAGS_LLF_POS_VALID) ?
1279 			    "Valid" : "Invalid"));
1280 			mdb_printf("Flags: 0x%08x\n", of->f_flags);
1281 			mdb_printf("User: %p\n", of->f_user);
1282 			mdb_printf("Tree: %p\n", of->f_tree);
1283 			mdb_printf("Credential: %p\n\n", of->f_cr);
1284 		} else {
1285 			if (DCMD_HDRSPEC(flags))
1286 				mdb_printf(
1287 				    "%<b>%<u>%-?s "
1288 				    "%-5s "
1289 				    "%-?s "
1290 				    "%-?s%</u>%</b>\n",
1291 				    "OFILE", "FID", "SMB NODE", "CRED");
1292 
1293 			mdb_printf("%?p %-5u %-p %p\n", addr,
1294 			    of->f_fid, of->f_node, of->f_cr);
1295 		}
1296 	}
1297 	return (DCMD_OK);
1298 }
1299 
1300 /*
1301  * *****************************************************************************
1302  * ******************************** smb_kshare_t *******************************
1303  * *****************************************************************************
1304  */
1305 
1306 static int
1307 smb_kshare_cb(uintptr_t addr, const void *data, void *arg)
1308 {
1309 	uint_t *opts = arg;
1310 	uintptr_t ta, sa;
1311 	char name[32];
1312 	char path[64];
1313 	_NOTE(ARGUNUSED(data));
1314 
1315 	if (*opts & SMB_OPT_VERBOSE) {
1316 		mdb_arg_t	argv;
1317 
1318 		argv.a_type = MDB_TYPE_STRING;
1319 		argv.a_un.a_str = "smb_kshare_t";
1320 		/* Don't fail the walk if this fails. */
1321 		mdb_call_dcmd("print", addr, 0, 1, &argv);
1322 	} else {
1323 		/*
1324 		 * Summary line for a kshare
1325 		 * Don't fail the walk if any of these fail.
1326 		 */
1327 		ta = addr + OFFSETOF(smb_kshare_t, shr_name);
1328 		if (mdb_vread(&sa, sizeof (sa), ta) < 0 ||
1329 		    mdb_readstr(name, sizeof (name), sa) <= 0)
1330 			strcpy(name, "?");
1331 
1332 		ta = addr + OFFSETOF(smb_kshare_t, shr_path);
1333 		if (mdb_vread(&sa, sizeof (sa), ta) < 0 ||
1334 		    mdb_readstr(path, sizeof (path), sa) <= 0)
1335 			strcpy(path, "?");
1336 
1337 		mdb_printf("%-?p ", addr);	/* smb_kshare_t */
1338 		mdb_printf("%-16s ", name);
1339 		mdb_printf("%-s", path);
1340 		mdb_printf("\n");
1341 	}
1342 
1343 	return (WALK_NEXT);
1344 }
1345 
1346 /*
1347  * ::smbshares
1348  *
1349  * dcmd - Print out smb_kshare structures.
1350  */
1351 /*ARGSUSED*/
1352 static int
1353 smb_dcmd_kshare(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1354 {
1355 	GElf_Sym	sym;
1356 	uint_t		opts = 0;
1357 
1358 	if (mdb_getopts(argc, argv,
1359 	    'v', MDB_OPT_SETBITS, SMB_OPT_VERBOSE, &opts,
1360 	    NULL) != argc)
1361 		return (DCMD_USAGE);
1362 
1363 	if (!(flags & DCMD_ADDRSPEC)) {
1364 		if (mdb_lookup_by_name("smb_export", &sym) == -1) {
1365 			mdb_warn("failed to find symbol smb_export");
1366 			return (DCMD_ERR);
1367 		}
1368 		addr = (uintptr_t)sym.st_value +
1369 		    OFFSETOF(smb_export_t, e_share_avl.avl_tree);
1370 	}
1371 
1372 	if (DCMD_HDRSPEC(flags)) {
1373 		mdb_printf(
1374 		    "%<b>%<u>"
1375 		    "%-?s "
1376 		    "%-16s "
1377 		    "%-s"
1378 		    "%</u>%</b>\n",
1379 		    "smb_kshare_t", "name", "path");
1380 	}
1381 
1382 	if (mdb_pwalk("genunix`avl", smb_kshare_cb, &opts, addr) == -1) {
1383 		mdb_warn("cannot walk smb_kshare avl");
1384 		return (DCMD_ERR);
1385 	}
1386 
1387 	return (DCMD_OK);
1388 }
1389 
1390 /*
1391  * *****************************************************************************
1392  * ******************************** smb_vfs_t **********************************
1393  * *****************************************************************************
1394  */
1395 
1396 /*
1397  * ::smbvfs
1398  *
1399  * smbvfs dcmd - Prints out smb_vfs structures.
1400  */
1401 /*ARGSUSED*/
1402 static int
1403 smb_dcmd_vfs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1404 {
1405 	int		verbose = FALSE;
1406 	smb_vfs_t	*sf;
1407 	vnode_t		*vn;
1408 	char		*path;
1409 
1410 	if (mdb_getopts(argc, argv,
1411 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1412 	    NULL) != argc)
1413 		return (DCMD_USAGE);
1414 
1415 	/*
1416 	 * If no smb_vfs address was specified on the command line, we can
1417 	 * print out all smb_vfs by invoking the smb_vfs walker, using
1418 	 * this dcmd itself as the callback.
1419 	 */
1420 	if (!(flags & DCMD_ADDRSPEC)) {
1421 		if (mdb_walk_dcmd("smbvfs_walker", "smbvfs",
1422 		    argc, argv) == -1) {
1423 			mdb_warn("failed to walk 'smb_vfs'");
1424 			return (DCMD_ERR);
1425 		}
1426 		return (DCMD_OK);
1427 	}
1428 
1429 	if (DCMD_HDRSPEC(flags)) {
1430 		mdb_printf(
1431 		    "%<b>%<u>"
1432 		    "%-?s "
1433 		    "%-10s "
1434 		    "%-16s "
1435 		    "%-16s"
1436 		    "%-s"
1437 		    "%</u>%</b>\n",
1438 		    "SMB_VFS", "REFCNT", "VFS", "VNODE", "ROOT");
1439 	}
1440 
1441 	sf = mdb_alloc(sizeof (*sf), UM_SLEEP | UM_GC);
1442 	if (mdb_vread(sf, sizeof (*sf), addr) == -1) {
1443 		mdb_warn("failed to read smb_vfs at %p", addr);
1444 		return (DCMD_ERR);
1445 	}
1446 
1447 	vn = mdb_alloc(sizeof (*vn), UM_SLEEP | UM_GC);
1448 	if (mdb_vread(vn, sizeof (*vn),
1449 	    (uintptr_t)sf->sv_rootvp) == -1) {
1450 		mdb_warn("failed to read vnode at %p", sf->sv_rootvp);
1451 		return (DCMD_ERR);
1452 	}
1453 
1454 	path = mdb_zalloc(MAXPATHLEN, UM_SLEEP | UM_GC);
1455 	(void) mdb_vread(path, MAXPATHLEN, (uintptr_t)vn->v_path);
1456 
1457 	mdb_printf(
1458 	    "%-?p %-10d %-?p %-?p %-s\n", addr, sf->sv_refcnt,
1459 	    sf->sv_vfsp, sf->sv_rootvp, path);
1460 
1461 	return (DCMD_OK);
1462 }
1463 
1464 /*
1465  * Initialize the smb_vfs_t walker by reading the value of smb_export
1466  * in the kernel's symbol table. Only global walk supported.
1467  */
1468 static int
1469 smb_vfs_walk_init(mdb_walk_state_t *wsp)
1470 {
1471 	GElf_Sym	sym;
1472 
1473 	if (wsp->walk_addr != NULL) {
1474 		mdb_printf("smb_vfs walk only supports global walks\n");
1475 		return (WALK_ERR);
1476 	}
1477 
1478 	if (mdb_lookup_by_name("smb_export", &sym) == -1) {
1479 		mdb_warn("failed to find 'smb_export'");
1480 		return (WALK_ERR);
1481 	}
1482 
1483 	wsp->walk_addr = (uintptr_t)sym.st_value +
1484 	    offsetof(smb_export_t, e_vfs_list) + offsetof(smb_llist_t, ll_list);
1485 
1486 	if (mdb_layered_walk("list", wsp) == -1) {
1487 		mdb_warn("failed to walk list of VFS");
1488 		return (WALK_ERR);
1489 	}
1490 
1491 	return (WALK_NEXT);
1492 }
1493 
1494 static int
1495 smb_vfs_walk_step(mdb_walk_state_t *wsp)
1496 {
1497 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1498 	    wsp->walk_cbdata));
1499 }
1500 
1501 /*
1502  * *****************************************************************************
1503  * ******************************* smb_node_t **********************************
1504  * *****************************************************************************
1505  */
1506 
1507 static void
1508 smb_node_help(void)
1509 {
1510 	mdb_printf(
1511 	    "Display the contents of smb_node_t, with optional filtering.\n\n");
1512 	(void) mdb_dec_indent(2);
1513 	mdb_printf("%<b>OPTIONS%</b>\n");
1514 	(void) mdb_inc_indent(2);
1515 	mdb_printf(
1516 	    "-v\tDisplay verbose smb_node information\n"
1517 	    "-p\tDisplay the full path of the vnode associated\n"
1518 	    "-s\tDisplay the stack of the last 16 calls that modified the "
1519 	    "reference\n\tcount\n");
1520 }
1521 
1522 /*
1523  * ::smbnode
1524  *
1525  * smb_node dcmd - Print out smb_node structure.
1526  */
1527 static int
1528 smb_dcmd_node(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1529 {
1530 	smb_node_t	node;
1531 	int		rc;
1532 	int		verbose = FALSE;
1533 	int		print_full_path = FALSE;
1534 	int		stack_trace = FALSE;
1535 	vnode_t		vnode;
1536 	char		od_name[MAXNAMELEN];
1537 	char		path_name[1024];
1538 	uintptr_t	list_addr, oplock_addr;
1539 
1540 	if (mdb_getopts(argc, argv,
1541 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1542 	    'p', MDB_OPT_SETBITS, TRUE, &print_full_path,
1543 	    's', MDB_OPT_SETBITS, TRUE, &stack_trace,
1544 	    NULL) != argc)
1545 		return (DCMD_USAGE);
1546 
1547 	/*
1548 	 * If no smb_node address was specified on the command line, we can
1549 	 * print out all smb nodes by invoking the smb_node walker, using
1550 	 * this dcmd itself as the callback.
1551 	 */
1552 	if (!(flags & DCMD_ADDRSPEC)) {
1553 		if (mdb_walk_dcmd("smbnode_walker", "smbnode",
1554 		    argc, argv) == -1) {
1555 			mdb_warn("failed to walk 'smb_node'");
1556 			return (DCMD_ERR);
1557 		}
1558 		return (DCMD_OK);
1559 	}
1560 
1561 	/*
1562 	 * If this is the first invocation of the command, print a nice
1563 	 * header line for the output that will follow.
1564 	 */
1565 	if (DCMD_HDRSPEC(flags)) {
1566 		if (verbose) {
1567 			mdb_printf("%<b>%<u>SMB node information:%</u>%</b>\n");
1568 		} else {
1569 			mdb_printf(
1570 			    "%<b>%<u>%-?s "
1571 			    "%-?s "
1572 			    "%-18s "
1573 			    "%-6s "
1574 			    "%-6s "
1575 			    "%-8s "
1576 			    "%-6s%</u>%</b>\n",
1577 			    "ADDR", "VP", "NODE-NAME", "OFILES", "LOCKS",
1578 			    "OPLOCK", "REF");
1579 		}
1580 	}
1581 
1582 	/*
1583 	 * For each smb_node, we just need to read the smb_node_t struct, read
1584 	 * and then print out the following fields.
1585 	 */
1586 	if (mdb_vread(&node, sizeof (node), addr) == sizeof (node)) {
1587 		(void) mdb_snprintf(od_name, sizeof (od_name), "%s",
1588 		    node.od_name);
1589 		if (print_full_path) {
1590 			if (mdb_vread(&vnode, sizeof (vnode_t),
1591 			    (uintptr_t)node.vp) == sizeof (vnode_t)) {
1592 				if (mdb_readstr(path_name, sizeof (path_name),
1593 				    (uintptr_t)vnode.v_path) != 0) {
1594 					(void) mdb_snprintf(od_name,
1595 					    sizeof (od_name), "N/A");
1596 				}
1597 			}
1598 		}
1599 		if (verbose) {
1600 			mdb_printf("VP: %p\n", node.vp);
1601 			mdb_printf("Name: %s\n", od_name);
1602 			if (print_full_path)
1603 				mdb_printf("V-node Path: %s\n", path_name);
1604 			mdb_printf("Ofiles: %u\n", node.n_ofile_list.ll_count);
1605 			mdb_printf("Range Locks: %u\n",
1606 			    node.n_lock_list.ll_count);
1607 			if (node.n_lock_list.ll_count != 0) {
1608 				(void) mdb_inc_indent(SMB_DCMD_INDENT);
1609 				list_addr = addr +
1610 				    offsetof(smb_node_t, n_lock_list) +
1611 				    offsetof(smb_llist_t, ll_list);
1612 				if (mdb_pwalk_dcmd("list", "smblock", 0,
1613 				    NULL, list_addr)) {
1614 					mdb_warn("failed to walk node's active"
1615 					    " locks");
1616 				}
1617 				(void) mdb_dec_indent(SMB_DCMD_INDENT);
1618 			}
1619 			if (node.n_oplock.ol_count == 0) {
1620 				mdb_printf("Opportunistic Locks: 0\n");
1621 			} else {
1622 				oplock_addr =
1623 				    addr + offsetof(smb_node_t, n_oplock);
1624 				mdb_printf("Opportunistic Lock: %p\n",
1625 				    oplock_addr);
1626 				rc = mdb_call_dcmd("smboplock", oplock_addr,
1627 				    flags, argc, argv);
1628 				if (rc != DCMD_OK)
1629 					return (rc);
1630 			}
1631 			mdb_printf("Reference Count: %u\n\n", node.n_refcnt);
1632 		} else {
1633 			mdb_printf("%-?p %-?p %-18s %-6d %-6d %-8d %-6d ",
1634 			    addr, node.vp, od_name, node.n_ofile_list.ll_count,
1635 			    node.n_lock_list.ll_count,
1636 			    node.n_oplock.ol_count, node.n_refcnt);
1637 
1638 			if (print_full_path)
1639 				mdb_printf("\t%s\n", path_name);
1640 		}
1641 		if (stack_trace && node.n_audit_buf) {
1642 			int ctr;
1643 			smb_audit_buf_node_t *anb;
1644 
1645 			anb = mdb_alloc(sizeof (smb_audit_buf_node_t),
1646 			    UM_SLEEP | UM_GC);
1647 
1648 			if (mdb_vread(anb, sizeof (*anb),
1649 			    (uintptr_t)node.n_audit_buf) != sizeof (*anb)) {
1650 				mdb_warn("failed to read audit buffer");
1651 				return (DCMD_ERR);
1652 			}
1653 			ctr = anb->anb_max_index + 1;
1654 			anb->anb_index--;
1655 			anb->anb_index &= anb->anb_max_index;
1656 
1657 			while (ctr) {
1658 				smb_audit_record_node_t	*anr;
1659 
1660 				anr = anb->anb_records + anb->anb_index;
1661 
1662 				if (anr->anr_depth) {
1663 					char c[MDB_SYM_NAMLEN];
1664 					GElf_Sym sym;
1665 					int i;
1666 
1667 					mdb_printf("\nRefCnt: %u\t",
1668 					    anr->anr_refcnt);
1669 
1670 					for (i = 0;
1671 					    i < anr->anr_depth;
1672 					    i++) {
1673 						if (mdb_lookup_by_addr(
1674 						    anr->anr_stack[i],
1675 						    MDB_SYM_FUZZY,
1676 						    c, sizeof (c),
1677 						    &sym) == -1) {
1678 							continue;
1679 						}
1680 						mdb_printf("%s+0x%1x",
1681 						    c,
1682 						    anr->anr_stack[i] -
1683 						    (uintptr_t)sym.st_value);
1684 						++i;
1685 						break;
1686 					}
1687 
1688 					while (i < anr->anr_depth) {
1689 						if (mdb_lookup_by_addr(
1690 						    anr->anr_stack[i],
1691 						    MDB_SYM_FUZZY,
1692 						    c, sizeof (c),
1693 						    &sym) == -1) {
1694 							++i;
1695 							continue;
1696 						}
1697 						mdb_printf("\n\t\t%s+0x%1x",
1698 						    c,
1699 						    anr->anr_stack[i] -
1700 						    (uintptr_t)sym.st_value);
1701 						++i;
1702 					}
1703 					mdb_printf("\n");
1704 				}
1705 				anb->anb_index--;
1706 				anb->anb_index &= anb->anb_max_index;
1707 				ctr--;
1708 			}
1709 		}
1710 	} else {
1711 		mdb_warn("failed to read struct smb_node at %p", addr);
1712 		return (DCMD_ERR);
1713 	}
1714 
1715 	return (DCMD_OK);
1716 }
1717 
1718 /*
1719  * Initialize the smb_node_t walker by reading the value of smb_node_hash_table
1720  * in the kernel's symbol table. Only global walk supported.
1721  */
1722 static int
1723 smb_node_walk_init(mdb_walk_state_t *wsp)
1724 {
1725 	GElf_Sym	sym;
1726 	int		i;
1727 	uintptr_t	node_hash_table_addr;
1728 
1729 	if (wsp->walk_addr == NULL) {
1730 		if (mdb_lookup_by_name("smb_node_hash_table", &sym) == -1) {
1731 			mdb_warn("failed to find 'smb_node_hash_table'");
1732 			return (WALK_ERR);
1733 		}
1734 		node_hash_table_addr = (uintptr_t)sym.st_value;
1735 	} else {
1736 		mdb_printf("smb_node walk only supports global walks\n");
1737 		return (WALK_ERR);
1738 	}
1739 
1740 	for (i = 0; i < SMBND_HASH_MASK + 1; i++) {
1741 		wsp->walk_addr = node_hash_table_addr +
1742 		    (i * sizeof (smb_llist_t)) + offsetof(smb_llist_t, ll_list);
1743 		if (mdb_layered_walk("list", wsp) == -1) {
1744 			mdb_warn("failed to walk 'list'");
1745 			return (WALK_ERR);
1746 		}
1747 	}
1748 
1749 	return (WALK_NEXT);
1750 }
1751 
1752 static int
1753 smb_node_walk_step(mdb_walk_state_t *wsp)
1754 {
1755 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1756 	    wsp->walk_cbdata));
1757 }
1758 
1759 /*
1760  * *****************************************************************************
1761  * ****************************** smb_lock_t ***********************************
1762  * *****************************************************************************
1763  */
1764 
1765 static int
1766 smb_lock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1767 {
1768 	smb_lock_t	lock;
1769 	int		verbose = FALSE;
1770 	uintptr_t	list_addr;
1771 	char		*lock_type;
1772 
1773 	if (mdb_getopts(argc, argv,
1774 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1775 	    NULL) != argc)
1776 		return (DCMD_USAGE);
1777 
1778 	/*
1779 	 * An smb_lock_t address must be specified.
1780 	 */
1781 	if (!(flags & DCMD_ADDRSPEC))
1782 		return (DCMD_USAGE);
1783 
1784 	/*
1785 	 * If this is the first invocation of the command, print a nice
1786 	 * header line for the output that will follow.
1787 	 */
1788 	if (DCMD_HDRSPEC(flags)) {
1789 		if (verbose)
1790 			mdb_printf("SMB lock information:\n\n");
1791 		else
1792 			mdb_printf("%<u>%-?s %4s %16s %8s %9s%</u>\n",
1793 			    "Locks: ", "TYPE", "START", "LENGTH",
1794 			    "CONFLICTS");
1795 	}
1796 
1797 	if (mdb_vread(&lock, sizeof (lock), addr) == sizeof (lock)) {
1798 		switch (lock.l_type) {
1799 		case SMB_LOCK_TYPE_READWRITE:
1800 			lock_type = "RW";
1801 			break;
1802 		case SMB_LOCK_TYPE_READONLY:
1803 			lock_type = "RO";
1804 			break;
1805 		default:
1806 			lock_type = "N/A";
1807 			break;
1808 		}
1809 		if (verbose) {
1810 			mdb_printf("Type             :\t%s (%u)\n",
1811 			    lock_type, lock.l_type);
1812 			mdb_printf("Start            :\t%llx\n",
1813 			    lock.l_start);
1814 			mdb_printf("Length           :\t%lx\n",
1815 			    lock.l_length);
1816 			mdb_printf("Session          :\t%p\n",
1817 			    lock.l_session);
1818 			mdb_printf("File             :\t%p\n",
1819 			    lock.l_file);
1820 			mdb_printf("User ID          :\t%u\n",
1821 			    lock.l_uid);
1822 			mdb_printf("Process ID       :\t%u\n",
1823 			    lock.l_pid);
1824 			mdb_printf("Conflicts        :\t%u\n",
1825 			    lock.l_conflict_list.sl_count);
1826 			if (lock.l_conflict_list.sl_count != 0) {
1827 				(void) mdb_inc_indent(SMB_DCMD_INDENT);
1828 				list_addr = addr +
1829 				    offsetof(smb_lock_t, l_conflict_list) +
1830 				    offsetof(smb_slist_t, sl_list);
1831 				if (mdb_pwalk_dcmd("list", "smb_lock",
1832 				    0, NULL, list_addr)) {
1833 					mdb_warn("failed to walk conflict "
1834 					    "locks ");
1835 				}
1836 				(void) mdb_dec_indent(SMB_DCMD_INDENT);
1837 			}
1838 			mdb_printf("Blocked by       :\t%p\n",
1839 			    lock.l_blocked_by);
1840 			mdb_printf("Flags            :\t0x%x\n",
1841 			    lock.l_flags);
1842 			mdb_printf("\n");
1843 		} else {
1844 			mdb_printf("%?p %4s %16llx %08lx %9x", addr,
1845 			    lock_type, lock.l_start, lock.l_length,
1846 			    lock.l_conflict_list.sl_count);
1847 		}
1848 	} else {
1849 		mdb_warn("failed to read struct smb_request at %p", addr);
1850 		return (DCMD_ERR);
1851 	}
1852 
1853 	return (DCMD_OK);
1854 }
1855 
1856 /*
1857  * *****************************************************************************
1858  * ************************** smb_oplock_grant_t *******************************
1859  * *****************************************************************************
1860  */
1861 /*ARGSUSED*/
1862 static int
1863 smb_oplock_grant(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1864 {
1865 	smb_oplock_grant_t	grant;
1866 	char			 *level;
1867 
1868 	if (!(flags & DCMD_ADDRSPEC))
1869 		return (DCMD_USAGE);
1870 
1871 	/*
1872 	 * If this is the first invocation of the command, print a nice
1873 	 * header line for the output that will follow.
1874 	 */
1875 	if (DCMD_HDRSPEC(flags)) {
1876 		mdb_printf("%<u>%-16s %-10s %-16s%</u>\n",
1877 		    "Grants:", "LEVEL", "OFILE");
1878 	}
1879 
1880 	if (mdb_vread(&grant, sizeof (grant), addr) == sizeof (grant)) {
1881 		switch (grant.og_level) {
1882 		case SMB_OPLOCK_EXCLUSIVE:
1883 			level = "EXCLUSIVE";
1884 			break;
1885 		case SMB_OPLOCK_BATCH:
1886 			level = "BATCH";
1887 			break;
1888 		case SMB_OPLOCK_LEVEL_II:
1889 			level = "LEVEL_II";
1890 			break;
1891 		default:
1892 			level = "UNKNOWN";
1893 			break;
1894 		}
1895 
1896 		mdb_printf("%-16p %-10s %-16p", addr, level, grant.og_ofile);
1897 	}
1898 	return (DCMD_OK);
1899 }
1900 
1901 /*
1902  * *****************************************************************************
1903  * ***************************** smb_oplock_t **********************************
1904  * *****************************************************************************
1905  */
1906 /*ARGSUSED*/
1907 static int
1908 smb_oplock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1909 {
1910 	smb_oplock_t	oplock;
1911 	uintptr_t	list_addr;
1912 
1913 	if (!(flags & DCMD_ADDRSPEC))
1914 		return (DCMD_USAGE);
1915 
1916 	if (mdb_vread(&oplock, sizeof (oplock), addr) != sizeof (oplock)) {
1917 		mdb_warn("failed to read struct smb_oplock at %p", addr);
1918 		return (DCMD_ERR);
1919 	}
1920 
1921 	if (oplock.ol_count == 0)
1922 		return (DCMD_OK);
1923 
1924 	(void) mdb_inc_indent(SMB_DCMD_INDENT);
1925 	switch (oplock.ol_break) {
1926 	case SMB_OPLOCK_BREAK_TO_NONE:
1927 		mdb_printf("Break Pending: BREAK_TO_NONE\n");
1928 		break;
1929 	case SMB_OPLOCK_BREAK_TO_LEVEL_II:
1930 		mdb_printf(
1931 		    "Break Pending: BREAK_TO_LEVEL_II\n");
1932 		break;
1933 	default:
1934 		break;
1935 	}
1936 
1937 	list_addr = addr + offsetof(smb_oplock_t, ol_grants);
1938 
1939 	if (mdb_pwalk_dcmd("list", "smboplockgrant",
1940 	    argc, argv, list_addr)) {
1941 		mdb_warn("failed to walk oplock grants");
1942 	}
1943 
1944 	(void) mdb_dec_indent(SMB_DCMD_INDENT);
1945 
1946 	return (DCMD_OK);
1947 }
1948 
1949 /*
1950  * ::smbstat
1951  *
1952  * Prints SMB requests statistics.
1953  */
1954 /*ARGSUSED*/
1955 static int
1956 smb_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1957 {
1958 	smb_server_t	*sv;
1959 
1960 	if (!(flags & DCMD_ADDRSPEC))
1961 		return (DCMD_USAGE);
1962 
1963 	sv = mdb_alloc(sizeof (*sv), UM_SLEEP | UM_GC);
1964 	if (mdb_vread(sv, sizeof (*sv), addr) == -1) {
1965 		mdb_warn("failed to read server object at %p", addr);
1966 		return (DCMD_ERR);
1967 	}
1968 	if (sv->sv_magic != SMB_SERVER_MAGIC) {
1969 		mdb_warn("not an smb_server_t (%p)>", addr);
1970 		return (DCMD_ERR);
1971 	}
1972 	mdb_printf(
1973 	    "\n%<b>  nbt   tcp users trees files pipes%</b>\n"
1974 	    "%5d %5d %5d %5d %5d %5d\n",
1975 	    sv->sv_nbt_sess,
1976 	    sv->sv_tcp_sess,
1977 	    sv->sv_users,
1978 	    sv->sv_trees,
1979 	    sv->sv_files,
1980 	    sv->sv_pipes);
1981 
1982 	return (DCMD_OK);
1983 }
1984 
1985 /*
1986  * *****************************************************************************
1987  * ******************************** smb_ace_t **********************************
1988  * *****************************************************************************
1989  */
1990 static const ace_type_entry_t	ace_types[ACE_TYPE_TABLEN] =
1991 {
1992 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_ACE_TYPE),
1993 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_ACE_TYPE),
1994 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_ACE_TYPE),
1995 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_ACE_TYPE),
1996 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_COMPOUND_ACE_TYPE),
1997 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE),
1998 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_OBJECT_ACE_TYPE),
1999 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE),
2000 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE),
2001 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_CALLBACK_ACE_TYPE),
2002 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_CALLBACK_ACE_TYPE),
2003 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE),
2004 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE),
2005 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_CALLBACK_ACE_TYPE),
2006 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_CALLBACK_ACE_TYPE),
2007 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE),
2008 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE),
2009 	ACE_TYPE_ENTRY(0x11),
2010 	ACE_TYPE_ENTRY(0x12),
2011 	ACE_TYPE_ENTRY(0x13),
2012 	ACE_TYPE_ENTRY(0x14),
2013 	ACE_TYPE_ENTRY(0x15),
2014 	ACE_TYPE_ENTRY(0x16),
2015 	ACE_TYPE_ENTRY(0x17),
2016 	ACE_TYPE_ENTRY(0x18),
2017 	ACE_TYPE_ENTRY(0x19),
2018 	ACE_TYPE_ENTRY(0x1A),
2019 	ACE_TYPE_ENTRY(0x1B),
2020 	ACE_TYPE_ENTRY(0x1C),
2021 	ACE_TYPE_ENTRY(0x1D),
2022 	ACE_TYPE_ENTRY(0x1E),
2023 	ACE_TYPE_ENTRY(0x1F)
2024 };
2025 
2026 static const mdb_bitmask_t ace_flag_bits[] = {
2027 	{ "OBJECT_INHERIT_ACE", OBJECT_INHERIT_ACE, OBJECT_INHERIT_ACE },
2028 	{ "CONTAINER_INHERIT_ACE", CONTAINER_INHERIT_ACE,
2029 	    CONTAINER_INHERIT_ACE },
2030 	{ "NO_PROPOGATE_INHERIT_ACE", NO_PROPOGATE_INHERIT_ACE,
2031 	    NO_PROPOGATE_INHERIT_ACE },
2032 	{ "INHERIT_ONLY_ACE", INHERIT_ONLY_ACE, INHERIT_ONLY_ACE },
2033 	{ "INHERITED_ACE", INHERITED_ACE, INHERITED_ACE },
2034 	{ "SUCCESSFUL_ACCESS_ACE_FLAG", SUCCESSFUL_ACCESS_ACE_FLAG,
2035 	    SUCCESSFUL_ACCESS_ACE_FLAG },
2036 	{ "FAILED_ACCESS_ACE_FLAG", FAILED_ACCESS_ACE_FLAG,
2037 	    FAILED_ACCESS_ACE_FLAG },
2038 	{ NULL, 0, 0 }
2039 };
2040 
2041 /*
2042  * ::smbace
2043  */
2044 static int
2045 smb_ace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2046 {
2047 	smb_ace_t	ace;
2048 	int		verbose = FALSE;
2049 	const char	*ptr;
2050 	int		rc;
2051 
2052 	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &verbose,
2053 	    NULL) != argc)
2054 		return (DCMD_USAGE);
2055 
2056 	/*
2057 	 * An smb_ace address is required.
2058 	 */
2059 	if (!(flags & DCMD_ADDRSPEC))
2060 		return (DCMD_USAGE);
2061 
2062 	if (mdb_vread(&ace, sizeof (ace), addr) != sizeof (ace)) {
2063 		mdb_warn("failed to read struct smb_ace at %p", addr);
2064 		return (DCMD_ERR);
2065 	}
2066 
2067 	if (verbose) {
2068 		if (ace.se_hdr.se_type < ACE_TYPE_TABLEN)
2069 			ptr = ace_types[ace.se_hdr.se_type].ace_type_sting;
2070 		else
2071 			ptr = "Unknown";
2072 
2073 		mdb_printf("ACE Type: 0x%02x (%s)\n", ace.se_hdr.se_type, ptr);
2074 		mdb_printf("ACE Flags: %b\n", (int)ace.se_hdr.se_flags,
2075 		    ace_flag_bits);
2076 		mdb_printf("ACE Wire Size: 0x%04x\n", ace.se_hdr.se_bsize);
2077 		mdb_printf("ACE Mask: 0x%08x\n", ace.se_mask);
2078 		mdb_printf("ACE SID: ");
2079 	} else {
2080 		if (DCMD_HDRSPEC(flags))
2081 			mdb_printf(
2082 			    "%<b>%<u>%?-s %-4s %-4s %-8s %s%</u>%</b>\n",
2083 			    "ACE", "TYPE", "FLAGS", "MASK", "SID");
2084 		mdb_printf("%?p 0x%02x 0x%02x 0x%08x ", addr,
2085 		    ace.se_hdr.se_type, ace.se_hdr.se_flags, ace.se_mask);
2086 	}
2087 	rc = smb_sid_print((uintptr_t)ace.se_sid);
2088 	mdb_printf("\n");
2089 	return (rc);
2090 }
2091 
2092 static int
2093 smb_ace_walk_init(mdb_walk_state_t *wsp)
2094 {
2095 	if (wsp->walk_addr == 0) {
2096 		mdb_printf("smb_ace walk only supports local walks\n");
2097 		return (WALK_ERR);
2098 	}
2099 
2100 	wsp->walk_addr += offsetof(smb_acl_t, sl_sorted);
2101 
2102 	if (mdb_layered_walk("list", wsp) == -1) {
2103 		mdb_warn("failed to walk list of ACEs");
2104 		return (WALK_ERR);
2105 	}
2106 
2107 	return (WALK_NEXT);
2108 }
2109 
2110 static int
2111 smb_ace_walk_step(mdb_walk_state_t *wsp)
2112 {
2113 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
2114 	    wsp->walk_cbdata));
2115 }
2116 
2117 /*
2118  * *****************************************************************************
2119  * ******************************** smb_acl_t **********************************
2120  * *****************************************************************************
2121  */
2122 
2123 /*
2124  * ::smbacl
2125  */
2126 static int
2127 smb_acl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2128 {
2129 	smb_acl_t	acl;
2130 
2131 	/* An smb_acl address is required. */
2132 	if (!(flags & DCMD_ADDRSPEC))
2133 		return (DCMD_USAGE);
2134 
2135 	if (mdb_vread(&acl, sizeof (acl), addr) != sizeof (acl)) {
2136 		mdb_warn("failed to read struct smb_acl at %p", addr);
2137 		return (DCMD_ERR);
2138 	}
2139 
2140 	mdb_printf("ACL Revision: %d\n", acl.sl_revision);
2141 	mdb_printf("ACL Size on Wire: %d\n", acl.sl_bsize);
2142 	mdb_printf("ACL Number of ACEs: %d\n", acl.sl_acecnt);
2143 
2144 	(void) mdb_inc_indent(SMB_DCMD_INDENT);
2145 	if (mdb_pwalk_dcmd("smbace_walker", "smbace", argc, argv, addr)) {
2146 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2147 		mdb_warn("failed to walk list of ACEs for ACL %p", addr);
2148 		return (DCMD_ERR);
2149 	}
2150 	(void) mdb_dec_indent(SMB_DCMD_INDENT);
2151 	return (DCMD_OK);
2152 }
2153 
2154 /*
2155  * *****************************************************************************
2156  * ********************************* smb_sd_t **********************************
2157  * *****************************************************************************
2158  */
2159 
2160 /*
2161  * ::smbsd
2162  */
2163 static int
2164 smb_sd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2165 {
2166 	smb_sd_t	sd;
2167 	int		rc;
2168 
2169 	/*
2170 	 * An smb_sid address is required.
2171 	 */
2172 	if (!(flags & DCMD_ADDRSPEC))
2173 		return (DCMD_USAGE);
2174 
2175 	if (mdb_vread(&sd, sizeof (sd), addr) != sizeof (sd)) {
2176 		mdb_warn("failed to read struct smb_sd at %p", addr);
2177 		return (DCMD_ERR);
2178 	}
2179 
2180 	mdb_printf("SD Revision: %d\n", sd.sd_revision);
2181 	mdb_printf("SD Control: %04x\n", sd.sd_control);
2182 	if (sd.sd_control & SE_OWNER_DEFAULTED)
2183 		mdb_printf("\t    SE_OWNER_DEFAULTED\n");
2184 	if (sd.sd_control & SE_GROUP_DEFAULTED)
2185 		mdb_printf("\t    SE_GROUP_DEFAULTED\n");
2186 	if (sd.sd_control & SE_DACL_PRESENT)
2187 		mdb_printf("\t    SE_DACL_PRESENT\n");
2188 	if (sd.sd_control & SE_DACL_DEFAULTED)
2189 		mdb_printf("\t    SE_DACL_DEFAULTED\n");
2190 	if (sd.sd_control & SE_SACL_PRESENT)
2191 		mdb_printf("\t    SE_SACL_PRESENT\n");
2192 	if (sd.sd_control & SE_SACL_DEFAULTED)
2193 		mdb_printf("\t    SE_SACL_DEFAULTED\n");
2194 	if (sd.sd_control & SE_DACL_AUTO_INHERIT_REQ)
2195 		mdb_printf("\t    SE_DACL_AUTO_INHERIT_REQ\n");
2196 	if (sd.sd_control & SE_SACL_AUTO_INHERIT_REQ)
2197 		mdb_printf("\t    SE_SACL_AUTO_INHERIT_REQ\n");
2198 	if (sd.sd_control & SE_DACL_AUTO_INHERITED)
2199 		mdb_printf("\t    SE_DACL_AUTO_INHERITED\n");
2200 	if (sd.sd_control & SE_SACL_AUTO_INHERITED)
2201 		mdb_printf("\t    SE_SACL_AUTO_INHERITED\n");
2202 	if (sd.sd_control & SE_DACL_PROTECTED)
2203 		mdb_printf("\t    SE_DACL_PROTECTED\n");
2204 	if (sd.sd_control & SE_SACL_PROTECTED)
2205 		mdb_printf("\t    SE_SACL_PROTECTED\n");
2206 	if (sd.sd_control & SE_SELF_RELATIVE)
2207 		mdb_printf("\t    SE_SELF_RELATIVE\n");
2208 
2209 	mdb_printf("SID of Owner: ");
2210 	rc = smb_sid_print((uintptr_t)sd.sd_owner);
2211 	if (rc != DCMD_OK)
2212 		return (rc);
2213 	mdb_printf("\nSID of Group: ");
2214 	rc = smb_sid_print((uintptr_t)sd.sd_group);
2215 	if (rc != DCMD_OK)
2216 		return (rc);
2217 	mdb_printf("\n");
2218 
2219 	if (sd.sd_control & SE_SACL_PRESENT && sd.sd_sacl) {
2220 		mdb_printf("%<b>%<u>System ACL%</u>%</b>\n");
2221 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2222 		rc = mdb_call_dcmd("smbacl", (uintptr_t)sd.sd_sacl, flags,
2223 		    argc, argv);
2224 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2225 		if (rc != DCMD_OK)
2226 			return (rc);
2227 	}
2228 	if (sd.sd_control & SE_DACL_PRESENT && sd.sd_dacl) {
2229 		mdb_printf("%<b>%<u>Discretionary ACL%</u>%</b>\n");
2230 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2231 		rc = mdb_call_dcmd("smbacl", (uintptr_t)sd.sd_dacl, flags,
2232 		    argc, argv);
2233 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2234 		if (rc != DCMD_OK)
2235 			return (rc);
2236 	}
2237 
2238 	return (DCMD_OK);
2239 }
2240 
2241 /*
2242  * *****************************************************************************
2243  * ********************************* smb_sid_t *********************************
2244  * *****************************************************************************
2245  */
2246 
2247 /*
2248  * ::smbsid
2249  */
2250 /*ARGSUSED*/
2251 static int
2252 smb_sid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2253 {
2254 	/*
2255 	 * An smb_sid address is required.
2256 	 */
2257 	if (!(flags & DCMD_ADDRSPEC))
2258 		return (DCMD_USAGE);
2259 
2260 	return (smb_sid_print(addr));
2261 }
2262 
2263 /*
2264  * smb_sid_print
2265  */
2266 static int
2267 smb_sid_print(uintptr_t addr)
2268 {
2269 	smb_sid_t	sid;
2270 	smb_sid_t	*psid;
2271 	size_t		sid_size;
2272 	int		i;
2273 	uint64_t	authority;
2274 
2275 	sid_size = offsetof(smb_sid_t, sid_subauth);
2276 
2277 	if (mdb_vread(&sid, sid_size, addr) != sid_size) {
2278 		mdb_warn("failed to read struct smb_sid at %p", addr);
2279 		return (DCMD_ERR);
2280 	}
2281 
2282 	sid_size += sid.sid_subauthcnt * sizeof (sid.sid_subauth[0]);
2283 
2284 	psid = mdb_zalloc(sid_size, UM_SLEEP | UM_GC);
2285 	if (mdb_vread(psid, sid_size, addr) != sid_size) {
2286 		mdb_warn("failed to read struct smb_sid at %p", addr);
2287 		return (DCMD_ERR);
2288 	}
2289 
2290 	mdb_printf("S-%d", psid->sid_revision);
2291 	authority = 0;
2292 	for (i = 0; i < NT_SID_AUTH_MAX; i++) {
2293 		authority += ((uint64_t)psid->sid_authority[i]) <<
2294 		    (8 * (NT_SID_AUTH_MAX - 1) - i);
2295 	}
2296 	mdb_printf("-%ll", authority);
2297 
2298 	for (i = 0; i < psid->sid_subauthcnt; i++)
2299 		mdb_printf("-%d", psid->sid_subauth[i]);
2300 
2301 	return (DCMD_OK);
2302 }
2303 
2304 /*
2305  * *****************************************************************************
2306  * ********************************* smb_fssd_t ********************************
2307  * *****************************************************************************
2308  */
2309 
2310 /*
2311  * ::smbfssd
2312  */
2313 static int
2314 smb_fssd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2315 {
2316 	smb_fssd_t	fssd;
2317 	int		rc;
2318 
2319 	/*
2320 	 * An smb_fssd address is required.
2321 	 */
2322 	if (!(flags & DCMD_ADDRSPEC))
2323 		return (DCMD_USAGE);
2324 
2325 	if (mdb_vread(&fssd, sizeof (fssd), addr) != sizeof (fssd)) {
2326 		mdb_warn("failed to read struct smb_fssd at %p", addr);
2327 		return (DCMD_ERR);
2328 	}
2329 
2330 	mdb_printf("FSSD secinfo: 0x%x\n", fssd.sd_secinfo);
2331 	if (fssd.sd_secinfo & SMB_OWNER_SECINFO)
2332 		mdb_printf("FSSD uid: %d\n", fssd.sd_uid);
2333 	if (fssd.sd_secinfo & SMB_GROUP_SECINFO)
2334 		mdb_printf("FSSD gid: %d\n", fssd.sd_gid);
2335 	if (fssd.sd_secinfo & SMB_SACL_SECINFO && fssd.sd_zsacl) {
2336 		mdb_printf("%<b>%<u>System ACL%</u>%</b>\n");
2337 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2338 		rc = mdb_call_dcmd("smbacl", (uintptr_t)fssd.sd_zsacl, flags,
2339 		    argc, argv);
2340 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2341 		if (rc != DCMD_OK)
2342 			return (rc);
2343 	}
2344 	if (fssd.sd_secinfo & SMB_DACL_SECINFO && fssd.sd_zdacl) {
2345 		mdb_printf("%<b>%<u>Discretionary ACL%</u>%</b>\n");
2346 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2347 		rc = mdb_call_dcmd("smbacl", (uintptr_t)fssd.sd_zdacl, flags,
2348 		    argc, argv);
2349 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2350 		if (rc != DCMD_OK)
2351 			return (rc);
2352 	}
2353 
2354 	return (DCMD_OK);
2355 }
2356 
2357 /*
2358  * *****************************************************************************
2359  * **************************** Utility Funcions *******************************
2360  * *****************************************************************************
2361  */
2362 
2363 /*
2364  * smb_dcmd_getopt
2365  *
2366  * This function analyzes the arguments passed in and sets the bit corresponding
2367  * to the options found in the opts variable.
2368  *
2369  * Return Value
2370  *
2371  *	-1	An error occured during the decoding
2372  *	0	The decoding was successful
2373  */
2374 static int
2375 smb_dcmd_getopt(uint_t *opts, int argc, const mdb_arg_t *argv)
2376 {
2377 	*opts = 0;
2378 
2379 	if (mdb_getopts(argc, argv,
2380 	    's', MDB_OPT_SETBITS, SMB_OPT_SERVER, opts,
2381 	    'e', MDB_OPT_SETBITS, SMB_OPT_SESSION, opts,
2382 	    'r', MDB_OPT_SETBITS, SMB_OPT_REQUEST, opts,
2383 	    'u', MDB_OPT_SETBITS, SMB_OPT_USER, opts,
2384 	    't', MDB_OPT_SETBITS, SMB_OPT_TREE, opts,
2385 	    'f', MDB_OPT_SETBITS, SMB_OPT_OFILE, opts,
2386 	    'd', MDB_OPT_SETBITS, SMB_OPT_ODIR, opts,
2387 	    'w', MDB_OPT_SETBITS, SMB_OPT_WALK, opts,
2388 	    'v', MDB_OPT_SETBITS, SMB_OPT_VERBOSE, opts,
2389 	    NULL) != argc)
2390 		return (-1);
2391 
2392 	return (0);
2393 }
2394 
2395 /*
2396  * smb_dcmd_setopt
2397  *
2398  * This function set the arguments corresponding to the bits set in opts.
2399  *
2400  * Return Value
2401  *
2402  *	Number of arguments set.
2403  */
2404 static int
2405 smb_dcmd_setopt(uint_t opts, int max_argc, mdb_arg_t *argv)
2406 {
2407 	int	i;
2408 	int	argc = 0;
2409 
2410 	for (i = 0; i < SMB_MDB_MAX_OPTS; i++) {
2411 		if ((opts & smb_opts[i].o_value) && (argc < max_argc)) {
2412 			argv->a_type = MDB_TYPE_STRING;
2413 			argv->a_un.a_str = smb_opts[i].o_name;
2414 			argc++;
2415 			argv++;
2416 		}
2417 	}
2418 	return (argc);
2419 }
2420 
2421 /*
2422  * smb_obj_expand
2423  */
2424 static int
2425 smb_obj_expand(uintptr_t addr, uint_t opts, const smb_exp_t *x, ulong_t indent)
2426 {
2427 	int		rc = 0;
2428 	int		argc;
2429 	mdb_arg_t	argv[SMB_MDB_MAX_OPTS];
2430 
2431 	argc = smb_dcmd_setopt(opts | SMB_OPT_WALK, SMB_MDB_MAX_OPTS, argv);
2432 
2433 	(void) mdb_inc_indent(indent);
2434 	while (x->ex_dcmd) {
2435 		if (x->ex_mask & opts) {
2436 			rc = mdb_pwalk_dcmd("list", x->ex_dcmd, argc, argv,
2437 			    addr + x->ex_offset);
2438 
2439 			if (rc) {
2440 				mdb_warn("failed to walk the list of %s in %p",
2441 				    x->ex_name, addr + x->ex_offset);
2442 				break;
2443 			}
2444 		}
2445 		x++;
2446 	}
2447 	(void) mdb_dec_indent(indent);
2448 	return (rc);
2449 }
2450 
2451 /*
2452  * smb_obj_list
2453  *
2454  * Function called by the DCMDs when no address is provided. It expands the
2455  * tree under the object type associated with the calling DCMD (based on the
2456  * flags passed in).
2457  *
2458  * Return Value
2459  *
2460  *	DCMD_OK
2461  *	DCMD_ERR
2462  */
2463 static int
2464 smb_obj_list(const char *name, uint_t opts, uint_t flags)
2465 {
2466 	int		argc;
2467 	mdb_arg_t	argv[SMB_MDB_MAX_OPTS];
2468 
2469 	argc = smb_dcmd_setopt(opts, SMB_MDB_MAX_OPTS, argv);
2470 
2471 	if (mdb_call_dcmd("smblist", 0, flags, argc, argv)) {
2472 		mdb_warn("failed to list %s", name);
2473 		return (DCMD_ERR);
2474 	}
2475 	return (DCMD_OK);
2476 }
2477 
2478 static int
2479 smb_worker_findstack(uintptr_t addr)
2480 {
2481 	kthread_t	t;
2482 	taskq_t		tq;
2483 	char		cmd[80];
2484 	mdb_arg_t	cmdarg;
2485 
2486 	if (mdb_vread(&t, sizeof (kthread_t), addr) == -1) {
2487 		mdb_warn("failed to read kthread_t at %p", addr);
2488 		return (DCMD_ERR);
2489 	}
2490 
2491 	if (mdb_vread(&tq, sizeof (taskq_t), (uintptr_t)t.t_taskq) == -1)
2492 		tq.tq_name[0] = '\0';
2493 
2494 	mdb_inc_indent(2);
2495 
2496 	mdb_printf("PC: %a", t.t_pc);
2497 	if (t.t_tid == 0) {
2498 		if (tq.tq_name[0] != '\0')
2499 			mdb_printf("    TASKQ: %s\n", tq.tq_name);
2500 		else
2501 			mdb_printf("    THREAD: %a()\n", t.t_startpc);
2502 	}
2503 
2504 	mdb_snprintf(cmd, sizeof (cmd), "<.$c%d", 16);
2505 	cmdarg.a_type = MDB_TYPE_STRING;
2506 	cmdarg.a_un.a_str = cmd;
2507 	(void) mdb_call_dcmd("findstack", addr, DCMD_ADDRSPEC, 1, &cmdarg);
2508 	mdb_dec_indent(2);
2509 	mdb_printf("\n");
2510 	return (DCMD_OK);
2511 }
2512