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