xref: /titanic_51/usr/src/cmd/mdb/common/modules/genunix/vfs.c (revision e762302f03f5529f83d98085b915143b08d3ae1b)
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 <mdb/mdb_modapi.h>
27 #include <mdb/mdb_ks.h>
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/door.h>
32 #include <sys/file.h>
33 #include <sys/mount.h>
34 #include <sys/proc.h>
35 #include <sys/procfs.h>
36 #include <sys/proc/prdata.h>
37 #include <sys/stat.h>
38 #include <sys/vfs.h>
39 #include <sys/vnode.h>
40 #include <sys/fs/snode.h>
41 #include <sys/fs/fifonode.h>
42 #include <sys/fs/namenode.h>
43 #include <sys/socket.h>
44 #include <sys/stropts.h>
45 #include <sys/socketvar.h>
46 #include <sys/strsubr.h>
47 #include <sys/un.h>
48 #include <fs/sockfs/socktpi_impl.h>
49 #include <inet/ipclassifier.h>
50 #include <inet/ip_if.h>
51 #include <inet/sctp/sctp_impl.h>
52 #include <inet/sctp/sctp_addr.h>
53 
54 int
55 vfs_walk_init(mdb_walk_state_t *wsp)
56 {
57 	if (wsp->walk_addr == NULL &&
58 	    mdb_readvar(&wsp->walk_addr, "rootvfs") == -1) {
59 		mdb_warn("failed to read 'rootvfs'");
60 		return (WALK_ERR);
61 	}
62 
63 	wsp->walk_data = (void *)wsp->walk_addr;
64 	return (WALK_NEXT);
65 }
66 
67 int
68 vfs_walk_step(mdb_walk_state_t *wsp)
69 {
70 	vfs_t vfs;
71 	int status;
72 
73 	if (mdb_vread(&vfs, sizeof (vfs), wsp->walk_addr) == -1) {
74 		mdb_warn("failed to read vfs_t at %p", wsp->walk_addr);
75 		return (WALK_DONE);
76 	}
77 
78 	status = wsp->walk_callback(wsp->walk_addr, &vfs, wsp->walk_cbdata);
79 
80 	if (vfs.vfs_next == wsp->walk_data)
81 		return (WALK_DONE);
82 
83 	wsp->walk_addr = (uintptr_t)vfs.vfs_next;
84 
85 	return (status);
86 }
87 
88 /*
89  * Utility routine to read in a filesystem name given a vfs pointer.  If
90  * no vfssw entry for the vfs is available (as is the case with some pseudo-
91  * filesystems), we check against some known problem fs's: doorfs and
92  * portfs.  If that fails, we try to guess the filesystem name using
93  * symbol names.  fsname should be a buffer of size _ST_FSTYPSZ.
94  */
95 static int
96 read_fsname(uintptr_t vfsp, char *fsname)
97 {
98 	vfs_t vfs;
99 	struct vfssw vfssw_entry;
100 	GElf_Sym vfssw_sym, test_sym;
101 	char testname[MDB_SYM_NAMLEN];
102 
103 	if (mdb_vread(&vfs, sizeof (vfs), vfsp) == -1) {
104 		mdb_warn("failed to read vfs %p", vfsp);
105 		return (-1);
106 	}
107 
108 	if (mdb_lookup_by_name("vfssw", &vfssw_sym) == -1) {
109 		mdb_warn("failed to find vfssw");
110 		return (-1);
111 	}
112 
113 	/*
114 	 * vfssw is an array; we need vfssw[vfs.vfs_fstype].
115 	 */
116 	if (mdb_vread(&vfssw_entry, sizeof (vfssw_entry),
117 	    vfssw_sym.st_value + (sizeof (struct vfssw) * vfs.vfs_fstype))
118 	    == -1) {
119 		mdb_warn("failed to read vfssw index %d", vfs.vfs_fstype);
120 		return (-1);
121 	}
122 
123 	if (vfs.vfs_fstype != 0) {
124 		if (mdb_readstr(fsname, _ST_FSTYPSZ,
125 		    (uintptr_t)vfssw_entry.vsw_name) == -1) {
126 			mdb_warn("failed to find fs name %p",
127 			    vfssw_entry.vsw_name);
128 			return (-1);
129 		}
130 		return (0);
131 	}
132 
133 	/*
134 	 * Do precise detection for certain filesystem types that we
135 	 * know do not appear in vfssw[], and that we depend upon in other
136 	 * parts of the code: doorfs and portfs.
137 	 */
138 	if (mdb_lookup_by_name("door_vfs", &test_sym) != -1) {
139 		if (test_sym.st_value == vfsp) {
140 			strcpy(fsname, "doorfs");
141 			return (0);
142 		}
143 	}
144 	if (mdb_lookup_by_name("port_vfs", &test_sym) != -1) {
145 		if (test_sym.st_value == vfsp) {
146 			strcpy(fsname, "portfs");
147 			return (0);
148 		}
149 	}
150 
151 	/*
152 	 * Heuristic detection for other filesystems that don't have a
153 	 * vfssw[] entry.  These tend to be named <fsname>_vfs, so we do a
154 	 * lookup_by_addr and see if we find a symbol of that name.
155 	 */
156 	if (mdb_lookup_by_addr(vfsp, MDB_SYM_EXACT, testname, sizeof (testname),
157 	    &test_sym) != -1) {
158 		if ((strlen(testname) > 4) &&
159 		    (strcmp(testname + strlen(testname) - 4, "_vfs") == 0)) {
160 			testname[strlen(testname) - 4] = '\0';
161 			strncpy(fsname, testname, _ST_FSTYPSZ);
162 			return (0);
163 		}
164 	}
165 
166 	mdb_warn("unknown filesystem type for vfs %p", vfsp);
167 	return (-1);
168 }
169 
170 /*
171  * Column widths for mount point display in ::fsinfo output.
172  */
173 #ifdef _LP64
174 #define	FSINFO_MNTLEN	48
175 #else
176 #define	FSINFO_MNTLEN	56
177 #endif
178 
179 /* ARGSUSED */
180 int
181 fsinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
182 {
183 	vfs_t vfs;
184 	int len;
185 	int opt_v = 0;
186 	char buf[MAXPATHLEN];
187 	char fsname[_ST_FSTYPSZ];
188 	mntopt_t *mntopts;
189 	size_t size;
190 	int i;
191 	int first = 1;
192 	char opt[MAX_MNTOPT_STR];
193 	uintptr_t global_zone;
194 
195 	if (!(flags & DCMD_ADDRSPEC)) {
196 		if (mdb_walk_dcmd("vfs", "fsinfo", argc, argv) == -1) {
197 			mdb_warn("failed to walk file system list");
198 			return (DCMD_ERR);
199 		}
200 		return (DCMD_OK);
201 	}
202 
203 	if (mdb_getopts(argc, argv,
204 	    'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc)
205 		return (DCMD_USAGE);
206 
207 	if (DCMD_HDRSPEC(flags))
208 		mdb_printf("%<u>%?s %-15s %s%</u>\n",
209 		    "VFSP", "FS", "MOUNT");
210 
211 	if (mdb_vread(&vfs, sizeof (vfs), addr) == -1) {
212 		mdb_warn("failed to read vfs_t %p", addr);
213 		return (DCMD_ERR);
214 	}
215 
216 	if ((len = mdb_read_refstr((uintptr_t)vfs.vfs_mntpt, buf,
217 	    sizeof (buf))) <= 0)
218 		strcpy(buf, "??");
219 
220 	else if (!opt_v && (len >= FSINFO_MNTLEN))
221 		/*
222 		 * In normal mode, we truncate the path to keep the output
223 		 * clean.  In -v mode, we just print the full path.
224 		 */
225 		strcpy(&buf[FSINFO_MNTLEN - 4], "...");
226 
227 	if (read_fsname(addr, fsname) == -1)
228 		return (DCMD_ERR);
229 
230 	mdb_printf("%0?p %-15s %s\n", addr, fsname, buf);
231 
232 	if (!opt_v)
233 		return (DCMD_OK);
234 
235 	/*
236 	 * Print 'resource' string; this shows what we're mounted upon.
237 	 */
238 	if (mdb_read_refstr((uintptr_t)vfs.vfs_resource, buf,
239 	    MAXPATHLEN) <= 0)
240 		strcpy(buf, "??");
241 
242 	mdb_printf("%?s %s\n", "R:", buf);
243 
244 	/*
245 	 * Print mount options array; it sucks to be a mimic, but we copy
246 	 * the same logic as in mntvnops.c for adding zone= tags, and we
247 	 * don't bother with the obsolete dev= option.
248 	 */
249 	size = vfs.vfs_mntopts.mo_count * sizeof (mntopt_t);
250 	mntopts = mdb_alloc(size, UM_SLEEP | UM_GC);
251 
252 	if (mdb_vread(mntopts, size,
253 	    (uintptr_t)vfs.vfs_mntopts.mo_list) == -1) {
254 		mdb_warn("failed to read mntopts %p", vfs.vfs_mntopts.mo_list);
255 		return (DCMD_ERR);
256 	}
257 
258 	for (i = 0; i < vfs.vfs_mntopts.mo_count; i++) {
259 		if (mntopts[i].mo_flags & MO_SET) {
260 			if (mdb_readstr(opt, sizeof (opt),
261 			    (uintptr_t)mntopts[i].mo_name) == -1) {
262 				mdb_warn("failed to read mntopt name %p",
263 				    mntopts[i].mo_name);
264 				return (DCMD_ERR);
265 			}
266 			if (first) {
267 				mdb_printf("%?s ", "O:");
268 				first = 0;
269 			} else {
270 				mdb_printf(",");
271 			}
272 			mdb_printf("%s", opt);
273 			if (mntopts[i].mo_flags & MO_HASVALUE) {
274 				if (mdb_readstr(opt, sizeof (opt),
275 				    (uintptr_t)mntopts[i].mo_arg) == -1) {
276 					mdb_warn("failed to read mntopt "
277 					    "value %p", mntopts[i].mo_arg);
278 					return (DCMD_ERR);
279 				}
280 				mdb_printf("=%s", opt);
281 			}
282 		}
283 	}
284 
285 	if (mdb_readvar(&global_zone, "global_zone") == -1) {
286 		mdb_warn("failed to locate global_zone");
287 		return (DCMD_ERR);
288 	}
289 
290 	if ((vfs.vfs_zone != NULL) &&
291 	    ((uintptr_t)vfs.vfs_zone != global_zone)) {
292 		zone_t z;
293 
294 		if (mdb_vread(&z, sizeof (z), (uintptr_t)vfs.vfs_zone) == -1) {
295 			mdb_warn("failed to read zone");
296 			return (DCMD_ERR);
297 		}
298 		/*
299 		 * zone names are much shorter than MAX_MNTOPT_STR
300 		 */
301 		if (mdb_readstr(opt, sizeof (opt),
302 		    (uintptr_t)z.zone_name) == -1) {
303 			mdb_warn("failed to read zone name");
304 			return (DCMD_ERR);
305 		}
306 		if (first) {
307 			mdb_printf("%?s ", "O:");
308 		} else {
309 			mdb_printf(",");
310 		}
311 		mdb_printf("zone=%s", opt);
312 	}
313 	return (DCMD_OK);
314 }
315 
316 
317 #define	REALVP_DONE	0
318 #define	REALVP_ERR	1
319 #define	REALVP_CONTINUE	2
320 
321 static int
322 next_realvp(uintptr_t invp, struct vnode *outvn, uintptr_t *outvp)
323 {
324 	char fsname[_ST_FSTYPSZ];
325 
326 	*outvp = invp;
327 	if (mdb_vread(outvn, sizeof (struct vnode), invp) == -1) {
328 		mdb_warn("failed to read vnode at %p", invp);
329 		return (REALVP_ERR);
330 	}
331 
332 	if (read_fsname((uintptr_t)outvn->v_vfsp, fsname) == -1)
333 		return (REALVP_ERR);
334 
335 	/*
336 	 * We know how to do 'realvp' for as many filesystems as possible;
337 	 * for all other filesystems, we assume that the vp we are given
338 	 * is the realvp.  In the kernel, a realvp operation will sometimes
339 	 * dig through multiple layers.  Here, we only fetch the pointer
340 	 * to the next layer down.  This allows dcmds to print out the
341 	 * various layers.
342 	 */
343 	if (strcmp(fsname, "fifofs") == 0) {
344 		fifonode_t fn;
345 		if (mdb_vread(&fn, sizeof (fn),
346 		    (uintptr_t)outvn->v_data) == -1) {
347 			mdb_warn("failed to read fifonode");
348 			return (REALVP_ERR);
349 		}
350 		*outvp = (uintptr_t)fn.fn_realvp;
351 
352 	} else if (strcmp(fsname, "namefs") == 0) {
353 		struct namenode nn;
354 		if (mdb_vread(&nn, sizeof (nn),
355 		    (uintptr_t)outvn->v_data) == -1) {
356 			mdb_warn("failed to read namenode");
357 			return (REALVP_ERR);
358 		}
359 		*outvp = (uintptr_t)nn.nm_filevp;
360 
361 	} else if (outvn->v_type == VSOCK && outvn->v_stream != NULL) {
362 		struct stdata stream;
363 
364 		/*
365 		 * Sockets have a strange and different layering scheme; we
366 		 * hop over into the sockfs vnode (accessible via the stream
367 		 * head) if possible.
368 		 */
369 		if (mdb_vread(&stream, sizeof (stream),
370 		    (uintptr_t)outvn->v_stream) == -1) {
371 			mdb_warn("failed to read stream data");
372 			return (REALVP_ERR);
373 		}
374 		*outvp = (uintptr_t)stream.sd_vnode;
375 	}
376 
377 	if (*outvp == invp || *outvp == NULL)
378 		return (REALVP_DONE);
379 
380 	return (REALVP_CONTINUE);
381 }
382 
383 static void
384 pfiles_print_addr(struct sockaddr *addr)
385 {
386 	struct sockaddr_in *s_in;
387 	struct sockaddr_un *s_un;
388 	struct sockaddr_in6 *s_in6;
389 	in_port_t port;
390 
391 	switch (addr->sa_family) {
392 	case AF_INET:
393 		/* LINTED: alignment */
394 		s_in = (struct sockaddr_in *)addr;
395 		mdb_nhconvert(&port, &s_in->sin_port, sizeof (port));
396 		mdb_printf("AF_INET %I %d ", s_in->sin_addr.s_addr, port);
397 		break;
398 
399 	case AF_INET6:
400 		/* LINTED: alignment */
401 		s_in6 = (struct sockaddr_in6 *)addr;
402 		mdb_nhconvert(&port, &s_in6->sin6_port, sizeof (port));
403 		mdb_printf("AF_INET6 %N %d ", &(s_in6->sin6_addr), port);
404 		break;
405 
406 	case AF_UNIX:
407 		s_un = (struct sockaddr_un *)addr;
408 		mdb_printf("AF_UNIX %s ", s_un->sun_path);
409 		break;
410 	default:
411 		mdb_printf("AF_?? (%d) ", addr->sa_family);
412 		break;
413 	}
414 }
415 
416 static int
417 pfiles_get_sonode(vnode_t *v_sock, struct sonode *sonode)
418 {
419 	if (mdb_vread(sonode, sizeof (struct sonode),
420 	    (uintptr_t)v_sock->v_data) == -1) {
421 		mdb_warn("failed to read sonode");
422 		return (-1);
423 	}
424 
425 	return (0);
426 }
427 
428 static int
429 pfiles_get_tpi_sonode(vnode_t *v_sock, sotpi_sonode_t *sotpi_sonode)
430 {
431 
432 	struct stdata stream;
433 
434 	if (mdb_vread(&stream, sizeof (stream),
435 	    (uintptr_t)v_sock->v_stream) == -1) {
436 		mdb_warn("failed to read stream data");
437 		return (-1);
438 	}
439 
440 	if (mdb_vread(v_sock, sizeof (vnode_t),
441 	    (uintptr_t)stream.sd_vnode) == -1) {
442 		mdb_warn("failed to read stream vnode");
443 		return (-1);
444 	}
445 
446 	if (mdb_vread(sotpi_sonode, sizeof (sotpi_sonode_t),
447 	    (uintptr_t)v_sock->v_data) == -1) {
448 		mdb_warn("failed to read sotpi_sonode");
449 		return (-1);
450 	}
451 
452 	return (0);
453 }
454 
455 /*
456  * Do some digging to get a reasonable pathname for this vnode. 'path'
457  * should point at a buffer of MAXPATHLEN in size.
458  */
459 static int
460 pfiles_dig_pathname(uintptr_t vp, char *path)
461 {
462 	vnode_t v;
463 
464 	bzero(path, MAXPATHLEN);
465 
466 	if (mdb_vread(&v, sizeof (v), vp) == -1) {
467 		mdb_warn("failed to read vnode");
468 		return (-1);
469 	}
470 
471 	if (v.v_path == NULL) {
472 		/*
473 		 * fifo's and doors are special.   Some have pathnames, and
474 		 * some do not.  And for these, it is pointless to go off to
475 		 * mdb_vnode2path, which is very slow.
476 		 *
477 		 * Event ports never have a pathname.
478 		 */
479 		if (v.v_type == VFIFO || v.v_type == VDOOR || v.v_type == VPORT)
480 			return (0);
481 
482 		/*
483 		 * For sockets, we won't find a path unless we print the path
484 		 * associated with transport's STREAM device.
485 		 */
486 		if (v.v_type == VSOCK) {
487 			struct sonode sonode;
488 			struct sockparams sockparams;
489 
490 			if (pfiles_get_sonode(&v, &sonode) == -1) {
491 				return (-1);
492 			}
493 			if (mdb_vread(&sockparams, sizeof (sockparams),
494 			    (uintptr_t)sonode.so_sockparams) == -1) {
495 				mdb_warn("failed to read sockparams");
496 				return (-1);
497 			}
498 
499 			if (!SOCK_IS_NONSTR(&sonode)) {
500 				vp = (uintptr_t)
501 				    sockparams.sp_sdev_info.sd_vnode;
502 			} else {
503 				vp = NULL;
504 			}
505 		}
506 	}
507 
508 
509 	/*
510 	 * mdb_vnode2path will print an error for us as needed, but not
511 	 * finding a pathname is not really an error, so we plow on.
512 	 */
513 	(void) mdb_vnode2path(vp, path, MAXPATHLEN);
514 
515 	/*
516 	 * A common problem is that device pathnames are prefixed with
517 	 * /dev/../devices/.  We just clean those up slightly:
518 	 * 	/dev/../devices/<mumble> --> /devices/<mumble>
519 	 * 	/dev/pts/../../devices/<mumble> --> /devices/<mumble>
520 	 */
521 	if (strncmp("/dev/../devices/", path, strlen("/dev/../devices/")) == 0)
522 		strcpy(path, path + 7);
523 
524 	if (strncmp("/dev/pts/../../devices/", path,
525 	    strlen("/dev/pts/../../devices/")) == 0)
526 		strcpy(path, path + 14);
527 
528 	return (0);
529 }
530 
531 const struct fs_type {
532 	int type;
533 	const char *name;
534 } fs_types[] = {
535 	{ VNON,   "NON" },
536 	{ VREG,   "REG" },
537 	{ VDIR,   "DIR" },
538 	{ VBLK,   "BLK" },
539 	{ VCHR,   "CHR" },
540 	{ VLNK,   "LNK" },
541 	{ VFIFO,  "FIFO" },
542 	{ VDOOR,  "DOOR" },
543 	{ VPROC,  "PROC" },
544 	{ VSOCK,  "SOCK" },
545 	{ VPORT,  "PORT" },
546 	{ VBAD,   "BAD" }
547 };
548 
549 #define	NUM_FS_TYPES (sizeof (fs_types) / sizeof (struct fs_type))
550 
551 struct pfiles_cbdata {
552 	int opt_p;
553 	int fd;
554 };
555 
556 #define	list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
557 #define	list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
558 
559 /*
560  * SCTP interface for geting the first source address of a sctp_t.
561  */
562 int
563 sctp_getsockaddr(sctp_t *sctp, struct sockaddr *addr)
564 {
565 	int			err = -1;
566 	int			i;
567 	int			l;
568 	sctp_saddr_ipif_t	*pobj;
569 	sctp_saddr_ipif_t	obj;
570 	size_t			added = 0;
571 	sin6_t			*sin6;
572 	sin_t			*sin4;
573 	int			scanned = 0;
574 	boolean_t		skip_lback = B_FALSE;
575 
576 	addr->sa_family = sctp->sctp_family;
577 	if (sctp->sctp_nsaddrs == 0)
578 		goto done;
579 
580 	/*
581 	 * Skip loopback addresses for non-loopback assoc.
582 	 */
583 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback) {
584 		skip_lback = B_TRUE;
585 	}
586 
587 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
588 		if (sctp->sctp_saddrs[i].ipif_count == 0)
589 			continue;
590 
591 		pobj = list_object(&sctp->sctp_saddrs[i].sctp_ipif_list,
592 		    sctp->sctp_saddrs[i].sctp_ipif_list.list_head.list_next);
593 		if (mdb_vread(&obj, sizeof (sctp_saddr_ipif_t),
594 		    (uintptr_t)pobj) == -1) {
595 			mdb_warn("failed to read sctp_saddr_ipif_t");
596 			return (err);
597 		}
598 
599 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
600 			sctp_ipif_t	ipif;
601 			in6_addr_t	laddr;
602 			list_node_t 	*pnode;
603 			list_node_t	node;
604 
605 			if (mdb_vread(&ipif, sizeof (sctp_ipif_t),
606 			    (uintptr_t)obj.saddr_ipifp) == -1) {
607 				mdb_warn("failed to read sctp_ipif_t");
608 				return (err);
609 			}
610 			laddr = ipif.sctp_ipif_saddr;
611 
612 			scanned++;
613 			if ((ipif.sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
614 			    SCTP_DONT_SRC(&obj) ||
615 			    (ipif.sctp_ipif_ill->sctp_ill_flags &
616 			    PHYI_LOOPBACK) && skip_lback) {
617 				if (scanned >= sctp->sctp_nsaddrs)
618 					goto done;
619 
620 				/* LINTED: alignment */
621 				pnode = list_d2l(&sctp->sctp_saddrs[i].
622 				    sctp_ipif_list, pobj);
623 				if (mdb_vread(&node, sizeof (list_node_t),
624 				    (uintptr_t)pnode) == -1) {
625 					mdb_warn("failed to read list_node_t");
626 					return (err);
627 				}
628 				pobj = list_object(&sctp->sctp_saddrs[i].
629 				    sctp_ipif_list, node.list_next);
630 				if (mdb_vread(&obj, sizeof (sctp_saddr_ipif_t),
631 				    (uintptr_t)pobj) == -1) {
632 					mdb_warn("failed to read "
633 					    "sctp_saddr_ipif_t");
634 					return (err);
635 				}
636 				continue;
637 			}
638 
639 			switch (sctp->sctp_family) {
640 			case AF_INET:
641 				/* LINTED: alignment */
642 				sin4 = (sin_t *)addr;
643 				if ((sctp->sctp_state <= SCTPS_LISTEN) &&
644 				    sctp->sctp_bound_to_all) {
645 					sin4->sin_addr.s_addr = INADDR_ANY;
646 					sin4->sin_port = sctp->sctp_lport;
647 				} else {
648 					sin4 += added;
649 					sin4->sin_family = AF_INET;
650 					sin4->sin_port = sctp->sctp_lport;
651 					IN6_V4MAPPED_TO_INADDR(&laddr,
652 					    &sin4->sin_addr);
653 				}
654 				break;
655 
656 			case AF_INET6:
657 				/* LINTED: alignment */
658 				sin6 = (sin6_t *)addr;
659 				if ((sctp->sctp_state <= SCTPS_LISTEN) &&
660 				    sctp->sctp_bound_to_all) {
661 					bzero(&sin6->sin6_addr,
662 					    sizeof (sin6->sin6_addr));
663 					sin6->sin6_port = sctp->sctp_lport;
664 				} else {
665 					sin6 += added;
666 					sin6->sin6_family = AF_INET6;
667 					sin6->sin6_port = sctp->sctp_lport;
668 					sin6->sin6_addr = laddr;
669 				}
670 				sin6->sin6_flowinfo = sctp->sctp_ip6h->ip6_vcf &
671 				    ~IPV6_VERS_AND_FLOW_MASK;
672 				sin6->sin6_scope_id = 0;
673 				sin6->__sin6_src_id = 0;
674 				break;
675 			}
676 			added++;
677 			if (added >= 1) {
678 				err = 0;
679 				goto done;
680 			}
681 			if (scanned >= sctp->sctp_nsaddrs)
682 				goto done;
683 
684 			/* LINTED: alignment */
685 			pnode = list_d2l(&sctp->sctp_saddrs[i].sctp_ipif_list,
686 			    pobj);
687 			if (mdb_vread(&node, sizeof (list_node_t),
688 			    (uintptr_t)pnode) == -1) {
689 				mdb_warn("failed to read list_node_t");
690 				return (err);
691 			}
692 			pobj = list_object(&sctp->sctp_saddrs[i].
693 			    sctp_ipif_list, node.list_next);
694 			if (mdb_vread(&obj, sizeof (sctp_saddr_ipif_t),
695 			    (uintptr_t)pobj) == -1) {
696 				mdb_warn("failed to read sctp_saddr_ipif_t");
697 				return (err);
698 			}
699 		}
700 	}
701 done:
702 	return (err);
703 }
704 
705 /*
706  * SCTP interface for geting the primary peer address of a sctp_t.
707  */
708 static int
709 sctp_getpeeraddr(sctp_t *sctp, struct sockaddr *addr)
710 {
711 	struct sockaddr_in	*sin4;
712 	struct sockaddr_in6	*sin6;
713 	sctp_faddr_t		sctp_primary;
714 	in6_addr_t		faddr;
715 
716 	if (sctp->sctp_faddrs == NULL)
717 		return (-1);
718 
719 	addr->sa_family = sctp->sctp_family;
720 	if (mdb_vread(&sctp_primary, sizeof (sctp_faddr_t),
721 	    (uintptr_t)sctp->sctp_primary) == -1) {
722 		mdb_warn("failed to read sctp primary faddr");
723 		return (-1);
724 	}
725 	faddr = sctp_primary.faddr;
726 
727 	switch (sctp->sctp_family) {
728 	case AF_INET:
729 		/* LINTED: alignment */
730 		sin4 = (struct sockaddr_in *)addr;
731 		IN6_V4MAPPED_TO_INADDR(&faddr, &sin4->sin_addr);
732 		sin4->sin_port = sctp->sctp_fport;
733 		sin4->sin_family = AF_INET;
734 		break;
735 
736 	case AF_INET6:
737 		/* LINTED: alignment */
738 		sin6 = (struct sockaddr_in6 *)addr;
739 		sin6->sin6_addr = faddr;
740 		sin6->sin6_port = sctp->sctp_fport;
741 		sin6->sin6_family = AF_INET6;
742 		sin6->sin6_flowinfo = 0;
743 		sin6->sin6_scope_id = 0;
744 		sin6->__sin6_src_id = 0;
745 		break;
746 	}
747 
748 	return (0);
749 }
750 
751 static int
752 tpi_sock_print(sotpi_sonode_t *sotpi_sonode)
753 {
754 	if (sotpi_sonode->st_info.sti_laddr_valid == 1) {
755 		struct sockaddr *laddr =
756 		    mdb_alloc(sotpi_sonode->st_info.sti_laddr_len, UM_SLEEP);
757 		if (mdb_vread(laddr, sotpi_sonode->st_info.sti_laddr_len,
758 		    (uintptr_t)sotpi_sonode->st_info.sti_laddr_sa) == -1) {
759 			mdb_warn("failed to read sotpi_sonode socket addr");
760 			return (-1);
761 		}
762 
763 		mdb_printf("socket: ");
764 		pfiles_print_addr(laddr);
765 	}
766 
767 	if (sotpi_sonode->st_info.sti_faddr_valid == 1) {
768 		struct sockaddr *faddr =
769 		    mdb_alloc(sotpi_sonode->st_info.sti_faddr_len, UM_SLEEP);
770 		if (mdb_vread(faddr, sotpi_sonode->st_info.sti_faddr_len,
771 		    (uintptr_t)sotpi_sonode->st_info.sti_faddr_sa) == -1) {
772 			mdb_warn("failed to read sotpi_sonode remote addr");
773 			return (-1);
774 		}
775 
776 		mdb_printf("remote: ");
777 		pfiles_print_addr(faddr);
778 	}
779 
780 	return (0);
781 }
782 
783 static int
784 tcpip_sock_print(struct sonode *socknode)
785 {
786 	switch (socknode->so_family) {
787 	case AF_INET:
788 	{
789 		conn_t conn_t;
790 		in_port_t port;
791 
792 		if (mdb_vread(&conn_t, sizeof (conn_t),
793 		    (uintptr_t)socknode->so_proto_handle) == -1) {
794 			mdb_warn("failed to read conn_t V4");
795 			return (-1);
796 		}
797 
798 		mdb_printf("socket: ");
799 		mdb_nhconvert(&port, &conn_t.conn_lport, sizeof (port));
800 		mdb_printf("AF_INET %I %d ", conn_t.conn_src, port);
801 
802 		/*
803 		 * If this is a listening socket, we don't print
804 		 * the remote address.
805 		 */
806 		if (IPCL_IS_TCP(&conn_t) && IPCL_IS_BOUND(&conn_t) == 0 ||
807 		    IPCL_IS_UDP(&conn_t) && IPCL_IS_CONNECTED(&conn_t)) {
808 			mdb_printf("remote: ");
809 			mdb_nhconvert(&port, &conn_t.conn_fport, sizeof (port));
810 			mdb_printf("AF_INET %I %d ", conn_t.conn_rem, port);
811 		}
812 
813 		break;
814 	}
815 
816 	case AF_INET6:
817 	{
818 		conn_t conn_t;
819 		in_port_t port;
820 
821 		if (mdb_vread(&conn_t, sizeof (conn_t),
822 		    (uintptr_t)socknode->so_proto_handle) == -1) {
823 			mdb_warn("failed to read conn_t V6");
824 			return (-1);
825 		}
826 
827 		mdb_printf("socket: ");
828 		mdb_nhconvert(&port, &conn_t.conn_lport, sizeof (port));
829 		mdb_printf("AF_INET6 %N %d ", &conn_t.conn_srcv6, port);
830 
831 		/*
832 		 * If this is a listening socket, we don't print
833 		 * the remote address.
834 		 */
835 		if (IPCL_IS_TCP(&conn_t) && IPCL_IS_BOUND(&conn_t) == 0 ||
836 		    IPCL_IS_UDP(&conn_t) && IPCL_IS_CONNECTED(&conn_t)) {
837 			mdb_printf("remote: ");
838 			mdb_nhconvert(&port, &conn_t.conn_fport, sizeof (port));
839 			mdb_printf("AF_INET6 %N %d ", &conn_t.conn_remv6, port);
840 		}
841 
842 		break;
843 	}
844 
845 	default:
846 		mdb_printf("AF_?? (%d)", socknode->so_family);
847 		break;
848 	}
849 
850 	return (0);
851 }
852 
853 static int
854 sctp_sock_print(struct sonode *socknode)
855 {
856 	sctp_t sctp_t;
857 
858 	struct sockaddr *laddr = mdb_alloc(sizeof (struct sockaddr), UM_SLEEP);
859 	struct sockaddr *faddr = mdb_alloc(sizeof (struct sockaddr), UM_SLEEP);
860 
861 	if (mdb_vread(&sctp_t, sizeof (sctp_t),
862 	    (uintptr_t)socknode->so_proto_handle) == -1) {
863 		mdb_warn("failed to read sctp_t");
864 		return (-1);
865 	}
866 
867 	if (sctp_getsockaddr(&sctp_t, laddr) == 0) {
868 		mdb_printf("socket:");
869 		pfiles_print_addr(laddr);
870 	}
871 	if (sctp_getpeeraddr(&sctp_t, faddr) == 0) {
872 		mdb_printf("remote:");
873 		pfiles_print_addr(faddr);
874 	}
875 
876 	return (0);
877 }
878 
879 /* ARGSUSED */
880 static int
881 sdp_sock_print(struct sonode *socknode)
882 {
883 	return (0);
884 }
885 
886 struct sock_print {
887 	int	family;
888 	int	type;
889 	int	pro;
890 	int	(*print)(struct sonode *socknode);
891 } sock_prints[] = {
892 	{ 2,	2,	0,	tcpip_sock_print },	/* /dev/tcp	*/
893 	{ 2,	2,	6,	tcpip_sock_print },	/* /dev/tcp	*/
894 	{ 26,	2,	0,	tcpip_sock_print },	/* /dev/tcp6	*/
895 	{ 26,	2,	6,	tcpip_sock_print },	/* /dev/tcp6	*/
896 	{ 2,	1,	0,	tcpip_sock_print },	/* /dev/udp	*/
897 	{ 2,	1,	17,	tcpip_sock_print },	/* /dev/udp	*/
898 	{ 26,	1,	0,	tcpip_sock_print },	/* /dev/udp6	*/
899 	{ 26,	1,	17,	tcpip_sock_print },	/* /dev/udp6	*/
900 	{ 2,	4,	0,	tcpip_sock_print },	/* /dev/rawip	*/
901 	{ 26,	4,	0,	tcpip_sock_print },	/* /dev/rawip6	*/
902 	{ 2,	2,	132,	sctp_sock_print },	/* /dev/sctp	*/
903 	{ 26,	2,	132,	sctp_sock_print },	/* /dev/sctp6	*/
904 	{ 2,	6,	132,	sctp_sock_print },	/* /dev/sctp	*/
905 	{ 26,	6,	132,	sctp_sock_print },	/* /dev/sctp6	*/
906 	{ 24,	4,	0,	tcpip_sock_print },	/* /dev/rts	*/
907 	{ 2,	2,	257,	sdp_sock_print },	/* /dev/sdp	*/
908 	{ 26,	2,	257,	sdp_sock_print },	/* /dev/sdp	*/
909 };
910 
911 #define	NUM_SOCK_PRINTS                                         \
912 	(sizeof (sock_prints) / sizeof (struct sock_print))
913 
914 static int
915 pfile_callback(uintptr_t addr, const struct file *f, struct pfiles_cbdata *cb)
916 {
917 	vnode_t v, layer_vn;
918 	int myfd = cb->fd;
919 	const char *type;
920 	char path[MAXPATHLEN];
921 	uintptr_t top_vnodep, realvpp;
922 	char fsname[_ST_FSTYPSZ];
923 	int err, i;
924 
925 	cb->fd++;
926 
927 	if (addr == NULL) {
928 		return (WALK_NEXT);
929 	}
930 
931 	top_vnodep = realvpp = (uintptr_t)f->f_vnode;
932 
933 	if (mdb_vread(&v, sizeof (v), realvpp) == -1) {
934 		mdb_warn("failed to read vnode");
935 		return (DCMD_ERR);
936 	}
937 
938 	type = "?";
939 	for (i = 0; i <= NUM_FS_TYPES; i++) {
940 		if (fs_types[i].type == v.v_type)
941 			type = fs_types[i].name;
942 	}
943 
944 	do {
945 		uintptr_t next_realvpp;
946 
947 		err = next_realvp(realvpp, &layer_vn, &next_realvpp);
948 		if (next_realvpp != NULL)
949 			realvpp = next_realvpp;
950 
951 	} while (err == REALVP_CONTINUE);
952 
953 	if (err == REALVP_ERR) {
954 		mdb_warn("failed to do realvp() for %p", realvpp);
955 		return (DCMD_ERR);
956 	}
957 
958 	if (read_fsname((uintptr_t)layer_vn.v_vfsp, fsname) == -1)
959 		return (DCMD_ERR);
960 
961 	mdb_printf("%4d %4s %?0p ", myfd, type, top_vnodep);
962 
963 	if (cb->opt_p) {
964 		if (pfiles_dig_pathname(top_vnodep, path) == -1)
965 			return (DCMD_ERR);
966 
967 		mdb_printf("%s\n", path);
968 		return (DCMD_OK);
969 	}
970 
971 	/*
972 	 * Sockets generally don't have interesting pathnames; we only
973 	 * show those in the '-p' view.
974 	 */
975 	path[0] = '\0';
976 	if (v.v_type != VSOCK) {
977 		if (pfiles_dig_pathname(top_vnodep, path) == -1)
978 			return (DCMD_ERR);
979 	}
980 	mdb_printf("%s%s", path, path[0] == '\0' ? "" : " ");
981 
982 	switch (v.v_type) {
983 	case VDOOR:
984 	{
985 		door_node_t doornode;
986 		proc_t pr;
987 
988 		if (mdb_vread(&doornode, sizeof (doornode),
989 		    (uintptr_t)layer_vn.v_data) == -1) {
990 			mdb_warn("failed to read door_node");
991 			return (DCMD_ERR);
992 		}
993 
994 		if (mdb_vread(&pr, sizeof (pr),
995 		    (uintptr_t)doornode.door_target) == -1) {
996 			mdb_warn("failed to read door server process %p",
997 			    doornode.door_target);
998 			return (DCMD_ERR);
999 		}
1000 		mdb_printf("[door to '%s' (proc=%p)]", pr.p_user.u_comm,
1001 		    doornode.door_target);
1002 		break;
1003 	}
1004 
1005 	case VSOCK:
1006 	{
1007 		vnode_t v_sock;
1008 		struct sonode so;
1009 
1010 		if (mdb_vread(&v_sock, sizeof (v_sock), realvpp) == -1) {
1011 			mdb_warn("failed to read socket vnode");
1012 			return (DCMD_ERR);
1013 		}
1014 
1015 		/*
1016 		 * Sockets can be non-stream or stream, they have to be dealed
1017 		 * with differently.
1018 		 */
1019 		if (v_sock.v_stream == NULL) {
1020 			if (pfiles_get_sonode(&v_sock, &so) == -1)
1021 				return (DCMD_ERR);
1022 
1023 			/* Pick the proper methods. */
1024 			for (i = 0; i <= NUM_SOCK_PRINTS; i++) {
1025 				if ((sock_prints[i].family == so.so_family &&
1026 				    sock_prints[i].type == so.so_type &&
1027 				    sock_prints[i].pro == so.so_protocol) ||
1028 				    (sock_prints[i].family == so.so_family &&
1029 				    sock_prints[i].type == so.so_type &&
1030 				    so.so_type == SOCK_RAW)) {
1031 					if ((*sock_prints[i].print)(&so) == -1)
1032 						return (DCMD_ERR);
1033 				}
1034 			}
1035 		} else {
1036 			sotpi_sonode_t sotpi_sonode;
1037 
1038 			if (pfiles_get_sonode(&v_sock, &so) == -1)
1039 				return (DCMD_ERR);
1040 
1041 			/*
1042 			 * If the socket is a fallback socket, read its related
1043 			 * information separately; otherwise, read it as a whole
1044 			 * tpi socket.
1045 			 */
1046 			if (so.so_state & SS_FALLBACK_COMP) {
1047 				sotpi_sonode.st_sonode = so;
1048 
1049 				if (mdb_vread(&(sotpi_sonode.st_info),
1050 				    sizeof (sotpi_info_t),
1051 				    (uintptr_t)so.so_priv) == -1)
1052 					return (DCMD_ERR);
1053 			} else {
1054 				if (pfiles_get_tpi_sonode(&v_sock,
1055 				    &sotpi_sonode) == -1)
1056 					return (DCMD_ERR);
1057 			}
1058 
1059 			if (tpi_sock_print(&sotpi_sonode) == -1)
1060 				return (DCMD_ERR);
1061 		}
1062 
1063 		break;
1064 	}
1065 
1066 	case VPORT:
1067 		mdb_printf("[event port (port=%p)]", v.v_data);
1068 		break;
1069 
1070 	case VPROC:
1071 	{
1072 		prnode_t prnode;
1073 		prcommon_t prcommon;
1074 
1075 		if (mdb_vread(&prnode, sizeof (prnode),
1076 		    (uintptr_t)layer_vn.v_data) == -1) {
1077 			mdb_warn("failed to read prnode");
1078 			return (DCMD_ERR);
1079 		}
1080 
1081 		if (mdb_vread(&prcommon, sizeof (prcommon),
1082 		    (uintptr_t)prnode.pr_common) == -1) {
1083 			mdb_warn("failed to read prcommon %p",
1084 			    prnode.pr_common);
1085 			return (DCMD_ERR);
1086 		}
1087 
1088 		mdb_printf("(proc=%p)", prcommon.prc_proc);
1089 		break;
1090 	}
1091 
1092 	default:
1093 		break;
1094 	}
1095 
1096 	mdb_printf("\n");
1097 
1098 	return (WALK_NEXT);
1099 }
1100 
1101 static int
1102 file_t_callback(uintptr_t addr, const struct file *f, struct pfiles_cbdata *cb)
1103 {
1104 	int myfd = cb->fd;
1105 
1106 	cb->fd++;
1107 
1108 	if (addr == NULL) {
1109 		return (WALK_NEXT);
1110 	}
1111 
1112 	/*
1113 	 * We really need 20 digits to print a 64-bit offset_t, but this
1114 	 * is exceedingly rare, so we cheat and assume a column width of 10
1115 	 * digits, in order to fit everything cleanly into 80 columns.
1116 	 */
1117 	mdb_printf("%?0p %4d %8x %?0p %10lld %?0p %4d\n",
1118 	    addr, myfd, f->f_flag, f->f_vnode, f->f_offset, f->f_cred,
1119 	    f->f_count);
1120 
1121 	return (WALK_NEXT);
1122 }
1123 
1124 int
1125 pfiles(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1126 {
1127 	int opt_f = 0;
1128 
1129 	struct pfiles_cbdata cb;
1130 
1131 	bzero(&cb, sizeof (cb));
1132 
1133 	if (!(flags & DCMD_ADDRSPEC))
1134 		return (DCMD_USAGE);
1135 
1136 	if (mdb_getopts(argc, argv,
1137 	    'p', MDB_OPT_SETBITS, TRUE, &cb.opt_p,
1138 	    'f', MDB_OPT_SETBITS, TRUE, &opt_f, NULL) != argc)
1139 		return (DCMD_USAGE);
1140 
1141 	if (opt_f) {
1142 		mdb_printf("%<u>%?s %4s %8s %?s %10s %?s %4s%</u>\n", "FILE",
1143 		    "FD", "FLAG", "VNODE", "OFFSET", "CRED", "CNT");
1144 		if (mdb_pwalk("allfile", (mdb_walk_cb_t)file_t_callback, &cb,
1145 		    addr) == -1) {
1146 			mdb_warn("failed to walk 'allfile'");
1147 			return (DCMD_ERR);
1148 		}
1149 	} else {
1150 		mdb_printf("%<u>%-4s %4s %?s ", "FD", "TYPE", "VNODE");
1151 		if (cb.opt_p)
1152 			mdb_printf("PATH");
1153 		else
1154 			mdb_printf("INFO");
1155 		mdb_printf("%</u>\n");
1156 
1157 		if (mdb_pwalk("allfile", (mdb_walk_cb_t)pfile_callback, &cb,
1158 		    addr) == -1) {
1159 			mdb_warn("failed to walk 'allfile'");
1160 			return (DCMD_ERR);
1161 		}
1162 	}
1163 
1164 
1165 	return (DCMD_OK);
1166 }
1167 
1168 void
1169 pfiles_help(void)
1170 {
1171 	mdb_printf(
1172 	    "Given the address of a process, print information about files\n"
1173 	    "which the process has open.  By default, this includes decoded\n"
1174 	    "information about the file depending on file and filesystem type\n"
1175 	    "\n"
1176 	    "\t-p\tPathnames; omit decoded information.  Only display "
1177 	    "pathnames\n"
1178 	    "\t-f\tfile_t view; show the file_t structure corresponding to "
1179 	    "the fd\n");
1180 }
1181