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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 #include <dlfcn.h> 35 #include <stdio.h> 36 #include <stdarg.h> 37 #include <string.h> 38 #include <locale.h> 39 #include <libintl.h> 40 #include <stdlib.h> 41 #include <ftw.h> 42 #include <errno.h> 43 #include <sys/types.h> 44 #include <unistd.h> 45 #include <sys/statvfs.h> 46 #include <sys/stat.h> 47 #include <sys/param.h> 48 #include <sys/mnttab.h> 49 #include <sys/mntent.h> 50 #include <sys/vfstab.h> 51 #include <sys/wait.h> 52 #include <sys/mkdev.h> 53 #include <sys/int_limits.h> 54 #include <sys/zone.h> 55 #include <libzfs.h> 56 57 #include "fslib.h" 58 59 extern char *default_fstype(char *); 60 61 /* 62 * General notice: 63 * String pointers in this code may point to statically allocated memory 64 * or dynamically allocated memory. Furthermore, a dynamically allocated 65 * string may be pointed to by more than one pointer. This does not pose 66 * a problem because malloc'ed memory is never free'd (so we don't need 67 * to remember which pointers point to malloc'ed memory). 68 */ 69 70 /* 71 * TRANSLATION_NOTE 72 * Only strings passed as arguments to the TRANSLATE macro need to 73 * be translated. 74 */ 75 76 #ifndef MNTTYPE_LOFS 77 #define MNTTYPE_LOFS "lofs" 78 #endif 79 80 #define EQ(s1, s2) (strcmp(s1, s2) == 0) 81 #define NEW(type) xmalloc(sizeof (type)) 82 #define CLEAR(var) (void) memset(&(var), 0, sizeof (var)) 83 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 84 #define MAX3(a, b, c) MAX(a, MAX(b, c)) 85 #define TRANSLATE(s) new_string(gettext(s)) 86 87 #define MAX_OPTIONS 36 88 #define N_FSTYPES 20 89 #define MOUNT_TABLE_ENTRIES 40 /* initial allocation */ 90 #define MSGBUF_SIZE 1024 91 #define LINEBUF_SIZE 256 /* either input or output lines */ 92 93 #define BLOCK_SIZE 512 /* when reporting in terms of blocks */ 94 95 #define DEVNM_CMD "devnm" 96 #define FS_LIBPATH "/usr/lib/fs/" 97 #define MOUNT_TAB "/etc/mnttab" 98 #define VFS_TAB "/etc/vfstab" 99 #define REMOTE_FS "/etc/dfs/fstypes" 100 101 #define NUL '\0' 102 #define FALSE 0 103 #define TRUE 1 104 105 /* 106 * Formatting constants 107 */ 108 #define IBCS2_FILESYSTEM_WIDTH 15 /* Truncate to match ISC/SCO */ 109 #define IBCS2_MOUNT_POINT_WIDTH 10 /* Truncate to match ISC/SCO */ 110 #define FILESYSTEM_WIDTH 20 111 #define MOUNT_POINT_WIDTH 19 112 #define SPECIAL_DEVICE_WIDTH 18 113 #define FSTYPE_WIDTH 8 114 #define BLOCK_WIDTH 8 115 #define NFILES_WIDTH 8 116 #ifdef XPG4 117 #define KBYTE_WIDTH 11 118 #define AVAILABLE_WIDTH 10 119 #else 120 #define KBYTE_WIDTH 7 121 #define AVAILABLE_WIDTH 6 122 #endif 123 #define SCALED_WIDTH 6 124 #define CAPACITY_WIDTH 9 125 #define BSIZE_WIDTH 6 126 #define FRAGSIZE_WIDTH 7 127 #define FSID_WIDTH 7 128 #define FLAG_WIDTH 8 129 #define NAMELEN_WIDTH 7 130 #define MNT_SPEC_WIDTH MOUNT_POINT_WIDTH + SPECIAL_DEVICE_WIDTH + 2 131 132 /* 133 * Flags for the errmsg() function 134 */ 135 #define ERR_NOFLAGS 0x0 136 #define ERR_NONAME 0x1 /* don't include the program name */ 137 /* as a prefix */ 138 #define ERR_FATAL 0x2 /* call exit after printing the */ 139 /* message */ 140 #define ERR_PERROR 0x4 /* append an errno explanation to */ 141 /* the message */ 142 #define ERR_USAGE 0x8 /* print the usage line after the */ 143 /* message */ 144 145 #define NUMBER_WIDTH 40 146 147 /* 148 * A numbuf_t is used when converting a number to a string representation 149 */ 150 typedef char numbuf_t[ NUMBER_WIDTH ]; 151 152 /* 153 * We use bool_int instead of int to make clear which variables are 154 * supposed to be boolean 155 */ 156 typedef int bool_int; 157 158 struct mtab_entry { 159 bool_int mte_dev_is_valid; 160 dev_t mte_dev; 161 bool_int mte_ignore; /* the "ignore" option was set */ 162 struct extmnttab *mte_mount; 163 }; 164 165 166 struct df_request { 167 bool_int dfr_valid; 168 char *dfr_cmd_arg; /* what the user specified */ 169 struct mtab_entry *dfr_mte; 170 char *dfr_fstype; 171 int dfr_index; /* to make qsort stable */ 172 }; 173 174 #define DFR_MOUNT_POINT(dfrp) (dfrp)->dfr_mte->mte_mount->mnt_mountp 175 #define DFR_SPECIAL(dfrp) (dfrp)->dfr_mte->mte_mount->mnt_special 176 #define DFR_FSTYPE(dfrp) (dfrp)->dfr_mte->mte_mount->mnt_fstype 177 #define DFR_ISMOUNTEDFS(dfrp) ((dfrp)->dfr_mte != NULL) 178 179 #define DFRP(p) ((struct df_request *)(p)) 180 181 typedef void (*output_func)(struct df_request *, struct statvfs64 *); 182 183 struct df_output { 184 output_func dfo_func; /* function that will do the output */ 185 int dfo_flags; 186 }; 187 188 /* 189 * Output flags 190 */ 191 #define DFO_NOFLAGS 0x0 192 #define DFO_HEADER 0x1 /* output preceded by header */ 193 #define DFO_STATVFS 0x2 /* must do a statvfs64(2) */ 194 195 196 static char *program_name; 197 static char df_options[MAX_OPTIONS] = "-"; 198 static size_t df_options_len = 1; 199 static char *o_option_arg; /* arg to the -o option */ 200 static char *FSType; 201 static char *remote_fstypes[N_FSTYPES+1]; /* allocate an extra one */ 202 /* to use as a terminator */ 203 204 /* 205 * The following three variables support an in-memory copy of the mount table 206 * to speedup searches. 207 */ 208 static struct mtab_entry *mount_table; /* array of mtab_entry's */ 209 static size_t mount_table_entries; 210 static size_t mount_table_allocated_entries; 211 212 static bool_int F_option; 213 static bool_int V_option; 214 static bool_int P_option; /* Added for XCU4 compliance */ 215 static bool_int Z_option; 216 static bool_int v_option; 217 #ifdef _iBCS2 218 char *sysv3_set; 219 #endif /* _iBCS2 */ 220 static bool_int a_option; 221 static bool_int b_option; 222 static bool_int e_option; 223 static bool_int g_option; 224 static bool_int h_option; 225 static bool_int k_option; 226 static bool_int l_option; 227 static bool_int n_option; 228 static bool_int t_option; 229 static bool_int o_option; 230 231 static bool_int tty_output; 232 static bool_int use_scaling; 233 static int scale; 234 235 static void usage(void); 236 static void do_devnm(int, char **); 237 static void do_df(int, char **); 238 static void parse_options(int, char **); 239 static char *basename(char *); 240 241 242 /* ARGSUSED */ 243 static void 244 dummy_error_handler(const char *fmt, va_list ap) 245 { 246 /* Do nothing */ 247 } 248 249 static zfs_handle_t *(*_zfs_open)(const char *, int); 250 static void (*_zfs_close)(zfs_handle_t *); 251 static uint64_t (*_zfs_prop_get_int)(zfs_handle_t *, zfs_prop_t); 252 static void (*_zfs_set_error_handler)(void (*)(const char *, va_list)); 253 254 void 255 main(int argc, char *argv[]) 256 { 257 void *hdl; 258 259 (void) setlocale(LC_ALL, ""); 260 261 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 262 #define TEXT_DOMAIN "SYS_TEST" 263 #endif 264 (void) textdomain(TEXT_DOMAIN); 265 266 program_name = basename(argv[0]); 267 268 #ifdef _iBCS2 269 sysv3_set = getenv("SYSV3"); 270 #endif /* _iBCS2 */ 271 272 /* 273 * Dynamically check for libzfs, in case the user hasn't installed the 274 * SUNWzfs packages. A basic utility such as df shouldn't depend on 275 * optional filesystems. 276 */ 277 if ((hdl = dlopen("libzfs.so", RTLD_LAZY)) != NULL) { 278 _zfs_set_error_handler = (void (*)()) 279 dlsym(hdl, "zfs_set_error_handler"); 280 _zfs_open = (zfs_handle_t *(*)())dlsym(hdl, "zfs_open"); 281 _zfs_close = (void (*)())dlsym(hdl, "zfs_close"); 282 _zfs_prop_get_int = (uint64_t (*)()) 283 dlsym(hdl, "zfs_prop_get_int"); 284 285 if (_zfs_set_error_handler != NULL) { 286 assert(_zfs_open != NULL); 287 assert(_zfs_close != NULL); 288 assert(_zfs_prop_get_int != NULL); 289 290 /* 291 * Disable ZFS error reporting, so we don't get messages 292 * like "can't open ..." under race conditions. 293 */ 294 _zfs_set_error_handler(dummy_error_handler); 295 } 296 } 297 298 if (EQ(program_name, DEVNM_CMD)) 299 do_devnm(argc, argv); 300 301 parse_options(argc, argv); 302 303 /* 304 * The k_option implies SunOS 4.x compatibility: when the special 305 * device name is too long the line will be split except when the 306 * output has been redirected. 307 * This is also valid for the -h option. 308 */ 309 310 if (use_scaling || k_option || P_option || v_option) 311 tty_output = isatty(1); 312 313 do_df(argc - optind, &argv[optind]); 314 /* NOTREACHED */ 315 } 316 317 318 /* 319 * Prints an error message to stderr. 320 */ 321 /* VARARGS2 */ 322 static void 323 errmsg(int flags, char *fmt, ...) 324 { 325 char buf[MSGBUF_SIZE]; 326 va_list ap; 327 int cc; 328 int offset; 329 330 if (flags & ERR_NONAME) 331 offset = 0; 332 else 333 offset = sprintf(buf, "%s: ", program_name); 334 335 va_start(ap, fmt); 336 cc = vsprintf(&buf[offset], gettext(fmt), ap); 337 offset += cc; 338 va_end(ap); 339 340 if (flags & ERR_PERROR) { 341 if (buf[offset-1] != ' ') 342 (void) strcat(buf, " "); 343 (void) strcat(buf, strerror(errno)); 344 } 345 (void) fprintf(stderr, "%s\n", buf); 346 if (flags & ERR_USAGE) 347 usage(); 348 if (flags & ERR_FATAL) 349 exit(1); 350 } 351 352 353 static void 354 usage() 355 { 356 #ifdef XPG4 357 errmsg(ERR_NONAME, 358 "Usage: %s [-F FSType] [-abeghklntPVZ] [-o FSType-specific_options]" 359 " [directory | block_device | resource]", program_name); 360 #else 361 errmsg(ERR_NONAME, 362 "Usage: %s [-F FSType] [-abeghklntVvZ] [-o FSType-specific_options]" 363 " [directory | block_device | resource]", program_name); 364 #endif 365 exit(1); 366 /* NOTREACHED */ 367 } 368 369 370 static char * 371 new_string(char *s) 372 { 373 char *p = NULL; 374 375 if (s) { 376 p = strdup(s); 377 if (p) 378 return (p); 379 errmsg(ERR_FATAL, "out of memory"); 380 /* NOTREACHED */ 381 } 382 return (p); 383 } 384 385 386 /* 387 * Allocate memory using malloc but terminate if the allocation fails 388 */ 389 static void * 390 xmalloc(size_t size) 391 { 392 void *p = malloc(size); 393 394 if (p) 395 return (p); 396 errmsg(ERR_FATAL, "out of memory"); 397 /* NOTREACHED */ 398 } 399 400 401 /* 402 * Allocate memory using realloc but terminate if the allocation fails 403 */ 404 static void * 405 xrealloc(void *ptr, size_t size) 406 { 407 void *p = realloc(ptr, size); 408 409 if (p) 410 return (p); 411 errmsg(ERR_FATAL, "out of memory"); 412 /* NOTREACHED */ 413 } 414 415 416 /* 417 * fopen the specified file for reading but terminate if the fopen fails 418 */ 419 static FILE * 420 xfopen(char *file) 421 { 422 FILE *fp = fopen(file, "r"); 423 424 if (fp == NULL) 425 errmsg(ERR_FATAL + ERR_PERROR, "failed to open %s:", file); 426 return (fp); 427 } 428 429 430 /* 431 * Read remote file system types from REMOTE_FS into the 432 * remote_fstypes array. 433 */ 434 static void 435 init_remote_fs() 436 { 437 FILE *fp; 438 char line_buf[LINEBUF_SIZE]; 439 size_t fstype_index = 0; 440 441 if ((fp = fopen(REMOTE_FS, "r")) == NULL) { 442 errmsg(ERR_NOFLAGS, 443 "Warning: can't open %s, ignored", REMOTE_FS); 444 return; 445 } 446 447 while (fgets(line_buf, sizeof (line_buf), fp) != NULL) { 448 char buf[LINEBUF_SIZE]; 449 450 (void) sscanf(line_buf, "%s", buf); 451 remote_fstypes[fstype_index++] = new_string(buf); 452 453 if (fstype_index == N_FSTYPES) 454 break; 455 } 456 (void) fclose(fp); 457 } 458 459 460 /* 461 * Returns TRUE if fstype is a remote file system type; 462 * otherwise, returns FALSE. 463 */ 464 static int 465 is_remote_fs(char *fstype) 466 { 467 char **p; 468 static bool_int remote_fs_initialized; 469 470 if (! remote_fs_initialized) { 471 init_remote_fs(); 472 remote_fs_initialized = TRUE; 473 } 474 475 for (p = remote_fstypes; *p; p++) 476 if (EQ(fstype, *p)) 477 return (TRUE); 478 return (FALSE); 479 } 480 481 482 static char * 483 basename(char *s) 484 { 485 char *p = strrchr(s, '/'); 486 487 return (p ? p+1 : s); 488 } 489 490 491 /* 492 * Create a new "struct extmnttab" and make sure that its fields point 493 * to malloc'ed memory 494 */ 495 static struct extmnttab * 496 mntdup(struct extmnttab *old) 497 { 498 struct extmnttab *new = NEW(struct extmnttab); 499 500 new->mnt_special = new_string(old->mnt_special); 501 new->mnt_mountp = new_string(old->mnt_mountp); 502 new->mnt_fstype = new_string(old->mnt_fstype); 503 new->mnt_mntopts = new_string(old->mnt_mntopts); 504 new->mnt_time = new_string(old->mnt_time); 505 new->mnt_major = old->mnt_major; 506 new->mnt_minor = old->mnt_minor; 507 return (new); 508 } 509 510 511 static void 512 mtab_error(char *mtab_file, int status) 513 { 514 if (status == MNT_TOOLONG) 515 errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters", 516 mtab_file, MNT_LINE_MAX); 517 else if (status == MNT_TOOMANY) 518 errmsg(ERR_NOFLAGS, 519 "a line in %s has too many fields", mtab_file); 520 else if (status == MNT_TOOFEW) 521 errmsg(ERR_NOFLAGS, 522 "a line in %s has too few fields", mtab_file); 523 else 524 errmsg(ERR_NOFLAGS, 525 "error while reading %s: %d", mtab_file, status); 526 exit(1); 527 /* NOTREACHED */ 528 } 529 530 531 /* 532 * Read the mount table from the specified file. 533 * We keep the table in memory for faster lookups. 534 */ 535 static void 536 mtab_read_file() 537 { 538 char *mtab_file = MOUNT_TAB; 539 FILE *fp; 540 struct extmnttab mtab; 541 int status; 542 543 fp = xfopen(mtab_file); 544 545 resetmnttab(fp); 546 mount_table_allocated_entries = MOUNT_TABLE_ENTRIES; 547 mount_table_entries = 0; 548 mount_table = xmalloc( 549 mount_table_allocated_entries * sizeof (struct mtab_entry)); 550 551 while ((status = getextmntent(fp, &mtab, sizeof (struct extmnttab))) 552 == 0) { 553 struct mtab_entry *mtep; 554 555 if (mount_table_entries == mount_table_allocated_entries) { 556 mount_table_allocated_entries += MOUNT_TABLE_ENTRIES; 557 mount_table = xrealloc(mount_table, 558 mount_table_allocated_entries * 559 sizeof (struct mtab_entry)); 560 } 561 mtep = &mount_table[mount_table_entries++]; 562 mtep->mte_mount = mntdup(&mtab); 563 mtep->mte_dev_is_valid = FALSE; 564 mtep->mte_ignore = (hasmntopt((struct mnttab *)&mtab, 565 MNTOPT_IGNORE) != NULL); 566 } 567 568 (void) fclose(fp); 569 570 if (status == -1) /* reached EOF */ 571 return; 572 mtab_error(mtab_file, status); 573 /* NOTREACHED */ 574 } 575 576 577 /* 578 * We use this macro when we want to record the option for the purpose of 579 * passing it to the FS-specific df 580 */ 581 #define SET_OPTION(opt) opt##_option = TRUE, \ 582 df_options[df_options_len++] = arg 583 584 static void 585 parse_options(int argc, char *argv[]) 586 { 587 int arg; 588 589 opterr = 0; /* getopt shouldn't complain about unknown options */ 590 591 #ifdef XPG4 592 while ((arg = getopt(argc, argv, "F:o:abehkVtgnlPZ")) != EOF) { 593 #else 594 while ((arg = getopt(argc, argv, "F:o:abehkVtgnlvZ")) != EOF) { 595 #endif 596 if (arg == 'F') { 597 if (F_option) 598 errmsg(ERR_FATAL + ERR_USAGE, 599 "more than one FSType specified"); 600 F_option = 1; 601 FSType = optarg; 602 } else if (arg == 'V' && ! V_option) { 603 V_option = TRUE; 604 } else if (arg == 'v' && ! v_option) { 605 v_option = TRUE; 606 #ifdef XPG4 607 } else if (arg == 'P' && ! P_option) { 608 SET_OPTION(P); 609 #endif 610 } else if (arg == 'a' && ! a_option) { 611 SET_OPTION(a); 612 } else if (arg == 'b' && ! b_option) { 613 SET_OPTION(b); 614 } else if (arg == 'e' && ! e_option) { 615 SET_OPTION(e); 616 } else if (arg == 'g' && ! g_option) { 617 SET_OPTION(g); 618 } else if (arg == 'h') { 619 use_scaling = TRUE; 620 scale = 1024; 621 } else if (arg == 'k' && ! k_option) { 622 SET_OPTION(k); 623 } else if (arg == 'l' && ! l_option) { 624 SET_OPTION(l); 625 } else if (arg == 'n' && ! n_option) { 626 SET_OPTION(n); 627 } else if (arg == 't' && ! t_option) { 628 SET_OPTION(t); 629 } else if (arg == 'o') { 630 if (o_option) 631 errmsg(ERR_FATAL + ERR_USAGE, 632 "the -o option can only be specified once"); 633 o_option = TRUE; 634 o_option_arg = optarg; 635 } else if (arg == 'Z') { 636 SET_OPTION(Z); 637 } else if (arg == '?') { 638 errmsg(ERR_USAGE, "unknown option: %c", optopt); 639 } 640 } 641 642 /* 643 * Option sanity checks 644 */ 645 if (g_option && o_option) 646 errmsg(ERR_FATAL, "-o and -g options are incompatible"); 647 if (l_option && o_option) 648 errmsg(ERR_FATAL, "-o and -l options are incompatible"); 649 if (n_option && o_option) 650 errmsg(ERR_FATAL, "-o and -n options are incompatible"); 651 if (use_scaling && o_option) 652 errmsg(ERR_FATAL, "-o and -h options are incompatible"); 653 } 654 655 656 657 /* 658 * Check if the user-specified argument is a resource name. 659 * A resource name is whatever is placed in the mnt_special field of 660 * struct mnttab. In the case of NFS, a resource name has the form 661 * hostname:pathname 662 * We try to find an exact match between the user-specified argument 663 * and the mnt_special field of a mount table entry. 664 * We also use the heuristic of removing the basename from the user-specified 665 * argument and repeating the test until we get a match. This works 666 * fine for NFS but may fail for other remote file system types. However, 667 * it is guaranteed that the function will not fail if the user specifies 668 * the exact resource name. 669 * If successful, this function sets the 'dfr_mte' field of '*dfrp' 670 */ 671 static void 672 resource_mount_entry(struct df_request *dfrp) 673 { 674 char *name; 675 676 /* 677 * We need our own copy since we will modify the string 678 */ 679 name = new_string(dfrp->dfr_cmd_arg); 680 681 for (;;) { 682 char *p; 683 int i; 684 685 /* 686 * Compare against all known mount points. 687 * We start from the most recent mount, which is at the 688 * end of the array. 689 */ 690 for (i = mount_table_entries - 1; i >= 0; i--) { 691 struct mtab_entry *mtep = &mount_table[i]; 692 693 if (EQ(name, mtep->mte_mount->mnt_special)) { 694 dfrp->dfr_mte = mtep; 695 break; 696 } 697 } 698 699 /* 700 * Remove the last component of the pathname. 701 * If there is no such component, this is not a resource name. 702 */ 703 p = strrchr(name, '/'); 704 if (p == NULL) 705 break; 706 *p = NUL; 707 } 708 } 709 710 711 712 /* 713 * Try to match the command line argument which is a block special device 714 * with the special device of one of the mounted file systems. 715 * If one is found, set the appropriate field of 'dfrp' to the mount 716 * table entry. 717 */ 718 static void 719 bdev_mount_entry(struct df_request *dfrp) 720 { 721 int i; 722 char *special = dfrp->dfr_cmd_arg; 723 724 /* 725 * Compare against all known mount points. 726 * We start from the most recent mount, which is at the 727 * end of the array. 728 */ 729 for (i = mount_table_entries - 1; i >= 0; i--) { 730 struct mtab_entry *mtep = &mount_table[i]; 731 732 if (EQ(special, mtep->mte_mount->mnt_special)) { 733 dfrp->dfr_mte = mtep; 734 break; 735 } 736 } 737 } 738 739 static struct mtab_entry * 740 devid_matches(int i, dev_t devno) 741 { 742 struct mtab_entry *mtep = &mount_table[i]; 743 struct extmnttab *mtp = mtep->mte_mount; 744 /* int len = strlen(mtp->mnt_mountp); */ 745 746 if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP)) 747 return (NULL); 748 /* 749 * check if device numbers match. If there is a cached device number 750 * in the mtab_entry, use it, otherwise get the device number 751 * either from the mnttab entry or by stat'ing the mount point. 752 */ 753 if (! mtep->mte_dev_is_valid) { 754 struct stat64 st; 755 dev_t dev = NODEV; 756 757 dev = makedev(mtp->mnt_major, mtp->mnt_minor); 758 if (dev == 0) 759 dev = NODEV; 760 if (dev == NODEV) { 761 if (stat64(mtp->mnt_mountp, &st) == -1) { 762 return (NULL); 763 } else { 764 dev = st.st_dev; 765 } 766 } 767 mtep->mte_dev = dev; 768 mtep->mte_dev_is_valid = TRUE; 769 } 770 if (mtep->mte_dev == devno) { 771 return (mtep); 772 } 773 return (NULL); 774 } 775 776 /* 777 * Find the mount point under which the user-specified path resides 778 * and set the 'dfr_mte' field of '*dfrp' to point to the mount table entry. 779 */ 780 static void 781 path_mount_entry(struct df_request *dfrp, dev_t devno) 782 { 783 char dirpath[MAXPATHLEN]; 784 char *dir = dfrp->dfr_cmd_arg; 785 struct mtab_entry *match, *tmatch; 786 int i; 787 788 /* 789 * Expand the given path to get a canonical version (i.e. an absolute 790 * path without symbolic links). 791 */ 792 if (realpath(dir, dirpath) == NULL) { 793 errmsg(ERR_PERROR, "cannot canonicalize %s:", dir); 794 return; 795 } 796 /* 797 * If the mnt point is lofs, search from the top of entries from 798 * /etc/mnttab and return the first entry that matches the devid 799 * For non-lofs mount points, return the first entry from the bottom 800 * of the entries in /etc/mnttab that matches on the devid field 801 */ 802 match = NULL; 803 if (dfrp->dfr_fstype && EQ(dfrp->dfr_fstype, MNTTYPE_LOFS)) { 804 for (i = 0; i < mount_table_entries; i++) { 805 if (match = devid_matches(i, devno)) 806 break; 807 } 808 } else { 809 for (i = mount_table_entries - 1; i >= 0; i--) { 810 if (tmatch = devid_matches(i, devno)) { 811 /* 812 * If executing in a zone, there might be lofs 813 * mounts for which the real mount point is 814 * invisible; accept the "best fit" for this 815 * devid. 816 */ 817 match = tmatch; 818 if (!EQ(match->mte_mount->mnt_fstype, 819 MNTTYPE_LOFS)) { 820 break; 821 } 822 } 823 } 824 } 825 if (! match) { 826 errmsg(ERR_NOFLAGS, 827 "Could not find mount point for %s", dir); 828 return; 829 } 830 dfrp->dfr_mte = match; 831 } 832 833 /* 834 * Execute a single FS-specific df command for all given requests 835 * Return 0 if successful, 1 otherwise. 836 */ 837 static int 838 run_fs_specific_df(struct df_request request_list[], int entries) 839 { 840 int i; 841 int argv_index; 842 char **argv; 843 size_t size; 844 pid_t pid; 845 int status; 846 char cmd_path[MAXPATHLEN]; 847 char *fstype; 848 849 if (entries == 0) 850 return (0); 851 852 fstype = request_list[0].dfr_fstype; 853 854 if (F_option && ! EQ(FSType, fstype)) 855 return (0); 856 857 (void) sprintf(cmd_path, "%s%s/df", FS_LIBPATH, fstype); 858 /* 859 * Argv entries: 860 * 1 for the path 861 * 2 for -o <options> 862 * 1 for the generic options that we propagate 863 * 1 for the terminating NULL pointer 864 * n for the number of user-specified arguments 865 */ 866 size = (5 + entries) * sizeof (char *); 867 argv = xmalloc(size); 868 (void) memset(argv, 0, size); 869 870 argv[0] = cmd_path; 871 argv_index = 1; 872 if (o_option) { 873 argv[argv_index++] = "-o"; 874 argv[argv_index++] = o_option_arg; 875 } 876 877 /* 878 * Check if we need to propagate any generic options 879 */ 880 if (df_options_len > 1) 881 argv[argv_index++] = df_options; 882 883 /* 884 * If there is a user-specified path, we pass that to the 885 * FS-specific df. Otherwise, we are guaranteed to have a mount 886 * point, since a request without a user path implies that 887 * we are reporting only on mounted file systems. 888 */ 889 for (i = 0; i < entries; i++) { 890 struct df_request *dfrp = &request_list[i]; 891 892 argv[argv_index++] = (dfrp->dfr_cmd_arg == NULL) 893 ? DFR_MOUNT_POINT(dfrp) 894 : dfrp->dfr_cmd_arg; 895 } 896 897 if (V_option) { 898 for (i = 0; i < argv_index-1; i++) 899 (void) printf("%s ", argv[i]); 900 (void) printf("%s\n", argv[i]); 901 return (0); 902 } 903 904 pid = fork(); 905 906 if (pid == -1) { 907 errmsg(ERR_PERROR, "cannot fork process:"); 908 return (1); 909 } else if (pid == 0) { 910 (void) execv(cmd_path, argv); 911 if (errno == ENOENT) 912 errmsg(ERR_NOFLAGS, 913 "operation not applicable for FSType %s", 914 fstype); 915 else 916 errmsg(ERR_PERROR, "cannot execute %s:", cmd_path); 917 exit(2); 918 } 919 920 /* 921 * Reap the child 922 */ 923 for (;;) { 924 pid_t wpid = waitpid(pid, &status, 0); 925 926 if (wpid == -1) 927 if (errno == EINTR) 928 continue; 929 else { 930 errmsg(ERR_PERROR, "waitpid error:"); 931 return (1); 932 } 933 else 934 break; 935 } 936 937 return ((WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : 1); 938 } 939 940 941 942 /* 943 * Remove from the request list all requests that do not apply. 944 * Notice that the subsequent processing of the requests depends on 945 * the sanity checking performed by this function. 946 */ 947 static int 948 prune_list(struct df_request request_list[], 949 size_t n_requests, 950 size_t *valid_requests) 951 { 952 size_t i; 953 size_t n_valid = 0; 954 int errors = 0; 955 956 for (i = 0; i < n_requests; i++) { 957 struct df_request *dfrp = &request_list[i]; 958 959 /* 960 * Skip file systems that are not mounted if either the 961 * -l or -n options were specified. If none of these options 962 * are present, the appropriate FS-specific df will be invoked. 963 */ 964 if (! DFR_ISMOUNTEDFS(dfrp)) { 965 if (l_option || n_option) { 966 errmsg(ERR_NOFLAGS, 967 "%s option incompatible with unmounted 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() 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 given number is in units of "unit_from" size, 1152 * this will first be converted to a number in 1024 or 1000 byte size, 1153 * depending on the scaling factor. 1154 * Then the number is scaled down until it is small enough to be in a good 1155 * human readable format i.e. in the range 0 thru scale-1. 1156 * If it's smaller than 10 there's room enough to provide one decimal place. 1157 * The value "(unsigned long long)-1" is a special case and is always 1158 * converted to "-1". 1159 * Returns a pointer to the caller-supplied buffer. 1160 */ 1161 static char * 1162 number_to_scaled_string( 1163 numbuf_t buf, /* put the result here */ 1164 unsigned long long number, /* convert this number */ 1165 int unit_from, 1166 int scale) 1167 { 1168 unsigned long long save = 0; 1169 char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */ 1170 char *uom = M; /* unit of measurement, initially 'K' (=M[0]) */ 1171 1172 if ((long long)number == (long long)-1) { 1173 (void) strcpy(buf, "-1"); 1174 return (buf); 1175 } 1176 1177 /* 1178 * Convert number from unit_from to given scale (1024 or 1000). 1179 * This means multiply number by unit_from and divide by scale. 1180 * 1181 * Would like to multiply by unit_from and then divide by scale, 1182 * but if the first multiplication would overflow, then need to 1183 * divide by scale and then multiply by unit_from. 1184 */ 1185 if (number > (UINT64_MAX / (unsigned long long)unit_from)) { 1186 number = (number / (unsigned long long)scale) * 1187 (unsigned long long)unit_from; 1188 } else { 1189 number = (number * (unsigned long long)unit_from) / 1190 (unsigned long long)scale; 1191 } 1192 1193 /* 1194 * Now we have number as a count of scale units. 1195 * Stop scaling when we reached exa bytes, then something is 1196 * probably wrong with our number. 1197 */ 1198 1199 while ((number >= scale) && (*uom != 'E')) { 1200 uom++; /* next unit of measurement */ 1201 save = number; 1202 number = (number + (scale / 2)) / scale; 1203 } 1204 /* check if we should output a decimal place after the point */ 1205 if (save && ((save / scale) < 10)) { 1206 /* sprintf() will round for us */ 1207 float fnum = (float)save / scale; 1208 (void) sprintf(buf, "%2.1f%c", fnum, *uom); 1209 } else { 1210 (void) sprintf(buf, "%4llu%c", number, *uom); 1211 } 1212 return (buf); 1213 } 1214 1215 /* 1216 * The statvfs() implementation allows us to return only two values, the total 1217 * number of blocks and the number of blocks free. The equation 'used = total - 1218 * free' will not work for ZFS filesystems, due to the nature of pooled storage. 1219 * We choose to return values in the statvfs structure that will produce correct 1220 * results for 'used' and 'available', but not 'total'. This function will open 1221 * the underlying ZFS dataset if necessary and get the real value. 1222 */ 1223 static void 1224 adjust_total_blocks(struct df_request *dfrp, fsblkcnt64_t *total, 1225 uint64_t blocksize) 1226 { 1227 zfs_handle_t *zhp; 1228 char *dataset, *slash; 1229 uint64_t quota; 1230 1231 if (strcmp(DFR_FSTYPE(dfrp), MNTTYPE_ZFS) != 0 || 1232 _zfs_open == NULL) 1233 return; 1234 1235 /* 1236 * We want to get the total size for this filesystem as bounded by any 1237 * quotas. In order to do this, we start at the current filesystem and 1238 * work upwards until we find a dataset with a quota. If we reach the 1239 * pool itself, then the total space is the amount used plus the amount 1240 * available. 1241 */ 1242 if ((dataset = strdup(DFR_SPECIAL(dfrp))) == NULL) 1243 return; 1244 1245 slash = dataset + strlen(dataset); 1246 do { 1247 *slash = '\0'; 1248 1249 if ((zhp = _zfs_open(dataset, ZFS_TYPE_ANY)) == NULL) { 1250 free(dataset); 1251 return; 1252 } 1253 1254 if ((quota = _zfs_prop_get_int(zhp, ZFS_PROP_QUOTA)) != 0) { 1255 *total = quota / blocksize; 1256 _zfs_close(zhp); 1257 free(dataset); 1258 return; 1259 } 1260 1261 _zfs_close(zhp); 1262 1263 } while ((slash = strrchr(dataset, '/')) != NULL); 1264 1265 1266 if ((zhp = _zfs_open(dataset, ZFS_TYPE_ANY)) == NULL) { 1267 free(dataset); 1268 return; 1269 } 1270 1271 *total = (_zfs_prop_get_int(zhp, ZFS_PROP_USED) + 1272 _zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE)) / blocksize; 1273 1274 _zfs_close(zhp); 1275 free(dataset); 1276 } 1277 1278 /* 1279 * The output will appear properly columnized regardless of the names of 1280 * the various fields 1281 */ 1282 static void 1283 g_output(struct df_request *dfrp, struct statvfs64 *fsp) 1284 { 1285 fsblkcnt64_t available_blocks = fsp->f_bavail; 1286 fsblkcnt64_t total_blocks = fsp->f_blocks; 1287 numbuf_t total_blocks_buf; 1288 numbuf_t total_files_buf; 1289 numbuf_t free_blocks_buf; 1290 numbuf_t available_blocks_buf; 1291 numbuf_t free_files_buf; 1292 numbuf_t fname_buf; 1293 char *temp_buf; 1294 1295 #define DEFINE_STR_LEN(var) \ 1296 static char *var##_str; \ 1297 static size_t var##_len 1298 1299 #define SET_STR_LEN(name, var)\ 1300 if (! var##_str) {\ 1301 var##_str = TRANSLATE(name); \ 1302 var##_len = strlen(var##_str); \ 1303 } 1304 1305 DEFINE_STR_LEN(block_size); 1306 DEFINE_STR_LEN(frag_size); 1307 DEFINE_STR_LEN(total_blocks); 1308 DEFINE_STR_LEN(free_blocks); 1309 DEFINE_STR_LEN(available); 1310 DEFINE_STR_LEN(total_files); 1311 DEFINE_STR_LEN(free_files); 1312 DEFINE_STR_LEN(fstype); 1313 DEFINE_STR_LEN(fsys_id); 1314 DEFINE_STR_LEN(fname); 1315 DEFINE_STR_LEN(flag); 1316 1317 /* 1318 * TRANSLATION_NOTE 1319 * The first argument of each of the following macro invocations is a 1320 * string that needs to be translated. 1321 */ 1322 SET_STR_LEN("block size", block_size); 1323 SET_STR_LEN("frag size", frag_size); 1324 SET_STR_LEN("total blocks", total_blocks); 1325 SET_STR_LEN("free blocks", free_blocks); 1326 SET_STR_LEN("available", available); 1327 SET_STR_LEN("total files", total_files); 1328 SET_STR_LEN("free files", free_files); 1329 SET_STR_LEN("fstype", fstype); 1330 SET_STR_LEN("filesys id", fsys_id); 1331 SET_STR_LEN("filename length", fname); 1332 SET_STR_LEN("flag", flag); 1333 1334 #define NCOL1_WIDTH (int)MAX3(BLOCK_WIDTH, NFILES_WIDTH, FSTYPE_WIDTH) 1335 #define NCOL2_WIDTH (int)MAX3(BLOCK_WIDTH, FSID_WIDTH, FLAG_WIDTH) + 2 1336 #define NCOL3_WIDTH (int)MAX3(BSIZE_WIDTH, BLOCK_WIDTH, NAMELEN_WIDTH) 1337 #define NCOL4_WIDTH (int)MAX(FRAGSIZE_WIDTH, NFILES_WIDTH) 1338 1339 #define SCOL1_WIDTH (int)MAX3(total_blocks_len, free_files_len, fstype_len) 1340 #define SCOL2_WIDTH (int)MAX3(free_blocks_len, fsys_id_len, flag_len) 1341 #define SCOL3_WIDTH (int)MAX3(block_size_len, available_len, fname_len) 1342 #define SCOL4_WIDTH (int)MAX(frag_size_len, total_files_len) 1343 1344 temp_buf = xmalloc( 1345 MAX(MOUNT_POINT_WIDTH, strlen(DFR_MOUNT_POINT(dfrp))) 1346 + MAX(SPECIAL_DEVICE_WIDTH, strlen(DFR_SPECIAL(dfrp))) 1347 + 20); /* plus slop - nulls & formatting */ 1348 (void) sprintf(temp_buf, "%-*s(%-*s):", 1349 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1350 SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp)); 1351 1352 (void) printf("%-*s %*lu %-*s %*lu %-*s\n", 1353 NCOL1_WIDTH + 1 + SCOL1_WIDTH + 1 + NCOL2_WIDTH + 1 + SCOL2_WIDTH, 1354 temp_buf, 1355 NCOL3_WIDTH, fsp->f_bsize, SCOL3_WIDTH, block_size_str, 1356 NCOL4_WIDTH, fsp->f_frsize, SCOL4_WIDTH, frag_size_str); 1357 free(temp_buf); 1358 1359 /* 1360 * Adjust available_blocks value - it can be less than 0 on 1361 * a 4.x file system. Reset it to 0 in order to avoid printing 1362 * negative numbers. 1363 */ 1364 if ((long long)available_blocks < (long long)0) 1365 available_blocks = (fsblkcnt64_t)0; 1366 1367 adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize); 1368 1369 (void) printf("%*s %-*s %*s %-*s %*s %-*s %*s %-*s\n", 1370 NCOL1_WIDTH, number_to_string(total_blocks_buf, 1371 total_blocks, fsp->f_frsize, 512), 1372 SCOL1_WIDTH, total_blocks_str, 1373 NCOL2_WIDTH, number_to_string(free_blocks_buf, 1374 fsp->f_bfree, fsp->f_frsize, 512), 1375 SCOL2_WIDTH, free_blocks_str, 1376 NCOL3_WIDTH, number_to_string(available_blocks_buf, 1377 available_blocks, fsp->f_frsize, 512), 1378 SCOL3_WIDTH, available_str, 1379 NCOL4_WIDTH, number_to_string(total_files_buf, 1380 fsp->f_files, 1, 1), 1381 SCOL4_WIDTH, total_files_str); 1382 1383 (void) printf("%*s %-*s %*lu %-*s %s\n", 1384 NCOL1_WIDTH, number_to_string(free_files_buf, 1385 fsp->f_ffree, 1, 1), 1386 SCOL1_WIDTH, free_files_str, 1387 NCOL2_WIDTH, fsp->f_fsid, SCOL2_WIDTH, fsys_id_str, 1388 fsp->f_fstr); 1389 1390 (void) printf("%*s %-*s %#*.*lx %-*s %*s %-*s\n\n", 1391 NCOL1_WIDTH, fsp->f_basetype, SCOL1_WIDTH, fstype_str, 1392 NCOL2_WIDTH, NCOL2_WIDTH-2, fsp->f_flag, SCOL2_WIDTH, flag_str, 1393 NCOL3_WIDTH, number_to_string(fname_buf, 1394 (unsigned long long)fsp->f_namemax, 1, 1), 1395 SCOL3_WIDTH, fname_str); 1396 } 1397 1398 1399 static void 1400 k_output(struct df_request *dfrp, struct statvfs64 *fsp) 1401 { 1402 fsblkcnt64_t total_blocks = fsp->f_blocks; 1403 fsblkcnt64_t free_blocks = fsp->f_bfree; 1404 fsblkcnt64_t available_blocks = fsp->f_bavail; 1405 fsblkcnt64_t used_blocks; 1406 char *file_system = DFR_SPECIAL(dfrp); 1407 numbuf_t total_blocks_buf; 1408 numbuf_t used_blocks_buf; 1409 numbuf_t available_blocks_buf; 1410 char capacity_buf[LINEBUF_SIZE]; 1411 1412 /* 1413 * If the free block count is -1, don't trust anything but the total 1414 * number of blocks. 1415 */ 1416 if (free_blocks == (fsblkcnt64_t)-1) { 1417 used_blocks = (fsblkcnt64_t)-1; 1418 (void) strcpy(capacity_buf, " 100%"); 1419 } else { 1420 fsblkcnt64_t reserved_blocks = free_blocks - available_blocks; 1421 1422 used_blocks = total_blocks - free_blocks; 1423 1424 /* 1425 * The capacity estimation is bogus when available_blocks is 0 1426 * and the super-user has allocated more space. The reason 1427 * is that reserved_blocks is inaccurate in that case, because 1428 * when the super-user allocates space, free_blocks is updated 1429 * but available_blocks is not (since it can't drop below 0). 1430 * 1431 * XCU4 and POSIX.2 require that any fractional result of the 1432 * capacity estimation be rounded to the next highest integer, 1433 * hence the addition of 0.5. 1434 */ 1435 (void) sprintf(capacity_buf, "%5.0f%%", 1436 (total_blocks == 0) ? 0.0 : 1437 ((double)used_blocks / 1438 (double)(total_blocks - reserved_blocks)) 1439 * 100.0 + 0.5); 1440 } 1441 1442 /* 1443 * The available_blocks can be less than 0 on a 4.x file system. 1444 * Reset it to 0 in order to avoid printing negative numbers. 1445 */ 1446 if ((long long)available_blocks < (long long)0) 1447 available_blocks = (fsblkcnt64_t)0; 1448 /* 1449 * Print long special device names (usually NFS mounts) in a line 1450 * by themselves when the output is directed to a terminal. 1451 */ 1452 if (tty_output && strlen(file_system) > (size_t)FILESYSTEM_WIDTH) { 1453 (void) printf("%s\n", file_system); 1454 file_system = ""; 1455 } 1456 1457 adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize); 1458 1459 if (use_scaling) { /* comes from the -h option */ 1460 (void) printf("%-*s %*s %*s %*s %-*s %-s\n", 1461 FILESYSTEM_WIDTH, file_system, 1462 SCALED_WIDTH, number_to_scaled_string(total_blocks_buf, 1463 total_blocks, fsp->f_frsize, scale), 1464 SCALED_WIDTH, number_to_scaled_string(used_blocks_buf, 1465 used_blocks, fsp->f_frsize, scale), 1466 AVAILABLE_WIDTH, number_to_scaled_string(available_blocks_buf, 1467 available_blocks, fsp->f_frsize, scale), 1468 CAPACITY_WIDTH, capacity_buf, 1469 DFR_MOUNT_POINT(dfrp)); 1470 return; 1471 } 1472 1473 if (v_option) { 1474 (void) printf("%-*.*s %-*.*s %*lld %*lld %*lld %-.*s\n", 1475 IBCS2_MOUNT_POINT_WIDTH, IBCS2_MOUNT_POINT_WIDTH, 1476 DFR_MOUNT_POINT(dfrp), 1477 IBCS2_FILESYSTEM_WIDTH, IBCS2_FILESYSTEM_WIDTH, file_system, 1478 BLOCK_WIDTH, total_blocks, 1479 BLOCK_WIDTH, used_blocks, 1480 BLOCK_WIDTH, available_blocks, 1481 CAPACITY_WIDTH, capacity_buf); 1482 return; 1483 } 1484 1485 if (P_option && !k_option) { 1486 (void) printf("%-*s %*s %*s %*s %-*s %-s\n", 1487 FILESYSTEM_WIDTH, file_system, 1488 KBYTE_WIDTH, number_to_string(total_blocks_buf, 1489 total_blocks, fsp->f_frsize, 512), 1490 KBYTE_WIDTH, number_to_string(used_blocks_buf, 1491 used_blocks, fsp->f_frsize, 512), 1492 KBYTE_WIDTH, number_to_string(available_blocks_buf, 1493 available_blocks, fsp->f_frsize, 512), 1494 CAPACITY_WIDTH, capacity_buf, 1495 DFR_MOUNT_POINT(dfrp)); 1496 } else { 1497 (void) printf("%-*s %*s %*s %*s %-*s %-s\n", 1498 FILESYSTEM_WIDTH, file_system, 1499 KBYTE_WIDTH, number_to_string(total_blocks_buf, 1500 total_blocks, fsp->f_frsize, 1024), 1501 KBYTE_WIDTH, number_to_string(used_blocks_buf, 1502 used_blocks, fsp->f_frsize, 1024), 1503 KBYTE_WIDTH, number_to_string(available_blocks_buf, 1504 available_blocks, fsp->f_frsize, 1024), 1505 CAPACITY_WIDTH, capacity_buf, 1506 DFR_MOUNT_POINT(dfrp)); 1507 } 1508 } 1509 1510 /* 1511 * The following is for internationalization support. 1512 */ 1513 static bool_int strings_initialized; 1514 static char *files_str; 1515 static char *blocks_str; 1516 static char *total_str; 1517 static char *kilobytes_str; 1518 1519 static void 1520 strings_init() 1521 { 1522 total_str = TRANSLATE("total"); 1523 #ifdef _iBCS2 1524 /* ISC/SCO print i-nodes instead of files */ 1525 if (sysv3_set) 1526 files_str = TRANSLATE("i-nodes"); 1527 else 1528 #endif /* _iBCS2 */ 1529 files_str = TRANSLATE("files"); 1530 blocks_str = TRANSLATE("blocks"); 1531 kilobytes_str = TRANSLATE("kilobytes"); 1532 strings_initialized = TRUE; 1533 } 1534 1535 #define STRINGS_INIT() if (!strings_initialized) strings_init() 1536 1537 1538 static void 1539 t_output(struct df_request *dfrp, struct statvfs64 *fsp) 1540 { 1541 fsblkcnt64_t total_blocks = fsp->f_blocks; 1542 numbuf_t total_blocks_buf; 1543 numbuf_t total_files_buf; 1544 numbuf_t free_blocks_buf; 1545 numbuf_t free_files_buf; 1546 1547 STRINGS_INIT(); 1548 1549 adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize); 1550 1551 (void) printf("%-*s(%-*s): %*s %s %*s %s\n", 1552 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1553 SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp), 1554 BLOCK_WIDTH, number_to_string(free_blocks_buf, 1555 fsp->f_bfree, fsp->f_frsize, 512), 1556 blocks_str, 1557 NFILES_WIDTH, number_to_string(free_files_buf, 1558 fsp->f_ffree, 1, 1), 1559 files_str); 1560 /* 1561 * The total column used to use the same space as the mnt pt & special 1562 * dev fields. However, this doesn't work with massive special dev 1563 * fields * (eg > 500 chars) causing an enormous amount of white space 1564 * before the total column (see bug 4100411). So the code was 1565 * simplified to set the total column at the usual gap. 1566 * This had the side effect of fixing a bug where the previously 1567 * used static buffer was overflowed by the same massive special dev. 1568 */ 1569 (void) printf("%*s: %*s %s %*s %s\n", 1570 MNT_SPEC_WIDTH, total_str, 1571 BLOCK_WIDTH, number_to_string(total_blocks_buf, 1572 total_blocks, fsp->f_frsize, 512), 1573 blocks_str, 1574 NFILES_WIDTH, number_to_string(total_files_buf, 1575 fsp->f_files, 1, 1), 1576 files_str); 1577 } 1578 1579 1580 static void 1581 eb_output(struct df_request *dfrp, struct statvfs64 *fsp) 1582 { 1583 numbuf_t free_files_buf; 1584 numbuf_t free_kbytes_buf; 1585 1586 STRINGS_INIT(); 1587 1588 (void) printf("%-*s(%-*s): %*s %s\n", 1589 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1590 SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp), 1591 MAX(KBYTE_WIDTH, NFILES_WIDTH), 1592 number_to_string(free_kbytes_buf, 1593 fsp->f_bfree, fsp->f_frsize, 1024), 1594 kilobytes_str); 1595 (void) printf("%-*s(%-*s): %*s %s\n", 1596 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1597 SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp), 1598 MAX(NFILES_WIDTH, NFILES_WIDTH), 1599 number_to_string(free_files_buf, fsp->f_ffree, 1, 1), 1600 files_str); 1601 } 1602 1603 1604 static void 1605 e_output(struct df_request *dfrp, struct statvfs64 *fsp) 1606 { 1607 numbuf_t free_files_buf; 1608 1609 (void) printf("%-*s %*s\n", 1610 FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp), 1611 NFILES_WIDTH, 1612 number_to_string(free_files_buf, fsp->f_ffree, 1, 1)); 1613 } 1614 1615 1616 static void 1617 b_output(struct df_request *dfrp, struct statvfs64 *fsp) 1618 { 1619 numbuf_t free_blocks_buf; 1620 1621 (void) printf("%-*s %*s\n", 1622 FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp), 1623 BLOCK_WIDTH, number_to_string(free_blocks_buf, 1624 fsp->f_bfree, fsp->f_frsize, 1024)); 1625 } 1626 1627 1628 /* ARGSUSED */ 1629 static void 1630 n_output(struct df_request *dfrp, struct statvfs64 *fsp) 1631 { 1632 (void) printf("%-*s: %-*s\n", 1633 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1634 FSTYPE_WIDTH, dfrp->dfr_fstype); 1635 } 1636 1637 1638 static void 1639 default_output(struct df_request *dfrp, struct statvfs64 *fsp) 1640 { 1641 numbuf_t free_blocks_buf; 1642 numbuf_t free_files_buf; 1643 1644 STRINGS_INIT(); 1645 1646 (void) printf("%-*s(%-*s):%*s %s %*s %s\n", 1647 MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp), 1648 SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp), 1649 BLOCK_WIDTH, number_to_string(free_blocks_buf, 1650 fsp->f_bfree, fsp->f_frsize, 512), 1651 blocks_str, 1652 NFILES_WIDTH, number_to_string(free_files_buf, 1653 fsp->f_ffree, 1, 1), 1654 files_str); 1655 } 1656 1657 1658 /* ARGSUSED */ 1659 static void 1660 V_output(struct df_request *dfrp, struct statvfs64 *fsp) 1661 { 1662 char temp_buf[LINEBUF_SIZE]; 1663 1664 if (df_options_len > 1) 1665 (void) strcat(strcpy(temp_buf, df_options), " "); 1666 else 1667 temp_buf[0] = NUL; 1668 1669 (void) printf("%s -F %s %s%s\n", 1670 program_name, dfrp->dfr_fstype, temp_buf, 1671 dfrp->dfr_cmd_arg ? dfrp->dfr_cmd_arg: DFR_SPECIAL(dfrp)); 1672 } 1673 1674 1675 /* 1676 * This function is used to sort the array of df_requests according to fstype 1677 */ 1678 static int 1679 df_reqcomp(const void *p1, const void *p2) 1680 { 1681 int v = strcmp(DFRP(p1)->dfr_fstype, DFRP(p2)->dfr_fstype); 1682 1683 if (v != 0) 1684 return (v); 1685 else 1686 return (DFRP(p1)->dfr_index - DFRP(p2)->dfr_index); 1687 } 1688 1689 1690 static void 1691 vfs_error(char *file, int status) 1692 { 1693 if (status == VFS_TOOLONG) 1694 errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters", 1695 file, MNT_LINE_MAX); 1696 else if (status == VFS_TOOMANY) 1697 errmsg(ERR_NOFLAGS, "a line in %s has too many fields", file); 1698 else if (status == VFS_TOOFEW) 1699 errmsg(ERR_NOFLAGS, "a line in %s has too few fields", file); 1700 else 1701 errmsg(ERR_NOFLAGS, "error while reading %s: %d", file, status); 1702 } 1703 1704 1705 /* 1706 * Try to determine the fstype for the specified block device. 1707 * Return in order of decreasing preference: 1708 * file system type from vfstab 1709 * file system type as specified by -F option 1710 * default file system type 1711 */ 1712 static char * 1713 find_fstype(char *special) 1714 { 1715 struct vfstab vtab; 1716 FILE *fp; 1717 int status; 1718 char *vfstab_file = VFS_TAB; 1719 1720 fp = xfopen(vfstab_file); 1721 status = getvfsspec(fp, &vtab, special); 1722 (void) fclose(fp); 1723 if (status > 0) 1724 vfs_error(vfstab_file, status); 1725 1726 if (status == 0) { 1727 if (F_option && ! EQ(FSType, vtab.vfs_fstype)) 1728 errmsg(ERR_NOFLAGS, 1729 "warning: %s is of type %s", special, vtab.vfs_fstype); 1730 return (new_string(vtab.vfs_fstype)); 1731 } 1732 else 1733 return (F_option ? FSType : default_fstype(special)); 1734 } 1735 1736 /* 1737 * When this function returns, the following fields are filled for all 1738 * valid entries in the requests[] array: 1739 * dfr_mte (if the file system is mounted) 1740 * dfr_fstype 1741 * dfr_index 1742 * 1743 * The function returns the number of errors that occurred while building 1744 * the request list. 1745 */ 1746 static int 1747 create_request_list( 1748 int argc, 1749 char *argv[], 1750 struct df_request *requests_p[], 1751 size_t *request_count) 1752 { 1753 struct df_request *requests; 1754 struct df_request *dfrp; 1755 size_t size; 1756 size_t i; 1757 size_t request_index = 0; 1758 size_t max_requests; 1759 int errors = 0; 1760 1761 /* 1762 * If no args, use the mounted file systems, otherwise use the 1763 * user-specified arguments. 1764 */ 1765 if (argc == 0) { 1766 mtab_read_file(); 1767 max_requests = mount_table_entries; 1768 } else 1769 max_requests = argc; 1770 1771 size = max_requests * sizeof (struct df_request); 1772 requests = xmalloc(size); 1773 (void) memset(requests, 0, size); 1774 1775 if (argc == 0) { 1776 /* 1777 * If -Z wasn't specified, we skip mounts in other 1778 * zones. This obviously is a noop in a non-global 1779 * zone. 1780 */ 1781 boolean_t showall = (getzoneid() != GLOBAL_ZONEID) || Z_option; 1782 struct zone_summary *zsp; 1783 1784 if (!showall) { 1785 zsp = fs_get_zone_summaries(); 1786 if (zsp == NULL) 1787 errmsg(ERR_FATAL, 1788 "unable to retrieve list of zones"); 1789 } 1790 1791 for (i = 0; i < mount_table_entries; i++) { 1792 struct extmnttab *mtp = mount_table[i].mte_mount; 1793 1794 if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP)) 1795 continue; 1796 1797 if (!showall) { 1798 if (fs_mount_in_other_zone(zsp, 1799 mtp->mnt_mountp)) 1800 continue; 1801 } 1802 dfrp = &requests[request_index++]; 1803 dfrp->dfr_mte = &mount_table[i]; 1804 dfrp->dfr_fstype = mtp->mnt_fstype; 1805 dfrp->dfr_index = i; 1806 dfrp->dfr_valid = TRUE; 1807 } 1808 } else { 1809 struct stat64 *arg_stat; /* array of stat structures */ 1810 bool_int *valid_stat; /* which structures are valid */ 1811 1812 arg_stat = xmalloc(argc * sizeof (struct stat64)); 1813 valid_stat = xmalloc(argc * sizeof (bool_int)); 1814 1815 /* 1816 * Obtain stat64 information for each argument before 1817 * constructing the list of mounted file systems. By 1818 * touching all these places we force the automounter 1819 * to establish any mounts required to access the arguments, 1820 * so that the corresponding mount table entries will exist 1821 * when we look for them. 1822 * It is still possible that the automounter may timeout 1823 * mounts between the time we read the mount table and the 1824 * time we process the request. Even in that case, when 1825 * we issue the statvfs64(2) for the mount point, the file 1826 * system will be mounted again. The only problem will 1827 * occur if the automounter maps change in the meantime 1828 * and the mount point is eliminated. 1829 */ 1830 for (i = 0; i < argc; i++) 1831 valid_stat[i] = (stat64(argv[i], &arg_stat[i]) == 0); 1832 1833 mtab_read_file(); 1834 1835 for (i = 0; i < argc; i++) { 1836 char *arg = argv[i]; 1837 1838 dfrp = &requests[request_index]; 1839 1840 dfrp->dfr_index = request_index; 1841 dfrp->dfr_cmd_arg = arg; 1842 1843 if (valid_stat[i]) { 1844 if (S_ISBLK(arg_stat[i].st_mode)) { 1845 bdev_mount_entry(dfrp); 1846 dfrp->dfr_valid = TRUE; 1847 } else if (S_ISDIR(arg_stat[i].st_mode) || 1848 S_ISREG(arg_stat[i].st_mode) || 1849 S_ISFIFO(arg_stat[i].st_mode)) { 1850 path_mount_entry(dfrp, 1851 arg_stat[i].st_dev); 1852 if (! DFR_ISMOUNTEDFS(dfrp)) { 1853 errors++; 1854 continue; 1855 } 1856 dfrp->dfr_valid = TRUE; 1857 } 1858 } else { 1859 resource_mount_entry(dfrp); 1860 dfrp->dfr_valid = DFR_ISMOUNTEDFS(dfrp); 1861 } 1862 1863 /* 1864 * If we haven't managed to verify that the request 1865 * is valid, we must have gotten a bad argument. 1866 */ 1867 if (!dfrp->dfr_valid) { 1868 errmsg(ERR_NOFLAGS, 1869 "(%-10s) not a block device, directory or mounted resource", 1870 arg); 1871 errors++; 1872 continue; 1873 } 1874 1875 /* 1876 * Determine the file system type. 1877 */ 1878 if (DFR_ISMOUNTEDFS(dfrp)) 1879 dfrp->dfr_fstype = 1880 dfrp->dfr_mte->mte_mount->mnt_fstype; 1881 else 1882 dfrp->dfr_fstype = 1883 find_fstype(dfrp->dfr_cmd_arg); 1884 1885 request_index++; 1886 } 1887 } 1888 *requests_p = requests; 1889 *request_count = request_index; 1890 return (errors); 1891 } 1892 1893 1894 /* 1895 * Select the appropriate function and flags to use for output. 1896 * Notice that using both -e and -b options produces a different form of 1897 * output than either of those two options alone; this is the behavior of 1898 * the SVR4 df. 1899 */ 1900 static struct df_output * 1901 select_output() 1902 { 1903 static struct df_output dfo; 1904 1905 /* 1906 * The order of checking options follows the option precedence 1907 * rules as they are listed in the man page. 1908 */ 1909 if (use_scaling) { /* comes from the -h option */ 1910 dfo.dfo_func = k_output; 1911 dfo.dfo_flags = DFO_HEADER + DFO_STATVFS; 1912 } else if (V_option) { 1913 dfo.dfo_func = V_output; 1914 dfo.dfo_flags = DFO_NOFLAGS; 1915 } else if (g_option) { 1916 dfo.dfo_func = g_output; 1917 dfo.dfo_flags = DFO_STATVFS; 1918 } else if (k_option || P_option || v_option) { 1919 dfo.dfo_func = k_output; 1920 dfo.dfo_flags = DFO_HEADER + DFO_STATVFS; 1921 } else if (t_option) { 1922 dfo.dfo_func = t_output; 1923 dfo.dfo_flags = DFO_STATVFS; 1924 } else if (b_option && e_option) { 1925 dfo.dfo_func = eb_output; 1926 dfo.dfo_flags = DFO_STATVFS; 1927 } else if (b_option) { 1928 dfo.dfo_func = b_output; 1929 dfo.dfo_flags = DFO_HEADER + DFO_STATVFS; 1930 } else if (e_option) { 1931 dfo.dfo_func = e_output; 1932 dfo.dfo_flags = DFO_HEADER + DFO_STATVFS; 1933 } else if (n_option) { 1934 dfo.dfo_func = n_output; 1935 dfo.dfo_flags = DFO_NOFLAGS; 1936 } else { 1937 dfo.dfo_func = default_output; 1938 dfo.dfo_flags = DFO_STATVFS; 1939 } 1940 return (&dfo); 1941 } 1942 1943 1944 /* 1945 * The (argc,argv) pair contains all the non-option arguments 1946 */ 1947 static void 1948 do_df(int argc, char *argv[]) 1949 { 1950 size_t i; 1951 struct df_request *requests; /* array of requests */ 1952 size_t n_requests; 1953 struct df_request *dfrp; 1954 int errors; 1955 1956 errors = create_request_list(argc, argv, &requests, &n_requests); 1957 1958 if (n_requests == 0) 1959 exit(errors); 1960 1961 /* 1962 * If we are going to run the FSType-specific df command, 1963 * rearrange the requests so that we can issue a single command 1964 * per file system type. 1965 */ 1966 if (o_option) { 1967 size_t j; 1968 1969 /* 1970 * qsort is not a stable sorting method (i.e. requests of 1971 * the same file system type may be swapped, and hence appear 1972 * in the output in a different order from the one in which 1973 * they were listed in the command line). In order to force 1974 * stability, we use the dfr_index field which is unique 1975 * for each request. 1976 */ 1977 qsort(requests, 1978 n_requests, sizeof (struct df_request), df_reqcomp); 1979 for (i = 0; i < n_requests; i = j) { 1980 char *fstype = requests[i].dfr_fstype; 1981 1982 for (j = i+1; j < n_requests; j++) 1983 if (! EQ(fstype, requests[j].dfr_fstype)) 1984 break; 1985 1986 /* 1987 * At this point, requests in the range [i,j) are 1988 * of the same type. 1989 * 1990 * If the -F option was used, and the user specified 1991 * arguments, the filesystem types must match 1992 * 1993 * XXX: the alternative of doing this check here is to 1994 * invoke prune_list, but then we have to 1995 * modify this code to ignore invalid requests. 1996 */ 1997 if (F_option && ! EQ(fstype, FSType)) { 1998 size_t k; 1999 2000 for (k = i; k < j; k++) { 2001 dfrp = &requests[k]; 2002 if (dfrp->dfr_cmd_arg != NULL) { 2003 errmsg(ERR_NOFLAGS, 2004 "Warning: %s mounted as a %s file system", 2005 dfrp->dfr_cmd_arg, dfrp->dfr_fstype); 2006 errors++; 2007 } 2008 } 2009 } else 2010 errors += run_fs_specific_df(&requests[i], j-i); 2011 } 2012 } else { 2013 size_t valid_requests; 2014 2015 /* 2016 * We have to prune the request list to avoid printing a header 2017 * if there are no valid requests 2018 */ 2019 errors += prune_list(requests, n_requests, &valid_requests); 2020 2021 if (valid_requests) { 2022 struct df_output *dfop = select_output(); 2023 2024 /* indicates if we already printed out a header line */ 2025 int printed_header = 0; 2026 2027 for (i = 0; i < n_requests; i++) { 2028 dfrp = &requests[i]; 2029 if (! dfrp->dfr_valid) 2030 continue; 2031 2032 /* 2033 * If we don't have a mount point, 2034 * this must be a block device. 2035 */ 2036 if (DFR_ISMOUNTEDFS(dfrp)) { 2037 struct statvfs64 stvfs; 2038 2039 if ((dfop->dfo_flags & DFO_STATVFS) && 2040 statvfs64(DFR_MOUNT_POINT(dfrp), 2041 &stvfs) == -1) { 2042 errmsg(ERR_PERROR, 2043 "cannot statvfs %s:", 2044 DFR_MOUNT_POINT(dfrp)); 2045 errors++; 2046 continue; 2047 } 2048 if ((!printed_header) && 2049 (dfop->dfo_flags & DFO_HEADER)) { 2050 print_header(); 2051 printed_header = 1; 2052 } 2053 2054 (*dfop->dfo_func)(dfrp, &stvfs); 2055 } else { 2056 /* 2057 * -h option only works for 2058 * mounted filesystems 2059 */ 2060 if (use_scaling) { 2061 errmsg(ERR_NOFLAGS, 2062 "-h option incompatible with unmounted special device (%s)", 2063 dfrp->dfr_cmd_arg); 2064 errors++; 2065 continue; 2066 } 2067 errors += run_fs_specific_df(dfrp, 1); 2068 } 2069 } 2070 } 2071 } 2072 exit(errors); 2073 } 2074 2075 2076 /* 2077 * The rest of this file implements the devnm command 2078 */ 2079 2080 static char * 2081 find_dev_name(char *file, dev_t dev) 2082 { 2083 struct df_request dfreq; 2084 2085 dfreq.dfr_cmd_arg = file; 2086 dfreq.dfr_fstype = 0; 2087 dfreq.dfr_mte = NULL; 2088 path_mount_entry(&dfreq, dev); 2089 return (DFR_ISMOUNTEDFS(&dfreq) ? DFR_SPECIAL(&dfreq) : NULL); 2090 } 2091 2092 2093 static void 2094 do_devnm(int argc, char *argv[]) 2095 { 2096 int arg; 2097 int errors = 0; 2098 char *dev_name; 2099 2100 if (argc == 1) 2101 errmsg(ERR_NONAME, "Usage: %s name ...", DEVNM_CMD); 2102 2103 mtab_read_file(); 2104 2105 for (arg = 1; arg < argc; arg++) { 2106 char *file = argv[arg]; 2107 struct stat64 st; 2108 2109 if (stat64(file, &st) == -1) { 2110 errmsg(ERR_PERROR, "%s: ", file); 2111 errors++; 2112 continue; 2113 } 2114 2115 if (! is_remote_fs(st.st_fstype) && 2116 ! EQ(st.st_fstype, MNTTYPE_TMPFS) && 2117 (dev_name = find_dev_name(file, st.st_dev))) 2118 (void) printf("%s %s\n", dev_name, file); 2119 else 2120 errmsg(ERR_NOFLAGS, 2121 "%s not found", file); 2122 } 2123 exit(errors); 2124 /* NOTREACHED */ 2125 } 2126