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