1 /* $NetBSD: compat_statfs.c,v 1.3 2006/08/04 16:30:22 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 __RCSID("$NetBSD: compat_statfs.c,v 1.3 2006/08/04 16:30:22 yamt Exp $"); 42 #endif /* LIBC_SCCS and not lint */ 43 44 #define __LIBC12_SOURCE__ 45 46 #include "namespace.h" 47 #include <sys/types.h> 48 #include <sys/param.h> 49 #include <sys/mount.h> 50 #include <compat/sys/mount.h> 51 #include <compat/include/fstypes.h> 52 #include <string.h> 53 #include <stdlib.h> 54 55 __warn_references(statfs, 56 "warning: reference to obsolete statfs(); use statvfs()") 57 58 __warn_references(fstatfs, 59 "warning: reference to obsolete fstatfs(); use fstatvfs()") 60 61 __warn_references(fhstatfs, 62 "warning: reference to obsolete fhstatfs(); use fhstatvfs()") 63 64 __warn_references(getfsstat, 65 "warning: reference to obsolete getfsstat(); use getvfsstat()") 66 67 /* 68 * Convert from a new statvfs to an old statfs structure. 69 */ 70 71 static void vfs2fs(struct statfs12 *, const struct statvfs *); 72 73 #define MOUNTNO_NONE 0 74 #define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */ 75 #define MOUNTNO_NFS 2 /* Network Filesystem */ 76 #define MOUNTNO_MFS 3 /* Memory Filesystem */ 77 #define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */ 78 #define MOUNTNO_CD9660 5 /* iso9660 cdrom */ 79 #define MOUNTNO_FDESC 6 /* /dev/fd filesystem */ 80 #define MOUNTNO_KERNFS 7 /* kernel variable filesystem */ 81 #define MOUNTNO_DEVFS 8 /* device node filesystem */ 82 #define MOUNTNO_AFS 9 /* AFS 3.x */ 83 static const struct { 84 const char *name; 85 const int value; 86 } nv[] = { 87 { MOUNT_UFS, MOUNTNO_UFS }, 88 { MOUNT_NFS, MOUNTNO_NFS }, 89 { MOUNT_MFS, MOUNTNO_MFS }, 90 { MOUNT_MSDOS, MOUNTNO_MSDOS }, 91 { MOUNT_CD9660, MOUNTNO_CD9660 }, 92 { MOUNT_FDESC, MOUNTNO_FDESC }, 93 { MOUNT_KERNFS, MOUNTNO_KERNFS }, 94 { MOUNT_AFS, MOUNTNO_AFS }, 95 }; 96 97 static void 98 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs) 99 { 100 int i = 0; 101 bfs->f_type = 0; 102 bfs->f_oflags = (short)fs->f_flag; 103 104 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) { 105 if (strcmp(nv[i].name, fs->f_fstypename) == 0) { 106 bfs->f_type = nv[i].value; 107 break; 108 } 109 } 110 #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a)) 111 bfs->f_bsize = CLAMP(fs->f_frsize); 112 bfs->f_iosize = CLAMP(fs->f_iosize); 113 bfs->f_blocks = CLAMP(fs->f_blocks); 114 bfs->f_bfree = CLAMP(fs->f_bfree); 115 if (fs->f_bfree > fs->f_bresvd) 116 bfs->f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd); 117 else 118 bfs->f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree); 119 bfs->f_files = CLAMP(fs->f_files); 120 bfs->f_ffree = CLAMP(fs->f_ffree); 121 bfs->f_fsid = fs->f_fsidx; 122 bfs->f_owner = fs->f_owner; 123 bfs->f_flags = (long)fs->f_flag; 124 bfs->f_syncwrites = CLAMP(fs->f_syncwrites); 125 bfs->f_asyncwrites = CLAMP(fs->f_asyncwrites); 126 (void)strncpy(bfs->f_fstypename, fs->f_fstypename, 127 sizeof(bfs->f_fstypename)); 128 (void)strncpy(bfs->f_mntonname, fs->f_mntonname, 129 sizeof(bfs->f_mntonname)); 130 (void)strncpy(bfs->f_mntfromname, fs->f_mntfromname, 131 sizeof(bfs->f_mntfromname)); 132 } 133 134 int 135 statfs(const char *file, struct statfs12 *ost) 136 { 137 struct statvfs nst; 138 int ret; 139 140 if ((ret = statvfs(file, &nst)) == -1) 141 return ret; 142 vfs2fs(ost, &nst); 143 return ret; 144 } 145 146 int 147 fstatfs(int f, struct statfs12 *ost) 148 { 149 struct statvfs nst; 150 int ret; 151 152 if ((ret = fstatvfs(f, &nst)) == -1) 153 return ret; 154 vfs2fs(ost, &nst); 155 return ret; 156 } 157 158 int __fhstatvfs140(const void *fhp, size_t fh_size, struct statvfs *buf, 159 int flags); 160 161 int 162 fhstatfs(const struct compat_30_fhandle *fh, struct statfs12 *ost) 163 { 164 struct statvfs nst; 165 int ret; 166 167 if ((ret = __fhstatvfs140(fh, FHANDLE30_SIZE, &nst, ST_WAIT)) == -1) 168 return ret; 169 vfs2fs(ost, &nst); 170 return ret; 171 } 172 173 int 174 getfsstat(struct statfs12 *ost, long size, int flags) 175 { 176 struct statvfs *nst; 177 int ret, i; 178 size_t bsize = (size_t)(size / sizeof(*ost)) * sizeof(*nst); 179 180 if (ost != NULL) { 181 if ((nst = malloc(bsize)) == NULL) 182 return -1; 183 } else 184 nst = NULL; 185 186 if ((ret = getvfsstat(nst, bsize, flags)) == -1) 187 goto done; 188 if (nst) 189 for (i = 0; i < ret; i++) 190 vfs2fs(&ost[i], &nst[i]); 191 done: 192 if (nst) 193 free(nst); 194 return ret; 195 } 196