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