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