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