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