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