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