vfs_syscalls_43.c revision 1.56.4.1.4.1 1 /* $NetBSD: vfs_syscalls_43.c,v 1.56.4.1.4.1 2017/08/12 04:16:52 snj 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.28 (Berkeley) 12/10/94
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.56.4.1.4.1 2017/08/12 04:16:52 snj Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/file.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/dirent.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/stat.h>
58 #include <sys/malloc.h>
59 #include <sys/ioctl.h>
60 #include <sys/fcntl.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
64 #include <sys/resourcevar.h>
65 #include <sys/sysctl.h>
66
67 #include <sys/mount.h>
68 #include <sys/syscallargs.h>
69 #include <sys/vfs_syscalls.h>
70
71 #include <compat/sys/stat.h>
72 #include <compat/sys/mount.h>
73 #include <compat/sys/dirent.h>
74
75 #include <compat/common/compat_util.h>
76 #include <compat/common/compat_mod.h>
77
78 static void cvttimespec(struct timespec *, struct timespec50 *);
79 static void cvtstat(struct stat *, struct stat43 *);
80
81 /*
82 * Convert from an old to a new timespec structure.
83 */
84 static void
85 cvttimespec(struct timespec *ts, struct timespec50 *ots)
86 {
87
88 if (ts->tv_sec > INT_MAX) {
89 #if defined(DEBUG) || 1
90 static bool first = true;
91
92 if (first) {
93 first = false;
94 printf("%s[%s:%d]: time_t does not fit\n",
95 __func__, curlwp->l_proc->p_comm,
96 curlwp->l_lid);
97 }
98 #endif
99 ots->tv_sec = INT_MAX;
100 } else
101 ots->tv_sec = ts->tv_sec;
102 ots->tv_nsec = ts->tv_nsec;
103 }
104
105 /*
106 * Convert from an old to a new stat structure.
107 */
108 static void
109 cvtstat(struct stat *st, struct stat43 *ost)
110 {
111
112 /* Handle any padding. */
113 memset(ost, 0, sizeof *ost);
114 ost->st_dev = st->st_dev;
115 ost->st_ino = st->st_ino;
116 ost->st_mode = st->st_mode & 0xffff;
117 ost->st_nlink = st->st_nlink;
118 ost->st_uid = st->st_uid;
119 ost->st_gid = st->st_gid;
120 ost->st_rdev = st->st_rdev;
121 if (st->st_size < (quad_t)1 << 32)
122 ost->st_size = st->st_size;
123 else
124 ost->st_size = -2;
125 cvttimespec(&st->st_atimespec, &ost->st_atimespec);
126 cvttimespec(&st->st_mtimespec, &ost->st_mtimespec);
127 cvttimespec(&st->st_ctimespec, &ost->st_ctimespec);
128 ost->st_blksize = st->st_blksize;
129 ost->st_blocks = st->st_blocks;
130 ost->st_flags = st->st_flags;
131 ost->st_gen = st->st_gen;
132 }
133
134 /*
135 * Get file status; this version follows links.
136 */
137 /* ARGSUSED */
138 int
139 compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
140 {
141 /* {
142 syscallarg(char *) path;
143 syscallarg(struct stat43 *) ub;
144 } */
145 struct stat sb;
146 struct stat43 osb;
147 int error;
148
149 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
150 if (error)
151 return (error);
152 cvtstat(&sb, &osb);
153 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
154 return (error);
155 }
156
157 /*
158 * Get file status; this version does not follow links.
159 */
160 /* ARGSUSED */
161 int
162 compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
163 {
164 /* {
165 syscallarg(char *) path;
166 syscallarg(struct ostat *) ub;
167 } */
168 struct vnode *vp, *dvp;
169 struct stat sb, sb1;
170 struct stat43 osb;
171 int error;
172 struct pathbuf *pb;
173 struct nameidata nd;
174 int ndflags;
175
176 error = pathbuf_copyin(SCARG(uap, path), &pb);
177 if (error) {
178 return error;
179 }
180
181 ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
182 again:
183 NDINIT(&nd, LOOKUP, ndflags, pb);
184 if ((error = namei(&nd))) {
185 if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
186 /*
187 * Should only happen on '/'. Retry without LOCKPARENT;
188 * this is safe since the vnode won't be a VLNK.
189 */
190 ndflags &= ~LOCKPARENT;
191 goto again;
192 }
193 pathbuf_destroy(pb);
194 return (error);
195 }
196 /*
197 * For symbolic links, always return the attributes of its
198 * containing directory, except for mode, size, and links.
199 */
200 vp = nd.ni_vp;
201 dvp = nd.ni_dvp;
202 pathbuf_destroy(pb);
203 if (vp->v_type != VLNK) {
204 if ((ndflags & LOCKPARENT) != 0) {
205 if (dvp == vp)
206 vrele(dvp);
207 else
208 vput(dvp);
209 }
210 error = vn_stat(vp, &sb);
211 vput(vp);
212 if (error)
213 return (error);
214 } else {
215 error = vn_stat(dvp, &sb);
216 vput(dvp);
217 if (error) {
218 vput(vp);
219 return (error);
220 }
221 error = vn_stat(vp, &sb1);
222 vput(vp);
223 if (error)
224 return (error);
225 sb.st_mode &= ~S_IFDIR;
226 sb.st_mode |= S_IFLNK;
227 sb.st_nlink = sb1.st_nlink;
228 sb.st_size = sb1.st_size;
229 sb.st_blocks = sb1.st_blocks;
230 }
231 cvtstat(&sb, &osb);
232 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
233 return (error);
234 }
235
236 /*
237 * Return status information about a file descriptor.
238 */
239 /* ARGSUSED */
240 int
241 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
242 {
243 /* {
244 syscallarg(int) fd;
245 syscallarg(struct stat43 *) sb;
246 } */
247 struct stat ub;
248 struct stat43 oub;
249 int error;
250
251 error = do_sys_fstat(SCARG(uap, fd), &ub);
252 if (error == 0) {
253 cvtstat(&ub, &oub);
254 error = copyout((void *)&oub, (void *)SCARG(uap, sb),
255 sizeof (oub));
256 }
257
258 return (error);
259 }
260
261
262 /*
263 * Truncate a file given a file descriptor.
264 */
265 /* ARGSUSED */
266 int
267 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
268 {
269 /* {
270 syscallarg(int) fd;
271 syscallarg(long) length;
272 } */
273 struct sys_ftruncate_args /* {
274 syscallarg(int) fd;
275 syscallarg(int) pad;
276 syscallarg(off_t) length;
277 } */ nuap;
278
279 SCARG(&nuap, fd) = SCARG(uap, fd);
280 SCARG(&nuap, length) = SCARG(uap, length);
281 return (sys_ftruncate(l, &nuap, retval));
282 }
283
284 /*
285 * Truncate a file given its path name.
286 */
287 /* ARGSUSED */
288 int
289 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
290 {
291 /* {
292 syscallarg(char *) path;
293 syscallarg(long) length;
294 } */
295 struct sys_truncate_args /* {
296 syscallarg(char *) path;
297 syscallarg(int) pad;
298 syscallarg(off_t) length;
299 } */ nuap;
300
301 SCARG(&nuap, path) = SCARG(uap, path);
302 SCARG(&nuap, length) = SCARG(uap, length);
303 return (sys_truncate(l, &nuap, retval));
304 }
305
306
307 /*
308 * Reposition read/write file offset.
309 */
310 int
311 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
312 {
313 /* {
314 syscallarg(int) fd;
315 syscallarg(long) offset;
316 syscallarg(int) whence;
317 } */
318 struct sys_lseek_args /* {
319 syscallarg(int) fd;
320 syscallarg(int) pad;
321 syscallarg(off_t) offset;
322 syscallarg(int) whence;
323 } */ nuap;
324 off_t qret;
325 int error;
326
327 SCARG(&nuap, fd) = SCARG(uap, fd);
328 SCARG(&nuap, offset) = SCARG(uap, offset);
329 SCARG(&nuap, whence) = SCARG(uap, whence);
330 error = sys_lseek(l, &nuap, (void *)&qret);
331 *(long *)retval = qret;
332 return (error);
333 }
334
335
336 /*
337 * Create a file.
338 */
339 int
340 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
341 {
342 /* {
343 syscallarg(char *) path;
344 syscallarg(int) mode;
345 } */
346 struct sys_open_args /* {
347 syscallarg(char *) path;
348 syscallarg(int) flags;
349 syscallarg(int) mode;
350 } */ nuap;
351
352 SCARG(&nuap, path) = SCARG(uap, path);
353 SCARG(&nuap, mode) = SCARG(uap, mode);
354 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
355 return (sys_open(l, &nuap, retval));
356 }
357
358 /*ARGSUSED*/
359 int
360 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
361 {
362
363 return (ENOSYS);
364 }
365
366
367 /*
368 * Read a block of directory entries in a file system independent format.
369 */
370 int
371 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
372 {
373 /* {
374 syscallarg(int) fd;
375 syscallarg(char *) buf;
376 syscallarg(u_int) count;
377 syscallarg(long *) basep;
378 } */
379 struct dirent *bdp;
380 struct vnode *vp;
381 char *inp, *tbuf; /* Current-format */
382 int len, reclen; /* Current-format */
383 char *outp; /* Dirent12-format */
384 int resid, old_reclen = 0; /* Dirent12-format */
385 struct file *fp;
386 struct uio auio;
387 struct iovec aiov;
388 struct dirent43 idb;
389 off_t off; /* true file offset */
390 int buflen, error, eofflag, nbytes;
391 struct vattr va;
392 off_t *cookiebuf = NULL, *cookie;
393 int ncookies;
394 long loff;
395
396 /* fd_getvnode() will use the descriptor for us */
397 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
398 return (error);
399
400 if ((fp->f_flag & FREAD) == 0) {
401 error = EBADF;
402 goto out1;
403 }
404
405 vp = (struct vnode *)fp->f_data;
406 if (vp->v_type != VDIR) {
407 error = ENOTDIR;
408 goto out1;
409 }
410
411 vn_lock(vp, LK_SHARED | LK_RETRY);
412 error = VOP_GETATTR(vp, &va, l->l_cred);
413 VOP_UNLOCK(vp);
414 if (error)
415 goto out1;
416
417 loff = fp->f_offset;
418 nbytes = SCARG(uap, count);
419 buflen = min(MAXBSIZE, nbytes);
420 if (buflen < va.va_blocksize)
421 buflen = va.va_blocksize;
422 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
423
424 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
425 off = fp->f_offset;
426 again:
427 aiov.iov_base = tbuf;
428 aiov.iov_len = buflen;
429 auio.uio_iov = &aiov;
430 auio.uio_iovcnt = 1;
431 auio.uio_rw = UIO_READ;
432 auio.uio_resid = buflen;
433 auio.uio_offset = off;
434 UIO_SETUP_SYSSPACE(&auio);
435 /*
436 * First we read into the malloc'ed buffer, then
437 * we massage it into user space, one record at a time.
438 */
439 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
440 &ncookies);
441 if (error)
442 goto out;
443
444 inp = tbuf;
445 outp = SCARG(uap, buf);
446 resid = nbytes;
447 if ((len = buflen - auio.uio_resid) == 0)
448 goto eof;
449
450 for (cookie = cookiebuf; len > 0; len -= reclen) {
451 bdp = (struct dirent *)inp;
452 reclen = bdp->d_reclen;
453 if (reclen & 3) {
454 error = EIO;
455 goto out;
456 }
457 if (bdp->d_fileno == 0) {
458 inp += reclen; /* it is a hole; squish it out */
459 if (cookie)
460 off = *cookie++;
461 else
462 off += reclen;
463 continue;
464 }
465 old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
466 if (reclen > len || resid < old_reclen) {
467 /* entry too big for buffer, so just stop */
468 outp++;
469 break;
470 }
471 /*
472 * Massage in place to make a Dirent12-shaped dirent (otherwise
473 * we have to worry about touching user memory outside of
474 * the copyout() call).
475 */
476 idb.d_fileno = (uint32_t)bdp->d_fileno;
477 idb.d_reclen = (uint16_t)old_reclen;
478 idb.d_namlen = (uint16_t)bdp->d_namlen;
479 strcpy(idb.d_name, bdp->d_name);
480 if ((error = copyout(&idb, outp, old_reclen)))
481 goto out;
482 /* advance past this real entry */
483 inp += reclen;
484 if (cookie)
485 off = *cookie++; /* each entry points to itself */
486 else
487 off += reclen;
488 /* advance output past Dirent12-shaped entry */
489 outp += old_reclen;
490 resid -= old_reclen;
491 }
492
493 /* if we squished out the whole block, try again */
494 if (outp == SCARG(uap, buf)) {
495 if (cookiebuf)
496 free(cookiebuf, M_TEMP);
497 cookiebuf = NULL;
498 goto again;
499 }
500 fp->f_offset = off; /* update the vnode offset */
501
502 eof:
503 *retval = nbytes - resid;
504 out:
505 VOP_UNLOCK(vp);
506 if (cookiebuf)
507 free(cookiebuf, M_TEMP);
508 free(tbuf, M_TEMP);
509 out1:
510 fd_putfile(SCARG(uap, fd));
511 if (error)
512 return error;
513 return copyout(&loff, SCARG(uap, basep), sizeof(long));
514 }
515
516 /*
517 * sysctl helper routine for vfs.generic.conf lookups.
518 */
519 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
520
521 static int
522 sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
523 {
524 struct vfsconf vfc;
525 extern const char * const mountcompatnames[];
526 extern int nmountcompatnames;
527 struct sysctlnode node;
528 struct vfsops *vfsp;
529 u_int vfsnum;
530
531 if (namelen != 1)
532 return (ENOTDIR);
533 vfsnum = name[0];
534 if (vfsnum >= nmountcompatnames ||
535 mountcompatnames[vfsnum] == NULL)
536 return (EOPNOTSUPP);
537 vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
538 if (vfsp == NULL)
539 return (EOPNOTSUPP);
540
541 vfc.vfc_vfsops = vfsp;
542 strncpy(vfc.vfc_name, vfsp->vfs_name, sizeof(vfc.vfc_name));
543 vfc.vfc_typenum = vfsnum;
544 vfc.vfc_refcount = vfsp->vfs_refcount;
545 vfc.vfc_flags = 0;
546 vfc.vfc_mountroot = vfsp->vfs_mountroot;
547 vfc.vfc_next = NULL;
548 vfs_delref(vfsp);
549
550 node = *rnode;
551 node.sysctl_data = &vfc;
552 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
553 }
554
555 /*
556 * Top level filesystem related information gathering.
557 */
558 void
559 compat_sysctl_vfs(struct sysctllog **clog)
560 {
561 extern int nmountcompatnames;
562
563 sysctl_createv(clog, 0, NULL, NULL,
564 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
565 CTLTYPE_INT, "maxtypenum",
566 SYSCTL_DESCR("Highest valid filesystem type number"),
567 NULL, nmountcompatnames, NULL, 0,
568 CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
569 sysctl_createv(clog, 0, NULL, NULL,
570 CTLFLAG_PERMANENT,
571 CTLTYPE_STRUCT, "conf",
572 SYSCTL_DESCR("Filesystem configuration information"),
573 sysctl_vfs_generic_conf, 0, NULL,
574 sizeof(struct vfsconf),
575 CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
576 }
577 #endif
578