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