xref: /openbsd/bin/df/ext2fs_df.c (revision 65348f21)
18ede7795Sdenny /*
28ede7795Sdenny  * This file is substantially derived from src/sys/ufs/ext2fs/ext2fs_vfsops.c:e2fs_statfs().
38ede7795Sdenny  * That file's copyright is applied here.
48ede7795Sdenny  */
58ede7795Sdenny 
68ede7795Sdenny /* Modified for EXT2FS on NetBSD by Manuel Bouyer, April 1997 */
78ede7795Sdenny 
88ede7795Sdenny /*
98ede7795Sdenny  * Copyright (c) 1989, 1991, 1993, 1994
108ede7795Sdenny  *      The Regents of the University of California.  All rights reserved.
118ede7795Sdenny  *
128ede7795Sdenny  * Redistribution and use in source and binary forms, with or without
138ede7795Sdenny  * modification, are permitted provided that the following conditions
148ede7795Sdenny  * are met:
158ede7795Sdenny  * 1. Redistributions of source code must retain the above copyright
168ede7795Sdenny  *      notice, this list of conditions and the following disclaimer.
178ede7795Sdenny  * 2. Redistributions in binary form must reproduce the above copyright
188ede7795Sdenny  *      notice, this list of conditions and the following disclaimer in the
198ede7795Sdenny  *      documentation and/or other materials provided with the distribution.
208ede7795Sdenny  * 3. All advertising materials mentioning features or use of this software
218ede7795Sdenny  *      must display the following acknowledgement:
228ede7795Sdenny  *      This product includes software developed by the University of
238ede7795Sdenny  *      California, Berkeley and its contributors.
248ede7795Sdenny  * 4. Neither the name of the University nor the names of its contributors
258ede7795Sdenny  *      may be used to endorse or promote products derived from this software
268ede7795Sdenny  *      without specific prior written permission.
278ede7795Sdenny  *
288ede7795Sdenny  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
298ede7795Sdenny  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
308ede7795Sdenny  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
318ede7795Sdenny  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
328ede7795Sdenny  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
338ede7795Sdenny  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
348ede7795Sdenny  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358ede7795Sdenny  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
368ede7795Sdenny  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
378ede7795Sdenny  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
388ede7795Sdenny  * SUCH DAMAGE.
398ede7795Sdenny  *
408ede7795Sdenny  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
418ede7795Sdenny  */
428ede7795Sdenny 
438ede7795Sdenny #include <sys/param.h>
448ede7795Sdenny #include <sys/mount.h>
458ede7795Sdenny #include <ufs/ext2fs/ext2fs.h>
468ede7795Sdenny #include <ufs/ext2fs/ext2fs_dinode.h>
47fe6baaf7Sderaadt #include <unistd.h>
48fe6baaf7Sderaadt #include <stdlib.h>
49fe6baaf7Sderaadt #include <string.h>
50fe6baaf7Sderaadt #include <fcntl.h>
51fe6baaf7Sderaadt #include <fstab.h>
528ede7795Sdenny 
538ede7795Sdenny int		e2fs_df __P((int, char *, struct statfs *));
548ede7795Sdenny 
558ede7795Sdenny extern int	bread __P((int, off_t, void *, int));
568ede7795Sdenny extern char	*getmntpt __P((char *));
578ede7795Sdenny 
588ede7795Sdenny union {
598ede7795Sdenny 	struct ext2fs ie_fs;
608ede7795Sdenny 	char dummy[SBSIZE];
618ede7795Sdenny } sb;
628ede7795Sdenny #define sblock sb.ie_fs
638ede7795Sdenny 
648ede7795Sdenny int
658ede7795Sdenny e2fs_df(rfd, file, sfsp)
668ede7795Sdenny 	int rfd;
678ede7795Sdenny 	char *file;
688ede7795Sdenny 	struct statfs *sfsp;
698ede7795Sdenny {
708ede7795Sdenny 	char *mntpt;
718ede7795Sdenny 	u_int32_t overhead, overhead_per_group;
728ede7795Sdenny 	int32_t	ncg, ngdb, ipb, itpg;
738ede7795Sdenny 
748ede7795Sdenny 	if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
758ede7795Sdenny 		return (-1);
768ede7795Sdenny 	}
778ede7795Sdenny 	if ((sblock.e2fs_magic != E2FS_MAGIC) ||
78*65348f21Sjasoni 	    (sblock.e2fs_rev != E2FS_REV0 && sblock.e2fs_rev != E2FS_REV1)) {
798ede7795Sdenny 		return (-1);
808ede7795Sdenny 	}
818ede7795Sdenny 	sfsp->f_flags = 0;	/* The fs is not mapped, so no flags */
828ede7795Sdenny 	sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize;
838ede7795Sdenny 	sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize;
848ede7795Sdenny 
858ede7795Sdenny 	ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode);
868ede7795Sdenny 	itpg = sblock.e2fs_ipg/ipb;
878ede7795Sdenny 
888ede7795Sdenny 	ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock,
898ede7795Sdenny 		sblock.e2fs_bpg);
908ede7795Sdenny 	ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd));
918ede7795Sdenny 	overhead_per_group = 1 /* super block */ +
928ede7795Sdenny 					ngdb +
938ede7795Sdenny 					1 /* block bitmap */ +
948ede7795Sdenny 					1 /* inode bitmap */ +
958ede7795Sdenny 					itpg;
968ede7795Sdenny 	overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group;
978ede7795Sdenny 
988ede7795Sdenny 	sfsp->f_blocks = sblock.e2fs_bcount - overhead;
998ede7795Sdenny 	sfsp->f_bfree = sblock.e2fs_fbcount;
1008ede7795Sdenny 	sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount;
1018ede7795Sdenny 	sfsp->f_files = sblock.e2fs_icount;
1028ede7795Sdenny 	sfsp->f_ffree = sblock.e2fs_ficount;
1038ede7795Sdenny 	sfsp->f_fsid.val[0] = 0;
1048ede7795Sdenny 	sfsp->f_fsid.val[1] = 0;
1058ede7795Sdenny 	if ((mntpt = getmntpt(file)) == 0)
1068ede7795Sdenny 		mntpt = "";
1078ede7795Sdenny 	memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
1088ede7795Sdenny 	memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
1098ede7795Sdenny 	strncpy(sfsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN-1);
1108ede7795Sdenny 	sfsp->f_fstypename[MFSNAMELEN-1] = '\0';
1118ede7795Sdenny 	return (0);
1128ede7795Sdenny }
113