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