xref: /titanic_41/usr/src/cmd/mdb/common/modules/smbsrv/smbsrv.c (revision bb6fd8f9750a0c1d46a75c5bcc91952d3709334b)
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  *	requires addr of an smb_server_t
1351  */
1352 /*ARGSUSED*/
1353 static int
1354 smb_dcmd_kshare(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1355 {
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 		return (DCMD_USAGE);
1365 	addr += OFFSETOF(smb_server_t, sv_export.e_share_avl.avl_tree);
1366 
1367 	if (DCMD_HDRSPEC(flags)) {
1368 		mdb_printf(
1369 		    "%<b>%<u>"
1370 		    "%-?s "
1371 		    "%-16s "
1372 		    "%-s"
1373 		    "%</u>%</b>\n",
1374 		    "smb_kshare_t", "name", "path");
1375 	}
1376 
1377 	if (mdb_pwalk("genunix`avl", smb_kshare_cb, &opts, addr) == -1) {
1378 		mdb_warn("cannot walk smb_kshare avl");
1379 		return (DCMD_ERR);
1380 	}
1381 
1382 	return (DCMD_OK);
1383 }
1384 
1385 /*
1386  * *****************************************************************************
1387  * ******************************** smb_vfs_t **********************************
1388  * *****************************************************************************
1389  */
1390 
1391 /*
1392  * ::smbvfs
1393  *
1394  * smbvfs dcmd - Prints out smb_vfs structures.
1395  */
1396 /*ARGSUSED*/
1397 static int
1398 smb_dcmd_vfs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1399 {
1400 	int		verbose = FALSE;
1401 	smb_vfs_t	*sf;
1402 	vnode_t		*vn;
1403 	char		*path;
1404 
1405 	if (mdb_getopts(argc, argv,
1406 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1407 	    NULL) != argc)
1408 		return (DCMD_USAGE);
1409 
1410 	/*
1411 	 * If no smb_vfs address was specified on the command line, we can
1412 	 * print out all smb_vfs by invoking the smb_vfs walker, using
1413 	 * this dcmd itself as the callback.
1414 	 */
1415 	if (!(flags & DCMD_ADDRSPEC)) {
1416 		if (mdb_walk_dcmd("smbvfs_walker", "smbvfs",
1417 		    argc, argv) == -1) {
1418 			mdb_warn("failed to walk 'smb_vfs'");
1419 			return (DCMD_ERR);
1420 		}
1421 		return (DCMD_OK);
1422 	}
1423 
1424 	if (DCMD_HDRSPEC(flags)) {
1425 		mdb_printf(
1426 		    "%<b>%<u>"
1427 		    "%-?s "
1428 		    "%-10s "
1429 		    "%-16s "
1430 		    "%-16s"
1431 		    "%-s"
1432 		    "%</u>%</b>\n",
1433 		    "SMB_VFS", "REFCNT", "VFS", "VNODE", "ROOT");
1434 	}
1435 
1436 	sf = mdb_alloc(sizeof (*sf), UM_SLEEP | UM_GC);
1437 	if (mdb_vread(sf, sizeof (*sf), addr) == -1) {
1438 		mdb_warn("failed to read smb_vfs at %p", addr);
1439 		return (DCMD_ERR);
1440 	}
1441 
1442 	vn = mdb_alloc(sizeof (*vn), UM_SLEEP | UM_GC);
1443 	if (mdb_vread(vn, sizeof (*vn),
1444 	    (uintptr_t)sf->sv_rootvp) == -1) {
1445 		mdb_warn("failed to read vnode at %p", sf->sv_rootvp);
1446 		return (DCMD_ERR);
1447 	}
1448 
1449 	path = mdb_zalloc(MAXPATHLEN, UM_SLEEP | UM_GC);
1450 	(void) mdb_vread(path, MAXPATHLEN, (uintptr_t)vn->v_path);
1451 
1452 	mdb_printf(
1453 	    "%-?p %-10d %-?p %-?p %-s\n", addr, sf->sv_refcnt,
1454 	    sf->sv_vfsp, sf->sv_rootvp, path);
1455 
1456 	return (DCMD_OK);
1457 }
1458 
1459 /*
1460  * Initialize the smb_vfs_t walker to point to the smb_export
1461  * in the specified smb_server_t instance.  (no global walks)
1462  */
1463 static int
1464 smb_vfs_walk_init(mdb_walk_state_t *wsp)
1465 {
1466 
1467 	if (wsp->walk_addr == NULL) {
1468 		mdb_printf("require address of an smb_server_t\n");
1469 		return (WALK_ERR);
1470 	}
1471 
1472 	wsp->walk_addr +=
1473 	    OFFSETOF(smb_server_t, sv_export.e_vfs_list.ll_list);
1474 
1475 	if (mdb_layered_walk("list", wsp) == -1) {
1476 		mdb_warn("failed to walk list of VFS");
1477 		return (WALK_ERR);
1478 	}
1479 
1480 	return (WALK_NEXT);
1481 }
1482 
1483 static int
1484 smb_vfs_walk_step(mdb_walk_state_t *wsp)
1485 {
1486 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1487 	    wsp->walk_cbdata));
1488 }
1489 
1490 /*
1491  * *****************************************************************************
1492  * ******************************* smb_node_t **********************************
1493  * *****************************************************************************
1494  */
1495 
1496 static void
1497 smb_node_help(void)
1498 {
1499 	mdb_printf(
1500 	    "Display the contents of smb_node_t, with optional filtering.\n\n");
1501 	(void) mdb_dec_indent(2);
1502 	mdb_printf("%<b>OPTIONS%</b>\n");
1503 	(void) mdb_inc_indent(2);
1504 	mdb_printf(
1505 	    "-v\tDisplay verbose smb_node information\n"
1506 	    "-p\tDisplay the full path of the vnode associated\n"
1507 	    "-s\tDisplay the stack of the last 16 calls that modified the "
1508 	    "reference\n\tcount\n");
1509 }
1510 
1511 /*
1512  * ::smbnode
1513  *
1514  * smb_node dcmd - Print out smb_node structure.
1515  */
1516 static int
1517 smb_dcmd_node(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1518 {
1519 	smb_node_t	node;
1520 	int		rc;
1521 	int		verbose = FALSE;
1522 	int		print_full_path = FALSE;
1523 	int		stack_trace = FALSE;
1524 	vnode_t		vnode;
1525 	char		od_name[MAXNAMELEN];
1526 	char		path_name[1024];
1527 	uintptr_t	list_addr, oplock_addr;
1528 
1529 	if (mdb_getopts(argc, argv,
1530 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1531 	    'p', MDB_OPT_SETBITS, TRUE, &print_full_path,
1532 	    's', MDB_OPT_SETBITS, TRUE, &stack_trace,
1533 	    NULL) != argc)
1534 		return (DCMD_USAGE);
1535 
1536 	/*
1537 	 * If no smb_node address was specified on the command line, we can
1538 	 * print out all smb nodes by invoking the smb_node walker, using
1539 	 * this dcmd itself as the callback.
1540 	 */
1541 	if (!(flags & DCMD_ADDRSPEC)) {
1542 		if (mdb_walk_dcmd("smbnode_walker", "smbnode",
1543 		    argc, argv) == -1) {
1544 			mdb_warn("failed to walk 'smb_node'");
1545 			return (DCMD_ERR);
1546 		}
1547 		return (DCMD_OK);
1548 	}
1549 
1550 	/*
1551 	 * If this is the first invocation of the command, print a nice
1552 	 * header line for the output that will follow.
1553 	 */
1554 	if (DCMD_HDRSPEC(flags)) {
1555 		if (verbose) {
1556 			mdb_printf("%<b>%<u>SMB node information:%</u>%</b>\n");
1557 		} else {
1558 			mdb_printf(
1559 			    "%<b>%<u>%-?s "
1560 			    "%-?s "
1561 			    "%-18s "
1562 			    "%-6s "
1563 			    "%-6s "
1564 			    "%-8s "
1565 			    "%-6s%</u>%</b>\n",
1566 			    "ADDR", "VP", "NODE-NAME", "OFILES", "LOCKS",
1567 			    "OPLOCK", "REF");
1568 		}
1569 	}
1570 
1571 	/*
1572 	 * For each smb_node, we just need to read the smb_node_t struct, read
1573 	 * and then print out the following fields.
1574 	 */
1575 	if (mdb_vread(&node, sizeof (node), addr) == sizeof (node)) {
1576 		(void) mdb_snprintf(od_name, sizeof (od_name), "%s",
1577 		    node.od_name);
1578 		if (print_full_path) {
1579 			if (mdb_vread(&vnode, sizeof (vnode_t),
1580 			    (uintptr_t)node.vp) == sizeof (vnode_t)) {
1581 				if (mdb_readstr(path_name, sizeof (path_name),
1582 				    (uintptr_t)vnode.v_path) != 0) {
1583 					(void) mdb_snprintf(od_name,
1584 					    sizeof (od_name), "N/A");
1585 				}
1586 			}
1587 		}
1588 		if (verbose) {
1589 			mdb_printf("VP: %p\n", node.vp);
1590 			mdb_printf("Name: %s\n", od_name);
1591 			if (print_full_path)
1592 				mdb_printf("V-node Path: %s\n", path_name);
1593 			mdb_printf("Ofiles: %u\n", node.n_ofile_list.ll_count);
1594 			mdb_printf("Range Locks: %u\n",
1595 			    node.n_lock_list.ll_count);
1596 			if (node.n_lock_list.ll_count != 0) {
1597 				(void) mdb_inc_indent(SMB_DCMD_INDENT);
1598 				list_addr = addr +
1599 				    offsetof(smb_node_t, n_lock_list) +
1600 				    offsetof(smb_llist_t, ll_list);
1601 				if (mdb_pwalk_dcmd("list", "smblock", 0,
1602 				    NULL, list_addr)) {
1603 					mdb_warn("failed to walk node's active"
1604 					    " locks");
1605 				}
1606 				(void) mdb_dec_indent(SMB_DCMD_INDENT);
1607 			}
1608 			if (node.n_oplock.ol_count == 0) {
1609 				mdb_printf("Opportunistic Locks: 0\n");
1610 			} else {
1611 				oplock_addr =
1612 				    addr + offsetof(smb_node_t, n_oplock);
1613 				mdb_printf("Opportunistic Lock: %p\n",
1614 				    oplock_addr);
1615 				rc = mdb_call_dcmd("smboplock", oplock_addr,
1616 				    flags, argc, argv);
1617 				if (rc != DCMD_OK)
1618 					return (rc);
1619 			}
1620 			mdb_printf("Reference Count: %u\n\n", node.n_refcnt);
1621 		} else {
1622 			mdb_printf("%-?p %-?p %-18s %-6d %-6d %-8d %-6d ",
1623 			    addr, node.vp, od_name, node.n_ofile_list.ll_count,
1624 			    node.n_lock_list.ll_count,
1625 			    node.n_oplock.ol_count, node.n_refcnt);
1626 
1627 			if (print_full_path)
1628 				mdb_printf("\t%s\n", path_name);
1629 		}
1630 		if (stack_trace && node.n_audit_buf) {
1631 			int ctr;
1632 			smb_audit_buf_node_t *anb;
1633 
1634 			anb = mdb_alloc(sizeof (smb_audit_buf_node_t),
1635 			    UM_SLEEP | UM_GC);
1636 
1637 			if (mdb_vread(anb, sizeof (*anb),
1638 			    (uintptr_t)node.n_audit_buf) != sizeof (*anb)) {
1639 				mdb_warn("failed to read audit buffer");
1640 				return (DCMD_ERR);
1641 			}
1642 			ctr = anb->anb_max_index + 1;
1643 			anb->anb_index--;
1644 			anb->anb_index &= anb->anb_max_index;
1645 
1646 			while (ctr) {
1647 				smb_audit_record_node_t	*anr;
1648 
1649 				anr = anb->anb_records + anb->anb_index;
1650 
1651 				if (anr->anr_depth) {
1652 					char c[MDB_SYM_NAMLEN];
1653 					GElf_Sym sym;
1654 					int i;
1655 
1656 					mdb_printf("\nRefCnt: %u\t",
1657 					    anr->anr_refcnt);
1658 
1659 					for (i = 0;
1660 					    i < anr->anr_depth;
1661 					    i++) {
1662 						if (mdb_lookup_by_addr(
1663 						    anr->anr_stack[i],
1664 						    MDB_SYM_FUZZY,
1665 						    c, sizeof (c),
1666 						    &sym) == -1) {
1667 							continue;
1668 						}
1669 						mdb_printf("%s+0x%1x",
1670 						    c,
1671 						    anr->anr_stack[i] -
1672 						    (uintptr_t)sym.st_value);
1673 						++i;
1674 						break;
1675 					}
1676 
1677 					while (i < anr->anr_depth) {
1678 						if (mdb_lookup_by_addr(
1679 						    anr->anr_stack[i],
1680 						    MDB_SYM_FUZZY,
1681 						    c, sizeof (c),
1682 						    &sym) == -1) {
1683 							++i;
1684 							continue;
1685 						}
1686 						mdb_printf("\n\t\t%s+0x%1x",
1687 						    c,
1688 						    anr->anr_stack[i] -
1689 						    (uintptr_t)sym.st_value);
1690 						++i;
1691 					}
1692 					mdb_printf("\n");
1693 				}
1694 				anb->anb_index--;
1695 				anb->anb_index &= anb->anb_max_index;
1696 				ctr--;
1697 			}
1698 		}
1699 	} else {
1700 		mdb_warn("failed to read struct smb_node at %p", addr);
1701 		return (DCMD_ERR);
1702 	}
1703 
1704 	return (DCMD_OK);
1705 }
1706 
1707 /*
1708  * Initialize the smb_node_t walker by reading the value of smb_node_hash_table
1709  * in the kernel's symbol table. Only global walk supported.
1710  */
1711 static int
1712 smb_node_walk_init(mdb_walk_state_t *wsp)
1713 {
1714 	GElf_Sym	sym;
1715 	int		i;
1716 	uintptr_t	node_hash_table_addr;
1717 
1718 	if (wsp->walk_addr == NULL) {
1719 		if (mdb_lookup_by_name("smb_node_hash_table", &sym) == -1) {
1720 			mdb_warn("failed to find 'smb_node_hash_table'");
1721 			return (WALK_ERR);
1722 		}
1723 		node_hash_table_addr = (uintptr_t)sym.st_value;
1724 	} else {
1725 		mdb_printf("smb_node walk only supports global walks\n");
1726 		return (WALK_ERR);
1727 	}
1728 
1729 	for (i = 0; i < SMBND_HASH_MASK + 1; i++) {
1730 		wsp->walk_addr = node_hash_table_addr +
1731 		    (i * sizeof (smb_llist_t)) + offsetof(smb_llist_t, ll_list);
1732 		if (mdb_layered_walk("list", wsp) == -1) {
1733 			mdb_warn("failed to walk 'list'");
1734 			return (WALK_ERR);
1735 		}
1736 	}
1737 
1738 	return (WALK_NEXT);
1739 }
1740 
1741 static int
1742 smb_node_walk_step(mdb_walk_state_t *wsp)
1743 {
1744 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1745 	    wsp->walk_cbdata));
1746 }
1747 
1748 /*
1749  * *****************************************************************************
1750  * ****************************** smb_lock_t ***********************************
1751  * *****************************************************************************
1752  */
1753 
1754 static int
1755 smb_lock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1756 {
1757 	smb_lock_t	lock;
1758 	int		verbose = FALSE;
1759 	uintptr_t	list_addr;
1760 	char		*lock_type;
1761 
1762 	if (mdb_getopts(argc, argv,
1763 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1764 	    NULL) != argc)
1765 		return (DCMD_USAGE);
1766 
1767 	/*
1768 	 * An smb_lock_t address must be specified.
1769 	 */
1770 	if (!(flags & DCMD_ADDRSPEC))
1771 		return (DCMD_USAGE);
1772 
1773 	/*
1774 	 * If this is the first invocation of the command, print a nice
1775 	 * header line for the output that will follow.
1776 	 */
1777 	if (DCMD_HDRSPEC(flags)) {
1778 		if (verbose)
1779 			mdb_printf("SMB lock information:\n\n");
1780 		else
1781 			mdb_printf("%<u>%-?s %4s %16s %8s %9s%</u>\n",
1782 			    "Locks: ", "TYPE", "START", "LENGTH",
1783 			    "CONFLICTS");
1784 	}
1785 
1786 	if (mdb_vread(&lock, sizeof (lock), addr) == sizeof (lock)) {
1787 		switch (lock.l_type) {
1788 		case SMB_LOCK_TYPE_READWRITE:
1789 			lock_type = "RW";
1790 			break;
1791 		case SMB_LOCK_TYPE_READONLY:
1792 			lock_type = "RO";
1793 			break;
1794 		default:
1795 			lock_type = "N/A";
1796 			break;
1797 		}
1798 		if (verbose) {
1799 			mdb_printf("Type             :\t%s (%u)\n",
1800 			    lock_type, lock.l_type);
1801 			mdb_printf("Start            :\t%llx\n",
1802 			    lock.l_start);
1803 			mdb_printf("Length           :\t%lx\n",
1804 			    lock.l_length);
1805 			mdb_printf("Session          :\t%p\n",
1806 			    lock.l_session);
1807 			mdb_printf("File             :\t%p\n",
1808 			    lock.l_file);
1809 			mdb_printf("User ID          :\t%u\n",
1810 			    lock.l_uid);
1811 			mdb_printf("Process ID       :\t%u\n",
1812 			    lock.l_pid);
1813 			mdb_printf("Conflicts        :\t%u\n",
1814 			    lock.l_conflict_list.sl_count);
1815 			if (lock.l_conflict_list.sl_count != 0) {
1816 				(void) mdb_inc_indent(SMB_DCMD_INDENT);
1817 				list_addr = addr +
1818 				    offsetof(smb_lock_t, l_conflict_list) +
1819 				    offsetof(smb_slist_t, sl_list);
1820 				if (mdb_pwalk_dcmd("list", "smb_lock",
1821 				    0, NULL, list_addr)) {
1822 					mdb_warn("failed to walk conflict "
1823 					    "locks ");
1824 				}
1825 				(void) mdb_dec_indent(SMB_DCMD_INDENT);
1826 			}
1827 			mdb_printf("Blocked by       :\t%p\n",
1828 			    lock.l_blocked_by);
1829 			mdb_printf("Flags            :\t0x%x\n",
1830 			    lock.l_flags);
1831 			mdb_printf("\n");
1832 		} else {
1833 			mdb_printf("%?p %4s %16llx %08lx %9x", addr,
1834 			    lock_type, lock.l_start, lock.l_length,
1835 			    lock.l_conflict_list.sl_count);
1836 		}
1837 	} else {
1838 		mdb_warn("failed to read struct smb_request at %p", addr);
1839 		return (DCMD_ERR);
1840 	}
1841 
1842 	return (DCMD_OK);
1843 }
1844 
1845 /*
1846  * *****************************************************************************
1847  * ************************** smb_oplock_grant_t *******************************
1848  * *****************************************************************************
1849  */
1850 /*ARGSUSED*/
1851 static int
1852 smb_oplock_grant(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1853 {
1854 	smb_oplock_grant_t	grant;
1855 	char			 *level;
1856 
1857 	if (!(flags & DCMD_ADDRSPEC))
1858 		return (DCMD_USAGE);
1859 
1860 	/*
1861 	 * If this is the first invocation of the command, print a nice
1862 	 * header line for the output that will follow.
1863 	 */
1864 	if (DCMD_HDRSPEC(flags)) {
1865 		mdb_printf("%<u>%-16s %-10s %-16s%</u>\n",
1866 		    "Grants:", "LEVEL", "OFILE");
1867 	}
1868 
1869 	if (mdb_vread(&grant, sizeof (grant), addr) == sizeof (grant)) {
1870 		switch (grant.og_level) {
1871 		case SMB_OPLOCK_EXCLUSIVE:
1872 			level = "EXCLUSIVE";
1873 			break;
1874 		case SMB_OPLOCK_BATCH:
1875 			level = "BATCH";
1876 			break;
1877 		case SMB_OPLOCK_LEVEL_II:
1878 			level = "LEVEL_II";
1879 			break;
1880 		default:
1881 			level = "UNKNOWN";
1882 			break;
1883 		}
1884 
1885 		mdb_printf("%-16p %-10s %-16p", addr, level, grant.og_ofile);
1886 	}
1887 	return (DCMD_OK);
1888 }
1889 
1890 /*
1891  * *****************************************************************************
1892  * ***************************** smb_oplock_t **********************************
1893  * *****************************************************************************
1894  */
1895 /*ARGSUSED*/
1896 static int
1897 smb_oplock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1898 {
1899 	smb_oplock_t	oplock;
1900 	uintptr_t	list_addr;
1901 
1902 	if (!(flags & DCMD_ADDRSPEC))
1903 		return (DCMD_USAGE);
1904 
1905 	if (mdb_vread(&oplock, sizeof (oplock), addr) != sizeof (oplock)) {
1906 		mdb_warn("failed to read struct smb_oplock at %p", addr);
1907 		return (DCMD_ERR);
1908 	}
1909 
1910 	if (oplock.ol_count == 0)
1911 		return (DCMD_OK);
1912 
1913 	(void) mdb_inc_indent(SMB_DCMD_INDENT);
1914 	switch (oplock.ol_break) {
1915 	case SMB_OPLOCK_BREAK_TO_NONE:
1916 		mdb_printf("Break Pending: BREAK_TO_NONE\n");
1917 		break;
1918 	case SMB_OPLOCK_BREAK_TO_LEVEL_II:
1919 		mdb_printf(
1920 		    "Break Pending: BREAK_TO_LEVEL_II\n");
1921 		break;
1922 	default:
1923 		break;
1924 	}
1925 
1926 	list_addr = addr + offsetof(smb_oplock_t, ol_grants);
1927 
1928 	if (mdb_pwalk_dcmd("list", "smboplockgrant",
1929 	    argc, argv, list_addr)) {
1930 		mdb_warn("failed to walk oplock grants");
1931 	}
1932 
1933 	(void) mdb_dec_indent(SMB_DCMD_INDENT);
1934 
1935 	return (DCMD_OK);
1936 }
1937 
1938 /*
1939  * ::smbstat
1940  *
1941  * Prints SMB requests statistics.
1942  */
1943 /*ARGSUSED*/
1944 static int
1945 smb_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1946 {
1947 	smb_server_t	*sv;
1948 
1949 	if (!(flags & DCMD_ADDRSPEC))
1950 		return (DCMD_USAGE);
1951 
1952 	sv = mdb_alloc(sizeof (*sv), UM_SLEEP | UM_GC);
1953 	if (mdb_vread(sv, sizeof (*sv), addr) == -1) {
1954 		mdb_warn("failed to read server object at %p", addr);
1955 		return (DCMD_ERR);
1956 	}
1957 	if (sv->sv_magic != SMB_SERVER_MAGIC) {
1958 		mdb_warn("not an smb_server_t (%p)>", addr);
1959 		return (DCMD_ERR);
1960 	}
1961 	mdb_printf(
1962 	    "\n%<b>  nbt   tcp users trees files pipes%</b>\n"
1963 	    "%5d %5d %5d %5d %5d %5d\n",
1964 	    sv->sv_nbt_sess,
1965 	    sv->sv_tcp_sess,
1966 	    sv->sv_users,
1967 	    sv->sv_trees,
1968 	    sv->sv_files,
1969 	    sv->sv_pipes);
1970 
1971 	return (DCMD_OK);
1972 }
1973 
1974 /*
1975  * *****************************************************************************
1976  * ******************************** smb_ace_t **********************************
1977  * *****************************************************************************
1978  */
1979 static const ace_type_entry_t	ace_types[ACE_TYPE_TABLEN] =
1980 {
1981 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_ACE_TYPE),
1982 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_ACE_TYPE),
1983 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_ACE_TYPE),
1984 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_ACE_TYPE),
1985 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_COMPOUND_ACE_TYPE),
1986 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE),
1987 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_OBJECT_ACE_TYPE),
1988 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE),
1989 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE),
1990 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_CALLBACK_ACE_TYPE),
1991 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_CALLBACK_ACE_TYPE),
1992 	ACE_TYPE_ENTRY(ACE_ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE),
1993 	ACE_TYPE_ENTRY(ACE_ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE),
1994 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_CALLBACK_ACE_TYPE),
1995 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_CALLBACK_ACE_TYPE),
1996 	ACE_TYPE_ENTRY(ACE_SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE),
1997 	ACE_TYPE_ENTRY(ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE),
1998 	ACE_TYPE_ENTRY(0x11),
1999 	ACE_TYPE_ENTRY(0x12),
2000 	ACE_TYPE_ENTRY(0x13),
2001 	ACE_TYPE_ENTRY(0x14),
2002 	ACE_TYPE_ENTRY(0x15),
2003 	ACE_TYPE_ENTRY(0x16),
2004 	ACE_TYPE_ENTRY(0x17),
2005 	ACE_TYPE_ENTRY(0x18),
2006 	ACE_TYPE_ENTRY(0x19),
2007 	ACE_TYPE_ENTRY(0x1A),
2008 	ACE_TYPE_ENTRY(0x1B),
2009 	ACE_TYPE_ENTRY(0x1C),
2010 	ACE_TYPE_ENTRY(0x1D),
2011 	ACE_TYPE_ENTRY(0x1E),
2012 	ACE_TYPE_ENTRY(0x1F)
2013 };
2014 
2015 static const mdb_bitmask_t ace_flag_bits[] = {
2016 	{ "OBJECT_INHERIT_ACE", OBJECT_INHERIT_ACE, OBJECT_INHERIT_ACE },
2017 	{ "CONTAINER_INHERIT_ACE", CONTAINER_INHERIT_ACE,
2018 	    CONTAINER_INHERIT_ACE },
2019 	{ "NO_PROPOGATE_INHERIT_ACE", NO_PROPOGATE_INHERIT_ACE,
2020 	    NO_PROPOGATE_INHERIT_ACE },
2021 	{ "INHERIT_ONLY_ACE", INHERIT_ONLY_ACE, INHERIT_ONLY_ACE },
2022 	{ "INHERITED_ACE", INHERITED_ACE, INHERITED_ACE },
2023 	{ "SUCCESSFUL_ACCESS_ACE_FLAG", SUCCESSFUL_ACCESS_ACE_FLAG,
2024 	    SUCCESSFUL_ACCESS_ACE_FLAG },
2025 	{ "FAILED_ACCESS_ACE_FLAG", FAILED_ACCESS_ACE_FLAG,
2026 	    FAILED_ACCESS_ACE_FLAG },
2027 	{ NULL, 0, 0 }
2028 };
2029 
2030 /*
2031  * ::smbace
2032  */
2033 static int
2034 smb_ace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2035 {
2036 	smb_ace_t	ace;
2037 	int		verbose = FALSE;
2038 	const char	*ptr;
2039 	int		rc;
2040 
2041 	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &verbose,
2042 	    NULL) != argc)
2043 		return (DCMD_USAGE);
2044 
2045 	/*
2046 	 * An smb_ace address is required.
2047 	 */
2048 	if (!(flags & DCMD_ADDRSPEC))
2049 		return (DCMD_USAGE);
2050 
2051 	if (mdb_vread(&ace, sizeof (ace), addr) != sizeof (ace)) {
2052 		mdb_warn("failed to read struct smb_ace at %p", addr);
2053 		return (DCMD_ERR);
2054 	}
2055 
2056 	if (verbose) {
2057 		if (ace.se_hdr.se_type < ACE_TYPE_TABLEN)
2058 			ptr = ace_types[ace.se_hdr.se_type].ace_type_sting;
2059 		else
2060 			ptr = "Unknown";
2061 
2062 		mdb_printf("ACE Type: 0x%02x (%s)\n", ace.se_hdr.se_type, ptr);
2063 		mdb_printf("ACE Flags: %b\n", (int)ace.se_hdr.se_flags,
2064 		    ace_flag_bits);
2065 		mdb_printf("ACE Wire Size: 0x%04x\n", ace.se_hdr.se_bsize);
2066 		mdb_printf("ACE Mask: 0x%08x\n", ace.se_mask);
2067 		mdb_printf("ACE SID: ");
2068 	} else {
2069 		if (DCMD_HDRSPEC(flags))
2070 			mdb_printf(
2071 			    "%<b>%<u>%?-s %-4s %-4s %-8s %s%</u>%</b>\n",
2072 			    "ACE", "TYPE", "FLAGS", "MASK", "SID");
2073 		mdb_printf("%?p 0x%02x 0x%02x 0x%08x ", addr,
2074 		    ace.se_hdr.se_type, ace.se_hdr.se_flags, ace.se_mask);
2075 	}
2076 	rc = smb_sid_print((uintptr_t)ace.se_sid);
2077 	mdb_printf("\n");
2078 	return (rc);
2079 }
2080 
2081 static int
2082 smb_ace_walk_init(mdb_walk_state_t *wsp)
2083 {
2084 	if (wsp->walk_addr == 0) {
2085 		mdb_printf("smb_ace walk only supports local walks\n");
2086 		return (WALK_ERR);
2087 	}
2088 
2089 	wsp->walk_addr += offsetof(smb_acl_t, sl_sorted);
2090 
2091 	if (mdb_layered_walk("list", wsp) == -1) {
2092 		mdb_warn("failed to walk list of ACEs");
2093 		return (WALK_ERR);
2094 	}
2095 
2096 	return (WALK_NEXT);
2097 }
2098 
2099 static int
2100 smb_ace_walk_step(mdb_walk_state_t *wsp)
2101 {
2102 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
2103 	    wsp->walk_cbdata));
2104 }
2105 
2106 /*
2107  * *****************************************************************************
2108  * ******************************** smb_acl_t **********************************
2109  * *****************************************************************************
2110  */
2111 
2112 /*
2113  * ::smbacl
2114  */
2115 static int
2116 smb_acl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2117 {
2118 	smb_acl_t	acl;
2119 
2120 	/* An smb_acl address is required. */
2121 	if (!(flags & DCMD_ADDRSPEC))
2122 		return (DCMD_USAGE);
2123 
2124 	if (mdb_vread(&acl, sizeof (acl), addr) != sizeof (acl)) {
2125 		mdb_warn("failed to read struct smb_acl at %p", addr);
2126 		return (DCMD_ERR);
2127 	}
2128 
2129 	mdb_printf("ACL Revision: %d\n", acl.sl_revision);
2130 	mdb_printf("ACL Size on Wire: %d\n", acl.sl_bsize);
2131 	mdb_printf("ACL Number of ACEs: %d\n", acl.sl_acecnt);
2132 
2133 	(void) mdb_inc_indent(SMB_DCMD_INDENT);
2134 	if (mdb_pwalk_dcmd("smbace_walker", "smbace", argc, argv, addr)) {
2135 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2136 		mdb_warn("failed to walk list of ACEs for ACL %p", addr);
2137 		return (DCMD_ERR);
2138 	}
2139 	(void) mdb_dec_indent(SMB_DCMD_INDENT);
2140 	return (DCMD_OK);
2141 }
2142 
2143 /*
2144  * *****************************************************************************
2145  * ********************************* smb_sd_t **********************************
2146  * *****************************************************************************
2147  */
2148 
2149 /*
2150  * ::smbsd
2151  */
2152 static int
2153 smb_sd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2154 {
2155 	smb_sd_t	sd;
2156 	int		rc;
2157 
2158 	/*
2159 	 * An smb_sid address is required.
2160 	 */
2161 	if (!(flags & DCMD_ADDRSPEC))
2162 		return (DCMD_USAGE);
2163 
2164 	if (mdb_vread(&sd, sizeof (sd), addr) != sizeof (sd)) {
2165 		mdb_warn("failed to read struct smb_sd at %p", addr);
2166 		return (DCMD_ERR);
2167 	}
2168 
2169 	mdb_printf("SD Revision: %d\n", sd.sd_revision);
2170 	mdb_printf("SD Control: %04x\n", sd.sd_control);
2171 	if (sd.sd_control & SE_OWNER_DEFAULTED)
2172 		mdb_printf("\t    SE_OWNER_DEFAULTED\n");
2173 	if (sd.sd_control & SE_GROUP_DEFAULTED)
2174 		mdb_printf("\t    SE_GROUP_DEFAULTED\n");
2175 	if (sd.sd_control & SE_DACL_PRESENT)
2176 		mdb_printf("\t    SE_DACL_PRESENT\n");
2177 	if (sd.sd_control & SE_DACL_DEFAULTED)
2178 		mdb_printf("\t    SE_DACL_DEFAULTED\n");
2179 	if (sd.sd_control & SE_SACL_PRESENT)
2180 		mdb_printf("\t    SE_SACL_PRESENT\n");
2181 	if (sd.sd_control & SE_SACL_DEFAULTED)
2182 		mdb_printf("\t    SE_SACL_DEFAULTED\n");
2183 	if (sd.sd_control & SE_DACL_AUTO_INHERIT_REQ)
2184 		mdb_printf("\t    SE_DACL_AUTO_INHERIT_REQ\n");
2185 	if (sd.sd_control & SE_SACL_AUTO_INHERIT_REQ)
2186 		mdb_printf("\t    SE_SACL_AUTO_INHERIT_REQ\n");
2187 	if (sd.sd_control & SE_DACL_AUTO_INHERITED)
2188 		mdb_printf("\t    SE_DACL_AUTO_INHERITED\n");
2189 	if (sd.sd_control & SE_SACL_AUTO_INHERITED)
2190 		mdb_printf("\t    SE_SACL_AUTO_INHERITED\n");
2191 	if (sd.sd_control & SE_DACL_PROTECTED)
2192 		mdb_printf("\t    SE_DACL_PROTECTED\n");
2193 	if (sd.sd_control & SE_SACL_PROTECTED)
2194 		mdb_printf("\t    SE_SACL_PROTECTED\n");
2195 	if (sd.sd_control & SE_SELF_RELATIVE)
2196 		mdb_printf("\t    SE_SELF_RELATIVE\n");
2197 
2198 	mdb_printf("SID of Owner: ");
2199 	rc = smb_sid_print((uintptr_t)sd.sd_owner);
2200 	if (rc != DCMD_OK)
2201 		return (rc);
2202 	mdb_printf("\nSID of Group: ");
2203 	rc = smb_sid_print((uintptr_t)sd.sd_group);
2204 	if (rc != DCMD_OK)
2205 		return (rc);
2206 	mdb_printf("\n");
2207 
2208 	if (sd.sd_control & SE_SACL_PRESENT && sd.sd_sacl) {
2209 		mdb_printf("%<b>%<u>System ACL%</u>%</b>\n");
2210 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2211 		rc = mdb_call_dcmd("smbacl", (uintptr_t)sd.sd_sacl, flags,
2212 		    argc, argv);
2213 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2214 		if (rc != DCMD_OK)
2215 			return (rc);
2216 	}
2217 	if (sd.sd_control & SE_DACL_PRESENT && sd.sd_dacl) {
2218 		mdb_printf("%<b>%<u>Discretionary ACL%</u>%</b>\n");
2219 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2220 		rc = mdb_call_dcmd("smbacl", (uintptr_t)sd.sd_dacl, flags,
2221 		    argc, argv);
2222 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2223 		if (rc != DCMD_OK)
2224 			return (rc);
2225 	}
2226 
2227 	return (DCMD_OK);
2228 }
2229 
2230 /*
2231  * *****************************************************************************
2232  * ********************************* smb_sid_t *********************************
2233  * *****************************************************************************
2234  */
2235 
2236 /*
2237  * ::smbsid
2238  */
2239 /*ARGSUSED*/
2240 static int
2241 smb_sid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2242 {
2243 	/*
2244 	 * An smb_sid address is required.
2245 	 */
2246 	if (!(flags & DCMD_ADDRSPEC))
2247 		return (DCMD_USAGE);
2248 
2249 	return (smb_sid_print(addr));
2250 }
2251 
2252 /*
2253  * smb_sid_print
2254  */
2255 static int
2256 smb_sid_print(uintptr_t addr)
2257 {
2258 	smb_sid_t	sid;
2259 	smb_sid_t	*psid;
2260 	size_t		sid_size;
2261 	int		i;
2262 	uint64_t	authority;
2263 
2264 	sid_size = offsetof(smb_sid_t, sid_subauth);
2265 
2266 	if (mdb_vread(&sid, sid_size, addr) != sid_size) {
2267 		mdb_warn("failed to read struct smb_sid at %p", addr);
2268 		return (DCMD_ERR);
2269 	}
2270 
2271 	sid_size += sid.sid_subauthcnt * sizeof (sid.sid_subauth[0]);
2272 
2273 	psid = mdb_zalloc(sid_size, UM_SLEEP | UM_GC);
2274 	if (mdb_vread(psid, sid_size, addr) != sid_size) {
2275 		mdb_warn("failed to read struct smb_sid at %p", addr);
2276 		return (DCMD_ERR);
2277 	}
2278 
2279 	mdb_printf("S-%d", psid->sid_revision);
2280 	authority = 0;
2281 	for (i = 0; i < NT_SID_AUTH_MAX; i++) {
2282 		authority += ((uint64_t)psid->sid_authority[i]) <<
2283 		    (8 * (NT_SID_AUTH_MAX - 1) - i);
2284 	}
2285 	mdb_printf("-%ll", authority);
2286 
2287 	for (i = 0; i < psid->sid_subauthcnt; i++)
2288 		mdb_printf("-%d", psid->sid_subauth[i]);
2289 
2290 	return (DCMD_OK);
2291 }
2292 
2293 /*
2294  * *****************************************************************************
2295  * ********************************* smb_fssd_t ********************************
2296  * *****************************************************************************
2297  */
2298 
2299 /*
2300  * ::smbfssd
2301  */
2302 static int
2303 smb_fssd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2304 {
2305 	smb_fssd_t	fssd;
2306 	int		rc;
2307 
2308 	/*
2309 	 * An smb_fssd address is required.
2310 	 */
2311 	if (!(flags & DCMD_ADDRSPEC))
2312 		return (DCMD_USAGE);
2313 
2314 	if (mdb_vread(&fssd, sizeof (fssd), addr) != sizeof (fssd)) {
2315 		mdb_warn("failed to read struct smb_fssd at %p", addr);
2316 		return (DCMD_ERR);
2317 	}
2318 
2319 	mdb_printf("FSSD secinfo: 0x%x\n", fssd.sd_secinfo);
2320 	if (fssd.sd_secinfo & SMB_OWNER_SECINFO)
2321 		mdb_printf("FSSD uid: %d\n", fssd.sd_uid);
2322 	if (fssd.sd_secinfo & SMB_GROUP_SECINFO)
2323 		mdb_printf("FSSD gid: %d\n", fssd.sd_gid);
2324 	if (fssd.sd_secinfo & SMB_SACL_SECINFO && fssd.sd_zsacl) {
2325 		mdb_printf("%<b>%<u>System ACL%</u>%</b>\n");
2326 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2327 		rc = mdb_call_dcmd("smbacl", (uintptr_t)fssd.sd_zsacl, flags,
2328 		    argc, argv);
2329 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2330 		if (rc != DCMD_OK)
2331 			return (rc);
2332 	}
2333 	if (fssd.sd_secinfo & SMB_DACL_SECINFO && fssd.sd_zdacl) {
2334 		mdb_printf("%<b>%<u>Discretionary ACL%</u>%</b>\n");
2335 		(void) mdb_inc_indent(SMB_DCMD_INDENT);
2336 		rc = mdb_call_dcmd("smbacl", (uintptr_t)fssd.sd_zdacl, flags,
2337 		    argc, argv);
2338 		(void) mdb_dec_indent(SMB_DCMD_INDENT);
2339 		if (rc != DCMD_OK)
2340 			return (rc);
2341 	}
2342 
2343 	return (DCMD_OK);
2344 }
2345 
2346 /*
2347  * *****************************************************************************
2348  * **************************** Utility Funcions *******************************
2349  * *****************************************************************************
2350  */
2351 
2352 /*
2353  * smb_dcmd_getopt
2354  *
2355  * This function analyzes the arguments passed in and sets the bit corresponding
2356  * to the options found in the opts variable.
2357  *
2358  * Return Value
2359  *
2360  *	-1	An error occured during the decoding
2361  *	0	The decoding was successful
2362  */
2363 static int
2364 smb_dcmd_getopt(uint_t *opts, int argc, const mdb_arg_t *argv)
2365 {
2366 	*opts = 0;
2367 
2368 	if (mdb_getopts(argc, argv,
2369 	    's', MDB_OPT_SETBITS, SMB_OPT_SERVER, opts,
2370 	    'e', MDB_OPT_SETBITS, SMB_OPT_SESSION, opts,
2371 	    'r', MDB_OPT_SETBITS, SMB_OPT_REQUEST, opts,
2372 	    'u', MDB_OPT_SETBITS, SMB_OPT_USER, opts,
2373 	    't', MDB_OPT_SETBITS, SMB_OPT_TREE, opts,
2374 	    'f', MDB_OPT_SETBITS, SMB_OPT_OFILE, opts,
2375 	    'd', MDB_OPT_SETBITS, SMB_OPT_ODIR, opts,
2376 	    'w', MDB_OPT_SETBITS, SMB_OPT_WALK, opts,
2377 	    'v', MDB_OPT_SETBITS, SMB_OPT_VERBOSE, opts,
2378 	    NULL) != argc)
2379 		return (-1);
2380 
2381 	return (0);
2382 }
2383 
2384 /*
2385  * smb_dcmd_setopt
2386  *
2387  * This function set the arguments corresponding to the bits set in opts.
2388  *
2389  * Return Value
2390  *
2391  *	Number of arguments set.
2392  */
2393 static int
2394 smb_dcmd_setopt(uint_t opts, int max_argc, mdb_arg_t *argv)
2395 {
2396 	int	i;
2397 	int	argc = 0;
2398 
2399 	for (i = 0; i < SMB_MDB_MAX_OPTS; i++) {
2400 		if ((opts & smb_opts[i].o_value) && (argc < max_argc)) {
2401 			argv->a_type = MDB_TYPE_STRING;
2402 			argv->a_un.a_str = smb_opts[i].o_name;
2403 			argc++;
2404 			argv++;
2405 		}
2406 	}
2407 	return (argc);
2408 }
2409 
2410 /*
2411  * smb_obj_expand
2412  */
2413 static int
2414 smb_obj_expand(uintptr_t addr, uint_t opts, const smb_exp_t *x, ulong_t indent)
2415 {
2416 	int		rc = 0;
2417 	int		argc;
2418 	mdb_arg_t	argv[SMB_MDB_MAX_OPTS];
2419 
2420 	argc = smb_dcmd_setopt(opts | SMB_OPT_WALK, SMB_MDB_MAX_OPTS, argv);
2421 
2422 	(void) mdb_inc_indent(indent);
2423 	while (x->ex_dcmd) {
2424 		if (x->ex_mask & opts) {
2425 			rc = mdb_pwalk_dcmd("list", x->ex_dcmd, argc, argv,
2426 			    addr + x->ex_offset);
2427 
2428 			if (rc) {
2429 				mdb_warn("failed to walk the list of %s in %p",
2430 				    x->ex_name, addr + x->ex_offset);
2431 				break;
2432 			}
2433 		}
2434 		x++;
2435 	}
2436 	(void) mdb_dec_indent(indent);
2437 	return (rc);
2438 }
2439 
2440 /*
2441  * smb_obj_list
2442  *
2443  * Function called by the DCMDs when no address is provided. It expands the
2444  * tree under the object type associated with the calling DCMD (based on the
2445  * flags passed in).
2446  *
2447  * Return Value
2448  *
2449  *	DCMD_OK
2450  *	DCMD_ERR
2451  */
2452 static int
2453 smb_obj_list(const char *name, uint_t opts, uint_t flags)
2454 {
2455 	int		argc;
2456 	mdb_arg_t	argv[SMB_MDB_MAX_OPTS];
2457 
2458 	argc = smb_dcmd_setopt(opts, SMB_MDB_MAX_OPTS, argv);
2459 
2460 	if (mdb_call_dcmd("smblist", 0, flags, argc, argv)) {
2461 		mdb_warn("failed to list %s", name);
2462 		return (DCMD_ERR);
2463 	}
2464 	return (DCMD_OK);
2465 }
2466 
2467 static int
2468 smb_worker_findstack(uintptr_t addr)
2469 {
2470 	kthread_t	t;
2471 	taskq_t		tq;
2472 	char		cmd[80];
2473 	mdb_arg_t	cmdarg;
2474 
2475 	if (mdb_vread(&t, sizeof (kthread_t), addr) == -1) {
2476 		mdb_warn("failed to read kthread_t at %p", addr);
2477 		return (DCMD_ERR);
2478 	}
2479 
2480 	if (mdb_vread(&tq, sizeof (taskq_t), (uintptr_t)t.t_taskq) == -1)
2481 		tq.tq_name[0] = '\0';
2482 
2483 	mdb_inc_indent(2);
2484 
2485 	mdb_printf("PC: %a", t.t_pc);
2486 	if (t.t_tid == 0) {
2487 		if (tq.tq_name[0] != '\0')
2488 			mdb_printf("    TASKQ: %s\n", tq.tq_name);
2489 		else
2490 			mdb_printf("    THREAD: %a()\n", t.t_startpc);
2491 	}
2492 
2493 	mdb_snprintf(cmd, sizeof (cmd), "<.$c%d", 16);
2494 	cmdarg.a_type = MDB_TYPE_STRING;
2495 	cmdarg.a_un.a_str = cmd;
2496 	(void) mdb_call_dcmd("findstack", addr, DCMD_ADDRSPEC, 1, &cmdarg);
2497 	mdb_dec_indent(2);
2498 	mdb_printf("\n");
2499 	return (DCMD_OK);
2500 }
2501