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