vfs_syscalls_43.c revision 1.36 1 /* $NetBSD: vfs_syscalls_43.c,v 1.36 2007/03/04 06:01:13 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.28 (Berkeley) 12/10/94
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.36 2007/03/04 06:01:13 christos Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "fs_union.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/syslog.h>
62 #include <sys/unistd.h>
63 #include <sys/resourcevar.h>
64 #include <sys/sysctl.h>
65
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68
69 #include <compat/sys/stat.h>
70 #include <compat/sys/mount.h>
71
72 static void cvtstat __P((struct stat *, struct stat43 *));
73
74 /*
75 * Convert from an old to a new stat structure.
76 */
77 static void
78 cvtstat(st, ost)
79 struct stat *st;
80 struct stat43 *ost;
81 {
82
83 ost->st_dev = st->st_dev;
84 ost->st_ino = st->st_ino;
85 ost->st_mode = st->st_mode & 0xffff;
86 ost->st_nlink = st->st_nlink;
87 ost->st_uid = st->st_uid;
88 ost->st_gid = st->st_gid;
89 ost->st_rdev = st->st_rdev;
90 if (st->st_size < (quad_t)1 << 32)
91 ost->st_size = st->st_size;
92 else
93 ost->st_size = -2;
94 ost->st_atime = st->st_atime;
95 ost->st_mtime = st->st_mtime;
96 ost->st_ctime = st->st_ctime;
97 ost->st_blksize = st->st_blksize;
98 ost->st_blocks = st->st_blocks;
99 ost->st_flags = st->st_flags;
100 ost->st_gen = st->st_gen;
101 }
102
103 /*
104 * Get file status; this version follows links.
105 */
106 /* ARGSUSED */
107 int
108 compat_43_sys_stat(struct lwp *l, void *v, register_t *retval)
109 {
110 struct compat_43_sys_stat_args /* {
111 syscallarg(char *) path;
112 syscallarg(struct stat43 *) ub;
113 } */ *uap = v;
114 struct stat sb;
115 struct stat43 osb;
116 int error;
117 struct nameidata nd;
118
119 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
120 SCARG(uap, path), l);
121 if ((error = namei(&nd)) != 0)
122 return (error);
123 error = vn_stat(nd.ni_vp, &sb, l);
124 vput(nd.ni_vp);
125 if (error)
126 return (error);
127 cvtstat(&sb, &osb);
128 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
129 return (error);
130 }
131
132 /*
133 * Get file status; this version does not follow links.
134 */
135 /* ARGSUSED */
136 int
137 compat_43_sys_lstat(struct lwp *l, void *v, register_t *retval)
138 {
139 struct compat_43_sys_lstat_args /* {
140 syscallarg(char *) path;
141 syscallarg(struct ostat *) ub;
142 } */ *uap = v;
143 struct vnode *vp, *dvp;
144 struct stat sb, sb1;
145 struct stat43 osb;
146 int error;
147 struct nameidata nd;
148 int ndflags;
149
150 ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT;
151 again:
152 NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path), l);
153 if ((error = namei(&nd))) {
154 if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
155 /*
156 * Should only happen on '/'. Retry without LOCKPARENT;
157 * this is safe since the vnode won't be a VLNK.
158 */
159 ndflags &= ~LOCKPARENT;
160 goto again;
161 }
162 return (error);
163 }
164 /*
165 * For symbolic links, always return the attributes of its
166 * containing directory, except for mode, size, and links.
167 */
168 vp = nd.ni_vp;
169 dvp = nd.ni_dvp;
170 if (vp->v_type != VLNK) {
171 if ((ndflags & LOCKPARENT) != 0) {
172 if (dvp == vp)
173 vrele(dvp);
174 else
175 vput(dvp);
176 }
177 error = vn_stat(vp, &sb, l);
178 vput(vp);
179 if (error)
180 return (error);
181 } else {
182 error = vn_stat(dvp, &sb, l);
183 vput(dvp);
184 if (error) {
185 vput(vp);
186 return (error);
187 }
188 error = vn_stat(vp, &sb1, l);
189 vput(vp);
190 if (error)
191 return (error);
192 sb.st_mode &= ~S_IFDIR;
193 sb.st_mode |= S_IFLNK;
194 sb.st_nlink = sb1.st_nlink;
195 sb.st_size = sb1.st_size;
196 sb.st_blocks = sb1.st_blocks;
197 }
198 cvtstat(&sb, &osb);
199 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
200 return (error);
201 }
202
203 /*
204 * Return status information about a file descriptor.
205 */
206 /* ARGSUSED */
207 int
208 compat_43_sys_fstat(struct lwp *l, void *v, register_t *retval)
209 {
210 struct compat_43_sys_fstat_args /* {
211 syscallarg(int) fd;
212 syscallarg(struct stat43 *) sb;
213 } */ *uap = v;
214 struct proc *p = l->l_proc;
215 int fd = SCARG(uap, fd);
216 struct filedesc *fdp = p->p_fd;
217 struct file *fp;
218 struct stat ub;
219 struct stat43 oub;
220 int error;
221
222 if ((fp = fd_getfile(fdp, fd)) == NULL)
223 return (EBADF);
224
225 FILE_USE(fp);
226 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
227 FILE_UNUSE(fp, l);
228
229 if (error == 0) {
230 cvtstat(&ub, &oub);
231 error = copyout((void *)&oub, (void *)SCARG(uap, sb),
232 sizeof (oub));
233 }
234
235
236 return (error);
237 }
238
239
240 /*
241 * Truncate a file given a file descriptor.
242 */
243 /* ARGSUSED */
244 int
245 compat_43_sys_ftruncate(struct lwp *l, void *v, register_t *retval)
246 {
247 struct compat_43_sys_ftruncate_args /* {
248 syscallarg(int) fd;
249 syscallarg(long) length;
250 } */ *uap = v;
251 struct sys_ftruncate_args /* {
252 syscallarg(int) fd;
253 syscallarg(int) pad;
254 syscallarg(off_t) length;
255 } */ nuap;
256
257 SCARG(&nuap, fd) = SCARG(uap, fd);
258 SCARG(&nuap, length) = SCARG(uap, length);
259 return (sys_ftruncate(l, &nuap, retval));
260 }
261
262 /*
263 * Truncate a file given its path name.
264 */
265 /* ARGSUSED */
266 int
267 compat_43_sys_truncate(struct lwp *l, void *v, register_t *retval)
268 {
269 struct compat_43_sys_truncate_args /* {
270 syscallarg(char *) path;
271 syscallarg(long) length;
272 } */ *uap = v;
273 struct sys_truncate_args /* {
274 syscallarg(char *) path;
275 syscallarg(int) pad;
276 syscallarg(off_t) length;
277 } */ nuap;
278
279 SCARG(&nuap, path) = SCARG(uap, path);
280 SCARG(&nuap, length) = SCARG(uap, length);
281 return (sys_truncate(l, &nuap, retval));
282 }
283
284
285 /*
286 * Reposition read/write file offset.
287 */
288 int
289 compat_43_sys_lseek(struct lwp *l, void *v, register_t *retval)
290 {
291 struct compat_43_sys_lseek_args /* {
292 syscallarg(int) fd;
293 syscallarg(long) offset;
294 syscallarg(int) whence;
295 } */ *uap = v;
296 struct sys_lseek_args /* {
297 syscallarg(int) fd;
298 syscallarg(int) pad;
299 syscallarg(off_t) offset;
300 syscallarg(int) whence;
301 } */ nuap;
302 off_t qret;
303 int error;
304
305 SCARG(&nuap, fd) = SCARG(uap, fd);
306 SCARG(&nuap, offset) = SCARG(uap, offset);
307 SCARG(&nuap, whence) = SCARG(uap, whence);
308 error = sys_lseek(l, &nuap, (void *)&qret);
309 *(long *)retval = qret;
310 return (error);
311 }
312
313
314 /*
315 * Create a file.
316 */
317 int
318 compat_43_sys_creat(struct lwp *l, void *v, register_t *retval)
319 {
320 struct compat_43_sys_creat_args /* {
321 syscallarg(char *) path;
322 syscallarg(int) mode;
323 } */ *uap = v;
324 struct sys_open_args /* {
325 syscallarg(char *) path;
326 syscallarg(int) flags;
327 syscallarg(int) mode;
328 } */ nuap;
329
330 SCARG(&nuap, path) = SCARG(uap, path);
331 SCARG(&nuap, mode) = SCARG(uap, mode);
332 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
333 return (sys_open(l, &nuap, retval));
334 }
335
336 /*ARGSUSED*/
337 int
338 compat_43_sys_quota(struct lwp *l, void *v,
339 register_t *retval)
340 {
341
342 return (ENOSYS);
343 }
344
345
346 /*
347 * Read a block of directory entries in a file system independent format.
348 */
349 int
350 compat_43_sys_getdirentries(struct lwp *l, void *v, register_t *retval)
351 {
352 struct compat_43_sys_getdirentries_args /* {
353 syscallarg(int) fd;
354 syscallarg(char *) buf;
355 syscallarg(u_int) count;
356 syscallarg(long *) basep;
357 } */ *uap = v;
358 struct proc *p = l->l_proc;
359 struct vnode *vp;
360 struct file *fp;
361 struct uio auio, kuio;
362 struct iovec aiov, kiov;
363 struct dirent *dp, *edp;
364 char *dirbuf;
365 size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count));
366
367 int error, eofflag, readcnt;
368 long loff;
369
370 /* getvnode() will use the descriptor for us */
371 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
372 return (error);
373 if ((fp->f_flag & FREAD) == 0) {
374 error = EBADF;
375 goto out;
376 }
377 vp = (struct vnode *)fp->f_data;
378 unionread:
379 if (vp->v_type != VDIR) {
380 error = EINVAL;
381 goto out;
382 }
383 aiov.iov_base = SCARG(uap, buf);
384 aiov.iov_len = count;
385 auio.uio_iov = &aiov;
386 auio.uio_iovcnt = 1;
387 auio.uio_rw = UIO_READ;
388 auio.uio_resid = count;
389 KASSERT(l == curlwp);
390 auio.uio_vmspace = l->l_proc->p_vmspace;
391 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
392 loff = auio.uio_offset = fp->f_offset;
393 # if (BYTE_ORDER != LITTLE_ENDIAN)
394 if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) {
395 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
396 (off_t **)0, (int *)0);
397 fp->f_offset = auio.uio_offset;
398 } else
399 # endif
400 {
401 kuio = auio;
402 kuio.uio_iov = &kiov;
403 kiov.iov_len = count;
404 dirbuf = malloc(count, M_TEMP, M_WAITOK);
405 kiov.iov_base = dirbuf;
406 UIO_SETUP_SYSSPACE(&kuio);
407 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
408 (off_t **)0, (int *)0);
409 fp->f_offset = kuio.uio_offset;
410 if (error == 0) {
411 readcnt = count - kuio.uio_resid;
412 edp = (struct dirent *)&dirbuf[readcnt];
413 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
414 # if (BYTE_ORDER == LITTLE_ENDIAN)
415 /*
416 * The expected low byte of
417 * dp->d_namlen is our dp->d_type.
418 * The high MBZ byte of dp->d_namlen
419 * is our dp->d_namlen.
420 */
421 dp->d_type = dp->d_namlen;
422 dp->d_namlen = 0;
423 # else
424 /*
425 * The dp->d_type is the high byte
426 * of the expected dp->d_namlen,
427 * so must be zero'ed.
428 */
429 dp->d_type = 0;
430 # endif
431 if (dp->d_reclen > 0) {
432 dp = (struct dirent *)
433 ((char *)dp + dp->d_reclen);
434 } else {
435 error = EIO;
436 break;
437 }
438 }
439 if (dp >= edp)
440 error = uiomove(dirbuf, readcnt, &auio);
441 }
442 free(dirbuf, M_TEMP);
443 }
444 VOP_UNLOCK(vp, 0);
445 if (error)
446 goto out;
447
448 #ifdef UNION
449 {
450 extern int (**union_vnodeop_p) __P((void *));
451 extern struct vnode *union_dircache __P((struct vnode *));
452
453 if ((count == auio.uio_resid) &&
454 (vp->v_op == union_vnodeop_p)) {
455 struct vnode *lvp;
456
457 lvp = union_dircache(vp);
458 if (lvp != NULLVP) {
459 struct vattr va;
460
461 /*
462 * If the directory is opaque,
463 * then don't show lower entries
464 */
465 error = VOP_GETATTR(vp, &va, fp->f_cred, l);
466 if (va.va_flags & OPAQUE) {
467 vput(lvp);
468 lvp = NULL;
469 }
470 }
471
472 if (lvp != NULLVP) {
473 error = VOP_OPEN(lvp, FREAD, fp->f_cred, l);
474 VOP_UNLOCK(lvp, 0);
475
476 if (error) {
477 vrele(lvp);
478 goto out;
479 }
480 fp->f_data = (void *) lvp;
481 fp->f_offset = 0;
482 error = vn_close(vp, FREAD, fp->f_cred, l);
483 if (error)
484 goto out;
485 vp = lvp;
486 goto unionread;
487 }
488 }
489 }
490 #endif /* UNION */
491
492 if ((count == auio.uio_resid) &&
493 (vp->v_flag & VROOT) &&
494 (vp->v_mount->mnt_flag & MNT_UNION)) {
495 struct vnode *tvp = vp;
496 vp = vp->v_mount->mnt_vnodecovered;
497 VREF(vp);
498 fp->f_data = (void *) vp;
499 fp->f_offset = 0;
500 vrele(tvp);
501 goto unionread;
502 }
503 error = copyout((void *)&loff, (void *)SCARG(uap, basep),
504 sizeof(long));
505 *retval = count - auio.uio_resid;
506 out:
507 FILE_UNUSE(fp, l);
508 return (error);
509 }
510
511 /*
512 * sysctl helper routine for vfs.generic.conf lookups.
513 */
514 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
515 static int
516 sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
517 {
518 struct vfsconf vfc;
519 extern const char * const mountcompatnames[];
520 extern int nmountcompatnames;
521 struct sysctlnode node;
522 struct vfsops *vfsp;
523 u_int vfsnum;
524
525 if (namelen != 1)
526 return (ENOTDIR);
527 vfsnum = name[0];
528 if (vfsnum >= nmountcompatnames ||
529 mountcompatnames[vfsnum] == NULL)
530 return (EOPNOTSUPP);
531 vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
532 if (vfsp == NULL)
533 return (EOPNOTSUPP);
534
535 vfc.vfc_vfsops = vfsp;
536 strncpy(vfc.vfc_name, vfsp->vfs_name, MFSNAMELEN);
537 vfc.vfc_typenum = vfsnum;
538 vfc.vfc_refcount = vfsp->vfs_refcount;
539 vfc.vfc_flags = 0;
540 vfc.vfc_mountroot = vfsp->vfs_mountroot;
541 vfc.vfc_next = NULL;
542
543 node = *rnode;
544 node.sysctl_data = &vfc;
545 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
546 }
547
548 /*
549 * Top level filesystem related information gathering.
550 */
551 SYSCTL_SETUP(compat_sysctl_vfs_setup, "compat sysctl vfs subtree setup")
552 {
553 extern int nmountcompatnames;
554
555 sysctl_createv(clog, 0, NULL, NULL,
556 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
557 CTLTYPE_INT, "maxtypenum",
558 SYSCTL_DESCR("Highest valid filesystem type number"),
559 NULL, nmountcompatnames, NULL, 0,
560 CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
561 sysctl_createv(clog, 0, NULL, NULL,
562 CTLFLAG_PERMANENT,
563 CTLTYPE_STRUCT, "conf",
564 SYSCTL_DESCR("Filesystem configuration information"),
565 sysctl_vfs_generic_conf, 0, NULL,
566 sizeof(struct vfsconf),
567 CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
568 }
569 #endif
570