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