Use .Pa
[dragonfly.git] / sys / kern / vfs_syscalls.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)vfs_syscalls.c      8.13 (Berkeley) 4/15/94
39  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40  * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.119 2007/08/13 17:43:55 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/conf.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
55 #include <sys/file.h>
56 #include <sys/linker.h>
57 #include <sys/stat.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/proc.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
68 #include <sys/sysctl.h>
69 #include <sys/file2.h>
70 #include <sys/spinlock2.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 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
83 static int checkvp_chdir (struct vnode *vn, struct thread *td);
84 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
85 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
86 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
87 static int getutimes (const struct timeval *, struct timespec *);
88 static int setfown (struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, int);
91 static int setutimes (struct vnode *, const struct timespec *, int);
92 static int      usermount = 0;  /* if 1, non-root can mount fs. */
93
94 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
95
96 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
97
98 /*
99  * Virtual File System System Calls
100  */
101
102 /*
103  * Mount a file system.
104  */
105 /*
106  * mount_args(char *type, char *path, int flags, caddr_t data)
107  */
108 /* ARGSUSED */
109 int
110 sys_mount(struct mount_args *uap)
111 {
112         struct thread *td = curthread;
113         struct proc *p = td->td_proc;
114         struct vnode *vp;
115         struct nchandle nch;
116         struct mount *mp;
117         struct vfsconf *vfsp;
118         int error, flag = 0, flag2 = 0;
119         int hasmount;
120         struct vattr va;
121         struct nlookupdata nd;
122         char fstypename[MFSNAMELEN];
123         struct ucred *cred = p->p_ucred;
124
125         KKASSERT(p);
126         if (cred->cr_prison != NULL)
127                 return (EPERM);
128         if (usermount == 0 && (error = suser(td)))
129                 return (error);
130         /*
131          * Do not allow NFS export by non-root users.
132          */
133         if (uap->flags & MNT_EXPORTED) {
134                 error = suser(td);
135                 if (error)
136                         return (error);
137         }
138         /*
139          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
140          */
141         if (suser(td)) 
142                 uap->flags |= MNT_NOSUID | MNT_NODEV;
143
144         /*
145          * Lookup the requested path and extract the nch and vnode.
146          */
147         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
148         if (error == 0) {
149                 if ((error = nlookup(&nd)) == 0) {
150                         if (nd.nl_nch.ncp->nc_vp == NULL)
151                                 error = ENOENT;
152                 }
153         }
154         if (error) {
155                 nlookup_done(&nd);
156                 return (error);
157         }
158
159         /*
160          * Extract the locked+refd ncp and cleanup the nd structure
161          */
162         nch = nd.nl_nch;
163         cache_zero(&nd.nl_nch);
164         nlookup_done(&nd);
165
166         if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
167                 hasmount = 1;
168         else
169                 hasmount = 0;
170
171
172         /*
173          * now we have the locked ref'd nch and unreferenced vnode.
174          */
175         vp = nch.ncp->nc_vp;
176         if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
177                 cache_put(&nch);
178                 return (error);
179         }
180         cache_unlock(&nch);
181
182         /*
183          * Now we have an unlocked ref'd nch and a locked ref'd vp
184          */
185         if (uap->flags & MNT_UPDATE) {
186                 if ((vp->v_flag & VROOT) == 0) {
187                         cache_drop(&nch);
188                         vput(vp);
189                         return (EINVAL);
190                 }
191                 mp = vp->v_mount;
192                 flag = mp->mnt_flag;
193                 flag2 = mp->mnt_kern_flag;
194                 /*
195                  * We only allow the filesystem to be reloaded if it
196                  * is currently mounted read-only.
197                  */
198                 if ((uap->flags & MNT_RELOAD) &&
199                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
200                         cache_drop(&nch);
201                         vput(vp);
202                         return (EOPNOTSUPP);    /* Needs translation */
203                 }
204                 /*
205                  * Only root, or the user that did the original mount is
206                  * permitted to update it.
207                  */
208                 if (mp->mnt_stat.f_owner != cred->cr_uid &&
209                     (error = suser(td))) {
210                         cache_drop(&nch);
211                         vput(vp);
212                         return (error);
213                 }
214                 if (vfs_busy(mp, LK_NOWAIT)) {
215                         cache_drop(&nch);
216                         vput(vp);
217                         return (EBUSY);
218                 }
219                 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
220                         cache_drop(&nch);
221                         vfs_unbusy(mp);
222                         vput(vp);
223                         return (EBUSY);
224                 }
225                 vp->v_flag |= VMOUNT;
226                 mp->mnt_flag |=
227                     uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
228                 vn_unlock(vp);
229                 goto update;
230         }
231         /*
232          * If the user is not root, ensure that they own the directory
233          * onto which we are attempting to mount.
234          */
235         if ((error = VOP_GETATTR(vp, &va)) ||
236             (va.va_uid != cred->cr_uid && (error = suser(td)))) {
237                 cache_drop(&nch);
238                 vput(vp);
239                 return (error);
240         }
241         if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
242                 cache_drop(&nch);
243                 vput(vp);
244                 return (error);
245         }
246         if (vp->v_type != VDIR) {
247                 cache_drop(&nch);
248                 vput(vp);
249                 return (ENOTDIR);
250         }
251         if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
252                 cache_drop(&nch);
253                 vput(vp);
254                 return (error);
255         }
256         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
257                 if (!strcmp(vfsp->vfc_name, fstypename))
258                         break;
259         }
260         if (vfsp == NULL) {
261                 linker_file_t lf;
262
263                 /* Only load modules for root (very important!) */
264                 if ((error = suser(td)) != 0) {
265                         cache_drop(&nch);
266                         vput(vp);
267                         return error;
268                 }
269                 error = linker_load_file(fstypename, &lf);
270                 if (error || lf == NULL) {
271                         cache_drop(&nch);
272                         vput(vp);
273                         if (lf == NULL)
274                                 error = ENODEV;
275                         return error;
276                 }
277                 lf->userrefs++;
278                 /* lookup again, see if the VFS was loaded */
279                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
280                         if (!strcmp(vfsp->vfc_name, fstypename))
281                                 break;
282                 }
283                 if (vfsp == NULL) {
284                         lf->userrefs--;
285                         linker_file_unload(lf);
286                         cache_drop(&nch);
287                         vput(vp);
288                         return (ENODEV);
289                 }
290         }
291         if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
292                 cache_drop(&nch);
293                 vput(vp);
294                 return (EBUSY);
295         }
296         vp->v_flag |= VMOUNT;
297
298         /*
299          * Allocate and initialize the filesystem.
300          */
301         mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
302         TAILQ_INIT(&mp->mnt_nvnodelist);
303         TAILQ_INIT(&mp->mnt_reservedvnlist);
304         TAILQ_INIT(&mp->mnt_jlist);
305         mp->mnt_nvnodelistsize = 0;
306         lockinit(&mp->mnt_lock, "vfslock", 0, 0);
307         vfs_busy(mp, LK_NOWAIT);
308         mp->mnt_op = vfsp->vfc_vfsops;
309         mp->mnt_vfc = vfsp;
310         vfsp->vfc_refcount++;
311         mp->mnt_stat.f_type = vfsp->vfc_typenum;
312         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
313         strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
314         mp->mnt_stat.f_owner = cred->cr_uid;
315         mp->mnt_iosize_max = DFLTPHYS;
316         vn_unlock(vp);
317 update:
318         /*
319          * Set the mount level flags.
320          */
321         if (uap->flags & MNT_RDONLY)
322                 mp->mnt_flag |= MNT_RDONLY;
323         else if (mp->mnt_flag & MNT_RDONLY)
324                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
325         mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
326             MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
327             MNT_NOSYMFOLLOW | MNT_IGNORE |
328             MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
329         mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
330             MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
331             MNT_NOSYMFOLLOW | MNT_IGNORE |
332             MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333         /*
334          * Mount the filesystem.
335          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
336          * get. 
337          */
338         error = VFS_MOUNT(mp, uap->path, uap->data, cred);
339         if (mp->mnt_flag & MNT_UPDATE) {
340                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
341                         mp->mnt_flag &= ~MNT_RDONLY;
342                 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
343                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
344                 if (error) {
345                         mp->mnt_flag = flag;
346                         mp->mnt_kern_flag = flag2;
347                 }
348                 vfs_unbusy(mp);
349                 vp->v_flag &= ~VMOUNT;
350                 vrele(vp);
351                 cache_drop(&nch);
352                 return (error);
353         }
354         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
355         /*
356          * Put the new filesystem on the mount list after root.  The mount
357          * point gets its own mnt_ncmountpt (unless the VFS already set one
358          * up) which represents the root of the mount.  The lookup code
359          * detects the mount point going forward and checks the root of
360          * the mount going backwards.
361          *
362          * It is not necessary to invalidate or purge the vnode underneath
363          * because elements under the mount will be given their own glue
364          * namecache record.
365          */
366         if (!error) {
367                 if (mp->mnt_ncmountpt.ncp == NULL) {
368                         /* 
369                          * allocate, then unlock, but leave the ref intact 
370                          */
371                         cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
372                         cache_unlock(&mp->mnt_ncmountpt);
373                 }
374                 mp->mnt_ncmounton = nch;                /* inherits ref */
375                 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
376
377                 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
378                 vp->v_flag &= ~VMOUNT;
379                 mountlist_insert(mp, MNTINS_LAST);
380                 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
381                 vn_unlock(vp);
382                 error = vfs_allocate_syncvnode(mp);
383                 vfs_unbusy(mp);
384                 error = VFS_START(mp, 0);
385                 vrele(vp);
386         } else {
387                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
388                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
389                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
390                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
391                 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
392                 vp->v_flag &= ~VMOUNT;
393                 mp->mnt_vfc->vfc_refcount--;
394                 vfs_unbusy(mp);
395                 kfree(mp, M_MOUNT);
396                 cache_drop(&nch);
397                 vput(vp);
398         }
399         return (error);
400 }
401
402 /*
403  * Scan all active processes to see if any of them have a current
404  * or root directory onto which the new filesystem has just been
405  * mounted. If so, replace them with the new mount point.
406  *
407  * The passed ncp is ref'd and locked (from the mount code) and
408  * must be associated with the vnode representing the root of the
409  * mount point.
410  */
411 struct checkdirs_info {
412         struct nchandle old_nch;
413         struct nchandle new_nch;
414         struct vnode *old_vp;
415         struct vnode *new_vp;
416 };
417
418 static int checkdirs_callback(struct proc *p, void *data);
419
420 static void
421 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
422 {
423         struct checkdirs_info info;
424         struct vnode *olddp;
425         struct vnode *newdp;
426         struct mount *mp;
427
428         /*
429          * If the old mount point's vnode has a usecount of 1, it is not
430          * being held as a descriptor anywhere.
431          */
432         olddp = old_nch->ncp->nc_vp;
433         if (olddp == NULL || olddp->v_sysref.refcnt == 1)
434                 return;
435
436         /*
437          * Force the root vnode of the new mount point to be resolved
438          * so we can update any matching processes.
439          */
440         mp = new_nch->mount;
441         if (VFS_ROOT(mp, &newdp))
442                 panic("mount: lost mount");
443         cache_setunresolved(new_nch);
444         cache_setvp(new_nch, newdp);
445
446         /*
447          * Special handling of the root node
448          */
449         if (rootvnode == olddp) {
450                 vref(newdp);
451                 vfs_cache_setroot(newdp, cache_hold(new_nch));
452         }
453
454         /*
455          * Pass newdp separately so the callback does not have to access
456          * it via new_nch->ncp->nc_vp.
457          */
458         info.old_nch = *old_nch;
459         info.new_nch = *new_nch;
460         info.new_vp = newdp;
461         allproc_scan(checkdirs_callback, &info);
462         vput(newdp);
463 }
464
465 /*
466  * NOTE: callback is not MP safe because the scanned process's filedesc
467  * structure can be ripped out from under us, amoung other things.
468  */
469 static int
470 checkdirs_callback(struct proc *p, void *data)
471 {
472         struct checkdirs_info *info = data;
473         struct filedesc *fdp;
474         struct nchandle ncdrop1;
475         struct nchandle ncdrop2;
476         struct vnode *vprele1;
477         struct vnode *vprele2;
478
479         if ((fdp = p->p_fd) != NULL) {
480                 cache_zero(&ncdrop1);
481                 cache_zero(&ncdrop2);
482                 vprele1 = NULL;
483                 vprele2 = NULL;
484
485                 /*
486                  * MPUNSAFE - XXX fdp can be pulled out from under a
487                  * foreign process.
488                  *
489                  * A shared filedesc is ok, we don't have to copy it
490                  * because we are making this change globally.
491                  */
492                 spin_lock_wr(&fdp->fd_spin);
493                 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
494                     fdp->fd_ncdir.ncp == info->old_nch.ncp) {
495                         vprele1 = fdp->fd_cdir;
496                         vref(info->new_vp);
497                         fdp->fd_cdir = info->new_vp;
498                         ncdrop1 = fdp->fd_ncdir;
499                         cache_copy(&info->new_nch, &fdp->fd_ncdir);
500                 }
501                 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
502                     fdp->fd_nrdir.ncp == info->old_nch.ncp) {
503                         vprele2 = fdp->fd_rdir;
504                         vref(info->new_vp);
505                         fdp->fd_rdir = info->new_vp;
506                         ncdrop2 = fdp->fd_nrdir;
507                         cache_copy(&info->new_nch, &fdp->fd_nrdir);
508                 }
509                 spin_unlock_wr(&fdp->fd_spin);
510                 if (ncdrop1.ncp)
511                         cache_drop(&ncdrop1);
512                 if (ncdrop2.ncp)
513                         cache_drop(&ncdrop2);
514                 if (vprele1)
515                         vrele(vprele1);
516                 if (vprele2)
517                         vrele(vprele2);
518         }
519         return(0);
520 }
521
522 /*
523  * Unmount a file system.
524  *
525  * Note: unmount takes a path to the vnode mounted on as argument,
526  * not special file (as before).
527  */
528 /*
529  * umount_args(char *path, int flags)
530  */
531 /* ARGSUSED */
532 int
533 sys_unmount(struct unmount_args *uap)
534 {
535         struct thread *td = curthread;
536         struct proc *p = td->td_proc;
537         struct mount *mp = NULL;
538         int error;
539         struct nlookupdata nd;
540
541         KKASSERT(p);
542         if (p->p_ucred->cr_prison != NULL)
543                 return (EPERM);
544         if (usermount == 0 && (error = suser(td)))
545                 return (error);
546
547         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
548         if (error == 0)
549                 error = nlookup(&nd);
550         if (error)
551                 goto out;
552
553         mp = nd.nl_nch.mount;
554
555         /*
556          * Only root, or the user that did the original mount is
557          * permitted to unmount this filesystem.
558          */
559         if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
560             (error = suser(td)))
561                 goto out;
562
563         /*
564          * Don't allow unmounting the root file system.
565          */
566         if (mp->mnt_flag & MNT_ROOTFS) {
567                 error = EINVAL;
568                 goto out;
569         }
570
571         /*
572          * Must be the root of the filesystem
573          */
574         if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
575                 error = EINVAL;
576                 goto out;
577         }
578
579 out:
580         nlookup_done(&nd);
581         if (error)
582                 return (error);
583         return (dounmount(mp, uap->flags));
584 }
585
586 /*
587  * Do the actual file system unmount.
588  */
589 static int
590 dounmount_interlock(struct mount *mp)
591 {
592         if (mp->mnt_kern_flag & MNTK_UNMOUNT)
593                 return (EBUSY);
594         mp->mnt_kern_flag |= MNTK_UNMOUNT;
595         return(0);
596 }
597
598 int
599 dounmount(struct mount *mp, int flags)
600 {
601         struct namecache *ncp;
602         struct nchandle nch;
603         int error;
604         int async_flag;
605         int lflags;
606         int freeok = 1;
607
608         /*
609          * Exclusive access for unmounting purposes
610          */
611         if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
612                 return (error);
613
614         /*
615          * Allow filesystems to detect that a forced unmount is in progress.
616          */
617         if (flags & MNT_FORCE)
618                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
619         lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
620         error = lockmgr(&mp->mnt_lock, lflags);
621         if (error) {
622                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
623                 if (mp->mnt_kern_flag & MNTK_MWAIT)
624                         wakeup(mp);
625                 return (error);
626         }
627
628         if (mp->mnt_flag & MNT_EXPUBLIC)
629                 vfs_setpublicfs(NULL, NULL, NULL);
630
631         vfs_msync(mp, MNT_WAIT);
632         async_flag = mp->mnt_flag & MNT_ASYNC;
633         mp->mnt_flag &=~ MNT_ASYNC;
634
635         /*
636          * If this filesystem isn't aliasing other filesystems,
637          * try to invalidate any remaining namecache entries and
638          * check the count afterwords.
639          */
640         if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
641                 cache_lock(&mp->mnt_ncmountpt);
642                 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
643                 cache_unlock(&mp->mnt_ncmountpt);
644
645                 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
646                     (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
647
648                         if ((flags & MNT_FORCE) == 0) {
649                                 error = EBUSY;
650                                 mount_warning(mp, "Cannot unmount: "
651                                                   "%d namecache "
652                                                   "references still "
653                                                   "present",
654                                                   ncp->nc_refs - 1);
655                         } else {
656                                 mount_warning(mp, "Forced unmount: "
657                                                   "%d namecache "
658                                                   "references still "
659                                                   "present",
660                                                   ncp->nc_refs - 1);
661                                 freeok = 0;
662                         }
663                 }
664         }
665
666         /*
667          * nchandle records ref the mount structure.  Expect a count of 1
668          * (our mount->mnt_ncmountpt).
669          */
670         if (mp->mnt_refs != 1) {
671                 if ((flags & MNT_FORCE) == 0) {
672                         mount_warning(mp, "Cannot unmount: "
673                                           "%d process references still "
674                                           "present", mp->mnt_refs);
675                         error = EBUSY;
676                 } else {
677                         mount_warning(mp, "Forced unmount: "
678                                           "%d process references still "
679                                           "present", mp->mnt_refs);
680                         freeok = 0;
681                 }
682         }
683
684         if (error == 0) {
685                 if (mp->mnt_syncer != NULL)
686                         vrele(mp->mnt_syncer);
687                 if (((mp->mnt_flag & MNT_RDONLY) ||
688                      (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
689                     (flags & MNT_FORCE)) {
690                         error = VFS_UNMOUNT(mp, flags);
691                 }
692         }
693         if (error) {
694                 if (mp->mnt_syncer == NULL)
695                         vfs_allocate_syncvnode(mp);
696                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
697                 mp->mnt_flag |= async_flag;
698                 lockmgr(&mp->mnt_lock, LK_RELEASE);
699                 if (mp->mnt_kern_flag & MNTK_MWAIT)
700                         wakeup(mp);
701                 return (error);
702         }
703         /*
704          * Clean up any journals still associated with the mount after
705          * filesystem activity has ceased.
706          */
707         journal_remove_all_journals(mp, 
708             ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
709
710         mountlist_remove(mp);
711
712         /*
713          * Remove any installed vnode ops here so the individual VFSs don't
714          * have to.
715          */
716         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
717         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
718         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
719         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
720         vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
721
722         if (mp->mnt_ncmountpt.ncp != NULL) {
723                 nch = mp->mnt_ncmountpt;
724                 cache_zero(&mp->mnt_ncmountpt);
725                 cache_clrmountpt(&nch);
726                 cache_drop(&nch);
727         }
728         if (mp->mnt_ncmounton.ncp != NULL) {
729                 nch = mp->mnt_ncmounton;
730                 cache_zero(&mp->mnt_ncmounton);
731                 cache_clrmountpt(&nch);
732                 cache_drop(&nch);
733         }
734
735         mp->mnt_vfc->vfc_refcount--;
736         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
737                 panic("unmount: dangling vnode");
738         lockmgr(&mp->mnt_lock, LK_RELEASE);
739         if (mp->mnt_kern_flag & MNTK_MWAIT)
740                 wakeup(mp);
741         if (freeok)
742                 kfree(mp, M_MOUNT);
743         return (0);
744 }
745
746 static
747 void
748 mount_warning(struct mount *mp, const char *ctl, ...)
749 {
750         char *ptr;
751         char *buf;
752         __va_list va;
753
754         __va_start(va, ctl);
755         if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
756                 kprintf("unmount(%s): ", ptr);
757                 kvprintf(ctl, va);
758                 kprintf("\n");
759                 kfree(buf, M_TEMP);
760         } else {
761                 kprintf("unmount(%p", mp);
762                 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
763                         kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
764                 kprintf("): ");
765                 kvprintf(ctl, va);
766                 kprintf("\n");
767         }
768         __va_end(va);
769 }
770
771 /*
772  * Shim cache_fullpath() to handle the case where a process is chrooted into
773  * a subdirectory of a mount.  In this case if the root mount matches the
774  * process root directory's mount we have to specify the process's root
775  * directory instead of the mount point, because the mount point might
776  * be above the root directory.
777  */
778 static
779 int
780 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
781 {
782         struct nchandle *nch;
783
784         if (p && p->p_fd->fd_nrdir.mount == mp)
785                 nch = &p->p_fd->fd_nrdir;
786         else
787                 nch = &mp->mnt_ncmountpt;
788         return(cache_fullpath(p, nch, rb, fb));
789 }
790
791 /*
792  * Sync each mounted filesystem.
793  */
794
795 #ifdef DEBUG
796 static int syncprt = 0;
797 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
798 #endif /* DEBUG */
799
800 static int sync_callback(struct mount *mp, void *data);
801
802 /* ARGSUSED */
803 int
804 sys_sync(struct sync_args *uap)
805 {
806         mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
807 #ifdef DEBUG
808         /*
809          * print out buffer pool stat information on each sync() call.
810          */
811         if (syncprt)
812                 vfs_bufstats();
813 #endif /* DEBUG */
814         return (0);
815 }
816
817 static
818 int
819 sync_callback(struct mount *mp, void *data __unused)
820 {
821         int asyncflag;
822
823         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
824                 asyncflag = mp->mnt_flag & MNT_ASYNC;
825                 mp->mnt_flag &= ~MNT_ASYNC;
826                 vfs_msync(mp, MNT_NOWAIT);
827                 VFS_SYNC(mp, MNT_NOWAIT);
828                 mp->mnt_flag |= asyncflag;
829         }
830         return(0);
831 }
832
833 /* XXX PRISON: could be per prison flag */
834 static int prison_quotas;
835 #if 0
836 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
837 #endif
838
839 /*
840  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
841  *
842  * Change filesystem quotas.
843  */
844 /* ARGSUSED */
845 int
846 sys_quotactl(struct quotactl_args *uap)
847 {
848         struct nlookupdata nd;
849         struct thread *td;
850         struct proc *p;
851         struct mount *mp;
852         int error;
853
854         td = curthread;
855         p = td->td_proc;
856         if (p->p_ucred->cr_prison && !prison_quotas)
857                 return (EPERM);
858
859         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
860         if (error == 0)
861                 error = nlookup(&nd);
862         if (error == 0) {
863                 mp = nd.nl_nch.mount;
864                 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
865                                     uap->arg, nd.nl_cred);
866         }
867         nlookup_done(&nd);
868         return (error);
869 }
870
871 /*
872  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
873  *              void *buf, int buflen)
874  *
875  * This function operates on a mount point and executes the specified
876  * operation using the specified control data, and possibly returns data.
877  *
878  * The actual number of bytes stored in the result buffer is returned, 0
879  * if none, otherwise an error is returned.
880  */
881 /* ARGSUSED */
882 int
883 sys_mountctl(struct mountctl_args *uap)
884 {
885         struct thread *td = curthread;
886         struct proc *p = td->td_proc;
887         struct file *fp;
888         void *ctl = NULL;
889         void *buf = NULL;
890         char *path = NULL;
891         int error;
892
893         /*
894          * Sanity and permissions checks.  We must be root.
895          */
896         KKASSERT(p);
897         if (p->p_ucred->cr_prison != NULL)
898                 return (EPERM);
899         if ((error = suser(td)) != 0)
900                 return (error);
901
902         /*
903          * Argument length checks
904          */
905         if (uap->ctllen < 0 || uap->ctllen > 1024)
906                 return (EINVAL);
907         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
908                 return (EINVAL);
909         if (uap->path == NULL)
910                 return (EINVAL);
911
912         /*
913          * Allocate the necessary buffers and copyin data
914          */
915         path = objcache_get(namei_oc, M_WAITOK);
916         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
917         if (error)
918                 goto done;
919
920         if (uap->ctllen) {
921                 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
922                 error = copyin(uap->ctl, ctl, uap->ctllen);
923                 if (error)
924                         goto done;
925         }
926         if (uap->buflen)
927                 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
928
929         /*
930          * Validate the descriptor
931          */
932         if (uap->fd >= 0) {
933                 fp = holdfp(p->p_fd, uap->fd, -1);
934                 if (fp == NULL) {
935                         error = EBADF;
936                         goto done;
937                 }
938         } else {
939                 fp = NULL;
940         }
941
942         /*
943          * Execute the internal kernel function and clean up.
944          */
945         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
946         if (fp)
947                 fdrop(fp);
948         if (error == 0 && uap->sysmsg_result > 0)
949                 error = copyout(buf, uap->buf, uap->sysmsg_result);
950 done:
951         if (path)
952                 objcache_put(namei_oc, path);
953         if (ctl)
954                 kfree(ctl, M_TEMP);
955         if (buf)
956                 kfree(buf, M_TEMP);
957         return (error);
958 }
959
960 /*
961  * Execute a mount control operation by resolving the path to a mount point
962  * and calling vop_mountctl().  
963  */
964 int
965 kern_mountctl(const char *path, int op, struct file *fp, 
966                 const void *ctl, int ctllen, 
967                 void *buf, int buflen, int *res)
968 {
969         struct vnode *vp;
970         struct mount *mp;
971         struct nlookupdata nd;
972         int error;
973
974         *res = 0;
975         vp = NULL;
976         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
977         if (error == 0)
978                 error = nlookup(&nd);
979         if (error == 0)
980                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
981         nlookup_done(&nd);
982         if (error)
983                 return (error);
984
985         mp = vp->v_mount;
986
987         /*
988          * Must be the root of the filesystem
989          */
990         if ((vp->v_flag & VROOT) == 0) {
991                 vput(vp);
992                 return (EINVAL);
993         }
994         error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, 
995                                 buf, buflen, res);
996         vput(vp);
997         return (error);
998 }
999
1000 int
1001 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1002 {
1003         struct thread *td = curthread;
1004         struct proc *p = td->td_proc;
1005         struct mount *mp;
1006         struct statfs *sp;
1007         char *fullpath, *freepath;
1008         int error;
1009
1010         if ((error = nlookup(nd)) != 0)
1011                 return (error);
1012         mp = nd->nl_nch.mount;
1013         sp = &mp->mnt_stat;
1014         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1015                 return (error);
1016
1017         error = mount_path(p, mp, &fullpath, &freepath);
1018         if (error)
1019                 return(error);
1020         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1021         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1022         kfree(freepath, M_TEMP);
1023
1024         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1025         bcopy(sp, buf, sizeof(*buf));
1026         /* Only root should have access to the fsid's. */
1027         if (suser(td))
1028                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1029         return (0);
1030 }
1031
1032 /*
1033  * statfs_args(char *path, struct statfs *buf)
1034  *
1035  * Get filesystem statistics.
1036  */
1037 int
1038 sys_statfs(struct statfs_args *uap)
1039 {
1040         struct nlookupdata nd;
1041         struct statfs buf;
1042         int error;
1043
1044         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1045         if (error == 0)
1046                 error = kern_statfs(&nd, &buf);
1047         nlookup_done(&nd);
1048         if (error == 0)
1049                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1050         return (error);
1051 }
1052
1053 int
1054 kern_fstatfs(int fd, struct statfs *buf)
1055 {
1056         struct thread *td = curthread;
1057         struct proc *p = td->td_proc;
1058         struct file *fp;
1059         struct mount *mp;
1060         struct statfs *sp;
1061         char *fullpath, *freepath;
1062         int error;
1063
1064         KKASSERT(p);
1065         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1066                 return (error);
1067         mp = ((struct vnode *)fp->f_data)->v_mount;
1068         if (mp == NULL) {
1069                 error = EBADF;
1070                 goto done;
1071         }
1072         if (fp->f_cred == NULL) {
1073                 error = EINVAL;
1074                 goto done;
1075         }
1076         sp = &mp->mnt_stat;
1077         if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1078                 goto done;
1079
1080         if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1081                 goto done;
1082         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1083         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1084         kfree(freepath, M_TEMP);
1085
1086         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1087         bcopy(sp, buf, sizeof(*buf));
1088
1089         /* Only root should have access to the fsid's. */
1090         if (suser(td))
1091                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1092         error = 0;
1093 done:
1094         fdrop(fp);
1095         return (error);
1096 }
1097
1098 /*
1099  * fstatfs_args(int fd, struct statfs *buf)
1100  *
1101  * Get filesystem statistics.
1102  */
1103 int
1104 sys_fstatfs(struct fstatfs_args *uap)
1105 {
1106         struct statfs buf;
1107         int error;
1108
1109         error = kern_fstatfs(uap->fd, &buf);
1110
1111         if (error == 0)
1112                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1113         return (error);
1114 }
1115
1116 /*
1117  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1118  *
1119  * Get statistics on all filesystems.
1120  */
1121
1122 struct getfsstat_info {
1123         struct statfs *sfsp;
1124         long count;
1125         long maxcount;
1126         int error;
1127         int flags;
1128         struct proc *p;
1129 };
1130
1131 static int getfsstat_callback(struct mount *, void *);
1132
1133 /* ARGSUSED */
1134 int
1135 sys_getfsstat(struct getfsstat_args *uap)
1136 {
1137         struct thread *td = curthread;
1138         struct proc *p = td->td_proc;
1139         struct getfsstat_info info;
1140
1141         bzero(&info, sizeof(info));
1142
1143         info.maxcount = uap->bufsize / sizeof(struct statfs);
1144         info.sfsp = uap->buf;
1145         info.count = 0;
1146         info.flags = uap->flags;
1147         info.p = p;
1148
1149         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1150         if (info.sfsp && info.count > info.maxcount)
1151                 uap->sysmsg_result = info.maxcount;
1152         else
1153                 uap->sysmsg_result = info.count;
1154         return (info.error);
1155 }
1156
1157 static int
1158 getfsstat_callback(struct mount *mp, void *data)
1159 {
1160         struct getfsstat_info *info = data;
1161         struct statfs *sp;
1162         char *freepath;
1163         char *fullpath;
1164         int error;
1165
1166         if (info->sfsp && info->count < info->maxcount) {
1167                 if (info->p && !chroot_visible_mnt(mp, info->p))
1168                         return(0);
1169                 sp = &mp->mnt_stat;
1170
1171                 /*
1172                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1173                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1174                  * overrides MNT_WAIT.
1175                  */
1176                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1177                     (info->flags & MNT_WAIT)) &&
1178                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1179                         return(0);
1180                 }
1181                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1182
1183                 error = mount_path(info->p, mp, &fullpath, &freepath);
1184                 if (error) {
1185                         info->error = error;
1186                         return(-1);
1187                 }
1188                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1189                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1190                 kfree(freepath, M_TEMP);
1191
1192                 error = copyout(sp, info->sfsp, sizeof(*sp));
1193                 if (error) {
1194                         info->error = error;
1195                         return (-1);
1196                 }
1197                 ++info->sfsp;
1198         }
1199         info->count++;
1200         return(0);
1201 }
1202
1203 /*
1204  * fchdir_args(int fd)
1205  *
1206  * Change current working directory to a given file descriptor.
1207  */
1208 /* ARGSUSED */
1209 int
1210 sys_fchdir(struct fchdir_args *uap)
1211 {
1212         struct thread *td = curthread;
1213         struct proc *p = td->td_proc;
1214         struct filedesc *fdp = p->p_fd;
1215         struct vnode *vp, *ovp;
1216         struct mount *mp;
1217         struct file *fp;
1218         struct nchandle nch, onch, tnch;
1219         int error;
1220
1221         if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1222                 return (error);
1223         vp = (struct vnode *)fp->f_data;
1224         vref(vp);
1225         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1226         if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1227                 error = ENOTDIR;
1228         else
1229                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1230         if (error) {
1231                 vput(vp);
1232                 fdrop(fp);
1233                 return (error);
1234         }
1235         cache_copy(&fp->f_nchandle, &nch);
1236
1237         /*
1238          * If the ncp has become a mount point, traverse through
1239          * the mount point.
1240          */
1241
1242         while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1243                (mp = cache_findmount(&nch)) != NULL
1244         ) {
1245                 error = nlookup_mp(mp, &tnch);
1246                 if (error == 0) {
1247                         cache_unlock(&tnch);    /* leave ref intact */
1248                         vput(vp);
1249                         vp = tnch.ncp->nc_vp;
1250                         error = vget(vp, LK_SHARED);
1251                         KKASSERT(error == 0);
1252                         cache_drop(&nch);
1253                         nch = tnch;
1254                 }
1255         }
1256         if (error == 0) {
1257                 ovp = fdp->fd_cdir;
1258                 onch = fdp->fd_ncdir;
1259                 vn_unlock(vp);          /* leave ref intact */
1260                 fdp->fd_cdir = vp;
1261                 fdp->fd_ncdir = nch;
1262                 cache_drop(&onch);
1263                 vrele(ovp);
1264         } else {
1265                 cache_drop(&nch);
1266                 vput(vp);
1267         }
1268         fdrop(fp);
1269         return (error);
1270 }
1271
1272 int
1273 kern_chdir(struct nlookupdata *nd)
1274 {
1275         struct thread *td = curthread;
1276         struct proc *p = td->td_proc;
1277         struct filedesc *fdp = p->p_fd;
1278         struct vnode *vp, *ovp;
1279         struct nchandle onch;
1280         int error;
1281
1282         if ((error = nlookup(nd)) != 0)
1283                 return (error);
1284         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1285                 return (ENOENT);
1286         if ((error = vget(vp, LK_SHARED)) != 0)
1287                 return (error);
1288
1289         error = checkvp_chdir(vp, td);
1290         vn_unlock(vp);
1291         if (error == 0) {
1292                 ovp = fdp->fd_cdir;
1293                 onch = fdp->fd_ncdir;
1294                 cache_unlock(&nd->nl_nch);      /* leave reference intact */
1295                 fdp->fd_ncdir = nd->nl_nch;
1296                 fdp->fd_cdir = vp;
1297                 cache_drop(&onch);
1298                 vrele(ovp);
1299                 cache_zero(&nd->nl_nch);
1300         } else {
1301                 vrele(vp);
1302         }
1303         return (error);
1304 }
1305
1306 /*
1307  * chdir_args(char *path)
1308  *
1309  * Change current working directory (``.'').
1310  */
1311 int
1312 sys_chdir(struct chdir_args *uap)
1313 {
1314         struct nlookupdata nd;
1315         int error;
1316
1317         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1318         if (error == 0)
1319                 error = kern_chdir(&nd);
1320         nlookup_done(&nd);
1321         return (error);
1322 }
1323
1324 /*
1325  * Helper function for raised chroot(2) security function:  Refuse if
1326  * any filedescriptors are open directories.
1327  */
1328 static int
1329 chroot_refuse_vdir_fds(struct filedesc *fdp)
1330 {
1331         struct vnode *vp;
1332         struct file *fp;
1333         int error;
1334         int fd;
1335
1336         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1337                 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1338                         continue;
1339                 vp = (struct vnode *)fp->f_data;
1340                 if (vp->v_type != VDIR) {
1341                         fdrop(fp);
1342                         continue;
1343                 }
1344                 fdrop(fp);
1345                 return(EPERM);
1346         }
1347         return (0);
1348 }
1349
1350 /*
1351  * This sysctl determines if we will allow a process to chroot(2) if it
1352  * has a directory open:
1353  *      0: disallowed for all processes.
1354  *      1: allowed for processes that were not already chroot(2)'ed.
1355  *      2: allowed for all processes.
1356  */
1357
1358 static int chroot_allow_open_directories = 1;
1359
1360 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1361      &chroot_allow_open_directories, 0, "");
1362
1363 /*
1364  * chroot to the specified namecache entry.  We obtain the vp from the
1365  * namecache data.  The passed ncp must be locked and referenced and will
1366  * remain locked and referenced on return.
1367  */
1368 int
1369 kern_chroot(struct nchandle *nch)
1370 {
1371         struct thread *td = curthread;
1372         struct proc *p = td->td_proc;
1373         struct filedesc *fdp = p->p_fd;
1374         struct vnode *vp;
1375         int error;
1376
1377         /*
1378          * Only root can chroot
1379          */
1380         if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1381                 return (error);
1382
1383         /*
1384          * Disallow open directory descriptors (fchdir() breakouts).
1385          */
1386         if (chroot_allow_open_directories == 0 ||
1387            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1388                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1389                         return (error);
1390         }
1391         if ((vp = nch->ncp->nc_vp) == NULL)
1392                 return (ENOENT);
1393
1394         if ((error = vget(vp, LK_SHARED)) != 0)
1395                 return (error);
1396
1397         /*
1398          * Check the validity of vp as a directory to change to and 
1399          * associate it with rdir/jdir.
1400          */
1401         error = checkvp_chdir(vp, td);
1402         vn_unlock(vp);                  /* leave reference intact */
1403         if (error == 0) {
1404                 vrele(fdp->fd_rdir);
1405                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1406                 cache_drop(&fdp->fd_nrdir);
1407                 cache_copy(nch, &fdp->fd_nrdir);
1408                 if (fdp->fd_jdir == NULL) {
1409                         fdp->fd_jdir = vp;
1410                         vref(fdp->fd_jdir);
1411                         cache_copy(nch, &fdp->fd_njdir);
1412                 }
1413         } else {
1414                 vrele(vp);
1415         }
1416         return (error);
1417 }
1418
1419 /*
1420  * chroot_args(char *path)
1421  *
1422  * Change notion of root (``/'') directory.
1423  */
1424 /* ARGSUSED */
1425 int
1426 sys_chroot(struct chroot_args *uap)
1427 {
1428         struct thread *td = curthread;
1429         struct nlookupdata nd;
1430         int error;
1431
1432         KKASSERT(td->td_proc);
1433         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1434         if (error) {
1435                 nlookup_done(&nd);
1436                 return(error);
1437         }
1438         error = nlookup(&nd);
1439         if (error == 0)
1440                 error = kern_chroot(&nd.nl_nch);
1441         nlookup_done(&nd);
1442         return(error);
1443 }
1444
1445 /*
1446  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1447  * determine whether it is legal to chdir to the vnode.  The vnode's state
1448  * is not changed by this call.
1449  */
1450 int
1451 checkvp_chdir(struct vnode *vp, struct thread *td)
1452 {
1453         int error;
1454
1455         if (vp->v_type != VDIR)
1456                 error = ENOTDIR;
1457         else
1458                 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1459         return (error);
1460 }
1461
1462 int
1463 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1464 {
1465         struct thread *td = curthread;
1466         struct proc *p = td->td_proc;
1467         struct lwp *lp = td->td_lwp;
1468         struct filedesc *fdp = p->p_fd;
1469         int cmode, flags;
1470         struct file *nfp;
1471         struct file *fp;
1472         struct vnode *vp;
1473         int type, indx, error;
1474         struct flock lf;
1475
1476         if ((oflags & O_ACCMODE) == O_ACCMODE)
1477                 return (EINVAL);
1478         flags = FFLAGS(oflags);
1479         error = falloc(p, &nfp, NULL);
1480         if (error)
1481                 return (error);
1482         fp = nfp;
1483         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1484
1485         /*
1486          * XXX p_dupfd is a real mess.  It allows a device to return a
1487          * file descriptor to be duplicated rather then doing the open
1488          * itself.
1489          */
1490         lp->lwp_dupfd = -1;
1491
1492         /*
1493          * Call vn_open() to do the lookup and assign the vnode to the 
1494          * file pointer.  vn_open() does not change the ref count on fp
1495          * and the vnode, on success, will be inherited by the file pointer
1496          * and unlocked.
1497          */
1498         nd->nl_flags |= NLC_LOCKVP;
1499         error = vn_open(nd, fp, flags, cmode);
1500         nlookup_done(nd);
1501         if (error) {
1502                 /*
1503                  * handle special fdopen() case.  bleh.  dupfdopen() is
1504                  * responsible for dropping the old contents of ofiles[indx]
1505                  * if it succeeds.
1506                  *
1507                  * Note that fsetfd() will add a ref to fp which represents
1508                  * the fd_files[] assignment.  We must still drop our
1509                  * reference.
1510                  */
1511                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1512                         if (fdalloc(p, 0, &indx) == 0) {
1513                                 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1514                                 if (error == 0) {
1515                                         *res = indx;
1516                                         fdrop(fp);      /* our ref */
1517                                         return (0);
1518                                 }
1519                                 fsetfd(p, NULL, indx);
1520                         }
1521                 }
1522                 fdrop(fp);      /* our ref */
1523                 if (error == ERESTART)
1524                         error = EINTR;
1525                 return (error);
1526         }
1527
1528         /*
1529          * ref the vnode for ourselves so it can't be ripped out from under
1530          * is.  XXX need an ND flag to request that the vnode be returned
1531          * anyway.
1532          *
1533          * Reserve a file descriptor but do not assign it until the open
1534          * succeeds.
1535          */
1536         vp = (struct vnode *)fp->f_data;
1537         vref(vp);
1538         if ((error = fdalloc(p, 0, &indx)) != 0) {
1539                 fdrop(fp);
1540                 vrele(vp);
1541                 return (error);
1542         }
1543
1544         /*
1545          * If no error occurs the vp will have been assigned to the file
1546          * pointer.
1547          */
1548         lp->lwp_dupfd = 0;
1549
1550         if (flags & (O_EXLOCK | O_SHLOCK)) {
1551                 lf.l_whence = SEEK_SET;
1552                 lf.l_start = 0;
1553                 lf.l_len = 0;
1554                 if (flags & O_EXLOCK)
1555                         lf.l_type = F_WRLCK;
1556                 else
1557                         lf.l_type = F_RDLCK;
1558                 if (flags & FNONBLOCK)
1559                         type = 0;
1560                 else
1561                         type = F_WAIT;
1562
1563                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1564                         /*
1565                          * lock request failed.  Clean up the reserved
1566                          * descriptor.
1567                          */
1568                         vrele(vp);
1569                         fsetfd(p, NULL, indx);
1570                         fdrop(fp);
1571                         return (error);
1572                 }
1573                 fp->f_flag |= FHASLOCK;
1574         }
1575 #if 0
1576         /*
1577          * Assert that all regular file vnodes were created with a object.
1578          */
1579         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1580                 ("open: regular file has no backing object after vn_open"));
1581 #endif
1582
1583         vrele(vp);
1584
1585         /*
1586          * release our private reference, leaving the one associated with the
1587          * descriptor table intact.
1588          */
1589         fsetfd(p, fp, indx);
1590         fdrop(fp);
1591         *res = indx;
1592         return (0);
1593 }
1594
1595 /*
1596  * open_args(char *path, int flags, int mode)
1597  *
1598  * Check permissions, allocate an open file structure,
1599  * and call the device open routine if any.
1600  */
1601 int
1602 sys_open(struct open_args *uap)
1603 {
1604         struct nlookupdata nd;
1605         int error;
1606
1607         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1608         if (error == 0) {
1609                 error = kern_open(&nd, uap->flags,
1610                                     uap->mode, &uap->sysmsg_result);
1611         }
1612         nlookup_done(&nd);
1613         return (error);
1614 }
1615
1616 int
1617 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1618 {
1619         struct thread *td = curthread;
1620         struct proc *p = td->td_proc;
1621         struct vnode *vp;
1622         struct vnode *dvp;
1623         struct vattr vattr;
1624         int error;
1625         int whiteout = 0;
1626
1627         KKASSERT(p);
1628
1629         switch (mode & S_IFMT) {
1630         case S_IFCHR:
1631         case S_IFBLK:
1632                 error = suser(td);
1633                 break;
1634         default:
1635                 error = suser_cred(p->p_ucred, PRISON_ROOT);
1636                 break;
1637         }
1638         if (error)
1639                 return (error);
1640
1641         bwillwrite();
1642         nd->nl_flags |= NLC_CREATE;
1643         if ((error = nlookup(nd)) != 0)
1644                 return (error);
1645         if (nd->nl_nch.ncp->nc_vp)
1646                 return (EEXIST);
1647         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1648                 return (error);
1649         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1650                 return (EPERM);
1651         /* vhold(dvp); - DVP can't go away */
1652
1653         VATTR_NULL(&vattr);
1654         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1655         vattr.va_rmajor = rmajor;
1656         vattr.va_rminor = rminor;
1657         whiteout = 0;
1658
1659         switch (mode & S_IFMT) {
1660         case S_IFMT:    /* used by badsect to flag bad sectors */
1661                 vattr.va_type = VBAD;
1662                 break;
1663         case S_IFCHR:
1664                 vattr.va_type = VCHR;
1665                 break;
1666         case S_IFBLK:
1667                 vattr.va_type = VBLK;
1668                 break;
1669         case S_IFWHT:
1670                 whiteout = 1;
1671                 break;
1672         default:
1673                 error = EINVAL;
1674                 break;
1675         }
1676         if (error == 0) {
1677                 if (whiteout) {
1678                         error = VOP_NWHITEOUT(&nd->nl_nch, dvp, nd->nl_cred, NAMEI_CREATE);
1679                 } else {
1680                         vp = NULL;
1681                         error = VOP_NMKNOD(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr);
1682                         if (error == 0)
1683                                 vput(vp);
1684                 }
1685         }
1686         /* vdrop(dvp); */
1687         return (error);
1688 }
1689
1690 /*
1691  * mknod_args(char *path, int mode, int dev)
1692  *
1693  * Create a special file.
1694  */
1695 int
1696 sys_mknod(struct mknod_args *uap)
1697 {
1698         struct nlookupdata nd;
1699         int error;
1700
1701         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1702         if (error == 0) {
1703                 error = kern_mknod(&nd, uap->mode,
1704                                    umajor(uap->dev), uminor(uap->dev));
1705         }
1706         nlookup_done(&nd);
1707         return (error);
1708 }
1709
1710 int
1711 kern_mkfifo(struct nlookupdata *nd, int mode)
1712 {
1713         struct thread *td = curthread;
1714         struct proc *p = td->td_proc;
1715         struct vattr vattr;
1716         struct vnode *vp;
1717         struct vnode *dvp;
1718         int error;
1719
1720         bwillwrite();
1721
1722         nd->nl_flags |= NLC_CREATE;
1723         if ((error = nlookup(nd)) != 0)
1724                 return (error);
1725         if (nd->nl_nch.ncp->nc_vp)
1726                 return (EEXIST);
1727         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1728                 return (error);
1729         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1730                 return (EPERM);
1731         /* vhold(dvp); - DVP can't go away */
1732
1733         VATTR_NULL(&vattr);
1734         vattr.va_type = VFIFO;
1735         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1736         vp = NULL;
1737         error = VOP_NMKNOD(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr);
1738         /* vdrop(dvp); */
1739         if (error == 0)
1740                 vput(vp);
1741         return (error);
1742 }
1743
1744 /*
1745  * mkfifo_args(char *path, int mode)
1746  *
1747  * Create a named pipe.
1748  */
1749 int
1750 sys_mkfifo(struct mkfifo_args *uap)
1751 {
1752         struct nlookupdata nd;
1753         int error;
1754
1755         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1756         if (error == 0)
1757                 error = kern_mkfifo(&nd, uap->mode);
1758         nlookup_done(&nd);
1759         return (error);
1760 }
1761
1762 static int hardlink_check_uid = 0;
1763 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1764     &hardlink_check_uid, 0, 
1765     "Unprivileged processes cannot create hard links to files owned by other "
1766     "users");
1767 static int hardlink_check_gid = 0;
1768 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1769     &hardlink_check_gid, 0,
1770     "Unprivileged processes cannot create hard links to files owned by other "
1771     "groups");
1772
1773 static int
1774 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1775 {
1776         struct vattr va;
1777         int error;
1778
1779         /*
1780          * Shortcut if disabled
1781          */
1782         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1783                 return (0);
1784
1785         /*
1786          * root cred can always hardlink
1787          */
1788         if (suser_cred(cred, PRISON_ROOT) == 0)
1789                 return (0);
1790
1791         /*
1792          * Otherwise only if the originating file is owned by the
1793          * same user or group.  Note that any group is allowed if
1794          * the file is owned by the caller.
1795          */
1796         error = VOP_GETATTR(vp, &va);
1797         if (error != 0)
1798                 return (error);
1799         
1800         if (hardlink_check_uid) {
1801                 if (cred->cr_uid != va.va_uid)
1802                         return (EPERM);
1803         }
1804         
1805         if (hardlink_check_gid) {
1806                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1807                         return (EPERM);
1808         }
1809
1810         return (0);
1811 }
1812
1813 int
1814 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1815 {
1816         struct thread *td = curthread;
1817         struct vnode *vp;
1818         struct vnode *dvp;
1819         int error;
1820
1821         /*
1822          * Lookup the source and obtained a locked vnode.
1823          *
1824          * XXX relookup on vget failure / race ?
1825          */
1826         bwillwrite();
1827         if ((error = nlookup(nd)) != 0)
1828                 return (error);
1829         vp = nd->nl_nch.ncp->nc_vp;
1830         KKASSERT(vp != NULL);
1831         if (vp->v_type == VDIR)
1832                 return (EPERM);         /* POSIX */
1833         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1834                 return (error);
1835         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1836                 return (error);
1837
1838         /*
1839          * Unlock the source so we can lookup the target without deadlocking
1840          * (XXX vp is locked already, possible other deadlock?).  The target
1841          * must not exist.
1842          */
1843         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1844         nd->nl_flags &= ~NLC_NCPISLOCKED;
1845         cache_unlock(&nd->nl_nch);
1846
1847         linknd->nl_flags |= NLC_CREATE;
1848         if ((error = nlookup(linknd)) != 0) {
1849                 vput(vp);
1850                 return (error);
1851         }
1852         if (linknd->nl_nch.ncp->nc_vp) {
1853                 vput(vp);
1854                 return (EEXIST);
1855         }
1856         if ((dvp = linknd->nl_nch.ncp->nc_parent->nc_vp) == NULL) {
1857                 vput(vp);
1858                 return (EPERM);
1859         }
1860         /* vhold(dvp); - dvp can't go away */
1861
1862         /*
1863          * Finally run the new API VOP.
1864          */
1865         error = can_hardlink(vp, td, td->td_proc->p_ucred);
1866         if (error == 0)
1867                 error = VOP_NLINK(&linknd->nl_nch, dvp, vp, linknd->nl_cred);
1868         /* vdrop(dvp); */
1869         vput(vp);
1870         return (error);
1871 }
1872
1873 /*
1874  * link_args(char *path, char *link)
1875  *
1876  * Make a hard file link.
1877  */
1878 int
1879 sys_link(struct link_args *uap)
1880 {
1881         struct nlookupdata nd, linknd;
1882         int error;
1883
1884         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1885         if (error == 0) {
1886                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1887                 if (error == 0)
1888                         error = kern_link(&nd, &linknd);
1889                 nlookup_done(&linknd);
1890         }
1891         nlookup_done(&nd);
1892         return (error);
1893 }
1894
1895 int
1896 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1897 {
1898         struct vattr vattr;
1899         struct vnode *vp;
1900         struct vnode *dvp;
1901         int error;
1902
1903         bwillwrite();
1904         nd->nl_flags |= NLC_CREATE;
1905         if ((error = nlookup(nd)) != 0)
1906                 return (error);
1907         if (nd->nl_nch.ncp->nc_vp)
1908                 return (EEXIST);
1909         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1910                 return (error);
1911         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1912                 return (EPERM);
1913         /* vhold(dvp); - dvp can't go away */
1914         VATTR_NULL(&vattr);
1915         vattr.va_mode = mode;
1916         error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
1917         /* vdrop(dvp); */
1918         if (error == 0)
1919                 vput(vp);
1920         return (error);
1921 }
1922
1923 /*
1924  * symlink(char *path, char *link)
1925  *
1926  * Make a symbolic link.
1927  */
1928 int
1929 sys_symlink(struct symlink_args *uap)
1930 {
1931         struct thread *td = curthread;
1932         struct nlookupdata nd;
1933         char *path;
1934         int error;
1935         int mode;
1936
1937         path = objcache_get(namei_oc, M_WAITOK);
1938         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1939         if (error == 0) {
1940                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1941                 if (error == 0) {
1942                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1943                         error = kern_symlink(&nd, path, mode);
1944                 }
1945                 nlookup_done(&nd);
1946         }
1947         objcache_put(namei_oc, path);
1948         return (error);
1949 }
1950
1951 /*
1952  * undelete_args(char *path)
1953  *
1954  * Delete a whiteout from the filesystem.
1955  */
1956 /* ARGSUSED */
1957 int
1958 sys_undelete(struct undelete_args *uap)
1959 {
1960         struct nlookupdata nd;
1961         struct vnode *dvp;
1962         int error;
1963
1964         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1965         bwillwrite();
1966         nd.nl_flags |= NLC_DELETE;
1967         if (error == 0)
1968                 error = nlookup(&nd);
1969         if (error == 0)
1970                 error = ncp_writechk(&nd.nl_nch);
1971         dvp = NULL;
1972         if (error == 0) {
1973                 if ((dvp = nd.nl_nch.ncp->nc_parent->nc_vp) == NULL)
1974                         error = EPERM;
1975         }
1976         if (error == 0) {
1977                 /* vhold(dvp); - dvp can't go away */
1978                 error = VOP_NWHITEOUT(&nd.nl_nch, dvp, nd.nl_cred, NAMEI_DELETE);
1979                 /* vdrop(dvp); */
1980         }
1981         nlookup_done(&nd);
1982         return (error);
1983 }
1984
1985 int
1986 kern_unlink(struct nlookupdata *nd)
1987 {
1988         struct vnode *dvp;
1989         int error;
1990
1991         bwillwrite();
1992         nd->nl_flags |= NLC_DELETE;
1993         if ((error = nlookup(nd)) != 0)
1994                 return (error);
1995         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1996                 return (error);
1997         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1998                 return (EPERM);
1999         /* vhold(dvp); - dvp can't go away */
2000         error = VOP_NREMOVE(&nd->nl_nch, dvp, nd->nl_cred);
2001         /* vdrop(dvp); */
2002         return (error);
2003 }
2004
2005 /*
2006  * unlink_args(char *path)
2007  *
2008  * Delete a name from the filesystem.
2009  */
2010 int
2011 sys_unlink(struct unlink_args *uap)
2012 {
2013         struct nlookupdata nd;
2014         int error;
2015
2016         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2017         if (error == 0)
2018                 error = kern_unlink(&nd);
2019         nlookup_done(&nd);
2020         return (error);
2021 }
2022
2023 int
2024 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2025 {
2026         struct thread *td = curthread;
2027         struct proc *p = td->td_proc;
2028         struct file *fp;
2029         struct vattr vattr;
2030         int error;
2031
2032         fp = holdfp(p->p_fd, fd, -1);
2033         if (fp == NULL)
2034                 return (EBADF);
2035         if (fp->f_type != DTYPE_VNODE) {
2036                 error = ESPIPE;
2037                 goto done;
2038         }
2039
2040         switch (whence) {
2041         case L_INCR:
2042                 fp->f_offset += offset;
2043                 error = 0;
2044                 break;
2045         case L_XTND:
2046                 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2047                 if (error == 0)
2048                         fp->f_offset = offset + vattr.va_size;
2049                 break;
2050         case L_SET:
2051                 fp->f_offset = offset;
2052                 error = 0;
2053                 break;
2054         default:
2055                 error = EINVAL;
2056                 break;
2057         }
2058         *res = fp->f_offset;
2059 done:
2060         fdrop(fp);
2061         return (error);
2062 }
2063
2064 /*
2065  * lseek_args(int fd, int pad, off_t offset, int whence)
2066  *
2067  * Reposition read/write file offset.
2068  */
2069 int
2070 sys_lseek(struct lseek_args *uap)
2071 {
2072         int error;
2073
2074         error = kern_lseek(uap->fd, uap->offset, uap->whence,
2075             &uap->sysmsg_offset);
2076
2077         return (error);
2078 }
2079
2080 int
2081 kern_access(struct nlookupdata *nd, int aflags)
2082 {
2083         struct vnode *vp;
2084         int error, flags;
2085
2086         if ((error = nlookup(nd)) != 0)
2087                 return (error);
2088 retry:
2089         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2090         if (error)
2091                 return (error);
2092
2093         /* Flags == 0 means only check for existence. */
2094         if (aflags) {
2095                 flags = 0;
2096                 if (aflags & R_OK)
2097                         flags |= VREAD;
2098                 if (aflags & W_OK)
2099                         flags |= VWRITE;
2100                 if (aflags & X_OK)
2101                         flags |= VEXEC;
2102                 if ((flags & VWRITE) == 0 || 
2103                     (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2104                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
2105
2106                 /*
2107                  * If the file handle is stale we have to re-resolve the
2108                  * entry.  This is a hack at the moment.
2109                  */
2110                 if (error == ESTALE) {
2111                         cache_setunresolved(&nd->nl_nch);
2112                         error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2113                         if (error == 0) {
2114                                 vput(vp);
2115                                 vp = NULL;
2116                                 goto retry;
2117                         }
2118                 }
2119         }
2120         vput(vp);
2121         return (error);
2122 }
2123
2124 /*
2125  * access_args(char *path, int flags)
2126  *
2127  * Check access permissions.
2128  */
2129 int
2130 sys_access(struct access_args *uap)
2131 {
2132         struct nlookupdata nd;
2133         int error;
2134
2135         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2136         if (error == 0)
2137                 error = kern_access(&nd, uap->flags);
2138         nlookup_done(&nd);
2139         return (error);
2140 }
2141
2142 int
2143 kern_stat(struct nlookupdata *nd, struct stat *st)
2144 {
2145         int error;
2146         struct vnode *vp;
2147         thread_t td;
2148
2149         if ((error = nlookup(nd)) != 0)
2150                 return (error);
2151 again:
2152         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2153                 return (ENOENT);
2154
2155         td = curthread;
2156         if ((error = vget(vp, LK_SHARED)) != 0)
2157                 return (error);
2158         error = vn_stat(vp, st, nd->nl_cred);
2159
2160         /*
2161          * If the file handle is stale we have to re-resolve the entry.  This
2162          * is a hack at the moment.
2163          */
2164         if (error == ESTALE) {
2165                 cache_setunresolved(&nd->nl_nch);
2166                 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2167                 if (error == 0) {
2168                         vput(vp);
2169                         goto again;
2170                 }
2171         }
2172         vput(vp);
2173         return (error);
2174 }
2175
2176 /*
2177  * stat_args(char *path, struct stat *ub)
2178  *
2179  * Get file status; this version follows links.
2180  */
2181 int
2182 sys_stat(struct stat_args *uap)
2183 {
2184         struct nlookupdata nd;
2185         struct stat st;
2186         int error;
2187
2188         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2189         if (error == 0) {
2190                 error = kern_stat(&nd, &st);
2191                 if (error == 0)
2192                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2193         }
2194         nlookup_done(&nd);
2195         return (error);
2196 }
2197
2198 /*
2199  * lstat_args(char *path, struct stat *ub)
2200  *
2201  * Get file status; this version does not follow links.
2202  */
2203 int
2204 sys_lstat(struct lstat_args *uap)
2205 {
2206         struct nlookupdata nd;
2207         struct stat st;
2208         int error;
2209
2210         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2211         if (error == 0) {
2212                 error = kern_stat(&nd, &st);
2213                 if (error == 0)
2214                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2215         }
2216         nlookup_done(&nd);
2217         return (error);
2218 }
2219
2220 /*
2221  * pathconf_Args(char *path, int name)
2222  *
2223  * Get configurable pathname variables.
2224  */
2225 /* ARGSUSED */
2226 int
2227 sys_pathconf(struct pathconf_args *uap)
2228 {
2229         struct nlookupdata nd;
2230         struct vnode *vp;
2231         int error;
2232
2233         vp = NULL;
2234         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2235         if (error == 0)
2236                 error = nlookup(&nd);
2237         if (error == 0)
2238                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2239         nlookup_done(&nd);
2240         if (error == 0) {
2241                 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2242                 vput(vp);
2243         }
2244         return (error);
2245 }
2246
2247 /*
2248  * XXX: daver
2249  * kern_readlink isn't properly split yet.  There is a copyin burried
2250  * in VOP_READLINK().
2251  */
2252 int
2253 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2254 {
2255         struct thread *td = curthread;
2256         struct proc *p = td->td_proc;
2257         struct vnode *vp;
2258         struct iovec aiov;
2259         struct uio auio;
2260         int error;
2261
2262         if ((error = nlookup(nd)) != 0)
2263                 return (error);
2264         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2265         if (error)
2266                 return (error);
2267         if (vp->v_type != VLNK) {
2268                 error = EINVAL;
2269         } else {
2270                 aiov.iov_base = buf;
2271                 aiov.iov_len = count;
2272                 auio.uio_iov = &aiov;
2273                 auio.uio_iovcnt = 1;
2274                 auio.uio_offset = 0;
2275                 auio.uio_rw = UIO_READ;
2276                 auio.uio_segflg = UIO_USERSPACE;
2277                 auio.uio_td = td;
2278                 auio.uio_resid = count;
2279                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2280         }
2281         vput(vp);
2282         *res = count - auio.uio_resid;
2283         return (error);
2284 }
2285
2286 /*
2287  * readlink_args(char *path, char *buf, int count)
2288  *
2289  * Return target name of a symbolic link.
2290  */
2291 int
2292 sys_readlink(struct readlink_args *uap)
2293 {
2294         struct nlookupdata nd;
2295         int error;
2296
2297         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2298         if (error == 0) {
2299                 error = kern_readlink(&nd, uap->buf, uap->count,
2300                                         &uap->sysmsg_result);
2301         }
2302         nlookup_done(&nd);
2303         return (error);
2304 }
2305
2306 static int
2307 setfflags(struct vnode *vp, int flags)
2308 {
2309         struct thread *td = curthread;
2310         struct proc *p = td->td_proc;
2311         int error;
2312         struct vattr vattr;
2313
2314         /*
2315          * Prevent non-root users from setting flags on devices.  When
2316          * a device is reused, users can retain ownership of the device
2317          * if they are allowed to set flags and programs assume that
2318          * chown can't fail when done as root.
2319          */
2320         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2321             ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2322                 return (error);
2323
2324         /*
2325          * note: vget is required for any operation that might mod the vnode
2326          * so VINACTIVE is properly cleared.
2327          */
2328         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2329                 VATTR_NULL(&vattr);
2330                 vattr.va_flags = flags;
2331                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2332                 vput(vp);
2333         }
2334         return (error);
2335 }
2336
2337 /*
2338  * chflags(char *path, int flags)
2339  *
2340  * Change flags of a file given a path name.
2341  */
2342 /* ARGSUSED */
2343 int
2344 sys_chflags(struct chflags_args *uap)
2345 {
2346         struct nlookupdata nd;
2347         struct vnode *vp;
2348         int error;
2349
2350         vp = NULL;
2351         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2352         /* XXX Add NLC flag indicating modifying operation? */
2353         if (error == 0)
2354                 error = nlookup(&nd);
2355         if (error == 0)
2356                 error = ncp_writechk(&nd.nl_nch);
2357         if (error == 0)
2358                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2359         nlookup_done(&nd);
2360         if (error == 0) {
2361                 error = setfflags(vp, uap->flags);
2362                 vrele(vp);
2363         }
2364         return (error);
2365 }
2366
2367 /*
2368  * fchflags_args(int fd, int flags)
2369  *
2370  * Change flags of a file given a file descriptor.
2371  */
2372 /* ARGSUSED */
2373 int
2374 sys_fchflags(struct fchflags_args *uap)
2375 {
2376         struct thread *td = curthread;
2377         struct proc *p = td->td_proc;
2378         struct file *fp;
2379         int error;
2380
2381         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2382                 return (error);
2383         if (fp->f_nchandle.ncp)
2384                 error = ncp_writechk(&fp->f_nchandle);
2385         if (error == 0)
2386                 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2387         fdrop(fp);
2388         return (error);
2389 }
2390
2391 static int
2392 setfmode(struct vnode *vp, int mode)
2393 {
2394         struct thread *td = curthread;
2395         struct proc *p = td->td_proc;
2396         int error;
2397         struct vattr vattr;
2398
2399         /*
2400          * note: vget is required for any operation that might mod the vnode
2401          * so VINACTIVE is properly cleared.
2402          */
2403         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2404                 VATTR_NULL(&vattr);
2405                 vattr.va_mode = mode & ALLPERMS;
2406                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2407                 vput(vp);
2408         }
2409         return error;
2410 }
2411
2412 int
2413 kern_chmod(struct nlookupdata *nd, int mode)
2414 {
2415         struct vnode *vp;
2416         int error;
2417
2418         /* XXX Add NLC flag indicating modifying operation? */
2419         if ((error = nlookup(nd)) != 0)
2420                 return (error);
2421         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2422                 return (error);
2423         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2424                 error = setfmode(vp, mode);
2425         vrele(vp);
2426         return (error);
2427 }
2428
2429 /*
2430  * chmod_args(char *path, int mode)
2431  *
2432  * Change mode of a file given path name.
2433  */
2434 /* ARGSUSED */
2435 int
2436 sys_chmod(struct chmod_args *uap)
2437 {
2438         struct nlookupdata nd;
2439         int error;
2440
2441         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2442         if (error == 0)
2443                 error = kern_chmod(&nd, uap->mode);
2444         nlookup_done(&nd);
2445         return (error);
2446 }
2447
2448 /*
2449  * lchmod_args(char *path, int mode)
2450  *
2451  * Change mode of a file given path name (don't follow links.)
2452  */
2453 /* ARGSUSED */
2454 int
2455 sys_lchmod(struct lchmod_args *uap)
2456 {
2457         struct nlookupdata nd;
2458         int error;
2459
2460         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2461         if (error == 0)
2462                 error = kern_chmod(&nd, uap->mode);
2463         nlookup_done(&nd);
2464         return (error);
2465 }
2466
2467 /*
2468  * fchmod_args(int fd, int mode)
2469  *
2470  * Change mode of a file given a file descriptor.
2471  */
2472 /* ARGSUSED */
2473 int
2474 sys_fchmod(struct fchmod_args *uap)
2475 {
2476         struct thread *td = curthread;
2477         struct proc *p = td->td_proc;
2478         struct file *fp;
2479         int error;
2480
2481         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2482                 return (error);
2483         if (fp->f_nchandle.ncp)
2484                 error = ncp_writechk(&fp->f_nchandle);
2485         if (error == 0)
2486                 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2487         fdrop(fp);
2488         return (error);
2489 }
2490
2491 static int
2492 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2493 {
2494         struct thread *td = curthread;
2495         struct proc *p = td->td_proc;
2496         int error;
2497         struct vattr vattr;
2498
2499         /*
2500          * note: vget is required for any operation that might mod the vnode
2501          * so VINACTIVE is properly cleared.
2502          */
2503         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2504                 VATTR_NULL(&vattr);
2505                 vattr.va_uid = uid;
2506                 vattr.va_gid = gid;
2507                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2508                 vput(vp);
2509         }
2510         return error;
2511 }
2512
2513 int
2514 kern_chown(struct nlookupdata *nd, int uid, int gid)
2515 {
2516         struct vnode *vp;
2517         int error;
2518
2519         /* XXX Add NLC flag indicating modifying operation? */
2520         if ((error = nlookup(nd)) != 0)
2521                 return (error);
2522         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2523                 return (error);
2524         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2525                 error = setfown(vp, uid, gid);
2526         vrele(vp);
2527         return (error);
2528 }
2529
2530 /*
2531  * chown(char *path, int uid, int gid)
2532  *
2533  * Set ownership given a path name.
2534  */
2535 int
2536 sys_chown(struct chown_args *uap)
2537 {
2538         struct nlookupdata nd;
2539         int error;
2540
2541         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2542         if (error == 0)
2543                 error = kern_chown(&nd, uap->uid, uap->gid);
2544         nlookup_done(&nd);
2545         return (error);
2546 }
2547
2548 /*
2549  * lchown_args(char *path, int uid, int gid)
2550  *
2551  * Set ownership given a path name, do not cross symlinks.
2552  */
2553 int
2554 sys_lchown(struct lchown_args *uap)
2555 {
2556         struct nlookupdata nd;
2557         int error;
2558
2559         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2560         if (error == 0)
2561                 error = kern_chown(&nd, uap->uid, uap->gid);
2562         nlookup_done(&nd);
2563         return (error);
2564 }
2565
2566 /*
2567  * fchown_args(int fd, int uid, int gid)
2568  *
2569  * Set ownership given a file descriptor.
2570  */
2571 /* ARGSUSED */
2572 int
2573 sys_fchown(struct fchown_args *uap)
2574 {
2575         struct thread *td = curthread;
2576         struct proc *p = td->td_proc;
2577         struct file *fp;
2578         int error;
2579
2580         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2581                 return (error);
2582         if (fp->f_nchandle.ncp)
2583                 error = ncp_writechk(&fp->f_nchandle);
2584         if (error == 0)
2585                 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2586         fdrop(fp);
2587         return (error);
2588 }
2589
2590 static int
2591 getutimes(const struct timeval *tvp, struct timespec *tsp)
2592 {
2593         struct timeval tv[2];
2594
2595         if (tvp == NULL) {
2596                 microtime(&tv[0]);
2597                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2598                 tsp[1] = tsp[0];
2599         } else {
2600                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2601                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2602         }
2603         return 0;
2604 }
2605
2606 static int
2607 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2608 {
2609         struct thread *td = curthread;
2610         struct proc *p = td->td_proc;
2611         int error;
2612         struct vattr vattr;
2613
2614         /*
2615          * note: vget is required for any operation that might mod the vnode
2616          * so VINACTIVE is properly cleared.
2617          */
2618         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2619                 VATTR_NULL(&vattr);
2620                 vattr.va_atime = ts[0];
2621                 vattr.va_mtime = ts[1];
2622                 if (nullflag)
2623                         vattr.va_vaflags |= VA_UTIMES_NULL;
2624                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2625                 vput(vp);
2626         }
2627         return error;
2628 }
2629
2630 int
2631 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2632 {
2633         struct timespec ts[2];
2634         struct vnode *vp;
2635         int error;
2636
2637         if ((error = getutimes(tptr, ts)) != 0)
2638                 return (error);
2639         /* XXX Add NLC flag indicating modifying operation? */
2640         if ((error = nlookup(nd)) != 0)
2641                 return (error);
2642         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2643                 return (error);
2644         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2645                 return (error);
2646         error = setutimes(vp, ts, tptr == NULL);
2647         vrele(vp);
2648         return (error);
2649 }
2650
2651 /*
2652  * utimes_args(char *path, struct timeval *tptr)
2653  *
2654  * Set the access and modification times of a file.
2655  */
2656 int
2657 sys_utimes(struct utimes_args *uap)
2658 {
2659         struct timeval tv[2];
2660         struct nlookupdata nd;
2661         int error;
2662
2663         if (uap->tptr) {
2664                 error = copyin(uap->tptr, tv, sizeof(tv));
2665                 if (error)
2666                         return (error);
2667         }
2668         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2669         if (error == 0)
2670                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2671         nlookup_done(&nd);
2672         return (error);
2673 }
2674
2675 /*
2676  * lutimes_args(char *path, struct timeval *tptr)
2677  *
2678  * Set the access and modification times of a file.
2679  */
2680 int
2681 sys_lutimes(struct lutimes_args *uap)
2682 {
2683         struct timeval tv[2];
2684         struct nlookupdata nd;
2685         int error;
2686
2687         if (uap->tptr) {
2688                 error = copyin(uap->tptr, tv, sizeof(tv));
2689                 if (error)
2690                         return (error);
2691         }
2692         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2693         if (error == 0)
2694                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2695         nlookup_done(&nd);
2696         return (error);
2697 }
2698
2699 int
2700 kern_futimes(int fd, struct timeval *tptr)
2701 {
2702         struct thread *td = curthread;
2703         struct proc *p = td->td_proc;
2704         struct timespec ts[2];
2705         struct file *fp;
2706         int error;
2707
2708         error = getutimes(tptr, ts);
2709         if (error)
2710                 return (error);
2711         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2712                 return (error);
2713         if (fp->f_nchandle.ncp)
2714                 error = ncp_writechk(&fp->f_nchandle);
2715         if (error == 0)
2716                 error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2717         fdrop(fp);
2718         return (error);
2719 }
2720
2721 /*
2722  * futimes_args(int fd, struct timeval *tptr)
2723  *
2724  * Set the access and modification times of a file.
2725  */
2726 int
2727 sys_futimes(struct futimes_args *uap)
2728 {
2729         struct timeval tv[2];
2730         int error;
2731
2732         if (uap->tptr) {
2733                 error = copyin(uap->tptr, tv, sizeof(tv));
2734                 if (error)
2735                         return (error);
2736         }
2737
2738         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2739
2740         return (error);
2741 }
2742
2743 int
2744 kern_truncate(struct nlookupdata *nd, off_t length)
2745 {
2746         struct vnode *vp;
2747         struct vattr vattr;
2748         int error;
2749
2750         if (length < 0)
2751                 return(EINVAL);
2752         /* XXX Add NLC flag indicating modifying operation? */
2753         if ((error = nlookup(nd)) != 0)
2754                 return (error);
2755         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2756                 return (error);
2757         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2758                 return (error);
2759         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2760                 vrele(vp);
2761                 return (error);
2762         }
2763         if (vp->v_type == VDIR) {
2764                 error = EISDIR;
2765         } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2766             (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2767                 VATTR_NULL(&vattr);
2768                 vattr.va_size = length;
2769                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2770         }
2771         vput(vp);
2772         return (error);
2773 }
2774
2775 /*
2776  * truncate(char *path, int pad, off_t length)
2777  *
2778  * Truncate a file given its path name.
2779  */
2780 int
2781 sys_truncate(struct truncate_args *uap)
2782 {
2783         struct nlookupdata nd;
2784         int error;
2785
2786         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787         if (error == 0)
2788                 error = kern_truncate(&nd, uap->length);
2789         nlookup_done(&nd);
2790         return error;
2791 }
2792
2793 int
2794 kern_ftruncate(int fd, off_t length)
2795 {
2796         struct thread *td = curthread;
2797         struct proc *p = td->td_proc;
2798         struct vattr vattr;
2799         struct vnode *vp;
2800         struct file *fp;
2801         int error;
2802
2803         if (length < 0)
2804                 return(EINVAL);
2805         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2806                 return (error);
2807         if (fp->f_nchandle.ncp) {
2808                 error = ncp_writechk(&fp->f_nchandle);
2809                 if (error)
2810                         goto done;
2811         }
2812         if ((fp->f_flag & FWRITE) == 0) {
2813                 error = EINVAL;
2814                 goto done;
2815         }
2816         vp = (struct vnode *)fp->f_data;
2817         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2818         if (vp->v_type == VDIR) {
2819                 error = EISDIR;
2820         } else if ((error = vn_writechk(vp, NULL)) == 0) {
2821                 VATTR_NULL(&vattr);
2822                 vattr.va_size = length;
2823                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2824         }
2825         vn_unlock(vp);
2826 done:
2827         fdrop(fp);
2828         return (error);
2829 }
2830
2831 /*
2832  * ftruncate_args(int fd, int pad, off_t length)
2833  *
2834  * Truncate a file given a file descriptor.
2835  */
2836 int
2837 sys_ftruncate(struct ftruncate_args *uap)
2838 {
2839         int error;
2840
2841         error = kern_ftruncate(uap->fd, uap->length);
2842
2843         return (error);
2844 }
2845
2846 /*
2847  * fsync(int fd)
2848  *
2849  * Sync an open file.
2850  */
2851 /* ARGSUSED */
2852 int
2853 sys_fsync(struct fsync_args *uap)
2854 {
2855         struct thread *td = curthread;
2856         struct proc *p = td->td_proc;
2857         struct vnode *vp;
2858         struct file *fp;
2859         vm_object_t obj;
2860         int error;
2861
2862         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2863                 return (error);
2864         vp = (struct vnode *)fp->f_data;
2865         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2866         if ((obj = vp->v_object) != NULL)
2867                 vm_object_page_clean(obj, 0, 0, 0);
2868         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2869             vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2870             bioops.io_fsync) {
2871                 error = (*bioops.io_fsync)(vp);
2872         }
2873         vn_unlock(vp);
2874         fdrop(fp);
2875         return (error);
2876 }
2877
2878 int
2879 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2880 {
2881         struct nchandle fnchd;
2882         struct nchandle tnchd;
2883         struct namecache *ncp;
2884         struct vnode *fdvp;
2885         struct vnode *tdvp;
2886         struct mount *mp;
2887         int error;
2888
2889         bwillwrite();
2890         if ((error = nlookup(fromnd)) != 0)
2891                 return (error);
2892         if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2893                 return (ENOENT);
2894         fnchd.mount = fromnd->nl_nch.mount;
2895         cache_hold(&fnchd);
2896
2897         /*
2898          * unlock the source nch so we can lookup the target nch without
2899          * deadlocking.  The target may or may not exist so we do not check
2900          * for a target vp like kern_mkdir() and other creation functions do.
2901          *
2902          * The source and target directories are ref'd and rechecked after
2903          * everything is relocked to determine if the source or target file
2904          * has been renamed.
2905          */
2906         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2907         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2908         cache_unlock(&fromnd->nl_nch);
2909
2910         tond->nl_flags |= NLC_CREATE;
2911         if ((error = nlookup(tond)) != 0) {
2912                 cache_drop(&fnchd);
2913                 return (error);
2914         }
2915         if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2916                 cache_drop(&fnchd);
2917                 return (ENOENT);
2918         }
2919         tnchd.mount = tond->nl_nch.mount;
2920         cache_hold(&tnchd);
2921
2922         /*
2923          * If the source and target are the same there is nothing to do
2924          */
2925         if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2926                 cache_drop(&fnchd);
2927                 cache_drop(&tnchd);
2928                 return (0);
2929         }
2930
2931         /*
2932          * Mount points cannot be renamed or overwritten
2933          */
2934         if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2935             NCF_ISMOUNTPT
2936         ) {
2937                 cache_drop(&fnchd);
2938                 cache_drop(&tnchd);
2939                 return (EINVAL);
2940         }
2941
2942         /*
2943          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
2944          * may have become invalid while it was unlocked, nc_vp and nc_mount
2945          * could be NULL.
2946          */
2947         if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2948                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2949         } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2950                 cache_lock(&fromnd->nl_nch);
2951                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2952         } else {
2953                 cache_unlock(&tond->nl_nch);
2954                 cache_lock(&fromnd->nl_nch);
2955                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2956                 cache_lock(&tond->nl_nch);
2957                 cache_resolve(&tond->nl_nch, tond->nl_cred);
2958         }
2959         fromnd->nl_flags |= NLC_NCPISLOCKED;
2960
2961         /*
2962          * make sure the parent directories linkages are the same
2963          */
2964         if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2965             tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2966                 cache_drop(&fnchd);
2967                 cache_drop(&tnchd);
2968                 return (ENOENT);
2969         }
2970
2971         /*
2972          * Both the source and target must be within the same filesystem and
2973          * in the same filesystem as their parent directories within the
2974          * namecache topology.
2975          *
2976          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2977          */
2978         mp = fnchd.mount;
2979         if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2980             mp != tond->nl_nch.mount) {
2981                 cache_drop(&fnchd);
2982                 cache_drop(&tnchd);
2983                 return (EXDEV);
2984         }
2985
2986         /*
2987          * Make sure the mount point is writable
2988          */
2989         if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2990                 cache_drop(&fnchd);
2991                 cache_drop(&tnchd);
2992                 return (error);
2993         }
2994
2995         /*
2996          * If the target exists and either the source or target is a directory,
2997          * then both must be directories.
2998          *
2999          * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3000          * have become NULL.
3001          */
3002         if (tond->nl_nch.ncp->nc_vp) {
3003                 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3004                         error = ENOENT;
3005                 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3006                         if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3007                                 error = ENOTDIR;
3008                 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3009                         error = EISDIR;
3010                 }
3011         }
3012
3013         /*
3014          * You cannot rename a source into itself or a subdirectory of itself.
3015          * We check this by travsersing the target directory upwards looking
3016          * for a match against the source.
3017          */
3018         if (error == 0) {
3019                 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3020                         if (fromnd->nl_nch.ncp == ncp) {
3021                                 error = EINVAL;
3022                                 break;
3023                         }
3024                 }
3025         }
3026
3027         cache_drop(&fnchd);
3028         cache_drop(&tnchd);
3029
3030         /*
3031          * Even though the namespaces are different, they may still represent
3032          * hardlinks to the same file.  The filesystem might have a hard time
3033          * with this so we issue a NREMOVE of the source instead of a NRENAME
3034          * when we detect the situation.
3035          */
3036         if (error == 0) {
3037                 fdvp = fromnd->nl_nch.ncp->nc_parent->nc_vp;
3038                 tdvp = tond->nl_nch.ncp->nc_parent->nc_vp;
3039                 if (fdvp == NULL || tdvp == NULL) {
3040                         error = EPERM;
3041                 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3042                         /* vhold(fdvp); - dvp can't go away */
3043                         error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3044                                             fromnd->nl_cred);
3045                         /* vdrop(fdvp); */
3046                 } else {
3047                         /* vhold(fdvp); - dvp can't go away */
3048                         /* vhold(tdvp); - dvp can't go away */
3049                         error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 
3050                                             fdvp, tdvp, tond->nl_cred);
3051                         /* vdrop(fdvp); */
3052                         /* vdrop(tdvp); */
3053                 }
3054         }
3055         return (error);
3056 }
3057
3058 /*
3059  * rename_args(char *from, char *to)
3060  *
3061  * Rename files.  Source and destination must either both be directories,
3062  * or both not be directories.  If target is a directory, it must be empty.
3063  */
3064 int
3065 sys_rename(struct rename_args *uap)
3066 {
3067         struct nlookupdata fromnd, tond;
3068         int error;
3069
3070         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3071         if (error == 0) {
3072                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3073                 if (error == 0)
3074                         error = kern_rename(&fromnd, &tond);
3075                 nlookup_done(&tond);
3076         }
3077         nlookup_done(&fromnd);
3078         return (error);
3079 }
3080
3081 int
3082 kern_mkdir(struct nlookupdata *nd, int mode)
3083 {
3084         struct thread *td = curthread;
3085         struct proc *p = td->td_proc;
3086         struct vnode *vp;
3087         struct vnode *dvp;
3088         struct vattr vattr;
3089         int error;
3090
3091         bwillwrite();
3092         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3093         if ((error = nlookup(nd)) != 0)
3094                 return (error);
3095
3096         if (nd->nl_nch.ncp->nc_vp)
3097                 return (EEXIST);
3098         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3099                 return (error);
3100         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
3101                 return (EPERM);
3102         /* vhold(dvp); - dvp can't go away */
3103         VATTR_NULL(&vattr);
3104         vattr.va_type = VDIR;
3105         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3106
3107         vp = NULL;
3108         error = VOP_NMKDIR(&nd->nl_nch, dvp, &vp, p->p_ucred, &vattr);
3109         /* vdrop(dvp); */
3110         if (error == 0)
3111                 vput(vp);
3112         return (error);
3113 }
3114
3115 /*
3116  * mkdir_args(char *path, int mode)
3117  *
3118  * Make a directory file.
3119  */
3120 /* ARGSUSED */
3121 int
3122 sys_mkdir(struct mkdir_args *uap)
3123 {
3124         struct nlookupdata nd;
3125         int error;
3126
3127         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3128         if (error == 0)
3129                 error = kern_mkdir(&nd, uap->mode);
3130         nlookup_done(&nd);
3131         return (error);
3132 }
3133
3134 int
3135 kern_rmdir(struct nlookupdata *nd)
3136 {
3137         struct vnode *dvp;
3138         int error;
3139
3140         bwillwrite();
3141         nd->nl_flags |= NLC_DELETE;
3142         if ((error = nlookup(nd)) != 0)
3143                 return (error);
3144
3145         /*
3146          * Do not allow directories representing mount points to be
3147          * deleted, even if empty.  Check write perms on mount point
3148          * in case the vnode is aliased (aka nullfs).
3149          */
3150         if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3151                 return (EINVAL);
3152         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3153                 return (error);
3154         if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
3155                 return (EPERM);
3156         /* vhold(dvp); - dvp can't go away */
3157         error = VOP_NRMDIR(&nd->nl_nch, dvp, nd->nl_cred);
3158         /* vdrop(dvp); */
3159         return (error);
3160 }
3161
3162 /*
3163  * rmdir_args(char *path)
3164  *
3165  * Remove a directory file.
3166  */
3167 /* ARGSUSED */
3168 int
3169 sys_rmdir(struct rmdir_args *uap)
3170 {
3171         struct nlookupdata nd;
3172         int error;
3173
3174         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3175         if (error == 0)
3176                 error = kern_rmdir(&nd);
3177         nlookup_done(&nd);
3178         return (error);
3179 }
3180
3181 int
3182 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3183     enum uio_seg direction)
3184 {
3185         struct thread *td = curthread;
3186         struct proc *p = td->td_proc;
3187         struct vnode *vp;
3188         struct file *fp;
3189         struct uio auio;
3190         struct iovec aiov;
3191         long loff;
3192         int error, eofflag;
3193
3194         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3195                 return (error);
3196         if ((fp->f_flag & FREAD) == 0) {
3197                 error = EBADF;
3198                 goto done;
3199         }
3200         vp = (struct vnode *)fp->f_data;
3201 unionread:
3202         if (vp->v_type != VDIR) {
3203                 error = EINVAL;
3204                 goto done;
3205         }
3206         aiov.iov_base = buf;
3207         aiov.iov_len = count;
3208         auio.uio_iov = &aiov;
3209         auio.uio_iovcnt = 1;
3210         auio.uio_rw = UIO_READ;
3211         auio.uio_segflg = direction;
3212         auio.uio_td = td;
3213         auio.uio_resid = count;
3214         loff = auio.uio_offset = fp->f_offset;
3215         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3216         fp->f_offset = auio.uio_offset;
3217         if (error)
3218                 goto done;
3219         if (count == auio.uio_resid) {
3220                 if (union_dircheckp) {
3221                         error = union_dircheckp(td, &vp, fp);
3222                         if (error == -1)
3223                                 goto unionread;
3224                         if (error)
3225                                 goto done;
3226                 }
3227 #if 0
3228                 if ((vp->v_flag & VROOT) &&
3229                     (vp->v_mount->mnt_flag & MNT_UNION)) {
3230                         struct vnode *tvp = vp;
3231                         vp = vp->v_mount->mnt_vnodecovered;
3232                         vref(vp);
3233                         fp->f_data = vp;
3234                         fp->f_offset = 0;
3235                         vrele(tvp);
3236                         goto unionread;
3237                 }
3238 #endif
3239         }
3240         if (basep) {
3241                 *basep = loff;
3242         }
3243         *res = count - auio.uio_resid;
3244 done:
3245         fdrop(fp);
3246         return (error);
3247 }
3248
3249 /*
3250  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3251  *
3252  * Read a block of directory entries in a file system independent format.
3253  */
3254 int
3255 sys_getdirentries(struct getdirentries_args *uap)
3256 {
3257         long base;
3258         int error;
3259
3260         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3261             &uap->sysmsg_result, UIO_USERSPACE);
3262
3263         if (error == 0)
3264                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3265         return (error);
3266 }
3267
3268 /*
3269  * getdents_args(int fd, char *buf, size_t count)
3270  */
3271 int
3272 sys_getdents(struct getdents_args *uap)
3273 {
3274         int error;
3275
3276         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3277             &uap->sysmsg_result, UIO_USERSPACE);
3278
3279         return (error);
3280 }
3281
3282 /*
3283  * umask(int newmask)
3284  *
3285  * Set the mode mask for creation of filesystem nodes.
3286  *
3287  * MP SAFE
3288  */
3289 int
3290 sys_umask(struct umask_args *uap)
3291 {
3292         struct thread *td = curthread;
3293         struct proc *p = td->td_proc;
3294         struct filedesc *fdp;
3295
3296         fdp = p->p_fd;
3297         uap->sysmsg_result = fdp->fd_cmask;
3298         fdp->fd_cmask = uap->newmask & ALLPERMS;
3299         return (0);
3300 }
3301
3302 /*
3303  * revoke(char *path)
3304  *
3305  * Void all references to file by ripping underlying filesystem
3306  * away from vnode.
3307  */
3308 /* ARGSUSED */
3309 int
3310 sys_revoke(struct revoke_args *uap)
3311 {
3312         struct nlookupdata nd;
3313         struct vattr vattr;
3314         struct vnode *vp;
3315         struct ucred *cred;
3316         int error;
3317
3318         vp = NULL;
3319         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3320         if (error == 0)
3321                 error = nlookup(&nd);
3322         if (error == 0)
3323                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3324         cred = crhold(nd.nl_cred);
3325         nlookup_done(&nd);
3326         if (error == 0) {
3327                 if (vp->v_type != VCHR && vp->v_type != VBLK)
3328                         error = EINVAL;
3329                 if (error == 0)
3330                         error = VOP_GETATTR(vp, &vattr);
3331                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3332                         error = suser_cred(cred, PRISON_ROOT);
3333                 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3334                         error = 0;
3335                         vx_lock(vp);
3336                         VOP_REVOKE(vp, REVOKEALL);
3337                         vx_unlock(vp);
3338                 }
3339                 vrele(vp);
3340         }
3341         if (cred)
3342                 crfree(cred);
3343         return (error);
3344 }
3345
3346 /*
3347  * getfh_args(char *fname, fhandle_t *fhp)
3348  *
3349  * Get (NFS) file handle
3350  */
3351 int
3352 sys_getfh(struct getfh_args *uap)
3353 {
3354         struct thread *td = curthread;
3355         struct nlookupdata nd;
3356         fhandle_t fh;
3357         struct vnode *vp;
3358         int error;
3359
3360         /*
3361          * Must be super user
3362          */
3363         if ((error = suser(td)) != 0)
3364                 return (error);
3365
3366         vp = NULL;
3367         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3368         if (error == 0)
3369                 error = nlookup(&nd);
3370         if (error == 0)
3371                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3372         nlookup_done(&nd);
3373         if (error == 0) {
3374                 bzero(&fh, sizeof(fh));
3375                 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3376                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3377                 vput(vp);
3378                 if (error == 0)
3379                         error = copyout(&fh, uap->fhp, sizeof(fh));
3380         }
3381         return (error);
3382 }
3383
3384 /*
3385  * fhopen_args(const struct fhandle *u_fhp, int flags)
3386  *
3387  * syscall for the rpc.lockd to use to translate a NFS file handle into
3388  * an open descriptor.
3389  *
3390  * warning: do not remove the suser() call or this becomes one giant
3391  * security hole.
3392  */
3393 int
3394 sys_fhopen(struct fhopen_args *uap)
3395 {
3396         struct thread *td = curthread;
3397         struct proc *p = td->td_proc;
3398         struct mount *mp;
3399         struct vnode *vp;
3400         struct fhandle fhp;
3401         struct vattr vat;
3402         struct vattr *vap = &vat;
3403         struct flock lf;
3404         int fmode, mode, error, type;
3405         struct file *nfp; 
3406         struct file *fp;
3407         int indx;
3408
3409         /*
3410          * Must be super user
3411          */
3412         error = suser(td);
3413         if (error)
3414                 return (error);
3415
3416         fmode = FFLAGS(uap->flags);
3417         /* why not allow a non-read/write open for our lockd? */
3418         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3419                 return (EINVAL);
3420         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3421         if (error)
3422                 return(error);
3423         /* find the mount point */
3424         mp = vfs_getvfs(&fhp.fh_fsid);
3425         if (mp == NULL)
3426                 return (ESTALE);
3427         /* now give me my vnode, it gets returned to me locked */
3428         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3429         if (error)
3430                 return (error);
3431         /*
3432          * from now on we have to make sure not
3433          * to forget about the vnode
3434          * any error that causes an abort must vput(vp) 
3435          * just set error = err and 'goto bad;'.
3436          */
3437
3438         /* 
3439          * from vn_open 
3440          */
3441         if (vp->v_type == VLNK) {
3442                 error = EMLINK;
3443                 goto bad;
3444         }
3445         if (vp->v_type == VSOCK) {
3446                 error = EOPNOTSUPP;
3447                 goto bad;
3448         }
3449         mode = 0;
3450         if (fmode & (FWRITE | O_TRUNC)) {
3451                 if (vp->v_type == VDIR) {
3452                         error = EISDIR;
3453                         goto bad;
3454                 }
3455                 error = vn_writechk(vp, NULL);
3456                 if (error)
3457                         goto bad;
3458                 mode |= VWRITE;
3459         }
3460         if (fmode & FREAD)
3461                 mode |= VREAD;
3462         if (mode) {
3463                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3464                 if (error)
3465                         goto bad;
3466         }
3467         if (fmode & O_TRUNC) {
3468                 vn_unlock(vp);                          /* XXX */
3469                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3470                 VATTR_NULL(vap);
3471                 vap->va_size = 0;
3472                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3473                 if (error)
3474                         goto bad;
3475         }
3476
3477         /*
3478          * VOP_OPEN needs the file pointer so it can potentially override
3479          * it.
3480          *
3481          * WARNING! no f_nchandle will be associated when fhopen()ing a
3482          * directory.  XXX
3483          */
3484         if ((error = falloc(p, &nfp, &indx)) != 0)
3485                 goto bad;
3486         fp = nfp;
3487
3488         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3489         if (error) {
3490                 /*
3491                  * setting f_ops this way prevents VOP_CLOSE from being
3492                  * called or fdrop() releasing the vp from v_data.   Since
3493                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3494                  */
3495                 fp->f_ops = &badfileops;
3496                 fp->f_data = NULL;
3497                 goto bad_drop;
3498         }
3499
3500         /*
3501          * The fp is given its own reference, we still have our ref and lock.
3502          *
3503          * Assert that all regular files must be created with a VM object.
3504          */
3505         if (vp->v_type == VREG && vp->v_object == NULL) {
3506                 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3507                 goto bad_drop;
3508         }
3509
3510         /*
3511          * The open was successful.  Handle any locking requirements.
3512          */
3513         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3514                 lf.l_whence = SEEK_SET;
3515                 lf.l_start = 0;
3516                 lf.l_len = 0;
3517                 if (fmode & O_EXLOCK)
3518                         lf.l_type = F_WRLCK;
3519                 else
3520                         lf.l_type = F_RDLCK;
3521                 if (fmode & FNONBLOCK)
3522                         type = 0;
3523                 else
3524                         type = F_WAIT;
3525                 vn_unlock(vp);
3526                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3527                         /*
3528                          * release our private reference.
3529                          */
3530                         fsetfd(p, NULL, indx);
3531                         fdrop(fp);
3532                         vrele(vp);
3533                         return (error);
3534                 }
3535                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3536                 fp->f_flag |= FHASLOCK;
3537         }
3538
3539         /*
3540          * Clean up.  Associate the file pointer with the previously
3541          * reserved descriptor and return it.
3542          */
3543         vput(vp);
3544         fsetfd(p, fp, indx);
3545         fdrop(fp);
3546         uap->sysmsg_result = indx;
3547         return (0);
3548
3549 bad_drop:
3550         fsetfd(p, NULL, indx);
3551         fdrop(fp);
3552 bad:
3553         vput(vp);
3554         return (error);
3555 }
3556
3557 /*
3558  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3559  */
3560 int
3561 sys_fhstat(struct fhstat_args *uap)
3562 {
3563         struct thread *td = curthread;
3564         struct stat sb;
3565         fhandle_t fh;
3566         struct mount *mp;
3567         struct vnode *vp;
3568         int error;
3569
3570         /*
3571          * Must be super user
3572          */
3573         error = suser(td);
3574         if (error)
3575                 return (error);
3576         
3577         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3578         if (error)
3579                 return (error);
3580
3581         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3582                 return (ESTALE);
3583         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3584                 return (error);
3585         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3586         vput(vp);
3587         if (error)
3588                 return (error);
3589         error = copyout(&sb, uap->sb, sizeof(sb));
3590         return (error);
3591 }
3592
3593 /*
3594  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3595  */
3596 int
3597 sys_fhstatfs(struct fhstatfs_args *uap)
3598 {
3599         struct thread *td = curthread;
3600         struct proc *p = td->td_proc;
3601         struct statfs *sp;
3602         struct mount *mp;
3603         struct vnode *vp;
3604         struct statfs sb;
3605         char *fullpath, *freepath;
3606         fhandle_t fh;
3607         int error;
3608
3609         /*
3610          * Must be super user
3611          */
3612         if ((error = suser(td)))
3613                 return (error);
3614
3615         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3616                 return (error);
3617
3618         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3619                 return (ESTALE);
3620
3621         if (p != NULL && !chroot_visible_mnt(mp, p))
3622                 return (ESTALE);
3623
3624         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3625                 return (error);
3626         mp = vp->v_mount;
3627         sp = &mp->mnt_stat;
3628         vput(vp);
3629         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3630                 return (error);
3631
3632         error = mount_path(p, mp, &fullpath, &freepath);
3633         if (error)
3634                 return(error);
3635         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3636         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3637         kfree(freepath, M_TEMP);
3638
3639         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3640         if (suser(td)) {
3641                 bcopy(sp, &sb, sizeof(sb));
3642                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3643                 sp = &sb;
3644         }
3645         return (copyout(sp, uap->buf, sizeof(*sp)));
3646 }
3647
3648 /*
3649  * Syscall to push extended attribute configuration information into the
3650  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3651  * a command (int cmd), and attribute name and misc data.  For now, the
3652  * attribute name is left in userspace for consumption by the VFS_op.
3653  * It will probably be changed to be copied into sysspace by the
3654  * syscall in the future, once issues with various consumers of the
3655  * attribute code have raised their hands.
3656  *
3657  * Currently this is used only by UFS Extended Attributes.
3658  */
3659 int
3660 sys_extattrctl(struct extattrctl_args *uap)
3661 {
3662         struct nlookupdata nd;
3663         struct mount *mp;
3664         struct vnode *vp;
3665         int error;
3666
3667         vp = NULL;
3668         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3669         if (error == 0)
3670                 error = nlookup(&nd);
3671         if (error == 0) {
3672                 mp = nd.nl_nch.mount;
3673                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
3674                                 uap->attrname, uap->arg, 
3675                                 nd.nl_cred);
3676         }
3677         nlookup_done(&nd);
3678         return (error);
3679 }
3680
3681 /*
3682  * Syscall to set a named extended attribute on a file or directory.
3683  * Accepts attribute name, and a uio structure pointing to the data to set.
3684  * The uio is consumed in the style of writev().  The real work happens
3685  * in VOP_SETEXTATTR().
3686  */
3687 int
3688 sys_extattr_set_file(struct extattr_set_file_args *uap)
3689 {
3690         char attrname[EXTATTR_MAXNAMELEN];
3691         struct iovec aiov[UIO_SMALLIOV];
3692         struct iovec *needfree;
3693         struct nlookupdata nd;
3694         struct iovec *iov;
3695         struct vnode *vp;
3696         struct uio auio;
3697         u_int iovlen;
3698         u_int cnt;
3699         int error;
3700         int i;
3701
3702         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3703         if (error)
3704                 return (error);
3705
3706         vp = NULL;
3707         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3708         if (error == 0)
3709                 error = nlookup(&nd);
3710         if (error == 0)
3711                 error = ncp_writechk(&nd.nl_nch);
3712         if (error == 0)
3713                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3714         if (error) {
3715                 nlookup_done(&nd);
3716                 return (error);
3717         }
3718
3719         needfree = NULL;
3720         iovlen = uap->iovcnt * sizeof(struct iovec);
3721         if (uap->iovcnt > UIO_SMALLIOV) {
3722                 if (uap->iovcnt > UIO_MAXIOV) {
3723                         error = EINVAL;
3724                         goto done;
3725                 }
3726                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3727                 needfree = iov;
3728         } else {
3729                 iov = aiov;
3730         }
3731         auio.uio_iov = iov;
3732         auio.uio_iovcnt = uap->iovcnt;
3733         auio.uio_rw = UIO_WRITE;
3734         auio.uio_segflg = UIO_USERSPACE;
3735         auio.uio_td = nd.nl_td;
3736         auio.uio_offset = 0;
3737         if ((error = copyin(uap->iovp, iov, iovlen)))
3738                 goto done;
3739         auio.uio_resid = 0;
3740         for (i = 0; i < uap->iovcnt; i++) {
3741                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3742                         error = EINVAL;
3743                         goto done;
3744                 }
3745                 auio.uio_resid += iov->iov_len;
3746                 iov++;
3747         }
3748         cnt = auio.uio_resid;
3749         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3750         cnt -= auio.uio_resid;
3751         uap->sysmsg_result = cnt;
3752 done:
3753         vput(vp);
3754         nlookup_done(&nd);
3755         if (needfree)
3756                 FREE(needfree, M_IOV);
3757         return (error);
3758 }
3759
3760 /*
3761  * Syscall to get a named extended attribute on a file or directory.
3762  * Accepts attribute name, and a uio structure pointing to a buffer for the
3763  * data.  The uio is consumed in the style of readv().  The real work
3764  * happens in VOP_GETEXTATTR();
3765  */
3766 int
3767 sys_extattr_get_file(struct extattr_get_file_args *uap)
3768 {
3769         char attrname[EXTATTR_MAXNAMELEN];
3770         struct iovec aiov[UIO_SMALLIOV];
3771         struct iovec *needfree;
3772         struct nlookupdata nd;
3773         struct iovec *iov;
3774         struct vnode *vp;
3775         struct uio auio;
3776         u_int iovlen;
3777         u_int cnt;
3778         int error;
3779         int i;
3780
3781         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3782         if (error)
3783                 return (error);
3784
3785         vp = NULL;
3786         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3787         if (error == 0)
3788                 error = nlookup(&nd);
3789         if (error == 0)
3790                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3791         if (error) {
3792                 nlookup_done(&nd);
3793                 return (error);
3794         }
3795
3796         iovlen = uap->iovcnt * sizeof (struct iovec);
3797         needfree = NULL;
3798         if (uap->iovcnt > UIO_SMALLIOV) {
3799                 if (uap->iovcnt > UIO_MAXIOV) {
3800                         error = EINVAL;
3801                         goto done;
3802                 }
3803                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3804                 needfree = iov;
3805         } else {
3806                 iov = aiov;
3807         }
3808         auio.uio_iov = iov;
3809         auio.uio_iovcnt = uap->iovcnt;
3810         auio.uio_rw = UIO_READ;
3811         auio.uio_segflg = UIO_USERSPACE;
3812         auio.uio_td = nd.nl_td;
3813         auio.uio_offset = 0;
3814         if ((error = copyin(uap->iovp, iov, iovlen)))
3815                 goto done;
3816         auio.uio_resid = 0;
3817         for (i = 0; i < uap->iovcnt; i++) {
3818                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3819                         error = EINVAL;
3820                         goto done;
3821                 }
3822                 auio.uio_resid += iov->iov_len;
3823                 iov++;
3824         }
3825         cnt = auio.uio_resid;
3826         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3827         cnt -= auio.uio_resid;
3828         uap->sysmsg_result = cnt;
3829 done:
3830         vput(vp);
3831         nlookup_done(&nd);
3832         if (needfree)
3833                 FREE(needfree, M_IOV);
3834         return(error);
3835 }
3836
3837 /*
3838  * Syscall to delete a named extended attribute from a file or directory.
3839  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3840  */
3841 int
3842 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3843 {
3844         char attrname[EXTATTR_MAXNAMELEN];
3845         struct nlookupdata nd;
3846         struct vnode *vp;
3847         int error;
3848
3849         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3850         if (error)
3851                 return(error);
3852
3853         vp = NULL;
3854         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3855         if (error == 0)
3856                 error = nlookup(&nd);
3857         if (error == 0)
3858                 error = ncp_writechk(&nd.nl_nch);
3859         if (error == 0)
3860                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3861         if (error) {
3862                 nlookup_done(&nd);
3863                 return (error);
3864         }
3865
3866         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3867         vput(vp);
3868         nlookup_done(&nd);
3869         return(error);
3870 }
3871
3872 /*
3873  * Determine if the mount is visible to the process.
3874  */
3875 static int
3876 chroot_visible_mnt(struct mount *mp, struct proc *p)
3877 {
3878         struct nchandle nch;
3879
3880         /*
3881          * Traverse from the mount point upwards.  If we hit the process
3882          * root then the mount point is visible to the process.
3883          */
3884         nch = mp->mnt_ncmountpt;
3885         while (nch.ncp) {
3886                 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3887                     nch.ncp == p->p_fd->fd_nrdir.ncp) {
3888                         return(1);
3889                 }
3890                 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3891                         nch = nch.mount->mnt_ncmounton;
3892                 } else {
3893                         nch.ncp = nch.ncp->nc_parent;
3894                 }
3895         }
3896
3897         /*
3898          * If the mount point is not visible to the process, but the
3899          * process root is in a subdirectory of the mount, return
3900          * TRUE anyway.
3901          */
3902         if (p->p_fd->fd_nrdir.mount == mp)
3903                 return(1);
3904
3905         return(0);
3906 }
3907