]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_extattr.c
This commit was generated by cvs2svn to compensate for changes in r94209,
[FreeBSD/FreeBSD.git] / sys / kern / vfs_extattr.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$
40  */
41
42 /* For 4.3 integer FS ID compatibility */
43 #include "opt_compat.h"
44 #include "opt_ffs.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
59 #include <sys/file.h>
60 #include <sys/linker.h>
61 #include <sys/stat.h>
62 #include <sys/sx.h>
63 #include <sys/unistd.h>
64 #include <sys/vnode.h>
65 #include <sys/proc.h>
66 #include <sys/dirent.h>
67 #include <sys/extattr.h>
68 #include <sys/jail.h>
69 #include <sys/sysctl.h>
70
71 #include <machine/limits.h>
72 #include <machine/stdarg.h>
73
74 #include <vm/vm.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/uma.h>
78
79 static int change_dir(struct nameidata *ndp, struct thread *td);
80 static void checkdirs(struct vnode *olddp, struct vnode *newdp);
81 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
82 static int getutimes(const struct timeval *, struct timespec *);
83 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
84 static int setfmode(struct thread *td, struct vnode *, int);
85 static int setfflags(struct thread *td, struct vnode *, int);
86 static int setutimes(struct thread *td, struct vnode *,
87     const struct timespec *, int);
88 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
89     struct thread *td);
90 static void vfs_freeopts(struct vfsoptlist *opt);
91 static int vfs_nmount(struct thread *td, int, struct uio *);
92
93 static int      usermount = 0;  /* if 1, non-root can mount fs. */
94
95 int (*union_dircheckp)(struct thread *td, struct vnode **, struct file *);
96
97 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
98
99 /*
100  * Virtual File System System Calls
101  */
102
103 #ifndef _SYS_SYSPROTO_H_
104 struct nmount_args {
105         struct iovec    *iovp;
106         unsigned int    iovcnt;
107         int             flags;
108 };
109 #endif
110 /* ARGSUSED */
111 int
112 nmount(td, uap)
113         struct thread *td;
114         struct nmount_args /* {
115                 syscallarg(struct iovec *) iovp;
116                 syscallarg(unsigned int) iovcnt;
117                 syscallarg(int) flags;
118         } */ *uap;
119 {
120         struct uio auio;
121         struct iovec *iov, *needfree;
122         struct iovec aiov[UIO_SMALLIOV];
123         long error, i;
124         u_int iovlen, iovcnt;
125
126         iovcnt = SCARG(uap, iovcnt);
127         iovlen = iovcnt * sizeof (struct iovec);
128         /*
129          * Check that we have an even number of iovec's
130          * and that we have at least two options.
131          */
132         if ((iovcnt & 1) || (iovcnt < 4) || (iovcnt > UIO_MAXIOV))
133                 return (EINVAL);
134
135         if (iovcnt > UIO_SMALLIOV) {
136                 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
137                 needfree = iov;
138         } else {
139                 iov = aiov;
140                 needfree = NULL;
141         }
142         auio.uio_iov = iov;
143         auio.uio_iovcnt = iovcnt;
144         auio.uio_rw = UIO_WRITE;
145         auio.uio_segflg = UIO_USERSPACE;
146         auio.uio_td = td;
147         auio.uio_offset = 0;
148         auio.uio_resid = 0;
149         if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
150                 goto finish;
151         for (i = 0; i < iovcnt; i++) {
152                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
153                         error = EINVAL;
154                         goto finish;
155                 }
156                 auio.uio_resid += iov->iov_len;
157                 iov++;
158         }
159         error = vfs_nmount(td, SCARG(uap, flags), &auio);
160 finish:
161         if (needfree != NULL)
162                 free(needfree, M_TEMP);
163         return (error);
164 }
165
166 /*
167  * Release all resources related to the
168  * mount options.
169  */
170 static void
171 vfs_freeopts(struct vfsoptlist *opt)
172 {
173         free(opt->opt, M_MOUNT);
174         free(opt->optbuf, M_MOUNT);
175         free(opt, M_MOUNT);
176 }
177
178 int
179 kernel_mount(iovp, iovcnt, flags)
180         struct iovec *iovp;
181         unsigned int iovcnt;
182         int flags;
183 {
184         struct uio auio;
185         struct iovec *iov;
186         int error, i;
187
188         /*
189          * Check that we have an even number of iovec's
190          * and that we have at least two options.
191          */
192         if ((iovcnt & 1) || (iovcnt < 4))
193                 return (EINVAL);
194
195         auio.uio_iov = iovp;
196         auio.uio_iovcnt = iovcnt;
197         auio.uio_rw = UIO_WRITE;
198         auio.uio_segflg = UIO_SYSSPACE;
199         auio.uio_offset = 0;
200         auio.uio_td = NULL;
201         auio.uio_resid = 0;
202         iov = iovp;
203         for (i = 0; i < iovcnt; i++) {
204                 if (iov->iov_len > INT_MAX - auio.uio_resid) {
205                         return (EINVAL);
206                 }
207                 auio.uio_resid += iov->iov_len;
208                 iov++;
209         }
210
211         error = vfs_nmount(curthread, flags, &auio);
212         return (error);
213 }
214
215 int
216 kernel_vmount(int flags, ...)
217 {
218         struct iovec *iovp;
219         struct uio auio;
220         va_list ap;
221         unsigned int iovcnt, iovlen, len;
222         const char *cp;
223         char *buf, *pos;
224         int error, i, n;
225
226         len = 0;
227         va_start(ap, flags);
228         for (iovcnt = 0; (cp = va_arg(ap, const char *)) != NULL; iovcnt++)
229                 len += strlen(cp) + 1;
230         va_end(ap);
231
232         if (iovcnt < 4 || iovcnt & 1)
233                 return (EINVAL);
234
235         iovlen = iovcnt * sizeof (struct iovec);
236         MALLOC(iovp, struct iovec *, iovlen, M_MOUNT, M_WAITOK);
237         MALLOC(buf, char *, len, M_MOUNT, M_WAITOK);
238         pos = buf;
239         va_start(ap, flags);
240         for (i = 0; i < iovcnt; i++) {
241                 cp = va_arg(ap, const char *);
242                 copystr(cp, pos, len - (pos - buf), &n);
243                 iovp[i].iov_base = pos;
244                 iovp[i].iov_len = n;
245                 pos += n;
246         }
247         va_end(ap);
248
249         auio.uio_iov = iovp;
250         auio.uio_iovcnt = iovcnt;
251         auio.uio_rw = UIO_WRITE;
252         auio.uio_segflg = UIO_SYSSPACE;
253         auio.uio_offset = 0;
254         auio.uio_td = NULL;
255         auio.uio_resid = len;
256
257         error = vfs_nmount(curthread, flags, &auio);
258         FREE(iovp, M_MOUNT);
259         FREE(buf, M_MOUNT);
260         return (error);
261 }
262
263 /*
264  * vfs_nmount(): actually attempt a filesystem mount.
265  */
266 static int
267 vfs_nmount(td, fsflags, fsoptions)
268         struct thread *td;
269         int fsflags;            /* Flags common to all filesystems. */
270         struct uio *fsoptions;  /* Options local to the filesystem. */
271 {
272         linker_file_t lf;
273         struct vnode *vp;
274         struct mount *mp;
275         struct vfsconf *vfsp;
276         struct iovec *cur;
277         struct vfsoptlist *optlist;
278         struct vfsopt *opt;
279         char *buf, *fstype, *fspath;
280         int error, flag = 0, kern_flag = 0, i, len, optcnt;
281         int offset, iovcnt, fstypelen, fspathlen;
282         struct vattr va;
283         struct nameidata nd;
284
285         /*
286          * Allocate memory to hold the vfsopt structures.
287          */
288         iovcnt = fsoptions->uio_iovcnt;
289         optcnt = iovcnt >> 1;
290         opt = malloc(sizeof (struct vfsopt) * optcnt,
291             M_MOUNT, M_WAITOK | M_ZERO);
292
293         /*
294          * Count the size of the buffer for options,
295          * allocate it, and fill in the vfsopt structures.
296          */
297         cur = fsoptions->uio_iov;
298         len = fsoptions->uio_resid;
299         buf = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
300
301         optlist = malloc(sizeof (struct vfsoptlist), M_MOUNT, M_WAITOK);
302         optlist->opt = opt;
303         optlist->optbuf = buf;
304         optlist->optcnt = optcnt;
305
306         offset = i = 0;
307         cur = fsoptions->uio_iov;
308         while (i < optcnt) {
309                 opt[i].name = buf + offset;
310                 /* Ensure the name of an option is a string. */
311                 if (opt[i].name[cur->iov_len - 1] != '\0') {
312                         error = EINVAL;
313                         goto bad;
314                 }
315                 offset += cur->iov_len;
316                 cur++;
317                 opt[i].len = cur->iov_len;
318                 /*
319                  * Prevent consumers from trying to
320                  * read the value of a 0 length option
321                  * by setting it to NULL.
322                  */
323                 if (opt[i].len == 0)
324                         opt[i].value = NULL;
325                 else
326                         opt[i].value = buf + offset;
327                 offset += cur->iov_len;
328                 cur++; i++;
329         }
330
331         if ((error = uiomove(buf, len, fsoptions)) != 0)
332                 goto bad;
333
334         /*
335          * We need these two options before the others,
336          * and they are mandatory for any filesystem.
337          * Ensure they are NULL terminated as well.
338          */
339         fstypelen = 0;
340         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
341         if (error || fstype[fstypelen - 1] != '\0') {
342                 error = EINVAL;
343                 goto bad;
344         }
345         fspathlen = 0;
346         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
347         if (error || fspath[fspathlen - 1] != '\0') {
348                 error = EINVAL;
349                 goto bad;
350         }
351
352         /*
353          * Be ultra-paranoid about making sure the type and fspath
354          * variables will fit in our mp buffers, including the
355          * terminating NUL.
356          */
357         if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
358                 error = ENAMETOOLONG;
359                 goto bad;
360         }
361
362         if (usermount == 0) {
363                 error = suser(td);
364                 if (error)
365                         goto bad;
366         }
367         /*
368          * Do not allow NFS export by non-root users.
369          */
370         if (fsflags & MNT_EXPORTED) {
371                 error = suser(td);
372                 if (error)
373                         goto bad;
374         }
375         /*
376          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users.
377          */
378         if (suser(td)) 
379                 fsflags |= MNT_NOSUID | MNT_NODEV;
380         /*
381          * Get vnode to be covered
382          */
383         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
384         if ((error = namei(&nd)) != 0)
385                 goto bad;
386         NDFREE(&nd, NDF_ONLY_PNBUF);
387         vp = nd.ni_vp;
388         if (fsflags & MNT_UPDATE) {
389                 if ((vp->v_flag & VROOT) == 0) {
390                         vput(vp);
391                         error = EINVAL;
392                         goto bad;
393                 }
394                 mp = vp->v_mount;
395                 flag = mp->mnt_flag;
396                 kern_flag = mp->mnt_kern_flag;
397                 /*
398                  * We only allow the filesystem to be reloaded if it
399                  * is currently mounted read-only.
400                  */
401                 if ((fsflags & MNT_RELOAD) &&
402                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
403                         vput(vp);
404                         error = EOPNOTSUPP;     /* Needs translation */
405                         goto bad;
406                 }
407                 /*
408                  * Only root, or the user that did the original mount is
409                  * permitted to update it.
410                  */
411                 if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) {
412                         error = suser(td);
413                         if (error) {
414                                 vput(vp);
415                                 goto bad;
416                         }
417                 }
418                 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
419                         vput(vp);
420                         error = EBUSY;
421                         goto bad;
422                 }
423                 mtx_lock(&vp->v_interlock);
424                 if ((vp->v_flag & VMOUNT) != 0 || vp->v_mountedhere != NULL) {
425                         mtx_unlock(&vp->v_interlock);
426                         vfs_unbusy(mp, td);
427                         vput(vp);
428                         error = EBUSY;
429                         goto bad;
430                 }
431                 vp->v_flag |= VMOUNT;
432                 mtx_unlock(&vp->v_interlock);
433                 mp->mnt_flag |= fsflags &
434                     (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT);
435                 VOP_UNLOCK(vp, 0, td);
436                 mp->mnt_optnew = optlist;
437                 goto update;
438         }
439         /*
440          * If the user is not root, ensure that they own the directory
441          * onto which we are attempting to mount.
442          */
443         error = VOP_GETATTR(vp, &va, td->td_ucred, td);
444         if (error) {
445                 vput(vp);
446                 goto bad;
447         }
448         if (va.va_uid != td->td_ucred->cr_uid) {
449                 error = suser(td);
450                 if (error) {
451                         vput(vp);
452                         goto bad;
453                 }
454         }
455         if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) {
456                 vput(vp);
457                 goto bad;
458         }
459         if (vp->v_type != VDIR) {
460                 vput(vp);
461                 error = ENOTDIR;
462                 goto bad;
463         }
464         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
465                 if (!strcmp(vfsp->vfc_name, fstype))
466                         break;
467         if (vfsp == NULL) {
468                 /* Only load modules for root (very important!). */
469                 error = suser(td);
470                 if (error) {
471                         vput(vp);
472                         goto bad;
473                 }
474                 error = securelevel_gt(td->td_ucred, 0);
475                 if (error) {
476                         vput(vp);
477                         goto bad;
478                 }
479                 error = linker_load_file(fstype, &lf);
480                 if (error || lf == NULL) {
481                         vput(vp);
482                         if (lf == NULL)
483                                 error = ENODEV;
484                         goto bad;
485                 }
486                 lf->userrefs++;
487                 /* Look up again to see if the VFS was loaded. */
488                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
489                         if (!strcmp(vfsp->vfc_name, fstype))
490                                 break;
491                 if (vfsp == NULL) {
492                         lf->userrefs--;
493                         linker_file_unload(lf);
494                         vput(vp);
495                         error = ENODEV;
496                         goto bad;
497                 }
498         }
499         mtx_lock(&vp->v_interlock);
500         if ((vp->v_flag & VMOUNT) != 0 ||
501             vp->v_mountedhere != NULL) {
502                 mtx_unlock(&vp->v_interlock);
503                 vput(vp);
504                 error = EBUSY;
505                 goto bad;
506         }
507         vp->v_flag |= VMOUNT;
508         mtx_unlock(&vp->v_interlock);
509
510         /*
511          * Allocate and initialize the filesystem.
512          */
513         mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
514         TAILQ_INIT(&mp->mnt_nvnodelist);
515         TAILQ_INIT(&mp->mnt_reservedvnlist);
516         lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
517         (void)vfs_busy(mp, LK_NOWAIT, 0, td);
518         mp->mnt_op = vfsp->vfc_vfsops;
519         mp->mnt_vfc = vfsp;
520         vfsp->vfc_refcount++;
521         mp->mnt_stat.f_type = vfsp->vfc_typenum;
522         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
523         strncpy(mp->mnt_stat.f_fstypename, fstype, MFSNAMELEN);
524         mp->mnt_vnodecovered = vp;
525         mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
526         strncpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
527         mp->mnt_iosize_max = DFLTPHYS;
528         VOP_UNLOCK(vp, 0, td);
529
530         mp->mnt_opt = optlist;
531 update:
532         /*
533          * Check if the fs implements the new VFS_NMOUNT()
534          * function, since the new system call was used.
535          */
536         if (mp->mnt_op->vfs_mount != NULL) {
537                 printf("%s doesn't support the new mount syscall\n",
538                     mp->mnt_vfc->vfc_name);
539                 mtx_lock(&vp->v_interlock);
540                 vp->v_flag &= ~VMOUNT;
541                 mtx_unlock(&vp->v_interlock);
542                 if (mp->mnt_flag & MNT_UPDATE)
543                         vfs_unbusy(mp, td);
544                 else {
545                         mp->mnt_vfc->vfc_refcount--;
546                         vfs_unbusy(mp, td);
547                         free((caddr_t)mp, M_MOUNT);
548                 }
549                 vput(vp);
550                 error = EOPNOTSUPP;
551                 goto bad;
552         }
553
554         /*
555          * Set the mount level flags.
556          */
557         if (fsflags & MNT_RDONLY)
558                 mp->mnt_flag |= MNT_RDONLY;
559         else if (mp->mnt_flag & MNT_RDONLY)
560                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
561         mp->mnt_flag &=~ MNT_UPDATEMASK;
562         mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE);
563         /*
564          * Mount the filesystem.
565          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
566          * get.  No freeing of cn_pnbuf.
567          */
568         error = VFS_NMOUNT(mp, &nd, td);
569         if (mp->mnt_flag & MNT_UPDATE) {
570                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
571                         mp->mnt_flag &= ~MNT_RDONLY;
572                 mp->mnt_flag &=~
573                     (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
574                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
575                 if (error) {
576                         mp->mnt_flag = flag;
577                         mp->mnt_kern_flag = kern_flag;
578                         vfs_freeopts(mp->mnt_optnew);
579                 } else {
580                         vfs_freeopts(mp->mnt_opt);
581                         mp->mnt_opt = mp->mnt_optnew;
582                 }
583                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
584                         if (mp->mnt_syncer == NULL)
585                                 error = vfs_allocate_syncvnode(mp);
586                 } else {
587                         if (mp->mnt_syncer != NULL)
588                                 vrele(mp->mnt_syncer);
589                         mp->mnt_syncer = NULL;
590                 }
591                 vfs_unbusy(mp, td);
592                 mtx_lock(&vp->v_interlock);
593                 vp->v_flag &= ~VMOUNT;
594                 mtx_unlock(&vp->v_interlock);
595                 vrele(vp);
596                 return (error);
597         }
598         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
599         /*
600          * Put the new filesystem on the mount list after root.
601          */
602         cache_purge(vp);
603         if (!error) {
604                 struct vnode *newdp;
605
606                 mtx_lock(&vp->v_interlock);
607                 vp->v_flag &= ~VMOUNT;
608                 vp->v_mountedhere = mp;
609                 mtx_unlock(&vp->v_interlock);
610                 mtx_lock(&mountlist_mtx);
611                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
612                 mtx_unlock(&mountlist_mtx);
613                 if (VFS_ROOT(mp, &newdp))
614                         panic("mount: lost mount");
615                 checkdirs(vp, newdp);
616                 vput(newdp);
617                 VOP_UNLOCK(vp, 0, td);
618                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
619                         error = vfs_allocate_syncvnode(mp);
620                 vfs_unbusy(mp, td);
621                 if ((error = VFS_START(mp, 0, td)) != 0) {
622                         vrele(vp);
623                         goto bad;
624                 }
625         } else {
626                 mtx_lock(&vp->v_interlock);
627                 vp->v_flag &= ~VMOUNT;
628                 mtx_unlock(&vp->v_interlock);
629                 mp->mnt_vfc->vfc_refcount--;
630                 vfs_unbusy(mp, td);
631                 free((caddr_t)mp, M_MOUNT);
632                 vput(vp);
633                 goto bad;
634         }
635         return (0);
636 bad:
637         vfs_freeopts(optlist);
638         return (error);
639 }
640
641 /*
642  * Old Mount API.
643  */
644 #ifndef _SYS_SYSPROTO_H_
645 struct mount_args {
646         char    *type;
647         char    *path;
648         int     flags;
649         caddr_t data;
650 };
651 #endif
652 /* ARGSUSED */
653 int
654 mount(td, uap)
655         struct thread *td;
656         struct mount_args /* {
657                 syscallarg(char *) type;
658                 syscallarg(char *) path;
659                 syscallarg(int) flags;
660                 syscallarg(caddr_t) data;
661         } */ *uap;
662 {
663         char *fstype;
664         char *fspath;
665         int error;
666
667         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
668         fspath = malloc(MNAMELEN, M_TEMP, M_WAITOK);
669
670         /*
671          * vfs_mount() actually takes a kernel string for `type' and
672          * `path' now, so extract them.
673          */
674         error = copyinstr(SCARG(uap, type), fstype, MFSNAMELEN, NULL);
675         if (error)
676                 goto finish;
677         error = copyinstr(SCARG(uap, path), fspath, MNAMELEN, NULL);
678         if (error)
679                 goto finish;
680         error = vfs_mount(td, fstype, fspath, SCARG(uap, flags),
681             SCARG(uap, data));
682 finish:
683         free(fstype, M_TEMP);
684         free(fspath, M_TEMP);
685         return (error);
686 }
687
688 /*
689  * vfs_mount(): actually attempt a filesystem mount.
690  *
691  * This routine is designed to be a "generic" entry point for routines
692  * that wish to mount a filesystem. All parameters except `fsdata' are
693  * pointers into kernel space. `fsdata' is currently still a pointer
694  * into userspace.
695  */
696 int
697 vfs_mount(td, fstype, fspath, fsflags, fsdata)
698         struct thread *td;
699         const char *fstype;
700         char *fspath;
701         int fsflags;
702         void *fsdata;
703 {
704         linker_file_t lf;
705         struct vnode *vp;
706         struct mount *mp;
707         struct vfsconf *vfsp;
708         int error, flag = 0, kern_flag = 0;
709         struct vattr va;
710         struct nameidata nd;
711
712         /*
713          * Be ultra-paranoid about making sure the type and fspath
714          * variables will fit in our mp buffers, including the
715          * terminating NUL.
716          */
717         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
718                 return (ENAMETOOLONG);
719
720         if (usermount == 0) {
721                 error = suser(td);
722                 if (error)
723                         return (error);
724         }
725         /*
726          * Do not allow NFS export by non-root users.
727          */
728         if (fsflags & MNT_EXPORTED) {
729                 error = suser(td);
730                 if (error)
731                         return (error);
732         }
733         /*
734          * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users.
735          */
736         if (suser(td)) 
737                 fsflags |= MNT_NOSUID | MNT_NODEV;
738         /*
739          * Get vnode to be covered
740          */
741         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
742         if ((error = namei(&nd)) != 0)
743                 return (error);
744         NDFREE(&nd, NDF_ONLY_PNBUF);
745         vp = nd.ni_vp;
746         if (fsflags & MNT_UPDATE) {
747                 if ((vp->v_flag & VROOT) == 0) {
748                         vput(vp);
749                         return (EINVAL);
750                 }
751                 mp = vp->v_mount;
752                 flag = mp->mnt_flag;
753                 kern_flag = mp->mnt_kern_flag;
754                 /*
755                  * We only allow the filesystem to be reloaded if it
756                  * is currently mounted read-only.
757                  */
758                 if ((fsflags & MNT_RELOAD) &&
759                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
760                         vput(vp);
761                         return (EOPNOTSUPP);    /* Needs translation */
762                 }
763                 /*
764                  * Only root, or the user that did the original mount is
765                  * permitted to update it.
766                  */
767                 if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) {
768                         error = suser(td);
769                         if (error) {
770                                 vput(vp);
771                                 return (error);
772                         }
773                 }
774                 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
775                         vput(vp);
776                         return (EBUSY);
777                 }
778                 mtx_lock(&vp->v_interlock);
779                 if ((vp->v_flag & VMOUNT) != 0 || vp->v_mountedhere != NULL) {
780                         mtx_unlock(&vp->v_interlock);
781                         vfs_unbusy(mp, td);
782                         vput(vp);
783                         return (EBUSY);
784                 }
785                 vp->v_flag |= VMOUNT;
786                 mtx_unlock(&vp->v_interlock);
787                 mp->mnt_flag |= fsflags &
788                     (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT);
789                 VOP_UNLOCK(vp, 0, td);
790                 goto update;
791         }
792         /*
793          * If the user is not root, ensure that they own the directory
794          * onto which we are attempting to mount.
795          */
796         error = VOP_GETATTR(vp, &va, td->td_ucred, td);
797         if (error) {
798                 vput(vp);
799                 return (error);
800         }
801         if (va.va_uid != td->td_ucred->cr_uid) {
802                 error = suser(td);
803                 if (error) {
804                         vput(vp);
805                         return (error);
806                 }
807         }
808         if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) {
809                 vput(vp);
810                 return (error);
811         }
812         if (vp->v_type != VDIR) {
813                 vput(vp);
814                 return (ENOTDIR);
815         }
816         for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
817                 if (!strcmp(vfsp->vfc_name, fstype))
818                         break;
819         if (vfsp == NULL) {
820                 /* Only load modules for root (very important!). */
821                 error = suser(td);
822                 if (error) {
823                         vput(vp);
824                         return (error);
825                 }
826                 error = securelevel_gt(td->td_ucred, 0);
827                 if (error) {
828                         vput(vp);
829                         return (error);
830                 }
831                 error = linker_load_file(fstype, &lf);
832                 if (error || lf == NULL) {
833                         vput(vp);
834                         if (lf == NULL)
835                                 error = ENODEV;
836                         return (error);
837                 }
838                 lf->userrefs++;
839                 /* Look up again to see if the VFS was loaded. */
840                 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
841                         if (!strcmp(vfsp->vfc_name, fstype))
842                                 break;
843                 if (vfsp == NULL) {
844                         lf->userrefs--;
845                         linker_file_unload(lf);
846                         vput(vp);
847                         return (ENODEV);
848                 }
849         }
850         mtx_lock(&vp->v_interlock);
851         if ((vp->v_flag & VMOUNT) != 0 ||
852             vp->v_mountedhere != NULL) {
853                 mtx_unlock(&vp->v_interlock);
854                 vput(vp);
855                 return (EBUSY);
856         }
857         vp->v_flag |= VMOUNT;
858         mtx_unlock(&vp->v_interlock);
859
860         /*
861          * Allocate and initialize the filesystem.
862          */
863         mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
864         TAILQ_INIT(&mp->mnt_nvnodelist);
865         TAILQ_INIT(&mp->mnt_reservedvnlist);
866         lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
867         (void)vfs_busy(mp, LK_NOWAIT, 0, td);
868         mp->mnt_op = vfsp->vfc_vfsops;
869         mp->mnt_vfc = vfsp;
870         vfsp->vfc_refcount++;
871         mp->mnt_stat.f_type = vfsp->vfc_typenum;
872         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
873         strncpy(mp->mnt_stat.f_fstypename, fstype, MFSNAMELEN);
874         mp->mnt_vnodecovered = vp;
875         mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
876         strncpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
877         mp->mnt_iosize_max = DFLTPHYS;
878         VOP_UNLOCK(vp, 0, td);
879 update:
880         /*
881          * Check if the fs implements the old VFS_MOUNT()
882          * function, since the old system call was used.
883          */
884         if (mp->mnt_op->vfs_mount == NULL) {
885                 printf("%s doesn't support the old mount syscall\n",
886                     mp->mnt_vfc->vfc_name);
887                 mtx_lock(&vp->v_interlock);
888                 vp->v_flag &= ~VMOUNT;
889                 mtx_unlock(&vp->v_interlock);
890                 if (mp->mnt_flag & MNT_UPDATE)
891                         vfs_unbusy(mp, td);
892                 else {
893                         mp->mnt_vfc->vfc_refcount--;
894                         vfs_unbusy(mp, td);
895                         free((caddr_t)mp, M_MOUNT);
896                 }
897                 vput(vp);
898                 return (EOPNOTSUPP);
899         }
900
901         /*
902          * Set the mount level flags.
903          */
904         if (fsflags & MNT_RDONLY)
905                 mp->mnt_flag |= MNT_RDONLY;
906         else if (mp->mnt_flag & MNT_RDONLY)
907                 mp->mnt_kern_flag |= MNTK_WANTRDWR;
908         mp->mnt_flag &=~ MNT_UPDATEMASK;
909         mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE);
910         /*
911          * Mount the filesystem.
912          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
913          * get.  No freeing of cn_pnbuf.
914          */
915         error = VFS_MOUNT(mp, fspath, fsdata, &nd, td);
916         if (mp->mnt_flag & MNT_UPDATE) {
917                 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
918                         mp->mnt_flag &= ~MNT_RDONLY;
919                 mp->mnt_flag &=~
920                     (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
921                 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
922                 if (error) {
923                         mp->mnt_flag = flag;
924                         mp->mnt_kern_flag = kern_flag;
925                 }
926                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
927                         if (mp->mnt_syncer == NULL)
928                                 error = vfs_allocate_syncvnode(mp);
929                 } else {
930                         if (mp->mnt_syncer != NULL)
931                                 vrele(mp->mnt_syncer);
932                         mp->mnt_syncer = NULL;
933                 }
934                 vfs_unbusy(mp, td);
935                 mtx_lock(&vp->v_interlock);
936                 vp->v_flag &= ~VMOUNT;
937                 mtx_unlock(&vp->v_interlock);
938                 vrele(vp);
939                 return (error);
940         }
941         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
942         /*
943          * Put the new filesystem on the mount list after root.
944          */
945         cache_purge(vp);
946         if (!error) {
947                 struct vnode *newdp;
948
949                 mtx_lock(&vp->v_interlock);
950                 vp->v_flag &= ~VMOUNT;
951                 vp->v_mountedhere = mp;
952                 mtx_unlock(&vp->v_interlock);
953                 mtx_lock(&mountlist_mtx);
954                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
955                 mtx_unlock(&mountlist_mtx);
956                 if (VFS_ROOT(mp, &newdp))
957                         panic("mount: lost mount");
958                 checkdirs(vp, newdp);
959                 vput(newdp);
960                 VOP_UNLOCK(vp, 0, td);
961                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
962                         error = vfs_allocate_syncvnode(mp);
963                 vfs_unbusy(mp, td);
964                 if ((error = VFS_START(mp, 0, td)) != 0)
965                         vrele(vp);
966         } else {
967                 mtx_lock(&vp->v_interlock);
968                 vp->v_flag &= ~VMOUNT;
969                 mtx_unlock(&vp->v_interlock);
970                 mp->mnt_vfc->vfc_refcount--;
971                 vfs_unbusy(mp, td);
972                 free((caddr_t)mp, M_MOUNT);
973                 vput(vp);
974         }
975         return (error);
976 }
977
978 /*
979  * Scan all active processes to see if any of them have a current
980  * or root directory of `olddp'. If so, replace them with the new
981  * mount point.
982  */
983 static void
984 checkdirs(olddp, newdp)
985         struct vnode *olddp, *newdp;
986 {
987         struct filedesc *fdp;
988         struct proc *p;
989         int nrele;
990
991         if (olddp->v_usecount == 1)
992                 return;
993         sx_slock(&allproc_lock);
994         LIST_FOREACH(p, &allproc, p_list) {
995                 PROC_LOCK(p);
996                 fdp = p->p_fd;
997                 if (fdp == NULL) {
998                         PROC_UNLOCK(p);
999                         continue;
1000                 }
1001                 nrele = 0;
1002                 FILEDESC_LOCK(fdp);
1003                 if (fdp->fd_cdir == olddp) {
1004                         VREF(newdp);
1005                         fdp->fd_cdir = newdp;
1006                         nrele++;
1007                 }
1008                 if (fdp->fd_rdir == olddp) {
1009                         VREF(newdp);
1010                         fdp->fd_rdir = newdp;
1011                         nrele++;
1012                 }
1013                 FILEDESC_UNLOCK(fdp);
1014                 PROC_UNLOCK(p);
1015                 while (nrele--)
1016                         vrele(olddp);
1017         }
1018         sx_sunlock(&allproc_lock);
1019         if (rootvnode == olddp) {
1020                 vrele(rootvnode);
1021                 VREF(newdp);
1022                 rootvnode = newdp;
1023         }
1024 }
1025
1026 /*
1027  * Unmount a file system.
1028  *
1029  * Note: unmount takes a path to the vnode mounted on as argument,
1030  * not special file (as before).
1031  */
1032 #ifndef _SYS_SYSPROTO_H_
1033 struct unmount_args {
1034         char    *path;
1035         int     flags;
1036 };
1037 #endif
1038 /* ARGSUSED */
1039 int
1040 unmount(td, uap)
1041         struct thread *td;
1042         register struct unmount_args /* {
1043                 syscallarg(char *) path;
1044                 syscallarg(int) flags;
1045         } */ *uap;
1046 {
1047         register struct vnode *vp;
1048         struct mount *mp;
1049         int error;
1050         struct nameidata nd;
1051
1052         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1053             SCARG(uap, path), td);
1054         if ((error = namei(&nd)) != 0)
1055                 return (error);
1056         vp = nd.ni_vp;
1057         NDFREE(&nd, NDF_ONLY_PNBUF);
1058         mp = vp->v_mount;
1059
1060         /*
1061          * Only root, or the user that did the original mount is
1062          * permitted to unmount this filesystem.
1063          */
1064         if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) {
1065                 error = suser(td);
1066                 if (error) {
1067                         vput(vp);
1068                         return (error);
1069                 }
1070         }
1071
1072         /*
1073          * Don't allow unmounting the root file system.
1074          */
1075         if (mp->mnt_flag & MNT_ROOTFS) {
1076                 vput(vp);
1077                 return (EINVAL);
1078         }
1079
1080         /*
1081          * Must be the root of the filesystem
1082          */
1083         if ((vp->v_flag & VROOT) == 0) {
1084                 vput(vp);
1085                 return (EINVAL);
1086         }
1087         vput(vp);
1088         return (dounmount(mp, SCARG(uap, flags), td));
1089 }
1090
1091 /*
1092  * Do the actual file system unmount.
1093  */
1094 int
1095 dounmount(mp, flags, td)
1096         struct mount *mp;
1097         int flags;
1098         struct thread *td;
1099 {
1100         struct vnode *coveredvp, *fsrootvp;
1101         int error;
1102         int async_flag;
1103
1104         mtx_lock(&mountlist_mtx);
1105         mp->mnt_kern_flag |= MNTK_UNMOUNT;
1106         error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1107             ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_mtx, td);
1108         if (error) {
1109                 mp->mnt_kern_flag &= ~MNTK_UNMOUNT;
1110                 if (mp->mnt_kern_flag & MNTK_MWAIT)
1111                         wakeup((caddr_t)mp);
1112                 return (error);
1113         }
1114         vn_start_write(NULL, &mp, V_WAIT);
1115
1116         if (mp->mnt_flag & MNT_EXPUBLIC)
1117                 vfs_setpublicfs(NULL, NULL, NULL);
1118
1119         vfs_msync(mp, MNT_WAIT);
1120         async_flag = mp->mnt_flag & MNT_ASYNC;
1121         mp->mnt_flag &=~ MNT_ASYNC;
1122         cache_purgevfs(mp);     /* remove cache entries for this file sys */
1123         if (mp->mnt_syncer != NULL)
1124                 vrele(mp->mnt_syncer);
1125         /* Move process cdir/rdir refs on fs root to underlying vnode. */
1126         if (VFS_ROOT(mp, &fsrootvp) == 0) {
1127                 if (mp->mnt_vnodecovered != NULL)
1128                         checkdirs(fsrootvp, mp->mnt_vnodecovered);
1129                 if (fsrootvp == rootvnode) {
1130                         vrele(rootvnode);
1131                         rootvnode = NULL;
1132                 }
1133                 vput(fsrootvp);
1134         }
1135         if (((mp->mnt_flag & MNT_RDONLY) ||
1136              (error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) == 0) ||
1137             (flags & MNT_FORCE)) {
1138                 error = VFS_UNMOUNT(mp, flags, td);
1139         }
1140         vn_finished_write(mp);
1141         if (error) {
1142                 /* Undo cdir/rdir and rootvnode changes made above. */
1143                 if (VFS_ROOT(mp, &fsrootvp) == 0) {
1144                         if (mp->mnt_vnodecovered != NULL)
1145                                 checkdirs(mp->mnt_vnodecovered, fsrootvp);
1146                         if (rootvnode == NULL) {
1147                                 rootvnode = fsrootvp;
1148                                 vref(rootvnode);
1149                         }
1150                         vput(fsrootvp);
1151                 }
1152                 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1153                         (void) vfs_allocate_syncvnode(mp);
1154                 mtx_lock(&mountlist_mtx);
1155                 mp->mnt_kern_flag &= ~MNTK_UNMOUNT;
1156                 mp->mnt_flag |= async_flag;
1157                 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK,
1158                     &mountlist_mtx, td);
1159                 if (mp->mnt_kern_flag & MNTK_MWAIT)
1160                         wakeup((caddr_t)mp);
1161                 return (error);
1162         }
1163         mtx_lock(&mountlist_mtx);
1164         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1165         if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1166                 coveredvp->v_mountedhere = NULL;
1167         mp->mnt_vfc->vfc_refcount--;
1168         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
1169                 panic("unmount: dangling vnode");
1170         lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_mtx, td);
1171         lockdestroy(&mp->mnt_lock);
1172         if (coveredvp != NULL)
1173                 vrele(coveredvp);
1174         if (mp->mnt_kern_flag & MNTK_MWAIT)
1175                 wakeup((caddr_t)mp);
1176         if (mp->mnt_op->vfs_mount == NULL)
1177                 vfs_freeopts(mp->mnt_opt);
1178         free((caddr_t)mp, M_MOUNT);
1179         return (0);
1180 }
1181
1182 /*
1183  * Sync each mounted filesystem.
1184  */
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct sync_args {
1187         int     dummy;
1188 };
1189 #endif
1190
1191 #ifdef DEBUG
1192 static int syncprt = 0;
1193 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1194 #endif
1195
1196 /* ARGSUSED */
1197 int
1198 sync(td, uap)
1199         struct thread *td;
1200         struct sync_args *uap;
1201 {
1202         struct mount *mp, *nmp;
1203         int asyncflag;
1204
1205         mtx_lock(&mountlist_mtx);
1206         for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1207                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
1208                         nmp = TAILQ_NEXT(mp, mnt_list);
1209                         continue;
1210                 }
1211                 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1212                     vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
1213                         asyncflag = mp->mnt_flag & MNT_ASYNC;
1214                         mp->mnt_flag &= ~MNT_ASYNC;
1215                         vfs_msync(mp, MNT_NOWAIT);
1216                         VFS_SYNC(mp, MNT_NOWAIT,
1217                             ((td != NULL) ? td->td_ucred : NOCRED), td);
1218                         mp->mnt_flag |= asyncflag;
1219                         vn_finished_write(mp);
1220                 }
1221                 mtx_lock(&mountlist_mtx);
1222                 nmp = TAILQ_NEXT(mp, mnt_list);
1223                 vfs_unbusy(mp, td);
1224         }
1225         mtx_unlock(&mountlist_mtx);
1226 #if 0
1227 /*
1228  * XXX don't call vfs_bufstats() yet because that routine
1229  * was not imported in the Lite2 merge.
1230  */
1231 #ifdef DIAGNOSTIC
1232         if (syncprt)
1233                 vfs_bufstats();
1234 #endif /* DIAGNOSTIC */
1235 #endif
1236         return (0);
1237 }
1238
1239 /* XXX PRISON: could be per prison flag */
1240 static int prison_quotas;
1241 #if 0
1242 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1243 #endif
1244
1245 /*
1246  * Change filesystem quotas.
1247  */
1248 #ifndef _SYS_SYSPROTO_H_
1249 struct quotactl_args {
1250         char *path;
1251         int cmd;
1252         int uid;
1253         caddr_t arg;
1254 };
1255 #endif
1256 /* ARGSUSED */
1257 int
1258 quotactl(td, uap)
1259         struct thread *td;
1260         register struct quotactl_args /* {
1261                 syscallarg(char *) path;
1262                 syscallarg(int) cmd;
1263                 syscallarg(int) uid;
1264                 syscallarg(caddr_t) arg;
1265         } */ *uap;
1266 {
1267         struct mount *mp;
1268         int error;
1269         struct nameidata nd;
1270
1271         if (jailed(td->td_ucred) && !prison_quotas)
1272                 return (EPERM);
1273         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1274         if ((error = namei(&nd)) != 0)
1275                 return (error);
1276         NDFREE(&nd, NDF_ONLY_PNBUF);
1277         error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
1278         vrele(nd.ni_vp);
1279         if (error)
1280                 return (error);
1281         error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
1282             SCARG(uap, arg), td);
1283         vn_finished_write(mp);
1284         return (error);
1285 }
1286
1287 /*
1288  * Get filesystem statistics.
1289  */
1290 #ifndef _SYS_SYSPROTO_H_
1291 struct statfs_args {
1292         char *path;
1293         struct statfs *buf;
1294 };
1295 #endif
1296 /* ARGSUSED */
1297 int
1298 statfs(td, uap)
1299         struct thread *td;
1300         register struct statfs_args /* {
1301                 syscallarg(char *) path;
1302                 syscallarg(struct statfs *) buf;
1303         } */ *uap;
1304 {
1305         register struct mount *mp;
1306         register struct statfs *sp;
1307         int error;
1308         struct nameidata nd;
1309         struct statfs sb;
1310
1311         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1312         if ((error = namei(&nd)) != 0)
1313                 return (error);
1314         mp = nd.ni_vp->v_mount;
1315         sp = &mp->mnt_stat;
1316         NDFREE(&nd, NDF_ONLY_PNBUF);
1317         vrele(nd.ni_vp);
1318         error = VFS_STATFS(mp, sp, td);
1319         if (error)
1320                 return (error);
1321         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1322         if (suser(td)) {
1323                 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
1324                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
1325                 sp = &sb;
1326         }
1327         return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
1328 }
1329
1330 /*
1331  * Get filesystem statistics.
1332  */
1333 #ifndef _SYS_SYSPROTO_H_
1334 struct fstatfs_args {
1335         int fd;
1336         struct statfs *buf;
1337 };
1338 #endif
1339 /* ARGSUSED */
1340 int
1341 fstatfs(td, uap)
1342         struct thread *td;
1343         register struct fstatfs_args /* {
1344                 syscallarg(int) fd;
1345                 syscallarg(struct statfs *) buf;
1346         } */ *uap;
1347 {
1348         struct file *fp;
1349         struct mount *mp;
1350         register struct statfs *sp;
1351         int error;
1352         struct statfs sb;
1353
1354         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
1355                 return (error);
1356         mp = ((struct vnode *)fp->f_data)->v_mount;
1357         fdrop(fp, td);
1358         if (mp == NULL)
1359                 return (EBADF);
1360         sp = &mp->mnt_stat;
1361         error = VFS_STATFS(mp, sp, td);
1362         if (error)
1363                 return (error);
1364         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1365         if (suser(td)) {
1366                 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
1367                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
1368                 sp = &sb;
1369         }
1370         return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
1371 }
1372
1373 /*
1374  * Get statistics on all filesystems.
1375  */
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct getfsstat_args {
1378         struct statfs *buf;
1379         long bufsize;
1380         int flags;
1381 };
1382 #endif
1383 int
1384 getfsstat(td, uap)
1385         struct thread *td;
1386         register struct getfsstat_args /* {
1387                 syscallarg(struct statfs *) buf;
1388                 syscallarg(long) bufsize;
1389                 syscallarg(int) flags;
1390         } */ *uap;
1391 {
1392         register struct mount *mp, *nmp;
1393         register struct statfs *sp;
1394         caddr_t sfsp;
1395         long count, maxcount, error;
1396
1397         maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
1398         sfsp = (caddr_t)SCARG(uap, buf);
1399         count = 0;
1400         mtx_lock(&mountlist_mtx);
1401         for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1402                 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
1403                         nmp = TAILQ_NEXT(mp, mnt_list);
1404                         continue;
1405                 }
1406                 if (sfsp && count < maxcount) {
1407                         sp = &mp->mnt_stat;
1408                         /*
1409                          * If MNT_NOWAIT or MNT_LAZY is specified, do not
1410                          * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1411                          * overrides MNT_WAIT.
1412                          */
1413                         if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1414                             (SCARG(uap, flags) & MNT_WAIT)) &&
1415                             (error = VFS_STATFS(mp, sp, td))) {
1416                                 mtx_lock(&mountlist_mtx);
1417                                 nmp = TAILQ_NEXT(mp, mnt_list);
1418                                 vfs_unbusy(mp, td);
1419                                 continue;
1420                         }
1421                         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1422                         error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
1423                         if (error) {
1424                                 vfs_unbusy(mp, td);
1425                                 return (error);
1426                         }
1427                         sfsp += sizeof(*sp);
1428                 }
1429                 count++;
1430                 mtx_lock(&mountlist_mtx);
1431                 nmp = TAILQ_NEXT(mp, mnt_list);
1432                 vfs_unbusy(mp, td);
1433         }
1434         mtx_unlock(&mountlist_mtx);
1435         if (sfsp && count > maxcount)
1436                 td->td_retval[0] = maxcount;
1437         else
1438                 td->td_retval[0] = count;
1439         return (0);
1440 }
1441
1442 /*
1443  * Change current working directory to a given file descriptor.
1444  */
1445 #ifndef _SYS_SYSPROTO_H_
1446 struct fchdir_args {
1447         int     fd;
1448 };
1449 #endif
1450 /* ARGSUSED */
1451 int
1452 fchdir(td, uap)
1453         struct thread *td;
1454         struct fchdir_args /* {
1455                 syscallarg(int) fd;
1456         } */ *uap;
1457 {
1458         register struct filedesc *fdp = td->td_proc->p_fd;
1459         struct vnode *vp, *tdp, *vpold;
1460         struct mount *mp;
1461         struct file *fp;
1462         int error;
1463
1464         if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1465                 return (error);
1466         vp = (struct vnode *)fp->f_data;
1467         VREF(vp);
1468         fdrop(fp, td);
1469         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1470         if (vp->v_type != VDIR)
1471                 error = ENOTDIR;
1472         else
1473                 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1474         while (!error && (mp = vp->v_mountedhere) != NULL) {
1475                 if (vfs_busy(mp, 0, 0, td))
1476                         continue;
1477                 error = VFS_ROOT(mp, &tdp);
1478                 vfs_unbusy(mp, td);
1479                 if (error)
1480                         break;
1481                 vput(vp);
1482                 vp = tdp;
1483         }
1484         if (error) {
1485                 vput(vp);
1486                 return (error);
1487         }
1488         VOP_UNLOCK(vp, 0, td);
1489         FILEDESC_LOCK(fdp);
1490         vpold = fdp->fd_cdir;
1491         fdp->fd_cdir = vp;
1492         FILEDESC_UNLOCK(fdp);
1493         vrele(vpold);
1494         return (0);
1495 }
1496
1497 /*
1498  * Change current working directory (``.'').
1499  */
1500 #ifndef _SYS_SYSPROTO_H_
1501 struct chdir_args {
1502         char    *path;
1503 };
1504 #endif
1505 /* ARGSUSED */
1506 int
1507 chdir(td, uap)
1508         struct thread *td;
1509         struct chdir_args /* {
1510                 syscallarg(char *) path;
1511         } */ *uap;
1512 {
1513         register struct filedesc *fdp = td->td_proc->p_fd;
1514         int error;
1515         struct nameidata nd;
1516         struct vnode *vp;
1517
1518         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1519             SCARG(uap, path), td);
1520         if ((error = change_dir(&nd, td)) != 0)
1521                 return (error);
1522         NDFREE(&nd, NDF_ONLY_PNBUF);
1523         FILEDESC_LOCK(fdp);
1524         vp = fdp->fd_cdir;
1525         fdp->fd_cdir = nd.ni_vp;
1526         FILEDESC_UNLOCK(fdp);
1527         vrele(vp);
1528         return (0);
1529 }
1530
1531 /*
1532  * Helper function for raised chroot(2) security function:  Refuse if
1533  * any filedescriptors are open directories.
1534  */
1535 static int
1536 chroot_refuse_vdir_fds(fdp)
1537         struct filedesc *fdp;
1538 {
1539         struct vnode *vp;
1540         struct file *fp;
1541         int fd;
1542
1543         FILEDESC_LOCK(fdp);
1544         for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1545                 fp = fget_locked(fdp, fd);
1546                 if (fp == NULL)
1547                         continue;
1548                 if (fp->f_type == DTYPE_VNODE) {
1549                         vp = (struct vnode *)fp->f_data;
1550                         if (vp->v_type == VDIR) {
1551                                 FILEDESC_UNLOCK(fdp);
1552                                 return (EPERM);
1553                         }
1554                 }
1555         }
1556         FILEDESC_UNLOCK(fdp);
1557         return (0);
1558 }
1559
1560 /*
1561  * This sysctl determines if we will allow a process to chroot(2) if it
1562  * has a directory open:
1563  *      0: disallowed for all processes.
1564  *      1: allowed for processes that were not already chroot(2)'ed.
1565  *      2: allowed for all processes.
1566  */
1567
1568 static int chroot_allow_open_directories = 1;
1569
1570 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1571      &chroot_allow_open_directories, 0, "");
1572
1573 /*
1574  * Change notion of root (``/'') directory.
1575  */
1576 #ifndef _SYS_SYSPROTO_H_
1577 struct chroot_args {
1578         char    *path;
1579 };
1580 #endif
1581 /* ARGSUSED */
1582 int
1583 chroot(td, uap)
1584         struct thread *td;
1585         struct chroot_args /* {
1586                 syscallarg(char *) path;
1587         } */ *uap;
1588 {
1589         register struct filedesc *fdp = td->td_proc->p_fd;
1590         int error;
1591         struct nameidata nd;
1592         struct vnode *vp;
1593
1594         error = suser_cred(td->td_ucred, PRISON_ROOT);
1595         if (error)
1596                 return (error);
1597         FILEDESC_LOCK(fdp);
1598         if (chroot_allow_open_directories == 0 ||
1599             (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1600                 FILEDESC_UNLOCK(fdp);
1601                 error = chroot_refuse_vdir_fds(fdp);
1602         } else
1603                 FILEDESC_UNLOCK(fdp);
1604         if (error)
1605                 return (error);
1606         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1607             SCARG(uap, path), td);
1608         if ((error = change_dir(&nd, td)) != 0)
1609                 return (error);
1610         NDFREE(&nd, NDF_ONLY_PNBUF);
1611         FILEDESC_LOCK(fdp);
1612         vp = fdp->fd_rdir;
1613         fdp->fd_rdir = nd.ni_vp;
1614         if (!fdp->fd_jdir) {
1615                 fdp->fd_jdir = nd.ni_vp;
1616                 VREF(fdp->fd_jdir);
1617         }
1618         FILEDESC_UNLOCK(fdp);
1619         vrele(vp);
1620         return (0);
1621 }
1622
1623 /*
1624  * Common routine for chroot and chdir.
1625  */
1626 static int
1627 change_dir(ndp, td)
1628         register struct nameidata *ndp;
1629         struct thread *td;
1630 {
1631         struct vnode *vp;
1632         int error;
1633
1634         error = namei(ndp);
1635         if (error)
1636                 return (error);
1637         vp = ndp->ni_vp;
1638         if (vp->v_type != VDIR)
1639                 error = ENOTDIR;
1640         else
1641                 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1642         if (error)
1643                 vput(vp);
1644         else
1645                 VOP_UNLOCK(vp, 0, td);
1646         return (error);
1647 }
1648
1649 /*
1650  * Check permissions, allocate an open file structure,
1651  * and call the device open routine if any.
1652  */
1653 #ifndef _SYS_SYSPROTO_H_
1654 struct open_args {
1655         char    *path;
1656         int     flags;
1657         int     mode;
1658 };
1659 #endif
1660 int
1661 open(td, uap)
1662         struct thread *td;
1663         register struct open_args /* {
1664                 syscallarg(char *) path;
1665                 syscallarg(int) flags;
1666                 syscallarg(int) mode;
1667         } */ *uap;
1668 {
1669         struct proc *p = td->td_proc;
1670         struct filedesc *fdp = p->p_fd;
1671         struct file *fp;
1672         struct vnode *vp;
1673         struct vattr vat;
1674         struct mount *mp;
1675         int cmode, flags, oflags;
1676         struct file *nfp;
1677         int type, indx, error;
1678         struct flock lf;
1679         struct nameidata nd;
1680
1681         oflags = SCARG(uap, flags);
1682         if ((oflags & O_ACCMODE) == O_ACCMODE)
1683                 return (EINVAL);
1684         flags = FFLAGS(oflags);
1685         error = falloc(td, &nfp, &indx);
1686         if (error)
1687                 return (error);
1688         fp = nfp;
1689         FILEDESC_LOCK(fdp);
1690         cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1691         FILEDESC_UNLOCK(fdp);
1692         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1693         td->td_dupfd = -indx - 1;               /* XXX check for fdopen */
1694         /*
1695          * Bump the ref count to prevent another process from closing
1696          * the descriptor while we are blocked in vn_open()
1697          */
1698         fhold(fp);
1699         error = vn_open(&nd, &flags, cmode);
1700         if (error) {
1701                 /*
1702                  * release our own reference
1703                  */
1704                 fdrop(fp, td);
1705
1706                 /*
1707                  * handle special fdopen() case.  bleh.  dupfdopen() is
1708                  * responsible for dropping the old contents of ofiles[indx]
1709                  * if it succeeds.
1710                  */
1711                 if ((error == ENODEV || error == ENXIO) &&
1712                     td->td_dupfd >= 0 &&                /* XXX from fdopen */
1713                     (error =
1714                         dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1715                         td->td_retval[0] = indx;
1716                         return (0);
1717                 }
1718                 /*
1719                  * Clean up the descriptor, but only if another thread hadn't
1720                  * replaced or closed it.
1721                  */
1722                 FILEDESC_LOCK(fdp);
1723                 if (fdp->fd_ofiles[indx] == fp) {
1724                         fdp->fd_ofiles[indx] = NULL;
1725                         FILEDESC_UNLOCK(fdp);
1726                         fdrop(fp, td);
1727                 } else
1728                         FILEDESC_UNLOCK(fdp);
1729
1730                 if (error == ERESTART)
1731                         error = EINTR;
1732                 return (error);
1733         }
1734         td->td_dupfd = 0;
1735         NDFREE(&nd, NDF_ONLY_PNBUF);
1736         vp = nd.ni_vp;
1737
1738         /*
1739          * There should be 2 references on the file, one from the descriptor
1740          * table, and one for us.
1741          *
1742          * Handle the case where someone closed the file (via its file
1743          * descriptor) while we were blocked.  The end result should look
1744          * like opening the file succeeded but it was immediately closed.
1745          */
1746         FILEDESC_LOCK(fdp);
1747         FILE_LOCK(fp);
1748         if (fp->f_count == 1) {
1749                 KASSERT(fdp->fd_ofiles[indx] != fp,
1750                     ("Open file descriptor lost all refs"));
1751                 FILEDESC_UNLOCK(fdp);
1752                 FILE_UNLOCK(fp);
1753                 VOP_UNLOCK(vp, 0, td);
1754                 vn_close(vp, flags & FMASK, fp->f_cred, td);
1755                 fdrop(fp, td);
1756                 td->td_retval[0] = indx;
1757                 return 0;
1758         }
1759
1760         fp->f_data = (caddr_t)vp;
1761         fp->f_flag = flags & FMASK;
1762         fp->f_ops = &vnops;
1763         fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1764         FILEDESC_UNLOCK(fdp);
1765         FILE_UNLOCK(fp);
1766         VOP_UNLOCK(vp, 0, td);
1767         if (flags & (O_EXLOCK | O_SHLOCK)) {
1768                 lf.l_whence = SEEK_SET;
1769                 lf.l_start = 0;
1770                 lf.l_len = 0;
1771                 if (flags & O_EXLOCK)
1772                         lf.l_type = F_WRLCK;
1773                 else
1774                         lf.l_type = F_RDLCK;
1775                 type = F_FLOCK;
1776                 if ((flags & FNONBLOCK) == 0)
1777                         type |= F_WAIT;
1778                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0)
1779                         goto bad;
1780                 fp->f_flag |= FHASLOCK;
1781         }
1782         if (flags & O_TRUNC) {
1783                 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1784                         goto bad;
1785                 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1786                 VATTR_NULL(&vat);
1787                 vat.va_size = 0;
1788                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1789                 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1790                 VOP_UNLOCK(vp, 0, td);
1791                 vn_finished_write(mp);
1792                 if (error)
1793                         goto bad;
1794         }
1795         /* assert that vn_open created a backing object if one is needed */
1796         KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1797                 ("open: vmio vnode has no backing object after vn_open"));
1798         /*
1799          * Release our private reference, leaving the one associated with
1800          * the descriptor table intact.
1801          */
1802         fdrop(fp, td);
1803         td->td_retval[0] = indx;
1804         return (0);
1805 bad:
1806         FILEDESC_LOCK(fdp);
1807         if (fdp->fd_ofiles[indx] == fp) {
1808                 fdp->fd_ofiles[indx] = NULL;
1809                 FILEDESC_UNLOCK(fdp);
1810                 fdrop(fp, td);
1811         } else
1812                 FILEDESC_UNLOCK(fdp);
1813         return (error);
1814 }
1815
1816 #ifdef COMPAT_43
1817 /*
1818  * Create a file.
1819  */
1820 #ifndef _SYS_SYSPROTO_H_
1821 struct ocreat_args {
1822         char    *path;
1823         int     mode;
1824 };
1825 #endif
1826 int
1827 ocreat(td, uap)
1828         struct thread *td;
1829         register struct ocreat_args /* {
1830                 syscallarg(char *) path;
1831                 syscallarg(int) mode;
1832         } */ *uap;
1833 {
1834         struct open_args /* {
1835                 syscallarg(char *) path;
1836                 syscallarg(int) flags;
1837                 syscallarg(int) mode;
1838         } */ nuap;
1839
1840         SCARG(&nuap, path) = SCARG(uap, path);
1841         SCARG(&nuap, mode) = SCARG(uap, mode);
1842         SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1843         return (open(td, &nuap));
1844 }
1845 #endif /* COMPAT_43 */
1846
1847 /*
1848  * Create a special file.
1849  */
1850 #ifndef _SYS_SYSPROTO_H_
1851 struct mknod_args {
1852         char    *path;
1853         int     mode;
1854         int     dev;
1855 };
1856 #endif
1857 /* ARGSUSED */
1858 int
1859 mknod(td, uap)
1860         struct thread *td;
1861         register struct mknod_args /* {
1862                 syscallarg(char *) path;
1863                 syscallarg(int) mode;
1864                 syscallarg(int) dev;
1865         } */ *uap;
1866 {
1867         struct vnode *vp;
1868         struct mount *mp;
1869         struct vattr vattr;
1870         int error;
1871         int whiteout = 0;
1872         struct nameidata nd;
1873
1874         switch (SCARG(uap, mode) & S_IFMT) {
1875         case S_IFCHR:
1876         case S_IFBLK:
1877                 error = suser(td);
1878                 break;
1879         default:
1880                 error = suser_cred(td->td_ucred, PRISON_ROOT);
1881                 break;
1882         }
1883         if (error)
1884                 return (error);
1885 restart:
1886         bwillwrite();
1887         NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1888         if ((error = namei(&nd)) != 0)
1889                 return (error);
1890         vp = nd.ni_vp;
1891         if (vp != NULL) {
1892                 vrele(vp);
1893                 error = EEXIST;
1894         } else {
1895                 VATTR_NULL(&vattr);
1896                 FILEDESC_LOCK(td->td_proc->p_fd);
1897                 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ td->td_proc->p_fd->fd_cmask;
1898                 FILEDESC_UNLOCK(td->td_proc->p_fd);
1899                 vattr.va_rdev = SCARG(uap, dev);
1900                 whiteout = 0;
1901
1902                 switch (SCARG(uap, mode) & S_IFMT) {
1903                 case S_IFMT:    /* used by badsect to flag bad sectors */
1904                         vattr.va_type = VBAD;
1905                         break;
1906                 case S_IFCHR:
1907                         vattr.va_type = VCHR;
1908                         break;
1909                 case S_IFBLK:
1910                         vattr.va_type = VBLK;
1911                         break;
1912                 case S_IFWHT:
1913                         whiteout = 1;
1914                         break;
1915                 default:
1916                         error = EINVAL;
1917                         break;
1918                 }
1919         }
1920         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1921                 NDFREE(&nd, NDF_ONLY_PNBUF);
1922                 vput(nd.ni_dvp);
1923                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1924                         return (error);
1925                 goto restart;
1926         }
1927         if (!error) {
1928                 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1929                 if (whiteout)
1930                         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1931                 else {
1932                         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1933                                                 &nd.ni_cnd, &vattr);
1934                         if (error == 0)
1935                                 vput(nd.ni_vp);
1936                 }
1937         }
1938         NDFREE(&nd, NDF_ONLY_PNBUF);
1939         vput(nd.ni_dvp);
1940         vn_finished_write(mp);
1941         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1942         ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1943         return (error);
1944 }
1945
1946 /*
1947  * Create a named pipe.
1948  */
1949 #ifndef _SYS_SYSPROTO_H_
1950 struct mkfifo_args {
1951         char    *path;
1952         int     mode;
1953 };
1954 #endif
1955 /* ARGSUSED */
1956 int
1957 mkfifo(td, uap)
1958         struct thread *td;
1959         register struct mkfifo_args /* {
1960                 syscallarg(char *) path;
1961                 syscallarg(int) mode;
1962         } */ *uap;
1963 {
1964         struct mount *mp;
1965         struct vattr vattr;
1966         int error;
1967         struct nameidata nd;
1968
1969 restart:
1970         bwillwrite();
1971         NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1972         if ((error = namei(&nd)) != 0)
1973                 return (error);
1974         if (nd.ni_vp != NULL) {
1975                 NDFREE(&nd, NDF_ONLY_PNBUF);
1976                 vrele(nd.ni_vp);
1977                 vput(nd.ni_dvp);
1978                 return (EEXIST);
1979         }
1980         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1981                 NDFREE(&nd, NDF_ONLY_PNBUF);
1982                 vput(nd.ni_dvp);
1983                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1984                         return (error);
1985                 goto restart;
1986         }
1987         VATTR_NULL(&vattr);
1988         vattr.va_type = VFIFO;
1989         FILEDESC_LOCK(td->td_proc->p_fd);
1990         vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ td->td_proc->p_fd->fd_cmask;
1991         FILEDESC_UNLOCK(td->td_proc->p_fd);
1992         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1993         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1994         if (error == 0)
1995                 vput(nd.ni_vp);
1996         NDFREE(&nd, NDF_ONLY_PNBUF);
1997         vput(nd.ni_dvp);
1998         vn_finished_write(mp);
1999         return (error);
2000 }
2001
2002 /*
2003  * Make a hard file link.
2004  */
2005 #ifndef _SYS_SYSPROTO_H_
2006 struct link_args {
2007         char    *path;
2008         char    *link;
2009 };
2010 #endif
2011 /* ARGSUSED */
2012 int
2013 link(td, uap)
2014         struct thread *td;
2015         register struct link_args /* {
2016                 syscallarg(char *) path;
2017                 syscallarg(char *) link;
2018         } */ *uap;
2019 {
2020         struct vnode *vp;
2021         struct mount *mp;
2022         struct nameidata nd;
2023         int error;
2024
2025         bwillwrite();
2026         NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
2027         if ((error = namei(&nd)) != 0)
2028                 return (error);
2029         NDFREE(&nd, NDF_ONLY_PNBUF);
2030         vp = nd.ni_vp;
2031         if (vp->v_type == VDIR) {
2032                 vrele(vp);
2033                 return (EPERM);         /* POSIX */
2034         }
2035         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2036                 vrele(vp);
2037                 return (error);
2038         }
2039         NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
2040         if ((error = namei(&nd)) == 0) {
2041                 if (nd.ni_vp != NULL) {
2042                         vrele(nd.ni_vp);
2043                         error = EEXIST;
2044                 } else {
2045                         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2046                         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2047                         error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2048                 }
2049                 NDFREE(&nd, NDF_ONLY_PNBUF);
2050                 vput(nd.ni_dvp);
2051         }
2052         vrele(vp);
2053         vn_finished_write(mp);
2054         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
2055         ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
2056         return (error);
2057 }
2058
2059 /*
2060  * Make a symbolic link.
2061  */
2062 #ifndef _SYS_SYSPROTO_H_
2063 struct symlink_args {
2064         char    *path;
2065         char    *link;
2066 };
2067 #endif
2068 /* ARGSUSED */
2069 int
2070 symlink(td, uap)
2071         struct thread *td;
2072         register struct symlink_args /* {
2073                 syscallarg(char *) path;
2074                 syscallarg(char *) link;
2075         } */ *uap;
2076 {
2077         struct mount *mp;
2078         struct vattr vattr;
2079         char *path;
2080         int error;
2081         struct nameidata nd;
2082
2083         path = uma_zalloc(namei_zone, M_WAITOK);
2084         if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
2085                 goto out;
2086 restart:
2087         bwillwrite();
2088         NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
2089         if ((error = namei(&nd)) != 0)
2090                 goto out;
2091         if (nd.ni_vp) {
2092                 NDFREE(&nd, NDF_ONLY_PNBUF);
2093                 vrele(nd.ni_vp);
2094                 vput(nd.ni_dvp);
2095                 error = EEXIST;
2096                 goto out;
2097         }
2098         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2099                 NDFREE(&nd, NDF_ONLY_PNBUF);
2100                 vput(nd.ni_dvp);
2101                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2102                         return (error);
2103                 goto restart;
2104         }
2105         VATTR_NULL(&vattr);
2106         FILEDESC_LOCK(td->td_proc->p_fd);
2107         vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
2108         FILEDESC_UNLOCK(td->td_proc->p_fd);
2109         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2110         error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2111         NDFREE(&nd, NDF_ONLY_PNBUF);
2112         if (error == 0)
2113                 vput(nd.ni_vp);
2114         vput(nd.ni_dvp);
2115         vn_finished_write(mp);
2116         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
2117         ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
2118 out:
2119         uma_zfree(namei_zone, path);
2120         return (error);
2121 }
2122
2123 /*
2124  * Delete a whiteout from the filesystem.
2125  */
2126 /* ARGSUSED */
2127 int
2128 undelete(td, uap)
2129         struct thread *td;
2130         register struct undelete_args /* {
2131                 syscallarg(char *) path;
2132         } */ *uap;
2133 {
2134         int error;
2135         struct mount *mp;
2136         struct nameidata nd;
2137
2138 restart:
2139         bwillwrite();
2140         NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
2141             SCARG(uap, path), td);
2142         error = namei(&nd);
2143         if (error)
2144                 return (error);
2145
2146         if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2147                 NDFREE(&nd, NDF_ONLY_PNBUF);
2148                 if (nd.ni_vp)
2149                         vrele(nd.ni_vp);
2150                 vput(nd.ni_dvp);
2151                 return (EEXIST);
2152         }
2153         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2154                 NDFREE(&nd, NDF_ONLY_PNBUF);
2155                 vput(nd.ni_dvp);
2156                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2157                         return (error);
2158                 goto restart;
2159         }
2160         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2161         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
2162         NDFREE(&nd, NDF_ONLY_PNBUF);
2163         vput(nd.ni_dvp);
2164         vn_finished_write(mp);
2165         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
2166         ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
2167         return (error);
2168 }
2169
2170 /*
2171  * Delete a name from the filesystem.
2172  */
2173 #ifndef _SYS_SYSPROTO_H_
2174 struct unlink_args {
2175         char    *path;
2176 };
2177 #endif
2178 /* ARGSUSED */
2179 int
2180 unlink(td, uap)
2181         struct thread *td;
2182         struct unlink_args /* {
2183                 syscallarg(char *) path;
2184         } */ *uap;
2185 {
2186         struct mount *mp;
2187         struct vnode *vp;
2188         int error;
2189         struct nameidata nd;
2190
2191 restart:
2192         bwillwrite();
2193         NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
2194         if ((error = namei(&nd)) != 0)
2195                 return (error);
2196         vp = nd.ni_vp;
2197         if (vp->v_type == VDIR)
2198                 error = EPERM;          /* POSIX */
2199         else {
2200                 /*
2201                  * The root of a mounted filesystem cannot be deleted.
2202                  *
2203                  * XXX: can this only be a VDIR case?
2204                  */
2205                 if (vp->v_flag & VROOT)
2206                         error = EBUSY;
2207         }
2208         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2209                 NDFREE(&nd, NDF_ONLY_PNBUF);
2210                 vrele(vp);
2211                 vput(nd.ni_dvp);
2212                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2213                         return (error);
2214                 goto restart;
2215         }
2216         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2217         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2218         if (!error) {
2219                 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2220                 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
2221         }
2222         NDFREE(&nd, NDF_ONLY_PNBUF);
2223         vput(nd.ni_dvp);
2224         vput(vp);
2225         vn_finished_write(mp);
2226         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
2227         ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
2228         return (error);
2229 }
2230
2231 /*
2232  * Reposition read/write file offset.
2233  */
2234 #ifndef _SYS_SYSPROTO_H_
2235 struct lseek_args {
2236         int     fd;
2237         int     pad;
2238         off_t   offset;
2239         int     whence;
2240 };
2241 #endif
2242 int
2243 lseek(td, uap)
2244         struct thread *td;
2245         register struct lseek_args /* {
2246                 syscallarg(int) fd;
2247                 syscallarg(int) pad;
2248                 syscallarg(off_t) offset;
2249                 syscallarg(int) whence;
2250         } */ *uap;
2251 {
2252         struct ucred *cred = td->td_ucred;
2253         struct file *fp;
2254         struct vnode *vp;
2255         struct vattr vattr;
2256         off_t offset;
2257         int error, noneg;
2258
2259         if ((error = fget(td, uap->fd, &fp)) != 0)
2260                 return (error);
2261         if (fp->f_type != DTYPE_VNODE) {
2262                 fdrop(fp, td);
2263                 return (ESPIPE);
2264         }
2265         vp = (struct vnode *)fp->f_data;
2266         noneg = (vp->v_type != VCHR);
2267         offset = SCARG(uap, offset);
2268         switch (SCARG(uap, whence)) {
2269         case L_INCR:
2270                 if (noneg &&
2271                     (fp->f_offset < 0 ||
2272                      (offset > 0 && fp->f_offset > OFF_MAX - offset)))
2273                         return (EOVERFLOW);
2274                 offset += fp->f_offset;
2275                 break;
2276         case L_XTND:
2277                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2278                 error = VOP_GETATTR(vp, &vattr, cred, td);
2279                 VOP_UNLOCK(vp, 0, td);
2280                 if (error)
2281                         return (error);
2282                 if (noneg &&
2283                     (vattr.va_size > OFF_MAX ||
2284                      (offset > 0 && vattr.va_size > OFF_MAX - offset)))
2285                         return (EOVERFLOW);
2286                 offset += vattr.va_size;
2287                 break;
2288         case L_SET:
2289                 break;
2290         default:
2291                 fdrop(fp, td);
2292                 return (EINVAL);
2293         }
2294         if (noneg && offset < 0)
2295                 return (EINVAL);
2296         fp->f_offset = offset;
2297         *(off_t *)(td->td_retval) = fp->f_offset;
2298         fdrop(fp, td);
2299         return (0);
2300 }
2301
2302 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2303 /*
2304  * Reposition read/write file offset.
2305  */
2306 #ifndef _SYS_SYSPROTO_H_
2307 struct olseek_args {
2308         int     fd;
2309         long    offset;
2310         int     whence;
2311 };
2312 #endif
2313 int
2314 olseek(td, uap)
2315         struct thread *td;
2316         register struct olseek_args /* {
2317                 syscallarg(int) fd;
2318                 syscallarg(long) offset;
2319                 syscallarg(int) whence;
2320         } */ *uap;
2321 {
2322         struct lseek_args /* {
2323                 syscallarg(int) fd;
2324                 syscallarg(int) pad;
2325                 syscallarg(off_t) offset;
2326                 syscallarg(int) whence;
2327         } */ nuap;
2328         int error;
2329
2330         SCARG(&nuap, fd) = SCARG(uap, fd);
2331         SCARG(&nuap, offset) = SCARG(uap, offset);
2332         SCARG(&nuap, whence) = SCARG(uap, whence);
2333         error = lseek(td, &nuap);
2334         return (error);
2335 }
2336 #endif /* COMPAT_43 */
2337
2338 /*
2339  * Check access permissions using passed credentials.
2340  */
2341 static int
2342 vn_access(vp, user_flags, cred, td)
2343         struct vnode    *vp;
2344         int             user_flags;
2345         struct ucred    *cred;
2346         struct thread   *td;
2347 {
2348         int error, flags;
2349
2350         /* Flags == 0 means only check for existence. */
2351         error = 0;
2352         if (user_flags) {
2353                 flags = 0;
2354                 if (user_flags & R_OK)
2355                         flags |= VREAD;
2356                 if (user_flags & W_OK)
2357                         flags |= VWRITE;
2358                 if (user_flags & X_OK)
2359                         flags |= VEXEC;
2360                 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2361                         error = VOP_ACCESS(vp, flags, cred, td);
2362         }
2363         return (error);
2364 }
2365
2366 /*
2367  * Check access permissions using "real" credentials.
2368  */
2369 #ifndef _SYS_SYSPROTO_H_
2370 struct access_args {
2371         char    *path;
2372         int     flags;
2373 };
2374 #endif
2375 int
2376 access(td, uap)
2377         struct thread *td;
2378         register struct access_args /* {
2379                 syscallarg(char *) path;
2380                 syscallarg(int) flags;
2381         } */ *uap;
2382 {
2383         struct ucred *cred, *tmpcred;
2384         register struct vnode *vp;
2385         int error;
2386         struct nameidata nd;
2387
2388         /*
2389          * Create and modify a temporary credential instead of one that
2390          * is potentially shared.  This could also mess up socket
2391          * buffer accounting which can run in an interrupt context.
2392          *
2393          * XXX - Depending on how "threads" are finally implemented, it
2394          * may be better to explicitly pass the credential to namei()
2395          * rather than to modify the potentially shared process structure.
2396          */
2397         cred = td->td_ucred;
2398         tmpcred = crdup(cred);
2399         tmpcred->cr_uid = cred->cr_ruid;
2400         tmpcred->cr_groups[0] = cred->cr_rgid;
2401         td->td_ucred = tmpcred;
2402         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2403             SCARG(uap, path), td);
2404         if ((error = namei(&nd)) != 0)
2405                 goto out1;
2406         vp = nd.ni_vp;
2407
2408         error = vn_access(vp, SCARG(uap, flags), tmpcred, td);
2409         NDFREE(&nd, NDF_ONLY_PNBUF);
2410         vput(vp);
2411 out1:
2412         td->td_ucred = cred;
2413         crfree(tmpcred);
2414         return (error);
2415 }
2416
2417 /*
2418  * Check access permissions using "effective" credentials.
2419  */
2420 #ifndef _SYS_SYSPROTO_H_
2421 struct eaccess_args {
2422         char    *path;
2423         int     flags;
2424 };
2425 #endif
2426 int
2427 eaccess(td, uap)
2428         struct thread *td;
2429         register struct eaccess_args /* {
2430                 syscallarg(char *) path;
2431                 syscallarg(int) flags;
2432         } */ *uap;
2433 {
2434         struct nameidata nd;
2435         struct vnode *vp;
2436         int error;
2437
2438         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2439             SCARG(uap, path), td);
2440         if ((error = namei(&nd)) != 0)
2441                 return (error);
2442         vp = nd.ni_vp;
2443
2444         error = vn_access(vp, SCARG(uap, flags), td->td_ucred, td);
2445         NDFREE(&nd, NDF_ONLY_PNBUF);
2446         vput(vp);
2447         return (error);
2448 }
2449
2450 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2451 /*
2452  * Get file status; this version follows links.
2453  */
2454 #ifndef _SYS_SYSPROTO_H_
2455 struct ostat_args {
2456         char    *path;
2457         struct ostat *ub;
2458 };
2459 #endif
2460 /* ARGSUSED */
2461 int
2462 ostat(td, uap)
2463         struct thread *td;
2464         register struct ostat_args /* {
2465                 syscallarg(char *) path;
2466                 syscallarg(struct ostat *) ub;
2467         } */ *uap;
2468 {
2469         struct stat sb;
2470         struct ostat osb;
2471         int error;
2472         struct nameidata nd;
2473
2474         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2475             SCARG(uap, path), td);
2476         if ((error = namei(&nd)) != 0)
2477                 return (error);
2478         NDFREE(&nd, NDF_ONLY_PNBUF);
2479         error = vn_stat(nd.ni_vp, &sb, td);
2480         vput(nd.ni_vp);
2481         if (error)
2482                 return (error);
2483         cvtstat(&sb, &osb);
2484         error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
2485         return (error);
2486 }
2487
2488 /*
2489  * Get file status; this version does not follow links.
2490  */
2491 #ifndef _SYS_SYSPROTO_H_
2492 struct olstat_args {
2493         char    *path;
2494         struct ostat *ub;
2495 };
2496 #endif
2497 /* ARGSUSED */
2498 int
2499 olstat(td, uap)
2500         struct thread *td;
2501         register struct olstat_args /* {
2502                 syscallarg(char *) path;
2503                 syscallarg(struct ostat *) ub;
2504         } */ *uap;
2505 {
2506         struct vnode *vp;
2507         struct stat sb;
2508         struct ostat osb;
2509         int error;
2510         struct nameidata nd;
2511
2512         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2513             SCARG(uap, path), td);
2514         if ((error = namei(&nd)) != 0)
2515                 return (error);
2516         vp = nd.ni_vp;
2517         error = vn_stat(vp, &sb, td);
2518         NDFREE(&nd, NDF_ONLY_PNBUF);
2519         vput(vp);
2520         if (error)
2521                 return (error);
2522         cvtstat(&sb, &osb);
2523         error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
2524         return (error);
2525 }
2526
2527 /*
2528  * Convert from an old to a new stat structure.
2529  */
2530 void
2531 cvtstat(st, ost)
2532         struct stat *st;
2533         struct ostat *ost;
2534 {
2535
2536         ost->st_dev = st->st_dev;
2537         ost->st_ino = st->st_ino;
2538         ost->st_mode = st->st_mode;
2539         ost->st_nlink = st->st_nlink;
2540         ost->st_uid = st->st_uid;
2541         ost->st_gid = st->st_gid;
2542         ost->st_rdev = st->st_rdev;
2543         if (st->st_size < (quad_t)1 << 32)
2544                 ost->st_size = st->st_size;
2545         else
2546                 ost->st_size = -2;
2547         ost->st_atime = st->st_atime;
2548         ost->st_mtime = st->st_mtime;
2549         ost->st_ctime = st->st_ctime;
2550         ost->st_blksize = st->st_blksize;
2551         ost->st_blocks = st->st_blocks;
2552         ost->st_flags = st->st_flags;
2553         ost->st_gen = st->st_gen;
2554 }
2555 #endif /* COMPAT_43 || COMPAT_SUNOS */
2556
2557 /*
2558  * Get file status; this version follows links.
2559  */
2560 #ifndef _SYS_SYSPROTO_H_
2561 struct stat_args {
2562         char    *path;
2563         struct stat *ub;
2564 };
2565 #endif
2566 /* ARGSUSED */
2567 int
2568 stat(td, uap)
2569         struct thread *td;
2570         register struct stat_args /* {
2571                 syscallarg(char *) path;
2572                 syscallarg(struct stat *) ub;
2573         } */ *uap;
2574 {
2575         struct stat sb;
2576         int error;
2577         struct nameidata nd;
2578
2579 #ifdef LOOKUP_SHARED
2580         NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | NOOBJ,
2581             UIO_USERSPACE, SCARG(uap, path), td);
2582 #else
2583         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2584             SCARG(uap, path), td);
2585 #endif
2586         if ((error = namei(&nd)) != 0)
2587                 return (error);
2588         error = vn_stat(nd.ni_vp, &sb, td);
2589         NDFREE(&nd, NDF_ONLY_PNBUF);
2590         vput(nd.ni_vp);
2591         if (error)
2592                 return (error);
2593         error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
2594         return (error);
2595 }
2596
2597 /*
2598  * Get file status; this version does not follow links.
2599  */
2600 #ifndef _SYS_SYSPROTO_H_
2601 struct lstat_args {
2602         char    *path;
2603         struct stat *ub;
2604 };
2605 #endif
2606 /* ARGSUSED */
2607 int
2608 lstat(td, uap)
2609         struct thread *td;
2610         register struct lstat_args /* {
2611                 syscallarg(char *) path;
2612                 syscallarg(struct stat *) ub;
2613         } */ *uap;
2614 {
2615         int error;
2616         struct vnode *vp;
2617         struct stat sb;
2618         struct nameidata nd;
2619
2620         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2621             SCARG(uap, path), td);
2622         if ((error = namei(&nd)) != 0)
2623                 return (error);
2624         vp = nd.ni_vp;
2625         error = vn_stat(vp, &sb, td);
2626         NDFREE(&nd, NDF_ONLY_PNBUF);
2627         vput(vp);
2628         if (error)
2629                 return (error);
2630         error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
2631         return (error);
2632 }
2633
2634 /*
2635  * Implementation of the NetBSD stat() function.
2636  * XXX This should probably be collapsed with the FreeBSD version,
2637  * as the differences are only due to vn_stat() clearing spares at
2638  * the end of the structures.  vn_stat could be split to avoid this,
2639  * and thus collapse the following to close to zero code.
2640  */
2641 void
2642 cvtnstat(sb, nsb)
2643         struct stat *sb;
2644         struct nstat *nsb;
2645 {
2646         nsb->st_dev = sb->st_dev;
2647         nsb->st_ino = sb->st_ino;
2648         nsb->st_mode = sb->st_mode;
2649         nsb->st_nlink = sb->st_nlink;
2650         nsb->st_uid = sb->st_uid;
2651         nsb->st_gid = sb->st_gid;
2652         nsb->st_rdev = sb->st_rdev;
2653         nsb->st_atimespec = sb->st_atimespec;
2654         nsb->st_mtimespec = sb->st_mtimespec;
2655         nsb->st_ctimespec = sb->st_ctimespec;
2656         nsb->st_size = sb->st_size;
2657         nsb->st_blocks = sb->st_blocks;
2658         nsb->st_blksize = sb->st_blksize;
2659         nsb->st_flags = sb->st_flags;
2660         nsb->st_gen = sb->st_gen;
2661         nsb->st_qspare[0] = sb->st_qspare[0];
2662         nsb->st_qspare[1] = sb->st_qspare[1];
2663 }
2664
2665 #ifndef _SYS_SYSPROTO_H_
2666 struct nstat_args {
2667         char    *path;
2668         struct nstat *ub;
2669 };
2670 #endif
2671 /* ARGSUSED */
2672 int
2673 nstat(td, uap)
2674         struct thread *td;
2675         register struct nstat_args /* {
2676                 syscallarg(char *) path;
2677                 syscallarg(struct nstat *) ub;
2678         } */ *uap;
2679 {
2680         struct stat sb;
2681         struct nstat nsb;
2682         int error;
2683         struct nameidata nd;
2684
2685         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2686             SCARG(uap, path), td);
2687         if ((error = namei(&nd)) != 0)
2688                 return (error);
2689         NDFREE(&nd, NDF_ONLY_PNBUF);
2690         error = vn_stat(nd.ni_vp, &sb, td);
2691         vput(nd.ni_vp);
2692         if (error)
2693                 return (error);
2694         cvtnstat(&sb, &nsb);
2695         error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
2696         return (error);
2697 }
2698
2699 /*
2700  * NetBSD lstat.  Get file status; this version does not follow links.
2701  */
2702 #ifndef _SYS_SYSPROTO_H_
2703 struct lstat_args {
2704         char    *path;
2705         struct stat *ub;
2706 };
2707 #endif
2708 /* ARGSUSED */
2709 int
2710 nlstat(td, uap)
2711         struct thread *td;
2712         register struct nlstat_args /* {
2713                 syscallarg(char *) path;
2714                 syscallarg(struct nstat *) ub;
2715         } */ *uap;
2716 {
2717         int error;
2718         struct vnode *vp;
2719         struct stat sb;
2720         struct nstat nsb;
2721         struct nameidata nd;
2722
2723         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2724             SCARG(uap, path), td);
2725         if ((error = namei(&nd)) != 0)
2726                 return (error);
2727         vp = nd.ni_vp;
2728         NDFREE(&nd, NDF_ONLY_PNBUF);
2729         error = vn_stat(vp, &sb, td);
2730         vput(vp);
2731         if (error)
2732                 return (error);
2733         cvtnstat(&sb, &nsb);
2734         error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
2735         return (error);
2736 }
2737
2738 /*
2739  * Get configurable pathname variables.
2740  */
2741 #ifndef _SYS_SYSPROTO_H_
2742 struct pathconf_args {
2743         char    *path;
2744         int     name;
2745 };
2746 #endif
2747 /* ARGSUSED */
2748 int
2749 pathconf(td, uap)
2750         struct thread *td;
2751         register struct pathconf_args /* {
2752                 syscallarg(char *) path;
2753                 syscallarg(int) name;
2754         } */ *uap;
2755 {
2756         int error;
2757         struct nameidata nd;
2758
2759         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2760             SCARG(uap, path), td);
2761         if ((error = namei(&nd)) != 0)
2762                 return (error);
2763         NDFREE(&nd, NDF_ONLY_PNBUF);
2764         error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), td->td_retval);
2765         vput(nd.ni_vp);
2766         return (error);
2767 }
2768
2769 /*
2770  * Return target name of a symbolic link.
2771  */
2772 #ifndef _SYS_SYSPROTO_H_
2773 struct readlink_args {
2774         char    *path;
2775         char    *buf;
2776         int     count;
2777 };
2778 #endif
2779 /* ARGSUSED */
2780 int
2781 readlink(td, uap)
2782         struct thread *td;
2783         register struct readlink_args /* {
2784                 syscallarg(char *) path;
2785                 syscallarg(char *) buf;
2786                 syscallarg(int) count;
2787         } */ *uap;
2788 {
2789         register struct vnode *vp;
2790         struct iovec aiov;
2791         struct uio auio;
2792         int error;
2793         struct nameidata nd;
2794
2795         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
2796             SCARG(uap, path), td);
2797         if ((error = namei(&nd)) != 0)
2798                 return (error);
2799         NDFREE(&nd, NDF_ONLY_PNBUF);
2800         vp = nd.ni_vp;
2801         if (vp->v_type != VLNK)
2802                 error = EINVAL;
2803         else {
2804                 aiov.iov_base = SCARG(uap, buf);
2805                 aiov.iov_len = SCARG(uap, count);
2806                 auio.uio_iov = &aiov;
2807                 auio.uio_iovcnt = 1;
2808                 auio.uio_offset = 0;
2809                 auio.uio_rw = UIO_READ;
2810                 auio.uio_segflg = UIO_USERSPACE;
2811                 auio.uio_td = td;
2812                 auio.uio_resid = SCARG(uap, count);
2813                 error = VOP_READLINK(vp, &auio, td->td_ucred);
2814         }
2815         vput(vp);
2816         td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
2817         return (error);
2818 }
2819
2820 /*
2821  * Common implementation code for chflags() and fchflags().
2822  */
2823 static int
2824 setfflags(td, vp, flags)
2825         struct thread *td;
2826         struct vnode *vp;
2827         int flags;
2828 {
2829         int error;
2830         struct mount *mp;
2831         struct vattr vattr;
2832
2833         /*
2834          * Prevent non-root users from setting flags on devices.  When
2835          * a device is reused, users can retain ownership of the device
2836          * if they are allowed to set flags and programs assume that
2837          * chown can't fail when done as root.
2838          */
2839         if (vp->v_type == VCHR || vp->v_type == VBLK) {
2840                 error = suser_cred(td->td_ucred, PRISON_ROOT);
2841                 if (error)
2842                         return (error);
2843         }
2844
2845         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2846                 return (error);
2847         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2848         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2849         VATTR_NULL(&vattr);
2850         vattr.va_flags = flags;
2851         error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2852         VOP_UNLOCK(vp, 0, td);
2853         vn_finished_write(mp);
2854         return (error);
2855 }
2856
2857 /*
2858  * Change flags of a file given a path name.
2859  */
2860 #ifndef _SYS_SYSPROTO_H_
2861 struct chflags_args {
2862         char    *path;
2863         int     flags;
2864 };
2865 #endif
2866 /* ARGSUSED */
2867 int
2868 chflags(td, uap)
2869         struct thread *td;
2870         register struct chflags_args /* {
2871                 syscallarg(char *) path;
2872                 syscallarg(int) flags;
2873         } */ *uap;
2874 {
2875         int error;
2876         struct nameidata nd;
2877
2878         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2879         if ((error = namei(&nd)) != 0)
2880                 return (error);
2881         NDFREE(&nd, NDF_ONLY_PNBUF);
2882         error = setfflags(td, nd.ni_vp, SCARG(uap, flags));
2883         vrele(nd.ni_vp);
2884         return error;
2885 }
2886
2887 /*
2888  * Change flags of a file given a file descriptor.
2889  */
2890 #ifndef _SYS_SYSPROTO_H_
2891 struct fchflags_args {
2892         int     fd;
2893         int     flags;
2894 };
2895 #endif
2896 /* ARGSUSED */
2897 int
2898 fchflags(td, uap)
2899         struct thread *td;
2900         register struct fchflags_args /* {
2901                 syscallarg(int) fd;
2902                 syscallarg(int) flags;
2903         } */ *uap;
2904 {
2905         struct file *fp;
2906         int error;
2907
2908         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2909                 return (error);
2910         error = setfflags(td, (struct vnode *) fp->f_data, SCARG(uap, flags));
2911         fdrop(fp, td);
2912         return (error);
2913 }
2914
2915 /*
2916  * Common implementation code for chmod(), lchmod() and fchmod().
2917  */
2918 static int
2919 setfmode(td, vp, mode)
2920         struct thread *td;
2921         struct vnode *vp;
2922         int mode;
2923 {
2924         int error;
2925         struct mount *mp;
2926         struct vattr vattr;
2927
2928         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2929                 return (error);
2930         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2931         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2932         VATTR_NULL(&vattr);
2933         vattr.va_mode = mode & ALLPERMS;
2934         error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2935         VOP_UNLOCK(vp, 0, td);
2936         vn_finished_write(mp);
2937         return error;
2938 }
2939
2940 /*
2941  * Change mode of a file given path name.
2942  */
2943 #ifndef _SYS_SYSPROTO_H_
2944 struct chmod_args {
2945         char    *path;
2946         int     mode;
2947 };
2948 #endif
2949 /* ARGSUSED */
2950 int
2951 chmod(td, uap)
2952         struct thread *td;
2953         register struct chmod_args /* {
2954                 syscallarg(char *) path;
2955                 syscallarg(int) mode;
2956         } */ *uap;
2957 {
2958         int error;
2959         struct nameidata nd;
2960
2961         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2962         if ((error = namei(&nd)) != 0)
2963                 return (error);
2964         NDFREE(&nd, NDF_ONLY_PNBUF);
2965         error = setfmode(td, nd.ni_vp, SCARG(uap, mode));
2966         vrele(nd.ni_vp);
2967         return error;
2968 }
2969
2970 /*
2971  * Change mode of a file given path name (don't follow links.)
2972  */
2973 #ifndef _SYS_SYSPROTO_H_
2974 struct lchmod_args {
2975         char    *path;
2976         int     mode;
2977 };
2978 #endif
2979 /* ARGSUSED */
2980 int
2981 lchmod(td, uap)
2982         struct thread *td;
2983         register struct lchmod_args /* {
2984                 syscallarg(char *) path;
2985                 syscallarg(int) mode;
2986         } */ *uap;
2987 {
2988         int error;
2989         struct nameidata nd;
2990
2991         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2992         if ((error = namei(&nd)) != 0)
2993                 return (error);
2994         NDFREE(&nd, NDF_ONLY_PNBUF);
2995         error = setfmode(td, nd.ni_vp, SCARG(uap, mode));
2996         vrele(nd.ni_vp);
2997         return error;
2998 }
2999
3000 /*
3001  * Change mode of a file given a file descriptor.
3002  */
3003 #ifndef _SYS_SYSPROTO_H_
3004 struct fchmod_args {
3005         int     fd;
3006         int     mode;
3007 };
3008 #endif
3009 /* ARGSUSED */
3010 int
3011 fchmod(td, uap)
3012         struct thread *td;
3013         register struct fchmod_args /* {
3014                 syscallarg(int) fd;
3015                 syscallarg(int) mode;
3016         } */ *uap;
3017 {
3018         struct file *fp;
3019         struct vnode *vp;
3020         int error;
3021
3022         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3023                 return (error);
3024         vp = (struct vnode *)fp->f_data;
3025         error = setfmode(td, (struct vnode *)fp->f_data, SCARG(uap, mode));
3026         fdrop(fp, td);
3027         return (error);
3028 }
3029
3030 /*
3031  * Common implementation for chown(), lchown(), and fchown()
3032  */
3033 static int
3034 setfown(td, vp, uid, gid)
3035         struct thread *td;
3036         struct vnode *vp;
3037         uid_t uid;
3038         gid_t gid;
3039 {
3040         int error;
3041         struct mount *mp;
3042         struct vattr vattr;
3043
3044         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3045                 return (error);
3046         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3047         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3048         VATTR_NULL(&vattr);
3049         vattr.va_uid = uid;
3050         vattr.va_gid = gid;
3051         error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3052         VOP_UNLOCK(vp, 0, td);
3053         vn_finished_write(mp);
3054         return error;
3055 }
3056
3057 /*
3058  * Set ownership given a path name.
3059  */
3060 #ifndef _SYS_SYSPROTO_H_
3061 struct chown_args {
3062         char    *path;
3063         int     uid;
3064         int     gid;
3065 };
3066 #endif
3067 /* ARGSUSED */
3068 int
3069 chown(td, uap)
3070         struct thread *td;
3071         register struct chown_args /* {
3072                 syscallarg(char *) path;
3073                 syscallarg(int) uid;
3074                 syscallarg(int) gid;
3075         } */ *uap;
3076 {
3077         int error;
3078         struct nameidata nd;
3079
3080         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3081         if ((error = namei(&nd)) != 0)
3082                 return (error);
3083         NDFREE(&nd, NDF_ONLY_PNBUF);
3084         error = setfown(td, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
3085         vrele(nd.ni_vp);
3086         return (error);
3087 }
3088
3089 /*
3090  * Set ownership given a path name, do not cross symlinks.
3091  */
3092 #ifndef _SYS_SYSPROTO_H_
3093 struct lchown_args {
3094         char    *path;
3095         int     uid;
3096         int     gid;
3097 };
3098 #endif
3099 /* ARGSUSED */
3100 int
3101 lchown(td, uap)
3102         struct thread *td;
3103         register struct lchown_args /* {
3104                 syscallarg(char *) path;
3105                 syscallarg(int) uid;
3106                 syscallarg(int) gid;
3107         } */ *uap;
3108 {
3109         int error;
3110         struct nameidata nd;
3111
3112         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3113         if ((error = namei(&nd)) != 0)
3114                 return (error);
3115         NDFREE(&nd, NDF_ONLY_PNBUF);
3116         error = setfown(td, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
3117         vrele(nd.ni_vp);
3118         return (error);
3119 }
3120
3121 /*
3122  * Set ownership given a file descriptor.
3123  */
3124 #ifndef _SYS_SYSPROTO_H_
3125 struct fchown_args {
3126         int     fd;
3127         int     uid;
3128         int     gid;
3129 };
3130 #endif
3131 /* ARGSUSED */
3132 int
3133 fchown(td, uap)
3134         struct thread *td;
3135         register struct fchown_args /* {
3136                 syscallarg(int) fd;
3137                 syscallarg(int) uid;
3138                 syscallarg(int) gid;
3139         } */ *uap;
3140 {
3141         struct file *fp;
3142         struct vnode *vp;
3143         int error;
3144
3145         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3146                 return (error);
3147         vp = (struct vnode *)fp->f_data;
3148         error = setfown(td, (struct vnode *)fp->f_data,
3149                 SCARG(uap, uid), SCARG(uap, gid));
3150         fdrop(fp, td);
3151         return (error);
3152 }
3153
3154 /*
3155  * Common implementation code for utimes(), lutimes(), and futimes().
3156  */
3157 static int
3158 getutimes(usrtvp, tsp)
3159         const struct timeval *usrtvp;
3160         struct timespec *tsp;
3161 {
3162         struct timeval tv[2];
3163         int error;
3164
3165         if (usrtvp == NULL) {
3166                 microtime(&tv[0]);
3167                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3168                 tsp[1] = tsp[0];
3169         } else {
3170                 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
3171                         return (error);
3172                 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3173                 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
3174         }
3175         return 0;
3176 }
3177
3178 /*
3179  * Common implementation code for utimes(), lutimes(), and futimes().
3180  */
3181 static int
3182 setutimes(td, vp, ts, nullflag)
3183         struct thread *td;
3184         struct vnode *vp;
3185         const struct timespec *ts;
3186         int nullflag;
3187 {
3188         int error;
3189         struct mount *mp;
3190         struct vattr vattr;
3191
3192         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3193                 return (error);
3194         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3195         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3196         VATTR_NULL(&vattr);
3197         vattr.va_atime = ts[0];
3198         vattr.va_mtime = ts[1];
3199         if (nullflag)
3200                 vattr.va_vaflags |= VA_UTIMES_NULL;
3201         error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3202         VOP_UNLOCK(vp, 0, td);
3203         vn_finished_write(mp);
3204         return error;
3205 }
3206
3207 /*
3208  * Set the access and modification times of a file.
3209  */
3210 #ifndef _SYS_SYSPROTO_H_
3211 struct utimes_args {
3212         char    *path;
3213         struct  timeval *tptr;
3214 };
3215 #endif
3216 /* ARGSUSED */
3217 int
3218 utimes(td, uap)
3219         struct thread *td;
3220         register struct utimes_args /* {
3221                 syscallarg(char *) path;
3222                 syscallarg(struct timeval *) tptr;
3223         } */ *uap;
3224 {
3225         struct timespec ts[2];
3226         struct timeval *usrtvp;
3227         int error;
3228         struct nameidata nd;
3229
3230         usrtvp = SCARG(uap, tptr);
3231         if ((error = getutimes(usrtvp, ts)) != 0)
3232                 return (error);
3233         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3234         if ((error = namei(&nd)) != 0)
3235                 return (error);
3236         NDFREE(&nd, NDF_ONLY_PNBUF);
3237         error = setutimes(td, nd.ni_vp, ts, usrtvp == NULL);
3238         vrele(nd.ni_vp);
3239         return (error);
3240 }
3241
3242 /*
3243  * Set the access and modification times of a file.
3244  */
3245 #ifndef _SYS_SYSPROTO_H_
3246 struct lutimes_args {
3247         char    *path;
3248         struct  timeval *tptr;
3249 };
3250 #endif
3251 /* ARGSUSED */
3252 int
3253 lutimes(td, uap)
3254         struct thread *td;
3255         register struct lutimes_args /* {
3256                 syscallarg(char *) path;
3257                 syscallarg(struct timeval *) tptr;
3258         } */ *uap;
3259 {
3260         struct timespec ts[2];
3261         struct timeval *usrtvp;
3262         int error;
3263         struct nameidata nd;
3264
3265         usrtvp = SCARG(uap, tptr);
3266         if ((error = getutimes(usrtvp, ts)) != 0)
3267                 return (error);
3268         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3269         if ((error = namei(&nd)) != 0)
3270                 return (error);
3271         NDFREE(&nd, NDF_ONLY_PNBUF);
3272         error = setutimes(td, nd.ni_vp, ts, usrtvp == NULL);
3273         vrele(nd.ni_vp);
3274         return (error);
3275 }
3276
3277 /*
3278  * Set the access and modification times of a file.
3279  */
3280 #ifndef _SYS_SYSPROTO_H_
3281 struct futimes_args {
3282         int     fd;
3283         struct  timeval *tptr;
3284 };
3285 #endif
3286 /* ARGSUSED */
3287 int
3288 futimes(td, uap)
3289         struct thread *td;
3290         register struct futimes_args /* {
3291                 syscallarg(int ) fd;
3292                 syscallarg(struct timeval *) tptr;
3293         } */ *uap;
3294 {
3295         struct timespec ts[2];
3296         struct file *fp;
3297         struct timeval *usrtvp;
3298         int error;
3299
3300         usrtvp = SCARG(uap, tptr);
3301         if ((error = getutimes(usrtvp, ts)) != 0)
3302                 return (error);
3303         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3304                 return (error);
3305         error = setutimes(td, (struct vnode *)fp->f_data, ts, usrtvp == NULL);
3306         fdrop(fp, td);
3307         return (error);
3308 }
3309
3310 /*
3311  * Truncate a file given its path name.
3312  */
3313 #ifndef _SYS_SYSPROTO_H_
3314 struct truncate_args {
3315         char    *path;
3316         int     pad;
3317         off_t   length;
3318 };
3319 #endif
3320 /* ARGSUSED */
3321 int
3322 truncate(td, uap)
3323         struct thread *td;
3324         register struct truncate_args /* {
3325                 syscallarg(char *) path;
3326                 syscallarg(int) pad;
3327                 syscallarg(off_t) length;
3328         } */ *uap;
3329 {
3330         struct mount *mp;
3331         struct vnode *vp;
3332         struct vattr vattr;
3333         int error;
3334         struct nameidata nd;
3335
3336         if (uap->length < 0)
3337                 return(EINVAL);
3338         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3339         if ((error = namei(&nd)) != 0)
3340                 return (error);
3341         vp = nd.ni_vp;
3342         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3343                 vrele(vp);
3344                 return (error);
3345         }
3346         NDFREE(&nd, NDF_ONLY_PNBUF);
3347         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3348         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3349         if (vp->v_type == VDIR)
3350                 error = EISDIR;
3351         else if ((error = vn_writechk(vp)) == 0 &&
3352             (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3353                 VATTR_NULL(&vattr);
3354                 vattr.va_size = SCARG(uap, length);
3355                 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3356         }
3357         vput(vp);
3358         vn_finished_write(mp);
3359         return (error);
3360 }
3361
3362 /*
3363  * Truncate a file given a file descriptor.
3364  */
3365 #ifndef _SYS_SYSPROTO_H_
3366 struct ftruncate_args {
3367         int     fd;
3368         int     pad;
3369         off_t   length;
3370 };
3371 #endif
3372 /* ARGSUSED */
3373 int
3374 ftruncate(td, uap)
3375         struct thread *td;
3376         register struct ftruncate_args /* {
3377                 syscallarg(int) fd;
3378                 syscallarg(int) pad;
3379                 syscallarg(off_t) length;
3380         } */ *uap;
3381 {
3382         struct mount *mp;
3383         struct vattr vattr;
3384         struct vnode *vp;
3385         struct file *fp;
3386         int error;
3387
3388         if (uap->length < 0)
3389                 return(EINVAL);
3390         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3391                 return (error);
3392         if ((fp->f_flag & FWRITE) == 0) {
3393                 fdrop(fp, td);
3394                 return (EINVAL);
3395         }
3396         vp = (struct vnode *)fp->f_data;
3397         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3398                 fdrop(fp, td);
3399                 return (error);
3400         }
3401         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3402         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3403         if (vp->v_type == VDIR)
3404                 error = EISDIR;
3405         else if ((error = vn_writechk(vp)) == 0) {
3406                 VATTR_NULL(&vattr);
3407                 vattr.va_size = SCARG(uap, length);
3408                 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3409         }
3410         VOP_UNLOCK(vp, 0, td);
3411         vn_finished_write(mp);
3412         fdrop(fp, td);
3413         return (error);
3414 }
3415
3416 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
3417 /*
3418  * Truncate a file given its path name.
3419  */
3420 #ifndef _SYS_SYSPROTO_H_
3421 struct otruncate_args {
3422         char    *path;
3423         long    length;
3424 };
3425 #endif
3426 /* ARGSUSED */
3427 int
3428 otruncate(td, uap)
3429         struct thread *td;
3430         register struct otruncate_args /* {
3431                 syscallarg(char *) path;
3432                 syscallarg(long) length;
3433         } */ *uap;
3434 {
3435         struct truncate_args /* {
3436                 syscallarg(char *) path;
3437                 syscallarg(int) pad;
3438                 syscallarg(off_t) length;
3439         } */ nuap;
3440
3441         SCARG(&nuap, path) = SCARG(uap, path);
3442         SCARG(&nuap, length) = SCARG(uap, length);
3443         return (truncate(td, &nuap));
3444 }
3445
3446 /*
3447  * Truncate a file given a file descriptor.
3448  */
3449 #ifndef _SYS_SYSPROTO_H_
3450 struct oftruncate_args {
3451         int     fd;
3452         long    length;
3453 };
3454 #endif
3455 /* ARGSUSED */
3456 int
3457 oftruncate(td, uap)
3458         struct thread *td;
3459         register struct oftruncate_args /* {
3460                 syscallarg(int) fd;
3461                 syscallarg(long) length;
3462         } */ *uap;
3463 {
3464         struct ftruncate_args /* {
3465                 syscallarg(int) fd;
3466                 syscallarg(int) pad;
3467                 syscallarg(off_t) length;
3468         } */ nuap;
3469
3470         SCARG(&nuap, fd) = SCARG(uap, fd);
3471         SCARG(&nuap, length) = SCARG(uap, length);
3472         return (ftruncate(td, &nuap));
3473 }
3474 #endif /* COMPAT_43 || COMPAT_SUNOS */
3475
3476 /*
3477  * Sync an open file.
3478  */
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct fsync_args {
3481         int     fd;
3482 };
3483 #endif
3484 /* ARGSUSED */
3485 int
3486 fsync(td, uap)
3487         struct thread *td;
3488         struct fsync_args /* {
3489                 syscallarg(int) fd;
3490         } */ *uap;
3491 {
3492         struct vnode *vp;
3493         struct mount *mp;
3494         struct file *fp;
3495         vm_object_t obj;
3496         int error;
3497
3498         GIANT_REQUIRED;
3499
3500         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3501                 return (error);
3502         vp = (struct vnode *)fp->f_data;
3503         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3504                 fdrop(fp, td);
3505                 return (error);
3506         }
3507         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3508         if (VOP_GETVOBJECT(vp, &obj) == 0) {
3509                 vm_object_page_clean(obj, 0, 0, 0);
3510         }
3511         error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, td);
3512 #ifdef SOFTUPDATES
3513         if (error == 0 && vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3514             error = softdep_fsync(vp);
3515 #endif
3516
3517         VOP_UNLOCK(vp, 0, td);
3518         vn_finished_write(mp);
3519         fdrop(fp, td);
3520         return (error);
3521 }
3522
3523 /*
3524  * Rename files.  Source and destination must either both be directories,
3525  * or both not be directories.  If target is a directory, it must be empty.
3526  */
3527 #ifndef _SYS_SYSPROTO_H_
3528 struct rename_args {
3529         char    *from;
3530         char    *to;
3531 };
3532 #endif
3533 /* ARGSUSED */
3534 int
3535 rename(td, uap)
3536         struct thread *td;
3537         register struct rename_args /* {
3538                 syscallarg(char *) from;
3539                 syscallarg(char *) to;
3540         } */ *uap;
3541 {
3542         struct mount *mp;
3543         struct vnode *tvp, *fvp, *tdvp;
3544         struct nameidata fromnd, tond;
3545         int error;
3546
3547         bwillwrite();
3548         NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
3549             SCARG(uap, from), td);
3550         if ((error = namei(&fromnd)) != 0)
3551                 return (error);
3552         fvp = fromnd.ni_vp;
3553         if ((error = vn_start_write(fvp, &mp, V_WAIT | PCATCH)) != 0) {
3554                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3555                 vrele(fromnd.ni_dvp);
3556                 vrele(fvp);
3557                 goto out1;
3558         }
3559         NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
3560             UIO_USERSPACE, SCARG(uap, to), td);
3561         if (fromnd.ni_vp->v_type == VDIR)
3562                 tond.ni_cnd.cn_flags |= WILLBEDIR;
3563         if ((error = namei(&tond)) != 0) {
3564                 /* Translate error code for rename("dir1", "dir2/."). */
3565                 if (error == EISDIR && fvp->v_type == VDIR)
3566                         error = EINVAL;
3567                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3568                 vrele(fromnd.ni_dvp);
3569                 vrele(fvp);
3570                 goto out1;
3571         }
3572         tdvp = tond.ni_dvp;
3573         tvp = tond.ni_vp;
3574         if (tvp != NULL) {
3575                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3576                         error = ENOTDIR;
3577                         goto out;
3578                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3579                         error = EISDIR;
3580                         goto out;
3581                 }
3582         }
3583         if (fvp == tdvp)
3584                 error = EINVAL;
3585         /*
3586          * If source is the same as the destination (that is the
3587          * same inode number with the same name in the same directory),
3588          * then there is nothing to do.
3589          */
3590         if (fvp == tvp && fromnd.ni_dvp == tdvp &&
3591             fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3592             !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
3593               fromnd.ni_cnd.cn_namelen))
3594                 error = -1;
3595 out:
3596         if (!error) {
3597                 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3598                 if (fromnd.ni_dvp != tdvp) {
3599                         VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3600                 }
3601                 if (tvp) {
3602                         VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3603                 }
3604                 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3605                                    tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3606                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3607                 NDFREE(&tond, NDF_ONLY_PNBUF);
3608         } else {
3609                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3610                 NDFREE(&tond, NDF_ONLY_PNBUF);
3611                 if (tdvp == tvp)
3612                         vrele(tdvp);
3613                 else
3614                         vput(tdvp);
3615                 if (tvp)
3616                         vput(tvp);
3617                 vrele(fromnd.ni_dvp);
3618                 vrele(fvp);
3619         }
3620         vrele(tond.ni_startdir);
3621         vn_finished_write(mp);
3622         ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
3623         ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
3624         ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
3625         ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
3626 out1:
3627         if (fromnd.ni_startdir)
3628                 vrele(fromnd.ni_startdir);
3629         if (error == -1)
3630                 return (0);
3631         return (error);
3632 }
3633
3634 /*
3635  * Make a directory file.
3636  */
3637 #ifndef _SYS_SYSPROTO_H_
3638 struct mkdir_args {
3639         char    *path;
3640         int     mode;
3641 };
3642 #endif
3643 /* ARGSUSED */
3644 int
3645 mkdir(td, uap)
3646         struct thread *td;
3647         register struct mkdir_args /* {
3648                 syscallarg(char *) path;
3649                 syscallarg(int) mode;
3650         } */ *uap;
3651 {
3652
3653         return vn_mkdir(uap->path, uap->mode, UIO_USERSPACE, td);
3654 }
3655
3656 int
3657 vn_mkdir(path, mode, segflg, td)
3658         char *path;
3659         int mode;
3660         enum uio_seg segflg;
3661         struct thread *td;
3662 {
3663         struct mount *mp;
3664         struct vnode *vp;
3665         struct vattr vattr;
3666         int error;
3667         struct nameidata nd;
3668
3669 restart:
3670         bwillwrite();
3671         NDINIT(&nd, CREATE, LOCKPARENT, segflg, path, td);
3672         nd.ni_cnd.cn_flags |= WILLBEDIR;
3673         if ((error = namei(&nd)) != 0)
3674                 return (error);
3675         vp = nd.ni_vp;
3676         if (vp != NULL) {
3677                 NDFREE(&nd, NDF_ONLY_PNBUF);
3678                 vrele(vp);
3679                 vput(nd.ni_dvp);
3680                 return (EEXIST);
3681         }
3682         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3683                 NDFREE(&nd, NDF_ONLY_PNBUF);
3684                 vput(nd.ni_dvp);
3685                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3686                         return (error);
3687                 goto restart;
3688         }
3689         VATTR_NULL(&vattr);
3690         vattr.va_type = VDIR;
3691         FILEDESC_LOCK(td->td_proc->p_fd);
3692         vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3693         FILEDESC_UNLOCK(td->td_proc->p_fd);
3694         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3695         error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3696         NDFREE(&nd, NDF_ONLY_PNBUF);
3697         vput(nd.ni_dvp);
3698         if (!error)
3699                 vput(nd.ni_vp);
3700         vn_finished_write(mp);
3701         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
3702         ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
3703         return (error);
3704 }
3705
3706 /*
3707  * Remove a directory file.
3708  */
3709 #ifndef _SYS_SYSPROTO_H_
3710 struct rmdir_args {
3711         char    *path;
3712 };
3713 #endif
3714 /* ARGSUSED */
3715 int
3716 rmdir(td, uap)
3717         struct thread *td;
3718         struct rmdir_args /* {
3719                 syscallarg(char *) path;
3720         } */ *uap;
3721 {
3722         struct mount *mp;
3723         struct vnode *vp;
3724         int error;
3725         struct nameidata nd;
3726
3727 restart:
3728         bwillwrite();
3729         NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
3730             SCARG(uap, path), td);
3731         if ((error = namei(&nd)) != 0)
3732                 return (error);
3733         vp = nd.ni_vp;
3734         if (vp->v_type != VDIR) {
3735                 error = ENOTDIR;
3736                 goto out;
3737         }
3738         /*
3739          * No rmdir "." please.
3740          */
3741         if (nd.ni_dvp == vp) {
3742                 error = EINVAL;
3743                 goto out;
3744         }
3745         /*
3746          * The root of a mounted filesystem cannot be deleted.
3747          */
3748         if (vp->v_flag & VROOT) {
3749                 error = EBUSY;
3750                 goto out;
3751         }
3752         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3753                 NDFREE(&nd, NDF_ONLY_PNBUF);
3754                 if (nd.ni_dvp == vp)
3755                         vrele(nd.ni_dvp);
3756                 else
3757                         vput(nd.ni_dvp);
3758                 vput(vp);
3759                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3760                         return (error);
3761                 goto restart;
3762         }
3763         VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3764         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3765         error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3766         vn_finished_write(mp);
3767 out:
3768         NDFREE(&nd, NDF_ONLY_PNBUF);
3769         if (nd.ni_dvp == vp)
3770                 vrele(nd.ni_dvp);
3771         else
3772                 vput(nd.ni_dvp);
3773         vput(vp);
3774         ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
3775         ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
3776         return (error);
3777 }
3778
3779 #ifdef COMPAT_43
3780 /*
3781  * Read a block of directory entries in a file system independent format.
3782  */
3783 #ifndef _SYS_SYSPROTO_H_
3784 struct ogetdirentries_args {
3785         int     fd;
3786         char    *buf;
3787         u_int   count;
3788         long    *basep;
3789 };
3790 #endif
3791 int
3792 ogetdirentries(td, uap)
3793         struct thread *td;
3794         register struct ogetdirentries_args /* {
3795                 syscallarg(int) fd;
3796                 syscallarg(char *) buf;
3797                 syscallarg(u_int) count;
3798                 syscallarg(long *) basep;
3799         } */ *uap;
3800 {
3801         struct vnode *vp;
3802         struct file *fp;
3803         struct uio auio, kuio;
3804         struct iovec aiov, kiov;
3805         struct dirent *dp, *edp;
3806         caddr_t dirbuf;
3807         int error, eofflag, readcnt;
3808         long loff;
3809
3810         /* XXX arbitrary sanity limit on `count'. */
3811         if (SCARG(uap, count) > 64 * 1024)
3812                 return (EINVAL);
3813         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3814                 return (error);
3815         if ((fp->f_flag & FREAD) == 0) {
3816                 fdrop(fp, td);
3817                 return (EBADF);
3818         }
3819         vp = (struct vnode *)fp->f_data;
3820 unionread:
3821         if (vp->v_type != VDIR) {
3822                 fdrop(fp, td);
3823                 return (EINVAL);
3824         }
3825         aiov.iov_base = SCARG(uap, buf);
3826         aiov.iov_len = SCARG(uap, count);
3827         auio.uio_iov = &aiov;
3828         auio.uio_iovcnt = 1;
3829         auio.uio_rw = UIO_READ;
3830         auio.uio_segflg = UIO_USERSPACE;
3831         auio.uio_td = td;
3832         auio.uio_resid = SCARG(uap, count);
3833         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3834         loff = auio.uio_offset = fp->f_offset;
3835 #       if (BYTE_ORDER != LITTLE_ENDIAN)
3836                 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3837                         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3838                             NULL, NULL);
3839                         fp->f_offset = auio.uio_offset;
3840                 } else
3841 #       endif
3842         {
3843                 kuio = auio;
3844                 kuio.uio_iov = &kiov;
3845                 kuio.uio_segflg = UIO_SYSSPACE;
3846                 kiov.iov_len = SCARG(uap, count);
3847                 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
3848                 kiov.iov_base = dirbuf;
3849                 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3850                             NULL, NULL);
3851                 fp->f_offset = kuio.uio_offset;
3852                 if (error == 0) {
3853                         readcnt = SCARG(uap, count) - kuio.uio_resid;
3854                         edp = (struct dirent *)&dirbuf[readcnt];
3855                         for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3856 #                               if (BYTE_ORDER == LITTLE_ENDIAN)
3857                                         /*
3858                                          * The expected low byte of
3859                                          * dp->d_namlen is our dp->d_type.
3860                                          * The high MBZ byte of dp->d_namlen
3861                                          * is our dp->d_namlen.
3862                                          */
3863                                         dp->d_type = dp->d_namlen;
3864                                         dp->d_namlen = 0;
3865 #                               else
3866                                         /*
3867                                          * The dp->d_type is the high byte
3868                                          * of the expected dp->d_namlen,
3869                                          * so must be zero'ed.
3870                                          */
3871                                         dp->d_type = 0;
3872 #                               endif
3873                                 if (dp->d_reclen > 0) {
3874                                         dp = (struct dirent *)
3875                                             ((char *)dp + dp->d_reclen);
3876                                 } else {
3877                                         error = EIO;
3878                                         break;
3879                                 }
3880                         }
3881                         if (dp >= edp)
3882                                 error = uiomove(dirbuf, readcnt, &auio);
3883                 }
3884                 FREE(dirbuf, M_TEMP);
3885         }
3886         VOP_UNLOCK(vp, 0, td);
3887         if (error) {
3888                 fdrop(fp, td);
3889                 return (error);
3890         }
3891         if (SCARG(uap, count) == auio.uio_resid) {
3892                 if (union_dircheckp) {
3893                         error = union_dircheckp(td, &vp, fp);
3894                         if (error == -1)
3895                                 goto unionread;
3896                         if (error) {
3897                                 fdrop(fp, td);
3898                                 return (error);
3899                         }
3900                 }
3901                 if ((vp->v_flag & VROOT) &&
3902                     (vp->v_mount->mnt_flag & MNT_UNION)) {
3903                         struct vnode *tvp = vp;
3904                         vp = vp->v_mount->mnt_vnodecovered;
3905                         VREF(vp);
3906                         fp->f_data = (caddr_t) vp;
3907                         fp->f_offset = 0;
3908                         vrele(tvp);
3909                         goto unionread;
3910                 }
3911         }
3912         error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
3913             sizeof(long));
3914         fdrop(fp, td);
3915         td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
3916         return (error);
3917 }
3918 #endif /* COMPAT_43 */
3919
3920 /*
3921  * Read a block of directory entries in a file system independent format.
3922  */
3923 #ifndef _SYS_SYSPROTO_H_
3924 struct getdirentries_args {
3925         int     fd;
3926         char    *buf;
3927         u_int   count;
3928         long    *basep;
3929 };
3930 #endif
3931 int
3932 getdirentries(td, uap)
3933         struct thread *td;
3934         register struct getdirentries_args /* {
3935                 syscallarg(int) fd;
3936                 syscallarg(char *) buf;
3937                 syscallarg(u_int) count;
3938                 syscallarg(long *) basep;
3939         } */ *uap;
3940 {
3941         struct vnode *vp;
3942         struct file *fp;
3943         struct uio auio;
3944         struct iovec aiov;
3945         long loff;
3946         int error, eofflag;
3947
3948         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
3949                 return (error);
3950         if ((fp->f_flag & FREAD) == 0) {
3951                 fdrop(fp, td);
3952                 return (EBADF);
3953         }
3954         vp = (struct vnode *)fp->f_data;
3955 unionread:
3956         if (vp->v_type != VDIR) {
3957                 fdrop(fp, td);
3958                 return (EINVAL);
3959         }
3960         aiov.iov_base = SCARG(uap, buf);
3961         aiov.iov_len = SCARG(uap, count);
3962         auio.uio_iov = &aiov;
3963         auio.uio_iovcnt = 1;
3964         auio.uio_rw = UIO_READ;
3965         auio.uio_segflg = UIO_USERSPACE;
3966         auio.uio_td = td;
3967         auio.uio_resid = SCARG(uap, count);
3968         /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3969         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3970         loff = auio.uio_offset = fp->f_offset;
3971         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3972         fp->f_offset = auio.uio_offset;
3973         VOP_UNLOCK(vp, 0, td);
3974         if (error) {
3975                 fdrop(fp, td);
3976                 return (error);
3977         }
3978         if (SCARG(uap, count) == auio.uio_resid) {
3979                 if (union_dircheckp) {
3980                         error = union_dircheckp(td, &vp, fp);
3981                         if (error == -1)
3982                                 goto unionread;
3983                         if (error) {
3984                                 fdrop(fp, td);
3985                                 return (error);
3986                         }
3987                 }
3988                 if ((vp->v_flag & VROOT) &&
3989                     (vp->v_mount->mnt_flag & MNT_UNION)) {
3990                         struct vnode *tvp = vp;
3991                         vp = vp->v_mount->mnt_vnodecovered;
3992                         VREF(vp);
3993                         fp->f_data = (caddr_t) vp;
3994                         fp->f_offset = 0;
3995                         vrele(tvp);
3996                         goto unionread;
3997                 }
3998         }
3999         if (SCARG(uap, basep) != NULL) {
4000                 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
4001                     sizeof(long));
4002         }
4003         td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
4004         fdrop(fp, td);
4005         return (error);
4006 }
4007 #ifndef _SYS_SYSPROTO_H_
4008 struct getdents_args {
4009         int fd;
4010         char *buf;
4011         size_t count;
4012 };
4013 #endif
4014 int
4015 getdents(td, uap)
4016         struct thread *td;
4017         register struct getdents_args /* {
4018                 syscallarg(int) fd;
4019                 syscallarg(char *) buf;
4020                 syscallarg(u_int) count;
4021         } */ *uap;
4022 {
4023         struct getdirentries_args ap;
4024         ap.fd = uap->fd;
4025         ap.buf = uap->buf;
4026         ap.count = uap->count;
4027         ap.basep = NULL;
4028         return getdirentries(td, &ap);
4029 }
4030
4031 /*
4032  * Set the mode mask for creation of filesystem nodes.
4033  *
4034  * MP SAFE
4035  */
4036 #ifndef _SYS_SYSPROTO_H_
4037 struct umask_args {
4038         int     newmask;
4039 };
4040 #endif
4041 int
4042 umask(td, uap)
4043         struct thread *td;
4044         struct umask_args /* {
4045                 syscallarg(int) newmask;
4046         } */ *uap;
4047 {
4048         register struct filedesc *fdp;
4049
4050         FILEDESC_LOCK(td->td_proc->p_fd);
4051         fdp = td->td_proc->p_fd;
4052         td->td_retval[0] = fdp->fd_cmask;
4053         fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
4054         FILEDESC_UNLOCK(td->td_proc->p_fd);
4055         return (0);
4056 }
4057
4058 /*
4059  * Void all references to file by ripping underlying filesystem
4060  * away from vnode.
4061  */
4062 #ifndef _SYS_SYSPROTO_H_
4063 struct revoke_args {
4064         char    *path;
4065 };
4066 #endif
4067 /* ARGSUSED */
4068 int
4069 revoke(td, uap)
4070         struct thread *td;
4071         register struct revoke_args /* {
4072                 syscallarg(char *) path;
4073         } */ *uap;
4074 {
4075         struct mount *mp;
4076         struct vnode *vp;
4077         struct vattr vattr;
4078         int error;
4079         struct nameidata nd;
4080
4081         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, SCARG(uap, path),
4082             td);
4083         if ((error = namei(&nd)) != 0)
4084                 return (error);
4085         vp = nd.ni_vp;
4086         NDFREE(&nd, NDF_ONLY_PNBUF);
4087         if (vp->v_type != VCHR) {
4088                 vput(vp);
4089                 return (EINVAL);
4090         }
4091         error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
4092         if (error) {
4093                 vput(vp);
4094                 return (error);
4095         }
4096         VOP_UNLOCK(vp, 0, td);
4097         if (td->td_ucred->cr_uid != vattr.va_uid) {
4098                 error = suser_cred(td->td_ucred, PRISON_ROOT);
4099                 if (error)
4100                         goto out;
4101         }
4102         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
4103                 goto out;
4104         if (vcount(vp) > 1)
4105                 VOP_REVOKE(vp, REVOKEALL);
4106         vn_finished_write(mp);
4107 out:
4108         vrele(vp);
4109         return (error);
4110 }
4111
4112 /*
4113  * Convert a user file descriptor to a kernel file entry.
4114  * The file entry is locked upon returning.
4115  */
4116 int
4117 getvnode(fdp, fd, fpp)
4118         struct filedesc *fdp;
4119         int fd;
4120         struct file **fpp;
4121 {
4122         int error;
4123         struct file *fp;
4124
4125         fp = NULL;
4126         if (fdp == NULL)
4127                 error = EBADF;
4128         else {
4129                 FILEDESC_LOCK(fdp);
4130                 if ((u_int)fd >= fdp->fd_nfiles ||
4131                     (fp = fdp->fd_ofiles[fd]) == NULL)
4132                         error = EBADF;
4133                 else if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
4134                         fp = NULL;
4135                         error = EINVAL;
4136                 } else {
4137                         fhold(fp);
4138                         error = 0;
4139                 }
4140                 FILEDESC_UNLOCK(fdp);
4141         }
4142         *fpp = fp;
4143         return (error);
4144 }
4145 /*
4146  * Get (NFS) file handle
4147  */
4148 #ifndef _SYS_SYSPROTO_H_
4149 struct getfh_args {
4150         char    *fname;
4151         fhandle_t *fhp;
4152 };
4153 #endif
4154 int
4155 getfh(td, uap)
4156         struct thread *td;
4157         register struct getfh_args *uap;
4158 {
4159         struct nameidata nd;
4160         fhandle_t fh;
4161         register struct vnode *vp;
4162         int error;
4163
4164         /*
4165          * Must be super user
4166          */
4167         error = suser(td);
4168         if (error)
4169                 return (error);
4170         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
4171         error = namei(&nd);
4172         if (error)
4173                 return (error);
4174         NDFREE(&nd, NDF_ONLY_PNBUF);
4175         vp = nd.ni_vp;
4176         bzero(&fh, sizeof(fh));
4177         fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4178         error = VFS_VPTOFH(vp, &fh.fh_fid);
4179         vput(vp);
4180         if (error)
4181                 return (error);
4182         error = copyout(&fh, uap->fhp, sizeof (fh));
4183         return (error);
4184 }
4185
4186 /*
4187  * syscall for the rpc.lockd to use to translate a NFS file handle into
4188  * an open descriptor.
4189  *
4190  * warning: do not remove the suser() call or this becomes one giant
4191  * security hole.
4192  */
4193 #ifndef _SYS_SYSPROTO_H_
4194 struct fhopen_args {
4195         const struct fhandle *u_fhp;
4196         int flags;
4197 };
4198 #endif
4199 int
4200 fhopen(td, uap)
4201         struct thread *td;
4202         struct fhopen_args /* {
4203                 syscallarg(const struct fhandle *) u_fhp;
4204                 syscallarg(int) flags;
4205         } */ *uap;
4206 {
4207         struct proc *p = td->td_proc;
4208         struct mount *mp;
4209         struct vnode *vp;
4210         struct fhandle fhp;
4211         struct vattr vat;
4212         struct vattr *vap = &vat;
4213         struct flock lf;
4214         struct file *fp;
4215         register struct filedesc *fdp = p->p_fd;
4216         int fmode, mode, error, type;
4217         struct file *nfp; 
4218         int indx;
4219
4220         /*
4221          * Must be super user
4222          */
4223         error = suser(td);
4224         if (error)
4225                 return (error);
4226
4227         fmode = FFLAGS(SCARG(uap, flags));
4228         /* why not allow a non-read/write open for our lockd? */
4229         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4230                 return (EINVAL);
4231         error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
4232         if (error)
4233                 return(error);
4234         /* find the mount point */
4235         mp = vfs_getvfs(&fhp.fh_fsid);
4236         if (mp == NULL)
4237                 return (ESTALE);
4238         /* now give me my vnode, it gets returned to me locked */
4239         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4240         if (error)
4241                 return (error);
4242         /*
4243          * from now on we have to make sure not
4244          * to forget about the vnode
4245          * any error that causes an abort must vput(vp) 
4246          * just set error = err and 'goto bad;'.
4247          */
4248
4249         /* 
4250          * from vn_open 
4251          */
4252         if (vp->v_type == VLNK) {
4253                 error = EMLINK;
4254                 goto bad;
4255         }
4256         if (vp->v_type == VSOCK) {
4257                 error = EOPNOTSUPP;
4258                 goto bad;
4259         }
4260         mode = 0;
4261         if (fmode & (FWRITE | O_TRUNC)) {
4262                 if (vp->v_type == VDIR) {
4263                         error = EISDIR;
4264                         goto bad;
4265                 }
4266                 error = vn_writechk(vp);
4267                 if (error)
4268                         goto bad;
4269                 mode |= VWRITE;
4270         }
4271         if (fmode & FREAD)
4272                 mode |= VREAD;
4273         if (mode) {
4274                 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4275                 if (error)
4276                         goto bad;
4277         }
4278         if (fmode & O_TRUNC) {
4279                 VOP_UNLOCK(vp, 0, td);                          /* XXX */
4280                 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4281                         vrele(vp);
4282                         return (error);
4283                 }
4284                 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4285                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);       /* XXX */
4286                 VATTR_NULL(vap);
4287                 vap->va_size = 0;
4288                 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4289                 vn_finished_write(mp);
4290                 if (error)
4291                         goto bad;
4292         }
4293         error = VOP_OPEN(vp, fmode, td->td_ucred, td);
4294         if (error)
4295                 goto bad;
4296         /*
4297          * Make sure that a VM object is created for VMIO support.
4298          */
4299         if (vn_canvmio(vp) == TRUE) {
4300                 if ((error = vfs_object_create(vp, td, td->td_ucred)) != 0)
4301                         goto bad;
4302         }
4303         if (fmode & FWRITE)
4304                 vp->v_writecount++;
4305
4306         /*
4307          * end of vn_open code 
4308          */
4309
4310         if ((error = falloc(td, &nfp, &indx)) != 0) {
4311                 if (fmode & FWRITE)
4312                         vp->v_writecount--;
4313                 goto bad;
4314         }
4315         fp = nfp;       
4316
4317         /*
4318          * Hold an extra reference to avoid having fp ripped out 
4319          * from under us while we block in the lock op
4320          */
4321         fhold(fp);
4322         nfp->f_data = (caddr_t)vp;
4323         nfp->f_flag = fmode & FMASK;
4324         nfp->f_ops = &vnops;
4325         nfp->f_type = DTYPE_VNODE;
4326         if (fmode & (O_EXLOCK | O_SHLOCK)) {
4327                 lf.l_whence = SEEK_SET;
4328                 lf.l_start = 0;
4329                 lf.l_len = 0;
4330                 if (fmode & O_EXLOCK)
4331                         lf.l_type = F_WRLCK;
4332                 else
4333                         lf.l_type = F_RDLCK;
4334                 type = F_FLOCK;
4335                 if ((fmode & FNONBLOCK) == 0)
4336                         type |= F_WAIT;
4337                 VOP_UNLOCK(vp, 0, td);
4338                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4339                         /*
4340                          * The lock request failed.  Normally close the
4341                          * descriptor but handle the case where someone might
4342                          * have dup()d or close()d it when we weren't looking.
4343                          */
4344                         FILEDESC_LOCK(fdp);
4345                         if (fdp->fd_ofiles[indx] == fp) {
4346                                 fdp->fd_ofiles[indx] = NULL;
4347                                 FILEDESC_UNLOCK(fdp);
4348                                 fdrop(fp, td);
4349                         } else
4350                                 FILEDESC_UNLOCK(fdp);
4351                         /*
4352                          * release our private reference
4353                          */
4354                         fdrop(fp, td);
4355                         return(error);
4356                 }
4357                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4358                 fp->f_flag |= FHASLOCK;
4359         }
4360         if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
4361                 vfs_object_create(vp, td, td->td_ucred);
4362
4363         VOP_UNLOCK(vp, 0, td);
4364         fdrop(fp, td);
4365         td->td_retval[0] = indx;
4366         return (0);
4367
4368 bad:
4369         vput(vp);
4370         return (error);
4371 }
4372
4373 /*
4374  * Stat an (NFS) file handle.
4375  */
4376 #ifndef _SYS_SYSPROTO_H_
4377 struct fhstat_args {
4378         struct fhandle *u_fhp;
4379         struct stat *sb;
4380 };
4381 #endif
4382 int
4383 fhstat(td, uap)
4384         struct thread *td;
4385         register struct fhstat_args /* {
4386                 syscallarg(struct fhandle *) u_fhp;
4387                 syscallarg(struct stat *) sb;
4388         } */ *uap;
4389 {
4390         struct stat sb;
4391         fhandle_t fh;
4392         struct mount *mp;
4393         struct vnode *vp;
4394         int error;
4395
4396         /*
4397          * Must be super user
4398          */
4399         error = suser(td);
4400         if (error)
4401                 return (error);
4402         
4403         error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
4404         if (error)
4405                 return (error);
4406
4407         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4408                 return (ESTALE);
4409         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
4410                 return (error);
4411         error = vn_stat(vp, &sb, td);
4412         vput(vp);
4413         if (error)
4414                 return (error);
4415         error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
4416         return (error);
4417 }
4418
4419 /*
4420  * Implement fstatfs() for (NFS) file handles.
4421  */
4422 #ifndef _SYS_SYSPROTO_H_
4423 struct fhstatfs_args {
4424         struct fhandle *u_fhp;
4425         struct statfs *buf;
4426 };
4427 #endif
4428 int
4429 fhstatfs(td, uap)
4430         struct thread *td;
4431         struct fhstatfs_args /* {
4432                 syscallarg(struct fhandle) *u_fhp;
4433                 syscallarg(struct statfs) *buf;
4434         } */ *uap;
4435 {
4436         struct statfs *sp;
4437         struct mount *mp;
4438         struct vnode *vp;
4439         struct statfs sb;
4440         fhandle_t fh;
4441         int error;
4442
4443         /*
4444          * Must be super user
4445          */
4446         error = suser(td);
4447         if (error)
4448                 return (error);
4449
4450         if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
4451                 return (error);
4452
4453         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4454                 return (ESTALE);
4455         if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
4456                 return (error);
4457         mp = vp->v_mount;
4458         sp = &mp->mnt_stat;
4459         vput(vp);
4460         if ((error = VFS_STATFS(mp, sp, td)) != 0)
4461                 return (error);
4462         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4463         if (suser(td)) {
4464                 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
4465                 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4466                 sp = &sb;
4467         }
4468         return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
4469 }
4470
4471 /*
4472  * Syscall to push extended attribute configuration information into the
4473  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
4474  * a command (int cmd), and attribute name and misc data.  For now, the
4475  * attribute name is left in userspace for consumption by the VFS_op.
4476  * It will probably be changed to be copied into sysspace by the
4477  * syscall in the future, once issues with various consumers of the
4478  * attribute code have raised their hands.
4479  *
4480  * Currently this is used only by UFS Extended Attributes.
4481  */
4482 int
4483 extattrctl(td, uap)
4484         struct thread *td;
4485         struct extattrctl_args *uap;
4486 {
4487         struct vnode *filename_vp;
4488         struct nameidata nd;
4489         struct mount *mp, *mp_writable;
4490         char attrname[EXTATTR_MAXNAMELEN];
4491         int error;
4492
4493         /*
4494          * SCARG(uap, attrname) not always defined.  We check again later
4495          * when we invoke the VFS call so as to pass in NULL there if needed.
4496          */
4497         if (SCARG(uap, attrname) != NULL) {
4498                 error = copyinstr(SCARG(uap, attrname), attrname,
4499                     EXTATTR_MAXNAMELEN, NULL);
4500                 if (error)
4501                         return (error);
4502         }
4503
4504         /*
4505          * SCARG(uap, filename) not always defined.  If it is, grab
4506          * a vnode lock, which VFS_EXTATTRCTL() will later release.
4507          */
4508         filename_vp = NULL;
4509         if (SCARG(uap, filename) != NULL) {
4510                 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
4511                     SCARG(uap, filename), td);
4512                 if ((error = namei(&nd)) != 0)
4513                         return (error);
4514                 filename_vp = nd.ni_vp;
4515                 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
4516         }
4517
4518         /* SCARG(uap, path) always defined. */
4519         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
4520         if ((error = namei(&nd)) != 0) {
4521                 if (filename_vp != NULL)
4522                         vput(filename_vp);
4523                 return (error);
4524         }
4525         mp = nd.ni_vp->v_mount;
4526         error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
4527         NDFREE(&nd, 0);
4528         if (error) {
4529                 if (filename_vp != NULL)
4530                         vput(filename_vp);
4531                 return (error);
4532         }
4533
4534         if (SCARG(uap, attrname) != NULL) {
4535                 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd), filename_vp,
4536                     SCARG(uap, attrnamespace), attrname, td);
4537         } else {
4538                 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd), filename_vp,
4539                     SCARG(uap, attrnamespace), NULL, td);
4540         }
4541
4542         vn_finished_write(mp_writable);
4543         /*
4544          * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
4545          * filename_vp, so vrele it if it is defined.
4546          */
4547         if (filename_vp != NULL)
4548                 vrele(filename_vp);
4549
4550         return (error);
4551 }
4552
4553 /*-
4554  * Set a named extended attribute on a file or directory
4555  * 
4556  * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4557  *            kernelspace string pointer "attrname", userspace buffer
4558  *            pointer "data", buffer length "nbytes", thread "td".
4559  * Returns: 0 on success, an error number otherwise
4560  * Locks: none
4561  * References: vp must be a valid reference for the duration of the call
4562  */
4563 static int
4564 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4565     void *data, size_t nbytes, struct thread *td)
4566 {
4567         struct mount *mp;
4568         struct uio auio;
4569         struct iovec aiov;
4570         ssize_t cnt;
4571         int error;
4572
4573         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
4574                 return (error);
4575         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4576         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4577
4578         aiov.iov_base = data;
4579         aiov.iov_len = nbytes;
4580         auio.uio_iov = &aiov;
4581         auio.uio_iovcnt = 1;
4582         auio.uio_offset = 0;
4583         if (nbytes > INT_MAX) {
4584                 error = EINVAL;
4585                 goto done;
4586         }
4587         auio.uio_resid = nbytes;
4588         auio.uio_rw = UIO_WRITE;
4589         auio.uio_segflg = UIO_USERSPACE;
4590         auio.uio_td = td;
4591         cnt = nbytes;
4592
4593         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
4594             td->td_ucred, td);
4595         cnt -= auio.uio_resid;
4596         td->td_retval[0] = cnt;
4597
4598 done:
4599         VOP_UNLOCK(vp, 0, td);
4600         vn_finished_write(mp);
4601         return (error);
4602 }
4603
4604 int
4605 extattr_set_file(td, uap)
4606         struct thread *td;
4607         struct extattr_set_file_args *uap;
4608 {
4609         struct nameidata nd;
4610         char attrname[EXTATTR_MAXNAMELEN];
4611         int error;
4612
4613         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4614             NULL);
4615         if (error)
4616                 return (error);
4617
4618         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
4619         if ((error = namei(&nd)) != 0)
4620                 return (error);
4621         NDFREE(&nd, NDF_ONLY_PNBUF);
4622
4623         error = extattr_set_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
4624             SCARG(uap, data), SCARG(uap, nbytes), td);
4625
4626         vrele(nd.ni_vp);
4627         return (error);
4628 }
4629
4630 int
4631 extattr_set_fd(td, uap)
4632         struct thread *td;
4633         struct extattr_set_fd_args *uap;
4634 {
4635         struct file *fp;
4636         char attrname[EXTATTR_MAXNAMELEN];
4637         int error;
4638
4639         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4640             NULL);
4641         if (error)
4642                 return (error);
4643
4644         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
4645                 return (error);
4646
4647         error = extattr_set_vp((struct vnode *)fp->f_data,
4648             SCARG(uap, attrnamespace), attrname, SCARG(uap, data),
4649             SCARG(uap, nbytes), td);
4650         fdrop(fp, td);
4651
4652         return (error);
4653 }
4654
4655 /*-
4656  * Get a named extended attribute on a file or directory
4657  * 
4658  * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4659  *            kernelspace string pointer "attrname", userspace buffer
4660  *            pointer "data", buffer length "nbytes", thread "td".
4661  * Returns: 0 on success, an error number otherwise
4662  * Locks: none
4663  * References: vp must be a valid reference for the duration of the call
4664  */
4665 static int
4666 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4667     void *data, size_t nbytes, struct thread *td)
4668 {
4669         struct uio auio;
4670         struct iovec aiov;
4671         ssize_t cnt;
4672         size_t size;
4673         int error;
4674
4675         VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4676         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4677
4678         /*
4679          * Slightly unusual semantics: if the user provides a NULL data
4680          * pointer, they don't want to receive the data, just the
4681          * maximum read length.
4682          */
4683         if (data != NULL) {
4684                 aiov.iov_base = data;
4685                 aiov.iov_len = nbytes;
4686                 auio.uio_iov = &aiov;
4687                 auio.uio_offset = 0;
4688                 if (nbytes > INT_MAX) {
4689                         error = EINVAL;
4690                         goto done;
4691                 }
4692                 auio.uio_resid = nbytes;
4693                 auio.uio_rw = UIO_READ;
4694                 auio.uio_segflg = UIO_USERSPACE;
4695                 auio.uio_td = td;
4696                 cnt = nbytes;
4697                 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio,
4698                     NULL, td->td_ucred, td);
4699                 cnt -= auio.uio_resid;
4700                 td->td_retval[0] = cnt;
4701         } else {
4702                 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, NULL,
4703                     &size, td->td_ucred, td);
4704                 td->td_retval[0] = size;
4705         }
4706 done:
4707         VOP_UNLOCK(vp, 0, td);
4708         return (error);
4709 }
4710
4711 int
4712 extattr_get_file(td, uap)
4713         struct thread *td;
4714         struct extattr_get_file_args *uap;
4715 {
4716         struct nameidata nd;
4717         char attrname[EXTATTR_MAXNAMELEN];
4718         int error;
4719
4720         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4721             NULL);
4722         if (error)
4723                 return (error);
4724
4725         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
4726         if ((error = namei(&nd)) != 0)
4727                 return (error);
4728         NDFREE(&nd, NDF_ONLY_PNBUF);
4729
4730         error = extattr_get_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
4731             SCARG(uap, data), SCARG(uap, nbytes), td);
4732
4733         vrele(nd.ni_vp);
4734         return (error);
4735 }
4736
4737 int
4738 extattr_get_fd(td, uap)
4739         struct thread *td;
4740         struct extattr_get_fd_args *uap;
4741 {
4742         struct file *fp;
4743         char attrname[EXTATTR_MAXNAMELEN];
4744         int error;
4745
4746         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4747             NULL);
4748         if (error)
4749                 return (error);
4750
4751         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
4752                 return (error);
4753
4754         error = extattr_get_vp((struct vnode *)fp->f_data,
4755             SCARG(uap, attrnamespace), attrname, SCARG(uap, data),
4756             SCARG(uap, nbytes), td);
4757
4758         fdrop(fp, td);
4759         return (error);
4760 }
4761
4762 /*
4763  * extattr_delete_vp(): Delete a named extended attribute on a file or
4764  *                      directory
4765  * 
4766  * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4767  *            kernelspace string pointer "attrname", proc "p"
4768  * Returns: 0 on success, an error number otherwise
4769  * Locks: none
4770  * References: vp must be a valid reference for the duration of the call
4771  */
4772 static int
4773 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4774     struct thread *td)
4775 {
4776         struct mount *mp;
4777         int error;
4778
4779         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
4780                 return (error);
4781         VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4782         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4783
4784         error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, td->td_ucred,
4785             td);
4786
4787         VOP_UNLOCK(vp, 0, td);
4788         vn_finished_write(mp);
4789         return (error);
4790 }
4791
4792 int
4793 extattr_delete_file(td, uap)
4794         struct thread *td;
4795         struct extattr_delete_file_args *uap;
4796 {
4797         struct nameidata nd;
4798         char attrname[EXTATTR_MAXNAMELEN];
4799         int error;
4800
4801         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4802              NULL);
4803         if (error)
4804                 return(error);
4805
4806         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
4807         if ((error = namei(&nd)) != 0)
4808                 return(error);
4809         NDFREE(&nd, NDF_ONLY_PNBUF);
4810
4811         error = extattr_delete_vp(nd.ni_vp, SCARG(uap, attrnamespace),
4812             attrname, td);
4813
4814         vrele(nd.ni_vp);
4815         return(error);
4816 }
4817
4818 int
4819 extattr_delete_fd(td, uap)
4820         struct thread *td;
4821         struct extattr_delete_fd_args *uap;
4822 {
4823         struct file *fp;
4824         struct vnode *vp;
4825         char attrname[EXTATTR_MAXNAMELEN];
4826         int error;
4827
4828         error = copyinstr(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN,
4829             NULL);
4830         if (error)
4831                 return (error);
4832
4833         if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
4834                 return (error);
4835         vp = (struct vnode *)fp->f_data;
4836
4837         error = extattr_delete_vp((struct vnode *)fp->f_data,
4838             SCARG(uap, attrnamespace), attrname, td);
4839
4840         fdrop(fp, td);
4841         return (error);
4842 }