Merge branch 'vendor/OPENSSL'
[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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)vfs_syscalls.c      8.13 (Berkeley) 4/15/94
35  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/linker.h>
52 #include <sys/stat.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/proc.h>
56 #include <sys/priv.h>
57 #include <sys/jail.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
66
67 #include <sys/buf2.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
70 #include <sys/mplock2.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
78
79 static void mount_warning(struct mount *mp, const char *ctl, ...)
80                 __printflike(2, 3);
81 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
82 static int checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
84 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (struct timeval *, struct timespec *);
87 static int getutimens (const struct timespec *, struct timespec *, int *);
88 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, int);
91 static int setutimes (struct vnode *, struct vattr *,
92                         const struct timespec *, int);
93 static int      usermount = 0;  /* if 1, non-root can mount fs. */
94
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
96     "Allow non-root users to mount filesystems");
97
98 /*
99  * Virtual File System System Calls
100  */
101
102 /*
103  * Mount a file system.
104  *
105  * mount_args(char *type, char *path, int flags, caddr_t data)
106  *
107  * MPALMOSTSAFE
108  */
109 int
110 sys_mount(struct mount_args *uap)
111 {
112         struct thread *td = curthread;
113         struct vnode *vp;
114         struct nchandle nch;
115         struct mount *mp, *nullmp;
116         struct vfsconf *vfsp;
117         int error, flag = 0, flag2 = 0;
118         int hasmount;
119         struct vattr va;
120         struct nlookupdata nd;
121         char fstypename[MFSNAMELEN];
122         struct ucred *cred;
123
124         cred = td->td_ucred;
125         if (jailed(cred)) {
126                 error = EPERM;
127                 goto done;
128         }
129         if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
130                 goto done;
131
132         /*
133          * Do not allow NFS export by non-root users.
134          */
135         if (uap->flags & MNT_EXPORTED) {
136                 error = priv_check(td, PRIV_ROOT);
137                 if (error)
138                         goto done;
139         }
140         /*
141          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142          */
143         if (priv_check(td, PRIV_ROOT)) 
144                 uap->flags |= MNT_NOSUID | MNT_NODEV;
145
146         /*
147          * Lookup the requested path and extract the nch and vnode.
148          */
149         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
150         if (error == 0) {
151                 if ((error = nlookup(&nd)) == 0) {
152                         if (nd.nl_nch.ncp->nc_vp == NULL)
153                                 error = ENOENT;
154                 }
155         }
156         if (error) {
157                 nlookup_done(&nd);
158                 goto done;
159         }
160
161         /*
162          * If the target filesystem is resolved via a nullfs mount, then
163          * nd.nl_nch.mount will be pointing to the nullfs mount structure
164          * instead of the target file system. We need it in case we are
165          * doing an update.
166          */
167         nullmp = nd.nl_nch.mount;
168
169         /*
170          * Extract the locked+refd ncp and cleanup the nd structure
171          */
172         nch = nd.nl_nch;
173         cache_zero(&nd.nl_nch);
174         nlookup_done(&nd);
175
176         if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
177             (mp = cache_findmount(&nch)) != NULL) {
178                 cache_dropmount(mp);
179                 hasmount = 1;
180         } else {
181                 hasmount = 0;
182         }
183
184
185         /*
186          * now we have the locked ref'd nch and unreferenced vnode.
187          */
188         vp = nch.ncp->nc_vp;
189         if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
190                 cache_put(&nch);
191                 goto done;
192         }
193         cache_unlock(&nch);
194
195         /*
196          * Extract the file system type. We need to know this early, to take
197          * appropriate actions if we are dealing with a nullfs.
198          */
199         if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
200                 cache_drop(&nch);
201                 vput(vp);
202                 goto done;
203         }
204
205         /*
206          * Now we have an unlocked ref'd nch and a locked ref'd vp
207          */
208         if (uap->flags & MNT_UPDATE) {
209                 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
210                         cache_drop(&nch);
211                         vput(vp);
212                         error = EINVAL;
213                         goto done;
214                 }
215
216                 if (strncmp(fstypename, "null", 5) == 0) {
217                         KKASSERT(nullmp);
218                         mp = nullmp;
219                 } else {
220                         mp = vp->v_mount;
221                 }
222
223                 flag = mp->mnt_flag;
224                 flag2 = mp->mnt_kern_flag;
225                 /*
226                  * We only allow the filesystem to be reloaded if it
227                  * is currently mounted read-only.
228                  */
229                 if ((uap->flags & MNT_RELOAD) &&
230                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
231                         cache_drop(&nch);
232                         vput(vp);
233                         error = EOPNOTSUPP;     /* Needs translation */
234                         goto done;
235                 }
236                 /*
237                  * Only root, or the user that did the original mount is
238                  * permitted to update it.
239                  */
240                 if (mp->mnt_stat.f_owner != cred->cr_uid &&
241                     (error = priv_check(td, PRIV_ROOT))) {
242                         cache_drop(&nch);
243                         vput(vp);
244                         goto done;
245                 }
246                 if (vfs_busy(mp, LK_NOWAIT)) {
247                         cache_drop(&nch);
248                         vput(vp);
249                         error = EBUSY;
250                         goto done;
251                 }
252                 if (hasmount) {
253                         cache_drop(&nch);
254                         vfs_unbusy(mp);
255                         vput(vp);
256                         error = EBUSY;
257                         goto done;
258                 }
259                 mp->mnt_flag |=
260                     uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
261                 lwkt_gettoken(&mp->mnt_token);
262                 vn_unlock(vp);
263                 goto update;
264         }
265
266         /*
267          * If the user is not root, ensure that they own the directory
268          * onto which we are attempting to mount.
269          */
270         if ((error = VOP_GETATTR(vp, &va)) ||
271             (va.va_uid != cred->cr_uid &&
272              (error = priv_check(td, PRIV_ROOT)))) {
273                 cache_drop(&nch);
274                 vput(vp);
275                 goto done;
276         }
277         if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
278                 cache_drop(&nch);
279                 vput(vp);
280                 goto done;
281         }
282         if (vp->v_type != VDIR) {
283                 cache_drop(&nch);
284                 vput(vp);
285                 error = ENOTDIR;
286                 goto done;
287         }
288         if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
289                 cache_drop(&nch);
290                 vput(vp);
291                 error = EPERM;
292                 goto done;
293         }
294         vfsp = vfsconf_find_by_name(fstypename);
295         if (vfsp == NULL) {
296                 linker_file_t lf;
297
298                 /* Only load modules for root (very important!) */
299                 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
300                         cache_drop(&nch);
301                         vput(vp);
302                         goto done;
303                 }
304                 error = linker_load_file(fstypename, &lf);
305                 if (error || lf == NULL) {
306                         cache_drop(&nch);
307                         vput(vp);
308                         if (lf == NULL)
309                                 error = ENODEV;
310                         goto done;
311                 }
312                 lf->userrefs++;
313                 /* lookup again, see if the VFS was loaded */
314                 vfsp = vfsconf_find_by_name(fstypename);
315                 if (vfsp == NULL) {
316                         lf->userrefs--;
317                         linker_file_unload(lf);
318                         cache_drop(&nch);
319                         vput(vp);
320                         error = ENODEV;
321                         goto done;
322                 }
323         }
324         if (hasmount) {
325                 cache_drop(&nch);
326                 vput(vp);
327                 error = EBUSY;
328                 goto done;
329         }
330
331         /*
332          * Allocate and initialize the filesystem.
333          */
334         mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
335         mount_init(mp);
336         vfs_busy(mp, LK_NOWAIT);
337         mp->mnt_op = vfsp->vfc_vfsops;
338         mp->mnt_vfc = vfsp;
339         vfsp->vfc_refcount++;
340         mp->mnt_stat.f_type = vfsp->vfc_typenum;
341         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
342         strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
343         mp->mnt_stat.f_owner = cred->cr_uid;
344         lwkt_gettoken(&mp->mnt_token);
345         vn_unlock(vp);
346 update:
347         /*
348          * (per-mount token acquired at this point)
349          *
350          * Set the mount level flags.
351          */
352         if (uap->flags & MNT_RDONLY)
353                 mp->mnt_flag |= MNT_RDONLY;
354         else if (mp->mnt_flag & MNT_RDONLY)
355                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
356         mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
357             MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
358             MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
359             MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
360         mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
361             MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
362             MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363             MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364         /*
365          * Mount the filesystem.
366          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
367          * get. 
368          */
369         error = VFS_MOUNT(mp, uap->path, uap->data, cred);
370         if (mp->mnt_flag & MNT_UPDATE) {
371                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
372                         mp->mnt_flag &= ~MNT_RDONLY;
373                 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
374                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
375                 if (error) {
376                         mp->mnt_flag = flag;
377                         mp->mnt_kern_flag = flag2;
378                 }
379                 lwkt_reltoken(&mp->mnt_token);
380                 vfs_unbusy(mp);
381                 vrele(vp);
382                 cache_drop(&nch);
383                 goto done;
384         }
385         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
386
387         /*
388          * Put the new filesystem on the mount list after root.  The mount
389          * point gets its own mnt_ncmountpt (unless the VFS already set one
390          * up) which represents the root of the mount.  The lookup code
391          * detects the mount point going forward and checks the root of
392          * the mount going backwards.
393          *
394          * It is not necessary to invalidate or purge the vnode underneath
395          * because elements under the mount will be given their own glue
396          * namecache record.
397          */
398         if (!error) {
399                 if (mp->mnt_ncmountpt.ncp == NULL) {
400                         /* 
401                          * allocate, then unlock, but leave the ref intact 
402                          */
403                         cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
404                         cache_unlock(&mp->mnt_ncmountpt);
405                 }
406                 mp->mnt_ncmounton = nch;                /* inherits ref */
407                 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
408                 cache_ismounting(mp);
409
410                 mountlist_insert(mp, MNTINS_LAST);
411                 vn_unlock(vp);
412                 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
413                 error = vfs_allocate_syncvnode(mp);
414                 lwkt_reltoken(&mp->mnt_token);
415                 vfs_unbusy(mp);
416                 error = VFS_START(mp, 0);
417                 vrele(vp);
418         } else {
419                 vn_syncer_thr_stop(mp);
420                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
421                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
422                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
423                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
424                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
425                 mp->mnt_vfc->vfc_refcount--;
426                 lwkt_reltoken(&mp->mnt_token);
427                 vfs_unbusy(mp);
428                 kfree(mp, M_MOUNT);
429                 cache_drop(&nch);
430                 vput(vp);
431         }
432 done:
433         return (error);
434 }
435
436 /*
437  * Scan all active processes to see if any of them have a current
438  * or root directory onto which the new filesystem has just been
439  * mounted. If so, replace them with the new mount point.
440  *
441  * Both old_nch and new_nch are ref'd on call but not locked.
442  * new_nch must be temporarily locked so it can be associated with the
443  * vnode representing the root of the mount point.
444  */
445 struct checkdirs_info {
446         struct nchandle old_nch;
447         struct nchandle new_nch;
448         struct vnode *old_vp;
449         struct vnode *new_vp;
450 };
451
452 static int checkdirs_callback(struct proc *p, void *data);
453
454 static void
455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
456 {
457         struct checkdirs_info info;
458         struct vnode *olddp;
459         struct vnode *newdp;
460         struct mount *mp;
461
462         /*
463          * If the old mount point's vnode has a usecount of 1, it is not
464          * being held as a descriptor anywhere.
465          */
466         olddp = old_nch->ncp->nc_vp;
467         if (olddp == NULL || VREFCNT(olddp) == 1)
468                 return;
469
470         /*
471          * Force the root vnode of the new mount point to be resolved
472          * so we can update any matching processes.
473          */
474         mp = new_nch->mount;
475         if (VFS_ROOT(mp, &newdp))
476                 panic("mount: lost mount");
477         vn_unlock(newdp);
478         cache_lock(new_nch);
479         vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
480         cache_setunresolved(new_nch);
481         cache_setvp(new_nch, newdp);
482         cache_unlock(new_nch);
483
484         /*
485          * Special handling of the root node
486          */
487         if (rootvnode == olddp) {
488                 vref(newdp);
489                 vfs_cache_setroot(newdp, cache_hold(new_nch));
490         }
491
492         /*
493          * Pass newdp separately so the callback does not have to access
494          * it via new_nch->ncp->nc_vp.
495          */
496         info.old_nch = *old_nch;
497         info.new_nch = *new_nch;
498         info.new_vp = newdp;
499         allproc_scan(checkdirs_callback, &info);
500         vput(newdp);
501 }
502
503 /*
504  * NOTE: callback is not MP safe because the scanned process's filedesc
505  * structure can be ripped out from under us, amoung other things.
506  */
507 static int
508 checkdirs_callback(struct proc *p, void *data)
509 {
510         struct checkdirs_info *info = data;
511         struct filedesc *fdp;
512         struct nchandle ncdrop1;
513         struct nchandle ncdrop2;
514         struct vnode *vprele1;
515         struct vnode *vprele2;
516
517         if ((fdp = p->p_fd) != NULL) {
518                 cache_zero(&ncdrop1);
519                 cache_zero(&ncdrop2);
520                 vprele1 = NULL;
521                 vprele2 = NULL;
522
523                 /*
524                  * MPUNSAFE - XXX fdp can be pulled out from under a
525                  * foreign process.
526                  *
527                  * A shared filedesc is ok, we don't have to copy it
528                  * because we are making this change globally.
529                  */
530                 spin_lock(&fdp->fd_spin);
531                 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
532                     fdp->fd_ncdir.ncp == info->old_nch.ncp) {
533                         vprele1 = fdp->fd_cdir;
534                         vref(info->new_vp);
535                         fdp->fd_cdir = info->new_vp;
536                         ncdrop1 = fdp->fd_ncdir;
537                         cache_copy(&info->new_nch, &fdp->fd_ncdir);
538                 }
539                 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
540                     fdp->fd_nrdir.ncp == info->old_nch.ncp) {
541                         vprele2 = fdp->fd_rdir;
542                         vref(info->new_vp);
543                         fdp->fd_rdir = info->new_vp;
544                         ncdrop2 = fdp->fd_nrdir;
545                         cache_copy(&info->new_nch, &fdp->fd_nrdir);
546                 }
547                 spin_unlock(&fdp->fd_spin);
548                 if (ncdrop1.ncp)
549                         cache_drop(&ncdrop1);
550                 if (ncdrop2.ncp)
551                         cache_drop(&ncdrop2);
552                 if (vprele1)
553                         vrele(vprele1);
554                 if (vprele2)
555                         vrele(vprele2);
556         }
557         return(0);
558 }
559
560 /*
561  * Unmount a file system.
562  *
563  * Note: unmount takes a path to the vnode mounted on as argument,
564  * not special file (as before).
565  *
566  * umount_args(char *path, int flags)
567  *
568  * MPALMOSTSAFE
569  */
570 int
571 sys_unmount(struct unmount_args *uap)
572 {
573         struct thread *td = curthread;
574         struct proc *p __debugvar = td->td_proc;
575         struct mount *mp = NULL;
576         struct nlookupdata nd;
577         int error;
578
579         KKASSERT(p);
580         get_mplock();
581         if (td->td_ucred->cr_prison != NULL) {
582                 error = EPERM;
583                 goto done;
584         }
585         if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
586                 goto done;
587
588         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
589         if (error == 0)
590                 error = nlookup(&nd);
591         if (error)
592                 goto out;
593
594         mp = nd.nl_nch.mount;
595
596         /*
597          * Only root, or the user that did the original mount is
598          * permitted to unmount this filesystem.
599          */
600         if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
601             (error = priv_check(td, PRIV_ROOT)))
602                 goto out;
603
604         /*
605          * Don't allow unmounting the root file system.
606          */
607         if (mp->mnt_flag & MNT_ROOTFS) {
608                 error = EINVAL;
609                 goto out;
610         }
611
612         /*
613          * Must be the root of the filesystem
614          */
615         if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
616                 error = EINVAL;
617                 goto out;
618         }
619
620 out:
621         nlookup_done(&nd);
622         if (error == 0)
623                 error = dounmount(mp, uap->flags);
624 done:
625         rel_mplock();
626         return (error);
627 }
628
629 /*
630  * Do the actual file system unmount.
631  */
632 static int
633 dounmount_interlock(struct mount *mp)
634 {
635         if (mp->mnt_kern_flag & MNTK_UNMOUNT)
636                 return (EBUSY);
637         mp->mnt_kern_flag |= MNTK_UNMOUNT;
638         return(0);
639 }
640
641 static int
642 unmount_allproc_cb(struct proc *p, void *arg)
643 {
644         struct mount *mp;
645
646         if (p->p_textnch.ncp == NULL)
647                 return 0;
648
649         mp = (struct mount *)arg;
650         if (p->p_textnch.mount == mp)
651                 cache_drop(&p->p_textnch);
652
653         return 0;
654 }
655
656 int
657 dounmount(struct mount *mp, int flags)
658 {
659         struct namecache *ncp;
660         struct nchandle nch;
661         struct vnode *vp;
662         int error;
663         int async_flag;
664         int lflags;
665         int freeok = 1;
666         int retry;
667
668         lwkt_gettoken(&mp->mnt_token);
669         /*
670          * Exclusive access for unmounting purposes
671          */
672         if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
673                 goto out;
674
675         /*
676          * Allow filesystems to detect that a forced unmount is in progress.
677          */
678         if (flags & MNT_FORCE)
679                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
680         lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
681         error = lockmgr(&mp->mnt_lock, lflags);
682         if (error) {
683                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
684                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
685                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
686                         wakeup(mp);
687                 }
688                 goto out;
689         }
690
691         if (mp->mnt_flag & MNT_EXPUBLIC)
692                 vfs_setpublicfs(NULL, NULL, NULL);
693
694         vfs_msync(mp, MNT_WAIT);
695         async_flag = mp->mnt_flag & MNT_ASYNC;
696         mp->mnt_flag &=~ MNT_ASYNC;
697
698         /*
699          * If this filesystem isn't aliasing other filesystems,
700          * try to invalidate any remaining namecache entries and
701          * check the count afterwords.
702          */
703         if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
704                 cache_lock(&mp->mnt_ncmountpt);
705                 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
706                 cache_unlock(&mp->mnt_ncmountpt);
707
708                 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
709                     (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
710                         allproc_scan(&unmount_allproc_cb, mp);
711                 }
712
713                 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
714                     (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
715
716                         if ((flags & MNT_FORCE) == 0) {
717                                 error = EBUSY;
718                                 mount_warning(mp, "Cannot unmount: "
719                                                   "%d namecache "
720                                                   "references still "
721                                                   "present",
722                                                   ncp->nc_refs - 1);
723                         } else {
724                                 mount_warning(mp, "Forced unmount: "
725                                                   "%d namecache "
726                                                   "references still "
727                                                   "present",
728                                                   ncp->nc_refs - 1);
729                                 freeok = 0;
730                         }
731                 }
732         }
733
734         /*
735          * Decomission our special mnt_syncer vnode.  This also stops
736          * the vnlru code.  If we are unable to unmount we recommission
737          * the vnode.
738          *
739          * Then sync the filesystem.
740          */
741         if ((vp = mp->mnt_syncer) != NULL) {
742                 mp->mnt_syncer = NULL;
743                 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
744                 vrele(vp);
745         }
746         if ((mp->mnt_flag & MNT_RDONLY) == 0)
747                 VFS_SYNC(mp, MNT_WAIT);
748
749         /*
750          * nchandle records ref the mount structure.  Expect a count of 1
751          * (our mount->mnt_ncmountpt).
752          *
753          * Scans can get temporary refs on a mountpoint (thought really
754          * heavy duty stuff like cache_findmount() do not).
755          */
756         for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
757                 cache_unmounting(mp);
758                 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
759         }
760         if (mp->mnt_refs != 1) {
761                 if ((flags & MNT_FORCE) == 0) {
762                         mount_warning(mp, "Cannot unmount: "
763                                           "%d mount refs still present",
764                                           mp->mnt_refs);
765                         error = EBUSY;
766                 } else {
767                         mount_warning(mp, "Forced unmount: "
768                                           "%d mount refs still present",
769                                           mp->mnt_refs);
770                         freeok = 0;
771                 }
772         }
773
774         /*
775          * So far so good, sync the filesystem once more and
776          * call the VFS unmount code if the sync succeeds.
777          *
778          * A filesystem without explicitly initialzed .vfs_sync
779          * fails here since vfs_stdnosync() returns EOPNOTSUPP.
780          * A filesystem needs to implement vfs_stdsync() which
781          * returns 0 even if there is no backing storage.
782          */
783         if (error == 0) {
784                 if (((mp->mnt_flag & MNT_RDONLY) ||
785                      (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
786                     (flags & MNT_FORCE)) {
787                         error = VFS_UNMOUNT(mp, flags);
788                 }
789         }
790
791         /*
792          * If an error occurred we can still recover, restoring the
793          * syncer vnode and misc flags.
794          */
795         if (error) {
796                 if (mp->mnt_syncer == NULL)
797                         vfs_allocate_syncvnode(mp);
798                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
799                 mp->mnt_flag |= async_flag;
800                 lockmgr(&mp->mnt_lock, LK_RELEASE);
801                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
802                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
803                         wakeup(mp);
804                 }
805                 goto out;
806         }
807         /*
808          * Clean up any journals still associated with the mount after
809          * filesystem activity has ceased.
810          */
811         journal_remove_all_journals(mp, 
812             ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
813
814         mountlist_remove(mp);
815
816         /*
817          * Remove any installed vnode ops here so the individual VFSs don't
818          * have to.
819          */
820         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
821         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
822         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
823         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
824         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
825
826         if (mp->mnt_ncmountpt.ncp != NULL) {
827                 nch = mp->mnt_ncmountpt;
828                 cache_zero(&mp->mnt_ncmountpt);
829                 cache_clrmountpt(&nch);
830                 cache_drop(&nch);
831         }
832         if (mp->mnt_ncmounton.ncp != NULL) {
833                 cache_unmounting(mp);
834                 nch = mp->mnt_ncmounton;
835                 cache_zero(&mp->mnt_ncmounton);
836                 cache_clrmountpt(&nch);
837                 cache_drop(&nch);
838         }
839
840         mp->mnt_vfc->vfc_refcount--;
841         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
842                 panic("unmount: dangling vnode");
843         lockmgr(&mp->mnt_lock, LK_RELEASE);
844         if (mp->mnt_kern_flag & MNTK_MWAIT) {
845                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
846                 wakeup(mp);
847         }
848
849         /*
850          * If we reach here and freeok != 0 we must free the mount.
851          * If refs > 1 cycle and wait, just in case someone tried
852          * to busy the mount after we decided to do the unmount.
853          */
854         if (freeok) {
855                 while (mp->mnt_refs > 1) {
856                         cache_unmounting(mp);
857                         wakeup(mp);
858                         tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
859                 }
860                 lwkt_reltoken(&mp->mnt_token);
861                 kfree(mp, M_MOUNT);
862                 mp = NULL;
863         }
864         error = 0;
865 out:
866         if (mp)
867                 lwkt_reltoken(&mp->mnt_token);
868         return (error);
869 }
870
871 static
872 void
873 mount_warning(struct mount *mp, const char *ctl, ...)
874 {
875         char *ptr;
876         char *buf;
877         __va_list va;
878
879         __va_start(va, ctl);
880         if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
881                            &ptr, &buf, 0) == 0) {
882                 kprintf("unmount(%s): ", ptr);
883                 kvprintf(ctl, va);
884                 kprintf("\n");
885                 kfree(buf, M_TEMP);
886         } else {
887                 kprintf("unmount(%p", mp);
888                 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
889                         kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
890                 kprintf("): ");
891                 kvprintf(ctl, va);
892                 kprintf("\n");
893         }
894         __va_end(va);
895 }
896
897 /*
898  * Shim cache_fullpath() to handle the case where a process is chrooted into
899  * a subdirectory of a mount.  In this case if the root mount matches the
900  * process root directory's mount we have to specify the process's root
901  * directory instead of the mount point, because the mount point might
902  * be above the root directory.
903  */
904 static
905 int
906 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
907 {
908         struct nchandle *nch;
909
910         if (p && p->p_fd->fd_nrdir.mount == mp)
911                 nch = &p->p_fd->fd_nrdir;
912         else
913                 nch = &mp->mnt_ncmountpt;
914         return(cache_fullpath(p, nch, NULL, rb, fb, 0));
915 }
916
917 /*
918  * Sync each mounted filesystem.
919  */
920
921 #ifdef DEBUG
922 static int syncprt = 0;
923 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
924 #endif /* DEBUG */
925
926 static int sync_callback(struct mount *mp, void *data);
927
928 int
929 sys_sync(struct sync_args *uap)
930 {
931         mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
932         return (0);
933 }
934
935 static
936 int
937 sync_callback(struct mount *mp, void *data __unused)
938 {
939         int asyncflag;
940
941         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
942                 asyncflag = mp->mnt_flag & MNT_ASYNC;
943                 mp->mnt_flag &= ~MNT_ASYNC;
944                 vfs_msync(mp, MNT_NOWAIT);
945                 VFS_SYNC(mp, MNT_NOWAIT);
946                 mp->mnt_flag |= asyncflag;
947         }
948         return(0);
949 }
950
951 /* XXX PRISON: could be per prison flag */
952 static int prison_quotas;
953 #if 0
954 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
955 #endif
956
957 /*
958  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
959  *
960  * Change filesystem quotas.
961  *
962  * MPALMOSTSAFE
963  */
964 int
965 sys_quotactl(struct quotactl_args *uap)
966 {
967         struct nlookupdata nd;
968         struct thread *td;
969         struct mount *mp;
970         int error;
971
972         get_mplock();
973         td = curthread;
974         if (td->td_ucred->cr_prison && !prison_quotas) {
975                 error = EPERM;
976                 goto done;
977         }
978
979         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
980         if (error == 0)
981                 error = nlookup(&nd);
982         if (error == 0) {
983                 mp = nd.nl_nch.mount;
984                 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
985                                     uap->arg, nd.nl_cred);
986         }
987         nlookup_done(&nd);
988 done:
989         rel_mplock();
990         return (error);
991 }
992
993 /*
994  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
995  *              void *buf, int buflen)
996  *
997  * This function operates on a mount point and executes the specified
998  * operation using the specified control data, and possibly returns data.
999  *
1000  * The actual number of bytes stored in the result buffer is returned, 0
1001  * if none, otherwise an error is returned.
1002  *
1003  * MPALMOSTSAFE
1004  */
1005 int
1006 sys_mountctl(struct mountctl_args *uap)
1007 {
1008         struct thread *td = curthread;
1009         struct proc *p = td->td_proc;
1010         struct file *fp;
1011         void *ctl = NULL;
1012         void *buf = NULL;
1013         char *path = NULL;
1014         int error;
1015
1016         /*
1017          * Sanity and permissions checks.  We must be root.
1018          */
1019         KKASSERT(p);
1020         if (td->td_ucred->cr_prison != NULL)
1021                 return (EPERM);
1022         if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1023             (error = priv_check(td, PRIV_ROOT)) != 0)
1024                 return (error);
1025
1026         /*
1027          * Argument length checks
1028          */
1029         if (uap->ctllen < 0 || uap->ctllen > 1024)
1030                 return (EINVAL);
1031         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1032                 return (EINVAL);
1033         if (uap->path == NULL)
1034                 return (EINVAL);
1035
1036         /*
1037          * Allocate the necessary buffers and copyin data
1038          */
1039         path = objcache_get(namei_oc, M_WAITOK);
1040         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1041         if (error)
1042                 goto done;
1043
1044         if (uap->ctllen) {
1045                 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1046                 error = copyin(uap->ctl, ctl, uap->ctllen);
1047                 if (error)
1048                         goto done;
1049         }
1050         if (uap->buflen)
1051                 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1052
1053         /*
1054          * Validate the descriptor
1055          */
1056         if (uap->fd >= 0) {
1057                 fp = holdfp(p->p_fd, uap->fd, -1);
1058                 if (fp == NULL) {
1059                         error = EBADF;
1060                         goto done;
1061                 }
1062         } else {
1063                 fp = NULL;
1064         }
1065
1066         /*
1067          * Execute the internal kernel function and clean up.
1068          */
1069         get_mplock();
1070         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1071         rel_mplock();
1072         if (fp)
1073                 fdrop(fp);
1074         if (error == 0 && uap->sysmsg_result > 0)
1075                 error = copyout(buf, uap->buf, uap->sysmsg_result);
1076 done:
1077         if (path)
1078                 objcache_put(namei_oc, path);
1079         if (ctl)
1080                 kfree(ctl, M_TEMP);
1081         if (buf)
1082                 kfree(buf, M_TEMP);
1083         return (error);
1084 }
1085
1086 /*
1087  * Execute a mount control operation by resolving the path to a mount point
1088  * and calling vop_mountctl().  
1089  *
1090  * Use the mount point from the nch instead of the vnode so nullfs mounts
1091  * can properly spike the VOP.
1092  */
1093 int
1094 kern_mountctl(const char *path, int op, struct file *fp, 
1095                 const void *ctl, int ctllen, 
1096                 void *buf, int buflen, int *res)
1097 {
1098         struct vnode *vp;
1099         struct mount *mp;
1100         struct nlookupdata nd;
1101         int error;
1102
1103         *res = 0;
1104         vp = NULL;
1105         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1106         if (error == 0)
1107                 error = nlookup(&nd);
1108         if (error == 0)
1109                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1110         mp = nd.nl_nch.mount;
1111         nlookup_done(&nd);
1112         if (error)
1113                 return (error);
1114         vn_unlock(vp);
1115
1116         /*
1117          * Must be the root of the filesystem
1118          */
1119         if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1120                 vrele(vp);
1121                 return (EINVAL);
1122         }
1123         error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1124                              buf, buflen, res);
1125         vrele(vp);
1126         return (error);
1127 }
1128
1129 int
1130 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1131 {
1132         struct thread *td = curthread;
1133         struct proc *p = td->td_proc;
1134         struct mount *mp;
1135         struct statfs *sp;
1136         char *fullpath, *freepath;
1137         int error;
1138
1139         if ((error = nlookup(nd)) != 0)
1140                 return (error);
1141         mp = nd->nl_nch.mount;
1142         sp = &mp->mnt_stat;
1143         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1144                 return (error);
1145
1146         error = mount_path(p, mp, &fullpath, &freepath);
1147         if (error)
1148                 return(error);
1149         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1150         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1151         kfree(freepath, M_TEMP);
1152
1153         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1154         bcopy(sp, buf, sizeof(*buf));
1155         /* Only root should have access to the fsid's. */
1156         if (priv_check(td, PRIV_ROOT))
1157                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1158         return (0);
1159 }
1160
1161 /*
1162  * statfs_args(char *path, struct statfs *buf)
1163  *
1164  * Get filesystem statistics.
1165  */
1166 int
1167 sys_statfs(struct statfs_args *uap)
1168 {
1169         struct nlookupdata nd;
1170         struct statfs buf;
1171         int error;
1172
1173         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1174         if (error == 0)
1175                 error = kern_statfs(&nd, &buf);
1176         nlookup_done(&nd);
1177         if (error == 0)
1178                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1179         return (error);
1180 }
1181
1182 int
1183 kern_fstatfs(int fd, struct statfs *buf)
1184 {
1185         struct thread *td = curthread;
1186         struct proc *p = td->td_proc;
1187         struct file *fp;
1188         struct mount *mp;
1189         struct statfs *sp;
1190         char *fullpath, *freepath;
1191         int error;
1192
1193         KKASSERT(p);
1194         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1195                 return (error);
1196
1197         /*
1198          * Try to use mount info from any overlays rather than the
1199          * mount info for the underlying vnode, otherwise we will
1200          * fail when operating on null-mounted paths inside a chroot.
1201          */
1202         if ((mp = fp->f_nchandle.mount) == NULL)
1203                 mp = ((struct vnode *)fp->f_data)->v_mount;
1204         if (mp == NULL) {
1205                 error = EBADF;
1206                 goto done;
1207         }
1208         if (fp->f_cred == NULL) {
1209                 error = EINVAL;
1210                 goto done;
1211         }
1212         sp = &mp->mnt_stat;
1213         if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1214                 goto done;
1215
1216         if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1217                 goto done;
1218         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1219         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1220         kfree(freepath, M_TEMP);
1221
1222         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1223         bcopy(sp, buf, sizeof(*buf));
1224
1225         /* Only root should have access to the fsid's. */
1226         if (priv_check(td, PRIV_ROOT))
1227                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1228         error = 0;
1229 done:
1230         fdrop(fp);
1231         return (error);
1232 }
1233
1234 /*
1235  * fstatfs_args(int fd, struct statfs *buf)
1236  *
1237  * Get filesystem statistics.
1238  */
1239 int
1240 sys_fstatfs(struct fstatfs_args *uap)
1241 {
1242         struct statfs buf;
1243         int error;
1244
1245         error = kern_fstatfs(uap->fd, &buf);
1246
1247         if (error == 0)
1248                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1249         return (error);
1250 }
1251
1252 int
1253 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1254 {
1255         struct mount *mp;
1256         struct statvfs *sp;
1257         int error;
1258
1259         if ((error = nlookup(nd)) != 0)
1260                 return (error);
1261         mp = nd->nl_nch.mount;
1262         sp = &mp->mnt_vstat;
1263         if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1264                 return (error);
1265
1266         sp->f_flag = 0;
1267         if (mp->mnt_flag & MNT_RDONLY)
1268                 sp->f_flag |= ST_RDONLY;
1269         if (mp->mnt_flag & MNT_NOSUID)
1270                 sp->f_flag |= ST_NOSUID;
1271         bcopy(sp, buf, sizeof(*buf));
1272         return (0);
1273 }
1274
1275 /*
1276  * statfs_args(char *path, struct statfs *buf)
1277  *
1278  * Get filesystem statistics.
1279  */
1280 int
1281 sys_statvfs(struct statvfs_args *uap)
1282 {
1283         struct nlookupdata nd;
1284         struct statvfs buf;
1285         int error;
1286
1287         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1288         if (error == 0)
1289                 error = kern_statvfs(&nd, &buf);
1290         nlookup_done(&nd);
1291         if (error == 0)
1292                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1293         return (error);
1294 }
1295
1296 int
1297 kern_fstatvfs(int fd, struct statvfs *buf)
1298 {
1299         struct thread *td = curthread;
1300         struct proc *p = td->td_proc;
1301         struct file *fp;
1302         struct mount *mp;
1303         struct statvfs *sp;
1304         int error;
1305
1306         KKASSERT(p);
1307         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1308                 return (error);
1309         if ((mp = fp->f_nchandle.mount) == NULL)
1310                 mp = ((struct vnode *)fp->f_data)->v_mount;
1311         if (mp == NULL) {
1312                 error = EBADF;
1313                 goto done;
1314         }
1315         if (fp->f_cred == NULL) {
1316                 error = EINVAL;
1317                 goto done;
1318         }
1319         sp = &mp->mnt_vstat;
1320         if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1321                 goto done;
1322
1323         sp->f_flag = 0;
1324         if (mp->mnt_flag & MNT_RDONLY)
1325                 sp->f_flag |= ST_RDONLY;
1326         if (mp->mnt_flag & MNT_NOSUID)
1327                 sp->f_flag |= ST_NOSUID;
1328
1329         bcopy(sp, buf, sizeof(*buf));
1330         error = 0;
1331 done:
1332         fdrop(fp);
1333         return (error);
1334 }
1335
1336 /*
1337  * fstatfs_args(int fd, struct statfs *buf)
1338  *
1339  * Get filesystem statistics.
1340  */
1341 int
1342 sys_fstatvfs(struct fstatvfs_args *uap)
1343 {
1344         struct statvfs buf;
1345         int error;
1346
1347         error = kern_fstatvfs(uap->fd, &buf);
1348
1349         if (error == 0)
1350                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1351         return (error);
1352 }
1353
1354 /*
1355  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1356  *
1357  * Get statistics on all filesystems.
1358  */
1359
1360 struct getfsstat_info {
1361         struct statfs *sfsp;
1362         long count;
1363         long maxcount;
1364         int error;
1365         int flags;
1366         struct thread *td;
1367 };
1368
1369 static int getfsstat_callback(struct mount *, void *);
1370
1371 int
1372 sys_getfsstat(struct getfsstat_args *uap)
1373 {
1374         struct thread *td = curthread;
1375         struct getfsstat_info info;
1376
1377         bzero(&info, sizeof(info));
1378
1379         info.maxcount = uap->bufsize / sizeof(struct statfs);
1380         info.sfsp = uap->buf;
1381         info.count = 0;
1382         info.flags = uap->flags;
1383         info.td = td;
1384
1385         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1386         if (info.sfsp && info.count > info.maxcount)
1387                 uap->sysmsg_result = info.maxcount;
1388         else
1389                 uap->sysmsg_result = info.count;
1390         return (info.error);
1391 }
1392
1393 static int
1394 getfsstat_callback(struct mount *mp, void *data)
1395 {
1396         struct getfsstat_info *info = data;
1397         struct statfs *sp;
1398         char *freepath;
1399         char *fullpath;
1400         int error;
1401
1402         if (info->sfsp && info->count < info->maxcount) {
1403                 if (info->td->td_proc &&
1404                     !chroot_visible_mnt(mp, info->td->td_proc)) {
1405                         return(0);
1406                 }
1407                 sp = &mp->mnt_stat;
1408
1409                 /*
1410                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1411                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1412                  * overrides MNT_WAIT.
1413                  */
1414                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1415                     (info->flags & MNT_WAIT)) &&
1416                     (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1417                         return(0);
1418                 }
1419                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1420
1421                 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1422                 if (error) {
1423                         info->error = error;
1424                         return(-1);
1425                 }
1426                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1427                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1428                 kfree(freepath, M_TEMP);
1429
1430                 error = copyout(sp, info->sfsp, sizeof(*sp));
1431                 if (error) {
1432                         info->error = error;
1433                         return (-1);
1434                 }
1435                 ++info->sfsp;
1436         }
1437         info->count++;
1438         return(0);
1439 }
1440
1441 /*
1442  * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1443                    long bufsize, int flags)
1444  *
1445  * Get statistics on all filesystems.
1446  */
1447
1448 struct getvfsstat_info {
1449         struct statfs *sfsp;
1450         struct statvfs *vsfsp;
1451         long count;
1452         long maxcount;
1453         int error;
1454         int flags;
1455         struct thread *td;
1456 };
1457
1458 static int getvfsstat_callback(struct mount *, void *);
1459
1460 int
1461 sys_getvfsstat(struct getvfsstat_args *uap)
1462 {
1463         struct thread *td = curthread;
1464         struct getvfsstat_info info;
1465
1466         bzero(&info, sizeof(info));
1467
1468         info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1469         info.sfsp = uap->buf;
1470         info.vsfsp = uap->vbuf;
1471         info.count = 0;
1472         info.flags = uap->flags;
1473         info.td = td;
1474
1475         mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1476         if (info.vsfsp && info.count > info.maxcount)
1477                 uap->sysmsg_result = info.maxcount;
1478         else
1479                 uap->sysmsg_result = info.count;
1480         return (info.error);
1481 }
1482
1483 static int
1484 getvfsstat_callback(struct mount *mp, void *data)
1485 {
1486         struct getvfsstat_info *info = data;
1487         struct statfs *sp;
1488         struct statvfs *vsp;
1489         char *freepath;
1490         char *fullpath;
1491         int error;
1492
1493         if (info->vsfsp && info->count < info->maxcount) {
1494                 if (info->td->td_proc &&
1495                     !chroot_visible_mnt(mp, info->td->td_proc)) {
1496                         return(0);
1497                 }
1498                 sp = &mp->mnt_stat;
1499                 vsp = &mp->mnt_vstat;
1500
1501                 /*
1502                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1503                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1504                  * overrides MNT_WAIT.
1505                  */
1506                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1507                     (info->flags & MNT_WAIT)) &&
1508                     (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1509                         return(0);
1510                 }
1511                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1512
1513                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1514                     (info->flags & MNT_WAIT)) &&
1515                     (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1516                         return(0);
1517                 }
1518                 vsp->f_flag = 0;
1519                 if (mp->mnt_flag & MNT_RDONLY)
1520                         vsp->f_flag |= ST_RDONLY;
1521                 if (mp->mnt_flag & MNT_NOSUID)
1522                         vsp->f_flag |= ST_NOSUID;
1523
1524                 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1525                 if (error) {
1526                         info->error = error;
1527                         return(-1);
1528                 }
1529                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1530                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1531                 kfree(freepath, M_TEMP);
1532
1533                 error = copyout(sp, info->sfsp, sizeof(*sp));
1534                 if (error == 0)
1535                         error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1536                 if (error) {
1537                         info->error = error;
1538                         return (-1);
1539                 }
1540                 ++info->sfsp;
1541                 ++info->vsfsp;
1542         }
1543         info->count++;
1544         return(0);
1545 }
1546
1547
1548 /*
1549  * fchdir_args(int fd)
1550  *
1551  * Change current working directory to a given file descriptor.
1552  */
1553 int
1554 sys_fchdir(struct fchdir_args *uap)
1555 {
1556         struct thread *td = curthread;
1557         struct proc *p = td->td_proc;
1558         struct filedesc *fdp = p->p_fd;
1559         struct vnode *vp, *ovp;
1560         struct mount *mp;
1561         struct file *fp;
1562         struct nchandle nch, onch, tnch;
1563         int error;
1564
1565         if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1566                 return (error);
1567         lwkt_gettoken(&p->p_token);
1568         vp = (struct vnode *)fp->f_data;
1569         vref(vp);
1570         vn_lock(vp, LK_SHARED | LK_RETRY);
1571         if (fp->f_nchandle.ncp == NULL)
1572                 error = ENOTDIR;
1573         else
1574                 error = checkvp_chdir(vp, td);
1575         if (error) {
1576                 vput(vp);
1577                 goto done;
1578         }
1579         cache_copy(&fp->f_nchandle, &nch);
1580
1581         /*
1582          * If the ncp has become a mount point, traverse through
1583          * the mount point.
1584          */
1585
1586         while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1587                (mp = cache_findmount(&nch)) != NULL
1588         ) {
1589                 error = nlookup_mp(mp, &tnch);
1590                 if (error == 0) {
1591                         cache_unlock(&tnch);    /* leave ref intact */
1592                         vput(vp);
1593                         vp = tnch.ncp->nc_vp;
1594                         error = vget(vp, LK_SHARED);
1595                         KKASSERT(error == 0);
1596                         cache_drop(&nch);
1597                         nch = tnch;
1598                 }
1599                 cache_dropmount(mp);
1600         }
1601         if (error == 0) {
1602                 ovp = fdp->fd_cdir;
1603                 onch = fdp->fd_ncdir;
1604                 vn_unlock(vp);          /* leave ref intact */
1605                 fdp->fd_cdir = vp;
1606                 fdp->fd_ncdir = nch;
1607                 cache_drop(&onch);
1608                 vrele(ovp);
1609         } else {
1610                 cache_drop(&nch);
1611                 vput(vp);
1612         }
1613         fdrop(fp);
1614 done:
1615         lwkt_reltoken(&p->p_token);
1616         return (error);
1617 }
1618
1619 int
1620 kern_chdir(struct nlookupdata *nd)
1621 {
1622         struct thread *td = curthread;
1623         struct proc *p = td->td_proc;
1624         struct filedesc *fdp = p->p_fd;
1625         struct vnode *vp, *ovp;
1626         struct nchandle onch;
1627         int error;
1628
1629         nd->nl_flags |= NLC_SHAREDLOCK;
1630         if ((error = nlookup(nd)) != 0)
1631                 return (error);
1632         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1633                 return (ENOENT);
1634         if ((error = vget(vp, LK_SHARED)) != 0)
1635                 return (error);
1636
1637         lwkt_gettoken(&p->p_token);
1638         error = checkvp_chdir(vp, td);
1639         vn_unlock(vp);
1640         if (error == 0) {
1641                 ovp = fdp->fd_cdir;
1642                 onch = fdp->fd_ncdir;
1643                 cache_unlock(&nd->nl_nch);      /* leave reference intact */
1644                 fdp->fd_ncdir = nd->nl_nch;
1645                 fdp->fd_cdir = vp;
1646                 cache_drop(&onch);
1647                 vrele(ovp);
1648                 cache_zero(&nd->nl_nch);
1649         } else {
1650                 vrele(vp);
1651         }
1652         lwkt_reltoken(&p->p_token);
1653         return (error);
1654 }
1655
1656 /*
1657  * chdir_args(char *path)
1658  *
1659  * Change current working directory (``.'').
1660  */
1661 int
1662 sys_chdir(struct chdir_args *uap)
1663 {
1664         struct nlookupdata nd;
1665         int error;
1666
1667         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1668         if (error == 0)
1669                 error = kern_chdir(&nd);
1670         nlookup_done(&nd);
1671         return (error);
1672 }
1673
1674 /*
1675  * Helper function for raised chroot(2) security function:  Refuse if
1676  * any filedescriptors are open directories.
1677  */
1678 static int
1679 chroot_refuse_vdir_fds(struct filedesc *fdp)
1680 {
1681         struct vnode *vp;
1682         struct file *fp;
1683         int error;
1684         int fd;
1685
1686         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1687                 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1688                         continue;
1689                 vp = (struct vnode *)fp->f_data;
1690                 if (vp->v_type != VDIR) {
1691                         fdrop(fp);
1692                         continue;
1693                 }
1694                 fdrop(fp);
1695                 return(EPERM);
1696         }
1697         return (0);
1698 }
1699
1700 /*
1701  * This sysctl determines if we will allow a process to chroot(2) if it
1702  * has a directory open:
1703  *      0: disallowed for all processes.
1704  *      1: allowed for processes that were not already chroot(2)'ed.
1705  *      2: allowed for all processes.
1706  */
1707
1708 static int chroot_allow_open_directories = 1;
1709
1710 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1711      &chroot_allow_open_directories, 0, "");
1712
1713 /*
1714  * chroot to the specified namecache entry.  We obtain the vp from the
1715  * namecache data.  The passed ncp must be locked and referenced and will
1716  * remain locked and referenced on return.
1717  */
1718 int
1719 kern_chroot(struct nchandle *nch)
1720 {
1721         struct thread *td = curthread;
1722         struct proc *p = td->td_proc;
1723         struct filedesc *fdp = p->p_fd;
1724         struct vnode *vp;
1725         int error;
1726
1727         /*
1728          * Only privileged user can chroot
1729          */
1730         error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1731         if (error)
1732                 return (error);
1733
1734         /*
1735          * Disallow open directory descriptors (fchdir() breakouts).
1736          */
1737         if (chroot_allow_open_directories == 0 ||
1738            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1739                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1740                         return (error);
1741         }
1742         if ((vp = nch->ncp->nc_vp) == NULL)
1743                 return (ENOENT);
1744
1745         if ((error = vget(vp, LK_SHARED)) != 0)
1746                 return (error);
1747
1748         /*
1749          * Check the validity of vp as a directory to change to and 
1750          * associate it with rdir/jdir.
1751          */
1752         error = checkvp_chdir(vp, td);
1753         vn_unlock(vp);                  /* leave reference intact */
1754         if (error == 0) {
1755                 vrele(fdp->fd_rdir);
1756                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1757                 cache_drop(&fdp->fd_nrdir);
1758                 cache_copy(nch, &fdp->fd_nrdir);
1759                 if (fdp->fd_jdir == NULL) {
1760                         fdp->fd_jdir = vp;
1761                         vref(fdp->fd_jdir);
1762                         cache_copy(nch, &fdp->fd_njdir);
1763                 }
1764         } else {
1765                 vrele(vp);
1766         }
1767         return (error);
1768 }
1769
1770 /*
1771  * chroot_args(char *path)
1772  *
1773  * Change notion of root (``/'') directory.
1774  */
1775 int
1776 sys_chroot(struct chroot_args *uap)
1777 {
1778         struct thread *td __debugvar = curthread;
1779         struct nlookupdata nd;
1780         int error;
1781
1782         KKASSERT(td->td_proc);
1783         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1784         if (error == 0) {
1785                 nd.nl_flags |= NLC_EXEC;
1786                 error = nlookup(&nd);
1787                 if (error == 0)
1788                         error = kern_chroot(&nd.nl_nch);
1789         }
1790         nlookup_done(&nd);
1791         return(error);
1792 }
1793
1794 int
1795 sys_chroot_kernel(struct chroot_kernel_args *uap)
1796 {
1797         struct thread *td = curthread;
1798         struct nlookupdata nd;
1799         struct nchandle *nch;
1800         struct vnode *vp;
1801         int error;
1802
1803         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1804         if (error)
1805                 goto error_nond;
1806
1807         error = nlookup(&nd);
1808         if (error)
1809                 goto error_out;
1810
1811         nch = &nd.nl_nch;
1812
1813         error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1814         if (error)
1815                 goto error_out;
1816
1817         if ((vp = nch->ncp->nc_vp) == NULL) {
1818                 error = ENOENT;
1819                 goto error_out;
1820         }
1821
1822         if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1823                 goto error_out;
1824
1825         kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1826         get_mplock();
1827         vfs_cache_setroot(vp, cache_hold(nch));
1828         rel_mplock();
1829
1830 error_out:
1831         nlookup_done(&nd);
1832 error_nond:
1833         return(error);
1834 }
1835
1836 /*
1837  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1838  * determine whether it is legal to chdir to the vnode.  The vnode's state
1839  * is not changed by this call.
1840  */
1841 static int
1842 checkvp_chdir(struct vnode *vp, struct thread *td)
1843 {
1844         int error;
1845
1846         if (vp->v_type != VDIR)
1847                 error = ENOTDIR;
1848         else
1849                 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1850         return (error);
1851 }
1852
1853 int
1854 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1855 {
1856         struct thread *td = curthread;
1857         struct proc *p = td->td_proc;
1858         struct lwp *lp = td->td_lwp;
1859         struct filedesc *fdp = p->p_fd;
1860         int cmode, flags;
1861         struct file *nfp;
1862         struct file *fp;
1863         struct vnode *vp;
1864         int type, indx, error = 0;
1865         struct flock lf;
1866
1867         if ((oflags & O_ACCMODE) == O_ACCMODE)
1868                 return (EINVAL);
1869         flags = FFLAGS(oflags);
1870         error = falloc(lp, &nfp, NULL);
1871         if (error)
1872                 return (error);
1873         fp = nfp;
1874         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1875
1876         /*
1877          * XXX p_dupfd is a real mess.  It allows a device to return a
1878          * file descriptor to be duplicated rather then doing the open
1879          * itself.
1880          */
1881         lp->lwp_dupfd = -1;
1882
1883         /*
1884          * Call vn_open() to do the lookup and assign the vnode to the 
1885          * file pointer.  vn_open() does not change the ref count on fp
1886          * and the vnode, on success, will be inherited by the file pointer
1887          * and unlocked.
1888          *
1889          * Request a shared lock on the vnode if possible.
1890          */
1891         nd->nl_flags |= NLC_LOCKVP;
1892         if ((flags & (O_CREAT|O_TRUNC)) == 0)
1893                 nd->nl_flags |= NLC_SHAREDLOCK;
1894
1895         error = vn_open(nd, fp, flags, cmode);
1896         nlookup_done(nd);
1897
1898         if (error) {
1899                 /*
1900                  * handle special fdopen() case.  bleh.  dupfdopen() is
1901                  * responsible for dropping the old contents of ofiles[indx]
1902                  * if it succeeds.
1903                  *
1904                  * Note that fsetfd() will add a ref to fp which represents
1905                  * the fd_files[] assignment.  We must still drop our
1906                  * reference.
1907                  */
1908                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1909                         if (fdalloc(p, 0, &indx) == 0) {
1910                                 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1911                                 if (error == 0) {
1912                                         *res = indx;
1913                                         fdrop(fp);      /* our ref */
1914                                         return (0);
1915                                 }
1916                                 fsetfd(fdp, NULL, indx);
1917                         }
1918                 }
1919                 fdrop(fp);      /* our ref */
1920                 if (error == ERESTART)
1921                         error = EINTR;
1922                 return (error);
1923         }
1924
1925         /*
1926          * ref the vnode for ourselves so it can't be ripped out from under
1927          * is.  XXX need an ND flag to request that the vnode be returned
1928          * anyway.
1929          *
1930          * Reserve a file descriptor but do not assign it until the open
1931          * succeeds.
1932          */
1933         vp = (struct vnode *)fp->f_data;
1934         vref(vp);
1935         if ((error = fdalloc(p, 0, &indx)) != 0) {
1936                 fdrop(fp);
1937                 vrele(vp);
1938                 return (error);
1939         }
1940
1941         /*
1942          * If no error occurs the vp will have been assigned to the file
1943          * pointer.
1944          */
1945         lp->lwp_dupfd = 0;
1946
1947         if (flags & (O_EXLOCK | O_SHLOCK)) {
1948                 lf.l_whence = SEEK_SET;
1949                 lf.l_start = 0;
1950                 lf.l_len = 0;
1951                 if (flags & O_EXLOCK)
1952                         lf.l_type = F_WRLCK;
1953                 else
1954                         lf.l_type = F_RDLCK;
1955                 if (flags & FNONBLOCK)
1956                         type = 0;
1957                 else
1958                         type = F_WAIT;
1959
1960                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1961                         /*
1962                          * lock request failed.  Clean up the reserved
1963                          * descriptor.
1964                          */
1965                         vrele(vp);
1966                         fsetfd(fdp, NULL, indx);
1967                         fdrop(fp);
1968                         return (error);
1969                 }
1970                 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
1971         }
1972 #if 0
1973         /*
1974          * Assert that all regular file vnodes were created with a object.
1975          */
1976         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1977                 ("open: regular file has no backing object after vn_open"));
1978 #endif
1979
1980         vrele(vp);
1981
1982         /*
1983          * release our private reference, leaving the one associated with the
1984          * descriptor table intact.
1985          */
1986         if (oflags & O_CLOEXEC)
1987                 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
1988         fsetfd(fdp, fp, indx);
1989         fdrop(fp);
1990         *res = indx;
1991         return (error);
1992 }
1993
1994 /*
1995  * open_args(char *path, int flags, int mode)
1996  *
1997  * Check permissions, allocate an open file structure,
1998  * and call the device open routine if any.
1999  */
2000 int
2001 sys_open(struct open_args *uap)
2002 {
2003         struct nlookupdata nd;
2004         int error;
2005
2006         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2007         if (error == 0) {
2008                 error = kern_open(&nd, uap->flags,
2009                                     uap->mode, &uap->sysmsg_result);
2010         }
2011         nlookup_done(&nd);
2012         return (error);
2013 }
2014
2015 /*
2016  * openat_args(int fd, char *path, int flags, int mode)
2017  */
2018 int
2019 sys_openat(struct openat_args *uap)
2020 {
2021         struct nlookupdata nd;
2022         int error;
2023         struct file *fp;
2024
2025         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2026         if (error == 0) {
2027                 error = kern_open(&nd, uap->flags, uap->mode, 
2028                                         &uap->sysmsg_result);
2029         }
2030         nlookup_done_at(&nd, fp);
2031         return (error);
2032 }
2033
2034 int
2035 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2036 {
2037         struct thread *td = curthread;
2038         struct proc *p = td->td_proc;
2039         struct vnode *vp;
2040         struct vattr vattr;
2041         int error;
2042         int whiteout = 0;
2043
2044         KKASSERT(p);
2045
2046         VATTR_NULL(&vattr);
2047         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2048         vattr.va_rmajor = rmajor;
2049         vattr.va_rminor = rminor;
2050
2051         switch (mode & S_IFMT) {
2052         case S_IFMT:    /* used by badsect to flag bad sectors */
2053                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2054                 vattr.va_type = VBAD;
2055                 break;
2056         case S_IFCHR:
2057                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2058                 vattr.va_type = VCHR;
2059                 break;
2060         case S_IFBLK:
2061                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2062                 vattr.va_type = VBLK;
2063                 break;
2064         case S_IFWHT:
2065                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2066                 whiteout = 1;
2067                 break;
2068         case S_IFDIR:   /* special directories support for HAMMER */
2069                 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2070                 vattr.va_type = VDIR;
2071                 break;
2072         default:
2073                 error = EINVAL;
2074                 break;
2075         }
2076
2077         if (error)
2078                 return (error);
2079
2080         bwillinode(1);
2081         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2082         if ((error = nlookup(nd)) != 0)
2083                 return (error);
2084         if (nd->nl_nch.ncp->nc_vp)
2085                 return (EEXIST);
2086         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2087                 return (error);
2088
2089         if (whiteout) {
2090                 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2091                                       nd->nl_cred, NAMEI_CREATE);
2092         } else {
2093                 vp = NULL;
2094                 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2095                                    &vp, nd->nl_cred, &vattr);
2096                 if (error == 0)
2097                         vput(vp);
2098         }
2099         return (error);
2100 }
2101
2102 /*
2103  * mknod_args(char *path, int mode, int dev)
2104  *
2105  * Create a special file.
2106  */
2107 int
2108 sys_mknod(struct mknod_args *uap)
2109 {
2110         struct nlookupdata nd;
2111         int error;
2112
2113         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2114         if (error == 0) {
2115                 error = kern_mknod(&nd, uap->mode,
2116                                    umajor(uap->dev), uminor(uap->dev));
2117         }
2118         nlookup_done(&nd);
2119         return (error);
2120 }
2121
2122 /*
2123  * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2124  *
2125  * Create a special file.  The path is relative to the directory associated
2126  * with fd.
2127  */
2128 int
2129 sys_mknodat(struct mknodat_args *uap)
2130 {
2131         struct nlookupdata nd;
2132         struct file *fp;
2133         int error;
2134
2135         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2136         if (error == 0) {
2137                 error = kern_mknod(&nd, uap->mode,
2138                                    umajor(uap->dev), uminor(uap->dev));
2139         }
2140         nlookup_done_at(&nd, fp);
2141         return (error);
2142 }
2143
2144 int
2145 kern_mkfifo(struct nlookupdata *nd, int mode)
2146 {
2147         struct thread *td = curthread;
2148         struct proc *p = td->td_proc;
2149         struct vattr vattr;
2150         struct vnode *vp;
2151         int error;
2152
2153         bwillinode(1);
2154
2155         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2156         if ((error = nlookup(nd)) != 0)
2157                 return (error);
2158         if (nd->nl_nch.ncp->nc_vp)
2159                 return (EEXIST);
2160         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2161                 return (error);
2162
2163         VATTR_NULL(&vattr);
2164         vattr.va_type = VFIFO;
2165         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2166         vp = NULL;
2167         error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2168         if (error == 0)
2169                 vput(vp);
2170         return (error);
2171 }
2172
2173 /*
2174  * mkfifo_args(char *path, int mode)
2175  *
2176  * Create a named pipe.
2177  */
2178 int
2179 sys_mkfifo(struct mkfifo_args *uap)
2180 {
2181         struct nlookupdata nd;
2182         int error;
2183
2184         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2185         if (error == 0)
2186                 error = kern_mkfifo(&nd, uap->mode);
2187         nlookup_done(&nd);
2188         return (error);
2189 }
2190
2191 /*
2192  * mkfifoat_args(int fd, char *path, mode_t mode)
2193  *
2194  * Create a named pipe.  The path is relative to the directory associated
2195  * with fd.
2196  */
2197 int
2198 sys_mkfifoat(struct mkfifoat_args *uap)
2199 {
2200         struct nlookupdata nd;
2201         struct file *fp;
2202         int error;
2203
2204         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2205         if (error == 0)
2206                 error = kern_mkfifo(&nd, uap->mode);
2207         nlookup_done_at(&nd, fp);
2208         return (error);
2209 }
2210
2211 static int hardlink_check_uid = 0;
2212 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2213     &hardlink_check_uid, 0, 
2214     "Unprivileged processes cannot create hard links to files owned by other "
2215     "users");
2216 static int hardlink_check_gid = 0;
2217 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2218     &hardlink_check_gid, 0,
2219     "Unprivileged processes cannot create hard links to files owned by other "
2220     "groups");
2221
2222 static int
2223 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2224 {
2225         struct vattr va;
2226         int error;
2227
2228         /*
2229          * Shortcut if disabled
2230          */
2231         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2232                 return (0);
2233
2234         /*
2235          * Privileged user can always hardlink
2236          */
2237         if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2238                 return (0);
2239
2240         /*
2241          * Otherwise only if the originating file is owned by the
2242          * same user or group.  Note that any group is allowed if
2243          * the file is owned by the caller.
2244          */
2245         error = VOP_GETATTR(vp, &va);
2246         if (error != 0)
2247                 return (error);
2248         
2249         if (hardlink_check_uid) {
2250                 if (cred->cr_uid != va.va_uid)
2251                         return (EPERM);
2252         }
2253         
2254         if (hardlink_check_gid) {
2255                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2256                         return (EPERM);
2257         }
2258
2259         return (0);
2260 }
2261
2262 int
2263 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2264 {
2265         struct thread *td = curthread;
2266         struct vnode *vp;
2267         int error;
2268
2269         /*
2270          * Lookup the source and obtained a locked vnode.
2271          *
2272          * You may only hardlink a file which you have write permission
2273          * on or which you own.
2274          *
2275          * XXX relookup on vget failure / race ?
2276          */
2277         bwillinode(1);
2278         nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2279         if ((error = nlookup(nd)) != 0)
2280                 return (error);
2281         vp = nd->nl_nch.ncp->nc_vp;
2282         KKASSERT(vp != NULL);
2283         if (vp->v_type == VDIR)
2284                 return (EPERM);         /* POSIX */
2285         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2286                 return (error);
2287         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2288                 return (error);
2289
2290         /*
2291          * Unlock the source so we can lookup the target without deadlocking
2292          * (XXX vp is locked already, possible other deadlock?).  The target
2293          * must not exist.
2294          */
2295         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2296         nd->nl_flags &= ~NLC_NCPISLOCKED;
2297         cache_unlock(&nd->nl_nch);
2298         vn_unlock(vp);
2299
2300         linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2301         if ((error = nlookup(linknd)) != 0) {
2302                 vrele(vp);
2303                 return (error);
2304         }
2305         if (linknd->nl_nch.ncp->nc_vp) {
2306                 vrele(vp);
2307                 return (EEXIST);
2308         }
2309         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2310         if (error) {
2311                 vrele(vp);
2312                 return (error);
2313         }
2314
2315         /*
2316          * Finally run the new API VOP.
2317          */
2318         error = can_hardlink(vp, td, td->td_ucred);
2319         if (error == 0) {
2320                 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2321                                   vp, linknd->nl_cred);
2322         }
2323         vput(vp);
2324         return (error);
2325 }
2326
2327 /*
2328  * link_args(char *path, char *link)
2329  *
2330  * Make a hard file link.
2331  */
2332 int
2333 sys_link(struct link_args *uap)
2334 {
2335         struct nlookupdata nd, linknd;
2336         int error;
2337
2338         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2339         if (error == 0) {
2340                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2341                 if (error == 0)
2342                         error = kern_link(&nd, &linknd);
2343                 nlookup_done(&linknd);
2344         }
2345         nlookup_done(&nd);
2346         return (error);
2347 }
2348
2349 /*
2350  * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2351  *
2352  * Make a hard file link. The path1 argument is relative to the directory
2353  * associated with fd1, and similarly the path2 argument is relative to
2354  * the directory associated with fd2.
2355  */
2356 int
2357 sys_linkat(struct linkat_args *uap)
2358 {
2359         struct nlookupdata nd, linknd;
2360         struct file *fp1, *fp2;
2361         int error;
2362
2363         error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2364             (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2365         if (error == 0) {
2366                 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2367                     uap->path2, UIO_USERSPACE, 0);
2368                 if (error == 0)
2369                         error = kern_link(&nd, &linknd);
2370                 nlookup_done_at(&linknd, fp2);
2371         }
2372         nlookup_done_at(&nd, fp1);
2373         return (error);
2374 }
2375
2376 int
2377 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2378 {
2379         struct vattr vattr;
2380         struct vnode *vp;
2381         struct vnode *dvp;
2382         int error;
2383
2384         bwillinode(1);
2385         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2386         if ((error = nlookup(nd)) != 0)
2387                 return (error);
2388         if (nd->nl_nch.ncp->nc_vp)
2389                 return (EEXIST);
2390         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2391                 return (error);
2392         dvp = nd->nl_dvp;
2393         VATTR_NULL(&vattr);
2394         vattr.va_mode = mode;
2395         error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2396         if (error == 0)
2397                 vput(vp);
2398         return (error);
2399 }
2400
2401 /*
2402  * symlink(char *path, char *link)
2403  *
2404  * Make a symbolic link.
2405  */
2406 int
2407 sys_symlink(struct symlink_args *uap)
2408 {
2409         struct thread *td = curthread;
2410         struct nlookupdata nd;
2411         char *path;
2412         int error;
2413         int mode;
2414
2415         path = objcache_get(namei_oc, M_WAITOK);
2416         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2417         if (error == 0) {
2418                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2419                 if (error == 0) {
2420                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2421                         error = kern_symlink(&nd, path, mode);
2422                 }
2423                 nlookup_done(&nd);
2424         }
2425         objcache_put(namei_oc, path);
2426         return (error);
2427 }
2428
2429 /*
2430  * symlinkat_args(char *path1, int fd, char *path2)
2431  *
2432  * Make a symbolic link.  The path2 argument is relative to the directory
2433  * associated with fd.
2434  */
2435 int
2436 sys_symlinkat(struct symlinkat_args *uap)
2437 {
2438         struct thread *td = curthread;
2439         struct nlookupdata nd;
2440         struct file *fp;
2441         char *path1;
2442         int error;
2443         int mode;
2444
2445         path1 = objcache_get(namei_oc, M_WAITOK);
2446         error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2447         if (error == 0) {
2448                 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2449                     UIO_USERSPACE, 0);
2450                 if (error == 0) {
2451                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2452                         error = kern_symlink(&nd, path1, mode);
2453                 }
2454                 nlookup_done_at(&nd, fp);
2455         }
2456         objcache_put(namei_oc, path1);
2457         return (error);
2458 }
2459
2460 /*
2461  * undelete_args(char *path)
2462  *
2463  * Delete a whiteout from the filesystem.
2464  */
2465 int
2466 sys_undelete(struct undelete_args *uap)
2467 {
2468         struct nlookupdata nd;
2469         int error;
2470
2471         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2472         bwillinode(1);
2473         nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2474         if (error == 0)
2475                 error = nlookup(&nd);
2476         if (error == 0)
2477                 error = ncp_writechk(&nd.nl_nch);
2478         if (error == 0) {
2479                 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2480                                       NAMEI_DELETE);
2481         }
2482         nlookup_done(&nd);
2483         return (error);
2484 }
2485
2486 int
2487 kern_unlink(struct nlookupdata *nd)
2488 {
2489         int error;
2490
2491         bwillinode(1);
2492         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2493         if ((error = nlookup(nd)) != 0)
2494                 return (error);
2495         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2496                 return (error);
2497         error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2498         return (error);
2499 }
2500
2501 /*
2502  * unlink_args(char *path)
2503  *
2504  * Delete a name from the filesystem.
2505  */
2506 int
2507 sys_unlink(struct unlink_args *uap)
2508 {
2509         struct nlookupdata nd;
2510         int error;
2511
2512         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2513         if (error == 0)
2514                 error = kern_unlink(&nd);
2515         nlookup_done(&nd);
2516         return (error);
2517 }
2518
2519
2520 /*
2521  * unlinkat_args(int fd, char *path, int flags)
2522  *
2523  * Delete the file or directory entry pointed to by fd/path.
2524  */
2525 int
2526 sys_unlinkat(struct unlinkat_args *uap)
2527 {
2528         struct nlookupdata nd;
2529         struct file *fp;
2530         int error;
2531
2532         if (uap->flags & ~AT_REMOVEDIR)
2533                 return (EINVAL);
2534
2535         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2536         if (error == 0) {
2537                 if (uap->flags & AT_REMOVEDIR)
2538                         error = kern_rmdir(&nd);
2539                 else
2540                         error = kern_unlink(&nd);
2541         }
2542         nlookup_done_at(&nd, fp);
2543         return (error);
2544 }
2545
2546 int
2547 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2548 {
2549         struct thread *td = curthread;
2550         struct proc *p = td->td_proc;
2551         struct file *fp;
2552         struct vnode *vp;
2553         struct vattr vattr;
2554         off_t new_offset;
2555         int error;
2556
2557         fp = holdfp(p->p_fd, fd, -1);
2558         if (fp == NULL)
2559                 return (EBADF);
2560         if (fp->f_type != DTYPE_VNODE) {
2561                 error = ESPIPE;
2562                 goto done;
2563         }
2564         vp = (struct vnode *)fp->f_data;
2565
2566         switch (whence) {
2567         case L_INCR:
2568                 spin_lock(&fp->f_spin);
2569                 new_offset = fp->f_offset + offset;
2570                 error = 0;
2571                 break;
2572         case L_XTND:
2573                 error = VOP_GETATTR(vp, &vattr);
2574                 spin_lock(&fp->f_spin);
2575                 new_offset = offset + vattr.va_size;
2576                 break;
2577         case L_SET:
2578                 new_offset = offset;
2579                 error = 0;
2580                 spin_lock(&fp->f_spin);
2581                 break;
2582         default:
2583                 new_offset = 0;
2584                 error = EINVAL;
2585                 spin_lock(&fp->f_spin);
2586                 break;
2587         }
2588
2589         /*
2590          * Validate the seek position.  Negative offsets are not allowed
2591          * for regular files or directories.
2592          *
2593          * Normally we would also not want to allow negative offsets for
2594          * character and block-special devices.  However kvm addresses
2595          * on 64 bit architectures might appear to be negative and must
2596          * be allowed.
2597          */
2598         if (error == 0) {
2599                 if (new_offset < 0 &&
2600                     (vp->v_type == VREG || vp->v_type == VDIR)) {
2601                         error = EINVAL;
2602                 } else {
2603                         fp->f_offset = new_offset;
2604                 }
2605         }
2606         *res = fp->f_offset;
2607         spin_unlock(&fp->f_spin);
2608 done:
2609         fdrop(fp);
2610         return (error);
2611 }
2612
2613 /*
2614  * lseek_args(int fd, int pad, off_t offset, int whence)
2615  *
2616  * Reposition read/write file offset.
2617  */
2618 int
2619 sys_lseek(struct lseek_args *uap)
2620 {
2621         int error;
2622
2623         error = kern_lseek(uap->fd, uap->offset, uap->whence,
2624                            &uap->sysmsg_offset);
2625
2626         return (error);
2627 }
2628
2629 /*
2630  * Check if current process can access given file.  amode is a bitmask of *_OK
2631  * access bits.  flags is a bitmask of AT_* flags.
2632  */
2633 int
2634 kern_access(struct nlookupdata *nd, int amode, int flags)
2635 {
2636         struct vnode *vp;
2637         int error, mode;
2638
2639         if (flags & ~AT_EACCESS)
2640                 return (EINVAL);
2641         nd->nl_flags |= NLC_SHAREDLOCK;
2642         if ((error = nlookup(nd)) != 0)
2643                 return (error);
2644 retry:
2645         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2646         if (error)
2647                 return (error);
2648
2649         /* Flags == 0 means only check for existence. */
2650         if (amode) {
2651                 mode = 0;
2652                 if (amode & R_OK)
2653                         mode |= VREAD;
2654                 if (amode & W_OK)
2655                         mode |= VWRITE;
2656                 if (amode & X_OK)
2657                         mode |= VEXEC;
2658                 if ((mode & VWRITE) == 0 || 
2659                     (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2660                         error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2661
2662                 /*
2663                  * If the file handle is stale we have to re-resolve the
2664                  * entry with the ncp held exclusively.  This is a hack
2665                  * at the moment.
2666                  */
2667                 if (error == ESTALE) {
2668                         vput(vp);
2669                         cache_unlock(&nd->nl_nch);
2670                         cache_lock(&nd->nl_nch);
2671                         cache_setunresolved(&nd->nl_nch);
2672                         error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2673                         if (error == 0) {
2674                                 vp = NULL;
2675                                 goto retry;
2676                         }
2677                         return(error);
2678                 }
2679         }
2680         vput(vp);
2681         return (error);
2682 }
2683
2684 /*
2685  * access_args(char *path, int flags)
2686  *
2687  * Check access permissions.
2688  */
2689 int
2690 sys_access(struct access_args *uap)
2691 {
2692         struct nlookupdata nd;
2693         int error;
2694
2695         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2696         if (error == 0)
2697                 error = kern_access(&nd, uap->flags, 0);
2698         nlookup_done(&nd);
2699         return (error);
2700 }
2701
2702
2703 /*
2704  * eaccess_args(char *path, int flags)
2705  *
2706  * Check access permissions.
2707  */
2708 int
2709 sys_eaccess(struct eaccess_args *uap)
2710 {
2711         struct nlookupdata nd;
2712         int error;
2713
2714         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2715         if (error == 0)
2716                 error = kern_access(&nd, uap->flags, AT_EACCESS);
2717         nlookup_done(&nd);
2718         return (error);
2719 }
2720
2721
2722 /*
2723  * faccessat_args(int fd, char *path, int amode, int flags)
2724  *
2725  * Check access permissions.
2726  */
2727 int
2728 sys_faccessat(struct faccessat_args *uap)
2729 {
2730         struct nlookupdata nd;
2731         struct file *fp;
2732         int error;
2733
2734         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 
2735                                 NLC_FOLLOW);
2736         if (error == 0)
2737                 error = kern_access(&nd, uap->amode, uap->flags);
2738         nlookup_done_at(&nd, fp);
2739         return (error);
2740 }
2741
2742 int
2743 kern_stat(struct nlookupdata *nd, struct stat *st)
2744 {
2745         int error;
2746         struct vnode *vp;
2747
2748         nd->nl_flags |= NLC_SHAREDLOCK;
2749         if ((error = nlookup(nd)) != 0)
2750                 return (error);
2751 again:
2752         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2753                 return (ENOENT);
2754
2755         if ((error = vget(vp, LK_SHARED)) != 0)
2756                 return (error);
2757         error = vn_stat(vp, st, nd->nl_cred);
2758
2759         /*
2760          * If the file handle is stale we have to re-resolve the
2761          * entry with the ncp held exclusively.  This is a hack
2762          * at the moment.
2763          */
2764         if (error == ESTALE) {
2765                 vput(vp);
2766                 cache_unlock(&nd->nl_nch);
2767                 cache_lock(&nd->nl_nch);
2768                 cache_setunresolved(&nd->nl_nch);
2769                 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2770                 if (error == 0)
2771                         goto again;
2772         } else {
2773                 vput(vp);
2774         }
2775         return (error);
2776 }
2777
2778 /*
2779  * stat_args(char *path, struct stat *ub)
2780  *
2781  * Get file status; this version follows links.
2782  */
2783 int
2784 sys_stat(struct stat_args *uap)
2785 {
2786         struct nlookupdata nd;
2787         struct stat st;
2788         int error;
2789
2790         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2791         if (error == 0) {
2792                 error = kern_stat(&nd, &st);
2793                 if (error == 0)
2794                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2795         }
2796         nlookup_done(&nd);
2797         return (error);
2798 }
2799
2800 /*
2801  * lstat_args(char *path, struct stat *ub)
2802  *
2803  * Get file status; this version does not follow links.
2804  */
2805 int
2806 sys_lstat(struct lstat_args *uap)
2807 {
2808         struct nlookupdata nd;
2809         struct stat st;
2810         int error;
2811
2812         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2813         if (error == 0) {
2814                 error = kern_stat(&nd, &st);
2815                 if (error == 0)
2816                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2817         }
2818         nlookup_done(&nd);
2819         return (error);
2820 }
2821
2822 /*
2823  * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2824  *
2825  * Get status of file pointed to by fd/path.
2826  */
2827 int
2828 sys_fstatat(struct fstatat_args *uap)
2829 {
2830         struct nlookupdata nd;
2831         struct stat st;
2832         int error;
2833         int flags;
2834         struct file *fp;
2835
2836         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2837                 return (EINVAL);
2838
2839         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2840
2841         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
2842                                 UIO_USERSPACE, flags);
2843         if (error == 0) {
2844                 error = kern_stat(&nd, &st);
2845                 if (error == 0)
2846                         error = copyout(&st, uap->sb, sizeof(*uap->sb));
2847         }
2848         nlookup_done_at(&nd, fp);
2849         return (error);
2850 }
2851
2852 static int
2853 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2854 {
2855         struct nlookupdata nd;
2856         struct vnode *vp;
2857         int error;
2858
2859         vp = NULL;
2860         error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2861         if (error == 0)
2862                 error = nlookup(&nd);
2863         if (error == 0)
2864                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2865         nlookup_done(&nd);
2866         if (error == 0) {
2867                 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2868                 vput(vp);
2869         }
2870         return (error);
2871 }
2872
2873 /*
2874  * pathconf_Args(char *path, int name)
2875  *
2876  * Get configurable pathname variables.
2877  */
2878 int
2879 sys_pathconf(struct pathconf_args *uap)
2880 {
2881         return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2882                 &uap->sysmsg_reg));
2883 }
2884
2885 /*
2886  * lpathconf_Args(char *path, int name)
2887  *
2888  * Get configurable pathname variables, but don't follow symlinks.
2889  */
2890 int
2891 sys_lpathconf(struct lpathconf_args *uap)
2892 {
2893         return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2894 }
2895
2896 /*
2897  * XXX: daver
2898  * kern_readlink isn't properly split yet.  There is a copyin burried
2899  * in VOP_READLINK().
2900  */
2901 int
2902 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2903 {
2904         struct thread *td = curthread;
2905         struct vnode *vp;
2906         struct iovec aiov;
2907         struct uio auio;
2908         int error;
2909
2910         nd->nl_flags |= NLC_SHAREDLOCK;
2911         if ((error = nlookup(nd)) != 0)
2912                 return (error);
2913         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2914         if (error)
2915                 return (error);
2916         if (vp->v_type != VLNK) {
2917                 error = EINVAL;
2918         } else {
2919                 aiov.iov_base = buf;
2920                 aiov.iov_len = count;
2921                 auio.uio_iov = &aiov;
2922                 auio.uio_iovcnt = 1;
2923                 auio.uio_offset = 0;
2924                 auio.uio_rw = UIO_READ;
2925                 auio.uio_segflg = UIO_USERSPACE;
2926                 auio.uio_td = td;
2927                 auio.uio_resid = count;
2928                 error = VOP_READLINK(vp, &auio, td->td_ucred);
2929         }
2930         vput(vp);
2931         *res = count - auio.uio_resid;
2932         return (error);
2933 }
2934
2935 /*
2936  * readlink_args(char *path, char *buf, int count)
2937  *
2938  * Return target name of a symbolic link.
2939  */
2940 int
2941 sys_readlink(struct readlink_args *uap)
2942 {
2943         struct nlookupdata nd;
2944         int error;
2945
2946         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2947         if (error == 0) {
2948                 error = kern_readlink(&nd, uap->buf, uap->count,
2949                                         &uap->sysmsg_result);
2950         }
2951         nlookup_done(&nd);
2952         return (error);
2953 }
2954
2955 /*
2956  * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2957  *
2958  * Return target name of a symbolic link.  The path is relative to the
2959  * directory associated with fd.
2960  */
2961 int
2962 sys_readlinkat(struct readlinkat_args *uap)
2963 {
2964         struct nlookupdata nd;
2965         struct file *fp;
2966         int error;
2967
2968         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2969         if (error == 0) {
2970                 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2971                                         &uap->sysmsg_result);
2972         }
2973         nlookup_done_at(&nd, fp);
2974         return (error);
2975 }
2976
2977 static int
2978 setfflags(struct vnode *vp, int flags)
2979 {
2980         struct thread *td = curthread;
2981         int error;
2982         struct vattr vattr;
2983
2984         /*
2985          * Prevent non-root users from setting flags on devices.  When
2986          * a device is reused, users can retain ownership of the device
2987          * if they are allowed to set flags and programs assume that
2988          * chown can't fail when done as root.
2989          */
2990         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2991             ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2992                 return (error);
2993
2994         /*
2995          * note: vget is required for any operation that might mod the vnode
2996          * so VINACTIVE is properly cleared.
2997          */
2998         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2999                 VATTR_NULL(&vattr);
3000                 vattr.va_flags = flags;
3001                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3002                 vput(vp);
3003         }
3004         return (error);
3005 }
3006
3007 /*
3008  * chflags(char *path, int flags)
3009  *
3010  * Change flags of a file given a path name.
3011  */
3012 int
3013 sys_chflags(struct chflags_args *uap)
3014 {
3015         struct nlookupdata nd;
3016         struct vnode *vp;
3017         int error;
3018
3019         vp = NULL;
3020         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3021         if (error == 0)
3022                 error = nlookup(&nd);
3023         if (error == 0)
3024                 error = ncp_writechk(&nd.nl_nch);
3025         if (error == 0)
3026                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3027         nlookup_done(&nd);
3028         if (error == 0) {
3029                 error = setfflags(vp, uap->flags);
3030                 vrele(vp);
3031         }
3032         return (error);
3033 }
3034
3035 /*
3036  * lchflags(char *path, int flags)
3037  *
3038  * Change flags of a file given a path name, but don't follow symlinks.
3039  */
3040 int
3041 sys_lchflags(struct lchflags_args *uap)
3042 {
3043         struct nlookupdata nd;
3044         struct vnode *vp;
3045         int error;
3046
3047         vp = NULL;
3048         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3049         if (error == 0)
3050                 error = nlookup(&nd);
3051         if (error == 0)
3052                 error = ncp_writechk(&nd.nl_nch);
3053         if (error == 0)
3054                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3055         nlookup_done(&nd);
3056         if (error == 0) {
3057                 error = setfflags(vp, uap->flags);
3058                 vrele(vp);
3059         }
3060         return (error);
3061 }
3062
3063 /*
3064  * fchflags_args(int fd, int flags)
3065  *
3066  * Change flags of a file given a file descriptor.
3067  */
3068 int
3069 sys_fchflags(struct fchflags_args *uap)
3070 {
3071         struct thread *td = curthread;
3072         struct proc *p = td->td_proc;
3073         struct file *fp;
3074         int error;
3075
3076         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3077                 return (error);
3078         if (fp->f_nchandle.ncp)
3079                 error = ncp_writechk(&fp->f_nchandle);
3080         if (error == 0)
3081                 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3082         fdrop(fp);
3083         return (error);
3084 }
3085
3086 /*
3087  * chflagsat_args(int fd, const char *path, int flags, int atflags)
3088  * change flags given a pathname relative to a filedescriptor
3089  */
3090 int sys_chflagsat(struct chflagsat_args *uap)
3091 {
3092         struct nlookupdata nd;
3093         struct vnode *vp;
3094         struct file *fp;
3095         int error;
3096         int lookupflags;
3097
3098         if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3099                 return (EINVAL);
3100
3101         lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3102
3103         vp = NULL;
3104         error = nlookup_init_at(&nd, &fp, uap->fd,  uap->path, UIO_USERSPACE, lookupflags);
3105         if (error == 0)
3106                 error = nlookup(&nd);
3107         if (error == 0)
3108                 error = ncp_writechk(&nd.nl_nch);
3109         if (error == 0)
3110                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3111         nlookup_done_at(&nd, fp);
3112         if (error == 0) {
3113                 error = setfflags(vp, uap->flags);
3114                 vrele(vp);
3115         }
3116         return (error);
3117 }
3118
3119
3120 static int
3121 setfmode(struct vnode *vp, int mode)
3122 {
3123         struct thread *td = curthread;
3124         int error;
3125         struct vattr vattr;
3126
3127         /*
3128          * note: vget is required for any operation that might mod the vnode
3129          * so VINACTIVE is properly cleared.
3130          */
3131         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3132                 VATTR_NULL(&vattr);
3133                 vattr.va_mode = mode & ALLPERMS;
3134                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3135                 vput(vp);
3136         }
3137         return error;
3138 }
3139
3140 int
3141 kern_chmod(struct nlookupdata *nd, int mode)
3142 {
3143         struct vnode *vp;
3144         int error;
3145
3146         if ((error = nlookup(nd)) != 0)
3147                 return (error);
3148         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3149                 return (error);
3150         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3151                 error = setfmode(vp, mode);
3152         vrele(vp);
3153         return (error);
3154 }
3155
3156 /*
3157  * chmod_args(char *path, int mode)
3158  *
3159  * Change mode of a file given path name.
3160  */
3161 int
3162 sys_chmod(struct chmod_args *uap)
3163 {
3164         struct nlookupdata nd;
3165         int error;
3166
3167         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3168         if (error == 0)
3169                 error = kern_chmod(&nd, uap->mode);
3170         nlookup_done(&nd);
3171         return (error);
3172 }
3173
3174 /*
3175  * lchmod_args(char *path, int mode)
3176  *
3177  * Change mode of a file given path name (don't follow links.)
3178  */
3179 int
3180 sys_lchmod(struct lchmod_args *uap)
3181 {
3182         struct nlookupdata nd;
3183         int error;
3184
3185         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3186         if (error == 0)
3187                 error = kern_chmod(&nd, uap->mode);
3188         nlookup_done(&nd);
3189         return (error);
3190 }
3191
3192 /*
3193  * fchmod_args(int fd, int mode)
3194  *
3195  * Change mode of a file given a file descriptor.
3196  */
3197 int
3198 sys_fchmod(struct fchmod_args *uap)
3199 {
3200         struct thread *td = curthread;
3201         struct proc *p = td->td_proc;
3202         struct file *fp;
3203         int error;
3204
3205         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3206                 return (error);
3207         if (fp->f_nchandle.ncp)
3208                 error = ncp_writechk(&fp->f_nchandle);
3209         if (error == 0)
3210                 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3211         fdrop(fp);
3212         return (error);
3213 }
3214
3215 /*
3216  * fchmodat_args(char *path, int mode)
3217  *
3218  * Change mode of a file pointed to by fd/path.
3219  */
3220 int
3221 sys_fchmodat(struct fchmodat_args *uap)
3222 {
3223         struct nlookupdata nd;
3224         struct file *fp;
3225         int error;
3226         int flags;
3227
3228         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3229                 return (EINVAL);
3230         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3231
3232         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
3233                                 UIO_USERSPACE, flags);
3234         if (error == 0)
3235                 error = kern_chmod(&nd, uap->mode);
3236         nlookup_done_at(&nd, fp);
3237         return (error);
3238 }
3239
3240 static int
3241 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3242 {
3243         struct thread *td = curthread;
3244         int error;
3245         struct vattr vattr;
3246         uid_t o_uid;
3247         gid_t o_gid;
3248         uint64_t size;
3249
3250         /*
3251          * note: vget is required for any operation that might mod the vnode
3252          * so VINACTIVE is properly cleared.
3253          */
3254         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3255                 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3256                         return error;
3257                 o_uid = vattr.va_uid;
3258                 o_gid = vattr.va_gid;
3259                 size = vattr.va_size;
3260
3261                 VATTR_NULL(&vattr);
3262                 vattr.va_uid = uid;
3263                 vattr.va_gid = gid;
3264                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3265                 vput(vp);
3266         }
3267
3268         if (error == 0) {
3269                 if (uid == -1)
3270                         uid = o_uid;
3271                 if (gid == -1)
3272                         gid = o_gid;
3273                 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3274                 VFS_ACCOUNT(mp,   uid,   gid,  size);
3275         }
3276
3277         return error;
3278 }
3279
3280 int
3281 kern_chown(struct nlookupdata *nd, int uid, int gid)
3282 {
3283         struct vnode *vp;
3284         int error;
3285
3286         if ((error = nlookup(nd)) != 0)
3287                 return (error);
3288         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3289                 return (error);
3290         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3291                 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3292         vrele(vp);
3293         return (error);
3294 }
3295
3296 /*
3297  * chown(char *path, int uid, int gid)
3298  *
3299  * Set ownership given a path name.
3300  */
3301 int
3302 sys_chown(struct chown_args *uap)
3303 {
3304         struct nlookupdata nd;
3305         int error;
3306
3307         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3308         if (error == 0)
3309                 error = kern_chown(&nd, uap->uid, uap->gid);
3310         nlookup_done(&nd);
3311         return (error);
3312 }
3313
3314 /*
3315  * lchown_args(char *path, int uid, int gid)
3316  *
3317  * Set ownership given a path name, do not cross symlinks.
3318  */
3319 int
3320 sys_lchown(struct lchown_args *uap)
3321 {
3322         struct nlookupdata nd;
3323         int error;
3324
3325         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3326         if (error == 0)
3327                 error = kern_chown(&nd, uap->uid, uap->gid);
3328         nlookup_done(&nd);
3329         return (error);
3330 }
3331
3332 /*
3333  * fchown_args(int fd, int uid, int gid)
3334  *
3335  * Set ownership given a file descriptor.
3336  */
3337 int
3338 sys_fchown(struct fchown_args *uap)
3339 {
3340         struct thread *td = curthread;
3341         struct proc *p = td->td_proc;
3342         struct file *fp;
3343         int error;
3344
3345         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3346                 return (error);
3347         if (fp->f_nchandle.ncp)
3348                 error = ncp_writechk(&fp->f_nchandle);
3349         if (error == 0)
3350                 error = setfown(p->p_fd->fd_ncdir.mount,
3351                         (struct vnode *)fp->f_data, uap->uid, uap->gid);
3352         fdrop(fp);
3353         return (error);
3354 }
3355
3356 /*
3357  * fchownat(int fd, char *path, int uid, int gid, int flags)
3358  *
3359  * Set ownership of file pointed to by fd/path.
3360  */
3361 int
3362 sys_fchownat(struct fchownat_args *uap)
3363 {
3364         struct nlookupdata nd;
3365         struct file *fp;
3366         int error;
3367         int flags;
3368
3369         if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3370                 return (EINVAL);
3371         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3372
3373         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
3374                                 UIO_USERSPACE, flags);
3375         if (error == 0)
3376                 error = kern_chown(&nd, uap->uid, uap->gid);
3377         nlookup_done_at(&nd, fp);
3378         return (error);
3379 }
3380
3381
3382 static int
3383 getutimes(struct timeval *tvp, struct timespec *tsp)
3384 {
3385         struct timeval tv[2];
3386         int error;
3387
3388         if (tvp == NULL) {
3389                 microtime(&tv[0]);
3390                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3391                 tsp[1] = tsp[0];
3392         } else {
3393                 if ((error = itimerfix(tvp)) != 0)
3394                         return (error);
3395                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3396                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3397         }
3398         return 0;
3399 }
3400
3401 static int
3402 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3403 {
3404         struct timespec tsnow;
3405         int error;
3406
3407         *nullflag = 0;
3408         nanotime(&tsnow);
3409         if (ts == NULL) {
3410                 newts[0] = tsnow;
3411                 newts[1] = tsnow;
3412                 *nullflag = 1;
3413                 return (0);
3414         }
3415
3416         newts[0] = ts[0];
3417         newts[1] = ts[1];
3418         if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3419                 return (0);
3420         if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3421                 *nullflag = 1;
3422
3423         if (newts[0].tv_nsec == UTIME_OMIT)
3424                 newts[0].tv_sec = VNOVAL;
3425         else if (newts[0].tv_nsec == UTIME_NOW)
3426                 newts[0] = tsnow;
3427         else if ((error = itimespecfix(&newts[0])) != 0)
3428                 return (error);
3429
3430         if (newts[1].tv_nsec == UTIME_OMIT)
3431                 newts[1].tv_sec = VNOVAL;
3432         else if (newts[1].tv_nsec == UTIME_NOW)
3433                 newts[1] = tsnow;
3434         else if ((error = itimespecfix(&newts[1])) != 0)
3435                 return (error);
3436
3437         return (0);
3438 }
3439
3440 static int
3441 setutimes(struct vnode *vp, struct vattr *vattr,
3442           const struct timespec *ts, int nullflag)
3443 {
3444         struct thread *td = curthread;
3445         int error;
3446
3447         VATTR_NULL(vattr);
3448         vattr->va_atime = ts[0];
3449         vattr->va_mtime = ts[1];
3450         if (nullflag)
3451                 vattr->va_vaflags |= VA_UTIMES_NULL;
3452         error = VOP_SETATTR(vp, vattr, td->td_ucred);
3453
3454         return error;
3455 }
3456
3457 int
3458 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3459 {
3460         struct timespec ts[2];
3461         int error;
3462
3463         if (tptr) {
3464                 if ((error = getutimes(tptr, ts)) != 0)
3465                         return (error);
3466         }
3467         error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3468         return (error);
3469 }
3470
3471 /*
3472  * utimes_args(char *path, struct timeval *tptr)
3473  *
3474  * Set the access and modification times of a file.
3475  */
3476 int
3477 sys_utimes(struct utimes_args *uap)
3478 {
3479         struct timeval tv[2];
3480         struct nlookupdata nd;
3481         int error;
3482
3483         if (uap->tptr) {
3484                 error = copyin(uap->tptr, tv, sizeof(tv));
3485                 if (error)
3486                         return (error);
3487         }
3488         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3489         if (error == 0)
3490                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3491         nlookup_done(&nd);
3492         return (error);
3493 }
3494
3495 /*
3496  * lutimes_args(char *path, struct timeval *tptr)
3497  *
3498  * Set the access and modification times of a file.
3499  */
3500 int
3501 sys_lutimes(struct lutimes_args *uap)
3502 {
3503         struct timeval tv[2];
3504         struct nlookupdata nd;
3505         int error;
3506
3507         if (uap->tptr) {
3508                 error = copyin(uap->tptr, tv, sizeof(tv));
3509                 if (error)
3510                         return (error);
3511         }
3512         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3513         if (error == 0)
3514                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3515         nlookup_done(&nd);
3516         return (error);
3517 }
3518
3519 /*
3520  * Set utimes on a file descriptor.  The creds used to open the
3521  * file are used to determine whether the operation is allowed
3522  * or not.
3523  */
3524 int
3525 kern_futimens(int fd, struct timespec *ts)
3526 {
3527         struct thread *td = curthread;
3528         struct proc *p = td->td_proc;
3529         struct timespec newts[2];
3530         struct file *fp;
3531         struct vnode *vp;
3532         struct vattr vattr;
3533         int nullflag;
3534         int error;
3535
3536         error = getutimens(ts, newts, &nullflag);
3537         if (error)
3538                 return (error);
3539         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3540                 return (error);
3541         if (fp->f_nchandle.ncp)
3542                 error = ncp_writechk(&fp->f_nchandle);
3543         if (error == 0) {
3544                 vp = fp->f_data;
3545                 error = vget(vp, LK_EXCLUSIVE);
3546                 if (error == 0) {
3547                         error = VOP_GETATTR(vp, &vattr);
3548                         if (error == 0) {
3549                                 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3550                                                    fp->f_cred);
3551                         }
3552                         if (error == 0) {
3553                                 error = setutimes(vp, &vattr, newts, nullflag);
3554                         }
3555                         vput(vp);
3556                 }
3557         }
3558         fdrop(fp);
3559         return (error);
3560 }
3561
3562 /*
3563  * futimens_args(int fd, struct timespec *ts)
3564  *
3565  * Set the access and modification times of a file.
3566  */
3567 int
3568 sys_futimens(struct futimens_args *uap)
3569 {
3570         struct timespec ts[2];
3571         int error;
3572
3573         if (uap->ts) {
3574                 error = copyin(uap->ts, ts, sizeof(ts));
3575                 if (error)
3576                         return (error);
3577         }
3578         error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3579         return (error);
3580 }
3581
3582 int
3583 kern_futimes(int fd, struct timeval *tptr)
3584 {
3585         struct timespec ts[2];
3586         int error;
3587
3588         if (tptr) {
3589                 if ((error = getutimes(tptr, ts)) != 0)
3590                         return (error);
3591         }
3592         error = kern_futimens(fd, tptr ? ts : NULL);
3593         return (error);
3594 }
3595
3596 /*
3597  * futimes_args(int fd, struct timeval *tptr)
3598  *
3599  * Set the access and modification times of a file.
3600  */
3601 int
3602 sys_futimes(struct futimes_args *uap)
3603 {
3604         struct timeval tv[2];
3605         int error;
3606
3607         if (uap->tptr) {
3608                 error = copyin(uap->tptr, tv, sizeof(tv));
3609                 if (error)
3610                         return (error);
3611         }
3612         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3613         return (error);
3614 }
3615
3616 int
3617 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3618 {
3619         struct timespec newts[2];
3620         struct vnode *vp;
3621         struct vattr vattr;
3622         int nullflag;
3623         int error;
3624
3625         if (flags & ~AT_SYMLINK_NOFOLLOW)
3626                 return (EINVAL);
3627
3628         error = getutimens(ts, newts, &nullflag);
3629         if (error)
3630                 return (error);
3631
3632         nd->nl_flags |= NLC_OWN | NLC_WRITE;
3633         if ((error = nlookup(nd)) != 0)
3634                 return (error);
3635         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3636                 return (error);
3637         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3638                 return (error);
3639         if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3640                 error = vget(vp, LK_EXCLUSIVE);
3641                 if (error == 0) {
3642                         error = setutimes(vp, &vattr, newts, nullflag);
3643                         vput(vp);
3644                 }
3645         }
3646         vrele(vp);
3647         return (error);
3648 }
3649
3650 /*
3651  * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3652  *
3653  * Set file access and modification times of a file.
3654  */
3655 int
3656 sys_utimensat(struct utimensat_args *uap)
3657 {
3658         struct timespec ts[2];
3659         struct nlookupdata nd;
3660         struct file *fp;
3661         int error;
3662         int flags;
3663
3664         if (uap->ts) {
3665                 error = copyin(uap->ts, ts, sizeof(ts));
3666                 if (error)
3667                         return (error);
3668         }
3669
3670         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3671         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3672                                 UIO_USERSPACE, flags);
3673         if (error == 0)
3674                 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3675         nlookup_done_at(&nd, fp);
3676         return (error);
3677 }
3678
3679 int
3680 kern_truncate(struct nlookupdata *nd, off_t length)
3681 {
3682         struct vnode *vp;
3683         struct vattr vattr;
3684         int error;
3685         uid_t uid = 0;
3686         gid_t gid = 0;
3687         uint64_t old_size = 0;
3688
3689         if (length < 0)
3690                 return(EINVAL);
3691         nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3692         if ((error = nlookup(nd)) != 0)
3693                 return (error);
3694         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3695                 return (error);
3696         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3697                 return (error);
3698         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3699         if (error) {
3700                 vrele(vp);
3701                 return (error);
3702         }
3703         if (vp->v_type == VDIR) {
3704                 error = EISDIR;
3705                 goto done;
3706         }
3707         if (vfs_quota_enabled) {
3708                 error = VOP_GETATTR(vp, &vattr);
3709                 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3710                 uid = vattr.va_uid;
3711                 gid = vattr.va_gid;
3712                 old_size = vattr.va_size;
3713         }
3714
3715         if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3716                 VATTR_NULL(&vattr);
3717                 vattr.va_size = length;
3718                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3719                 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3720         }
3721 done:
3722         vput(vp);
3723         return (error);
3724 }
3725
3726 /*
3727  * truncate(char *path, int pad, off_t length)
3728  *
3729  * Truncate a file given its path name.
3730  */
3731 int
3732 sys_truncate(struct truncate_args *uap)
3733 {
3734         struct nlookupdata nd;
3735         int error;
3736
3737         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3738         if (error == 0)
3739                 error = kern_truncate(&nd, uap->length);
3740         nlookup_done(&nd);
3741         return error;
3742 }
3743
3744 int
3745 kern_ftruncate(int fd, off_t length)
3746 {
3747         struct thread *td = curthread;
3748         struct proc *p = td->td_proc;
3749         struct vattr vattr;
3750         struct vnode *vp;
3751         struct file *fp;
3752         int error;
3753         uid_t uid = 0;
3754         gid_t gid = 0;
3755         uint64_t old_size = 0;
3756         struct mount *mp;
3757
3758         if (length < 0)
3759                 return(EINVAL);
3760         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3761                 return (error);
3762         if (fp->f_nchandle.ncp) {
3763                 error = ncp_writechk(&fp->f_nchandle);
3764                 if (error)
3765                         goto done;
3766         }
3767         if ((fp->f_flag & FWRITE) == 0) {
3768                 error = EINVAL;
3769                 goto done;
3770         }
3771         if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3772                 error = EINVAL;
3773                 goto done;
3774         }
3775         vp = (struct vnode *)fp->f_data;
3776         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3777         if (vp->v_type == VDIR) {
3778                 error = EISDIR;
3779                 vn_unlock(vp);
3780                 goto done;
3781         }
3782
3783         if (vfs_quota_enabled) {
3784                 error = VOP_GETATTR(vp, &vattr);
3785                 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3786                 uid = vattr.va_uid;
3787                 gid = vattr.va_gid;
3788                 old_size = vattr.va_size;
3789         }
3790
3791         if ((error = vn_writechk(vp, NULL)) == 0) {
3792                 VATTR_NULL(&vattr);
3793                 vattr.va_size = length;
3794                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3795                 mp = vq_vptomp(vp);
3796                 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3797         }
3798         vn_unlock(vp);
3799 done:
3800         fdrop(fp);
3801         return (error);
3802 }
3803
3804 /*
3805  * ftruncate_args(int fd, int pad, off_t length)
3806  *
3807  * Truncate a file given a file descriptor.
3808  */
3809 int
3810 sys_ftruncate(struct ftruncate_args *uap)
3811 {
3812         int error;
3813
3814         error = kern_ftruncate(uap->fd, uap->length);
3815
3816         return (error);
3817 }
3818
3819 /*
3820  * fsync(int fd)
3821  *
3822  * Sync an open file.
3823  */
3824 int
3825 sys_fsync(struct fsync_args *uap)
3826 {
3827         struct thread *td = curthread;
3828         struct proc *p = td->td_proc;
3829         struct vnode *vp;
3830         struct file *fp;
3831         vm_object_t obj;
3832         int error;
3833
3834         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3835                 return (error);
3836         vp = (struct vnode *)fp->f_data;
3837         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3838         if ((obj = vp->v_object) != NULL) {
3839                 if (vp->v_mount == NULL ||
3840                     (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3841                         vm_object_page_clean(obj, 0, 0, 0);
3842                 }
3843         }
3844         error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3845         if (error == 0 && vp->v_mount)
3846                 error = buf_fsync(vp);
3847         vn_unlock(vp);
3848         fdrop(fp);
3849
3850         return (error);
3851 }
3852
3853 int
3854 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3855 {
3856         struct nchandle fnchd;
3857         struct nchandle tnchd;
3858         struct namecache *ncp;
3859         struct vnode *fdvp;
3860         struct vnode *tdvp;
3861         struct mount *mp;
3862         int error;
3863         u_int fncp_gen;
3864         u_int tncp_gen;
3865
3866         bwillinode(1);
3867         fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3868         if ((error = nlookup(fromnd)) != 0)
3869                 return (error);
3870         if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3871                 return (ENOENT);
3872         fnchd.mount = fromnd->nl_nch.mount;
3873         cache_hold(&fnchd);
3874
3875         /*
3876          * unlock the source nch so we can lookup the target nch without
3877          * deadlocking.  The target may or may not exist so we do not check
3878          * for a target vp like kern_mkdir() and other creation functions do.
3879          *
3880          * The source and target directories are ref'd and rechecked after
3881          * everything is relocked to determine if the source or target file
3882          * has been renamed.
3883          */
3884         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3885         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3886
3887         fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3888
3889         cache_unlock(&fromnd->nl_nch);
3890
3891         tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3892         if ((error = nlookup(tond)) != 0) {
3893                 cache_drop(&fnchd);
3894                 return (error);
3895         }
3896         tncp_gen = tond->nl_nch.ncp->nc_generation;
3897
3898         if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3899                 cache_drop(&fnchd);
3900                 return (ENOENT);
3901         }
3902         tnchd.mount = tond->nl_nch.mount;
3903         cache_hold(&tnchd);
3904
3905         /*
3906          * If the source and target are the same there is nothing to do
3907          */
3908         if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3909                 cache_drop(&fnchd);
3910                 cache_drop(&tnchd);
3911                 return (0);
3912         }
3913
3914         /*
3915          * Mount points cannot be renamed or overwritten
3916          */
3917         if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3918             NCF_ISMOUNTPT
3919         ) {
3920                 cache_drop(&fnchd);
3921                 cache_drop(&tnchd);
3922                 return (EINVAL);
3923         }
3924
3925         /*
3926          * Relock the source ncp.  cache_relock() will deal with any
3927          * deadlocks against the already-locked tond and will also
3928          * make sure both are resolved.
3929          *
3930          * NOTE AFTER RELOCKING: The source or target ncp may have become
3931          * invalid while they were unlocked, nc_vp and nc_mount could
3932          * be NULL.
3933          */
3934         cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3935                      &tond->nl_nch, tond->nl_cred);
3936         fromnd->nl_flags |= NLC_NCPISLOCKED;
3937
3938         /*
3939          * If the namecache generation changed for either fromnd or tond,
3940          * we must retry.
3941          */
3942         if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3943             tond->nl_nch.ncp->nc_generation != tncp_gen) {
3944                 kprintf("kern_rename: retry due to gen on: "
3945                         "\"%s\" -> \"%s\"\n",
3946                         fromnd->nl_nch.ncp->nc_name,
3947                         tond->nl_nch.ncp->nc_name);
3948                 cache_drop(&fnchd);
3949                 cache_drop(&tnchd);
3950                 return (EAGAIN);
3951         }
3952
3953         /*
3954          * If either fromnd or tond are marked destroyed a ripout occured
3955          * out from under us and we must retry.
3956          */
3957         if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3958             fromnd->nl_nch.ncp->nc_vp == NULL ||
3959             (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3960                 kprintf("kern_rename: retry due to ripout on: "
3961                         "\"%s\" -> \"%s\"\n",
3962                         fromnd->nl_nch.ncp->nc_name,
3963                         tond->nl_nch.ncp->nc_name);
3964                 cache_drop(&fnchd);
3965                 cache_drop(&tnchd);
3966                 return (EAGAIN);
3967         }
3968
3969         /*
3970          * Make sure the parent directories linkages are the same.
3971          * XXX shouldn't be needed any more w/ generation check above.
3972          */
3973         if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3974             tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3975                 cache_drop(&fnchd);
3976                 cache_drop(&tnchd);
3977                 return (ENOENT);
3978         }
3979
3980         /*
3981          * Both the source and target must be within the same filesystem and
3982          * in the same filesystem as their parent directories within the
3983          * namecache topology.
3984          *
3985          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3986          */
3987         mp = fnchd.mount;
3988         if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3989             mp != tond->nl_nch.mount) {
3990                 cache_drop(&fnchd);
3991                 cache_drop(&tnchd);
3992                 return (EXDEV);
3993         }
3994
3995         /*
3996          * Make sure the mount point is writable
3997          */
3998         if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3999                 cache_drop(&fnchd);
4000                 cache_drop(&tnchd);
4001                 return (error);
4002         }
4003
4004         /*
4005          * If the target exists and either the source or target is a directory,
4006          * then both must be directories.
4007          *
4008          * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4009          * have become NULL.
4010          */
4011         if (tond->nl_nch.ncp->nc_vp) {
4012                 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4013                         error = ENOENT;
4014                 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4015                         if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4016                                 error = ENOTDIR;
4017                 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4018                         error = EISDIR;
4019                 }
4020         }
4021
4022         /*
4023          * You cannot rename a source into itself or a subdirectory of itself.
4024          * We check this by travsersing the target directory upwards looking
4025          * for a match against the source.
4026          *
4027          * XXX MPSAFE
4028          */
4029         if (error == 0) {
4030                 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4031                         if (fromnd->nl_nch.ncp == ncp) {
4032                                 error = EINVAL;
4033                                 break;
4034                         }
4035                 }
4036         }
4037
4038         cache_drop(&fnchd);
4039         cache_drop(&tnchd);
4040
4041         /*
4042          * Even though the namespaces are different, they may still represent
4043          * hardlinks to the same file.  The filesystem might have a hard time
4044          * with this so we issue a NREMOVE of the source instead of a NRENAME
4045          * when we detect the situation.
4046          */
4047         if (error == 0) {
4048                 fdvp = fromnd->nl_dvp;
4049                 tdvp = tond->nl_dvp;
4050                 if (fdvp == NULL || tdvp == NULL) {
4051                         error = EPERM;
4052                 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4053                         error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4054                                             fromnd->nl_cred);
4055                 } else {
4056                         error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 
4057                                             fdvp, tdvp, tond->nl_cred);
4058                 }
4059         }
4060         return (error);
4061 }
4062
4063 /*
4064  * rename_args(char *from, char *to)
4065  *
4066  * Rename files.  Source and destination must either both be directories,
4067  * or both not be directories.  If target is a directory, it must be empty.
4068  */
4069 int
4070 sys_rename(struct rename_args *uap)
4071 {
4072         struct nlookupdata fromnd, tond;
4073         int error;
4074
4075         do {
4076                 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4077                 if (error == 0) {
4078                         error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4079                         if (error == 0)
4080                                 error = kern_rename(&fromnd, &tond);
4081                         nlookup_done(&tond);
4082                 }
4083                 nlookup_done(&fromnd);
4084         } while (error == EAGAIN);
4085         return (error);
4086 }
4087
4088 /*
4089  * renameat_args(int oldfd, char *old, int newfd, char *new)
4090  *
4091  * Rename files using paths relative to the directories associated with
4092  * oldfd and newfd.  Source and destination must either both be directories,
4093  * or both not be directories.  If target is a directory, it must be empty.
4094  */
4095 int
4096 sys_renameat(struct renameat_args *uap)
4097 {
4098         struct nlookupdata oldnd, newnd;
4099         struct file *oldfp, *newfp;
4100         int error;
4101
4102         do {
4103                 error = nlookup_init_at(&oldnd, &oldfp,
4104                                         uap->oldfd, uap->old,
4105                                         UIO_USERSPACE, 0);
4106                 if (error == 0) {
4107                         error = nlookup_init_at(&newnd, &newfp,
4108                                                 uap->newfd, uap->new,
4109                                                 UIO_USERSPACE, 0);
4110                         if (error == 0)
4111                                 error = kern_rename(&oldnd, &newnd);
4112                         nlookup_done_at(&newnd, newfp);
4113                 }
4114                 nlookup_done_at(&oldnd, oldfp);
4115         } while (error == EAGAIN);
4116         return (error);
4117 }
4118
4119 int
4120 kern_mkdir(struct nlookupdata *nd, int mode)
4121 {
4122         struct thread *td = curthread;
4123         struct proc *p = td->td_proc;
4124         struct vnode *vp;
4125         struct vattr vattr;
4126         int error;
4127
4128         bwillinode(1);
4129         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4130         if ((error = nlookup(nd)) != 0)
4131                 return (error);
4132
4133         if (nd->nl_nch.ncp->nc_vp)
4134                 return (EEXIST);
4135         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4136                 return (error);
4137         VATTR_NULL(&vattr);
4138         vattr.va_type = VDIR;
4139         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4140
4141         vp = NULL;
4142         error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4143         if (error == 0)
4144                 vput(vp);
4145         return (error);
4146 }
4147
4148 /*
4149  * mkdir_args(char *path, int mode)
4150  *
4151  * Make a directory file.
4152  */
4153 int
4154 sys_mkdir(struct mkdir_args *uap)
4155 {
4156         struct nlookupdata nd;
4157         int error;
4158
4159         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4160         if (error == 0)
4161                 error = kern_mkdir(&nd, uap->mode);
4162         nlookup_done(&nd);
4163         return (error);
4164 }
4165
4166 /*
4167  * mkdirat_args(int fd, char *path, mode_t mode)
4168  *
4169  * Make a directory file.  The path is relative to the directory associated
4170  * with fd.
4171  */
4172 int
4173 sys_mkdirat(struct mkdirat_args *uap)
4174 {
4175         struct nlookupdata nd;
4176         struct file *fp;
4177         int error;
4178
4179         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4180         if (error == 0)
4181                 error = kern_mkdir(&nd, uap->mode);
4182         nlookup_done_at(&nd, fp);
4183         return (error);
4184 }
4185
4186 int
4187 kern_rmdir(struct nlookupdata *nd)
4188 {
4189         int error;
4190
4191         bwillinode(1);
4192         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4193         if ((error = nlookup(nd)) != 0)
4194                 return (error);
4195
4196         /*
4197          * Do not allow directories representing mount points to be
4198          * deleted, even if empty.  Check write perms on mount point
4199          * in case the vnode is aliased (aka nullfs).
4200          */
4201         if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4202                 return (EBUSY);
4203         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4204                 return (error);
4205         error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4206         return (error);
4207 }
4208
4209 /*
4210  * rmdir_args(char *path)
4211  *
4212  * Remove a directory file.
4213  */
4214 int
4215 sys_rmdir(struct rmdir_args *uap)
4216 {
4217         struct nlookupdata nd;
4218         int error;
4219
4220         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4221         if (error == 0)
4222                 error = kern_rmdir(&nd);
4223         nlookup_done(&nd);
4224         return (error);
4225 }
4226
4227 int
4228 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4229                    enum uio_seg direction)
4230 {
4231         struct thread *td = curthread;
4232         struct proc *p = td->td_proc;
4233         struct vnode *vp;
4234         struct file *fp;
4235         struct uio auio;
4236         struct iovec aiov;
4237         off_t loff;
4238         int error, eofflag;
4239
4240         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4241                 return (error);
4242         if ((fp->f_flag & FREAD) == 0) {
4243                 error = EBADF;
4244                 goto done;
4245         }
4246         vp = (struct vnode *)fp->f_data;
4247         if (vp->v_type != VDIR) {
4248                 error = EINVAL;
4249                 goto done;
4250         }
4251         aiov.iov_base = buf;
4252         aiov.iov_len = count;
4253         auio.uio_iov = &aiov;
4254         auio.uio_iovcnt = 1;
4255         auio.uio_rw = UIO_READ;
4256         auio.uio_segflg = direction;
4257         auio.uio_td = td;
4258         auio.uio_resid = count;
4259         loff = auio.uio_offset = fp->f_offset;
4260         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4261         fp->f_offset = auio.uio_offset;
4262         if (error)
4263                 goto done;
4264
4265         /*
4266          * WARNING!  *basep may not be wide enough to accomodate the
4267          * seek offset.   XXX should we hack this to return the upper 32 bits
4268          * for offsets greater then 4G?
4269          */
4270         if (basep) {
4271                 *basep = (long)loff;
4272         }
4273         *res = count - auio.uio_resid;
4274 done:
4275         fdrop(fp);
4276         return (error);
4277 }
4278
4279 /*
4280  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4281  *
4282  * Read a block of directory entries in a file system independent format.
4283  */
4284 int
4285 sys_getdirentries(struct getdirentries_args *uap)
4286 {
4287         long base;
4288         int error;
4289
4290         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4291                                    &uap->sysmsg_result, UIO_USERSPACE);
4292
4293         if (error == 0 && uap->basep)
4294                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4295         return (error);
4296 }
4297
4298 /*
4299  * getdents_args(int fd, char *buf, size_t count)
4300  */
4301 int
4302 sys_getdents(struct getdents_args *uap)
4303 {
4304         int error;
4305
4306         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4307                                    &uap->sysmsg_result, UIO_USERSPACE);
4308
4309         return (error);
4310 }
4311
4312 /*
4313  * Set the mode mask for creation of filesystem nodes.
4314  *
4315  * umask(int newmask)
4316  */
4317 int
4318 sys_umask(struct umask_args *uap)
4319 {
4320         struct thread *td = curthread;
4321         struct proc *p = td->td_proc;
4322         struct filedesc *fdp;
4323
4324         fdp = p->p_fd;
4325         uap->sysmsg_result = fdp->fd_cmask;
4326         fdp->fd_cmask = uap->newmask & ALLPERMS;
4327         return (0);
4328 }
4329
4330 /*
4331  * revoke(char *path)
4332  *
4333  * Void all references to file by ripping underlying filesystem
4334  * away from vnode.
4335  */
4336 int
4337 sys_revoke(struct revoke_args *uap)
4338 {
4339         struct nlookupdata nd;
4340         struct vattr vattr;
4341         struct vnode *vp;
4342         struct ucred *cred;
4343         int error;
4344
4345         vp = NULL;
4346         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4347         if (error == 0)
4348                 error = nlookup(&nd);
4349         if (error == 0)
4350                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4351         cred = crhold(nd.nl_cred);
4352         nlookup_done(&nd);
4353         if (error == 0) {
4354                 if (error == 0)
4355                         error = VOP_GETATTR(vp, &vattr);
4356                 if (error == 0 && cred->cr_uid != vattr.va_uid)
4357                         error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4358                 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4359                         if (vcount(vp) > 0)
4360                                 error = vrevoke(vp, cred);
4361                 } else if (error == 0) {
4362                         error = vrevoke(vp, cred);
4363                 }
4364                 vrele(vp);
4365         }
4366         if (cred)
4367                 crfree(cred);
4368         return (error);
4369 }
4370
4371 /*
4372  * getfh_args(char *fname, fhandle_t *fhp)
4373  *
4374  * Get (NFS) file handle
4375  *
4376  * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4377  * mount.  This allows nullfs mounts to be explicitly exported. 
4378  *
4379  * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4380  *
4381  *          nullfs mounts of subdirectories are not safe.  That is, it will
4382  *          work, but you do not really have protection against access to
4383  *          the related parent directories.
4384  */
4385 int
4386 sys_getfh(struct getfh_args *uap)
4387 {
4388         struct thread *td = curthread;
4389         struct nlookupdata nd;
4390         fhandle_t fh;
4391         struct vnode *vp;
4392         struct mount *mp;
4393         int error;
4394
4395         /*
4396          * Must be super user
4397          */
4398         if ((error = priv_check(td, PRIV_ROOT)) != 0)
4399                 return (error);
4400
4401         vp = NULL;
4402         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4403         if (error == 0)
4404                 error = nlookup(&nd);
4405         if (error == 0)
4406                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4407         mp = nd.nl_nch.mount;
4408         nlookup_done(&nd);
4409         if (error == 0) {
4410                 bzero(&fh, sizeof(fh));
4411                 fh.fh_fsid = mp->mnt_stat.f_fsid;
4412                 error = VFS_VPTOFH(vp, &fh.fh_fid);
4413                 vput(vp);
4414                 if (error == 0)
4415                         error = copyout(&fh, uap->fhp, sizeof(fh));
4416         }
4417         return (error);
4418 }
4419
4420 /*
4421  * fhopen_args(const struct fhandle *u_fhp, int flags)
4422  *
4423  * syscall for the rpc.lockd to use to translate a NFS file handle into
4424  * an open descriptor.
4425  *
4426  * warning: do not remove the priv_check() call or this becomes one giant
4427  * security hole.
4428  */
4429 int
4430 sys_fhopen(struct fhopen_args *uap)
4431 {
4432         struct thread *td = curthread;
4433         struct filedesc *fdp = td->td_proc->p_fd;
4434         struct mount *mp;
4435         struct vnode *vp;
4436         struct fhandle fhp;
4437         struct vattr vat;
4438         struct vattr *vap = &vat;
4439         struct flock lf;
4440         int fmode, mode, error = 0, type;
4441         struct file *nfp; 
4442         struct file *fp;
4443         int indx;
4444
4445         /*
4446          * Must be super user
4447          */
4448         error = priv_check(td, PRIV_ROOT);
4449         if (error)
4450                 return (error);
4451
4452         fmode = FFLAGS(uap->flags);
4453
4454         /*
4455          * Why not allow a non-read/write open for our lockd?
4456          */
4457         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4458                 return (EINVAL);
4459         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4460         if (error)
4461                 return(error);
4462
4463         /*
4464          * Find the mount point
4465          */
4466         mp = vfs_getvfs(&fhp.fh_fsid);
4467         if (mp == NULL) {
4468                 error = ESTALE;
4469                 goto  done;
4470         }
4471         /* now give me my vnode, it gets returned to me locked */
4472         error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4473         if (error)
4474                 goto done;
4475         /*
4476          * from now on we have to make sure not
4477          * to forget about the vnode
4478          * any error that causes an abort must vput(vp) 
4479          * just set error = err and 'goto bad;'.
4480          */
4481
4482         /* 
4483          * from vn_open 
4484          */
4485         if (vp->v_type == VLNK) {
4486                 error = EMLINK;
4487                 goto bad;
4488         }
4489         if (vp->v_type == VSOCK) {
4490                 error = EOPNOTSUPP;
4491                 goto bad;
4492         }
4493         mode = 0;
4494         if (fmode & (FWRITE | O_TRUNC)) {
4495                 if (vp->v_type == VDIR) {
4496                         error = EISDIR;
4497                         goto bad;
4498                 }
4499                 error = vn_writechk(vp, NULL);
4500                 if (error)
4501                         goto bad;
4502                 mode |= VWRITE;
4503         }
4504         if (fmode & FREAD)
4505                 mode |= VREAD;
4506         if (mode) {
4507                 error = VOP_ACCESS(vp, mode, td->td_ucred);
4508                 if (error)
4509                         goto bad;
4510         }
4511         if (fmode & O_TRUNC) {
4512                 vn_unlock(vp);                          /* XXX */
4513                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
4514                 VATTR_NULL(vap);
4515                 vap->va_size = 0;
4516                 error = VOP_SETATTR(vp, vap, td->td_ucred);
4517                 if (error)
4518                         goto bad;
4519         }
4520
4521         /*
4522          * VOP_OPEN needs the file pointer so it can potentially override
4523          * it.
4524          *
4525          * WARNING! no f_nchandle will be associated when fhopen()ing a
4526          * directory.  XXX
4527          */
4528         if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4529                 goto bad;
4530         fp = nfp;
4531
4532         error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4533         if (error) {
4534                 /*
4535                  * setting f_ops this way prevents VOP_CLOSE from being
4536                  * called or fdrop() releasing the vp from v_data.   Since
4537                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
4538                  */
4539                 fp->f_ops = &badfileops;
4540                 fp->f_data = NULL;
4541                 goto bad_drop;
4542         }
4543
4544         /*
4545          * The fp is given its own reference, we still have our ref and lock.
4546          *
4547          * Assert that all regular files must be created with a VM object.
4548          */
4549         if (vp->v_type == VREG && vp->v_object == NULL) {
4550                 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4551                 goto bad_drop;
4552         }
4553
4554         /*
4555          * The open was successful.  Handle any locking requirements.
4556          */
4557         if (fmode & (O_EXLOCK | O_SHLOCK)) {
4558                 lf.l_whence = SEEK_SET;
4559                 lf.l_start = 0;
4560                 lf.l_len = 0;
4561                 if (fmode & O_EXLOCK)
4562                         lf.l_type = F_WRLCK;
4563                 else
4564                         lf.l_type = F_RDLCK;
4565                 if (fmode & FNONBLOCK)
4566                         type = 0;
4567                 else
4568                         type = F_WAIT;
4569                 vn_unlock(vp);
4570                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4571                         /*
4572                          * release our private reference.
4573                          */
4574                         fsetfd(fdp, NULL, indx);
4575                         fdrop(fp);
4576                         vrele(vp);
4577                         goto done;
4578                 }
4579                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4580                 atomic_set_int(&fp->f_flag, FHASLOCK);  /* race ok */
4581         }
4582
4583         /*
4584          * Clean up.  Associate the file pointer with the previously
4585          * reserved descriptor and return it.
4586          */
4587         vput(vp);
4588         if (uap->flags & O_CLOEXEC)
4589                 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4590         fsetfd(fdp, fp, indx);
4591         fdrop(fp);
4592         uap->sysmsg_result = indx;
4593         return (error);
4594
4595 bad_drop:
4596         fsetfd(fdp, NULL, indx);
4597         fdrop(fp);
4598 bad:
4599         vput(vp);
4600 done:
4601         return (error);
4602 }
4603
4604 /*
4605  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4606  */
4607 int
4608 sys_fhstat(struct fhstat_args *uap)
4609 {
4610         struct thread *td = curthread;
4611         struct stat sb;
4612         fhandle_t fh;
4613         struct mount *mp;
4614         struct vnode *vp;
4615         int error;
4616
4617         /*
4618          * Must be super user
4619          */
4620         error = priv_check(td, PRIV_ROOT);
4621         if (error)
4622                 return (error);
4623         
4624         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4625         if (error)
4626                 return (error);
4627
4628         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4629                 error = ESTALE;
4630         if (error == 0) {
4631                 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4632                         error = vn_stat(vp, &sb, td->td_ucred);
4633                         vput(vp);
4634                 }
4635         }
4636         if (error == 0)
4637                 error = copyout(&sb, uap->sb, sizeof(sb));
4638         return (error);
4639 }
4640
4641 /*
4642  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4643  */
4644 int
4645 sys_fhstatfs(struct fhstatfs_args *uap)
4646 {
4647         struct thread *td = curthread;
4648         struct proc *p = td->td_proc;
4649         struct statfs *sp;
4650         struct mount *mp;
4651         struct vnode *vp;
4652         struct statfs sb;
4653         char *fullpath, *freepath;
4654         fhandle_t fh;
4655         int error;
4656
4657         /*
4658          * Must be super user
4659          */
4660         if ((error = priv_check(td, PRIV_ROOT)))
4661                 return (error);
4662
4663         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4664                 return (error);
4665
4666         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4667                 error = ESTALE;
4668                 goto done;
4669         }
4670         if (p != NULL && !chroot_visible_mnt(mp, p)) {
4671                 error = ESTALE;
4672                 goto done;
4673         }
4674
4675         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4676                 goto done;
4677         mp = vp->v_mount;
4678         sp = &mp->mnt_stat;
4679         vput(vp);
4680         if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4681                 goto done;
4682
4683         error = mount_path(p, mp, &fullpath, &freepath);
4684         if (error)
4685                 goto done;
4686         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4687         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4688         kfree(freepath, M_TEMP);
4689
4690         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4691         if (priv_check(td, PRIV_ROOT)) {
4692                 bcopy(sp, &sb, sizeof(sb));
4693                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4694                 sp = &sb;
4695         }
4696         error = copyout(sp, uap->buf, sizeof(*sp));
4697 done:
4698         return (error);
4699 }
4700
4701 /*
4702  * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4703  */
4704 int
4705 sys_fhstatvfs(struct fhstatvfs_args *uap)
4706 {
4707         struct thread *td = curthread;
4708         struct proc *p = td->td_proc;
4709         struct statvfs *sp;
4710         struct mount *mp;
4711         struct vnode *vp;
4712         fhandle_t fh;
4713         int error;
4714
4715         /*
4716          * Must be super user
4717          */
4718         if ((error = priv_check(td, PRIV_ROOT)))
4719                 return (error);
4720
4721         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4722                 return (error);
4723
4724         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4725                 error = ESTALE;
4726                 goto done;
4727         }
4728         if (p != NULL && !chroot_visible_mnt(mp, p)) {
4729                 error = ESTALE;
4730                 goto done;
4731         }
4732
4733         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4734                 goto done;
4735         mp = vp->v_mount;
4736         sp = &mp->mnt_vstat;
4737         vput(vp);
4738         if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4739                 goto done;
4740
4741         sp->f_flag = 0;
4742         if (mp->mnt_flag & MNT_RDONLY)
4743                 sp->f_flag |= ST_RDONLY;
4744         if (mp->mnt_flag & MNT_NOSUID)
4745                 sp->f_flag |= ST_NOSUID;
4746         error = copyout(sp, uap->buf, sizeof(*sp));
4747 done:
4748         return (error);
4749 }
4750
4751
4752 /*
4753  * Syscall to push extended attribute configuration information into the
4754  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
4755  * a command (int cmd), and attribute name and misc data.  For now, the
4756  * attribute name is left in userspace for consumption by the VFS_op.
4757  * It will probably be changed to be copied into sysspace by the
4758  * syscall in the future, once issues with various consumers of the
4759  * attribute code have raised their hands.
4760  *
4761  * Currently this is used only by UFS Extended Attributes.
4762  */
4763 int
4764 sys_extattrctl(struct extattrctl_args *uap)
4765 {
4766         struct nlookupdata nd;
4767         struct vnode *vp;
4768         char attrname[EXTATTR_MAXNAMELEN];
4769         int error;
4770         size_t size;
4771
4772         attrname[0] = 0;
4773         vp = NULL;
4774         error = 0;
4775
4776         if (error == 0 && uap->filename) {
4777                 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4778                                      NLC_FOLLOW);
4779                 if (error == 0)
4780                         error = nlookup(&nd);
4781                 if (error == 0)
4782                         error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4783                 nlookup_done(&nd);
4784         }
4785
4786         if (error == 0 && uap->attrname) {
4787                 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4788                                   &size);
4789         }
4790
4791         if (error == 0) {
4792                 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4793                 if (error == 0)
4794                         error = nlookup(&nd);
4795                 if (error == 0)
4796                         error = ncp_writechk(&nd.nl_nch);
4797                 if (error == 0) {
4798                         error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4799                                                uap->attrnamespace,
4800                                                uap->attrname, nd.nl_cred);
4801                 }
4802                 nlookup_done(&nd);
4803         }
4804
4805         return (error);
4806 }
4807
4808 /*
4809  * Syscall to get a named extended attribute on a file or directory.
4810  */
4811 int
4812 sys_extattr_set_file(struct extattr_set_file_args *uap)
4813 {
4814         char attrname[EXTATTR_MAXNAMELEN];
4815         struct nlookupdata nd;
4816         struct vnode *vp;
4817         struct uio auio;
4818         struct iovec aiov;
4819         int error;
4820
4821         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4822         if (error)
4823                 return (error);
4824
4825         vp = NULL;
4826
4827         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4828         if (error == 0)
4829                 error = nlookup(&nd);
4830         if (error == 0)
4831                 error = ncp_writechk(&nd.nl_nch);
4832         if (error == 0)
4833                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4834         if (error) {
4835                 nlookup_done(&nd);
4836                 return (error);
4837         }
4838
4839         bzero(&auio, sizeof(auio));
4840         aiov.iov_base = uap->data;
4841         aiov.iov_len = uap->nbytes;
4842         auio.uio_iov = &aiov;
4843         auio.uio_iovcnt = 1;
4844         auio.uio_offset = 0;
4845         auio.uio_resid = uap->nbytes;
4846         auio.uio_rw = UIO_WRITE;
4847         auio.uio_td = curthread;
4848
4849         error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4850                                &auio, nd.nl_cred);
4851
4852         vput(vp);
4853         nlookup_done(&nd);
4854         return (error);
4855 }
4856
4857 /*
4858  * Syscall to get a named extended attribute on a file or directory.
4859  */
4860 int
4861 sys_extattr_get_file(struct extattr_get_file_args *uap)
4862 {
4863         char attrname[EXTATTR_MAXNAMELEN];
4864         struct nlookupdata nd;
4865         struct uio auio;
4866         struct iovec aiov;
4867         struct vnode *vp;
4868         int error;
4869
4870         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4871         if (error)
4872                 return (error);
4873
4874         vp = NULL;
4875
4876         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4877         if (error == 0)
4878                 error = nlookup(&nd);
4879         if (error == 0)
4880                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4881         if (error) {
4882                 nlookup_done(&nd);
4883                 return (error);
4884         }
4885
4886         bzero(&auio, sizeof(auio));
4887         aiov.iov_base = uap->data;
4888         aiov.iov_len = uap->nbytes;
4889         auio.uio_iov = &aiov;
4890         auio.uio_iovcnt = 1;
4891         auio.uio_offset = 0;
4892         auio.uio_resid = uap->nbytes;
4893         auio.uio_rw = UIO_READ;
4894         auio.uio_td = curthread;
4895
4896         error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4897                                 &auio, nd.nl_cred);
4898         uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4899
4900         vput(vp);
4901         nlookup_done(&nd);
4902         return(error);
4903 }
4904
4905 /*
4906  * Syscall to delete a named extended attribute from a file or directory.
4907  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
4908  */
4909 int
4910 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4911 {
4912         char attrname[EXTATTR_MAXNAMELEN];
4913         struct nlookupdata nd;
4914         struct vnode *vp;
4915         int error;
4916
4917         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4918         if (error)
4919                 return(error);
4920
4921         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4922         if (error == 0)
4923                 error = nlookup(&nd);
4924         if (error == 0)
4925                 error = ncp_writechk(&nd.nl_nch);
4926         if (error == 0) {
4927                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4928                 if (error == 0) {
4929                         error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4930                                                attrname, NULL, nd.nl_cred);
4931                         vput(vp);
4932                 }
4933         }
4934         nlookup_done(&nd);
4935         return(error);
4936 }
4937
4938 /*
4939  * Determine if the mount is visible to the process.
4940  */
4941 static int
4942 chroot_visible_mnt(struct mount *mp, struct proc *p)
4943 {
4944         struct nchandle nch;
4945
4946         /*
4947          * Traverse from the mount point upwards.  If we hit the process
4948          * root then the mount point is visible to the process.
4949          */
4950         nch = mp->mnt_ncmountpt;
4951         while (nch.ncp) {
4952                 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4953                     nch.ncp == p->p_fd->fd_nrdir.ncp) {
4954                         return(1);
4955                 }
4956                 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4957                         nch = nch.mount->mnt_ncmounton;
4958                 } else {
4959                         nch.ncp = nch.ncp->nc_parent;
4960                 }
4961         }
4962
4963         /*
4964          * If the mount point is not visible to the process, but the
4965          * process root is in a subdirectory of the mount, return
4966          * TRUE anyway.
4967          */
4968         if (p->p_fd->fd_nrdir.mount == mp)
4969                 return(1);
4970
4971         return(0);
4972 }
4973