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