Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_20.c revision 1.21
      1 /*	$NetBSD: vfs_syscalls_20.c,v 1.21 2007/12/08 18:35:55 dsl 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.21 2007/12/08 18:35:55 dsl Exp $");
     41 
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_compat_43.h"
     44 #include "fss.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/proc.h>
     56 #include <sys/uio.h>
     57 #include <sys/malloc.h>
     58 #include <sys/dirent.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/syscallargs.h>
     61 #include <sys/kauth.h>
     62 
     63 #include <compat/sys/mount.h>
     64 
     65 #ifdef COMPAT_09
     66 #define MOUNTNO_NONE	0
     67 #define MOUNTNO_UFS	1		/* UNIX "Fast" Filesystem */
     68 #define MOUNTNO_NFS	2		/* Network Filesystem */
     69 #define MOUNTNO_MFS	3		/* Memory Filesystem */
     70 #define MOUNTNO_MSDOS	4		/* MSDOS Filesystem */
     71 #define MOUNTNO_CD9660	5		/* iso9660 cdrom */
     72 #define MOUNTNO_FDESC	6		/* /dev/fd filesystem */
     73 #define MOUNTNO_KERNFS	7		/* kernel variable filesystem */
     74 #define MOUNTNO_DEVFS	8		/* device node filesystem */
     75 #define MOUNTNO_AFS	9		/* AFS 3.x */
     76 static const struct {
     77 	const char *name;
     78 	const int value;
     79 } nv[] = {
     80 	{ MOUNT_UFS, MOUNTNO_UFS },
     81 	{ MOUNT_NFS, MOUNTNO_NFS },
     82 	{ MOUNT_MFS, MOUNTNO_MFS },
     83 	{ MOUNT_MSDOS, MOUNTNO_MSDOS },
     84 	{ MOUNT_CD9660, MOUNTNO_CD9660 },
     85 	{ MOUNT_FDESC, MOUNTNO_FDESC },
     86 	{ MOUNT_KERNFS, MOUNTNO_KERNFS },
     87 	{ MOUNT_AFS, MOUNTNO_AFS },
     88 };
     89 #endif
     90 
     91 static int
     92 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
     93 {
     94 	struct statfs12 ofs;
     95 #ifdef COMPAT_09
     96 	int i = 0;
     97 	ofs.f_type = 0;
     98 	ofs.f_oflags = (short)fs->f_flag;
     99 
    100 	for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
    101 		if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
    102 			ofs.f_type = nv[i].value;
    103 			break;
    104 		}
    105 	}
    106 #else
    107 	ofs.f_type = 0;
    108 #endif
    109 #define CLAMP(a)	(long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
    110 	ofs.f_bsize = CLAMP(fs->f_frsize);
    111 	ofs.f_iosize = CLAMP(fs->f_iosize);
    112 	ofs.f_blocks = CLAMP(fs->f_blocks);
    113 	ofs.f_bfree = CLAMP(fs->f_bfree);
    114 	if (fs->f_bfree > fs->f_bresvd)
    115 		ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
    116 	else
    117 		ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
    118 	ofs.f_files = CLAMP(fs->f_files);
    119 	ofs.f_ffree = CLAMP(fs->f_ffree);
    120 	ofs.f_fsid = fs->f_fsidx;
    121 	ofs.f_owner = fs->f_owner;
    122 	ofs.f_flags = (long)fs->f_flag;
    123 	ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
    124 	ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
    125 	(void)strncpy(ofs.f_fstypename, fs->f_fstypename,
    126 	    sizeof(ofs.f_fstypename));
    127 	(void)strncpy(ofs.f_mntonname, fs->f_mntonname,
    128 	    sizeof(ofs.f_mntonname));
    129 	(void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
    130 	    sizeof(ofs.f_mntfromname));
    131 
    132 	return copyout(&ofs, bfs, sizeof(ofs));
    133 }
    134 
    135 /*
    136  * Get filesystem statistics.
    137  */
    138 /* ARGSUSED */
    139 int
    140 compat_20_sys_statfs(struct lwp *l, void *v, register_t *retval)
    141 {
    142 	struct compat_20_sys_statfs_args /* {
    143 		syscallarg(const char *) path;
    144 		syscallarg(struct statfs12 *) buf;
    145 	} */ *uap = v;
    146 	struct mount *mp;
    147 	struct statvfs *sbuf;
    148 	int error = 0;
    149 	struct nameidata nd;
    150 
    151 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
    152 	if ((error = namei(&nd)) != 0)
    153 		return error;
    154 
    155 	mp = nd.ni_vp->v_mount;
    156 	vrele(nd.ni_vp);
    157 
    158 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    159 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
    160 		goto done;
    161 
    162 	error = vfs2fs(SCARG(uap, buf), sbuf);
    163 done:
    164 	free(sbuf, M_TEMP);
    165 	return error;
    166 }
    167 
    168 /*
    169  * Get filesystem statistics.
    170  */
    171 /* ARGSUSED */
    172 int
    173 compat_20_sys_fstatfs(struct lwp *l, void *v, register_t *retval)
    174 {
    175 	struct compat_20_sys_fstatfs_args /* {
    176 		syscallarg(int) fd;
    177 		syscallarg(struct statfs12 *) buf;
    178 	} */ *uap = v;
    179 	struct proc *p = l->l_proc;
    180 	struct file *fp;
    181 	struct mount *mp;
    182 	struct statvfs *sbuf;
    183 	int error;
    184 
    185 	/* getvnode() will use the descriptor for us */
    186 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    187 		return (error);
    188 	mp = ((struct vnode *)fp->f_data)->v_mount;
    189 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    190 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
    191 		goto out;
    192 	error = vfs2fs(SCARG(uap, buf), sbuf);
    193  out:
    194 	FILE_UNUSE(fp, l);
    195 	free(sbuf, M_TEMP);
    196 	return error;
    197 }
    198 
    199 
    200 /*
    201  * Get statistics on all filesystems.
    202  */
    203 int
    204 compat_20_sys_getfsstat(struct lwp *l, void *v, register_t *retval)
    205 {
    206 	struct compat_20_sys_getfsstat_args /* {
    207 		syscallarg(struct statfs12 *) buf;
    208 		syscallarg(long) bufsize;
    209 		syscallarg(int) flags;
    210 	} */ *uap = v;
    211 	int root = 0;
    212 	struct mount *mp, *nmp;
    213 	struct statvfs *sbuf;
    214 	struct statfs12 *sfsp;
    215 	size_t count, maxcount;
    216 	int error = 0;
    217 
    218 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    219 	maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
    220 	sfsp = SCARG(uap, buf);
    221 	mutex_enter(&mountlist_lock);
    222 	count = 0;
    223 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    224 	     mp = nmp) {
    225 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
    226 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    227 			continue;
    228 		}
    229 		if (sfsp && count < maxcount) {
    230 			error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
    231 			if (error) {
    232 				mutex_enter(&mountlist_lock);
    233 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    234 				vfs_unbusy(mp);
    235 				continue;
    236 			}
    237 			error = vfs2fs(sfsp, sbuf);
    238 			if (error) {
    239 				vfs_unbusy(mp);
    240 				goto out;
    241 			}
    242 			sfsp++;
    243 			root |= strcmp(sbuf->f_mntonname, "/") == 0;
    244 		}
    245 		count++;
    246 		mutex_enter(&mountlist_lock);
    247 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    248 		vfs_unbusy(mp);
    249 	}
    250 	mutex_exit(&mountlist_lock);
    251 	if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
    252 		/*
    253 		 * fake a root entry
    254 		 */
    255 		if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount,
    256 				       sbuf, l, SCARG(uap, flags), 1)) != 0)
    257 			goto out;
    258 		if (sfsp)
    259 			error = vfs2fs(sfsp, sbuf);
    260 		count++;
    261 	}
    262 	if (sfsp && count > maxcount)
    263 		*retval = maxcount;
    264 	else
    265 		*retval = count;
    266 out:
    267 	free(sbuf, M_TEMP);
    268 	return error;
    269 }
    270 
    271 int
    272 compat_20_sys_fhstatfs(struct lwp *l, void *v, register_t *retval)
    273 {
    274 	struct compat_20_sys_fhstatfs_args /*
    275 		syscallarg(const struct compat_30_fhandle *) fhp;
    276 		syscallarg(struct statfs12 *) buf;
    277 	} */ *uap = v;
    278 	struct statvfs *sbuf;
    279 	struct compat_30_fhandle fh;
    280 	struct mount *mp;
    281 	struct vnode *vp;
    282 	int error;
    283 
    284 	/*
    285 	 * Must be super user
    286 	 */
    287 	if ((error = kauth_authorize_system(l->l_cred,
    288 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    289 		return (error);
    290 
    291 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
    292 		return (error);
    293 
    294 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    295 		return (ESTALE);
    296 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    297 		return (error);
    298 	mp = vp->v_mount;
    299 	vput(vp);
    300 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    301 	if ((error = VFS_STATVFS(mp, sbuf)) != 0)
    302 		goto out;
    303 	error = vfs2fs(SCARG(uap, buf), sbuf);
    304 out:
    305 	free(sbuf, M_TEMP);
    306 	return error;
    307 }
    308