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