Consolidate the file descriptor destruction code used when a newly created
[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.90 2006/05/19 05:15:35 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 filedesc *fdp = p->p_fd;
1804         struct file *fp;
1805         struct vattr vattr;
1806         int error;
1807
1808         if ((u_int)fd >= fdp->fd_nfiles ||
1809             (fp = fdp->fd_files[fd].fp) == NULL)
1810                 return (EBADF);
1811         if (fp->f_type != DTYPE_VNODE)
1812                 return (ESPIPE);
1813         switch (whence) {
1814         case L_INCR:
1815                 fp->f_offset += offset;
1816                 break;
1817         case L_XTND:
1818                 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1819                 if (error)
1820                         return (error);
1821                 fp->f_offset = offset + vattr.va_size;
1822                 break;
1823         case L_SET:
1824                 fp->f_offset = offset;
1825                 break;
1826         default:
1827                 return (EINVAL);
1828         }
1829         *res = fp->f_offset;
1830         return (0);
1831 }
1832
1833 /*
1834  * lseek_args(int fd, int pad, off_t offset, int whence)
1835  *
1836  * Reposition read/write file offset.
1837  */
1838 int
1839 lseek(struct lseek_args *uap)
1840 {
1841         int error;
1842
1843         error = kern_lseek(uap->fd, uap->offset, uap->whence,
1844             &uap->sysmsg_offset);
1845
1846         return (error);
1847 }
1848
1849 int
1850 kern_access(struct nlookupdata *nd, int aflags)
1851 {
1852         struct vnode *vp;
1853         int error, flags;
1854
1855         if ((error = nlookup(nd)) != 0)
1856                 return (error);
1857 retry:
1858         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1859         if (error)
1860                 return (error);
1861
1862         /* Flags == 0 means only check for existence. */
1863         if (aflags) {
1864                 flags = 0;
1865                 if (aflags & R_OK)
1866                         flags |= VREAD;
1867                 if (aflags & W_OK)
1868                         flags |= VWRITE;
1869                 if (aflags & X_OK)
1870                         flags |= VEXEC;
1871                 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1872                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
1873
1874                 /*
1875                  * If the file handle is stale we have to re-resolve the
1876                  * entry.  This is a hack at the moment.
1877                  */
1878                 if (error == ESTALE) {
1879                         cache_setunresolved(nd->nl_ncp);
1880                         error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1881                         if (error == 0) {
1882                                 vput(vp);
1883                                 vp = NULL;
1884                                 goto retry;
1885                         }
1886                 }
1887         }
1888         vput(vp);
1889         return (error);
1890 }
1891
1892 /*
1893  * access_args(char *path, int flags)
1894  *
1895  * Check access permissions.
1896  */
1897 int
1898 access(struct access_args *uap)
1899 {
1900         struct nlookupdata nd;
1901         int error;
1902
1903         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1904         if (error == 0)
1905                 error = kern_access(&nd, uap->flags);
1906         nlookup_done(&nd);
1907         return (error);
1908 }
1909
1910 int
1911 kern_stat(struct nlookupdata *nd, struct stat *st)
1912 {
1913         int error;
1914         struct vnode *vp;
1915         thread_t td;
1916
1917         if ((error = nlookup(nd)) != 0)
1918                 return (error);
1919 again:
1920         if ((vp = nd->nl_ncp->nc_vp) == NULL)
1921                 return (ENOENT);
1922
1923         td = curthread;
1924         if ((error = vget(vp, LK_SHARED)) != 0)
1925                 return (error);
1926         error = vn_stat(vp, st, nd->nl_cred);
1927
1928         /*
1929          * If the file handle is stale we have to re-resolve the entry.  This
1930          * is a hack at the moment.
1931          */
1932         if (error == ESTALE) {
1933                 cache_setunresolved(nd->nl_ncp);
1934                 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1935                 if (error == 0) {
1936                         vput(vp);
1937                         goto again;
1938                 }
1939         }
1940         vput(vp);
1941         return (error);
1942 }
1943
1944 /*
1945  * stat_args(char *path, struct stat *ub)
1946  *
1947  * Get file status; this version follows links.
1948  */
1949 int
1950 stat(struct stat_args *uap)
1951 {
1952         struct nlookupdata nd;
1953         struct stat st;
1954         int error;
1955
1956         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1957         if (error == 0) {
1958                 error = kern_stat(&nd, &st);
1959                 if (error == 0)
1960                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1961         }
1962         nlookup_done(&nd);
1963         return (error);
1964 }
1965
1966 /*
1967  * lstat_args(char *path, struct stat *ub)
1968  *
1969  * Get file status; this version does not follow links.
1970  */
1971 int
1972 lstat(struct lstat_args *uap)
1973 {
1974         struct nlookupdata nd;
1975         struct stat st;
1976         int error;
1977
1978         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1979         if (error == 0) {
1980                 error = kern_stat(&nd, &st);
1981                 if (error == 0)
1982                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1983         }
1984         nlookup_done(&nd);
1985         return (error);
1986 }
1987
1988 /*
1989  * pathconf_Args(char *path, int name)
1990  *
1991  * Get configurable pathname variables.
1992  */
1993 /* ARGSUSED */
1994 int
1995 pathconf(struct pathconf_args *uap)
1996 {
1997         struct nlookupdata nd;
1998         struct vnode *vp;
1999         int error;
2000
2001         vp = NULL;
2002         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2003         if (error == 0)
2004                 error = nlookup(&nd);
2005         if (error == 0)
2006                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2007         nlookup_done(&nd);
2008         if (error == 0) {
2009                 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2010                 vput(vp);
2011         }
2012         return (error);
2013 }
2014
2015 /*
2016  * XXX: daver
2017  * kern_readlink isn't properly split yet.  There is a copyin burried
2018  * in VOP_READLINK().
2019  */
2020 int
2021 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2022 {
2023         struct thread *td = curthread;
2024         struct proc *p = td->td_proc;
2025         struct vnode *vp;
2026         struct iovec aiov;
2027         struct uio auio;
2028         int error;
2029
2030         if ((error = nlookup(nd)) != 0)
2031                 return (error);
2032         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2033         if (error)
2034                 return (error);
2035         if (vp->v_type != VLNK) {
2036                 error = EINVAL;
2037         } else {
2038                 aiov.iov_base = buf;
2039                 aiov.iov_len = count;
2040                 auio.uio_iov = &aiov;
2041                 auio.uio_iovcnt = 1;
2042                 auio.uio_offset = 0;
2043                 auio.uio_rw = UIO_READ;
2044                 auio.uio_segflg = UIO_USERSPACE;
2045                 auio.uio_td = td;
2046                 auio.uio_resid = count;
2047                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2048         }
2049         vput(vp);
2050         *res = count - auio.uio_resid;
2051         return (error);
2052 }
2053
2054 /*
2055  * readlink_args(char *path, char *buf, int count)
2056  *
2057  * Return target name of a symbolic link.
2058  */
2059 int
2060 readlink(struct readlink_args *uap)
2061 {
2062         struct nlookupdata nd;
2063         int error;
2064
2065         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2066         if (error == 0) {
2067                 error = kern_readlink(&nd, uap->buf, uap->count,
2068                                         &uap->sysmsg_result);
2069         }
2070         nlookup_done(&nd);
2071         return (error);
2072 }
2073
2074 static int
2075 setfflags(struct vnode *vp, int flags)
2076 {
2077         struct thread *td = curthread;
2078         struct proc *p = td->td_proc;
2079         int error;
2080         struct vattr vattr;
2081
2082         /*
2083          * Prevent non-root users from setting flags on devices.  When
2084          * a device is reused, users can retain ownership of the device
2085          * if they are allowed to set flags and programs assume that
2086          * chown can't fail when done as root.
2087          */
2088         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2089             ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2090                 return (error);
2091
2092         /*
2093          * note: vget is required for any operation that might mod the vnode
2094          * so VINACTIVE is properly cleared.
2095          */
2096         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2097                 VATTR_NULL(&vattr);
2098                 vattr.va_flags = flags;
2099                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2100                 vput(vp);
2101         }
2102         return (error);
2103 }
2104
2105 /*
2106  * chflags(char *path, int flags)
2107  *
2108  * Change flags of a file given a path name.
2109  */
2110 /* ARGSUSED */
2111 int
2112 chflags(struct chflags_args *uap)
2113 {
2114         struct nlookupdata nd;
2115         struct vnode *vp;
2116         int error;
2117
2118         vp = NULL;
2119         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2120         /* XXX Add NLC flag indicating modifying operation? */
2121         if (error == 0)
2122                 error = nlookup(&nd);
2123         if (error == 0)
2124                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2125         nlookup_done(&nd);
2126         if (error == 0) {
2127                 error = setfflags(vp, uap->flags);
2128                 vrele(vp);
2129         }
2130         return (error);
2131 }
2132
2133 /*
2134  * fchflags_args(int fd, int flags)
2135  *
2136  * Change flags of a file given a file descriptor.
2137  */
2138 /* ARGSUSED */
2139 int
2140 fchflags(struct fchflags_args *uap)
2141 {
2142         struct thread *td = curthread;
2143         struct proc *p = td->td_proc;
2144         struct file *fp;
2145         int error;
2146
2147         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2148                 return (error);
2149         return setfflags((struct vnode *) fp->f_data, uap->flags);
2150 }
2151
2152 static int
2153 setfmode(struct vnode *vp, int mode)
2154 {
2155         struct thread *td = curthread;
2156         struct proc *p = td->td_proc;
2157         int error;
2158         struct vattr vattr;
2159
2160         /*
2161          * note: vget is required for any operation that might mod the vnode
2162          * so VINACTIVE is properly cleared.
2163          */
2164         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2165                 VATTR_NULL(&vattr);
2166                 vattr.va_mode = mode & ALLPERMS;
2167                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2168                 vput(vp);
2169         }
2170         return error;
2171 }
2172
2173 int
2174 kern_chmod(struct nlookupdata *nd, int mode)
2175 {
2176         struct vnode *vp;
2177         int error;
2178
2179         /* XXX Add NLC flag indicating modifying operation? */
2180         if ((error = nlookup(nd)) != 0)
2181                 return (error);
2182         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2183                 return (error);
2184         error = setfmode(vp, mode);
2185         vrele(vp);
2186         return (error);
2187 }
2188
2189 /*
2190  * chmod_args(char *path, int mode)
2191  *
2192  * Change mode of a file given path name.
2193  */
2194 /* ARGSUSED */
2195 int
2196 chmod(struct chmod_args *uap)
2197 {
2198         struct nlookupdata nd;
2199         int error;
2200
2201         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2202         if (error == 0)
2203                 error = kern_chmod(&nd, uap->mode);
2204         nlookup_done(&nd);
2205         return (error);
2206 }
2207
2208 /*
2209  * lchmod_args(char *path, int mode)
2210  *
2211  * Change mode of a file given path name (don't follow links.)
2212  */
2213 /* ARGSUSED */
2214 int
2215 lchmod(struct lchmod_args *uap)
2216 {
2217         struct nlookupdata nd;
2218         int error;
2219
2220         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2221         if (error == 0)
2222                 error = kern_chmod(&nd, uap->mode);
2223         nlookup_done(&nd);
2224         return (error);
2225 }
2226
2227 /*
2228  * fchmod_args(int fd, int mode)
2229  *
2230  * Change mode of a file given a file descriptor.
2231  */
2232 /* ARGSUSED */
2233 int
2234 fchmod(struct fchmod_args *uap)
2235 {
2236         struct thread *td = curthread;
2237         struct proc *p = td->td_proc;
2238         struct file *fp;
2239         int error;
2240
2241         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2242                 return (error);
2243         return setfmode((struct vnode *)fp->f_data, uap->mode);
2244 }
2245
2246 static int
2247 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2248 {
2249         struct thread *td = curthread;
2250         struct proc *p = td->td_proc;
2251         int error;
2252         struct vattr vattr;
2253
2254         /*
2255          * note: vget is required for any operation that might mod the vnode
2256          * so VINACTIVE is properly cleared.
2257          */
2258         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2259                 VATTR_NULL(&vattr);
2260                 vattr.va_uid = uid;
2261                 vattr.va_gid = gid;
2262                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2263                 vput(vp);
2264         }
2265         return error;
2266 }
2267
2268 int
2269 kern_chown(struct nlookupdata *nd, int uid, int gid)
2270 {
2271         struct vnode *vp;
2272         int error;
2273
2274         /* XXX Add NLC flag indicating modifying operation? */
2275         if ((error = nlookup(nd)) != 0)
2276                 return (error);
2277         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2278                 return (error);
2279         error = setfown(vp, uid, gid);
2280         vrele(vp);
2281         return (error);
2282 }
2283
2284 /*
2285  * chown(char *path, int uid, int gid)
2286  *
2287  * Set ownership given a path name.
2288  */
2289 int
2290 chown(struct chown_args *uap)
2291 {
2292         struct nlookupdata nd;
2293         int error;
2294
2295         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2296         if (error == 0)
2297                 error = kern_chown(&nd, uap->uid, uap->gid);
2298         nlookup_done(&nd);
2299         return (error);
2300 }
2301
2302 /*
2303  * lchown_args(char *path, int uid, int gid)
2304  *
2305  * Set ownership given a path name, do not cross symlinks.
2306  */
2307 int
2308 lchown(struct lchown_args *uap)
2309 {
2310         struct nlookupdata nd;
2311         int error;
2312
2313         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2314         if (error == 0)
2315                 error = kern_chown(&nd, uap->uid, uap->gid);
2316         nlookup_done(&nd);
2317         return (error);
2318 }
2319
2320 /*
2321  * fchown_args(int fd, int uid, int gid)
2322  *
2323  * Set ownership given a file descriptor.
2324  */
2325 /* ARGSUSED */
2326 int
2327 fchown(struct fchown_args *uap)
2328 {
2329         struct thread *td = curthread;
2330         struct proc *p = td->td_proc;
2331         struct file *fp;
2332         int error;
2333
2334         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2335                 return (error);
2336         return setfown((struct vnode *)fp->f_data,
2337                 uap->uid, uap->gid);
2338 }
2339
2340 static int
2341 getutimes(const struct timeval *tvp, struct timespec *tsp)
2342 {
2343         struct timeval tv[2];
2344
2345         if (tvp == NULL) {
2346                 microtime(&tv[0]);
2347                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2348                 tsp[1] = tsp[0];
2349         } else {
2350                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2351                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2352         }
2353         return 0;
2354 }
2355
2356 static int
2357 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2358 {
2359         struct thread *td = curthread;
2360         struct proc *p = td->td_proc;
2361         int error;
2362         struct vattr vattr;
2363
2364         /*
2365          * note: vget is required for any operation that might mod the vnode
2366          * so VINACTIVE is properly cleared.
2367          */
2368         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2369                 VATTR_NULL(&vattr);
2370                 vattr.va_atime = ts[0];
2371                 vattr.va_mtime = ts[1];
2372                 if (nullflag)
2373                         vattr.va_vaflags |= VA_UTIMES_NULL;
2374                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2375                 vput(vp);
2376         }
2377         return error;
2378 }
2379
2380 int
2381 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2382 {
2383         struct timespec ts[2];
2384         struct vnode *vp;
2385         int error;
2386
2387         if ((error = getutimes(tptr, ts)) != 0)
2388                 return (error);
2389         /* XXX Add NLC flag indicating modifying operation? */
2390         if ((error = nlookup(nd)) != 0)
2391                 return (error);
2392         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2393                 return (error);
2394         error = setutimes(vp, ts, tptr == NULL);
2395         vrele(vp);
2396         return (error);
2397 }
2398
2399 /*
2400  * utimes_args(char *path, struct timeval *tptr)
2401  *
2402  * Set the access and modification times of a file.
2403  */
2404 int
2405 utimes(struct utimes_args *uap)
2406 {
2407         struct timeval tv[2];
2408         struct nlookupdata nd;
2409         int error;
2410
2411         if (uap->tptr) {
2412                 error = copyin(uap->tptr, tv, sizeof(tv));
2413                 if (error)
2414                         return (error);
2415         }
2416         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2417         if (error == 0)
2418                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2419         nlookup_done(&nd);
2420         return (error);
2421 }
2422
2423 /*
2424  * lutimes_args(char *path, struct timeval *tptr)
2425  *
2426  * Set the access and modification times of a file.
2427  */
2428 int
2429 lutimes(struct lutimes_args *uap)
2430 {
2431         struct timeval tv[2];
2432         struct nlookupdata nd;
2433         int error;
2434
2435         if (uap->tptr) {
2436                 error = copyin(uap->tptr, tv, sizeof(tv));
2437                 if (error)
2438                         return (error);
2439         }
2440         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2441         if (error == 0)
2442                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2443         nlookup_done(&nd);
2444         return (error);
2445 }
2446
2447 int
2448 kern_futimes(int fd, struct timeval *tptr)
2449 {
2450         struct thread *td = curthread;
2451         struct proc *p = td->td_proc;
2452         struct timespec ts[2];
2453         struct file *fp;
2454         int error;
2455
2456         error = getutimes(tptr, ts);
2457         if (error)
2458                 return (error);
2459         error = getvnode(p->p_fd, fd, &fp);
2460         if (error)
2461                 return (error);
2462         error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2463         return (error);
2464 }
2465
2466 /*
2467  * futimes_args(int fd, struct timeval *tptr)
2468  *
2469  * Set the access and modification times of a file.
2470  */
2471 int
2472 futimes(struct futimes_args *uap)
2473 {
2474         struct timeval tv[2];
2475         int error;
2476
2477         if (uap->tptr) {
2478                 error = copyin(uap->tptr, tv, sizeof(tv));
2479                 if (error)
2480                         return (error);
2481         }
2482
2483         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2484
2485         return (error);
2486 }
2487
2488 int
2489 kern_truncate(struct nlookupdata *nd, off_t length)
2490 {
2491         struct vnode *vp;
2492         struct vattr vattr;
2493         int error;
2494
2495         if (length < 0)
2496                 return(EINVAL);
2497         /* XXX Add NLC flag indicating modifying operation? */
2498         if ((error = nlookup(nd)) != 0)
2499                 return (error);
2500         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2501                 return (error);
2502         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2503                 vrele(vp);
2504                 return (error);
2505         }
2506         if (vp->v_type == VDIR) {
2507                 error = EISDIR;
2508         } else if ((error = vn_writechk(vp)) == 0 &&
2509             (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2510                 VATTR_NULL(&vattr);
2511                 vattr.va_size = length;
2512                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2513         }
2514         vput(vp);
2515         return (error);
2516 }
2517
2518 /*
2519  * truncate(char *path, int pad, off_t length)
2520  *
2521  * Truncate a file given its path name.
2522  */
2523 int
2524 truncate(struct truncate_args *uap)
2525 {
2526         struct nlookupdata nd;
2527         int error;
2528
2529         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2530         if (error == 0)
2531                 error = kern_truncate(&nd, uap->length);
2532         nlookup_done(&nd);
2533         return error;
2534 }
2535
2536 int
2537 kern_ftruncate(int fd, off_t length)
2538 {
2539         struct thread *td = curthread;
2540         struct proc *p = td->td_proc;
2541         struct vattr vattr;
2542         struct vnode *vp;
2543         struct file *fp;
2544         int error;
2545
2546         if (length < 0)
2547                 return(EINVAL);
2548         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2549                 return (error);
2550         if ((fp->f_flag & FWRITE) == 0)
2551                 return (EINVAL);
2552         vp = (struct vnode *)fp->f_data;
2553         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2554         if (vp->v_type == VDIR)
2555                 error = EISDIR;
2556         else if ((error = vn_writechk(vp)) == 0) {
2557                 VATTR_NULL(&vattr);
2558                 vattr.va_size = length;
2559                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2560         }
2561         VOP_UNLOCK(vp, 0);
2562         return (error);
2563 }
2564
2565 /*
2566  * ftruncate_args(int fd, int pad, off_t length)
2567  *
2568  * Truncate a file given a file descriptor.
2569  */
2570 int
2571 ftruncate(struct ftruncate_args *uap)
2572 {
2573         int error;
2574
2575         error = kern_ftruncate(uap->fd, uap->length);
2576
2577         return (error);
2578 }
2579
2580 /*
2581  * fsync(int fd)
2582  *
2583  * Sync an open file.
2584  */
2585 /* ARGSUSED */
2586 int
2587 fsync(struct fsync_args *uap)
2588 {
2589         struct thread *td = curthread;
2590         struct proc *p = td->td_proc;
2591         struct vnode *vp;
2592         struct file *fp;
2593         vm_object_t obj;
2594         int error;
2595
2596         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2597                 return (error);
2598         vp = (struct vnode *)fp->f_data;
2599         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2600         if ((obj = vp->v_object) != NULL)
2601                 vm_object_page_clean(obj, 0, 0, 0);
2602         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2603             vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2604             bioops.io_fsync)
2605                 error = (*bioops.io_fsync)(vp);
2606         VOP_UNLOCK(vp, 0);
2607         return (error);
2608 }
2609
2610 int
2611 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2612 {
2613         struct namecache *fncpd;
2614         struct namecache *tncpd;
2615         struct namecache *ncp;
2616         struct mount *mp;
2617         int error;
2618
2619         bwillwrite();
2620         if ((error = nlookup(fromnd)) != 0)
2621                 return (error);
2622         if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2623                 return (ENOENT);
2624         cache_hold(fncpd);
2625
2626         /*
2627          * unlock the source ncp so we can lookup the target ncp without
2628          * deadlocking.  The target may or may not exist so we do not check
2629          * for a target vp like kern_mkdir() and other creation functions do.
2630          *
2631          * The source and target directories are ref'd and rechecked after
2632          * everything is relocked to determine if the source or target file
2633          * has been renamed.
2634          */
2635         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2636         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2637         cache_unlock(fromnd->nl_ncp);
2638
2639         tond->nl_flags |= NLC_CREATE;
2640         if ((error = nlookup(tond)) != 0) {
2641                 cache_drop(fncpd);
2642                 return (error);
2643         }
2644         if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2645                 cache_drop(fncpd);
2646                 return (ENOENT);
2647         }
2648         cache_hold(tncpd);
2649
2650         /*
2651          * If the source and target are the same there is nothing to do
2652          */
2653         if (fromnd->nl_ncp == tond->nl_ncp) {
2654                 cache_drop(fncpd);
2655                 cache_drop(tncpd);
2656                 return (0);
2657         }
2658
2659         /*
2660          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
2661          * may have become invalid while it was unlocked, nc_vp and nc_mount
2662          * could be NULL.
2663          */
2664         if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2665                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2666         } else if (fromnd->nl_ncp > tond->nl_ncp) {
2667                 cache_lock(fromnd->nl_ncp);
2668                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2669         } else {
2670                 cache_unlock(tond->nl_ncp);
2671                 cache_lock(fromnd->nl_ncp);
2672                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2673                 cache_lock(tond->nl_ncp);
2674                 cache_resolve(tond->nl_ncp, tond->nl_cred);
2675         }
2676         fromnd->nl_flags |= NLC_NCPISLOCKED;
2677
2678         /*
2679          * make sure the parent directories linkages are the same
2680          */
2681         if (fncpd != fromnd->nl_ncp->nc_parent ||
2682             tncpd != tond->nl_ncp->nc_parent) {
2683                 cache_drop(fncpd);
2684                 cache_drop(tncpd);
2685                 return (ENOENT);
2686         }
2687
2688         /*
2689          * Both the source and target must be within the same filesystem and
2690          * in the same filesystem as their parent directories within the
2691          * namecache topology.
2692          *
2693          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2694          */
2695         mp = fncpd->nc_mount;
2696         if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2697             mp != tond->nl_ncp->nc_mount) {
2698                 cache_drop(fncpd);
2699                 cache_drop(tncpd);
2700                 return (EXDEV);
2701         }
2702
2703         /*
2704          * If the target exists and either the source or target is a directory,
2705          * then both must be directories.
2706          *
2707          * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2708          * become NULL.
2709          */
2710         if (tond->nl_ncp->nc_vp) {
2711                 if (fromnd->nl_ncp->nc_vp == NULL) {
2712                         error = ENOENT;
2713                 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2714                         if (tond->nl_ncp->nc_vp->v_type != VDIR)
2715                                 error = ENOTDIR;
2716                 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2717                         error = EISDIR;
2718                 }
2719         }
2720
2721         /*
2722          * You cannot rename a source into itself or a subdirectory of itself.
2723          * We check this by travsersing the target directory upwards looking
2724          * for a match against the source.
2725          */
2726         if (error == 0) {
2727                 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2728                         if (fromnd->nl_ncp == ncp) {
2729                                 error = EINVAL;
2730                                 break;
2731                         }
2732                 }
2733         }
2734
2735         cache_drop(fncpd);
2736         cache_drop(tncpd);
2737
2738         /*
2739          * Even though the namespaces are different, they may still represent
2740          * hardlinks to the same file.  The filesystem might have a hard time
2741          * with this so we issue a NREMOVE of the source instead of a NRENAME
2742          * when we detect the situation.
2743          */
2744         if (error == 0) {
2745                 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2746                         error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2747                 } else {
2748                         error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, 
2749                                             tond->nl_cred);
2750                 }
2751         }
2752         return (error);
2753 }
2754
2755 /*
2756  * rename_args(char *from, char *to)
2757  *
2758  * Rename files.  Source and destination must either both be directories,
2759  * or both not be directories.  If target is a directory, it must be empty.
2760  */
2761 int
2762 rename(struct rename_args *uap)
2763 {
2764         struct nlookupdata fromnd, tond;
2765         int error;
2766
2767         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2768         if (error == 0) {
2769                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2770                 if (error == 0)
2771                         error = kern_rename(&fromnd, &tond);
2772                 nlookup_done(&tond);
2773         }
2774         nlookup_done(&fromnd);
2775         return (error);
2776 }
2777
2778 int
2779 kern_mkdir(struct nlookupdata *nd, int mode)
2780 {
2781         struct thread *td = curthread;
2782         struct proc *p = td->td_proc;
2783         struct namecache *ncp;
2784         struct vnode *vp;
2785         struct vattr vattr;
2786         int error;
2787
2788         bwillwrite();
2789         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2790         if ((error = nlookup(nd)) != 0)
2791                 return (error);
2792
2793         ncp = nd->nl_ncp;
2794         if (ncp->nc_vp)
2795                 return (EEXIST);
2796
2797         VATTR_NULL(&vattr);
2798         vattr.va_type = VDIR;
2799         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2800
2801         vp = NULL;
2802         error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2803         if (error == 0)
2804                 vput(vp);
2805         return (error);
2806 }
2807
2808 /*
2809  * mkdir_args(char *path, int mode)
2810  *
2811  * Make a directory file.
2812  */
2813 /* ARGSUSED */
2814 int
2815 mkdir(struct mkdir_args *uap)
2816 {
2817         struct nlookupdata nd;
2818         int error;
2819
2820         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2821         if (error == 0)
2822                 error = kern_mkdir(&nd, uap->mode);
2823         nlookup_done(&nd);
2824         return (error);
2825 }
2826
2827 int
2828 kern_rmdir(struct nlookupdata *nd)
2829 {
2830         struct namecache *ncp;
2831         int error;
2832
2833         bwillwrite();
2834         nd->nl_flags |= NLC_DELETE;
2835         if ((error = nlookup(nd)) != 0)
2836                 return (error);
2837
2838         ncp = nd->nl_ncp;
2839         error = VOP_NRMDIR(ncp, nd->nl_cred);
2840         return (error);
2841 }
2842
2843 /*
2844  * rmdir_args(char *path)
2845  *
2846  * Remove a directory file.
2847  */
2848 /* ARGSUSED */
2849 int
2850 rmdir(struct rmdir_args *uap)
2851 {
2852         struct nlookupdata nd;
2853         int error;
2854
2855         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2856         if (error == 0)
2857                 error = kern_rmdir(&nd);
2858         nlookup_done(&nd);
2859         return (error);
2860 }
2861
2862 int
2863 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2864     enum uio_seg direction)
2865 {
2866         struct thread *td = curthread;
2867         struct proc *p = td->td_proc;
2868         struct vnode *vp;
2869         struct file *fp;
2870         struct uio auio;
2871         struct iovec aiov;
2872         long loff;
2873         int error, eofflag;
2874
2875         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2876                 return (error);
2877         if ((fp->f_flag & FREAD) == 0)
2878                 return (EBADF);
2879         vp = (struct vnode *)fp->f_data;
2880 unionread:
2881         if (vp->v_type != VDIR)
2882                 return (EINVAL);
2883         aiov.iov_base = buf;
2884         aiov.iov_len = count;
2885         auio.uio_iov = &aiov;
2886         auio.uio_iovcnt = 1;
2887         auio.uio_rw = UIO_READ;
2888         auio.uio_segflg = direction;
2889         auio.uio_td = td;
2890         auio.uio_resid = count;
2891         /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2892         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2893         loff = auio.uio_offset = fp->f_offset;
2894         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2895         fp->f_offset = auio.uio_offset;
2896         VOP_UNLOCK(vp, 0);
2897         if (error)
2898                 return (error);
2899         if (count == auio.uio_resid) {
2900                 if (union_dircheckp) {
2901                         error = union_dircheckp(td, &vp, fp);
2902                         if (error == -1)
2903                                 goto unionread;
2904                         if (error)
2905                                 return (error);
2906                 }
2907                 if ((vp->v_flag & VROOT) &&
2908                     (vp->v_mount->mnt_flag & MNT_UNION)) {
2909                         struct vnode *tvp = vp;
2910                         vp = vp->v_mount->mnt_vnodecovered;
2911                         vref(vp);
2912                         fp->f_data = vp;
2913                         fp->f_offset = 0;
2914                         vrele(tvp);
2915                         goto unionread;
2916                 }
2917         }
2918         if (basep) {
2919                 *basep = loff;
2920         }
2921         *res = count - auio.uio_resid;
2922         return (error);
2923 }
2924
2925 /*
2926  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2927  *
2928  * Read a block of directory entries in a file system independent format.
2929  */
2930 int
2931 getdirentries(struct getdirentries_args *uap)
2932 {
2933         long base;
2934         int error;
2935
2936         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2937             &uap->sysmsg_result, UIO_USERSPACE);
2938
2939         if (error == 0)
2940                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2941         return (error);
2942 }
2943
2944 /*
2945  * getdents_args(int fd, char *buf, size_t count)
2946  */
2947 int
2948 getdents(struct getdents_args *uap)
2949 {
2950         int error;
2951
2952         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2953             &uap->sysmsg_result, UIO_USERSPACE);
2954
2955         return (error);
2956 }
2957
2958 /*
2959  * umask(int newmask)
2960  *
2961  * Set the mode mask for creation of filesystem nodes.
2962  *
2963  * MP SAFE
2964  */
2965 int
2966 umask(struct umask_args *uap)
2967 {
2968         struct thread *td = curthread;
2969         struct proc *p = td->td_proc;
2970         struct filedesc *fdp;
2971
2972         fdp = p->p_fd;
2973         uap->sysmsg_result = fdp->fd_cmask;
2974         fdp->fd_cmask = uap->newmask & ALLPERMS;
2975         return (0);
2976 }
2977
2978 /*
2979  * revoke(char *path)
2980  *
2981  * Void all references to file by ripping underlying filesystem
2982  * away from vnode.
2983  */
2984 /* ARGSUSED */
2985 int
2986 revoke(struct revoke_args *uap)
2987 {
2988         struct nlookupdata nd;
2989         struct vattr vattr;
2990         struct vnode *vp;
2991         struct ucred *cred;
2992         int error;
2993
2994         vp = NULL;
2995         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2996         if (error == 0)
2997                 error = nlookup(&nd);
2998         if (error == 0)
2999                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3000         cred = crhold(nd.nl_cred);
3001         nlookup_done(&nd);
3002         if (error == 0) {
3003                 if (vp->v_type != VCHR && vp->v_type != VBLK)
3004                         error = EINVAL;
3005                 if (error == 0)
3006                         error = VOP_GETATTR(vp, &vattr);
3007                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3008                         error = suser_cred(cred, PRISON_ROOT);
3009                 if (error == 0 && count_udev(vp->v_udev) > 0) {
3010                         if ((error = vx_lock(vp)) == 0) {
3011                                 VOP_REVOKE(vp, REVOKEALL);
3012                                 vx_unlock(vp);
3013                         }
3014                 }
3015                 vrele(vp);
3016         }
3017         if (cred)
3018                 crfree(cred);
3019         return (error);
3020 }
3021
3022 /*
3023  * Convert a user file descriptor to a kernel file entry.
3024  */
3025 int
3026 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3027 {
3028         struct file *fp;
3029
3030         if ((u_int)fd >= fdp->fd_nfiles ||
3031             (fp = fdp->fd_files[fd].fp) == NULL)
3032                 return (EBADF);
3033         if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3034                 return (EINVAL);
3035         *fpp = fp;
3036         return (0);
3037 }
3038 /*
3039  * getfh_args(char *fname, fhandle_t *fhp)
3040  *
3041  * Get (NFS) file handle
3042  */
3043 int
3044 getfh(struct getfh_args *uap)
3045 {
3046         struct thread *td = curthread;
3047         struct nlookupdata nd;
3048         fhandle_t fh;
3049         struct vnode *vp;
3050         int error;
3051
3052         /*
3053          * Must be super user
3054          */
3055         if ((error = suser(td)) != 0)
3056                 return (error);
3057
3058         vp = NULL;
3059         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3060         if (error == 0)
3061                 error = nlookup(&nd);
3062         if (error == 0)
3063                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3064         nlookup_done(&nd);
3065         if (error == 0) {
3066                 bzero(&fh, sizeof(fh));
3067                 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3068                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3069                 vput(vp);
3070                 if (error == 0)
3071                         error = copyout(&fh, uap->fhp, sizeof(fh));
3072         }
3073         return (error);
3074 }
3075
3076 /*
3077  * fhopen_args(const struct fhandle *u_fhp, int flags)
3078  *
3079  * syscall for the rpc.lockd to use to translate a NFS file handle into
3080  * an open descriptor.
3081  *
3082  * warning: do not remove the suser() call or this becomes one giant
3083  * security hole.
3084  */
3085 int
3086 fhopen(struct fhopen_args *uap)
3087 {
3088         struct thread *td = curthread;
3089         struct proc *p = td->td_proc;
3090         struct mount *mp;
3091         struct vnode *vp;
3092         struct fhandle fhp;
3093         struct vattr vat;
3094         struct vattr *vap = &vat;
3095         struct flock lf;
3096         int fmode, mode, error, type;
3097         struct file *nfp; 
3098         struct file *fp;
3099         int indx;
3100
3101         /*
3102          * Must be super user
3103          */
3104         error = suser(td);
3105         if (error)
3106                 return (error);
3107
3108         fmode = FFLAGS(uap->flags);
3109         /* why not allow a non-read/write open for our lockd? */
3110         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3111                 return (EINVAL);
3112         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3113         if (error)
3114                 return(error);
3115         /* find the mount point */
3116         mp = vfs_getvfs(&fhp.fh_fsid);
3117         if (mp == NULL)
3118                 return (ESTALE);
3119         /* now give me my vnode, it gets returned to me locked */
3120         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3121         if (error)
3122                 return (error);
3123         /*
3124          * from now on we have to make sure not
3125          * to forget about the vnode
3126          * any error that causes an abort must vput(vp) 
3127          * just set error = err and 'goto bad;'.
3128          */
3129
3130         /* 
3131          * from vn_open 
3132          */
3133         if (vp->v_type == VLNK) {
3134                 error = EMLINK;
3135                 goto bad;
3136         }
3137         if (vp->v_type == VSOCK) {
3138                 error = EOPNOTSUPP;
3139                 goto bad;
3140         }
3141         mode = 0;
3142         if (fmode & (FWRITE | O_TRUNC)) {
3143                 if (vp->v_type == VDIR) {
3144                         error = EISDIR;
3145                         goto bad;
3146                 }
3147                 error = vn_writechk(vp);
3148                 if (error)
3149                         goto bad;
3150                 mode |= VWRITE;
3151         }
3152         if (fmode & FREAD)
3153                 mode |= VREAD;
3154         if (mode) {
3155                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3156                 if (error)
3157                         goto bad;
3158         }
3159         if (fmode & O_TRUNC) {
3160                 VOP_UNLOCK(vp, 0);                      /* XXX */
3161                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3162                 VATTR_NULL(vap);
3163                 vap->va_size = 0;
3164                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3165                 if (error)
3166                         goto bad;
3167         }
3168
3169         /*
3170          * VOP_OPEN needs the file pointer so it can potentially override
3171          * it.
3172          *
3173          * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3174          * XXX
3175          */
3176         if ((error = falloc(p, &nfp, NULL)) != 0)
3177                 goto bad;
3178         fp = nfp;
3179
3180         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3181         if (error) {
3182                 /*
3183                  * setting f_ops this way prevents VOP_CLOSE from being
3184                  * called or fdrop() releasing the vp from v_data.   Since
3185                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3186                  */
3187                 fp->f_ops = &badfileops;
3188                 fp->f_data = NULL;
3189                 fdrop(fp);
3190                 goto bad;
3191         }
3192
3193         /*
3194          * The fp is given its own reference, we still have our ref and lock.
3195          *
3196          * Assert that all regular files must be created with a VM object.
3197          */
3198         if (vp->v_type == VREG && vp->v_object == NULL) {
3199                 printf("fhopen: regular file did not have VM object: %p\n", vp);
3200                 fdrop(fp);
3201                 goto bad;
3202         }
3203
3204         /*
3205          * The open was successful, associate it with a file descriptor.
3206          */
3207         if ((error = fsetfd(p, fp, &indx)) != 0) {
3208                 fdrop(fp);
3209                 goto bad;
3210         }
3211
3212         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3213                 lf.l_whence = SEEK_SET;
3214                 lf.l_start = 0;
3215                 lf.l_len = 0;
3216                 if (fmode & O_EXLOCK)
3217                         lf.l_type = F_WRLCK;
3218                 else
3219                         lf.l_type = F_RDLCK;
3220                 if (fmode & FNONBLOCK)
3221                         type = 0;
3222                 else
3223                         type = F_WAIT;
3224                 VOP_UNLOCK(vp, 0);
3225                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3226                         /*
3227                          * lock request failed.  Normally close the descriptor
3228                          * but handle the case where someone might have dup()d
3229                          * or close()d it when we weren't looking.
3230                          */
3231                         fdealloc(p, fp, indx);
3232
3233                         /*
3234                          * release our private reference.
3235                          */
3236                         fdrop(fp);
3237                         vrele(vp);
3238                         return (error);
3239                 }
3240                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3241                 fp->f_flag |= FHASLOCK;
3242         }
3243
3244         vput(vp);
3245         fdrop(fp);
3246         uap->sysmsg_result = indx;
3247         return (0);
3248
3249 bad:
3250         vput(vp);
3251         return (error);
3252 }
3253
3254 /*
3255  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3256  */
3257 int
3258 fhstat(struct fhstat_args *uap)
3259 {
3260         struct thread *td = curthread;
3261         struct stat sb;
3262         fhandle_t fh;
3263         struct mount *mp;
3264         struct vnode *vp;
3265         int error;
3266
3267         /*
3268          * Must be super user
3269          */
3270         error = suser(td);
3271         if (error)
3272                 return (error);
3273         
3274         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3275         if (error)
3276                 return (error);
3277
3278         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3279                 return (ESTALE);
3280         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3281                 return (error);
3282         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3283         vput(vp);
3284         if (error)
3285                 return (error);
3286         error = copyout(&sb, uap->sb, sizeof(sb));
3287         return (error);
3288 }
3289
3290 /*
3291  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3292  */
3293 int
3294 fhstatfs(struct fhstatfs_args *uap)
3295 {
3296         struct thread *td = curthread;
3297         struct proc *p = td->td_proc;
3298         struct statfs *sp;
3299         struct mount *mp;
3300         struct vnode *vp;
3301         struct statfs sb;
3302         char *fullpath, *freepath;
3303         fhandle_t fh;
3304         int error;
3305
3306         /*
3307          * Must be super user
3308          */
3309         if ((error = suser(td)))
3310                 return (error);
3311
3312         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3313                 return (error);
3314
3315         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3316                 return (ESTALE);
3317
3318         if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3319             !chroot_visible_mnt(mp, p))
3320                 return (ESTALE);
3321
3322         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3323                 return (error);
3324         mp = vp->v_mount;
3325         sp = &mp->mnt_stat;
3326         vput(vp);
3327         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3328                 return (error);
3329
3330         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3331         if (error)
3332                 return(error);
3333         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3334         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3335         free(freepath, M_TEMP);
3336
3337         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3338         if (suser(td)) {
3339                 bcopy(sp, &sb, sizeof(sb));
3340                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3341                 sp = &sb;
3342         }
3343         return (copyout(sp, uap->buf, sizeof(*sp)));
3344 }
3345
3346 /*
3347  * Syscall to push extended attribute configuration information into the
3348  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3349  * a command (int cmd), and attribute name and misc data.  For now, the
3350  * attribute name is left in userspace for consumption by the VFS_op.
3351  * It will probably be changed to be copied into sysspace by the
3352  * syscall in the future, once issues with various consumers of the
3353  * attribute code have raised their hands.
3354  *
3355  * Currently this is used only by UFS Extended Attributes.
3356  */
3357 int
3358 extattrctl(struct extattrctl_args *uap)
3359 {
3360         struct nlookupdata nd;
3361         struct mount *mp;
3362         struct vnode *vp;
3363         int error;
3364
3365         vp = NULL;
3366         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3367         if (error == 0)
3368                 error = nlookup(&nd);
3369         if (error == 0) {
3370                 mp = nd.nl_ncp->nc_mount;
3371                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
3372                                 uap->attrname, uap->arg, 
3373                                 nd.nl_cred);
3374         }
3375         nlookup_done(&nd);
3376         return (error);
3377 }
3378
3379 /*
3380  * Syscall to set a named extended attribute on a file or directory.
3381  * Accepts attribute name, and a uio structure pointing to the data to set.
3382  * The uio is consumed in the style of writev().  The real work happens
3383  * in VOP_SETEXTATTR().
3384  */
3385 int
3386 extattr_set_file(struct extattr_set_file_args *uap)
3387 {
3388         char attrname[EXTATTR_MAXNAMELEN];
3389         struct iovec aiov[UIO_SMALLIOV];
3390         struct iovec *needfree;
3391         struct nlookupdata nd;
3392         struct iovec *iov;
3393         struct vnode *vp;
3394         struct uio auio;
3395         u_int iovlen;
3396         u_int cnt;
3397         int error;
3398         int i;
3399
3400         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3401         if (error)
3402                 return (error);
3403
3404         vp = NULL;
3405         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3406         if (error == 0)
3407                 error = nlookup(&nd);
3408         if (error == 0)
3409                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3410         if (error) {
3411                 nlookup_done(&nd);
3412                 return (error);
3413         }
3414
3415         needfree = NULL;
3416         iovlen = uap->iovcnt * sizeof(struct iovec);
3417         if (uap->iovcnt > UIO_SMALLIOV) {
3418                 if (uap->iovcnt > UIO_MAXIOV) {
3419                         error = EINVAL;
3420                         goto done;
3421                 }
3422                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3423                 needfree = iov;
3424         } else {
3425                 iov = aiov;
3426         }
3427         auio.uio_iov = iov;
3428         auio.uio_iovcnt = uap->iovcnt;
3429         auio.uio_rw = UIO_WRITE;
3430         auio.uio_segflg = UIO_USERSPACE;
3431         auio.uio_td = nd.nl_td;
3432         auio.uio_offset = 0;
3433         if ((error = copyin(uap->iovp, iov, iovlen)))
3434                 goto done;
3435         auio.uio_resid = 0;
3436         for (i = 0; i < uap->iovcnt; i++) {
3437                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3438                         error = EINVAL;
3439                         goto done;
3440                 }
3441                 auio.uio_resid += iov->iov_len;
3442                 iov++;
3443         }
3444         cnt = auio.uio_resid;
3445         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3446         cnt -= auio.uio_resid;
3447         uap->sysmsg_result = cnt;
3448 done:
3449         vput(vp);
3450         nlookup_done(&nd);
3451         if (needfree)
3452                 FREE(needfree, M_IOV);
3453         return (error);
3454 }
3455
3456 /*
3457  * Syscall to get a named extended attribute on a file or directory.
3458  * Accepts attribute name, and a uio structure pointing to a buffer for the
3459  * data.  The uio is consumed in the style of readv().  The real work
3460  * happens in VOP_GETEXTATTR();
3461  */
3462 int
3463 extattr_get_file(struct extattr_get_file_args *uap)
3464 {
3465         char attrname[EXTATTR_MAXNAMELEN];
3466         struct iovec aiov[UIO_SMALLIOV];
3467         struct iovec *needfree;
3468         struct nlookupdata nd;
3469         struct iovec *iov;
3470         struct vnode *vp;
3471         struct uio auio;
3472         u_int iovlen;
3473         u_int cnt;
3474         int error;
3475         int i;
3476
3477         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3478         if (error)
3479                 return (error);
3480
3481         vp = NULL;
3482         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3483         if (error == 0)
3484                 error = nlookup(&nd);
3485         if (error == 0)
3486                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3487         if (error) {
3488                 nlookup_done(&nd);
3489                 return (error);
3490         }
3491
3492         iovlen = uap->iovcnt * sizeof (struct iovec);
3493         needfree = NULL;
3494         if (uap->iovcnt > UIO_SMALLIOV) {
3495                 if (uap->iovcnt > UIO_MAXIOV) {
3496                         error = EINVAL;
3497                         goto done;
3498                 }
3499                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3500                 needfree = iov;
3501         } else {
3502                 iov = aiov;
3503         }
3504         auio.uio_iov = iov;
3505         auio.uio_iovcnt = uap->iovcnt;
3506         auio.uio_rw = UIO_READ;
3507         auio.uio_segflg = UIO_USERSPACE;
3508         auio.uio_td = nd.nl_td;
3509         auio.uio_offset = 0;
3510         if ((error = copyin(uap->iovp, iov, iovlen)))
3511                 goto done;
3512         auio.uio_resid = 0;
3513         for (i = 0; i < uap->iovcnt; i++) {
3514                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3515                         error = EINVAL;
3516                         goto done;
3517                 }
3518                 auio.uio_resid += iov->iov_len;
3519                 iov++;
3520         }
3521         cnt = auio.uio_resid;
3522         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3523         cnt -= auio.uio_resid;
3524         uap->sysmsg_result = cnt;
3525 done:
3526         vput(vp);
3527         nlookup_done(&nd);
3528         if (needfree)
3529                 FREE(needfree, M_IOV);
3530         return(error);
3531 }
3532
3533 /*
3534  * Syscall to delete a named extended attribute from a file or directory.
3535  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3536  */
3537 int
3538 extattr_delete_file(struct extattr_delete_file_args *uap)
3539 {
3540         char attrname[EXTATTR_MAXNAMELEN];
3541         struct nlookupdata nd;
3542         struct vnode *vp;
3543         int error;
3544
3545         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3546         if (error)
3547                 return(error);
3548
3549         vp = NULL;
3550         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3551         if (error == 0)
3552                 error = nlookup(&nd);
3553         if (error == 0)
3554                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3555         if (error) {
3556                 nlookup_done(&nd);
3557                 return (error);
3558         }
3559
3560         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3561         vput(vp);
3562         nlookup_done(&nd);
3563         return(error);
3564 }
3565
3566 static int
3567 chroot_visible_mnt(struct mount *mp, struct proc *p)
3568 {
3569         struct namecache *ncp;
3570         /*
3571          * First check if this file system is below
3572          * the chroot path.
3573          */
3574         ncp = mp->mnt_ncp;
3575         while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3576                 ncp = ncp->nc_parent;
3577         if (ncp == NULL) {
3578                 /*
3579                  * This is not below the chroot path.
3580                  *
3581                  * Check if the chroot path is on the same filesystem,
3582                  * by determing if we have to cross a mount point
3583                  * before reaching mp->mnt_ncp.
3584                  */
3585                 ncp = p->p_fd->fd_nrdir;
3586                 while (ncp != NULL && ncp != mp->mnt_ncp) {
3587                         if (ncp->nc_flag & NCF_MOUNTPT) {
3588                                 ncp = NULL;
3589                                 break;
3590                         }
3591                         ncp = ncp->nc_parent;
3592                 }
3593         }
3594         return(ncp != NULL);
3595 }