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