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