]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/vfs_syscalls.c
MFC r206547:
[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         int cmode;
1051         struct file *nfp;
1052         int type, indx, error;
1053         struct flock lf;
1054         struct nameidata nd;
1055         int vfslocked;
1056
1057         AUDIT_ARG_FFLAGS(flags);
1058         AUDIT_ARG_MODE(mode);
1059         /* XXX: audit dirfd */
1060         /*
1061          * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1062          * be specified.
1063          */
1064         if (flags & O_EXEC) {
1065                 if (flags & O_ACCMODE)
1066                         return (EINVAL);
1067         } else if ((flags & O_ACCMODE) == O_ACCMODE)
1068                 return (EINVAL);
1069         else
1070                 flags = FFLAGS(flags);
1071
1072         error = falloc(td, &nfp, &indx);
1073         if (error)
1074                 return (error);
1075         /* An extra reference on `nfp' has been held for us by falloc(). */
1076         fp = nfp;
1077         /* Set the flags early so the finit in devfs can pick them up. */
1078         fp->f_flag = flags & FMASK;
1079         cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1080         NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1081             td);
1082         td->td_dupfd = -1;              /* XXX check for fdopen */
1083         error = vn_open(&nd, &flags, cmode, fp);
1084         if (error) {
1085                 /*
1086                  * If the vn_open replaced the method vector, something
1087                  * wonderous happened deep below and we just pass it up
1088                  * pretending we know what we do.
1089                  */
1090                 if (error == ENXIO && fp->f_ops != &badfileops) {
1091                         fdrop(fp, td);
1092                         td->td_retval[0] = indx;
1093                         return (0);
1094                 }
1095
1096                 /*
1097                  * handle special fdopen() case.  bleh.  dupfdopen() is
1098                  * responsible for dropping the old contents of ofiles[indx]
1099                  * if it succeeds.
1100                  */
1101                 if ((error == ENODEV || error == ENXIO) &&
1102                     td->td_dupfd >= 0 &&                /* XXX from fdopen */
1103                     (error =
1104                         dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1105                         td->td_retval[0] = indx;
1106                         fdrop(fp, td);
1107                         return (0);
1108                 }
1109                 /*
1110                  * Clean up the descriptor, but only if another thread hadn't
1111                  * replaced or closed it.
1112                  */
1113                 fdclose(fdp, fp, indx, td);
1114                 fdrop(fp, td);
1115
1116                 if (error == ERESTART)
1117                         error = EINTR;
1118                 return (error);
1119         }
1120         td->td_dupfd = 0;
1121         vfslocked = NDHASGIANT(&nd);
1122         NDFREE(&nd, NDF_ONLY_PNBUF);
1123         vp = nd.ni_vp;
1124
1125         /*
1126          * Store the vnode, for any f_type. Typically, the vnode use
1127          * count is decremented by direct call to vn_closefile() for
1128          * files that switched type in the cdevsw fdopen() method.
1129          */
1130         fp->f_vnode = vp;
1131         /*
1132          * If the file wasn't claimed by devfs bind it to the normal
1133          * vnode operations here.
1134          */
1135         if (fp->f_ops == &badfileops) {
1136                 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1137                 fp->f_seqcount = 1;
1138                 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1139         }
1140
1141         VOP_UNLOCK(vp, 0);
1142         if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1143                 lf.l_whence = SEEK_SET;
1144                 lf.l_start = 0;
1145                 lf.l_len = 0;
1146                 if (flags & O_EXLOCK)
1147                         lf.l_type = F_WRLCK;
1148                 else
1149                         lf.l_type = F_RDLCK;
1150                 type = F_FLOCK;
1151                 if ((flags & FNONBLOCK) == 0)
1152                         type |= F_WAIT;
1153                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1154                             type)) != 0)
1155                         goto bad;
1156                 atomic_set_int(&fp->f_flag, FHASLOCK);
1157         }
1158         if (flags & O_TRUNC) {
1159                 error = fo_truncate(fp, 0, td->td_ucred, td);
1160                 if (error)
1161                         goto bad;
1162         }
1163         VFS_UNLOCK_GIANT(vfslocked);
1164         /*
1165          * Release our private reference, leaving the one associated with
1166          * the descriptor table intact.
1167          */
1168         fdrop(fp, td);
1169         td->td_retval[0] = indx;
1170         return (0);
1171 bad:
1172         VFS_UNLOCK_GIANT(vfslocked);
1173         fdclose(fdp, fp, indx, td);
1174         fdrop(fp, td);
1175         return (error);
1176 }
1177
1178 #ifdef COMPAT_43
1179 /*
1180  * Create a file.
1181  */
1182 #ifndef _SYS_SYSPROTO_H_
1183 struct ocreat_args {
1184         char    *path;
1185         int     mode;
1186 };
1187 #endif
1188 int
1189 ocreat(td, uap)
1190         struct thread *td;
1191         register struct ocreat_args /* {
1192                 char *path;
1193                 int mode;
1194         } */ *uap;
1195 {
1196
1197         return (kern_open(td, uap->path, UIO_USERSPACE,
1198             O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1199 }
1200 #endif /* COMPAT_43 */
1201
1202 /*
1203  * Create a special file.
1204  */
1205 #ifndef _SYS_SYSPROTO_H_
1206 struct mknod_args {
1207         char    *path;
1208         int     mode;
1209         int     dev;
1210 };
1211 #endif
1212 int
1213 mknod(td, uap)
1214         struct thread *td;
1215         register struct mknod_args /* {
1216                 char *path;
1217                 int mode;
1218                 int dev;
1219         } */ *uap;
1220 {
1221
1222         return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1223 }
1224
1225 #ifndef _SYS_SYSPROTO_H_
1226 struct mknodat_args {
1227         int     fd;
1228         char    *path;
1229         mode_t  mode;
1230         dev_t   dev;
1231 };
1232 #endif
1233 int
1234 mknodat(struct thread *td, struct mknodat_args *uap)
1235 {
1236
1237         return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1238             uap->dev));
1239 }
1240
1241 int
1242 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1243     int dev)
1244 {
1245
1246         return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1247 }
1248
1249 int
1250 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1251     int mode, int dev)
1252 {
1253         struct vnode *vp;
1254         struct mount *mp;
1255         struct vattr vattr;
1256         int error;
1257         int whiteout = 0;
1258         struct nameidata nd;
1259         int vfslocked;
1260
1261         AUDIT_ARG_MODE(mode);
1262         AUDIT_ARG_DEV(dev);
1263         switch (mode & S_IFMT) {
1264         case S_IFCHR:
1265         case S_IFBLK:
1266                 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1267                 break;
1268         case S_IFMT:
1269                 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1270                 break;
1271         case S_IFWHT:
1272                 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1273                 break;
1274         case S_IFIFO:
1275                 if (dev == 0)
1276                         return (kern_mkfifoat(td, fd, path, pathseg, mode));
1277                 /* FALLTHROUGH */
1278         default:
1279                 error = EINVAL;
1280                 break;
1281         }
1282         if (error)
1283                 return (error);
1284 restart:
1285         bwillwrite();
1286         NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1287             pathseg, path, fd, td);
1288         if ((error = namei(&nd)) != 0)
1289                 return (error);
1290         vfslocked = NDHASGIANT(&nd);
1291         vp = nd.ni_vp;
1292         if (vp != NULL) {
1293                 NDFREE(&nd, NDF_ONLY_PNBUF);
1294                 if (vp == nd.ni_dvp)
1295                         vrele(nd.ni_dvp);
1296                 else
1297                         vput(nd.ni_dvp);
1298                 vrele(vp);
1299                 VFS_UNLOCK_GIANT(vfslocked);
1300                 return (EEXIST);
1301         } else {
1302                 VATTR_NULL(&vattr);
1303                 vattr.va_mode = (mode & ALLPERMS) &
1304                     ~td->td_proc->p_fd->fd_cmask;
1305                 vattr.va_rdev = dev;
1306                 whiteout = 0;
1307
1308                 switch (mode & S_IFMT) {
1309                 case S_IFMT:    /* used by badsect to flag bad sectors */
1310                         vattr.va_type = VBAD;
1311                         break;
1312                 case S_IFCHR:
1313                         vattr.va_type = VCHR;
1314                         break;
1315                 case S_IFBLK:
1316                         vattr.va_type = VBLK;
1317                         break;
1318                 case S_IFWHT:
1319                         whiteout = 1;
1320                         break;
1321                 default:
1322                         panic("kern_mknod: invalid mode");
1323                 }
1324         }
1325         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1326                 NDFREE(&nd, NDF_ONLY_PNBUF);
1327                 vput(nd.ni_dvp);
1328                 VFS_UNLOCK_GIANT(vfslocked);
1329                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1330                         return (error);
1331                 goto restart;
1332         }
1333 #ifdef MAC
1334         if (error == 0 && !whiteout)
1335                 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1336                     &nd.ni_cnd, &vattr);
1337 #endif
1338         if (!error) {
1339                 if (whiteout)
1340                         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1341                 else {
1342                         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1343                                                 &nd.ni_cnd, &vattr);
1344                         if (error == 0)
1345                                 vput(nd.ni_vp);
1346                 }
1347         }
1348         NDFREE(&nd, NDF_ONLY_PNBUF);
1349         vput(nd.ni_dvp);
1350         vn_finished_write(mp);
1351         VFS_UNLOCK_GIANT(vfslocked);
1352         return (error);
1353 }
1354
1355 /*
1356  * Create a named pipe.
1357  */
1358 #ifndef _SYS_SYSPROTO_H_
1359 struct mkfifo_args {
1360         char    *path;
1361         int     mode;
1362 };
1363 #endif
1364 int
1365 mkfifo(td, uap)
1366         struct thread *td;
1367         register struct mkfifo_args /* {
1368                 char *path;
1369                 int mode;
1370         } */ *uap;
1371 {
1372
1373         return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1374 }
1375
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct mkfifoat_args {
1378         int     fd;
1379         char    *path;
1380         mode_t  mode;
1381 };
1382 #endif
1383 int
1384 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1385 {
1386
1387         return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1388             uap->mode));
1389 }
1390
1391 int
1392 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1393 {
1394
1395         return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1396 }
1397
1398 int
1399 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1400     int mode)
1401 {
1402         struct mount *mp;
1403         struct vattr vattr;
1404         int error;
1405         struct nameidata nd;
1406         int vfslocked;
1407
1408         AUDIT_ARG_MODE(mode);
1409 restart:
1410         bwillwrite();
1411         NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1412             pathseg, path, fd, td);
1413         if ((error = namei(&nd)) != 0)
1414                 return (error);
1415         vfslocked = NDHASGIANT(&nd);
1416         if (nd.ni_vp != NULL) {
1417                 NDFREE(&nd, NDF_ONLY_PNBUF);
1418                 if (nd.ni_vp == nd.ni_dvp)
1419                         vrele(nd.ni_dvp);
1420                 else
1421                         vput(nd.ni_dvp);
1422                 vrele(nd.ni_vp);
1423                 VFS_UNLOCK_GIANT(vfslocked);
1424                 return (EEXIST);
1425         }
1426         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1427                 NDFREE(&nd, NDF_ONLY_PNBUF);
1428                 vput(nd.ni_dvp);
1429                 VFS_UNLOCK_GIANT(vfslocked);
1430                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1431                         return (error);
1432                 goto restart;
1433         }
1434         VATTR_NULL(&vattr);
1435         vattr.va_type = VFIFO;
1436         vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1437 #ifdef MAC
1438         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1439             &vattr);
1440         if (error)
1441                 goto out;
1442 #endif
1443         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1444         if (error == 0)
1445                 vput(nd.ni_vp);
1446 #ifdef MAC
1447 out:
1448 #endif
1449         vput(nd.ni_dvp);
1450         vn_finished_write(mp);
1451         VFS_UNLOCK_GIANT(vfslocked);
1452         NDFREE(&nd, NDF_ONLY_PNBUF);
1453         return (error);
1454 }
1455
1456 /*
1457  * Make a hard file link.
1458  */
1459 #ifndef _SYS_SYSPROTO_H_
1460 struct link_args {
1461         char    *path;
1462         char    *link;
1463 };
1464 #endif
1465 int
1466 link(td, uap)
1467         struct thread *td;
1468         register struct link_args /* {
1469                 char *path;
1470                 char *link;
1471         } */ *uap;
1472 {
1473
1474         return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1475 }
1476
1477 #ifndef _SYS_SYSPROTO_H_
1478 struct linkat_args {
1479         int     fd1;
1480         char    *path1;
1481         int     fd2;
1482         char    *path2;
1483         int     flag;
1484 };
1485 #endif
1486 int
1487 linkat(struct thread *td, struct linkat_args *uap)
1488 {
1489         int flag;
1490
1491         flag = uap->flag;
1492         if (flag & ~AT_SYMLINK_FOLLOW)
1493                 return (EINVAL);
1494
1495         return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1496             UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1497 }
1498
1499 int hardlink_check_uid = 0;
1500 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1501     &hardlink_check_uid, 0,
1502     "Unprivileged processes cannot create hard links to files owned by other "
1503     "users");
1504 static int hardlink_check_gid = 0;
1505 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1506     &hardlink_check_gid, 0,
1507     "Unprivileged processes cannot create hard links to files owned by other "
1508     "groups");
1509
1510 static int
1511 can_hardlink(struct vnode *vp, struct ucred *cred)
1512 {
1513         struct vattr va;
1514         int error;
1515
1516         if (!hardlink_check_uid && !hardlink_check_gid)
1517                 return (0);
1518
1519         error = VOP_GETATTR(vp, &va, cred);
1520         if (error != 0)
1521                 return (error);
1522
1523         if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1524                 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1525                 if (error)
1526                         return (error);
1527         }
1528
1529         if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1530                 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1531                 if (error)
1532                         return (error);
1533         }
1534
1535         return (0);
1536 }
1537
1538 int
1539 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1540 {
1541
1542         return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1543 }
1544
1545 int
1546 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1547     enum uio_seg segflg, int follow)
1548 {
1549         struct vnode *vp;
1550         struct mount *mp;
1551         struct nameidata nd;
1552         int vfslocked;
1553         int lvfslocked;
1554         int error;
1555
1556         bwillwrite();
1557         NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1558             fd1, td);
1559
1560         if ((error = namei(&nd)) != 0)
1561                 return (error);
1562         vfslocked = NDHASGIANT(&nd);
1563         NDFREE(&nd, NDF_ONLY_PNBUF);
1564         vp = nd.ni_vp;
1565         if (vp->v_type == VDIR) {
1566                 vrele(vp);
1567                 VFS_UNLOCK_GIANT(vfslocked);
1568                 return (EPERM);         /* POSIX */
1569         }
1570         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1571                 vrele(vp);
1572                 VFS_UNLOCK_GIANT(vfslocked);
1573                 return (error);
1574         }
1575         NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1576             segflg, path2, fd2, td);
1577         if ((error = namei(&nd)) == 0) {
1578                 lvfslocked = NDHASGIANT(&nd);
1579                 if (nd.ni_vp != NULL) {
1580                         if (nd.ni_dvp == nd.ni_vp)
1581                                 vrele(nd.ni_dvp);
1582                         else
1583                                 vput(nd.ni_dvp);
1584                         vrele(nd.ni_vp);
1585                         error = EEXIST;
1586                 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1587                     == 0) {
1588                         error = can_hardlink(vp, td->td_ucred);
1589                         if (error == 0)
1590 #ifdef MAC
1591                                 error = mac_vnode_check_link(td->td_ucred,
1592                                     nd.ni_dvp, vp, &nd.ni_cnd);
1593                         if (error == 0)
1594 #endif
1595                                 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1596                         VOP_UNLOCK(vp, 0);
1597                         vput(nd.ni_dvp);
1598                 }
1599                 NDFREE(&nd, NDF_ONLY_PNBUF);
1600                 VFS_UNLOCK_GIANT(lvfslocked);
1601         }
1602         vrele(vp);
1603         vn_finished_write(mp);
1604         VFS_UNLOCK_GIANT(vfslocked);
1605         return (error);
1606 }
1607
1608 /*
1609  * Make a symbolic link.
1610  */
1611 #ifndef _SYS_SYSPROTO_H_
1612 struct symlink_args {
1613         char    *path;
1614         char    *link;
1615 };
1616 #endif
1617 int
1618 symlink(td, uap)
1619         struct thread *td;
1620         register struct symlink_args /* {
1621                 char *path;
1622                 char *link;
1623         } */ *uap;
1624 {
1625
1626         return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1627 }
1628
1629 #ifndef _SYS_SYSPROTO_H_
1630 struct symlinkat_args {
1631         char    *path;
1632         int     fd;
1633         char    *path2;
1634 };
1635 #endif
1636 int
1637 symlinkat(struct thread *td, struct symlinkat_args *uap)
1638 {
1639
1640         return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1641             UIO_USERSPACE));
1642 }
1643
1644 int
1645 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1646 {
1647
1648         return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1649 }
1650
1651 int
1652 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1653     enum uio_seg segflg)
1654 {
1655         struct mount *mp;
1656         struct vattr vattr;
1657         char *syspath;
1658         int error;
1659         struct nameidata nd;
1660         int vfslocked;
1661
1662         if (segflg == UIO_SYSSPACE) {
1663                 syspath = path1;
1664         } else {
1665                 syspath = uma_zalloc(namei_zone, M_WAITOK);
1666                 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1667                         goto out;
1668         }
1669         AUDIT_ARG_TEXT(syspath);
1670 restart:
1671         bwillwrite();
1672         NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1673             segflg, path2, fd, td);
1674         if ((error = namei(&nd)) != 0)
1675                 goto out;
1676         vfslocked = NDHASGIANT(&nd);
1677         if (nd.ni_vp) {
1678                 NDFREE(&nd, NDF_ONLY_PNBUF);
1679                 if (nd.ni_vp == nd.ni_dvp)
1680                         vrele(nd.ni_dvp);
1681                 else
1682                         vput(nd.ni_dvp);
1683                 vrele(nd.ni_vp);
1684                 VFS_UNLOCK_GIANT(vfslocked);
1685                 error = EEXIST;
1686                 goto out;
1687         }
1688         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1689                 NDFREE(&nd, NDF_ONLY_PNBUF);
1690                 vput(nd.ni_dvp);
1691                 VFS_UNLOCK_GIANT(vfslocked);
1692                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1693                         goto out;
1694                 goto restart;
1695         }
1696         VATTR_NULL(&vattr);
1697         vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1698 #ifdef MAC
1699         vattr.va_type = VLNK;
1700         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1701             &vattr);
1702         if (error)
1703                 goto out2;
1704 #endif
1705         error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1706         if (error == 0)
1707                 vput(nd.ni_vp);
1708 #ifdef MAC
1709 out2:
1710 #endif
1711         NDFREE(&nd, NDF_ONLY_PNBUF);
1712         vput(nd.ni_dvp);
1713         vn_finished_write(mp);
1714         VFS_UNLOCK_GIANT(vfslocked);
1715 out:
1716         if (segflg != UIO_SYSSPACE)
1717                 uma_zfree(namei_zone, syspath);
1718         return (error);
1719 }
1720
1721 /*
1722  * Delete a whiteout from the filesystem.
1723  */
1724 int
1725 undelete(td, uap)
1726         struct thread *td;
1727         register struct undelete_args /* {
1728                 char *path;
1729         } */ *uap;
1730 {
1731         int error;
1732         struct mount *mp;
1733         struct nameidata nd;
1734         int vfslocked;
1735
1736 restart:
1737         bwillwrite();
1738         NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1739             UIO_USERSPACE, uap->path, td);
1740         error = namei(&nd);
1741         if (error)
1742                 return (error);
1743         vfslocked = NDHASGIANT(&nd);
1744
1745         if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1746                 NDFREE(&nd, NDF_ONLY_PNBUF);
1747                 if (nd.ni_vp == nd.ni_dvp)
1748                         vrele(nd.ni_dvp);
1749                 else
1750                         vput(nd.ni_dvp);
1751                 if (nd.ni_vp)
1752                         vrele(nd.ni_vp);
1753                 VFS_UNLOCK_GIANT(vfslocked);
1754                 return (EEXIST);
1755         }
1756         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1757                 NDFREE(&nd, NDF_ONLY_PNBUF);
1758                 vput(nd.ni_dvp);
1759                 VFS_UNLOCK_GIANT(vfslocked);
1760                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1761                         return (error);
1762                 goto restart;
1763         }
1764         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1765         NDFREE(&nd, NDF_ONLY_PNBUF);
1766         vput(nd.ni_dvp);
1767         vn_finished_write(mp);
1768         VFS_UNLOCK_GIANT(vfslocked);
1769         return (error);
1770 }
1771
1772 /*
1773  * Delete a name from the filesystem.
1774  */
1775 #ifndef _SYS_SYSPROTO_H_
1776 struct unlink_args {
1777         char    *path;
1778 };
1779 #endif
1780 int
1781 unlink(td, uap)
1782         struct thread *td;
1783         struct unlink_args /* {
1784                 char *path;
1785         } */ *uap;
1786 {
1787
1788         return (kern_unlink(td, uap->path, UIO_USERSPACE));
1789 }
1790
1791 #ifndef _SYS_SYSPROTO_H_
1792 struct unlinkat_args {
1793         int     fd;
1794         char    *path;
1795         int     flag;
1796 };
1797 #endif
1798 int
1799 unlinkat(struct thread *td, struct unlinkat_args *uap)
1800 {
1801         int flag = uap->flag;
1802         int fd = uap->fd;
1803         char *path = uap->path;
1804
1805         if (flag & ~AT_REMOVEDIR)
1806                 return (EINVAL);
1807
1808         if (flag & AT_REMOVEDIR)
1809                 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1810         else
1811                 return (kern_unlinkat(td, fd, path, UIO_USERSPACE));
1812 }
1813
1814 int
1815 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1816 {
1817
1818         return (kern_unlinkat(td, AT_FDCWD, path, pathseg));
1819 }
1820
1821 int
1822 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
1823 {
1824         struct mount *mp;
1825         struct vnode *vp;
1826         int error;
1827         struct nameidata nd;
1828         int vfslocked;
1829
1830 restart:
1831         bwillwrite();
1832         NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1833             pathseg, path, fd, td);
1834         if ((error = namei(&nd)) != 0)
1835                 return (error == EINVAL ? EPERM : error);
1836         vfslocked = NDHASGIANT(&nd);
1837         vp = nd.ni_vp;
1838         if (vp->v_type == VDIR)
1839                 error = EPERM;          /* POSIX */
1840         else {
1841                 /*
1842                  * The root of a mounted filesystem cannot be deleted.
1843                  *
1844                  * XXX: can this only be a VDIR case?
1845                  */
1846                 if (vp->v_vflag & VV_ROOT)
1847                         error = EBUSY;
1848         }
1849         if (error == 0) {
1850                 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1851                         NDFREE(&nd, NDF_ONLY_PNBUF);
1852                         vput(nd.ni_dvp);
1853                         if (vp == nd.ni_dvp)
1854                                 vrele(vp);
1855                         else
1856                                 vput(vp);
1857                         VFS_UNLOCK_GIANT(vfslocked);
1858                         if ((error = vn_start_write(NULL, &mp,
1859                             V_XSLEEP | PCATCH)) != 0)
1860                                 return (error);
1861                         goto restart;
1862                 }
1863 #ifdef MAC
1864                 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1865                     &nd.ni_cnd);
1866                 if (error)
1867                         goto out;
1868 #endif
1869                 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1870 #ifdef MAC
1871 out:
1872 #endif
1873                 vn_finished_write(mp);
1874         }
1875         NDFREE(&nd, NDF_ONLY_PNBUF);
1876         vput(nd.ni_dvp);
1877         if (vp == nd.ni_dvp)
1878                 vrele(vp);
1879         else
1880                 vput(vp);
1881         VFS_UNLOCK_GIANT(vfslocked);
1882         return (error);
1883 }
1884
1885 /*
1886  * Reposition read/write file offset.
1887  */
1888 #ifndef _SYS_SYSPROTO_H_
1889 struct lseek_args {
1890         int     fd;
1891         int     pad;
1892         off_t   offset;
1893         int     whence;
1894 };
1895 #endif
1896 int
1897 lseek(td, uap)
1898         struct thread *td;
1899         register struct lseek_args /* {
1900                 int fd;
1901                 int pad;
1902                 off_t offset;
1903                 int whence;
1904         } */ *uap;
1905 {
1906         struct ucred *cred = td->td_ucred;
1907         struct file *fp;
1908         struct vnode *vp;
1909         struct vattr vattr;
1910         off_t offset, size;
1911         int error, noneg;
1912         int vfslocked;
1913
1914         AUDIT_ARG_FD(uap->fd);
1915         if ((error = fget(td, uap->fd, &fp)) != 0)
1916                 return (error);
1917         if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1918                 fdrop(fp, td);
1919                 return (ESPIPE);
1920         }
1921         vp = fp->f_vnode;
1922         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1923         noneg = (vp->v_type != VCHR);
1924         offset = uap->offset;
1925         switch (uap->whence) {
1926         case L_INCR:
1927                 if (noneg &&
1928                     (fp->f_offset < 0 ||
1929                     (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1930                         error = EOVERFLOW;
1931                         break;
1932                 }
1933                 offset += fp->f_offset;
1934                 break;
1935         case L_XTND:
1936                 vn_lock(vp, LK_SHARED | LK_RETRY);
1937                 error = VOP_GETATTR(vp, &vattr, cred);
1938                 VOP_UNLOCK(vp, 0);
1939                 if (error)
1940                         break;
1941
1942                 /*
1943                  * If the file references a disk device, then fetch
1944                  * the media size and use that to determine the ending
1945                  * offset.
1946                  */
1947                 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1948                     fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1949                         vattr.va_size = size;
1950                 if (noneg &&
1951                     (vattr.va_size > OFF_MAX ||
1952                     (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1953                         error = EOVERFLOW;
1954                         break;
1955                 }
1956                 offset += vattr.va_size;
1957                 break;
1958         case L_SET:
1959                 break;
1960         case SEEK_DATA:
1961                 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1962                 break;
1963         case SEEK_HOLE:
1964                 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1965                 break;
1966         default:
1967                 error = EINVAL;
1968         }
1969         if (error == 0 && noneg && offset < 0)
1970                 error = EINVAL;
1971         if (error != 0)
1972                 goto drop;
1973         fp->f_offset = offset;
1974         *(off_t *)(td->td_retval) = fp->f_offset;
1975 drop:
1976         fdrop(fp, td);
1977         VFS_UNLOCK_GIANT(vfslocked);
1978         return (error);
1979 }
1980
1981 #if defined(COMPAT_43)
1982 /*
1983  * Reposition read/write file offset.
1984  */
1985 #ifndef _SYS_SYSPROTO_H_
1986 struct olseek_args {
1987         int     fd;
1988         long    offset;
1989         int     whence;
1990 };
1991 #endif
1992 int
1993 olseek(td, uap)
1994         struct thread *td;
1995         register struct olseek_args /* {
1996                 int fd;
1997                 long offset;
1998                 int whence;
1999         } */ *uap;
2000 {
2001         struct lseek_args /* {
2002                 int fd;
2003                 int pad;
2004                 off_t offset;
2005                 int whence;
2006         } */ nuap;
2007
2008         nuap.fd = uap->fd;
2009         nuap.offset = uap->offset;
2010         nuap.whence = uap->whence;
2011         return (lseek(td, &nuap));
2012 }
2013 #endif /* COMPAT_43 */
2014
2015 /* Version with the 'pad' argument */
2016 int
2017 freebsd6_lseek(td, uap)
2018         struct thread *td;
2019         register struct freebsd6_lseek_args *uap;
2020 {
2021         struct lseek_args ouap;
2022
2023         ouap.fd = uap->fd;
2024         ouap.offset = uap->offset;
2025         ouap.whence = uap->whence;
2026         return (lseek(td, &ouap));
2027 }
2028
2029 /*
2030  * Check access permissions using passed credentials.
2031  */
2032 static int
2033 vn_access(vp, user_flags, cred, td)
2034         struct vnode    *vp;
2035         int             user_flags;
2036         struct ucred    *cred;
2037         struct thread   *td;
2038 {
2039         int error;
2040         accmode_t accmode;
2041
2042         /* Flags == 0 means only check for existence. */
2043         error = 0;
2044         if (user_flags) {
2045                 accmode = 0;
2046                 if (user_flags & R_OK)
2047                         accmode |= VREAD;
2048                 if (user_flags & W_OK)
2049                         accmode |= VWRITE;
2050                 if (user_flags & X_OK)
2051                         accmode |= VEXEC;
2052 #ifdef MAC
2053                 error = mac_vnode_check_access(cred, vp, accmode);
2054                 if (error)
2055                         return (error);
2056 #endif
2057                 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2058                         error = VOP_ACCESS(vp, accmode, cred, td);
2059         }
2060         return (error);
2061 }
2062
2063 /*
2064  * Check access permissions using "real" credentials.
2065  */
2066 #ifndef _SYS_SYSPROTO_H_
2067 struct access_args {
2068         char    *path;
2069         int     flags;
2070 };
2071 #endif
2072 int
2073 access(td, uap)
2074         struct thread *td;
2075         register struct access_args /* {
2076                 char *path;
2077                 int flags;
2078         } */ *uap;
2079 {
2080
2081         return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2082 }
2083
2084 #ifndef _SYS_SYSPROTO_H_
2085 struct faccessat_args {
2086         int     dirfd;
2087         char    *path;
2088         int     mode;
2089         int     flag;
2090 }
2091 #endif
2092 int
2093 faccessat(struct thread *td, struct faccessat_args *uap)
2094 {
2095
2096         if (uap->flag & ~AT_EACCESS)
2097                 return (EINVAL);
2098         return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2099             uap->mode));
2100 }
2101
2102 int
2103 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2104 {
2105
2106         return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2107 }
2108
2109 int
2110 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2111     int flags, int mode)
2112 {
2113         struct ucred *cred, *tmpcred;
2114         struct vnode *vp;
2115         struct nameidata nd;
2116         int vfslocked;
2117         int error;
2118
2119         /*
2120          * Create and modify a temporary credential instead of one that
2121          * is potentially shared.  This could also mess up socket
2122          * buffer accounting which can run in an interrupt context.
2123          */
2124         if (!(flags & AT_EACCESS)) {
2125                 cred = td->td_ucred;
2126                 tmpcred = crdup(cred);
2127                 tmpcred->cr_uid = cred->cr_ruid;
2128                 tmpcred->cr_groups[0] = cred->cr_rgid;
2129                 td->td_ucred = tmpcred;
2130         } else
2131                 cred = tmpcred = td->td_ucred;
2132         AUDIT_ARG_VALUE(mode);
2133         NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2134             AUDITVNODE1, pathseg, path, fd, td);
2135         if ((error = namei(&nd)) != 0)
2136                 goto out1;
2137         vfslocked = NDHASGIANT(&nd);
2138         vp = nd.ni_vp;
2139
2140         error = vn_access(vp, mode, tmpcred, td);
2141         NDFREE(&nd, NDF_ONLY_PNBUF);
2142         vput(vp);
2143         VFS_UNLOCK_GIANT(vfslocked);
2144 out1:
2145         if (!(flags & AT_EACCESS)) {
2146                 td->td_ucred = cred;
2147                 crfree(tmpcred);
2148         }
2149         return (error);
2150 }
2151
2152 /*
2153  * Check access permissions using "effective" credentials.
2154  */
2155 #ifndef _SYS_SYSPROTO_H_
2156 struct eaccess_args {
2157         char    *path;
2158         int     flags;
2159 };
2160 #endif
2161 int
2162 eaccess(td, uap)
2163         struct thread *td;
2164         register struct eaccess_args /* {
2165                 char *path;
2166                 int flags;
2167         } */ *uap;
2168 {
2169
2170         return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2171 }
2172
2173 int
2174 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2175 {
2176
2177         return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2178 }
2179
2180 #if defined(COMPAT_43)
2181 /*
2182  * Get file status; this version follows links.
2183  */
2184 #ifndef _SYS_SYSPROTO_H_
2185 struct ostat_args {
2186         char    *path;
2187         struct ostat *ub;
2188 };
2189 #endif
2190 int
2191 ostat(td, uap)
2192         struct thread *td;
2193         register struct ostat_args /* {
2194                 char *path;
2195                 struct ostat *ub;
2196         } */ *uap;
2197 {
2198         struct stat sb;
2199         struct ostat osb;
2200         int error;
2201
2202         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2203         if (error)
2204                 return (error);
2205         cvtstat(&sb, &osb);
2206         error = copyout(&osb, uap->ub, sizeof (osb));
2207         return (error);
2208 }
2209
2210 /*
2211  * Get file status; this version does not follow links.
2212  */
2213 #ifndef _SYS_SYSPROTO_H_
2214 struct olstat_args {
2215         char    *path;
2216         struct ostat *ub;
2217 };
2218 #endif
2219 int
2220 olstat(td, uap)
2221         struct thread *td;
2222         register struct olstat_args /* {
2223                 char *path;
2224                 struct ostat *ub;
2225         } */ *uap;
2226 {
2227         struct stat sb;
2228         struct ostat osb;
2229         int error;
2230
2231         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2232         if (error)
2233                 return (error);
2234         cvtstat(&sb, &osb);
2235         error = copyout(&osb, uap->ub, sizeof (osb));
2236         return (error);
2237 }
2238
2239 /*
2240  * Convert from an old to a new stat structure.
2241  */
2242 void
2243 cvtstat(st, ost)
2244         struct stat *st;
2245         struct ostat *ost;
2246 {
2247
2248         ost->st_dev = st->st_dev;
2249         ost->st_ino = st->st_ino;
2250         ost->st_mode = st->st_mode;
2251         ost->st_nlink = st->st_nlink;
2252         ost->st_uid = st->st_uid;
2253         ost->st_gid = st->st_gid;
2254         ost->st_rdev = st->st_rdev;
2255         if (st->st_size < (quad_t)1 << 32)
2256                 ost->st_size = st->st_size;
2257         else
2258                 ost->st_size = -2;
2259         ost->st_atime = st->st_atime;
2260         ost->st_mtime = st->st_mtime;
2261         ost->st_ctime = st->st_ctime;
2262         ost->st_blksize = st->st_blksize;
2263         ost->st_blocks = st->st_blocks;
2264         ost->st_flags = st->st_flags;
2265         ost->st_gen = st->st_gen;
2266 }
2267 #endif /* COMPAT_43 */
2268
2269 /*
2270  * Get file status; this version follows links.
2271  */
2272 #ifndef _SYS_SYSPROTO_H_
2273 struct stat_args {
2274         char    *path;
2275         struct stat *ub;
2276 };
2277 #endif
2278 int
2279 stat(td, uap)
2280         struct thread *td;
2281         register struct stat_args /* {
2282                 char *path;
2283                 struct stat *ub;
2284         } */ *uap;
2285 {
2286         struct stat sb;
2287         int error;
2288
2289         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2290         if (error == 0)
2291                 error = copyout(&sb, uap->ub, sizeof (sb));
2292         return (error);
2293 }
2294
2295 #ifndef _SYS_SYSPROTO_H_
2296 struct fstatat_args {
2297         int     fd;
2298         char    *path;
2299         struct stat     *buf;
2300         int     flag;
2301 }
2302 #endif
2303 int
2304 fstatat(struct thread *td, struct fstatat_args *uap)
2305 {
2306         struct stat sb;
2307         int error;
2308
2309         error = kern_statat(td, uap->flag, uap->fd, uap->path,
2310             UIO_USERSPACE, &sb);
2311         if (error == 0)
2312                 error = copyout(&sb, uap->buf, sizeof (sb));
2313         return (error);
2314 }
2315
2316 int
2317 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2318 {
2319
2320         return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2321 }
2322
2323 int
2324 kern_statat(struct thread *td, int flag, int fd, char *path,
2325     enum uio_seg pathseg, struct stat *sbp)
2326 {
2327
2328         return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2329 }
2330
2331 int
2332 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2333     enum uio_seg pathseg, struct stat *sbp,
2334     void (*hook)(struct vnode *vp, struct stat *sbp))
2335 {
2336         struct nameidata nd;
2337         struct stat sb;
2338         int error, vfslocked;
2339
2340         if (flag & ~AT_SYMLINK_NOFOLLOW)
2341                 return (EINVAL);
2342
2343         NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2344             FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2345             path, fd, td);
2346
2347         if ((error = namei(&nd)) != 0)
2348                 return (error);
2349         vfslocked = NDHASGIANT(&nd);
2350         error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2351         if (!error) {
2352                 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2353                 if (S_ISREG(sb.st_mode))
2354                         SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2355                 if (__predict_false(hook != NULL))
2356                         hook(nd.ni_vp, &sb);
2357         }
2358         NDFREE(&nd, NDF_ONLY_PNBUF);
2359         vput(nd.ni_vp);
2360         VFS_UNLOCK_GIANT(vfslocked);
2361         if (error)
2362                 return (error);
2363         *sbp = sb;
2364 #ifdef KTRACE
2365         if (KTRPOINT(td, KTR_STRUCT))
2366                 ktrstat(&sb);
2367 #endif
2368         return (0);
2369 }
2370
2371 /*
2372  * Get file status; this version does not follow links.
2373  */
2374 #ifndef _SYS_SYSPROTO_H_
2375 struct lstat_args {
2376         char    *path;
2377         struct stat *ub;
2378 };
2379 #endif
2380 int
2381 lstat(td, uap)
2382         struct thread *td;
2383         register struct lstat_args /* {
2384                 char *path;
2385                 struct stat *ub;
2386         } */ *uap;
2387 {
2388         struct stat sb;
2389         int error;
2390
2391         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2392         if (error == 0)
2393                 error = copyout(&sb, uap->ub, sizeof (sb));
2394         return (error);
2395 }
2396
2397 int
2398 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2399 {
2400
2401         return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2402             sbp));
2403 }
2404
2405 /*
2406  * Implementation of the NetBSD [l]stat() functions.
2407  */
2408 void
2409 cvtnstat(sb, nsb)
2410         struct stat *sb;
2411         struct nstat *nsb;
2412 {
2413         bzero(nsb, sizeof *nsb);
2414         nsb->st_dev = sb->st_dev;
2415         nsb->st_ino = sb->st_ino;
2416         nsb->st_mode = sb->st_mode;
2417         nsb->st_nlink = sb->st_nlink;
2418         nsb->st_uid = sb->st_uid;
2419         nsb->st_gid = sb->st_gid;
2420         nsb->st_rdev = sb->st_rdev;
2421         nsb->st_atimespec = sb->st_atimespec;
2422         nsb->st_mtimespec = sb->st_mtimespec;
2423         nsb->st_ctimespec = sb->st_ctimespec;
2424         nsb->st_size = sb->st_size;
2425         nsb->st_blocks = sb->st_blocks;
2426         nsb->st_blksize = sb->st_blksize;
2427         nsb->st_flags = sb->st_flags;
2428         nsb->st_gen = sb->st_gen;
2429         nsb->st_birthtimespec = sb->st_birthtimespec;
2430 }
2431
2432 #ifndef _SYS_SYSPROTO_H_
2433 struct nstat_args {
2434         char    *path;
2435         struct nstat *ub;
2436 };
2437 #endif
2438 int
2439 nstat(td, uap)
2440         struct thread *td;
2441         register struct nstat_args /* {
2442                 char *path;
2443                 struct nstat *ub;
2444         } */ *uap;
2445 {
2446         struct stat sb;
2447         struct nstat nsb;
2448         int error;
2449
2450         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2451         if (error)
2452                 return (error);
2453         cvtnstat(&sb, &nsb);
2454         error = copyout(&nsb, uap->ub, sizeof (nsb));
2455         return (error);
2456 }
2457
2458 /*
2459  * NetBSD lstat.  Get file status; this version does not follow links.
2460  */
2461 #ifndef _SYS_SYSPROTO_H_
2462 struct lstat_args {
2463         char    *path;
2464         struct stat *ub;
2465 };
2466 #endif
2467 int
2468 nlstat(td, uap)
2469         struct thread *td;
2470         register struct nlstat_args /* {
2471                 char *path;
2472                 struct nstat *ub;
2473         } */ *uap;
2474 {
2475         struct stat sb;
2476         struct nstat nsb;
2477         int error;
2478
2479         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2480         if (error)
2481                 return (error);
2482         cvtnstat(&sb, &nsb);
2483         error = copyout(&nsb, uap->ub, sizeof (nsb));
2484         return (error);
2485 }
2486
2487 /*
2488  * Get configurable pathname variables.
2489  */
2490 #ifndef _SYS_SYSPROTO_H_
2491 struct pathconf_args {
2492         char    *path;
2493         int     name;
2494 };
2495 #endif
2496 int
2497 pathconf(td, uap)
2498         struct thread *td;
2499         register struct pathconf_args /* {
2500                 char *path;
2501                 int name;
2502         } */ *uap;
2503 {
2504
2505         return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2506 }
2507
2508 #ifndef _SYS_SYSPROTO_H_
2509 struct lpathconf_args {
2510         char    *path;
2511         int     name;
2512 };
2513 #endif
2514 int
2515 lpathconf(td, uap)
2516         struct thread *td;
2517         register struct lpathconf_args /* {
2518                 char *path;
2519                 int name;
2520         } */ *uap;
2521 {
2522
2523         return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2524 }
2525
2526 int
2527 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2528     u_long flags)
2529 {
2530         struct nameidata nd;
2531         int error, vfslocked;
2532
2533         NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2534             flags, pathseg, path, td);
2535         if ((error = namei(&nd)) != 0)
2536                 return (error);
2537         vfslocked = NDHASGIANT(&nd);
2538         NDFREE(&nd, NDF_ONLY_PNBUF);
2539
2540         /* If asynchronous I/O is available, it works for all files. */
2541         if (name == _PC_ASYNC_IO)
2542                 td->td_retval[0] = async_io_version;
2543         else
2544                 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2545         vput(nd.ni_vp);
2546         VFS_UNLOCK_GIANT(vfslocked);
2547         return (error);
2548 }
2549
2550 /*
2551  * Return target name of a symbolic link.
2552  */
2553 #ifndef _SYS_SYSPROTO_H_
2554 struct readlink_args {
2555         char    *path;
2556         char    *buf;
2557         size_t  count;
2558 };
2559 #endif
2560 int
2561 readlink(td, uap)
2562         struct thread *td;
2563         register struct readlink_args /* {
2564                 char *path;
2565                 char *buf;
2566                 size_t count;
2567         } */ *uap;
2568 {
2569
2570         return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2571             UIO_USERSPACE, uap->count));
2572 }
2573 #ifndef _SYS_SYSPROTO_H_
2574 struct readlinkat_args {
2575         int     fd;
2576         char    *path;
2577         char    *buf;
2578         size_t  bufsize;
2579 };
2580 #endif
2581 int
2582 readlinkat(struct thread *td, struct readlinkat_args *uap)
2583 {
2584
2585         return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2586             uap->buf, UIO_USERSPACE, uap->bufsize));
2587 }
2588
2589 int
2590 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2591     enum uio_seg bufseg, size_t count)
2592 {
2593
2594         return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2595             count));
2596 }
2597
2598 int
2599 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2600     char *buf, enum uio_seg bufseg, size_t count)
2601 {
2602         struct vnode *vp;
2603         struct iovec aiov;
2604         struct uio auio;
2605         int error;
2606         struct nameidata nd;
2607         int vfslocked;
2608
2609         if (count > INT_MAX)
2610                 return (EINVAL);
2611
2612         NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2613             AUDITVNODE1, pathseg, path, fd, td);
2614
2615         if ((error = namei(&nd)) != 0)
2616                 return (error);
2617         NDFREE(&nd, NDF_ONLY_PNBUF);
2618         vfslocked = NDHASGIANT(&nd);
2619         vp = nd.ni_vp;
2620 #ifdef MAC
2621         error = mac_vnode_check_readlink(td->td_ucred, vp);
2622         if (error) {
2623                 vput(vp);
2624                 VFS_UNLOCK_GIANT(vfslocked);
2625                 return (error);
2626         }
2627 #endif
2628         if (vp->v_type != VLNK)
2629                 error = EINVAL;
2630         else {
2631                 aiov.iov_base = buf;
2632                 aiov.iov_len = count;
2633                 auio.uio_iov = &aiov;
2634                 auio.uio_iovcnt = 1;
2635                 auio.uio_offset = 0;
2636                 auio.uio_rw = UIO_READ;
2637                 auio.uio_segflg = bufseg;
2638                 auio.uio_td = td;
2639                 auio.uio_resid = count;
2640                 error = VOP_READLINK(vp, &auio, td->td_ucred);
2641         }
2642         vput(vp);
2643         VFS_UNLOCK_GIANT(vfslocked);
2644         td->td_retval[0] = count - auio.uio_resid;
2645         return (error);
2646 }
2647
2648 /*
2649  * Common implementation code for chflags() and fchflags().
2650  */
2651 static int
2652 setfflags(td, vp, flags)
2653         struct thread *td;
2654         struct vnode *vp;
2655         int flags;
2656 {
2657         int error;
2658         struct mount *mp;
2659         struct vattr vattr;
2660
2661         /*
2662          * Prevent non-root users from setting flags on devices.  When
2663          * a device is reused, users can retain ownership of the device
2664          * if they are allowed to set flags and programs assume that
2665          * chown can't fail when done as root.
2666          */
2667         if (vp->v_type == VCHR || vp->v_type == VBLK) {
2668                 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2669                 if (error)
2670                         return (error);
2671         }
2672
2673         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2674                 return (error);
2675         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2676         VATTR_NULL(&vattr);
2677         vattr.va_flags = flags;
2678 #ifdef MAC
2679         error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2680         if (error == 0)
2681 #endif
2682                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2683         VOP_UNLOCK(vp, 0);
2684         vn_finished_write(mp);
2685         return (error);
2686 }
2687
2688 /*
2689  * Change flags of a file given a path name.
2690  */
2691 #ifndef _SYS_SYSPROTO_H_
2692 struct chflags_args {
2693         char    *path;
2694         int     flags;
2695 };
2696 #endif
2697 int
2698 chflags(td, uap)
2699         struct thread *td;
2700         register struct chflags_args /* {
2701                 char *path;
2702                 int flags;
2703         } */ *uap;
2704 {
2705         int error;
2706         struct nameidata nd;
2707         int vfslocked;
2708
2709         AUDIT_ARG_FFLAGS(uap->flags);
2710         NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2711             uap->path, td);
2712         if ((error = namei(&nd)) != 0)
2713                 return (error);
2714         NDFREE(&nd, NDF_ONLY_PNBUF);
2715         vfslocked = NDHASGIANT(&nd);
2716         error = setfflags(td, nd.ni_vp, uap->flags);
2717         vrele(nd.ni_vp);
2718         VFS_UNLOCK_GIANT(vfslocked);
2719         return (error);
2720 }
2721
2722 /*
2723  * Same as chflags() but doesn't follow symlinks.
2724  */
2725 int
2726 lchflags(td, uap)
2727         struct thread *td;
2728         register struct lchflags_args /* {
2729                 char *path;
2730                 int flags;
2731         } */ *uap;
2732 {
2733         int error;
2734         struct nameidata nd;
2735         int vfslocked;
2736
2737         AUDIT_ARG_FFLAGS(uap->flags);
2738         NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2739             uap->path, td);
2740         if ((error = namei(&nd)) != 0)
2741                 return (error);
2742         vfslocked = NDHASGIANT(&nd);
2743         NDFREE(&nd, NDF_ONLY_PNBUF);
2744         error = setfflags(td, nd.ni_vp, uap->flags);
2745         vrele(nd.ni_vp);
2746         VFS_UNLOCK_GIANT(vfslocked);
2747         return (error);
2748 }
2749
2750 /*
2751  * Change flags of a file given a file descriptor.
2752  */
2753 #ifndef _SYS_SYSPROTO_H_
2754 struct fchflags_args {
2755         int     fd;
2756         int     flags;
2757 };
2758 #endif
2759 int
2760 fchflags(td, uap)
2761         struct thread *td;
2762         register struct fchflags_args /* {
2763                 int fd;
2764                 int flags;
2765         } */ *uap;
2766 {
2767         struct file *fp;
2768         int vfslocked;
2769         int error;
2770
2771         AUDIT_ARG_FD(uap->fd);
2772         AUDIT_ARG_FFLAGS(uap->flags);
2773         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2774                 return (error);
2775         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2776 #ifdef AUDIT
2777         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2778         AUDIT_ARG_VNODE1(fp->f_vnode);
2779         VOP_UNLOCK(fp->f_vnode, 0);
2780 #endif
2781         error = setfflags(td, fp->f_vnode, uap->flags);
2782         VFS_UNLOCK_GIANT(vfslocked);
2783         fdrop(fp, td);
2784         return (error);
2785 }
2786
2787 /*
2788  * Common implementation code for chmod(), lchmod() and fchmod().
2789  */
2790 static int
2791 setfmode(td, vp, mode)
2792         struct thread *td;
2793         struct vnode *vp;
2794         int mode;
2795 {
2796         int error;
2797         struct mount *mp;
2798         struct vattr vattr;
2799
2800         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2801                 return (error);
2802         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2803         VATTR_NULL(&vattr);
2804         vattr.va_mode = mode & ALLPERMS;
2805 #ifdef MAC
2806         error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2807         if (error == 0)
2808 #endif
2809                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2810         VOP_UNLOCK(vp, 0);
2811         vn_finished_write(mp);
2812         return (error);
2813 }
2814
2815 /*
2816  * Change mode of a file given path name.
2817  */
2818 #ifndef _SYS_SYSPROTO_H_
2819 struct chmod_args {
2820         char    *path;
2821         int     mode;
2822 };
2823 #endif
2824 int
2825 chmod(td, uap)
2826         struct thread *td;
2827         register struct chmod_args /* {
2828                 char *path;
2829                 int mode;
2830         } */ *uap;
2831 {
2832
2833         return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2834 }
2835
2836 #ifndef _SYS_SYSPROTO_H_
2837 struct fchmodat_args {
2838         int     dirfd;
2839         char    *path;
2840         mode_t  mode;
2841         int     flag;
2842 }
2843 #endif
2844 int
2845 fchmodat(struct thread *td, struct fchmodat_args *uap)
2846 {
2847         int flag = uap->flag;
2848         int fd = uap->fd;
2849         char *path = uap->path;
2850         mode_t mode = uap->mode;
2851
2852         if (flag & ~AT_SYMLINK_NOFOLLOW)
2853                 return (EINVAL);
2854
2855         return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2856 }
2857
2858 int
2859 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2860 {
2861
2862         return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2863 }
2864
2865 /*
2866  * Change mode of a file given path name (don't follow links.)
2867  */
2868 #ifndef _SYS_SYSPROTO_H_
2869 struct lchmod_args {
2870         char    *path;
2871         int     mode;
2872 };
2873 #endif
2874 int
2875 lchmod(td, uap)
2876         struct thread *td;
2877         register struct lchmod_args /* {
2878                 char *path;
2879                 int mode;
2880         } */ *uap;
2881 {
2882
2883         return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2884             uap->mode, AT_SYMLINK_NOFOLLOW));
2885 }
2886
2887
2888 int
2889 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2890     mode_t mode, int flag)
2891 {
2892         int error;
2893         struct nameidata nd;
2894         int vfslocked;
2895         int follow;
2896
2897         AUDIT_ARG_MODE(mode);
2898         follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2899         NDINIT_AT(&nd, LOOKUP,  follow | MPSAFE | AUDITVNODE1, pathseg, path,
2900             fd, td);
2901         if ((error = namei(&nd)) != 0)
2902                 return (error);
2903         vfslocked = NDHASGIANT(&nd);
2904         NDFREE(&nd, NDF_ONLY_PNBUF);
2905         error = setfmode(td, nd.ni_vp, mode);
2906         vrele(nd.ni_vp);
2907         VFS_UNLOCK_GIANT(vfslocked);
2908         return (error);
2909 }
2910
2911 /*
2912  * Change mode of a file given a file descriptor.
2913  */
2914 #ifndef _SYS_SYSPROTO_H_
2915 struct fchmod_args {
2916         int     fd;
2917         int     mode;
2918 };
2919 #endif
2920 int
2921 fchmod(td, uap)
2922         struct thread *td;
2923         register struct fchmod_args /* {
2924                 int fd;
2925                 int mode;
2926         } */ *uap;
2927 {
2928         struct file *fp;
2929         int vfslocked;
2930         int error;
2931
2932         AUDIT_ARG_FD(uap->fd);
2933         AUDIT_ARG_MODE(uap->mode);
2934         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2935                 return (error);
2936         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2937 #ifdef AUDIT
2938         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2939         AUDIT_ARG_VNODE1(fp->f_vnode);
2940         VOP_UNLOCK(fp->f_vnode, 0);
2941 #endif
2942         error = setfmode(td, fp->f_vnode, uap->mode);
2943         VFS_UNLOCK_GIANT(vfslocked);
2944         fdrop(fp, td);
2945         return (error);
2946 }
2947
2948 /*
2949  * Common implementation for chown(), lchown(), and fchown()
2950  */
2951 static int
2952 setfown(td, vp, uid, gid)
2953         struct thread *td;
2954         struct vnode *vp;
2955         uid_t uid;
2956         gid_t gid;
2957 {
2958         int error;
2959         struct mount *mp;
2960         struct vattr vattr;
2961
2962         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2963                 return (error);
2964         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2965         VATTR_NULL(&vattr);
2966         vattr.va_uid = uid;
2967         vattr.va_gid = gid;
2968 #ifdef MAC
2969         error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2970             vattr.va_gid);
2971         if (error == 0)
2972 #endif
2973                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2974         VOP_UNLOCK(vp, 0);
2975         vn_finished_write(mp);
2976         return (error);
2977 }
2978
2979 /*
2980  * Set ownership given a path name.
2981  */
2982 #ifndef _SYS_SYSPROTO_H_
2983 struct chown_args {
2984         char    *path;
2985         int     uid;
2986         int     gid;
2987 };
2988 #endif
2989 int
2990 chown(td, uap)
2991         struct thread *td;
2992         register struct chown_args /* {
2993                 char *path;
2994                 int uid;
2995                 int gid;
2996         } */ *uap;
2997 {
2998
2999         return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3000 }
3001
3002 #ifndef _SYS_SYSPROTO_H_
3003 struct fchownat_args {
3004         int fd;
3005         const char * path;
3006         uid_t uid;
3007         gid_t gid;
3008         int flag;
3009 };
3010 #endif
3011 int
3012 fchownat(struct thread *td, struct fchownat_args *uap)
3013 {
3014         int flag;
3015
3016         flag = uap->flag;
3017         if (flag & ~AT_SYMLINK_NOFOLLOW)
3018                 return (EINVAL);
3019
3020         return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3021             uap->gid, uap->flag));
3022 }
3023
3024 int
3025 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3026     int gid)
3027 {
3028
3029         return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3030 }
3031
3032 int
3033 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3034     int uid, int gid, int flag)
3035 {
3036         struct nameidata nd;
3037         int error, vfslocked, follow;
3038
3039         AUDIT_ARG_OWNER(uid, gid);
3040         follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3041         NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3042             fd, td);
3043
3044         if ((error = namei(&nd)) != 0)
3045                 return (error);
3046         vfslocked = NDHASGIANT(&nd);
3047         NDFREE(&nd, NDF_ONLY_PNBUF);
3048         error = setfown(td, nd.ni_vp, uid, gid);
3049         vrele(nd.ni_vp);
3050         VFS_UNLOCK_GIANT(vfslocked);
3051         return (error);
3052 }
3053
3054 /*
3055  * Set ownership given a path name, do not cross symlinks.
3056  */
3057 #ifndef _SYS_SYSPROTO_H_
3058 struct lchown_args {
3059         char    *path;
3060         int     uid;
3061         int     gid;
3062 };
3063 #endif
3064 int
3065 lchown(td, uap)
3066         struct thread *td;
3067         register struct lchown_args /* {
3068                 char *path;
3069                 int uid;
3070                 int gid;
3071         } */ *uap;
3072 {
3073
3074         return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3075 }
3076
3077 int
3078 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3079     int gid)
3080 {
3081
3082         return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3083             AT_SYMLINK_NOFOLLOW));
3084 }
3085
3086 /*
3087  * Set ownership given a file descriptor.
3088  */
3089 #ifndef _SYS_SYSPROTO_H_
3090 struct fchown_args {
3091         int     fd;
3092         int     uid;
3093         int     gid;
3094 };
3095 #endif
3096 int
3097 fchown(td, uap)
3098         struct thread *td;
3099         register struct fchown_args /* {
3100                 int fd;
3101                 int uid;
3102                 int gid;
3103         } */ *uap;
3104 {
3105         struct file *fp;
3106         int vfslocked;
3107         int error;
3108
3109         AUDIT_ARG_FD(uap->fd);
3110         AUDIT_ARG_OWNER(uap->uid, uap->gid);
3111         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3112                 return (error);
3113         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3114 #ifdef AUDIT
3115         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3116         AUDIT_ARG_VNODE1(fp->f_vnode);
3117         VOP_UNLOCK(fp->f_vnode, 0);
3118 #endif
3119         error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3120         VFS_UNLOCK_GIANT(vfslocked);
3121         fdrop(fp, td);
3122         return (error);
3123 }
3124
3125 /*
3126  * Common implementation code for utimes(), lutimes(), and futimes().
3127  */
3128 static int
3129 getutimes(usrtvp, tvpseg, tsp)
3130         const struct timeval *usrtvp;
3131         enum uio_seg tvpseg;
3132         struct timespec *tsp;
3133 {
3134         struct timeval tv[2];
3135         const struct timeval *tvp;
3136         int error;
3137
3138         if (usrtvp == NULL) {
3139                 vfs_timestamp(&tsp[0]);
3140                 tsp[1] = tsp[0];
3141         } else {
3142                 if (tvpseg == UIO_SYSSPACE) {
3143                         tvp = usrtvp;
3144                 } else {
3145                         if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3146                                 return (error);
3147                         tvp = tv;
3148                 }
3149
3150                 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3151                     tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3152                         return (EINVAL);
3153                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3154                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3155         }
3156         return (0);
3157 }
3158
3159 /*
3160  * Common implementation code for utimes(), lutimes(), and futimes().
3161  */
3162 static int
3163 setutimes(td, vp, ts, numtimes, nullflag)
3164         struct thread *td;
3165         struct vnode *vp;
3166         const struct timespec *ts;
3167         int numtimes;
3168         int nullflag;
3169 {
3170         int error, setbirthtime;
3171         struct mount *mp;
3172         struct vattr vattr;
3173
3174         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3175                 return (error);
3176         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3177         setbirthtime = 0;
3178         if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3179             timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3180                 setbirthtime = 1;
3181         VATTR_NULL(&vattr);
3182         vattr.va_atime = ts[0];
3183         vattr.va_mtime = ts[1];
3184         if (setbirthtime)
3185                 vattr.va_birthtime = ts[1];
3186         if (numtimes > 2)
3187                 vattr.va_birthtime = ts[2];
3188         if (nullflag)
3189                 vattr.va_vaflags |= VA_UTIMES_NULL;
3190 #ifdef MAC
3191         error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3192             vattr.va_mtime);
3193 #endif
3194         if (error == 0)
3195                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3196         VOP_UNLOCK(vp, 0);
3197         vn_finished_write(mp);
3198         return (error);
3199 }
3200
3201 /*
3202  * Set the access and modification times of a file.
3203  */
3204 #ifndef _SYS_SYSPROTO_H_
3205 struct utimes_args {
3206         char    *path;
3207         struct  timeval *tptr;
3208 };
3209 #endif
3210 int
3211 utimes(td, uap)
3212         struct thread *td;
3213         register struct utimes_args /* {
3214                 char *path;
3215                 struct timeval *tptr;
3216         } */ *uap;
3217 {
3218
3219         return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3220             UIO_USERSPACE));
3221 }
3222
3223 #ifndef _SYS_SYSPROTO_H_
3224 struct futimesat_args {
3225         int fd;
3226         const char * path;
3227         const struct timeval * times;
3228 };
3229 #endif
3230 int
3231 futimesat(struct thread *td, struct futimesat_args *uap)
3232 {
3233
3234         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3235             uap->times, UIO_USERSPACE));
3236 }
3237
3238 int
3239 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3240     struct timeval *tptr, enum uio_seg tptrseg)
3241 {
3242
3243         return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3244 }
3245
3246 int
3247 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3248     struct timeval *tptr, enum uio_seg tptrseg)
3249 {
3250         struct nameidata nd;
3251         struct timespec ts[2];
3252         int error, vfslocked;
3253
3254         if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3255                 return (error);
3256         NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3257             fd, td);
3258
3259         if ((error = namei(&nd)) != 0)
3260                 return (error);
3261         vfslocked = NDHASGIANT(&nd);
3262         NDFREE(&nd, NDF_ONLY_PNBUF);
3263         error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3264         vrele(nd.ni_vp);
3265         VFS_UNLOCK_GIANT(vfslocked);
3266         return (error);
3267 }
3268
3269 /*
3270  * Set the access and modification times of a file.
3271  */
3272 #ifndef _SYS_SYSPROTO_H_
3273 struct lutimes_args {
3274         char    *path;
3275         struct  timeval *tptr;
3276 };
3277 #endif
3278 int
3279 lutimes(td, uap)
3280         struct thread *td;
3281         register struct lutimes_args /* {
3282                 char *path;
3283                 struct timeval *tptr;
3284         } */ *uap;
3285 {
3286
3287         return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3288             UIO_USERSPACE));
3289 }
3290
3291 int
3292 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3293     struct timeval *tptr, enum uio_seg tptrseg)
3294 {
3295         struct timespec ts[2];
3296         int error;
3297         struct nameidata nd;
3298         int vfslocked;
3299
3300         if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3301                 return (error);
3302         NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3303         if ((error = namei(&nd)) != 0)
3304                 return (error);
3305         vfslocked = NDHASGIANT(&nd);
3306         NDFREE(&nd, NDF_ONLY_PNBUF);
3307         error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3308         vrele(nd.ni_vp);
3309         VFS_UNLOCK_GIANT(vfslocked);
3310         return (error);
3311 }
3312
3313 /*
3314  * Set the access and modification times of a file.
3315  */
3316 #ifndef _SYS_SYSPROTO_H_
3317 struct futimes_args {
3318         int     fd;
3319         struct  timeval *tptr;
3320 };
3321 #endif
3322 int
3323 futimes(td, uap)
3324         struct thread *td;
3325         register struct futimes_args /* {
3326                 int  fd;
3327                 struct timeval *tptr;
3328         } */ *uap;
3329 {
3330
3331         return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3332 }
3333
3334 int
3335 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3336     enum uio_seg tptrseg)
3337 {
3338         struct timespec ts[2];
3339         struct file *fp;
3340         int vfslocked;
3341         int error;
3342
3343         AUDIT_ARG_FD(fd);
3344         if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3345                 return (error);
3346         if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3347                 return (error);
3348         vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3349 #ifdef AUDIT
3350         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3351         AUDIT_ARG_VNODE1(fp->f_vnode);
3352         VOP_UNLOCK(fp->f_vnode, 0);
3353 #endif
3354         error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3355         VFS_UNLOCK_GIANT(vfslocked);
3356         fdrop(fp, td);
3357         return (error);
3358 }
3359
3360 /*
3361  * Truncate a file given its path name.
3362  */
3363 #ifndef _SYS_SYSPROTO_H_
3364 struct truncate_args {
3365         char    *path;
3366         int     pad;
3367         off_t   length;
3368 };
3369 #endif
3370 int
3371 truncate(td, uap)
3372         struct thread *td;
3373         register struct truncate_args /* {
3374                 char *path;
3375                 int pad;
3376                 off_t length;
3377         } */ *uap;
3378 {
3379
3380         return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3381 }
3382
3383 int
3384 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3385 {
3386         struct mount *mp;
3387         struct vnode *vp;
3388         struct vattr vattr;
3389         int error;
3390         struct nameidata nd;
3391         int vfslocked;
3392
3393         if (length < 0)
3394                 return(EINVAL);
3395         NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3396         if ((error = namei(&nd)) != 0)
3397                 return (error);
3398         vfslocked = NDHASGIANT(&nd);
3399         vp = nd.ni_vp;
3400         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3401                 vrele(vp);
3402                 VFS_UNLOCK_GIANT(vfslocked);
3403                 return (error);
3404         }
3405         NDFREE(&nd, NDF_ONLY_PNBUF);
3406         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3407         if (vp->v_type == VDIR)
3408                 error = EISDIR;
3409 #ifdef MAC
3410         else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3411         }
3412 #endif
3413         else if ((error = vn_writechk(vp)) == 0 &&
3414             (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3415                 VATTR_NULL(&vattr);
3416                 vattr.va_size = length;
3417                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3418         }
3419         vput(vp);
3420         vn_finished_write(mp);
3421         VFS_UNLOCK_GIANT(vfslocked);
3422         return (error);
3423 }
3424
3425 #if defined(COMPAT_43)
3426 /*
3427  * Truncate a file given its path name.
3428  */
3429 #ifndef _SYS_SYSPROTO_H_
3430 struct otruncate_args {
3431         char    *path;
3432         long    length;
3433 };
3434 #endif
3435 int
3436 otruncate(td, uap)
3437         struct thread *td;
3438         register struct otruncate_args /* {
3439                 char *path;
3440                 long length;
3441         } */ *uap;
3442 {
3443         struct truncate_args /* {
3444                 char *path;
3445                 int pad;
3446                 off_t length;
3447         } */ nuap;
3448
3449         nuap.path = uap->path;
3450         nuap.length = uap->length;
3451         return (truncate(td, &nuap));
3452 }
3453 #endif /* COMPAT_43 */
3454
3455 /* Versions with the pad argument */
3456 int
3457 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3458 {
3459         struct truncate_args ouap;
3460
3461         ouap.path = uap->path;
3462         ouap.length = uap->length;
3463         return (truncate(td, &ouap));
3464 }
3465
3466 int
3467 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3468 {
3469         struct ftruncate_args ouap;
3470
3471         ouap.fd = uap->fd;
3472         ouap.length = uap->length;
3473         return (ftruncate(td, &ouap));
3474 }
3475
3476 /*
3477  * Sync an open file.
3478  */
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct fsync_args {
3481         int     fd;
3482 };
3483 #endif
3484 int
3485 fsync(td, uap)
3486         struct thread *td;
3487         struct fsync_args /* {
3488                 int fd;
3489         } */ *uap;
3490 {
3491         struct vnode *vp;
3492         struct mount *mp;
3493         struct file *fp;
3494         int vfslocked;
3495         int error, lock_flags;
3496
3497         AUDIT_ARG_FD(uap->fd);
3498         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3499                 return (error);
3500         vp = fp->f_vnode;
3501         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3502         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3503                 goto drop;
3504         if (MNT_SHARED_WRITES(mp) ||
3505             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3506                 lock_flags = LK_SHARED;
3507         } else {
3508                 lock_flags = LK_EXCLUSIVE;
3509         }
3510         vn_lock(vp, lock_flags | LK_RETRY);
3511         AUDIT_ARG_VNODE1(vp);
3512         if (vp->v_object != NULL) {
3513                 VM_OBJECT_LOCK(vp->v_object);
3514                 vm_object_page_clean(vp->v_object, 0, 0, 0);
3515                 VM_OBJECT_UNLOCK(vp->v_object);
3516         }
3517         error = VOP_FSYNC(vp, MNT_WAIT, td);
3518
3519         VOP_UNLOCK(vp, 0);
3520         vn_finished_write(mp);
3521 drop:
3522         VFS_UNLOCK_GIANT(vfslocked);
3523         fdrop(fp, td);
3524         return (error);
3525 }
3526
3527 /*
3528  * Rename files.  Source and destination must either both be directories, or
3529  * both not be directories.  If target is a directory, it must be empty.
3530  */
3531 #ifndef _SYS_SYSPROTO_H_
3532 struct rename_args {
3533         char    *from;
3534         char    *to;
3535 };
3536 #endif
3537 int
3538 rename(td, uap)
3539         struct thread *td;
3540         register struct rename_args /* {
3541                 char *from;
3542                 char *to;
3543         } */ *uap;
3544 {
3545
3546         return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3547 }
3548
3549 #ifndef _SYS_SYSPROTO_H_
3550 struct renameat_args {
3551         int     oldfd;
3552         char    *old;
3553         int     newfd;
3554         char    *new;
3555 };
3556 #endif
3557 int
3558 renameat(struct thread *td, struct renameat_args *uap)
3559 {
3560
3561         return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3562             UIO_USERSPACE));
3563 }
3564
3565 int
3566 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3567 {
3568
3569         return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3570 }
3571
3572 int
3573 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3574     enum uio_seg pathseg)
3575 {
3576         struct mount *mp = NULL;
3577         struct vnode *tvp, *fvp, *tdvp;
3578         struct nameidata fromnd, tond;
3579         int tvfslocked;
3580         int fvfslocked;
3581         int error;
3582
3583         bwillwrite();
3584 #ifdef MAC
3585         NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3586             AUDITVNODE1, pathseg, old, oldfd, td);
3587 #else
3588         NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3589             AUDITVNODE1, pathseg, old, oldfd, td);
3590 #endif
3591
3592         if ((error = namei(&fromnd)) != 0)
3593                 return (error);
3594         fvfslocked = NDHASGIANT(&fromnd);
3595         tvfslocked = 0;
3596 #ifdef MAC
3597         error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3598             fromnd.ni_vp, &fromnd.ni_cnd);
3599         VOP_UNLOCK(fromnd.ni_dvp, 0);
3600         if (fromnd.ni_dvp != fromnd.ni_vp)
3601                 VOP_UNLOCK(fromnd.ni_vp, 0);
3602 #endif
3603         fvp = fromnd.ni_vp;
3604         if (error == 0)
3605                 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3606         if (error != 0) {
3607                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3608                 vrele(fromnd.ni_dvp);
3609                 vrele(fvp);
3610                 goto out1;
3611         }
3612         NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3613             MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3614         if (fromnd.ni_vp->v_type == VDIR)
3615                 tond.ni_cnd.cn_flags |= WILLBEDIR;
3616         if ((error = namei(&tond)) != 0) {
3617                 /* Translate error code for rename("dir1", "dir2/."). */
3618                 if (error == EISDIR && fvp->v_type == VDIR)
3619                         error = EINVAL;
3620                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3621                 vrele(fromnd.ni_dvp);
3622                 vrele(fvp);
3623                 vn_finished_write(mp);
3624                 goto out1;
3625         }
3626         tvfslocked = NDHASGIANT(&tond);
3627         tdvp = tond.ni_dvp;
3628         tvp = tond.ni_vp;
3629         if (tvp != NULL) {
3630                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3631                         error = ENOTDIR;
3632                         goto out;
3633                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3634                         error = EISDIR;
3635                         goto out;
3636                 }
3637         }
3638         if (fvp == tdvp) {
3639                 error = EINVAL;
3640                 goto out;
3641         }
3642         /*
3643          * If the source is the same as the destination (that is, if they
3644          * are links to the same vnode), then there is nothing to do.
3645          */
3646         if (fvp == tvp)
3647                 error = -1;
3648 #ifdef MAC
3649         else
3650                 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3651                     tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3652 #endif
3653 out:
3654         if (!error) {
3655                 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3656                                    tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3657                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3658                 NDFREE(&tond, NDF_ONLY_PNBUF);
3659         } else {
3660                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3661                 NDFREE(&tond, NDF_ONLY_PNBUF);
3662                 if (tvp)
3663                         vput(tvp);
3664                 if (tdvp == tvp)
3665                         vrele(tdvp);
3666                 else
3667                         vput(tdvp);
3668                 vrele(fromnd.ni_dvp);
3669                 vrele(fvp);
3670         }
3671         vrele(tond.ni_startdir);
3672         vn_finished_write(mp);
3673 out1:
3674         if (fromnd.ni_startdir)
3675                 vrele(fromnd.ni_startdir);
3676         VFS_UNLOCK_GIANT(fvfslocked);
3677         VFS_UNLOCK_GIANT(tvfslocked);
3678         if (error == -1)
3679                 return (0);
3680         return (error);
3681 }
3682
3683 /*
3684  * Make a directory file.
3685  */
3686 #ifndef _SYS_SYSPROTO_H_
3687 struct mkdir_args {
3688         char    *path;
3689         int     mode;
3690 };
3691 #endif
3692 int
3693 mkdir(td, uap)
3694         struct thread *td;
3695         register struct mkdir_args /* {
3696                 char *path;
3697                 int mode;
3698         } */ *uap;
3699 {
3700
3701         return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3702 }
3703
3704 #ifndef _SYS_SYSPROTO_H_
3705 struct mkdirat_args {
3706         int     fd;
3707         char    *path;
3708         mode_t  mode;
3709 };
3710 #endif
3711 int
3712 mkdirat(struct thread *td, struct mkdirat_args *uap)
3713 {
3714
3715         return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3716 }
3717
3718 int
3719 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3720 {
3721
3722         return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3723 }
3724
3725 int
3726 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3727     int mode)
3728 {
3729         struct mount *mp;
3730         struct vnode *vp;
3731         struct vattr vattr;
3732         int error;
3733         struct nameidata nd;
3734         int vfslocked;
3735
3736         AUDIT_ARG_MODE(mode);
3737 restart:
3738         bwillwrite();
3739         NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3740             segflg, path, fd, td);
3741         nd.ni_cnd.cn_flags |= WILLBEDIR;
3742         if ((error = namei(&nd)) != 0)
3743                 return (error);
3744         vfslocked = NDHASGIANT(&nd);
3745         vp = nd.ni_vp;
3746         if (vp != NULL) {
3747                 NDFREE(&nd, NDF_ONLY_PNBUF);
3748                 /*
3749                  * XXX namei called with LOCKPARENT but not LOCKLEAF has
3750                  * the strange behaviour of leaving the vnode unlocked
3751                  * if the target is the same vnode as the parent.
3752                  */
3753                 if (vp == nd.ni_dvp)
3754                         vrele(nd.ni_dvp);
3755                 else
3756                         vput(nd.ni_dvp);
3757                 vrele(vp);
3758                 VFS_UNLOCK_GIANT(vfslocked);
3759                 return (EEXIST);
3760         }
3761         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3762                 NDFREE(&nd, NDF_ONLY_PNBUF);
3763                 vput(nd.ni_dvp);
3764                 VFS_UNLOCK_GIANT(vfslocked);
3765                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3766                         return (error);
3767                 goto restart;
3768         }
3769         VATTR_NULL(&vattr);
3770         vattr.va_type = VDIR;
3771         vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3772 #ifdef MAC
3773         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3774             &vattr);
3775         if (error)
3776                 goto out;
3777 #endif
3778         error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3779 #ifdef MAC
3780 out:
3781 #endif
3782         NDFREE(&nd, NDF_ONLY_PNBUF);
3783         vput(nd.ni_dvp);
3784         if (!error)
3785                 vput(nd.ni_vp);
3786         vn_finished_write(mp);
3787         VFS_UNLOCK_GIANT(vfslocked);
3788         return (error);
3789 }
3790
3791 /*
3792  * Remove a directory file.
3793  */
3794 #ifndef _SYS_SYSPROTO_H_
3795 struct rmdir_args {
3796         char    *path;
3797 };
3798 #endif
3799 int
3800 rmdir(td, uap)
3801         struct thread *td;
3802         struct rmdir_args /* {
3803                 char *path;
3804         } */ *uap;
3805 {
3806
3807         return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3808 }
3809
3810 int
3811 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3812 {
3813
3814         return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3815 }
3816
3817 int
3818 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3819 {
3820         struct mount *mp;
3821         struct vnode *vp;
3822         int error;
3823         struct nameidata nd;
3824         int vfslocked;
3825
3826 restart:
3827         bwillwrite();
3828         NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3829             pathseg, path, fd, td);
3830         if ((error = namei(&nd)) != 0)
3831                 return (error);
3832         vfslocked = NDHASGIANT(&nd);
3833         vp = nd.ni_vp;
3834         if (vp->v_type != VDIR) {
3835                 error = ENOTDIR;
3836                 goto out;
3837         }
3838         /*
3839          * No rmdir "." please.
3840          */
3841         if (nd.ni_dvp == vp) {
3842                 error = EINVAL;
3843                 goto out;
3844         }
3845         /*
3846          * The root of a mounted filesystem cannot be deleted.
3847          */
3848         if (vp->v_vflag & VV_ROOT) {
3849                 error = EBUSY;
3850                 goto out;
3851         }
3852 #ifdef MAC
3853         error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3854             &nd.ni_cnd);
3855         if (error)
3856                 goto out;
3857 #endif
3858         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3859                 NDFREE(&nd, NDF_ONLY_PNBUF);
3860                 vput(vp);
3861                 if (nd.ni_dvp == vp)
3862                         vrele(nd.ni_dvp);
3863                 else
3864                         vput(nd.ni_dvp);
3865                 VFS_UNLOCK_GIANT(vfslocked);
3866                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3867                         return (error);
3868                 goto restart;
3869         }
3870         error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3871         vn_finished_write(mp);
3872 out:
3873         NDFREE(&nd, NDF_ONLY_PNBUF);
3874         vput(vp);
3875         if (nd.ni_dvp == vp)
3876                 vrele(nd.ni_dvp);
3877         else
3878                 vput(nd.ni_dvp);
3879         VFS_UNLOCK_GIANT(vfslocked);
3880         return (error);
3881 }
3882
3883 #ifdef COMPAT_43
3884 /*
3885  * Read a block of directory entries in a filesystem independent format.
3886  */
3887 #ifndef _SYS_SYSPROTO_H_
3888 struct ogetdirentries_args {
3889         int     fd;
3890         char    *buf;
3891         u_int   count;
3892         long    *basep;
3893 };
3894 #endif
3895 int
3896 ogetdirentries(td, uap)
3897         struct thread *td;
3898         register struct ogetdirentries_args /* {
3899                 int fd;
3900                 char *buf;
3901                 u_int count;
3902                 long *basep;
3903         } */ *uap;
3904 {
3905         struct vnode *vp;
3906         struct file *fp;
3907         struct uio auio, kuio;
3908         struct iovec aiov, kiov;
3909         struct dirent *dp, *edp;
3910         caddr_t dirbuf;
3911         int error, eofflag, readcnt, vfslocked;
3912         long loff;
3913
3914         /* XXX arbitrary sanity limit on `count'. */
3915         if (uap->count > 64 * 1024)
3916                 return (EINVAL);
3917         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3918                 return (error);
3919         if ((fp->f_flag & FREAD) == 0) {
3920                 fdrop(fp, td);
3921                 return (EBADF);
3922         }
3923         vp = fp->f_vnode;
3924 unionread:
3925         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3926         if (vp->v_type != VDIR) {
3927                 VFS_UNLOCK_GIANT(vfslocked);
3928                 fdrop(fp, td);
3929                 return (EINVAL);
3930         }
3931         aiov.iov_base = uap->buf;
3932         aiov.iov_len = uap->count;
3933         auio.uio_iov = &aiov;
3934         auio.uio_iovcnt = 1;
3935         auio.uio_rw = UIO_READ;
3936         auio.uio_segflg = UIO_USERSPACE;
3937         auio.uio_td = td;
3938         auio.uio_resid = uap->count;
3939         vn_lock(vp, LK_SHARED | LK_RETRY);
3940         loff = auio.uio_offset = fp->f_offset;
3941 #ifdef MAC
3942         error = mac_vnode_check_readdir(td->td_ucred, vp);
3943         if (error) {
3944                 VOP_UNLOCK(vp, 0);
3945                 VFS_UNLOCK_GIANT(vfslocked);
3946                 fdrop(fp, td);
3947                 return (error);
3948         }
3949 #endif
3950 #       if (BYTE_ORDER != LITTLE_ENDIAN)
3951                 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3952                         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3953                             NULL, NULL);
3954                         fp->f_offset = auio.uio_offset;
3955                 } else
3956 #       endif
3957         {
3958                 kuio = auio;
3959                 kuio.uio_iov = &kiov;
3960                 kuio.uio_segflg = UIO_SYSSPACE;
3961                 kiov.iov_len = uap->count;
3962                 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3963                 kiov.iov_base = dirbuf;
3964                 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3965                             NULL, NULL);
3966                 fp->f_offset = kuio.uio_offset;
3967                 if (error == 0) {
3968                         readcnt = uap->count - kuio.uio_resid;
3969                         edp = (struct dirent *)&dirbuf[readcnt];
3970                         for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3971 #                               if (BYTE_ORDER == LITTLE_ENDIAN)
3972                                         /*
3973                                          * The expected low byte of
3974                                          * dp->d_namlen is our dp->d_type.
3975                                          * The high MBZ byte of dp->d_namlen
3976                                          * is our dp->d_namlen.
3977                                          */
3978                                         dp->d_type = dp->d_namlen;
3979                                         dp->d_namlen = 0;
3980 #                               else
3981                                         /*
3982                                          * The dp->d_type is the high byte
3983                                          * of the expected dp->d_namlen,
3984                                          * so must be zero'ed.
3985                                          */
3986                                         dp->d_type = 0;
3987 #                               endif
3988                                 if (dp->d_reclen > 0) {
3989                                         dp = (struct dirent *)
3990                                             ((char *)dp + dp->d_reclen);
3991                                 } else {
3992                                         error = EIO;
3993                                         break;
3994                                 }
3995                         }
3996                         if (dp >= edp)
3997                                 error = uiomove(dirbuf, readcnt, &auio);
3998                 }
3999                 free(dirbuf, M_TEMP);
4000         }
4001         if (error) {
4002                 VOP_UNLOCK(vp, 0);
4003                 VFS_UNLOCK_GIANT(vfslocked);
4004                 fdrop(fp, td);
4005                 return (error);
4006         }
4007         if (uap->count == auio.uio_resid &&
4008             (vp->v_vflag & VV_ROOT) &&
4009             (vp->v_mount->mnt_flag & MNT_UNION)) {
4010                 struct vnode *tvp = vp;
4011                 vp = vp->v_mount->mnt_vnodecovered;
4012                 VREF(vp);
4013                 fp->f_vnode = vp;
4014                 fp->f_data = vp;
4015                 fp->f_offset = 0;
4016                 vput(tvp);
4017                 VFS_UNLOCK_GIANT(vfslocked);
4018                 goto unionread;
4019         }
4020         VOP_UNLOCK(vp, 0);
4021         VFS_UNLOCK_GIANT(vfslocked);
4022         error = copyout(&loff, uap->basep, sizeof(long));
4023         fdrop(fp, td);
4024         td->td_retval[0] = uap->count - auio.uio_resid;
4025         return (error);
4026 }
4027 #endif /* COMPAT_43 */
4028
4029 /*
4030  * Read a block of directory entries in a filesystem independent format.
4031  */
4032 #ifndef _SYS_SYSPROTO_H_
4033 struct getdirentries_args {
4034         int     fd;
4035         char    *buf;
4036         u_int   count;
4037         long    *basep;
4038 };
4039 #endif
4040 int
4041 getdirentries(td, uap)
4042         struct thread *td;
4043         register struct getdirentries_args /* {
4044                 int fd;
4045                 char *buf;
4046                 u_int count;
4047                 long *basep;
4048         } */ *uap;
4049 {
4050         long base;
4051         int error;
4052
4053         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4054         if (error)
4055                 return (error);
4056         if (uap->basep != NULL)
4057                 error = copyout(&base, uap->basep, sizeof(long));
4058         return (error);
4059 }
4060
4061 int
4062 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4063     long *basep)
4064 {
4065         struct vnode *vp;
4066         struct file *fp;
4067         struct uio auio;
4068         struct iovec aiov;
4069         int vfslocked;
4070         long loff;
4071         int error, eofflag;
4072
4073         AUDIT_ARG_FD(fd);
4074         if (count > INT_MAX)
4075                 return (EINVAL);
4076         if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4077                 return (error);
4078         if ((fp->f_flag & FREAD) == 0) {
4079                 fdrop(fp, td);
4080                 return (EBADF);
4081         }
4082         vp = fp->f_vnode;
4083 unionread:
4084         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4085         if (vp->v_type != VDIR) {
4086                 VFS_UNLOCK_GIANT(vfslocked);
4087                 error = EINVAL;
4088                 goto fail;
4089         }
4090         aiov.iov_base = buf;
4091         aiov.iov_len = count;
4092         auio.uio_iov = &aiov;
4093         auio.uio_iovcnt = 1;
4094         auio.uio_rw = UIO_READ;
4095         auio.uio_segflg = UIO_USERSPACE;
4096         auio.uio_td = td;
4097         auio.uio_resid = count;
4098         vn_lock(vp, LK_SHARED | LK_RETRY);
4099         AUDIT_ARG_VNODE1(vp);
4100         loff = auio.uio_offset = fp->f_offset;
4101 #ifdef MAC
4102         error = mac_vnode_check_readdir(td->td_ucred, vp);
4103         if (error == 0)
4104 #endif
4105                 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4106                     NULL);
4107         fp->f_offset = auio.uio_offset;
4108         if (error) {
4109                 VOP_UNLOCK(vp, 0);
4110                 VFS_UNLOCK_GIANT(vfslocked);
4111                 goto fail;
4112         }
4113         if (count == auio.uio_resid &&
4114             (vp->v_vflag & VV_ROOT) &&
4115             (vp->v_mount->mnt_flag & MNT_UNION)) {
4116                 struct vnode *tvp = vp;
4117                 vp = vp->v_mount->mnt_vnodecovered;
4118                 VREF(vp);
4119                 fp->f_vnode = vp;
4120                 fp->f_data = vp;
4121                 fp->f_offset = 0;
4122                 vput(tvp);
4123                 VFS_UNLOCK_GIANT(vfslocked);
4124                 goto unionread;
4125         }
4126         VOP_UNLOCK(vp, 0);
4127         VFS_UNLOCK_GIANT(vfslocked);
4128         *basep = loff;
4129         td->td_retval[0] = count - auio.uio_resid;
4130 fail:
4131         fdrop(fp, td);
4132         return (error);
4133 }
4134
4135 #ifndef _SYS_SYSPROTO_H_
4136 struct getdents_args {
4137         int fd;
4138         char *buf;
4139         size_t count;
4140 };
4141 #endif
4142 int
4143 getdents(td, uap)
4144         struct thread *td;
4145         register struct getdents_args /* {
4146                 int fd;
4147                 char *buf;
4148                 u_int count;
4149         } */ *uap;
4150 {
4151         struct getdirentries_args ap;
4152         ap.fd = uap->fd;
4153         ap.buf = uap->buf;
4154         ap.count = uap->count;
4155         ap.basep = NULL;
4156         return (getdirentries(td, &ap));
4157 }
4158
4159 /*
4160  * Set the mode mask for creation of filesystem nodes.
4161  */
4162 #ifndef _SYS_SYSPROTO_H_
4163 struct umask_args {
4164         int     newmask;
4165 };
4166 #endif
4167 int
4168 umask(td, uap)
4169         struct thread *td;
4170         struct umask_args /* {
4171                 int newmask;
4172         } */ *uap;
4173 {
4174         register struct filedesc *fdp;
4175
4176         FILEDESC_XLOCK(td->td_proc->p_fd);
4177         fdp = td->td_proc->p_fd;
4178         td->td_retval[0] = fdp->fd_cmask;
4179         fdp->fd_cmask = uap->newmask & ALLPERMS;
4180         FILEDESC_XUNLOCK(td->td_proc->p_fd);
4181         return (0);
4182 }
4183
4184 /*
4185  * Void all references to file by ripping underlying filesystem away from
4186  * vnode.
4187  */
4188 #ifndef _SYS_SYSPROTO_H_
4189 struct revoke_args {
4190         char    *path;
4191 };
4192 #endif
4193 int
4194 revoke(td, uap)
4195         struct thread *td;
4196         register struct revoke_args /* {
4197                 char *path;
4198         } */ *uap;
4199 {
4200         struct vnode *vp;
4201         struct vattr vattr;
4202         int error;
4203         struct nameidata nd;
4204         int vfslocked;
4205
4206         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4207             UIO_USERSPACE, uap->path, td);
4208         if ((error = namei(&nd)) != 0)
4209                 return (error);
4210         vfslocked = NDHASGIANT(&nd);
4211         vp = nd.ni_vp;
4212         NDFREE(&nd, NDF_ONLY_PNBUF);
4213         if (vp->v_type != VCHR) {
4214                 error = EINVAL;
4215                 goto out;
4216         }
4217 #ifdef MAC
4218         error = mac_vnode_check_revoke(td->td_ucred, vp);
4219         if (error)
4220                 goto out;
4221 #endif
4222         error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4223         if (error)
4224                 goto out;
4225         if (td->td_ucred->cr_uid != vattr.va_uid) {
4226                 error = priv_check(td, PRIV_VFS_ADMIN);
4227                 if (error)
4228                         goto out;
4229         }
4230         if (vcount(vp) > 1)
4231                 VOP_REVOKE(vp, REVOKEALL);
4232 out:
4233         vput(vp);
4234         VFS_UNLOCK_GIANT(vfslocked);
4235         return (error);
4236 }
4237
4238 /*
4239  * Convert a user file descriptor to a kernel file entry.
4240  * A reference on the file entry is held upon returning.
4241  */
4242 int
4243 getvnode(fdp, fd, fpp)
4244         struct filedesc *fdp;
4245         int fd;
4246         struct file **fpp;
4247 {
4248         int error;
4249         struct file *fp;
4250
4251         error = 0;
4252         fp = NULL;
4253         if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4254                 error = EBADF;
4255         else if (fp->f_vnode == NULL) {
4256                 error = EINVAL;
4257                 fdrop(fp, curthread);
4258         }
4259         *fpp = fp;
4260         return (error);
4261 }
4262
4263 /*
4264  * Get an (NFS) file handle.
4265  */
4266 #ifndef _SYS_SYSPROTO_H_
4267 struct lgetfh_args {
4268         char    *fname;
4269         fhandle_t *fhp;
4270 };
4271 #endif
4272 int
4273 lgetfh(td, uap)
4274         struct thread *td;
4275         register struct lgetfh_args *uap;
4276 {
4277         struct nameidata nd;
4278         fhandle_t fh;
4279         register struct vnode *vp;
4280         int vfslocked;
4281         int error;
4282
4283         error = priv_check(td, PRIV_VFS_GETFH);
4284         if (error)
4285                 return (error);
4286         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4287             UIO_USERSPACE, uap->fname, td);
4288         error = namei(&nd);
4289         if (error)
4290                 return (error);
4291         vfslocked = NDHASGIANT(&nd);
4292         NDFREE(&nd, NDF_ONLY_PNBUF);
4293         vp = nd.ni_vp;
4294         bzero(&fh, sizeof(fh));
4295         fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4296         error = VOP_VPTOFH(vp, &fh.fh_fid);
4297         vput(vp);
4298         VFS_UNLOCK_GIANT(vfslocked);
4299         if (error)
4300                 return (error);
4301         error = copyout(&fh, uap->fhp, sizeof (fh));
4302         return (error);
4303 }
4304
4305 #ifndef _SYS_SYSPROTO_H_
4306 struct getfh_args {
4307         char    *fname;
4308         fhandle_t *fhp;
4309 };
4310 #endif
4311 int
4312 getfh(td, uap)
4313         struct thread *td;
4314         register struct getfh_args *uap;
4315 {
4316         struct nameidata nd;
4317         fhandle_t fh;
4318         register struct vnode *vp;
4319         int vfslocked;
4320         int error;
4321
4322         error = priv_check(td, PRIV_VFS_GETFH);
4323         if (error)
4324                 return (error);
4325         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4326             UIO_USERSPACE, uap->fname, td);
4327         error = namei(&nd);
4328         if (error)
4329                 return (error);
4330         vfslocked = NDHASGIANT(&nd);
4331         NDFREE(&nd, NDF_ONLY_PNBUF);
4332         vp = nd.ni_vp;
4333         bzero(&fh, sizeof(fh));
4334         fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4335         error = VOP_VPTOFH(vp, &fh.fh_fid);
4336         vput(vp);
4337         VFS_UNLOCK_GIANT(vfslocked);
4338         if (error)
4339                 return (error);
4340         error = copyout(&fh, uap->fhp, sizeof (fh));
4341         return (error);
4342 }
4343
4344 /*
4345  * syscall for the rpc.lockd to use to translate a NFS file handle into an
4346  * open descriptor.
4347  *
4348  * warning: do not remove the priv_check() call or this becomes one giant
4349  * security hole.
4350  */
4351 #ifndef _SYS_SYSPROTO_H_
4352 struct fhopen_args {
4353         const struct fhandle *u_fhp;
4354         int flags;
4355 };
4356 #endif
4357 int
4358 fhopen(td, uap)
4359         struct thread *td;
4360         struct fhopen_args /* {
4361                 const struct fhandle *u_fhp;
4362                 int flags;
4363         } */ *uap;
4364 {
4365         struct proc *p = td->td_proc;
4366         struct mount *mp;
4367         struct vnode *vp;
4368         struct fhandle fhp;
4369         struct vattr vat;
4370         struct vattr *vap = &vat;
4371         struct flock lf;
4372         struct file *fp;
4373         register struct filedesc *fdp = p->p_fd;
4374         int fmode, error, type;
4375         accmode_t accmode;
4376         struct file *nfp;
4377         int vfslocked;
4378         int indx;
4379
4380         error = priv_check(td, PRIV_VFS_FHOPEN);
4381         if (error)
4382                 return (error);
4383         fmode = FFLAGS(uap->flags);
4384         /* why not allow a non-read/write open for our lockd? */
4385         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4386                 return (EINVAL);
4387         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4388         if (error)
4389                 return(error);
4390         /* find the mount point */
4391         mp = vfs_busyfs(&fhp.fh_fsid);
4392         if (mp == NULL)
4393                 return (ESTALE);
4394         vfslocked = VFS_LOCK_GIANT(mp);
4395         /* now give me my vnode, it gets returned to me locked */
4396         error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4397         vfs_unbusy(mp);
4398         if (error)
4399                 goto out;
4400         /*
4401          * from now on we have to make sure not
4402          * to forget about the vnode
4403          * any error that causes an abort must vput(vp)
4404          * just set error = err and 'goto bad;'.
4405          */
4406
4407         /*
4408          * from vn_open
4409          */
4410         if (vp->v_type == VLNK) {
4411                 error = EMLINK;
4412                 goto bad;
4413         }
4414         if (vp->v_type == VSOCK) {
4415                 error = EOPNOTSUPP;
4416                 goto bad;
4417         }
4418         accmode = 0;
4419         if (fmode & (FWRITE | O_TRUNC)) {
4420                 if (vp->v_type == VDIR) {
4421                         error = EISDIR;
4422                         goto bad;
4423                 }
4424                 error = vn_writechk(vp);
4425                 if (error)
4426                         goto bad;
4427                 accmode |= VWRITE;
4428         }
4429         if (fmode & FREAD)
4430                 accmode |= VREAD;
4431         if ((fmode & O_APPEND) && (fmode & FWRITE))
4432                 accmode |= VAPPEND;
4433 #ifdef MAC
4434         error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4435         if (error)
4436                 goto bad;
4437 #endif
4438         if (accmode) {
4439                 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4440                 if (error)
4441                         goto bad;
4442         }
4443         if (fmode & O_TRUNC) {
4444                 vfs_ref(mp);
4445                 VOP_UNLOCK(vp, 0);                              /* XXX */
4446                 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4447                         vrele(vp);
4448                         vfs_rel(mp);
4449                         goto out;
4450                 }
4451                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
4452                 vfs_rel(mp);
4453 #ifdef MAC
4454                 /*
4455                  * We don't yet have fp->f_cred, so use td->td_ucred, which
4456                  * should be right.
4457                  */
4458                 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4459                 if (error == 0) {
4460 #endif
4461                         VATTR_NULL(vap);
4462                         vap->va_size = 0;
4463                         error = VOP_SETATTR(vp, vap, td->td_ucred);
4464 #ifdef MAC
4465                 }
4466 #endif
4467                 vn_finished_write(mp);
4468                 if (error)
4469                         goto bad;
4470         }
4471         error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4472         if (error)
4473                 goto bad;
4474
4475         if (fmode & FWRITE)
4476                 vp->v_writecount++;
4477
4478         /*
4479          * end of vn_open code
4480          */
4481
4482         if ((error = falloc(td, &nfp, &indx)) != 0) {
4483                 if (fmode & FWRITE)
4484                         vp->v_writecount--;
4485                 goto bad;
4486         }
4487         /* An extra reference on `nfp' has been held for us by falloc(). */
4488         fp = nfp;
4489         nfp->f_vnode = vp;
4490         finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4491         if (fmode & (O_EXLOCK | O_SHLOCK)) {
4492                 lf.l_whence = SEEK_SET;
4493                 lf.l_start = 0;
4494                 lf.l_len = 0;
4495                 if (fmode & O_EXLOCK)
4496                         lf.l_type = F_WRLCK;
4497                 else
4498                         lf.l_type = F_RDLCK;
4499                 type = F_FLOCK;
4500                 if ((fmode & FNONBLOCK) == 0)
4501                         type |= F_WAIT;
4502                 VOP_UNLOCK(vp, 0);
4503                 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4504                             type)) != 0) {
4505                         /*
4506                          * The lock request failed.  Normally close the
4507                          * descriptor but handle the case where someone might
4508                          * have dup()d or close()d it when we weren't looking.
4509                          */
4510                         fdclose(fdp, fp, indx, td);
4511
4512                         /*
4513                          * release our private reference
4514                          */
4515                         fdrop(fp, td);
4516                         goto out;
4517                 }
4518                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4519                 atomic_set_int(&fp->f_flag, FHASLOCK);
4520         }
4521
4522         VOP_UNLOCK(vp, 0);
4523         fdrop(fp, td);
4524         VFS_UNLOCK_GIANT(vfslocked);
4525         td->td_retval[0] = indx;
4526         return (0);
4527
4528 bad:
4529         vput(vp);
4530 out:
4531         VFS_UNLOCK_GIANT(vfslocked);
4532         return (error);
4533 }
4534
4535 /*
4536  * Stat an (NFS) file handle.
4537  */
4538 #ifndef _SYS_SYSPROTO_H_
4539 struct fhstat_args {
4540         struct fhandle *u_fhp;
4541         struct stat *sb;
4542 };
4543 #endif
4544 int
4545 fhstat(td, uap)
4546         struct thread *td;
4547         register struct fhstat_args /* {
4548                 struct fhandle *u_fhp;
4549                 struct stat *sb;
4550         } */ *uap;
4551 {
4552         struct stat sb;
4553         fhandle_t fh;
4554         struct mount *mp;
4555         struct vnode *vp;
4556         int vfslocked;
4557         int error;
4558
4559         error = priv_check(td, PRIV_VFS_FHSTAT);
4560         if (error)
4561                 return (error);
4562         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4563         if (error)
4564                 return (error);
4565         if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4566                 return (ESTALE);
4567         vfslocked = VFS_LOCK_GIANT(mp);
4568         error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4569         vfs_unbusy(mp);
4570         if (error) {
4571                 VFS_UNLOCK_GIANT(vfslocked);
4572                 return (error);
4573         }
4574         error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4575         vput(vp);
4576         VFS_UNLOCK_GIANT(vfslocked);
4577         if (error)
4578                 return (error);
4579         error = copyout(&sb, uap->sb, sizeof(sb));
4580         return (error);
4581 }
4582
4583 /*
4584  * Implement fstatfs() for (NFS) file handles.
4585  */
4586 #ifndef _SYS_SYSPROTO_H_
4587 struct fhstatfs_args {
4588         struct fhandle *u_fhp;
4589         struct statfs *buf;
4590 };
4591 #endif
4592 int
4593 fhstatfs(td, uap)
4594         struct thread *td;
4595         struct fhstatfs_args /* {
4596                 struct fhandle *u_fhp;
4597                 struct statfs *buf;
4598         } */ *uap;
4599 {
4600         struct statfs sf;
4601         fhandle_t fh;
4602         int error;
4603
4604         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4605         if (error)
4606                 return (error);
4607         error = kern_fhstatfs(td, fh, &sf);
4608         if (error)
4609                 return (error);
4610         return (copyout(&sf, uap->buf, sizeof(sf)));
4611 }
4612
4613 int
4614 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4615 {
4616         struct statfs *sp;
4617         struct mount *mp;
4618         struct vnode *vp;
4619         int vfslocked;
4620         int error;
4621
4622         error = priv_check(td, PRIV_VFS_FHSTATFS);
4623         if (error)
4624                 return (error);
4625         if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4626                 return (ESTALE);
4627         vfslocked = VFS_LOCK_GIANT(mp);
4628         error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4629         if (error) {
4630                 vfs_unbusy(mp);
4631                 VFS_UNLOCK_GIANT(vfslocked);
4632                 return (error);
4633         }
4634         vput(vp);
4635         error = prison_canseemount(td->td_ucred, mp);
4636         if (error)
4637                 goto out;
4638 #ifdef MAC
4639         error = mac_mount_check_stat(td->td_ucred, mp);
4640         if (error)
4641                 goto out;
4642 #endif
4643         /*
4644          * Set these in case the underlying filesystem fails to do so.
4645          */
4646         sp = &mp->mnt_stat;
4647         sp->f_version = STATFS_VERSION;
4648         sp->f_namemax = NAME_MAX;
4649         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4650         error = VFS_STATFS(mp, sp);
4651         if (error == 0)
4652                 *buf = *sp;
4653 out:
4654         vfs_unbusy(mp);
4655         VFS_UNLOCK_GIANT(vfslocked);
4656         return (error);
4657 }