Mop up remains of the ibcs2/streams/svr4 removal:
[dragonfly.git] / sys / kern / vfs_syscalls.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)vfs_syscalls.c      8.13 (Berkeley) 4/15/94
39  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40  * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.91 2006/05/19 07:33:45 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/conf.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
55 #include <sys/file.h>
56 #include <sys/linker.h>
57 #include <sys/stat.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/proc.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/kern_syscall.h>
66
67 #include <machine/limits.h>
68 #include <vfs/union/union.h>
69 #include <sys/sysctl.h>
70 #include <vm/vm.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_zone.h>
73 #include <vm/vm_page.h>
74
75 #include <sys/file2.h>
76
77 static int checkvp_chdir (struct vnode *vn, struct thread *td);
78 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
79 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
80 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
81 static int getutimes (const struct timeval *, struct timespec *);
82 static int setfown (struct vnode *, uid_t, gid_t);
83 static int setfmode (struct vnode *, int);
84 static int setfflags (struct vnode *, int);
85 static int setutimes (struct vnode *, const struct timespec *, int);
86 static int      usermount = 0;  /* if 1, non-root can mount fs. */
87
88 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
89
90 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91
92 /*
93  * Virtual File System System Calls
94  */
95
96 /*
97  * Mount a file system.
98  */
99 /*
100  * mount_args(char *type, char *path, int flags, caddr_t data)
101  */
102 /* ARGSUSED */
103 int
104 mount(struct mount_args *uap)
105 {
106         struct thread *td = curthread;
107         struct proc *p = td->td_proc;
108         struct vnode *vp;
109         struct namecache *ncp;
110         struct mount *mp;
111         struct vfsconf *vfsp;
112         int error, flag = 0, flag2 = 0;
113         struct vattr va;
114         struct nlookupdata nd;
115         char fstypename[MFSNAMELEN];
116         struct nlcomponent nlc;
117         struct ucred *cred = p->p_ucred;
118
119         KKASSERT(p);
120         if (cred->cr_prison != NULL)
121                 return (EPERM);
122         if (usermount == 0 && (error = suser(td)))
123                 return (error);
124         /*
125          * Do not allow NFS export by non-root users.
126          */
127         if (uap->flags & MNT_EXPORTED) {
128                 error = suser(td);
129                 if (error)
130                         return (error);
131         }
132         /*
133          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
134          */
135         if (suser(td)) 
136                 uap->flags |= MNT_NOSUID | MNT_NODEV;
137
138         /*
139          * Lookup the requested path and extract the ncp and vnode.
140          */
141         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
142         if (error == 0) {
143                 if ((error = nlookup(&nd)) == 0) {
144                         if (nd.nl_ncp->nc_vp == NULL)
145                                 error = ENOENT;
146                 }
147         }
148         if (error) {
149                 nlookup_done(&nd);
150                 return (error);
151         }
152
153         /*
154          * Extract the locked+refd ncp and cleanup the nd structure
155          */
156         ncp = nd.nl_ncp;
157         nd.nl_ncp = NULL;
158         nlookup_done(&nd);
159
160         /*
161          * now we have the locked ref'd ncp and unreferenced vnode.
162          */
163         vp = ncp->nc_vp;
164         if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
165                 cache_put(ncp);
166                 return (error);
167         }
168         cache_unlock(ncp);
169
170         /*
171          * Now we have an unlocked ref'd ncp and a locked ref'd vp
172          */
173         if (uap->flags & MNT_UPDATE) {
174                 if ((vp->v_flag & VROOT) == 0) {
175                         cache_drop(ncp);
176                         vput(vp);
177                         return (EINVAL);
178                 }
179                 mp = vp->v_mount;
180                 flag = mp->mnt_flag;
181                 flag2 = mp->mnt_kern_flag;
182                 /*
183                  * We only allow the filesystem to be reloaded if it
184                  * is currently mounted read-only.
185                  */
186                 if ((uap->flags & MNT_RELOAD) &&
187                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
188                         cache_drop(ncp);
189                         vput(vp);
190                         return (EOPNOTSUPP);    /* Needs translation */
191                 }
192                 /*
193                  * Only root, or the user that did the original mount is
194                  * permitted to update it.
195                  */
196                 if (mp->mnt_stat.f_owner != cred->cr_uid &&
197                     (error = suser(td))) {
198                         cache_drop(ncp);
199                         vput(vp);
200                         return (error);
201                 }
202                 if (vfs_busy(mp, LK_NOWAIT)) {
203                         cache_drop(ncp);
204                         vput(vp);
205                         return (EBUSY);
206                 }
207                 if ((vp->v_flag & VMOUNT) != 0 ||
208                     vp->v_mountedhere != NULL) {
209                         cache_drop(ncp);
210                         vfs_unbusy(mp);
211                         vput(vp);
212                         return (EBUSY);
213                 }
214                 vp->v_flag |= VMOUNT;
215                 mp->mnt_flag |=
216                     uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
217                 VOP_UNLOCK(vp, 0);
218                 goto update;
219         }
220         /*
221          * If the user is not root, ensure that they own the directory
222          * onto which we are attempting to mount.
223          */
224         if ((error = VOP_GETATTR(vp, &va)) ||
225             (va.va_uid != cred->cr_uid && (error = suser(td)))) {
226                 cache_drop(ncp);
227                 vput(vp);
228                 return (error);
229         }
230         if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
231                 cache_drop(ncp);
232                 vput(vp);
233                 return (error);
234         }
235         if (vp->v_type != VDIR) {
236                 cache_drop(ncp);
237                 vput(vp);
238                 return (ENOTDIR);
239         }
240         if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
241                 cache_drop(ncp);
242                 vput(vp);
243                 return (error);
244         }
245         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
246                 if (!strcmp(vfsp->vfc_name, fstypename))
247                         break;
248         }
249         if (vfsp == NULL) {
250                 linker_file_t lf;
251
252                 /* Only load modules for root (very important!) */
253                 if ((error = suser(td)) != 0) {
254                         cache_drop(ncp);
255                         vput(vp);
256                         return error;
257                 }
258                 error = linker_load_file(fstypename, &lf);
259                 if (error || lf == NULL) {
260                         cache_drop(ncp);
261                         vput(vp);
262                         if (lf == NULL)
263                                 error = ENODEV;
264                         return error;
265                 }
266                 lf->userrefs++;
267                 /* lookup again, see if the VFS was loaded */
268                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
269                         if (!strcmp(vfsp->vfc_name, fstypename))
270                                 break;
271                 }
272                 if (vfsp == NULL) {
273                         lf->userrefs--;
274                         linker_file_unload(lf);
275                         cache_drop(ncp);
276                         vput(vp);
277                         return (ENODEV);
278                 }
279         }
280         if ((vp->v_flag & VMOUNT) != 0 ||
281             vp->v_mountedhere != NULL) {
282                 cache_drop(ncp);
283                 vput(vp);
284                 return (EBUSY);
285         }
286         vp->v_flag |= VMOUNT;
287
288         /*
289          * Allocate and initialize the filesystem.
290          */
291         mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
292         TAILQ_INIT(&mp->mnt_nvnodelist);
293         TAILQ_INIT(&mp->mnt_reservedvnlist);
294         TAILQ_INIT(&mp->mnt_jlist);
295         mp->mnt_nvnodelistsize = 0;
296         lockinit(&mp->mnt_lock, "vfslock", 0, LK_NOPAUSE);
297         vfs_busy(mp, LK_NOWAIT);
298         mp->mnt_op = vfsp->vfc_vfsops;
299         mp->mnt_vfc = vfsp;
300         vfsp->vfc_refcount++;
301         mp->mnt_stat.f_type = vfsp->vfc_typenum;
302         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
303         strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
304         mp->mnt_vnodecovered = vp;
305         mp->mnt_stat.f_owner = cred->cr_uid;
306         mp->mnt_iosize_max = DFLTPHYS;
307         VOP_UNLOCK(vp, 0);
308 update:
309         /*
310          * Set the mount level flags.
311          */
312         if (uap->flags & MNT_RDONLY)
313                 mp->mnt_flag |= MNT_RDONLY;
314         else if (mp->mnt_flag & MNT_RDONLY)
315                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
316         mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
317             MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
318             MNT_NOSYMFOLLOW | MNT_IGNORE |
319             MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
320         mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
321             MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
322             MNT_NOSYMFOLLOW | MNT_IGNORE |
323             MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
324         /*
325          * Mount the filesystem.
326          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
327          * get. 
328          */
329         error = VFS_MOUNT(mp, uap->path, uap->data, cred);
330         if (mp->mnt_flag & MNT_UPDATE) {
331                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
332                         mp->mnt_flag &= ~MNT_RDONLY;
333                 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
334                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
335                 if (error) {
336                         mp->mnt_flag = flag;
337                         mp->mnt_kern_flag = flag2;
338                 }
339                 vfs_unbusy(mp);
340                 vp->v_flag &= ~VMOUNT;
341                 vrele(vp);
342                 cache_drop(ncp);
343                 return (error);
344         }
345         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
346         /*
347          * Put the new filesystem on the mount list after root.  The mount
348          * point gets its own mnt_ncp which is a special ncp linking the
349          * vnode-under to the root of the new mount.  The lookup code
350          * detects the mount point going forward and detects the special
351          * mnt_ncp via NCP_MOUNTPT going backwards.
352          *
353          * It is not necessary to invalidate or purge the vnode underneath
354          * because elements under the mount will be given their own glue
355          * namecache record.
356          */
357         if (!error) {
358                 nlc.nlc_nameptr = "";
359                 nlc.nlc_namelen = 0;
360                 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
361                 cache_setunresolved(mp->mnt_ncp);
362                 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
363                 mp->mnt_ncp->nc_mount = mp;
364                 cache_drop(ncp);
365                 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
366                 vp->v_flag &= ~VMOUNT;
367                 vp->v_mountedhere = mp;
368                 mountlist_insert(mp, MNTINS_LAST);
369                 checkdirs(vp, mp->mnt_ncp);
370                 cache_unlock(mp->mnt_ncp);      /* leave ref intact */
371                 VOP_UNLOCK(vp, 0);
372                 error = vfs_allocate_syncvnode(mp);
373                 vfs_unbusy(mp);
374                 if ((error = VFS_START(mp, 0)) != 0)
375                         vrele(vp);
376         } else {
377                 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
378                 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
379                 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
380                 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
381                 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
382                 vp->v_flag &= ~VMOUNT;
383                 mp->mnt_vfc->vfc_refcount--;
384                 vfs_unbusy(mp);
385                 free(mp, M_MOUNT);
386                 cache_drop(ncp);
387                 vput(vp);
388         }
389         return (error);
390 }
391
392 /*
393  * Scan all active processes to see if any of them have a current
394  * or root directory onto which the new filesystem has just been
395  * mounted. If so, replace them with the new mount point.
396  *
397  * The passed ncp is ref'd and locked (from the mount code) and
398  * must be associated with the vnode representing the root of the
399  * mount point.
400  */
401 static void
402 checkdirs(struct vnode *olddp, struct namecache *ncp)
403 {
404         struct filedesc *fdp;
405         struct vnode *newdp;
406         struct mount *mp;
407         struct proc *p;
408
409         if (olddp->v_usecount == 1)
410                 return;
411         mp = olddp->v_mountedhere;
412         if (VFS_ROOT(mp, &newdp))
413                 panic("mount: lost mount");
414         cache_setvp(ncp, newdp);
415
416         if (rootvnode == olddp) {
417                 vref(newdp);
418                 vfs_cache_setroot(newdp, cache_hold(ncp));
419         }
420
421         FOREACH_PROC_IN_SYSTEM(p) {
422                 fdp = p->p_fd;
423                 if (fdp->fd_cdir == olddp) {
424                         vrele(fdp->fd_cdir);
425                         vref(newdp);
426                         fdp->fd_cdir = newdp;
427                         cache_drop(fdp->fd_ncdir);
428                         fdp->fd_ncdir = cache_hold(ncp);
429                 }
430                 if (fdp->fd_rdir == olddp) {
431                         vrele(fdp->fd_rdir);
432                         vref(newdp);
433                         fdp->fd_rdir = newdp;
434                         cache_drop(fdp->fd_nrdir);
435                         fdp->fd_nrdir = cache_hold(ncp);
436                 }
437         }
438         vput(newdp);
439 }
440
441 /*
442  * Unmount a file system.
443  *
444  * Note: unmount takes a path to the vnode mounted on as argument,
445  * not special file (as before).
446  */
447 /*
448  * umount_args(char *path, int flags)
449  */
450 /* ARGSUSED */
451 int
452 unmount(struct unmount_args *uap)
453 {
454         struct thread *td = curthread;
455         struct proc *p = td->td_proc;
456         struct mount *mp = NULL;
457         int error;
458         struct nlookupdata nd;
459
460         KKASSERT(p);
461         if (p->p_ucred->cr_prison != NULL)
462                 return (EPERM);
463         if (usermount == 0 && (error = suser(td)))
464                 return (error);
465
466         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
467         if (error == 0)
468                 error = nlookup(&nd);
469         if (error)
470                 goto out;
471
472         mp = nd.nl_ncp->nc_mount;
473
474         /*
475          * Only root, or the user that did the original mount is
476          * permitted to unmount this filesystem.
477          */
478         if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
479             (error = suser(td)))
480                 goto out;
481
482         /*
483          * Don't allow unmounting the root file system.
484          */
485         if (mp->mnt_flag & MNT_ROOTFS) {
486                 error = EINVAL;
487                 goto out;
488         }
489
490         /*
491          * Must be the root of the filesystem
492          */
493         if (! (nd.nl_ncp->nc_flag & NCF_MOUNTPT)) {
494                 error = EINVAL;
495                 goto out;
496         }
497
498 out:
499         nlookup_done(&nd);
500         if (error)
501                 return (error);
502         return (dounmount(mp, uap->flags));
503 }
504
505 /*
506  * Do the actual file system unmount.
507  */
508 static int
509 dounmount_interlock(struct mount *mp)
510 {
511         if (mp->mnt_kern_flag & MNTK_UNMOUNT)
512                 return (EBUSY);
513         mp->mnt_kern_flag |= MNTK_UNMOUNT;
514         return(0);
515 }
516
517 int
518 dounmount(struct mount *mp, int flags)
519 {
520         struct vnode *coveredvp;
521         int error;
522         int async_flag;
523         int lflags;
524
525         /*
526          * Exclusive access for unmounting purposes
527          */
528         if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
529                 return (error);
530
531         /*
532          * Allow filesystems to detect that a forced unmount is in progress.
533          */
534         if (flags & MNT_FORCE)
535                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
536         lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
537         error = lockmgr(&mp->mnt_lock, lflags);
538         if (error) {
539                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
540                 if (mp->mnt_kern_flag & MNTK_MWAIT)
541                         wakeup(mp);
542                 return (error);
543         }
544
545         if (mp->mnt_flag & MNT_EXPUBLIC)
546                 vfs_setpublicfs(NULL, NULL, NULL);
547
548         vfs_msync(mp, MNT_WAIT);
549         async_flag = mp->mnt_flag & MNT_ASYNC;
550         mp->mnt_flag &=~ MNT_ASYNC;
551         cache_purgevfs(mp);     /* remove cache entries for this file sys */
552         if (mp->mnt_syncer != NULL)
553                 vrele(mp->mnt_syncer);
554         if (((mp->mnt_flag & MNT_RDONLY) ||
555              (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
556             (flags & MNT_FORCE))
557                 error = VFS_UNMOUNT(mp, flags);
558         if (error) {
559                 if (mp->mnt_syncer == NULL)
560                         vfs_allocate_syncvnode(mp);
561                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
562                 mp->mnt_flag |= async_flag;
563                 lockmgr(&mp->mnt_lock, LK_RELEASE);
564                 if (mp->mnt_kern_flag & MNTK_MWAIT)
565                         wakeup(mp);
566                 return (error);
567         }
568         /*
569          * Clean up any journals still associated with the mount after
570          * filesystem activity has ceased.
571          */
572         journal_remove_all_journals(mp, 
573             ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
574
575         mountlist_remove(mp);
576
577         /*
578          * Remove any installed vnode ops here so the individual VFSs don't
579          * have to.
580          */
581         vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
582         vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
583         vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
584         vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
585         vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
586
587         if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
588                 coveredvp->v_mountedhere = NULL;
589                 vrele(coveredvp);
590                 cache_drop(mp->mnt_ncp);
591                 mp->mnt_ncp = NULL;
592         }
593         mp->mnt_vfc->vfc_refcount--;
594         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
595                 panic("unmount: dangling vnode");
596         lockmgr(&mp->mnt_lock, LK_RELEASE);
597         if (mp->mnt_kern_flag & MNTK_MWAIT)
598                 wakeup(mp);
599         free(mp, M_MOUNT);
600         return (0);
601 }
602
603 /*
604  * Sync each mounted filesystem.
605  */
606
607 #ifdef DEBUG
608 static int syncprt = 0;
609 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
610 #endif /* DEBUG */
611
612 static int sync_callback(struct mount *mp, void *data);
613
614 /* ARGSUSED */
615 int
616 sync(struct sync_args *uap)
617 {
618         mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
619 #ifdef DEBUG
620         /*
621          * print out buffer pool stat information on each sync() call.
622          */
623         if (syncprt)
624                 vfs_bufstats();
625 #endif /* DEBUG */
626         return (0);
627 }
628
629 static
630 int
631 sync_callback(struct mount *mp, void *data __unused)
632 {
633         int asyncflag;
634
635         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
636                 asyncflag = mp->mnt_flag & MNT_ASYNC;
637                 mp->mnt_flag &= ~MNT_ASYNC;
638                 vfs_msync(mp, MNT_NOWAIT);
639                 VFS_SYNC(mp, MNT_NOWAIT);
640                 mp->mnt_flag |= asyncflag;
641         }
642         return(0);
643 }
644
645 /* XXX PRISON: could be per prison flag */
646 static int prison_quotas;
647 #if 0
648 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
649 #endif
650
651 /*
652  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
653  *
654  * Change filesystem quotas.
655  */
656 /* ARGSUSED */
657 int
658 quotactl(struct quotactl_args *uap)
659 {
660         struct nlookupdata nd;
661         struct thread *td;
662         struct proc *p;
663         struct mount *mp;
664         int error;
665
666         td = curthread;
667         p = td->td_proc;
668         if (p->p_ucred->cr_prison && !prison_quotas)
669                 return (EPERM);
670
671         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
672         if (error == 0)
673                 error = nlookup(&nd);
674         if (error == 0) {
675                 mp = nd.nl_ncp->nc_mount;
676                 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
677                                     uap->arg, nd.nl_cred);
678         }
679         nlookup_done(&nd);
680         return (error);
681 }
682
683 /*
684  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
685  *              void *buf, int buflen)
686  *
687  * This function operates on a mount point and executes the specified
688  * operation using the specified control data, and possibly returns data.
689  *
690  * The actual number of bytes stored in the result buffer is returned, 0
691  * if none, otherwise an error is returned.
692  */
693 /* ARGSUSED */
694 int
695 mountctl(struct mountctl_args *uap)
696 {
697         struct thread *td = curthread;
698         struct proc *p = td->td_proc;
699         struct file *fp;
700         void *ctl = NULL;
701         void *buf = NULL;
702         char *path = NULL;
703         int error;
704
705         /*
706          * Sanity and permissions checks.  We must be root.
707          */
708         KKASSERT(p);
709         if (p->p_ucred->cr_prison != NULL)
710                 return (EPERM);
711         if ((error = suser(td)) != 0)
712                 return (error);
713
714         /*
715          * Argument length checks
716          */
717         if (uap->ctllen < 0 || uap->ctllen > 1024)
718                 return (EINVAL);
719         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
720                 return (EINVAL);
721         if (uap->path == NULL)
722                 return (EINVAL);
723
724         /*
725          * Allocate the necessary buffers and copyin data
726          */
727         path = zalloc(namei_zone);
728         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
729         if (error)
730                 goto done;
731
732         if (uap->ctllen) {
733                 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
734                 error = copyin(uap->ctl, ctl, uap->ctllen);
735                 if (error)
736                         goto done;
737         }
738         if (uap->buflen)
739                 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
740
741         /*
742          * Validate the descriptor
743          */
744         fp = holdfp(p->p_fd, uap->fd, -1);
745         if (fp == NULL) {
746                 error = EBADF;
747                 goto done;
748         }
749
750         /*
751          * Execute the internal kernel function and clean up.
752          */
753         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
754         if (fp)
755                 fdrop(fp);
756         if (error == 0 && uap->sysmsg_result > 0)
757                 error = copyout(buf, uap->buf, uap->sysmsg_result);
758 done:
759         if (path)
760                 zfree(namei_zone, path);
761         if (ctl)
762                 free(ctl, M_TEMP);
763         if (buf)
764                 free(buf, M_TEMP);
765         return (error);
766 }
767
768 /*
769  * Execute a mount control operation by resolving the path to a mount point
770  * and calling vop_mountctl().  
771  */
772 int
773 kern_mountctl(const char *path, int op, struct file *fp, 
774                 const void *ctl, int ctllen, 
775                 void *buf, int buflen, int *res)
776 {
777         struct vnode *vp;
778         struct mount *mp;
779         struct nlookupdata nd;
780         int error;
781
782         *res = 0;
783         vp = NULL;
784         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
785         if (error == 0)
786                 error = nlookup(&nd);
787         if (error == 0)
788                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
789         nlookup_done(&nd);
790         if (error)
791                 return (error);
792
793         mp = vp->v_mount;
794
795         /*
796          * Must be the root of the filesystem
797          */
798         if ((vp->v_flag & VROOT) == 0) {
799                 vput(vp);
800                 return (EINVAL);
801         }
802         error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, 
803                                 buf, buflen, res);
804         vput(vp);
805         return (error);
806 }
807
808 int
809 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
810 {
811         struct thread *td = curthread;
812         struct proc *p = td->td_proc;
813         struct mount *mp;
814         struct statfs *sp;
815         char *fullpath, *freepath;
816         int error;
817
818         if ((error = nlookup(nd)) != 0)
819                 return (error);
820         mp = nd->nl_ncp->nc_mount;
821         sp = &mp->mnt_stat;
822         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
823                 return (error);
824
825         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
826         if (error)
827                 return(error);
828         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
829         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
830         free(freepath, M_TEMP);
831
832         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
833         bcopy(sp, buf, sizeof(*buf));
834         /* Only root should have access to the fsid's. */
835         if (suser(td))
836                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
837         return (0);
838 }
839
840 /*
841  * statfs_args(char *path, struct statfs *buf)
842  *
843  * Get filesystem statistics.
844  */
845 int
846 statfs(struct statfs_args *uap)
847 {
848         struct nlookupdata nd;
849         struct statfs buf;
850         int error;
851
852         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
853         if (error == 0)
854                 error = kern_statfs(&nd, &buf);
855         nlookup_done(&nd);
856         if (error == 0)
857                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
858         return (error);
859 }
860
861 int
862 kern_fstatfs(int fd, struct statfs *buf)
863 {
864         struct thread *td = curthread;
865         struct proc *p = td->td_proc;
866         struct file *fp;
867         struct mount *mp;
868         struct statfs *sp;
869         char *fullpath, *freepath;
870         int error;
871
872         KKASSERT(p);
873         error = getvnode(p->p_fd, fd, &fp);
874         if (error)
875                 return (error);
876         mp = ((struct vnode *)fp->f_data)->v_mount;
877         if (mp == NULL)
878                 return (EBADF);
879         if (fp->f_cred == NULL)
880                 return (EINVAL);
881         sp = &mp->mnt_stat;
882         error = VFS_STATFS(mp, sp, fp->f_cred);
883         if (error)
884                 return (error);
885
886         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
887         if (error)
888                 return(error);
889         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
890         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
891         free(freepath, M_TEMP);
892
893         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
894         bcopy(sp, buf, sizeof(*buf));
895
896         /* Only root should have access to the fsid's. */
897         if (suser(td))
898                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
899         return (0);
900 }
901
902 /*
903  * fstatfs_args(int fd, struct statfs *buf)
904  *
905  * Get filesystem statistics.
906  */
907 int
908 fstatfs(struct fstatfs_args *uap)
909 {
910         struct statfs buf;
911         int error;
912
913         error = kern_fstatfs(uap->fd, &buf);
914
915         if (error == 0)
916                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
917         return (error);
918 }
919
920 /*
921  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
922  *
923  * Get statistics on all filesystems.
924  */
925
926 struct getfsstat_info {
927         struct statfs *sfsp;
928         long count;
929         long maxcount;
930         int error;
931         int flags;
932         int is_chrooted;
933         struct proc *p;
934 };
935
936 static int getfsstat_callback(struct mount *, void *);
937
938 /* ARGSUSED */
939 int
940 getfsstat(struct getfsstat_args *uap)
941 {
942         struct thread *td = curthread;
943         struct proc *p = td->td_proc;
944         struct getfsstat_info info;
945
946         bzero(&info, sizeof(info));
947         if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
948                 info.is_chrooted = 1;
949         else
950                 info.is_chrooted = 0;
951
952         info.maxcount = uap->bufsize / sizeof(struct statfs);
953         info.sfsp = uap->buf;
954         info.count = 0;
955         info.flags = uap->flags;
956         info.p = p;
957
958         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
959         if (info.sfsp && info.count > info.maxcount)
960                 uap->sysmsg_result = info.maxcount;
961         else
962                 uap->sysmsg_result = info.count;
963         return (info.error);
964 }
965
966 static int
967 getfsstat_callback(struct mount *mp, void *data)
968 {
969         struct getfsstat_info *info = data;
970         struct statfs *sp;
971         char *freepath;
972         char *fullpath;
973         int error;
974
975         if (info->sfsp && info->count < info->maxcount) {
976                 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
977                         return(0);
978                 sp = &mp->mnt_stat;
979
980                 /*
981                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
982                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
983                  * overrides MNT_WAIT.
984                  */
985                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
986                     (info->flags & MNT_WAIT)) &&
987                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
988                         return(0);
989                 }
990                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
991
992                 error = cache_fullpath(info->p, mp->mnt_ncp,
993                                         &fullpath, &freepath);
994                 if (error) {
995                         info->error = error;
996                         return(-1);
997                 }
998                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
999                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1000                 free(freepath, M_TEMP);
1001
1002                 error = copyout(sp, info->sfsp, sizeof(*sp));
1003                 if (error) {
1004                         info->error = error;
1005                         return (-1);
1006                 }
1007                 ++info->sfsp;
1008         }
1009         info->count++;
1010         return(0);
1011 }
1012
1013 /*
1014  * fchdir_args(int fd)
1015  *
1016  * Change current working directory to a given file descriptor.
1017  */
1018 /* ARGSUSED */
1019 int
1020 fchdir(struct fchdir_args *uap)
1021 {
1022         struct thread *td = curthread;
1023         struct proc *p = td->td_proc;
1024         struct filedesc *fdp = p->p_fd;
1025         struct vnode *vp, *ovp;
1026         struct mount *mp;
1027         struct file *fp;
1028         struct namecache *ncp, *oncp;
1029         struct namecache *nct;
1030         int error;
1031
1032         if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
1033                 return (error);
1034         vp = (struct vnode *)fp->f_data;
1035         vref(vp);
1036         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1037         if (vp->v_type != VDIR || fp->f_ncp == NULL)
1038                 error = ENOTDIR;
1039         else
1040                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1041         if (error) {
1042                 vput(vp);
1043                 return (error);
1044         }
1045         ncp = cache_hold(fp->f_ncp);
1046         while (!error && (mp = vp->v_mountedhere) != NULL) {
1047                 error = nlookup_mp(mp, &nct);
1048                 if (error == 0) {
1049                         cache_unlock(nct);      /* leave ref intact */
1050                         vput(vp);
1051                         vp = nct->nc_vp;
1052                         error = vget(vp, LK_SHARED);
1053                         KKASSERT(error == 0);
1054                         cache_drop(ncp);
1055                         ncp = nct;
1056                 }
1057         }
1058         if (error == 0) {
1059                 ovp = fdp->fd_cdir;
1060                 oncp = fdp->fd_ncdir;
1061                 VOP_UNLOCK(vp, 0);      /* leave ref intact */
1062                 fdp->fd_cdir = vp;
1063                 fdp->fd_ncdir = ncp;
1064                 cache_drop(oncp);
1065                 vrele(ovp);
1066         } else {
1067                 cache_drop(ncp);
1068                 vput(vp);
1069         }
1070         return (error);
1071 }
1072
1073 int
1074 kern_chdir(struct nlookupdata *nd)
1075 {
1076         struct thread *td = curthread;
1077         struct proc *p = td->td_proc;
1078         struct filedesc *fdp = p->p_fd;
1079         struct vnode *vp, *ovp;
1080         struct namecache *oncp;
1081         int error;
1082
1083         if ((error = nlookup(nd)) != 0)
1084                 return (error);
1085         if ((vp = nd->nl_ncp->nc_vp) == NULL)
1086                 return (ENOENT);
1087         if ((error = vget(vp, LK_SHARED)) != 0)
1088                 return (error);
1089
1090         error = checkvp_chdir(vp, td);
1091         VOP_UNLOCK(vp, 0);
1092         if (error == 0) {
1093                 ovp = fdp->fd_cdir;
1094                 oncp = fdp->fd_ncdir;
1095                 cache_unlock(nd->nl_ncp);       /* leave reference intact */
1096                 fdp->fd_ncdir = nd->nl_ncp;
1097                 fdp->fd_cdir = vp;
1098                 cache_drop(oncp);
1099                 vrele(ovp);
1100                 nd->nl_ncp = NULL;
1101         } else {
1102                 vrele(vp);
1103         }
1104         return (error);
1105 }
1106
1107 /*
1108  * chdir_args(char *path)
1109  *
1110  * Change current working directory (``.'').
1111  */
1112 int
1113 chdir(struct chdir_args *uap)
1114 {
1115         struct nlookupdata nd;
1116         int error;
1117
1118         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1119         if (error == 0)
1120                 error = kern_chdir(&nd);
1121         nlookup_done(&nd);
1122         return (error);
1123 }
1124
1125 /*
1126  * Helper function for raised chroot(2) security function:  Refuse if
1127  * any filedescriptors are open directories.
1128  */
1129 static int
1130 chroot_refuse_vdir_fds(fdp)
1131         struct filedesc *fdp;
1132 {
1133         struct vnode *vp;
1134         struct file *fp;
1135         int error;
1136         int fd;
1137
1138         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1139                 error = getvnode(fdp, fd, &fp);
1140                 if (error)
1141                         continue;
1142                 vp = (struct vnode *)fp->f_data;
1143                 if (vp->v_type != VDIR)
1144                         continue;
1145                 return(EPERM);
1146         }
1147         return (0);
1148 }
1149
1150 /*
1151  * This sysctl determines if we will allow a process to chroot(2) if it
1152  * has a directory open:
1153  *      0: disallowed for all processes.
1154  *      1: allowed for processes that were not already chroot(2)'ed.
1155  *      2: allowed for all processes.
1156  */
1157
1158 static int chroot_allow_open_directories = 1;
1159
1160 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1161      &chroot_allow_open_directories, 0, "");
1162
1163 /*
1164  * chroot to the specified namecache entry.  We obtain the vp from the
1165  * namecache data.  The passed ncp must be locked and referenced and will
1166  * remain locked and referenced on return.
1167  */
1168 int
1169 kern_chroot(struct namecache *ncp)
1170 {
1171         struct thread *td = curthread;
1172         struct proc *p = td->td_proc;
1173         struct filedesc *fdp = p->p_fd;
1174         struct vnode *vp;
1175         int error;
1176
1177         /*
1178          * Only root can chroot
1179          */
1180         if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1181                 return (error);
1182
1183         /*
1184          * Disallow open directory descriptors (fchdir() breakouts).
1185          */
1186         if (chroot_allow_open_directories == 0 ||
1187            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1188                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1189                         return (error);
1190         }
1191         if ((vp = ncp->nc_vp) == NULL)
1192                 return (ENOENT);
1193
1194         if ((error = vget(vp, LK_SHARED)) != 0)
1195                 return (error);
1196
1197         /*
1198          * Check the validity of vp as a directory to change to and 
1199          * associate it with rdir/jdir.
1200          */
1201         error = checkvp_chdir(vp, td);
1202         VOP_UNLOCK(vp, 0);      /* leave reference intact */
1203         if (error == 0) {
1204                 vrele(fdp->fd_rdir);
1205                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1206                 cache_drop(fdp->fd_nrdir);
1207                 fdp->fd_nrdir = cache_hold(ncp);
1208                 if (fdp->fd_jdir == NULL) {
1209                         fdp->fd_jdir = vp;
1210                         vref(fdp->fd_jdir);
1211                         fdp->fd_njdir = cache_hold(ncp);
1212                 }
1213         } else {
1214                 vrele(vp);
1215         }
1216         return (error);
1217 }
1218
1219 /*
1220  * chroot_args(char *path)
1221  *
1222  * Change notion of root (``/'') directory.
1223  */
1224 /* ARGSUSED */
1225 int
1226 chroot(struct chroot_args *uap)
1227 {
1228         struct thread *td = curthread;
1229         struct nlookupdata nd;
1230         int error;
1231
1232         KKASSERT(td->td_proc);
1233         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1234         if (error) {
1235                 nlookup_done(&nd);
1236                 return(error);
1237         }
1238         error = nlookup(&nd);
1239         if (error == 0)
1240                 error = kern_chroot(nd.nl_ncp);
1241         nlookup_done(&nd);
1242         return(error);
1243 }
1244
1245 /*
1246  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1247  * determine whether it is legal to chdir to the vnode.  The vnode's state
1248  * is not changed by this call.
1249  */
1250 int
1251 checkvp_chdir(struct vnode *vp, struct thread *td)
1252 {
1253         int error;
1254
1255         if (vp->v_type != VDIR)
1256                 error = ENOTDIR;
1257         else
1258                 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1259         return (error);
1260 }
1261
1262 int
1263 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1264 {
1265         struct thread *td = curthread;
1266         struct proc *p = td->td_proc;
1267         struct lwp *lp = td->td_lwp;
1268         struct filedesc *fdp = p->p_fd;
1269         int cmode, flags;
1270         struct file *nfp;
1271         struct file *fp;
1272         struct vnode *vp;
1273         int type, indx, error;
1274         struct flock lf;
1275
1276         if ((oflags & O_ACCMODE) == O_ACCMODE)
1277                 return (EINVAL);
1278         flags = FFLAGS(oflags);
1279         error = falloc(p, &nfp, NULL);
1280         if (error)
1281                 return (error);
1282         fp = nfp;
1283         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1284
1285         /*
1286          * XXX p_dupfd is a real mess.  It allows a device to return a
1287          * file descriptor to be duplicated rather then doing the open
1288          * itself.
1289          */
1290         lp->lwp_dupfd = -1;
1291
1292         /*
1293          * Call vn_open() to do the lookup and assign the vnode to the 
1294          * file pointer.  vn_open() does not change the ref count on fp
1295          * and the vnode, on success, will be inherited by the file pointer
1296          * and unlocked.
1297          */
1298         nd->nl_flags |= NLC_LOCKVP;
1299         error = vn_open(nd, fp, flags, cmode);
1300         nlookup_done(nd);
1301         if (error) {
1302                 /*
1303                  * handle special fdopen() case.  bleh.  dupfdopen() is
1304                  * responsible for dropping the old contents of ofiles[indx]
1305                  * if it succeeds.
1306                  *
1307                  * Note that if fsetfd() succeeds it will add a ref to fp
1308                  * which represents the fd_files[] assignment.  We must still
1309                  * drop our reference.
1310                  */
1311                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1312                         if (fsetfd(p, fp, &indx) == 0) {
1313                                 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1314                                 if (error == 0) {
1315                                         *res = indx;
1316                                         fdrop(fp);      /* our ref */
1317                                         return (0);
1318                                 }
1319                                 fdealloc(p, fp, indx);
1320                         }
1321                 }
1322                 fdrop(fp);      /* our ref */
1323                 if (error == ERESTART)
1324                         error = EINTR;
1325                 return (error);
1326         }
1327
1328         /*
1329          * ref the vnode for ourselves so it can't be ripped out from under
1330          * is.  XXX need an ND flag to request that the vnode be returned
1331          * anyway.
1332          */
1333         vp = (struct vnode *)fp->f_data;
1334         vref(vp);
1335         if ((error = fsetfd(p, fp, &indx)) != 0) {
1336                 fdrop(fp);
1337                 vrele(vp);
1338                 return (error);
1339         }
1340
1341         /*
1342          * If no error occurs the vp will have been assigned to the file
1343          * pointer.
1344          */
1345         lp->lwp_dupfd = 0;
1346
1347         /*
1348          * There should be 2 references on the file, one from the descriptor
1349          * table, and one for us.
1350          *
1351          * Handle the case where someone closed the file (via its file
1352          * descriptor) while we were blocked.  The end result should look
1353          * like opening the file succeeded but it was immediately closed.
1354          */
1355         if (fp->f_count == 1) {
1356                 KASSERT(fdp->fd_files[indx].fp != fp,
1357                     ("Open file descriptor lost all refs"));
1358                 vrele(vp);
1359                 fo_close(fp);
1360                 fdrop(fp);
1361                 *res = indx;
1362                 return 0;
1363         }
1364
1365         if (flags & (O_EXLOCK | O_SHLOCK)) {
1366                 lf.l_whence = SEEK_SET;
1367                 lf.l_start = 0;
1368                 lf.l_len = 0;
1369                 if (flags & O_EXLOCK)
1370                         lf.l_type = F_WRLCK;
1371                 else
1372                         lf.l_type = F_RDLCK;
1373                 if (flags & FNONBLOCK)
1374                         type = 0;
1375                 else
1376                         type = F_WAIT;
1377
1378                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1379                         /*
1380                          * lock request failed.  Normally close the descriptor
1381                          * but handle the case where someone might have dup()d
1382                          * it when we weren't looking.  One reference is
1383                          * owned by the descriptor array, the other by us.
1384                          */
1385                         vrele(vp);
1386                         fdealloc(p, fp, indx);
1387                         fdrop(fp);
1388                         return (error);
1389                 }
1390                 fp->f_flag |= FHASLOCK;
1391         }
1392
1393 #if 0
1394         /*
1395          * Assert that all regular file vnodes were created with a object.
1396          */
1397         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1398                 ("open: regular file has no backing object after vn_open"));
1399 #endif
1400
1401         vrele(vp);
1402
1403         /*
1404          * release our private reference, leaving the one associated with the
1405          * descriptor table intact.
1406          */
1407         fdrop(fp);
1408         *res = indx;
1409         return (0);
1410 }
1411
1412 /*
1413  * open_args(char *path, int flags, int mode)
1414  *
1415  * Check permissions, allocate an open file structure,
1416  * and call the device open routine if any.
1417  */
1418 int
1419 open(struct open_args *uap)
1420 {
1421         struct nlookupdata nd;
1422         int error;
1423
1424         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1425         if (error == 0) {
1426                 error = kern_open(&nd, uap->flags,
1427                                     uap->mode, &uap->sysmsg_result);
1428         }
1429         nlookup_done(&nd);
1430         return (error);
1431 }
1432
1433 int
1434 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1435 {
1436         struct namecache *ncp;
1437         struct thread *td = curthread;
1438         struct proc *p = td->td_proc;
1439         struct vnode *vp;
1440         struct vattr vattr;
1441         int error;
1442         int whiteout = 0;
1443
1444         KKASSERT(p);
1445
1446         switch (mode & S_IFMT) {
1447         case S_IFCHR:
1448         case S_IFBLK:
1449                 error = suser(td);
1450                 break;
1451         default:
1452                 error = suser_cred(p->p_ucred, PRISON_ROOT);
1453                 break;
1454         }
1455         if (error)
1456                 return (error);
1457
1458         bwillwrite();
1459         nd->nl_flags |= NLC_CREATE;
1460         if ((error = nlookup(nd)) != 0)
1461                 return (error);
1462         ncp = nd->nl_ncp;
1463         if (ncp->nc_vp)
1464                 return (EEXIST);
1465
1466         VATTR_NULL(&vattr);
1467         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1468         vattr.va_rdev = dev;
1469         whiteout = 0;
1470
1471         switch (mode & S_IFMT) {
1472         case S_IFMT:    /* used by badsect to flag bad sectors */
1473                 vattr.va_type = VBAD;
1474                 break;
1475         case S_IFCHR:
1476                 vattr.va_type = VCHR;
1477                 break;
1478         case S_IFBLK:
1479                 vattr.va_type = VBLK;
1480                 break;
1481         case S_IFWHT:
1482                 whiteout = 1;
1483                 break;
1484         default:
1485                 error = EINVAL;
1486                 break;
1487         }
1488         if (error == 0) {
1489                 if (whiteout) {
1490                         error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1491                 } else {
1492                         vp = NULL;
1493                         error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1494                         if (error == 0)
1495                                 vput(vp);
1496                 }
1497         }
1498         return (error);
1499 }
1500
1501 /*
1502  * mknod_args(char *path, int mode, int dev)
1503  *
1504  * Create a special file.
1505  */
1506 int
1507 mknod(struct mknod_args *uap)
1508 {
1509         struct nlookupdata nd;
1510         int error;
1511
1512         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1513         if (error == 0)
1514                 error = kern_mknod(&nd, uap->mode, uap->dev);
1515         nlookup_done(&nd);
1516         return (error);
1517 }
1518
1519 int
1520 kern_mkfifo(struct nlookupdata *nd, int mode)
1521 {
1522         struct namecache *ncp;
1523         struct thread *td = curthread;
1524         struct proc *p = td->td_proc;
1525         struct vattr vattr;
1526         struct vnode *vp;
1527         int error;
1528
1529         bwillwrite();
1530
1531         nd->nl_flags |= NLC_CREATE;
1532         if ((error = nlookup(nd)) != 0)
1533                 return (error);
1534         ncp = nd->nl_ncp;
1535         if (ncp->nc_vp)
1536                 return (EEXIST);
1537
1538         VATTR_NULL(&vattr);
1539         vattr.va_type = VFIFO;
1540         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1541         vp = NULL;
1542         error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1543         if (error == 0)
1544                 vput(vp);
1545         return (error);
1546 }
1547
1548 /*
1549  * mkfifo_args(char *path, int mode)
1550  *
1551  * Create a named pipe.
1552  */
1553 int
1554 mkfifo(struct mkfifo_args *uap)
1555 {
1556         struct nlookupdata nd;
1557         int error;
1558
1559         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1560         if (error == 0)
1561                 error = kern_mkfifo(&nd, uap->mode);
1562         nlookup_done(&nd);
1563         return (error);
1564 }
1565
1566 static int hardlink_check_uid = 0;
1567 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1568     &hardlink_check_uid, 0, 
1569     "Unprivileged processes cannot create hard links to files owned by other "
1570     "users");
1571 static int hardlink_check_gid = 0;
1572 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1573     &hardlink_check_gid, 0,
1574     "Unprivileged processes cannot create hard links to files owned by other "
1575     "groups");
1576
1577 static int
1578 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1579 {
1580         struct vattr va;
1581         int error;
1582
1583         /*
1584          * Shortcut if disabled
1585          */
1586         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1587                 return (0);
1588
1589         /*
1590          * root cred can always hardlink
1591          */
1592         if (suser_cred(cred, PRISON_ROOT) == 0)
1593                 return (0);
1594
1595         /*
1596          * Otherwise only if the originating file is owned by the
1597          * same user or group.  Note that any group is allowed if
1598          * the file is owned by the caller.
1599          */
1600         error = VOP_GETATTR(vp, &va);
1601         if (error != 0)
1602                 return (error);
1603         
1604         if (hardlink_check_uid) {
1605                 if (cred->cr_uid != va.va_uid)
1606                         return (EPERM);
1607         }
1608         
1609         if (hardlink_check_gid) {
1610                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1611                         return (EPERM);
1612         }
1613
1614         return (0);
1615 }
1616
1617 int
1618 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1619 {
1620         struct thread *td = curthread;
1621         struct vnode *vp;
1622         int error;
1623
1624         /*
1625          * Lookup the source and obtained a locked vnode.
1626          *
1627          * XXX relookup on vget failure / race ?
1628          */
1629         bwillwrite();
1630         if ((error = nlookup(nd)) != 0)
1631                 return (error);
1632         vp = nd->nl_ncp->nc_vp;
1633         KKASSERT(vp != NULL);
1634         if (vp->v_type == VDIR)
1635                 return (EPERM);         /* POSIX */
1636         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1637                 return (error);
1638
1639         /*
1640          * Unlock the source so we can lookup the target without deadlocking
1641          * (XXX vp is locked already, possible other deadlock?).  The target
1642          * must not exist.
1643          */
1644         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1645         nd->nl_flags &= ~NLC_NCPISLOCKED;
1646         cache_unlock(nd->nl_ncp);
1647
1648         linknd->nl_flags |= NLC_CREATE;
1649         if ((error = nlookup(linknd)) != 0) {
1650                 vput(vp);
1651                 return (error);
1652         }
1653         if (linknd->nl_ncp->nc_vp) {
1654                 vput(vp);
1655                 return (EEXIST);
1656         }
1657
1658         /*
1659          * Finally run the new API VOP.
1660          */
1661         error = can_hardlink(vp, td, td->td_proc->p_ucred);
1662         if (error == 0)
1663                 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1664         vput(vp);
1665         return (error);
1666 }
1667
1668 /*
1669  * link_args(char *path, char *link)
1670  *
1671  * Make a hard file link.
1672  */
1673 int
1674 link(struct link_args *uap)
1675 {
1676         struct nlookupdata nd, linknd;
1677         int error;
1678
1679         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1680         if (error == 0) {
1681                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1682                 if (error == 0)
1683                         error = kern_link(&nd, &linknd);
1684                 nlookup_done(&linknd);
1685         }
1686         nlookup_done(&nd);
1687         return (error);
1688 }
1689
1690 int
1691 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1692 {
1693         struct namecache *ncp;
1694         struct vattr vattr;
1695         struct vnode *vp;
1696         int error;
1697
1698         bwillwrite();
1699         nd->nl_flags |= NLC_CREATE;
1700         if ((error = nlookup(nd)) != 0)
1701                 return (error);
1702         ncp = nd->nl_ncp;
1703         if (ncp->nc_vp)
1704                 return (EEXIST);
1705
1706         VATTR_NULL(&vattr);
1707         vattr.va_mode = mode;
1708         error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1709         if (error == 0)
1710                 vput(vp);
1711         return (error);
1712 }
1713
1714 /*
1715  * symlink(char *path, char *link)
1716  *
1717  * Make a symbolic link.
1718  */
1719 int
1720 symlink(struct symlink_args *uap)
1721 {
1722         struct thread *td = curthread;
1723         struct nlookupdata nd;
1724         char *path;
1725         int error;
1726         int mode;
1727
1728         path = zalloc(namei_zone);
1729         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1730         if (error == 0) {
1731                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1732                 if (error == 0) {
1733                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1734                         error = kern_symlink(&nd, path, mode);
1735                 }
1736                 nlookup_done(&nd);
1737         }
1738         zfree(namei_zone, path);
1739         return (error);
1740 }
1741
1742 /*
1743  * undelete_args(char *path)
1744  *
1745  * Delete a whiteout from the filesystem.
1746  */
1747 /* ARGSUSED */
1748 int
1749 undelete(struct undelete_args *uap)
1750 {
1751         struct nlookupdata nd;
1752         int error;
1753
1754         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1755         bwillwrite();
1756         nd.nl_flags |= NLC_DELETE;
1757         if (error == 0)
1758                 error = nlookup(&nd);
1759         if (error == 0)
1760                 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1761         nlookup_done(&nd);
1762         return (error);
1763 }
1764
1765 int
1766 kern_unlink(struct nlookupdata *nd)
1767 {
1768         struct namecache *ncp;
1769         int error;
1770
1771         bwillwrite();
1772         nd->nl_flags |= NLC_DELETE;
1773         if ((error = nlookup(nd)) != 0)
1774                 return (error);
1775         ncp = nd->nl_ncp;
1776         error = VOP_NREMOVE(ncp, nd->nl_cred);
1777         return (error);
1778 }
1779
1780 /*
1781  * unlink_args(char *path)
1782  *
1783  * Delete a name from the filesystem.
1784  */
1785 int
1786 unlink(struct unlink_args *uap)
1787 {
1788         struct nlookupdata nd;
1789         int error;
1790
1791         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1792         if (error == 0)
1793                 error = kern_unlink(&nd);
1794         nlookup_done(&nd);
1795         return (error);
1796 }
1797
1798 int
1799 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1800 {
1801         struct thread *td = curthread;
1802         struct proc *p = td->td_proc;
1803         struct file *fp;
1804         struct vattr vattr;
1805         int error;
1806
1807         fp = holdfp(p->p_fd, fd, -1);
1808         if (fp == NULL)
1809                 return (EBADF);
1810         if (fp->f_type != DTYPE_VNODE) {
1811                 error = ESPIPE;
1812                 goto done;
1813         }
1814
1815         switch (whence) {
1816         case L_INCR:
1817                 fp->f_offset += offset;
1818                 error = 0;
1819                 break;
1820         case L_XTND:
1821                 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1822                 if (error == 0)
1823                         fp->f_offset = offset + vattr.va_size;
1824                 break;
1825         case L_SET:
1826                 fp->f_offset = offset;
1827                 error = 0;
1828                 break;
1829         default:
1830                 error = EINVAL;
1831                 break;
1832         }
1833         *res = fp->f_offset;
1834 done:
1835         fdrop(fp);
1836         return (error);
1837 }
1838
1839 /*
1840  * lseek_args(int fd, int pad, off_t offset, int whence)
1841  *
1842  * Reposition read/write file offset.
1843  */
1844 int
1845 lseek(struct lseek_args *uap)
1846 {
1847         int error;
1848
1849         error = kern_lseek(uap->fd, uap->offset, uap->whence,
1850             &uap->sysmsg_offset);
1851
1852         return (error);
1853 }
1854
1855 int
1856 kern_access(struct nlookupdata *nd, int aflags)
1857 {
1858         struct vnode *vp;
1859         int error, flags;
1860
1861         if ((error = nlookup(nd)) != 0)
1862                 return (error);
1863 retry:
1864         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1865         if (error)
1866                 return (error);
1867
1868         /* Flags == 0 means only check for existence. */
1869         if (aflags) {
1870                 flags = 0;
1871                 if (aflags & R_OK)
1872                         flags |= VREAD;
1873                 if (aflags & W_OK)
1874                         flags |= VWRITE;
1875                 if (aflags & X_OK)
1876                         flags |= VEXEC;
1877                 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1878                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
1879
1880                 /*
1881                  * If the file handle is stale we have to re-resolve the
1882                  * entry.  This is a hack at the moment.
1883                  */
1884                 if (error == ESTALE) {
1885                         cache_setunresolved(nd->nl_ncp);
1886                         error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1887                         if (error == 0) {
1888                                 vput(vp);
1889                                 vp = NULL;
1890                                 goto retry;
1891                         }
1892                 }
1893         }
1894         vput(vp);
1895         return (error);
1896 }
1897
1898 /*
1899  * access_args(char *path, int flags)
1900  *
1901  * Check access permissions.
1902  */
1903 int
1904 access(struct access_args *uap)
1905 {
1906         struct nlookupdata nd;
1907         int error;
1908
1909         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1910         if (error == 0)
1911                 error = kern_access(&nd, uap->flags);
1912         nlookup_done(&nd);
1913         return (error);
1914 }
1915
1916 int
1917 kern_stat(struct nlookupdata *nd, struct stat *st)
1918 {
1919         int error;
1920         struct vnode *vp;
1921         thread_t td;
1922
1923         if ((error = nlookup(nd)) != 0)
1924                 return (error);
1925 again:
1926         if ((vp = nd->nl_ncp->nc_vp) == NULL)
1927                 return (ENOENT);
1928
1929         td = curthread;
1930         if ((error = vget(vp, LK_SHARED)) != 0)
1931                 return (error);
1932         error = vn_stat(vp, st, nd->nl_cred);
1933
1934         /*
1935          * If the file handle is stale we have to re-resolve the entry.  This
1936          * is a hack at the moment.
1937          */
1938         if (error == ESTALE) {
1939                 cache_setunresolved(nd->nl_ncp);
1940                 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1941                 if (error == 0) {
1942                         vput(vp);
1943                         goto again;
1944                 }
1945         }
1946         vput(vp);
1947         return (error);
1948 }
1949
1950 /*
1951  * stat_args(char *path, struct stat *ub)
1952  *
1953  * Get file status; this version follows links.
1954  */
1955 int
1956 stat(struct stat_args *uap)
1957 {
1958         struct nlookupdata nd;
1959         struct stat st;
1960         int error;
1961
1962         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1963         if (error == 0) {
1964                 error = kern_stat(&nd, &st);
1965                 if (error == 0)
1966                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1967         }
1968         nlookup_done(&nd);
1969         return (error);
1970 }
1971
1972 /*
1973  * lstat_args(char *path, struct stat *ub)
1974  *
1975  * Get file status; this version does not follow links.
1976  */
1977 int
1978 lstat(struct lstat_args *uap)
1979 {
1980         struct nlookupdata nd;
1981         struct stat st;
1982         int error;
1983
1984         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1985         if (error == 0) {
1986                 error = kern_stat(&nd, &st);
1987                 if (error == 0)
1988                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1989         }
1990         nlookup_done(&nd);
1991         return (error);
1992 }
1993
1994 /*
1995  * pathconf_Args(char *path, int name)
1996  *
1997  * Get configurable pathname variables.
1998  */
1999 /* ARGSUSED */
2000 int
2001 pathconf(struct pathconf_args *uap)
2002 {
2003         struct nlookupdata nd;
2004         struct vnode *vp;
2005         int error;
2006
2007         vp = NULL;
2008         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2009         if (error == 0)
2010                 error = nlookup(&nd);
2011         if (error == 0)
2012                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2013         nlookup_done(&nd);
2014         if (error == 0) {
2015                 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2016                 vput(vp);
2017         }
2018         return (error);
2019 }
2020
2021 /*
2022  * XXX: daver
2023  * kern_readlink isn't properly split yet.  There is a copyin burried
2024  * in VOP_READLINK().
2025  */
2026 int
2027 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2028 {
2029         struct thread *td = curthread;
2030         struct proc *p = td->td_proc;
2031         struct vnode *vp;
2032         struct iovec aiov;
2033         struct uio auio;
2034         int error;
2035
2036         if ((error = nlookup(nd)) != 0)
2037                 return (error);
2038         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2039         if (error)
2040                 return (error);
2041         if (vp->v_type != VLNK) {
2042                 error = EINVAL;
2043         } else {
2044                 aiov.iov_base = buf;
2045                 aiov.iov_len = count;
2046                 auio.uio_iov = &aiov;
2047                 auio.uio_iovcnt = 1;
2048                 auio.uio_offset = 0;
2049                 auio.uio_rw = UIO_READ;
2050                 auio.uio_segflg = UIO_USERSPACE;
2051                 auio.uio_td = td;
2052                 auio.uio_resid = count;
2053                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2054         }
2055         vput(vp);
2056         *res = count - auio.uio_resid;
2057         return (error);
2058 }
2059
2060 /*
2061  * readlink_args(char *path, char *buf, int count)
2062  *
2063  * Return target name of a symbolic link.
2064  */
2065 int
2066 readlink(struct readlink_args *uap)
2067 {
2068         struct nlookupdata nd;
2069         int error;
2070
2071         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2072         if (error == 0) {
2073                 error = kern_readlink(&nd, uap->buf, uap->count,
2074                                         &uap->sysmsg_result);
2075         }
2076         nlookup_done(&nd);
2077         return (error);
2078 }
2079
2080 static int
2081 setfflags(struct vnode *vp, int flags)
2082 {
2083         struct thread *td = curthread;
2084         struct proc *p = td->td_proc;
2085         int error;
2086         struct vattr vattr;
2087
2088         /*
2089          * Prevent non-root users from setting flags on devices.  When
2090          * a device is reused, users can retain ownership of the device
2091          * if they are allowed to set flags and programs assume that
2092          * chown can't fail when done as root.
2093          */
2094         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2095             ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2096                 return (error);
2097
2098         /*
2099          * note: vget is required for any operation that might mod the vnode
2100          * so VINACTIVE is properly cleared.
2101          */
2102         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2103                 VATTR_NULL(&vattr);
2104                 vattr.va_flags = flags;
2105                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2106                 vput(vp);
2107         }
2108         return (error);
2109 }
2110
2111 /*
2112  * chflags(char *path, int flags)
2113  *
2114  * Change flags of a file given a path name.
2115  */
2116 /* ARGSUSED */
2117 int
2118 chflags(struct chflags_args *uap)
2119 {
2120         struct nlookupdata nd;
2121         struct vnode *vp;
2122         int error;
2123
2124         vp = NULL;
2125         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2126         /* XXX Add NLC flag indicating modifying operation? */
2127         if (error == 0)
2128                 error = nlookup(&nd);
2129         if (error == 0)
2130                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2131         nlookup_done(&nd);
2132         if (error == 0) {
2133                 error = setfflags(vp, uap->flags);
2134                 vrele(vp);
2135         }
2136         return (error);
2137 }
2138
2139 /*
2140  * fchflags_args(int fd, int flags)
2141  *
2142  * Change flags of a file given a file descriptor.
2143  */
2144 /* ARGSUSED */
2145 int
2146 fchflags(struct fchflags_args *uap)
2147 {
2148         struct thread *td = curthread;
2149         struct proc *p = td->td_proc;
2150         struct file *fp;
2151         int error;
2152
2153         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2154                 return (error);
2155         return setfflags((struct vnode *) fp->f_data, uap->flags);
2156 }
2157
2158 static int
2159 setfmode(struct vnode *vp, int mode)
2160 {
2161         struct thread *td = curthread;
2162         struct proc *p = td->td_proc;
2163         int error;
2164         struct vattr vattr;
2165
2166         /*
2167          * note: vget is required for any operation that might mod the vnode
2168          * so VINACTIVE is properly cleared.
2169          */
2170         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2171                 VATTR_NULL(&vattr);
2172                 vattr.va_mode = mode & ALLPERMS;
2173                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2174                 vput(vp);
2175         }
2176         return error;
2177 }
2178
2179 int
2180 kern_chmod(struct nlookupdata *nd, int mode)
2181 {
2182         struct vnode *vp;
2183         int error;
2184
2185         /* XXX Add NLC flag indicating modifying operation? */
2186         if ((error = nlookup(nd)) != 0)
2187                 return (error);
2188         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2189                 return (error);
2190         error = setfmode(vp, mode);
2191         vrele(vp);
2192         return (error);
2193 }
2194
2195 /*
2196  * chmod_args(char *path, int mode)
2197  *
2198  * Change mode of a file given path name.
2199  */
2200 /* ARGSUSED */
2201 int
2202 chmod(struct chmod_args *uap)
2203 {
2204         struct nlookupdata nd;
2205         int error;
2206
2207         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2208         if (error == 0)
2209                 error = kern_chmod(&nd, uap->mode);
2210         nlookup_done(&nd);
2211         return (error);
2212 }
2213
2214 /*
2215  * lchmod_args(char *path, int mode)
2216  *
2217  * Change mode of a file given path name (don't follow links.)
2218  */
2219 /* ARGSUSED */
2220 int
2221 lchmod(struct lchmod_args *uap)
2222 {
2223         struct nlookupdata nd;
2224         int error;
2225
2226         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2227         if (error == 0)
2228                 error = kern_chmod(&nd, uap->mode);
2229         nlookup_done(&nd);
2230         return (error);
2231 }
2232
2233 /*
2234  * fchmod_args(int fd, int mode)
2235  *
2236  * Change mode of a file given a file descriptor.
2237  */
2238 /* ARGSUSED */
2239 int
2240 fchmod(struct fchmod_args *uap)
2241 {
2242         struct thread *td = curthread;
2243         struct proc *p = td->td_proc;
2244         struct file *fp;
2245         int error;
2246
2247         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2248                 return (error);
2249         return setfmode((struct vnode *)fp->f_data, uap->mode);
2250 }
2251
2252 static int
2253 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2254 {
2255         struct thread *td = curthread;
2256         struct proc *p = td->td_proc;
2257         int error;
2258         struct vattr vattr;
2259
2260         /*
2261          * note: vget is required for any operation that might mod the vnode
2262          * so VINACTIVE is properly cleared.
2263          */
2264         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2265                 VATTR_NULL(&vattr);
2266                 vattr.va_uid = uid;
2267                 vattr.va_gid = gid;
2268                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2269                 vput(vp);
2270         }
2271         return error;
2272 }
2273
2274 int
2275 kern_chown(struct nlookupdata *nd, int uid, int gid)
2276 {
2277         struct vnode *vp;
2278         int error;
2279
2280         /* XXX Add NLC flag indicating modifying operation? */
2281         if ((error = nlookup(nd)) != 0)
2282                 return (error);
2283         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2284                 return (error);
2285         error = setfown(vp, uid, gid);
2286         vrele(vp);
2287         return (error);
2288 }
2289
2290 /*
2291  * chown(char *path, int uid, int gid)
2292  *
2293  * Set ownership given a path name.
2294  */
2295 int
2296 chown(struct chown_args *uap)
2297 {
2298         struct nlookupdata nd;
2299         int error;
2300
2301         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2302         if (error == 0)
2303                 error = kern_chown(&nd, uap->uid, uap->gid);
2304         nlookup_done(&nd);
2305         return (error);
2306 }
2307
2308 /*
2309  * lchown_args(char *path, int uid, int gid)
2310  *
2311  * Set ownership given a path name, do not cross symlinks.
2312  */
2313 int
2314 lchown(struct lchown_args *uap)
2315 {
2316         struct nlookupdata nd;
2317         int error;
2318
2319         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2320         if (error == 0)
2321                 error = kern_chown(&nd, uap->uid, uap->gid);
2322         nlookup_done(&nd);
2323         return (error);
2324 }
2325
2326 /*
2327  * fchown_args(int fd, int uid, int gid)
2328  *
2329  * Set ownership given a file descriptor.
2330  */
2331 /* ARGSUSED */
2332 int
2333 fchown(struct fchown_args *uap)
2334 {
2335         struct thread *td = curthread;
2336         struct proc *p = td->td_proc;
2337         struct file *fp;
2338         int error;
2339
2340         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2341                 return (error);
2342         return setfown((struct vnode *)fp->f_data,
2343                 uap->uid, uap->gid);
2344 }
2345
2346 static int
2347 getutimes(const struct timeval *tvp, struct timespec *tsp)
2348 {
2349         struct timeval tv[2];
2350
2351         if (tvp == NULL) {
2352                 microtime(&tv[0]);
2353                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2354                 tsp[1] = tsp[0];
2355         } else {
2356                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2357                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2358         }
2359         return 0;
2360 }
2361
2362 static int
2363 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2364 {
2365         struct thread *td = curthread;
2366         struct proc *p = td->td_proc;
2367         int error;
2368         struct vattr vattr;
2369
2370         /*
2371          * note: vget is required for any operation that might mod the vnode
2372          * so VINACTIVE is properly cleared.
2373          */
2374         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2375                 VATTR_NULL(&vattr);
2376                 vattr.va_atime = ts[0];
2377                 vattr.va_mtime = ts[1];
2378                 if (nullflag)
2379                         vattr.va_vaflags |= VA_UTIMES_NULL;
2380                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2381                 vput(vp);
2382         }
2383         return error;
2384 }
2385
2386 int
2387 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2388 {
2389         struct timespec ts[2];
2390         struct vnode *vp;
2391         int error;
2392
2393         if ((error = getutimes(tptr, ts)) != 0)
2394                 return (error);
2395         /* XXX Add NLC flag indicating modifying operation? */
2396         if ((error = nlookup(nd)) != 0)
2397                 return (error);
2398         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2399                 return (error);
2400         error = setutimes(vp, ts, tptr == NULL);
2401         vrele(vp);
2402         return (error);
2403 }
2404
2405 /*
2406  * utimes_args(char *path, struct timeval *tptr)
2407  *
2408  * Set the access and modification times of a file.
2409  */
2410 int
2411 utimes(struct utimes_args *uap)
2412 {
2413         struct timeval tv[2];
2414         struct nlookupdata nd;
2415         int error;
2416
2417         if (uap->tptr) {
2418                 error = copyin(uap->tptr, tv, sizeof(tv));
2419                 if (error)
2420                         return (error);
2421         }
2422         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2423         if (error == 0)
2424                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2425         nlookup_done(&nd);
2426         return (error);
2427 }
2428
2429 /*
2430  * lutimes_args(char *path, struct timeval *tptr)
2431  *
2432  * Set the access and modification times of a file.
2433  */
2434 int
2435 lutimes(struct lutimes_args *uap)
2436 {
2437         struct timeval tv[2];
2438         struct nlookupdata nd;
2439         int error;
2440
2441         if (uap->tptr) {
2442                 error = copyin(uap->tptr, tv, sizeof(tv));
2443                 if (error)
2444                         return (error);
2445         }
2446         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2447         if (error == 0)
2448                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2449         nlookup_done(&nd);
2450         return (error);
2451 }
2452
2453 int
2454 kern_futimes(int fd, struct timeval *tptr)
2455 {
2456         struct thread *td = curthread;
2457         struct proc *p = td->td_proc;
2458         struct timespec ts[2];
2459         struct file *fp;
2460         int error;
2461
2462         error = getutimes(tptr, ts);
2463         if (error)
2464                 return (error);
2465         error = getvnode(p->p_fd, fd, &fp);
2466         if (error)
2467                 return (error);
2468         error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2469         return (error);
2470 }
2471
2472 /*
2473  * futimes_args(int fd, struct timeval *tptr)
2474  *
2475  * Set the access and modification times of a file.
2476  */
2477 int
2478 futimes(struct futimes_args *uap)
2479 {
2480         struct timeval tv[2];
2481         int error;
2482
2483         if (uap->tptr) {
2484                 error = copyin(uap->tptr, tv, sizeof(tv));
2485                 if (error)
2486                         return (error);
2487         }
2488
2489         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2490
2491         return (error);
2492 }
2493
2494 int
2495 kern_truncate(struct nlookupdata *nd, off_t length)
2496 {
2497         struct vnode *vp;
2498         struct vattr vattr;
2499         int error;
2500
2501         if (length < 0)
2502                 return(EINVAL);
2503         /* XXX Add NLC flag indicating modifying operation? */
2504         if ((error = nlookup(nd)) != 0)
2505                 return (error);
2506         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2507                 return (error);
2508         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2509                 vrele(vp);
2510                 return (error);
2511         }
2512         if (vp->v_type == VDIR) {
2513                 error = EISDIR;
2514         } else if ((error = vn_writechk(vp)) == 0 &&
2515             (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2516                 VATTR_NULL(&vattr);
2517                 vattr.va_size = length;
2518                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2519         }
2520         vput(vp);
2521         return (error);
2522 }
2523
2524 /*
2525  * truncate(char *path, int pad, off_t length)
2526  *
2527  * Truncate a file given its path name.
2528  */
2529 int
2530 truncate(struct truncate_args *uap)
2531 {
2532         struct nlookupdata nd;
2533         int error;
2534
2535         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2536         if (error == 0)
2537                 error = kern_truncate(&nd, uap->length);
2538         nlookup_done(&nd);
2539         return error;
2540 }
2541
2542 int
2543 kern_ftruncate(int fd, off_t length)
2544 {
2545         struct thread *td = curthread;
2546         struct proc *p = td->td_proc;
2547         struct vattr vattr;
2548         struct vnode *vp;
2549         struct file *fp;
2550         int error;
2551
2552         if (length < 0)
2553                 return(EINVAL);
2554         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2555                 return (error);
2556         if ((fp->f_flag & FWRITE) == 0)
2557                 return (EINVAL);
2558         vp = (struct vnode *)fp->f_data;
2559         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2560         if (vp->v_type == VDIR)
2561                 error = EISDIR;
2562         else if ((error = vn_writechk(vp)) == 0) {
2563                 VATTR_NULL(&vattr);
2564                 vattr.va_size = length;
2565                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2566         }
2567         VOP_UNLOCK(vp, 0);
2568         return (error);
2569 }
2570
2571 /*
2572  * ftruncate_args(int fd, int pad, off_t length)
2573  *
2574  * Truncate a file given a file descriptor.
2575  */
2576 int
2577 ftruncate(struct ftruncate_args *uap)
2578 {
2579         int error;
2580
2581         error = kern_ftruncate(uap->fd, uap->length);
2582
2583         return (error);
2584 }
2585
2586 /*
2587  * fsync(int fd)
2588  *
2589  * Sync an open file.
2590  */
2591 /* ARGSUSED */
2592 int
2593 fsync(struct fsync_args *uap)
2594 {
2595         struct thread *td = curthread;
2596         struct proc *p = td->td_proc;
2597         struct vnode *vp;
2598         struct file *fp;
2599         vm_object_t obj;
2600         int error;
2601
2602         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2603                 return (error);
2604         vp = (struct vnode *)fp->f_data;
2605         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2606         if ((obj = vp->v_object) != NULL)
2607                 vm_object_page_clean(obj, 0, 0, 0);
2608         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2609             vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2610             bioops.io_fsync)
2611                 error = (*bioops.io_fsync)(vp);
2612         VOP_UNLOCK(vp, 0);
2613         return (error);
2614 }
2615
2616 int
2617 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2618 {
2619         struct namecache *fncpd;
2620         struct namecache *tncpd;
2621         struct namecache *ncp;
2622         struct mount *mp;
2623         int error;
2624
2625         bwillwrite();
2626         if ((error = nlookup(fromnd)) != 0)
2627                 return (error);
2628         if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2629                 return (ENOENT);
2630         cache_hold(fncpd);
2631
2632         /*
2633          * unlock the source ncp so we can lookup the target ncp without
2634          * deadlocking.  The target may or may not exist so we do not check
2635          * for a target vp like kern_mkdir() and other creation functions do.
2636          *
2637          * The source and target directories are ref'd and rechecked after
2638          * everything is relocked to determine if the source or target file
2639          * has been renamed.
2640          */
2641         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2642         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2643         cache_unlock(fromnd->nl_ncp);
2644
2645         tond->nl_flags |= NLC_CREATE;
2646         if ((error = nlookup(tond)) != 0) {
2647                 cache_drop(fncpd);
2648                 return (error);
2649         }
2650         if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2651                 cache_drop(fncpd);
2652                 return (ENOENT);
2653         }
2654         cache_hold(tncpd);
2655
2656         /*
2657          * If the source and target are the same there is nothing to do
2658          */
2659         if (fromnd->nl_ncp == tond->nl_ncp) {
2660                 cache_drop(fncpd);
2661                 cache_drop(tncpd);
2662                 return (0);
2663         }
2664
2665         /*
2666          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
2667          * may have become invalid while it was unlocked, nc_vp and nc_mount
2668          * could be NULL.
2669          */
2670         if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2671                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2672         } else if (fromnd->nl_ncp > tond->nl_ncp) {
2673                 cache_lock(fromnd->nl_ncp);
2674                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2675         } else {
2676                 cache_unlock(tond->nl_ncp);
2677                 cache_lock(fromnd->nl_ncp);
2678                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2679                 cache_lock(tond->nl_ncp);
2680                 cache_resolve(tond->nl_ncp, tond->nl_cred);
2681         }
2682         fromnd->nl_flags |= NLC_NCPISLOCKED;
2683
2684         /*
2685          * make sure the parent directories linkages are the same
2686          */
2687         if (fncpd != fromnd->nl_ncp->nc_parent ||
2688             tncpd != tond->nl_ncp->nc_parent) {
2689                 cache_drop(fncpd);
2690                 cache_drop(tncpd);
2691                 return (ENOENT);
2692         }
2693
2694         /*
2695          * Both the source and target must be within the same filesystem and
2696          * in the same filesystem as their parent directories within the
2697          * namecache topology.
2698          *
2699          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2700          */
2701         mp = fncpd->nc_mount;
2702         if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2703             mp != tond->nl_ncp->nc_mount) {
2704                 cache_drop(fncpd);
2705                 cache_drop(tncpd);
2706                 return (EXDEV);
2707         }
2708
2709         /*
2710          * If the target exists and either the source or target is a directory,
2711          * then both must be directories.
2712          *
2713          * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2714          * become NULL.
2715          */
2716         if (tond->nl_ncp->nc_vp) {
2717                 if (fromnd->nl_ncp->nc_vp == NULL) {
2718                         error = ENOENT;
2719                 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2720                         if (tond->nl_ncp->nc_vp->v_type != VDIR)
2721                                 error = ENOTDIR;
2722                 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2723                         error = EISDIR;
2724                 }
2725         }
2726
2727         /*
2728          * You cannot rename a source into itself or a subdirectory of itself.
2729          * We check this by travsersing the target directory upwards looking
2730          * for a match against the source.
2731          */
2732         if (error == 0) {
2733                 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2734                         if (fromnd->nl_ncp == ncp) {
2735                                 error = EINVAL;
2736                                 break;
2737                         }
2738                 }
2739         }
2740
2741         cache_drop(fncpd);
2742         cache_drop(tncpd);
2743
2744         /*
2745          * Even though the namespaces are different, they may still represent
2746          * hardlinks to the same file.  The filesystem might have a hard time
2747          * with this so we issue a NREMOVE of the source instead of a NRENAME
2748          * when we detect the situation.
2749          */
2750         if (error == 0) {
2751                 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2752                         error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2753                 } else {
2754                         error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, 
2755                                             tond->nl_cred);
2756                 }
2757         }
2758         return (error);
2759 }
2760
2761 /*
2762  * rename_args(char *from, char *to)
2763  *
2764  * Rename files.  Source and destination must either both be directories,
2765  * or both not be directories.  If target is a directory, it must be empty.
2766  */
2767 int
2768 rename(struct rename_args *uap)
2769 {
2770         struct nlookupdata fromnd, tond;
2771         int error;
2772
2773         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2774         if (error == 0) {
2775                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2776                 if (error == 0)
2777                         error = kern_rename(&fromnd, &tond);
2778                 nlookup_done(&tond);
2779         }
2780         nlookup_done(&fromnd);
2781         return (error);
2782 }
2783
2784 int
2785 kern_mkdir(struct nlookupdata *nd, int mode)
2786 {
2787         struct thread *td = curthread;
2788         struct proc *p = td->td_proc;
2789         struct namecache *ncp;
2790         struct vnode *vp;
2791         struct vattr vattr;
2792         int error;
2793
2794         bwillwrite();
2795         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2796         if ((error = nlookup(nd)) != 0)
2797                 return (error);
2798
2799         ncp = nd->nl_ncp;
2800         if (ncp->nc_vp)
2801                 return (EEXIST);
2802
2803         VATTR_NULL(&vattr);
2804         vattr.va_type = VDIR;
2805         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2806
2807         vp = NULL;
2808         error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2809         if (error == 0)
2810                 vput(vp);
2811         return (error);
2812 }
2813
2814 /*
2815  * mkdir_args(char *path, int mode)
2816  *
2817  * Make a directory file.
2818  */
2819 /* ARGSUSED */
2820 int
2821 mkdir(struct mkdir_args *uap)
2822 {
2823         struct nlookupdata nd;
2824         int error;
2825
2826         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2827         if (error == 0)
2828                 error = kern_mkdir(&nd, uap->mode);
2829         nlookup_done(&nd);
2830         return (error);
2831 }
2832
2833 int
2834 kern_rmdir(struct nlookupdata *nd)
2835 {
2836         struct namecache *ncp;
2837         int error;
2838
2839         bwillwrite();
2840         nd->nl_flags |= NLC_DELETE;
2841         if ((error = nlookup(nd)) != 0)
2842                 return (error);
2843
2844         ncp = nd->nl_ncp;
2845         error = VOP_NRMDIR(ncp, nd->nl_cred);
2846         return (error);
2847 }
2848
2849 /*
2850  * rmdir_args(char *path)
2851  *
2852  * Remove a directory file.
2853  */
2854 /* ARGSUSED */
2855 int
2856 rmdir(struct rmdir_args *uap)
2857 {
2858         struct nlookupdata nd;
2859         int error;
2860
2861         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2862         if (error == 0)
2863                 error = kern_rmdir(&nd);
2864         nlookup_done(&nd);
2865         return (error);
2866 }
2867
2868 int
2869 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2870     enum uio_seg direction)
2871 {
2872         struct thread *td = curthread;
2873         struct proc *p = td->td_proc;
2874         struct vnode *vp;
2875         struct file *fp;
2876         struct uio auio;
2877         struct iovec aiov;
2878         long loff;
2879         int error, eofflag;
2880
2881         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2882                 return (error);
2883         if ((fp->f_flag & FREAD) == 0)
2884                 return (EBADF);
2885         vp = (struct vnode *)fp->f_data;
2886 unionread:
2887         if (vp->v_type != VDIR)
2888                 return (EINVAL);
2889         aiov.iov_base = buf;
2890         aiov.iov_len = count;
2891         auio.uio_iov = &aiov;
2892         auio.uio_iovcnt = 1;
2893         auio.uio_rw = UIO_READ;
2894         auio.uio_segflg = direction;
2895         auio.uio_td = td;
2896         auio.uio_resid = count;
2897         /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2898         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2899         loff = auio.uio_offset = fp->f_offset;
2900         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2901         fp->f_offset = auio.uio_offset;
2902         VOP_UNLOCK(vp, 0);
2903         if (error)
2904                 return (error);
2905         if (count == auio.uio_resid) {
2906                 if (union_dircheckp) {
2907                         error = union_dircheckp(td, &vp, fp);
2908                         if (error == -1)
2909                                 goto unionread;
2910                         if (error)
2911                                 return (error);
2912                 }
2913                 if ((vp->v_flag & VROOT) &&
2914                     (vp->v_mount->mnt_flag & MNT_UNION)) {
2915                         struct vnode *tvp = vp;
2916                         vp = vp->v_mount->mnt_vnodecovered;
2917                         vref(vp);
2918                         fp->f_data = vp;
2919                         fp->f_offset = 0;
2920                         vrele(tvp);
2921                         goto unionread;
2922                 }
2923         }
2924         if (basep) {
2925                 *basep = loff;
2926         }
2927         *res = count - auio.uio_resid;
2928         return (error);
2929 }
2930
2931 /*
2932  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2933  *
2934  * Read a block of directory entries in a file system independent format.
2935  */
2936 int
2937 getdirentries(struct getdirentries_args *uap)
2938 {
2939         long base;
2940         int error;
2941
2942         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2943             &uap->sysmsg_result, UIO_USERSPACE);
2944
2945         if (error == 0)
2946                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2947         return (error);
2948 }
2949
2950 /*
2951  * getdents_args(int fd, char *buf, size_t count)
2952  */
2953 int
2954 getdents(struct getdents_args *uap)
2955 {
2956         int error;
2957
2958         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2959             &uap->sysmsg_result, UIO_USERSPACE);
2960
2961         return (error);
2962 }
2963
2964 /*
2965  * umask(int newmask)
2966  *
2967  * Set the mode mask for creation of filesystem nodes.
2968  *
2969  * MP SAFE
2970  */
2971 int
2972 umask(struct umask_args *uap)
2973 {
2974         struct thread *td = curthread;
2975         struct proc *p = td->td_proc;
2976         struct filedesc *fdp;
2977
2978         fdp = p->p_fd;
2979         uap->sysmsg_result = fdp->fd_cmask;
2980         fdp->fd_cmask = uap->newmask & ALLPERMS;
2981         return (0);
2982 }
2983
2984 /*
2985  * revoke(char *path)
2986  *
2987  * Void all references to file by ripping underlying filesystem
2988  * away from vnode.
2989  */
2990 /* ARGSUSED */
2991 int
2992 revoke(struct revoke_args *uap)
2993 {
2994         struct nlookupdata nd;
2995         struct vattr vattr;
2996         struct vnode *vp;
2997         struct ucred *cred;
2998         int error;
2999
3000         vp = NULL;
3001         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3002         if (error == 0)
3003                 error = nlookup(&nd);
3004         if (error == 0)
3005                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3006         cred = crhold(nd.nl_cred);
3007         nlookup_done(&nd);
3008         if (error == 0) {
3009                 if (vp->v_type != VCHR && vp->v_type != VBLK)
3010                         error = EINVAL;
3011                 if (error == 0)
3012                         error = VOP_GETATTR(vp, &vattr);
3013                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3014                         error = suser_cred(cred, PRISON_ROOT);
3015                 if (error == 0 && count_udev(vp->v_udev) > 0) {
3016                         if ((error = vx_lock(vp)) == 0) {
3017                                 VOP_REVOKE(vp, REVOKEALL);
3018                                 vx_unlock(vp);
3019                         }
3020                 }
3021                 vrele(vp);
3022         }
3023         if (cred)
3024                 crfree(cred);
3025         return (error);
3026 }
3027
3028 /*
3029  * getfh_args(char *fname, fhandle_t *fhp)
3030  *
3031  * Get (NFS) file handle
3032  */
3033 int
3034 getfh(struct getfh_args *uap)
3035 {
3036         struct thread *td = curthread;
3037         struct nlookupdata nd;
3038         fhandle_t fh;
3039         struct vnode *vp;
3040         int error;
3041
3042         /*
3043          * Must be super user
3044          */
3045         if ((error = suser(td)) != 0)
3046                 return (error);
3047
3048         vp = NULL;
3049         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3050         if (error == 0)
3051                 error = nlookup(&nd);
3052         if (error == 0)
3053                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3054         nlookup_done(&nd);
3055         if (error == 0) {
3056                 bzero(&fh, sizeof(fh));
3057                 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3058                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3059                 vput(vp);
3060                 if (error == 0)
3061                         error = copyout(&fh, uap->fhp, sizeof(fh));
3062         }
3063         return (error);
3064 }
3065
3066 /*
3067  * fhopen_args(const struct fhandle *u_fhp, int flags)
3068  *
3069  * syscall for the rpc.lockd to use to translate a NFS file handle into
3070  * an open descriptor.
3071  *
3072  * warning: do not remove the suser() call or this becomes one giant
3073  * security hole.
3074  */
3075 int
3076 fhopen(struct fhopen_args *uap)
3077 {
3078         struct thread *td = curthread;
3079         struct proc *p = td->td_proc;
3080         struct mount *mp;
3081         struct vnode *vp;
3082         struct fhandle fhp;
3083         struct vattr vat;
3084         struct vattr *vap = &vat;
3085         struct flock lf;
3086         int fmode, mode, error, type;
3087         struct file *nfp; 
3088         struct file *fp;
3089         int indx;
3090
3091         /*
3092          * Must be super user
3093          */
3094         error = suser(td);
3095         if (error)
3096                 return (error);
3097
3098         fmode = FFLAGS(uap->flags);
3099         /* why not allow a non-read/write open for our lockd? */
3100         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3101                 return (EINVAL);
3102         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3103         if (error)
3104                 return(error);
3105         /* find the mount point */
3106         mp = vfs_getvfs(&fhp.fh_fsid);
3107         if (mp == NULL)
3108                 return (ESTALE);
3109         /* now give me my vnode, it gets returned to me locked */
3110         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3111         if (error)
3112                 return (error);
3113         /*
3114          * from now on we have to make sure not
3115          * to forget about the vnode
3116          * any error that causes an abort must vput(vp) 
3117          * just set error = err and 'goto bad;'.
3118          */
3119
3120         /* 
3121          * from vn_open 
3122          */
3123         if (vp->v_type == VLNK) {
3124                 error = EMLINK;
3125                 goto bad;
3126         }
3127         if (vp->v_type == VSOCK) {
3128                 error = EOPNOTSUPP;
3129                 goto bad;
3130         }
3131         mode = 0;
3132         if (fmode & (FWRITE | O_TRUNC)) {
3133                 if (vp->v_type == VDIR) {
3134                         error = EISDIR;
3135                         goto bad;
3136                 }
3137                 error = vn_writechk(vp);
3138                 if (error)
3139                         goto bad;
3140                 mode |= VWRITE;
3141         }
3142         if (fmode & FREAD)
3143                 mode |= VREAD;
3144         if (mode) {
3145                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3146                 if (error)
3147                         goto bad;
3148         }
3149         if (fmode & O_TRUNC) {
3150                 VOP_UNLOCK(vp, 0);                      /* XXX */
3151                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3152                 VATTR_NULL(vap);
3153                 vap->va_size = 0;
3154                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3155                 if (error)
3156                         goto bad;
3157         }
3158
3159         /*
3160          * VOP_OPEN needs the file pointer so it can potentially override
3161          * it.
3162          *
3163          * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3164          * XXX
3165          */
3166         if ((error = falloc(p, &nfp, NULL)) != 0)
3167                 goto bad;
3168         fp = nfp;
3169
3170         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3171         if (error) {
3172                 /*
3173                  * setting f_ops this way prevents VOP_CLOSE from being
3174                  * called or fdrop() releasing the vp from v_data.   Since
3175                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3176                  */
3177                 fp->f_ops = &badfileops;
3178                 fp->f_data = NULL;
3179                 fdrop(fp);
3180                 goto bad;
3181         }
3182
3183         /*
3184          * The fp is given its own reference, we still have our ref and lock.
3185          *
3186          * Assert that all regular files must be created with a VM object.
3187          */
3188         if (vp->v_type == VREG && vp->v_object == NULL) {
3189                 printf("fhopen: regular file did not have VM object: %p\n", vp);
3190                 fdrop(fp);
3191                 goto bad;
3192         }
3193
3194         /*
3195          * The open was successful, associate it with a file descriptor.
3196          */
3197         if ((error = fsetfd(p, fp, &indx)) != 0) {
3198                 fdrop(fp);
3199                 goto bad;
3200         }
3201
3202         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3203                 lf.l_whence = SEEK_SET;
3204                 lf.l_start = 0;
3205                 lf.l_len = 0;
3206                 if (fmode & O_EXLOCK)
3207                         lf.l_type = F_WRLCK;
3208                 else
3209                         lf.l_type = F_RDLCK;
3210                 if (fmode & FNONBLOCK)
3211                         type = 0;
3212                 else
3213                         type = F_WAIT;
3214                 VOP_UNLOCK(vp, 0);
3215                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3216                         /*
3217                          * lock request failed.  Normally close the descriptor
3218                          * but handle the case where someone might have dup()d
3219                          * or close()d it when we weren't looking.
3220                          */
3221                         fdealloc(p, fp, indx);
3222
3223                         /*
3224                          * release our private reference.
3225                          */
3226                         fdrop(fp);
3227                         vrele(vp);
3228                         return (error);
3229                 }
3230                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3231                 fp->f_flag |= FHASLOCK;
3232         }
3233
3234         vput(vp);
3235         fdrop(fp);
3236         uap->sysmsg_result = indx;
3237         return (0);
3238
3239 bad:
3240         vput(vp);
3241         return (error);
3242 }
3243
3244 /*
3245  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3246  */
3247 int
3248 fhstat(struct fhstat_args *uap)
3249 {
3250         struct thread *td = curthread;
3251         struct stat sb;
3252         fhandle_t fh;
3253         struct mount *mp;
3254         struct vnode *vp;
3255         int error;
3256
3257         /*
3258          * Must be super user
3259          */
3260         error = suser(td);
3261         if (error)
3262                 return (error);
3263         
3264         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3265         if (error)
3266                 return (error);
3267
3268         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3269                 return (ESTALE);
3270         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3271                 return (error);
3272         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3273         vput(vp);
3274         if (error)
3275                 return (error);
3276         error = copyout(&sb, uap->sb, sizeof(sb));
3277         return (error);
3278 }
3279
3280 /*
3281  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3282  */
3283 int
3284 fhstatfs(struct fhstatfs_args *uap)
3285 {
3286         struct thread *td = curthread;
3287         struct proc *p = td->td_proc;
3288         struct statfs *sp;
3289         struct mount *mp;
3290         struct vnode *vp;
3291         struct statfs sb;
3292         char *fullpath, *freepath;
3293         fhandle_t fh;
3294         int error;
3295
3296         /*
3297          * Must be super user
3298          */
3299         if ((error = suser(td)))
3300                 return (error);
3301
3302         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3303                 return (error);
3304
3305         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3306                 return (ESTALE);
3307
3308         if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3309             !chroot_visible_mnt(mp, p))
3310                 return (ESTALE);
3311
3312         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3313                 return (error);
3314         mp = vp->v_mount;
3315         sp = &mp->mnt_stat;
3316         vput(vp);
3317         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3318                 return (error);
3319
3320         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3321         if (error)
3322                 return(error);
3323         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3324         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3325         free(freepath, M_TEMP);
3326
3327         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3328         if (suser(td)) {
3329                 bcopy(sp, &sb, sizeof(sb));
3330                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3331                 sp = &sb;
3332         }
3333         return (copyout(sp, uap->buf, sizeof(*sp)));
3334 }
3335
3336 /*
3337  * Syscall to push extended attribute configuration information into the
3338  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3339  * a command (int cmd), and attribute name and misc data.  For now, the
3340  * attribute name is left in userspace for consumption by the VFS_op.
3341  * It will probably be changed to be copied into sysspace by the
3342  * syscall in the future, once issues with various consumers of the
3343  * attribute code have raised their hands.
3344  *
3345  * Currently this is used only by UFS Extended Attributes.
3346  */
3347 int
3348 extattrctl(struct extattrctl_args *uap)
3349 {
3350         struct nlookupdata nd;
3351         struct mount *mp;
3352         struct vnode *vp;
3353         int error;
3354
3355         vp = NULL;
3356         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3357         if (error == 0)
3358                 error = nlookup(&nd);
3359         if (error == 0) {
3360                 mp = nd.nl_ncp->nc_mount;
3361                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
3362                                 uap->attrname, uap->arg, 
3363                                 nd.nl_cred);
3364         }
3365         nlookup_done(&nd);
3366         return (error);
3367 }
3368
3369 /*
3370  * Syscall to set a named extended attribute on a file or directory.
3371  * Accepts attribute name, and a uio structure pointing to the data to set.
3372  * The uio is consumed in the style of writev().  The real work happens
3373  * in VOP_SETEXTATTR().
3374  */
3375 int
3376 extattr_set_file(struct extattr_set_file_args *uap)
3377 {
3378         char attrname[EXTATTR_MAXNAMELEN];
3379         struct iovec aiov[UIO_SMALLIOV];
3380         struct iovec *needfree;
3381         struct nlookupdata nd;
3382         struct iovec *iov;
3383         struct vnode *vp;
3384         struct uio auio;
3385         u_int iovlen;
3386         u_int cnt;
3387         int error;
3388         int i;
3389
3390         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3391         if (error)
3392                 return (error);
3393
3394         vp = NULL;
3395         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3396         if (error == 0)
3397                 error = nlookup(&nd);
3398         if (error == 0)
3399                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3400         if (error) {
3401                 nlookup_done(&nd);
3402                 return (error);
3403         }
3404
3405         needfree = NULL;
3406         iovlen = uap->iovcnt * sizeof(struct iovec);
3407         if (uap->iovcnt > UIO_SMALLIOV) {
3408                 if (uap->iovcnt > UIO_MAXIOV) {
3409                         error = EINVAL;
3410                         goto done;
3411                 }
3412                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3413                 needfree = iov;
3414         } else {
3415                 iov = aiov;
3416         }
3417         auio.uio_iov = iov;
3418         auio.uio_iovcnt = uap->iovcnt;
3419         auio.uio_rw = UIO_WRITE;
3420         auio.uio_segflg = UIO_USERSPACE;
3421         auio.uio_td = nd.nl_td;
3422         auio.uio_offset = 0;
3423         if ((error = copyin(uap->iovp, iov, iovlen)))
3424                 goto done;
3425         auio.uio_resid = 0;
3426         for (i = 0; i < uap->iovcnt; i++) {
3427                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3428                         error = EINVAL;
3429                         goto done;
3430                 }
3431                 auio.uio_resid += iov->iov_len;
3432                 iov++;
3433         }
3434         cnt = auio.uio_resid;
3435         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3436         cnt -= auio.uio_resid;
3437         uap->sysmsg_result = cnt;
3438 done:
3439         vput(vp);
3440         nlookup_done(&nd);
3441         if (needfree)
3442                 FREE(needfree, M_IOV);
3443         return (error);
3444 }
3445
3446 /*
3447  * Syscall to get a named extended attribute on a file or directory.
3448  * Accepts attribute name, and a uio structure pointing to a buffer for the
3449  * data.  The uio is consumed in the style of readv().  The real work
3450  * happens in VOP_GETEXTATTR();
3451  */
3452 int
3453 extattr_get_file(struct extattr_get_file_args *uap)
3454 {
3455         char attrname[EXTATTR_MAXNAMELEN];
3456         struct iovec aiov[UIO_SMALLIOV];
3457         struct iovec *needfree;
3458         struct nlookupdata nd;
3459         struct iovec *iov;
3460         struct vnode *vp;
3461         struct uio auio;
3462         u_int iovlen;
3463         u_int cnt;
3464         int error;
3465         int i;
3466
3467         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3468         if (error)
3469                 return (error);
3470
3471         vp = NULL;
3472         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3473         if (error == 0)
3474                 error = nlookup(&nd);
3475         if (error == 0)
3476                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3477         if (error) {
3478                 nlookup_done(&nd);
3479                 return (error);
3480         }
3481
3482         iovlen = uap->iovcnt * sizeof (struct iovec);
3483         needfree = NULL;
3484         if (uap->iovcnt > UIO_SMALLIOV) {
3485                 if (uap->iovcnt > UIO_MAXIOV) {
3486                         error = EINVAL;
3487                         goto done;
3488                 }
3489                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3490                 needfree = iov;
3491         } else {
3492                 iov = aiov;
3493         }
3494         auio.uio_iov = iov;
3495         auio.uio_iovcnt = uap->iovcnt;
3496         auio.uio_rw = UIO_READ;
3497         auio.uio_segflg = UIO_USERSPACE;
3498         auio.uio_td = nd.nl_td;
3499         auio.uio_offset = 0;
3500         if ((error = copyin(uap->iovp, iov, iovlen)))
3501                 goto done;
3502         auio.uio_resid = 0;
3503         for (i = 0; i < uap->iovcnt; i++) {
3504                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3505                         error = EINVAL;
3506                         goto done;
3507                 }
3508                 auio.uio_resid += iov->iov_len;
3509                 iov++;
3510         }
3511         cnt = auio.uio_resid;
3512         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3513         cnt -= auio.uio_resid;
3514         uap->sysmsg_result = cnt;
3515 done:
3516         vput(vp);
3517         nlookup_done(&nd);
3518         if (needfree)
3519                 FREE(needfree, M_IOV);
3520         return(error);
3521 }
3522
3523 /*
3524  * Syscall to delete a named extended attribute from a file or directory.
3525  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3526  */
3527 int
3528 extattr_delete_file(struct extattr_delete_file_args *uap)
3529 {
3530         char attrname[EXTATTR_MAXNAMELEN];
3531         struct nlookupdata nd;
3532         struct vnode *vp;
3533         int error;
3534
3535         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3536         if (error)
3537                 return(error);
3538
3539         vp = NULL;
3540         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3541         if (error == 0)
3542                 error = nlookup(&nd);
3543         if (error == 0)
3544                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3545         if (error) {
3546                 nlookup_done(&nd);
3547                 return (error);
3548         }
3549
3550         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3551         vput(vp);
3552         nlookup_done(&nd);
3553         return(error);
3554 }
3555
3556 static int
3557 chroot_visible_mnt(struct mount *mp, struct proc *p)
3558 {
3559         struct namecache *ncp;
3560         /*
3561          * First check if this file system is below
3562          * the chroot path.
3563          */
3564         ncp = mp->mnt_ncp;
3565         while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3566                 ncp = ncp->nc_parent;
3567         if (ncp == NULL) {
3568                 /*
3569                  * This is not below the chroot path.
3570                  *
3571                  * Check if the chroot path is on the same filesystem,
3572                  * by determing if we have to cross a mount point
3573                  * before reaching mp->mnt_ncp.
3574                  */
3575                 ncp = p->p_fd->fd_nrdir;
3576                 while (ncp != NULL && ncp != mp->mnt_ncp) {
3577                         if (ncp->nc_flag & NCF_MOUNTPT) {
3578                                 ncp = NULL;
3579                                 break;
3580                         }
3581                         ncp = ncp->nc_parent;
3582                 }
3583         }
3584         return(ncp != NULL);
3585 }