Merge from vendor branch GCC:
[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.113 2007/01/26 18:05:23 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_usecount == 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                 kvprintf(ctl, va);
763                 kprintf("\n");
764         }
765         __va_end(va);
766 }
767
768 /*
769  * Shim cache_fullpath() to handle the case where a process is chrooted into
770  * a subdirectory of a mount.  In this case if the root mount matches the
771  * process root directory's mount we have to specify the process's root
772  * directory instead of the mount point, because the mount point might
773  * be above the root directory.
774  */
775 static
776 int
777 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
778 {
779         struct nchandle *nch;
780         int error;
781
782         if (p && p->p_fd->fd_nrdir.mount == mp)
783                 nch = &p->p_fd->fd_nrdir;
784         else
785                 nch = &mp->mnt_ncmountpt;
786         return(cache_fullpath(p, nch, rb, fb));
787 }
788
789 /*
790  * Sync each mounted filesystem.
791  */
792
793 #ifdef DEBUG
794 static int syncprt = 0;
795 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
796 #endif /* DEBUG */
797
798 static int sync_callback(struct mount *mp, void *data);
799
800 /* ARGSUSED */
801 int
802 sys_sync(struct sync_args *uap)
803 {
804         mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
805 #ifdef DEBUG
806         /*
807          * print out buffer pool stat information on each sync() call.
808          */
809         if (syncprt)
810                 vfs_bufstats();
811 #endif /* DEBUG */
812         return (0);
813 }
814
815 static
816 int
817 sync_callback(struct mount *mp, void *data __unused)
818 {
819         int asyncflag;
820
821         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
822                 asyncflag = mp->mnt_flag & MNT_ASYNC;
823                 mp->mnt_flag &= ~MNT_ASYNC;
824                 vfs_msync(mp, MNT_NOWAIT);
825                 VFS_SYNC(mp, MNT_NOWAIT);
826                 mp->mnt_flag |= asyncflag;
827         }
828         return(0);
829 }
830
831 /* XXX PRISON: could be per prison flag */
832 static int prison_quotas;
833 #if 0
834 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
835 #endif
836
837 /*
838  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
839  *
840  * Change filesystem quotas.
841  */
842 /* ARGSUSED */
843 int
844 sys_quotactl(struct quotactl_args *uap)
845 {
846         struct nlookupdata nd;
847         struct thread *td;
848         struct proc *p;
849         struct mount *mp;
850         int error;
851
852         td = curthread;
853         p = td->td_proc;
854         if (p->p_ucred->cr_prison && !prison_quotas)
855                 return (EPERM);
856
857         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
858         if (error == 0)
859                 error = nlookup(&nd);
860         if (error == 0) {
861                 mp = nd.nl_nch.mount;
862                 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
863                                     uap->arg, nd.nl_cred);
864         }
865         nlookup_done(&nd);
866         return (error);
867 }
868
869 /*
870  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
871  *              void *buf, int buflen)
872  *
873  * This function operates on a mount point and executes the specified
874  * operation using the specified control data, and possibly returns data.
875  *
876  * The actual number of bytes stored in the result buffer is returned, 0
877  * if none, otherwise an error is returned.
878  */
879 /* ARGSUSED */
880 int
881 sys_mountctl(struct mountctl_args *uap)
882 {
883         struct thread *td = curthread;
884         struct proc *p = td->td_proc;
885         struct file *fp;
886         void *ctl = NULL;
887         void *buf = NULL;
888         char *path = NULL;
889         int error;
890
891         /*
892          * Sanity and permissions checks.  We must be root.
893          */
894         KKASSERT(p);
895         if (p->p_ucred->cr_prison != NULL)
896                 return (EPERM);
897         if ((error = suser(td)) != 0)
898                 return (error);
899
900         /*
901          * Argument length checks
902          */
903         if (uap->ctllen < 0 || uap->ctllen > 1024)
904                 return (EINVAL);
905         if (uap->buflen < 0 || uap->buflen > 16 * 1024)
906                 return (EINVAL);
907         if (uap->path == NULL)
908                 return (EINVAL);
909
910         /*
911          * Allocate the necessary buffers and copyin data
912          */
913         path = objcache_get(namei_oc, M_WAITOK);
914         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
915         if (error)
916                 goto done;
917
918         if (uap->ctllen) {
919                 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
920                 error = copyin(uap->ctl, ctl, uap->ctllen);
921                 if (error)
922                         goto done;
923         }
924         if (uap->buflen)
925                 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
926
927         /*
928          * Validate the descriptor
929          */
930         fp = holdfp(p->p_fd, uap->fd, -1);
931         if (fp == NULL) {
932                 error = EBADF;
933                 goto done;
934         }
935
936         /*
937          * Execute the internal kernel function and clean up.
938          */
939         error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
940         if (fp)
941                 fdrop(fp);
942         if (error == 0 && uap->sysmsg_result > 0)
943                 error = copyout(buf, uap->buf, uap->sysmsg_result);
944 done:
945         if (path)
946                 objcache_put(namei_oc, path);
947         if (ctl)
948                 kfree(ctl, M_TEMP);
949         if (buf)
950                 kfree(buf, M_TEMP);
951         return (error);
952 }
953
954 /*
955  * Execute a mount control operation by resolving the path to a mount point
956  * and calling vop_mountctl().  
957  */
958 int
959 kern_mountctl(const char *path, int op, struct file *fp, 
960                 const void *ctl, int ctllen, 
961                 void *buf, int buflen, int *res)
962 {
963         struct vnode *vp;
964         struct mount *mp;
965         struct nlookupdata nd;
966         int error;
967
968         *res = 0;
969         vp = NULL;
970         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
971         if (error == 0)
972                 error = nlookup(&nd);
973         if (error == 0)
974                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
975         nlookup_done(&nd);
976         if (error)
977                 return (error);
978
979         mp = vp->v_mount;
980
981         /*
982          * Must be the root of the filesystem
983          */
984         if ((vp->v_flag & VROOT) == 0) {
985                 vput(vp);
986                 return (EINVAL);
987         }
988         error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, 
989                                 buf, buflen, res);
990         vput(vp);
991         return (error);
992 }
993
994 int
995 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
996 {
997         struct thread *td = curthread;
998         struct proc *p = td->td_proc;
999         struct mount *mp;
1000         struct statfs *sp;
1001         char *fullpath, *freepath;
1002         int error;
1003
1004         if ((error = nlookup(nd)) != 0)
1005                 return (error);
1006         mp = nd->nl_nch.mount;
1007         sp = &mp->mnt_stat;
1008         if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1009                 return (error);
1010
1011         error = mount_path(p, mp, &fullpath, &freepath);
1012         if (error)
1013                 return(error);
1014         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1015         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1016         kfree(freepath, M_TEMP);
1017
1018         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1019         bcopy(sp, buf, sizeof(*buf));
1020         /* Only root should have access to the fsid's. */
1021         if (suser(td))
1022                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1023         return (0);
1024 }
1025
1026 /*
1027  * statfs_args(char *path, struct statfs *buf)
1028  *
1029  * Get filesystem statistics.
1030  */
1031 int
1032 sys_statfs(struct statfs_args *uap)
1033 {
1034         struct nlookupdata nd;
1035         struct statfs buf;
1036         int error;
1037
1038         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1039         if (error == 0)
1040                 error = kern_statfs(&nd, &buf);
1041         nlookup_done(&nd);
1042         if (error == 0)
1043                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1044         return (error);
1045 }
1046
1047 int
1048 kern_fstatfs(int fd, struct statfs *buf)
1049 {
1050         struct thread *td = curthread;
1051         struct proc *p = td->td_proc;
1052         struct file *fp;
1053         struct mount *mp;
1054         struct statfs *sp;
1055         char *fullpath, *freepath;
1056         int error;
1057
1058         KKASSERT(p);
1059         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1060                 return (error);
1061         mp = ((struct vnode *)fp->f_data)->v_mount;
1062         if (mp == NULL) {
1063                 error = EBADF;
1064                 goto done;
1065         }
1066         if (fp->f_cred == NULL) {
1067                 error = EINVAL;
1068                 goto done;
1069         }
1070         sp = &mp->mnt_stat;
1071         if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1072                 goto done;
1073
1074         if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1075                 goto done;
1076         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1077         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1078         kfree(freepath, M_TEMP);
1079
1080         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1081         bcopy(sp, buf, sizeof(*buf));
1082
1083         /* Only root should have access to the fsid's. */
1084         if (suser(td))
1085                 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1086         error = 0;
1087 done:
1088         fdrop(fp);
1089         return (error);
1090 }
1091
1092 /*
1093  * fstatfs_args(int fd, struct statfs *buf)
1094  *
1095  * Get filesystem statistics.
1096  */
1097 int
1098 sys_fstatfs(struct fstatfs_args *uap)
1099 {
1100         struct statfs buf;
1101         int error;
1102
1103         error = kern_fstatfs(uap->fd, &buf);
1104
1105         if (error == 0)
1106                 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1107         return (error);
1108 }
1109
1110 /*
1111  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1112  *
1113  * Get statistics on all filesystems.
1114  */
1115
1116 struct getfsstat_info {
1117         struct statfs *sfsp;
1118         long count;
1119         long maxcount;
1120         int error;
1121         int flags;
1122         struct proc *p;
1123 };
1124
1125 static int getfsstat_callback(struct mount *, void *);
1126
1127 /* ARGSUSED */
1128 int
1129 sys_getfsstat(struct getfsstat_args *uap)
1130 {
1131         struct thread *td = curthread;
1132         struct proc *p = td->td_proc;
1133         struct getfsstat_info info;
1134
1135         bzero(&info, sizeof(info));
1136
1137         info.maxcount = uap->bufsize / sizeof(struct statfs);
1138         info.sfsp = uap->buf;
1139         info.count = 0;
1140         info.flags = uap->flags;
1141         info.p = p;
1142
1143         mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1144         if (info.sfsp && info.count > info.maxcount)
1145                 uap->sysmsg_result = info.maxcount;
1146         else
1147                 uap->sysmsg_result = info.count;
1148         return (info.error);
1149 }
1150
1151 static int
1152 getfsstat_callback(struct mount *mp, void *data)
1153 {
1154         struct getfsstat_info *info = data;
1155         struct statfs *sp;
1156         char *freepath;
1157         char *fullpath;
1158         int error;
1159
1160         if (info->sfsp && info->count < info->maxcount) {
1161                 if (info->p && !chroot_visible_mnt(mp, info->p))
1162                         return(0);
1163                 sp = &mp->mnt_stat;
1164
1165                 /*
1166                  * If MNT_NOWAIT or MNT_LAZY is specified, do not
1167                  * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1168                  * overrides MNT_WAIT.
1169                  */
1170                 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1171                     (info->flags & MNT_WAIT)) &&
1172                     (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1173                         return(0);
1174                 }
1175                 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1176
1177                 error = mount_path(info->p, mp, &fullpath, &freepath);
1178                 if (error) {
1179                         info->error = error;
1180                         return(-1);
1181                 }
1182                 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1183                 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1184                 kfree(freepath, M_TEMP);
1185
1186                 error = copyout(sp, info->sfsp, sizeof(*sp));
1187                 if (error) {
1188                         info->error = error;
1189                         return (-1);
1190                 }
1191                 ++info->sfsp;
1192         }
1193         info->count++;
1194         return(0);
1195 }
1196
1197 /*
1198  * fchdir_args(int fd)
1199  *
1200  * Change current working directory to a given file descriptor.
1201  */
1202 /* ARGSUSED */
1203 int
1204 sys_fchdir(struct fchdir_args *uap)
1205 {
1206         struct thread *td = curthread;
1207         struct proc *p = td->td_proc;
1208         struct filedesc *fdp = p->p_fd;
1209         struct vnode *vp, *ovp;
1210         struct mount *mp;
1211         struct file *fp;
1212         struct nchandle nch, onch, tnch;
1213         int error;
1214
1215         if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1216                 return (error);
1217         vp = (struct vnode *)fp->f_data;
1218         vref(vp);
1219         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1220         if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1221                 error = ENOTDIR;
1222         else
1223                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1224         if (error) {
1225                 vput(vp);
1226                 fdrop(fp);
1227                 return (error);
1228         }
1229         cache_copy(&fp->f_nchandle, &nch);
1230
1231         /*
1232          * If the ncp has become a mount point, traverse through
1233          * the mount point.
1234          */
1235
1236         while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1237                (mp = cache_findmount(&nch)) != NULL
1238         ) {
1239                 error = nlookup_mp(mp, &tnch);
1240                 if (error == 0) {
1241                         cache_unlock(&tnch);    /* leave ref intact */
1242                         vput(vp);
1243                         vp = tnch.ncp->nc_vp;
1244                         error = vget(vp, LK_SHARED);
1245                         KKASSERT(error == 0);
1246                         cache_drop(&nch);
1247                         nch = tnch;
1248                 }
1249         }
1250         if (error == 0) {
1251                 ovp = fdp->fd_cdir;
1252                 onch = fdp->fd_ncdir;
1253                 vn_unlock(vp);          /* leave ref intact */
1254                 fdp->fd_cdir = vp;
1255                 fdp->fd_ncdir = nch;
1256                 cache_drop(&onch);
1257                 vrele(ovp);
1258         } else {
1259                 cache_drop(&nch);
1260                 vput(vp);
1261         }
1262         fdrop(fp);
1263         return (error);
1264 }
1265
1266 int
1267 kern_chdir(struct nlookupdata *nd)
1268 {
1269         struct thread *td = curthread;
1270         struct proc *p = td->td_proc;
1271         struct filedesc *fdp = p->p_fd;
1272         struct vnode *vp, *ovp;
1273         struct nchandle onch;
1274         int error;
1275
1276         if ((error = nlookup(nd)) != 0)
1277                 return (error);
1278         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1279                 return (ENOENT);
1280         if ((error = vget(vp, LK_SHARED)) != 0)
1281                 return (error);
1282
1283         error = checkvp_chdir(vp, td);
1284         vn_unlock(vp);
1285         if (error == 0) {
1286                 ovp = fdp->fd_cdir;
1287                 onch = fdp->fd_ncdir;
1288                 cache_unlock(&nd->nl_nch);      /* leave reference intact */
1289                 fdp->fd_ncdir = nd->nl_nch;
1290                 fdp->fd_cdir = vp;
1291                 cache_drop(&onch);
1292                 vrele(ovp);
1293                 cache_zero(&nd->nl_nch);
1294         } else {
1295                 vrele(vp);
1296         }
1297         return (error);
1298 }
1299
1300 /*
1301  * chdir_args(char *path)
1302  *
1303  * Change current working directory (``.'').
1304  */
1305 int
1306 sys_chdir(struct chdir_args *uap)
1307 {
1308         struct nlookupdata nd;
1309         int error;
1310
1311         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1312         if (error == 0)
1313                 error = kern_chdir(&nd);
1314         nlookup_done(&nd);
1315         return (error);
1316 }
1317
1318 /*
1319  * Helper function for raised chroot(2) security function:  Refuse if
1320  * any filedescriptors are open directories.
1321  */
1322 static int
1323 chroot_refuse_vdir_fds(struct filedesc *fdp)
1324 {
1325         struct vnode *vp;
1326         struct file *fp;
1327         int error;
1328         int fd;
1329
1330         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1331                 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1332                         continue;
1333                 vp = (struct vnode *)fp->f_data;
1334                 if (vp->v_type != VDIR) {
1335                         fdrop(fp);
1336                         continue;
1337                 }
1338                 fdrop(fp);
1339                 return(EPERM);
1340         }
1341         return (0);
1342 }
1343
1344 /*
1345  * This sysctl determines if we will allow a process to chroot(2) if it
1346  * has a directory open:
1347  *      0: disallowed for all processes.
1348  *      1: allowed for processes that were not already chroot(2)'ed.
1349  *      2: allowed for all processes.
1350  */
1351
1352 static int chroot_allow_open_directories = 1;
1353
1354 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1355      &chroot_allow_open_directories, 0, "");
1356
1357 /*
1358  * chroot to the specified namecache entry.  We obtain the vp from the
1359  * namecache data.  The passed ncp must be locked and referenced and will
1360  * remain locked and referenced on return.
1361  */
1362 int
1363 kern_chroot(struct nchandle *nch)
1364 {
1365         struct thread *td = curthread;
1366         struct proc *p = td->td_proc;
1367         struct filedesc *fdp = p->p_fd;
1368         struct vnode *vp;
1369         int error;
1370
1371         /*
1372          * Only root can chroot
1373          */
1374         if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1375                 return (error);
1376
1377         /*
1378          * Disallow open directory descriptors (fchdir() breakouts).
1379          */
1380         if (chroot_allow_open_directories == 0 ||
1381            (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1382                 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1383                         return (error);
1384         }
1385         if ((vp = nch->ncp->nc_vp) == NULL)
1386                 return (ENOENT);
1387
1388         if ((error = vget(vp, LK_SHARED)) != 0)
1389                 return (error);
1390
1391         /*
1392          * Check the validity of vp as a directory to change to and 
1393          * associate it with rdir/jdir.
1394          */
1395         error = checkvp_chdir(vp, td);
1396         vn_unlock(vp);                  /* leave reference intact */
1397         if (error == 0) {
1398                 vrele(fdp->fd_rdir);
1399                 fdp->fd_rdir = vp;      /* reference inherited by fd_rdir */
1400                 cache_drop(&fdp->fd_nrdir);
1401                 cache_copy(nch, &fdp->fd_nrdir);
1402                 if (fdp->fd_jdir == NULL) {
1403                         fdp->fd_jdir = vp;
1404                         vref(fdp->fd_jdir);
1405                         cache_copy(nch, &fdp->fd_njdir);
1406                 }
1407         } else {
1408                 vrele(vp);
1409         }
1410         return (error);
1411 }
1412
1413 /*
1414  * chroot_args(char *path)
1415  *
1416  * Change notion of root (``/'') directory.
1417  */
1418 /* ARGSUSED */
1419 int
1420 sys_chroot(struct chroot_args *uap)
1421 {
1422         struct thread *td = curthread;
1423         struct nlookupdata nd;
1424         int error;
1425
1426         KKASSERT(td->td_proc);
1427         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1428         if (error) {
1429                 nlookup_done(&nd);
1430                 return(error);
1431         }
1432         error = nlookup(&nd);
1433         if (error == 0)
1434                 error = kern_chroot(&nd.nl_nch);
1435         nlookup_done(&nd);
1436         return(error);
1437 }
1438
1439 /*
1440  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1441  * determine whether it is legal to chdir to the vnode.  The vnode's state
1442  * is not changed by this call.
1443  */
1444 int
1445 checkvp_chdir(struct vnode *vp, struct thread *td)
1446 {
1447         int error;
1448
1449         if (vp->v_type != VDIR)
1450                 error = ENOTDIR;
1451         else
1452                 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1453         return (error);
1454 }
1455
1456 int
1457 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1458 {
1459         struct thread *td = curthread;
1460         struct proc *p = td->td_proc;
1461         struct lwp *lp = td->td_lwp;
1462         struct filedesc *fdp = p->p_fd;
1463         int cmode, flags;
1464         struct file *nfp;
1465         struct file *fp;
1466         struct vnode *vp;
1467         int type, indx, error;
1468         struct flock lf;
1469
1470         if ((oflags & O_ACCMODE) == O_ACCMODE)
1471                 return (EINVAL);
1472         flags = FFLAGS(oflags);
1473         error = falloc(p, &nfp, NULL);
1474         if (error)
1475                 return (error);
1476         fp = nfp;
1477         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1478
1479         /*
1480          * XXX p_dupfd is a real mess.  It allows a device to return a
1481          * file descriptor to be duplicated rather then doing the open
1482          * itself.
1483          */
1484         lp->lwp_dupfd = -1;
1485
1486         /*
1487          * Call vn_open() to do the lookup and assign the vnode to the 
1488          * file pointer.  vn_open() does not change the ref count on fp
1489          * and the vnode, on success, will be inherited by the file pointer
1490          * and unlocked.
1491          */
1492         nd->nl_flags |= NLC_LOCKVP;
1493         error = vn_open(nd, fp, flags, cmode);
1494         nlookup_done(nd);
1495         if (error) {
1496                 /*
1497                  * handle special fdopen() case.  bleh.  dupfdopen() is
1498                  * responsible for dropping the old contents of ofiles[indx]
1499                  * if it succeeds.
1500                  *
1501                  * Note that fsetfd() will add a ref to fp which represents
1502                  * the fd_files[] assignment.  We must still drop our
1503                  * reference.
1504                  */
1505                 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1506                         if (fdalloc(p, 0, &indx) == 0) {
1507                                 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1508                                 if (error == 0) {
1509                                         *res = indx;
1510                                         fdrop(fp);      /* our ref */
1511                                         return (0);
1512                                 }
1513                                 fsetfd(p, NULL, indx);
1514                         }
1515                 }
1516                 fdrop(fp);      /* our ref */
1517                 if (error == ERESTART)
1518                         error = EINTR;
1519                 return (error);
1520         }
1521
1522         /*
1523          * ref the vnode for ourselves so it can't be ripped out from under
1524          * is.  XXX need an ND flag to request that the vnode be returned
1525          * anyway.
1526          *
1527          * Reserve a file descriptor but do not assign it until the open
1528          * succeeds.
1529          */
1530         vp = (struct vnode *)fp->f_data;
1531         vref(vp);
1532         if ((error = fdalloc(p, 0, &indx)) != 0) {
1533                 fdrop(fp);
1534                 vrele(vp);
1535                 return (error);
1536         }
1537
1538         /*
1539          * If no error occurs the vp will have been assigned to the file
1540          * pointer.
1541          */
1542         lp->lwp_dupfd = 0;
1543
1544         if (flags & (O_EXLOCK | O_SHLOCK)) {
1545                 lf.l_whence = SEEK_SET;
1546                 lf.l_start = 0;
1547                 lf.l_len = 0;
1548                 if (flags & O_EXLOCK)
1549                         lf.l_type = F_WRLCK;
1550                 else
1551                         lf.l_type = F_RDLCK;
1552                 if (flags & FNONBLOCK)
1553                         type = 0;
1554                 else
1555                         type = F_WAIT;
1556
1557                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1558                         /*
1559                          * lock request failed.  Clean up the reserved
1560                          * descriptor.
1561                          */
1562                         vrele(vp);
1563                         fsetfd(p, NULL, indx);
1564                         fdrop(fp);
1565                         return (error);
1566                 }
1567                 fp->f_flag |= FHASLOCK;
1568         }
1569 #if 0
1570         /*
1571          * Assert that all regular file vnodes were created with a object.
1572          */
1573         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1574                 ("open: regular file has no backing object after vn_open"));
1575 #endif
1576
1577         vrele(vp);
1578
1579         /*
1580          * release our private reference, leaving the one associated with the
1581          * descriptor table intact.
1582          */
1583         fsetfd(p, fp, indx);
1584         fdrop(fp);
1585         *res = indx;
1586         return (0);
1587 }
1588
1589 /*
1590  * open_args(char *path, int flags, int mode)
1591  *
1592  * Check permissions, allocate an open file structure,
1593  * and call the device open routine if any.
1594  */
1595 int
1596 sys_open(struct open_args *uap)
1597 {
1598         struct nlookupdata nd;
1599         int error;
1600
1601         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1602         if (error == 0) {
1603                 error = kern_open(&nd, uap->flags,
1604                                     uap->mode, &uap->sysmsg_result);
1605         }
1606         nlookup_done(&nd);
1607         return (error);
1608 }
1609
1610 int
1611 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1612 {
1613         struct thread *td = curthread;
1614         struct proc *p = td->td_proc;
1615         struct vnode *vp;
1616         struct vattr vattr;
1617         int error;
1618         int whiteout = 0;
1619
1620         KKASSERT(p);
1621
1622         switch (mode & S_IFMT) {
1623         case S_IFCHR:
1624         case S_IFBLK:
1625                 error = suser(td);
1626                 break;
1627         default:
1628                 error = suser_cred(p->p_ucred, PRISON_ROOT);
1629                 break;
1630         }
1631         if (error)
1632                 return (error);
1633
1634         bwillwrite();
1635         nd->nl_flags |= NLC_CREATE;
1636         if ((error = nlookup(nd)) != 0)
1637                 return (error);
1638         if (nd->nl_nch.ncp->nc_vp)
1639                 return (EEXIST);
1640         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1641                 return (error);
1642
1643         VATTR_NULL(&vattr);
1644         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1645         vattr.va_rdev = dev;
1646         whiteout = 0;
1647
1648         switch (mode & S_IFMT) {
1649         case S_IFMT:    /* used by badsect to flag bad sectors */
1650                 vattr.va_type = VBAD;
1651                 break;
1652         case S_IFCHR:
1653                 vattr.va_type = VCHR;
1654                 break;
1655         case S_IFBLK:
1656                 vattr.va_type = VBLK;
1657                 break;
1658         case S_IFWHT:
1659                 whiteout = 1;
1660                 break;
1661         default:
1662                 error = EINVAL;
1663                 break;
1664         }
1665         if (error == 0) {
1666                 if (whiteout) {
1667                         error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1668                 } else {
1669                         vp = NULL;
1670                         error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1671                         if (error == 0)
1672                                 vput(vp);
1673                 }
1674         }
1675         return (error);
1676 }
1677
1678 /*
1679  * mknod_args(char *path, int mode, int dev)
1680  *
1681  * Create a special file.
1682  */
1683 int
1684 sys_mknod(struct mknod_args *uap)
1685 {
1686         struct nlookupdata nd;
1687         int error;
1688
1689         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1690         if (error == 0)
1691                 error = kern_mknod(&nd, uap->mode, uap->dev);
1692         nlookup_done(&nd);
1693         return (error);
1694 }
1695
1696 int
1697 kern_mkfifo(struct nlookupdata *nd, int mode)
1698 {
1699         struct thread *td = curthread;
1700         struct proc *p = td->td_proc;
1701         struct vattr vattr;
1702         struct vnode *vp;
1703         int error;
1704
1705         bwillwrite();
1706
1707         nd->nl_flags |= NLC_CREATE;
1708         if ((error = nlookup(nd)) != 0)
1709                 return (error);
1710         if (nd->nl_nch.ncp->nc_vp)
1711                 return (EEXIST);
1712         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1713                 return (error);
1714
1715         VATTR_NULL(&vattr);
1716         vattr.va_type = VFIFO;
1717         vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1718         vp = NULL;
1719         error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1720         if (error == 0)
1721                 vput(vp);
1722         return (error);
1723 }
1724
1725 /*
1726  * mkfifo_args(char *path, int mode)
1727  *
1728  * Create a named pipe.
1729  */
1730 int
1731 sys_mkfifo(struct mkfifo_args *uap)
1732 {
1733         struct nlookupdata nd;
1734         int error;
1735
1736         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1737         if (error == 0)
1738                 error = kern_mkfifo(&nd, uap->mode);
1739         nlookup_done(&nd);
1740         return (error);
1741 }
1742
1743 static int hardlink_check_uid = 0;
1744 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1745     &hardlink_check_uid, 0, 
1746     "Unprivileged processes cannot create hard links to files owned by other "
1747     "users");
1748 static int hardlink_check_gid = 0;
1749 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1750     &hardlink_check_gid, 0,
1751     "Unprivileged processes cannot create hard links to files owned by other "
1752     "groups");
1753
1754 static int
1755 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1756 {
1757         struct vattr va;
1758         int error;
1759
1760         /*
1761          * Shortcut if disabled
1762          */
1763         if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1764                 return (0);
1765
1766         /*
1767          * root cred can always hardlink
1768          */
1769         if (suser_cred(cred, PRISON_ROOT) == 0)
1770                 return (0);
1771
1772         /*
1773          * Otherwise only if the originating file is owned by the
1774          * same user or group.  Note that any group is allowed if
1775          * the file is owned by the caller.
1776          */
1777         error = VOP_GETATTR(vp, &va);
1778         if (error != 0)
1779                 return (error);
1780         
1781         if (hardlink_check_uid) {
1782                 if (cred->cr_uid != va.va_uid)
1783                         return (EPERM);
1784         }
1785         
1786         if (hardlink_check_gid) {
1787                 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1788                         return (EPERM);
1789         }
1790
1791         return (0);
1792 }
1793
1794 int
1795 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1796 {
1797         struct thread *td = curthread;
1798         struct vnode *vp;
1799         int error;
1800
1801         /*
1802          * Lookup the source and obtained a locked vnode.
1803          *
1804          * XXX relookup on vget failure / race ?
1805          */
1806         bwillwrite();
1807         if ((error = nlookup(nd)) != 0)
1808                 return (error);
1809         vp = nd->nl_nch.ncp->nc_vp;
1810         KKASSERT(vp != NULL);
1811         if (vp->v_type == VDIR)
1812                 return (EPERM);         /* POSIX */
1813         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1814                 return (error);
1815         if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1816                 return (error);
1817
1818         /*
1819          * Unlock the source so we can lookup the target without deadlocking
1820          * (XXX vp is locked already, possible other deadlock?).  The target
1821          * must not exist.
1822          */
1823         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1824         nd->nl_flags &= ~NLC_NCPISLOCKED;
1825         cache_unlock(&nd->nl_nch);
1826
1827         linknd->nl_flags |= NLC_CREATE;
1828         if ((error = nlookup(linknd)) != 0) {
1829                 vput(vp);
1830                 return (error);
1831         }
1832         if (linknd->nl_nch.ncp->nc_vp) {
1833                 vput(vp);
1834                 return (EEXIST);
1835         }
1836
1837         /*
1838          * Finally run the new API VOP.
1839          */
1840         error = can_hardlink(vp, td, td->td_proc->p_ucred);
1841         if (error == 0)
1842                 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1843         vput(vp);
1844         return (error);
1845 }
1846
1847 /*
1848  * link_args(char *path, char *link)
1849  *
1850  * Make a hard file link.
1851  */
1852 int
1853 sys_link(struct link_args *uap)
1854 {
1855         struct nlookupdata nd, linknd;
1856         int error;
1857
1858         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1859         if (error == 0) {
1860                 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1861                 if (error == 0)
1862                         error = kern_link(&nd, &linknd);
1863                 nlookup_done(&linknd);
1864         }
1865         nlookup_done(&nd);
1866         return (error);
1867 }
1868
1869 int
1870 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1871 {
1872         struct vattr vattr;
1873         struct vnode *vp;
1874         int error;
1875
1876         bwillwrite();
1877         nd->nl_flags |= NLC_CREATE;
1878         if ((error = nlookup(nd)) != 0)
1879                 return (error);
1880         if (nd->nl_nch.ncp->nc_vp)
1881                 return (EEXIST);
1882         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1883                 return (error);
1884         VATTR_NULL(&vattr);
1885         vattr.va_mode = mode;
1886         error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1887         if (error == 0)
1888                 vput(vp);
1889         return (error);
1890 }
1891
1892 /*
1893  * symlink(char *path, char *link)
1894  *
1895  * Make a symbolic link.
1896  */
1897 int
1898 sys_symlink(struct symlink_args *uap)
1899 {
1900         struct thread *td = curthread;
1901         struct nlookupdata nd;
1902         char *path;
1903         int error;
1904         int mode;
1905
1906         path = objcache_get(namei_oc, M_WAITOK);
1907         error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1908         if (error == 0) {
1909                 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1910                 if (error == 0) {
1911                         mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1912                         error = kern_symlink(&nd, path, mode);
1913                 }
1914                 nlookup_done(&nd);
1915         }
1916         objcache_put(namei_oc, path);
1917         return (error);
1918 }
1919
1920 /*
1921  * undelete_args(char *path)
1922  *
1923  * Delete a whiteout from the filesystem.
1924  */
1925 /* ARGSUSED */
1926 int
1927 sys_undelete(struct undelete_args *uap)
1928 {
1929         struct nlookupdata nd;
1930         int error;
1931
1932         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1933         bwillwrite();
1934         nd.nl_flags |= NLC_DELETE;
1935         if (error == 0)
1936                 error = nlookup(&nd);
1937         if (error == 0)
1938                 error = ncp_writechk(&nd.nl_nch);
1939         if (error == 0)
1940                 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1941         nlookup_done(&nd);
1942         return (error);
1943 }
1944
1945 int
1946 kern_unlink(struct nlookupdata *nd)
1947 {
1948         int error;
1949
1950         bwillwrite();
1951         nd->nl_flags |= NLC_DELETE;
1952         if ((error = nlookup(nd)) != 0)
1953                 return (error);
1954         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1955                 return (error);
1956         error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1957         return (error);
1958 }
1959
1960 /*
1961  * unlink_args(char *path)
1962  *
1963  * Delete a name from the filesystem.
1964  */
1965 int
1966 sys_unlink(struct unlink_args *uap)
1967 {
1968         struct nlookupdata nd;
1969         int error;
1970
1971         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1972         if (error == 0)
1973                 error = kern_unlink(&nd);
1974         nlookup_done(&nd);
1975         return (error);
1976 }
1977
1978 int
1979 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1980 {
1981         struct thread *td = curthread;
1982         struct proc *p = td->td_proc;
1983         struct file *fp;
1984         struct vattr vattr;
1985         int error;
1986
1987         fp = holdfp(p->p_fd, fd, -1);
1988         if (fp == NULL)
1989                 return (EBADF);
1990         if (fp->f_type != DTYPE_VNODE) {
1991                 error = ESPIPE;
1992                 goto done;
1993         }
1994
1995         switch (whence) {
1996         case L_INCR:
1997                 fp->f_offset += offset;
1998                 error = 0;
1999                 break;
2000         case L_XTND:
2001                 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2002                 if (error == 0)
2003                         fp->f_offset = offset + vattr.va_size;
2004                 break;
2005         case L_SET:
2006                 fp->f_offset = offset;
2007                 error = 0;
2008                 break;
2009         default:
2010                 error = EINVAL;
2011                 break;
2012         }
2013         *res = fp->f_offset;
2014 done:
2015         fdrop(fp);
2016         return (error);
2017 }
2018
2019 /*
2020  * lseek_args(int fd, int pad, off_t offset, int whence)
2021  *
2022  * Reposition read/write file offset.
2023  */
2024 int
2025 sys_lseek(struct lseek_args *uap)
2026 {
2027         int error;
2028
2029         error = kern_lseek(uap->fd, uap->offset, uap->whence,
2030             &uap->sysmsg_offset);
2031
2032         return (error);
2033 }
2034
2035 int
2036 kern_access(struct nlookupdata *nd, int aflags)
2037 {
2038         struct vnode *vp;
2039         int error, flags;
2040
2041         if ((error = nlookup(nd)) != 0)
2042                 return (error);
2043 retry:
2044         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2045         if (error)
2046                 return (error);
2047
2048         /* Flags == 0 means only check for existence. */
2049         if (aflags) {
2050                 flags = 0;
2051                 if (aflags & R_OK)
2052                         flags |= VREAD;
2053                 if (aflags & W_OK)
2054                         flags |= VWRITE;
2055                 if (aflags & X_OK)
2056                         flags |= VEXEC;
2057                 if ((flags & VWRITE) == 0 || 
2058                     (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2059                         error = VOP_ACCESS(vp, flags, nd->nl_cred);
2060
2061                 /*
2062                  * If the file handle is stale we have to re-resolve the
2063                  * entry.  This is a hack at the moment.
2064                  */
2065                 if (error == ESTALE) {
2066                         cache_setunresolved(&nd->nl_nch);
2067                         error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2068                         if (error == 0) {
2069                                 vput(vp);
2070                                 vp = NULL;
2071                                 goto retry;
2072                         }
2073                 }
2074         }
2075         vput(vp);
2076         return (error);
2077 }
2078
2079 /*
2080  * access_args(char *path, int flags)
2081  *
2082  * Check access permissions.
2083  */
2084 int
2085 sys_access(struct access_args *uap)
2086 {
2087         struct nlookupdata nd;
2088         int error;
2089
2090         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2091         if (error == 0)
2092                 error = kern_access(&nd, uap->flags);
2093         nlookup_done(&nd);
2094         return (error);
2095 }
2096
2097 int
2098 kern_stat(struct nlookupdata *nd, struct stat *st)
2099 {
2100         int error;
2101         struct vnode *vp;
2102         thread_t td;
2103
2104         if ((error = nlookup(nd)) != 0)
2105                 return (error);
2106 again:
2107         if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2108                 return (ENOENT);
2109
2110         td = curthread;
2111         if ((error = vget(vp, LK_SHARED)) != 0)
2112                 return (error);
2113         error = vn_stat(vp, st, nd->nl_cred);
2114
2115         /*
2116          * If the file handle is stale we have to re-resolve the entry.  This
2117          * is a hack at the moment.
2118          */
2119         if (error == ESTALE) {
2120                 cache_setunresolved(&nd->nl_nch);
2121                 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2122                 if (error == 0) {
2123                         vput(vp);
2124                         goto again;
2125                 }
2126         }
2127         vput(vp);
2128         return (error);
2129 }
2130
2131 /*
2132  * stat_args(char *path, struct stat *ub)
2133  *
2134  * Get file status; this version follows links.
2135  */
2136 int
2137 sys_stat(struct stat_args *uap)
2138 {
2139         struct nlookupdata nd;
2140         struct stat st;
2141         int error;
2142
2143         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2144         if (error == 0) {
2145                 error = kern_stat(&nd, &st);
2146                 if (error == 0)
2147                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2148         }
2149         nlookup_done(&nd);
2150         return (error);
2151 }
2152
2153 /*
2154  * lstat_args(char *path, struct stat *ub)
2155  *
2156  * Get file status; this version does not follow links.
2157  */
2158 int
2159 sys_lstat(struct lstat_args *uap)
2160 {
2161         struct nlookupdata nd;
2162         struct stat st;
2163         int error;
2164
2165         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2166         if (error == 0) {
2167                 error = kern_stat(&nd, &st);
2168                 if (error == 0)
2169                         error = copyout(&st, uap->ub, sizeof(*uap->ub));
2170         }
2171         nlookup_done(&nd);
2172         return (error);
2173 }
2174
2175 /*
2176  * pathconf_Args(char *path, int name)
2177  *
2178  * Get configurable pathname variables.
2179  */
2180 /* ARGSUSED */
2181 int
2182 sys_pathconf(struct pathconf_args *uap)
2183 {
2184         struct nlookupdata nd;
2185         struct vnode *vp;
2186         int error;
2187
2188         vp = NULL;
2189         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2190         if (error == 0)
2191                 error = nlookup(&nd);
2192         if (error == 0)
2193                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2194         nlookup_done(&nd);
2195         if (error == 0) {
2196                 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2197                 vput(vp);
2198         }
2199         return (error);
2200 }
2201
2202 /*
2203  * XXX: daver
2204  * kern_readlink isn't properly split yet.  There is a copyin burried
2205  * in VOP_READLINK().
2206  */
2207 int
2208 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2209 {
2210         struct thread *td = curthread;
2211         struct proc *p = td->td_proc;
2212         struct vnode *vp;
2213         struct iovec aiov;
2214         struct uio auio;
2215         int error;
2216
2217         if ((error = nlookup(nd)) != 0)
2218                 return (error);
2219         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2220         if (error)
2221                 return (error);
2222         if (vp->v_type != VLNK) {
2223                 error = EINVAL;
2224         } else {
2225                 aiov.iov_base = buf;
2226                 aiov.iov_len = count;
2227                 auio.uio_iov = &aiov;
2228                 auio.uio_iovcnt = 1;
2229                 auio.uio_offset = 0;
2230                 auio.uio_rw = UIO_READ;
2231                 auio.uio_segflg = UIO_USERSPACE;
2232                 auio.uio_td = td;
2233                 auio.uio_resid = count;
2234                 error = VOP_READLINK(vp, &auio, p->p_ucred);
2235         }
2236         vput(vp);
2237         *res = count - auio.uio_resid;
2238         return (error);
2239 }
2240
2241 /*
2242  * readlink_args(char *path, char *buf, int count)
2243  *
2244  * Return target name of a symbolic link.
2245  */
2246 int
2247 sys_readlink(struct readlink_args *uap)
2248 {
2249         struct nlookupdata nd;
2250         int error;
2251
2252         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2253         if (error == 0) {
2254                 error = kern_readlink(&nd, uap->buf, uap->count,
2255                                         &uap->sysmsg_result);
2256         }
2257         nlookup_done(&nd);
2258         return (error);
2259 }
2260
2261 static int
2262 setfflags(struct vnode *vp, int flags)
2263 {
2264         struct thread *td = curthread;
2265         struct proc *p = td->td_proc;
2266         int error;
2267         struct vattr vattr;
2268
2269         /*
2270          * Prevent non-root users from setting flags on devices.  When
2271          * a device is reused, users can retain ownership of the device
2272          * if they are allowed to set flags and programs assume that
2273          * chown can't fail when done as root.
2274          */
2275         if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
2276             ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2277                 return (error);
2278
2279         /*
2280          * note: vget is required for any operation that might mod the vnode
2281          * so VINACTIVE is properly cleared.
2282          */
2283         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2284                 VATTR_NULL(&vattr);
2285                 vattr.va_flags = flags;
2286                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2287                 vput(vp);
2288         }
2289         return (error);
2290 }
2291
2292 /*
2293  * chflags(char *path, int flags)
2294  *
2295  * Change flags of a file given a path name.
2296  */
2297 /* ARGSUSED */
2298 int
2299 sys_chflags(struct chflags_args *uap)
2300 {
2301         struct nlookupdata nd;
2302         struct vnode *vp;
2303         int error;
2304
2305         vp = NULL;
2306         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2307         /* XXX Add NLC flag indicating modifying operation? */
2308         if (error == 0)
2309                 error = nlookup(&nd);
2310         if (error == 0)
2311                 error = ncp_writechk(&nd.nl_nch);
2312         if (error == 0)
2313                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2314         nlookup_done(&nd);
2315         if (error == 0) {
2316                 error = setfflags(vp, uap->flags);
2317                 vrele(vp);
2318         }
2319         return (error);
2320 }
2321
2322 /*
2323  * fchflags_args(int fd, int flags)
2324  *
2325  * Change flags of a file given a file descriptor.
2326  */
2327 /* ARGSUSED */
2328 int
2329 sys_fchflags(struct fchflags_args *uap)
2330 {
2331         struct thread *td = curthread;
2332         struct proc *p = td->td_proc;
2333         struct file *fp;
2334         int error;
2335
2336         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2337                 return (error);
2338         if (fp->f_nchandle.ncp)
2339                 error = ncp_writechk(&fp->f_nchandle);
2340         if (error == 0)
2341                 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2342         fdrop(fp);
2343         return (error);
2344 }
2345
2346 static int
2347 setfmode(struct vnode *vp, int mode)
2348 {
2349         struct thread *td = curthread;
2350         struct proc *p = td->td_proc;
2351         int error;
2352         struct vattr vattr;
2353
2354         /*
2355          * note: vget is required for any operation that might mod the vnode
2356          * so VINACTIVE is properly cleared.
2357          */
2358         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2359                 VATTR_NULL(&vattr);
2360                 vattr.va_mode = mode & ALLPERMS;
2361                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2362                 vput(vp);
2363         }
2364         return error;
2365 }
2366
2367 int
2368 kern_chmod(struct nlookupdata *nd, int mode)
2369 {
2370         struct vnode *vp;
2371         int error;
2372
2373         /* XXX Add NLC flag indicating modifying operation? */
2374         if ((error = nlookup(nd)) != 0)
2375                 return (error);
2376         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2377                 return (error);
2378         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2379                 error = setfmode(vp, mode);
2380         vrele(vp);
2381         return (error);
2382 }
2383
2384 /*
2385  * chmod_args(char *path, int mode)
2386  *
2387  * Change mode of a file given path name.
2388  */
2389 /* ARGSUSED */
2390 int
2391 sys_chmod(struct chmod_args *uap)
2392 {
2393         struct nlookupdata nd;
2394         int error;
2395
2396         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2397         if (error == 0)
2398                 error = kern_chmod(&nd, uap->mode);
2399         nlookup_done(&nd);
2400         return (error);
2401 }
2402
2403 /*
2404  * lchmod_args(char *path, int mode)
2405  *
2406  * Change mode of a file given path name (don't follow links.)
2407  */
2408 /* ARGSUSED */
2409 int
2410 sys_lchmod(struct lchmod_args *uap)
2411 {
2412         struct nlookupdata nd;
2413         int error;
2414
2415         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2416         if (error == 0)
2417                 error = kern_chmod(&nd, uap->mode);
2418         nlookup_done(&nd);
2419         return (error);
2420 }
2421
2422 /*
2423  * fchmod_args(int fd, int mode)
2424  *
2425  * Change mode of a file given a file descriptor.
2426  */
2427 /* ARGSUSED */
2428 int
2429 sys_fchmod(struct fchmod_args *uap)
2430 {
2431         struct thread *td = curthread;
2432         struct proc *p = td->td_proc;
2433         struct file *fp;
2434         int error;
2435
2436         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2437                 return (error);
2438         if (fp->f_nchandle.ncp)
2439                 error = ncp_writechk(&fp->f_nchandle);
2440         if (error == 0)
2441                 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2442         fdrop(fp);
2443         return (error);
2444 }
2445
2446 static int
2447 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2448 {
2449         struct thread *td = curthread;
2450         struct proc *p = td->td_proc;
2451         int error;
2452         struct vattr vattr;
2453
2454         /*
2455          * note: vget is required for any operation that might mod the vnode
2456          * so VINACTIVE is properly cleared.
2457          */
2458         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2459                 VATTR_NULL(&vattr);
2460                 vattr.va_uid = uid;
2461                 vattr.va_gid = gid;
2462                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2463                 vput(vp);
2464         }
2465         return error;
2466 }
2467
2468 int
2469 kern_chown(struct nlookupdata *nd, int uid, int gid)
2470 {
2471         struct vnode *vp;
2472         int error;
2473
2474         /* XXX Add NLC flag indicating modifying operation? */
2475         if ((error = nlookup(nd)) != 0)
2476                 return (error);
2477         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2478                 return (error);
2479         if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2480                 error = setfown(vp, uid, gid);
2481         vrele(vp);
2482         return (error);
2483 }
2484
2485 /*
2486  * chown(char *path, int uid, int gid)
2487  *
2488  * Set ownership given a path name.
2489  */
2490 int
2491 sys_chown(struct chown_args *uap)
2492 {
2493         struct nlookupdata nd;
2494         int error;
2495
2496         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2497         if (error == 0)
2498                 error = kern_chown(&nd, uap->uid, uap->gid);
2499         nlookup_done(&nd);
2500         return (error);
2501 }
2502
2503 /*
2504  * lchown_args(char *path, int uid, int gid)
2505  *
2506  * Set ownership given a path name, do not cross symlinks.
2507  */
2508 int
2509 sys_lchown(struct lchown_args *uap)
2510 {
2511         struct nlookupdata nd;
2512         int error;
2513
2514         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2515         if (error == 0)
2516                 error = kern_chown(&nd, uap->uid, uap->gid);
2517         nlookup_done(&nd);
2518         return (error);
2519 }
2520
2521 /*
2522  * fchown_args(int fd, int uid, int gid)
2523  *
2524  * Set ownership given a file descriptor.
2525  */
2526 /* ARGSUSED */
2527 int
2528 sys_fchown(struct fchown_args *uap)
2529 {
2530         struct thread *td = curthread;
2531         struct proc *p = td->td_proc;
2532         struct file *fp;
2533         int error;
2534
2535         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2536                 return (error);
2537         if (fp->f_nchandle.ncp)
2538                 error = ncp_writechk(&fp->f_nchandle);
2539         if (error == 0)
2540                 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2541         fdrop(fp);
2542         return (error);
2543 }
2544
2545 static int
2546 getutimes(const struct timeval *tvp, struct timespec *tsp)
2547 {
2548         struct timeval tv[2];
2549
2550         if (tvp == NULL) {
2551                 microtime(&tv[0]);
2552                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2553                 tsp[1] = tsp[0];
2554         } else {
2555                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2556                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2557         }
2558         return 0;
2559 }
2560
2561 static int
2562 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2563 {
2564         struct thread *td = curthread;
2565         struct proc *p = td->td_proc;
2566         int error;
2567         struct vattr vattr;
2568
2569         /*
2570          * note: vget is required for any operation that might mod the vnode
2571          * so VINACTIVE is properly cleared.
2572          */
2573         if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2574                 VATTR_NULL(&vattr);
2575                 vattr.va_atime = ts[0];
2576                 vattr.va_mtime = ts[1];
2577                 if (nullflag)
2578                         vattr.va_vaflags |= VA_UTIMES_NULL;
2579                 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2580                 vput(vp);
2581         }
2582         return error;
2583 }
2584
2585 int
2586 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2587 {
2588         struct timespec ts[2];
2589         struct vnode *vp;
2590         int error;
2591
2592         if ((error = getutimes(tptr, ts)) != 0)
2593                 return (error);
2594         /* XXX Add NLC flag indicating modifying operation? */
2595         if ((error = nlookup(nd)) != 0)
2596                 return (error);
2597         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2598                 return (error);
2599         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2600                 return (error);
2601         error = setutimes(vp, ts, tptr == NULL);
2602         vrele(vp);
2603         return (error);
2604 }
2605
2606 /*
2607  * utimes_args(char *path, struct timeval *tptr)
2608  *
2609  * Set the access and modification times of a file.
2610  */
2611 int
2612 sys_utimes(struct utimes_args *uap)
2613 {
2614         struct timeval tv[2];
2615         struct nlookupdata nd;
2616         int error;
2617
2618         if (uap->tptr) {
2619                 error = copyin(uap->tptr, tv, sizeof(tv));
2620                 if (error)
2621                         return (error);
2622         }
2623         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2624         if (error == 0)
2625                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2626         nlookup_done(&nd);
2627         return (error);
2628 }
2629
2630 /*
2631  * lutimes_args(char *path, struct timeval *tptr)
2632  *
2633  * Set the access and modification times of a file.
2634  */
2635 int
2636 sys_lutimes(struct lutimes_args *uap)
2637 {
2638         struct timeval tv[2];
2639         struct nlookupdata nd;
2640         int error;
2641
2642         if (uap->tptr) {
2643                 error = copyin(uap->tptr, tv, sizeof(tv));
2644                 if (error)
2645                         return (error);
2646         }
2647         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2648         if (error == 0)
2649                 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2650         nlookup_done(&nd);
2651         return (error);
2652 }
2653
2654 int
2655 kern_futimes(int fd, struct timeval *tptr)
2656 {
2657         struct thread *td = curthread;
2658         struct proc *p = td->td_proc;
2659         struct timespec ts[2];
2660         struct file *fp;
2661         int error;
2662
2663         error = getutimes(tptr, ts);
2664         if (error)
2665                 return (error);
2666         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2667                 return (error);
2668         if (fp->f_nchandle.ncp)
2669                 error = ncp_writechk(&fp->f_nchandle);
2670         if (error == 0)
2671                 error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2672         fdrop(fp);
2673         return (error);
2674 }
2675
2676 /*
2677  * futimes_args(int fd, struct timeval *tptr)
2678  *
2679  * Set the access and modification times of a file.
2680  */
2681 int
2682 sys_futimes(struct futimes_args *uap)
2683 {
2684         struct timeval tv[2];
2685         int error;
2686
2687         if (uap->tptr) {
2688                 error = copyin(uap->tptr, tv, sizeof(tv));
2689                 if (error)
2690                         return (error);
2691         }
2692
2693         error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2694
2695         return (error);
2696 }
2697
2698 int
2699 kern_truncate(struct nlookupdata *nd, off_t length)
2700 {
2701         struct vnode *vp;
2702         struct vattr vattr;
2703         int error;
2704
2705         if (length < 0)
2706                 return(EINVAL);
2707         /* XXX Add NLC flag indicating modifying operation? */
2708         if ((error = nlookup(nd)) != 0)
2709                 return (error);
2710         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2711                 return (error);
2712         if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2713                 return (error);
2714         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2715                 vrele(vp);
2716                 return (error);
2717         }
2718         if (vp->v_type == VDIR) {
2719                 error = EISDIR;
2720         } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2721             (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2722                 VATTR_NULL(&vattr);
2723                 vattr.va_size = length;
2724                 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2725         }
2726         vput(vp);
2727         return (error);
2728 }
2729
2730 /*
2731  * truncate(char *path, int pad, off_t length)
2732  *
2733  * Truncate a file given its path name.
2734  */
2735 int
2736 sys_truncate(struct truncate_args *uap)
2737 {
2738         struct nlookupdata nd;
2739         int error;
2740
2741         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2742         if (error == 0)
2743                 error = kern_truncate(&nd, uap->length);
2744         nlookup_done(&nd);
2745         return error;
2746 }
2747
2748 int
2749 kern_ftruncate(int fd, off_t length)
2750 {
2751         struct thread *td = curthread;
2752         struct proc *p = td->td_proc;
2753         struct vattr vattr;
2754         struct vnode *vp;
2755         struct file *fp;
2756         int error;
2757
2758         if (length < 0)
2759                 return(EINVAL);
2760         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2761                 return (error);
2762         if (fp->f_nchandle.ncp) {
2763                 error = ncp_writechk(&fp->f_nchandle);
2764                 if (error)
2765                         goto done;
2766         }
2767         if ((fp->f_flag & FWRITE) == 0) {
2768                 error = EINVAL;
2769                 goto done;
2770         }
2771         vp = (struct vnode *)fp->f_data;
2772         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2773         if (vp->v_type == VDIR) {
2774                 error = EISDIR;
2775         } else if ((error = vn_writechk(vp, NULL)) == 0) {
2776                 VATTR_NULL(&vattr);
2777                 vattr.va_size = length;
2778                 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2779         }
2780         vn_unlock(vp);
2781 done:
2782         fdrop(fp);
2783         return (error);
2784 }
2785
2786 /*
2787  * ftruncate_args(int fd, int pad, off_t length)
2788  *
2789  * Truncate a file given a file descriptor.
2790  */
2791 int
2792 sys_ftruncate(struct ftruncate_args *uap)
2793 {
2794         int error;
2795
2796         error = kern_ftruncate(uap->fd, uap->length);
2797
2798         return (error);
2799 }
2800
2801 /*
2802  * fsync(int fd)
2803  *
2804  * Sync an open file.
2805  */
2806 /* ARGSUSED */
2807 int
2808 sys_fsync(struct fsync_args *uap)
2809 {
2810         struct thread *td = curthread;
2811         struct proc *p = td->td_proc;
2812         struct vnode *vp;
2813         struct file *fp;
2814         vm_object_t obj;
2815         int error;
2816
2817         if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2818                 return (error);
2819         vp = (struct vnode *)fp->f_data;
2820         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2821         if ((obj = vp->v_object) != NULL)
2822                 vm_object_page_clean(obj, 0, 0, 0);
2823         if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2824             vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2825             bioops.io_fsync) {
2826                 error = (*bioops.io_fsync)(vp);
2827         }
2828         vn_unlock(vp);
2829         fdrop(fp);
2830         return (error);
2831 }
2832
2833 int
2834 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2835 {
2836         struct nchandle fnchd;
2837         struct nchandle tnchd;
2838         struct namecache *ncp;
2839         struct mount *mp;
2840         int error;
2841
2842         bwillwrite();
2843         if ((error = nlookup(fromnd)) != 0)
2844                 return (error);
2845         if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2846                 return (ENOENT);
2847         fnchd.mount = fromnd->nl_nch.mount;
2848         cache_hold(&fnchd);
2849
2850         /*
2851          * unlock the source nch so we can lookup the target nch without
2852          * deadlocking.  The target may or may not exist so we do not check
2853          * for a target vp like kern_mkdir() and other creation functions do.
2854          *
2855          * The source and target directories are ref'd and rechecked after
2856          * everything is relocked to determine if the source or target file
2857          * has been renamed.
2858          */
2859         KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2860         fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2861         cache_unlock(&fromnd->nl_nch);
2862
2863         tond->nl_flags |= NLC_CREATE;
2864         if ((error = nlookup(tond)) != 0) {
2865                 cache_drop(&fnchd);
2866                 return (error);
2867         }
2868         if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2869                 cache_drop(&fnchd);
2870                 return (ENOENT);
2871         }
2872         tnchd.mount = tond->nl_nch.mount;
2873         cache_hold(&tnchd);
2874
2875         /*
2876          * If the source and target are the same there is nothing to do
2877          */
2878         if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2879                 cache_drop(&fnchd);
2880                 cache_drop(&tnchd);
2881                 return (0);
2882         }
2883
2884         /*
2885          * Mount points cannot be renamed or overwritten
2886          */
2887         if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2888             NCF_ISMOUNTPT
2889         ) {
2890                 cache_drop(&fnchd);
2891                 cache_drop(&tnchd);
2892                 return (EINVAL);
2893         }
2894
2895         /*
2896          * relock the source ncp.  NOTE AFTER RELOCKING: the source ncp
2897          * may have become invalid while it was unlocked, nc_vp and nc_mount
2898          * could be NULL.
2899          */
2900         if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2901                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2902         } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2903                 cache_lock(&fromnd->nl_nch);
2904                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2905         } else {
2906                 cache_unlock(&tond->nl_nch);
2907                 cache_lock(&fromnd->nl_nch);
2908                 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2909                 cache_lock(&tond->nl_nch);
2910                 cache_resolve(&tond->nl_nch, tond->nl_cred);
2911         }
2912         fromnd->nl_flags |= NLC_NCPISLOCKED;
2913
2914         /*
2915          * make sure the parent directories linkages are the same
2916          */
2917         if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2918             tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2919                 cache_drop(&fnchd);
2920                 cache_drop(&tnchd);
2921                 return (ENOENT);
2922         }
2923
2924         /*
2925          * Both the source and target must be within the same filesystem and
2926          * in the same filesystem as their parent directories within the
2927          * namecache topology.
2928          *
2929          * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2930          */
2931         mp = fnchd.mount;
2932         if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2933             mp != tond->nl_nch.mount) {
2934                 cache_drop(&fnchd);
2935                 cache_drop(&tnchd);
2936                 return (EXDEV);
2937         }
2938
2939         /*
2940          * Make sure the mount point is writable
2941          */
2942         if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2943                 cache_drop(&fnchd);
2944                 cache_drop(&tnchd);
2945                 return (error);
2946         }
2947
2948         /*
2949          * If the target exists and either the source or target is a directory,
2950          * then both must be directories.
2951          *
2952          * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2953          * have become NULL.
2954          */
2955         if (tond->nl_nch.ncp->nc_vp) {
2956                 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2957                         error = ENOENT;
2958                 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2959                         if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2960                                 error = ENOTDIR;
2961                 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2962                         error = EISDIR;
2963                 }
2964         }
2965
2966         /*
2967          * You cannot rename a source into itself or a subdirectory of itself.
2968          * We check this by travsersing the target directory upwards looking
2969          * for a match against the source.
2970          */
2971         if (error == 0) {
2972                 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2973                         if (fromnd->nl_nch.ncp == ncp) {
2974                                 error = EINVAL;
2975                                 break;
2976                         }
2977                 }
2978         }
2979
2980         cache_drop(&fnchd);
2981         cache_drop(&tnchd);
2982
2983         /*
2984          * Even though the namespaces are different, they may still represent
2985          * hardlinks to the same file.  The filesystem might have a hard time
2986          * with this so we issue a NREMOVE of the source instead of a NRENAME
2987          * when we detect the situation.
2988          */
2989         if (error == 0) {
2990                 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
2991                         error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
2992                 } else {
2993                         error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 
2994                                             tond->nl_cred);
2995                 }
2996         }
2997         return (error);
2998 }
2999
3000 /*
3001  * rename_args(char *from, char *to)
3002  *
3003  * Rename files.  Source and destination must either both be directories,
3004  * or both not be directories.  If target is a directory, it must be empty.
3005  */
3006 int
3007 sys_rename(struct rename_args *uap)
3008 {
3009         struct nlookupdata fromnd, tond;
3010         int error;
3011
3012         error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3013         if (error == 0) {
3014                 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3015                 if (error == 0)
3016                         error = kern_rename(&fromnd, &tond);
3017                 nlookup_done(&tond);
3018         }
3019         nlookup_done(&fromnd);
3020         return (error);
3021 }
3022
3023 int
3024 kern_mkdir(struct nlookupdata *nd, int mode)
3025 {
3026         struct thread *td = curthread;
3027         struct proc *p = td->td_proc;
3028         struct vnode *vp;
3029         struct vattr vattr;
3030         int error;
3031
3032         bwillwrite();
3033         nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3034         if ((error = nlookup(nd)) != 0)
3035                 return (error);
3036
3037         if (nd->nl_nch.ncp->nc_vp)
3038                 return (EEXIST);
3039         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3040                 return (error);
3041
3042         VATTR_NULL(&vattr);
3043         vattr.va_type = VDIR;
3044         vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3045
3046         vp = NULL;
3047         error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3048         if (error == 0)
3049                 vput(vp);
3050         return (error);
3051 }
3052
3053 /*
3054  * mkdir_args(char *path, int mode)
3055  *
3056  * Make a directory file.
3057  */
3058 /* ARGSUSED */
3059 int
3060 sys_mkdir(struct mkdir_args *uap)
3061 {
3062         struct nlookupdata nd;
3063         int error;
3064
3065         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3066         if (error == 0)
3067                 error = kern_mkdir(&nd, uap->mode);
3068         nlookup_done(&nd);
3069         return (error);
3070 }
3071
3072 int
3073 kern_rmdir(struct nlookupdata *nd)
3074 {
3075         int error;
3076
3077         bwillwrite();
3078         nd->nl_flags |= NLC_DELETE;
3079         if ((error = nlookup(nd)) != 0)
3080                 return (error);
3081
3082         /*
3083          * Do not allow directories representing mount points to be
3084          * deleted, even if empty.  Check write perms on mount point
3085          * in case the vnode is aliased (aka nullfs).
3086          */
3087         if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3088                 return (EINVAL);
3089         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3090                 return (error);
3091
3092         error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3093         return (error);
3094 }
3095
3096 /*
3097  * rmdir_args(char *path)
3098  *
3099  * Remove a directory file.
3100  */
3101 /* ARGSUSED */
3102 int
3103 sys_rmdir(struct rmdir_args *uap)
3104 {
3105         struct nlookupdata nd;
3106         int error;
3107
3108         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3109         if (error == 0)
3110                 error = kern_rmdir(&nd);
3111         nlookup_done(&nd);
3112         return (error);
3113 }
3114
3115 int
3116 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3117     enum uio_seg direction)
3118 {
3119         struct thread *td = curthread;
3120         struct proc *p = td->td_proc;
3121         struct vnode *vp;
3122         struct file *fp;
3123         struct uio auio;
3124         struct iovec aiov;
3125         long loff;
3126         int error, eofflag;
3127
3128         if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3129                 return (error);
3130         if ((fp->f_flag & FREAD) == 0) {
3131                 error = EBADF;
3132                 goto done;
3133         }
3134         vp = (struct vnode *)fp->f_data;
3135 unionread:
3136         if (vp->v_type != VDIR) {
3137                 error = EINVAL;
3138                 goto done;
3139         }
3140         aiov.iov_base = buf;
3141         aiov.iov_len = count;
3142         auio.uio_iov = &aiov;
3143         auio.uio_iovcnt = 1;
3144         auio.uio_rw = UIO_READ;
3145         auio.uio_segflg = direction;
3146         auio.uio_td = td;
3147         auio.uio_resid = count;
3148         loff = auio.uio_offset = fp->f_offset;
3149         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3150         fp->f_offset = auio.uio_offset;
3151         if (error)
3152                 goto done;
3153         if (count == auio.uio_resid) {
3154                 if (union_dircheckp) {
3155                         error = union_dircheckp(td, &vp, fp);
3156                         if (error == -1)
3157                                 goto unionread;
3158                         if (error)
3159                                 goto done;
3160                 }
3161 #if 0
3162                 if ((vp->v_flag & VROOT) &&
3163                     (vp->v_mount->mnt_flag & MNT_UNION)) {
3164                         struct vnode *tvp = vp;
3165                         vp = vp->v_mount->mnt_vnodecovered;
3166                         vref(vp);
3167                         fp->f_data = vp;
3168                         fp->f_offset = 0;
3169                         vrele(tvp);
3170                         goto unionread;
3171                 }
3172 #endif
3173         }
3174         if (basep) {
3175                 *basep = loff;
3176         }
3177         *res = count - auio.uio_resid;
3178 done:
3179         fdrop(fp);
3180         return (error);
3181 }
3182
3183 /*
3184  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3185  *
3186  * Read a block of directory entries in a file system independent format.
3187  */
3188 int
3189 sys_getdirentries(struct getdirentries_args *uap)
3190 {
3191         long base;
3192         int error;
3193
3194         error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3195             &uap->sysmsg_result, UIO_USERSPACE);
3196
3197         if (error == 0)
3198                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3199         return (error);
3200 }
3201
3202 /*
3203  * getdents_args(int fd, char *buf, size_t count)
3204  */
3205 int
3206 sys_getdents(struct getdents_args *uap)
3207 {
3208         int error;
3209
3210         error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3211             &uap->sysmsg_result, UIO_USERSPACE);
3212
3213         return (error);
3214 }
3215
3216 /*
3217  * umask(int newmask)
3218  *
3219  * Set the mode mask for creation of filesystem nodes.
3220  *
3221  * MP SAFE
3222  */
3223 int
3224 sys_umask(struct umask_args *uap)
3225 {
3226         struct thread *td = curthread;
3227         struct proc *p = td->td_proc;
3228         struct filedesc *fdp;
3229
3230         fdp = p->p_fd;
3231         uap->sysmsg_result = fdp->fd_cmask;
3232         fdp->fd_cmask = uap->newmask & ALLPERMS;
3233         return (0);
3234 }
3235
3236 /*
3237  * revoke(char *path)
3238  *
3239  * Void all references to file by ripping underlying filesystem
3240  * away from vnode.
3241  */
3242 /* ARGSUSED */
3243 int
3244 sys_revoke(struct revoke_args *uap)
3245 {
3246         struct nlookupdata nd;
3247         struct vattr vattr;
3248         struct vnode *vp;
3249         struct ucred *cred;
3250         int error;
3251
3252         vp = NULL;
3253         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3254         if (error == 0)
3255                 error = nlookup(&nd);
3256         if (error == 0)
3257                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3258         cred = crhold(nd.nl_cred);
3259         nlookup_done(&nd);
3260         if (error == 0) {
3261                 if (vp->v_type != VCHR && vp->v_type != VBLK)
3262                         error = EINVAL;
3263                 if (error == 0)
3264                         error = VOP_GETATTR(vp, &vattr);
3265                 if (error == 0 && cred->cr_uid != vattr.va_uid)
3266                         error = suser_cred(cred, PRISON_ROOT);
3267                 if (error == 0 && count_udev(vp->v_udev) > 0) {
3268                         error = 0;
3269                         vx_lock(vp);
3270                         VOP_REVOKE(vp, REVOKEALL);
3271                         vx_unlock(vp);
3272                 }
3273                 vrele(vp);
3274         }
3275         if (cred)
3276                 crfree(cred);
3277         return (error);
3278 }
3279
3280 /*
3281  * getfh_args(char *fname, fhandle_t *fhp)
3282  *
3283  * Get (NFS) file handle
3284  */
3285 int
3286 sys_getfh(struct getfh_args *uap)
3287 {
3288         struct thread *td = curthread;
3289         struct nlookupdata nd;
3290         fhandle_t fh;
3291         struct vnode *vp;
3292         int error;
3293
3294         /*
3295          * Must be super user
3296          */
3297         if ((error = suser(td)) != 0)
3298                 return (error);
3299
3300         vp = NULL;
3301         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3302         if (error == 0)
3303                 error = nlookup(&nd);
3304         if (error == 0)
3305                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3306         nlookup_done(&nd);
3307         if (error == 0) {
3308                 bzero(&fh, sizeof(fh));
3309                 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3310                 error = VFS_VPTOFH(vp, &fh.fh_fid);
3311                 vput(vp);
3312                 if (error == 0)
3313                         error = copyout(&fh, uap->fhp, sizeof(fh));
3314         }
3315         return (error);
3316 }
3317
3318 /*
3319  * fhopen_args(const struct fhandle *u_fhp, int flags)
3320  *
3321  * syscall for the rpc.lockd to use to translate a NFS file handle into
3322  * an open descriptor.
3323  *
3324  * warning: do not remove the suser() call or this becomes one giant
3325  * security hole.
3326  */
3327 int
3328 sys_fhopen(struct fhopen_args *uap)
3329 {
3330         struct thread *td = curthread;
3331         struct proc *p = td->td_proc;
3332         struct mount *mp;
3333         struct vnode *vp;
3334         struct fhandle fhp;
3335         struct vattr vat;
3336         struct vattr *vap = &vat;
3337         struct flock lf;
3338         int fmode, mode, error, type;
3339         struct file *nfp; 
3340         struct file *fp;
3341         int indx;
3342
3343         /*
3344          * Must be super user
3345          */
3346         error = suser(td);
3347         if (error)
3348                 return (error);
3349
3350         fmode = FFLAGS(uap->flags);
3351         /* why not allow a non-read/write open for our lockd? */
3352         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3353                 return (EINVAL);
3354         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3355         if (error)
3356                 return(error);
3357         /* find the mount point */
3358         mp = vfs_getvfs(&fhp.fh_fsid);
3359         if (mp == NULL)
3360                 return (ESTALE);
3361         /* now give me my vnode, it gets returned to me locked */
3362         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3363         if (error)
3364                 return (error);
3365         /*
3366          * from now on we have to make sure not
3367          * to forget about the vnode
3368          * any error that causes an abort must vput(vp) 
3369          * just set error = err and 'goto bad;'.
3370          */
3371
3372         /* 
3373          * from vn_open 
3374          */
3375         if (vp->v_type == VLNK) {
3376                 error = EMLINK;
3377                 goto bad;
3378         }
3379         if (vp->v_type == VSOCK) {
3380                 error = EOPNOTSUPP;
3381                 goto bad;
3382         }
3383         mode = 0;
3384         if (fmode & (FWRITE | O_TRUNC)) {
3385                 if (vp->v_type == VDIR) {
3386                         error = EISDIR;
3387                         goto bad;
3388                 }
3389                 error = vn_writechk(vp, NULL);
3390                 if (error)
3391                         goto bad;
3392                 mode |= VWRITE;
3393         }
3394         if (fmode & FREAD)
3395                 mode |= VREAD;
3396         if (mode) {
3397                 error = VOP_ACCESS(vp, mode, p->p_ucred);
3398                 if (error)
3399                         goto bad;
3400         }
3401         if (fmode & O_TRUNC) {
3402                 vn_unlock(vp);                          /* XXX */
3403                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
3404                 VATTR_NULL(vap);
3405                 vap->va_size = 0;
3406                 error = VOP_SETATTR(vp, vap, p->p_ucred);
3407                 if (error)
3408                         goto bad;
3409         }
3410
3411         /*
3412          * VOP_OPEN needs the file pointer so it can potentially override
3413          * it.
3414          *
3415          * WARNING! no f_nchandle will be associated when fhopen()ing a
3416          * directory.  XXX
3417          */
3418         if ((error = falloc(p, &nfp, &indx)) != 0)
3419                 goto bad;
3420         fp = nfp;
3421
3422         error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3423         if (error) {
3424                 /*
3425                  * setting f_ops this way prevents VOP_CLOSE from being
3426                  * called or fdrop() releasing the vp from v_data.   Since
3427                  * the VOP_OPEN failed we don't want to VOP_CLOSE.
3428                  */
3429                 fp->f_ops = &badfileops;
3430                 fp->f_data = NULL;
3431                 goto bad_drop;
3432         }
3433
3434         /*
3435          * The fp is given its own reference, we still have our ref and lock.
3436          *
3437          * Assert that all regular files must be created with a VM object.
3438          */
3439         if (vp->v_type == VREG && vp->v_object == NULL) {
3440                 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3441                 goto bad_drop;
3442         }
3443
3444         /*
3445          * The open was successful.  Handle any locking requirements.
3446          */
3447         if (fmode & (O_EXLOCK | O_SHLOCK)) {
3448                 lf.l_whence = SEEK_SET;
3449                 lf.l_start = 0;
3450                 lf.l_len = 0;
3451                 if (fmode & O_EXLOCK)
3452                         lf.l_type = F_WRLCK;
3453                 else
3454                         lf.l_type = F_RDLCK;
3455                 if (fmode & FNONBLOCK)
3456                         type = 0;
3457                 else
3458                         type = F_WAIT;
3459                 vn_unlock(vp);
3460                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3461                         /*
3462                          * release our private reference.
3463                          */
3464                         fsetfd(p, NULL, indx);
3465                         fdrop(fp);
3466                         vrele(vp);
3467                         return (error);
3468                 }
3469                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3470                 fp->f_flag |= FHASLOCK;
3471         }
3472
3473         /*
3474          * Clean up.  Associate the file pointer with the previously
3475          * reserved descriptor and return it.
3476          */
3477         vput(vp);
3478         fsetfd(p, fp, indx);
3479         fdrop(fp);
3480         uap->sysmsg_result = indx;
3481         return (0);
3482
3483 bad_drop:
3484         fsetfd(p, NULL, indx);
3485         fdrop(fp);
3486 bad:
3487         vput(vp);
3488         return (error);
3489 }
3490
3491 /*
3492  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3493  */
3494 int
3495 sys_fhstat(struct fhstat_args *uap)
3496 {
3497         struct thread *td = curthread;
3498         struct stat sb;
3499         fhandle_t fh;
3500         struct mount *mp;
3501         struct vnode *vp;
3502         int error;
3503
3504         /*
3505          * Must be super user
3506          */
3507         error = suser(td);
3508         if (error)
3509                 return (error);
3510         
3511         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3512         if (error)
3513                 return (error);
3514
3515         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3516                 return (ESTALE);
3517         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3518                 return (error);
3519         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3520         vput(vp);
3521         if (error)
3522                 return (error);
3523         error = copyout(&sb, uap->sb, sizeof(sb));
3524         return (error);
3525 }
3526
3527 /*
3528  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3529  */
3530 int
3531 sys_fhstatfs(struct fhstatfs_args *uap)
3532 {
3533         struct thread *td = curthread;
3534         struct proc *p = td->td_proc;
3535         struct statfs *sp;
3536         struct mount *mp;
3537         struct vnode *vp;
3538         struct statfs sb;
3539         char *fullpath, *freepath;
3540         fhandle_t fh;
3541         int error;
3542
3543         /*
3544          * Must be super user
3545          */
3546         if ((error = suser(td)))
3547                 return (error);
3548
3549         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3550                 return (error);
3551
3552         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3553                 return (ESTALE);
3554
3555         if (p != NULL && !chroot_visible_mnt(mp, p))
3556                 return (ESTALE);
3557
3558         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3559                 return (error);
3560         mp = vp->v_mount;
3561         sp = &mp->mnt_stat;
3562         vput(vp);
3563         if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3564                 return (error);
3565
3566         error = mount_path(p, mp, &fullpath, &freepath);
3567         if (error)
3568                 return(error);
3569         bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3570         strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3571         kfree(freepath, M_TEMP);
3572
3573         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3574         if (suser(td)) {
3575                 bcopy(sp, &sb, sizeof(sb));
3576                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3577                 sp = &sb;
3578         }
3579         return (copyout(sp, uap->buf, sizeof(*sp)));
3580 }
3581
3582 /*
3583  * Syscall to push extended attribute configuration information into the
3584  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3585  * a command (int cmd), and attribute name and misc data.  For now, the
3586  * attribute name is left in userspace for consumption by the VFS_op.
3587  * It will probably be changed to be copied into sysspace by the
3588  * syscall in the future, once issues with various consumers of the
3589  * attribute code have raised their hands.
3590  *
3591  * Currently this is used only by UFS Extended Attributes.
3592  */
3593 int
3594 sys_extattrctl(struct extattrctl_args *uap)
3595 {
3596         struct nlookupdata nd;
3597         struct mount *mp;
3598         struct vnode *vp;
3599         int error;
3600
3601         vp = NULL;
3602         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3603         if (error == 0)
3604                 error = nlookup(&nd);
3605         if (error == 0) {
3606                 mp = nd.nl_nch.mount;
3607                 error = VFS_EXTATTRCTL(mp, uap->cmd, 
3608                                 uap->attrname, uap->arg, 
3609                                 nd.nl_cred);
3610         }
3611         nlookup_done(&nd);
3612         return (error);
3613 }
3614
3615 /*
3616  * Syscall to set a named extended attribute on a file or directory.
3617  * Accepts attribute name, and a uio structure pointing to the data to set.
3618  * The uio is consumed in the style of writev().  The real work happens
3619  * in VOP_SETEXTATTR().
3620  */
3621 int
3622 sys_extattr_set_file(struct extattr_set_file_args *uap)
3623 {
3624         char attrname[EXTATTR_MAXNAMELEN];
3625         struct iovec aiov[UIO_SMALLIOV];
3626         struct iovec *needfree;
3627         struct nlookupdata nd;
3628         struct iovec *iov;
3629         struct vnode *vp;
3630         struct uio auio;
3631         u_int iovlen;
3632         u_int cnt;
3633         int error;
3634         int i;
3635
3636         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3637         if (error)
3638                 return (error);
3639
3640         vp = NULL;
3641         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3642         if (error == 0)
3643                 error = nlookup(&nd);
3644         if (error == 0)
3645                 error = ncp_writechk(&nd.nl_nch);
3646         if (error == 0)
3647                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3648         if (error) {
3649                 nlookup_done(&nd);
3650                 return (error);
3651         }
3652
3653         needfree = NULL;
3654         iovlen = uap->iovcnt * sizeof(struct iovec);
3655         if (uap->iovcnt > UIO_SMALLIOV) {
3656                 if (uap->iovcnt > UIO_MAXIOV) {
3657                         error = EINVAL;
3658                         goto done;
3659                 }
3660                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3661                 needfree = iov;
3662         } else {
3663                 iov = aiov;
3664         }
3665         auio.uio_iov = iov;
3666         auio.uio_iovcnt = uap->iovcnt;
3667         auio.uio_rw = UIO_WRITE;
3668         auio.uio_segflg = UIO_USERSPACE;
3669         auio.uio_td = nd.nl_td;
3670         auio.uio_offset = 0;
3671         if ((error = copyin(uap->iovp, iov, iovlen)))
3672                 goto done;
3673         auio.uio_resid = 0;
3674         for (i = 0; i < uap->iovcnt; i++) {
3675                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3676                         error = EINVAL;
3677                         goto done;
3678                 }
3679                 auio.uio_resid += iov->iov_len;
3680                 iov++;
3681         }
3682         cnt = auio.uio_resid;
3683         error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3684         cnt -= auio.uio_resid;
3685         uap->sysmsg_result = cnt;
3686 done:
3687         vput(vp);
3688         nlookup_done(&nd);
3689         if (needfree)
3690                 FREE(needfree, M_IOV);
3691         return (error);
3692 }
3693
3694 /*
3695  * Syscall to get a named extended attribute on a file or directory.
3696  * Accepts attribute name, and a uio structure pointing to a buffer for the
3697  * data.  The uio is consumed in the style of readv().  The real work
3698  * happens in VOP_GETEXTATTR();
3699  */
3700 int
3701 sys_extattr_get_file(struct extattr_get_file_args *uap)
3702 {
3703         char attrname[EXTATTR_MAXNAMELEN];
3704         struct iovec aiov[UIO_SMALLIOV];
3705         struct iovec *needfree;
3706         struct nlookupdata nd;
3707         struct iovec *iov;
3708         struct vnode *vp;
3709         struct uio auio;
3710         u_int iovlen;
3711         u_int cnt;
3712         int error;
3713         int i;
3714
3715         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3716         if (error)
3717                 return (error);
3718
3719         vp = NULL;
3720         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3721         if (error == 0)
3722                 error = nlookup(&nd);
3723         if (error == 0)
3724                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3725         if (error) {
3726                 nlookup_done(&nd);
3727                 return (error);
3728         }
3729
3730         iovlen = uap->iovcnt * sizeof (struct iovec);
3731         needfree = NULL;
3732         if (uap->iovcnt > UIO_SMALLIOV) {
3733                 if (uap->iovcnt > UIO_MAXIOV) {
3734                         error = EINVAL;
3735                         goto done;
3736                 }
3737                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3738                 needfree = iov;
3739         } else {
3740                 iov = aiov;
3741         }
3742         auio.uio_iov = iov;
3743         auio.uio_iovcnt = uap->iovcnt;
3744         auio.uio_rw = UIO_READ;
3745         auio.uio_segflg = UIO_USERSPACE;
3746         auio.uio_td = nd.nl_td;
3747         auio.uio_offset = 0;
3748         if ((error = copyin(uap->iovp, iov, iovlen)))
3749                 goto done;
3750         auio.uio_resid = 0;
3751         for (i = 0; i < uap->iovcnt; i++) {
3752                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3753                         error = EINVAL;
3754                         goto done;
3755                 }
3756                 auio.uio_resid += iov->iov_len;
3757                 iov++;
3758         }
3759         cnt = auio.uio_resid;
3760         error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3761         cnt -= auio.uio_resid;
3762         uap->sysmsg_result = cnt;
3763 done:
3764         vput(vp);
3765         nlookup_done(&nd);
3766         if (needfree)
3767                 FREE(needfree, M_IOV);
3768         return(error);
3769 }
3770
3771 /*
3772  * Syscall to delete a named extended attribute from a file or directory.
3773  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3774  */
3775 int
3776 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3777 {
3778         char attrname[EXTATTR_MAXNAMELEN];
3779         struct nlookupdata nd;
3780         struct vnode *vp;
3781         int error;
3782
3783         error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3784         if (error)
3785                 return(error);
3786
3787         vp = NULL;
3788         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3789         if (error == 0)
3790                 error = nlookup(&nd);
3791         if (error == 0)
3792                 error = ncp_writechk(&nd.nl_nch);
3793         if (error == 0)
3794                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3795         if (error) {
3796                 nlookup_done(&nd);
3797                 return (error);
3798         }
3799
3800         error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3801         vput(vp);
3802         nlookup_done(&nd);
3803         return(error);
3804 }
3805
3806 /*
3807  * Determine if the mount is visible to the process.
3808  */
3809 static int
3810 chroot_visible_mnt(struct mount *mp, struct proc *p)
3811 {
3812         struct nchandle nch;
3813
3814         /*
3815          * Traverse from the mount point upwards.  If we hit the process
3816          * root then the mount point is visible to the process.
3817          */
3818         nch = mp->mnt_ncmountpt;
3819         while (nch.ncp) {
3820                 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3821                     nch.ncp == p->p_fd->fd_nrdir.ncp) {
3822                         return(1);
3823                 }
3824                 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3825                         nch = nch.mount->mnt_ncmounton;
3826                 } else {
3827                         nch.ncp = nch.ncp->nc_parent;
3828                 }
3829         }
3830
3831         /*
3832          * If the mount point is not visible to the process, but the
3833          * process root is in a subdirectory of the mount, return
3834          * TRUE anyway.
3835          */
3836         if (p->p_fd->fd_nrdir.mount == mp)
3837                 return(1);
3838
3839         return(0);
3840 }
3841