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