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