vfs_syscalls_30.c revision 1.38 1 /* $NetBSD: vfs_syscalls_30.c,v 1.38 2019/01/27 02:08:39 pgoyette 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.38 2019/01/27 02:08:39 pgoyette 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 #include <compat/sys/stat.h>
61 #include <compat/sys/dirent.h>
62 #include <compat/sys/mount.h>
63
64 static void cvtstat(struct stat13 *, const struct stat *);
65
66 static const struct syscall_package vfs_syscalls_30_syscalls[] = {
67 { SYS_compat_30___fhstat30, 0, (sy_call_t *)compat_30_sys___fhstat30 },
68 { SYS_compat_30___fstat13, 0, (sy_call_t *)compat_30_sys___fstat13 },
69 { SYS_compat_30___lstat13, 0, (sy_call_t *)compat_30_sys___lstat13 },
70 { SYS_compat_30___stat13, 0, (sy_call_t *)compat_30_sys___stat13 },
71 { SYS_compat_30_fhopen, 0, (sy_call_t *)compat_30_sys_fhopen },
72 { SYS_compat_30_fhstat, 0, (sy_call_t *)compat_30_sys_fhstat },
73 { SYS_compat_30_fhstatvfs1, 0, (sy_call_t *)compat_30_sys_fhstatvfs1 },
74 { SYS_compat_30_getdents, 0, (sy_call_t *)compat_30_sys_getdents },
75 { SYS_compat_30_getfh, 0, (sy_call_t *)compat_30_sys_getfh },
76 { 0,0, NULL }
77 };
78
79 /*
80 * Convert from a new to an old stat structure.
81 */
82 static void
83 cvtstat(struct stat13 *ost, const struct stat *st)
84 {
85
86 ost->st_dev = st->st_dev;
87 ost->st_ino = (uint32_t)st->st_ino;
88 ost->st_mode = st->st_mode;
89 ost->st_nlink = st->st_nlink;
90 ost->st_uid = st->st_uid;
91 ost->st_gid = st->st_gid;
92 ost->st_rdev = st->st_rdev;
93 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
94 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
95 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
96 timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec);
97 ost->st_size = st->st_size;
98 ost->st_blocks = st->st_blocks;
99 ost->st_blksize = st->st_blksize;
100 ost->st_flags = st->st_flags;
101 ost->st_gen = st->st_gen;
102 }
103
104 /*
105 * Get file status; this version follows links.
106 */
107 /* ARGSUSED */
108 int
109 compat_30_sys___stat13(struct lwp *l,
110 const struct compat_30_sys___stat13_args *uap, register_t *retval)
111 {
112 /* {
113 syscallarg(const char *) path;
114 syscallarg(struct stat13 *) ub;
115 } */
116 struct stat sb;
117 struct stat13 osb;
118 int error;
119
120 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
121 if (error)
122 return error;
123 cvtstat(&osb, &sb);
124 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
125 return error;
126 }
127
128
129 /*
130 * Get file status; this version does not follow links.
131 */
132 /* ARGSUSED */
133 int
134 compat_30_sys___lstat13(struct lwp *l,
135 const struct compat_30_sys___lstat13_args *uap, register_t *retval)
136 {
137 /* {
138 syscallarg(const char *) path;
139 syscallarg(struct stat13 *) ub;
140 } */
141 struct stat sb;
142 struct stat13 osb;
143 int error;
144
145 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
146 if (error)
147 return error;
148 cvtstat(&osb, &sb);
149 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
150 return error;
151 }
152
153 /* ARGSUSED */
154 int
155 compat_30_sys_fhstat(struct lwp *l,
156 const struct compat_30_sys_fhstat_args *uap, register_t *retval)
157 {
158 /* {
159 syscallarg(const struct compat_30_fhandle *) fhp;
160 syscallarg(struct stat13 *) sb;
161 } */
162 struct stat sb;
163 struct stat13 osb;
164 int error;
165 struct compat_30_fhandle fh;
166 struct mount *mp;
167 struct vnode *vp;
168
169 /*
170 * Must be super user
171 */
172 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
173 0, NULL, NULL, NULL)))
174 return (error);
175
176 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
177 return (error);
178
179 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
180 return (ESTALE);
181 if (mp->mnt_op->vfs_fhtovp == NULL)
182 return EOPNOTSUPP;
183 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
184 return (error);
185 error = vn_stat(vp, &sb);
186 vput(vp);
187 if (error)
188 return (error);
189 cvtstat(&osb, &sb);
190 error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
191 return (error);
192 }
193
194 /*
195 * Return status information about a file descriptor.
196 */
197 /* ARGSUSED */
198 int
199 compat_30_sys___fstat13(struct lwp *l,
200 const struct compat_30_sys___fstat13_args *uap, register_t *retval)
201 {
202 /* {
203 syscallarg(int) fd;
204 syscallarg(struct stat13 *) sb;
205 } */
206 struct stat sb;
207 struct stat13 osb;
208 int error;
209
210 error = do_sys_fstat(SCARG(uap, fd), &sb);
211 if (error)
212 return error;
213 cvtstat(&osb, &sb);
214 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
215 return error;
216 }
217
218 /*
219 * Read a block of directory entries in a file system independent format.
220 */
221 int
222 compat_30_sys_getdents(struct lwp *l,
223 const struct compat_30_sys_getdents_args *uap, register_t *retval)
224 {
225 /* {
226 syscallarg(int) fd;
227 syscallarg(char *) buf;
228 syscallarg(size_t) count;
229 } */
230 struct dirent *bdp;
231 struct vnode *vp;
232 char *inp, *tbuf; /* BSD-format */
233 int len, reclen; /* BSD-format */
234 char *outp; /* NetBSD-3.0-format */
235 int resid;
236 struct file *fp;
237 struct uio auio;
238 struct iovec aiov;
239 struct dirent12 idb;
240 off_t off; /* true file offset */
241 int buflen, error, eofflag;
242 off_t *cookiebuf = NULL, *cookie;
243 int ncookies;
244
245 /* fd_getvnode() will use the descriptor for us */
246 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
247 return error;
248
249 if ((fp->f_flag & FREAD) == 0) {
250 error = EBADF;
251 goto out1;
252 }
253
254 vp = fp->f_vnode;
255 if (vp->v_type != VDIR) {
256 error = EINVAL;
257 goto out1;
258 }
259
260 buflen = uimin(MAXBSIZE, SCARG(uap, count));
261 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
262 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
263 off = fp->f_offset;
264 again:
265 aiov.iov_base = tbuf;
266 aiov.iov_len = buflen;
267 auio.uio_iov = &aiov;
268 auio.uio_iovcnt = 1;
269 auio.uio_rw = UIO_READ;
270 auio.uio_resid = buflen;
271 auio.uio_offset = off;
272 UIO_SETUP_SYSSPACE(&auio);
273 /*
274 * First we read into the malloc'ed buffer, then
275 * we massage it into user space, one record at a time.
276 */
277 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
278 &ncookies);
279 if (error)
280 goto out;
281
282 inp = tbuf;
283 outp = SCARG(uap, buf);
284 resid = SCARG(uap, count);
285 if ((len = buflen - auio.uio_resid) == 0)
286 goto eof;
287
288 for (cookie = cookiebuf; len > 0; len -= reclen) {
289 bdp = (struct dirent *)inp;
290 reclen = bdp->d_reclen;
291 if (reclen & _DIRENT_ALIGN(bdp))
292 panic("netbsd30_getdents: bad reclen %d", reclen);
293 if (cookie)
294 off = *cookie++; /* each entry points to the next */
295 else
296 off += reclen;
297 if ((off >> 32) != 0) {
298 compat_offseterr(vp, "netbsd30_getdents");
299 error = EINVAL;
300 goto out;
301 }
302 if (bdp->d_namlen >= sizeof(idb.d_name))
303 idb.d_namlen = sizeof(idb.d_name) - 1;
304 else
305 idb.d_namlen = bdp->d_namlen;
306 idb.d_reclen = _DIRENT_SIZE(&idb);
307 if (reclen > len || resid < idb.d_reclen) {
308 /* entry too big for buffer, so just stop */
309 outp++;
310 break;
311 }
312 /*
313 * Massage in place to make a NetBSD-3.0-shaped dirent
314 * (otherwise we have to worry about touching user memory
315 * outside of the copyout() call).
316 */
317 idb.d_fileno = (u_int32_t)bdp->d_fileno;
318 idb.d_type = bdp->d_type;
319 (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
320 memset(idb.d_name + idb.d_namlen, 0,
321 idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
322 if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
323 goto out;
324 /* advance past this real entry */
325 inp += reclen;
326 /* advance output past NetBSD-3.0-shaped entry */
327 outp += idb.d_reclen;
328 resid -= idb.d_reclen;
329 }
330
331 /* if we squished out the whole block, try again */
332 if (outp == SCARG(uap, buf)) {
333 if (cookiebuf)
334 free(cookiebuf, M_TEMP);
335 cookiebuf = NULL;
336 goto again;
337 }
338 fp->f_offset = off; /* update the vnode offset */
339
340 eof:
341 *retval = SCARG(uap, count) - resid;
342 out:
343 VOP_UNLOCK(vp);
344 if (cookiebuf)
345 free(cookiebuf, M_TEMP);
346 free(tbuf, M_TEMP);
347 out1:
348 fd_putfile(SCARG(uap, fd));
349 return error;
350 }
351
352 /*
353 * Get file handle system call
354 */
355 int
356 compat_30_sys_getfh(struct lwp *l, const struct compat_30_sys_getfh_args *uap,
357 register_t *retval)
358 {
359 /* {
360 syscallarg(char *) fname;
361 syscallarg(struct compat_30_fhandle *) fhp;
362 } */
363 struct vnode *vp;
364 struct compat_30_fhandle fh;
365 int error;
366 struct pathbuf *pb;
367 struct nameidata nd;
368 size_t sz;
369
370 /*
371 * Must be super user
372 */
373 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
374 0, NULL, NULL, NULL);
375 if (error)
376 return (error);
377
378 error = pathbuf_copyin(SCARG(uap, fname), &pb);
379 if (error) {
380 return error;
381 }
382 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
383 error = namei(&nd);
384 pathbuf_destroy(pb);
385 if (error)
386 return error;
387 vp = nd.ni_vp;
388
389 sz = sizeof(struct compat_30_fhandle);
390 error = vfs_composefh(vp, (void *)&fh, &sz);
391 vput(vp);
392 if (sz != FHANDLE_SIZE_COMPAT) {
393 error = EINVAL;
394 }
395 if (error)
396 return (error);
397 error = copyout(&fh, SCARG(uap, fhp), sizeof(struct compat_30_fhandle));
398 return (error);
399 }
400
401 /*
402 * Open a file given a file handle.
403 *
404 * Check permissions, allocate an open file structure,
405 * and call the device open routine if any.
406 */
407 int
408 compat_30_sys_fhopen(struct lwp *l,
409 const struct compat_30_sys_fhopen_args *uap, register_t *retval)
410 {
411 /* {
412 syscallarg(const fhandle_t *) fhp;
413 syscallarg(int) flags;
414 } */
415
416 return dofhopen(l, SCARG(uap, fhp), FHANDLE_SIZE_COMPAT,
417 SCARG(uap, flags), retval);
418 }
419
420 /* ARGSUSED */
421 int
422 compat_30_sys___fhstat30(struct lwp *l,
423 const struct compat_30_sys___fhstat30_args *uap_30, register_t *retval)
424 {
425 /* {
426 syscallarg(const fhandle_t *) fhp;
427 syscallarg(struct stat30 *) sb;
428 } */
429 struct stat sb;
430 struct stat13 osb;
431 int error;
432
433 error = do_fhstat(l, SCARG(uap_30, fhp), FHANDLE_SIZE_COMPAT, &sb);
434 if (error)
435 return error;
436 cvtstat(&osb, &sb);
437 error = copyout(&osb, SCARG(uap_30, sb), sizeof (osb));
438 return error;
439 }
440
441 /* ARGSUSED */
442 int
443 compat_30_sys_fhstatvfs1(struct lwp *l,
444 const struct compat_30_sys_fhstatvfs1_args *uap_30, register_t *retval)
445 {
446 /* {
447 syscallarg(const fhandle_t *) fhp;
448 syscallarg(struct statvfs *) buf;
449 syscallarg(int) flags;
450 } */
451 struct sys___fhstatvfs140_args uap;
452
453 SCARG(&uap, fhp) = SCARG(uap_30, fhp);
454 SCARG(&uap, fh_size) = FHANDLE_SIZE_COMPAT;
455 SCARG(&uap, buf) = SCARG(uap_30, buf);
456 SCARG(&uap, flags) = SCARG(uap_30, flags);
457
458 return sys___fhstatvfs140(l, &uap, retval);
459 }
460
461 int
462 vfs_syscalls_30_init(void)
463 {
464
465 return syscall_establish(NULL, vfs_syscalls_30_syscalls);
466 }
467
468 int
469 vfs_syscalls_30_fini(void)
470 {
471
472 return syscall_disestablish(NULL, vfs_syscalls_30_syscalls);
473 }
474