Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_20.c revision 1.30
      1 /*	$NetBSD: vfs_syscalls_20.c,v 1.30 2008/05/06 18:43:44 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.30 2008/05/06 18:43:44 ad Exp $");
     41 
     42 #include "opt_compat_netbsd.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/namei.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/file.h>
     50 #include <sys/stat.h>
     51 #include <sys/vnode.h>
     52 #include <sys/mount.h>
     53 #include <sys/proc.h>
     54 #include <sys/uio.h>
     55 #include <sys/malloc.h>
     56 #include <sys/dirent.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/syscallargs.h>
     59 #include <sys/kauth.h>
     60 
     61 #include <compat/sys/mount.h>
     62 
     63 #ifdef COMPAT_09
     64 #define MOUNTNO_NONE	0
     65 #define MOUNTNO_UFS	1		/* UNIX "Fast" Filesystem */
     66 #define MOUNTNO_NFS	2		/* Network Filesystem */
     67 #define MOUNTNO_MFS	3		/* Memory Filesystem */
     68 #define MOUNTNO_MSDOS	4		/* MSDOS Filesystem */
     69 #define MOUNTNO_CD9660	5		/* iso9660 cdrom */
     70 #define MOUNTNO_FDESC	6		/* /dev/fd filesystem */
     71 #define MOUNTNO_KERNFS	7		/* kernel variable filesystem */
     72 #define MOUNTNO_DEVFS	8		/* device node filesystem */
     73 #define MOUNTNO_AFS	9		/* AFS 3.x */
     74 static const struct {
     75 	const char *name;
     76 	const int value;
     77 } nv[] = {
     78 	{ MOUNT_UFS, MOUNTNO_UFS },
     79 	{ MOUNT_NFS, MOUNTNO_NFS },
     80 	{ MOUNT_MFS, MOUNTNO_MFS },
     81 	{ MOUNT_MSDOS, MOUNTNO_MSDOS },
     82 	{ MOUNT_CD9660, MOUNTNO_CD9660 },
     83 	{ MOUNT_FDESC, MOUNTNO_FDESC },
     84 	{ MOUNT_KERNFS, MOUNTNO_KERNFS },
     85 	{ MOUNT_AFS, MOUNTNO_AFS },
     86 };
     87 #endif
     88 
     89 static int
     90 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
     91 {
     92 	struct statfs12 ofs;
     93 #ifdef COMPAT_09
     94 	int i = 0;
     95 	ofs.f_type = 0;
     96 	ofs.f_oflags = (short)fs->f_flag;
     97 
     98 	for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
     99 		if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
    100 			ofs.f_type = nv[i].value;
    101 			break;
    102 		}
    103 	}
    104 #else
    105 	ofs.f_type = 0;
    106 #endif
    107 #define CLAMP(a)	(long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
    108 	ofs.f_bsize = CLAMP(fs->f_frsize);
    109 	ofs.f_iosize = CLAMP(fs->f_iosize);
    110 	ofs.f_blocks = CLAMP(fs->f_blocks);
    111 	ofs.f_bfree = CLAMP(fs->f_bfree);
    112 	if (fs->f_bfree > fs->f_bresvd)
    113 		ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
    114 	else
    115 		ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
    116 	ofs.f_files = CLAMP(fs->f_files);
    117 	ofs.f_ffree = CLAMP(fs->f_ffree);
    118 	ofs.f_fsid = fs->f_fsidx;
    119 	ofs.f_owner = fs->f_owner;
    120 	ofs.f_flags = (long)fs->f_flag;
    121 	ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
    122 	ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
    123 	(void)strncpy(ofs.f_fstypename, fs->f_fstypename,
    124 	    sizeof(ofs.f_fstypename));
    125 	(void)strncpy(ofs.f_mntonname, fs->f_mntonname,
    126 	    sizeof(ofs.f_mntonname));
    127 	(void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
    128 	    sizeof(ofs.f_mntfromname));
    129 
    130 	return copyout(&ofs, bfs, sizeof(ofs));
    131 }
    132 
    133 /*
    134  * Get filesystem statistics.
    135  */
    136 /* ARGSUSED */
    137 int
    138 compat_20_sys_statfs(struct lwp *l, const struct compat_20_sys_statfs_args *uap, register_t *retval)
    139 {
    140 	/* {
    141 		syscallarg(const char *) path;
    142 		syscallarg(struct statfs12 *) buf;
    143 	} */
    144 	struct mount *mp;
    145 	struct statvfs *sbuf;
    146 	int error = 0;
    147 	struct nameidata nd;
    148 
    149 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
    150 	    SCARG(uap, path));
    151 	if ((error = namei(&nd)) != 0)
    152 		return error;
    153 
    154 	mp = nd.ni_vp->v_mount;
    155 
    156 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    157 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
    158 		goto done;
    159 
    160 	error = vfs2fs(SCARG(uap, buf), sbuf);
    161 done:
    162 	vrele(nd.ni_vp);
    163 	free(sbuf, M_TEMP);
    164 	return error;
    165 }
    166 
    167 /*
    168  * Get filesystem statistics.
    169  */
    170 /* ARGSUSED */
    171 int
    172 compat_20_sys_fstatfs(struct lwp *l, const struct compat_20_sys_fstatfs_args *uap, register_t *retval)
    173 {
    174 	/* {
    175 		syscallarg(int) fd;
    176 		syscallarg(struct statfs12 *) buf;
    177 	} */
    178 	struct file *fp;
    179 	struct mount *mp;
    180 	struct statvfs *sbuf;
    181 	int error;
    182 
    183 	/* getvnode() will use the descriptor for us */
    184 	if ((error = getvnode(SCARG(uap, fd), &fp)) != 0)
    185 		return (error);
    186 	mp = ((struct vnode *)fp->f_data)->v_mount;
    187 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    188 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
    189 		goto out;
    190 	error = vfs2fs(SCARG(uap, buf), sbuf);
    191  out:
    192 	fd_putfile(SCARG(uap, fd));
    193 	free(sbuf, M_TEMP);
    194 	return error;
    195 }
    196 
    197 
    198 /*
    199  * Get statistics on all filesystems.
    200  */
    201 int
    202 compat_20_sys_getfsstat(struct lwp *l, const struct compat_20_sys_getfsstat_args *uap, register_t *retval)
    203 {
    204 	/* {
    205 		syscallarg(struct statfs12 *) buf;
    206 		syscallarg(long) bufsize;
    207 		syscallarg(int) flags;
    208 	} */
    209 	int root = 0;
    210 	struct mount *mp, *nmp;
    211 	struct statvfs *sbuf;
    212 	struct statfs12 *sfsp;
    213 	size_t count, maxcount;
    214 	int error = 0;
    215 
    216 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    217 	maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
    218 	sfsp = SCARG(uap, buf);
    219 	mutex_enter(&mountlist_lock);
    220 	count = 0;
    221 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    222 	     mp = nmp) {
    223 		if (vfs_busy(mp, &nmp)) {
    224 			continue;
    225 		}
    226 		if (sfsp && count < maxcount) {
    227 			error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
    228 			if (error) {
    229 				vfs_unbusy(mp, false, &nmp);
    230 				continue;
    231 			}
    232 			error = vfs2fs(sfsp, sbuf);
    233 			if (error) {
    234 				vfs_unbusy(mp, false, NULL);
    235 				goto out;
    236 			}
    237 			sfsp++;
    238 			root |= strcmp(sbuf->f_mntonname, "/") == 0;
    239 		}
    240 		count++;
    241 		vfs_unbusy(mp, false, &nmp);
    242 	}
    243 	mutex_exit(&mountlist_lock);
    244 	if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
    245 		/*
    246 		 * fake a root entry
    247 		 */
    248 		if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount,
    249 				       sbuf, l, SCARG(uap, flags), 1)) != 0)
    250 			goto out;
    251 		if (sfsp)
    252 			error = vfs2fs(sfsp, sbuf);
    253 		count++;
    254 	}
    255 	if (sfsp && count > maxcount)
    256 		*retval = maxcount;
    257 	else
    258 		*retval = count;
    259 out:
    260 	free(sbuf, M_TEMP);
    261 	return error;
    262 }
    263 
    264 int
    265 compat_20_sys_fhstatfs(struct lwp *l, const struct compat_20_sys_fhstatfs_args *uap, register_t *retval)
    266 {
    267 	/* {
    268 		syscallarg(const struct compat_30_fhandle *) fhp;
    269 		syscallarg(struct statfs12 *) buf;
    270 	} */
    271 	struct statvfs *sbuf;
    272 	struct compat_30_fhandle fh;
    273 	struct mount *mp;
    274 	struct vnode *vp;
    275 	int error;
    276 
    277 	/*
    278 	 * Must be super user
    279 	 */
    280 	if ((error = kauth_authorize_system(l->l_cred,
    281 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    282 		return (error);
    283 
    284 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
    285 		return (error);
    286 
    287 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    288 		return (ESTALE);
    289 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    290 		return (error);
    291 	mp = vp->v_mount;
    292 	VOP_UNLOCK(vp, 0);
    293 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    294 	if ((error = VFS_STATVFS(mp, sbuf)) != 0)
    295 		goto out;
    296 	error = vfs2fs(SCARG(uap, buf), sbuf);
    297 out:
    298 	vrele(vp);
    299 	free(sbuf, M_TEMP);
    300 	return error;
    301 }
    302