1 /* 2 * Copyright (c) 1980, 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 static const char copyright[] = 41 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43 #endif /* not lint */ 44 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)df.c 8.9 (Berkeley) 5/8/95"; 48 #else 49 static const char rcsid[] = 50 "$Id: df.c,v 1.16 1997/03/28 15:24:17 imp Exp $"; 51 #endif 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/stat.h> 56 #include <sys/mount.h> 57 #include <ufs/ufs/ufsmount.h> 58 59 #include <err.h> 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 int checkvfsname __P((const char *, char **)); 68 char **makevfslist __P((char *)); 69 long regetmntinfo __P((struct statfs **, long, char **)); 70 int bread __P((off_t, void *, int)); 71 char *getmntpt __P((char *)); 72 void prtstat __P((struct statfs *, int)); 73 void ufs_df __P((char *, int)); 74 void usage __P((void)); 75 76 int iflag, nflag; 77 struct ufs_args mdev; 78 79 int 80 main(argc, argv) 81 int argc; 82 char *argv[]; 83 { 84 struct stat stbuf; 85 struct statfs statfsbuf, *mntbuf; 86 long mntsize; 87 int ch, err, i, maxwidth, width; 88 char *mntpt, **vfslist; 89 90 vfslist = NULL; 91 while ((ch = getopt(argc, argv, "iknt:")) != -1) 92 switch (ch) { 93 case 'i': 94 iflag = 1; 95 break; 96 case 'k': 97 putenv("BLOCKSIZE=1k"); 98 break; 99 case 'n': 100 nflag = 1; 101 break; 102 case 't': 103 if (vfslist != NULL) 104 errx(1, "only one -t option may be specified."); 105 vfslist = makevfslist(optarg); 106 break; 107 case '?': 108 default: 109 usage(); 110 } 111 argc -= optind; 112 argv += optind; 113 114 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 115 maxwidth = 0; 116 for (i = 0; i < mntsize; i++) { 117 width = strlen(mntbuf[i].f_mntfromname); 118 if (width > maxwidth) 119 maxwidth = width; 120 } 121 122 if (!*argv) { 123 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); 124 if (vfslist != NULL) { 125 maxwidth = 0; 126 for (i = 0; i < mntsize; i++) { 127 width = strlen(mntbuf[i].f_mntfromname); 128 if (width > maxwidth) 129 maxwidth = width; 130 } 131 } 132 for (i = 0; i < mntsize; i++) 133 prtstat(&mntbuf[i], maxwidth); 134 exit(0); 135 } 136 137 for (; *argv; argv++) { 138 if (stat(*argv, &stbuf) < 0) { 139 err = errno; 140 if ((mntpt = getmntpt(*argv)) == 0) { 141 warn("%s", *argv); 142 continue; 143 } 144 } else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) { 145 ufs_df(*argv, maxwidth); 146 continue; 147 } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) { 148 if ((mntpt = getmntpt(*argv)) == 0) { 149 mntpt = mktemp(strdup("/tmp/df.XXXXXX")); 150 mdev.fspec = *argv; 151 if (mkdir(mntpt, DEFFILEMODE) != 0) { 152 warn("%s", mntpt); 153 continue; 154 } 155 if (mount("ufs", mntpt, MNT_RDONLY, 156 &mdev) != 0) { 157 ufs_df(*argv, maxwidth); 158 (void)rmdir(mntpt); 159 continue; 160 } else if (statfs(mntpt, &statfsbuf) == 0) { 161 statfsbuf.f_mntonname[0] = '\0'; 162 prtstat(&statfsbuf, maxwidth); 163 } else 164 warn("%s", *argv); 165 (void)unmount(mntpt, 0); 166 (void)rmdir(mntpt); 167 continue; 168 } 169 } else 170 mntpt = *argv; 171 /* 172 * Statfs does not take a `wait' flag, so we cannot 173 * implement nflag here. 174 */ 175 if (statfs(mntpt, &statfsbuf) < 0) { 176 warn("%s", mntpt); 177 continue; 178 } 179 if (argc == 1) 180 maxwidth = strlen(statfsbuf.f_mntfromname) + 1; 181 prtstat(&statfsbuf, maxwidth); 182 } 183 return (0); 184 } 185 186 char * 187 getmntpt(name) 188 char *name; 189 { 190 long mntsize, i; 191 struct statfs *mntbuf; 192 193 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 194 for (i = 0; i < mntsize; i++) { 195 if (!strcmp(mntbuf[i].f_mntfromname, name)) 196 return (mntbuf[i].f_mntonname); 197 } 198 return (0); 199 } 200 201 /* 202 * Make a pass over the filesystem info in ``mntbuf'' filtering out 203 * filesystem types not in vfslist and possibly re-stating to get 204 * current (not cached) info. Returns the new count of valid statfs bufs. 205 */ 206 long 207 regetmntinfo(mntbufp, mntsize, vfslist) 208 struct statfs **mntbufp; 209 long mntsize; 210 char **vfslist; 211 { 212 int i, j; 213 struct statfs *mntbuf; 214 215 if (vfslist == NULL) 216 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 217 218 mntbuf = *mntbufp; 219 for (j = 0, i = 0; i < mntsize; i++) { 220 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 221 continue; 222 if (!nflag) 223 (void)statfs(mntbuf[i].f_mntonname,&mntbuf[j]); 224 else if (i != j) 225 mntbuf[j] = mntbuf[i]; 226 j++; 227 } 228 return (j); 229 } 230 231 /* 232 * Convert statfs returned filesystem size into BLOCKSIZE units. 233 * Attempts to avoid overflow for large filesystems. 234 */ 235 #define fsbtoblk(num, fsbs, bs) \ 236 (((fsbs) != 0 && (fsbs) < (bs)) ? \ 237 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs))) 238 239 /* 240 * Print out status about a filesystem. 241 */ 242 void 243 prtstat(sfsp, maxwidth) 244 struct statfs *sfsp; 245 int maxwidth; 246 { 247 static long blocksize; 248 static int headerlen, timesthrough; 249 static char *header; 250 long used, availblks, inodes; 251 252 if (maxwidth < 11) 253 maxwidth = 11; 254 if (++timesthrough == 1) { 255 header = getbsize(&headerlen, &blocksize); 256 (void)printf("%-*.*s %s Used Avail Capacity", 257 maxwidth, maxwidth, "Filesystem", header); 258 if (iflag) 259 (void)printf(" iused ifree %%iused"); 260 (void)printf(" Mounted on\n"); 261 } 262 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname); 263 used = sfsp->f_blocks - sfsp->f_bfree; 264 availblks = sfsp->f_bavail + used; 265 (void)printf(" %*ld %8ld %8ld", headerlen, 266 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), 267 fsbtoblk(used, sfsp->f_bsize, blocksize), 268 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize)); 269 (void)printf(" %5.0f%%", 270 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 271 if (iflag) { 272 inodes = sfsp->f_files; 273 used = inodes - sfsp->f_ffree; 274 (void)printf(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree, 275 inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0); 276 } else 277 (void)printf(" "); 278 (void)printf(" %s\n", sfsp->f_mntonname); 279 } 280 281 /* 282 * This code constitutes the pre-system call Berkeley df code for extracting 283 * information from filesystem superblocks. 284 */ 285 #include <ufs/ufs/dinode.h> 286 #include <ufs/ffs/fs.h> 287 #include <errno.h> 288 #include <fstab.h> 289 290 union { 291 struct fs iu_fs; 292 char dummy[SBSIZE]; 293 } sb; 294 #define sblock sb.iu_fs 295 296 int rfd; 297 298 void 299 ufs_df(file, maxwidth) 300 char *file; 301 int maxwidth; 302 { 303 struct statfs statfsbuf; 304 struct statfs *sfsp; 305 char *mntpt; 306 static int synced; 307 308 if (synced++ == 0) 309 sync(); 310 311 if ((rfd = open(file, O_RDONLY)) < 0) { 312 warn("%s", file); 313 return; 314 } 315 if (bread((off_t)SBOFF, &sblock, SBSIZE) == 0) { 316 (void)close(rfd); 317 return; 318 } 319 sfsp = &statfsbuf; 320 sfsp->f_type = 1; 321 strcpy(sfsp->f_fstypename, "ufs"); 322 sfsp->f_flags = 0; 323 sfsp->f_bsize = sblock.fs_fsize; 324 sfsp->f_iosize = sblock.fs_bsize; 325 sfsp->f_blocks = sblock.fs_dsize; 326 sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag + 327 sblock.fs_cstotal.cs_nffree; 328 sfsp->f_bavail = freespace(&sblock, sblock.fs_minfree); 329 sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg; 330 sfsp->f_ffree = sblock.fs_cstotal.cs_nifree; 331 sfsp->f_fsid.val[0] = 0; 332 sfsp->f_fsid.val[1] = 0; 333 if ((mntpt = getmntpt(file)) == 0) 334 mntpt = ""; 335 memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN); 336 memmove(&sfsp->f_mntfromname[0], file, MNAMELEN); 337 prtstat(sfsp, maxwidth); 338 (void)close(rfd); 339 } 340 341 int 342 bread(off, buf, cnt) 343 off_t off; 344 void *buf; 345 int cnt; 346 { 347 int nr; 348 349 (void)lseek(rfd, off, SEEK_SET); 350 if ((nr = read(rfd, buf, cnt)) != cnt) { 351 /* Probably a dismounted disk if errno == EIO. */ 352 if (errno != EIO) 353 (void)fprintf(stderr, "\ndf: %qd: %s\n", 354 off, strerror(nr > 0 ? EIO : errno)); 355 return (0); 356 } 357 return (1); 358 } 359 360 void 361 usage() 362 { 363 (void)fprintf(stderr, 364 "usage: df [-ikn] [-t type] [file | filesystem ...]\n"); 365 exit(1); 366 } 367