1 /* $NetBSD: compat_statfs.c,v 1.1 2005/09/13 01:44:09 christos 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.1 2005/09/13 01:44:09 christos 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 <string.h> 52 #include <stdlib.h> 53 54 __warn_references(statfs, 55 "warning: reference to obsolete statfs(); use statvfs()") 56 57 __warn_references(fstatfs, 58 "warning: reference to obsolete fstatfs(); use fstatvfs()") 59 60 __warn_references(fhstatfs, 61 "warning: reference to obsolete fhstatfs(); use fhstatvfs()") 62 63 __warn_references(getfsstat, 64 "warning: reference to obsolete getfsstat(); use getvfsstat()") 65 66 /* 67 * Convert from a new statvfs to an old statfs structure. 68 */ 69 70 static void vfs2fs(struct statfs12 *, const struct statvfs *); 71 72 #define MOUNTNO_NONE 0 73 #define MOUNTNO_UFS 1 /* UNIX "Fast" Filesystem */ 74 #define MOUNTNO_NFS 2 /* Network Filesystem */ 75 #define MOUNTNO_MFS 3 /* Memory Filesystem */ 76 #define MOUNTNO_MSDOS 4 /* MSDOS Filesystem */ 77 #define MOUNTNO_CD9660 5 /* iso9660 cdrom */ 78 #define MOUNTNO_FDESC 6 /* /dev/fd filesystem */ 79 #define MOUNTNO_KERNFS 7 /* kernel variable filesystem */ 80 #define MOUNTNO_DEVFS 8 /* device node filesystem */ 81 #define MOUNTNO_AFS 9 /* AFS 3.x */ 82 static const struct { 83 const char *name; 84 const int value; 85 } nv[] = { 86 { MOUNT_UFS, MOUNTNO_UFS }, 87 { MOUNT_NFS, MOUNTNO_NFS }, 88 { MOUNT_MFS, MOUNTNO_MFS }, 89 { MOUNT_MSDOS, MOUNTNO_MSDOS }, 90 { MOUNT_CD9660, MOUNTNO_CD9660 }, 91 { MOUNT_FDESC, MOUNTNO_FDESC }, 92 { MOUNT_KERNFS, MOUNTNO_KERNFS }, 93 { MOUNT_AFS, MOUNTNO_AFS }, 94 }; 95 96 static void 97 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs) 98 { 99 int i = 0; 100 bfs->f_type = 0; 101 bfs->f_oflags = (short)fs->f_flag; 102 103 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) { 104 if (strcmp(nv[i].name, fs->f_fstypename) == 0) { 105 bfs->f_type = nv[i].value; 106 break; 107 } 108 } 109 #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a)) 110 bfs->f_bsize = CLAMP(fs->f_frsize); 111 bfs->f_iosize = CLAMP(fs->f_iosize); 112 bfs->f_blocks = CLAMP(fs->f_blocks); 113 bfs->f_bfree = CLAMP(fs->f_bfree); 114 if (fs->f_bfree > fs->f_bresvd) 115 bfs->f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd); 116 else 117 bfs->f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree); 118 bfs->f_files = CLAMP(fs->f_files); 119 bfs->f_ffree = CLAMP(fs->f_ffree); 120 bfs->f_fsid = fs->f_fsidx; 121 bfs->f_owner = fs->f_owner; 122 bfs->f_flags = (long)fs->f_flag; 123 bfs->f_syncwrites = CLAMP(fs->f_syncwrites); 124 bfs->f_asyncwrites = CLAMP(fs->f_asyncwrites); 125 (void)strncpy(bfs->f_fstypename, fs->f_fstypename, 126 sizeof(bfs->f_fstypename)); 127 (void)strncpy(bfs->f_mntonname, fs->f_mntonname, 128 sizeof(bfs->f_mntonname)); 129 (void)strncpy(bfs->f_mntfromname, fs->f_mntfromname, 130 sizeof(bfs->f_mntfromname)); 131 } 132 133 int 134 statfs(const char *file, struct statfs12 *ost) 135 { 136 struct statvfs nst; 137 int ret; 138 139 if ((ret = statvfs(file, &nst)) == -1) 140 return ret; 141 vfs2fs(ost, &nst); 142 return ret; 143 } 144 145 int 146 fstatfs(int f, struct statfs12 *ost) 147 { 148 struct statvfs nst; 149 int ret; 150 151 if ((ret = fstatvfs(f, &nst)) == -1) 152 return ret; 153 vfs2fs(ost, &nst); 154 return ret; 155 } 156 157 int 158 fhstatfs(const fhandle_t *fh, struct statfs12 *ost) 159 { 160 struct statvfs nst; 161 int ret; 162 163 if ((ret = fhstatvfs(fh, &nst)) == -1) 164 return ret; 165 vfs2fs(ost, &nst); 166 return ret; 167 } 168 169 int 170 getfsstat(struct statfs12 *ost, long size, int flags) 171 { 172 struct statvfs *nst; 173 int ret, i; 174 size_t bsize = (size_t)(size / sizeof(*ost)) * sizeof(*nst); 175 176 if (ost != NULL) { 177 if ((nst = malloc(bsize)) == NULL) 178 return -1; 179 } else 180 nst = NULL; 181 182 if ((ret = getvfsstat(nst, bsize, flags)) == -1) 183 goto done; 184 if (nst) 185 for (i = 0; i < ret; i++) 186 vfs2fs(&ost[i], &nst[i]); 187 done: 188 if (nst) 189 free(nst); 190 return ret; 191 } 192