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