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