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