We have to use pmap_extract() here. If we lose a race against page
[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.88 2006/05/06 18:48:52 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 filedesc *fdp = p->p_fd;
700         struct file *fp;
701         void *ctl = NULL;
702         void *buf = NULL;
703         char *path = NULL;
704         int error;
705
706         /*
707          * Sanity and permissions checks.  We must be root.
708          */
709         KKASSERT(p);
710         if (p->p_ucred->cr_prison != NULL)
711                 return (EPERM);
712         if ((error = suser(td)) != 0)
713                 return (error);
714
715         /*
716          * Argument length checks
717          */
718         if (uap->ctllen < 0 || uap->ctllen > 1024)
719                 return (EINVAL);
720         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
721                 return (EINVAL);
722         if (uap->path == NULL)
723                 return (EINVAL);
724
725         /*
726          * Allocate the necessary buffers and copyin data
727          */
728         path = zalloc(namei_zone);
729         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
730         if (error)
731                 goto done;
732
733         if (uap->ctllen) {
734                 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
735                 error = copyin(uap->ctl, ctl, uap->ctllen);
736                 if (error)
737                         goto done;
738         }
739         if (uap->buflen)
740                 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
741
742         /*
743          * Validate the descriptor
744          */
745         if (uap->fd == -1) {
746                 fp = NULL;
747         } else if ((u_int)uap->fd >= fdp->fd_nfiles ||
748             (fp = fdp->fd_files[uap->fd].fp) == NULL) {
749                 error = EBADF;
750                 goto done;
751         }
752         if (fp)
753                 fhold(fp);
754
755         /*
756          * Execute the internal kernel function and clean up.
757          */
758         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
759         if (fp)
760                 fdrop(fp);
761         if (error == 0 && uap->sysmsg_result > 0)
762                 error = copyout(buf, uap->buf, uap->sysmsg_result);
763 done:
764         if (path)
765                 zfree(namei_zone, path);
766         if (ctl)
767                 free(ctl, M_TEMP);
768         if (buf)
769                 free(buf, M_TEMP);
770         return (error);
771 }
772
773 /*
774  * Execute a mount control operation by resolving the path to a mount point
775  * and calling vop_mountctl().  
776  */
777 int
778 kern_mountctl(const char *path, int op, struct file *fp, 
779                 const void *ctl, int ctllen, 
780                 void *buf, int buflen, int *res)
781 {
782         struct vnode *vp;
783         struct mount *mp;
784         struct nlookupdata nd;
785         int error;
786
787         *res = 0;
788         vp = NULL;
789         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
790         if (error == 0)
791                 error = nlookup(&nd);
792         if (error == 0)
793                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
794         nlookup_done(&nd);
795         if (error)
796                 return (error);
797
798         mp = vp->v_mount;
799
800         /*
801          * Must be the root of the filesystem
802          */
803         if ((vp->v_flag & VROOT) == 0) {
804                 vput(vp);
805                 return (EINVAL);
806         }
807         error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, 
808                                 buf, buflen, res);
809         vput(vp);
810         return (error);
811 }
812
813 int
814 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
815 {
816         struct thread *td = curthread;
817         struct proc *p = td->td_proc;
818         struct mount *mp;
819         struct statfs *sp;
820         char *fullpath, *freepath;
821         int error;
822
823         if ((error = nlookup(nd)) != 0)
824                 return (error);
825         mp = nd->nl_ncp->nc_mount;
826         sp = &mp->mnt_stat;
827         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
828                 return (error);
829
830         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
831         if (error)
832                 return(error);
833         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
834         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
835         free(freepath, M_TEMP);
836
837         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
838         bcopy(sp, buf, sizeof(*buf));
839         /* Only root should have access to the fsid's. */
840         if (suser(td))
841                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
842         return (0);
843 }
844
845 /*
846  * statfs_args(char *path, struct statfs *buf)
847  *
848  * Get filesystem statistics.
849  */
850 int
851 statfs(struct statfs_args *uap)
852 {
853         struct nlookupdata nd;
854         struct statfs buf;
855         int error;
856
857         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
858         if (error == 0)
859                 error = kern_statfs(&nd, &buf);
860         nlookup_done(&nd);
861         if (error == 0)
862                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
863         return (error);
864 }
865
866 int
867 kern_fstatfs(int fd, struct statfs *buf)
868 {
869         struct thread *td = curthread;
870         struct proc *p = td->td_proc;
871         struct file *fp;
872         struct mount *mp;
873         struct statfs *sp;
874         char *fullpath, *freepath;
875         int error;
876
877         KKASSERT(p);
878         error = getvnode(p->p_fd, fd, &fp);
879         if (error)
880                 return (error);
881         mp = ((struct vnode *)fp->f_data)->v_mount;
882         if (mp == NULL)
883                 return (EBADF);
884         if (fp->f_cred == NULL)
885                 return (EINVAL);
886         sp = &mp->mnt_stat;
887         error = VFS_STATFS(mp, sp, fp->f_cred);
888         if (error)
889                 return (error);
890
891         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
892         if (error)
893                 return(error);
894         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
895         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
896         free(freepath, M_TEMP);
897
898         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
899         bcopy(sp, buf, sizeof(*buf));
900
901         /* Only root should have access to the fsid's. */
902         if (suser(td))
903                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
904         return (0);
905 }
906
907 /*
908  * fstatfs_args(int fd, struct statfs *buf)
909  *
910  * Get filesystem statistics.
911  */
912 int
913 fstatfs(struct fstatfs_args *uap)
914 {
915         struct statfs buf;
916         int error;
917
918         error = kern_fstatfs(uap->fd, &buf);
919
920         if (error == 0)
921                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
922         return (error);
923 }
924
925 /*
926  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
927  *
928  * Get statistics on all filesystems.
929  */
930
931 struct getfsstat_info {
932         struct statfs *sfsp;
933         long count;
934         long maxcount;
935         int error;
936         int flags;
937         int is_chrooted;
938         struct proc *p;
939 };
940
941 static int getfsstat_callback(struct mount *, void *);
942
943 /* ARGSUSED */
944 int
945 getfsstat(struct getfsstat_args *uap)
946 {
947         struct thread *td = curthread;
948         struct proc *p = td->td_proc;
949         struct getfsstat_info info;
950
951         bzero(&info, sizeof(info));
952         if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
953                 info.is_chrooted = 1;
954         else
955                 info.is_chrooted = 0;
956
957         info.maxcount = uap->bufsize / sizeof(struct statfs);
958         info.sfsp = uap->buf;
959         info.count = 0;
960         info.flags = uap->flags;
961         info.p = p;
962
963         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
964         if (info.sfsp && info.count > info.maxcount)
965                 uap->sysmsg_result = info.maxcount;
966         else
967                 uap->sysmsg_result = info.count;
968         return (info.error);
969 }
970
971 static int
972 getfsstat_callback(struct mount *mp, void *data)
973 {
974         struct getfsstat_info *info = data;
975         struct statfs *sp;
976         char *freepath;
977         char *fullpath;
978         int error;
979
980         if (info->sfsp && info->count < info->maxcount) {
981                 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
982                         return(0);
983                 sp = &mp->mnt_stat;
984
985                 /*
986                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
987                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
988                  * overrides MNT_WAIT.
989                  */
990                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
991                     (info->flags & MNT_WAIT)) &&
992                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
993                         return(0);
994                 }
995                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
996
997                 error = cache_fullpath(info->p, mp->mnt_ncp,
998                                         &fullpath, &freepath);
999                 if (error) {
1000                         info->error = error;
1001                         return(-1);
1002                 }
1003                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1004                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1005                 free(freepath, M_TEMP);
1006
1007                 error = copyout(sp, info->sfsp, sizeof(*sp));
1008                 if (error) {
1009                         info->error = error;
1010                         return (-1);
1011                 }
1012                 ++info->sfsp;
1013         }
1014         info->count++;
1015         return(0);
1016 }
1017
1018 /*
1019  * fchdir_args(int fd)
1020  *
1021  * Change current working directory to a given file descriptor.
1022  */
1023 /* ARGSUSED */
1024 int
1025 fchdir(struct fchdir_args *uap)
1026 {
1027         struct thread *td = curthread;
1028         struct proc *p = td->td_proc;
1029         struct filedesc *fdp = p->p_fd;
1030         struct vnode *vp, *ovp;
1031         struct mount *mp;
1032         struct file *fp;
1033         struct namecache *ncp, *oncp;
1034         struct namecache *nct;
1035         int error;
1036
1037         if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
1038                 return (error);
1039         vp = (struct vnode *)fp->f_data;
1040         vref(vp);
1041         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1042         if (vp->v_type != VDIR || fp->f_ncp == NULL)
1043                 error = ENOTDIR;
1044         else
1045                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1046         if (error) {
1047                 vput(vp);
1048                 return (error);
1049         }
1050         ncp = cache_hold(fp->f_ncp);
1051         while (!error && (mp = vp->v_mountedhere) != NULL) {
1052                 error = nlookup_mp(mp, &nct);
1053                 if (error == 0) {
1054                         cache_unlock(nct);      /* leave ref intact */
1055                         vput(vp);
1056                         vp = nct->nc_vp;
1057                         error = vget(vp, LK_SHARED);
1058                         KKASSERT(error == 0);
1059                         cache_drop(ncp);
1060                         ncp = nct;
1061                 }
1062         }
1063         if (error == 0) {
1064                 ovp = fdp->fd_cdir;
1065                 oncp = fdp->fd_ncdir;
1066                 VOP_UNLOCK(vp, 0);      /* leave ref intact */
1067                 fdp->fd_cdir = vp;
1068                 fdp->fd_ncdir = ncp;
1069                 cache_drop(oncp);
1070                 vrele(ovp);
1071         } else {
1072                 cache_drop(ncp);
1073                 vput(vp);
1074         }
1075         return (error);
1076 }
1077
1078 int
1079 kern_chdir(struct nlookupdata *nd)
1080 {
1081         struct thread *td = curthread;
1082         struct proc *p = td->td_proc;
1083         struct filedesc *fdp = p->p_fd;
1084         struct vnode *vp, *ovp;
1085         struct namecache *oncp;
1086         int error;
1087
1088         if ((error = nlookup(nd)) != 0)
1089                 return (error);
1090         if ((vp = nd->nl_ncp->nc_vp) == NULL)
1091                 return (ENOENT);
1092         if ((error = vget(vp, LK_SHARED)) != 0)
1093                 return (error);
1094
1095         error = checkvp_chdir(vp, td);
1096         VOP_UNLOCK(vp, 0);
1097         if (error == 0) {
1098                 ovp = fdp->fd_cdir;
1099                 oncp = fdp->fd_ncdir;
1100                 cache_unlock(nd->nl_ncp);       /* leave reference intact */
1101                 fdp->fd_ncdir = nd->nl_ncp;
1102                 fdp->fd_cdir = vp;
1103                 cache_drop(oncp);
1104                 vrele(ovp);
1105                 nd->nl_ncp = NULL;
1106         } else {
1107                 vrele(vp);
1108         }
1109         return (error);
1110 }
1111
1112 /*
1113  * chdir_args(char *path)
1114  *
1115  * Change current working directory (``.'').
1116  */
1117 int
1118 chdir(struct chdir_args *uap)
1119 {
1120         struct nlookupdata nd;
1121         int error;
1122
1123         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1124         if (error == 0)
1125                 error = kern_chdir(&nd);
1126         nlookup_done(&nd);
1127         return (error);
1128 }
1129
1130 /*
1131  * Helper function for raised chroot(2) security function:  Refuse if
1132  * any filedescriptors are open directories.
1133  */
1134 static int
1135 chroot_refuse_vdir_fds(fdp)
1136         struct filedesc *fdp;
1137 {
1138         struct vnode *vp;
1139         struct file *fp;
1140         int error;
1141         int fd;
1142
1143         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1144                 error = getvnode(fdp, fd, &fp);
1145                 if (error)
1146                         continue;
1147                 vp = (struct vnode *)fp->f_data;
1148                 if (vp->v_type != VDIR)
1149                         continue;
1150                 return(EPERM);
1151         }
1152         return (0);
1153 }
1154
1155 /*
1156  * This sysctl determines if we will allow a process to chroot(2) if it
1157  * has a directory open:
1158  *      0: disallowed for all processes.
1159  *      1: allowed for processes that were not already chroot(2)'ed.
1160  *      2: allowed for all processes.
1161  */
1162
1163 static int chroot_allow_open_directories = 1;
1164
1165 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1166      &chroot_allow_open_directories, 0, "");
1167
1168 /*
1169  * chroot to the specified namecache entry.  We obtain the vp from the
1170  * namecache data.  The passed ncp must be locked and referenced and will
1171  * remain locked and referenced on return.
1172  */
1173 int
1174 kern_chroot(struct namecache *ncp)
1175 {
1176         struct thread *td = curthread;
1177         struct proc *p = td->td_proc;
1178         struct filedesc *fdp = p->p_fd;
1179         struct vnode *vp;
1180         int error;
1181
1182         /*
1183          * Only root can chroot
1184          */
1185         if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1186                 return (error);
1187
1188         /*
1189          * Disallow open directory descriptors (fchdir() breakouts).
1190          */
1191         if (chroot_allow_open_directories == 0 ||
1192            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1193                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1194                         return (error);
1195         }
1196         if ((vp = ncp->nc_vp) == NULL)
1197                 return (ENOENT);
1198
1199         if ((error = vget(vp, LK_SHARED)) != 0)
1200                 return (error);
1201
1202         /*
1203          * Check the validity of vp as a directory to change to and 
1204          * associate it with rdir/jdir.
1205          */
1206         error = checkvp_chdir(vp, td);
1207         VOP_UNLOCK(vp, 0);      /* leave reference intact */
1208         if (error == 0) {
1209                 vrele(fdp->fd_rdir);
1210                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1211                 cache_drop(fdp->fd_nrdir);
1212                 fdp->fd_nrdir = cache_hold(ncp);
1213                 if (fdp->fd_jdir == NULL) {
1214                         fdp->fd_jdir = vp;
1215                         vref(fdp->fd_jdir);
1216                         fdp->fd_njdir = cache_hold(ncp);
1217                 }
1218         } else {
1219                 vrele(vp);
1220         }
1221         return (error);
1222 }
1223
1224 /*
1225  * chroot_args(char *path)
1226  *
1227  * Change notion of root (``/'') directory.
1228  */
1229 /* ARGSUSED */
1230 int
1231 chroot(struct chroot_args *uap)
1232 {
1233         struct thread *td = curthread;
1234         struct nlookupdata nd;
1235         int error;
1236
1237         KKASSERT(td->td_proc);
1238         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1239         if (error) {
1240                 nlookup_done(&nd);
1241                 return(error);
1242         }
1243         error = nlookup(&nd);
1244         if (error == 0)
1245                 error = kern_chroot(nd.nl_ncp);
1246         nlookup_done(&nd);
1247         return(error);
1248 }
1249
1250 /*
1251  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1252  * determine whether it is legal to chdir to the vnode.  The vnode's state
1253  * is not changed by this call.
1254  */
1255 int
1256 checkvp_chdir(struct vnode *vp, struct thread *td)
1257 {
1258         int error;
1259
1260         if (vp->v_type != VDIR)
1261                 error = ENOTDIR;
1262         else
1263                 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1264         return (error);
1265 }
1266
1267 int
1268 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1269 {
1270         struct thread *td = curthread;
1271         struct proc *p = td->td_proc;
1272         struct lwp *lp = td->td_lwp;
1273         struct filedesc *fdp = p->p_fd;
1274         int cmode, flags;
1275         struct file *nfp;
1276         struct file *fp;
1277         struct vnode *vp;
1278         int type, indx, error;
1279         struct flock lf;
1280
1281         if ((oflags & O_ACCMODE) == O_ACCMODE)
1282                 return (EINVAL);
1283         flags = FFLAGS(oflags);
1284         error = falloc(p, &nfp, NULL);
1285         if (error)
1286                 return (error);
1287         fp = nfp;
1288         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1289
1290         /*
1291          * XXX p_dupfd is a real mess.  It allows a device to return a
1292          * file descriptor to be duplicated rather then doing the open
1293          * itself.
1294          */
1295         lp->lwp_dupfd = -1;
1296
1297         /*
1298          * Call vn_open() to do the lookup and assign the vnode to the 
1299          * file pointer.  vn_open() does not change the ref count on fp
1300          * and the vnode, on success, will be inherited by the file pointer
1301          * and unlocked.
1302          */
1303         nd->nl_flags |= NLC_LOCKVP;
1304         error = vn_open(nd, fp, flags, cmode);
1305         nlookup_done(nd);
1306         if (error) {
1307                 /*
1308                  * handle special fdopen() case.  bleh.  dupfdopen() is
1309                  * responsible for dropping the old contents of ofiles[indx]
1310                  * if it succeeds.
1311                  *
1312                  * Note that if fsetfd() succeeds it will add a ref to fp
1313                  * which represents the fd_files[] assignment.  We must still
1314                  * drop our reference.
1315                  */
1316                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1317                         if (fsetfd(p, fp, &indx) == 0) {
1318                                 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1319                                 if (error == 0) {
1320                                         *res = indx;
1321                                         fdrop(fp);      /* our ref */
1322                                         return (0);
1323                                 }
1324                                 if (fdp->fd_files[indx].fp == fp) {
1325                                         funsetfd(fdp, indx);
1326                                         fdrop(fp);      /* fd_files[] ref */
1327                                 }
1328                         }
1329                 }
1330                 fdrop(fp);      /* our ref */
1331                 if (error == ERESTART)
1332                         error = EINTR;
1333                 return (error);
1334         }
1335
1336         /*
1337          * ref the vnode for ourselves so it can't be ripped out from under
1338          * is.  XXX need an ND flag to request that the vnode be returned
1339          * anyway.
1340          */
1341         vp = (struct vnode *)fp->f_data;
1342         vref(vp);
1343         if ((error = fsetfd(p, fp, &indx)) != 0) {
1344                 fdrop(fp);
1345                 vrele(vp);
1346                 return (error);
1347         }
1348
1349         /*
1350          * If no error occurs the vp will have been assigned to the file
1351          * pointer.
1352          */
1353         lp->lwp_dupfd = 0;
1354
1355         /*
1356          * There should be 2 references on the file, one from the descriptor
1357          * table, and one for us.
1358          *
1359          * Handle the case where someone closed the file (via its file
1360          * descriptor) while we were blocked.  The end result should look
1361          * like opening the file succeeded but it was immediately closed.
1362          */
1363         if (fp->f_count == 1) {
1364                 KASSERT(fdp->fd_files[indx].fp != fp,
1365                     ("Open file descriptor lost all refs"));
1366                 vrele(vp);
1367                 fo_close(fp);
1368                 fdrop(fp);
1369                 *res = indx;
1370                 return 0;
1371         }
1372
1373         if (flags & (O_EXLOCK | O_SHLOCK)) {
1374                 lf.l_whence = SEEK_SET;
1375                 lf.l_start = 0;
1376                 lf.l_len = 0;
1377                 if (flags & O_EXLOCK)
1378                         lf.l_type = F_WRLCK;
1379                 else
1380                         lf.l_type = F_RDLCK;
1381                 type = F_FLOCK;
1382                 if ((flags & FNONBLOCK) == 0)
1383                         type |= F_WAIT;
1384
1385                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1386                         /*
1387                          * lock request failed.  Normally close the descriptor
1388                          * but handle the case where someone might have dup()d
1389                          * it when we weren't looking.  One reference is
1390                          * owned by the descriptor array, the other by us.
1391                          */
1392                         vrele(vp);
1393                         if (fdp->fd_files[indx].fp == fp) {
1394                                 funsetfd(fdp, indx);
1395                                 fdrop(fp);
1396                         }
1397                         fdrop(fp);
1398                         return (error);
1399                 }
1400                 fp->f_flag |= FHASLOCK;
1401         }
1402
1403 #if 0
1404         /*
1405          * Assert that all regular file vnodes were created with a object.
1406          */
1407         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1408                 ("open: regular file has no backing object after vn_open"));
1409 #endif
1410
1411         vrele(vp);
1412
1413         /*
1414          * release our private reference, leaving the one associated with the
1415          * descriptor table intact.
1416          */
1417         fdrop(fp);
1418         *res = indx;
1419         return (0);
1420 }
1421
1422 /*
1423  * open_args(char *path, int flags, int mode)
1424  *
1425  * Check permissions, allocate an open file structure,
1426  * and call the device open routine if any.
1427  */
1428 int
1429 open(struct open_args *uap)
1430 {
1431         struct nlookupdata nd;
1432         int error;
1433
1434         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1435         if (error == 0) {
1436                 error = kern_open(&nd, uap->flags,
1437                                     uap->mode, &uap->sysmsg_result);
1438         }
1439         nlookup_done(&nd);
1440         return (error);
1441 }
1442
1443 int
1444 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1445 {
1446         struct namecache *ncp;
1447         struct thread *td = curthread;
1448         struct proc *p = td->td_proc;
1449         struct vnode *vp;
1450         struct vattr vattr;
1451         int error;
1452         int whiteout = 0;
1453
1454         KKASSERT(p);
1455
1456         switch (mode & S_IFMT) {
1457         case S_IFCHR:
1458         case S_IFBLK:
1459                 error = suser(td);
1460                 break;
1461         default:
1462                 error = suser_cred(p->p_ucred, PRISON_ROOT);
1463                 break;
1464         }
1465         if (error)
1466                 return (error);
1467
1468         bwillwrite();
1469         nd->nl_flags |= NLC_CREATE;
1470         if ((error = nlookup(nd)) != 0)
1471                 return (error);
1472         ncp = nd->nl_ncp;
1473         if (ncp->nc_vp)
1474                 return (EEXIST);
1475
1476         VATTR_NULL(&vattr);
1477         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1478         vattr.va_rdev = dev;
1479         whiteout = 0;
1480
1481         switch (mode & S_IFMT) {
1482         case S_IFMT:    /* used by badsect to flag bad sectors */
1483                 vattr.va_type = VBAD;
1484                 break;
1485         case S_IFCHR:
1486                 vattr.va_type = VCHR;
1487                 break;
1488         case S_IFBLK:
1489                 vattr.va_type = VBLK;
1490                 break;
1491         case S_IFWHT:
1492                 whiteout = 1;
1493                 break;
1494         default:
1495                 error = EINVAL;
1496                 break;
1497         }
1498         if (error == 0) {
1499                 if (whiteout) {
1500                         error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1501                 } else {
1502                         vp = NULL;
1503                         error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1504                         if (error == 0)
1505                                 vput(vp);
1506                 }
1507         }
1508         return (error);
1509 }
1510
1511 /*
1512  * mknod_args(char *path, int mode, int dev)
1513  *
1514  * Create a special file.
1515  */
1516 int
1517 mknod(struct mknod_args *uap)
1518 {
1519         struct nlookupdata nd;
1520         int error;
1521
1522         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1523         if (error == 0)
1524                 error = kern_mknod(&nd, uap->mode, uap->dev);
1525         nlookup_done(&nd);
1526         return (error);
1527 }
1528
1529 int
1530 kern_mkfifo(struct nlookupdata *nd, int mode)
1531 {
1532         struct namecache *ncp;
1533         struct thread *td = curthread;
1534         struct proc *p = td->td_proc;
1535         struct vattr vattr;
1536         struct vnode *vp;
1537         int error;
1538
1539         bwillwrite();
1540
1541         nd->nl_flags |= NLC_CREATE;
1542         if ((error = nlookup(nd)) != 0)
1543                 return (error);
1544         ncp = nd->nl_ncp;
1545         if (ncp->nc_vp)
1546                 return (EEXIST);
1547
1548         VATTR_NULL(&vattr);
1549         vattr.va_type = VFIFO;
1550         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1551         vp = NULL;
1552         error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1553         if (error == 0)
1554                 vput(vp);
1555         return (error);
1556 }
1557
1558 /*
1559  * mkfifo_args(char *path, int mode)
1560  *
1561  * Create a named pipe.
1562  */
1563 int
1564 mkfifo(struct mkfifo_args *uap)
1565 {
1566         struct nlookupdata nd;
1567         int error;
1568
1569         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1570         if (error == 0)
1571                 error = kern_mkfifo(&nd, uap->mode);
1572         nlookup_done(&nd);
1573         return (error);
1574 }
1575
1576 static int hardlink_check_uid = 0;
1577 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1578     &hardlink_check_uid, 0, 
1579     "Unprivileged processes cannot create hard links to files owned by other "
1580     "users");
1581 static int hardlink_check_gid = 0;
1582 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1583     &hardlink_check_gid, 0,
1584     "Unprivileged processes cannot create hard links to files owned by other "
1585     "groups");
1586
1587 static int
1588 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1589 {
1590         struct vattr va;
1591         int error;
1592
1593         /*
1594          * Shortcut if disabled
1595          */
1596         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1597                 return (0);
1598
1599         /*
1600          * root cred can always hardlink
1601          */
1602         if (suser_cred(cred, PRISON_ROOT) == 0)
1603                 return (0);
1604
1605         /*
1606          * Otherwise only if the originating file is owned by the
1607          * same user or group.  Note that any group is allowed if
1608          * the file is owned by the caller.
1609          */
1610         error = VOP_GETATTR(vp, &va);
1611         if (error != 0)
1612                 return (error);
1613         
1614         if (hardlink_check_uid) {
1615                 if (cred->cr_uid != va.va_uid)
1616                         return (EPERM);
1617         }
1618         
1619         if (hardlink_check_gid) {
1620                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1621                         return (EPERM);
1622         }
1623
1624         return (0);
1625 }
1626
1627 int
1628 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1629 {
1630         struct thread *td = curthread;
1631         struct vnode *vp;
1632         int error;
1633
1634         /*
1635          * Lookup the source and obtained a locked vnode.
1636          *
1637          * XXX relookup on vget failure / race ?
1638          */
1639         bwillwrite();
1640         if ((error = nlookup(nd)) != 0)
1641                 return (error);
1642         vp = nd->nl_ncp->nc_vp;
1643         KKASSERT(vp != NULL);
1644         if (vp->v_type == VDIR)
1645                 return (EPERM);         /* POSIX */
1646         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1647                 return (error);
1648
1649         /*
1650          * Unlock the source so we can lookup the target without deadlocking
1651          * (XXX vp is locked already, possible other deadlock?).  The target
1652          * must not exist.
1653          */
1654         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1655         nd->nl_flags &= ~NLC_NCPISLOCKED;
1656         cache_unlock(nd->nl_ncp);
1657
1658         linknd->nl_flags |= NLC_CREATE;
1659         if ((error = nlookup(linknd)) != 0) {
1660                 vput(vp);
1661                 return (error);
1662         }
1663         if (linknd->nl_ncp->nc_vp) {
1664                 vput(vp);
1665                 return (EEXIST);
1666         }
1667
1668         /*
1669          * Finally run the new API VOP.
1670          */
1671         error = can_hardlink(vp, td, td->td_proc->p_ucred);
1672         if (error == 0)
1673                 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1674         vput(vp);
1675         return (error);
1676 }
1677
1678 /*
1679  * link_args(char *path, char *link)
1680  *
1681  * Make a hard file link.
1682  */
1683 int
1684 link(struct link_args *uap)
1685 {
1686         struct nlookupdata nd, linknd;
1687         int error;
1688
1689         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1690         if (error == 0) {
1691                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1692                 if (error == 0)
1693                         error = kern_link(&nd, &linknd);
1694                 nlookup_done(&linknd);
1695         }
1696         nlookup_done(&nd);
1697         return (error);
1698 }
1699
1700 int
1701 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1702 {
1703         struct namecache *ncp;
1704         struct vattr vattr;
1705         struct vnode *vp;
1706         int error;
1707
1708         bwillwrite();
1709         nd->nl_flags |= NLC_CREATE;
1710         if ((error = nlookup(nd)) != 0)
1711                 return (error);
1712         ncp = nd->nl_ncp;
1713         if (ncp->nc_vp)
1714                 return (EEXIST);
1715
1716         VATTR_NULL(&vattr);
1717         vattr.va_mode = mode;
1718         error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1719         if (error == 0)
1720                 vput(vp);
1721         return (error);
1722 }
1723
1724 /*
1725  * symlink(char *path, char *link)
1726  *
1727  * Make a symbolic link.
1728  */
1729 int
1730 symlink(struct symlink_args *uap)
1731 {
1732         struct thread *td = curthread;
1733         struct nlookupdata nd;
1734         char *path;
1735         int error;
1736         int mode;
1737
1738         path = zalloc(namei_zone);
1739         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1740         if (error == 0) {
1741                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1742                 if (error == 0) {
1743                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1744                         error = kern_symlink(&nd, path, mode);
1745                 }
1746                 nlookup_done(&nd);
1747         }
1748         zfree(namei_zone, path);
1749         return (error);
1750 }
1751
1752 /*
1753  * undelete_args(char *path)
1754  *
1755  * Delete a whiteout from the filesystem.
1756  */
1757 /* ARGSUSED */
1758 int
1759 undelete(struct undelete_args *uap)
1760 {
1761         struct nlookupdata nd;
1762         int error;
1763
1764         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1765         bwillwrite();
1766         nd.nl_flags |= NLC_DELETE;
1767         if (error == 0)
1768                 error = nlookup(&nd);
1769         if (error == 0)
1770                 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1771         nlookup_done(&nd);
1772         return (error);
1773 }
1774
1775 int
1776 kern_unlink(struct nlookupdata *nd)
1777 {
1778         struct namecache *ncp;
1779         int error;
1780
1781         bwillwrite();
1782         nd->nl_flags |= NLC_DELETE;
1783         if ((error = nlookup(nd)) != 0)
1784                 return (error);
1785         ncp = nd->nl_ncp;
1786         error = VOP_NREMOVE(ncp, nd->nl_cred);
1787         return (error);
1788 }
1789
1790 /*
1791  * unlink_args(char *path)
1792  *
1793  * Delete a name from the filesystem.
1794  */
1795 int
1796 unlink(struct unlink_args *uap)
1797 {
1798         struct nlookupdata nd;
1799         int error;
1800
1801         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1802         if (error == 0)
1803                 error = kern_unlink(&nd);
1804         nlookup_done(&nd);
1805         return (error);
1806 }
1807
1808 int
1809 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1810 {
1811         struct thread *td = curthread;
1812         struct proc *p = td->td_proc;
1813         struct filedesc *fdp = p->p_fd;
1814         struct file *fp;
1815         struct vattr vattr;
1816         int error;
1817
1818         if ((u_int)fd >= fdp->fd_nfiles ||
1819             (fp = fdp->fd_files[fd].fp) == NULL)
1820                 return (EBADF);
1821         if (fp->f_type != DTYPE_VNODE)
1822                 return (ESPIPE);
1823         switch (whence) {
1824         case L_INCR:
1825                 fp->f_offset += offset;
1826                 break;
1827         case L_XTND:
1828                 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1829                 if (error)
1830                         return (error);
1831                 fp->f_offset = offset + vattr.va_size;
1832                 break;
1833         case L_SET:
1834                 fp->f_offset = offset;
1835                 break;
1836         default:
1837                 return (EINVAL);
1838         }
1839         *res = fp->f_offset;
1840         return (0);
1841 }
1842
1843 /*
1844  * lseek_args(int fd, int pad, off_t offset, int whence)
1845  *
1846  * Reposition read/write file offset.
1847  */
1848 int
1849 lseek(struct lseek_args *uap)
1850 {
1851         int error;
1852
1853         error = kern_lseek(uap->fd, uap->offset, uap->whence,
1854             &uap->sysmsg_offset);
1855
1856         return (error);
1857 }
1858
1859 int
1860 kern_access(struct nlookupdata *nd, int aflags)
1861 {
1862         struct vnode *vp;
1863         int error, flags;
1864
1865         if ((error = nlookup(nd)) != 0)
1866                 return (error);
1867 retry:
1868         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1869         if (error)
1870                 return (error);
1871
1872         /* Flags == 0 means only check for existence. */
1873         if (aflags) {
1874                 flags = 0;
1875                 if (aflags & R_OK)
1876                         flags |= VREAD;
1877                 if (aflags & W_OK)
1878                         flags |= VWRITE;
1879                 if (aflags & X_OK)
1880                         flags |= VEXEC;
1881                 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1882                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
1883
1884                 /*
1885                  * If the file handle is stale we have to re-resolve the
1886                  * entry.  This is a hack at the moment.
1887                  */
1888                 if (error == ESTALE) {
1889                         cache_setunresolved(nd->nl_ncp);
1890                         error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1891                         if (error == 0) {
1892                                 vput(vp);
1893                                 vp = NULL;
1894                                 goto retry;
1895                         }
1896                 }
1897         }
1898         vput(vp);
1899         return (error);
1900 }
1901
1902 /*
1903  * access_args(char *path, int flags)
1904  *
1905  * Check access permissions.
1906  */
1907 int
1908 access(struct access_args *uap)
1909 {
1910         struct nlookupdata nd;
1911         int error;
1912
1913         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1914         if (error == 0)
1915                 error = kern_access(&nd, uap->flags);
1916         nlookup_done(&nd);
1917         return (error);
1918 }
1919
1920 int
1921 kern_stat(struct nlookupdata *nd, struct stat *st)
1922 {
1923         int error;
1924         struct vnode *vp;
1925         thread_t td;
1926
1927         if ((error = nlookup(nd)) != 0)
1928                 return (error);
1929 again:
1930         if ((vp = nd->nl_ncp->nc_vp) == NULL)
1931                 return (ENOENT);
1932
1933         td = curthread;
1934         if ((error = vget(vp, LK_SHARED)) != 0)
1935                 return (error);
1936         error = vn_stat(vp, st, nd->nl_cred);
1937
1938         /*
1939          * If the file handle is stale we have to re-resolve the entry.  This
1940          * is a hack at the moment.
1941          */
1942         if (error == ESTALE) {
1943                 cache_setunresolved(nd->nl_ncp);
1944                 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1945                 if (error == 0) {
1946                         vput(vp);
1947                         goto again;
1948                 }
1949         }
1950         vput(vp);
1951         return (error);
1952 }
1953
1954 /*
1955  * stat_args(char *path, struct stat *ub)
1956  *
1957  * Get file status; this version follows links.
1958  */
1959 int
1960 stat(struct stat_args *uap)
1961 {
1962         struct nlookupdata nd;
1963         struct stat st;
1964         int error;
1965
1966         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1967         if (error == 0) {
1968                 error = kern_stat(&nd, &st);
1969                 if (error == 0)
1970                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1971         }
1972         nlookup_done(&nd);
1973         return (error);
1974 }
1975
1976 /*
1977  * lstat_args(char *path, struct stat *ub)
1978  *
1979  * Get file status; this version does not follow links.
1980  */
1981 int
1982 lstat(struct lstat_args *uap)
1983 {
1984         struct nlookupdata nd;
1985         struct stat st;
1986         int error;
1987
1988         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1989         if (error == 0) {
1990                 error = kern_stat(&nd, &st);
1991                 if (error == 0)
1992                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
1993         }
1994         nlookup_done(&nd);
1995         return (error);
1996 }
1997
1998 /*
1999  * pathconf_Args(char *path, int name)
2000  *
2001  * Get configurable pathname variables.
2002  */
2003 /* ARGSUSED */
2004 int
2005 pathconf(struct pathconf_args *uap)
2006 {
2007         struct nlookupdata nd;
2008         struct vnode *vp;
2009         int error;
2010
2011         vp = NULL;
2012         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2013         if (error == 0)
2014                 error = nlookup(&nd);
2015         if (error == 0)
2016                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2017         nlookup_done(&nd);
2018         if (error == 0) {
2019                 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2020                 vput(vp);
2021         }
2022         return (error);
2023 }
2024
2025 /*
2026  * XXX: daver
2027  * kern_readlink isn't properly split yet.  There is a copyin burried
2028  * in VOP_READLINK().
2029  */
2030 int
2031 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2032 {
2033         struct thread *td = curthread;
2034         struct proc *p = td->td_proc;
2035         struct vnode *vp;
2036         struct iovec aiov;
2037         struct uio auio;
2038         int error;
2039
2040         if ((error = nlookup(nd)) != 0)
2041                 return (error);
2042         error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2043         if (error)
2044                 return (error);
2045         if (vp->v_type != VLNK) {
2046                 error = EINVAL;
2047         } else {
2048                 aiov.iov_base = buf;
2049                 aiov.iov_len = count;
2050                 auio.uio_iov = &aiov;
2051                 auio.uio_iovcnt = 1;
2052                 auio.uio_offset = 0;
2053                 auio.uio_rw = UIO_READ;
2054                 auio.uio_segflg = UIO_USERSPACE;
2055                 auio.uio_td = td;
2056                 auio.uio_resid = count;
2057                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2058         }
2059         vput(vp);
2060         *res = count - auio.uio_resid;
2061         return (error);
2062 }
2063
2064 /*
2065  * readlink_args(char *path, char *buf, int count)
2066  *
2067  * Return target name of a symbolic link.
2068  */
2069 int
2070 readlink(struct readlink_args *uap)
2071 {
2072         struct nlookupdata nd;
2073         int error;
2074
2075         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2076         if (error == 0) {
2077                 error = kern_readlink(&nd, uap->buf, uap->count,
2078                                         &uap->sysmsg_result);
2079         }
2080         nlookup_done(&nd);
2081         return (error);
2082 }
2083
2084 static int
2085 setfflags(struct vnode *vp, int flags)
2086 {
2087         struct thread *td = curthread;
2088         struct proc *p = td->td_proc;
2089         int error;
2090         struct vattr vattr;
2091
2092         /*
2093          * Prevent non-root users from setting flags on devices.  When
2094          * a device is reused, users can retain ownership of the device
2095          * if they are allowed to set flags and programs assume that
2096          * chown can't fail when done as root.
2097          */
2098         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2099             ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2100                 return (error);
2101
2102         /*
2103          * note: vget is required for any operation that might mod the vnode
2104          * so VINACTIVE is properly cleared.
2105          */
2106         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2107                 VATTR_NULL(&vattr);
2108                 vattr.va_flags = flags;
2109                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2110                 vput(vp);
2111         }
2112         return (error);
2113 }
2114
2115 /*
2116  * chflags(char *path, int flags)
2117  *
2118  * Change flags of a file given a path name.
2119  */
2120 /* ARGSUSED */
2121 int
2122 chflags(struct chflags_args *uap)
2123 {
2124         struct nlookupdata nd;
2125         struct vnode *vp;
2126         int error;
2127
2128         vp = NULL;
2129         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2130         /* XXX Add NLC flag indicating modifying operation? */
2131         if (error == 0)
2132                 error = nlookup(&nd);
2133         if (error == 0)
2134                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2135         nlookup_done(&nd);
2136         if (error == 0) {
2137                 error = setfflags(vp, uap->flags);
2138                 vrele(vp);
2139         }
2140         return (error);
2141 }
2142
2143 /*
2144  * fchflags_args(int fd, int flags)
2145  *
2146  * Change flags of a file given a file descriptor.
2147  */
2148 /* ARGSUSED */
2149 int
2150 fchflags(struct fchflags_args *uap)
2151 {
2152         struct thread *td = curthread;
2153         struct proc *p = td->td_proc;
2154         struct file *fp;
2155         int error;
2156
2157         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2158                 return (error);
2159         return setfflags((struct vnode *) fp->f_data, uap->flags);
2160 }
2161
2162 static int
2163 setfmode(struct vnode *vp, int mode)
2164 {
2165         struct thread *td = curthread;
2166         struct proc *p = td->td_proc;
2167         int error;
2168         struct vattr vattr;
2169
2170         /*
2171          * note: vget is required for any operation that might mod the vnode
2172          * so VINACTIVE is properly cleared.
2173          */
2174         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2175                 VATTR_NULL(&vattr);
2176                 vattr.va_mode = mode & ALLPERMS;
2177                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2178                 vput(vp);
2179         }
2180         return error;
2181 }
2182
2183 int
2184 kern_chmod(struct nlookupdata *nd, int mode)
2185 {
2186         struct vnode *vp;
2187         int error;
2188
2189         /* XXX Add NLC flag indicating modifying operation? */
2190         if ((error = nlookup(nd)) != 0)
2191                 return (error);
2192         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2193                 return (error);
2194         error = setfmode(vp, mode);
2195         vrele(vp);
2196         return (error);
2197 }
2198
2199 /*
2200  * chmod_args(char *path, int mode)
2201  *
2202  * Change mode of a file given path name.
2203  */
2204 /* ARGSUSED */
2205 int
2206 chmod(struct chmod_args *uap)
2207 {
2208         struct nlookupdata nd;
2209         int error;
2210
2211         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2212         if (error == 0)
2213                 error = kern_chmod(&nd, uap->mode);
2214         nlookup_done(&nd);
2215         return (error);
2216 }
2217
2218 /*
2219  * lchmod_args(char *path, int mode)
2220  *
2221  * Change mode of a file given path name (don't follow links.)
2222  */
2223 /* ARGSUSED */
2224 int
2225 lchmod(struct lchmod_args *uap)
2226 {
2227         struct nlookupdata nd;
2228         int error;
2229
2230         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2231         if (error == 0)
2232                 error = kern_chmod(&nd, uap->mode);
2233         nlookup_done(&nd);
2234         return (error);
2235 }
2236
2237 /*
2238  * fchmod_args(int fd, int mode)
2239  *
2240  * Change mode of a file given a file descriptor.
2241  */
2242 /* ARGSUSED */
2243 int
2244 fchmod(struct fchmod_args *uap)
2245 {
2246         struct thread *td = curthread;
2247         struct proc *p = td->td_proc;
2248         struct file *fp;
2249         int error;
2250
2251         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2252                 return (error);
2253         return setfmode((struct vnode *)fp->f_data, uap->mode);
2254 }
2255
2256 static int
2257 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2258 {
2259         struct thread *td = curthread;
2260         struct proc *p = td->td_proc;
2261         int error;
2262         struct vattr vattr;
2263
2264         /*
2265          * note: vget is required for any operation that might mod the vnode
2266          * so VINACTIVE is properly cleared.
2267          */
2268         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2269                 VATTR_NULL(&vattr);
2270                 vattr.va_uid = uid;
2271                 vattr.va_gid = gid;
2272                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2273                 vput(vp);
2274         }
2275         return error;
2276 }
2277
2278 int
2279 kern_chown(struct nlookupdata *nd, int uid, int gid)
2280 {
2281         struct vnode *vp;
2282         int error;
2283
2284         /* XXX Add NLC flag indicating modifying operation? */
2285         if ((error = nlookup(nd)) != 0)
2286                 return (error);
2287         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2288                 return (error);
2289         error = setfown(vp, uid, gid);
2290         vrele(vp);
2291         return (error);
2292 }
2293
2294 /*
2295  * chown(char *path, int uid, int gid)
2296  *
2297  * Set ownership given a path name.
2298  */
2299 int
2300 chown(struct chown_args *uap)
2301 {
2302         struct nlookupdata nd;
2303         int error;
2304
2305         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2306         if (error == 0)
2307                 error = kern_chown(&nd, uap->uid, uap->gid);
2308         nlookup_done(&nd);
2309         return (error);
2310 }
2311
2312 /*
2313  * lchown_args(char *path, int uid, int gid)
2314  *
2315  * Set ownership given a path name, do not cross symlinks.
2316  */
2317 int
2318 lchown(struct lchown_args *uap)
2319 {
2320         struct nlookupdata nd;
2321         int error;
2322
2323         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2324         if (error == 0)
2325                 error = kern_chown(&nd, uap->uid, uap->gid);
2326         nlookup_done(&nd);
2327         return (error);
2328 }
2329
2330 /*
2331  * fchown_args(int fd, int uid, int gid)
2332  *
2333  * Set ownership given a file descriptor.
2334  */
2335 /* ARGSUSED */
2336 int
2337 fchown(struct fchown_args *uap)
2338 {
2339         struct thread *td = curthread;
2340         struct proc *p = td->td_proc;
2341         struct file *fp;
2342         int error;
2343
2344         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2345                 return (error);
2346         return setfown((struct vnode *)fp->f_data,
2347                 uap->uid, uap->gid);
2348 }
2349
2350 static int
2351 getutimes(const struct timeval *tvp, struct timespec *tsp)
2352 {
2353         struct timeval tv[2];
2354
2355         if (tvp == NULL) {
2356                 microtime(&tv[0]);
2357                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2358                 tsp[1] = tsp[0];
2359         } else {
2360                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2361                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2362         }
2363         return 0;
2364 }
2365
2366 static int
2367 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2368 {
2369         struct thread *td = curthread;
2370         struct proc *p = td->td_proc;
2371         int error;
2372         struct vattr vattr;
2373
2374         /*
2375          * note: vget is required for any operation that might mod the vnode
2376          * so VINACTIVE is properly cleared.
2377          */
2378         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2379                 VATTR_NULL(&vattr);
2380                 vattr.va_atime = ts[0];
2381                 vattr.va_mtime = ts[1];
2382                 if (nullflag)
2383                         vattr.va_vaflags |= VA_UTIMES_NULL;
2384                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2385                 vput(vp);
2386         }
2387         return error;
2388 }
2389
2390 int
2391 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2392 {
2393         struct timespec ts[2];
2394         struct vnode *vp;
2395         int error;
2396
2397         if ((error = getutimes(tptr, ts)) != 0)
2398                 return (error);
2399         /* XXX Add NLC flag indicating modifying operation? */
2400         if ((error = nlookup(nd)) != 0)
2401                 return (error);
2402         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2403                 return (error);
2404         error = setutimes(vp, ts, tptr == NULL);
2405         vrele(vp);
2406         return (error);
2407 }
2408
2409 /*
2410  * utimes_args(char *path, struct timeval *tptr)
2411  *
2412  * Set the access and modification times of a file.
2413  */
2414 int
2415 utimes(struct utimes_args *uap)
2416 {
2417         struct timeval tv[2];
2418         struct nlookupdata nd;
2419         int error;
2420
2421         if (uap->tptr) {
2422                 error = copyin(uap->tptr, tv, sizeof(tv));
2423                 if (error)
2424                         return (error);
2425         }
2426         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2427         if (error == 0)
2428                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2429         nlookup_done(&nd);
2430         return (error);
2431 }
2432
2433 /*
2434  * lutimes_args(char *path, struct timeval *tptr)
2435  *
2436  * Set the access and modification times of a file.
2437  */
2438 int
2439 lutimes(struct lutimes_args *uap)
2440 {
2441         struct timeval tv[2];
2442         struct nlookupdata nd;
2443         int error;
2444
2445         if (uap->tptr) {
2446                 error = copyin(uap->tptr, tv, sizeof(tv));
2447                 if (error)
2448                         return (error);
2449         }
2450         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2451         if (error == 0)
2452                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2453         nlookup_done(&nd);
2454         return (error);
2455 }
2456
2457 int
2458 kern_futimes(int fd, struct timeval *tptr)
2459 {
2460         struct thread *td = curthread;
2461         struct proc *p = td->td_proc;
2462         struct timespec ts[2];
2463         struct file *fp;
2464         int error;
2465
2466         error = getutimes(tptr, ts);
2467         if (error)
2468                 return (error);
2469         error = getvnode(p->p_fd, fd, &fp);
2470         if (error)
2471                 return (error);
2472         error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2473         return (error);
2474 }
2475
2476 /*
2477  * futimes_args(int fd, struct timeval *tptr)
2478  *
2479  * Set the access and modification times of a file.
2480  */
2481 int
2482 futimes(struct futimes_args *uap)
2483 {
2484         struct timeval tv[2];
2485         int error;
2486
2487         if (uap->tptr) {
2488                 error = copyin(uap->tptr, tv, sizeof(tv));
2489                 if (error)
2490                         return (error);
2491         }
2492
2493         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2494
2495         return (error);
2496 }
2497
2498 int
2499 kern_truncate(struct nlookupdata *nd, off_t length)
2500 {
2501         struct vnode *vp;
2502         struct vattr vattr;
2503         int error;
2504
2505         if (length < 0)
2506                 return(EINVAL);
2507         /* XXX Add NLC flag indicating modifying operation? */
2508         if ((error = nlookup(nd)) != 0)
2509                 return (error);
2510         if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2511                 return (error);
2512         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2513                 vrele(vp);
2514                 return (error);
2515         }
2516         if (vp->v_type == VDIR) {
2517                 error = EISDIR;
2518         } else if ((error = vn_writechk(vp)) == 0 &&
2519             (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2520                 VATTR_NULL(&vattr);
2521                 vattr.va_size = length;
2522                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2523         }
2524         vput(vp);
2525         return (error);
2526 }
2527
2528 /*
2529  * truncate(char *path, int pad, off_t length)
2530  *
2531  * Truncate a file given its path name.
2532  */
2533 int
2534 truncate(struct truncate_args *uap)
2535 {
2536         struct nlookupdata nd;
2537         int error;
2538
2539         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2540         if (error == 0)
2541                 error = kern_truncate(&nd, uap->length);
2542         nlookup_done(&nd);
2543         return error;
2544 }
2545
2546 int
2547 kern_ftruncate(int fd, off_t length)
2548 {
2549         struct thread *td = curthread;
2550         struct proc *p = td->td_proc;
2551         struct vattr vattr;
2552         struct vnode *vp;
2553         struct file *fp;
2554         int error;
2555
2556         if (length < 0)
2557                 return(EINVAL);
2558         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2559                 return (error);
2560         if ((fp->f_flag & FWRITE) == 0)
2561                 return (EINVAL);
2562         vp = (struct vnode *)fp->f_data;
2563         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2564         if (vp->v_type == VDIR)
2565                 error = EISDIR;
2566         else if ((error = vn_writechk(vp)) == 0) {
2567                 VATTR_NULL(&vattr);
2568                 vattr.va_size = length;
2569                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2570         }
2571         VOP_UNLOCK(vp, 0);
2572         return (error);
2573 }
2574
2575 /*
2576  * ftruncate_args(int fd, int pad, off_t length)
2577  *
2578  * Truncate a file given a file descriptor.
2579  */
2580 int
2581 ftruncate(struct ftruncate_args *uap)
2582 {
2583         int error;
2584
2585         error = kern_ftruncate(uap->fd, uap->length);
2586
2587         return (error);
2588 }
2589
2590 /*
2591  * fsync(int fd)
2592  *
2593  * Sync an open file.
2594  */
2595 /* ARGSUSED */
2596 int
2597 fsync(struct fsync_args *uap)
2598 {
2599         struct thread *td = curthread;
2600         struct proc *p = td->td_proc;
2601         struct vnode *vp;
2602         struct file *fp;
2603         vm_object_t obj;
2604         int error;
2605
2606         if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2607                 return (error);
2608         vp = (struct vnode *)fp->f_data;
2609         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2610         if ((obj = vp->v_object) != NULL)
2611                 vm_object_page_clean(obj, 0, 0, 0);
2612         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2613             vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2614             bioops.io_fsync)
2615                 error = (*bioops.io_fsync)(vp);
2616         VOP_UNLOCK(vp, 0);
2617         return (error);
2618 }
2619
2620 int
2621 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2622 {
2623         struct namecache *fncpd;
2624         struct namecache *tncpd;
2625         struct namecache *ncp;
2626         struct mount *mp;
2627         int error;
2628
2629         bwillwrite();
2630         if ((error = nlookup(fromnd)) != 0)
2631                 return (error);
2632         if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2633                 return (ENOENT);
2634         cache_hold(fncpd);
2635
2636         /*
2637          * unlock the source ncp so we can lookup the target ncp without
2638          * deadlocking.  The target may or may not exist so we do not check
2639          * for a target vp like kern_mkdir() and other creation functions do.
2640          *
2641          * The source and target directories are ref'd and rechecked after
2642          * everything is relocked to determine if the source or target file
2643          * has been renamed.
2644          */
2645         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2646         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2647         cache_unlock(fromnd->nl_ncp);
2648
2649         tond->nl_flags |= NLC_CREATE;
2650         if ((error = nlookup(tond)) != 0) {
2651                 cache_drop(fncpd);
2652                 return (error);
2653         }
2654         if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2655                 cache_drop(fncpd);
2656                 return (ENOENT);
2657         }
2658         cache_hold(tncpd);
2659
2660         /*
2661          * If the source and target are the same there is nothing to do
2662          */
2663         if (fromnd->nl_ncp == tond->nl_ncp) {
2664                 cache_drop(fncpd);
2665                 cache_drop(tncpd);
2666                 return (0);
2667         }
2668
2669         /*
2670          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
2671          * may have become invalid while it was unlocked, nc_vp and nc_mount
2672          * could be NULL.
2673          */
2674         if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2675                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2676         } else if (fromnd->nl_ncp > tond->nl_ncp) {
2677                 cache_lock(fromnd->nl_ncp);
2678                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2679         } else {
2680                 cache_unlock(tond->nl_ncp);
2681                 cache_lock(fromnd->nl_ncp);
2682                 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2683                 cache_lock(tond->nl_ncp);
2684                 cache_resolve(tond->nl_ncp, tond->nl_cred);
2685         }
2686         fromnd->nl_flags |= NLC_NCPISLOCKED;
2687
2688         /*
2689          * make sure the parent directories linkages are the same
2690          */
2691         if (fncpd != fromnd->nl_ncp->nc_parent ||
2692             tncpd != tond->nl_ncp->nc_parent) {
2693                 cache_drop(fncpd);
2694                 cache_drop(tncpd);
2695                 return (ENOENT);
2696         }
2697
2698         /*
2699          * Both the source and target must be within the same filesystem and
2700          * in the same filesystem as their parent directories within the
2701          * namecache topology.
2702          *
2703          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2704          */
2705         mp = fncpd->nc_mount;
2706         if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2707             mp != tond->nl_ncp->nc_mount) {
2708                 cache_drop(fncpd);
2709                 cache_drop(tncpd);
2710                 return (EXDEV);
2711         }
2712
2713         /*
2714          * If the target exists and either the source or target is a directory,
2715          * then both must be directories.
2716          *
2717          * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2718          * become NULL.
2719          */
2720         if (tond->nl_ncp->nc_vp) {
2721                 if (fromnd->nl_ncp->nc_vp == NULL) {
2722                         error = ENOENT;
2723                 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2724                         if (tond->nl_ncp->nc_vp->v_type != VDIR)
2725                                 error = ENOTDIR;
2726                 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2727                         error = EISDIR;
2728                 }
2729         }
2730
2731         /*
2732          * You cannot rename a source into itself or a subdirectory of itself.
2733          * We check this by travsersing the target directory upwards looking
2734          * for a match against the source.
2735          */
2736         if (error == 0) {
2737                 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2738                         if (fromnd->nl_ncp == ncp) {
2739                                 error = EINVAL;
2740                                 break;
2741                         }
2742                 }
2743         }
2744
2745         cache_drop(fncpd);
2746         cache_drop(tncpd);
2747
2748         /*
2749          * Even though the namespaces are different, they may still represent
2750          * hardlinks to the same file.  The filesystem might have a hard time
2751          * with this so we issue a NREMOVE of the source instead of a NRENAME
2752          * when we detect the situation.
2753          */
2754         if (error == 0) {
2755                 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2756                         error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2757                 } else {
2758                         error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, 
2759                                             tond->nl_cred);
2760                 }
2761         }
2762         return (error);
2763 }
2764
2765 /*
2766  * rename_args(char *from, char *to)
2767  *
2768  * Rename files.  Source and destination must either both be directories,
2769  * or both not be directories.  If target is a directory, it must be empty.
2770  */
2771 int
2772 rename(struct rename_args *uap)
2773 {
2774         struct nlookupdata fromnd, tond;
2775         int error;
2776
2777         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2778         if (error == 0) {
2779                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2780                 if (error == 0)
2781                         error = kern_rename(&fromnd, &tond);
2782                 nlookup_done(&tond);
2783         }
2784         nlookup_done(&fromnd);
2785         return (error);
2786 }
2787
2788 int
2789 kern_mkdir(struct nlookupdata *nd, int mode)
2790 {
2791         struct thread *td = curthread;
2792         struct proc *p = td->td_proc;
2793         struct namecache *ncp;
2794         struct vnode *vp;
2795         struct vattr vattr;
2796         int error;
2797
2798         bwillwrite();
2799         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2800         if ((error = nlookup(nd)) != 0)
2801                 return (error);
2802
2803         ncp = nd->nl_ncp;
2804         if (ncp->nc_vp)
2805                 return (EEXIST);
2806
2807         VATTR_NULL(&vattr);
2808         vattr.va_type = VDIR;
2809         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2810
2811         vp = NULL;
2812         error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2813         if (error == 0)
2814                 vput(vp);
2815         return (error);
2816 }
2817
2818 /*
2819  * mkdir_args(char *path, int mode)
2820  *
2821  * Make a directory file.
2822  */
2823 /* ARGSUSED */
2824 int
2825 mkdir(struct mkdir_args *uap)
2826 {
2827         struct nlookupdata nd;
2828         int error;
2829
2830         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2831         if (error == 0)
2832                 error = kern_mkdir(&nd, uap->mode);
2833         nlookup_done(&nd);
2834         return (error);
2835 }
2836
2837 int
2838 kern_rmdir(struct nlookupdata *nd)
2839 {
2840         struct namecache *ncp;
2841         int error;
2842
2843         bwillwrite();
2844         nd->nl_flags |= NLC_DELETE;
2845         if ((error = nlookup(nd)) != 0)
2846                 return (error);
2847
2848         ncp = nd->nl_ncp;
2849         error = VOP_NRMDIR(ncp, nd->nl_cred);
2850         return (error);
2851 }
2852
2853 /*
2854  * rmdir_args(char *path)
2855  *
2856  * Remove a directory file.
2857  */
2858 /* ARGSUSED */
2859 int
2860 rmdir(struct rmdir_args *uap)
2861 {
2862         struct nlookupdata nd;
2863         int error;
2864
2865         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2866         if (error == 0)
2867                 error = kern_rmdir(&nd);
2868         nlookup_done(&nd);
2869         return (error);
2870 }
2871
2872 int
2873 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2874     enum uio_seg direction)
2875 {
2876         struct thread *td = curthread;
2877         struct proc *p = td->td_proc;
2878         struct vnode *vp;
2879         struct file *fp;
2880         struct uio auio;
2881         struct iovec aiov;
2882         long loff;
2883         int error, eofflag;
2884
2885         if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2886                 return (error);
2887         if ((fp->f_flag & FREAD) == 0)
2888                 return (EBADF);
2889         vp = (struct vnode *)fp->f_data;
2890 unionread:
2891         if (vp->v_type != VDIR)
2892                 return (EINVAL);
2893         aiov.iov_base = buf;
2894         aiov.iov_len = count;
2895         auio.uio_iov = &aiov;
2896         auio.uio_iovcnt = 1;
2897         auio.uio_rw = UIO_READ;
2898         auio.uio_segflg = direction;
2899         auio.uio_td = td;
2900         auio.uio_resid = count;
2901         /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2902         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2903         loff = auio.uio_offset = fp->f_offset;
2904         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2905         fp->f_offset = auio.uio_offset;
2906         VOP_UNLOCK(vp, 0);
2907         if (error)
2908                 return (error);
2909         if (count == auio.uio_resid) {
2910                 if (union_dircheckp) {
2911                         error = union_dircheckp(td, &vp, fp);
2912                         if (error == -1)
2913                                 goto unionread;
2914                         if (error)
2915                                 return (error);
2916                 }
2917                 if ((vp->v_flag & VROOT) &&
2918                     (vp->v_mount->mnt_flag & MNT_UNION)) {
2919                         struct vnode *tvp = vp;
2920                         vp = vp->v_mount->mnt_vnodecovered;
2921                         vref(vp);
2922                         fp->f_data = vp;
2923                         fp->f_offset = 0;
2924                         vrele(tvp);
2925                         goto unionread;
2926                 }
2927         }
2928         if (basep) {
2929                 *basep = loff;
2930         }
2931         *res = count - auio.uio_resid;
2932         return (error);
2933 }
2934
2935 /*
2936  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2937  *
2938  * Read a block of directory entries in a file system independent format.
2939  */
2940 int
2941 getdirentries(struct getdirentries_args *uap)
2942 {
2943         long base;
2944         int error;
2945
2946         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2947             &uap->sysmsg_result, UIO_USERSPACE);
2948
2949         if (error == 0)
2950                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2951         return (error);
2952 }
2953
2954 /*
2955  * getdents_args(int fd, char *buf, size_t count)
2956  */
2957 int
2958 getdents(struct getdents_args *uap)
2959 {
2960         int error;
2961
2962         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2963             &uap->sysmsg_result, UIO_USERSPACE);
2964
2965         return (error);
2966 }
2967
2968 /*
2969  * umask(int newmask)
2970  *
2971  * Set the mode mask for creation of filesystem nodes.
2972  *
2973  * MP SAFE
2974  */
2975 int
2976 umask(struct umask_args *uap)
2977 {
2978         struct thread *td = curthread;
2979         struct proc *p = td->td_proc;
2980         struct filedesc *fdp;
2981
2982         fdp = p->p_fd;
2983         uap->sysmsg_result = fdp->fd_cmask;
2984         fdp->fd_cmask = uap->newmask & ALLPERMS;
2985         return (0);
2986 }
2987
2988 /*
2989  * revoke(char *path)
2990  *
2991  * Void all references to file by ripping underlying filesystem
2992  * away from vnode.
2993  */
2994 /* ARGSUSED */
2995 int
2996 revoke(struct revoke_args *uap)
2997 {
2998         struct nlookupdata nd;
2999         struct vattr vattr;
3000         struct vnode *vp;
3001         struct ucred *cred;
3002         int error;
3003
3004         vp = NULL;
3005         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3006         if (error == 0)
3007                 error = nlookup(&nd);
3008         if (error == 0)
3009                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3010         cred = crhold(nd.nl_cred);
3011         nlookup_done(&nd);
3012         if (error == 0) {
3013                 if (vp->v_type != VCHR && vp->v_type != VBLK)
3014                         error = EINVAL;
3015                 if (error == 0)
3016                         error = VOP_GETATTR(vp, &vattr);
3017                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3018                         error = suser_cred(cred, PRISON_ROOT);
3019                 if (error == 0 && count_udev(vp->v_udev) > 0) {
3020                         if ((error = vx_lock(vp)) == 0) {
3021                                 VOP_REVOKE(vp, REVOKEALL);
3022                                 vx_unlock(vp);
3023                         }
3024                 }
3025                 vrele(vp);
3026         }
3027         if (cred)
3028                 crfree(cred);
3029         return (error);
3030 }
3031
3032 /*
3033  * Convert a user file descriptor to a kernel file entry.
3034  */
3035 int
3036 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3037 {
3038         struct file *fp;
3039
3040         if ((u_int)fd >= fdp->fd_nfiles ||
3041             (fp = fdp->fd_files[fd].fp) == NULL)
3042                 return (EBADF);
3043         if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3044                 return (EINVAL);
3045         *fpp = fp;
3046         return (0);
3047 }
3048 /*
3049  * getfh_args(char *fname, fhandle_t *fhp)
3050  *
3051  * Get (NFS) file handle
3052  */
3053 int
3054 getfh(struct getfh_args *uap)
3055 {
3056         struct thread *td = curthread;
3057         struct nlookupdata nd;
3058         fhandle_t fh;
3059         struct vnode *vp;
3060         int error;
3061
3062         /*
3063          * Must be super user
3064          */
3065         if ((error = suser(td)) != 0)
3066                 return (error);
3067
3068         vp = NULL;
3069         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3070         if (error == 0)
3071                 error = nlookup(&nd);
3072         if (error == 0)
3073                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3074         nlookup_done(&nd);
3075         if (error == 0) {
3076                 bzero(&fh, sizeof(fh));
3077                 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3078                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3079                 vput(vp);
3080                 if (error == 0)
3081                         error = copyout(&fh, uap->fhp, sizeof(fh));
3082         }
3083         return (error);
3084 }
3085
3086 /*
3087  * fhopen_args(const struct fhandle *u_fhp, int flags)
3088  *
3089  * syscall for the rpc.lockd to use to translate a NFS file handle into
3090  * an open descriptor.
3091  *
3092  * warning: do not remove the suser() call or this becomes one giant
3093  * security hole.
3094  */
3095 int
3096 fhopen(struct fhopen_args *uap)
3097 {
3098         struct thread *td = curthread;
3099         struct proc *p = td->td_proc;
3100         struct mount *mp;
3101         struct vnode *vp;
3102         struct fhandle fhp;
3103         struct vattr vat;
3104         struct vattr *vap = &vat;
3105         struct flock lf;
3106         struct filedesc *fdp = p->p_fd;
3107         int fmode, mode, error, type;
3108         struct file *nfp; 
3109         struct file *fp;
3110         int indx;
3111
3112         /*
3113          * Must be super user
3114          */
3115         error = suser(td);
3116         if (error)
3117                 return (error);
3118
3119         fmode = FFLAGS(uap->flags);
3120         /* why not allow a non-read/write open for our lockd? */
3121         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3122                 return (EINVAL);
3123         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3124         if (error)
3125                 return(error);
3126         /* find the mount point */
3127         mp = vfs_getvfs(&fhp.fh_fsid);
3128         if (mp == NULL)
3129                 return (ESTALE);
3130         /* now give me my vnode, it gets returned to me locked */
3131         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3132         if (error)
3133                 return (error);
3134         /*
3135          * from now on we have to make sure not
3136          * to forget about the vnode
3137          * any error that causes an abort must vput(vp) 
3138          * just set error = err and 'goto bad;'.
3139          */
3140
3141         /* 
3142          * from vn_open 
3143          */
3144         if (vp->v_type == VLNK) {
3145                 error = EMLINK;
3146                 goto bad;
3147         }
3148         if (vp->v_type == VSOCK) {
3149                 error = EOPNOTSUPP;
3150                 goto bad;
3151         }
3152         mode = 0;
3153         if (fmode & (FWRITE | O_TRUNC)) {
3154                 if (vp->v_type == VDIR) {
3155                         error = EISDIR;
3156                         goto bad;
3157                 }
3158                 error = vn_writechk(vp);
3159                 if (error)
3160                         goto bad;
3161                 mode |= VWRITE;
3162         }
3163         if (fmode & FREAD)
3164                 mode |= VREAD;
3165         if (mode) {
3166                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3167                 if (error)
3168                         goto bad;
3169         }
3170         if (fmode & O_TRUNC) {
3171                 VOP_UNLOCK(vp, 0);                      /* XXX */
3172                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3173                 VATTR_NULL(vap);
3174                 vap->va_size = 0;
3175                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3176                 if (error)
3177                         goto bad;
3178         }
3179
3180         /*
3181          * VOP_OPEN needs the file pointer so it can potentially override
3182          * it.
3183          *
3184          * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3185          * XXX
3186          */
3187         if ((error = falloc(p, &nfp, NULL)) != 0)
3188                 goto bad;
3189         fp = nfp;
3190
3191         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3192         if (error) {
3193                 /*
3194                  * setting f_ops this way prevents VOP_CLOSE from being
3195                  * called or fdrop() releasing the vp from v_data.   Since
3196                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3197                  */
3198                 fp->f_ops = &badfileops;
3199                 fp->f_data = NULL;
3200                 fdrop(fp);
3201                 goto bad;
3202         }
3203
3204         /*
3205          * The fp is given its own reference, we still have our ref and lock.
3206          *
3207          * Assert that all regular files must be created with a VM object.
3208          */
3209         if (vp->v_type == VREG && vp->v_object == NULL) {
3210                 printf("fhopen: regular file did not have VM object: %p\n", vp);
3211                 fdrop(fp);
3212                 goto bad;
3213         }
3214
3215         /*
3216          * The open was successful, associate it with a file descriptor.
3217          */
3218         if ((error = fsetfd(p, fp, &indx)) != 0) {
3219                 fdrop(fp);
3220                 goto bad;
3221         }
3222
3223         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3224                 lf.l_whence = SEEK_SET;
3225                 lf.l_start = 0;
3226                 lf.l_len = 0;
3227                 if (fmode & O_EXLOCK)
3228                         lf.l_type = F_WRLCK;
3229                 else
3230                         lf.l_type = F_RDLCK;
3231                 type = F_FLOCK;
3232                 if ((fmode & FNONBLOCK) == 0)
3233                         type |= F_WAIT;
3234                 VOP_UNLOCK(vp, 0);
3235                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3236                         /*
3237                          * lock request failed.  Normally close the descriptor
3238                          * but handle the case where someone might have dup()d
3239                          * or close()d it when we weren't looking.
3240                          */
3241                         if (fdp->fd_files[indx].fp == fp) {
3242                                 funsetfd(fdp, indx);
3243                                 fdrop(fp);
3244                         }
3245
3246                         /*
3247                          * release our private reference.
3248                          */
3249                         fdrop(fp);
3250                         vrele(vp);
3251                         return (error);
3252                 }
3253                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3254                 fp->f_flag |= FHASLOCK;
3255         }
3256
3257         vput(vp);
3258         fdrop(fp);
3259         uap->sysmsg_result = indx;
3260         return (0);
3261
3262 bad:
3263         vput(vp);
3264         return (error);
3265 }
3266
3267 /*
3268  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3269  */
3270 int
3271 fhstat(struct fhstat_args *uap)
3272 {
3273         struct thread *td = curthread;
3274         struct stat sb;
3275         fhandle_t fh;
3276         struct mount *mp;
3277         struct vnode *vp;
3278         int error;
3279
3280         /*
3281          * Must be super user
3282          */
3283         error = suser(td);
3284         if (error)
3285                 return (error);
3286         
3287         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3288         if (error)
3289                 return (error);
3290
3291         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3292                 return (ESTALE);
3293         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3294                 return (error);
3295         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3296         vput(vp);
3297         if (error)
3298                 return (error);
3299         error = copyout(&sb, uap->sb, sizeof(sb));
3300         return (error);
3301 }
3302
3303 /*
3304  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3305  */
3306 int
3307 fhstatfs(struct fhstatfs_args *uap)
3308 {
3309         struct thread *td = curthread;
3310         struct proc *p = td->td_proc;
3311         struct statfs *sp;
3312         struct mount *mp;
3313         struct vnode *vp;
3314         struct statfs sb;
3315         char *fullpath, *freepath;
3316         fhandle_t fh;
3317         int error;
3318
3319         /*
3320          * Must be super user
3321          */
3322         if ((error = suser(td)))
3323                 return (error);
3324
3325         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3326                 return (error);
3327
3328         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3329                 return (ESTALE);
3330
3331         if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3332             !chroot_visible_mnt(mp, p))
3333                 return (ESTALE);
3334
3335         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3336                 return (error);
3337         mp = vp->v_mount;
3338         sp = &mp->mnt_stat;
3339         vput(vp);
3340         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3341                 return (error);
3342
3343         error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3344         if (error)
3345                 return(error);
3346         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3347         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3348         free(freepath, M_TEMP);
3349
3350         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3351         if (suser(td)) {
3352                 bcopy(sp, &sb, sizeof(sb));
3353                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3354                 sp = &sb;
3355         }
3356         return (copyout(sp, uap->buf, sizeof(*sp)));
3357 }
3358
3359 /*
3360  * Syscall to push extended attribute configuration information into the
3361  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3362  * a command (int cmd), and attribute name and misc data.  For now, the
3363  * attribute name is left in userspace for consumption by the VFS_op.
3364  * It will probably be changed to be copied into sysspace by the
3365  * syscall in the future, once issues with various consumers of the
3366  * attribute code have raised their hands.
3367  *
3368  * Currently this is used only by UFS Extended Attributes.
3369  */
3370 int
3371 extattrctl(struct extattrctl_args *uap)
3372 {
3373         struct nlookupdata nd;
3374         struct mount *mp;
3375         struct vnode *vp;
3376         int error;
3377
3378         vp = NULL;
3379         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3380         if (error == 0)
3381                 error = nlookup(&nd);
3382         if (error == 0) {
3383                 mp = nd.nl_ncp->nc_mount;
3384                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
3385                                 uap->attrname, uap->arg, 
3386                                 nd.nl_cred);
3387         }
3388         nlookup_done(&nd);
3389         return (error);
3390 }
3391
3392 /*
3393  * Syscall to set a named extended attribute on a file or directory.
3394  * Accepts attribute name, and a uio structure pointing to the data to set.
3395  * The uio is consumed in the style of writev().  The real work happens
3396  * in VOP_SETEXTATTR().
3397  */
3398 int
3399 extattr_set_file(struct extattr_set_file_args *uap)
3400 {
3401         char attrname[EXTATTR_MAXNAMELEN];
3402         struct iovec aiov[UIO_SMALLIOV];
3403         struct iovec *needfree;
3404         struct nlookupdata nd;
3405         struct iovec *iov;
3406         struct vnode *vp;
3407         struct uio auio;
3408         u_int iovlen;
3409         u_int cnt;
3410         int error;
3411         int i;
3412
3413         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3414         if (error)
3415                 return (error);
3416
3417         vp = NULL;
3418         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3419         if (error == 0)
3420                 error = nlookup(&nd);
3421         if (error == 0)
3422                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3423         if (error) {
3424                 nlookup_done(&nd);
3425                 return (error);
3426         }
3427
3428         needfree = NULL;
3429         iovlen = uap->iovcnt * sizeof(struct iovec);
3430         if (uap->iovcnt > UIO_SMALLIOV) {
3431                 if (uap->iovcnt > UIO_MAXIOV) {
3432                         error = EINVAL;
3433                         goto done;
3434                 }
3435                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3436                 needfree = iov;
3437         } else {
3438                 iov = aiov;
3439         }
3440         auio.uio_iov = iov;
3441         auio.uio_iovcnt = uap->iovcnt;
3442         auio.uio_rw = UIO_WRITE;
3443         auio.uio_segflg = UIO_USERSPACE;
3444         auio.uio_td = nd.nl_td;
3445         auio.uio_offset = 0;
3446         if ((error = copyin(uap->iovp, iov, iovlen)))
3447                 goto done;
3448         auio.uio_resid = 0;
3449         for (i = 0; i < uap->iovcnt; i++) {
3450                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3451                         error = EINVAL;
3452                         goto done;
3453                 }
3454                 auio.uio_resid += iov->iov_len;
3455                 iov++;
3456         }
3457         cnt = auio.uio_resid;
3458         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3459         cnt -= auio.uio_resid;
3460         uap->sysmsg_result = cnt;
3461 done:
3462         vput(vp);
3463         nlookup_done(&nd);
3464         if (needfree)
3465                 FREE(needfree, M_IOV);
3466         return (error);
3467 }
3468
3469 /*
3470  * Syscall to get a named extended attribute on a file or directory.
3471  * Accepts attribute name, and a uio structure pointing to a buffer for the
3472  * data.  The uio is consumed in the style of readv().  The real work
3473  * happens in VOP_GETEXTATTR();
3474  */
3475 int
3476 extattr_get_file(struct extattr_get_file_args *uap)
3477 {
3478         char attrname[EXTATTR_MAXNAMELEN];
3479         struct iovec aiov[UIO_SMALLIOV];
3480         struct iovec *needfree;
3481         struct nlookupdata nd;
3482         struct iovec *iov;
3483         struct vnode *vp;
3484         struct uio auio;
3485         u_int iovlen;
3486         u_int cnt;
3487         int error;
3488         int i;
3489
3490         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3491         if (error)
3492                 return (error);
3493
3494         vp = NULL;
3495         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3496         if (error == 0)
3497                 error = nlookup(&nd);
3498         if (error == 0)
3499                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3500         if (error) {
3501                 nlookup_done(&nd);
3502                 return (error);
3503         }
3504
3505         iovlen = uap->iovcnt * sizeof (struct iovec);
3506         needfree = NULL;
3507         if (uap->iovcnt > UIO_SMALLIOV) {
3508                 if (uap->iovcnt > UIO_MAXIOV) {
3509                         error = EINVAL;
3510                         goto done;
3511                 }
3512                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3513                 needfree = iov;
3514         } else {
3515                 iov = aiov;
3516         }
3517         auio.uio_iov = iov;
3518         auio.uio_iovcnt = uap->iovcnt;
3519         auio.uio_rw = UIO_READ;
3520         auio.uio_segflg = UIO_USERSPACE;
3521         auio.uio_td = nd.nl_td;
3522         auio.uio_offset = 0;
3523         if ((error = copyin(uap->iovp, iov, iovlen)))
3524                 goto done;
3525         auio.uio_resid = 0;
3526         for (i = 0; i < uap->iovcnt; i++) {
3527                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3528                         error = EINVAL;
3529                         goto done;
3530                 }
3531                 auio.uio_resid += iov->iov_len;
3532                 iov++;
3533         }
3534         cnt = auio.uio_resid;
3535         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3536         cnt -= auio.uio_resid;
3537         uap->sysmsg_result = cnt;
3538 done:
3539         vput(vp);
3540         nlookup_done(&nd);
3541         if (needfree)
3542                 FREE(needfree, M_IOV);
3543         return(error);
3544 }
3545
3546 /*
3547  * Syscall to delete a named extended attribute from a file or directory.
3548  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3549  */
3550 int
3551 extattr_delete_file(struct extattr_delete_file_args *uap)
3552 {
3553         char attrname[EXTATTR_MAXNAMELEN];
3554         struct nlookupdata nd;
3555         struct vnode *vp;
3556         int error;
3557
3558         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3559         if (error)
3560                 return(error);
3561
3562         vp = NULL;
3563         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3564         if (error == 0)
3565                 error = nlookup(&nd);
3566         if (error == 0)
3567                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3568         if (error) {
3569                 nlookup_done(&nd);
3570                 return (error);
3571         }
3572
3573         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3574         vput(vp);
3575         nlookup_done(&nd);
3576         return(error);
3577 }
3578
3579 static int
3580 chroot_visible_mnt(struct mount *mp, struct proc *p)
3581 {
3582         struct namecache *ncp;
3583         /*
3584          * First check if this file system is below
3585          * the chroot path.
3586          */
3587         ncp = mp->mnt_ncp;
3588         while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3589                 ncp = ncp->nc_parent;
3590         if (ncp == NULL) {
3591                 /*
3592                  * This is not below the chroot path.
3593                  *
3594                  * Check if the chroot path is on the same filesystem,
3595                  * by determing if we have to cross a mount point
3596                  * before reaching mp->mnt_ncp.
3597                  */
3598                 ncp = p->p_fd->fd_nrdir;
3599                 while (ncp != NULL && ncp != mp->mnt_ncp) {
3600                         if (ncp->nc_flag & NCF_MOUNTPT) {
3601                                 ncp = NULL;
3602                                 break;
3603                         }
3604                         ncp = ncp->nc_parent;
3605                 }
3606         }
3607         return(ncp != NULL);
3608 }