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