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