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