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