xref: /openbsd/bin/df/ext2fs_df.c (revision d89ec533)
1 /*	$OpenBSD: ext2fs_df.c,v 1.16 2016/03/01 17:57:49 mmcc 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/types.h>
42 #include <sys/mount.h>
43 #include <ufs/ext2fs/ext2fs.h>
44 #include <ufs/ext2fs/ext2fs_dinode.h>
45 #include <string.h>
46 
47 int		e2fs_df(int, char *, struct statfs *);
48 
49 extern int	bread(int, off_t, void *, int);
50 extern char	*getmntpt(char *);
51 
52 static union {
53 	struct ext2fs ie_fs;
54 	char dummy[SBSIZE];
55 } sb;
56 #define sblock sb.ie_fs
57 
58 int
59 e2fs_df(int rfd, char *file, struct statfs *sfsp)
60 {
61 	char *mntpt;
62 	u_int32_t overhead, overhead_per_group;
63 	int32_t	ncg, ngdb, ipb, itpg;
64 
65 	if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
66 		return (-1);
67 	}
68 	if ((sblock.e2fs_magic != E2FS_MAGIC) ||
69 	    (sblock.e2fs_rev != E2FS_REV0 && sblock.e2fs_rev != E2FS_REV1)) {
70 		return (-1);
71 	}
72 	sfsp->f_flags = 0;	/* The fs is not mapped, so no flags */
73 	sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize;
74 	sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize;
75 
76 	if ((ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode)) == 0)
77 		return (-1);
78 	itpg = sblock.e2fs_ipg / ipb;
79 
80 	ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock,
81 		sblock.e2fs_bpg);
82 	ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd));
83 	overhead_per_group = 1 /* super block */ +
84 					ngdb +
85 					1 /* block bitmap */ +
86 					1 /* inode bitmap */ +
87 					itpg;
88 	overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group;
89 
90 	sfsp->f_blocks = sblock.e2fs_bcount - overhead;
91 	sfsp->f_bfree = sblock.e2fs_fbcount;
92 	sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount;
93 	sfsp->f_files = sblock.e2fs_icount;
94 	sfsp->f_ffree = sblock.e2fs_ficount;
95 	sfsp->f_fsid.val[0] = 0;
96 	sfsp->f_fsid.val[1] = 0;
97 	if ((mntpt = getmntpt(file)) == 0)
98 		mntpt = "";
99 	strlcpy(sfsp->f_mntonname, mntpt, sizeof(sfsp->f_mntonname));
100 	strlcpy(sfsp->f_mntfromname, file, sizeof(sfsp->f_mntfromname));
101 	strlcpy(sfsp->f_fstypename, MOUNT_EXT2FS, sizeof(sfsp->f_fstypename));
102 	return (0);
103 }
104