Home | History | Annotate | Download | only in fs.d
      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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
     22 /*	  All Rights Reserved  	*/
     23 
     24 
     25 /*
     26  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     27  * Use is subject to license terms.
     28  */
     29 
     30 
     31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     32 
     33 #include <dlfcn.h>
     34 #include <stdio.h>
     35 #include <stdarg.h>
     36 #include <string.h>
     37 #include <locale.h>
     38 #include <libintl.h>
     39 #include <stdlib.h>
     40 #include <ftw.h>
     41 #include <errno.h>
     42 #include <sys/types.h>
     43 #include <unistd.h>
     44 #include <sys/statvfs.h>
     45 #include <sys/stat.h>
     46 #include <sys/param.h>
     47 #include <sys/mnttab.h>
     48 #include <sys/mntent.h>
     49 #include <sys/vfstab.h>
     50 #include <sys/wait.h>
     51 #include <sys/mkdev.h>
     52 #include <sys/int_limits.h>
     53 #include <sys/zone.h>
     54 #include <libzfs.h>
     55 
     56 #include "fslib.h"
     57 
     58 extern char *default_fstype(char *);
     59 
     60 /*
     61  * General notice:
     62  * String pointers in this code may point to statically allocated memory
     63  * or dynamically allocated memory. Furthermore, a dynamically allocated
     64  * string may be pointed to by more than one pointer. This does not pose
     65  * a problem because malloc'ed memory is never free'd (so we don't need
     66  * to remember which pointers point to malloc'ed memory).
     67  */
     68 
     69 /*
     70  * TRANSLATION_NOTE
     71  * Only strings passed as arguments to the TRANSLATE macro need to
     72  * be translated.
     73  */
     74 
     75 #ifndef MNTTYPE_LOFS
     76 #define	MNTTYPE_LOFS		"lofs"
     77 #endif
     78 
     79 #define	EQ(s1, s2)		(strcmp(s1, s2) == 0)
     80 #define	NEW(type)		xmalloc(sizeof (type))
     81 #define	CLEAR(var)		(void) memset(&(var), 0, sizeof (var))
     82 #define	MAX(a, b)		((a) > (b) ? (a) : (b))
     83 #define	MAX3(a, b, c)		MAX(a, MAX(b, c))
     84 #define	TRANSLATE(s)		new_string(gettext(s))
     85 
     86 #define	MAX_OPTIONS		36
     87 #define	N_FSTYPES		20
     88 #define	MOUNT_TABLE_ENTRIES	40	/* initial allocation */
     89 #define	MSGBUF_SIZE		1024
     90 #define	LINEBUF_SIZE		256	/* either input or output lines */
     91 
     92 #define	BLOCK_SIZE		512	/* when reporting in terms of blocks */
     93 
     94 #define	DEVNM_CMD		"devnm"
     95 #define	FS_LIBPATH		"/usr/lib/fs/"
     96 #define	MOUNT_TAB		"/etc/mnttab"
     97 #define	VFS_TAB			"/etc/vfstab"
     98 #define	REMOTE_FS		"/etc/dfs/fstypes"
     99 
    100 #define	NUL			'\0'
    101 #define	FALSE			0
    102 #define	TRUE			1
    103 
    104 /*
    105  * Formatting constants
    106  */
    107 #define	IBCS2_FILESYSTEM_WIDTH	15	/* Truncate to match ISC/SCO */
    108 #define	IBCS2_MOUNT_POINT_WIDTH	10	/* Truncate to match ISC/SCO */
    109 #define	FILESYSTEM_WIDTH	20
    110 #define	MOUNT_POINT_WIDTH	19
    111 #define	SPECIAL_DEVICE_WIDTH	18
    112 #define	FSTYPE_WIDTH		8
    113 #define	BLOCK_WIDTH		8
    114 #define	NFILES_WIDTH		8
    115 #ifdef XPG4
    116 #define	KBYTE_WIDTH		11
    117 #define	AVAILABLE_WIDTH		10
    118 #else
    119 #define	KBYTE_WIDTH		7
    120 #define	AVAILABLE_WIDTH		6
    121 #endif
    122 #define	SCALED_WIDTH		6
    123 #define	CAPACITY_WIDTH		9
    124 #define	BSIZE_WIDTH		6
    125 #define	FRAGSIZE_WIDTH		7
    126 #define	FSID_WIDTH		7
    127 #define	FLAG_WIDTH		8
    128 #define	NAMELEN_WIDTH		7
    129 #define	MNT_SPEC_WIDTH		MOUNT_POINT_WIDTH + SPECIAL_DEVICE_WIDTH + 2
    130 
    131 /*
    132  * Flags for the errmsg() function
    133  */
    134 #define	ERR_NOFLAGS		0x0
    135 #define	ERR_NONAME		0x1	/* don't include the program name */
    136 					/* as a prefix */
    137 #define	ERR_FATAL		0x2	/* call exit after printing the */
    138 					/* message */
    139 #define	ERR_PERROR		0x4	/* append an errno explanation to */
    140 					/* the message */
    141 #define	ERR_USAGE		0x8	/* print the usage line after the */
    142 					/* message */
    143 
    144 #define	NUMBER_WIDTH		40
    145 
    146 /*
    147  * A numbuf_t is used when converting a number to a string representation
    148  */
    149 typedef char numbuf_t[ NUMBER_WIDTH ];
    150 
    151 /*
    152  * We use bool_int instead of int to make clear which variables are
    153  * supposed to be boolean
    154  */
    155 typedef int bool_int;
    156 
    157 struct mtab_entry {
    158 	bool_int	mte_dev_is_valid;
    159 	dev_t		mte_dev;
    160 	bool_int	mte_ignore;	/* the "ignore" option was set */
    161 	struct extmnttab	*mte_mount;
    162 };
    163 
    164 
    165 struct df_request {
    166 	bool_int		dfr_valid;
    167 	char			*dfr_cmd_arg;	/* what the user specified */
    168 	struct mtab_entry	*dfr_mte;
    169 	char			*dfr_fstype;
    170 	int			dfr_index;	/* to make qsort stable	*/
    171 };
    172 
    173 #define	DFR_MOUNT_POINT(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_mountp
    174 #define	DFR_SPECIAL(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_special
    175 #define	DFR_FSTYPE(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_fstype
    176 #define	DFR_ISMOUNTEDFS(dfrp)	((dfrp)->dfr_mte != NULL)
    177 
    178 #define	DFRP(p)			((struct df_request *)(p))
    179 
    180 typedef void (*output_func)(struct df_request *, struct statvfs64 *);
    181 
    182 struct df_output {
    183 	output_func	dfo_func;	/* function that will do the output */
    184 	int		dfo_flags;
    185 };
    186 
    187 /*
    188  * Output flags
    189  */
    190 #define	DFO_NOFLAGS	0x0
    191 #define	DFO_HEADER	0x1		/* output preceded by header */
    192 #define	DFO_STATVFS	0x2		/* must do a statvfs64(2) */
    193 
    194 
    195 static char	*program_name;
    196 static char	df_options[MAX_OPTIONS] = "-";
    197 static size_t	df_options_len = 1;
    198 static char	*o_option_arg;			/* arg to the -o option */
    199 static char	*FSType;
    200 static char	*remote_fstypes[N_FSTYPES+1];	/* allocate an extra one */
    201 						/* to use as a terminator */
    202 
    203 /*
    204  * The following three variables support an in-memory copy of the mount table
    205  * to speedup searches.
    206  */
    207 static struct mtab_entry	*mount_table;	/* array of mtab_entry's */
    208 static size_t			mount_table_entries;
    209 static size_t			mount_table_allocated_entries;
    210 
    211 static bool_int		F_option;
    212 static bool_int		V_option;
    213 static bool_int		P_option;	/* Added for XCU4 compliance */
    214 static bool_int		Z_option;
    215 static bool_int		v_option;
    216 #ifdef	_iBCS2
    217 char			*sysv3_set;
    218 #endif /* _iBCS2 */
    219 static bool_int		a_option;
    220 static bool_int		b_option;
    221 static bool_int		e_option;
    222 static bool_int		g_option;
    223 static bool_int		h_option;
    224 static bool_int		k_option;
    225 static bool_int		l_option;
    226 static bool_int		n_option;
    227 static bool_int		t_option;
    228 static bool_int		o_option;
    229 
    230 static bool_int		tty_output;
    231 static bool_int		use_scaling;
    232 static int		scale;
    233 
    234 static void usage(void);
    235 static void do_devnm(int, char **);
    236 static void do_df(int, char **)	__NORETURN;
    237 static void parse_options(int, char **);
    238 static char *basename(char *);
    239 
    240 static libzfs_handle_t *(*_libzfs_init)(boolean_t);
    241 static zfs_handle_t *(*_zfs_open)(libzfs_handle_t *, const char *, int);
    242 static void (*_zfs_close)(zfs_handle_t *);
    243 static uint64_t (*_zfs_prop_get_int)(zfs_handle_t *, zfs_prop_t);
    244 static libzfs_handle_t *g_zfs;
    245 
    246 /*
    247  * Dynamically check for libzfs, in case the user hasn't installed the SUNWzfs
    248  * packages.  A basic utility such as df shouldn't depend on optional
    249  * filesystems.
    250  */
    251 static boolean_t
    252 load_libzfs(void)
    253 {
    254 	void *hdl;
    255 
    256 	if (_libzfs_init != NULL)
    257 		return (g_zfs != NULL);
    258 
    259 	if ((hdl = dlopen("libzfs.so", RTLD_LAZY)) != NULL) {
    260 		_libzfs_init = (libzfs_handle_t *(*)(boolean_t))dlsym(hdl,
    261 		    "libzfs_init");
    262 		_zfs_open = (zfs_handle_t *(*)())dlsym(hdl, "zfs_open");
    263 		_zfs_close = (void (*)())dlsym(hdl, "zfs_close");
    264 		_zfs_prop_get_int = (uint64_t (*)())
    265 		    dlsym(hdl, "zfs_prop_get_int");
    266 
    267 		if (_libzfs_init != NULL) {
    268 			assert(_zfs_open != NULL);
    269 			assert(_zfs_close != NULL);
    270 			assert(_zfs_prop_get_int != NULL);
    271 
    272 			g_zfs = _libzfs_init(B_FALSE);
    273 		}
    274 	}
    275 
    276 	return (g_zfs != NULL);
    277 }
    278 
    279 int
    280 main(int argc, char *argv[])
    281 {
    282 	(void) setlocale(LC_ALL, "");
    283 
    284 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
    285 #define	TEXT_DOMAIN "SYS_TEST"
    286 #endif
    287 	(void) textdomain(TEXT_DOMAIN);
    288 
    289 	program_name = basename(argv[0]);
    290 
    291 #ifdef	_iBCS2
    292 	sysv3_set = getenv("SYSV3");
    293 #endif	/* _iBCS2 */
    294 
    295 	if (EQ(program_name, DEVNM_CMD))
    296 		do_devnm(argc, argv);
    297 
    298 	parse_options(argc, argv);
    299 
    300 	/*
    301 	 * The k_option implies SunOS 4.x compatibility: when the special
    302 	 * device name is too long the line will be split except when the
    303 	 * output has been redirected.
    304 	 * This is also valid for the -h option.
    305 	 */
    306 
    307 	if (use_scaling || k_option || P_option || v_option)
    308 		tty_output = isatty(1);
    309 
    310 	do_df(argc - optind, &argv[optind]);
    311 	/* NOTREACHED */
    312 }
    313 
    314 
    315 /*
    316  * Prints an error message to stderr.
    317  */
    318 /* VARARGS2 */
    319 static void
    320 errmsg(int flags, char *fmt, ...)
    321 {
    322 	char buf[MSGBUF_SIZE];
    323 	va_list ap;
    324 	int cc;
    325 	int offset;
    326 
    327 	if (flags & ERR_NONAME)
    328 		offset = 0;
    329 	else
    330 		offset = sprintf(buf, "%s: ", program_name);
    331 
    332 	va_start(ap, fmt);
    333 	cc = vsprintf(&buf[offset], gettext(fmt), ap);
    334 	offset += cc;
    335 	va_end(ap);
    336 
    337 	if (flags & ERR_PERROR) {
    338 		if (buf[offset-1] != ' ')
    339 			(void) strcat(buf, " ");
    340 		(void) strcat(buf, strerror(errno));
    341 	}
    342 	(void) fprintf(stderr, "%s\n", buf);
    343 	if (flags & ERR_USAGE)
    344 		usage();
    345 	if (flags & ERR_FATAL)
    346 		exit(1);
    347 }
    348 
    349 
    350 static void
    351 usage(void)
    352 {
    353 #ifdef  XPG4
    354 	errmsg(ERR_NONAME,
    355 	    "Usage: %s [-F FSType] [-abeghklntPVZ] [-o FSType-specific_options]"
    356 	    " [directory | block_device | resource]", program_name);
    357 #else
    358 	errmsg(ERR_NONAME,
    359 	    "Usage: %s [-F FSType] [-abeghklntVvZ] [-o FSType-specific_options]"
    360 	    " [directory | block_device | resource]", program_name);
    361 #endif
    362 	exit(1);
    363 	/* NOTREACHED */
    364 }
    365 
    366 
    367 static char *
    368 new_string(char *s)
    369 {
    370 	char *p = NULL;
    371 
    372 	if (s) {
    373 		p = strdup(s);
    374 		if (p)
    375 			return (p);
    376 		errmsg(ERR_FATAL, "out of memory");
    377 		/* NOTREACHED */
    378 	}
    379 	return (p);
    380 }
    381 
    382 
    383 /*
    384  * Allocate memory using malloc but terminate if the allocation fails
    385  */
    386 static void *
    387 xmalloc(size_t size)
    388 {
    389 	void *p = malloc(size);
    390 
    391 	if (p)
    392 		return (p);
    393 	errmsg(ERR_FATAL, "out of memory");
    394 	/* NOTREACHED */
    395 	return (NULL);
    396 }
    397 
    398 
    399 /*
    400  * Allocate memory using realloc but terminate if the allocation fails
    401  */
    402 static void *
    403 xrealloc(void *ptr, size_t size)
    404 {
    405 	void *p = realloc(ptr, size);
    406 
    407 	if (p)
    408 		return (p);
    409 	errmsg(ERR_FATAL, "out of memory");
    410 	/* NOTREACHED */
    411 	return (NULL);
    412 }
    413 
    414 
    415 /*
    416  * fopen the specified file for reading but terminate if the fopen fails
    417  */
    418 static FILE *
    419 xfopen(char *file)
    420 {
    421 	FILE *fp = fopen(file, "r");
    422 
    423 	if (fp == NULL)
    424 		errmsg(ERR_FATAL + ERR_PERROR, "failed to open %s:", file);
    425 	return (fp);
    426 }
    427 
    428 
    429 /*
    430  * Read remote file system types from REMOTE_FS into the
    431  * remote_fstypes array.
    432  */
    433 static void
    434 init_remote_fs(void)
    435 {
    436 	FILE	*fp;
    437 	char	line_buf[LINEBUF_SIZE];
    438 	size_t	fstype_index = 0;
    439 
    440 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
    441 		errmsg(ERR_NOFLAGS,
    442 		    "Warning: can't open %s, ignored", REMOTE_FS);
    443 		return;
    444 	}
    445 
    446 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
    447 		char buf[LINEBUF_SIZE];
    448 
    449 		(void) sscanf(line_buf, "%s", buf);
    450 		remote_fstypes[fstype_index++] = new_string(buf);
    451 
    452 		if (fstype_index == N_FSTYPES)
    453 			break;
    454 	}
    455 	(void) fclose(fp);
    456 }
    457 
    458 
    459 /*
    460  * Returns TRUE if fstype is a remote file system type;
    461  * otherwise, returns FALSE.
    462  */
    463 static int
    464 is_remote_fs(char *fstype)
    465 {
    466 	char **p;
    467 	static bool_int remote_fs_initialized;
    468 
    469 	if (! remote_fs_initialized) {
    470 		init_remote_fs();
    471 		remote_fs_initialized = TRUE;
    472 	}
    473 
    474 	for (p = remote_fstypes; *p; p++)
    475 		if (EQ(fstype, *p))
    476 			return (TRUE);
    477 	return (FALSE);
    478 }
    479 
    480 
    481 static char *
    482 basename(char *s)
    483 {
    484 	char *p = strrchr(s, '/');
    485 
    486 	return (p ? p+1 : s);
    487 }
    488 
    489 
    490 /*
    491  * Create a new "struct extmnttab" and make sure that its fields point
    492  * to malloc'ed memory
    493  */
    494 static struct extmnttab *
    495 mntdup(struct extmnttab *old)
    496 {
    497 	struct extmnttab *new = NEW(struct extmnttab);
    498 
    499 	new->mnt_special = new_string(old->mnt_special);
    500 	new->mnt_mountp  = new_string(old->mnt_mountp);
    501 	new->mnt_fstype  = new_string(old->mnt_fstype);
    502 	new->mnt_mntopts = new_string(old->mnt_mntopts);
    503 	new->mnt_time    = new_string(old->mnt_time);
    504 	new->mnt_major   = old->mnt_major;
    505 	new->mnt_minor   = old->mnt_minor;
    506 	return (new);
    507 }
    508 
    509 
    510 static void
    511 mtab_error(char *mtab_file, int status)
    512 {
    513 	if (status == MNT_TOOLONG)
    514 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
    515 		    mtab_file, MNT_LINE_MAX);
    516 	else if (status == MNT_TOOMANY)
    517 		errmsg(ERR_NOFLAGS,
    518 		    "a line in %s has too many fields", mtab_file);
    519 	else if (status == MNT_TOOFEW)
    520 		errmsg(ERR_NOFLAGS,
    521 		    "a line in %s has too few fields", mtab_file);
    522 	else
    523 		errmsg(ERR_NOFLAGS,
    524 		    "error while reading %s: %d", mtab_file, status);
    525 	exit(1);
    526 	/* NOTREACHED */
    527 }
    528 
    529 
    530 /*
    531  * Read the mount table from the specified file.
    532  * We keep the table in memory for faster lookups.
    533  */
    534 static void
    535 mtab_read_file(void)
    536 {
    537 	char		*mtab_file = MOUNT_TAB;
    538 	FILE		*fp;
    539 	struct extmnttab	mtab;
    540 	int		status;
    541 
    542 	fp = xfopen(mtab_file);
    543 
    544 	resetmnttab(fp);
    545 	mount_table_allocated_entries = MOUNT_TABLE_ENTRIES;
    546 	mount_table_entries = 0;
    547 	mount_table = xmalloc(
    548 	    mount_table_allocated_entries * sizeof (struct mtab_entry));
    549 
    550 	while ((status = getextmntent(fp, &mtab, sizeof (struct extmnttab)))
    551 	    == 0) {
    552 		struct mtab_entry *mtep;
    553 
    554 		if (mount_table_entries == mount_table_allocated_entries) {
    555 			mount_table_allocated_entries += MOUNT_TABLE_ENTRIES;
    556 			mount_table = xrealloc(mount_table,
    557 			    mount_table_allocated_entries *
    558 			    sizeof (struct mtab_entry));
    559 		}
    560 		mtep = &mount_table[mount_table_entries++];
    561 		mtep->mte_mount = mntdup(&mtab);
    562 		mtep->mte_dev_is_valid = FALSE;
    563 		mtep->mte_ignore = (hasmntopt((struct mnttab *)&mtab,
    564 		    MNTOPT_IGNORE) != NULL);
    565 	}
    566 
    567 	(void) fclose(fp);
    568 
    569 	if (status == -1)			/* reached EOF */
    570 		return;
    571 	mtab_error(mtab_file, status);
    572 	/* NOTREACHED */
    573 }
    574 
    575 
    576 /*
    577  * We use this macro when we want to record the option for the purpose of
    578  * passing it to the FS-specific df
    579  */
    580 #define	SET_OPTION(opt)		opt##_option = TRUE, \
    581 				df_options[df_options_len++] = arg
    582 
    583 static void
    584 parse_options(int argc, char *argv[])
    585 {
    586 	int arg;
    587 
    588 	opterr = 0;	/* getopt shouldn't complain about unknown options */
    589 
    590 #ifdef XPG4
    591 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlPZ")) != EOF) {
    592 #else
    593 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlvZ")) != EOF) {
    594 #endif
    595 		if (arg == 'F') {
    596 			if (F_option)
    597 				errmsg(ERR_FATAL + ERR_USAGE,
    598 				    "more than one FSType specified");
    599 			F_option = 1;
    600 			FSType = optarg;
    601 		} else if (arg == 'V' && ! V_option) {
    602 			V_option = TRUE;
    603 		} else if (arg == 'v' && ! v_option) {
    604 			v_option = TRUE;
    605 #ifdef XPG4
    606 		} else if (arg == 'P' && ! P_option) {
    607 			SET_OPTION(P);
    608 #endif
    609 		} else if (arg == 'a' && ! a_option) {
    610 			SET_OPTION(a);
    611 		} else if (arg == 'b' && ! b_option) {
    612 			SET_OPTION(b);
    613 		} else if (arg == 'e' && ! e_option) {
    614 			SET_OPTION(e);
    615 		} else if (arg == 'g' && ! g_option) {
    616 			SET_OPTION(g);
    617 		} else if (arg == 'h') {
    618 			use_scaling = TRUE;
    619 			scale = 1024;
    620 		} else if (arg == 'k' && ! k_option) {
    621 			SET_OPTION(k);
    622 		} else if (arg == 'l' && ! l_option) {
    623 			SET_OPTION(l);
    624 		} else if (arg == 'n' && ! n_option) {
    625 			SET_OPTION(n);
    626 		} else if (arg == 't' && ! t_option) {
    627 			SET_OPTION(t);
    628 		} else if (arg == 'o') {
    629 			if (o_option)
    630 				errmsg(ERR_FATAL + ERR_USAGE,
    631 				"the -o option can only be specified once");
    632 			o_option = TRUE;
    633 			o_option_arg = optarg;
    634 		} else if (arg == 'Z') {
    635 			SET_OPTION(Z);
    636 		} else if (arg == '?') {
    637 			errmsg(ERR_USAGE, "unknown option: %c", optopt);
    638 		}
    639 	}
    640 
    641 	/*
    642 	 * Option sanity checks
    643 	 */
    644 	if (g_option && o_option)
    645 		errmsg(ERR_FATAL, "-o and -g options are incompatible");
    646 	if (l_option && o_option)
    647 		errmsg(ERR_FATAL, "-o and -l options are incompatible");
    648 	if (n_option && o_option)
    649 		errmsg(ERR_FATAL, "-o and -n options are incompatible");
    650 	if (use_scaling && o_option)
    651 		errmsg(ERR_FATAL, "-o and -h options are incompatible");
    652 }
    653 
    654 
    655 
    656 /*
    657  * Check if the user-specified argument is a resource name.
    658  * A resource name is whatever is placed in the mnt_special field of
    659  * struct mnttab. In the case of NFS, a resource name has the form
    660  * hostname:pathname
    661  * We try to find an exact match between the user-specified argument
    662  * and the mnt_special field of a mount table entry.
    663  * We also use the heuristic of removing the basename from the user-specified
    664  * argument and repeating the test until we get a match. This works
    665  * fine for NFS but may fail for other remote file system types. However,
    666  * it is guaranteed that the function will not fail if the user specifies
    667  * the exact resource name.
    668  * If successful, this function sets the 'dfr_mte' field of '*dfrp'
    669  */
    670 static void
    671 resource_mount_entry(struct df_request *dfrp)
    672 {
    673 	char *name;
    674 
    675 	/*
    676 	 * We need our own copy since we will modify the string
    677 	 */
    678 	name = new_string(dfrp->dfr_cmd_arg);
    679 
    680 	for (;;) {
    681 		char *p;
    682 		int i;
    683 
    684 		/*
    685 		 * Compare against all known mount points.
    686 		 * We start from the most recent mount, which is at the
    687 		 * end of the array.
    688 		 */
    689 		for (i = mount_table_entries - 1; i >= 0; i--) {
    690 			struct mtab_entry *mtep = &mount_table[i];
    691 
    692 			if (EQ(name, mtep->mte_mount->mnt_special)) {
    693 				dfrp->dfr_mte = mtep;
    694 				break;
    695 			}
    696 		}
    697 
    698 		/*
    699 		 * Remove the last component of the pathname.
    700 		 * If there is no such component, this is not a resource name.
    701 		 */
    702 		p = strrchr(name, '/');
    703 		if (p == NULL)
    704 			break;
    705 		*p = NUL;
    706 	}
    707 }
    708 
    709 
    710 
    711 /*
    712  * Try to match the command line argument which is a block special device
    713  * with the special device of one of the mounted file systems.
    714  * If one is found, set the appropriate field of 'dfrp' to the mount
    715  * table entry.
    716  */
    717 static void
    718 bdev_mount_entry(struct df_request *dfrp)
    719 {
    720 	int i;
    721 	char *special = dfrp->dfr_cmd_arg;
    722 
    723 	/*
    724 	 * Compare against all known mount points.
    725 	 * We start from the most recent mount, which is at the
    726 	 * end of the array.
    727 	 */
    728 	for (i = mount_table_entries - 1; i >= 0; i--) {
    729 		struct mtab_entry *mtep = &mount_table[i];
    730 
    731 		if (EQ(special, mtep->mte_mount->mnt_special)) {
    732 			dfrp->dfr_mte = mtep;
    733 			break;
    734 		}
    735 	}
    736 }
    737 
    738 static struct mtab_entry *
    739 devid_matches(int i, dev_t devno)
    740 {
    741 	struct mtab_entry	*mtep = &mount_table[i];
    742 	struct extmnttab	*mtp = mtep->mte_mount;
    743 	/* int	len = strlen(mtp->mnt_mountp); */
    744 
    745 	if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
    746 		return (NULL);
    747 	/*
    748 	 * check if device numbers match. If there is a cached device number
    749 	 * in the mtab_entry, use it, otherwise get the device number
    750 	 * either from the mnttab entry or by stat'ing the mount point.
    751 	 */
    752 	if (! mtep->mte_dev_is_valid) {
    753 		struct stat64 st;
    754 		dev_t dev = NODEV;
    755 
    756 		dev = makedev(mtp->mnt_major, mtp->mnt_minor);
    757 		if (dev == 0)
    758 			dev = NODEV;
    759 		if (dev == NODEV) {
    760 			if (stat64(mtp->mnt_mountp, &st) == -1) {
    761 				return (NULL);
    762 			} else {
    763 				dev = st.st_dev;
    764 			}
    765 		}
    766 		mtep->mte_dev = dev;
    767 		mtep->mte_dev_is_valid = TRUE;
    768 	}
    769 	if (mtep->mte_dev == devno) {
    770 		return (mtep);
    771 	}
    772 	return (NULL);
    773 }
    774 
    775 /*
    776  * Find the mount point under which the user-specified path resides
    777  * and set the 'dfr_mte' field of '*dfrp' to point to the mount table entry.
    778  */
    779 static void
    780 path_mount_entry(struct df_request *dfrp, dev_t devno)
    781 {
    782 	char			dirpath[MAXPATHLEN];
    783 	char			*dir = dfrp->dfr_cmd_arg;
    784 	struct mtab_entry	*match, *tmatch;
    785 	int i;
    786 
    787 	/*
    788 	 * Expand the given path to get a canonical version (i.e. an absolute
    789 	 * path without symbolic links).
    790 	 */
    791 	if (realpath(dir, dirpath) == NULL) {
    792 		errmsg(ERR_PERROR, "cannot canonicalize %s:", dir);
    793 		return;
    794 	}
    795 	/*
    796 	 * If the mnt point is lofs, search from the top of entries from
    797 	 * /etc/mnttab and return the first entry that matches the devid
    798 	 * For non-lofs mount points, return the first entry from the bottom
    799 	 * of the entries in /etc/mnttab that matches on the devid field
    800 	 */
    801 	match = NULL;
    802 	if (dfrp->dfr_fstype && EQ(dfrp->dfr_fstype, MNTTYPE_LOFS)) {
    803 		for (i = 0; i < mount_table_entries; i++) {
    804 			if (match = devid_matches(i, devno))
    805 				break;
    806 		}
    807 	} else {
    808 		for (i = mount_table_entries - 1; i >= 0; i--) {
    809 			if (tmatch = devid_matches(i, devno)) {
    810 				/*
    811 				 * If executing in a zone, there might be lofs
    812 				 * mounts for which the real mount point is
    813 				 * invisible; accept the "best fit" for this
    814 				 * devid.
    815 				 */
    816 				match = tmatch;
    817 				if (!EQ(match->mte_mount->mnt_fstype,
    818 				    MNTTYPE_LOFS)) {
    819 					break;
    820 				}
    821 			}
    822 		}
    823 	}
    824 	if (! match) {
    825 		errmsg(ERR_NOFLAGS,
    826 		    "Could not find mount point for %s", dir);
    827 		return;
    828 	}
    829 	dfrp->dfr_mte = match;
    830 }
    831 
    832 /*
    833  * Execute a single FS-specific df command for all given requests
    834  * Return 0 if successful, 1 otherwise.
    835  */
    836 static int
    837 run_fs_specific_df(struct df_request request_list[], int entries)
    838 {
    839 	int	i;
    840 	int	argv_index;
    841 	char	**argv;
    842 	size_t	size;
    843 	pid_t	pid;
    844 	int	status;
    845 	char	cmd_path[MAXPATHLEN];
    846 	char	*fstype;
    847 
    848 	if (entries == 0)
    849 		return (0);
    850 
    851 	fstype = request_list[0].dfr_fstype;
    852 
    853 	if (F_option && ! EQ(FSType, fstype))
    854 		return (0);
    855 
    856 	(void) sprintf(cmd_path, "%s%s/df", FS_LIBPATH, fstype);
    857 	/*
    858 	 * Argv entries:
    859 	 *		1 for the path
    860 	 *		2 for -o <options>
    861 	 *		1 for the generic options that we propagate
    862 	 *		1 for the terminating NULL pointer
    863 	 *		n for the number of user-specified arguments
    864 	 */
    865 	size = (5 + entries) * sizeof (char *);
    866 	argv = xmalloc(size);
    867 	(void) memset(argv, 0, size);
    868 
    869 	argv[0] = cmd_path;
    870 	argv_index = 1;
    871 	if (o_option) {
    872 		argv[argv_index++] = "-o";
    873 		argv[argv_index++] = o_option_arg;
    874 	}
    875 
    876 	/*
    877 	 * Check if we need to propagate any generic options
    878 	 */
    879 	if (df_options_len > 1)
    880 		argv[argv_index++] = df_options;
    881 
    882 	/*
    883 	 * If there is a user-specified path, we pass that to the
    884 	 * FS-specific df. Otherwise, we are guaranteed to have a mount
    885 	 * point, since a request without a user path implies that
    886 	 * we are reporting only on mounted file systems.
    887 	 */
    888 	for (i = 0; i < entries; i++) {
    889 		struct df_request *dfrp = &request_list[i];
    890 
    891 		argv[argv_index++] = (dfrp->dfr_cmd_arg == NULL)
    892 		    ? DFR_MOUNT_POINT(dfrp)
    893 		    : dfrp->dfr_cmd_arg;
    894 	}
    895 
    896 	if (V_option) {
    897 		for (i = 0; i < argv_index-1; i++)
    898 			(void) printf("%s ", argv[i]);
    899 		(void) printf("%s\n", argv[i]);
    900 		return (0);
    901 	}
    902 
    903 	pid = fork();
    904 
    905 	if (pid == -1) {
    906 		errmsg(ERR_PERROR, "cannot fork process:");
    907 		return (1);
    908 	} else if (pid == 0) {
    909 		(void) execv(cmd_path, argv);
    910 		if (errno == ENOENT)
    911 			errmsg(ERR_NOFLAGS,
    912 			    "operation not applicable for FSType %s",
    913 			    fstype);
    914 		else
    915 			errmsg(ERR_PERROR, "cannot execute %s:", cmd_path);
    916 		exit(2);
    917 	}
    918 
    919 	/*
    920 	 * Reap the child
    921 	 */
    922 	for (;;) {
    923 		pid_t wpid = waitpid(pid, &status, 0);
    924 
    925 		if (wpid == -1)
    926 			if (errno == EINTR)
    927 				continue;
    928 			else {
    929 				errmsg(ERR_PERROR, "waitpid error:");
    930 				return (1);
    931 			}
    932 		else
    933 			break;
    934 	}
    935 
    936 	return ((WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : 1);
    937 }
    938 
    939 
    940 
    941 /*
    942  * Remove from the request list all requests that do not apply.
    943  * Notice that the subsequent processing of the requests depends on
    944  * the sanity checking performed by this function.
    945  */
    946 static int
    947 prune_list(struct df_request request_list[],
    948 		size_t n_requests,
    949 		size_t *valid_requests)
    950 {
    951 	size_t	i;
    952 	size_t	n_valid = 0;
    953 	int	errors = 0;
    954 
    955 	for (i = 0; i < n_requests; i++) {
    956 		struct df_request *dfrp = &request_list[i];
    957 
    958 		/*
    959 		 * Skip file systems that are not mounted if either the
    960 		 * -l or -n options were specified. If none of these options
    961 		 * are present, the appropriate FS-specific df will be invoked.
    962 		 */
    963 		if (! DFR_ISMOUNTEDFS(dfrp)) {
    964 			if (l_option || n_option) {
    965 				errmsg(ERR_NOFLAGS,
    966 				    "%s option incompatible with unmounted "
    967 				    "special device (%s)",
    968 				    l_option ? "-l" : "-n", dfrp->dfr_cmd_arg);
    969 				dfrp->dfr_valid = FALSE;
    970 				errors++;
    971 			}
    972 			else
    973 				n_valid++;
    974 			continue;
    975 		}
    976 
    977 		/*
    978 		 * Check for inconsistency between the argument of -F and
    979 		 * the actual file system type.
    980 		 * If there is an inconsistency and the user specified a
    981 		 * path, this is an error since we are asked to interpret
    982 		 * the path using the wrong file system type. If there is
    983 		 * no path associated with this request, we quietly ignore it.
    984 		 */
    985 		if (F_option && ! EQ(dfrp->dfr_fstype, FSType)) {
    986 			dfrp->dfr_valid = FALSE;
    987 			if (dfrp->dfr_cmd_arg != NULL) {
    988 				errmsg(ERR_NOFLAGS,
    989 				"Warning: %s mounted as a %s file system",
    990 				    dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
    991 				errors++;
    992 			}
    993 			continue;
    994 		}
    995 
    996 		/*
    997 		 * Skip remote file systems if the -l option is present
    998 		 */
    999 		if (l_option && is_remote_fs(dfrp->dfr_fstype)) {
   1000 			if (dfrp->dfr_cmd_arg != NULL) {
   1001 				errmsg(ERR_NOFLAGS,
   1002 				    "Warning: %s is not a local file system",
   1003 				    dfrp->dfr_cmd_arg);
   1004 				errors++;
   1005 			}
   1006 			dfrp->dfr_valid = FALSE;
   1007 			continue;
   1008 		}
   1009 
   1010 		/*
   1011 		 * Skip file systems mounted as "ignore" unless the -a option
   1012 		 * is present, or the user explicitly specified them on
   1013 		 * the command line.
   1014 		 */
   1015 		if (dfrp->dfr_mte->mte_ignore &&
   1016 		    ! (a_option || dfrp->dfr_cmd_arg)) {
   1017 			dfrp->dfr_valid = FALSE;
   1018 			continue;
   1019 		}
   1020 
   1021 		n_valid++;
   1022 	}
   1023 	*valid_requests = n_valid;
   1024 	return (errors);
   1025 }
   1026 
   1027 
   1028 /*
   1029  * Print the appropriate header for the requested output format.
   1030  * Options are checked in order of their precedence.
   1031  */
   1032 static void
   1033 print_header(void)
   1034 {
   1035 	if (use_scaling) { /* this comes from the -h option */
   1036 		int arg = 'h';
   1037 
   1038 		(void) printf("%-*s %*s %*s %*s %-*s %s\n",
   1039 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1040 #ifdef XPG4
   1041 		    SCALED_WIDTH, TRANSLATE("Size"),
   1042 		    SCALED_WIDTH, TRANSLATE("Used"),
   1043 		    AVAILABLE_WIDTH, TRANSLATE("Available"),
   1044 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
   1045 #else
   1046 		    SCALED_WIDTH, TRANSLATE("size"),
   1047 		    SCALED_WIDTH, TRANSLATE("used"),
   1048 		    AVAILABLE_WIDTH, TRANSLATE("avail"),
   1049 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
   1050 #endif
   1051 		    TRANSLATE("Mounted on"));
   1052 		SET_OPTION(h);
   1053 		return;
   1054 	}
   1055 	if (k_option) {
   1056 		int arg = 'h';
   1057 
   1058 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
   1059 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1060 #ifdef XPG4
   1061 		    KBYTE_WIDTH, TRANSLATE("1024-blocks"),
   1062 		    KBYTE_WIDTH, TRANSLATE("Used"),
   1063 		    KBYTE_WIDTH, TRANSLATE("Available"),
   1064 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
   1065 #else
   1066 		    KBYTE_WIDTH, TRANSLATE("kbytes"),
   1067 		    KBYTE_WIDTH, TRANSLATE("used"),
   1068 		    KBYTE_WIDTH, TRANSLATE("avail"),
   1069 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
   1070 #endif
   1071 		    TRANSLATE("Mounted on"));
   1072 		SET_OPTION(h);
   1073 		return;
   1074 	}
   1075 	/* Added for XCU4 compliance */
   1076 	if (P_option) {
   1077 		int arg = 'h';
   1078 
   1079 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
   1080 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1081 		    KBYTE_WIDTH, TRANSLATE("512-blocks"),
   1082 		    KBYTE_WIDTH, TRANSLATE("Used"),
   1083 		    KBYTE_WIDTH, TRANSLATE("Available"),
   1084 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
   1085 		    TRANSLATE("Mounted on"));
   1086 
   1087 		SET_OPTION(h);
   1088 		return;
   1089 	}
   1090 	/* End XCU4 */
   1091 	if (v_option) {
   1092 		(void) printf("%-*s %-*s %*s %*s %*s %-*s\n",
   1093 		    IBCS2_MOUNT_POINT_WIDTH, TRANSLATE("Mount Dir"),
   1094 		    IBCS2_FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1095 		    BLOCK_WIDTH, TRANSLATE("blocks"),
   1096 		    BLOCK_WIDTH, TRANSLATE("used"),
   1097 		    BLOCK_WIDTH, TRANSLATE("free"),
   1098 		    CAPACITY_WIDTH, TRANSLATE(" %used"));
   1099 		return;
   1100 	}
   1101 	if (e_option) {
   1102 		(void) printf(gettext("%-*s %*s\n"),
   1103 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1104 		    BLOCK_WIDTH, TRANSLATE("ifree"));
   1105 		return;
   1106 	}
   1107 	if (b_option) {
   1108 		(void) printf(gettext("%-*s %*s\n"),
   1109 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
   1110 		    BLOCK_WIDTH, TRANSLATE("avail"));
   1111 		return;
   1112 	}
   1113 }
   1114 
   1115 
   1116 /*
   1117  * Convert an unsigned long long to a string representation and place the
   1118  * result in the caller-supplied buffer.
   1119  * The given number is in units of "unit_from" size, but the
   1120  * converted number will be in units of "unit_to" size. The unit sizes
   1121  * must be powers of 2.
   1122  * The value "(unsigned long long)-1" is a special case and is always
   1123  * converted to "-1".
   1124  * Returns a pointer to the caller-supplied buffer.
   1125  */
   1126 static char *
   1127 number_to_string(
   1128 			char *buf,		/* put the result here */
   1129 			unsigned long long number, /* convert this number */
   1130 			int unit_from,		/* from units of this size */
   1131 			int unit_to)		/* to units of this size */
   1132 {
   1133 	if ((long long)number == (long long)-1)
   1134 		(void) strcpy(buf, "-1");
   1135 	else {
   1136 		if (unit_from == unit_to)
   1137 			(void) sprintf(buf, "%llu", number);
   1138 		else if (unit_from < unit_to)
   1139 			(void) sprintf(buf, "%llu",
   1140 			    number / (unsigned long long)(unit_to / unit_from));
   1141 		else
   1142 			(void) sprintf(buf, "%llu",
   1143 			    number * (unsigned long long)(unit_from / unit_to));
   1144 	}
   1145 	return (buf);
   1146 }
   1147 
   1148 /*
   1149  * Convert an unsigned long long to a string representation and place the
   1150  * result in the caller-supplied buffer.
   1151  * The