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