xref: /openbsd-src/bin/df/ext2fs_df.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: ext2fs_df.c,v 1.7 2001/05/11 18:40:46 mickey 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. All advertising materials mentioning features or use of this software
23  *      must display the following acknowledgement:
24  *      This product includes software developed by the University of
25  *      California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *      may be used to endorse or promote products derived from this software
28  *      without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
43  */
44 
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <ufs/ext2fs/ext2fs.h>
48 #include <ufs/ext2fs/ext2fs_dinode.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <fcntl.h>
53 #include <fstab.h>
54 
55 int		e2fs_df __P((int, char *, struct statfs *));
56 
57 extern int	bread __P((int, off_t, void *, int));
58 extern char	*getmntpt __P((char *));
59 
60 union {
61 	struct ext2fs ie_fs;
62 	char dummy[SBSIZE];
63 } sb;
64 #define sblock sb.ie_fs
65 
66 int
67 e2fs_df(rfd, file, sfsp)
68 	int rfd;
69 	char *file;
70 	struct statfs *sfsp;
71 {
72 	char *mntpt;
73 	u_int32_t overhead, overhead_per_group;
74 	int32_t	ncg, ngdb, ipb, itpg;
75 
76 	if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
77 		return (-1);
78 	}
79 	if ((sblock.e2fs_magic != E2FS_MAGIC) ||
80 	    (sblock.e2fs_rev != E2FS_REV0 && sblock.e2fs_rev != E2FS_REV1)) {
81 		return (-1);
82 	}
83 	sfsp->f_flags = 0;	/* The fs is not mapped, so no flags */
84 	sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize;
85 	sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize;
86 
87 	ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode);
88 	itpg = sblock.e2fs_ipg/ipb;
89 
90 	ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock,
91 		sblock.e2fs_bpg);
92 	ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd));
93 	overhead_per_group = 1 /* super block */ +
94 					ngdb +
95 					1 /* block bitmap */ +
96 					1 /* inode bitmap */ +
97 					itpg;
98 	overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group;
99 
100 	sfsp->f_blocks = sblock.e2fs_bcount - overhead;
101 	sfsp->f_bfree = sblock.e2fs_fbcount;
102 	sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount;
103 	sfsp->f_files = sblock.e2fs_icount;
104 	sfsp->f_ffree = sblock.e2fs_ficount;
105 	sfsp->f_fsid.val[0] = 0;
106 	sfsp->f_fsid.val[1] = 0;
107 	if ((mntpt = getmntpt(file)) == 0)
108 		mntpt = "";
109 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
110 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
111 	strlcpy(sfsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN);
112 	return (0);
113 }
114