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