vfs_syscalls.c revision 1.44 1 /* $NetBSD: vfs_syscalls.c,v 1.44 1994/12/14 19:08:07 mycroft 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/uio.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56
57 #include <sys/syscallargs.h>
58
59 #include <vm/vm.h>
60 #include <sys/sysctl.h>
61
62 static int change_dir __P((struct nameidata *ndp, struct proc *p));
63
64 /*
65 * Virtual File System System Calls
66 */
67
68 /*
69 * Mount a file system.
70 */
71 /* ARGSUSED */
72 mount(p, uap, retval)
73 struct proc *p;
74 register struct mount_args /* {
75 syscallarg(char *) type;
76 syscallarg(char *) path;
77 syscallarg(int) flags;
78 syscallarg(caddr_t) data;
79 } */ *uap;
80 register_t *retval;
81 {
82 register struct vnode *vp;
83 register struct mount *mp;
84 int error, flag;
85 u_long fsindex;
86 char fstypename[MFSNAMELEN];
87 struct vattr va;
88 struct nameidata nd;
89
90 /*
91 * Get vnode to be covered
92 */
93 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
94 SCARG(uap, path), p);
95 if (error = namei(&nd))
96 return (error);
97 vp = nd.ni_vp;
98 if (SCARG(uap, flags) & MNT_UPDATE) {
99 if ((vp->v_flag & VROOT) == 0) {
100 vput(vp);
101 return (EINVAL);
102 }
103 mp = vp->v_mount;
104 flag = mp->mnt_flag;
105 /*
106 * We only allow the filesystem to be reloaded if it
107 * is currently mounted read-only.
108 */
109 if ((SCARG(uap, flags) & MNT_RELOAD) &&
110 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
111 vput(vp);
112 return (EOPNOTSUPP); /* Needs translation */
113 }
114 mp->mnt_flag |=
115 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
116 /*
117 * Only root, or the user that did the original mount is
118 * permitted to update it.
119 */
120 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
121 (error = suser(p->p_ucred, &p->p_acflag))) {
122 vput(vp);
123 return (error);
124 }
125 /*
126 * Do not allow NFS export by non-root users. Silently
127 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
128 */
129 if (p->p_ucred->cr_uid != 0) {
130 if (SCARG(uap, flags) & MNT_EXPORTED) {
131 vput(vp);
132 return (EPERM);
133 }
134 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
135 }
136 VOP_UNLOCK(vp);
137 goto update;
138 }
139 /*
140 * If the user is not root, ensure that they own the directory
141 * onto which we are attempting to mount.
142 */
143 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
144 (va.va_uid != p->p_ucred->cr_uid &&
145 (error = suser(p->p_ucred, &p->p_acflag)))) {
146 vput(vp);
147 return (error);
148 }
149 /*
150 * Do not allow NFS export by non-root users. Silently
151 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
152 */
153 if (p->p_ucred->cr_uid != 0) {
154 if (SCARG(uap, flags) & MNT_EXPORTED) {
155 vput(vp);
156 return (EPERM);
157 }
158 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
159 }
160 if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))
161 return (error);
162 if (vp->v_type != VDIR) {
163 vput(vp);
164 return (ENOTDIR);
165 }
166 if (error = copyin(SCARG(uap, type), fstypename, MFSNAMELEN)) {
167 #ifdef COMPAT_09
168 goto check_num;
169 #else
170 vput(vp);
171 return (error);
172 #endif
173 }
174 fstypename[MFSNAMELEN] = '\0';
175 for (fsindex = 0; fsindex < nvfssw; fsindex++)
176 if (vfssw[fsindex] != NULL &&
177 !strncmp(vfssw[fsindex]->vfs_name, fstypename, MFSNAMELEN))
178 break;
179 if (fsindex >= nvfssw) {
180 #ifdef COMPAT_09
181 check_num:
182 fsindex = (u_long)SCARG(uap, type);
183 if (fsindex >= nvfssw || vfssw[fsindex] == NULL) {
184 vput(vp);
185 return (ENODEV);
186 }
187 #else
188 vput(vp);
189 return (ENODEV);
190 #endif
191 }
192 if (vp->v_mountedhere != NULL) {
193 vput(vp);
194 return (EBUSY);
195 }
196
197 /*
198 * Allocate and initialize the file system.
199 */
200 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
201 M_MOUNT, M_WAITOK);
202 bzero((char *)mp, (u_long)sizeof(struct mount));
203 mp->mnt_op = vfssw[fsindex];
204 if (error = vfs_lock(mp)) {
205 free((caddr_t)mp, M_MOUNT);
206 vput(vp);
207 return (error);
208 }
209 /* Do this early in case we block later. */
210 vfssw[fsindex]->vfs_refcount++;
211 vp->v_mountedhere = mp;
212 mp->mnt_vnodecovered = vp;
213 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
214 update:
215 /*
216 * Set the mount level flags.
217 */
218 if (SCARG(uap, flags) & MNT_RDONLY)
219 mp->mnt_flag |= MNT_RDONLY;
220 else if (mp->mnt_flag & MNT_RDONLY)
221 mp->mnt_flag |= MNT_WANTRDWR;
222 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
223 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
224 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
225 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
226 /*
227 * Mount the filesystem.
228 */
229 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
230 if (mp->mnt_flag & MNT_UPDATE) {
231 vrele(vp);
232 if (mp->mnt_flag & MNT_WANTRDWR)
233 mp->mnt_flag &= ~MNT_RDONLY;
234 mp->mnt_flag &=~
235 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
236 if (error)
237 mp->mnt_flag = flag;
238 return (error);
239 }
240 /*
241 * Put the new filesystem on the mount list after root.
242 */
243 cache_purge(vp);
244 if (!error) {
245 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
246 checkdirs(vp);
247 VOP_UNLOCK(vp);
248 vfs_unlock(mp);
249 error = VFS_START(mp, 0, p);
250 } else {
251 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
252 vfssw[fsindex]->vfs_refcount--;
253 vfs_unlock(mp);
254 free((caddr_t)mp, M_MOUNT);
255 vput(vp);
256 }
257 return (error);
258 }
259
260 /*
261 * Scan all active processes to see if any of them have a current
262 * or root directory onto which the new filesystem has just been
263 * mounted. If so, replace them with the new mount point.
264 */
265 checkdirs(olddp)
266 struct vnode *olddp;
267 {
268 struct filedesc *fdp;
269 struct vnode *newdp;
270 struct proc *p;
271
272 if (olddp->v_usecount == 1)
273 return;
274 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
275 panic("mount: lost mount");
276 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
277 fdp = p->p_fd;
278 if (fdp->fd_cdir == olddp) {
279 vrele(fdp->fd_cdir);
280 VREF(newdp);
281 fdp->fd_cdir = newdp;
282 }
283 if (fdp->fd_rdir == olddp) {
284 vrele(fdp->fd_rdir);
285 VREF(newdp);
286 fdp->fd_rdir = newdp;
287 }
288 }
289 if (rootvnode == olddp) {
290 vrele(rootvnode);
291 VREF(newdp);
292 rootvnode = newdp;
293 }
294 vput(newdp);
295 }
296
297 /*
298 * Unmount a file system.
299 *
300 * Note: unmount takes a path to the vnode mounted on as argument,
301 * not special file (as before).
302 */
303 /* ARGSUSED */
304 unmount(p, uap, retval)
305 struct proc *p;
306 register struct unmount_args /* {
307 syscallarg(char *) path;
308 syscallarg(int) flags;
309 } */ *uap;
310 register_t *retval;
311 {
312 register struct vnode *vp;
313 struct mount *mp;
314 int error;
315 struct nameidata nd;
316
317 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
318 SCARG(uap, path), p);
319 if (error = namei(&nd))
320 return (error);
321 vp = nd.ni_vp;
322 mp = vp->v_mount;
323
324 /*
325 * Only root, or the user that did the original mount is
326 * permitted to unmount this filesystem.
327 */
328 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
329 (error = suser(p->p_ucred, &p->p_acflag))) {
330 vput(vp);
331 return (error);
332 }
333
334 /*
335 * Must be the root of the filesystem
336 */
337 if ((vp->v_flag & VROOT) == 0) {
338 vput(vp);
339 return (EINVAL);
340 }
341 vput(vp);
342 return (dounmount(mp, SCARG(uap, flags), p));
343 }
344
345 /*
346 * Do the actual file system unmount.
347 */
348 dounmount(mp, flags, p)
349 register struct mount *mp;
350 int flags;
351 struct proc *p;
352 {
353 struct vnode *coveredvp;
354 int error;
355
356 coveredvp = mp->mnt_vnodecovered;
357 if (vfs_busy(mp))
358 return (EBUSY);
359 mp->mnt_flag |= MNT_UNMOUNT;
360 if (error = vfs_lock(mp))
361 return (error);
362
363 mp->mnt_flag &=~ MNT_ASYNC;
364 vnode_pager_umount(mp); /* release cached vnodes */
365 cache_purgevfs(mp); /* remove cache entries for this file sys */
366 if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
367 (flags & MNT_FORCE))
368 error = VFS_UNMOUNT(mp, flags, p);
369 mp->mnt_flag &= ~MNT_UNMOUNT;
370 vfs_unbusy(mp);
371 if (error) {
372 vfs_unlock(mp);
373 } else {
374 vrele(coveredvp);
375 TAILQ_REMOVE(&mountlist, mp, mnt_list);
376 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
377 mp->mnt_op->vfs_refcount--;
378 vfs_unlock(mp);
379 if (mp->mnt_vnodelist.lh_first != NULL)
380 panic("unmount: dangling vnode");
381 free((caddr_t)mp, M_MOUNT);
382 }
383 return (error);
384 }
385
386 /*
387 * Sync each mounted filesystem.
388 */
389 #ifdef DEBUG
390 int syncprt = 0;
391 struct ctldebug debug0 = { "syncprt", &syncprt };
392 #endif
393
394 /* ARGSUSED */
395 sync(p, uap, retval)
396 struct proc *p;
397 void *uap;
398 register_t *retval;
399 {
400 register struct mount *mp, *nmp;
401 int asyncflag;
402
403 for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
404 /*
405 * Get the next pointer in case we hang on vfs_busy
406 * while we are being unmounted.
407 */
408 nmp = mp->mnt_list.tqe_next;
409 /*
410 * The lock check below is to avoid races with mount
411 * and unmount.
412 */
413 if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
414 !vfs_busy(mp)) {
415 asyncflag = mp->mnt_flag & MNT_ASYNC;
416 mp->mnt_flag &= ~MNT_ASYNC;
417 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
418 if (asyncflag)
419 mp->mnt_flag |= MNT_ASYNC;
420 /*
421 * Get the next pointer again, as the next filesystem
422 * might have been unmounted while we were sync'ing.
423 */
424 nmp = mp->mnt_list.tqe_next;
425 vfs_unbusy(mp);
426 }
427 }
428 #ifdef DEBUG
429 if (syncprt)
430 vfs_bufstats();
431 #endif /* DEBUG */
432 return (0);
433 }
434
435 /*
436 * Change filesystem quotas.
437 */
438 /* ARGSUSED */
439 quotactl(p, uap, retval)
440 struct proc *p;
441 register struct quotactl_args /* {
442 syscallarg(char *) path;
443 syscallarg(int) cmd;
444 syscallarg(int) uid;
445 syscallarg(caddr_t) arg;
446 } */ *uap;
447 register_t *retval;
448 {
449 register struct mount *mp;
450 int error;
451 struct nameidata nd;
452
453 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
454 if (error = namei(&nd))
455 return (error);
456 mp = nd.ni_vp->v_mount;
457 vrele(nd.ni_vp);
458 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
459 SCARG(uap, arg), p));
460 }
461
462 /*
463 * Get filesystem statistics.
464 */
465 /* ARGSUSED */
466 statfs(p, uap, retval)
467 struct proc *p;
468 register struct statfs_args /* {
469 syscallarg(char *) path;
470 syscallarg(struct statfs *) buf;
471 } */ *uap;
472 register_t *retval;
473 {
474 register struct mount *mp;
475 register struct statfs *sp;
476 int error;
477 struct nameidata nd;
478
479 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
480 if (error = namei(&nd))
481 return (error);
482 mp = nd.ni_vp->v_mount;
483 sp = &mp->mnt_stat;
484 vrele(nd.ni_vp);
485 if (error = VFS_STATFS(mp, sp, p))
486 return (error);
487 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
488 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
489 }
490
491 /*
492 * Get filesystem statistics.
493 */
494 /* ARGSUSED */
495 fstatfs(p, uap, retval)
496 struct proc *p;
497 register struct fstatfs_args /* {
498 syscallarg(int) fd;
499 syscallarg(struct statfs *) buf;
500 } */ *uap;
501 register_t *retval;
502 {
503 struct file *fp;
504 struct mount *mp;
505 register struct statfs *sp;
506 int error;
507
508 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
509 return (error);
510 mp = ((struct vnode *)fp->f_data)->v_mount;
511 sp = &mp->mnt_stat;
512 if (error = VFS_STATFS(mp, sp, p))
513 return (error);
514 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
515 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
516 }
517
518 /*
519 * Get statistics on all filesystems.
520 */
521 getfsstat(p, uap, retval)
522 struct proc *p;
523 register struct getfsstat_args /* {
524 syscallarg(struct statfs *) buf;
525 syscallarg(long) bufsize;
526 syscallarg(int) flags;
527 } */ *uap;
528 register_t *retval;
529 {
530 register struct mount *mp, *nmp;
531 register struct statfs *sp;
532 caddr_t sfsp;
533 long count, maxcount, error;
534
535 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
536 sfsp = (caddr_t)SCARG(uap, buf);
537 for (count = 0, mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
538 nmp = mp->mnt_list.tqe_next;
539 if (sfsp && count < maxcount &&
540 ((mp->mnt_flag & MNT_MLOCK) == 0)) {
541 sp = &mp->mnt_stat;
542 /*
543 * If MNT_NOWAIT is specified, do not refresh the
544 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
545 */
546 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
547 (SCARG(uap, flags) & MNT_WAIT)) &&
548 (error = VFS_STATFS(mp, sp, p)))
549 continue;
550 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
551 if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))
552 return (error);
553 sfsp += sizeof(*sp);
554 }
555 count++;
556 }
557 if (sfsp && count > maxcount)
558 *retval = maxcount;
559 else
560 *retval = count;
561 return (0);
562 }
563
564 /*
565 * Change current working directory to a given file descriptor.
566 */
567 /* ARGSUSED */
568 fchdir(p, uap, retval)
569 struct proc *p;
570 struct fchdir_args /* {
571 syscallarg(int) fd;
572 } */ *uap;
573 register_t *retval;
574 {
575 register struct filedesc *fdp = p->p_fd;
576 struct vnode *vp, *tdp;
577 struct mount *mp;
578 struct file *fp;
579 int error;
580
581 if (error = getvnode(fdp, SCARG(uap, fd), &fp))
582 return (error);
583 vp = (struct vnode *)fp->f_data;
584 VREF(vp);
585 VOP_LOCK(vp);
586 if (vp->v_type != VDIR)
587 error = ENOTDIR;
588 else
589 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
590 while (!error && (mp = vp->v_mountedhere) != NULL) {
591 if (mp->mnt_flag & MNT_MLOCK) {
592 mp->mnt_flag |= MNT_MWAIT;
593 sleep((caddr_t)mp, PVFS);
594 continue;
595 }
596 if (error = VFS_ROOT(mp, &tdp))
597 break;
598 vput(vp);
599 vp = tdp;
600 }
601 VOP_UNLOCK(vp);
602 if (error) {
603 vrele(vp);
604 return (error);
605 }
606 vrele(fdp->fd_cdir);
607 fdp->fd_cdir = vp;
608 return (0);
609 }
610
611 /*
612 * Change current working directory (``.'').
613 */
614 /* ARGSUSED */
615 chdir(p, uap, retval)
616 struct proc *p;
617 struct chdir_args /* {
618 syscallarg(char *) path;
619 } */ *uap;
620 register_t *retval;
621 {
622 register struct filedesc *fdp = p->p_fd;
623 int error;
624 struct nameidata nd;
625
626 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
627 SCARG(uap, path), p);
628 if (error = change_dir(&nd, p))
629 return (error);
630 vrele(fdp->fd_cdir);
631 fdp->fd_cdir = nd.ni_vp;
632 return (0);
633 }
634
635 /*
636 * Change notion of root (``/'') directory.
637 */
638 /* ARGSUSED */
639 chroot(p, uap, retval)
640 struct proc *p;
641 struct chroot_args /* {
642 syscallarg(char *) path;
643 } */ *uap;
644 register_t *retval;
645 {
646 register struct filedesc *fdp = p->p_fd;
647 int error;
648 struct nameidata nd;
649
650 if (error = suser(p->p_ucred, &p->p_acflag))
651 return (error);
652 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
653 SCARG(uap, path), p);
654 if (error = change_dir(&nd, p))
655 return (error);
656 if (fdp->fd_rdir != NULL)
657 vrele(fdp->fd_rdir);
658 fdp->fd_rdir = nd.ni_vp;
659 return (0);
660 }
661
662 /*
663 * Common routine for chroot and chdir.
664 */
665 static int
666 change_dir(ndp, p)
667 register struct nameidata *ndp;
668 struct proc *p;
669 {
670 struct vnode *vp;
671 int error;
672
673 if (error = namei(ndp))
674 return (error);
675 vp = ndp->ni_vp;
676 if (vp->v_type != VDIR)
677 error = ENOTDIR;
678 else
679 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
680 VOP_UNLOCK(vp);
681 if (error)
682 vrele(vp);
683 return (error);
684 }
685
686 /*
687 * Check permissions, allocate an open file structure,
688 * and call the device open routine if any.
689 */
690 open(p, uap, retval)
691 struct proc *p;
692 register struct open_args /* {
693 syscallarg(char *) path;
694 syscallarg(int) flags;
695 syscallarg(int) mode;
696 } */ *uap;
697 register_t *retval;
698 {
699 register struct filedesc *fdp = p->p_fd;
700 register struct file *fp;
701 register struct vnode *vp;
702 int flags, cmode;
703 struct file *nfp;
704 int type, indx, error;
705 struct flock lf;
706 struct nameidata nd;
707 extern struct fileops vnops;
708
709 if (error = falloc(p, &nfp, &indx))
710 return (error);
711 fp = nfp;
712 flags = FFLAGS(SCARG(uap, flags));
713 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
714 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
715 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
716 if (error = vn_open(&nd, flags, cmode)) {
717 ffree(fp);
718 switch (error) {
719 case ENODEV: /* XXX from fdopen or fdesc_open? */
720 case ENXIO: /* XXX from portal_open? */
721 if (p->p_dupfd >= 0 && (error =
722 dupfdopen(fdp, indx, p->p_dupfd, flags, error))) {
723 *retval = indx;
724 return (0);
725 }
726 break;
727
728 case EJUSTRETURN: /* XXX cloning device? */
729 if (p->p_dupfd >= 0) {
730 *retval = indx;
731 return (0);
732 }
733 break;
734
735 case ERESTART:
736 error = EINTR;
737 break;
738 }
739 fdp->fd_ofiles[indx] = NULL;
740 return (error);
741 }
742 p->p_dupfd = 0;
743 vp = nd.ni_vp;
744 fp->f_flag = flags & FMASK;
745 fp->f_type = DTYPE_VNODE;
746 fp->f_ops = &vnops;
747 fp->f_data = (caddr_t)vp;
748 if (flags & (O_EXLOCK | O_SHLOCK)) {
749 lf.l_whence = SEEK_SET;
750 lf.l_start = 0;
751 lf.l_len = 0;
752 if (flags & O_EXLOCK)
753 lf.l_type = F_WRLCK;
754 else
755 lf.l_type = F_RDLCK;
756 type = F_FLOCK;
757 if ((flags & FNONBLOCK) == 0)
758 type |= F_WAIT;
759 VOP_UNLOCK(vp);
760 if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
761 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
762 ffree(fp);
763 fdp->fd_ofiles[indx] = NULL;
764 return (error);
765 }
766 VOP_LOCK(vp);
767 fp->f_flag |= FHASLOCK;
768 }
769 VOP_UNLOCK(vp);
770 *retval = indx;
771 return (0);
772 }
773
774 #ifdef COMPAT_43
775 /*
776 * Create a file.
777 */
778 compat_43_creat(p, uap, retval)
779 struct proc *p;
780 register struct compat_43_creat_args /* {
781 syscallarg(char *) path;
782 syscallarg(int) mode;
783 } */ *uap;
784 register_t *retval;
785 {
786 struct open_args /* {
787 syscallarg(char *) path;
788 syscallarg(int) flags;
789 syscallarg(int) mode;
790 } */ nuap;
791
792 SCARG(&nuap, path) = SCARG(uap, path);
793 SCARG(&nuap, mode) = SCARG(uap, mode);
794 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
795 return (open(p, &nuap, retval));
796 }
797 #endif /* COMPAT_43 */
798
799 /*
800 * Create a special file.
801 */
802 /* ARGSUSED */
803 mknod(p, uap, retval)
804 struct proc *p;
805 register struct mknod_args /* {
806 syscallarg(char *) path;
807 syscallarg(int) mode;
808 syscallarg(int) dev;
809 } */ *uap;
810 register_t *retval;
811 {
812 register struct vnode *vp;
813 struct vattr vattr;
814 int error;
815 int whiteout;
816 struct nameidata nd;
817
818 if (error = suser(p->p_ucred, &p->p_acflag))
819 return (error);
820 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
821 if (error = namei(&nd))
822 return (error);
823 vp = nd.ni_vp;
824 if (vp != NULL)
825 error = EEXIST;
826 else {
827 VATTR_NULL(&vattr);
828 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
829 vattr.va_rdev = SCARG(uap, dev);
830 whiteout = 0;
831
832 switch (SCARG(uap, mode) & S_IFMT) {
833 case S_IFMT: /* used by badsect to flag bad sectors */
834 vattr.va_type = VBAD;
835 break;
836 case S_IFCHR:
837 vattr.va_type = VCHR;
838 break;
839 case S_IFBLK:
840 vattr.va_type = VBLK;
841 break;
842 case S_IFWHT:
843 whiteout = 1;
844 break;
845 default:
846 error = EINVAL;
847 break;
848 }
849 }
850 if (!error) {
851 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
852 if (whiteout) {
853 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
854 if (error)
855 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
856 vput(nd.ni_dvp);
857 } else {
858 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
859 &nd.ni_cnd, &vattr);
860 }
861 } else {
862 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
863 if (nd.ni_dvp == vp)
864 vrele(nd.ni_dvp);
865 else
866 vput(nd.ni_dvp);
867 if (vp)
868 vrele(vp);
869 }
870 return (error);
871 }
872
873 /*
874 * Create a named pipe.
875 */
876 /* ARGSUSED */
877 mkfifo(p, uap, retval)
878 struct proc *p;
879 register struct mkfifo_args /* {
880 syscallarg(char *) path;
881 syscallarg(int) mode;
882 } */ *uap;
883 register_t *retval;
884 {
885 struct vattr vattr;
886 int error;
887 struct nameidata nd;
888
889 #ifndef FIFO
890 return (EOPNOTSUPP);
891 #else
892 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
893 if (error = namei(&nd))
894 return (error);
895 if (nd.ni_vp != NULL) {
896 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
897 if (nd.ni_dvp == nd.ni_vp)
898 vrele(nd.ni_dvp);
899 else
900 vput(nd.ni_dvp);
901 vrele(nd.ni_vp);
902 return (EEXIST);
903 }
904 VATTR_NULL(&vattr);
905 vattr.va_type = VFIFO;
906 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
907 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
908 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
909 #endif /* FIFO */
910 }
911
912 /*
913 * Make a hard file link.
914 */
915 /* ARGSUSED */
916 link(p, uap, retval)
917 struct proc *p;
918 register struct link_args /* {
919 syscallarg(char *) path;
920 syscallarg(char *) link;
921 } */ *uap;
922 register_t *retval;
923 {
924 register struct vnode *vp;
925 struct nameidata nd;
926 int error;
927
928 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
929 if (error = namei(&nd))
930 return (error);
931 vp = nd.ni_vp;
932 if (vp->v_type != VDIR ||
933 (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
934 nd.ni_cnd.cn_nameiop = CREATE;
935 nd.ni_cnd.cn_flags = LOCKPARENT;
936 nd.ni_dirp = SCARG(uap, link);
937 if ((error = namei(&nd)) == 0) {
938 if (nd.ni_vp != NULL)
939 error = EEXIST;
940 if (!error) {
941 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
942 LEASE_WRITE);
943 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
944 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
945 } else {
946 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
947 if (nd.ni_dvp == nd.ni_vp)
948 vrele(nd.ni_dvp);
949 else
950 vput(nd.ni_dvp);
951 if (nd.ni_vp)
952 vrele(nd.ni_vp);
953 }
954 }
955 }
956 vrele(vp);
957 return (error);
958 }
959
960 /*
961 * Make a symbolic link.
962 */
963 /* ARGSUSED */
964 symlink(p, uap, retval)
965 struct proc *p;
966 register struct symlink_args /* {
967 syscallarg(char *) path;
968 syscallarg(char *) link;
969 } */ *uap;
970 register_t *retval;
971 {
972 struct vattr vattr;
973 char *path;
974 int error;
975 struct nameidata nd;
976
977 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
978 if (error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, (u_int*)0))
979 goto out;
980 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
981 if (error = namei(&nd))
982 goto out;
983 if (nd.ni_vp) {
984 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
985 if (nd.ni_dvp == nd.ni_vp)
986 vrele(nd.ni_dvp);
987 else
988 vput(nd.ni_dvp);
989 vrele(nd.ni_vp);
990 error = EEXIST;
991 goto out;
992 }
993 VATTR_NULL(&vattr);
994 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
995 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
996 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
997 out:
998 FREE(path, M_NAMEI);
999 return (error);
1000 }
1001
1002 /*
1003 * Delete a whiteout from the filesystem.
1004 */
1005 /* ARGSUSED */
1006 undelete(p, uap, retval)
1007 struct proc *p;
1008 register struct undelete_args /* {
1009 syscallarg(char *) path;
1010 } */ *uap;
1011 register_t *retval;
1012 {
1013 int error;
1014 struct nameidata nd;
1015
1016 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1017 SCARG(uap, path), p);
1018 error = namei(&nd);
1019 if (error)
1020 return (error);
1021
1022 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1023 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1024 if (nd.ni_dvp == nd.ni_vp)
1025 vrele(nd.ni_dvp);
1026 else
1027 vput(nd.ni_dvp);
1028 if (nd.ni_vp)
1029 vrele(nd.ni_vp);
1030 return (EEXIST);
1031 }
1032
1033 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1034 if (error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE))
1035 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1036 vput(nd.ni_dvp);
1037 return (error);
1038 }
1039
1040 /*
1041 * Delete a name from the filesystem.
1042 */
1043 /* ARGSUSED */
1044 unlink(p, uap, retval)
1045 struct proc *p;
1046 struct unlink_args /* {
1047 syscallarg(char *) path;
1048 } */ *uap;
1049 register_t *retval;
1050 {
1051 register struct vnode *vp;
1052 int error;
1053 struct nameidata nd;
1054
1055 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1056 if (error = namei(&nd))
1057 return (error);
1058 vp = nd.ni_vp;
1059 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1060 VOP_LOCK(vp);
1061
1062 if (vp->v_type != VDIR ||
1063 (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
1064 /*
1065 * The root of a mounted filesystem cannot be deleted.
1066 */
1067 if (vp->v_flag & VROOT)
1068 error = EBUSY;
1069 else
1070 (void)vnode_pager_uncache(vp);
1071 }
1072
1073 if (!error) {
1074 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1075 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1076 } else {
1077 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1078 if (nd.ni_dvp == vp)
1079 vrele(nd.ni_dvp);
1080 else
1081 vput(nd.ni_dvp);
1082 if (vp != NULLVP)
1083 vput(vp);
1084 }
1085 return (error);
1086 }
1087
1088 /*
1089 * Reposition read/write file offset.
1090 */
1091 lseek(p, uap, retval)
1092 struct proc *p;
1093 register struct lseek_args /* {
1094 syscallarg(int) fd;
1095 syscallarg(int) pad;
1096 syscallarg(off_t) offset;
1097 syscallarg(int) whence;
1098 } */ *uap;
1099 register_t *retval;
1100 {
1101 struct ucred *cred = p->p_ucred;
1102 register struct filedesc *fdp = p->p_fd;
1103 register struct file *fp;
1104 struct vattr vattr;
1105 int error;
1106
1107 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1108 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1109 return (EBADF);
1110 if (fp->f_type != DTYPE_VNODE)
1111 return (ESPIPE);
1112 switch (SCARG(uap, whence)) {
1113 case L_INCR:
1114 fp->f_offset += SCARG(uap, offset);
1115 break;
1116 case L_XTND:
1117 if (error =
1118 VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p))
1119 return (error);
1120 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1121 break;
1122 case L_SET:
1123 fp->f_offset = SCARG(uap, offset);
1124 break;
1125 default:
1126 return (EINVAL);
1127 }
1128 *(off_t *)retval = fp->f_offset;
1129 return (0);
1130 }
1131
1132 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4)
1133 /*
1134 * Reposition read/write file offset.
1135 */
1136 compat_43_lseek(p, uap, retval)
1137 struct proc *p;
1138 register struct compat_43_lseek_args /* {
1139 syscallarg(int) fd;
1140 syscallarg(long) offset;
1141 syscallarg(int) whence;
1142 } */ *uap;
1143 register_t *retval;
1144 {
1145 struct lseek_args /* {
1146 syscallarg(int) fd;
1147 syscallarg(int) pad;
1148 syscallarg(off_t) offset;
1149 syscallarg(int) whence;
1150 } */ nuap;
1151 off_t qret;
1152 int error;
1153
1154 SCARG(&nuap, fd) = SCARG(uap, fd);
1155 SCARG(&nuap, offset) = SCARG(uap, offset);
1156 SCARG(&nuap, whence) = SCARG(uap, whence);
1157 error = lseek(p, &nuap, &qret);
1158 *(long *)retval = qret;
1159 return (error);
1160 }
1161 #endif /* COMPAT_43 || COMPAT_SUNOS */
1162
1163 /*
1164 * Check access permissions.
1165 */
1166 access(p, uap, retval)
1167 struct proc *p;
1168 register struct access_args /* {
1169 syscallarg(char *) path;
1170 syscallarg(int) flags;
1171 } */ *uap;
1172 register_t *retval;
1173 {
1174 register struct ucred *cred = p->p_ucred;
1175 register struct vnode *vp;
1176 int error, flags, t_gid, t_uid;
1177 struct nameidata nd;
1178
1179 t_uid = cred->cr_uid;
1180 t_gid = cred->cr_groups[0];
1181 cred->cr_uid = p->p_cred->p_ruid;
1182 cred->cr_groups[0] = p->p_cred->p_rgid;
1183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1184 SCARG(uap, path), p);
1185 if (error = namei(&nd))
1186 goto out1;
1187 vp = nd.ni_vp;
1188
1189 /* Flags == 0 means only check for existence. */
1190 if (SCARG(uap, flags)) {
1191 flags = 0;
1192 if (SCARG(uap, flags) & R_OK)
1193 flags |= VREAD;
1194 if (SCARG(uap, flags) & W_OK)
1195 flags |= VWRITE;
1196 if (SCARG(uap, flags) & X_OK)
1197 flags |= VEXEC;
1198 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1199 error = VOP_ACCESS(vp, flags, cred, p);
1200 }
1201 vput(vp);
1202 out1:
1203 cred->cr_uid = t_uid;
1204 cred->cr_groups[0] = t_gid;
1205 return (error);
1206 }
1207
1208 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2)
1209 /*
1210 * Get file status; this version follows links.
1211 */
1212 /* ARGSUSED */
1213 compat_43_stat(p, uap, retval)
1214 struct proc *p;
1215 register struct compat_43_stat_args /* {
1216 syscallarg(char *) path;
1217 syscallarg(struct ostat *) ub;
1218 } */ *uap;
1219 register_t *retval;
1220 {
1221 struct stat sb;
1222 struct ostat osb;
1223 int error;
1224 struct nameidata nd;
1225
1226 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1227 SCARG(uap, path), p);
1228 if (error = namei(&nd))
1229 return (error);
1230 error = vn_stat(nd.ni_vp, &sb, p);
1231 vput(nd.ni_vp);
1232 if (error)
1233 return (error);
1234 cvtstat(&sb, &osb);
1235 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1236 return (error);
1237 }
1238
1239 /*
1240 * Get file status; this version does not follow links.
1241 */
1242 /* ARGSUSED */
1243 compat_43_lstat(p, uap, retval)
1244 struct proc *p;
1245 register struct compat_43_lstat_args /* {
1246 syscallarg(char *) path;
1247 syscallarg(struct ostat *) ub;
1248 } */ *uap;
1249 register_t *retval;
1250 {
1251 struct vnode *vp, *dvp;
1252 struct stat sb, sb1;
1253 struct ostat osb;
1254 int error;
1255 struct nameidata nd;
1256
1257 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1258 SCARG(uap, path), p);
1259 if (error = namei(&nd))
1260 return (error);
1261 /*
1262 * For symbolic links, always return the attributes of its
1263 * containing directory, except for mode, size, and links.
1264 */
1265 vp = nd.ni_vp;
1266 dvp = nd.ni_dvp;
1267 if (vp->v_type != VLNK) {
1268 if (dvp == vp)
1269 vrele(dvp);
1270 else
1271 vput(dvp);
1272 error = vn_stat(vp, &sb, p);
1273 vput(vp);
1274 if (error)
1275 return (error);
1276 } else {
1277 error = vn_stat(dvp, &sb, p);
1278 vput(dvp);
1279 if (error) {
1280 vput(vp);
1281 return (error);
1282 }
1283 error = vn_stat(vp, &sb1, p);
1284 vput(vp);
1285 if (error)
1286 return (error);
1287 sb.st_mode &= ~S_IFDIR;
1288 sb.st_mode |= S_IFLNK;
1289 sb.st_nlink = sb1.st_nlink;
1290 sb.st_size = sb1.st_size;
1291 sb.st_blocks = sb1.st_blocks;
1292 }
1293 cvtstat(&sb, &osb);
1294 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1295 return (error);
1296 }
1297
1298 /*
1299 * Convert from an old to a new stat structure.
1300 */
1301 cvtstat(st, ost)
1302 struct stat *st;
1303 struct ostat *ost;
1304 {
1305
1306 ost->st_dev = st->st_dev;
1307 ost->st_ino = st->st_ino;
1308 ost->st_mode = st->st_mode;
1309 ost->st_nlink = st->st_nlink;
1310 ost->st_uid = st->st_uid;
1311 ost->st_gid = st->st_gid;
1312 ost->st_rdev = st->st_rdev;
1313 if (st->st_size < (quad_t)1 << 32)
1314 ost->st_size = st->st_size;
1315 else
1316 ost->st_size = -2;
1317 ost->st_atime = st->st_atime;
1318 ost->st_mtime = st->st_mtime;
1319 ost->st_ctime = st->st_ctime;
1320 ost->st_blksize = st->st_blksize;
1321 ost->st_blocks = st->st_blocks;
1322 ost->st_flags = st->st_flags;
1323 ost->st_gen = st->st_gen;
1324 }
1325 #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_IBCS2 */
1326
1327 /*
1328 * Get file status; this version follows links.
1329 */
1330 /* ARGSUSED */
1331 stat(p, uap, retval)
1332 struct proc *p;
1333 register struct stat_args /* {
1334 syscallarg(char *) path;
1335 syscallarg(struct stat *) ub;
1336 } */ *uap;
1337 register_t *retval;
1338 {
1339 struct stat sb;
1340 int error;
1341 struct nameidata nd;
1342
1343 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1344 SCARG(uap, path), p);
1345 if (error = namei(&nd))
1346 return (error);
1347 error = vn_stat(nd.ni_vp, &sb, p);
1348 vput(nd.ni_vp);
1349 if (error)
1350 return (error);
1351 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1352 return (error);
1353 }
1354
1355 /*
1356 * Get file status; this version does not follow links.
1357 */
1358 /* ARGSUSED */
1359 lstat(p, uap, retval)
1360 struct proc *p;
1361 register struct lstat_args /* {
1362 syscallarg(char *) path;
1363 syscallarg(struct stat *) ub;
1364 } */ *uap;
1365 register_t *retval;
1366 {
1367 int error;
1368 struct vnode *vp, *dvp;
1369 struct stat sb, sb1;
1370 struct nameidata nd;
1371
1372 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1373 SCARG(uap, path), p);
1374 if (error = namei(&nd))
1375 return (error);
1376 /*
1377 * For symbolic links, always return the attributes of its
1378 * containing directory, except for mode, size, and links.
1379 */
1380 vp = nd.ni_vp;
1381 dvp = nd.ni_dvp;
1382 if (vp->v_type != VLNK) {
1383 if (dvp == vp)
1384 vrele(dvp);
1385 else
1386 vput(dvp);
1387 error = vn_stat(vp, &sb, p);
1388 vput(vp);
1389 if (error)
1390 return (error);
1391 } else {
1392 error = vn_stat(dvp, &sb, p);
1393 vput(dvp);
1394 if (error) {
1395 vput(vp);
1396 return (error);
1397 }
1398 error = vn_stat(vp, &sb1, p);
1399 vput(vp);
1400 if (error)
1401 return (error);
1402 sb.st_mode &= ~S_IFDIR;
1403 sb.st_mode |= S_IFLNK;
1404 sb.st_nlink = sb1.st_nlink;
1405 sb.st_size = sb1.st_size;
1406 sb.st_blocks = sb1.st_blocks;
1407 }
1408 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1409 return (error);
1410 }
1411
1412 /*
1413 * Get configurable pathname variables.
1414 */
1415 /* ARGSUSED */
1416 pathconf(p, uap, retval)
1417 struct proc *p;
1418 register struct pathconf_args /* {
1419 syscallarg(char *) path;
1420 syscallarg(int) name;
1421 } */ *uap;
1422 register_t *retval;
1423 {
1424 int error;
1425 struct nameidata nd;
1426
1427 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1428 SCARG(uap, path), p);
1429 if (error = namei(&nd))
1430 return (error);
1431 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1432 vput(nd.ni_vp);
1433 return (error);
1434 }
1435
1436 /*
1437 * Return target name of a symbolic link.
1438 */
1439 /* ARGSUSED */
1440 readlink(p, uap, retval)
1441 struct proc *p;
1442 register struct readlink_args /* {
1443 syscallarg(char *) path;
1444 syscallarg(char *) buf;
1445 syscallarg(int) count;
1446 } */ *uap;
1447 register_t *retval;
1448 {
1449 register struct vnode *vp;
1450 struct iovec aiov;
1451 struct uio auio;
1452 int error;
1453 struct nameidata nd;
1454
1455 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1456 SCARG(uap, path), p);
1457 if (error = namei(&nd))
1458 return (error);
1459 vp = nd.ni_vp;
1460 if (vp->v_type != VLNK)
1461 error = EINVAL;
1462 else {
1463 aiov.iov_base = SCARG(uap, buf);
1464 aiov.iov_len = SCARG(uap, count);
1465 auio.uio_iov = &aiov;
1466 auio.uio_iovcnt = 1;
1467 auio.uio_offset = 0;
1468 auio.uio_rw = UIO_READ;
1469 auio.uio_segflg = UIO_USERSPACE;
1470 auio.uio_procp = p;
1471 auio.uio_resid = SCARG(uap, count);
1472 error = VOP_READLINK(vp, &auio, p->p_ucred);
1473 }
1474 vput(vp);
1475 *retval = SCARG(uap, count) - auio.uio_resid;
1476 return (error);
1477 }
1478
1479 /*
1480 * Change flags of a file given a path name.
1481 */
1482 /* ARGSUSED */
1483 chflags(p, uap, retval)
1484 struct proc *p;
1485 register struct chflags_args /* {
1486 syscallarg(char *) path;
1487 syscallarg(int) flags;
1488 } */ *uap;
1489 register_t *retval;
1490 {
1491 register struct vnode *vp;
1492 struct vattr vattr;
1493 int error;
1494 struct nameidata nd;
1495
1496 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1497 if (error = namei(&nd))
1498 return (error);
1499 vp = nd.ni_vp;
1500 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1501 VOP_LOCK(vp);
1502 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1503 error = EROFS;
1504 else {
1505 VATTR_NULL(&vattr);
1506 vattr.va_flags = SCARG(uap, flags);
1507 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1508 }
1509 vput(vp);
1510 return (error);
1511 }
1512
1513 /*
1514 * Change flags of a file given a file descriptor.
1515 */
1516 /* ARGSUSED */
1517 fchflags(p, uap, retval)
1518 struct proc *p;
1519 register struct fchflags_args /* {
1520 syscallarg(int) fd;
1521 syscallarg(int) flags;
1522 } */ *uap;
1523 register_t *retval;
1524 {
1525 struct vattr vattr;
1526 struct vnode *vp;
1527 struct file *fp;
1528 int error;
1529
1530 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1531 return (error);
1532 vp = (struct vnode *)fp->f_data;
1533 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1534 VOP_LOCK(vp);
1535 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1536 error = EROFS;
1537 else {
1538 VATTR_NULL(&vattr);
1539 vattr.va_flags = SCARG(uap, flags);
1540 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1541 }
1542 VOP_UNLOCK(vp);
1543 return (error);
1544 }
1545
1546 /*
1547 * Change mode of a file given path name.
1548 */
1549 /* ARGSUSED */
1550 chmod(p, uap, retval)
1551 struct proc *p;
1552 register struct chmod_args /* {
1553 syscallarg(char *) path;
1554 syscallarg(int) mode;
1555 } */ *uap;
1556 register_t *retval;
1557 {
1558 register struct vnode *vp;
1559 struct vattr vattr;
1560 int error;
1561 struct nameidata nd;
1562
1563 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1564 if (error = namei(&nd))
1565 return (error);
1566 vp = nd.ni_vp;
1567 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1568 VOP_LOCK(vp);
1569 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1570 error = EROFS;
1571 else {
1572 VATTR_NULL(&vattr);
1573 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1574 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1575 }
1576 vput(vp);
1577 return (error);
1578 }
1579
1580 /*
1581 * Change mode of a file given a file descriptor.
1582 */
1583 /* ARGSUSED */
1584 fchmod(p, uap, retval)
1585 struct proc *p;
1586 register struct fchmod_args /* {
1587 syscallarg(int) fd;
1588 syscallarg(int) mode;
1589 } */ *uap;
1590 register_t *retval;
1591 {
1592 struct vattr vattr;
1593 struct vnode *vp;
1594 struct file *fp;
1595 int error;
1596
1597 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1598 return (error);
1599 vp = (struct vnode *)fp->f_data;
1600 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1601 VOP_LOCK(vp);
1602 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1603 error = EROFS;
1604 else {
1605 VATTR_NULL(&vattr);
1606 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1607 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1608 }
1609 VOP_UNLOCK(vp);
1610 return (error);
1611 }
1612
1613 /*
1614 * Set ownership given a path name.
1615 */
1616 /* ARGSUSED */
1617 chown(p, uap, retval)
1618 struct proc *p;
1619 register struct chown_args /* {
1620 syscallarg(char *) path;
1621 syscallarg(int) uid;
1622 syscallarg(int) gid;
1623 } */ *uap;
1624 register_t *retval;
1625 {
1626 register struct vnode *vp;
1627 struct vattr vattr;
1628 int error;
1629 struct nameidata nd;
1630
1631 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1632 if (error = namei(&nd))
1633 return (error);
1634 vp = nd.ni_vp;
1635 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1636 VOP_LOCK(vp);
1637 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1638 error = EROFS;
1639 else {
1640 VATTR_NULL(&vattr);
1641 vattr.va_uid = SCARG(uap, uid);
1642 vattr.va_gid = SCARG(uap, gid);
1643 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1644 }
1645 vput(vp);
1646 return (error);
1647 }
1648
1649 /*
1650 * Set ownership given a file descriptor.
1651 */
1652 /* ARGSUSED */
1653 fchown(p, uap, retval)
1654 struct proc *p;
1655 register struct fchown_args /* {
1656 syscallarg(int) fd;
1657 syscallarg(int) uid;
1658 syscallarg(int) gid;
1659 } */ *uap;
1660 register_t *retval;
1661 {
1662 struct vattr vattr;
1663 struct vnode *vp;
1664 struct file *fp;
1665 int error;
1666
1667 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1668 return (error);
1669 vp = (struct vnode *)fp->f_data;
1670 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1671 VOP_LOCK(vp);
1672 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1673 error = EROFS;
1674 else {
1675 VATTR_NULL(&vattr);
1676 vattr.va_uid = SCARG(uap, uid);
1677 vattr.va_gid = SCARG(uap, gid);
1678 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1679 }
1680 VOP_UNLOCK(vp);
1681 return (error);
1682 }
1683
1684 /*
1685 * Set the access and modification times of a file.
1686 */
1687 /* ARGSUSED */
1688 utimes(p, uap, retval)
1689 struct proc *p;
1690 register struct utimes_args /* {
1691 syscallarg(char *) path;
1692 syscallarg(struct timeval *) tptr;
1693 } */ *uap;
1694 register_t *retval;
1695 {
1696 register struct vnode *vp;
1697 struct timeval tv[2];
1698 struct vattr vattr;
1699 int error;
1700 struct nameidata nd;
1701
1702 VATTR_NULL(&vattr);
1703 if (SCARG(uap, tptr) == NULL) {
1704 microtime(&tv[0]);
1705 tv[1] = tv[0];
1706 vattr.va_vaflags |= VA_UTIMES_NULL;
1707 } else if (error = copyin((caddr_t)SCARG(uap, tptr), (caddr_t)tv,
1708 sizeof (tv)))
1709 return (error);
1710 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1711 if (error = namei(&nd))
1712 return (error);
1713 vp = nd.ni_vp;
1714 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1715 VOP_LOCK(vp);
1716 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1717 error = EROFS;
1718 else {
1719 vattr.va_atime.ts_sec = tv[0].tv_sec;
1720 vattr.va_atime.ts_nsec = tv[0].tv_usec * 1000;
1721 vattr.va_mtime.ts_sec = tv[1].tv_sec;
1722 vattr.va_mtime.ts_nsec = tv[1].tv_usec * 1000;
1723 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1724 }
1725 vput(vp);
1726 return (error);
1727 }
1728
1729 /*
1730 * Truncate a file given its path name.
1731 */
1732 /* ARGSUSED */
1733 truncate(p, uap, retval)
1734 struct proc *p;
1735 register struct truncate_args /* {
1736 syscallarg(char *) path;
1737 syscallarg(int) pad;
1738 syscallarg(off_t) length;
1739 } */ *uap;
1740 register_t *retval;
1741 {
1742 register struct vnode *vp;
1743 struct vattr vattr;
1744 int error;
1745 struct nameidata nd;
1746
1747 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1748 if (error = namei(&nd))
1749 return (error);
1750 vp = nd.ni_vp;
1751 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1752 VOP_LOCK(vp);
1753 if (vp->v_type == VDIR)
1754 error = EISDIR;
1755 else if ((error = vn_writechk(vp)) == 0 &&
1756 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1757 VATTR_NULL(&vattr);
1758 vattr.va_size = SCARG(uap, length);
1759 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1760 }
1761 vput(vp);
1762 return (error);
1763 }
1764
1765 /*
1766 * Truncate a file given a file descriptor.
1767 */
1768 /* ARGSUSED */
1769 ftruncate(p, uap, retval)
1770 struct proc *p;
1771 register struct ftruncate_args /* {
1772 syscallarg(int) fd;
1773 syscallarg(int) pad;
1774 syscallarg(off_t) length;
1775 } */ *uap;
1776 register_t *retval;
1777 {
1778 struct vattr vattr;
1779 struct vnode *vp;
1780 struct file *fp;
1781 int error;
1782
1783 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1784 return (error);
1785 if ((fp->f_flag & FWRITE) == 0)
1786 return (EINVAL);
1787 vp = (struct vnode *)fp->f_data;
1788 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1789 VOP_LOCK(vp);
1790 if (vp->v_type == VDIR)
1791 error = EISDIR;
1792 else if ((error = vn_writechk(vp)) == 0) {
1793 VATTR_NULL(&vattr);
1794 vattr.va_size = SCARG(uap, length);
1795 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1796 }
1797 VOP_UNLOCK(vp);
1798 return (error);
1799 }
1800
1801 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1802 /*
1803 * Truncate a file given its path name.
1804 */
1805 /* ARGSUSED */
1806 compat_43_truncate(p, uap, retval)
1807 struct proc *p;
1808 register struct compat_43_truncate_args /* {
1809 syscallarg(char *) path;
1810 syscallarg(long) length;
1811 } */ *uap;
1812 register_t *retval;
1813 {
1814 struct truncate_args /* {
1815 syscallarg(char *) path;
1816 syscallarg(int) pad;
1817 syscallarg(off_t) length;
1818 } */ nuap;
1819
1820 SCARG(&nuap, path) = SCARG(uap, path);
1821 SCARG(&nuap, length) = SCARG(uap, length);
1822 return (truncate(p, &nuap, retval));
1823 }
1824
1825 /*
1826 * Truncate a file given a file descriptor.
1827 */
1828 /* ARGSUSED */
1829 compat_43_ftruncate(p, uap, retval)
1830 struct proc *p;
1831 register struct compat_43_ftruncate_args /* {
1832 syscallarg(int) fd;
1833 syscallarg(long) length;
1834 } */ *uap;
1835 register_t *retval;
1836 {
1837 struct ftruncate_args /* {
1838 syscallarg(int) fd;
1839 syscallarg(int) pad;
1840 syscallarg(off_t) length;
1841 } */ nuap;
1842
1843 SCARG(&nuap, fd) = SCARG(uap, fd);
1844 SCARG(&nuap, length) = SCARG(uap, length);
1845 return (ftruncate(p, &nuap, retval));
1846 }
1847 #endif /* COMPAT_43 || COMPAT_SUNOS */
1848
1849 /*
1850 * Sync an open file.
1851 */
1852 /* ARGSUSED */
1853 fsync(p, uap, retval)
1854 struct proc *p;
1855 struct fsync_args /* {
1856 syscallarg(int) fd;
1857 } */ *uap;
1858 register_t *retval;
1859 {
1860 register struct vnode *vp;
1861 struct file *fp;
1862 int error;
1863
1864 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1865 return (error);
1866 vp = (struct vnode *)fp->f_data;
1867 VOP_LOCK(vp);
1868 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1869 VOP_UNLOCK(vp);
1870 return (error);
1871 }
1872
1873 /*
1874 * Rename files. Source and destination must either both be directories,
1875 * or both not be directories. If target is a directory, it must be empty.
1876 */
1877 /* ARGSUSED */
1878 rename(p, uap, retval)
1879 struct proc *p;
1880 register struct rename_args /* {
1881 syscallarg(char *) from;
1882 syscallarg(char *) to;
1883 } */ *uap;
1884 register_t *retval;
1885 {
1886 register struct vnode *tvp, *fvp, *tdvp;
1887 struct nameidata fromnd, tond;
1888 int error;
1889
1890 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1891 SCARG(uap, from), p);
1892 if (error = namei(&fromnd))
1893 return (error);
1894 fvp = fromnd.ni_vp;
1895 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1896 UIO_USERSPACE, SCARG(uap, to), p);
1897 if (error = namei(&tond)) {
1898 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1899 vrele(fromnd.ni_dvp);
1900 vrele(fvp);
1901 goto out1;
1902 }
1903 tdvp = tond.ni_dvp;
1904 tvp = tond.ni_vp;
1905 if (tvp != NULL) {
1906 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1907 error = ENOTDIR;
1908 goto out;
1909 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1910 error = EISDIR;
1911 goto out;
1912 }
1913 }
1914 if (fvp == tdvp)
1915 error = EINVAL;
1916 /*
1917 * If source is the same as the destination (that is the
1918 * same inode number with the same name in the same directory),
1919 * then there is nothing to do.
1920 */
1921 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1922 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1923 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1924 fromnd.ni_cnd.cn_namelen))
1925 error = -1;
1926 out:
1927 if (!error) {
1928 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1929 if (fromnd.ni_dvp != tdvp)
1930 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1931 if (tvp)
1932 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1933 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1934 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1935 } else {
1936 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1937 if (tdvp == tvp)
1938 vrele(tdvp);
1939 else
1940 vput(tdvp);
1941 if (tvp)
1942 vput(tvp);
1943 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1944 vrele(fromnd.ni_dvp);
1945 vrele(fvp);
1946 }
1947 vrele(tond.ni_startdir);
1948 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1949 out1:
1950 if (fromnd.ni_startdir)
1951 vrele(fromnd.ni_startdir);
1952 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1953 if (error == -1)
1954 return (0);
1955 return (error);
1956 }
1957
1958 /*
1959 * Make a directory file.
1960 */
1961 /* ARGSUSED */
1962 mkdir(p, uap, retval)
1963 struct proc *p;
1964 register struct mkdir_args /* {
1965 syscallarg(char *) path;
1966 syscallarg(int) mode;
1967 } */ *uap;
1968 register_t *retval;
1969 {
1970 register struct vnode *vp;
1971 struct vattr vattr;
1972 int error;
1973 struct nameidata nd;
1974
1975 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1976 if (error = namei(&nd))
1977 return (error);
1978 vp = nd.ni_vp;
1979 if (vp != NULL) {
1980 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1981 if (nd.ni_dvp == vp)
1982 vrele(nd.ni_dvp);
1983 else
1984 vput(nd.ni_dvp);
1985 vrele(vp);
1986 return (EEXIST);
1987 }
1988 VATTR_NULL(&vattr);
1989 vattr.va_type = VDIR;
1990 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1991 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1992 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1993 if (!error)
1994 vput(nd.ni_vp);
1995 return (error);
1996 }
1997
1998 /*
1999 * Remove a directory file.
2000 */
2001 /* ARGSUSED */
2002 rmdir(p, uap, retval)
2003 struct proc *p;
2004 struct rmdir_args /* {
2005 syscallarg(char *) path;
2006 } */ *uap;
2007 register_t *retval;
2008 {
2009 register struct vnode *vp;
2010 int error;
2011 struct nameidata nd;
2012
2013 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2014 SCARG(uap, path), p);
2015 if (error = namei(&nd))
2016 return (error);
2017 vp = nd.ni_vp;
2018 if (vp->v_type != VDIR) {
2019 error = ENOTDIR;
2020 goto out;
2021 }
2022 /*
2023 * No rmdir "." please.
2024 */
2025 if (nd.ni_dvp == vp) {
2026 error = EINVAL;
2027 goto out;
2028 }
2029 /*
2030 * The root of a mounted filesystem cannot be deleted.
2031 */
2032 if (vp->v_flag & VROOT)
2033 error = EBUSY;
2034 out:
2035 if (!error) {
2036 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2037 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2038 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2039 } else {
2040 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2041 if (nd.ni_dvp == vp)
2042 vrele(nd.ni_dvp);
2043 else
2044 vput(nd.ni_dvp);
2045 vput(vp);
2046 }
2047 return (error);
2048 }
2049
2050 #if defined(COMPAT_43) || defined(COMPAT_HPUX)
2051 /*
2052 * Read a block of directory entries in a file system independent format.
2053 */
2054 compat_43_getdirentries(p, uap, retval)
2055 struct proc *p;
2056 register struct compat_43_getdirentries_args /* {
2057 syscallarg(int) fd;
2058 syscallarg(char *) buf;
2059 syscallarg(u_int) count;
2060 syscallarg(long *) basep;
2061 } */ *uap;
2062 register_t *retval;
2063 {
2064 register struct vnode *vp;
2065 struct file *fp;
2066 struct uio auio, kuio;
2067 struct iovec aiov, kiov;
2068 struct dirent *dp, *edp;
2069 caddr_t dirbuf;
2070 int error, eofflag, readcnt;
2071 long loff;
2072
2073 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2074 return (error);
2075 if ((fp->f_flag & FREAD) == 0)
2076 return (EBADF);
2077 vp = (struct vnode *)fp->f_data;
2078 unionread:
2079 if (vp->v_type != VDIR)
2080 return (EINVAL);
2081 aiov.iov_base = SCARG(uap, buf);
2082 aiov.iov_len = SCARG(uap, count);
2083 auio.uio_iov = &aiov;
2084 auio.uio_iovcnt = 1;
2085 auio.uio_rw = UIO_READ;
2086 auio.uio_segflg = UIO_USERSPACE;
2087 auio.uio_procp = p;
2088 auio.uio_resid = SCARG(uap, count);
2089 VOP_LOCK(vp);
2090 loff = auio.uio_offset = fp->f_offset;
2091 # if (BYTE_ORDER != LITTLE_ENDIAN)
2092 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2093 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2094 (u_long *)0, 0);
2095 fp->f_offset = auio.uio_offset;
2096 } else
2097 # endif
2098 {
2099 kuio = auio;
2100 kuio.uio_iov = &kiov;
2101 kuio.uio_segflg = UIO_SYSSPACE;
2102 kiov.iov_len = SCARG(uap, count);
2103 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2104 kiov.iov_base = dirbuf;
2105 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2106 (u_long *)0, 0);
2107 fp->f_offset = kuio.uio_offset;
2108 if (error == 0) {
2109 readcnt = SCARG(uap, count) - kuio.uio_resid;
2110 edp = (struct dirent *)&dirbuf[readcnt];
2111 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2112 # if (BYTE_ORDER == LITTLE_ENDIAN)
2113 /*
2114 * The expected low byte of
2115 * dp->d_namlen is our dp->d_type.
2116 * The high MBZ byte of dp->d_namlen
2117 * is our dp->d_namlen.
2118 */
2119 dp->d_type = dp->d_namlen;
2120 dp->d_namlen = 0;
2121 # else
2122 /*
2123 * The dp->d_type is the high byte
2124 * of the expected dp->d_namlen,
2125 * so must be zero'ed.
2126 */
2127 dp->d_type = 0;
2128 # endif
2129 if (dp->d_reclen > 0) {
2130 dp = (struct dirent *)
2131 ((char *)dp + dp->d_reclen);
2132 } else {
2133 error = EIO;
2134 break;
2135 }
2136 }
2137 if (dp >= edp)
2138 error = uiomove(dirbuf, readcnt, &auio);
2139 }
2140 FREE(dirbuf, M_TEMP);
2141 }
2142 VOP_UNLOCK(vp);
2143 if (error)
2144 return (error);
2145
2146 #ifdef UNION
2147 {
2148 extern int (**union_vnodeop_p)();
2149 extern struct vnode *union_dircache __P((struct vnode *));
2150
2151 if ((SCARG(uap, count) == auio.uio_resid) &&
2152 (vp->v_op == union_vnodeop_p)) {
2153 struct vnode *lvp;
2154
2155 lvp = union_dircache(vp);
2156 if (lvp != NULLVP) {
2157 struct vattr va;
2158
2159 /*
2160 * If the directory is opaque,
2161 * then don't show lower entries
2162 */
2163 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2164 if (va.va_flags & OPAQUE) {
2165 vput(lvp);
2166 lvp = NULL;
2167 }
2168 }
2169
2170 if (lvp != NULLVP) {
2171 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2172 VOP_UNLOCK(lvp);
2173
2174 if (error) {
2175 vrele(lvp);
2176 return (error);
2177 }
2178 fp->f_data = (caddr_t) lvp;
2179 fp->f_offset = 0;
2180 error = vn_close(vp, FREAD, fp->f_cred, p);
2181 if (error)
2182 return (error);
2183 vp = lvp;
2184 goto unionread;
2185 }
2186 }
2187 }
2188 #endif /* UNION */
2189
2190 if ((SCARG(uap, count) == auio.uio_resid) &&
2191 (vp->v_flag & VROOT) &&
2192 (vp->v_mount->mnt_flag & MNT_UNION)) {
2193 struct vnode *tvp = vp;
2194 vp = vp->v_mount->mnt_vnodecovered;
2195 VREF(vp);
2196 fp->f_data = (caddr_t) vp;
2197 fp->f_offset = 0;
2198 vrele(tvp);
2199 goto unionread;
2200 }
2201 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2202 sizeof(long));
2203 *retval = SCARG(uap, count) - auio.uio_resid;
2204 return (error);
2205 }
2206 #endif /* COMPAT_43 */
2207
2208 /*
2209 * Read a block of directory entries in a file system independent format.
2210 */
2211 getdirentries(p, uap, retval)
2212 struct proc *p;
2213 register struct getdirentries_args /* {
2214 syscallarg(int) fd;
2215 syscallarg(char *) buf;
2216 syscallarg(u_int) count;
2217 syscallarg(long *) basep;
2218 } */ *uap;
2219 register_t *retval;
2220 {
2221 register struct vnode *vp;
2222 struct file *fp;
2223 struct uio auio;
2224 struct iovec aiov;
2225 long loff;
2226 int error, eofflag;
2227
2228 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2229 return (error);
2230 if ((fp->f_flag & FREAD) == 0)
2231 return (EBADF);
2232 vp = (struct vnode *)fp->f_data;
2233 unionread:
2234 if (vp->v_type != VDIR)
2235 return (EINVAL);
2236 aiov.iov_base = SCARG(uap, buf);
2237 aiov.iov_len = SCARG(uap, count);
2238 auio.uio_iov = &aiov;
2239 auio.uio_iovcnt = 1;
2240 auio.uio_rw = UIO_READ;
2241 auio.uio_segflg = UIO_USERSPACE;
2242 auio.uio_procp = p;
2243 auio.uio_resid = SCARG(uap, count);
2244 VOP_LOCK(vp);
2245 loff = auio.uio_offset = fp->f_offset;
2246 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
2247 fp->f_offset = auio.uio_offset;
2248 VOP_UNLOCK(vp);
2249 if (error)
2250 return (error);
2251
2252 #ifdef UNION
2253 {
2254 extern int (**union_vnodeop_p)();
2255 extern struct vnode *union_dircache __P((struct vnode *));
2256
2257 if ((SCARG(uap, count) == auio.uio_resid) &&
2258 (vp->v_op == union_vnodeop_p)) {
2259 struct vnode *lvp;
2260
2261 lvp = union_dircache(vp);
2262 if (lvp != NULLVP) {
2263 struct vattr va;
2264
2265 /*
2266 * If the directory is opaque,
2267 * then don't show lower entries
2268 */
2269 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2270 if (va.va_flags & OPAQUE) {
2271 vput(lvp);
2272 lvp = NULL;
2273 }
2274 }
2275
2276 if (lvp != NULLVP) {
2277 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2278 VOP_UNLOCK(lvp);
2279
2280 if (error) {
2281 vrele(lvp);
2282 return (error);
2283 }
2284 fp->f_data = (caddr_t) lvp;
2285 fp->f_offset = 0;
2286 error = vn_close(vp, FREAD, fp->f_cred, p);
2287 if (error)
2288 return (error);
2289 vp = lvp;
2290 goto unionread;
2291 }
2292 }
2293 }
2294 #endif /* UNION */
2295
2296 if ((SCARG(uap, count) == auio.uio_resid) &&
2297 (vp->v_flag & VROOT) &&
2298 (vp->v_mount->mnt_flag & MNT_UNION)) {
2299 struct vnode *tvp = vp;
2300 vp = vp->v_mount->mnt_vnodecovered;
2301 VREF(vp);
2302 fp->f_data = (caddr_t) vp;
2303 fp->f_offset = 0;
2304 vrele(tvp);
2305 goto unionread;
2306 }
2307 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2308 sizeof(long));
2309 *retval = SCARG(uap, count) - auio.uio_resid;
2310 return (error);
2311 }
2312
2313 /*
2314 * Set the mode mask for creation of filesystem nodes.
2315 */
2316 mode_t /* XXX */
2317 umask(p, uap, retval)
2318 struct proc *p;
2319 struct umask_args /* {
2320 syscallarg(int) newmask;
2321 } */ *uap;
2322 register_t *retval;
2323 {
2324 register struct filedesc *fdp;
2325
2326 fdp = p->p_fd;
2327 *retval = fdp->fd_cmask;
2328 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2329 return (0);
2330 }
2331
2332 /*
2333 * Void all references to file by ripping underlying filesystem
2334 * away from vnode.
2335 */
2336 /* ARGSUSED */
2337 revoke(p, uap, retval)
2338 struct proc *p;
2339 register struct revoke_args /* {
2340 syscallarg(char *) path;
2341 } */ *uap;
2342 register_t *retval;
2343 {
2344 register struct vnode *vp;
2345 struct vattr vattr;
2346 int error;
2347 struct nameidata nd;
2348
2349 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2350 if (error = namei(&nd))
2351 return (error);
2352 vp = nd.ni_vp;
2353 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2354 error = EINVAL;
2355 goto out;
2356 }
2357 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
2358 goto out;
2359 if (p->p_ucred->cr_uid != vattr.va_uid &&
2360 (error = suser(p->p_ucred, &p->p_acflag)))
2361 goto out;
2362 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2363 vgoneall(vp);
2364 out:
2365 vrele(vp);
2366 return (error);
2367 }
2368
2369 /*
2370 * Convert a user file descriptor to a kernel file entry.
2371 */
2372 getvnode(fdp, fd, fpp)
2373 struct filedesc *fdp;
2374 struct file **fpp;
2375 int fd;
2376 {
2377 struct file *fp;
2378
2379 if ((u_int)fd >= fdp->fd_nfiles ||
2380 (fp = fdp->fd_ofiles[fd]) == NULL)
2381 return (EBADF);
2382 if (fp->f_type != DTYPE_VNODE)
2383 return (EINVAL);
2384 *fpp = fp;
2385 return (0);
2386 }
2387