1*29295d1cSmillert /* $OpenBSD: ext2fs_df.c,v 1.10 2003/06/02 23:32:07 millert 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. 22*29295d1cSmillert * 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 418ede7795Sdenny #include <sys/param.h> 428ede7795Sdenny #include <sys/mount.h> 438ede7795Sdenny #include <ufs/ext2fs/ext2fs.h> 448ede7795Sdenny #include <ufs/ext2fs/ext2fs_dinode.h> 45fe6baaf7Sderaadt #include <unistd.h> 46fe6baaf7Sderaadt #include <stdlib.h> 47fe6baaf7Sderaadt #include <string.h> 48fe6baaf7Sderaadt #include <fcntl.h> 49fe6baaf7Sderaadt #include <fstab.h> 508ede7795Sdenny 51c72b5b24Smillert int e2fs_df(int, char *, struct statfs *); 528ede7795Sdenny 53c72b5b24Smillert extern int bread(int, off_t, void *, int); 54c72b5b24Smillert extern char *getmntpt(char *); 558ede7795Sdenny 568ede7795Sdenny union { 578ede7795Sdenny struct ext2fs ie_fs; 588ede7795Sdenny char dummy[SBSIZE]; 598ede7795Sdenny } sb; 608ede7795Sdenny #define sblock sb.ie_fs 618ede7795Sdenny 628ede7795Sdenny int 63ab83b6d6Sderaadt e2fs_df(int rfd, char *file, struct statfs *sfsp) 648ede7795Sdenny { 658ede7795Sdenny char *mntpt; 668ede7795Sdenny u_int32_t overhead, overhead_per_group; 678ede7795Sdenny int32_t ncg, ngdb, ipb, itpg; 688ede7795Sdenny 698ede7795Sdenny if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) { 708ede7795Sdenny return (-1); 718ede7795Sdenny } 728ede7795Sdenny if ((sblock.e2fs_magic != E2FS_MAGIC) || 7365348f21Sjasoni (sblock.e2fs_rev != E2FS_REV0 && sblock.e2fs_rev != E2FS_REV1)) { 748ede7795Sdenny return (-1); 758ede7795Sdenny } 768ede7795Sdenny sfsp->f_flags = 0; /* The fs is not mapped, so no flags */ 778ede7795Sdenny sfsp->f_bsize = 1024 << sblock.e2fs_log_bsize; 788ede7795Sdenny sfsp->f_iosize = 1024 << sblock.e2fs_log_bsize; 798ede7795Sdenny 808ede7795Sdenny ipb = sfsp->f_bsize / sizeof(struct ext2fs_dinode); 818ede7795Sdenny itpg = sblock.e2fs_ipg/ipb; 828ede7795Sdenny 838ede7795Sdenny ncg = howmany(sblock.e2fs_bcount - sblock.e2fs_first_dblock, 848ede7795Sdenny sblock.e2fs_bpg); 858ede7795Sdenny ngdb = howmany(ncg, sfsp->f_bsize / sizeof(struct ext2_gd)); 868ede7795Sdenny overhead_per_group = 1 /* super block */ + 878ede7795Sdenny ngdb + 888ede7795Sdenny 1 /* block bitmap */ + 898ede7795Sdenny 1 /* inode bitmap */ + 908ede7795Sdenny itpg; 918ede7795Sdenny overhead = sblock.e2fs_first_dblock + ncg * overhead_per_group; 928ede7795Sdenny 938ede7795Sdenny sfsp->f_blocks = sblock.e2fs_bcount - overhead; 948ede7795Sdenny sfsp->f_bfree = sblock.e2fs_fbcount; 958ede7795Sdenny sfsp->f_bavail = sfsp->f_bfree - sblock.e2fs_rbcount; 968ede7795Sdenny sfsp->f_files = sblock.e2fs_icount; 978ede7795Sdenny sfsp->f_ffree = sblock.e2fs_ficount; 988ede7795Sdenny sfsp->f_fsid.val[0] = 0; 998ede7795Sdenny sfsp->f_fsid.val[1] = 0; 1008ede7795Sdenny if ((mntpt = getmntpt(file)) == 0) 1018ede7795Sdenny mntpt = ""; 1028ede7795Sdenny memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN); 1038ede7795Sdenny memmove(&sfsp->f_mntfromname[0], file, MNAMELEN); 1042dc3481aSmickey strlcpy(sfsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN); 1058ede7795Sdenny return (0); 1068ede7795Sdenny } 107