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