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