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