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