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