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