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