vfs_syscalls_20.c revision 1.42 1 /* $NetBSD: vfs_syscalls_20.c,v 1.42 2019/09/22 22:59:38 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.42 2019/09/22 22:59:38 christos Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_compat_netbsd.h"
44 #endif
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/dirent.h>
58 #include <sys/sysctl.h>
59 #include <sys/syscall.h>
60 #include <sys/syscallvar.h>
61 #include <sys/syscallargs.h>
62 #include <sys/kauth.h>
63 #include <sys/vfs_syscalls.h>
64
65 #include <compat/common/compat_mod.h>
66
67 #include <compat/sys/mount.h>
68 #include <compat/sys/statvfs.h>
69
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
94 static const struct syscall_package vfs_syscalls_20_syscalls[] = {
95 { SYS_compat_20_fhstatfs, 0, (sy_call_t *)compat_20_sys_fhstatfs },
96 { SYS_compat_20_fstatfs, 0, (sy_call_t *)compat_20_sys_fstatfs },
97 { SYS_compat_20_getfsstat, 0, (sy_call_t *)compat_20_sys_getfsstat },
98 { SYS_compat_20_statfs, 0, (sy_call_t *)compat_20_sys_statfs },
99 { 0, 0, NULL }
100 };
101
102 static int
103 statvfs_to_statfs12(const void *vfs, void *vbfs, size_t len)
104 {
105 struct statfs12 ofs, *bfs = vbfs;
106 const struct statvfs *fs = vfs;
107 int i;
108 ofs.f_type = 0;
109 ofs.f_oflags = (short)fs->f_flag;
110
111 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
112 if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
113 ofs.f_type = nv[i].value;
114 break;
115 }
116 }
117 #define CLAMP(a) (long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
118 ofs.f_bsize = CLAMP(fs->f_frsize);
119 ofs.f_iosize = CLAMP(fs->f_iosize);
120 ofs.f_blocks = CLAMP(fs->f_blocks);
121 ofs.f_bfree = CLAMP(fs->f_bfree);
122 if (fs->f_bfree > fs->f_bresvd)
123 ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
124 else
125 ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
126 ofs.f_files = CLAMP(fs->f_files);
127 ofs.f_ffree = CLAMP(fs->f_ffree);
128 ofs.f_fsid = fs->f_fsidx;
129 ofs.f_owner = fs->f_owner;
130 ofs.f_flags = (long)fs->f_flag;
131 ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
132 ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
133 (void)strncpy(ofs.f_fstypename, fs->f_fstypename,
134 sizeof(ofs.f_fstypename));
135 (void)strncpy(ofs.f_mntonname, fs->f_mntonname,
136 sizeof(ofs.f_mntonname));
137 (void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
138 sizeof(ofs.f_mntfromname));
139
140 return copyout(&ofs, bfs, sizeof(ofs));
141 }
142
143 /*
144 * Get filesystem statistics.
145 */
146 /* ARGSUSED */
147 int
148 compat_20_sys_statfs(struct lwp *l, const struct compat_20_sys_statfs_args *uap, register_t *retval)
149 {
150 /* {
151 syscallarg(const char *) path;
152 syscallarg(struct statfs12 *) buf;
153 } */
154 struct mount *mp;
155 struct statvfs *sbuf;
156 int error;
157 struct vnode *vp;
158
159 error = namei_simple_user(SCARG(uap, path),
160 NSM_FOLLOW_TRYEMULROOT, &vp);
161 if (error != 0)
162 return error;
163
164 mp = vp->v_mount;
165
166 sbuf = STATVFSBUF_GET();
167 if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
168 goto done;
169
170 error = statvfs_to_statfs12(sbuf, SCARG(uap, buf), 0);
171 done:
172 vrele(vp);
173 STATVFSBUF_PUT(sbuf);
174 return error;
175 }
176
177 /*
178 * Get filesystem statistics.
179 */
180 /* ARGSUSED */
181 int
182 compat_20_sys_fstatfs(struct lwp *l, const struct compat_20_sys_fstatfs_args *uap, register_t *retval)
183 {
184 /* {
185 syscallarg(int) fd;
186 syscallarg(struct statfs12 *) buf;
187 } */
188 struct file *fp;
189 struct mount *mp;
190 struct statvfs *sbuf;
191 int error;
192
193 /* fd_getvnode() will use the descriptor for us */
194 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
195 return (error);
196 mp = fp->f_vnode->v_mount;
197 sbuf = STATVFSBUF_GET();
198 if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
199 goto out;
200 error = statvfs_to_statfs12(sbuf, SCARG(uap, buf), 0);
201 out:
202 fd_putfile(SCARG(uap, fd));
203 STATVFSBUF_PUT(sbuf);
204 return error;
205 }
206
207
208 /*
209 * Get statistics on all filesystems.
210 */
211 int
212 compat_20_sys_getfsstat(struct lwp *l, const struct compat_20_sys_getfsstat_args *uap, register_t *retval)
213 {
214 /* {
215 syscallarg(struct statfs12 *) buf;
216 syscallarg(long) bufsize;
217 syscallarg(int) flags;
218 } */
219 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
220 SCARG(uap, flags), statvfs_to_statfs12,
221 sizeof(struct statvfs90), retval);
222 }
223
224 int
225 compat_20_sys_fhstatfs(struct lwp *l, const struct compat_20_sys_fhstatfs_args *uap, register_t *retval)
226 {
227 /* {
228 syscallarg(const struct compat_30_fhandle *) fhp;
229 syscallarg(struct statfs12 *) buf;
230 } */
231 struct statvfs *sbuf;
232 struct compat_30_fhandle fh;
233 struct mount *mp;
234 struct vnode *vp;
235 int error;
236
237 /*
238 * Must be super user
239 */
240 if ((error = kauth_authorize_system(l->l_cred,
241 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
242 return (error);
243
244 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
245 return (error);
246
247 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
248 return (ESTALE);
249 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
250 return (error);
251 mp = vp->v_mount;
252 VOP_UNLOCK(vp);
253 sbuf = STATVFSBUF_GET();
254 if ((error = VFS_STATVFS(mp, sbuf)) != 0)
255 goto out;
256 error = statvfs_to_statfs12(sbuf, SCARG(uap, buf), 0);
257 out:
258 vrele(vp);
259 STATVFSBUF_PUT(sbuf);
260 return error;
261 }
262
263 int
264 vfs_syscalls_20_init(void)
265 {
266
267 return syscall_establish(NULL, vfs_syscalls_20_syscalls);
268 }
269
270 int
271 vfs_syscalls_20_fini(void)
272 {
273
274 return syscall_disestablish(NULL, vfs_syscalls_20_syscalls);
275 }
276