1 1.46 christos /* $NetBSD: vfs_syscalls_20.c,v 1.46 2020/06/28 14:37:53 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.46 christos __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.46 2020/06/28 14:37:53 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.41 pgoyette static const struct syscall_package vfs_syscalls_20_syscalls[] = { 71 1.41 pgoyette { SYS_compat_20_fhstatfs, 0, (sy_call_t *)compat_20_sys_fhstatfs }, 72 1.41 pgoyette { SYS_compat_20_fstatfs, 0, (sy_call_t *)compat_20_sys_fstatfs }, 73 1.41 pgoyette { SYS_compat_20_getfsstat, 0, (sy_call_t *)compat_20_sys_getfsstat }, 74 1.41 pgoyette { SYS_compat_20_statfs, 0, (sy_call_t *)compat_20_sys_statfs }, 75 1.41 pgoyette { 0, 0, NULL } 76 1.41 pgoyette }; 77 1.41 pgoyette 78 1.1 christos /* 79 1.1 christos * Get filesystem statistics. 80 1.1 christos */ 81 1.1 christos /* ARGSUSED */ 82 1.1 christos int 83 1.23 dsl compat_20_sys_statfs(struct lwp *l, const struct compat_20_sys_statfs_args *uap, register_t *retval) 84 1.1 christos { 85 1.23 dsl /* { 86 1.1 christos syscallarg(const char *) path; 87 1.1 christos syscallarg(struct statfs12 *) buf; 88 1.23 dsl } */ 89 1.1 christos struct mount *mp; 90 1.2 christos struct statvfs *sbuf; 91 1.39 maxv int error; 92 1.34 dholland struct vnode *vp; 93 1.1 christos 94 1.34 dholland error = namei_simple_user(SCARG(uap, path), 95 1.34 dholland NSM_FOLLOW_TRYEMULROOT, &vp); 96 1.34 dholland if (error != 0) 97 1.1 christos return error; 98 1.2 christos 99 1.34 dholland mp = vp->v_mount; 100 1.2 christos 101 1.42 christos sbuf = STATVFSBUF_GET(); 102 1.6 christos if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0) 103 1.2 christos goto done; 104 1.2 christos 105 1.44 christos error = statvfs_to_statfs12_copy(sbuf, SCARG(uap, buf), 0); 106 1.2 christos done: 107 1.34 dholland vrele(vp); 108 1.42 christos STATVFSBUF_PUT(sbuf); 109 1.2 christos return error; 110 1.1 christos } 111 1.1 christos 112 1.1 christos /* 113 1.1 christos * Get filesystem statistics. 114 1.1 christos */ 115 1.1 christos /* ARGSUSED */ 116 1.1 christos int 117 1.23 dsl compat_20_sys_fstatfs(struct lwp *l, const struct compat_20_sys_fstatfs_args *uap, register_t *retval) 118 1.1 christos { 119 1.23 dsl /* { 120 1.1 christos syscallarg(int) fd; 121 1.1 christos syscallarg(struct statfs12 *) buf; 122 1.23 dsl } */ 123 1.1 christos struct file *fp; 124 1.1 christos struct mount *mp; 125 1.9 christos struct statvfs *sbuf; 126 1.1 christos int error; 127 1.1 christos 128 1.31 ad /* fd_getvnode() will use the descriptor for us */ 129 1.31 ad if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 130 1.1 christos return (error); 131 1.38 matt mp = fp->f_vnode->v_mount; 132 1.42 christos sbuf = STATVFSBUF_GET(); 133 1.9 christos if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0) 134 1.1 christos goto out; 135 1.44 christos error = statvfs_to_statfs12_copy(sbuf, SCARG(uap, buf), 0); 136 1.1 christos out: 137 1.26 ad fd_putfile(SCARG(uap, fd)); 138 1.42 christos STATVFSBUF_PUT(sbuf); 139 1.1 christos return error; 140 1.1 christos } 141 1.1 christos 142 1.1 christos 143 1.1 christos /* 144 1.1 christos * Get statistics on all filesystems. 145 1.1 christos */ 146 1.1 christos int 147 1.23 dsl compat_20_sys_getfsstat(struct lwp *l, const struct compat_20_sys_getfsstat_args *uap, register_t *retval) 148 1.1 christos { 149 1.23 dsl /* { 150 1.1 christos syscallarg(struct statfs12 *) buf; 151 1.1 christos syscallarg(long) bufsize; 152 1.1 christos syscallarg(int) flags; 153 1.23 dsl } */ 154 1.42 christos return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize), 155 1.44 christos SCARG(uap, flags), statvfs_to_statfs12_copy, 156 1.46 christos sizeof(struct statfs12), retval); 157 1.1 christos } 158 1.1 christos 159 1.1 christos int 160 1.23 dsl compat_20_sys_fhstatfs(struct lwp *l, const struct compat_20_sys_fhstatfs_args *uap, register_t *retval) 161 1.1 christos { 162 1.23 dsl /* { 163 1.11 martin syscallarg(const struct compat_30_fhandle *) fhp; 164 1.1 christos syscallarg(struct statfs12 *) buf; 165 1.23 dsl } */ 166 1.9 christos struct statvfs *sbuf; 167 1.11 martin struct compat_30_fhandle fh; 168 1.1 christos struct mount *mp; 169 1.1 christos struct vnode *vp; 170 1.1 christos int error; 171 1.1 christos 172 1.1 christos /* 173 1.1 christos * Must be super user 174 1.1 christos */ 175 1.14 elad if ((error = kauth_authorize_system(l->l_cred, 176 1.14 elad KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL))) 177 1.1 christos return (error); 178 1.1 christos 179 1.11 martin if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0) 180 1.1 christos return (error); 181 1.1 christos 182 1.1 christos if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 183 1.1 christos return (ESTALE); 184 1.45 ad error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, LK_EXCLUSIVE, &vp); 185 1.45 ad if (error != 0) 186 1.1 christos return (error); 187 1.1 christos mp = vp->v_mount; 188 1.35 hannken VOP_UNLOCK(vp); 189 1.42 christos sbuf = STATVFSBUF_GET(); 190 1.20 pooka if ((error = VFS_STATVFS(mp, sbuf)) != 0) 191 1.9 christos goto out; 192 1.44 christos error = statvfs_to_statfs12_copy(sbuf, SCARG(uap, buf), 0); 193 1.9 christos out: 194 1.24 ad vrele(vp); 195 1.42 christos STATVFSBUF_PUT(sbuf); 196 1.9 christos return error; 197 1.1 christos } 198 1.41 pgoyette 199 1.41 pgoyette int 200 1.41 pgoyette vfs_syscalls_20_init(void) 201 1.41 pgoyette { 202 1.41 pgoyette 203 1.41 pgoyette return syscall_establish(NULL, vfs_syscalls_20_syscalls); 204 1.41 pgoyette } 205 1.41 pgoyette 206 1.41 pgoyette int 207 1.41 pgoyette vfs_syscalls_20_fini(void) 208 1.41 pgoyette { 209 1.41 pgoyette 210 1.41 pgoyette return syscall_disestablish(NULL, vfs_syscalls_20_syscalls); 211 1.41 pgoyette } 212