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