mount - Finish refactoring mount -v and MOUNTCTL_MOUNTFLAGS
[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, 0);
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 ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
912             (error = priv_check(td, PRIV_ROOT)) != 0)
913                 return (error);
914
915         /*
916          * Argument length checks
917          */
918         if (uap->ctllen < 0 || uap->ctllen > 1024)
919                 return (EINVAL);
920         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
921                 return (EINVAL);
922         if (uap->path == NULL)
923                 return (EINVAL);
924
925         /*
926          * Allocate the necessary buffers and copyin data
927          */
928         path = objcache_get(namei_oc, M_WAITOK);
929         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
930         if (error)
931                 goto done;
932
933         if (uap->ctllen) {
934                 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
935                 error = copyin(uap->ctl, ctl, uap->ctllen);
936                 if (error)
937                         goto done;
938         }
939         if (uap->buflen)
940                 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
941
942         /*
943          * Validate the descriptor
944          */
945         if (uap->fd >= 0) {
946                 fp = holdfp(p->p_fd, uap->fd, -1);
947                 if (fp == NULL) {
948                         error = EBADF;
949                         goto done;
950                 }
951         } else {
952                 fp = NULL;
953         }
954
955         /*
956          * Execute the internal kernel function and clean up.
957          */
958         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
959         if (fp)
960                 fdrop(fp);
961         if (error == 0 && uap->sysmsg_result > 0)
962                 error = copyout(buf, uap->buf, uap->sysmsg_result);
963 done:
964         if (path)
965                 objcache_put(namei_oc, path);
966         if (ctl)
967                 kfree(ctl, M_TEMP);
968         if (buf)
969                 kfree(buf, M_TEMP);
970         return (error);
971 }
972
973 /*
974  * Execute a mount control operation by resolving the path to a mount point
975  * and calling vop_mountctl().  
976  *
977  * Use the mount point from the nch instead of the vnode so nullfs mounts
978  * can properly spike the VOP.
979  */
980 int
981 kern_mountctl(const char *path, int op, struct file *fp, 
982                 const void *ctl, int ctllen, 
983                 void *buf, int buflen, int *res)
984 {
985         struct vnode *vp;
986         struct mount *mp;
987         struct nlookupdata nd;
988         int error;
989
990         *res = 0;
991         vp = NULL;
992         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
993         if (error == 0)
994                 error = nlookup(&nd);
995         if (error == 0)
996                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
997         mp = nd.nl_nch.mount;
998         nlookup_done(&nd);
999         if (error)
1000                 return (error);
1001
1002         /*
1003          * Must be the root of the filesystem
1004          */
1005         if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1006                 vput(vp);
1007                 return (EINVAL);
1008         }
1009         error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, 
1010                                 buf, buflen, res);
1011         vput(vp);
1012         return (error);
1013 }
1014
1015 int
1016 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1017 {
1018         struct thread *td = curthread;
1019         struct proc *p = td->td_proc;
1020         struct mount *mp;
1021         struct statfs *sp;
1022         char *fullpath, *freepath;
1023         int error;
1024
1025         if ((error = nlookup(nd)) != 0)
1026                 return (error);
1027         mp = nd->nl_nch.mount;
1028         sp = &mp->mnt_stat;
1029         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1030                 return (error);
1031
1032         error = mount_path(p, mp, &fullpath, &freepath);
1033         if (error)
1034                 return(error);
1035         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1036         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1037         kfree(freepath, M_TEMP);
1038
1039         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1040         bcopy(sp, buf, sizeof(*buf));
1041         /* Only root should have access to the fsid's. */
1042         if (priv_check(td, PRIV_ROOT))
1043                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1044         return (0);
1045 }
1046
1047 /*
1048  * statfs_args(char *path, struct statfs *buf)
1049  *
1050  * Get filesystem statistics.
1051  */
1052 int
1053 sys_statfs(struct statfs_args *uap)
1054 {
1055         struct nlookupdata nd;
1056         struct statfs buf;
1057         int error;
1058
1059         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1060         if (error == 0)
1061                 error = kern_statfs(&nd, &buf);
1062         nlookup_done(&nd);
1063         if (error == 0)
1064                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1065         return (error);
1066 }
1067
1068 int
1069 kern_fstatfs(int fd, struct statfs *buf)
1070 {
1071         struct thread *td = curthread;
1072         struct proc *p = td->td_proc;
1073         struct file *fp;
1074         struct mount *mp;
1075         struct statfs *sp;
1076         char *fullpath, *freepath;
1077         int error;
1078
1079         KKASSERT(p);
1080         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1081                 return (error);
1082         mp = ((struct vnode *)fp->f_data)->v_mount;
1083         if (mp == NULL) {
1084                 error = EBADF;
1085                 goto done;
1086         }
1087         if (fp->f_cred == NULL) {
1088                 error = EINVAL;
1089                 goto done;
1090         }
1091         sp = &mp->mnt_stat;
1092         if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1093                 goto done;
1094
1095         if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1096                 goto done;
1097         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1098         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1099         kfree(freepath, M_TEMP);
1100
1101         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1102         bcopy(sp, buf, sizeof(*buf));
1103
1104         /* Only root should have access to the fsid's. */
1105         if (priv_check(td, PRIV_ROOT))
1106                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1107         error = 0;
1108 done:
1109         fdrop(fp);
1110         return (error);
1111 }
1112
1113 /*
1114  * fstatfs_args(int fd, struct statfs *buf)
1115  *
1116  * Get filesystem statistics.
1117  */
1118 int
1119 sys_fstatfs(struct fstatfs_args *uap)
1120 {
1121         struct statfs buf;
1122         int error;
1123
1124         error = kern_fstatfs(uap->fd, &buf);
1125
1126         if (error == 0)
1127                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1128         return (error);
1129 }
1130
1131 int
1132 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1133 {
1134         struct mount *mp;
1135         struct statvfs *sp;
1136         int error;
1137
1138         if ((error = nlookup(nd)) != 0)
1139                 return (error);
1140         mp = nd->nl_nch.mount;
1141         sp = &mp->mnt_vstat;
1142         if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1143                 return (error);
1144
1145         sp->f_flag = 0;
1146         if (mp->mnt_flag & MNT_RDONLY)
1147                 sp->f_flag |= ST_RDONLY;
1148         if (mp->mnt_flag & MNT_NOSUID)
1149                 sp->f_flag |= ST_NOSUID;
1150         bcopy(sp, buf, sizeof(*buf));
1151         return (0);
1152 }
1153
1154 /*
1155  * statfs_args(char *path, struct statfs *buf)
1156  *
1157  * Get filesystem statistics.
1158  */
1159 int
1160 sys_statvfs(struct statvfs_args *uap)
1161 {
1162         struct nlookupdata nd;
1163         struct statvfs buf;
1164         int error;
1165
1166         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1167         if (error == 0)
1168                 error = kern_statvfs(&nd, &buf);
1169         nlookup_done(&nd);
1170         if (error == 0)
1171                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1172         return (error);
1173 }
1174
1175 int
1176 kern_fstatvfs(int fd, struct statvfs *buf)
1177 {
1178         struct thread *td = curthread;
1179         struct proc *p = td->td_proc;
1180         struct file *fp;
1181         struct mount *mp;
1182         struct statvfs *sp;
1183         int error;
1184
1185         KKASSERT(p);
1186         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1187                 return (error);
1188         mp = ((struct vnode *)fp->f_data)->v_mount;
1189         if (mp == NULL) {
1190                 error = EBADF;
1191                 goto done;
1192         }
1193         if (fp->f_cred == NULL) {
1194                 error = EINVAL;
1195                 goto done;
1196         }
1197         sp = &mp->mnt_vstat;
1198         if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1199                 goto done;
1200
1201         sp->f_flag = 0;
1202         if (mp->mnt_flag & MNT_RDONLY)
1203                 sp->f_flag |= ST_RDONLY;
1204         if (mp->mnt_flag & MNT_NOSUID)
1205                 sp->f_flag |= ST_NOSUID;
1206
1207         bcopy(sp, buf, sizeof(*buf));
1208         error = 0;
1209 done:
1210         fdrop(fp);
1211         return (error);
1212 }
1213
1214 /*
1215  * fstatfs_args(int fd, struct statfs *buf)
1216  *
1217  * Get filesystem statistics.
1218  */
1219 int
1220 sys_fstatvfs(struct fstatvfs_args *uap)
1221 {
1222         struct statvfs buf;
1223         int error;
1224
1225         error = kern_fstatvfs(uap->fd, &buf);
1226
1227         if (error == 0)
1228                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1229         return (error);
1230 }
1231
1232 /*
1233  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1234  *
1235  * Get statistics on all filesystems.
1236  */
1237
1238 struct getfsstat_info {
1239         struct statfs *sfsp;
1240         long count;
1241         long maxcount;
1242         int error;
1243         int flags;
1244         struct proc *p;
1245 };
1246
1247 static int getfsstat_callback(struct mount *, void *);
1248
1249 /* ARGSUSED */
1250 int
1251 sys_getfsstat(struct getfsstat_args *uap)
1252 {
1253         struct thread *td = curthread;
1254         struct proc *p = td->td_proc;
1255         struct getfsstat_info info;
1256
1257         bzero(&info, sizeof(info));
1258
1259         info.maxcount = uap->bufsize / sizeof(struct statfs);
1260         info.sfsp = uap->buf;
1261         info.count = 0;
1262         info.flags = uap->flags;
1263         info.p = p;
1264
1265         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1266         if (info.sfsp && info.count > info.maxcount)
1267                 uap->sysmsg_result = info.maxcount;
1268         else
1269                 uap->sysmsg_result = info.count;
1270         return (info.error);
1271 }
1272
1273 static int
1274 getfsstat_callback(struct mount *mp, void *data)
1275 {
1276         struct getfsstat_info *info = data;
1277         struct statfs *sp;
1278         char *freepath;
1279         char *fullpath;
1280         int error;
1281
1282         if (info->sfsp && info->count < info->maxcount) {
1283                 if (info->p && !chroot_visible_mnt(mp, info->p))
1284                         return(0);
1285                 sp = &mp->mnt_stat;
1286
1287                 /*
1288                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1289                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1290                  * overrides MNT_WAIT.
1291                  */
1292                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1293                     (info->flags & MNT_WAIT)) &&
1294                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1295                         return(0);
1296                 }
1297                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1298
1299                 error = mount_path(info->p, mp, &fullpath, &freepath);
1300                 if (error) {
1301                         info->error = error;
1302                         return(-1);
1303                 }
1304                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1305                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1306                 kfree(freepath, M_TEMP);
1307
1308                 error = copyout(sp, info->sfsp, sizeof(*sp));
1309                 if (error) {
1310                         info->error = error;
1311                         return (-1);
1312                 }
1313                 ++info->sfsp;
1314         }
1315         info->count++;
1316         return(0);
1317 }
1318
1319 /*
1320  * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1321                    long bufsize, int flags)
1322  *
1323  * Get statistics on all filesystems.
1324  */
1325
1326 struct getvfsstat_info {
1327         struct statfs *sfsp;
1328         struct statvfs *vsfsp;
1329         long count;
1330         long maxcount;
1331         int error;
1332         int flags;
1333         struct proc *p;
1334 };
1335
1336 static int getvfsstat_callback(struct mount *, void *);
1337
1338 /* ARGSUSED */
1339 int
1340 sys_getvfsstat(struct getvfsstat_args *uap)
1341 {
1342         struct thread *td = curthread;
1343         struct proc *p = td->td_proc;
1344         struct getvfsstat_info info;
1345
1346         bzero(&info, sizeof(info));
1347
1348         info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1349         info.sfsp = uap->buf;
1350         info.vsfsp = uap->vbuf;
1351         info.count = 0;
1352         info.flags = uap->flags;
1353         info.p = p;
1354
1355         mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1356         if (info.vsfsp && info.count > info.maxcount)
1357                 uap->sysmsg_result = info.maxcount;
1358         else
1359                 uap->sysmsg_result = info.count;
1360         return (info.error);
1361 }
1362
1363 static int
1364 getvfsstat_callback(struct mount *mp, void *data)
1365 {
1366         struct getvfsstat_info *info = data;
1367         struct statfs *sp;
1368         struct statvfs *vsp;
1369         char *freepath;
1370         char *fullpath;
1371         int error;
1372
1373         if (info->vsfsp && info->count < info->maxcount) {
1374                 if (info->p && !chroot_visible_mnt(mp, info->p))
1375                         return(0);
1376                 sp = &mp->mnt_stat;
1377                 vsp = &mp->mnt_vstat;
1378
1379                 /*
1380                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382                  * overrides MNT_WAIT.
1383                  */
1384                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385                     (info->flags & MNT_WAIT)) &&
1386                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1387                         return(0);
1388                 }
1389                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1390
1391                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1392                     (info->flags & MNT_WAIT)) &&
1393                     (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1394                         return(0);
1395                 }
1396                 vsp->f_flag = 0;
1397                 if (mp->mnt_flag & MNT_RDONLY)
1398                         vsp->f_flag |= ST_RDONLY;
1399                 if (mp->mnt_flag & MNT_NOSUID)
1400                         vsp->f_flag |= ST_NOSUID;
1401
1402                 error = mount_path(info->p, mp, &fullpath, &freepath);
1403                 if (error) {
1404                         info->error = error;
1405                         return(-1);
1406                 }
1407                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1408                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1409                 kfree(freepath, M_TEMP);
1410
1411                 error = copyout(sp, info->sfsp, sizeof(*sp));
1412                 if (error == 0)
1413                         error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1414                 if (error) {
1415                         info->error = error;
1416                         return (-1);
1417                 }
1418                 ++info->sfsp;
1419                 ++info->vsfsp;
1420         }
1421         info->count++;
1422         return(0);
1423 }
1424
1425
1426 /*
1427  * fchdir_args(int fd)
1428  *
1429  * Change current working directory to a given file descriptor.
1430  */
1431 /* ARGSUSED */
1432 int
1433 sys_fchdir(struct fchdir_args *uap)
1434 {
1435         struct thread *td = curthread;
1436         struct proc *p = td->td_proc;
1437         struct filedesc *fdp = p->p_fd;
1438         struct vnode *vp, *ovp;
1439         struct mount *mp;
1440         struct file *fp;
1441         struct nchandle nch, onch, tnch;
1442         int error;
1443
1444         if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1445                 return (error);
1446         vp = (struct vnode *)fp->f_data;
1447         vref(vp);
1448         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1449         if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1450                 error = ENOTDIR;
1451         else
1452                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1453         if (error) {
1454                 vput(vp);
1455                 fdrop(fp);
1456                 return (error);
1457         }
1458         cache_copy(&fp->f_nchandle, &nch);
1459
1460         /*
1461          * If the ncp has become a mount point, traverse through
1462          * the mount point.
1463          */
1464
1465         while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1466                (mp = cache_findmount(&nch)) != NULL
1467         ) {
1468                 error = nlookup_mp(mp, &tnch);
1469                 if (error == 0) {
1470                         cache_unlock(&tnch);    /* leave ref intact */
1471                         vput(vp);
1472                         vp = tnch.ncp->nc_vp;
1473                         error = vget(vp, LK_SHARED);
1474                         KKASSERT(error == 0);
1475                         cache_drop(&nch);
1476                         nch = tnch;
1477                 }
1478         }
1479         if (error == 0) {
1480                 ovp = fdp->fd_cdir;
1481                 onch = fdp->fd_ncdir;
1482                 vn_unlock(vp);          /* leave ref intact */
1483                 fdp->fd_cdir = vp;
1484                 fdp->fd_ncdir = nch;
1485                 cache_drop(&onch);
1486                 vrele(ovp);
1487         } else {
1488                 cache_drop(&nch);
1489                 vput(vp);
1490         }
1491         fdrop(fp);
1492         return (error);
1493 }
1494
1495 int
1496 kern_chdir(struct nlookupdata *nd)
1497 {
1498         struct thread *td = curthread;
1499         struct proc *p = td->td_proc;
1500         struct filedesc *fdp = p->p_fd;
1501         struct vnode *vp, *ovp;
1502         struct nchandle onch;
1503         int error;
1504
1505         if ((error = nlookup(nd)) != 0)
1506                 return (error);
1507         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1508                 return (ENOENT);
1509         if ((error = vget(vp, LK_SHARED)) != 0)
1510                 return (error);
1511
1512         error = checkvp_chdir(vp, td);
1513         vn_unlock(vp);
1514         if (error == 0) {
1515                 ovp = fdp->fd_cdir;
1516                 onch = fdp->fd_ncdir;
1517                 cache_unlock(&nd->nl_nch);      /* leave reference intact */
1518                 fdp->fd_ncdir = nd->nl_nch;
1519                 fdp->fd_cdir = vp;
1520                 cache_drop(&onch);
1521                 vrele(ovp);
1522                 cache_zero(&nd->nl_nch);
1523         } else {
1524                 vrele(vp);
1525         }
1526         return (error);
1527 }
1528
1529 /*
1530  * chdir_args(char *path)
1531  *
1532  * Change current working directory (``.'').
1533  */
1534 int
1535 sys_chdir(struct chdir_args *uap)
1536 {
1537         struct nlookupdata nd;
1538         int error;
1539
1540         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1541         if (error == 0)
1542                 error = kern_chdir(&nd);
1543         nlookup_done(&nd);
1544         return (error);
1545 }
1546
1547 /*
1548  * Helper function for raised chroot(2) security function:  Refuse if
1549  * any filedescriptors are open directories.
1550  */
1551 static int
1552 chroot_refuse_vdir_fds(struct filedesc *fdp)
1553 {
1554         struct vnode *vp;
1555         struct file *fp;
1556         int error;
1557         int fd;
1558
1559         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1560                 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1561                         continue;
1562                 vp = (struct vnode *)fp->f_data;
1563                 if (vp->v_type != VDIR) {
1564                         fdrop(fp);
1565                         continue;
1566                 }
1567                 fdrop(fp);
1568                 return(EPERM);
1569         }
1570         return (0);
1571 }
1572
1573 /*
1574  * This sysctl determines if we will allow a process to chroot(2) if it
1575  * has a directory open:
1576  *      0: disallowed for all processes.
1577  *      1: allowed for processes that were not already chroot(2)'ed.
1578  *      2: allowed for all processes.
1579  */
1580
1581 static int chroot_allow_open_directories = 1;
1582
1583 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1584      &chroot_allow_open_directories, 0, "");
1585
1586 /*
1587  * chroot to the specified namecache entry.  We obtain the vp from the
1588  * namecache data.  The passed ncp must be locked and referenced and will
1589  * remain locked and referenced on return.
1590  */
1591 int
1592 kern_chroot(struct nchandle *nch)
1593 {
1594         struct thread *td = curthread;
1595         struct proc *p = td->td_proc;
1596         struct filedesc *fdp = p->p_fd;
1597         struct vnode *vp;
1598         int error;
1599
1600         /*
1601          * Only privileged user can chroot
1602          */
1603         error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1604         if (error)
1605                 return (error);
1606
1607         /*
1608          * Disallow open directory descriptors (fchdir() breakouts).
1609          */
1610         if (chroot_allow_open_directories == 0 ||
1611            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1612                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1613                         return (error);
1614         }
1615         if ((vp = nch->ncp->nc_vp) == NULL)
1616                 return (ENOENT);
1617
1618         if ((error = vget(vp, LK_SHARED)) != 0)
1619                 return (error);
1620
1621         /*
1622          * Check the validity of vp as a directory to change to and 
1623          * associate it with rdir/jdir.
1624          */
1625         error = checkvp_chdir(vp, td);
1626         vn_unlock(vp);                  /* leave reference intact */
1627         if (error == 0) {
1628                 vrele(fdp->fd_rdir);
1629                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1630                 cache_drop(&fdp->fd_nrdir);
1631                 cache_copy(nch, &fdp->fd_nrdir);
1632                 if (fdp->fd_jdir == NULL) {
1633                         fdp->fd_jdir = vp;
1634                         vref(fdp->fd_jdir);
1635                         cache_copy(nch, &fdp->fd_njdir);
1636                 }
1637         } else {
1638                 vrele(vp);
1639         }
1640         return (error);
1641 }
1642
1643 /*
1644  * chroot_args(char *path)
1645  *
1646  * Change notion of root (``/'') directory.
1647  */
1648 /* ARGSUSED */
1649 int
1650 sys_chroot(struct chroot_args *uap)
1651 {
1652         struct thread *td = curthread;
1653         struct nlookupdata nd;
1654         int error;
1655
1656         KKASSERT(td->td_proc);
1657         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1658         if (error) {
1659                 nlookup_done(&nd);
1660                 return(error);
1661         }
1662         nd.nl_flags |= NLC_EXEC;
1663         error = nlookup(&nd);
1664         if (error == 0)
1665                 error = kern_chroot(&nd.nl_nch);
1666         nlookup_done(&nd);
1667         return(error);
1668 }
1669
1670 /*
1671  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1672  * determine whether it is legal to chdir to the vnode.  The vnode's state
1673  * is not changed by this call.
1674  */
1675 int
1676 checkvp_chdir(struct vnode *vp, struct thread *td)
1677 {
1678         int error;
1679
1680         if (vp->v_type != VDIR)
1681                 error = ENOTDIR;
1682         else
1683                 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1684         return (error);
1685 }
1686
1687 int
1688 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1689 {
1690         struct thread *td = curthread;
1691         struct proc *p = td->td_proc;
1692         struct lwp *lp = td->td_lwp;
1693         struct filedesc *fdp = p->p_fd;
1694         int cmode, flags;
1695         struct file *nfp;
1696         struct file *fp;
1697         struct vnode *vp;
1698         int type, indx, error;
1699         struct flock lf;
1700
1701         if ((oflags & O_ACCMODE) == O_ACCMODE)
1702                 return (EINVAL);
1703         flags = FFLAGS(oflags);
1704         error = falloc(p, &nfp, NULL);
1705         if (error)
1706                 return (error);
1707         fp = nfp;
1708         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1709
1710         /*
1711          * XXX p_dupfd is a real mess.  It allows a device to return a
1712          * file descriptor to be duplicated rather then doing the open
1713          * itself.
1714          */
1715         lp->lwp_dupfd = -1;
1716
1717         /*
1718          * Call vn_open() to do the lookup and assign the vnode to the 
1719          * file pointer.  vn_open() does not change the ref count on fp
1720          * and the vnode, on success, will be inherited by the file pointer
1721          * and unlocked.
1722          */
1723         nd->nl_flags |= NLC_LOCKVP;
1724         error = vn_open(nd, fp, flags, cmode);
1725         nlookup_done(nd);
1726         if (error) {
1727                 /*
1728                  * handle special fdopen() case.  bleh.  dupfdopen() is
1729                  * responsible for dropping the old contents of ofiles[indx]
1730                  * if it succeeds.
1731                  *
1732                  * Note that fsetfd() will add a ref to fp which represents
1733                  * the fd_files[] assignment.  We must still drop our
1734                  * reference.
1735                  */
1736                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1737                         if (fdalloc(p, 0, &indx) == 0) {
1738                                 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1739                                 if (error == 0) {
1740                                         *res = indx;
1741                                         fdrop(fp);      /* our ref */
1742                                         return (0);
1743                                 }
1744                                 fsetfd(p, NULL, indx);
1745                         }
1746                 }
1747                 fdrop(fp);      /* our ref */
1748                 if (error == ERESTART)
1749                         error = EINTR;
1750                 return (error);
1751         }
1752
1753         /*
1754          * ref the vnode for ourselves so it can't be ripped out from under
1755          * is.  XXX need an ND flag to request that the vnode be returned
1756          * anyway.
1757          *
1758          * Reserve a file descriptor but do not assign it until the open
1759          * succeeds.
1760          */
1761         vp = (struct vnode *)fp->f_data;
1762         vref(vp);
1763         if ((error = fdalloc(p, 0, &indx)) != 0) {
1764                 fdrop(fp);
1765                 vrele(vp);
1766                 return (error);
1767         }
1768
1769         /*
1770          * If no error occurs the vp will have been assigned to the file
1771          * pointer.
1772          */
1773         lp->lwp_dupfd = 0;
1774
1775         if (flags & (O_EXLOCK | O_SHLOCK)) {
1776                 lf.l_whence = SEEK_SET;
1777                 lf.l_start = 0;
1778                 lf.l_len = 0;
1779                 if (flags & O_EXLOCK)
1780                         lf.l_type = F_WRLCK;
1781                 else
1782                         lf.l_type = F_RDLCK;
1783                 if (flags & FNONBLOCK)
1784                         type = 0;
1785                 else
1786                         type = F_WAIT;
1787
1788                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1789                         /*
1790                          * lock request failed.  Clean up the reserved
1791                          * descriptor.
1792                          */
1793                         vrele(vp);
1794                         fsetfd(p, NULL, indx);
1795                         fdrop(fp);
1796                         return (error);
1797                 }
1798                 fp->f_flag |= FHASLOCK;
1799         }
1800 #if 0
1801         /*
1802          * Assert that all regular file vnodes were created with a object.
1803          */
1804         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1805                 ("open: regular file has no backing object after vn_open"));
1806 #endif
1807
1808         vrele(vp);
1809
1810         /*
1811          * release our private reference, leaving the one associated with the
1812          * descriptor table intact.
1813          */
1814         fsetfd(p, fp, indx);
1815         fdrop(fp);
1816         *res = indx;
1817         return (0);
1818 }
1819
1820 /*
1821  * open_args(char *path, int flags, int mode)
1822  *
1823  * Check permissions, allocate an open file structure,
1824  * and call the device open routine if any.
1825  */
1826 int
1827 sys_open(struct open_args *uap)
1828 {
1829         struct nlookupdata nd;
1830         int error;
1831
1832         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1833         if (error == 0) {
1834                 error = kern_open(&nd, uap->flags,
1835                                     uap->mode, &uap->sysmsg_result);
1836         }
1837         nlookup_done(&nd);
1838         return (error);
1839 }
1840
1841 /*
1842  * openat_args(int fd, char *path, int flags, int mode)
1843  */
1844 int
1845 sys_openat(struct openat_args *uap)
1846 {
1847         struct nlookupdata nd;
1848         int error;
1849         struct file *fp;
1850
1851         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1852         if (error == 0) {
1853                 error = kern_open(&nd, uap->flags, uap->mode, 
1854                                         &uap->sysmsg_result);
1855         }
1856         nlookup_done_at(&nd, fp);
1857         return (error);
1858 }
1859
1860 int
1861 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1862 {
1863         struct thread *td = curthread;
1864         struct proc *p = td->td_proc;
1865         struct vnode *vp;
1866         struct vattr vattr;
1867         int error;
1868         int whiteout = 0;
1869
1870         KKASSERT(p);
1871
1872         VATTR_NULL(&vattr);
1873         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1874         vattr.va_rmajor = rmajor;
1875         vattr.va_rminor = rminor;
1876
1877         switch (mode & S_IFMT) {
1878         case S_IFMT:    /* used by badsect to flag bad sectors */
1879                 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1880                 vattr.va_type = VBAD;
1881                 break;
1882         case S_IFCHR:
1883                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1884                 vattr.va_type = VCHR;
1885                 break;
1886         case S_IFBLK:
1887                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1888                 vattr.va_type = VBLK;
1889                 break;
1890         case S_IFWHT:
1891                 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1892                 whiteout = 1;
1893                 break;
1894         case S_IFDIR:   /* special directories support for HAMMER */
1895                 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1896                 vattr.va_type = VDIR;
1897                 break;
1898         default:
1899                 error = EINVAL;
1900                 break;
1901         }
1902
1903         if (error)
1904                 return (error);
1905
1906         bwillinode(1);
1907         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1908         if ((error = nlookup(nd)) != 0)
1909                 return (error);
1910         if (nd->nl_nch.ncp->nc_vp)
1911                 return (EEXIST);
1912         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1913                 return (error);
1914
1915         if (whiteout) {
1916                 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1917                                       nd->nl_cred, NAMEI_CREATE);
1918         } else {
1919                 vp = NULL;
1920                 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1921                                    &vp, nd->nl_cred, &vattr);
1922                 if (error == 0)
1923                         vput(vp);
1924         }
1925         return (error);
1926 }
1927
1928 /*
1929  * mknod_args(char *path, int mode, int dev)
1930  *
1931  * Create a special file.
1932  */
1933 int
1934 sys_mknod(struct mknod_args *uap)
1935 {
1936         struct nlookupdata nd;
1937         int error;
1938
1939         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1940         if (error == 0) {
1941                 error = kern_mknod(&nd, uap->mode,
1942                                    umajor(uap->dev), uminor(uap->dev));
1943         }
1944         nlookup_done(&nd);
1945         return (error);
1946 }
1947
1948 int
1949 kern_mkfifo(struct nlookupdata *nd, int mode)
1950 {
1951         struct thread *td = curthread;
1952         struct proc *p = td->td_proc;
1953         struct vattr vattr;
1954         struct vnode *vp;
1955         int error;
1956
1957         bwillinode(1);
1958
1959         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1960         if ((error = nlookup(nd)) != 0)
1961                 return (error);
1962         if (nd->nl_nch.ncp->nc_vp)
1963                 return (EEXIST);
1964         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1965                 return (error);
1966
1967         VATTR_NULL(&vattr);
1968         vattr.va_type = VFIFO;
1969         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1970         vp = NULL;
1971         error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1972         if (error == 0)
1973                 vput(vp);
1974         return (error);
1975 }
1976
1977 /*
1978  * mkfifo_args(char *path, int mode)
1979  *
1980  * Create a named pipe.
1981  */
1982 int
1983 sys_mkfifo(struct mkfifo_args *uap)
1984 {
1985         struct nlookupdata nd;
1986         int error;
1987
1988         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1989         if (error == 0)
1990                 error = kern_mkfifo(&nd, uap->mode);
1991         nlookup_done(&nd);
1992         return (error);
1993 }
1994
1995 static int hardlink_check_uid = 0;
1996 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1997     &hardlink_check_uid, 0, 
1998     "Unprivileged processes cannot create hard links to files owned by other "
1999     "users");
2000 static int hardlink_check_gid = 0;
2001 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2002     &hardlink_check_gid, 0,
2003     "Unprivileged processes cannot create hard links to files owned by other "
2004     "groups");
2005
2006 static int
2007 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2008 {
2009         struct vattr va;
2010         int error;
2011
2012         /*
2013          * Shortcut if disabled
2014          */
2015         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2016                 return (0);
2017
2018         /*
2019          * Privileged user can always hardlink
2020          */
2021         if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2022                 return (0);
2023
2024         /*
2025          * Otherwise only if the originating file is owned by the
2026          * same user or group.  Note that any group is allowed if
2027          * the file is owned by the caller.
2028          */
2029         error = VOP_GETATTR(vp, &va);
2030         if (error != 0)
2031                 return (error);
2032         
2033         if (hardlink_check_uid) {
2034                 if (cred->cr_uid != va.va_uid)
2035                         return (EPERM);
2036         }
2037         
2038         if (hardlink_check_gid) {
2039                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2040                         return (EPERM);
2041         }
2042
2043         return (0);
2044 }
2045
2046 int
2047 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2048 {
2049         struct thread *td = curthread;
2050         struct vnode *vp;
2051         int error;
2052
2053         /*
2054          * Lookup the source and obtained a locked vnode.
2055          *
2056          * You may only hardlink a file which you have write permission
2057          * on or which you own.
2058          *
2059          * XXX relookup on vget failure / race ?
2060          */
2061         bwillinode(1);
2062         nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2063         if ((error = nlookup(nd)) != 0)
2064                 return (error);
2065         vp = nd->nl_nch.ncp->nc_vp;
2066         KKASSERT(vp != NULL);
2067         if (vp->v_type == VDIR)
2068                 return (EPERM);         /* POSIX */
2069         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2070                 return (error);
2071         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2072                 return (error);
2073
2074         /*
2075          * Unlock the source so we can lookup the target without deadlocking
2076          * (XXX vp is locked already, possible other deadlock?).  The target
2077          * must not exist.
2078          */
2079         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2080         nd->nl_flags &= ~NLC_NCPISLOCKED;
2081         cache_unlock(&nd->nl_nch);
2082
2083         linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2084         if ((error = nlookup(linknd)) != 0) {
2085                 vput(vp);
2086                 return (error);
2087         }
2088         if (linknd->nl_nch.ncp->nc_vp) {
2089                 vput(vp);
2090                 return (EEXIST);
2091         }
2092
2093         /*
2094          * Finally run the new API VOP.
2095          */
2096         error = can_hardlink(vp, td, td->td_proc->p_ucred);
2097         if (error == 0) {
2098                 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2099                                   vp, linknd->nl_cred);
2100         }
2101         vput(vp);
2102         return (error);
2103 }
2104
2105 /*
2106  * link_args(char *path, char *link)
2107  *
2108  * Make a hard file link.
2109  */
2110 int
2111 sys_link(struct link_args *uap)
2112 {
2113         struct nlookupdata nd, linknd;
2114         int error;
2115
2116         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2117         if (error == 0) {
2118                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2119                 if (error == 0)
2120                         error = kern_link(&nd, &linknd);
2121                 nlookup_done(&linknd);
2122         }
2123         nlookup_done(&nd);
2124         return (error);
2125 }
2126
2127 int
2128 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2129 {
2130         struct vattr vattr;
2131         struct vnode *vp;
2132         struct vnode *dvp;
2133         int error;
2134
2135         bwillinode(1);
2136         nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2137         if ((error = nlookup(nd)) != 0)
2138                 return (error);
2139         if (nd->nl_nch.ncp->nc_vp)
2140                 return (EEXIST);
2141         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2142                 return (error);
2143         dvp = nd->nl_dvp;
2144         VATTR_NULL(&vattr);
2145         vattr.va_mode = mode;
2146         error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2147         if (error == 0)
2148                 vput(vp);
2149         return (error);
2150 }
2151
2152 /*
2153  * symlink(char *path, char *link)
2154  *
2155  * Make a symbolic link.
2156  */
2157 int
2158 sys_symlink(struct symlink_args *uap)
2159 {
2160         struct thread *td = curthread;
2161         struct nlookupdata nd;
2162         char *path;
2163         int error;
2164         int mode;
2165
2166         path = objcache_get(namei_oc, M_WAITOK);
2167         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2168         if (error == 0) {
2169                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2170                 if (error == 0) {
2171                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2172                         error = kern_symlink(&nd, path, mode);
2173                 }
2174                 nlookup_done(&nd);
2175         }
2176         objcache_put(namei_oc, path);
2177         return (error);
2178 }
2179
2180 /*
2181  * undelete_args(char *path)
2182  *
2183  * Delete a whiteout from the filesystem.
2184  */
2185 /* ARGSUSED */
2186 int
2187 sys_undelete(struct undelete_args *uap)
2188 {
2189         struct nlookupdata nd;
2190         int error;
2191
2192         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2193         bwillinode(1);
2194         nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2195         if (error == 0)
2196                 error = nlookup(&nd);
2197         if (error == 0)
2198                 error = ncp_writechk(&nd.nl_nch);
2199         if (error == 0) {
2200                 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2201                                       NAMEI_DELETE);
2202         }
2203         nlookup_done(&nd);
2204         return (error);
2205 }
2206
2207 int
2208 kern_unlink(struct nlookupdata *nd)
2209 {
2210         int error;
2211
2212         bwillinode(1);
2213         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2214         if ((error = nlookup(nd)) != 0)
2215                 return (error);
2216         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2217                 return (error);
2218         error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2219         return (error);
2220 }
2221
2222 /*
2223  * unlink_args(char *path)
2224  *
2225  * Delete a name from the filesystem.
2226  */
2227 int
2228 sys_unlink(struct unlink_args *uap)
2229 {
2230         struct nlookupdata nd;
2231         int error;
2232
2233         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2234         if (error == 0)
2235                 error = kern_unlink(&nd);
2236         nlookup_done(&nd);
2237         return (error);
2238 }
2239
2240 int
2241 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2242 {
2243         struct thread *td = curthread;
2244         struct proc *p = td->td_proc;
2245         struct file *fp;
2246         struct vnode *vp;
2247         struct vattr vattr;
2248         off_t new_offset;
2249         int error;
2250
2251         fp = holdfp(p->p_fd, fd, -1);
2252         if (fp == NULL)
2253                 return (EBADF);
2254         if (fp->f_type != DTYPE_VNODE) {
2255                 error = ESPIPE;
2256                 goto done;
2257         }
2258         vp = (struct vnode *)fp->f_data;
2259
2260         switch (whence) {
2261         case L_INCR:
2262                 new_offset = fp->f_offset + offset;
2263                 error = 0;
2264                 break;
2265         case L_XTND:
2266                 error = VOP_GETATTR(vp, &vattr);
2267                 new_offset = offset + vattr.va_size;
2268                 break;
2269         case L_SET:
2270                 new_offset = offset;
2271                 error = 0;
2272                 break;
2273         default:
2274                 new_offset = 0;
2275                 error = EINVAL;
2276                 break;
2277         }
2278
2279         /*
2280          * Validate the seek position.  Negative offsets are not allowed
2281          * for regular files or directories.
2282          *
2283          * Normally we would also not want to allow negative offsets for
2284          * character and block-special devices.  However kvm addresses
2285          * on 64 bit architectures might appear to be negative and must
2286          * be allowed.
2287          */
2288         if (error == 0) {
2289                 if (new_offset < 0 &&
2290                     (vp->v_type == VREG || vp->v_type == VDIR)) {
2291                         error = EINVAL;
2292                 } else {
2293                         fp->f_offset = new_offset;
2294                 }
2295         }
2296         *res = fp->f_offset;
2297 done:
2298         fdrop(fp);
2299         return (error);
2300 }
2301
2302 /*
2303  * lseek_args(int fd, int pad, off_t offset, int whence)
2304  *
2305  * Reposition read/write file offset.
2306  */
2307 int
2308 sys_lseek(struct lseek_args *uap)
2309 {
2310         int error;
2311
2312         error = kern_lseek(uap->fd, uap->offset, uap->whence,
2313                            &uap->sysmsg_offset);
2314
2315         return (error);
2316 }
2317
2318 int
2319 kern_access(struct nlookupdata *nd, int aflags)
2320 {
2321         struct vnode *vp;
2322         int error, flags;
2323
2324         if ((error = nlookup(nd)) != 0)
2325                 return (error);
2326 retry:
2327         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2328         if (error)
2329                 return (error);
2330
2331         /* Flags == 0 means only check for existence. */
2332         if (aflags) {
2333                 flags = 0;
2334                 if (aflags & R_OK)
2335                         flags |= VREAD;
2336                 if (aflags & W_OK)
2337                         flags |= VWRITE;
2338                 if (aflags & X_OK)
2339                         flags |= VEXEC;
2340                 if ((flags & VWRITE) == 0 || 
2341                     (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2342                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
2343
2344                 /*
2345                  * If the file handle is stale we have to re-resolve the
2346                  * entry.  This is a hack at the moment.
2347                  */
2348                 if (error == ESTALE) {
2349                         vput(vp);
2350                         cache_setunresolved(&nd->nl_nch);
2351                         error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2352                         if (error == 0) {
2353                                 vp = NULL;
2354                                 goto retry;
2355                         }
2356                         return(error);
2357                 }
2358         }
2359         vput(vp);
2360         return (error);
2361 }
2362
2363 /*
2364  * access_args(char *path, int flags)
2365  *
2366  * Check access permissions.
2367  */
2368 int
2369 sys_access(struct access_args *uap)
2370 {
2371         struct nlookupdata nd;
2372         int error;
2373
2374         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2375         if (error == 0)
2376                 error = kern_access(&nd, uap->flags);
2377         nlookup_done(&nd);
2378         return (error);
2379 }
2380
2381 int
2382 kern_stat(struct nlookupdata *nd, struct stat *st)
2383 {
2384         int error;
2385         struct vnode *vp;
2386         thread_t td;
2387
2388         if ((error = nlookup(nd)) != 0)
2389                 return (error);
2390 again:
2391         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2392                 return (ENOENT);
2393
2394         td = curthread;
2395         if ((error = vget(vp, LK_SHARED)) != 0)
2396                 return (error);
2397         error = vn_stat(vp, st, nd->nl_cred);
2398
2399         /*
2400          * If the file handle is stale we have to re-resolve the entry.  This
2401          * is a hack at the moment.
2402          */
2403         if (error == ESTALE) {
2404                 vput(vp);
2405                 cache_setunresolved(&nd->nl_nch);
2406                 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2407                 if (error == 0)
2408                         goto again;
2409         } else {
2410                 vput(vp);
2411         }
2412         return (error);
2413 }
2414
2415 /*
2416  * stat_args(char *path, struct stat *ub)
2417  *
2418  * Get file status; this version follows links.
2419  */
2420 int
2421 sys_stat(struct stat_args *uap)
2422 {
2423         struct nlookupdata nd;
2424         struct stat st;
2425         int error;
2426
2427         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2428         if (error == 0) {
2429                 error = kern_stat(&nd, &st);
2430                 if (error == 0)
2431                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2432         }
2433         nlookup_done(&nd);
2434         return (error);
2435 }
2436
2437 /*
2438  * lstat_args(char *path, struct stat *ub)
2439  *
2440  * Get file status; this version does not follow links.
2441  */
2442 int
2443 sys_lstat(struct lstat_args *uap)
2444 {
2445         struct nlookupdata nd;
2446         struct stat st;
2447         int error;
2448
2449         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2450         if (error == 0) {
2451                 error = kern_stat(&nd, &st);
2452                 if (error == 0)
2453                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2454         }
2455         nlookup_done(&nd);
2456         return (error);
2457 }
2458
2459 /*
2460  * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2461  *
2462  * Get status of file pointed to by fd/path.
2463  */
2464 int
2465 sys_fstatat(struct fstatat_args *uap)
2466 {
2467         struct nlookupdata nd;
2468         struct stat st;
2469         int error;
2470         int flags;
2471         struct file *fp;
2472
2473         if (uap->flags & ~_AT_SYMLINK_MASK)
2474                 return (EINVAL);
2475
2476         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2477
2478         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
2479                                 UIO_USERSPACE, flags);
2480         if (error == 0) {
2481                 error = kern_stat(&nd, &st);
2482                 if (error == 0)
2483                         error = copyout(&st, uap->sb, sizeof(*uap->sb));
2484         }
2485         nlookup_done_at(&nd, fp);
2486         return (error);
2487 }
2488
2489 /*
2490  * pathconf_Args(char *path, int name)
2491  *
2492  * Get configurable pathname variables.
2493  */
2494 /* ARGSUSED */
2495 int
2496 sys_pathconf(struct pathconf_args *uap)
2497 {
2498         struct nlookupdata nd;
2499         struct vnode *vp;
2500         int error;
2501
2502         vp = NULL;
2503         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2504         if (error == 0)
2505                 error = nlookup(&nd);
2506         if (error == 0)
2507                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2508         nlookup_done(&nd);
2509         if (error == 0) {
2510                 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2511                 vput(vp);
2512         }
2513         return (error);
2514 }
2515
2516 /*
2517  * XXX: daver
2518  * kern_readlink isn't properly split yet.  There is a copyin burried
2519  * in VOP_READLINK().
2520  */
2521 int
2522 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2523 {
2524         struct thread *td = curthread;
2525         struct proc *p = td->td_proc;
2526         struct vnode *vp;
2527         struct iovec aiov;
2528         struct uio auio;
2529         int error;
2530
2531         if ((error = nlookup(nd)) != 0)
2532                 return (error);
2533         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2534         if (error)
2535                 return (error);
2536         if (vp->v_type != VLNK) {
2537                 error = EINVAL;
2538         } else {
2539                 aiov.iov_base = buf;
2540                 aiov.iov_len = count;
2541                 auio.uio_iov = &aiov;
2542                 auio.uio_iovcnt = 1;
2543                 auio.uio_offset = 0;
2544                 auio.uio_rw = UIO_READ;
2545                 auio.uio_segflg = UIO_USERSPACE;
2546                 auio.uio_td = td;
2547                 auio.uio_resid = count;
2548                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2549         }
2550         vput(vp);
2551         *res = count - auio.uio_resid;
2552         return (error);
2553 }
2554
2555 /*
2556  * readlink_args(char *path, char *buf, int count)
2557  *
2558  * Return target name of a symbolic link.
2559  */
2560 int
2561 sys_readlink(struct readlink_args *uap)
2562 {
2563         struct nlookupdata nd;
2564         int error;
2565
2566         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2567         if (error == 0) {
2568                 error = kern_readlink(&nd, uap->buf, uap->count,
2569                                         &uap->sysmsg_result);
2570         }
2571         nlookup_done(&nd);
2572         return (error);
2573 }
2574
2575 static int
2576 setfflags(struct vnode *vp, int flags)
2577 {
2578         struct thread *td = curthread;
2579         struct proc *p = td->td_proc;
2580         int error;
2581         struct vattr vattr;
2582
2583         /*
2584          * Prevent non-root users from setting flags on devices.  When
2585          * a device is reused, users can retain ownership of the device
2586          * if they are allowed to set flags and programs assume that
2587          * chown can't fail when done as root.
2588          */
2589         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2590             ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2591                 return (error);
2592
2593         /*
2594          * note: vget is required for any operation that might mod the vnode
2595          * so VINACTIVE is properly cleared.
2596          */
2597         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2598                 VATTR_NULL(&vattr);
2599                 vattr.va_flags = flags;
2600                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2601                 vput(vp);
2602         }
2603         return (error);
2604 }
2605
2606 /*
2607  * chflags(char *path, int flags)
2608  *
2609  * Change flags of a file given a path name.
2610  */
2611 /* ARGSUSED */
2612 int
2613 sys_chflags(struct chflags_args *uap)
2614 {
2615         struct nlookupdata nd;
2616         struct vnode *vp;
2617         int error;
2618
2619         vp = NULL;
2620         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2621         if (error == 0)
2622                 error = nlookup(&nd);
2623         if (error == 0)
2624                 error = ncp_writechk(&nd.nl_nch);
2625         if (error == 0)
2626                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2627         nlookup_done(&nd);
2628         if (error == 0) {
2629                 error = setfflags(vp, uap->flags);
2630                 vrele(vp);
2631         }
2632         return (error);
2633 }
2634
2635 /*
2636  * lchflags(char *path, int flags)
2637  *
2638  * Change flags of a file given a path name, but don't follow symlinks.
2639  */
2640 /* ARGSUSED */
2641 int
2642 sys_lchflags(struct lchflags_args *uap)
2643 {
2644         struct nlookupdata nd;
2645         struct vnode *vp;
2646         int error;
2647
2648         vp = NULL;
2649         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2650         if (error == 0)
2651                 error = nlookup(&nd);
2652         if (error == 0)
2653                 error = ncp_writechk(&nd.nl_nch);
2654         if (error == 0)
2655                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2656         nlookup_done(&nd);
2657         if (error == 0) {
2658                 error = setfflags(vp, uap->flags);
2659                 vrele(vp);
2660         }
2661         return (error);
2662 }
2663
2664 /*
2665  * fchflags_args(int fd, int flags)
2666  *
2667  * Change flags of a file given a file descriptor.
2668  */
2669 /* ARGSUSED */
2670 int
2671 sys_fchflags(struct fchflags_args *uap)
2672 {
2673         struct thread *td = curthread;
2674         struct proc *p = td->td_proc;
2675         struct file *fp;
2676         int error;
2677
2678         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2679                 return (error);
2680         if (fp->f_nchandle.ncp)
2681                 error = ncp_writechk(&fp->f_nchandle);
2682         if (error == 0)
2683                 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2684         fdrop(fp);
2685         return (error);
2686 }
2687
2688 static int
2689 setfmode(struct vnode *vp, int mode)
2690 {
2691         struct thread *td = curthread;
2692         struct proc *p = td->td_proc;
2693         int error;
2694         struct vattr vattr;
2695
2696         /*
2697          * note: vget is required for any operation that might mod the vnode
2698          * so VINACTIVE is properly cleared.
2699          */
2700         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2701                 VATTR_NULL(&vattr);
2702                 vattr.va_mode = mode & ALLPERMS;
2703                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2704                 vput(vp);
2705         }
2706         return error;
2707 }
2708
2709 int
2710 kern_chmod(struct nlookupdata *nd, int mode)
2711 {
2712         struct vnode *vp;
2713         int error;
2714
2715         if ((error = nlookup(nd)) != 0)
2716                 return (error);
2717         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2718                 return (error);
2719         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2720                 error = setfmode(vp, mode);
2721         vrele(vp);
2722         return (error);
2723 }
2724
2725 /*
2726  * chmod_args(char *path, int mode)
2727  *
2728  * Change mode of a file given path name.
2729  */
2730 /* ARGSUSED */
2731 int
2732 sys_chmod(struct chmod_args *uap)
2733 {
2734         struct nlookupdata nd;
2735         int error;
2736
2737         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2738         if (error == 0)
2739                 error = kern_chmod(&nd, uap->mode);
2740         nlookup_done(&nd);
2741         return (error);
2742 }
2743
2744 /*
2745  * lchmod_args(char *path, int mode)
2746  *
2747  * Change mode of a file given path name (don't follow links.)
2748  */
2749 /* ARGSUSED */
2750 int
2751 sys_lchmod(struct lchmod_args *uap)
2752 {
2753         struct nlookupdata nd;
2754         int error;
2755
2756         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2757         if (error == 0)
2758                 error = kern_chmod(&nd, uap->mode);
2759         nlookup_done(&nd);
2760         return (error);
2761 }
2762
2763 /*
2764  * fchmod_args(int fd, int mode)
2765  *
2766  * Change mode of a file given a file descriptor.
2767  */
2768 /* ARGSUSED */
2769 int
2770 sys_fchmod(struct fchmod_args *uap)
2771 {
2772         struct thread *td = curthread;
2773         struct proc *p = td->td_proc;
2774         struct file *fp;
2775         int error;
2776
2777         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2778                 return (error);
2779         if (fp->f_nchandle.ncp)
2780                 error = ncp_writechk(&fp->f_nchandle);
2781         if (error == 0)
2782                 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2783         fdrop(fp);
2784         return (error);
2785 }
2786
2787 /*
2788  * fchmodat_args(char *path, int mode)
2789  *
2790  * Change mode of a file pointed to by fd/path.
2791  */
2792 int
2793 sys_fchmodat(struct fchmodat_args *uap)
2794 {
2795         struct nlookupdata nd;
2796         struct file *fp;
2797         int error;
2798         int flags;
2799
2800         if (uap->flags & ~_AT_SYMLINK_MASK)
2801                 return (EINVAL);
2802         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2803
2804         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
2805                                 UIO_USERSPACE, flags);
2806         if (error == 0)
2807                 error = kern_chmod(&nd, uap->mode);
2808         nlookup_done_at(&nd, fp);
2809         return (error);
2810 }
2811
2812 static int
2813 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2814 {
2815         struct thread *td = curthread;
2816         struct proc *p = td->td_proc;
2817         int error;
2818         struct vattr vattr;
2819
2820         /*
2821          * note: vget is required for any operation that might mod the vnode
2822          * so VINACTIVE is properly cleared.
2823          */
2824         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2825                 VATTR_NULL(&vattr);
2826                 vattr.va_uid = uid;
2827                 vattr.va_gid = gid;
2828                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2829                 vput(vp);
2830         }
2831         return error;
2832 }
2833
2834 int
2835 kern_chown(struct nlookupdata *nd, int uid, int gid)
2836 {
2837         struct vnode *vp;
2838         int error;
2839
2840         if ((error = nlookup(nd)) != 0)
2841                 return (error);
2842         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2843                 return (error);
2844         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2845                 error = setfown(vp, uid, gid);
2846         vrele(vp);
2847         return (error);
2848 }
2849
2850 /*
2851  * chown(char *path, int uid, int gid)
2852  *
2853  * Set ownership given a path name.
2854  */
2855 int
2856 sys_chown(struct chown_args *uap)
2857 {
2858         struct nlookupdata nd;
2859         int error;
2860
2861         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2862         if (error == 0)
2863                 error = kern_chown(&nd, uap->uid, uap->gid);
2864         nlookup_done(&nd);
2865         return (error);
2866 }
2867
2868 /*
2869  * lchown_args(char *path, int uid, int gid)
2870  *
2871  * Set ownership given a path name, do not cross symlinks.
2872  */
2873 int
2874 sys_lchown(struct lchown_args *uap)
2875 {
2876         struct nlookupdata nd;
2877         int error;
2878
2879         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2880         if (error == 0)
2881                 error = kern_chown(&nd, uap->uid, uap->gid);
2882         nlookup_done(&nd);
2883         return (error);
2884 }
2885
2886 /*
2887  * fchown_args(int fd, int uid, int gid)
2888  *
2889  * Set ownership given a file descriptor.
2890  */
2891 /* ARGSUSED */
2892 int
2893 sys_fchown(struct fchown_args *uap)
2894 {
2895         struct thread *td = curthread;
2896         struct proc *p = td->td_proc;
2897         struct file *fp;
2898         int error;
2899
2900         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2901                 return (error);
2902         if (fp->f_nchandle.ncp)
2903                 error = ncp_writechk(&fp->f_nchandle);
2904         if (error == 0)
2905                 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2906         fdrop(fp);
2907         return (error);
2908 }
2909
2910 /*
2911  * fchownat(int fd, char *path, int uid, int gid, int flags)
2912  *
2913  * Set ownership of file pointed to by fd/path.
2914  */
2915 int
2916 sys_fchownat(struct fchownat_args *uap)
2917 {
2918         struct nlookupdata nd;
2919         struct file *fp;
2920         int error;
2921         int flags;
2922
2923         if (uap->flags & ~_AT_SYMLINK_MASK)
2924                 return (EINVAL);
2925         flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2926
2927         error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 
2928                                 UIO_USERSPACE, flags);
2929         if (error == 0)
2930                 error = kern_chown(&nd, uap->uid, uap->gid);
2931         nlookup_done_at(&nd, fp);
2932         return (error);
2933 }
2934
2935
2936 static int
2937 getutimes(const struct timeval *tvp, struct timespec *tsp)
2938 {
2939         struct timeval tv[2];
2940
2941         if (tvp == NULL) {
2942                 microtime(&tv[0]);
2943                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2944                 tsp[1] = tsp[0];
2945         } else {
2946                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2947                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2948         }
2949         return 0;
2950 }
2951
2952 static int
2953 setutimes(struct vnode *vp, struct vattr *vattr,
2954           const struct timespec *ts, int nullflag)
2955 {
2956         struct thread *td = curthread;
2957         struct proc *p = td->td_proc;
2958         int error;
2959
2960         VATTR_NULL(vattr);
2961         vattr->va_atime = ts[0];
2962         vattr->va_mtime = ts[1];
2963         if (nullflag)
2964                 vattr->va_vaflags |= VA_UTIMES_NULL;
2965         error = VOP_SETATTR(vp, vattr, p->p_ucred);
2966
2967         return error;
2968 }
2969
2970 int
2971 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2972 {
2973         struct timespec ts[2];
2974         struct vnode *vp;
2975         struct vattr vattr;
2976         int error;
2977
2978         if ((error = getutimes(tptr, ts)) != 0)
2979                 return (error);
2980
2981         /*
2982          * NOTE: utimes() succeeds for the owner even if the file
2983          * is not user-writable.
2984          */
2985         nd->nl_flags |= NLC_OWN | NLC_WRITE;
2986
2987         if ((error = nlookup(nd)) != 0)
2988                 return (error);
2989         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2990                 return (error);
2991         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2992                 return (error);
2993
2994         /*
2995          * note: vget is required for any operation that might mod the vnode
2996          * so VINACTIVE is properly cleared.
2997          */
2998         if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2999                 error = vget(vp, LK_EXCLUSIVE);
3000                 if (error == 0) {
3001                         error = setutimes(vp, &vattr, ts, (tptr == NULL));
3002                         vput(vp);
3003                 }
3004         }
3005         vrele(vp);
3006         return (error);
3007 }
3008
3009 /*
3010  * utimes_args(char *path, struct timeval *tptr)
3011  *
3012  * Set the access and modification times of a file.
3013  */
3014 int
3015 sys_utimes(struct utimes_args *uap)
3016 {
3017         struct timeval tv[2];
3018         struct nlookupdata nd;
3019         int error;
3020
3021         if (uap->tptr) {
3022                 error = copyin(uap->tptr, tv, sizeof(tv));
3023                 if (error)
3024                         return (error);
3025         }
3026         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3027         if (error == 0)
3028                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3029         nlookup_done(&nd);
3030         return (error);
3031 }
3032
3033 /*
3034  * lutimes_args(char *path, struct timeval *tptr)
3035  *
3036  * Set the access and modification times of a file.
3037  */
3038 int
3039 sys_lutimes(struct lutimes_args *uap)
3040 {
3041         struct timeval tv[2];
3042         struct nlookupdata nd;
3043         int error;
3044
3045         if (uap->tptr) {
3046                 error = copyin(uap->tptr, tv, sizeof(tv));
3047                 if (error)
3048                         return (error);
3049         }
3050         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3051         if (error == 0)
3052                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3053         nlookup_done(&nd);
3054         return (error);
3055 }
3056
3057 /*
3058  * Set utimes on a file descriptor.  The creds used to open the
3059  * file are used to determine whether the operation is allowed
3060  * or not.
3061  */
3062 int
3063 kern_futimes(int fd, struct timeval *tptr)
3064 {
3065         struct thread *td = curthread;
3066         struct proc *p = td->td_proc;
3067         struct timespec ts[2];
3068         struct file *fp;
3069         struct vnode *vp;
3070         struct vattr vattr;
3071         int error;
3072
3073         error = getutimes(tptr, ts);
3074         if (error)
3075                 return (error);
3076         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3077                 return (error);
3078         if (fp->f_nchandle.ncp)
3079                 error = ncp_writechk(&fp->f_nchandle);
3080         if (error == 0) {
3081                 vp = fp->f_data;
3082                 error = vget(vp, LK_EXCLUSIVE);
3083                 if (error == 0) {
3084                         error = VOP_GETATTR(vp, &vattr);
3085                         if (error == 0) {
3086                                 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3087                                                    fp->f_cred);
3088                         }
3089                         if (error == 0) {
3090                                 error = setutimes(vp, &vattr, ts,
3091                                                   (tptr == NULL));
3092                         }
3093                         vput(vp);
3094                 }
3095         }
3096         fdrop(fp);
3097         return (error);
3098 }
3099
3100 /*
3101  * futimes_args(int fd, struct timeval *tptr)
3102  *
3103  * Set the access and modification times of a file.
3104  */
3105 int
3106 sys_futimes(struct futimes_args *uap)
3107 {
3108         struct timeval tv[2];
3109         int error;
3110
3111         if (uap->tptr) {
3112                 error = copyin(uap->tptr, tv, sizeof(tv));
3113                 if (error)
3114                         return (error);
3115         }
3116
3117         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3118
3119         return (error);
3120 }
3121
3122 int
3123 kern_truncate(struct nlookupdata *nd, off_t length)
3124 {
3125         struct vnode *vp;
3126         struct vattr vattr;
3127         int error;
3128
3129         if (length < 0)
3130                 return(EINVAL);
3131         nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3132         if ((error = nlookup(nd)) != 0)
3133                 return (error);
3134         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3135                 return (error);
3136         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3137                 return (error);
3138         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3139                 vrele(vp);
3140                 return (error);
3141         }
3142         if (vp->v_type == VDIR) {
3143                 error = EISDIR;
3144         } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3145                 VATTR_NULL(&vattr);
3146                 vattr.va_size = length;
3147                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3148         }
3149         vput(vp);
3150         return (error);
3151 }
3152
3153 /*
3154  * truncate(char *path, int pad, off_t length)
3155  *
3156  * Truncate a file given its path name.
3157  */
3158 int
3159 sys_truncate(struct truncate_args *uap)
3160 {
3161         struct nlookupdata nd;
3162         int error;
3163
3164         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3165         if (error == 0)
3166                 error = kern_truncate(&nd, uap->length);
3167         nlookup_done(&nd);
3168         return error;
3169 }
3170
3171 int
3172 kern_ftruncate(int fd, off_t length)
3173 {
3174         struct thread *td = curthread;
3175         struct proc *p = td->td_proc;
3176         struct vattr vattr;
3177         struct vnode *vp;
3178         struct file *fp;
3179         int error;
3180
3181         if (length < 0)
3182                 return(EINVAL);
3183         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3184                 return (error);
3185         if (fp->f_nchandle.ncp) {
3186                 error = ncp_writechk(&fp->f_nchandle);
3187                 if (error)
3188                         goto done;
3189         }
3190         if ((fp->f_flag & FWRITE) == 0) {
3191                 error = EINVAL;
3192                 goto done;
3193         }
3194         if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3195                 error = EINVAL;
3196                 goto done;
3197         }
3198         vp = (struct vnode *)fp->f_data;
3199         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3200         if (vp->v_type == VDIR) {
3201                 error = EISDIR;
3202         } else if ((error = vn_writechk(vp, NULL)) == 0) {
3203                 VATTR_NULL(&vattr);
3204                 vattr.va_size = length;
3205                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3206         }
3207         vn_unlock(vp);
3208 done:
3209         fdrop(fp);
3210         return (error);
3211 }
3212
3213 /*
3214  * ftruncate_args(int fd, int pad, off_t length)
3215  *
3216  * Truncate a file given a file descriptor.
3217  */
3218 int
3219 sys_ftruncate(struct ftruncate_args *uap)
3220 {
3221         int error;
3222
3223         error = kern_ftruncate(uap->fd, uap->length);
3224
3225         return (error);
3226 }
3227
3228 /*
3229  * fsync(int fd)
3230  *
3231  * Sync an open file.
3232  */
3233 /* ARGSUSED */
3234 int
3235 sys_fsync(struct fsync_args *uap)
3236 {
3237         struct thread *td = curthread;
3238         struct proc *p = td->td_proc;
3239         struct vnode *vp;
3240         struct file *fp;
3241         vm_object_t obj;
3242         int error;
3243
3244         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3245                 return (error);
3246         vp = (struct vnode *)fp->f_data;
3247         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3248         if ((obj = vp->v_object) != NULL)
3249                 vm_object_page_clean(obj, 0, 0, 0);
3250         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3251                 error = buf_fsync(vp);
3252         vn_unlock(vp);
3253         fdrop(fp);
3254         return (error);
3255 }
3256
3257 int
3258 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3259 {
3260         struct nchandle fnchd;
3261         struct nchandle tnchd;
3262         struct namecache *ncp;
3263         struct vnode *fdvp;
3264         struct vnode *tdvp;
3265         struct mount *mp;
3266         int error;
3267
3268         bwillinode(1);
3269         fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3270         if ((error = nlookup(fromnd)) != 0)
3271                 return (error);
3272         if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3273                 return (ENOENT);
3274         fnchd.mount = fromnd->nl_nch.mount;
3275         cache_hold(&fnchd);
3276
3277         /*
3278          * unlock the source nch so we can lookup the target nch without
3279          * deadlocking.  The target may or may not exist so we do not check
3280          * for a target vp like kern_mkdir() and other creation functions do.
3281          *
3282          * The source and target directories are ref'd and rechecked after
3283          * everything is relocked to determine if the source or target file
3284          * has been renamed.
3285          */
3286         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3287         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3288         cache_unlock(&fromnd->nl_nch);
3289
3290         tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3291         if ((error = nlookup(tond)) != 0) {
3292                 cache_drop(&fnchd);
3293                 return (error);
3294         }
3295         if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3296                 cache_drop(&fnchd);
3297                 return (ENOENT);
3298         }
3299         tnchd.mount = tond->nl_nch.mount;
3300         cache_hold(&tnchd);
3301
3302         /*
3303          * If the source and target are the same there is nothing to do
3304          */
3305         if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3306                 cache_drop(&fnchd);
3307                 cache_drop(&tnchd);
3308                 return (0);
3309         }
3310
3311         /*
3312          * Mount points cannot be renamed or overwritten
3313          */
3314         if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3315             NCF_ISMOUNTPT
3316         ) {
3317                 cache_drop(&fnchd);
3318                 cache_drop(&tnchd);
3319                 return (EINVAL);
3320         }
3321
3322         /*
3323          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
3324          * may have become invalid while it was unlocked, nc_vp and nc_mount
3325          * could be NULL.
3326          */
3327         if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3328                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3329         } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3330                 cache_lock(&fromnd->nl_nch);
3331                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3332         } else {
3333                 cache_unlock(&tond->nl_nch);
3334                 cache_lock(&fromnd->nl_nch);
3335                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3336                 cache_lock(&tond->nl_nch);
3337                 cache_resolve(&tond->nl_nch, tond->nl_cred);
3338         }
3339         fromnd->nl_flags |= NLC_NCPISLOCKED;
3340
3341         /*
3342          * make sure the parent directories linkages are the same
3343          */
3344         if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3345             tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3346                 cache_drop(&fnchd);
3347                 cache_drop(&tnchd);
3348                 return (ENOENT);
3349         }
3350
3351         /*
3352          * Both the source and target must be within the same filesystem and
3353          * in the same filesystem as their parent directories within the
3354          * namecache topology.
3355          *
3356          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3357          */
3358         mp = fnchd.mount;
3359         if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3360             mp != tond->nl_nch.mount) {
3361                 cache_drop(&fnchd);
3362                 cache_drop(&tnchd);
3363                 return (EXDEV);
3364         }
3365
3366         /*
3367          * Make sure the mount point is writable
3368          */
3369         if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3370                 cache_drop(&fnchd);
3371                 cache_drop(&tnchd);
3372                 return (error);
3373         }
3374
3375         /*
3376          * If the target exists and either the source or target is a directory,
3377          * then both must be directories.
3378          *
3379          * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3380          * have become NULL.
3381          */
3382         if (tond->nl_nch.ncp->nc_vp) {
3383                 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3384                         error = ENOENT;
3385                 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3386                         if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3387                                 error = ENOTDIR;
3388                 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3389                         error = EISDIR;
3390                 }
3391         }
3392
3393         /*
3394          * You cannot rename a source into itself or a subdirectory of itself.
3395          * We check this by travsersing the target directory upwards looking
3396          * for a match against the source.
3397          */
3398         if (error == 0) {
3399                 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3400                         if (fromnd->nl_nch.ncp == ncp) {
3401                                 error = EINVAL;
3402                                 break;
3403                         }
3404                 }
3405         }
3406
3407         cache_drop(&fnchd);
3408         cache_drop(&tnchd);
3409
3410         /*
3411          * Even though the namespaces are different, they may still represent
3412          * hardlinks to the same file.  The filesystem might have a hard time
3413          * with this so we issue a NREMOVE of the source instead of a NRENAME
3414          * when we detect the situation.
3415          */
3416         if (error == 0) {
3417                 fdvp = fromnd->nl_dvp;
3418                 tdvp = tond->nl_dvp;
3419                 if (fdvp == NULL || tdvp == NULL) {
3420                         error = EPERM;
3421                 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3422                         error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3423                                             fromnd->nl_cred);
3424                 } else {
3425                         error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 
3426                                             fdvp, tdvp, tond->nl_cred);
3427                 }
3428         }
3429         return (error);
3430 }
3431
3432 /*
3433  * rename_args(char *from, char *to)
3434  *
3435  * Rename files.  Source and destination must either both be directories,
3436  * or both not be directories.  If target is a directory, it must be empty.
3437  */
3438 int
3439 sys_rename(struct rename_args *uap)
3440 {
3441         struct nlookupdata fromnd, tond;
3442         int error;
3443
3444         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3445         if (error == 0) {
3446                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3447                 if (error == 0)
3448                         error = kern_rename(&fromnd, &tond);
3449                 nlookup_done(&tond);
3450         }
3451         nlookup_done(&fromnd);
3452         return (error);
3453 }
3454
3455 int
3456 kern_mkdir(struct nlookupdata *nd, int mode)
3457 {
3458         struct thread *td = curthread;
3459         struct proc *p = td->td_proc;
3460         struct vnode *vp;
3461         struct vattr vattr;
3462         int error;
3463
3464         bwillinode(1);
3465         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3466         if ((error = nlookup(nd)) != 0)
3467                 return (error);
3468
3469         if (nd->nl_nch.ncp->nc_vp)
3470                 return (EEXIST);
3471         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3472                 return (error);
3473         VATTR_NULL(&vattr);
3474         vattr.va_type = VDIR;
3475         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3476
3477         vp = NULL;
3478         error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3479         if (error == 0)
3480                 vput(vp);
3481         return (error);
3482 }
3483
3484 /*
3485  * mkdir_args(char *path, int mode)
3486  *
3487  * Make a directory file.
3488  */
3489 /* ARGSUSED */
3490 int
3491 sys_mkdir(struct mkdir_args *uap)
3492 {
3493         struct nlookupdata nd;
3494         int error;
3495
3496         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3497         if (error == 0)
3498                 error = kern_mkdir(&nd, uap->mode);
3499         nlookup_done(&nd);
3500         return (error);
3501 }
3502
3503 int
3504 kern_rmdir(struct nlookupdata *nd)
3505 {
3506         int error;
3507
3508         bwillinode(1);
3509         nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3510         if ((error = nlookup(nd)) != 0)
3511                 return (error);
3512
3513         /*
3514          * Do not allow directories representing mount points to be
3515          * deleted, even if empty.  Check write perms on mount point
3516          * in case the vnode is aliased (aka nullfs).
3517          */
3518         if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3519                 return (EINVAL);
3520         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3521                 return (error);
3522         error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3523         return (error);
3524 }
3525
3526 /*
3527  * rmdir_args(char *path)
3528  *
3529  * Remove a directory file.
3530  */
3531 /* ARGSUSED */
3532 int
3533 sys_rmdir(struct rmdir_args *uap)
3534 {
3535         struct nlookupdata nd;
3536         int error;
3537
3538         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3539         if (error == 0)
3540                 error = kern_rmdir(&nd);
3541         nlookup_done(&nd);
3542         return (error);
3543 }
3544
3545 int
3546 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3547     enum uio_seg direction)
3548 {
3549         struct thread *td = curthread;
3550         struct proc *p = td->td_proc;
3551         struct vnode *vp;
3552         struct file *fp;
3553         struct uio auio;
3554         struct iovec aiov;
3555         off_t loff;
3556         int error, eofflag;
3557
3558         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3559                 return (error);
3560         if ((fp->f_flag & FREAD) == 0) {
3561                 error = EBADF;
3562                 goto done;
3563         }
3564         vp = (struct vnode *)fp->f_data;
3565 unionread:
3566         if (vp->v_type != VDIR) {
3567                 error = EINVAL;
3568                 goto done;
3569         }
3570         aiov.iov_base = buf;
3571         aiov.iov_len = count;
3572         auio.uio_iov = &aiov;
3573         auio.uio_iovcnt = 1;
3574         auio.uio_rw = UIO_READ;
3575         auio.uio_segflg = direction;
3576         auio.uio_td = td;
3577         auio.uio_resid = count;
3578         loff = auio.uio_offset = fp->f_offset;
3579         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3580         fp->f_offset = auio.uio_offset;
3581         if (error)
3582                 goto done;
3583         if (count == auio.uio_resid) {
3584                 if (union_dircheckp) {
3585                         error = union_dircheckp(td, &vp, fp);
3586                         if (error == -1)
3587                                 goto unionread;
3588                         if (error)
3589                                 goto done;
3590                 }
3591 #if 0
3592                 if ((vp->v_flag & VROOT) &&
3593                     (vp->v_mount->mnt_flag & MNT_UNION)) {
3594                         struct vnode *tvp = vp;
3595                         vp = vp->v_mount->mnt_vnodecovered;
3596                         vref(vp);
3597                         fp->f_data = vp;
3598                         fp->f_offset = 0;
3599                         vrele(tvp);
3600                         goto unionread;
3601                 }
3602 #endif
3603         }
3604
3605         /*
3606          * WARNING!  *basep may not be wide enough to accomodate the
3607          * seek offset.   XXX should we hack this to return the upper 32 bits
3608          * for offsets greater then 4G?
3609          */
3610         if (basep) {
3611                 *basep = (long)loff;
3612         }
3613         *res = count - auio.uio_resid;
3614 done:
3615         fdrop(fp);
3616         return (error);
3617 }
3618
3619 /*
3620  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3621  *
3622  * Read a block of directory entries in a file system independent format.
3623  */
3624 int
3625 sys_getdirentries(struct getdirentries_args *uap)
3626 {
3627         long base;
3628         int error;
3629
3630         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3631                                    &uap->sysmsg_result, UIO_USERSPACE);
3632
3633         if (error == 0 && uap->basep)
3634                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3635         return (error);
3636 }
3637
3638 /*
3639  * getdents_args(int fd, char *buf, size_t count)
3640  */
3641 int
3642 sys_getdents(struct getdents_args *uap)
3643 {
3644         int error;
3645
3646         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3647             &uap->sysmsg_result, UIO_USERSPACE);
3648
3649         return (error);
3650 }
3651
3652 /*
3653  * umask(int newmask)
3654  *
3655  * Set the mode mask for creation of filesystem nodes.
3656  *
3657  * MP SAFE
3658  */
3659 int
3660 sys_umask(struct umask_args *uap)
3661 {
3662         struct thread *td = curthread;
3663         struct proc *p = td->td_proc;
3664         struct filedesc *fdp;
3665
3666         fdp = p->p_fd;
3667         uap->sysmsg_result = fdp->fd_cmask;
3668         fdp->fd_cmask = uap->newmask & ALLPERMS;
3669         return (0);
3670 }
3671
3672 /*
3673  * revoke(char *path)
3674  *
3675  * Void all references to file by ripping underlying filesystem
3676  * away from vnode.
3677  */
3678 /* ARGSUSED */
3679 int
3680 sys_revoke(struct revoke_args *uap)
3681 {
3682         struct nlookupdata nd;
3683         struct vattr vattr;
3684         struct vnode *vp;
3685         struct ucred *cred;
3686         int error;
3687
3688         vp = NULL;
3689         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3690         if (error == 0)
3691                 error = nlookup(&nd);
3692         if (error == 0)
3693                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3694         cred = crhold(nd.nl_cred);
3695         nlookup_done(&nd);
3696         if (error == 0) {
3697                 if (error == 0)
3698                         error = VOP_GETATTR(vp, &vattr);
3699                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3700                         error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3701                 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3702                         if (vcount(vp) > 0)
3703                                 error = vrevoke(vp, cred);
3704                 } else if (error == 0) {
3705                         error = vrevoke(vp, cred);
3706                 }
3707                 vrele(vp);
3708         }
3709         if (cred)
3710                 crfree(cred);
3711         return (error);
3712 }
3713
3714 /*
3715  * getfh_args(char *fname, fhandle_t *fhp)
3716  *
3717  * Get (NFS) file handle
3718  *
3719  * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3720  * mount.  This allows nullfs mounts to be explicitly exported. 
3721  *
3722  * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3723  *
3724  *          nullfs mounts of subdirectories are not safe.  That is, it will
3725  *          work, but you do not really have protection against access to
3726  *          the related parent directories.
3727  */
3728 int
3729 sys_getfh(struct getfh_args *uap)
3730 {
3731         struct thread *td = curthread;
3732         struct nlookupdata nd;
3733         fhandle_t fh;
3734         struct vnode *vp;
3735         struct mount *mp;
3736         int error;
3737
3738         /*
3739          * Must be super user
3740          */
3741         if ((error = priv_check(td, PRIV_ROOT)) != 0)
3742                 return (error);
3743
3744         vp = NULL;
3745         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3746         if (error == 0)
3747                 error = nlookup(&nd);
3748         if (error == 0)
3749                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3750         mp = nd.nl_nch.mount;
3751         nlookup_done(&nd);
3752         if (error == 0) {
3753                 bzero(&fh, sizeof(fh));
3754                 fh.fh_fsid = mp->mnt_stat.f_fsid;
3755                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3756                 vput(vp);
3757                 if (error == 0)
3758                         error = copyout(&fh, uap->fhp, sizeof(fh));
3759         }
3760         return (error);
3761 }
3762
3763 /*
3764  * fhopen_args(const struct fhandle *u_fhp, int flags)
3765  *
3766  * syscall for the rpc.lockd to use to translate a NFS file handle into
3767  * an open descriptor.
3768  *
3769  * warning: do not remove the priv_check() call or this becomes one giant
3770  * security hole.
3771  */
3772 int
3773 sys_fhopen(struct fhopen_args *uap)
3774 {
3775         struct thread *td = curthread;
3776         struct proc *p = td->td_proc;
3777         struct mount *mp;
3778         struct vnode *vp;
3779         struct fhandle fhp;
3780         struct vattr vat;
3781         struct vattr *vap = &vat;
3782         struct flock lf;
3783         int fmode, mode, error, type;
3784         struct file *nfp; 
3785         struct file *fp;
3786         int indx;
3787
3788         /*
3789          * Must be super user
3790          */
3791         error = priv_check(td, PRIV_ROOT);
3792         if (error)
3793                 return (error);
3794
3795         fmode = FFLAGS(uap->flags);
3796         /* why not allow a non-read/write open for our lockd? */
3797         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3798                 return (EINVAL);
3799         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3800         if (error)
3801                 return(error);
3802         /* find the mount point */
3803         mp = vfs_getvfs(&fhp.fh_fsid);
3804         if (mp == NULL)
3805                 return (ESTALE);
3806         /* now give me my vnode, it gets returned to me locked */
3807         error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3808         if (error)
3809                 return (error);
3810         /*
3811          * from now on we have to make sure not
3812          * to forget about the vnode
3813          * any error that causes an abort must vput(vp) 
3814          * just set error = err and 'goto bad;'.
3815          */
3816
3817         /* 
3818          * from vn_open 
3819          */
3820         if (vp->v_type == VLNK) {
3821                 error = EMLINK;
3822                 goto bad;
3823         }
3824         if (vp->v_type == VSOCK) {
3825                 error = EOPNOTSUPP;
3826                 goto bad;
3827         }
3828         mode = 0;
3829         if (fmode & (FWRITE | O_TRUNC)) {
3830                 if (vp->v_type == VDIR) {
3831                         error = EISDIR;
3832                         goto bad;
3833                 }
3834                 error = vn_writechk(vp, NULL);
3835                 if (error)
3836                         goto bad;
3837                 mode |= VWRITE;
3838         }
3839         if (fmode & FREAD)
3840                 mode |= VREAD;
3841         if (mode) {
3842                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3843                 if (error)
3844                         goto bad;
3845         }
3846         if (fmode & O_TRUNC) {
3847                 vn_unlock(vp);                          /* XXX */
3848                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3849                 VATTR_NULL(vap);
3850                 vap->va_size = 0;
3851                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3852                 if (error)
3853                         goto bad;
3854         }
3855
3856         /*
3857          * VOP_OPEN needs the file pointer so it can potentially override
3858          * it.
3859          *
3860          * WARNING! no f_nchandle will be associated when fhopen()ing a
3861          * directory.  XXX
3862          */
3863         if ((error = falloc(p, &nfp, &indx)) != 0)
3864                 goto bad;
3865         fp = nfp;
3866
3867         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3868         if (error) {
3869                 /*
3870                  * setting f_ops this way prevents VOP_CLOSE from being
3871                  * called or fdrop() releasing the vp from v_data.   Since
3872                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3873                  */
3874                 fp->f_ops = &badfileops;
3875                 fp->f_data = NULL;
3876                 goto bad_drop;
3877         }
3878
3879         /*
3880          * The fp is given its own reference, we still have our ref and lock.
3881          *
3882          * Assert that all regular files must be created with a VM object.
3883          */
3884         if (vp->v_type == VREG && vp->v_object == NULL) {
3885                 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3886                 goto bad_drop;
3887         }
3888
3889         /*
3890          * The open was successful.  Handle any locking requirements.
3891          */
3892         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3893                 lf.l_whence = SEEK_SET;
3894                 lf.l_start = 0;
3895                 lf.l_len = 0;
3896                 if (fmode & O_EXLOCK)
3897                         lf.l_type = F_WRLCK;
3898                 else
3899                         lf.l_type = F_RDLCK;
3900                 if (fmode & FNONBLOCK)
3901                         type = 0;
3902                 else
3903                         type = F_WAIT;
3904                 vn_unlock(vp);
3905                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3906                         /*
3907                          * release our private reference.
3908                          */
3909                         fsetfd(p, NULL, indx);
3910                         fdrop(fp);
3911                         vrele(vp);
3912                         return (error);
3913                 }
3914                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3915                 fp->f_flag |= FHASLOCK;
3916         }
3917
3918         /*
3919          * Clean up.  Associate the file pointer with the previously
3920          * reserved descriptor and return it.
3921          */
3922         vput(vp);
3923         fsetfd(p, fp, indx);
3924         fdrop(fp);
3925         uap->sysmsg_result = indx;
3926         return (0);
3927
3928 bad_drop:
3929         fsetfd(p, NULL, indx);
3930         fdrop(fp);
3931 bad:
3932         vput(vp);
3933         return (error);
3934 }
3935
3936 /*
3937  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3938  */
3939 int
3940 sys_fhstat(struct fhstat_args *uap)
3941 {
3942         struct thread *td = curthread;
3943         struct stat sb;
3944         fhandle_t fh;
3945         struct mount *mp;
3946         struct vnode *vp;
3947         int error;
3948
3949         /*
3950          * Must be super user
3951          */
3952         error = priv_check(td, PRIV_ROOT);
3953         if (error)
3954                 return (error);
3955         
3956         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3957         if (error)
3958                 return (error);
3959
3960         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3961                 return (ESTALE);
3962         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3963                 return (error);
3964         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3965         vput(vp);
3966         if (error)
3967                 return (error);
3968         error = copyout(&sb, uap->sb, sizeof(sb));
3969         return (error);
3970 }
3971
3972 /*
3973  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3974  */
3975 int
3976 sys_fhstatfs(struct fhstatfs_args *uap)
3977 {
3978         struct thread *td = curthread;
3979         struct proc *p = td->td_proc;
3980         struct statfs *sp;
3981         struct mount *mp;
3982         struct vnode *vp;
3983         struct statfs sb;
3984         char *fullpath, *freepath;
3985         fhandle_t fh;
3986         int error;
3987
3988         /*
3989          * Must be super user
3990          */
3991         if ((error = priv_check(td, PRIV_ROOT)))
3992                 return (error);
3993
3994         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3995                 return (error);
3996
3997         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3998                 return (ESTALE);
3999
4000         if (p != NULL && !chroot_visible_mnt(mp, p))
4001                 return (ESTALE);
4002
4003         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4004                 return (error);
4005         mp = vp->v_mount;
4006         sp = &mp->mnt_stat;
4007         vput(vp);
4008         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4009                 return (error);
4010
4011         error = mount_path(p, mp, &fullpath, &freepath);
4012         if (error)
4013                 return(error);
4014         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4015         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4016         kfree(freepath, M_TEMP);
4017
4018         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4019         if (priv_check(td, PRIV_ROOT)) {
4020                 bcopy(sp, &sb, sizeof(sb));
4021                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4022                 sp = &sb;
4023         }
4024         return (copyout(sp, uap->buf, sizeof(*sp)));
4025 }
4026
4027 /*
4028  * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4029  */
4030 int
4031 sys_fhstatvfs(struct fhstatvfs_args *uap)
4032 {
4033         struct thread *td = curthread;
4034         struct proc *p = td->td_proc;
4035         struct statvfs *sp;
4036         struct mount *mp;
4037         struct vnode *vp;
4038         fhandle_t fh;
4039         int error;
4040
4041         /*
4042          * Must be super user
4043          */
4044         if ((error = priv_check(td, PRIV_ROOT)))
4045                 return (error);
4046
4047         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4048                 return (error);
4049
4050         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4051                 return (ESTALE);
4052
4053         if (p != NULL && !chroot_visible_mnt(mp, p))
4054                 return (ESTALE);
4055
4056         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4057                 return (error);
4058         mp = vp->v_mount;
4059         sp = &mp->mnt_vstat;
4060         vput(vp);
4061         if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4062                 return (error);
4063
4064         sp->f_flag = 0;
4065         if (mp->mnt_flag & MNT_RDONLY)
4066                 sp->f_flag |= ST_RDONLY;
4067         if (mp->mnt_flag & MNT_NOSUID)
4068                 sp->f_flag |= ST_NOSUID;
4069
4070         return (copyout(sp, uap->buf, sizeof(*sp)));
4071 }
4072
4073
4074 /*
4075  * Syscall to push extended attribute configuration information into the
4076  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
4077  * a command (int cmd), and attribute name and misc data.  For now, the
4078  * attribute name is left in userspace for consumption by the VFS_op.
4079  * It will probably be changed to be copied into sysspace by the
4080  * syscall in the future, once issues with various consumers of the
4081  * attribute code have raised their hands.
4082  *
4083  * Currently this is used only by UFS Extended Attributes.
4084  */
4085 int
4086 sys_extattrctl(struct extattrctl_args *uap)
4087 {
4088         struct nlookupdata nd;
4089         struct mount *mp;
4090         struct vnode *vp;
4091         int error;
4092
4093         vp = NULL;
4094         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4095         if (error == 0)
4096                 error = nlookup(&nd);
4097         if (error == 0) {
4098                 mp = nd.nl_nch.mount;
4099                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
4100                                 uap->attrname, uap->arg, 
4101                                 nd.nl_cred);
4102         }
4103         nlookup_done(&nd);
4104         return (error);
4105 }
4106
4107 /*
4108  * Syscall to set a named extended attribute on a file or directory.
4109  * Accepts attribute name, and a uio structure pointing to the data to set.
4110  * The uio is consumed in the style of writev().  The real work happens
4111  * in VOP_SETEXTATTR().
4112  */
4113 int
4114 sys_extattr_set_file(struct extattr_set_file_args *uap)
4115 {
4116         char attrname[EXTATTR_MAXNAMELEN];
4117         struct iovec aiov[UIO_SMALLIOV];
4118         struct iovec *needfree;
4119         struct nlookupdata nd;
4120         struct iovec *iov;
4121         struct vnode *vp;
4122         struct uio auio;
4123         u_int iovlen;
4124         u_int cnt;
4125         int error;
4126         int i;
4127
4128         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4129         if (error)
4130                 return (error);
4131
4132         vp = NULL;
4133         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4134         if (error == 0)
4135                 error = nlookup(&nd);
4136         if (error == 0)
4137                 error = ncp_writechk(&nd.nl_nch);
4138         if (error == 0)
4139                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4140         if (error) {
4141                 nlookup_done(&nd);
4142                 return (error);
4143         }
4144
4145         needfree = NULL;
4146         iovlen = uap->iovcnt * sizeof(struct iovec);
4147         if (uap->iovcnt > UIO_SMALLIOV) {
4148                 if (uap->iovcnt > UIO_MAXIOV) {
4149                         error = EINVAL;
4150                         goto done;
4151                 }
4152                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4153                 needfree = iov;
4154         } else {
4155                 iov = aiov;
4156         }
4157         auio.uio_iov = iov;
4158         auio.uio_iovcnt = uap->iovcnt;
4159         auio.uio_rw = UIO_WRITE;
4160         auio.uio_segflg = UIO_USERSPACE;
4161         auio.uio_td = nd.nl_td;
4162         auio.uio_offset = 0;
4163         if ((error = copyin(uap->iovp, iov, iovlen)))
4164                 goto done;
4165         auio.uio_resid = 0;
4166         for (i = 0; i < uap->iovcnt; i++) {
4167                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4168                         error = EINVAL;
4169                         goto done;
4170                 }
4171                 auio.uio_resid += iov->iov_len;
4172                 iov++;
4173         }
4174         cnt = auio.uio_resid;
4175         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4176         cnt -= auio.uio_resid;
4177         uap->sysmsg_result = cnt;
4178 done:
4179         vput(vp);
4180         nlookup_done(&nd);
4181         if (needfree)
4182                 FREE(needfree, M_IOV);
4183         return (error);
4184 }
4185
4186 /*
4187  * Syscall to get a named extended attribute on a file or directory.
4188  * Accepts attribute name, and a uio structure pointing to a buffer for the
4189  * data.  The uio is consumed in the style of readv().  The real work
4190  * happens in VOP_GETEXTATTR();
4191  */
4192 int
4193 sys_extattr_get_file(struct extattr_get_file_args *uap)
4194 {
4195         char attrname[EXTATTR_MAXNAMELEN];
4196         struct iovec aiov[UIO_SMALLIOV];
4197         struct iovec *needfree;
4198         struct nlookupdata nd;
4199         struct iovec *iov;
4200         struct vnode *vp;
4201         struct uio auio;
4202         u_int iovlen;
4203         u_int cnt;
4204         int error;
4205         int i;
4206
4207         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4208         if (error)
4209                 return (error);
4210
4211         vp = NULL;
4212         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4213         if (error == 0)
4214                 error = nlookup(&nd);
4215         if (error == 0)
4216                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4217         if (error) {
4218                 nlookup_done(&nd);
4219                 return (error);
4220         }
4221
4222         iovlen = uap->iovcnt * sizeof (struct iovec);
4223         needfree = NULL;
4224         if (uap->iovcnt > UIO_SMALLIOV) {
4225                 if (uap->iovcnt > UIO_MAXIOV) {
4226                         error = EINVAL;
4227                         goto done;
4228                 }
4229                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4230                 needfree = iov;
4231         } else {
4232                 iov = aiov;
4233         }
4234         auio.uio_iov = iov;
4235         auio.uio_iovcnt = uap->iovcnt;
4236         auio.uio_rw = UIO_READ;
4237         auio.uio_segflg = UIO_USERSPACE;
4238         auio.uio_td = nd.nl_td;
4239         auio.uio_offset = 0;
4240         if ((error = copyin(uap->iovp, iov, iovlen)))
4241                 goto done;
4242         auio.uio_resid = 0;
4243         for (i = 0; i < uap->iovcnt; i++) {
4244                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4245                         error = EINVAL;
4246                         goto done;
4247                 }
4248                 auio.uio_resid += iov->iov_len;
4249                 iov++;
4250         }
4251         cnt = auio.uio_resid;
4252         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4253         cnt -= auio.uio_resid;
4254         uap->sysmsg_result = cnt;
4255 done:
4256         vput(vp);
4257         nlookup_done(&nd);
4258         if (needfree)
4259                 FREE(needfree, M_IOV);
4260         return(error);
4261 }
4262
4263 /*
4264  * Syscall to delete a named extended attribute from a file or directory.
4265  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
4266  */
4267 int
4268 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4269 {
4270         char attrname[EXTATTR_MAXNAMELEN];
4271         struct nlookupdata nd;
4272         struct vnode *vp;
4273         int error;
4274
4275         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4276         if (error)
4277                 return(error);
4278
4279         vp = NULL;
4280         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4281         if (error == 0)
4282                 error = nlookup(&nd);
4283         if (error == 0)
4284                 error = ncp_writechk(&nd.nl_nch);
4285         if (error == 0)
4286                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4287         if (error) {
4288                 nlookup_done(&nd);
4289                 return (error);
4290         }
4291
4292         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4293         vput(vp);
4294         nlookup_done(&nd);
4295         return(error);
4296 }
4297
4298 /*
4299  * Determine if the mount is visible to the process.
4300  */
4301 static int
4302 chroot_visible_mnt(struct mount *mp, struct proc *p)
4303 {
4304         struct nchandle nch;
4305
4306         /*
4307          * Traverse from the mount point upwards.  If we hit the process
4308          * root then the mount point is visible to the process.
4309          */
4310         nch = mp->mnt_ncmountpt;
4311         while (nch.ncp) {
4312                 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4313                     nch.ncp == p->p_fd->fd_nrdir.ncp) {
4314                         return(1);
4315                 }
4316                 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4317                         nch = nch.mount->mnt_ncmounton;
4318                 } else {
4319                         nch.ncp = nch.ncp->nc_parent;
4320                 }
4321         }
4322
4323         /*
4324          * If the mount point is not visible to the process, but the
4325          * process root is in a subdirectory of the mount, return
4326          * TRUE anyway.
4327          */
4328         if (p->p_fd->fd_nrdir.mount == mp)
4329                 return(1);
4330
4331         return(0);
4332 }
4333