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