]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_syscalls.c
MFC r325386:
[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_IFWHT:
1191                 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1192                 break;
1193         case S_IFIFO:
1194                 if (dev == 0)
1195                         return (kern_mkfifoat(td, fd, path, pathseg, mode));
1196                 /* FALLTHROUGH */
1197         default:
1198                 error = EINVAL;
1199                 break;
1200         }
1201         if (error != 0)
1202                 return (error);
1203 restart:
1204         bwillwrite();
1205         NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1206             NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1207             td);
1208         if ((error = namei(&nd)) != 0)
1209                 return (error);
1210         vp = nd.ni_vp;
1211         if (vp != NULL) {
1212                 NDFREE(&nd, NDF_ONLY_PNBUF);
1213                 if (vp == nd.ni_dvp)
1214                         vrele(nd.ni_dvp);
1215                 else
1216                         vput(nd.ni_dvp);
1217                 vrele(vp);
1218                 return (EEXIST);
1219         } else {
1220                 VATTR_NULL(&vattr);
1221                 vattr.va_mode = (mode & ALLPERMS) &
1222                     ~td->td_proc->p_fd->fd_cmask;
1223                 vattr.va_rdev = dev;
1224                 whiteout = 0;
1225
1226                 switch (mode & S_IFMT) {
1227                 case S_IFCHR:
1228                         vattr.va_type = VCHR;
1229                         break;
1230                 case S_IFBLK:
1231                         vattr.va_type = VBLK;
1232                         break;
1233                 case S_IFWHT:
1234                         whiteout = 1;
1235                         break;
1236                 default:
1237                         panic("kern_mknod: invalid mode");
1238                 }
1239         }
1240         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1241                 NDFREE(&nd, NDF_ONLY_PNBUF);
1242                 vput(nd.ni_dvp);
1243                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1244                         return (error);
1245                 goto restart;
1246         }
1247 #ifdef MAC
1248         if (error == 0 && !whiteout)
1249                 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1250                     &nd.ni_cnd, &vattr);
1251 #endif
1252         if (error == 0) {
1253                 if (whiteout)
1254                         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1255                 else {
1256                         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1257                                                 &nd.ni_cnd, &vattr);
1258                         if (error == 0)
1259                                 vput(nd.ni_vp);
1260                 }
1261         }
1262         NDFREE(&nd, NDF_ONLY_PNBUF);
1263         vput(nd.ni_dvp);
1264         vn_finished_write(mp);
1265         return (error);
1266 }
1267
1268 /*
1269  * Create a named pipe.
1270  */
1271 #ifndef _SYS_SYSPROTO_H_
1272 struct mkfifo_args {
1273         char    *path;
1274         int     mode;
1275 };
1276 #endif
1277 int
1278 sys_mkfifo(td, uap)
1279         struct thread *td;
1280         register struct mkfifo_args /* {
1281                 char *path;
1282                 int mode;
1283         } */ *uap;
1284 {
1285
1286         return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1287             uap->mode));
1288 }
1289
1290 #ifndef _SYS_SYSPROTO_H_
1291 struct mkfifoat_args {
1292         int     fd;
1293         char    *path;
1294         mode_t  mode;
1295 };
1296 #endif
1297 int
1298 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1299 {
1300
1301         return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1302             uap->mode));
1303 }
1304
1305 int
1306 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1307     int mode)
1308 {
1309         struct mount *mp;
1310         struct vattr vattr;
1311         struct nameidata nd;
1312         cap_rights_t rights;
1313         int error;
1314
1315         AUDIT_ARG_MODE(mode);
1316 restart:
1317         bwillwrite();
1318         NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1319             NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1320             td);
1321         if ((error = namei(&nd)) != 0)
1322                 return (error);
1323         if (nd.ni_vp != NULL) {
1324                 NDFREE(&nd, NDF_ONLY_PNBUF);
1325                 if (nd.ni_vp == nd.ni_dvp)
1326                         vrele(nd.ni_dvp);
1327                 else
1328                         vput(nd.ni_dvp);
1329                 vrele(nd.ni_vp);
1330                 return (EEXIST);
1331         }
1332         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1333                 NDFREE(&nd, NDF_ONLY_PNBUF);
1334                 vput(nd.ni_dvp);
1335                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1336                         return (error);
1337                 goto restart;
1338         }
1339         VATTR_NULL(&vattr);
1340         vattr.va_type = VFIFO;
1341         vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1342 #ifdef MAC
1343         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1344             &vattr);
1345         if (error != 0)
1346                 goto out;
1347 #endif
1348         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1349         if (error == 0)
1350                 vput(nd.ni_vp);
1351 #ifdef MAC
1352 out:
1353 #endif
1354         vput(nd.ni_dvp);
1355         vn_finished_write(mp);
1356         NDFREE(&nd, NDF_ONLY_PNBUF);
1357         return (error);
1358 }
1359
1360 /*
1361  * Make a hard file link.
1362  */
1363 #ifndef _SYS_SYSPROTO_H_
1364 struct link_args {
1365         char    *path;
1366         char    *link;
1367 };
1368 #endif
1369 int
1370 sys_link(td, uap)
1371         struct thread *td;
1372         register struct link_args /* {
1373                 char *path;
1374                 char *link;
1375         } */ *uap;
1376 {
1377
1378         return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1379             UIO_USERSPACE, FOLLOW));
1380 }
1381
1382 #ifndef _SYS_SYSPROTO_H_
1383 struct linkat_args {
1384         int     fd1;
1385         char    *path1;
1386         int     fd2;
1387         char    *path2;
1388         int     flag;
1389 };
1390 #endif
1391 int
1392 sys_linkat(struct thread *td, struct linkat_args *uap)
1393 {
1394         int flag;
1395
1396         flag = uap->flag;
1397         if (flag & ~AT_SYMLINK_FOLLOW)
1398                 return (EINVAL);
1399
1400         return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1401             UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1402 }
1403
1404 int hardlink_check_uid = 0;
1405 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1406     &hardlink_check_uid, 0,
1407     "Unprivileged processes cannot create hard links to files owned by other "
1408     "users");
1409 static int hardlink_check_gid = 0;
1410 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1411     &hardlink_check_gid, 0,
1412     "Unprivileged processes cannot create hard links to files owned by other "
1413     "groups");
1414
1415 static int
1416 can_hardlink(struct vnode *vp, struct ucred *cred)
1417 {
1418         struct vattr va;
1419         int error;
1420
1421         if (!hardlink_check_uid && !hardlink_check_gid)
1422                 return (0);
1423
1424         error = VOP_GETATTR(vp, &va, cred);
1425         if (error != 0)
1426                 return (error);
1427
1428         if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1429                 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1430                 if (error != 0)
1431                         return (error);
1432         }
1433
1434         if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1435                 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1436                 if (error != 0)
1437                         return (error);
1438         }
1439
1440         return (0);
1441 }
1442
1443 int
1444 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1445     enum uio_seg segflg, int follow)
1446 {
1447         struct vnode *vp;
1448         struct mount *mp;
1449         struct nameidata nd;
1450         cap_rights_t rights;
1451         int error;
1452
1453 again:
1454         bwillwrite();
1455         NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1456             cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1457
1458         if ((error = namei(&nd)) != 0)
1459                 return (error);
1460         NDFREE(&nd, NDF_ONLY_PNBUF);
1461         vp = nd.ni_vp;
1462         if (vp->v_type == VDIR) {
1463                 vrele(vp);
1464                 return (EPERM);         /* POSIX */
1465         }
1466         NDINIT_ATRIGHTS(&nd, CREATE,
1467             LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1468             cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1469         if ((error = namei(&nd)) == 0) {
1470                 if (nd.ni_vp != NULL) {
1471                         NDFREE(&nd, NDF_ONLY_PNBUF);
1472                         if (nd.ni_dvp == nd.ni_vp)
1473                                 vrele(nd.ni_dvp);
1474                         else
1475                                 vput(nd.ni_dvp);
1476                         vrele(nd.ni_vp);
1477                         vrele(vp);
1478                         return (EEXIST);
1479                 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1480                         /*
1481                          * Cross-device link.  No need to recheck
1482                          * vp->v_type, since it cannot change, except
1483                          * to VBAD.
1484                          */
1485                         NDFREE(&nd, NDF_ONLY_PNBUF);
1486                         vput(nd.ni_dvp);
1487                         vrele(vp);
1488                         return (EXDEV);
1489                 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1490                         error = can_hardlink(vp, td->td_ucred);
1491 #ifdef MAC
1492                         if (error == 0)
1493                                 error = mac_vnode_check_link(td->td_ucred,
1494                                     nd.ni_dvp, vp, &nd.ni_cnd);
1495 #endif
1496                         if (error != 0) {
1497                                 vput(vp);
1498                                 vput(nd.ni_dvp);
1499                                 NDFREE(&nd, NDF_ONLY_PNBUF);
1500                                 return (error);
1501                         }
1502                         error = vn_start_write(vp, &mp, V_NOWAIT);
1503                         if (error != 0) {
1504                                 vput(vp);
1505                                 vput(nd.ni_dvp);
1506                                 NDFREE(&nd, NDF_ONLY_PNBUF);
1507                                 error = vn_start_write(NULL, &mp,
1508                                     V_XSLEEP | PCATCH);
1509                                 if (error != 0)
1510                                         return (error);
1511                                 goto again;
1512                         }
1513                         error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1514                         VOP_UNLOCK(vp, 0);
1515                         vput(nd.ni_dvp);
1516                         vn_finished_write(mp);
1517                         NDFREE(&nd, NDF_ONLY_PNBUF);
1518                 } else {
1519                         vput(nd.ni_dvp);
1520                         NDFREE(&nd, NDF_ONLY_PNBUF);
1521                         vrele(vp);
1522                         goto again;
1523                 }
1524         }
1525         vrele(vp);
1526         return (error);
1527 }
1528
1529 /*
1530  * Make a symbolic link.
1531  */
1532 #ifndef _SYS_SYSPROTO_H_
1533 struct symlink_args {
1534         char    *path;
1535         char    *link;
1536 };
1537 #endif
1538 int
1539 sys_symlink(td, uap)
1540         struct thread *td;
1541         register struct symlink_args /* {
1542                 char *path;
1543                 char *link;
1544         } */ *uap;
1545 {
1546
1547         return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1548             UIO_USERSPACE));
1549 }
1550
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct symlinkat_args {
1553         char    *path;
1554         int     fd;
1555         char    *path2;
1556 };
1557 #endif
1558 int
1559 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1560 {
1561
1562         return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1563             UIO_USERSPACE));
1564 }
1565
1566 int
1567 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1568     enum uio_seg segflg)
1569 {
1570         struct mount *mp;
1571         struct vattr vattr;
1572         char *syspath;
1573         struct nameidata nd;
1574         int error;
1575         cap_rights_t rights;
1576
1577         if (segflg == UIO_SYSSPACE) {
1578                 syspath = path1;
1579         } else {
1580                 syspath = uma_zalloc(namei_zone, M_WAITOK);
1581                 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1582                         goto out;
1583         }
1584         AUDIT_ARG_TEXT(syspath);
1585 restart:
1586         bwillwrite();
1587         NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1588             NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1589             td);
1590         if ((error = namei(&nd)) != 0)
1591                 goto out;
1592         if (nd.ni_vp) {
1593                 NDFREE(&nd, NDF_ONLY_PNBUF);
1594                 if (nd.ni_vp == nd.ni_dvp)
1595                         vrele(nd.ni_dvp);
1596                 else
1597                         vput(nd.ni_dvp);
1598                 vrele(nd.ni_vp);
1599                 error = EEXIST;
1600                 goto out;
1601         }
1602         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1603                 NDFREE(&nd, NDF_ONLY_PNBUF);
1604                 vput(nd.ni_dvp);
1605                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1606                         goto out;
1607                 goto restart;
1608         }
1609         VATTR_NULL(&vattr);
1610         vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1611 #ifdef MAC
1612         vattr.va_type = VLNK;
1613         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1614             &vattr);
1615         if (error != 0)
1616                 goto out2;
1617 #endif
1618         error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1619         if (error == 0)
1620                 vput(nd.ni_vp);
1621 #ifdef MAC
1622 out2:
1623 #endif
1624         NDFREE(&nd, NDF_ONLY_PNBUF);
1625         vput(nd.ni_dvp);
1626         vn_finished_write(mp);
1627 out:
1628         if (segflg != UIO_SYSSPACE)
1629                 uma_zfree(namei_zone, syspath);
1630         return (error);
1631 }
1632
1633 /*
1634  * Delete a whiteout from the filesystem.
1635  */
1636 int
1637 sys_undelete(td, uap)
1638         struct thread *td;
1639         register struct undelete_args /* {
1640                 char *path;
1641         } */ *uap;
1642 {
1643         struct mount *mp;
1644         struct nameidata nd;
1645         int error;
1646
1647 restart:
1648         bwillwrite();
1649         NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1650             UIO_USERSPACE, uap->path, td);
1651         error = namei(&nd);
1652         if (error != 0)
1653                 return (error);
1654
1655         if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1656                 NDFREE(&nd, NDF_ONLY_PNBUF);
1657                 if (nd.ni_vp == nd.ni_dvp)
1658                         vrele(nd.ni_dvp);
1659                 else
1660                         vput(nd.ni_dvp);
1661                 if (nd.ni_vp)
1662                         vrele(nd.ni_vp);
1663                 return (EEXIST);
1664         }
1665         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1666                 NDFREE(&nd, NDF_ONLY_PNBUF);
1667                 vput(nd.ni_dvp);
1668                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1669                         return (error);
1670                 goto restart;
1671         }
1672         error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1673         NDFREE(&nd, NDF_ONLY_PNBUF);
1674         vput(nd.ni_dvp);
1675         vn_finished_write(mp);
1676         return (error);
1677 }
1678
1679 /*
1680  * Delete a name from the filesystem.
1681  */
1682 #ifndef _SYS_SYSPROTO_H_
1683 struct unlink_args {
1684         char    *path;
1685 };
1686 #endif
1687 int
1688 sys_unlink(td, uap)
1689         struct thread *td;
1690         struct unlink_args /* {
1691                 char *path;
1692         } */ *uap;
1693 {
1694
1695         return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1696 }
1697
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct unlinkat_args {
1700         int     fd;
1701         char    *path;
1702         int     flag;
1703 };
1704 #endif
1705 int
1706 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1707 {
1708         int flag = uap->flag;
1709         int fd = uap->fd;
1710         char *path = uap->path;
1711
1712         if (flag & ~AT_REMOVEDIR)
1713                 return (EINVAL);
1714
1715         if (flag & AT_REMOVEDIR)
1716                 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1717         else
1718                 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1719 }
1720
1721 int
1722 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1723     ino_t oldinum)
1724 {
1725         struct mount *mp;
1726         struct vnode *vp;
1727         struct nameidata nd;
1728         struct stat sb;
1729         cap_rights_t rights;
1730         int error;
1731
1732 restart:
1733         bwillwrite();
1734         NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1735             pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1736         if ((error = namei(&nd)) != 0)
1737                 return (error == EINVAL ? EPERM : error);
1738         vp = nd.ni_vp;
1739         if (vp->v_type == VDIR && oldinum == 0) {
1740                 error = EPERM;          /* POSIX */
1741         } else if (oldinum != 0 &&
1742                   ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1743                   sb.st_ino != oldinum) {
1744                         error = EIDRM;  /* Identifier removed */
1745         } else {
1746                 /*
1747                  * The root of a mounted filesystem cannot be deleted.
1748                  *
1749                  * XXX: can this only be a VDIR case?
1750                  */
1751                 if (vp->v_vflag & VV_ROOT)
1752                         error = EBUSY;
1753         }
1754         if (error == 0) {
1755                 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1756                         NDFREE(&nd, NDF_ONLY_PNBUF);
1757                         vput(nd.ni_dvp);
1758                         if (vp == nd.ni_dvp)
1759                                 vrele(vp);
1760                         else
1761                                 vput(vp);
1762                         if ((error = vn_start_write(NULL, &mp,
1763                             V_XSLEEP | PCATCH)) != 0)
1764                                 return (error);
1765                         goto restart;
1766                 }
1767 #ifdef MAC
1768                 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1769                     &nd.ni_cnd);
1770                 if (error != 0)
1771                         goto out;
1772 #endif
1773                 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1774                 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1775 #ifdef MAC
1776 out:
1777 #endif
1778                 vn_finished_write(mp);
1779         }
1780         NDFREE(&nd, NDF_ONLY_PNBUF);
1781         vput(nd.ni_dvp);
1782         if (vp == nd.ni_dvp)
1783                 vrele(vp);
1784         else
1785                 vput(vp);
1786         return (error);
1787 }
1788
1789 /*
1790  * Reposition read/write file offset.
1791  */
1792 #ifndef _SYS_SYSPROTO_H_
1793 struct lseek_args {
1794         int     fd;
1795         int     pad;
1796         off_t   offset;
1797         int     whence;
1798 };
1799 #endif
1800 int
1801 sys_lseek(struct thread *td, struct lseek_args *uap)
1802 {
1803
1804         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1805 }
1806
1807 int
1808 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1809 {
1810         struct file *fp;
1811         cap_rights_t rights;
1812         int error;
1813
1814         AUDIT_ARG_FD(fd);
1815         error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1816         if (error != 0)
1817                 return (error);
1818         error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1819             fo_seek(fp, offset, whence, td) : ESPIPE;
1820         fdrop(fp, td);
1821         return (error);
1822 }
1823
1824 #if defined(COMPAT_43)
1825 /*
1826  * Reposition read/write file offset.
1827  */
1828 #ifndef _SYS_SYSPROTO_H_
1829 struct olseek_args {
1830         int     fd;
1831         long    offset;
1832         int     whence;
1833 };
1834 #endif
1835 int
1836 olseek(struct thread *td, struct olseek_args *uap)
1837 {
1838
1839         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1840 }
1841 #endif /* COMPAT_43 */
1842
1843 #if defined(COMPAT_FREEBSD6)
1844 /* Version with the 'pad' argument */
1845 int
1846 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1847 {
1848
1849         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1850 }
1851 #endif
1852
1853 /*
1854  * Check access permissions using passed credentials.
1855  */
1856 static int
1857 vn_access(vp, user_flags, cred, td)
1858         struct vnode    *vp;
1859         int             user_flags;
1860         struct ucred    *cred;
1861         struct thread   *td;
1862 {
1863         accmode_t accmode;
1864         int error;
1865
1866         /* Flags == 0 means only check for existence. */
1867         if (user_flags == 0)
1868                 return (0);
1869
1870         accmode = 0;
1871         if (user_flags & R_OK)
1872                 accmode |= VREAD;
1873         if (user_flags & W_OK)
1874                 accmode |= VWRITE;
1875         if (user_flags & X_OK)
1876                 accmode |= VEXEC;
1877 #ifdef MAC
1878         error = mac_vnode_check_access(cred, vp, accmode);
1879         if (error != 0)
1880                 return (error);
1881 #endif
1882         if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1883                 error = VOP_ACCESS(vp, accmode, cred, td);
1884         return (error);
1885 }
1886
1887 /*
1888  * Check access permissions using "real" credentials.
1889  */
1890 #ifndef _SYS_SYSPROTO_H_
1891 struct access_args {
1892         char    *path;
1893         int     amode;
1894 };
1895 #endif
1896 int
1897 sys_access(td, uap)
1898         struct thread *td;
1899         register struct access_args /* {
1900                 char *path;
1901                 int amode;
1902         } */ *uap;
1903 {
1904
1905         return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1906             0, uap->amode));
1907 }
1908
1909 #ifndef _SYS_SYSPROTO_H_
1910 struct faccessat_args {
1911         int     dirfd;
1912         char    *path;
1913         int     amode;
1914         int     flag;
1915 }
1916 #endif
1917 int
1918 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1919 {
1920
1921         return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1922             uap->amode));
1923 }
1924
1925 int
1926 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1927     int flag, int amode)
1928 {
1929         struct ucred *cred, *usecred;
1930         struct vnode *vp;
1931         struct nameidata nd;
1932         cap_rights_t rights;
1933         int error;
1934
1935         if (flag & ~AT_EACCESS)
1936                 return (EINVAL);
1937         if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1938                 return (EINVAL);
1939
1940         /*
1941          * Create and modify a temporary credential instead of one that
1942          * is potentially shared (if we need one).
1943          */
1944         cred = td->td_ucred;
1945         if ((flag & AT_EACCESS) == 0 &&
1946             ((cred->cr_uid != cred->cr_ruid ||
1947             cred->cr_rgid != cred->cr_groups[0]))) {
1948                 usecred = crdup(cred);
1949                 usecred->cr_uid = cred->cr_ruid;
1950                 usecred->cr_groups[0] = cred->cr_rgid;
1951                 td->td_ucred = usecred;
1952         } else
1953                 usecred = cred;
1954         AUDIT_ARG_VALUE(amode);
1955         NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1956             AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1957             td);
1958         if ((error = namei(&nd)) != 0)
1959                 goto out;
1960         vp = nd.ni_vp;
1961
1962         error = vn_access(vp, amode, usecred, td);
1963         NDFREE(&nd, NDF_ONLY_PNBUF);
1964         vput(vp);
1965 out:
1966         if (usecred != cred) {
1967                 td->td_ucred = cred;
1968                 crfree(usecred);
1969         }
1970         return (error);
1971 }
1972
1973 /*
1974  * Check access permissions using "effective" credentials.
1975  */
1976 #ifndef _SYS_SYSPROTO_H_
1977 struct eaccess_args {
1978         char    *path;
1979         int     amode;
1980 };
1981 #endif
1982 int
1983 sys_eaccess(td, uap)
1984         struct thread *td;
1985         register struct eaccess_args /* {
1986                 char *path;
1987                 int amode;
1988         } */ *uap;
1989 {
1990
1991         return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1992             AT_EACCESS, uap->amode));
1993 }
1994
1995 #if defined(COMPAT_43)
1996 /*
1997  * Get file status; this version follows links.
1998  */
1999 #ifndef _SYS_SYSPROTO_H_
2000 struct ostat_args {
2001         char    *path;
2002         struct ostat *ub;
2003 };
2004 #endif
2005 int
2006 ostat(td, uap)
2007         struct thread *td;
2008         register struct ostat_args /* {
2009                 char *path;
2010                 struct ostat *ub;
2011         } */ *uap;
2012 {
2013         struct stat sb;
2014         struct ostat osb;
2015         int error;
2016
2017         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2018             &sb, NULL);
2019         if (error != 0)
2020                 return (error);
2021         cvtstat(&sb, &osb);
2022         return (copyout(&osb, uap->ub, sizeof (osb)));
2023 }
2024
2025 /*
2026  * Get file status; this version does not follow links.
2027  */
2028 #ifndef _SYS_SYSPROTO_H_
2029 struct olstat_args {
2030         char    *path;
2031         struct ostat *ub;
2032 };
2033 #endif
2034 int
2035 olstat(td, uap)
2036         struct thread *td;
2037         register struct olstat_args /* {
2038                 char *path;
2039                 struct ostat *ub;
2040         } */ *uap;
2041 {
2042         struct stat sb;
2043         struct ostat osb;
2044         int error;
2045
2046         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2047             UIO_USERSPACE, &sb, NULL);
2048         if (error != 0)
2049                 return (error);
2050         cvtstat(&sb, &osb);
2051         return (copyout(&osb, uap->ub, sizeof (osb)));
2052 }
2053
2054 /*
2055  * Convert from an old to a new stat structure.
2056  */
2057 void
2058 cvtstat(st, ost)
2059         struct stat *st;
2060         struct ostat *ost;
2061 {
2062
2063         bzero(ost, sizeof(*ost));
2064         ost->st_dev = st->st_dev;
2065         ost->st_ino = st->st_ino;
2066         ost->st_mode = st->st_mode;
2067         ost->st_nlink = st->st_nlink;
2068         ost->st_uid = st->st_uid;
2069         ost->st_gid = st->st_gid;
2070         ost->st_rdev = st->st_rdev;
2071         if (st->st_size < (quad_t)1 << 32)
2072                 ost->st_size = st->st_size;
2073         else
2074                 ost->st_size = -2;
2075         ost->st_atim = st->st_atim;
2076         ost->st_mtim = st->st_mtim;
2077         ost->st_ctim = st->st_ctim;
2078         ost->st_blksize = st->st_blksize;
2079         ost->st_blocks = st->st_blocks;
2080         ost->st_flags = st->st_flags;
2081         ost->st_gen = st->st_gen;
2082 }
2083 #endif /* COMPAT_43 */
2084
2085 /*
2086  * Get file status; this version follows links.
2087  */
2088 #ifndef _SYS_SYSPROTO_H_
2089 struct stat_args {
2090         char    *path;
2091         struct stat *ub;
2092 };
2093 #endif
2094 int
2095 sys_stat(td, uap)
2096         struct thread *td;
2097         register struct stat_args /* {
2098                 char *path;
2099                 struct stat *ub;
2100         } */ *uap;
2101 {
2102         struct stat sb;
2103         int error;
2104
2105         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2106             &sb, NULL);
2107         if (error == 0)
2108                 error = copyout(&sb, uap->ub, sizeof (sb));
2109         return (error);
2110 }
2111
2112 #ifndef _SYS_SYSPROTO_H_
2113 struct fstatat_args {
2114         int     fd;
2115         char    *path;
2116         struct stat     *buf;
2117         int     flag;
2118 }
2119 #endif
2120 int
2121 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2122 {
2123         struct stat sb;
2124         int error;
2125
2126         error = kern_statat(td, uap->flag, uap->fd, uap->path,
2127             UIO_USERSPACE, &sb, NULL);
2128         if (error == 0)
2129                 error = copyout(&sb, uap->buf, sizeof (sb));
2130         return (error);
2131 }
2132
2133 int
2134 kern_statat(struct thread *td, int flag, int fd, char *path,
2135     enum uio_seg pathseg, struct stat *sbp,
2136     void (*hook)(struct vnode *vp, struct stat *sbp))
2137 {
2138         struct nameidata nd;
2139         struct stat sb;
2140         cap_rights_t rights;
2141         int error;
2142
2143         if (flag & ~AT_SYMLINK_NOFOLLOW)
2144                 return (EINVAL);
2145
2146         NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2147             FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2148             cap_rights_init(&rights, CAP_FSTAT), td);
2149
2150         if ((error = namei(&nd)) != 0)
2151                 return (error);
2152         error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2153         if (error == 0) {
2154                 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2155                 if (S_ISREG(sb.st_mode))
2156                         SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2157                 if (__predict_false(hook != NULL))
2158                         hook(nd.ni_vp, &sb);
2159         }
2160         NDFREE(&nd, NDF_ONLY_PNBUF);
2161         vput(nd.ni_vp);
2162         if (error != 0)
2163                 return (error);
2164         *sbp = sb;
2165 #ifdef KTRACE
2166         if (KTRPOINT(td, KTR_STRUCT))
2167                 ktrstat(&sb);
2168 #endif
2169         return (0);
2170 }
2171
2172 /*
2173  * Get file status; this version does not follow links.
2174  */
2175 #ifndef _SYS_SYSPROTO_H_
2176 struct lstat_args {
2177         char    *path;
2178         struct stat *ub;
2179 };
2180 #endif
2181 int
2182 sys_lstat(td, uap)
2183         struct thread *td;
2184         register struct lstat_args /* {
2185                 char *path;
2186                 struct stat *ub;
2187         } */ *uap;
2188 {
2189         struct stat sb;
2190         int error;
2191
2192         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2193             UIO_USERSPACE, &sb, NULL);
2194         if (error == 0)
2195                 error = copyout(&sb, uap->ub, sizeof (sb));
2196         return (error);
2197 }
2198
2199 /*
2200  * Implementation of the NetBSD [l]stat() functions.
2201  */
2202 void
2203 cvtnstat(sb, nsb)
2204         struct stat *sb;
2205         struct nstat *nsb;
2206 {
2207
2208         bzero(nsb, sizeof *nsb);
2209         nsb->st_dev = sb->st_dev;
2210         nsb->st_ino = sb->st_ino;
2211         nsb->st_mode = sb->st_mode;
2212         nsb->st_nlink = sb->st_nlink;
2213         nsb->st_uid = sb->st_uid;
2214         nsb->st_gid = sb->st_gid;
2215         nsb->st_rdev = sb->st_rdev;
2216         nsb->st_atim = sb->st_atim;
2217         nsb->st_mtim = sb->st_mtim;
2218         nsb->st_ctim = sb->st_ctim;
2219         nsb->st_size = sb->st_size;
2220         nsb->st_blocks = sb->st_blocks;
2221         nsb->st_blksize = sb->st_blksize;
2222         nsb->st_flags = sb->st_flags;
2223         nsb->st_gen = sb->st_gen;
2224         nsb->st_birthtim = sb->st_birthtim;
2225 }
2226
2227 #ifndef _SYS_SYSPROTO_H_
2228 struct nstat_args {
2229         char    *path;
2230         struct nstat *ub;
2231 };
2232 #endif
2233 int
2234 sys_nstat(td, uap)
2235         struct thread *td;
2236         register struct nstat_args /* {
2237                 char *path;
2238                 struct nstat *ub;
2239         } */ *uap;
2240 {
2241         struct stat sb;
2242         struct nstat nsb;
2243         int error;
2244
2245         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2246             &sb, NULL);
2247         if (error != 0)
2248                 return (error);
2249         cvtnstat(&sb, &nsb);
2250         return (copyout(&nsb, uap->ub, sizeof (nsb)));
2251 }
2252
2253 /*
2254  * NetBSD lstat.  Get file status; this version does not follow links.
2255  */
2256 #ifndef _SYS_SYSPROTO_H_
2257 struct lstat_args {
2258         char    *path;
2259         struct stat *ub;
2260 };
2261 #endif
2262 int
2263 sys_nlstat(td, uap)
2264         struct thread *td;
2265         register struct nlstat_args /* {
2266                 char *path;
2267                 struct nstat *ub;
2268         } */ *uap;
2269 {
2270         struct stat sb;
2271         struct nstat nsb;
2272         int error;
2273
2274         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2275             UIO_USERSPACE, &sb, NULL);
2276         if (error != 0)
2277                 return (error);
2278         cvtnstat(&sb, &nsb);
2279         return (copyout(&nsb, uap->ub, sizeof (nsb)));
2280 }
2281
2282 /*
2283  * Get configurable pathname variables.
2284  */
2285 #ifndef _SYS_SYSPROTO_H_
2286 struct pathconf_args {
2287         char    *path;
2288         int     name;
2289 };
2290 #endif
2291 int
2292 sys_pathconf(td, uap)
2293         struct thread *td;
2294         register struct pathconf_args /* {
2295                 char *path;
2296                 int name;
2297         } */ *uap;
2298 {
2299
2300         return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2301 }
2302
2303 #ifndef _SYS_SYSPROTO_H_
2304 struct lpathconf_args {
2305         char    *path;
2306         int     name;
2307 };
2308 #endif
2309 int
2310 sys_lpathconf(td, uap)
2311         struct thread *td;
2312         register struct lpathconf_args /* {
2313                 char *path;
2314                 int name;
2315         } */ *uap;
2316 {
2317
2318         return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2319             NOFOLLOW));
2320 }
2321
2322 int
2323 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2324     u_long flags)
2325 {
2326         struct nameidata nd;
2327         int error;
2328
2329         NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2330             pathseg, path, td);
2331         if ((error = namei(&nd)) != 0)
2332                 return (error);
2333         NDFREE(&nd, NDF_ONLY_PNBUF);
2334
2335         error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2336         vput(nd.ni_vp);
2337         return (error);
2338 }
2339
2340 /*
2341  * Return target name of a symbolic link.
2342  */
2343 #ifndef _SYS_SYSPROTO_H_
2344 struct readlink_args {
2345         char    *path;
2346         char    *buf;
2347         size_t  count;
2348 };
2349 #endif
2350 int
2351 sys_readlink(td, uap)
2352         struct thread *td;
2353         register struct readlink_args /* {
2354                 char *path;
2355                 char *buf;
2356                 size_t count;
2357         } */ *uap;
2358 {
2359
2360         return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2361             uap->buf, UIO_USERSPACE, uap->count));
2362 }
2363 #ifndef _SYS_SYSPROTO_H_
2364 struct readlinkat_args {
2365         int     fd;
2366         char    *path;
2367         char    *buf;
2368         size_t  bufsize;
2369 };
2370 #endif
2371 int
2372 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2373 {
2374
2375         return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2376             uap->buf, UIO_USERSPACE, uap->bufsize));
2377 }
2378
2379 int
2380 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2381     char *buf, enum uio_seg bufseg, size_t count)
2382 {
2383         struct vnode *vp;
2384         struct iovec aiov;
2385         struct uio auio;
2386         struct nameidata nd;
2387         int error;
2388
2389         if (count > IOSIZE_MAX)
2390                 return (EINVAL);
2391
2392         NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2393             pathseg, path, fd, td);
2394
2395         if ((error = namei(&nd)) != 0)
2396                 return (error);
2397         NDFREE(&nd, NDF_ONLY_PNBUF);
2398         vp = nd.ni_vp;
2399 #ifdef MAC
2400         error = mac_vnode_check_readlink(td->td_ucred, vp);
2401         if (error != 0) {
2402                 vput(vp);
2403                 return (error);
2404         }
2405 #endif
2406         if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2407                 error = EINVAL;
2408         else {
2409                 aiov.iov_base = buf;
2410                 aiov.iov_len = count;
2411                 auio.uio_iov = &aiov;
2412                 auio.uio_iovcnt = 1;
2413                 auio.uio_offset = 0;
2414                 auio.uio_rw = UIO_READ;
2415                 auio.uio_segflg = bufseg;
2416                 auio.uio_td = td;
2417                 auio.uio_resid = count;
2418                 error = VOP_READLINK(vp, &auio, td->td_ucred);
2419                 td->td_retval[0] = count - auio.uio_resid;
2420         }
2421         vput(vp);
2422         return (error);
2423 }
2424
2425 /*
2426  * Common implementation code for chflags() and fchflags().
2427  */
2428 static int
2429 setfflags(td, vp, flags)
2430         struct thread *td;
2431         struct vnode *vp;
2432         u_long flags;
2433 {
2434         struct mount *mp;
2435         struct vattr vattr;
2436         int error;
2437
2438         /* We can't support the value matching VNOVAL. */
2439         if (flags == VNOVAL)
2440                 return (EOPNOTSUPP);
2441
2442         /*
2443          * Prevent non-root users from setting flags on devices.  When
2444          * a device is reused, users can retain ownership of the device
2445          * if they are allowed to set flags and programs assume that
2446          * chown can't fail when done as root.
2447          */
2448         if (vp->v_type == VCHR || vp->v_type == VBLK) {
2449                 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2450                 if (error != 0)
2451                         return (error);
2452         }
2453
2454         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2455                 return (error);
2456         VATTR_NULL(&vattr);
2457         vattr.va_flags = flags;
2458         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2459 #ifdef MAC
2460         error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2461         if (error == 0)
2462 #endif
2463                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2464         VOP_UNLOCK(vp, 0);
2465         vn_finished_write(mp);
2466         return (error);
2467 }
2468
2469 /*
2470  * Change flags of a file given a path name.
2471  */
2472 #ifndef _SYS_SYSPROTO_H_
2473 struct chflags_args {
2474         const char *path;
2475         u_long  flags;
2476 };
2477 #endif
2478 int
2479 sys_chflags(td, uap)
2480         struct thread *td;
2481         register struct chflags_args /* {
2482                 const char *path;
2483                 u_long flags;
2484         } */ *uap;
2485 {
2486
2487         return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2488             uap->flags, 0));
2489 }
2490
2491 #ifndef _SYS_SYSPROTO_H_
2492 struct chflagsat_args {
2493         int     fd;
2494         const char *path;
2495         u_long  flags;
2496         int     atflag;
2497 }
2498 #endif
2499 int
2500 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2501 {
2502         int fd = uap->fd;
2503         const char *path = uap->path;
2504         u_long flags = uap->flags;
2505         int atflag = uap->atflag;
2506
2507         if (atflag & ~AT_SYMLINK_NOFOLLOW)
2508                 return (EINVAL);
2509
2510         return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2511 }
2512
2513 /*
2514  * Same as chflags() but doesn't follow symlinks.
2515  */
2516 int
2517 sys_lchflags(td, uap)
2518         struct thread *td;
2519         register struct lchflags_args /* {
2520                 const char *path;
2521                 u_long flags;
2522         } */ *uap;
2523 {
2524
2525         return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2526             uap->flags, AT_SYMLINK_NOFOLLOW));
2527 }
2528
2529 static int
2530 kern_chflagsat(struct thread *td, int fd, const char *path,
2531     enum uio_seg pathseg, u_long flags, int atflag)
2532 {
2533         struct nameidata nd;
2534         cap_rights_t rights;
2535         int error, follow;
2536
2537         AUDIT_ARG_FFLAGS(flags);
2538         follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2539         NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2540             cap_rights_init(&rights, CAP_FCHFLAGS), td);
2541         if ((error = namei(&nd)) != 0)
2542                 return (error);
2543         NDFREE(&nd, NDF_ONLY_PNBUF);
2544         error = setfflags(td, nd.ni_vp, flags);
2545         vrele(nd.ni_vp);
2546         return (error);
2547 }
2548
2549 /*
2550  * Change flags of a file given a file descriptor.
2551  */
2552 #ifndef _SYS_SYSPROTO_H_
2553 struct fchflags_args {
2554         int     fd;
2555         u_long  flags;
2556 };
2557 #endif
2558 int
2559 sys_fchflags(td, uap)
2560         struct thread *td;
2561         register struct fchflags_args /* {
2562                 int fd;
2563                 u_long flags;
2564         } */ *uap;
2565 {
2566         struct file *fp;
2567         cap_rights_t rights;
2568         int error;
2569
2570         AUDIT_ARG_FD(uap->fd);
2571         AUDIT_ARG_FFLAGS(uap->flags);
2572         error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2573             &fp);
2574         if (error != 0)
2575                 return (error);
2576 #ifdef AUDIT
2577         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2578         AUDIT_ARG_VNODE1(fp->f_vnode);
2579         VOP_UNLOCK(fp->f_vnode, 0);
2580 #endif
2581         error = setfflags(td, fp->f_vnode, uap->flags);
2582         fdrop(fp, td);
2583         return (error);
2584 }
2585
2586 /*
2587  * Common implementation code for chmod(), lchmod() and fchmod().
2588  */
2589 int
2590 setfmode(td, cred, vp, mode)
2591         struct thread *td;
2592         struct ucred *cred;
2593         struct vnode *vp;
2594         int mode;
2595 {
2596         struct mount *mp;
2597         struct vattr vattr;
2598         int error;
2599
2600         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2601                 return (error);
2602         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2603         VATTR_NULL(&vattr);
2604         vattr.va_mode = mode & ALLPERMS;
2605 #ifdef MAC
2606         error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2607         if (error == 0)
2608 #endif
2609                 error = VOP_SETATTR(vp, &vattr, cred);
2610         VOP_UNLOCK(vp, 0);
2611         vn_finished_write(mp);
2612         return (error);
2613 }
2614
2615 /*
2616  * Change mode of a file given path name.
2617  */
2618 #ifndef _SYS_SYSPROTO_H_
2619 struct chmod_args {
2620         char    *path;
2621         int     mode;
2622 };
2623 #endif
2624 int
2625 sys_chmod(td, uap)
2626         struct thread *td;
2627         register struct chmod_args /* {
2628                 char *path;
2629                 int mode;
2630         } */ *uap;
2631 {
2632
2633         return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2634             uap->mode, 0));
2635 }
2636
2637 #ifndef _SYS_SYSPROTO_H_
2638 struct fchmodat_args {
2639         int     dirfd;
2640         char    *path;
2641         mode_t  mode;
2642         int     flag;
2643 }
2644 #endif
2645 int
2646 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2647 {
2648         int flag = uap->flag;
2649         int fd = uap->fd;
2650         char *path = uap->path;
2651         mode_t mode = uap->mode;
2652
2653         if (flag & ~AT_SYMLINK_NOFOLLOW)
2654                 return (EINVAL);
2655
2656         return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2657 }
2658
2659 /*
2660  * Change mode of a file given path name (don't follow links.)
2661  */
2662 #ifndef _SYS_SYSPROTO_H_
2663 struct lchmod_args {
2664         char    *path;
2665         int     mode;
2666 };
2667 #endif
2668 int
2669 sys_lchmod(td, uap)
2670         struct thread *td;
2671         register struct lchmod_args /* {
2672                 char *path;
2673                 int mode;
2674         } */ *uap;
2675 {
2676
2677         return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2678             uap->mode, AT_SYMLINK_NOFOLLOW));
2679 }
2680
2681 int
2682 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2683     mode_t mode, int flag)
2684 {
2685         struct nameidata nd;
2686         cap_rights_t rights;
2687         int error, follow;
2688
2689         AUDIT_ARG_MODE(mode);
2690         follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2691         NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2692             cap_rights_init(&rights, CAP_FCHMOD), td);
2693         if ((error = namei(&nd)) != 0)
2694                 return (error);
2695         NDFREE(&nd, NDF_ONLY_PNBUF);
2696         error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2697         vrele(nd.ni_vp);
2698         return (error);
2699 }
2700
2701 /*
2702  * Change mode of a file given a file descriptor.
2703  */
2704 #ifndef _SYS_SYSPROTO_H_
2705 struct fchmod_args {
2706         int     fd;
2707         int     mode;
2708 };
2709 #endif
2710 int
2711 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2712 {
2713         struct file *fp;
2714         cap_rights_t rights;
2715         int error;
2716
2717         AUDIT_ARG_FD(uap->fd);
2718         AUDIT_ARG_MODE(uap->mode);
2719
2720         error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2721         if (error != 0)
2722                 return (error);
2723         error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2724         fdrop(fp, td);
2725         return (error);
2726 }
2727
2728 /*
2729  * Common implementation for chown(), lchown(), and fchown()
2730  */
2731 int
2732 setfown(td, cred, vp, uid, gid)
2733         struct thread *td;
2734         struct ucred *cred;
2735         struct vnode *vp;
2736         uid_t uid;
2737         gid_t gid;
2738 {
2739         struct mount *mp;
2740         struct vattr vattr;
2741         int error;
2742
2743         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2744                 return (error);
2745         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2746         VATTR_NULL(&vattr);
2747         vattr.va_uid = uid;
2748         vattr.va_gid = gid;
2749 #ifdef MAC
2750         error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2751             vattr.va_gid);
2752         if (error == 0)
2753 #endif
2754                 error = VOP_SETATTR(vp, &vattr, cred);
2755         VOP_UNLOCK(vp, 0);
2756         vn_finished_write(mp);
2757         return (error);
2758 }
2759
2760 /*
2761  * Set ownership given a path name.
2762  */
2763 #ifndef _SYS_SYSPROTO_H_
2764 struct chown_args {
2765         char    *path;
2766         int     uid;
2767         int     gid;
2768 };
2769 #endif
2770 int
2771 sys_chown(td, uap)
2772         struct thread *td;
2773         register struct chown_args /* {
2774                 char *path;
2775                 int uid;
2776                 int gid;
2777         } */ *uap;
2778 {
2779
2780         return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2781             uap->gid, 0));
2782 }
2783
2784 #ifndef _SYS_SYSPROTO_H_
2785 struct fchownat_args {
2786         int fd;
2787         const char * path;
2788         uid_t uid;
2789         gid_t gid;
2790         int flag;
2791 };
2792 #endif
2793 int
2794 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2795 {
2796         int flag;
2797
2798         flag = uap->flag;
2799         if (flag & ~AT_SYMLINK_NOFOLLOW)
2800                 return (EINVAL);
2801
2802         return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2803             uap->gid, uap->flag));
2804 }
2805
2806 int
2807 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2808     int uid, int gid, int flag)
2809 {
2810         struct nameidata nd;
2811         cap_rights_t rights;
2812         int error, follow;
2813
2814         AUDIT_ARG_OWNER(uid, gid);
2815         follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2816         NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2817             cap_rights_init(&rights, CAP_FCHOWN), td);
2818
2819         if ((error = namei(&nd)) != 0)
2820                 return (error);
2821         NDFREE(&nd, NDF_ONLY_PNBUF);
2822         error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2823         vrele(nd.ni_vp);
2824         return (error);
2825 }
2826
2827 /*
2828  * Set ownership given a path name, do not cross symlinks.
2829  */
2830 #ifndef _SYS_SYSPROTO_H_
2831 struct lchown_args {
2832         char    *path;
2833         int     uid;
2834         int     gid;
2835 };
2836 #endif
2837 int
2838 sys_lchown(td, uap)
2839         struct thread *td;
2840         register struct lchown_args /* {
2841                 char *path;
2842                 int uid;
2843                 int gid;
2844         } */ *uap;
2845 {
2846
2847         return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2848             uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2849 }
2850
2851 /*
2852  * Set ownership given a file descriptor.
2853  */
2854 #ifndef _SYS_SYSPROTO_H_
2855 struct fchown_args {
2856         int     fd;
2857         int     uid;
2858         int     gid;
2859 };
2860 #endif
2861 int
2862 sys_fchown(td, uap)
2863         struct thread *td;
2864         register struct fchown_args /* {
2865                 int fd;
2866                 int uid;
2867                 int gid;
2868         } */ *uap;
2869 {
2870         struct file *fp;
2871         cap_rights_t rights;
2872         int error;
2873
2874         AUDIT_ARG_FD(uap->fd);
2875         AUDIT_ARG_OWNER(uap->uid, uap->gid);
2876         error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2877         if (error != 0)
2878                 return (error);
2879         error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2880         fdrop(fp, td);
2881         return (error);
2882 }
2883
2884 /*
2885  * Common implementation code for utimes(), lutimes(), and futimes().
2886  */
2887 static int
2888 getutimes(usrtvp, tvpseg, tsp)
2889         const struct timeval *usrtvp;
2890         enum uio_seg tvpseg;
2891         struct timespec *tsp;
2892 {
2893         struct timeval tv[2];
2894         const struct timeval *tvp;
2895         int error;
2896
2897         if (usrtvp == NULL) {
2898                 vfs_timestamp(&tsp[0]);
2899                 tsp[1] = tsp[0];
2900         } else {
2901                 if (tvpseg == UIO_SYSSPACE) {
2902                         tvp = usrtvp;
2903                 } else {
2904                         if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2905                                 return (error);
2906                         tvp = tv;
2907                 }
2908
2909                 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2910                     tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2911                         return (EINVAL);
2912                 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2913                 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2914         }
2915         return (0);
2916 }
2917
2918 /*
2919  * Common implementation code for futimens(), utimensat().
2920  */
2921 #define UTIMENS_NULL    0x1
2922 #define UTIMENS_EXIT    0x2
2923 static int
2924 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2925     struct timespec *tsp, int *retflags)
2926 {
2927         struct timespec tsnow;
2928         int error;
2929
2930         vfs_timestamp(&tsnow);
2931         *retflags = 0;
2932         if (usrtsp == NULL) {
2933                 tsp[0] = tsnow;
2934                 tsp[1] = tsnow;
2935                 *retflags |= UTIMENS_NULL;
2936                 return (0);
2937         }
2938         if (tspseg == UIO_SYSSPACE) {
2939                 tsp[0] = usrtsp[0];
2940                 tsp[1] = usrtsp[1];
2941         } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2942                 return (error);
2943         if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2944                 *retflags |= UTIMENS_EXIT;
2945         if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2946                 *retflags |= UTIMENS_NULL;
2947         if (tsp[0].tv_nsec == UTIME_OMIT)
2948                 tsp[0].tv_sec = VNOVAL;
2949         else if (tsp[0].tv_nsec == UTIME_NOW)
2950                 tsp[0] = tsnow;
2951         else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2952                 return (EINVAL);
2953         if (tsp[1].tv_nsec == UTIME_OMIT)
2954                 tsp[1].tv_sec = VNOVAL;
2955         else if (tsp[1].tv_nsec == UTIME_NOW)
2956                 tsp[1] = tsnow;
2957         else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2958                 return (EINVAL);
2959
2960         return (0);
2961 }
2962
2963 /*
2964  * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2965  * and utimensat().
2966  */
2967 static int
2968 setutimes(td, vp, ts, numtimes, nullflag)
2969         struct thread *td;
2970         struct vnode *vp;
2971         const struct timespec *ts;
2972         int numtimes;
2973         int nullflag;
2974 {
2975         struct mount *mp;
2976         struct vattr vattr;
2977         int error, setbirthtime;
2978
2979         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2980                 return (error);
2981         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2982         setbirthtime = 0;
2983         if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2984             timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2985                 setbirthtime = 1;
2986         VATTR_NULL(&vattr);
2987         vattr.va_atime = ts[0];
2988         vattr.va_mtime = ts[1];
2989         if (setbirthtime)
2990                 vattr.va_birthtime = ts[1];
2991         if (numtimes > 2)
2992                 vattr.va_birthtime = ts[2];
2993         if (nullflag)
2994                 vattr.va_vaflags |= VA_UTIMES_NULL;
2995 #ifdef MAC
2996         error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
2997             vattr.va_mtime);
2998 #endif
2999         if (error == 0)
3000                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3001         VOP_UNLOCK(vp, 0);
3002         vn_finished_write(mp);
3003         return (error);
3004 }
3005
3006 /*
3007  * Set the access and modification times of a file.
3008  */
3009 #ifndef _SYS_SYSPROTO_H_
3010 struct utimes_args {
3011         char    *path;
3012         struct  timeval *tptr;
3013 };
3014 #endif
3015 int
3016 sys_utimes(td, uap)
3017         struct thread *td;
3018         register struct utimes_args /* {
3019                 char *path;
3020                 struct timeval *tptr;
3021         } */ *uap;
3022 {
3023
3024         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3025             uap->tptr, UIO_USERSPACE));
3026 }
3027
3028 #ifndef _SYS_SYSPROTO_H_
3029 struct futimesat_args {
3030         int fd;
3031         const char * path;
3032         const struct timeval * times;
3033 };
3034 #endif
3035 int
3036 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3037 {
3038
3039         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3040             uap->times, UIO_USERSPACE));
3041 }
3042
3043 int
3044 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3045     struct timeval *tptr, enum uio_seg tptrseg)
3046 {
3047         struct nameidata nd;
3048         struct timespec ts[2];
3049         cap_rights_t rights;
3050         int error;
3051
3052         if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3053                 return (error);
3054         NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3055             cap_rights_init(&rights, CAP_FUTIMES), td);
3056
3057         if ((error = namei(&nd)) != 0)
3058                 return (error);
3059         NDFREE(&nd, NDF_ONLY_PNBUF);
3060         error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3061         vrele(nd.ni_vp);
3062         return (error);
3063 }
3064
3065 /*
3066  * Set the access and modification times of a file.
3067  */
3068 #ifndef _SYS_SYSPROTO_H_
3069 struct lutimes_args {
3070         char    *path;
3071         struct  timeval *tptr;
3072 };
3073 #endif
3074 int
3075 sys_lutimes(td, uap)
3076         struct thread *td;
3077         register struct lutimes_args /* {
3078                 char *path;
3079                 struct timeval *tptr;
3080         } */ *uap;
3081 {
3082
3083         return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3084             UIO_USERSPACE));
3085 }
3086
3087 int
3088 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3089     struct timeval *tptr, enum uio_seg tptrseg)
3090 {
3091         struct timespec ts[2];
3092         struct nameidata nd;
3093         int error;
3094
3095         if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3096                 return (error);
3097         NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3098         if ((error = namei(&nd)) != 0)
3099                 return (error);
3100         NDFREE(&nd, NDF_ONLY_PNBUF);
3101         error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3102         vrele(nd.ni_vp);
3103         return (error);
3104 }
3105
3106 /*
3107  * Set the access and modification times of a file.
3108  */
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct futimes_args {
3111         int     fd;
3112         struct  timeval *tptr;
3113 };
3114 #endif
3115 int
3116 sys_futimes(td, uap)
3117         struct thread *td;
3118         register struct futimes_args /* {
3119                 int  fd;
3120                 struct timeval *tptr;
3121         } */ *uap;
3122 {
3123
3124         return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3125 }
3126
3127 int
3128 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3129     enum uio_seg tptrseg)
3130 {
3131         struct timespec ts[2];
3132         struct file *fp;
3133         cap_rights_t rights;
3134         int error;
3135
3136         AUDIT_ARG_FD(fd);
3137         error = getutimes(tptr, tptrseg, ts);
3138         if (error != 0)
3139                 return (error);
3140         error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3141         if (error != 0)
3142                 return (error);
3143 #ifdef AUDIT
3144         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3145         AUDIT_ARG_VNODE1(fp->f_vnode);
3146         VOP_UNLOCK(fp->f_vnode, 0);
3147 #endif
3148         error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3149         fdrop(fp, td);
3150         return (error);
3151 }
3152
3153 int
3154 sys_futimens(struct thread *td, struct futimens_args *uap)
3155 {
3156
3157         return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3158 }
3159
3160 int
3161 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3162     enum uio_seg tptrseg)
3163 {
3164         struct timespec ts[2];
3165         struct file *fp;
3166         cap_rights_t rights;
3167         int error, flags;
3168
3169         AUDIT_ARG_FD(fd);
3170         error = getutimens(tptr, tptrseg, ts, &flags);
3171         if (error != 0)
3172                 return (error);
3173         if (flags & UTIMENS_EXIT)
3174                 return (0);
3175         error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3176         if (error != 0)
3177                 return (error);
3178 #ifdef AUDIT
3179         vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3180         AUDIT_ARG_VNODE1(fp->f_vnode);
3181         VOP_UNLOCK(fp->f_vnode, 0);
3182 #endif
3183         error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3184         fdrop(fp, td);
3185         return (error);
3186 }
3187
3188 int
3189 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3190 {
3191
3192         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3193             uap->times, UIO_USERSPACE, uap->flag));
3194 }
3195
3196 int
3197 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3198     struct timespec *tptr, enum uio_seg tptrseg, int flag)
3199 {
3200         struct nameidata nd;
3201         struct timespec ts[2];
3202         cap_rights_t rights;
3203         int error, flags;
3204
3205         if (flag & ~AT_SYMLINK_NOFOLLOW)
3206                 return (EINVAL);
3207
3208         if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3209                 return (error);
3210         NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3211             FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3212             cap_rights_init(&rights, CAP_FUTIMES), td);
3213         if ((error = namei(&nd)) != 0)
3214                 return (error);
3215         /*
3216          * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3217          * POSIX states:
3218          * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3219          * "Search permission is denied by a component of the path prefix."
3220          */
3221         NDFREE(&nd, NDF_ONLY_PNBUF);
3222         if ((flags & UTIMENS_EXIT) == 0)
3223                 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3224         vrele(nd.ni_vp);
3225         return (error);
3226 }
3227
3228 /*
3229  * Truncate a file given its path name.
3230  */
3231 #ifndef _SYS_SYSPROTO_H_
3232 struct truncate_args {
3233         char    *path;
3234         int     pad;
3235         off_t   length;
3236 };
3237 #endif
3238 int
3239 sys_truncate(td, uap)
3240         struct thread *td;
3241         register struct truncate_args /* {
3242                 char *path;
3243                 int pad;
3244                 off_t length;
3245         } */ *uap;
3246 {
3247
3248         return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3249 }
3250
3251 int
3252 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3253 {
3254         struct mount *mp;
3255         struct vnode *vp;
3256         void *rl_cookie;
3257         struct vattr vattr;
3258         struct nameidata nd;
3259         int error;
3260
3261         if (length < 0)
3262                 return(EINVAL);
3263         NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3264         if ((error = namei(&nd)) != 0)
3265                 return (error);
3266         vp = nd.ni_vp;
3267         rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3268         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3269                 vn_rangelock_unlock(vp, rl_cookie);
3270                 vrele(vp);
3271                 return (error);
3272         }
3273         NDFREE(&nd, NDF_ONLY_PNBUF);
3274         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3275         if (vp->v_type == VDIR)
3276                 error = EISDIR;
3277 #ifdef MAC
3278         else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3279         }
3280 #endif
3281         else if ((error = vn_writechk(vp)) == 0 &&
3282             (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3283                 VATTR_NULL(&vattr);
3284                 vattr.va_size = length;
3285                 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3286         }
3287         VOP_UNLOCK(vp, 0);
3288         vn_finished_write(mp);
3289         vn_rangelock_unlock(vp, rl_cookie);
3290         vrele(vp);
3291         return (error);
3292 }
3293
3294 #if defined(COMPAT_43)
3295 /*
3296  * Truncate a file given its path name.
3297  */
3298 #ifndef _SYS_SYSPROTO_H_
3299 struct otruncate_args {
3300         char    *path;
3301         long    length;
3302 };
3303 #endif
3304 int
3305 otruncate(struct thread *td, struct otruncate_args *uap)
3306 {
3307
3308         return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3309 }
3310 #endif /* COMPAT_43 */
3311
3312 #if defined(COMPAT_FREEBSD6)
3313 /* Versions with the pad argument */
3314 int
3315 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3316 {
3317
3318         return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3319 }
3320
3321 int
3322 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3323 {
3324
3325         return (kern_ftruncate(td, uap->fd, uap->length));
3326 }
3327 #endif
3328
3329 int
3330 kern_fsync(struct thread *td, int fd, bool fullsync)
3331 {
3332         struct vnode *vp;
3333         struct mount *mp;
3334         struct file *fp;
3335         cap_rights_t rights;
3336         int error, lock_flags;
3337
3338         AUDIT_ARG_FD(fd);
3339         error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3340         if (error != 0)
3341                 return (error);
3342         vp = fp->f_vnode;
3343 #if 0
3344         if (!fullsync)
3345                 /* XXXKIB: compete outstanding aio writes */;
3346 #endif
3347         error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3348         if (error != 0)
3349                 goto drop;
3350         if (MNT_SHARED_WRITES(mp) ||
3351             ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3352                 lock_flags = LK_SHARED;
3353         } else {
3354                 lock_flags = LK_EXCLUSIVE;
3355         }
3356         vn_lock(vp, lock_flags | LK_RETRY);
3357         AUDIT_ARG_VNODE1(vp);
3358         if (vp->v_object != NULL) {
3359                 VM_OBJECT_WLOCK(vp->v_object);
3360                 vm_object_page_clean(vp->v_object, 0, 0, 0);
3361                 VM_OBJECT_WUNLOCK(vp->v_object);
3362         }
3363         error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3364         VOP_UNLOCK(vp, 0);
3365         vn_finished_write(mp);
3366 drop:
3367         fdrop(fp, td);
3368         return (error);
3369 }
3370
3371 /*
3372  * Sync an open file.
3373  */
3374 #ifndef _SYS_SYSPROTO_H_
3375 struct fsync_args {
3376         int     fd;
3377 };
3378 #endif
3379 int
3380 sys_fsync(struct thread *td, struct fsync_args *uap)
3381 {
3382
3383         return (kern_fsync(td, uap->fd, true));
3384 }
3385
3386 int
3387 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3388 {
3389
3390         return (kern_fsync(td, uap->fd, false));
3391 }
3392
3393 /*
3394  * Rename files.  Source and destination must either both be directories, or
3395  * both not be directories.  If target is a directory, it must be empty.
3396  */
3397 #ifndef _SYS_SYSPROTO_H_
3398 struct rename_args {
3399         char    *from;
3400         char    *to;
3401 };
3402 #endif
3403 int
3404 sys_rename(td, uap)
3405         struct thread *td;
3406         register struct rename_args /* {
3407                 char *from;
3408                 char *to;
3409         } */ *uap;
3410 {
3411
3412         return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3413             uap->to, UIO_USERSPACE));
3414 }
3415
3416 #ifndef _SYS_SYSPROTO_H_
3417 struct renameat_args {
3418         int     oldfd;
3419         char    *old;
3420         int     newfd;
3421         char    *new;
3422 };
3423 #endif
3424 int
3425 sys_renameat(struct thread *td, struct renameat_args *uap)
3426 {
3427
3428         return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3429             UIO_USERSPACE));
3430 }
3431
3432 int
3433 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3434     enum uio_seg pathseg)
3435 {
3436         struct mount *mp = NULL;
3437         struct vnode *tvp, *fvp, *tdvp;
3438         struct nameidata fromnd, tond;
3439         cap_rights_t rights;
3440         int error;
3441
3442 again:
3443         bwillwrite();
3444 #ifdef MAC
3445         NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3446             AUDITVNODE1, pathseg, old, oldfd,
3447             cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3448 #else
3449         NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3450             pathseg, old, oldfd,
3451             cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3452 #endif
3453
3454         if ((error = namei(&fromnd)) != 0)
3455                 return (error);
3456 #ifdef MAC
3457         error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3458             fromnd.ni_vp, &fromnd.ni_cnd);
3459         VOP_UNLOCK(fromnd.ni_dvp, 0);
3460         if (fromnd.ni_dvp != fromnd.ni_vp)
3461                 VOP_UNLOCK(fromnd.ni_vp, 0);
3462 #endif
3463         fvp = fromnd.ni_vp;
3464         NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3465             SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3466             cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3467         if (fromnd.ni_vp->v_type == VDIR)
3468                 tond.ni_cnd.cn_flags |= WILLBEDIR;
3469         if ((error = namei(&tond)) != 0) {
3470                 /* Translate error code for rename("dir1", "dir2/."). */
3471                 if (error == EISDIR && fvp->v_type == VDIR)
3472                         error = EINVAL;
3473                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3474                 vrele(fromnd.ni_dvp);
3475                 vrele(fvp);
3476                 goto out1;
3477         }
3478         tdvp = tond.ni_dvp;
3479         tvp = tond.ni_vp;
3480         error = vn_start_write(fvp, &mp, V_NOWAIT);
3481         if (error != 0) {
3482                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3483                 NDFREE(&tond, NDF_ONLY_PNBUF);
3484                 if (tvp != NULL)
3485                         vput(tvp);
3486                 if (tdvp == tvp)
3487                         vrele(tdvp);
3488                 else
3489                         vput(tdvp);
3490                 vrele(fromnd.ni_dvp);
3491                 vrele(fvp);
3492                 vrele(tond.ni_startdir);
3493                 if (fromnd.ni_startdir != NULL)
3494                         vrele(fromnd.ni_startdir);
3495                 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3496                 if (error != 0)
3497                         return (error);
3498                 goto again;
3499         }
3500         if (tvp != NULL) {
3501                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3502                         error = ENOTDIR;
3503                         goto out;
3504                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3505                         error = EISDIR;
3506                         goto out;
3507                 }
3508 #ifdef CAPABILITIES
3509                 if (newfd != AT_FDCWD) {
3510                         /*
3511                          * If the target already exists we require CAP_UNLINKAT
3512                          * from 'newfd'.
3513                          */
3514                         error = cap_check(&tond.ni_filecaps.fc_rights,
3515                             cap_rights_init(&rights, CAP_UNLINKAT));
3516                         if (error != 0)
3517                                 goto out;
3518                 }
3519 #endif
3520         }
3521         if (fvp == tdvp) {
3522                 error = EINVAL;
3523                 goto out;
3524         }
3525         /*
3526          * If the source is the same as the destination (that is, if they
3527          * are links to the same vnode), then there is nothing to do.
3528          */
3529         if (fvp == tvp)
3530                 error = -1;
3531 #ifdef MAC
3532         else
3533                 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3534                     tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3535 #endif
3536 out:
3537         if (error == 0) {
3538                 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3539                     tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3540                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3541                 NDFREE(&tond, NDF_ONLY_PNBUF);
3542         } else {
3543                 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3544                 NDFREE(&tond, NDF_ONLY_PNBUF);
3545                 if (tvp != NULL)
3546                         vput(tvp);
3547                 if (tdvp == tvp)
3548                         vrele(tdvp);
3549                 else
3550                         vput(tdvp);
3551                 vrele(fromnd.ni_dvp);
3552                 vrele(fvp);
3553         }
3554         vrele(tond.ni_startdir);
3555         vn_finished_write(mp);
3556 out1:
3557         if (fromnd.ni_startdir)
3558                 vrele(fromnd.ni_startdir);
3559         if (error == -1)
3560                 return (0);
3561         return (error);
3562 }
3563
3564 /*
3565  * Make a directory file.
3566  */
3567 #ifndef _SYS_SYSPROTO_H_
3568 struct mkdir_args {
3569         char    *path;
3570         int     mode;
3571 };
3572 #endif
3573 int
3574 sys_mkdir(td, uap)
3575         struct thread *td;
3576         register struct mkdir_args /* {
3577                 char *path;
3578                 int mode;
3579         } */ *uap;
3580 {
3581
3582         return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3583             uap->mode));
3584 }
3585
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct mkdirat_args {
3588         int     fd;
3589         char    *path;
3590         mode_t  mode;
3591 };
3592 #endif
3593 int
3594 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3595 {
3596
3597         return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3598 }
3599
3600 int
3601 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3602     int mode)
3603 {
3604         struct mount *mp;
3605         struct vnode *vp;
3606         struct vattr vattr;
3607         struct nameidata nd;
3608         cap_rights_t rights;
3609         int error;
3610
3611         AUDIT_ARG_MODE(mode);
3612 restart:
3613         bwillwrite();
3614         NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3615             NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3616             td);
3617         nd.ni_cnd.cn_flags |= WILLBEDIR;
3618         if ((error = namei(&nd)) != 0)
3619                 return (error);
3620         vp = nd.ni_vp;
3621         if (vp != NULL) {
3622                 NDFREE(&nd, NDF_ONLY_PNBUF);
3623                 /*
3624                  * XXX namei called with LOCKPARENT but not LOCKLEAF has
3625                  * the strange behaviour of leaving the vnode unlocked
3626                  * if the target is the same vnode as the parent.
3627                  */
3628                 if (vp == nd.ni_dvp)
3629                         vrele(nd.ni_dvp);
3630                 else
3631                         vput(nd.ni_dvp);
3632                 vrele(vp);
3633                 return (EEXIST);
3634         }
3635         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3636                 NDFREE(&nd, NDF_ONLY_PNBUF);
3637                 vput(nd.ni_dvp);
3638                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3639                         return (error);
3640                 goto restart;
3641         }
3642         VATTR_NULL(&vattr);
3643         vattr.va_type = VDIR;
3644         vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3645 #ifdef MAC
3646         error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3647             &vattr);
3648         if (error != 0)
3649                 goto out;
3650 #endif
3651         error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3652 #ifdef MAC
3653 out:
3654 #endif
3655         NDFREE(&nd, NDF_ONLY_PNBUF);
3656         vput(nd.ni_dvp);
3657         if (error == 0)
3658                 vput(nd.ni_vp);
3659         vn_finished_write(mp);
3660         return (error);
3661 }
3662
3663 /*
3664  * Remove a directory file.
3665  */
3666 #ifndef _SYS_SYSPROTO_H_
3667 struct rmdir_args {
3668         char    *path;
3669 };
3670 #endif
3671 int
3672 sys_rmdir(td, uap)
3673         struct thread *td;
3674         struct rmdir_args /* {
3675                 char *path;
3676         } */ *uap;
3677 {
3678
3679         return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3680 }
3681
3682 int
3683 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3684 {
3685         struct mount *mp;
3686         struct vnode *vp;
3687         struct nameidata nd;
3688         cap_rights_t rights;
3689         int error;
3690
3691 restart:
3692         bwillwrite();
3693         NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3694             pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3695         if ((error = namei(&nd)) != 0)
3696                 return (error);
3697         vp = nd.ni_vp;
3698         if (vp->v_type != VDIR) {
3699                 error = ENOTDIR;
3700                 goto out;
3701         }
3702         /*
3703          * No rmdir "." please.
3704          */
3705         if (nd.ni_dvp == vp) {
3706                 error = EINVAL;
3707                 goto out;
3708         }
3709         /*
3710          * The root of a mounted filesystem cannot be deleted.
3711          */
3712         if (vp->v_vflag & VV_ROOT) {
3713                 error = EBUSY;
3714                 goto out;
3715         }
3716 #ifdef MAC
3717         error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3718             &nd.ni_cnd);
3719         if (error != 0)
3720                 goto out;
3721 #endif
3722         if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3723                 NDFREE(&nd, NDF_ONLY_PNBUF);
3724                 vput(vp);
3725                 if (nd.ni_dvp == vp)
3726                         vrele(nd.ni_dvp);
3727                 else
3728                         vput(nd.ni_dvp);
3729                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3730                         return (error);
3731                 goto restart;
3732         }
3733         vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3734         error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3735         vn_finished_write(mp);
3736 out:
3737         NDFREE(&nd, NDF_ONLY_PNBUF);
3738         vput(vp);
3739         if (nd.ni_dvp == vp)
3740                 vrele(nd.ni_dvp);
3741         else
3742                 vput(nd.ni_dvp);
3743         return (error);
3744 }
3745
3746 #ifdef COMPAT_43
3747 /*
3748  * Read a block of directory entries in a filesystem independent format.
3749  */
3750 #ifndef _SYS_SYSPROTO_H_
3751 struct ogetdirentries_args {
3752         int     fd;
3753         char    *buf;
3754         u_int   count;
3755         long    *basep;
3756 };
3757 #endif
3758 int
3759 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3760 {
3761         long loff;
3762         int error;
3763
3764         error = kern_ogetdirentries(td, uap, &loff);
3765         if (error == 0)
3766                 error = copyout(&loff, uap->basep, sizeof(long));
3767         return (error);
3768 }
3769
3770 int
3771 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3772     long *ploff)
3773 {
3774         struct vnode *vp;
3775         struct file *fp;
3776         struct uio auio, kuio;
3777         struct iovec aiov, kiov;
3778         struct dirent *dp, *edp;
3779         cap_rights_t rights;
3780         caddr_t dirbuf;
3781         int error, eofflag, readcnt;
3782         long loff;
3783         off_t foffset;
3784
3785         /* XXX arbitrary sanity limit on `count'. */
3786         if (uap->count > 64 * 1024)
3787                 return (EINVAL);
3788         error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3789         if (error != 0)
3790                 return (error);
3791         if ((fp->f_flag & FREAD) == 0) {
3792                 fdrop(fp, td);
3793                 return (EBADF);
3794         }
3795         vp = fp->f_vnode;
3796         foffset = foffset_lock(fp, 0);
3797 unionread:
3798         if (vp->v_type != VDIR) {
3799                 foffset_unlock(fp, foffset, 0);
3800                 fdrop(fp, td);
3801                 return (EINVAL);
3802         }
3803         aiov.iov_base = uap->buf;
3804         aiov.iov_len = uap->count;
3805         auio.uio_iov = &aiov;
3806         auio.uio_iovcnt = 1;
3807         auio.uio_rw = UIO_READ;
3808         auio.uio_segflg = UIO_USERSPACE;
3809         auio.uio_td = td;
3810         auio.uio_resid = uap->count;
3811         vn_lock(vp, LK_SHARED | LK_RETRY);
3812         loff = auio.uio_offset = foffset;
3813 #ifdef MAC
3814         error = mac_vnode_check_readdir(td->td_ucred, vp);
3815         if (error != 0) {
3816                 VOP_UNLOCK(vp, 0);
3817                 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3818                 fdrop(fp, td);
3819                 return (error);
3820         }
3821 #endif
3822 #       if (BYTE_ORDER != LITTLE_ENDIAN)
3823                 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3824                         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3825                             NULL, NULL);
3826                         foffset = auio.uio_offset;
3827                 } else
3828 #       endif
3829         {
3830                 kuio = auio;
3831                 kuio.uio_iov = &kiov;
3832                 kuio.uio_segflg = UIO_SYSSPACE;
3833                 kiov.iov_len = uap->count;
3834                 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3835                 kiov.iov_base = dirbuf;
3836                 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3837                             NULL, NULL);
3838                 foffset = kuio.uio_offset;
3839                 if (error == 0) {
3840                         readcnt = uap->count - kuio.uio_resid;
3841                         edp = (struct dirent *)&dirbuf[readcnt];
3842                         for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3843 #                               if (BYTE_ORDER == LITTLE_ENDIAN)
3844                                         /*
3845                                          * The expected low byte of
3846                                          * dp->d_namlen is our dp->d_type.
3847                                          * The high MBZ byte of dp->d_namlen
3848                                          * is our dp->d_namlen.
3849                                          */
3850                                         dp->d_type = dp->d_namlen;
3851                                         dp->d_namlen = 0;
3852 #                               else
3853                                         /*
3854                                          * The dp->d_type is the high byte
3855                                          * of the expected dp->d_namlen,
3856                                          * so must be zero'ed.
3857                                          */
3858                                         dp->d_type = 0;
3859 #                               endif
3860                                 if (dp->d_reclen > 0) {
3861                                         dp = (struct dirent *)
3862                                             ((char *)dp + dp->d_reclen);
3863                                 } else {
3864                                         error = EIO;
3865                                         break;
3866                                 }
3867                         }
3868                         if (dp >= edp)
3869                                 error = uiomove(dirbuf, readcnt, &auio);
3870                 }
3871                 free(dirbuf, M_TEMP);
3872         }
3873         if (error != 0) {
3874                 VOP_UNLOCK(vp, 0);
3875                 foffset_unlock(fp, foffset, 0);
3876                 fdrop(fp, td);
3877                 return (error);
3878         }
3879         if (uap->count == auio.uio_resid &&
3880             (vp->v_vflag & VV_ROOT) &&
3881             (vp->v_mount->mnt_flag & MNT_UNION)) {
3882                 struct vnode *tvp = vp;
3883                 vp = vp->v_mount->mnt_vnodecovered;
3884                 VREF(vp);
3885                 fp->f_vnode = vp;
3886                 fp->f_data = vp;
3887                 foffset = 0;
3888                 vput(tvp);
3889                 goto unionread;
3890         }
3891         VOP_UNLOCK(vp, 0);
3892         foffset_unlock(fp, foffset, 0);
3893         fdrop(fp, td);
3894         td->td_retval[0] = uap->count - auio.uio_resid;
3895         if (error == 0)
3896                 *ploff = loff;
3897         return (error);
3898 }
3899 #endif /* COMPAT_43 */
3900
3901 /*
3902  * Read a block of directory entries in a filesystem independent format.
3903  */
3904 #ifndef _SYS_SYSPROTO_H_
3905 struct getdirentries_args {
3906         int     fd;
3907         char    *buf;
3908         u_int   count;
3909         long    *basep;
3910 };
3911 #endif
3912 int
3913 sys_getdirentries(td, uap)
3914         struct thread *td;
3915         register struct getdirentries_args /* {
3916                 int fd;
3917                 char *buf;
3918                 u_int count;
3919                 long *basep;
3920         } */ *uap;
3921 {
3922         long base;
3923         int error;
3924
3925         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3926             NULL, UIO_USERSPACE);
3927         if (error != 0)
3928                 return (error);
3929         if (uap->basep != NULL)
3930                 error = copyout(&base, uap->basep, sizeof(long));
3931         return (error);
3932 }
3933
3934 int
3935 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3936     long *basep, ssize_t *residp, enum uio_seg bufseg)
3937 {
3938         struct vnode *vp;
3939         struct file *fp;
3940         struct uio auio;
3941         struct iovec aiov;
3942         cap_rights_t rights;
3943         long loff;
3944         int error, eofflag;
3945         off_t foffset;
3946
3947         AUDIT_ARG_FD(fd);
3948         if (count > IOSIZE_MAX)
3949                 return (EINVAL);
3950         auio.uio_resid = count;
3951         error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3952         if (error != 0)
3953                 return (error);
3954         if ((fp->f_flag & FREAD) == 0) {
3955                 fdrop(fp, td);
3956                 return (EBADF);
3957         }
3958         vp = fp->f_vnode;
3959         foffset = foffset_lock(fp, 0);
3960 unionread:
3961         if (vp->v_type != VDIR) {
3962                 error = EINVAL;
3963                 goto fail;
3964         }
3965         aiov.iov_base = buf;
3966         aiov.iov_len = count;
3967         auio.uio_iov = &aiov;
3968         auio.uio_iovcnt = 1;
3969         auio.uio_rw = UIO_READ;
3970         auio.uio_segflg = bufseg;
3971         auio.uio_td = td;
3972         vn_lock(vp, LK_SHARED | LK_RETRY);
3973         AUDIT_ARG_VNODE1(vp);
3974         loff = auio.uio_offset = foffset;
3975 #ifdef MAC
3976         error = mac_vnode_check_readdir(td->td_ucred, vp);
3977         if (error == 0)
3978 #endif
3979                 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3980                     NULL);
3981         foffset = auio.uio_offset;
3982         if (error != 0) {
3983                 VOP_UNLOCK(vp, 0);
3984                 goto fail;
3985         }
3986         if (count == auio.uio_resid &&
3987             (vp->v_vflag & VV_ROOT) &&
3988             (vp->v_mount->mnt_flag & MNT_UNION)) {
3989                 struct vnode *tvp = vp;
3990
3991                 vp = vp->v_mount->mnt_vnodecovered;
3992                 VREF(vp);
3993                 fp->f_vnode = vp;
3994                 fp->f_data = vp;
3995                 foffset = 0;
3996                 vput(tvp);
3997                 goto unionread;
3998         }
3999         VOP_UNLOCK(vp, 0);
4000         *basep = loff;
4001         if (residp != NULL)
4002                 *residp = auio.uio_resid;
4003         td->td_retval[0] = count - auio.uio_resid;
4004 fail:
4005         foffset_unlock(fp, foffset, 0);
4006         fdrop(fp, td);
4007         return (error);
4008 }
4009
4010 #ifndef _SYS_SYSPROTO_H_
4011 struct getdents_args {
4012         int fd;
4013         char *buf;
4014         size_t count;
4015 };
4016 #endif
4017 int
4018 sys_getdents(td, uap)
4019         struct thread *td;
4020         register struct getdents_args /* {
4021                 int fd;
4022                 char *buf;
4023                 u_int count;
4024         } */ *uap;
4025 {
4026         struct getdirentries_args ap;
4027
4028         ap.fd = uap->fd;
4029         ap.buf = uap->buf;
4030         ap.count = uap->count;
4031         ap.basep = NULL;
4032         return (sys_getdirentries(td, &ap));
4033 }
4034
4035 /*
4036  * Set the mode mask for creation of filesystem nodes.
4037  */
4038 #ifndef _SYS_SYSPROTO_H_
4039 struct umask_args {
4040         int     newmask;
4041 };
4042 #endif
4043 int
4044 sys_umask(td, uap)
4045         struct thread *td;
4046         struct umask_args /* {
4047                 int newmask;
4048         } */ *uap;
4049 {
4050         struct filedesc *fdp;
4051
4052         fdp = td->td_proc->p_fd;
4053         FILEDESC_XLOCK(fdp);
4054         td->td_retval[0] = fdp->fd_cmask;
4055         fdp->fd_cmask = uap->newmask & ALLPERMS;
4056         FILEDESC_XUNLOCK(fdp);
4057         return (0);
4058 }
4059
4060 /*
4061  * Void all references to file by ripping underlying filesystem away from
4062  * vnode.
4063  */
4064 #ifndef _SYS_SYSPROTO_H_
4065 struct revoke_args {
4066         char    *path;
4067 };
4068 #endif
4069 int
4070 sys_revoke(td, uap)
4071         struct thread *td;
4072         register struct revoke_args /* {
4073                 char *path;
4074         } */ *uap;
4075 {
4076         struct vnode *vp;
4077         struct vattr vattr;
4078         struct nameidata nd;
4079         int error;
4080
4081         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4082             uap->path, td);
4083         if ((error = namei(&nd)) != 0)
4084                 return (error);
4085         vp = nd.ni_vp;
4086         NDFREE(&nd, NDF_ONLY_PNBUF);
4087         if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4088                 error = EINVAL;
4089                 goto out;
4090         }
4091 #ifdef MAC
4092         error = mac_vnode_check_revoke(td->td_ucred, vp);
4093         if (error != 0)
4094                 goto out;
4095 #endif
4096         error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4097         if (error != 0)
4098                 goto out;
4099         if (td->td_ucred->cr_uid != vattr.va_uid) {
4100                 error = priv_check(td, PRIV_VFS_ADMIN);
4101                 if (error != 0)
4102                         goto out;
4103         }
4104         if (vcount(vp) > 1)
4105                 VOP_REVOKE(vp, REVOKEALL);
4106 out:
4107         vput(vp);
4108         return (error);
4109 }
4110
4111 /*
4112  * Convert a user file descriptor to a kernel file entry and check that, if it
4113  * is a capability, the correct rights are present. A reference on the file
4114  * entry is held upon returning.
4115  */
4116 int
4117 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4118 {
4119         struct file *fp;
4120         int error;
4121
4122         error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4123         if (error != 0)
4124                 return (error);
4125
4126         /*
4127          * The file could be not of the vnode type, or it may be not
4128          * yet fully initialized, in which case the f_vnode pointer
4129          * may be set, but f_ops is still badfileops.  E.g.,
4130          * devfs_open() transiently create such situation to
4131          * facilitate csw d_fdopen().
4132          *
4133          * Dupfdopen() handling in kern_openat() installs the
4134          * half-baked file into the process descriptor table, allowing
4135          * other thread to dereference it. Guard against the race by
4136          * checking f_ops.
4137          */
4138         if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4139                 fdrop(fp, td);
4140                 return (EINVAL);
4141         }
4142         *fpp = fp;
4143         return (0);
4144 }
4145
4146
4147 /*
4148  * Get an (NFS) file handle.
4149  */
4150 #ifndef _SYS_SYSPROTO_H_
4151 struct lgetfh_args {
4152         char    *fname;
4153         fhandle_t *fhp;
4154 };
4155 #endif
4156 int
4157 sys_lgetfh(td, uap)
4158         struct thread *td;
4159         register struct lgetfh_args *uap;
4160 {
4161         struct nameidata nd;
4162         fhandle_t fh;
4163         register struct vnode *vp;
4164         int error;
4165
4166         error = priv_check(td, PRIV_VFS_GETFH);
4167         if (error != 0)
4168                 return (error);
4169         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4170             uap->fname, td);
4171         error = namei(&nd);
4172         if (error != 0)
4173                 return (error);
4174         NDFREE(&nd, NDF_ONLY_PNBUF);
4175         vp = nd.ni_vp;
4176         bzero(&fh, sizeof(fh));
4177         fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4178         error = VOP_VPTOFH(vp, &fh.fh_fid);
4179         vput(vp);
4180         if (error == 0)
4181                 error = copyout(&fh, uap->fhp, sizeof (fh));
4182         return (error);
4183 }
4184
4185 #ifndef _SYS_SYSPROTO_H_
4186 struct getfh_args {
4187         char    *fname;
4188         fhandle_t *fhp;
4189 };
4190 #endif
4191 int
4192 sys_getfh(td, uap)
4193         struct thread *td;
4194         register struct getfh_args *uap;
4195 {
4196         struct nameidata nd;
4197         fhandle_t fh;
4198         register struct vnode *vp;
4199         int error;
4200
4201         error = priv_check(td, PRIV_VFS_GETFH);
4202         if (error != 0)
4203                 return (error);
4204         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4205             uap->fname, td);
4206         error = namei(&nd);
4207         if (error != 0)
4208                 return (error);
4209         NDFREE(&nd, NDF_ONLY_PNBUF);
4210         vp = nd.ni_vp;
4211         bzero(&fh, sizeof(fh));
4212         fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4213         error = VOP_VPTOFH(vp, &fh.fh_fid);
4214         vput(vp);
4215         if (error == 0)
4216                 error = copyout(&fh, uap->fhp, sizeof (fh));
4217         return (error);
4218 }
4219
4220 /*
4221  * syscall for the rpc.lockd to use to translate a NFS file handle into an
4222  * open descriptor.
4223  *
4224  * warning: do not remove the priv_check() call or this becomes one giant
4225  * security hole.
4226  */
4227 #ifndef _SYS_SYSPROTO_H_
4228 struct fhopen_args {
4229         const struct fhandle *u_fhp;
4230         int flags;
4231 };
4232 #endif
4233 int
4234 sys_fhopen(td, uap)
4235         struct thread *td;
4236         struct fhopen_args /* {
4237                 const struct fhandle *u_fhp;
4238                 int flags;
4239         } */ *uap;
4240 {
4241         struct mount *mp;
4242         struct vnode *vp;
4243         struct fhandle fhp;
4244         struct file *fp;
4245         int fmode, error;
4246         int indx;
4247
4248         error = priv_check(td, PRIV_VFS_FHOPEN);
4249         if (error != 0)
4250                 return (error);
4251         indx = -1;
4252         fmode = FFLAGS(uap->flags);
4253         /* why not allow a non-read/write open for our lockd? */
4254         if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4255                 return (EINVAL);
4256         error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4257         if (error != 0)
4258                 return(error);
4259         /* find the mount point */
4260         mp = vfs_busyfs(&fhp.fh_fsid);
4261         if (mp == NULL)
4262                 return (ESTALE);
4263         /* now give me my vnode, it gets returned to me locked */
4264         error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4265         vfs_unbusy(mp);
4266         if (error != 0)
4267                 return (error);
4268
4269         error = falloc_noinstall(td, &fp);
4270         if (error != 0) {
4271                 vput(vp);
4272                 return (error);
4273         }
4274         /*
4275          * An extra reference on `fp' has been held for us by
4276          * falloc_noinstall().
4277          */
4278
4279 #ifdef INVARIANTS
4280         td->td_dupfd = -1;
4281 #endif
4282         error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4283         if (error != 0) {
4284                 KASSERT(fp->f_ops == &badfileops,
4285                     ("VOP_OPEN in fhopen() set f_ops"));
4286                 KASSERT(td->td_dupfd < 0,
4287                     ("fhopen() encountered fdopen()"));
4288
4289                 vput(vp);
4290                 goto bad;
4291         }
4292 #ifdef INVARIANTS
4293         td->td_dupfd = 0;
4294 #endif
4295         fp->f_vnode = vp;
4296         fp->f_seqcount = 1;
4297         finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4298             &vnops);
4299         VOP_UNLOCK(vp, 0);
4300         if ((fmode & O_TRUNC) != 0) {
4301                 error = fo_truncate(fp, 0, td->td_ucred, td);
4302                 if (error != 0)
4303                         goto bad;
4304         }
4305
4306         error = finstall(td, fp, &indx, fmode, NULL);
4307 bad:
4308         fdrop(fp, td);
4309         td->td_retval[0] = indx;
4310         return (error);
4311 }
4312
4313 /*
4314  * Stat an (NFS) file handle.
4315  */
4316 #ifndef _SYS_SYSPROTO_H_
4317 struct fhstat_args {
4318         struct fhandle *u_fhp;
4319         struct stat *sb;
4320 };
4321 #endif
4322 int
4323 sys_fhstat(td, uap)
4324         struct thread *td;
4325         register struct fhstat_args /* {
4326                 struct fhandle *u_fhp;
4327                 struct stat *sb;
4328         } */ *uap;
4329 {
4330         struct stat sb;
4331         struct fhandle fh;
4332         int error;
4333
4334         error = copyin(uap->u_fhp, &fh, sizeof(fh));
4335         if (error != 0)
4336                 return (error);
4337         error = kern_fhstat(td, fh, &sb);
4338         if (error == 0)
4339                 error = copyout(&sb, uap->sb, sizeof(sb));
4340         return (error);
4341 }
4342
4343 int
4344 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4345 {
4346         struct mount *mp;
4347         struct vnode *vp;
4348         int error;
4349
4350         error = priv_check(td, PRIV_VFS_FHSTAT);
4351         if (error != 0)
4352                 return (error);
4353         if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4354                 return (ESTALE);
4355         error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4356         vfs_unbusy(mp);
4357         if (error != 0)
4358                 return (error);
4359         error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4360         vput(vp);
4361         return (error);
4362 }
4363
4364 /*
4365  * Implement fstatfs() for (NFS) file handles.
4366  */
4367 #ifndef _SYS_SYSPROTO_H_
4368 struct fhstatfs_args {
4369         struct fhandle *u_fhp;
4370         struct statfs *buf;
4371 };
4372 #endif
4373 int
4374 sys_fhstatfs(td, uap)
4375         struct thread *td;
4376         struct fhstatfs_args /* {
4377                 struct fhandle *u_fhp;
4378                 struct statfs *buf;
4379         } */ *uap;
4380 {
4381         struct statfs *sfp;
4382         fhandle_t fh;
4383         int error;
4384
4385         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4386         if (error != 0)
4387                 return (error);
4388         sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4389         error = kern_fhstatfs(td, fh, sfp);
4390         if (error == 0)
4391                 error = copyout(sfp, uap->buf, sizeof(*sfp));
4392         free(sfp, M_STATFS);
4393         return (error);
4394 }
4395
4396 int
4397 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4398 {
4399         struct statfs *sp;
4400         struct mount *mp;
4401         struct vnode *vp;
4402         int error;
4403
4404         error = priv_check(td, PRIV_VFS_FHSTATFS);
4405         if (error != 0)
4406                 return (error);
4407         if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4408                 return (ESTALE);
4409         error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4410         if (error != 0) {
4411                 vfs_unbusy(mp);
4412                 return (error);
4413         }
4414         vput(vp);
4415         error = prison_canseemount(td->td_ucred, mp);
4416         if (error != 0)
4417                 goto out;
4418 #ifdef MAC
4419         error = mac_mount_check_stat(td->td_ucred, mp);
4420         if (error != 0)
4421                 goto out;
4422 #endif
4423         /*
4424          * Set these in case the underlying filesystem fails to do so.
4425          */
4426         sp = &mp->mnt_stat;
4427         sp->f_version = STATFS_VERSION;
4428         sp->f_namemax = NAME_MAX;
4429         sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4430         error = VFS_STATFS(mp, sp);
4431         if (error == 0)
4432                 *buf = *sp;
4433 out:
4434         vfs_unbusy(mp);
4435         return (error);
4436 }
4437
4438 int
4439 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4440 {
4441         struct file *fp;
4442         struct mount *mp;
4443         struct vnode *vp;
4444         cap_rights_t rights;
4445         off_t olen, ooffset;
4446         int error;
4447
4448         if (offset < 0 || len <= 0)
4449                 return (EINVAL);
4450         /* Check for wrap. */
4451         if (offset > OFF_MAX - len)
4452                 return (EFBIG);
4453         error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4454         if (error != 0)
4455                 return (error);
4456         if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4457                 error = ESPIPE;
4458                 goto out;
4459         }
4460         if ((fp->f_flag & FWRITE) == 0) {
4461                 error = EBADF;
4462                 goto out;
4463         }
4464         if (fp->f_type != DTYPE_VNODE) {
4465                 error = ENODEV;
4466                 goto out;
4467         }
4468         vp = fp->f_vnode;
4469         if (vp->v_type != VREG) {
4470                 error = ENODEV;
4471                 goto out;
4472         }
4473
4474         /* Allocating blocks may take a long time, so iterate. */
4475         for (;;) {
4476                 olen = len;
4477                 ooffset = offset;
4478
4479                 bwillwrite();
4480                 mp = NULL;
4481                 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4482                 if (error != 0)
4483                         break;
4484                 error = vn_lock(vp, LK_EXCLUSIVE);
4485                 if (error != 0) {
4486                         vn_finished_write(mp);
4487                         break;
4488                 }
4489 #ifdef MAC
4490                 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4491                 if (error == 0)
4492 #endif
4493                         error = VOP_ALLOCATE(vp, &offset, &len);
4494                 VOP_UNLOCK(vp, 0);
4495                 vn_finished_write(mp);
4496
4497                 if (olen + ooffset != offset + len) {
4498                         panic("offset + len changed from %jx/%jx to %jx/%jx",
4499                             ooffset, olen, offset, len);
4500                 }
4501                 if (error != 0 || len == 0)
4502                         break;
4503                 KASSERT(olen > len, ("Iteration did not make progress?"));
4504                 maybe_yield();
4505         }
4506  out:
4507         fdrop(fp, td);
4508         return (error);
4509 }
4510
4511 int
4512 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4513 {
4514         int error;
4515
4516         error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4517         return (kern_posix_error(td, error));
4518 }
4519
4520 /*
4521  * Unlike madvise(2), we do not make a best effort to remember every
4522  * possible caching hint.  Instead, we remember the last setting with
4523  * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4524  * region of any current setting.
4525  */
4526 int
4527 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4528     int advice)
4529 {
4530         struct fadvise_info *fa, *new;
4531         struct file *fp;
4532         struct vnode *vp;
4533         cap_rights_t rights;
4534         off_t end;
4535         int error;
4536
4537         if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4538                 return (EINVAL);
4539         switch (advice) {
4540         case POSIX_FADV_SEQUENTIAL:
4541         case POSIX_FADV_RANDOM:
4542         case POSIX_FADV_NOREUSE:
4543                 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4544                 break;
4545         case POSIX_FADV_NORMAL:
4546         case POSIX_FADV_WILLNEED:
4547         case POSIX_FADV_DONTNEED:
4548                 new = NULL;
4549                 break;
4550         default:
4551                 return (EINVAL);
4552         }
4553         /* XXX: CAP_POSIX_FADVISE? */
4554         error = fget(td, fd, cap_rights_init(&rights), &fp);
4555         if (error != 0)
4556                 goto out;
4557         if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4558                 error = ESPIPE;
4559                 goto out;
4560         }
4561         if (fp->f_type != DTYPE_VNODE) {
4562                 error = ENODEV;
4563                 goto out;
4564         }
4565         vp = fp->f_vnode;
4566         if (vp->v_type != VREG) {
4567                 error = ENODEV;
4568                 goto out;
4569         }
4570         if (len == 0)
4571                 end = OFF_MAX;
4572         else
4573                 end = offset + len - 1;
4574         switch (advice) {
4575         case POSIX_FADV_SEQUENTIAL:
4576         case POSIX_FADV_RANDOM:
4577         case POSIX_FADV_NOREUSE:
4578                 /*
4579                  * Try to merge any existing non-standard region with
4580                  * this new region if possible, otherwise create a new
4581                  * non-standard region for this request.
4582                  */
4583                 mtx_pool_lock(mtxpool_sleep, fp);
4584                 fa = fp->f_advice;
4585                 if (fa != NULL && fa->fa_advice == advice &&
4586                     ((fa->fa_start <= end && fa->fa_end >= offset) ||
4587                     (end != OFF_MAX && fa->fa_start == end + 1) ||
4588                     (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4589                         if (offset < fa->fa_start)
4590                                 fa->fa_start = offset;
4591                         if (end > fa->fa_end)
4592                                 fa->fa_end = end;
4593                 } else {
4594                         new->fa_advice = advice;
4595                         new->fa_start = offset;
4596                         new->fa_end = end;
4597                         fp->f_advice = new;
4598                         new = fa;
4599                 }
4600                 mtx_pool_unlock(mtxpool_sleep, fp);
4601                 break;
4602         case POSIX_FADV_NORMAL:
4603                 /*
4604                  * If a the "normal" region overlaps with an existing
4605                  * non-standard region, trim or remove the
4606                  * non-standard region.
4607                  */
4608                 mtx_pool_lock(mtxpool_sleep, fp);
4609                 fa = fp->f_advice;
4610                 if (fa != NULL) {
4611                         if (offset <= fa->fa_start && end >= fa->fa_end) {
4612                                 new = fa;
4613                                 fp->f_advice = NULL;
4614                         } else if (offset <= fa->fa_start &&
4615                             end >= fa->fa_start)
4616                                 fa->fa_start = end + 1;
4617                         else if (offset <= fa->fa_end && end >= fa->fa_end)
4618                                 fa->fa_end = offset - 1;
4619                         else if (offset >= fa->fa_start && end <= fa->fa_end) {
4620                                 /*
4621                                  * If the "normal" region is a middle
4622                                  * portion of the existing
4623                                  * non-standard region, just remove
4624                                  * the whole thing rather than picking
4625                                  * one side or the other to
4626                                  * preserve.
4627                                  */
4628                                 new = fa;
4629                                 fp->f_advice = NULL;
4630                         }
4631                 }
4632                 mtx_pool_unlock(mtxpool_sleep, fp);
4633                 break;
4634         case POSIX_FADV_WILLNEED:
4635         case POSIX_FADV_DONTNEED:
4636                 error = VOP_ADVISE(vp, offset, end, advice);
4637                 break;
4638         }
4639 out:
4640         if (fp != NULL)
4641                 fdrop(fp, td);
4642         free(new, M_FADVISE);
4643         return (error);
4644 }
4645
4646 int
4647 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4648 {
4649         int error;
4650
4651         error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4652             uap->advice);
4653         return (kern_posix_error(td, error));
4654 }