vfs_syscalls_30.c revision 1.39 1 /* $NetBSD: vfs_syscalls_30.c,v 1.39 2019/09/22 22:59:38 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.39 2019/09/22 22:59:38 christos Exp $");
33
34 #if defined(_KERNEL_OPT)
35 #include "opt_compat_netbsd.h"
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/filedesc.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/socketvar.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/proc.h>
49 #include <sys/uio.h>
50 #include <sys/dirent.h>
51 #include <sys/malloc.h>
52 #include <sys/kauth.h>
53 #include <sys/vfs_syscalls.h>
54 #include <sys/syscall.h>
55 #include <sys/syscallvar.h>
56 #include <sys/syscallargs.h>
57
58 #include <compat/common/compat_mod.h>
59 #include <compat/common/compat_util.h>
60
61 #include <compat/sys/stat.h>
62 #include <compat/sys/dirent.h>
63 #include <compat/sys/mount.h>
64 #include <compat/sys/statvfs.h>
65
66 static void cvtstat(struct stat13 *, const struct stat *);
67
68 static const struct syscall_package vfs_syscalls_30_syscalls[] = {
69 { SYS_compat_30___fhstat30, 0, (sy_call_t *)compat_30_sys___fhstat30 },
70 { SYS_compat_30___fstat13, 0, (sy_call_t *)compat_30_sys___fstat13 },
71 { SYS_compat_30___lstat13, 0, (sy_call_t *)compat_30_sys___lstat13 },
72 { SYS_compat_30___stat13, 0, (sy_call_t *)compat_30_sys___stat13 },
73 { SYS_compat_30_fhopen, 0, (sy_call_t *)compat_30_sys_fhopen },
74 { SYS_compat_30_fhstat, 0, (sy_call_t *)compat_30_sys_fhstat },
75 { SYS_compat_30_fhstatvfs1, 0, (sy_call_t *)compat_30_sys_fhstatvfs1 },
76 { SYS_compat_30_getdents, 0, (sy_call_t *)compat_30_sys_getdents },
77 { SYS_compat_30_getfh, 0, (sy_call_t *)compat_30_sys_getfh },
78 { 0,0, NULL }
79 };
80
81 /*
82 * Convert from a new to an old stat structure.
83 */
84 static void
85 cvtstat(struct stat13 *ost, const struct stat *st)
86 {
87
88 ost->st_dev = st->st_dev;
89 ost->st_ino = (uint32_t)st->st_ino;
90 ost->st_mode = st->st_mode;
91 ost->st_nlink = st->st_nlink;
92 ost->st_uid = st->st_uid;
93 ost->st_gid = st->st_gid;
94 ost->st_rdev = st->st_rdev;
95 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
96 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
97 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
98 timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec);
99 ost->st_size = st->st_size;
100 ost->st_blocks = st->st_blocks;
101 ost->st_blksize = st->st_blksize;
102 ost->st_flags = st->st_flags;
103 ost->st_gen = st->st_gen;
104 }
105
106 /*
107 * Get file status; this version follows links.
108 */
109 /* ARGSUSED */
110 int
111 compat_30_sys___stat13(struct lwp *l,
112 const struct compat_30_sys___stat13_args *uap, register_t *retval)
113 {
114 /* {
115 syscallarg(const char *) path;
116 syscallarg(struct stat13 *) ub;
117 } */
118 struct stat sb;
119 struct stat13 osb;
120 int error;
121
122 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
123 if (error)
124 return error;
125 cvtstat(&osb, &sb);
126 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
127 return error;
128 }
129
130
131 /*
132 * Get file status; this version does not follow links.
133 */
134 /* ARGSUSED */
135 int
136 compat_30_sys___lstat13(struct lwp *l,
137 const struct compat_30_sys___lstat13_args *uap, register_t *retval)
138 {
139 /* {
140 syscallarg(const char *) path;
141 syscallarg(struct stat13 *) ub;
142 } */
143 struct stat sb;
144 struct stat13 osb;
145 int error;
146
147 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
148 if (error)
149 return error;
150 cvtstat(&osb, &sb);
151 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
152 return error;
153 }
154
155 /* ARGSUSED */
156 int
157 compat_30_sys_fhstat(struct lwp *l,
158 const struct compat_30_sys_fhstat_args *uap, register_t *retval)
159 {
160 /* {
161 syscallarg(const struct compat_30_fhandle *) fhp;
162 syscallarg(struct stat13 *) sb;
163 } */
164 struct stat sb;
165 struct stat13 osb;
166 int error;
167 struct compat_30_fhandle fh;
168 struct mount *mp;
169 struct vnode *vp;
170
171 /*
172 * Must be super user
173 */
174 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
175 0, NULL, NULL, NULL)))
176 return (error);
177
178 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
179 return (error);
180
181 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
182 return (ESTALE);
183 if (mp->mnt_op->vfs_fhtovp == NULL)
184 return EOPNOTSUPP;
185 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
186 return (error);
187 error = vn_stat(vp, &sb);
188 vput(vp);
189 if (error)
190 return (error);
191 cvtstat(&osb, &sb);
192 error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
193 return (error);
194 }
195
196 /*
197 * Return status information about a file descriptor.
198 */
199 /* ARGSUSED */
200 int
201 compat_30_sys___fstat13(struct lwp *l,
202 const struct compat_30_sys___fstat13_args *uap, register_t *retval)
203 {
204 /* {
205 syscallarg(int) fd;
206 syscallarg(struct stat13 *) sb;
207 } */
208 struct stat sb;
209 struct stat13 osb;
210 int error;
211
212 error = do_sys_fstat(SCARG(uap, fd), &sb);
213 if (error)
214 return error;
215 cvtstat(&osb, &sb);
216 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
217 return error;
218 }
219
220 /*
221 * Read a block of directory entries in a file system independent format.
222 */
223 int
224 compat_30_sys_getdents(struct lwp *l,
225 const struct compat_30_sys_getdents_args *uap, register_t *retval)
226 {
227 /* {
228 syscallarg(int) fd;
229 syscallarg(char *) buf;
230 syscallarg(size_t) count;
231 } */
232 struct dirent *bdp;
233 struct vnode *vp;
234 char *inp, *tbuf; /* BSD-format */
235 int len, reclen; /* BSD-format */
236 char *outp; /* NetBSD-3.0-format */
237 int resid;
238 struct file *fp;
239 struct uio auio;
240 struct iovec aiov;
241 struct dirent12 idb;
242 off_t off; /* true file offset */
243 int buflen, error, eofflag;
244 off_t *cookiebuf = NULL, *cookie;
245 int ncookies;
246
247 /* fd_getvnode() will use the descriptor for us */
248 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
249 return error;
250
251 if ((fp->f_flag & FREAD) == 0) {
252 error = EBADF;
253 goto out1;
254 }
255
256 vp = fp->f_vnode;
257 if (vp->v_type != VDIR) {
258 error = EINVAL;
259 goto out1;
260 }
261
262 buflen = uimin(MAXBSIZE, SCARG(uap, count));
263 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
265 off = fp->f_offset;
266 again:
267 aiov.iov_base = tbuf;
268 aiov.iov_len = buflen;
269 auio.uio_iov = &aiov;
270 auio.uio_iovcnt = 1;
271 auio.uio_rw = UIO_READ;
272 auio.uio_resid = buflen;
273 auio.uio_offset = off;
274 UIO_SETUP_SYSSPACE(&auio);
275 /*
276 * First we read into the malloc'ed buffer, then
277 * we massage it into user space, one record at a time.
278 */
279 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
280 &ncookies);
281 if (error)
282 goto out;
283
284 inp = tbuf;
285 outp = SCARG(uap, buf);
286 resid = SCARG(uap, count);
287 if ((len = buflen - auio.uio_resid) == 0)
288 goto eof;
289
290 for (cookie = cookiebuf; len > 0; len -= reclen) {
291 bdp = (struct dirent *)inp;
292 reclen = bdp->d_reclen;
293 if (reclen & _DIRENT_ALIGN(bdp))
294 panic("netbsd30_getdents: bad reclen %d", reclen);
295 if (cookie)
296 off = *cookie++; /* each entry points to the next */
297 else
298 off += reclen;
299 if ((off >> 32) != 0) {
300 compat_offseterr(vp, "netbsd30_getdents");
301 error = EINVAL;
302 goto out;
303 }
304 if (bdp->d_namlen >= sizeof(idb.d_name))
305 idb.d_namlen = sizeof(idb.d_name) - 1;
306 else
307 idb.d_namlen = bdp->d_namlen;
308 idb.d_reclen = _DIRENT_SIZE(&idb);
309 if (reclen > len || resid < idb.d_reclen) {
310 /* entry too big for buffer, so just stop */
311 outp++;
312 break;
313 }
314 /*
315 * Massage in place to make a NetBSD-3.0-shaped dirent
316 * (otherwise we have to worry about touching user memory
317 * outside of the copyout() call).
318 */
319 idb.d_fileno = (u_int32_t)bdp->d_fileno;
320 idb.d_type = bdp->d_type;
321 (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
322 memset(idb.d_name + idb.d_namlen, 0,
323 idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
324 if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
325 goto out;
326 /* advance past this real entry */
327 inp += reclen;
328 /* advance output past NetBSD-3.0-shaped entry */
329 outp += idb.d_reclen;
330 resid -= idb.d_reclen;
331 }
332
333 /* if we squished out the whole block, try again */
334 if (outp == SCARG(uap, buf)) {
335 if (cookiebuf)
336 free(cookiebuf, M_TEMP);
337 cookiebuf = NULL;
338 goto again;
339 }
340 fp->f_offset = off; /* update the vnode offset */
341
342 eof:
343 *retval = SCARG(uap, count) - resid;
344 out:
345 VOP_UNLOCK(vp);
346 if (cookiebuf)
347 free(cookiebuf, M_TEMP);
348 free(tbuf, M_TEMP);
349 out1:
350 fd_putfile(SCARG(uap, fd));
351 return error;
352 }
353
354 /*
355 * Get file handle system call
356 */
357 int
358 compat_30_sys_getfh(struct lwp *l, const struct compat_30_sys_getfh_args *uap,
359 register_t *retval)
360 {
361 /* {
362 syscallarg(char *) fname;
363 syscallarg(struct compat_30_fhandle *) fhp;
364 } */
365 struct vnode *vp;
366 struct compat_30_fhandle fh;
367 int error;
368 struct pathbuf *pb;
369 struct nameidata nd;
370 size_t sz;
371
372 /*
373 * Must be super user
374 */
375 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
376 0, NULL, NULL, NULL);
377 if (error)
378 return (error);
379
380 error = pathbuf_copyin(SCARG(uap, fname), &pb);
381 if (error) {
382 return error;
383 }
384 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
385 error = namei(&nd);
386 pathbuf_destroy(pb);
387 if (error)
388 return error;
389 vp = nd.ni_vp;
390
391 sz = sizeof(struct compat_30_fhandle);
392 error = vfs_composefh(vp, (void *)&fh, &sz);
393 vput(vp);
394 if (sz != FHANDLE_SIZE_COMPAT) {
395 error = EINVAL;
396 }
397 if (error)
398 return (error);
399 error = copyout(&fh, SCARG(uap, fhp), sizeof(struct compat_30_fhandle));
400 return (error);
401 }
402
403 /*
404 * Open a file given a file handle.
405 *
406 * Check permissions, allocate an open file structure,
407 * and call the device open routine if any.
408 */
409 int
410 compat_30_sys_fhopen(struct lwp *l,
411 const struct compat_30_sys_fhopen_args *uap, register_t *retval)
412 {
413 /* {
414 syscallarg(const fhandle_t *) fhp;
415 syscallarg(int) flags;
416 } */
417
418 return dofhopen(l, SCARG(uap, fhp), FHANDLE_SIZE_COMPAT,
419 SCARG(uap, flags), retval);
420 }
421
422 /* ARGSUSED */
423 int
424 compat_30_sys___fhstat30(struct lwp *l,
425 const struct compat_30_sys___fhstat30_args *uap_30, register_t *retval)
426 {
427 /* {
428 syscallarg(const fhandle_t *) fhp;
429 syscallarg(struct stat30 *) sb;
430 } */
431 struct stat sb;
432 struct stat13 osb;
433 int error;
434
435 error = do_fhstat(l, SCARG(uap_30, fhp), FHANDLE_SIZE_COMPAT, &sb);
436 if (error)
437 return error;
438 cvtstat(&osb, &sb);
439 error = copyout(&osb, SCARG(uap_30, sb), sizeof (osb));
440 return error;
441 }
442
443 /* ARGSUSED */
444 int
445 compat_30_sys_fhstatvfs1(struct lwp *l,
446 const struct compat_30_sys_fhstatvfs1_args *uap, register_t *retval)
447 {
448 /* {
449 syscallarg(const fhandle_t *) fhp;
450 syscallarg(struct statvfs90 *) buf;
451 syscallarg(int) flags;
452 } */
453 struct statvfs *sb = STATVFSBUF_GET();
454 int error = do_fhstatvfs(l, SCARG(uap, fhp), FHANDLE_SIZE_COMPAT,
455 sb, SCARG(uap, flags));
456
457 if (!error) {
458 error = statvfs_to_statvfs90_copy(sb, SCARG(uap, buf),
459 sizeof(struct statvfs90));
460 }
461
462 STATVFSBUF_PUT(sb);
463
464 return error;
465 }
466
467 int
468 vfs_syscalls_30_init(void)
469 {
470
471 return syscall_establish(NULL, vfs_syscalls_30_syscalls);
472 }
473
474 int
475 vfs_syscalls_30_fini(void)
476 {
477
478 return syscall_disestablish(NULL, vfs_syscalls_30_syscalls);
479 }
480