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