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