xref: /openbsd-src/bin/df/ext2fs_df.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: ext2fs_df.c,v 1.11 2004/09/14 22:46:04 deraadt Exp $	*/
2 
3 /*
4  * This file is substantially derived from src/sys/ufs/ext2fs/ext2fs_vfsops.c:e2fs_statfs().
5  * That file's copyright is applied here.
6  */
7 
8 /* Modified for EXT2FS on NetBSD by Manuel Bouyer, April 1997 */
9 
10 /*
11  * Copyright (c) 1989, 1991, 1993, 1994
12  *      The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *      notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *      notice, this list of conditions and the following disclaimer in the
21  *      documentation and/or other materials provided with the distribution.
22  * 3. 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  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
39  */
40 
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <ufs/ext2fs/ext2fs.h>
44 #include <ufs/ext2fs/ext2fs_dinode.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <fstab.h>
50 
51 int		e2fs_df(int, char *, struct statfs *);
52 
53 extern int	bread(int, off_t, void *, int);
54 extern char	*getmntpt(char *);
55 
56 static union {
57 	struct ext2fs ie_fs;
58 	char dummy[SBSIZE];
59 } sb;
60 #define sblock sb.ie_fs
61 
62 int
63 e2fs_df(int rfd, char *file, struct statfs *sfsp)
64 {
65 	char *mntpt;
66 	u_int32_t overhead, overhead_per_group;
67 	int32_t	ncg, ngdb, ipb, itpg;
68 
69 	if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
70 		return (-1);
71 	}
72 	if ((sblock.e2fs_magic != E2FS_MAGIC) ||
73 	    (sblock.e2fs_rev != E2FS_REV0 && sblock.e2fs_rev != E2FS_REV1)) {
74 		return (-1);
75 	}
76 	sfsp->f_flags = 0;	/* The fs is not mapped, so no flags */
77 	sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize;
78 	sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize;
79 
80 	ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode);
81 	itpg = sblock.e2fs_ipg/ipb;
82 
83 	ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock,
84 		sblock.e2fs_bpg);
85 	ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd));
86 	overhead_per_group = 1 /* super block */ +
87 					ngdb +
88 					1 /* block bitmap */ +
89 					1 /* inode bitmap */ +
90 					itpg;
91 	overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group;
92 
93 	sfsp->f_blocks = sblock.e2fs_bcount - overhead;
94 	sfsp->f_bfree = sblock.e2fs_fbcount;
95 	sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount;
96 	sfsp->f_files = sblock.e2fs_icount;
97 	sfsp->f_ffree = sblock.e2fs_ficount;
98 	sfsp->f_fsid.val[0] = 0;
99 	sfsp->f_fsid.val[1] = 0;
100 	if ((mntpt = getmntpt(file)) == 0)
101 		mntpt = "";
102 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
103 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
104 	strlcpy(sfsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN);
105 	return (0);
106 }
107