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