xref: /openbsd/bin/df/ext2fs_df.c (revision 8ede7795)
1*8ede7795Sdenny /*
2*8ede7795Sdenny  * This file is substantially derived from src/sys/ufs/ext2fs/ext2fs_vfsops.c:e2fs_statfs().
3*8ede7795Sdenny  * That file's copyright is applied here.
4*8ede7795Sdenny  */
5*8ede7795Sdenny 
6*8ede7795Sdenny /* Modified for EXT2FS on NetBSD by Manuel Bouyer, April 1997 */
7*8ede7795Sdenny 
8*8ede7795Sdenny /*
9*8ede7795Sdenny  * Copyright (c) 1989, 1991, 1993, 1994
10*8ede7795Sdenny  *      The Regents of the University of California.  All rights reserved.
11*8ede7795Sdenny  *
12*8ede7795Sdenny  * Redistribution and use in source and binary forms, with or without
13*8ede7795Sdenny  * modification, are permitted provided that the following conditions
14*8ede7795Sdenny  * are met:
15*8ede7795Sdenny  * 1. Redistributions of source code must retain the above copyright
16*8ede7795Sdenny  *      notice, this list of conditions and the following disclaimer.
17*8ede7795Sdenny  * 2. Redistributions in binary form must reproduce the above copyright
18*8ede7795Sdenny  *      notice, this list of conditions and the following disclaimer in the
19*8ede7795Sdenny  *      documentation and/or other materials provided with the distribution.
20*8ede7795Sdenny  * 3. All advertising materials mentioning features or use of this software
21*8ede7795Sdenny  *      must display the following acknowledgement:
22*8ede7795Sdenny  *      This product includes software developed by the University of
23*8ede7795Sdenny  *      California, Berkeley and its contributors.
24*8ede7795Sdenny  * 4. Neither the name of the University nor the names of its contributors
25*8ede7795Sdenny  *      may be used to endorse or promote products derived from this software
26*8ede7795Sdenny  *      without specific prior written permission.
27*8ede7795Sdenny  *
28*8ede7795Sdenny  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29*8ede7795Sdenny  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30*8ede7795Sdenny  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31*8ede7795Sdenny  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32*8ede7795Sdenny  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*8ede7795Sdenny  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*8ede7795Sdenny  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*8ede7795Sdenny  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36*8ede7795Sdenny  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37*8ede7795Sdenny  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38*8ede7795Sdenny  * SUCH DAMAGE.
39*8ede7795Sdenny  *
40*8ede7795Sdenny  *      @(#)ffs_vfsops.c        8.14 (Berkeley) 11/28/94
41*8ede7795Sdenny  */
42*8ede7795Sdenny 
43*8ede7795Sdenny #include <unistd.h>
44*8ede7795Sdenny #include <fcntl.h>
45*8ede7795Sdenny #include <sys/param.h>
46*8ede7795Sdenny #include <sys/mount.h>
47*8ede7795Sdenny #include <fstab.h>
48*8ede7795Sdenny #include <ufs/ext2fs/ext2fs.h>
49*8ede7795Sdenny #include <ufs/ext2fs/ext2fs_dinode.h>
50*8ede7795Sdenny 
51*8ede7795Sdenny int		e2fs_df __P((int, char *, struct statfs *));
52*8ede7795Sdenny 
53*8ede7795Sdenny extern int	bread __P((int, off_t, void *, int));
54*8ede7795Sdenny extern char	*getmntpt __P((char *));
55*8ede7795Sdenny 
56*8ede7795Sdenny union {
57*8ede7795Sdenny 	struct ext2fs ie_fs;
58*8ede7795Sdenny 	char dummy[SBSIZE];
59*8ede7795Sdenny } sb;
60*8ede7795Sdenny #define sblock sb.ie_fs
61*8ede7795Sdenny 
62*8ede7795Sdenny int
63*8ede7795Sdenny e2fs_df(rfd, file, sfsp)
64*8ede7795Sdenny 	int rfd;
65*8ede7795Sdenny 	char *file;
66*8ede7795Sdenny 	struct statfs *sfsp;
67*8ede7795Sdenny {
68*8ede7795Sdenny 	char *mntpt;
69*8ede7795Sdenny 	u_int32_t overhead, overhead_per_group;
70*8ede7795Sdenny 	int32_t	ncg, ngdb, ipb, itpg;
71*8ede7795Sdenny 
72*8ede7795Sdenny 	if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
73*8ede7795Sdenny 		return (-1);
74*8ede7795Sdenny 	}
75*8ede7795Sdenny 	if ((sblock.e2fs_magic != E2FS_MAGIC) ||
76*8ede7795Sdenny 			(sblock.e2fs_rev != E2FS_REV)) {
77*8ede7795Sdenny 		return (-1);
78*8ede7795Sdenny 	}
79*8ede7795Sdenny 	sfsp->f_type = 0;	/* Unused field, set to 0 */
80*8ede7795Sdenny 	sfsp->f_flags = 0;	/* The fs is not mapped, so no flags */
81*8ede7795Sdenny 	sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize;
82*8ede7795Sdenny 	sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize;
83*8ede7795Sdenny 
84*8ede7795Sdenny 	ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode);
85*8ede7795Sdenny 	itpg = sblock.e2fs_ipg/ipb;
86*8ede7795Sdenny 
87*8ede7795Sdenny 	ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock,
88*8ede7795Sdenny 		sblock.e2fs_bpg);
89*8ede7795Sdenny 	ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd));
90*8ede7795Sdenny         overhead_per_group = 1 /* super block */ +
91*8ede7795Sdenny 					ngdb +
92*8ede7795Sdenny 					1 /* block bitmap */ +
93*8ede7795Sdenny 					1 /* inode bitmap */ +
94*8ede7795Sdenny 					itpg;
95*8ede7795Sdenny 	overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group;
96*8ede7795Sdenny 
97*8ede7795Sdenny 	sfsp->f_blocks = sblock.e2fs_bcount - overhead;
98*8ede7795Sdenny 	sfsp->f_bfree = sblock.e2fs_fbcount;
99*8ede7795Sdenny 	sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount;
100*8ede7795Sdenny 	sfsp->f_files = sblock.e2fs_icount;
101*8ede7795Sdenny 	sfsp->f_ffree = sblock.e2fs_ficount;
102*8ede7795Sdenny 	sfsp->f_fsid.val[0] = 0;
103*8ede7795Sdenny 	sfsp->f_fsid.val[1] = 0;
104*8ede7795Sdenny 	if ((mntpt = getmntpt(file)) == 0)
105*8ede7795Sdenny 		mntpt = "";
106*8ede7795Sdenny 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
107*8ede7795Sdenny 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
108*8ede7795Sdenny 	strncpy(sfsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN-1);
109*8ede7795Sdenny 	sfsp->f_fstypename[MFSNAMELEN-1] = '\0';
110*8ede7795Sdenny 	return (0);
111*8ede7795Sdenny }
112