vfs_syscalls.c revision 1.43 1 /* $NetBSD: vfs_syscalls.c,v 1.43 1994/12/14 16:30:40 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 fp->f_data = (caddr_t) NULL;
717 if (error = vn_open(&nd, flags, cmode, fp)) {
718 ffree(fp);
719 if (p->p_dupfd >= 0) {
720 switch (error) {
721 case ENODEV: /* XXX from fdopen or fdesc_open */
722 return (finishdup(fdp, p->p_dupfd, indx, retval));
723
724 case ENXIO: /* XXX from portal_open */
725 return (finishmove(fdp, p->p_dupfd, indx, retval));
726 }
727 }
728 if (error == ERESTART)
729 error = EINTR;
730 fdp->fd_ofiles[indx] = NULL;
731 return (error);
732 }
733
734 p->p_dupfd = 0;
735 vp = nd.ni_vp;
736
737 if (fp->f_data != (caddr_t) NULL) {
738 /*
739 * The fp data was changed, so it is a cloning operation
740 * Cleanup and return
741 */
742 if (flags & FWRITE)
743 vp->v_writecount--;
744 vput(vp);
745 *retval = indx;
746 return (0);
747 }
748
749 fp->f_flag = flags & FMASK;
750 fp->f_type = DTYPE_VNODE;
751 fp->f_ops = &vnops;
752 fp->f_data = (caddr_t)vp;
753 if (flags & (O_EXLOCK | O_SHLOCK)) {
754 lf.l_whence = SEEK_SET;
755 lf.l_start = 0;
756 lf.l_len = 0;
757 if (flags & O_EXLOCK)
758 lf.l_type = F_WRLCK;
759 else
760 lf.l_type = F_RDLCK;
761 type = F_FLOCK;
762 if ((flags & FNONBLOCK) == 0)
763 type |= F_WAIT;
764 VOP_UNLOCK(vp);
765 if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
766 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
767 ffree(fp);
768 fdp->fd_ofiles[indx] = NULL;
769 return (error);
770 }
771 VOP_LOCK(vp);
772 fp->f_flag |= FHASLOCK;
773 }
774 VOP_UNLOCK(vp);
775 *retval = indx;
776 return (0);
777 }
778
779 #ifdef COMPAT_43
780 /*
781 * Create a file.
782 */
783 compat_43_creat(p, uap, retval)
784 struct proc *p;
785 register struct compat_43_creat_args /* {
786 syscallarg(char *) path;
787 syscallarg(int) mode;
788 } */ *uap;
789 register_t *retval;
790 {
791 struct open_args /* {
792 syscallarg(char *) path;
793 syscallarg(int) flags;
794 syscallarg(int) mode;
795 } */ nuap;
796
797 SCARG(&nuap, path) = SCARG(uap, path);
798 SCARG(&nuap, mode) = SCARG(uap, mode);
799 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
800 return (open(p, &nuap, retval));
801 }
802 #endif /* COMPAT_43 */
803
804 /*
805 * Create a special file.
806 */
807 /* ARGSUSED */
808 mknod(p, uap, retval)
809 struct proc *p;
810 register struct mknod_args /* {
811 syscallarg(char *) path;
812 syscallarg(int) mode;
813 syscallarg(int) dev;
814 } */ *uap;
815 register_t *retval;
816 {
817 register struct vnode *vp;
818 struct vattr vattr;
819 int error;
820 int whiteout;
821 struct nameidata nd;
822
823 if (error = suser(p->p_ucred, &p->p_acflag))
824 return (error);
825 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
826 if (error = namei(&nd))
827 return (error);
828 vp = nd.ni_vp;
829 if (vp != NULL)
830 error = EEXIST;
831 else {
832 VATTR_NULL(&vattr);
833 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
834 vattr.va_rdev = SCARG(uap, dev);
835 whiteout = 0;
836
837 switch (SCARG(uap, mode) & S_IFMT) {
838 case S_IFMT: /* used by badsect to flag bad sectors */
839 vattr.va_type = VBAD;
840 break;
841 case S_IFCHR:
842 vattr.va_type = VCHR;
843 break;
844 case S_IFBLK:
845 vattr.va_type = VBLK;
846 break;
847 case S_IFWHT:
848 whiteout = 1;
849 break;
850 default:
851 error = EINVAL;
852 break;
853 }
854 }
855 if (!error) {
856 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
857 if (whiteout) {
858 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
859 if (error)
860 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
861 vput(nd.ni_dvp);
862 } else {
863 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
864 &nd.ni_cnd, &vattr);
865 }
866 } else {
867 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
868 if (nd.ni_dvp == vp)
869 vrele(nd.ni_dvp);
870 else
871 vput(nd.ni_dvp);
872 if (vp)
873 vrele(vp);
874 }
875 return (error);
876 }
877
878 /*
879 * Create a named pipe.
880 */
881 /* ARGSUSED */
882 mkfifo(p, uap, retval)
883 struct proc *p;
884 register struct mkfifo_args /* {
885 syscallarg(char *) path;
886 syscallarg(int) mode;
887 } */ *uap;
888 register_t *retval;
889 {
890 struct vattr vattr;
891 int error;
892 struct nameidata nd;
893
894 #ifndef FIFO
895 return (EOPNOTSUPP);
896 #else
897 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
898 if (error = namei(&nd))
899 return (error);
900 if (nd.ni_vp != NULL) {
901 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
902 if (nd.ni_dvp == nd.ni_vp)
903 vrele(nd.ni_dvp);
904 else
905 vput(nd.ni_dvp);
906 vrele(nd.ni_vp);
907 return (EEXIST);
908 }
909 VATTR_NULL(&vattr);
910 vattr.va_type = VFIFO;
911 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
912 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
913 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
914 #endif /* FIFO */
915 }
916
917 /*
918 * Make a hard file link.
919 */
920 /* ARGSUSED */
921 link(p, uap, retval)
922 struct proc *p;
923 register struct link_args /* {
924 syscallarg(char *) path;
925 syscallarg(char *) link;
926 } */ *uap;
927 register_t *retval;
928 {
929 register struct vnode *vp;
930 struct nameidata nd;
931 int error;
932
933 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
934 if (error = namei(&nd))
935 return (error);
936 vp = nd.ni_vp;
937 if (vp->v_type != VDIR ||
938 (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
939 nd.ni_cnd.cn_nameiop = CREATE;
940 nd.ni_cnd.cn_flags = LOCKPARENT;
941 nd.ni_dirp = SCARG(uap, link);
942 if ((error = namei(&nd)) == 0) {
943 if (nd.ni_vp != NULL)
944 error = EEXIST;
945 if (!error) {
946 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
947 LEASE_WRITE);
948 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
949 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
950 } else {
951 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
952 if (nd.ni_dvp == nd.ni_vp)
953 vrele(nd.ni_dvp);
954 else
955 vput(nd.ni_dvp);
956 if (nd.ni_vp)
957 vrele(nd.ni_vp);
958 }
959 }
960 }
961 vrele(vp);
962 return (error);
963 }
964
965 /*
966 * Make a symbolic link.
967 */
968 /* ARGSUSED */
969 symlink(p, uap, retval)
970 struct proc *p;
971 register struct symlink_args /* {
972 syscallarg(char *) path;
973 syscallarg(char *) link;
974 } */ *uap;
975 register_t *retval;
976 {
977 struct vattr vattr;
978 char *path;
979 int error;
980 struct nameidata nd;
981
982 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
983 if (error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, (u_int*)0))
984 goto out;
985 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
986 if (error = namei(&nd))
987 goto out;
988 if (nd.ni_vp) {
989 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
990 if (nd.ni_dvp == nd.ni_vp)
991 vrele(nd.ni_dvp);
992 else
993 vput(nd.ni_dvp);
994 vrele(nd.ni_vp);
995 error = EEXIST;
996 goto out;
997 }
998 VATTR_NULL(&vattr);
999 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1000 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1001 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1002 out:
1003 FREE(path, M_NAMEI);
1004 return (error);
1005 }
1006
1007 /*
1008 * Delete a whiteout from the filesystem.
1009 */
1010 /* ARGSUSED */
1011 undelete(p, uap, retval)
1012 struct proc *p;
1013 register struct undelete_args /* {
1014 syscallarg(char *) path;
1015 } */ *uap;
1016 register_t *retval;
1017 {
1018 int error;
1019 struct nameidata nd;
1020
1021 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1022 SCARG(uap, path), p);
1023 error = namei(&nd);
1024 if (error)
1025 return (error);
1026
1027 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1028 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1029 if (nd.ni_dvp == nd.ni_vp)
1030 vrele(nd.ni_dvp);
1031 else
1032 vput(nd.ni_dvp);
1033 if (nd.ni_vp)
1034 vrele(nd.ni_vp);
1035 return (EEXIST);
1036 }
1037
1038 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1039 if (error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE))
1040 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1041 vput(nd.ni_dvp);
1042 return (error);
1043 }
1044
1045 /*
1046 * Delete a name from the filesystem.
1047 */
1048 /* ARGSUSED */
1049 unlink(p, uap, retval)
1050 struct proc *p;
1051 struct unlink_args /* {
1052 syscallarg(char *) path;
1053 } */ *uap;
1054 register_t *retval;
1055 {
1056 register struct vnode *vp;
1057 int error;
1058 struct nameidata nd;
1059
1060 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1061 if (error = namei(&nd))
1062 return (error);
1063 vp = nd.ni_vp;
1064 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1065 VOP_LOCK(vp);
1066
1067 if (vp->v_type != VDIR ||
1068 (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
1069 /*
1070 * The root of a mounted filesystem cannot be deleted.
1071 */
1072 if (vp->v_flag & VROOT)
1073 error = EBUSY;
1074 else
1075 (void)vnode_pager_uncache(vp);
1076 }
1077
1078 if (!error) {
1079 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1080 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1081 } else {
1082 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1083 if (nd.ni_dvp == vp)
1084 vrele(nd.ni_dvp);
1085 else
1086 vput(nd.ni_dvp);
1087 if (vp != NULLVP)
1088 vput(vp);
1089 }
1090 return (error);
1091 }
1092
1093 /*
1094 * Reposition read/write file offset.
1095 */
1096 lseek(p, uap, retval)
1097 struct proc *p;
1098 register struct lseek_args /* {
1099 syscallarg(int) fd;
1100 syscallarg(int) pad;
1101 syscallarg(off_t) offset;
1102 syscallarg(int) whence;
1103 } */ *uap;
1104 register_t *retval;
1105 {
1106 struct ucred *cred = p->p_ucred;
1107 register struct filedesc *fdp = p->p_fd;
1108 register struct file *fp;
1109 struct vattr vattr;
1110 int error;
1111
1112 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1113 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1114 return (EBADF);
1115 if (fp->f_type != DTYPE_VNODE)
1116 return (ESPIPE);
1117 switch (SCARG(uap, whence)) {
1118 case L_INCR:
1119 fp->f_offset += SCARG(uap, offset);
1120 break;
1121 case L_XTND:
1122 if (error =
1123 VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p))
1124 return (error);
1125 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1126 break;
1127 case L_SET:
1128 fp->f_offset = SCARG(uap, offset);
1129 break;
1130 default:
1131 return (EINVAL);
1132 }
1133 *(off_t *)retval = fp->f_offset;
1134 return (0);
1135 }
1136
1137 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4)
1138 /*
1139 * Reposition read/write file offset.
1140 */
1141 compat_43_lseek(p, uap, retval)
1142 struct proc *p;
1143 register struct compat_43_lseek_args /* {
1144 syscallarg(int) fd;
1145 syscallarg(long) offset;
1146 syscallarg(int) whence;
1147 } */ *uap;
1148 register_t *retval;
1149 {
1150 struct lseek_args /* {
1151 syscallarg(int) fd;
1152 syscallarg(int) pad;
1153 syscallarg(off_t) offset;
1154 syscallarg(int) whence;
1155 } */ nuap;
1156 off_t qret;
1157 int error;
1158
1159 SCARG(&nuap, fd) = SCARG(uap, fd);
1160 SCARG(&nuap, offset) = SCARG(uap, offset);
1161 SCARG(&nuap, whence) = SCARG(uap, whence);
1162 error = lseek(p, &nuap, &qret);
1163 *(long *)retval = qret;
1164 return (error);
1165 }
1166 #endif /* COMPAT_43 || COMPAT_SUNOS */
1167
1168 /*
1169 * Check access permissions.
1170 */
1171 access(p, uap, retval)
1172 struct proc *p;
1173 register struct access_args /* {
1174 syscallarg(char *) path;
1175 syscallarg(int) flags;
1176 } */ *uap;
1177 register_t *retval;
1178 {
1179 register struct ucred *cred = p->p_ucred;
1180 register struct vnode *vp;
1181 int error, flags, t_gid, t_uid;
1182 struct nameidata nd;
1183
1184 t_uid = cred->cr_uid;
1185 t_gid = cred->cr_groups[0];
1186 cred->cr_uid = p->p_cred->p_ruid;
1187 cred->cr_groups[0] = p->p_cred->p_rgid;
1188 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1189 SCARG(uap, path), p);
1190 if (error = namei(&nd))
1191 goto out1;
1192 vp = nd.ni_vp;
1193
1194 /* Flags == 0 means only check for existence. */
1195 if (SCARG(uap, flags)) {
1196 flags = 0;
1197 if (SCARG(uap, flags) & R_OK)
1198 flags |= VREAD;
1199 if (SCARG(uap, flags) & W_OK)
1200 flags |= VWRITE;
1201 if (SCARG(uap, flags) & X_OK)
1202 flags |= VEXEC;
1203 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1204 error = VOP_ACCESS(vp, flags, cred, p);
1205 }
1206 vput(vp);
1207 out1:
1208 cred->cr_uid = t_uid;
1209 cred->cr_groups[0] = t_gid;
1210 return (error);
1211 }
1212
1213 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2)
1214 /*
1215 * Get file status; this version follows links.
1216 */
1217 /* ARGSUSED */
1218 compat_43_stat(p, uap, retval)
1219 struct proc *p;
1220 register struct compat_43_stat_args /* {
1221 syscallarg(char *) path;
1222 syscallarg(struct ostat *) ub;
1223 } */ *uap;
1224 register_t *retval;
1225 {
1226 struct stat sb;
1227 struct ostat osb;
1228 int error;
1229 struct nameidata nd;
1230
1231 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1232 SCARG(uap, path), p);
1233 if (error = namei(&nd))
1234 return (error);
1235 error = vn_stat(nd.ni_vp, &sb, p);
1236 vput(nd.ni_vp);
1237 if (error)
1238 return (error);
1239 cvtstat(&sb, &osb);
1240 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1241 return (error);
1242 }
1243
1244 /*
1245 * Get file status; this version does not follow links.
1246 */
1247 /* ARGSUSED */
1248 compat_43_lstat(p, uap, retval)
1249 struct proc *p;
1250 register struct compat_43_lstat_args /* {
1251 syscallarg(char *) path;
1252 syscallarg(struct ostat *) ub;
1253 } */ *uap;
1254 register_t *retval;
1255 {
1256 struct vnode *vp, *dvp;
1257 struct stat sb, sb1;
1258 struct ostat osb;
1259 int error;
1260 struct nameidata nd;
1261
1262 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1263 SCARG(uap, path), p);
1264 if (error = namei(&nd))
1265 return (error);
1266 /*
1267 * For symbolic links, always return the attributes of its
1268 * containing directory, except for mode, size, and links.
1269 */
1270 vp = nd.ni_vp;
1271 dvp = nd.ni_dvp;
1272 if (vp->v_type != VLNK) {
1273 if (dvp == vp)
1274 vrele(dvp);
1275 else
1276 vput(dvp);
1277 error = vn_stat(vp, &sb, p);
1278 vput(vp);
1279 if (error)
1280 return (error);
1281 } else {
1282 error = vn_stat(dvp, &sb, p);
1283 vput(dvp);
1284 if (error) {
1285 vput(vp);
1286 return (error);
1287 }
1288 error = vn_stat(vp, &sb1, p);
1289 vput(vp);
1290 if (error)
1291 return (error);
1292 sb.st_mode &= ~S_IFDIR;
1293 sb.st_mode |= S_IFLNK;
1294 sb.st_nlink = sb1.st_nlink;
1295 sb.st_size = sb1.st_size;
1296 sb.st_blocks = sb1.st_blocks;
1297 }
1298 cvtstat(&sb, &osb);
1299 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1300 return (error);
1301 }
1302
1303 /*
1304 * Convert from an old to a new stat structure.
1305 */
1306 cvtstat(st, ost)
1307 struct stat *st;
1308 struct ostat *ost;
1309 {
1310
1311 ost->st_dev = st->st_dev;
1312 ost->st_ino = st->st_ino;
1313 ost->st_mode = st->st_mode;
1314 ost->st_nlink = st->st_nlink;
1315 ost->st_uid = st->st_uid;
1316 ost->st_gid = st->st_gid;
1317 ost->st_rdev = st->st_rdev;
1318 if (st->st_size < (quad_t)1 << 32)
1319 ost->st_size = st->st_size;
1320 else
1321 ost->st_size = -2;
1322 ost->st_atime = st->st_atime;
1323 ost->st_mtime = st->st_mtime;
1324 ost->st_ctime = st->st_ctime;
1325 ost->st_blksize = st->st_blksize;
1326 ost->st_blocks = st->st_blocks;
1327 ost->st_flags = st->st_flags;
1328 ost->st_gen = st->st_gen;
1329 }
1330 #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_IBCS2 */
1331
1332 /*
1333 * Get file status; this version follows links.
1334 */
1335 /* ARGSUSED */
1336 stat(p, uap, retval)
1337 struct proc *p;
1338 register struct stat_args /* {
1339 syscallarg(char *) path;
1340 syscallarg(struct stat *) ub;
1341 } */ *uap;
1342 register_t *retval;
1343 {
1344 struct stat sb;
1345 int error;
1346 struct nameidata nd;
1347
1348 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1349 SCARG(uap, path), p);
1350 if (error = namei(&nd))
1351 return (error);
1352 error = vn_stat(nd.ni_vp, &sb, p);
1353 vput(nd.ni_vp);
1354 if (error)
1355 return (error);
1356 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1357 return (error);
1358 }
1359
1360 /*
1361 * Get file status; this version does not follow links.
1362 */
1363 /* ARGSUSED */
1364 lstat(p, uap, retval)
1365 struct proc *p;
1366 register struct lstat_args /* {
1367 syscallarg(char *) path;
1368 syscallarg(struct stat *) ub;
1369 } */ *uap;
1370 register_t *retval;
1371 {
1372 int error;
1373 struct vnode *vp, *dvp;
1374 struct stat sb, sb1;
1375 struct nameidata nd;
1376
1377 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1378 SCARG(uap, path), p);
1379 if (error = namei(&nd))
1380 return (error);
1381 /*
1382 * For symbolic links, always return the attributes of its
1383 * containing directory, except for mode, size, and links.
1384 */
1385 vp = nd.ni_vp;
1386 dvp = nd.ni_dvp;
1387 if (vp->v_type != VLNK) {
1388 if (dvp == vp)
1389 vrele(dvp);
1390 else
1391 vput(dvp);
1392 error = vn_stat(vp, &sb, p);
1393 vput(vp);
1394 if (error)
1395 return (error);
1396 } else {
1397 error = vn_stat(dvp, &sb, p);
1398 vput(dvp);
1399 if (error) {
1400 vput(vp);
1401 return (error);
1402 }
1403 error = vn_stat(vp, &sb1, p);
1404 vput(vp);
1405 if (error)
1406 return (error);
1407 sb.st_mode &= ~S_IFDIR;
1408 sb.st_mode |= S_IFLNK;
1409 sb.st_nlink = sb1.st_nlink;
1410 sb.st_size = sb1.st_size;
1411 sb.st_blocks = sb1.st_blocks;
1412 }
1413 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1414 return (error);
1415 }
1416
1417 /*
1418 * Get configurable pathname variables.
1419 */
1420 /* ARGSUSED */
1421 pathconf(p, uap, retval)
1422 struct proc *p;
1423 register struct pathconf_args /* {
1424 syscallarg(char *) path;
1425 syscallarg(int) name;
1426 } */ *uap;
1427 register_t *retval;
1428 {
1429 int error;
1430 struct nameidata nd;
1431
1432 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1433 SCARG(uap, path), p);
1434 if (error = namei(&nd))
1435 return (error);
1436 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1437 vput(nd.ni_vp);
1438 return (error);
1439 }
1440
1441 /*
1442 * Return target name of a symbolic link.
1443 */
1444 /* ARGSUSED */
1445 readlink(p, uap, retval)
1446 struct proc *p;
1447 register struct readlink_args /* {
1448 syscallarg(char *) path;
1449 syscallarg(char *) buf;
1450 syscallarg(int) count;
1451 } */ *uap;
1452 register_t *retval;
1453 {
1454 register struct vnode *vp;
1455 struct iovec aiov;
1456 struct uio auio;
1457 int error;
1458 struct nameidata nd;
1459
1460 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1461 SCARG(uap, path), p);
1462 if (error = namei(&nd))
1463 return (error);
1464 vp = nd.ni_vp;
1465 if (vp->v_type != VLNK)
1466 error = EINVAL;
1467 else {
1468 aiov.iov_base = SCARG(uap, buf);
1469 aiov.iov_len = SCARG(uap, count);
1470 auio.uio_iov = &aiov;
1471 auio.uio_iovcnt = 1;
1472 auio.uio_offset = 0;
1473 auio.uio_rw = UIO_READ;
1474 auio.uio_segflg = UIO_USERSPACE;
1475 auio.uio_procp = p;
1476 auio.uio_resid = SCARG(uap, count);
1477 error = VOP_READLINK(vp, &auio, p->p_ucred);
1478 }
1479 vput(vp);
1480 *retval = SCARG(uap, count) - auio.uio_resid;
1481 return (error);
1482 }
1483
1484 /*
1485 * Change flags of a file given a path name.
1486 */
1487 /* ARGSUSED */
1488 chflags(p, uap, retval)
1489 struct proc *p;
1490 register struct chflags_args /* {
1491 syscallarg(char *) path;
1492 syscallarg(int) flags;
1493 } */ *uap;
1494 register_t *retval;
1495 {
1496 register struct vnode *vp;
1497 struct vattr vattr;
1498 int error;
1499 struct nameidata nd;
1500
1501 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1502 if (error = namei(&nd))
1503 return (error);
1504 vp = nd.ni_vp;
1505 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1506 VOP_LOCK(vp);
1507 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1508 error = EROFS;
1509 else {
1510 VATTR_NULL(&vattr);
1511 vattr.va_flags = SCARG(uap, flags);
1512 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1513 }
1514 vput(vp);
1515 return (error);
1516 }
1517
1518 /*
1519 * Change flags of a file given a file descriptor.
1520 */
1521 /* ARGSUSED */
1522 fchflags(p, uap, retval)
1523 struct proc *p;
1524 register struct fchflags_args /* {
1525 syscallarg(int) fd;
1526 syscallarg(int) flags;
1527 } */ *uap;
1528 register_t *retval;
1529 {
1530 struct vattr vattr;
1531 struct vnode *vp;
1532 struct file *fp;
1533 int error;
1534
1535 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1536 return (error);
1537 vp = (struct vnode *)fp->f_data;
1538 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1539 VOP_LOCK(vp);
1540 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1541 error = EROFS;
1542 else {
1543 VATTR_NULL(&vattr);
1544 vattr.va_flags = SCARG(uap, flags);
1545 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1546 }
1547 VOP_UNLOCK(vp);
1548 return (error);
1549 }
1550
1551 /*
1552 * Change mode of a file given path name.
1553 */
1554 /* ARGSUSED */
1555 chmod(p, uap, retval)
1556 struct proc *p;
1557 register struct chmod_args /* {
1558 syscallarg(char *) path;
1559 syscallarg(int) mode;
1560 } */ *uap;
1561 register_t *retval;
1562 {
1563 register struct vnode *vp;
1564 struct vattr vattr;
1565 int error;
1566 struct nameidata nd;
1567
1568 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1569 if (error = namei(&nd))
1570 return (error);
1571 vp = nd.ni_vp;
1572 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1573 VOP_LOCK(vp);
1574 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1575 error = EROFS;
1576 else {
1577 VATTR_NULL(&vattr);
1578 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1579 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1580 }
1581 vput(vp);
1582 return (error);
1583 }
1584
1585 /*
1586 * Change mode of a file given a file descriptor.
1587 */
1588 /* ARGSUSED */
1589 fchmod(p, uap, retval)
1590 struct proc *p;
1591 register struct fchmod_args /* {
1592 syscallarg(int) fd;
1593 syscallarg(int) mode;
1594 } */ *uap;
1595 register_t *retval;
1596 {
1597 struct vattr vattr;
1598 struct vnode *vp;
1599 struct file *fp;
1600 int error;
1601
1602 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1603 return (error);
1604 vp = (struct vnode *)fp->f_data;
1605 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1606 VOP_LOCK(vp);
1607 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1608 error = EROFS;
1609 else {
1610 VATTR_NULL(&vattr);
1611 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1612 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1613 }
1614 VOP_UNLOCK(vp);
1615 return (error);
1616 }
1617
1618 /*
1619 * Set ownership given a path name.
1620 */
1621 /* ARGSUSED */
1622 chown(p, uap, retval)
1623 struct proc *p;
1624 register struct chown_args /* {
1625 syscallarg(char *) path;
1626 syscallarg(int) uid;
1627 syscallarg(int) gid;
1628 } */ *uap;
1629 register_t *retval;
1630 {
1631 register struct vnode *vp;
1632 struct vattr vattr;
1633 int error;
1634 struct nameidata nd;
1635
1636 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1637 if (error = namei(&nd))
1638 return (error);
1639 vp = nd.ni_vp;
1640 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1641 VOP_LOCK(vp);
1642 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1643 error = EROFS;
1644 else {
1645 VATTR_NULL(&vattr);
1646 vattr.va_uid = SCARG(uap, uid);
1647 vattr.va_gid = SCARG(uap, gid);
1648 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1649 }
1650 vput(vp);
1651 return (error);
1652 }
1653
1654 /*
1655 * Set ownership given a file descriptor.
1656 */
1657 /* ARGSUSED */
1658 fchown(p, uap, retval)
1659 struct proc *p;
1660 register struct fchown_args /* {
1661 syscallarg(int) fd;
1662 syscallarg(int) uid;
1663 syscallarg(int) gid;
1664 } */ *uap;
1665 register_t *retval;
1666 {
1667 struct vattr vattr;
1668 struct vnode *vp;
1669 struct file *fp;
1670 int error;
1671
1672 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1673 return (error);
1674 vp = (struct vnode *)fp->f_data;
1675 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1676 VOP_LOCK(vp);
1677 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1678 error = EROFS;
1679 else {
1680 VATTR_NULL(&vattr);
1681 vattr.va_uid = SCARG(uap, uid);
1682 vattr.va_gid = SCARG(uap, gid);
1683 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1684 }
1685 VOP_UNLOCK(vp);
1686 return (error);
1687 }
1688
1689 /*
1690 * Set the access and modification times of a file.
1691 */
1692 /* ARGSUSED */
1693 utimes(p, uap, retval)
1694 struct proc *p;
1695 register struct utimes_args /* {
1696 syscallarg(char *) path;
1697 syscallarg(struct timeval *) tptr;
1698 } */ *uap;
1699 register_t *retval;
1700 {
1701 register struct vnode *vp;
1702 struct timeval tv[2];
1703 struct vattr vattr;
1704 int error;
1705 struct nameidata nd;
1706
1707 VATTR_NULL(&vattr);
1708 if (SCARG(uap, tptr) == NULL) {
1709 microtime(&tv[0]);
1710 tv[1] = tv[0];
1711 vattr.va_vaflags |= VA_UTIMES_NULL;
1712 } else if (error = copyin((caddr_t)SCARG(uap, tptr), (caddr_t)tv,
1713 sizeof (tv)))
1714 return (error);
1715 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1716 if (error = namei(&nd))
1717 return (error);
1718 vp = nd.ni_vp;
1719 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1720 VOP_LOCK(vp);
1721 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1722 error = EROFS;
1723 else {
1724 vattr.va_atime.ts_sec = tv[0].tv_sec;
1725 vattr.va_atime.ts_nsec = tv[0].tv_usec * 1000;
1726 vattr.va_mtime.ts_sec = tv[1].tv_sec;
1727 vattr.va_mtime.ts_nsec = tv[1].tv_usec * 1000;
1728 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1729 }
1730 vput(vp);
1731 return (error);
1732 }
1733
1734 /*
1735 * Truncate a file given its path name.
1736 */
1737 /* ARGSUSED */
1738 truncate(p, uap, retval)
1739 struct proc *p;
1740 register struct truncate_args /* {
1741 syscallarg(char *) path;
1742 syscallarg(int) pad;
1743 syscallarg(off_t) length;
1744 } */ *uap;
1745 register_t *retval;
1746 {
1747 register struct vnode *vp;
1748 struct vattr vattr;
1749 int error;
1750 struct nameidata nd;
1751
1752 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1753 if (error = namei(&nd))
1754 return (error);
1755 vp = nd.ni_vp;
1756 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1757 VOP_LOCK(vp);
1758 if (vp->v_type == VDIR)
1759 error = EISDIR;
1760 else if ((error = vn_writechk(vp)) == 0 &&
1761 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1762 VATTR_NULL(&vattr);
1763 vattr.va_size = SCARG(uap, length);
1764 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1765 }
1766 vput(vp);
1767 return (error);
1768 }
1769
1770 /*
1771 * Truncate a file given a file descriptor.
1772 */
1773 /* ARGSUSED */
1774 ftruncate(p, uap, retval)
1775 struct proc *p;
1776 register struct ftruncate_args /* {
1777 syscallarg(int) fd;
1778 syscallarg(int) pad;
1779 syscallarg(off_t) length;
1780 } */ *uap;
1781 register_t *retval;
1782 {
1783 struct vattr vattr;
1784 struct vnode *vp;
1785 struct file *fp;
1786 int error;
1787
1788 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1789 return (error);
1790 if ((fp->f_flag & FWRITE) == 0)
1791 return (EINVAL);
1792 vp = (struct vnode *)fp->f_data;
1793 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1794 VOP_LOCK(vp);
1795 if (vp->v_type == VDIR)
1796 error = EISDIR;
1797 else if ((error = vn_writechk(vp)) == 0) {
1798 VATTR_NULL(&vattr);
1799 vattr.va_size = SCARG(uap, length);
1800 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1801 }
1802 VOP_UNLOCK(vp);
1803 return (error);
1804 }
1805
1806 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1807 /*
1808 * Truncate a file given its path name.
1809 */
1810 /* ARGSUSED */
1811 compat_43_truncate(p, uap, retval)
1812 struct proc *p;
1813 register struct compat_43_truncate_args /* {
1814 syscallarg(char *) path;
1815 syscallarg(long) length;
1816 } */ *uap;
1817 register_t *retval;
1818 {
1819 struct truncate_args /* {
1820 syscallarg(char *) path;
1821 syscallarg(int) pad;
1822 syscallarg(off_t) length;
1823 } */ nuap;
1824
1825 SCARG(&nuap, path) = SCARG(uap, path);
1826 SCARG(&nuap, length) = SCARG(uap, length);
1827 return (truncate(p, &nuap, retval));
1828 }
1829
1830 /*
1831 * Truncate a file given a file descriptor.
1832 */
1833 /* ARGSUSED */
1834 compat_43_ftruncate(p, uap, retval)
1835 struct proc *p;
1836 register struct compat_43_ftruncate_args /* {
1837 syscallarg(int) fd;
1838 syscallarg(long) length;
1839 } */ *uap;
1840 register_t *retval;
1841 {
1842 struct ftruncate_args /* {
1843 syscallarg(int) fd;
1844 syscallarg(int) pad;
1845 syscallarg(off_t) length;
1846 } */ nuap;
1847
1848 SCARG(&nuap, fd) = SCARG(uap, fd);
1849 SCARG(&nuap, length) = SCARG(uap, length);
1850 return (ftruncate(p, &nuap, retval));
1851 }
1852 #endif /* COMPAT_43 || COMPAT_SUNOS */
1853
1854 /*
1855 * Sync an open file.
1856 */
1857 /* ARGSUSED */
1858 fsync(p, uap, retval)
1859 struct proc *p;
1860 struct fsync_args /* {
1861 syscallarg(int) fd;
1862 } */ *uap;
1863 register_t *retval;
1864 {
1865 register struct vnode *vp;
1866 struct file *fp;
1867 int error;
1868
1869 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1870 return (error);
1871 vp = (struct vnode *)fp->f_data;
1872 VOP_LOCK(vp);
1873 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1874 VOP_UNLOCK(vp);
1875 return (error);
1876 }
1877
1878 /*
1879 * Rename files. Source and destination must either both be directories,
1880 * or both not be directories. If target is a directory, it must be empty.
1881 */
1882 /* ARGSUSED */
1883 rename(p, uap, retval)
1884 struct proc *p;
1885 register struct rename_args /* {
1886 syscallarg(char *) from;
1887 syscallarg(char *) to;
1888 } */ *uap;
1889 register_t *retval;
1890 {
1891 register struct vnode *tvp, *fvp, *tdvp;
1892 struct nameidata fromnd, tond;
1893 int error;
1894
1895 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1896 SCARG(uap, from), p);
1897 if (error = namei(&fromnd))
1898 return (error);
1899 fvp = fromnd.ni_vp;
1900 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1901 UIO_USERSPACE, SCARG(uap, to), p);
1902 if (error = namei(&tond)) {
1903 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1904 vrele(fromnd.ni_dvp);
1905 vrele(fvp);
1906 goto out1;
1907 }
1908 tdvp = tond.ni_dvp;
1909 tvp = tond.ni_vp;
1910 if (tvp != NULL) {
1911 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1912 error = ENOTDIR;
1913 goto out;
1914 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1915 error = EISDIR;
1916 goto out;
1917 }
1918 }
1919 if (fvp == tdvp)
1920 error = EINVAL;
1921 /*
1922 * If source is the same as the destination (that is the
1923 * same inode number with the same name in the same directory),
1924 * then there is nothing to do.
1925 */
1926 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1927 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1928 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1929 fromnd.ni_cnd.cn_namelen))
1930 error = -1;
1931 out:
1932 if (!error) {
1933 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1934 if (fromnd.ni_dvp != tdvp)
1935 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1936 if (tvp)
1937 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1938 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1939 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1940 } else {
1941 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1942 if (tdvp == tvp)
1943 vrele(tdvp);
1944 else
1945 vput(tdvp);
1946 if (tvp)
1947 vput(tvp);
1948 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1949 vrele(fromnd.ni_dvp);
1950 vrele(fvp);
1951 }
1952 vrele(tond.ni_startdir);
1953 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1954 out1:
1955 if (fromnd.ni_startdir)
1956 vrele(fromnd.ni_startdir);
1957 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1958 if (error == -1)
1959 return (0);
1960 return (error);
1961 }
1962
1963 /*
1964 * Make a directory file.
1965 */
1966 /* ARGSUSED */
1967 mkdir(p, uap, retval)
1968 struct proc *p;
1969 register struct mkdir_args /* {
1970 syscallarg(char *) path;
1971 syscallarg(int) mode;
1972 } */ *uap;
1973 register_t *retval;
1974 {
1975 register struct vnode *vp;
1976 struct vattr vattr;
1977 int error;
1978 struct nameidata nd;
1979
1980 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1981 if (error = namei(&nd))
1982 return (error);
1983 vp = nd.ni_vp;
1984 if (vp != NULL) {
1985 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1986 if (nd.ni_dvp == vp)
1987 vrele(nd.ni_dvp);
1988 else
1989 vput(nd.ni_dvp);
1990 vrele(vp);
1991 return (EEXIST);
1992 }
1993 VATTR_NULL(&vattr);
1994 vattr.va_type = VDIR;
1995 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1996 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1997 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1998 if (!error)
1999 vput(nd.ni_vp);
2000 return (error);
2001 }
2002
2003 /*
2004 * Remove a directory file.
2005 */
2006 /* ARGSUSED */
2007 rmdir(p, uap, retval)
2008 struct proc *p;
2009 struct rmdir_args /* {
2010 syscallarg(char *) path;
2011 } */ *uap;
2012 register_t *retval;
2013 {
2014 register struct vnode *vp;
2015 int error;
2016 struct nameidata nd;
2017
2018 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2019 SCARG(uap, path), p);
2020 if (error = namei(&nd))
2021 return (error);
2022 vp = nd.ni_vp;
2023 if (vp->v_type != VDIR) {
2024 error = ENOTDIR;
2025 goto out;
2026 }
2027 /*
2028 * No rmdir "." please.
2029 */
2030 if (nd.ni_dvp == vp) {
2031 error = EINVAL;
2032 goto out;
2033 }
2034 /*
2035 * The root of a mounted filesystem cannot be deleted.
2036 */
2037 if (vp->v_flag & VROOT)
2038 error = EBUSY;
2039 out:
2040 if (!error) {
2041 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2042 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2043 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2044 } else {
2045 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2046 if (nd.ni_dvp == vp)
2047 vrele(nd.ni_dvp);
2048 else
2049 vput(nd.ni_dvp);
2050 vput(vp);
2051 }
2052 return (error);
2053 }
2054
2055 #if defined(COMPAT_43) || defined(COMPAT_HPUX)
2056 /*
2057 * Read a block of directory entries in a file system independent format.
2058 */
2059 compat_43_getdirentries(p, uap, retval)
2060 struct proc *p;
2061 register struct compat_43_getdirentries_args /* {
2062 syscallarg(int) fd;
2063 syscallarg(char *) buf;
2064 syscallarg(u_int) count;
2065 syscallarg(long *) basep;
2066 } */ *uap;
2067 register_t *retval;
2068 {
2069 register struct vnode *vp;
2070 struct file *fp;
2071 struct uio auio, kuio;
2072 struct iovec aiov, kiov;
2073 struct dirent *dp, *edp;
2074 caddr_t dirbuf;
2075 int error, eofflag, readcnt;
2076 long loff;
2077
2078 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2079 return (error);
2080 if ((fp->f_flag & FREAD) == 0)
2081 return (EBADF);
2082 vp = (struct vnode *)fp->f_data;
2083 unionread:
2084 if (vp->v_type != VDIR)
2085 return (EINVAL);
2086 aiov.iov_base = SCARG(uap, buf);
2087 aiov.iov_len = SCARG(uap, count);
2088 auio.uio_iov = &aiov;
2089 auio.uio_iovcnt = 1;
2090 auio.uio_rw = UIO_READ;
2091 auio.uio_segflg = UIO_USERSPACE;
2092 auio.uio_procp = p;
2093 auio.uio_resid = SCARG(uap, count);
2094 VOP_LOCK(vp);
2095 loff = auio.uio_offset = fp->f_offset;
2096 # if (BYTE_ORDER != LITTLE_ENDIAN)
2097 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2098 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2099 (u_long *)0, 0);
2100 fp->f_offset = auio.uio_offset;
2101 } else
2102 # endif
2103 {
2104 kuio = auio;
2105 kuio.uio_iov = &kiov;
2106 kuio.uio_segflg = UIO_SYSSPACE;
2107 kiov.iov_len = SCARG(uap, count);
2108 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2109 kiov.iov_base = dirbuf;
2110 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2111 (u_long *)0, 0);
2112 fp->f_offset = kuio.uio_offset;
2113 if (error == 0) {
2114 readcnt = SCARG(uap, count) - kuio.uio_resid;
2115 edp = (struct dirent *)&dirbuf[readcnt];
2116 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2117 # if (BYTE_ORDER == LITTLE_ENDIAN)
2118 /*
2119 * The expected low byte of
2120 * dp->d_namlen is our dp->d_type.
2121 * The high MBZ byte of dp->d_namlen
2122 * is our dp->d_namlen.
2123 */
2124 dp->d_type = dp->d_namlen;
2125 dp->d_namlen = 0;
2126 # else
2127 /*
2128 * The dp->d_type is the high byte
2129 * of the expected dp->d_namlen,
2130 * so must be zero'ed.
2131 */
2132 dp->d_type = 0;
2133 # endif
2134 if (dp->d_reclen > 0) {
2135 dp = (struct dirent *)
2136 ((char *)dp + dp->d_reclen);
2137 } else {
2138 error = EIO;
2139 break;
2140 }
2141 }
2142 if (dp >= edp)
2143 error = uiomove(dirbuf, readcnt, &auio);
2144 }
2145 FREE(dirbuf, M_TEMP);
2146 }
2147 VOP_UNLOCK(vp);
2148 if (error)
2149 return (error);
2150
2151 #ifdef UNION
2152 {
2153 extern int (**union_vnodeop_p)();
2154 extern struct vnode *union_dircache __P((struct vnode *));
2155
2156 if ((SCARG(uap, count) == auio.uio_resid) &&
2157 (vp->v_op == union_vnodeop_p)) {
2158 struct vnode *lvp;
2159
2160 lvp = union_dircache(vp);
2161 if (lvp != NULLVP) {
2162 struct vattr va;
2163
2164 /*
2165 * If the directory is opaque,
2166 * then don't show lower entries
2167 */
2168 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2169 if (va.va_flags & OPAQUE) {
2170 vput(lvp);
2171 lvp = NULL;
2172 }
2173 }
2174
2175 if (lvp != NULLVP) {
2176 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2177 VOP_UNLOCK(lvp);
2178
2179 if (error) {
2180 vrele(lvp);
2181 return (error);
2182 }
2183 fp->f_data = (caddr_t) lvp;
2184 fp->f_offset = 0;
2185 error = vn_close(vp, FREAD, fp->f_cred, p);
2186 if (error)
2187 return (error);
2188 vp = lvp;
2189 goto unionread;
2190 }
2191 }
2192 }
2193 #endif /* UNION */
2194
2195 if ((SCARG(uap, count) == auio.uio_resid) &&
2196 (vp->v_flag & VROOT) &&
2197 (vp->v_mount->mnt_flag & MNT_UNION)) {
2198 struct vnode *tvp = vp;
2199 vp = vp->v_mount->mnt_vnodecovered;
2200 VREF(vp);
2201 fp->f_data = (caddr_t) vp;
2202 fp->f_offset = 0;
2203 vrele(tvp);
2204 goto unionread;
2205 }
2206 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2207 sizeof(long));
2208 *retval = SCARG(uap, count) - auio.uio_resid;
2209 return (error);
2210 }
2211 #endif /* COMPAT_43 */
2212
2213 /*
2214 * Read a block of directory entries in a file system independent format.
2215 */
2216 getdirentries(p, uap, retval)
2217 struct proc *p;
2218 register struct getdirentries_args /* {
2219 syscallarg(int) fd;
2220 syscallarg(char *) buf;
2221 syscallarg(u_int) count;
2222 syscallarg(long *) basep;
2223 } */ *uap;
2224 register_t *retval;
2225 {
2226 register struct vnode *vp;
2227 struct file *fp;
2228 struct uio auio;
2229 struct iovec aiov;
2230 long loff;
2231 int error, eofflag;
2232
2233 if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2234 return (error);
2235 if ((fp->f_flag & FREAD) == 0)
2236 return (EBADF);
2237 vp = (struct vnode *)fp->f_data;
2238 unionread:
2239 if (vp->v_type != VDIR)
2240 return (EINVAL);
2241 aiov.iov_base = SCARG(uap, buf);
2242 aiov.iov_len = SCARG(uap, count);
2243 auio.uio_iov = &aiov;
2244 auio.uio_iovcnt = 1;
2245 auio.uio_rw = UIO_READ;
2246 auio.uio_segflg = UIO_USERSPACE;
2247 auio.uio_procp = p;
2248 auio.uio_resid = SCARG(uap, count);
2249 VOP_LOCK(vp);
2250 loff = auio.uio_offset = fp->f_offset;
2251 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
2252 fp->f_offset = auio.uio_offset;
2253 VOP_UNLOCK(vp);
2254 if (error)
2255 return (error);
2256
2257 #ifdef UNION
2258 {
2259 extern int (**union_vnodeop_p)();
2260 extern struct vnode *union_dircache __P((struct vnode *));
2261
2262 if ((SCARG(uap, count) == auio.uio_resid) &&
2263 (vp->v_op == union_vnodeop_p)) {
2264 struct vnode *lvp;
2265
2266 lvp = union_dircache(vp);
2267 if (lvp != NULLVP) {
2268 struct vattr va;
2269
2270 /*
2271 * If the directory is opaque,
2272 * then don't show lower entries
2273 */
2274 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2275 if (va.va_flags & OPAQUE) {
2276 vput(lvp);
2277 lvp = NULL;
2278 }
2279 }
2280
2281 if (lvp != NULLVP) {
2282 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2283 VOP_UNLOCK(lvp);
2284
2285 if (error) {
2286 vrele(lvp);
2287 return (error);
2288 }
2289 fp->f_data = (caddr_t) lvp;
2290 fp->f_offset = 0;
2291 error = vn_close(vp, FREAD, fp->f_cred, p);
2292 if (error)
2293 return (error);
2294 vp = lvp;
2295 goto unionread;
2296 }
2297 }
2298 }
2299 #endif /* UNION */
2300
2301 if ((SCARG(uap, count) == auio.uio_resid) &&
2302 (vp->v_flag & VROOT) &&
2303 (vp->v_mount->mnt_flag & MNT_UNION)) {
2304 struct vnode *tvp = vp;
2305 vp = vp->v_mount->mnt_vnodecovered;
2306 VREF(vp);
2307 fp->f_data = (caddr_t) vp;
2308 fp->f_offset = 0;
2309 vrele(tvp);
2310 goto unionread;
2311 }
2312 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2313 sizeof(long));
2314 *retval = SCARG(uap, count) - auio.uio_resid;
2315 return (error);
2316 }
2317
2318 /*
2319 * Set the mode mask for creation of filesystem nodes.
2320 */
2321 mode_t /* XXX */
2322 umask(p, uap, retval)
2323 struct proc *p;
2324 struct umask_args /* {
2325 syscallarg(int) newmask;
2326 } */ *uap;
2327 register_t *retval;
2328 {
2329 register struct filedesc *fdp;
2330
2331 fdp = p->p_fd;
2332 *retval = fdp->fd_cmask;
2333 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2334 return (0);
2335 }
2336
2337 /*
2338 * Void all references to file by ripping underlying filesystem
2339 * away from vnode.
2340 */
2341 /* ARGSUSED */
2342 revoke(p, uap, retval)
2343 struct proc *p;
2344 register struct revoke_args /* {
2345 syscallarg(char *) path;
2346 } */ *uap;
2347 register_t *retval;
2348 {
2349 register struct vnode *vp;
2350 struct vattr vattr;
2351 int error;
2352 struct nameidata nd;
2353
2354 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2355 if (error = namei(&nd))
2356 return (error);
2357 vp = nd.ni_vp;
2358 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2359 error = EINVAL;
2360 goto out;
2361 }
2362 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
2363 goto out;
2364 if (p->p_ucred->cr_uid != vattr.va_uid &&
2365 (error = suser(p->p_ucred, &p->p_acflag)))
2366 goto out;
2367 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2368 vgoneall(vp);
2369 out:
2370 vrele(vp);
2371 return (error);
2372 }
2373
2374 /*
2375 * Convert a user file descriptor to a kernel file entry.
2376 */
2377 getvnode(fdp, fd, fpp)
2378 struct filedesc *fdp;
2379 struct file **fpp;
2380 int fd;
2381 {
2382 struct file *fp;
2383
2384 if ((u_int)fd >= fdp->fd_nfiles ||
2385 (fp = fdp->fd_ofiles[fd]) == NULL)
2386 return (EBADF);
2387 if (fp->f_type != DTYPE_VNODE)
2388 return (EINVAL);
2389 *fpp = fp;
2390 return (0);
2391 }
2392