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