Deleted Added
sdiff udiff text old ( 101983 ) new ( 102112 )
full compact
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: head/sys/kern/vfs_extattr.c 102112 2002-08-19 16:43:25Z rwatson $
40 */
41
42/* For 4.3 integer FS ID compatibility */
43#include "opt_compat.h"
44#include "opt_mac.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/bio.h>
49#include <sys/buf.h>
50#include <sys/sysent.h>
51#include <sys/mac.h>
52#include <sys/malloc.h>
53#include <sys/mount.h>
54#include <sys/mutex.h>
55#include <sys/sysproto.h>
56#include <sys/namei.h>
57#include <sys/filedesc.h>
58#include <sys/kernel.h>
59#include <sys/fcntl.h>
60#include <sys/file.h>
61#include <sys/linker.h>
62#include <sys/stat.h>
63#include <sys/sx.h>
64#include <sys/unistd.h>
65#include <sys/vnode.h>
66#include <sys/proc.h>
67#include <sys/dirent.h>
68#include <sys/extattr.h>
69#include <sys/jail.h>
70#include <sys/sysctl.h>
71
72#include <machine/limits.h>
73#include <machine/stdarg.h>
74
75#include <vm/vm.h>
76#include <vm/vm_object.h>
77#include <vm/vm_page.h>
78#include <vm/uma.h>
79
80static int change_dir(struct nameidata *ndp, struct thread *td);
81static int chroot_refuse_vdir_fds(struct filedesc *fdp);
82static int getutimes(const struct timeval *, struct timespec *);
83static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
84static int setfmode(struct thread *td, struct vnode *, int);
85static int setfflags(struct thread *td, struct vnode *, int);
86static int setutimes(struct thread *td, struct vnode *,
87 const struct timespec *, int, int);
88static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
89 struct thread *td);
90
91int (*union_dircheckp)(struct thread *td, struct vnode **, struct file *);
92int (*softdep_fsync_hook)(struct vnode *);
93
94/*
95 * Sync each mounted filesystem.
96 */
97#ifndef _SYS_SYSPROTO_H_
98struct sync_args {
99 int dummy;
100};
101#endif
102
103#ifdef DEBUG
104static int syncprt = 0;
105SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
106#endif
107
108/* ARGSUSED */
109int
110sync(td, uap)
111 struct thread *td;
112 struct sync_args *uap;
113{
114 struct mount *mp, *nmp;
115 int asyncflag;
116
117 mtx_lock(&mountlist_mtx);
118 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
119 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
120 nmp = TAILQ_NEXT(mp, mnt_list);
121 continue;
122 }
123 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
124 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
125 asyncflag = mp->mnt_flag & MNT_ASYNC;
126 mp->mnt_flag &= ~MNT_ASYNC;
127 vfs_msync(mp, MNT_NOWAIT);
128 VFS_SYNC(mp, MNT_NOWAIT,
129 ((td != NULL) ? td->td_ucred : NOCRED), td);
130 mp->mnt_flag |= asyncflag;
131 vn_finished_write(mp);
132 }
133 mtx_lock(&mountlist_mtx);
134 nmp = TAILQ_NEXT(mp, mnt_list);
135 vfs_unbusy(mp, td);
136 }
137 mtx_unlock(&mountlist_mtx);
138#if 0
139/*
140 * XXX don't call vfs_bufstats() yet because that routine
141 * was not imported in the Lite2 merge.
142 */
143#ifdef DIAGNOSTIC
144 if (syncprt)
145 vfs_bufstats();
146#endif /* DIAGNOSTIC */
147#endif
148 return (0);
149}
150
151/* XXX PRISON: could be per prison flag */
152static int prison_quotas;
153#if 0
154SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
155#endif
156
157/*
158 * Change filesystem quotas.
159 */
160#ifndef _SYS_SYSPROTO_H_
161struct quotactl_args {
162 char *path;
163 int cmd;
164 int uid;
165 caddr_t arg;
166};
167#endif
168/* ARGSUSED */
169int
170quotactl(td, uap)
171 struct thread *td;
172 register struct quotactl_args /* {
173 syscallarg(char *) path;
174 syscallarg(int) cmd;
175 syscallarg(int) uid;
176 syscallarg(caddr_t) arg;
177 } */ *uap;
178{
179 struct mount *mp;
180 int error;
181 struct nameidata nd;
182
183 if (jailed(td->td_ucred) && !prison_quotas)
184 return (EPERM);
185 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
186 if ((error = namei(&nd)) != 0)
187 return (error);
188 NDFREE(&nd, NDF_ONLY_PNBUF);
189 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
190 vrele(nd.ni_vp);
191 if (error)
192 return (error);
193 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
194 SCARG(uap, arg), td);
195 vn_finished_write(mp);
196 return (error);
197}
198
199/*
200 * Get filesystem statistics.
201 */
202#ifndef _SYS_SYSPROTO_H_
203struct statfs_args {
204 char *path;
205 struct statfs *buf;
206};
207#endif
208/* ARGSUSED */
209int
210statfs(td, uap)
211 struct thread *td;
212 register struct statfs_args /* {
213 syscallarg(char *) path;
214 syscallarg(struct statfs *) buf;
215 } */ *uap;
216{
217 register struct mount *mp;
218 register struct statfs *sp;
219 int error;
220 struct nameidata nd;
221 struct statfs sb;
222
223 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
224 if ((error = namei(&nd)) != 0)
225 return (error);
226 mp = nd.ni_vp->v_mount;
227 sp = &mp->mnt_stat;
228 NDFREE(&nd, NDF_ONLY_PNBUF);
229 vrele(nd.ni_vp);
230#ifdef MAC
231 error = mac_check_mount_stat(td->td_ucred, mp);
232 if (error)
233 return (error);
234#endif
235 error = VFS_STATFS(mp, sp, td);
236 if (error)
237 return (error);
238 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
239 if (suser(td)) {
240 bcopy(sp, &sb, sizeof(sb));
241 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
242 sp = &sb;
243 }
244 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
245}
246
247/*
248 * Get filesystem statistics.
249 */
250#ifndef _SYS_SYSPROTO_H_
251struct fstatfs_args {
252 int fd;
253 struct statfs *buf;
254};
255#endif
256/* ARGSUSED */
257int
258fstatfs(td, uap)
259 struct thread *td;
260 register struct fstatfs_args /* {
261 syscallarg(int) fd;
262 syscallarg(struct statfs *) buf;
263 } */ *uap;
264{
265 struct file *fp;
266 struct mount *mp;
267 register struct statfs *sp;
268 int error;
269 struct statfs sb;
270
271 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
272 return (error);
273 mp = ((struct vnode *)fp->f_data)->v_mount;
274 fdrop(fp, td);
275 if (mp == NULL)
276 return (EBADF);
277#ifdef MAC
278 error = mac_check_mount_stat(td->td_ucred, mp);
279 if (error)
280 return (error);
281#endif
282 sp = &mp->mnt_stat;
283 error = VFS_STATFS(mp, sp, td);
284 if (error)
285 return (error);
286 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
287 if (suser(td)) {
288 bcopy(sp, &sb, sizeof(sb));
289 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
290 sp = &sb;
291 }
292 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
293}
294
295/*
296 * Get statistics on all filesystems.
297 */
298#ifndef _SYS_SYSPROTO_H_
299struct getfsstat_args {
300 struct statfs *buf;
301 long bufsize;
302 int flags;
303};
304#endif
305int
306getfsstat(td, uap)
307 struct thread *td;
308 register struct getfsstat_args /* {
309 syscallarg(struct statfs *) buf;
310 syscallarg(long) bufsize;
311 syscallarg(int) flags;
312 } */ *uap;
313{
314 register struct mount *mp, *nmp;
315 register struct statfs *sp;
316 caddr_t sfsp;
317 long count, maxcount, error;
318
319 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
320 sfsp = (caddr_t)SCARG(uap, buf);
321 count = 0;
322 mtx_lock(&mountlist_mtx);
323 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
324#ifdef MAC
325 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
326 nmp = TAILQ_NEXT(mp, mnt_list);
327 continue;
328 }
329#endif
330 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
331 nmp = TAILQ_NEXT(mp, mnt_list);
332 continue;
333 }
334 if (sfsp && count < maxcount) {
335 sp = &mp->mnt_stat;
336 /*
337 * If MNT_NOWAIT or MNT_LAZY is specified, do not
338 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
339 * overrides MNT_WAIT.
340 */
341 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
342 (SCARG(uap, flags) & MNT_WAIT)) &&
343 (error = VFS_STATFS(mp, sp, td))) {
344 mtx_lock(&mountlist_mtx);
345 nmp = TAILQ_NEXT(mp, mnt_list);
346 vfs_unbusy(mp, td);
347 continue;
348 }
349 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
350 error = copyout(sp, sfsp, sizeof(*sp));
351 if (error) {
352 vfs_unbusy(mp, td);
353 return (error);
354 }
355 sfsp += sizeof(*sp);
356 }
357 count++;
358 mtx_lock(&mountlist_mtx);
359 nmp = TAILQ_NEXT(mp, mnt_list);
360 vfs_unbusy(mp, td);
361 }
362 mtx_unlock(&mountlist_mtx);
363 if (sfsp && count > maxcount)
364 td->td_retval[0] = maxcount;
365 else
366 td->td_retval[0] = count;
367 return (0);
368}
369
370/*
371 * Change current working directory to a given file descriptor.
372 */
373#ifndef _SYS_SYSPROTO_H_
374struct fchdir_args {
375 int fd;
376};
377#endif
378/* ARGSUSED */
379int
380fchdir(td, uap)
381 struct thread *td;
382 struct fchdir_args /* {
383 syscallarg(int) fd;
384 } */ *uap;
385{
386 register struct filedesc *fdp = td->td_proc->p_fd;
387 struct vnode *vp, *tdp, *vpold;
388 struct mount *mp;
389 struct file *fp;
390 int error;
391
392 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
393 return (error);
394 vp = (struct vnode *)fp->f_data;
395 VREF(vp);
396 fdrop(fp, td);
397 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
398 if (vp->v_type != VDIR)
399 error = ENOTDIR;
400#ifdef MAC
401 else if ((error = mac_check_vnode_chdir(td->td_ucred, vp)) != 0) {
402 }
403#endif
404 else
405 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
406 while (!error && (mp = vp->v_mountedhere) != NULL) {
407 if (vfs_busy(mp, 0, 0, td))
408 continue;
409 error = VFS_ROOT(mp, &tdp);
410 vfs_unbusy(mp, td);
411 if (error)
412 break;
413 vput(vp);
414 vp = tdp;
415 }
416 if (error) {
417 vput(vp);
418 return (error);
419 }
420 VOP_UNLOCK(vp, 0, td);
421 FILEDESC_LOCK(fdp);
422 vpold = fdp->fd_cdir;
423 fdp->fd_cdir = vp;
424 FILEDESC_UNLOCK(fdp);
425 vrele(vpold);
426 return (0);
427}
428
429/*
430 * Change current working directory (``.'').
431 */
432#ifndef _SYS_SYSPROTO_H_
433struct chdir_args {
434 char *path;
435};
436#endif
437/* ARGSUSED */
438int
439chdir(td, uap)
440 struct thread *td;
441 struct chdir_args /* {
442 syscallarg(char *) path;
443 } */ *uap;
444{
445 register struct filedesc *fdp = td->td_proc->p_fd;
446 int error;
447 struct nameidata nd;
448 struct vnode *vp;
449
450 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
451 SCARG(uap, path), td);
452 if ((error = change_dir(&nd, td)) != 0)
453 return (error);
454 NDFREE(&nd, NDF_ONLY_PNBUF);
455 FILEDESC_LOCK(fdp);
456 vp = fdp->fd_cdir;
457 fdp->fd_cdir = nd.ni_vp;
458 FILEDESC_UNLOCK(fdp);
459 vrele(vp);
460 return (0);
461}
462
463/*
464 * Helper function for raised chroot(2) security function: Refuse if
465 * any filedescriptors are open directories.
466 */
467static int
468chroot_refuse_vdir_fds(fdp)
469 struct filedesc *fdp;
470{
471 struct vnode *vp;
472 struct file *fp;
473 int fd;
474
475 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
476 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
477 fp = fget_locked(fdp, fd);
478 if (fp == NULL)
479 continue;
480 if (fp->f_type == DTYPE_VNODE) {
481 vp = (struct vnode *)fp->f_data;
482 if (vp->v_type == VDIR)
483 return (EPERM);
484 }
485 }
486 return (0);
487}
488
489/*
490 * This sysctl determines if we will allow a process to chroot(2) if it
491 * has a directory open:
492 * 0: disallowed for all processes.
493 * 1: allowed for processes that were not already chroot(2)'ed.
494 * 2: allowed for all processes.
495 */
496
497static int chroot_allow_open_directories = 1;
498
499SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
500 &chroot_allow_open_directories, 0, "");
501
502/*
503 * Change notion of root (``/'') directory.
504 */
505#ifndef _SYS_SYSPROTO_H_
506struct chroot_args {
507 char *path;
508};
509#endif
510/* ARGSUSED */
511int
512chroot(td, uap)
513 struct thread *td;
514 struct chroot_args /* {
515 syscallarg(char *) path;
516 } */ *uap;
517{
518 register struct filedesc *fdp = td->td_proc->p_fd;
519 int error;
520 struct nameidata nd;
521 struct vnode *vp;
522
523 error = suser_cred(td->td_ucred, PRISON_ROOT);
524 if (error)
525 return (error);
526 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
527 SCARG(uap, path), td);
528 mtx_lock(&Giant);
529 if ((error = change_dir(&nd, td)) != 0)
530 goto error;
531#ifdef MAC
532 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
533 goto error;
534#endif
535 FILEDESC_LOCK(fdp);
536 if (chroot_allow_open_directories == 0 ||
537 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
538 error = chroot_refuse_vdir_fds(fdp);
539 if (error)
540 goto error_unlock;
541 }
542 vp = fdp->fd_rdir;
543 fdp->fd_rdir = nd.ni_vp;
544 if (!fdp->fd_jdir) {
545 fdp->fd_jdir = nd.ni_vp;
546 VREF(fdp->fd_jdir);
547 }
548 FILEDESC_UNLOCK(fdp);
549 NDFREE(&nd, NDF_ONLY_PNBUF);
550 vrele(vp);
551 mtx_unlock(&Giant);
552 return (0);
553error_unlock:
554 FILEDESC_UNLOCK(fdp);
555error:
556 mtx_unlock(&Giant);
557 NDFREE(&nd, 0);
558 return (error);
559}
560
561/*
562 * Common routine for chroot and chdir.
563 */
564static int
565change_dir(ndp, td)
566 register struct nameidata *ndp;
567 struct thread *td;
568{
569 struct vnode *vp;
570 int error;
571
572 error = namei(ndp);
573 if (error)
574 return (error);
575 vp = ndp->ni_vp;
576 if (vp->v_type != VDIR)
577 error = ENOTDIR;
578#ifdef MAC
579 else if ((error = mac_check_vnode_chdir(td->td_ucred, vp)) != 0) {
580 }
581#endif
582 else
583 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
584 if (error)
585 vput(vp);
586 else
587 VOP_UNLOCK(vp, 0, td);
588 return (error);
589}
590
591/*
592 * Check permissions, allocate an open file structure,
593 * and call the device open routine if any.
594 */
595#ifndef _SYS_SYSPROTO_H_
596struct open_args {
597 char *path;
598 int flags;
599 int mode;
600};
601#endif
602int
603open(td, uap)
604 struct thread *td;
605 register struct open_args /* {
606 syscallarg(char *) path;
607 syscallarg(int) flags;
608 syscallarg(int) mode;
609 } */ *uap;
610{
611 struct proc *p = td->td_proc;
612 struct filedesc *fdp = p->p_fd;
613 struct file *fp;
614 struct vnode *vp;
615 struct vattr vat;
616 struct mount *mp;
617 int cmode, flags, oflags;
618 struct file *nfp;
619 int type, indx, error;
620 struct flock lf;
621 struct nameidata nd;
622
623 oflags = SCARG(uap, flags);
624 if ((oflags & O_ACCMODE) == O_ACCMODE)
625 return (EINVAL);
626 flags = FFLAGS(oflags);
627 error = falloc(td, &nfp, &indx);
628 if (error)
629 return (error);
630 fp = nfp;
631 FILEDESC_LOCK(fdp);
632 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
633 FILEDESC_UNLOCK(fdp);
634 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
635 td->td_dupfd = -indx - 1; /* XXX check for fdopen */
636 /*
637 * Bump the ref count to prevent another process from closing
638 * the descriptor while we are blocked in vn_open()
639 */
640 fhold(fp);
641 error = vn_open(&nd, &flags, cmode);
642 if (error) {
643 /*
644 * release our own reference
645 */
646 fdrop(fp, td);
647
648 /*
649 * handle special fdopen() case. bleh. dupfdopen() is
650 * responsible for dropping the old contents of ofiles[indx]
651 * if it succeeds.
652 */
653 if ((error == ENODEV || error == ENXIO) &&
654 td->td_dupfd >= 0 && /* XXX from fdopen */
655 (error =
656 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
657 td->td_retval[0] = indx;
658 return (0);
659 }
660 /*
661 * Clean up the descriptor, but only if another thread hadn't
662 * replaced or closed it.
663 */
664 FILEDESC_LOCK(fdp);
665 if (fdp->fd_ofiles[indx] == fp) {
666 fdp->fd_ofiles[indx] = NULL;
667 FILEDESC_UNLOCK(fdp);
668 fdrop(fp, td);
669 } else
670 FILEDESC_UNLOCK(fdp);
671
672 if (error == ERESTART)
673 error = EINTR;
674 return (error);
675 }
676 td->td_dupfd = 0;
677 NDFREE(&nd, NDF_ONLY_PNBUF);
678 vp = nd.ni_vp;
679
680 /*
681 * There should be 2 references on the file, one from the descriptor
682 * table, and one for us.
683 *
684 * Handle the case where someone closed the file (via its file
685 * descriptor) while we were blocked. The end result should look
686 * like opening the file succeeded but it was immediately closed.
687 */
688 FILEDESC_LOCK(fdp);
689 FILE_LOCK(fp);
690 if (fp->f_count == 1) {
691 KASSERT(fdp->fd_ofiles[indx] != fp,
692 ("Open file descriptor lost all refs"));
693 FILEDESC_UNLOCK(fdp);
694 FILE_UNLOCK(fp);
695 VOP_UNLOCK(vp, 0, td);
696 vn_close(vp, flags & FMASK, fp->f_cred, td);
697 fdrop(fp, td);
698 td->td_retval[0] = indx;
699 return 0;
700 }
701
702 /* assert that vn_open created a backing object if one is needed */
703 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
704 ("open: vmio vnode has no backing object after vn_open"));
705
706 fp->f_data = vp;
707 fp->f_flag = flags & FMASK;
708 fp->f_ops = &vnops;
709 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
710 FILEDESC_UNLOCK(fdp);
711 FILE_UNLOCK(fp);
712 VOP_UNLOCK(vp, 0, td);
713 if (flags & (O_EXLOCK | O_SHLOCK)) {
714 lf.l_whence = SEEK_SET;
715 lf.l_start = 0;
716 lf.l_len = 0;
717 if (flags & O_EXLOCK)
718 lf.l_type = F_WRLCK;
719 else
720 lf.l_type = F_RDLCK;
721 type = F_FLOCK;
722 if ((flags & FNONBLOCK) == 0)
723 type |= F_WAIT;
724 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
725 type)) != 0)
726 goto bad;
727 fp->f_flag |= FHASLOCK;
728 }
729 if (flags & O_TRUNC) {
730 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
731 goto bad;
732 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
733 VATTR_NULL(&vat);
734 vat.va_size = 0;
735 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
736#ifdef MAC
737 error = mac_check_vnode_write(td->td_ucred, vp);
738 if (error == 0)
739#endif
740 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
741 VOP_UNLOCK(vp, 0, td);
742 vn_finished_write(mp);
743 if (error)
744 goto bad;
745 }
746 /*
747 * Release our private reference, leaving the one associated with
748 * the descriptor table intact.
749 */
750 fdrop(fp, td);
751 td->td_retval[0] = indx;
752 return (0);
753bad:
754 FILEDESC_LOCK(fdp);
755 if (fdp->fd_ofiles[indx] == fp) {
756 fdp->fd_ofiles[indx] = NULL;
757 FILEDESC_UNLOCK(fdp);
758 fdrop(fp, td);
759 } else
760 FILEDESC_UNLOCK(fdp);
761 return (error);
762}
763
764#ifdef COMPAT_43
765/*
766 * Create a file.
767 */
768#ifndef _SYS_SYSPROTO_H_
769struct ocreat_args {
770 char *path;
771 int mode;
772};
773#endif
774int
775ocreat(td, uap)
776 struct thread *td;
777 register struct ocreat_args /* {
778 syscallarg(char *) path;
779 syscallarg(int) mode;
780 } */ *uap;
781{
782 struct open_args /* {
783 syscallarg(char *) path;
784 syscallarg(int) flags;
785 syscallarg(int) mode;
786 } */ nuap;
787
788 SCARG(&nuap, path) = SCARG(uap, path);
789 SCARG(&nuap, mode) = SCARG(uap, mode);
790 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
791 return (open(td, &nuap));
792}
793#endif /* COMPAT_43 */
794
795/*
796 * Create a special file.
797 */
798#ifndef _SYS_SYSPROTO_H_
799struct mknod_args {
800 char *path;
801 int mode;
802 int dev;
803};
804#endif
805/* ARGSUSED */
806int
807mknod(td, uap)
808 struct thread *td;
809 register struct mknod_args /* {
810 syscallarg(char *) path;
811 syscallarg(int) mode;
812 syscallarg(int) dev;
813 } */ *uap;
814{
815 struct vnode *vp;
816 struct mount *mp;
817 struct vattr vattr;
818 int error;
819 int whiteout = 0;
820 struct nameidata nd;
821
822 switch (SCARG(uap, mode) & S_IFMT) {
823 case S_IFCHR:
824 case S_IFBLK:
825 error = suser(td);
826 break;
827 default:
828 error = suser_cred(td->td_ucred, PRISON_ROOT);
829 break;
830 }
831 if (error)
832 return (error);
833restart:
834 bwillwrite();
835 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME, UIO_USERSPACE,
836 SCARG(uap, path), td);
837 if ((error = namei(&nd)) != 0)
838 return (error);
839 vp = nd.ni_vp;
840 if (vp != NULL) {
841 vrele(vp);
842 error = EEXIST;
843 } else {
844 VATTR_NULL(&vattr);
845 FILEDESC_LOCK(td->td_proc->p_fd);
846 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ td->td_proc->p_fd->fd_cmask;
847 FILEDESC_UNLOCK(td->td_proc->p_fd);
848 vattr.va_rdev = SCARG(uap, dev);
849 whiteout = 0;
850
851 switch (SCARG(uap, mode) & S_IFMT) {
852 case S_IFMT: /* used by badsect to flag bad sectors */
853 vattr.va_type = VBAD;
854 break;
855 case S_IFCHR:
856 vattr.va_type = VCHR;
857 break;
858 case S_IFBLK:
859 vattr.va_type = VBLK;
860 break;
861 case S_IFWHT:
862 whiteout = 1;
863 break;
864 default:
865 error = EINVAL;
866 break;
867 }
868 }
869 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
870 NDFREE(&nd, NDF_ONLY_PNBUF);
871 vput(nd.ni_dvp);
872 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
873 return (error);
874 goto restart;
875 }
876 if (!error) {
877 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
878 if (whiteout)
879 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
880 else {
881 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
882 &nd.ni_cnd, &vattr);
883 if (error == 0)
884 vput(nd.ni_vp);
885 }
886 }
887 NDFREE(&nd, NDF_ONLY_PNBUF);
888 vput(nd.ni_dvp);
889 vn_finished_write(mp);
890 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
891 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
892 return (error);
893}
894
895/*
896 * Create a named pipe.
897 */
898#ifndef _SYS_SYSPROTO_H_
899struct mkfifo_args {
900 char *path;
901 int mode;
902};
903#endif
904/* ARGSUSED */
905int
906mkfifo(td, uap)
907 struct thread *td;
908 register struct mkfifo_args /* {
909 syscallarg(char *) path;
910 syscallarg(int) mode;
911 } */ *uap;
912{
913 struct mount *mp;
914 struct vattr vattr;
915 int error;
916 struct nameidata nd;
917
918restart:
919 bwillwrite();
920 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME, UIO_USERSPACE,
921 SCARG(uap, path), td);
922 if ((error = namei(&nd)) != 0)
923 return (error);
924 if (nd.ni_vp != NULL) {
925 NDFREE(&nd, NDF_ONLY_PNBUF);
926 vrele(nd.ni_vp);
927 vput(nd.ni_dvp);
928 return (EEXIST);
929 }
930 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
931 NDFREE(&nd, NDF_ONLY_PNBUF);
932 vput(nd.ni_dvp);
933 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
934 return (error);
935 goto restart;
936 }
937 VATTR_NULL(&vattr);
938 vattr.va_type = VFIFO;
939 FILEDESC_LOCK(td->td_proc->p_fd);
940 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ td->td_proc->p_fd->fd_cmask;
941 FILEDESC_UNLOCK(td->td_proc->p_fd);
942 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
943 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
944 if (error == 0)
945 vput(nd.ni_vp);
946 NDFREE(&nd, NDF_ONLY_PNBUF);
947 vput(nd.ni_dvp);
948 vn_finished_write(mp);
949 return (error);
950}
951
952/*
953 * Make a hard file link.
954 */
955#ifndef _SYS_SYSPROTO_H_
956struct link_args {
957 char *path;
958 char *link;
959};
960#endif
961/* ARGSUSED */
962int
963link(td, uap)
964 struct thread *td;
965 register struct link_args /* {
966 syscallarg(char *) path;
967 syscallarg(char *) link;
968 } */ *uap;
969{
970 struct vnode *vp;
971 struct mount *mp;
972 struct nameidata nd;
973 int error;
974
975 bwillwrite();
976 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
977 if ((error = namei(&nd)) != 0)
978 return (error);
979 NDFREE(&nd, NDF_ONLY_PNBUF);
980 vp = nd.ni_vp;
981 if (vp->v_type == VDIR) {
982 vrele(vp);
983 return (EPERM); /* POSIX */
984 }
985 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
986 vrele(vp);
987 return (error);
988 }
989 NDINIT(&nd, CREATE, LOCKPARENT | NOOBJ | SAVENAME, UIO_USERSPACE,
990 SCARG(uap, link), td);
991 if ((error = namei(&nd)) == 0) {
992 if (nd.ni_vp != NULL) {
993 vrele(nd.ni_vp);
994 error = EEXIST;
995 } else {
996 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
997 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
998 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
999 }
1000 NDFREE(&nd, NDF_ONLY_PNBUF);
1001 vput(nd.ni_dvp);
1002 }
1003 vrele(vp);
1004 vn_finished_write(mp);
1005 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1006 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1007 return (error);
1008}
1009
1010/*
1011 * Make a symbolic link.
1012 */
1013#ifndef _SYS_SYSPROTO_H_
1014struct symlink_args {
1015 char *path;
1016 char *link;
1017};
1018#endif
1019/* ARGSUSED */
1020int
1021symlink(td, uap)
1022 struct thread *td;
1023 register struct symlink_args /* {
1024 syscallarg(char *) path;
1025 syscallarg(char *) link;
1026 } */ *uap;
1027{
1028 struct mount *mp;
1029 struct vattr vattr;
1030 char *path;
1031 int error;
1032 struct nameidata nd;
1033
1034 path = uma_zalloc(namei_zone, M_WAITOK);
1035 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1036 goto out;
1037restart:
1038 bwillwrite();
1039 NDINIT(&nd, CREATE, LOCKPARENT | NOOBJ | SAVENAME, UIO_USERSPACE,
1040 SCARG(uap, link), td);
1041 if ((error = namei(&nd)) != 0)
1042 goto out;
1043 if (nd.ni_vp) {
1044 NDFREE(&nd, NDF_ONLY_PNBUF);
1045 vrele(nd.ni_vp);
1046 vput(nd.ni_dvp);
1047 error = EEXIST;
1048 goto out;
1049 }
1050 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1051 NDFREE(&nd, NDF_ONLY_PNBUF);
1052 vput(nd.ni_dvp);
1053 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1054 return (error);
1055 goto restart;
1056 }
1057 VATTR_NULL(&vattr);
1058 FILEDESC_LOCK(td->td_proc->p_fd);
1059 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1060 FILEDESC_UNLOCK(td->td_proc->p_fd);
1061 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1062 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1063 NDFREE(&nd, NDF_ONLY_PNBUF);
1064 if (error == 0)
1065 vput(nd.ni_vp);
1066 vput(nd.ni_dvp);
1067 vn_finished_write(mp);
1068 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1069 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1070out:
1071 uma_zfree(namei_zone, path);
1072 return (error);
1073}
1074
1075/*
1076 * Delete a whiteout from the filesystem.
1077 */
1078/* ARGSUSED */
1079int
1080undelete(td, uap)
1081 struct thread *td;
1082 register struct undelete_args /* {
1083 syscallarg(char *) path;
1084 } */ *uap;
1085{
1086 int error;
1087 struct mount *mp;
1088 struct nameidata nd;
1089
1090restart:
1091 bwillwrite();
1092 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1093 SCARG(uap, path), td);
1094 error = namei(&nd);
1095 if (error)
1096 return (error);
1097
1098 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1099 NDFREE(&nd, NDF_ONLY_PNBUF);
1100 if (nd.ni_vp)
1101 vrele(nd.ni_vp);
1102 vput(nd.ni_dvp);
1103 return (EEXIST);
1104 }
1105 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1106 NDFREE(&nd, NDF_ONLY_PNBUF);
1107 vput(nd.ni_dvp);
1108 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1109 return (error);
1110 goto restart;
1111 }
1112 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1113 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1114 NDFREE(&nd, NDF_ONLY_PNBUF);
1115 vput(nd.ni_dvp);
1116 vn_finished_write(mp);
1117 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1118 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1119 return (error);
1120}
1121
1122/*
1123 * Delete a name from the filesystem.
1124 */
1125#ifndef _SYS_SYSPROTO_H_
1126struct unlink_args {
1127 char *path;
1128};
1129#endif
1130/* ARGSUSED */
1131int
1132unlink(td, uap)
1133 struct thread *td;
1134 struct unlink_args /* {
1135 syscallarg(char *) path;
1136 } */ *uap;
1137{
1138 struct mount *mp;
1139 struct vnode *vp;
1140 int error;
1141 struct nameidata nd;
1142
1143restart:
1144 bwillwrite();
1145 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1146 if ((error = namei(&nd)) != 0)
1147 return (error);
1148 vp = nd.ni_vp;
1149 if (vp->v_type == VDIR)
1150 error = EPERM; /* POSIX */
1151 else {
1152 /*
1153 * The root of a mounted filesystem cannot be deleted.
1154 *
1155 * XXX: can this only be a VDIR case?
1156 */
1157 mp_fixme("Accessing vflags w/o the vn lock.");
1158 if (vp->v_vflag & VV_ROOT)
1159 error = EBUSY;
1160 }
1161 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1162 NDFREE(&nd, NDF_ONLY_PNBUF);
1163 vrele(vp);
1164 vput(nd.ni_dvp);
1165 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1166 return (error);
1167 goto restart;
1168 }
1169 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1171 if (!error) {
1172 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1173 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1174 }
1175 NDFREE(&nd, NDF_ONLY_PNBUF);
1176 vput(nd.ni_dvp);
1177 vput(vp);
1178 vn_finished_write(mp);
1179 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1180 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1181 return (error);
1182}
1183
1184/*
1185 * Reposition read/write file offset.
1186 */
1187#ifndef _SYS_SYSPROTO_H_
1188struct lseek_args {
1189 int fd;
1190 int pad;
1191 off_t offset;
1192 int whence;
1193};
1194#endif
1195int
1196lseek(td, uap)
1197 struct thread *td;
1198 register struct lseek_args /* {
1199 syscallarg(int) fd;
1200 syscallarg(int) pad;
1201 syscallarg(off_t) offset;
1202 syscallarg(int) whence;
1203 } */ *uap;
1204{
1205 struct ucred *cred = td->td_ucred;
1206 struct file *fp;
1207 struct vnode *vp;
1208 struct vattr vattr;
1209 off_t offset;
1210 int error, noneg;
1211
1212 if ((error = fget(td, uap->fd, &fp)) != 0)
1213 return (error);
1214 if (fp->f_type != DTYPE_VNODE) {
1215 fdrop(fp, td);
1216 return (ESPIPE);
1217 }
1218 vp = (struct vnode *)fp->f_data;
1219 noneg = (vp->v_type != VCHR);
1220 offset = SCARG(uap, offset);
1221 switch (SCARG(uap, whence)) {
1222 case L_INCR:
1223 if (noneg &&
1224 (fp->f_offset < 0 ||
1225 (offset > 0 && fp->f_offset > OFF_MAX - offset)))
1226 return (EOVERFLOW);
1227 offset += fp->f_offset;
1228 break;
1229 case L_XTND:
1230 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1231 error = VOP_GETATTR(vp, &vattr, cred, td);
1232 VOP_UNLOCK(vp, 0, td);
1233 if (error)
1234 return (error);
1235 if (noneg &&
1236 (vattr.va_size > OFF_MAX ||
1237 (offset > 0 && vattr.va_size > OFF_MAX - offset)))
1238 return (EOVERFLOW);
1239 offset += vattr.va_size;
1240 break;
1241 case L_SET:
1242 break;
1243 default:
1244 fdrop(fp, td);
1245 return (EINVAL);
1246 }
1247 if (noneg && offset < 0)
1248 return (EINVAL);
1249 fp->f_offset = offset;
1250 *(off_t *)(td->td_retval) = fp->f_offset;
1251 fdrop(fp, td);
1252 return (0);
1253}
1254
1255#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1256/*
1257 * Reposition read/write file offset.
1258 */
1259#ifndef _SYS_SYSPROTO_H_
1260struct olseek_args {
1261 int fd;
1262 long offset;
1263 int whence;
1264};
1265#endif
1266int
1267olseek(td, uap)
1268 struct thread *td;
1269 register struct olseek_args /* {
1270 syscallarg(int) fd;
1271 syscallarg(long) offset;
1272 syscallarg(int) whence;
1273 } */ *uap;
1274{
1275 struct lseek_args /* {
1276 syscallarg(int) fd;
1277 syscallarg(int) pad;
1278 syscallarg(off_t) offset;
1279 syscallarg(int) whence;
1280 } */ nuap;
1281 int error;
1282
1283 SCARG(&nuap, fd) = SCARG(uap, fd);
1284 SCARG(&nuap, offset) = SCARG(uap, offset);
1285 SCARG(&nuap, whence) = SCARG(uap, whence);
1286 error = lseek(td, &nuap);
1287 return (error);
1288}
1289#endif /* COMPAT_43 */
1290
1291/*
1292 * Check access permissions using passed credentials.
1293 */
1294static int
1295vn_access(vp, user_flags, cred, td)
1296 struct vnode *vp;
1297 int user_flags;
1298 struct ucred *cred;
1299 struct thread *td;
1300{
1301 int error, flags;
1302
1303 /* Flags == 0 means only check for existence. */
1304 error = 0;
1305 if (user_flags) {
1306 flags = 0;
1307 if (user_flags & R_OK)
1308 flags |= VREAD;
1309 if (user_flags & W_OK)
1310 flags |= VWRITE;
1311 if (user_flags & X_OK)
1312 flags |= VEXEC;
1313#ifdef MAC
1314 error = mac_check_vnode_access(cred, vp, flags);
1315 if (error)
1316 return (error);
1317#endif
1318 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1319 error = VOP_ACCESS(vp, flags, cred, td);
1320 }
1321 return (error);
1322}
1323
1324/*
1325 * Check access permissions using "real" credentials.
1326 */
1327#ifndef _SYS_SYSPROTO_H_
1328struct access_args {
1329 char *path;
1330 int flags;
1331};
1332#endif
1333int
1334access(td, uap)
1335 struct thread *td;
1336 register struct access_args /* {
1337 syscallarg(char *) path;
1338 syscallarg(int) flags;
1339 } */ *uap;
1340{
1341 struct ucred *cred, *tmpcred;
1342 register struct vnode *vp;
1343 int error;
1344 struct nameidata nd;
1345
1346 /*
1347 * Create and modify a temporary credential instead of one that
1348 * is potentially shared. This could also mess up socket
1349 * buffer accounting which can run in an interrupt context.
1350 *
1351 * XXX - Depending on how "threads" are finally implemented, it
1352 * may be better to explicitly pass the credential to namei()
1353 * rather than to modify the potentially shared process structure.
1354 */
1355 cred = td->td_ucred;
1356 tmpcred = crdup(cred);
1357 tmpcred->cr_uid = cred->cr_ruid;
1358 tmpcred->cr_groups[0] = cred->cr_rgid;
1359 td->td_ucred = tmpcred;
1360 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1361 SCARG(uap, path), td);
1362 if ((error = namei(&nd)) != 0)
1363 goto out1;
1364 vp = nd.ni_vp;
1365
1366 error = vn_access(vp, SCARG(uap, flags), tmpcred, td);
1367 NDFREE(&nd, NDF_ONLY_PNBUF);
1368 vput(vp);
1369out1:
1370 td->td_ucred = cred;
1371 crfree(tmpcred);
1372 return (error);
1373}
1374
1375/*
1376 * Check access permissions using "effective" credentials.
1377 */
1378#ifndef _SYS_SYSPROTO_H_
1379struct eaccess_args {
1380 char *path;
1381 int flags;
1382};
1383#endif
1384int
1385eaccess(td, uap)
1386 struct thread *td;
1387 register struct eaccess_args /* {
1388 syscallarg(char *) path;
1389 syscallarg(int) flags;
1390 } */ *uap;
1391{
1392 struct nameidata nd;
1393 struct vnode *vp;
1394 int error;
1395
1396 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1397 SCARG(uap, path), td);
1398 if ((error = namei(&nd)) != 0)
1399 return (error);
1400 vp = nd.ni_vp;
1401
1402 error = vn_access(vp, SCARG(uap, flags), td->td_ucred, td);
1403 NDFREE(&nd, NDF_ONLY_PNBUF);
1404 vput(vp);
1405 return (error);
1406}
1407
1408#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1409/*
1410 * Get file status; this version follows links.
1411 */
1412#ifndef _SYS_SYSPROTO_H_
1413struct ostat_args {
1414 char *path;
1415 struct ostat *ub;
1416};
1417#endif
1418/* ARGSUSED */
1419int
1420ostat(td, uap)
1421 struct thread *td;
1422 register struct ostat_args /* {
1423 syscallarg(char *) path;
1424 syscallarg(struct ostat *) ub;
1425 } */ *uap;
1426{
1427 struct stat sb;
1428 struct ostat osb;
1429 int error;
1430 struct nameidata nd;
1431
1432 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1433 SCARG(uap, path), td);
1434 if ((error = namei(&nd)) != 0)
1435 return (error);
1436 NDFREE(&nd, NDF_ONLY_PNBUF);
1437 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
1438 vput(nd.ni_vp);
1439 if (error)
1440 return (error);
1441 cvtstat(&sb, &osb);
1442 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
1443 return (error);
1444}
1445
1446/*
1447 * Get file status; this version does not follow links.
1448 */
1449#ifndef _SYS_SYSPROTO_H_
1450struct olstat_args {
1451 char *path;
1452 struct ostat *ub;
1453};
1454#endif
1455/* ARGSUSED */
1456int
1457olstat(td, uap)
1458 struct thread *td;
1459 register struct olstat_args /* {
1460 syscallarg(char *) path;
1461 syscallarg(struct ostat *) ub;
1462 } */ *uap;
1463{
1464 struct vnode *vp;
1465 struct stat sb;
1466 struct ostat osb;
1467 int error;
1468 struct nameidata nd;
1469
1470 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1471 SCARG(uap, path), td);
1472 if ((error = namei(&nd)) != 0)
1473 return (error);
1474 vp = nd.ni_vp;
1475 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
1476 NDFREE(&nd, NDF_ONLY_PNBUF);
1477 vput(vp);
1478 if (error)
1479 return (error);
1480 cvtstat(&sb, &osb);
1481 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
1482 return (error);
1483}
1484
1485/*
1486 * Convert from an old to a new stat structure.
1487 */
1488void
1489cvtstat(st, ost)
1490 struct stat *st;
1491 struct ostat *ost;
1492{
1493
1494 ost->st_dev = st->st_dev;
1495 ost->st_ino = st->st_ino;
1496 ost->st_mode = st->st_mode;
1497 ost->st_nlink = st->st_nlink;
1498 ost->st_uid = st->st_uid;
1499 ost->st_gid = st->st_gid;
1500 ost->st_rdev = st->st_rdev;
1501 if (st->st_size < (quad_t)1 << 32)
1502 ost->st_size = st->st_size;
1503 else
1504 ost->st_size = -2;
1505 ost->st_atime = st->st_atime;
1506 ost->st_mtime = st->st_mtime;
1507 ost->st_ctime = st->st_ctime;
1508 ost->st_blksize = st->st_blksize;
1509 ost->st_blocks = st->st_blocks;
1510 ost->st_flags = st->st_flags;
1511 ost->st_gen = st->st_gen;
1512}
1513#endif /* COMPAT_43 || COMPAT_SUNOS */
1514
1515/*
1516 * Get file status; this version follows links.
1517 */
1518#ifndef _SYS_SYSPROTO_H_
1519struct stat_args {
1520 char *path;
1521 struct stat *ub;
1522};
1523#endif
1524/* ARGSUSED */
1525int
1526stat(td, uap)
1527 struct thread *td;
1528 register struct stat_args /* {
1529 syscallarg(char *) path;
1530 syscallarg(struct stat *) ub;
1531 } */ *uap;
1532{
1533 struct stat sb;
1534 int error;
1535 struct nameidata nd;
1536
1537#ifdef LOOKUP_SHARED
1538 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | NOOBJ,
1539 UIO_USERSPACE, SCARG(uap, path), td);
1540#else
1541 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1542 SCARG(uap, path), td);
1543#endif
1544 if ((error = namei(&nd)) != 0)
1545 return (error);
1546 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
1547 NDFREE(&nd, NDF_ONLY_PNBUF);
1548 vput(nd.ni_vp);
1549 if (error)
1550 return (error);
1551 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1552 return (error);
1553}
1554
1555/*
1556 * Get file status; this version does not follow links.
1557 */
1558#ifndef _SYS_SYSPROTO_H_
1559struct lstat_args {
1560 char *path;
1561 struct stat *ub;
1562};
1563#endif
1564/* ARGSUSED */
1565int
1566lstat(td, uap)
1567 struct thread *td;
1568 register struct lstat_args /* {
1569 syscallarg(char *) path;
1570 syscallarg(struct stat *) ub;
1571 } */ *uap;
1572{
1573 int error;
1574 struct vnode *vp;
1575 struct stat sb;
1576 struct nameidata nd;
1577
1578 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1579 SCARG(uap, path), td);
1580 if ((error = namei(&nd)) != 0)
1581 return (error);
1582 vp = nd.ni_vp;
1583 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 vput(vp);
1586 if (error)
1587 return (error);
1588 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1589 return (error);
1590}
1591
1592/*
1593 * Implementation of the NetBSD stat() function.
1594 * XXX This should probably be collapsed with the FreeBSD version,
1595 * as the differences are only due to vn_stat() clearing spares at
1596 * the end of the structures. vn_stat could be split to avoid this,
1597 * and thus collapse the following to close to zero code.
1598 */
1599void
1600cvtnstat(sb, nsb)
1601 struct stat *sb;
1602 struct nstat *nsb;
1603{
1604 bzero(nsb, sizeof *nsb);
1605 nsb->st_dev = sb->st_dev;
1606 nsb->st_ino = sb->st_ino;
1607 nsb->st_mode = sb->st_mode;
1608 nsb->st_nlink = sb->st_nlink;
1609 nsb->st_uid = sb->st_uid;
1610 nsb->st_gid = sb->st_gid;
1611 nsb->st_rdev = sb->st_rdev;
1612 nsb->st_atimespec = sb->st_atimespec;
1613 nsb->st_mtimespec = sb->st_mtimespec;
1614 nsb->st_ctimespec = sb->st_ctimespec;
1615 nsb->st_size = sb->st_size;
1616 nsb->st_blocks = sb->st_blocks;
1617 nsb->st_blksize = sb->st_blksize;
1618 nsb->st_flags = sb->st_flags;
1619 nsb->st_gen = sb->st_gen;
1620 nsb->st_birthtimespec = sb->st_birthtimespec;
1621}
1622
1623#ifndef _SYS_SYSPROTO_H_
1624struct nstat_args {
1625 char *path;
1626 struct nstat *ub;
1627};
1628#endif
1629/* ARGSUSED */
1630int
1631nstat(td, uap)
1632 struct thread *td;
1633 register struct nstat_args /* {
1634 syscallarg(char *) path;
1635 syscallarg(struct nstat *) ub;
1636 } */ *uap;
1637{
1638 struct stat sb;
1639 struct nstat nsb;
1640 int error;
1641 struct nameidata nd;
1642
1643 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1644 SCARG(uap, path), td);
1645 if ((error = namei(&nd)) != 0)
1646 return (error);
1647 NDFREE(&nd, NDF_ONLY_PNBUF);
1648 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
1649 vput(nd.ni_vp);
1650 if (error)
1651 return (error);
1652 cvtnstat(&sb, &nsb);
1653 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1654 return (error);
1655}
1656
1657/*
1658 * NetBSD lstat. Get file status; this version does not follow links.
1659 */
1660#ifndef _SYS_SYSPROTO_H_
1661struct lstat_args {
1662 char *path;
1663 struct stat *ub;
1664};
1665#endif
1666/* ARGSUSED */
1667int
1668nlstat(td, uap)
1669 struct thread *td;
1670 register struct nlstat_args /* {
1671 syscallarg(char *) path;
1672 syscallarg(struct nstat *) ub;
1673 } */ *uap;
1674{
1675 int error;
1676 struct vnode *vp;
1677 struct stat sb;
1678 struct nstat nsb;
1679 struct nameidata nd;
1680
1681 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1682 SCARG(uap, path), td);
1683 if ((error = namei(&nd)) != 0)
1684 return (error);
1685 vp = nd.ni_vp;
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
1688 vput(vp);
1689 if (error)
1690 return (error);
1691 cvtnstat(&sb, &nsb);
1692 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1693 return (error);
1694}
1695
1696/*
1697 * Get configurable pathname variables.
1698 */
1699#ifndef _SYS_SYSPROTO_H_
1700struct pathconf_args {
1701 char *path;
1702 int name;
1703};
1704#endif
1705/* ARGSUSED */
1706int
1707pathconf(td, uap)
1708 struct thread *td;
1709 register struct pathconf_args /* {
1710 syscallarg(char *) path;
1711 syscallarg(int) name;
1712 } */ *uap;
1713{
1714 int error;
1715 struct nameidata nd;
1716
1717 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1718 SCARG(uap, path), td);
1719 if ((error = namei(&nd)) != 0)
1720 return (error);
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), td->td_retval);
1723 vput(nd.ni_vp);
1724 return (error);
1725}
1726
1727/*
1728 * Return target name of a symbolic link.
1729 */
1730#ifndef _SYS_SYSPROTO_H_
1731struct readlink_args {
1732 char *path;
1733 char *buf;
1734 int count;
1735};
1736#endif
1737/* ARGSUSED */
1738int
1739readlink(td, uap)
1740 struct thread *td;
1741 register struct readlink_args /* {
1742 syscallarg(char *) path;
1743 syscallarg(char *) buf;
1744 syscallarg(int) count;
1745 } */ *uap;
1746{
1747 register struct vnode *vp;
1748 struct iovec aiov;
1749 struct uio auio;
1750 int error;
1751 struct nameidata nd;
1752
1753 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1754 SCARG(uap, path), td);
1755 if ((error = namei(&nd)) != 0)
1756 return (error);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 vp = nd.ni_vp;
1759#ifdef MAC
1760 error = mac_check_vnode_readlink(td->td_ucred, vp);
1761 if (error) {
1762 vput(vp);
1763 return (error);
1764 }
1765#endif
1766 if (vp->v_type != VLNK)
1767 error = EINVAL;
1768 else {
1769 aiov.iov_base = SCARG(uap, buf);
1770 aiov.iov_len = SCARG(uap, count);
1771 auio.uio_iov = &aiov;
1772 auio.uio_iovcnt = 1;
1773 auio.uio_offset = 0;
1774 auio.uio_rw = UIO_READ;
1775 auio.uio_segflg = UIO_USERSPACE;
1776 auio.uio_td = td;
1777 auio.uio_resid = SCARG(uap, count);
1778 error = VOP_READLINK(vp, &auio, td->td_ucred);
1779 }
1780 vput(vp);
1781 td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
1782 return (error);
1783}
1784
1785/*
1786 * Common implementation code for chflags() and fchflags().
1787 */
1788static int
1789setfflags(td, vp, flags)
1790 struct thread *td;
1791 struct vnode *vp;
1792 int flags;
1793{
1794 int error;
1795 struct mount *mp;
1796 struct vattr vattr;
1797
1798 /*
1799 * Prevent non-root users from setting flags on devices. When
1800 * a device is reused, users can retain ownership of the device
1801 * if they are allowed to set flags and programs assume that
1802 * chown can't fail when done as root.
1803 */
1804 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1805 error = suser_cred(td->td_ucred, PRISON_ROOT);
1806 if (error)
1807 return (error);
1808 }
1809
1810 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1811 return (error);
1812 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1813 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1814#ifdef MAC
1815 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
1816 if (error == 0) {
1817#endif
1818 VATTR_NULL(&vattr);
1819 vattr.va_flags = flags;
1820 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
1821#ifdef MAC
1822 }
1823#endif
1824 VOP_UNLOCK(vp, 0, td);
1825 vn_finished_write(mp);
1826 return (error);
1827}
1828
1829/*
1830 * Change flags of a file given a path name.
1831 */
1832#ifndef _SYS_SYSPROTO_H_
1833struct chflags_args {
1834 char *path;
1835 int flags;
1836};
1837#endif
1838/* ARGSUSED */
1839int
1840chflags(td, uap)
1841 struct thread *td;
1842 register struct chflags_args /* {
1843 syscallarg(char *) path;
1844 syscallarg(int) flags;
1845 } */ *uap;
1846{
1847 int error;
1848 struct nameidata nd;
1849
1850 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1851 if ((error = namei(&nd)) != 0)
1852 return (error);
1853 NDFREE(&nd, NDF_ONLY_PNBUF);
1854 error = setfflags(td, nd.ni_vp, SCARG(uap, flags));
1855 vrele(nd.ni_vp);
1856 return error;
1857}
1858
1859/*
1860 * Same as chflags() but doesn't follow symlinks.
1861 */
1862int
1863lchflags(td, uap)
1864 struct thread *td;
1865 register struct lchflags_args /* {
1866 syscallarg(char *) path;
1867 syscallarg(int) flags;
1868 } */ *uap;
1869{
1870 int error;
1871 struct nameidata nd;
1872
1873 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1874 if ((error = namei(&nd)) != 0)
1875 return (error);
1876 NDFREE(&nd, NDF_ONLY_PNBUF);
1877 error = setfflags(td, nd.ni_vp, SCARG(uap, flags));
1878 vrele(nd.ni_vp);
1879 return error;
1880}
1881
1882/*
1883 * Change flags of a file given a file descriptor.
1884 */
1885#ifndef _SYS_SYSPROTO_H_
1886struct fchflags_args {
1887 int fd;
1888 int flags;
1889};
1890#endif
1891/* ARGSUSED */
1892int
1893fchflags(td, uap)
1894 struct thread *td;
1895 register struct fchflags_args /* {
1896 syscallarg(int) fd;
1897 syscallarg(int) flags;
1898 } */ *uap;
1899{
1900 struct file *fp;
1901 int error;
1902
1903 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
1904 return (error);
1905 error = setfflags(td, (struct vnode *) fp->f_data, SCARG(uap, flags));
1906 fdrop(fp, td);
1907 return (error);
1908}
1909
1910/*
1911 * Common implementation code for chmod(), lchmod() and fchmod().
1912 */
1913static int
1914setfmode(td, vp, mode)
1915 struct thread *td;
1916 struct vnode *vp;
1917 int mode;
1918{
1919 int error;
1920 struct mount *mp;
1921 struct vattr vattr;
1922
1923 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1924 return (error);
1925 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1926 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1927 VATTR_NULL(&vattr);
1928 vattr.va_mode = mode & ALLPERMS;
1929#ifdef MAC
1930 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
1931 if (error == 0)
1932#endif
1933 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
1934 VOP_UNLOCK(vp, 0, td);
1935 vn_finished_write(mp);
1936 return error;
1937}
1938
1939/*
1940 * Change mode of a file given path name.
1941 */
1942#ifndef _SYS_SYSPROTO_H_
1943struct chmod_args {
1944 char *path;
1945 int mode;
1946};
1947#endif
1948/* ARGSUSED */
1949int
1950chmod(td, uap)
1951 struct thread *td;
1952 register struct chmod_args /* {
1953 syscallarg(char *) path;
1954 syscallarg(int) mode;
1955 } */ *uap;
1956{
1957 int error;
1958 struct nameidata nd;
1959
1960 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1961 if ((error = namei(&nd)) != 0)
1962 return (error);
1963 NDFREE(&nd, NDF_ONLY_PNBUF);
1964 error = setfmode(td, nd.ni_vp, SCARG(uap, mode));
1965 vrele(nd.ni_vp);
1966 return error;
1967}
1968
1969/*
1970 * Change mode of a file given path name (don't follow links.)
1971 */
1972#ifndef _SYS_SYSPROTO_H_
1973struct lchmod_args {
1974 char *path;
1975 int mode;
1976};
1977#endif
1978/* ARGSUSED */
1979int
1980lchmod(td, uap)
1981 struct thread *td;
1982 register struct lchmod_args /* {
1983 syscallarg(char *) path;
1984 syscallarg(int) mode;
1985 } */ *uap;
1986{
1987 int error;
1988 struct nameidata nd;
1989
1990 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1991 if ((error = namei(&nd)) != 0)
1992 return (error);
1993 NDFREE(&nd, NDF_ONLY_PNBUF);
1994 error = setfmode(td, nd.ni_vp, SCARG(uap, mode));
1995 vrele(nd.ni_vp);
1996 return error;
1997}
1998
1999/*
2000 * Change mode of a file given a file descriptor.
2001 */
2002#ifndef _SYS_SYSPROTO_H_
2003struct fchmod_args {
2004 int fd;
2005 int mode;
2006};
2007#endif
2008/* ARGSUSED */
2009int
2010fchmod(td, uap)
2011 struct thread *td;
2012 register struct fchmod_args /* {
2013 syscallarg(int) fd;
2014 syscallarg(int) mode;
2015 } */ *uap;
2016{
2017 struct file *fp;
2018 struct vnode *vp;
2019 int error;
2020
2021 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2022 return (error);
2023 vp = (struct vnode *)fp->f_data;
2024 error = setfmode(td, (struct vnode *)fp->f_data, SCARG(uap, mode));
2025 fdrop(fp, td);
2026 return (error);
2027}
2028
2029/*
2030 * Common implementation for chown(), lchown(), and fchown()
2031 */
2032static int
2033setfown(td, vp, uid, gid)
2034 struct thread *td;
2035 struct vnode *vp;
2036 uid_t uid;
2037 gid_t gid;
2038{
2039 int error;
2040 struct mount *mp;
2041 struct vattr vattr;
2042
2043 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2044 return (error);
2045 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2046 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2047 VATTR_NULL(&vattr);
2048 vattr.va_uid = uid;
2049 vattr.va_gid = gid;
2050#ifdef MAC
2051 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2052 vattr.va_gid);
2053 if (error == 0)
2054#endif
2055 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2056 VOP_UNLOCK(vp, 0, td);
2057 vn_finished_write(mp);
2058 return error;
2059}
2060
2061/*
2062 * Set ownership given a path name.
2063 */
2064#ifndef _SYS_SYSPROTO_H_
2065struct chown_args {
2066 char *path;
2067 int uid;
2068 int gid;
2069};
2070#endif
2071/* ARGSUSED */
2072int
2073chown(td, uap)
2074 struct thread *td;
2075 register struct chown_args /* {
2076 syscallarg(char *) path;
2077 syscallarg(int) uid;
2078 syscallarg(int) gid;
2079 } */ *uap;
2080{
2081 int error;
2082 struct nameidata nd;
2083
2084 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2085 if ((error = namei(&nd)) != 0)
2086 return (error);
2087 NDFREE(&nd, NDF_ONLY_PNBUF);
2088 error = setfown(td, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2089 vrele(nd.ni_vp);
2090 return (error);
2091}
2092
2093/*
2094 * Set ownership given a path name, do not cross symlinks.
2095 */
2096#ifndef _SYS_SYSPROTO_H_
2097struct lchown_args {
2098 char *path;
2099 int uid;
2100 int gid;
2101};
2102#endif
2103/* ARGSUSED */
2104int
2105lchown(td, uap)
2106 struct thread *td;
2107 register struct lchown_args /* {
2108 syscallarg(char *) path;
2109 syscallarg(int) uid;
2110 syscallarg(int) gid;
2111 } */ *uap;
2112{
2113 int error;
2114 struct nameidata nd;
2115
2116 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2117 if ((error = namei(&nd)) != 0)
2118 return (error);
2119 NDFREE(&nd, NDF_ONLY_PNBUF);
2120 error = setfown(td, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2121 vrele(nd.ni_vp);
2122 return (error);
2123}
2124
2125/*
2126 * Set ownership given a file descriptor.
2127 */
2128#ifndef _SYS_SYSPROTO_H_
2129struct fchown_args {
2130 int fd;
2131 int uid;
2132 int gid;
2133};
2134#endif
2135/* ARGSUSED */
2136int
2137fchown(td, uap)
2138 struct thread *td;
2139 register struct fchown_args /* {
2140 syscallarg(int) fd;
2141 syscallarg(int) uid;
2142 syscallarg(int) gid;
2143 } */ *uap;
2144{
2145 struct file *fp;
2146 struct vnode *vp;
2147 int error;
2148
2149 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2150 return (error);
2151 vp = (struct vnode *)fp->f_data;
2152 error = setfown(td, (struct vnode *)fp->f_data,
2153 SCARG(uap, uid), SCARG(uap, gid));
2154 fdrop(fp, td);
2155 return (error);
2156}
2157
2158/*
2159 * Common implementation code for utimes(), lutimes(), and futimes().
2160 */
2161static int
2162getutimes(usrtvp, tsp)
2163 const struct timeval *usrtvp;
2164 struct timespec *tsp;
2165{
2166 struct timeval tv[2];
2167 int error;
2168
2169 if (usrtvp == NULL) {
2170 microtime(&tv[0]);
2171 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2172 tsp[1] = tsp[0];
2173 } else {
2174 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2175 return (error);
2176 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2177 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2178 }
2179 return 0;
2180}
2181
2182/*
2183 * Common implementation code for utimes(), lutimes(), and futimes().
2184 */
2185static int
2186setutimes(td, vp, ts, numtimes, nullflag)
2187 struct thread *td;
2188 struct vnode *vp;
2189 const struct timespec *ts;
2190 int numtimes;
2191 int nullflag;
2192{
2193 int error, setbirthtime;
2194 struct mount *mp;
2195 struct vattr vattr;
2196
2197 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2198 return (error);
2199 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2200 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2201 setbirthtime = 0;
2202 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2203 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2204 setbirthtime = 1;
2205 VATTR_NULL(&vattr);
2206 vattr.va_atime = ts[0];
2207 vattr.va_mtime = ts[1];
2208 if (setbirthtime)
2209 vattr.va_birthtime = ts[1];
2210 if (numtimes > 2)
2211 vattr.va_birthtime = ts[2];
2212 if (nullflag)
2213 vattr.va_vaflags |= VA_UTIMES_NULL;
2214#ifdef MAC
2215 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2216 vattr.va_mtime);
2217 if (error == 0)
2218#endif
2219 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2220 VOP_UNLOCK(vp, 0, td);
2221 vn_finished_write(mp);
2222 return error;
2223}
2224
2225/*
2226 * Set the access and modification times of a file.
2227 */
2228#ifndef _SYS_SYSPROTO_H_
2229struct utimes_args {
2230 char *path;
2231 struct timeval *tptr;
2232};
2233#endif
2234/* ARGSUSED */
2235int
2236utimes(td, uap)
2237 struct thread *td;
2238 register struct utimes_args /* {
2239 syscallarg(char *) path;
2240 syscallarg(struct timeval *) tptr;
2241 } */ *uap;
2242{
2243 struct timespec ts[2];
2244 struct timeval *usrtvp;
2245 int error;
2246 struct nameidata nd;
2247
2248 usrtvp = SCARG(uap, tptr);
2249 if ((error = getutimes(usrtvp, ts)) != 0)
2250 return (error);
2251 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2252 if ((error = namei(&nd)) != 0)
2253 return (error);
2254 NDFREE(&nd, NDF_ONLY_PNBUF);
2255 error = setutimes(td, nd.ni_vp, ts, 2, usrtvp == NULL);
2256 vrele(nd.ni_vp);
2257 return (error);
2258}
2259
2260/*
2261 * Set the access and modification times of a file.
2262 */
2263#ifndef _SYS_SYSPROTO_H_
2264struct lutimes_args {
2265 char *path;
2266 struct timeval *tptr;
2267};
2268#endif
2269/* ARGSUSED */
2270int
2271lutimes(td, uap)
2272 struct thread *td;
2273 register struct lutimes_args /* {
2274 syscallarg(char *) path;
2275 syscallarg(struct timeval *) tptr;
2276 } */ *uap;
2277{
2278 struct timespec ts[2];
2279 struct timeval *usrtvp;
2280 int error;
2281 struct nameidata nd;
2282
2283 usrtvp = SCARG(uap, tptr);
2284 if ((error = getutimes(usrtvp, ts)) != 0)
2285 return (error);
2286 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2287 if ((error = namei(&nd)) != 0)
2288 return (error);
2289 NDFREE(&nd, NDF_ONLY_PNBUF);
2290 error = setutimes(td, nd.ni_vp, ts, 2, usrtvp == NULL);
2291 vrele(nd.ni_vp);
2292 return (error);
2293}
2294
2295/*
2296 * Set the access and modification times of a file.
2297 */
2298#ifndef _SYS_SYSPROTO_H_
2299struct futimes_args {
2300 int fd;
2301 struct timeval *tptr;
2302};
2303#endif
2304/* ARGSUSED */
2305int
2306futimes(td, uap)
2307 struct thread *td;
2308 register struct futimes_args /* {
2309 syscallarg(int ) fd;
2310 syscallarg(struct timeval *) tptr;
2311 } */ *uap;
2312{
2313 struct timespec ts[2];
2314 struct file *fp;
2315 struct timeval *usrtvp;
2316 int error;
2317
2318 usrtvp = SCARG(uap, tptr);
2319 if ((error = getutimes(usrtvp, ts)) != 0)
2320 return (error);
2321 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2322 return (error);
2323 error = setutimes(td, (struct vnode *)fp->f_data, ts, 2, usrtvp==NULL);
2324 fdrop(fp, td);
2325 return (error);
2326}
2327
2328/*
2329 * Truncate a file given its path name.
2330 */
2331#ifndef _SYS_SYSPROTO_H_
2332struct truncate_args {
2333 char *path;
2334 int pad;
2335 off_t length;
2336};
2337#endif
2338/* ARGSUSED */
2339int
2340truncate(td, uap)
2341 struct thread *td;
2342 register struct truncate_args /* {
2343 syscallarg(char *) path;
2344 syscallarg(int) pad;
2345 syscallarg(off_t) length;
2346 } */ *uap;
2347{
2348 struct mount *mp;
2349 struct vnode *vp;
2350 struct vattr vattr;
2351 int error;
2352 struct nameidata nd;
2353
2354 if (uap->length < 0)
2355 return(EINVAL);
2356 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2357 if ((error = namei(&nd)) != 0)
2358 return (error);
2359 vp = nd.ni_vp;
2360 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2361 vrele(vp);
2362 return (error);
2363 }
2364 NDFREE(&nd, NDF_ONLY_PNBUF);
2365 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2367 if (vp->v_type == VDIR)
2368 error = EISDIR;
2369#ifdef MAC
2370 else if ((error = mac_check_vnode_write(td->td_ucred, vp))) {}
2371#endif
2372 else if ((error = vn_writechk(vp)) == 0 &&
2373 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
2374 VATTR_NULL(&vattr);
2375 vattr.va_size = SCARG(uap, length);
2376 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2377 }
2378 vput(vp);
2379 vn_finished_write(mp);
2380 return (error);
2381}
2382
2383/*
2384 * Truncate a file given a file descriptor.
2385 */
2386#ifndef _SYS_SYSPROTO_H_
2387struct ftruncate_args {
2388 int fd;
2389 int pad;
2390 off_t length;
2391};
2392#endif
2393/* ARGSUSED */
2394int
2395ftruncate(td, uap)
2396 struct thread *td;
2397 register struct ftruncate_args /* {
2398 syscallarg(int) fd;
2399 syscallarg(int) pad;
2400 syscallarg(off_t) length;
2401 } */ *uap;
2402{
2403 struct mount *mp;
2404 struct vattr vattr;
2405 struct vnode *vp;
2406 struct file *fp;
2407 int error;
2408
2409 if (uap->length < 0)
2410 return(EINVAL);
2411 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2412 return (error);
2413 if ((fp->f_flag & FWRITE) == 0) {
2414 fdrop(fp, td);
2415 return (EINVAL);
2416 }
2417 vp = (struct vnode *)fp->f_data;
2418 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2419 fdrop(fp, td);
2420 return (error);
2421 }
2422 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2423 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2424 if (vp->v_type == VDIR)
2425 error = EISDIR;
2426#ifdef MAC
2427 else if ((error = mac_check_vnode_write(td->td_ucred, vp))) {}
2428#endif
2429 else if ((error = vn_writechk(vp)) == 0) {
2430 VATTR_NULL(&vattr);
2431 vattr.va_size = SCARG(uap, length);
2432 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2433 }
2434 VOP_UNLOCK(vp, 0, td);
2435 vn_finished_write(mp);
2436 fdrop(fp, td);
2437 return (error);
2438}
2439
2440#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2441/*
2442 * Truncate a file given its path name.
2443 */
2444#ifndef _SYS_SYSPROTO_H_
2445struct otruncate_args {
2446 char *path;
2447 long length;
2448};
2449#endif
2450/* ARGSUSED */
2451int
2452otruncate(td, uap)
2453 struct thread *td;
2454 register struct otruncate_args /* {
2455 syscallarg(char *) path;
2456 syscallarg(long) length;
2457 } */ *uap;
2458{
2459 struct truncate_args /* {
2460 syscallarg(char *) path;
2461 syscallarg(int) pad;
2462 syscallarg(off_t) length;
2463 } */ nuap;
2464
2465 SCARG(&nuap, path) = SCARG(uap, path);
2466 SCARG(&nuap, length) = SCARG(uap, length);
2467 return (truncate(td, &nuap));
2468}
2469
2470/*
2471 * Truncate a file given a file descriptor.
2472 */
2473#ifndef _SYS_SYSPROTO_H_
2474struct oftruncate_args {
2475 int fd;
2476 long length;
2477};
2478#endif
2479/* ARGSUSED */
2480int
2481oftruncate(td, uap)
2482 struct thread *td;
2483 register struct oftruncate_args /* {
2484 syscallarg(int) fd;
2485 syscallarg(long) length;
2486 } */ *uap;
2487{
2488 struct ftruncate_args /* {
2489 syscallarg(int) fd;
2490 syscallarg(int) pad;
2491 syscallarg(off_t) length;
2492 } */ nuap;
2493
2494 SCARG(&nuap, fd) = SCARG(uap, fd);
2495 SCARG(&nuap, length) = SCARG(uap, length);
2496 return (ftruncate(td, &nuap));
2497}
2498#endif /* COMPAT_43 || COMPAT_SUNOS */
2499
2500/*
2501 * Sync an open file.
2502 */
2503#ifndef _SYS_SYSPROTO_H_
2504struct fsync_args {
2505 int fd;
2506};
2507#endif
2508/* ARGSUSED */
2509int
2510fsync(td, uap)
2511 struct thread *td;
2512 struct fsync_args /* {
2513 syscallarg(int) fd;
2514 } */ *uap;
2515{
2516 struct vnode *vp;
2517 struct mount *mp;
2518 struct file *fp;
2519 vm_object_t obj;
2520 int error;
2521
2522 GIANT_REQUIRED;
2523
2524 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2525 return (error);
2526 vp = (struct vnode *)fp->f_data;
2527 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2528 fdrop(fp, td);
2529 return (error);
2530 }
2531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2532 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2533 vm_object_page_clean(obj, 0, 0, 0);
2534 }
2535 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, td);
2536 if (error == 0 && vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP)
2537 && softdep_fsync_hook != NULL)
2538 error = (*softdep_fsync_hook)(vp);
2539
2540 VOP_UNLOCK(vp, 0, td);
2541 vn_finished_write(mp);
2542 fdrop(fp, td);
2543 return (error);
2544}
2545
2546/*
2547 * Rename files. Source and destination must either both be directories,
2548 * or both not be directories. If target is a directory, it must be empty.
2549 */
2550#ifndef _SYS_SYSPROTO_H_
2551struct rename_args {
2552 char *from;
2553 char *to;
2554};
2555#endif
2556/* ARGSUSED */
2557int
2558rename(td, uap)
2559 struct thread *td;
2560 register struct rename_args /* {
2561 syscallarg(char *) from;
2562 syscallarg(char *) to;
2563 } */ *uap;
2564{
2565 struct mount *mp;
2566 struct vnode *tvp, *fvp, *tdvp;
2567 struct nameidata fromnd, tond;
2568 int error;
2569
2570 bwillwrite();
2571 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2572 SCARG(uap, from), td);
2573 if ((error = namei(&fromnd)) != 0)
2574 return (error);
2575 fvp = fromnd.ni_vp;
2576 if ((error = vn_start_write(fvp, &mp, V_WAIT | PCATCH)) != 0) {
2577 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2578 vrele(fromnd.ni_dvp);
2579 vrele(fvp);
2580 goto out1;
2581 }
2582 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2583 UIO_USERSPACE, SCARG(uap, to), td);
2584 if (fromnd.ni_vp->v_type == VDIR)
2585 tond.ni_cnd.cn_flags |= WILLBEDIR;
2586 if ((error = namei(&tond)) != 0) {
2587 /* Translate error code for rename("dir1", "dir2/."). */
2588 if (error == EISDIR && fvp->v_type == VDIR)
2589 error = EINVAL;
2590 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2591 vrele(fromnd.ni_dvp);
2592 vrele(fvp);
2593 goto out1;
2594 }
2595 tdvp = tond.ni_dvp;
2596 tvp = tond.ni_vp;
2597 if (tvp != NULL) {
2598 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2599 error = ENOTDIR;
2600 goto out;
2601 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2602 error = EISDIR;
2603 goto out;
2604 }
2605 }
2606 if (fvp == tdvp)
2607 error = EINVAL;
2608 /*
2609 * If source is the same as the destination (that is the
2610 * same inode number with the same name in the same directory),
2611 * then there is nothing to do.
2612 */
2613 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2614 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2615 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2616 fromnd.ni_cnd.cn_namelen))
2617 error = -1;
2618out:
2619 if (!error) {
2620 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
2621 if (fromnd.ni_dvp != tdvp) {
2622 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2623 }
2624 if (tvp) {
2625 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
2626 }
2627 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2628 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2629 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2630 NDFREE(&tond, NDF_ONLY_PNBUF);
2631 } else {
2632 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2633 NDFREE(&tond, NDF_ONLY_PNBUF);
2634 if (tdvp == tvp)
2635 vrele(tdvp);
2636 else
2637 vput(tdvp);
2638 if (tvp)
2639 vput(tvp);
2640 vrele(fromnd.ni_dvp);
2641 vrele(fvp);
2642 }
2643 vrele(tond.ni_startdir);
2644 vn_finished_write(mp);
2645 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2646 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2647 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2648 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2649out1:
2650 if (fromnd.ni_startdir)
2651 vrele(fromnd.ni_startdir);
2652 if (error == -1)
2653 return (0);
2654 return (error);
2655}
2656
2657/*
2658 * Make a directory file.
2659 */
2660#ifndef _SYS_SYSPROTO_H_
2661struct mkdir_args {
2662 char *path;
2663 int mode;
2664};
2665#endif
2666/* ARGSUSED */
2667int
2668mkdir(td, uap)
2669 struct thread *td;
2670 register struct mkdir_args /* {
2671 syscallarg(char *) path;
2672 syscallarg(int) mode;
2673 } */ *uap;
2674{
2675
2676 return vn_mkdir(uap->path, uap->mode, UIO_USERSPACE, td);
2677}
2678
2679int
2680vn_mkdir(path, mode, segflg, td)
2681 char *path;
2682 int mode;
2683 enum uio_seg segflg;
2684 struct thread *td;
2685{
2686 struct mount *mp;
2687 struct vnode *vp;
2688 struct vattr vattr;
2689 int error;
2690 struct nameidata nd;
2691
2692restart:
2693 bwillwrite();
2694 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME, segflg, path, td);
2695 nd.ni_cnd.cn_flags |= WILLBEDIR;
2696 if ((error = namei(&nd)) != 0)
2697 return (error);
2698 vp = nd.ni_vp;
2699 if (vp != NULL) {
2700 NDFREE(&nd, NDF_ONLY_PNBUF);
2701 vrele(vp);
2702 /*
2703 * XXX namei called with LOCKPARENT but not LOCKLEAF has
2704 * the strange behaviour of leaving the vnode unlocked
2705 * if the target is the same vnode as the parent.
2706 */
2707 if (vp == nd.ni_dvp)
2708 vrele(nd.ni_dvp);
2709 else
2710 vput(nd.ni_dvp);
2711 return (EEXIST);
2712 }
2713 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2714 NDFREE(&nd, NDF_ONLY_PNBUF);
2715 vput(nd.ni_dvp);
2716 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2717 return (error);
2718 goto restart;
2719 }
2720 VATTR_NULL(&vattr);
2721 vattr.va_type = VDIR;
2722 FILEDESC_LOCK(td->td_proc->p_fd);
2723 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
2724 FILEDESC_UNLOCK(td->td_proc->p_fd);
2725 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2726 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2727 NDFREE(&nd, NDF_ONLY_PNBUF);
2728 vput(nd.ni_dvp);
2729 if (!error)
2730 vput(nd.ni_vp);
2731 vn_finished_write(mp);
2732 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2733 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2734 return (error);
2735}
2736
2737/*
2738 * Remove a directory file.
2739 */
2740#ifndef _SYS_SYSPROTO_H_
2741struct rmdir_args {
2742 char *path;
2743};
2744#endif
2745/* ARGSUSED */
2746int
2747rmdir(td, uap)
2748 struct thread *td;
2749 struct rmdir_args /* {
2750 syscallarg(char *) path;
2751 } */ *uap;
2752{
2753 struct mount *mp;
2754 struct vnode *vp;
2755 int error;
2756 struct nameidata nd;
2757
2758restart:
2759 bwillwrite();
2760 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2761 SCARG(uap, path), td);
2762 if ((error = namei(&nd)) != 0)
2763 return (error);
2764 vp = nd.ni_vp;
2765 if (vp->v_type != VDIR) {
2766 error = ENOTDIR;
2767 goto out;
2768 }
2769 /*
2770 * No rmdir "." please.
2771 */
2772 if (nd.ni_dvp == vp) {
2773 error = EINVAL;
2774 goto out;
2775 }
2776 /*
2777 * The root of a mounted filesystem cannot be deleted.
2778 */
2779 if (vp->v_vflag & VV_ROOT) {
2780 error = EBUSY;
2781 goto out;
2782 }
2783 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2784 NDFREE(&nd, NDF_ONLY_PNBUF);
2785 if (nd.ni_dvp == vp)
2786 vrele(nd.ni_dvp);
2787 else
2788 vput(nd.ni_dvp);
2789 vput(vp);
2790 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2791 return (error);
2792 goto restart;
2793 }
2794 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
2795 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2796 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2797 vn_finished_write(mp);
2798out:
2799 NDFREE(&nd, NDF_ONLY_PNBUF);
2800 if (nd.ni_dvp == vp)
2801 vrele(nd.ni_dvp);
2802 else
2803 vput(nd.ni_dvp);
2804 vput(vp);
2805 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2806 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2807 return (error);
2808}
2809
2810#ifdef COMPAT_43
2811/*
2812 * Read a block of directory entries in a filesystem independent format.
2813 */
2814#ifndef _SYS_SYSPROTO_H_
2815struct ogetdirentries_args {
2816 int fd;
2817 char *buf;
2818 u_int count;
2819 long *basep;
2820};
2821#endif
2822int
2823ogetdirentries(td, uap)
2824 struct thread *td;
2825 register struct ogetdirentries_args /* {
2826 syscallarg(int) fd;
2827 syscallarg(char *) buf;
2828 syscallarg(u_int) count;
2829 syscallarg(long *) basep;
2830 } */ *uap;
2831{
2832 struct vnode *vp;
2833 struct file *fp;
2834 struct uio auio, kuio;
2835 struct iovec aiov, kiov;
2836 struct dirent *dp, *edp;
2837 caddr_t dirbuf;
2838 int error, eofflag, readcnt;
2839 long loff;
2840
2841 /* XXX arbitrary sanity limit on `count'. */
2842 if (SCARG(uap, count) > 64 * 1024)
2843 return (EINVAL);
2844 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2845 return (error);
2846 if ((fp->f_flag & FREAD) == 0) {
2847 fdrop(fp, td);
2848 return (EBADF);
2849 }
2850 vp = (struct vnode *)fp->f_data;
2851unionread:
2852 if (vp->v_type != VDIR) {
2853 fdrop(fp, td);
2854 return (EINVAL);
2855 }
2856 aiov.iov_base = SCARG(uap, buf);
2857 aiov.iov_len = SCARG(uap, count);
2858 auio.uio_iov = &aiov;
2859 auio.uio_iovcnt = 1;
2860 auio.uio_rw = UIO_READ;
2861 auio.uio_segflg = UIO_USERSPACE;
2862 auio.uio_td = td;
2863 auio.uio_resid = SCARG(uap, count);
2864 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2865 loff = auio.uio_offset = fp->f_offset;
2866#ifdef MAC
2867 error = mac_check_vnode_readdir(td->td_ucred, vp);
2868 if (error) {
2869 VOP_UNLOCK(vp, 0, td);
2870 fdrop(fp, td);
2871 return (error);
2872 }
2873#endif
2874# if (BYTE_ORDER != LITTLE_ENDIAN)
2875 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2876 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2877 NULL, NULL);
2878 fp->f_offset = auio.uio_offset;
2879 } else
2880# endif
2881 {
2882 kuio = auio;
2883 kuio.uio_iov = &kiov;
2884 kuio.uio_segflg = UIO_SYSSPACE;
2885 kiov.iov_len = SCARG(uap, count);
2886 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2887 kiov.iov_base = dirbuf;
2888 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2889 NULL, NULL);
2890 fp->f_offset = kuio.uio_offset;
2891 if (error == 0) {
2892 readcnt = SCARG(uap, count) - kuio.uio_resid;
2893 edp = (struct dirent *)&dirbuf[readcnt];
2894 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2895# if (BYTE_ORDER == LITTLE_ENDIAN)
2896 /*
2897 * The expected low byte of
2898 * dp->d_namlen is our dp->d_type.
2899 * The high MBZ byte of dp->d_namlen
2900 * is our dp->d_namlen.
2901 */
2902 dp->d_type = dp->d_namlen;
2903 dp->d_namlen = 0;
2904# else
2905 /*
2906 * The dp->d_type is the high byte
2907 * of the expected dp->d_namlen,
2908 * so must be zero'ed.
2909 */
2910 dp->d_type = 0;
2911# endif
2912 if (dp->d_reclen > 0) {
2913 dp = (struct dirent *)
2914 ((char *)dp + dp->d_reclen);
2915 } else {
2916 error = EIO;
2917 break;
2918 }
2919 }
2920 if (dp >= edp)
2921 error = uiomove(dirbuf, readcnt, &auio);
2922 }
2923 FREE(dirbuf, M_TEMP);
2924 }
2925 VOP_UNLOCK(vp, 0, td);
2926 if (error) {
2927 fdrop(fp, td);
2928 return (error);
2929 }
2930 if (SCARG(uap, count) == auio.uio_resid) {
2931 if (union_dircheckp) {
2932 error = union_dircheckp(td, &vp, fp);
2933 if (error == -1)
2934 goto unionread;
2935 if (error) {
2936 fdrop(fp, td);
2937 return (error);
2938 }
2939 }
2940 mp_fixme("Accessing vflags w/o vn lock.");
2941 if ((vp->v_vflag & VV_ROOT) &&
2942 (vp->v_mount->mnt_flag & MNT_UNION)) {
2943 struct vnode *tvp = vp;
2944 vp = vp->v_mount->mnt_vnodecovered;
2945 VREF(vp);
2946 fp->f_data = vp;
2947 fp->f_offset = 0;
2948 vrele(tvp);
2949 goto unionread;
2950 }
2951 }
2952 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
2953 fdrop(fp, td);
2954 td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
2955 return (error);
2956}
2957#endif /* COMPAT_43 */
2958
2959/*
2960 * Read a block of directory entries in a filesystem independent format.
2961 */
2962#ifndef _SYS_SYSPROTO_H_
2963struct getdirentries_args {
2964 int fd;
2965 char *buf;
2966 u_int count;
2967 long *basep;
2968};
2969#endif
2970int
2971getdirentries(td, uap)
2972 struct thread *td;
2973 register struct getdirentries_args /* {
2974 syscallarg(int) fd;
2975 syscallarg(char *) buf;
2976 syscallarg(u_int) count;
2977 syscallarg(long *) basep;
2978 } */ *uap;
2979{
2980 struct vnode *vp;
2981 struct file *fp;
2982 struct uio auio;
2983 struct iovec aiov;
2984 long loff;
2985 int error, eofflag;
2986
2987 if ((error = getvnode(td->td_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2988 return (error);
2989 if ((fp->f_flag & FREAD) == 0) {
2990 fdrop(fp, td);
2991 return (EBADF);
2992 }
2993 vp = (struct vnode *)fp->f_data;
2994unionread:
2995 if (vp->v_type != VDIR) {
2996 fdrop(fp, td);
2997 return (EINVAL);
2998 }
2999 aiov.iov_base = SCARG(uap, buf);
3000 aiov.iov_len = SCARG(uap, count);
3001 auio.uio_iov = &aiov;
3002 auio.uio_iovcnt = 1;
3003 auio.uio_rw = UIO_READ;
3004 auio.uio_segflg = UIO_USERSPACE;
3005 auio.uio_td = td;
3006 auio.uio_resid = SCARG(uap, count);
3007 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3008 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3009 loff = auio.uio_offset = fp->f_offset;
3010#ifdef MAC
3011 error = mac_check_vnode_readdir(td->td_ucred, vp);
3012 if (error == 0)
3013#endif
3014 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3015 NULL);
3016 fp->f_offset = auio.uio_offset;
3017 VOP_UNLOCK(vp, 0, td);
3018 if (error) {
3019 fdrop(fp, td);
3020 return (error);
3021 }
3022 if (SCARG(uap, count) == auio.uio_resid) {
3023 if (union_dircheckp) {
3024 error = union_dircheckp(td, &vp, fp);
3025 if (error == -1)
3026 goto unionread;
3027 if (error) {
3028 fdrop(fp, td);
3029 return (error);
3030 }
3031 }
3032 mp_fixme("Accessing vflag without vn lock.");
3033 if ((vp->v_vflag & VV_ROOT) &&
3034 (vp->v_mount->mnt_flag & MNT_UNION)) {
3035 struct vnode *tvp = vp;
3036 vp = vp->v_mount->mnt_vnodecovered;
3037 VREF(vp);
3038 fp->f_data = vp;
3039 fp->f_offset = 0;
3040 vrele(tvp);
3041 goto unionread;
3042 }
3043 }
3044 if (SCARG(uap, basep) != NULL) {
3045 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
3046 }
3047 td->td_retval[0] = SCARG(uap, count) - auio.uio_resid;
3048 fdrop(fp, td);
3049 return (error);
3050}
3051#ifndef _SYS_SYSPROTO_H_
3052struct getdents_args {
3053 int fd;
3054 char *buf;
3055 size_t count;
3056};
3057#endif
3058int
3059getdents(td, uap)
3060 struct thread *td;
3061 register struct getdents_args /* {
3062 syscallarg(int) fd;
3063 syscallarg(char *) buf;
3064 syscallarg(u_int) count;
3065 } */ *uap;
3066{
3067 struct getdirentries_args ap;
3068 ap.fd = uap->fd;
3069 ap.buf = uap->buf;
3070 ap.count = uap->count;
3071 ap.basep = NULL;
3072 return getdirentries(td, &ap);
3073}
3074
3075/*
3076 * Set the mode mask for creation of filesystem nodes.
3077 *
3078 * MP SAFE
3079 */
3080#ifndef _SYS_SYSPROTO_H_
3081struct umask_args {
3082 int newmask;
3083};
3084#endif
3085int
3086umask(td, uap)
3087 struct thread *td;
3088 struct umask_args /* {
3089 syscallarg(int) newmask;
3090 } */ *uap;
3091{
3092 register struct filedesc *fdp;
3093
3094 FILEDESC_LOCK(td->td_proc->p_fd);
3095 fdp = td->td_proc->p_fd;
3096 td->td_retval[0] = fdp->fd_cmask;
3097 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
3098 FILEDESC_UNLOCK(td->td_proc->p_fd);
3099 return (0);
3100}
3101
3102/*
3103 * Void all references to file by ripping underlying filesystem
3104 * away from vnode.
3105 */
3106#ifndef _SYS_SYSPROTO_H_
3107struct revoke_args {
3108 char *path;
3109};
3110#endif
3111/* ARGSUSED */
3112int
3113revoke(td, uap)
3114 struct thread *td;
3115 register struct revoke_args /* {
3116 syscallarg(char *) path;
3117 } */ *uap;
3118{
3119 struct mount *mp;
3120 struct vnode *vp;
3121 struct vattr vattr;
3122 int error;
3123 struct nameidata nd;
3124
3125 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, SCARG(uap, path),
3126 td);
3127 if ((error = namei(&nd)) != 0)
3128 return (error);
3129 vp = nd.ni_vp;
3130 NDFREE(&nd, NDF_ONLY_PNBUF);
3131 if (vp->v_type != VCHR) {
3132 vput(vp);
3133 return (EINVAL);
3134 }
3135#ifdef MAC
3136 error = mac_check_vnode_revoke(td->td_ucred, vp);
3137 if (error) {
3138 vput(vp);
3139 return (error);
3140 }
3141#endif
3142 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3143 if (error) {
3144 vput(vp);
3145 return (error);
3146 }
3147 VOP_UNLOCK(vp, 0, td);
3148 if (td->td_ucred->cr_uid != vattr.va_uid) {
3149 error = suser_cred(td->td_ucred, PRISON_ROOT);
3150 if (error)
3151 goto out;
3152 }
3153 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3154 goto out;
3155 if (vcount(vp) > 1)
3156 VOP_REVOKE(vp, REVOKEALL);
3157 vn_finished_write(mp);
3158out:
3159 vrele(vp);
3160 return (error);
3161}
3162
3163/*
3164 * Convert a user file descriptor to a kernel file entry.
3165 * The file entry is locked upon returning.
3166 */
3167int
3168getvnode(fdp, fd, fpp)
3169 struct filedesc *fdp;
3170 int fd;
3171 struct file **fpp;
3172{
3173 int error;
3174 struct file *fp;
3175
3176 fp = NULL;
3177 if (fdp == NULL)
3178 error = EBADF;
3179 else {
3180 FILEDESC_LOCK(fdp);
3181 if ((u_int)fd >= fdp->fd_nfiles ||
3182 (fp = fdp->fd_ofiles[fd]) == NULL)
3183 error = EBADF;
3184 else if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
3185 fp = NULL;
3186 error = EINVAL;
3187 } else {
3188 fhold(fp);
3189 error = 0;
3190 }
3191 FILEDESC_UNLOCK(fdp);
3192 }
3193 *fpp = fp;
3194 return (error);
3195}
3196/*
3197 * Get (NFS) file handle
3198 */
3199#ifndef _SYS_SYSPROTO_H_
3200struct getfh_args {
3201 char *fname;
3202 fhandle_t *fhp;
3203};
3204#endif
3205int
3206getfh(td, uap)
3207 struct thread *td;
3208 register struct getfh_args *uap;
3209{
3210 struct nameidata nd;
3211 fhandle_t fh;
3212 register struct vnode *vp;
3213 int error;
3214
3215 /*
3216 * Must be super user
3217 */
3218 error = suser(td);
3219 if (error)
3220 return (error);
3221 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
3222 error = namei(&nd);
3223 if (error)
3224 return (error);
3225 NDFREE(&nd, NDF_ONLY_PNBUF);
3226 vp = nd.ni_vp;
3227 bzero(&fh, sizeof(fh));
3228 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3229 error = VFS_VPTOFH(vp, &fh.fh_fid);
3230 vput(vp);
3231 if (error)
3232 return (error);
3233 error = copyout(&fh, uap->fhp, sizeof (fh));
3234 return (error);
3235}
3236
3237/*
3238 * syscall for the rpc.lockd to use to translate a NFS file handle into
3239 * an open descriptor.
3240 *
3241 * warning: do not remove the suser() call or this becomes one giant
3242 * security hole.
3243 */
3244#ifndef _SYS_SYSPROTO_H_
3245struct fhopen_args {
3246 const struct fhandle *u_fhp;
3247 int flags;
3248};
3249#endif
3250int
3251fhopen(td, uap)
3252 struct thread *td;
3253 struct fhopen_args /* {
3254 syscallarg(const struct fhandle *) u_fhp;
3255 syscallarg(int) flags;
3256 } */ *uap;
3257{
3258 struct proc *p = td->td_proc;
3259 struct mount *mp;
3260 struct vnode *vp;
3261 struct fhandle fhp;
3262 struct vattr vat;
3263 struct vattr *vap = &vat;
3264 struct flock lf;
3265 struct file *fp;
3266 register struct filedesc *fdp = p->p_fd;
3267 int fmode, mode, error, type;
3268 struct file *nfp;
3269 int indx;
3270
3271 /*
3272 * Must be super user
3273 */
3274 error = suser(td);
3275 if (error)
3276 return (error);
3277
3278 fmode = FFLAGS(SCARG(uap, flags));
3279 /* why not allow a non-read/write open for our lockd? */
3280 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3281 return (EINVAL);
3282 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3283 if (error)
3284 return(error);
3285 /* find the mount point */
3286 mp = vfs_getvfs(&fhp.fh_fsid);
3287 if (mp == NULL)
3288 return (ESTALE);
3289 /* now give me my vnode, it gets returned to me locked */
3290 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3291 if (error)
3292 return (error);
3293 /*
3294 * from now on we have to make sure not
3295 * to forget about the vnode
3296 * any error that causes an abort must vput(vp)
3297 * just set error = err and 'goto bad;'.
3298 */
3299
3300 /*
3301 * from vn_open
3302 */
3303 if (vp->v_type == VLNK) {
3304 error = EMLINK;
3305 goto bad;
3306 }
3307 if (vp->v_type == VSOCK) {
3308 error = EOPNOTSUPP;
3309 goto bad;
3310 }
3311 mode = 0;
3312 if (fmode & (FWRITE | O_TRUNC)) {
3313 if (vp->v_type == VDIR) {
3314 error = EISDIR;
3315 goto bad;
3316 }
3317 error = vn_writechk(vp);
3318 if (error)
3319 goto bad;
3320 mode |= VWRITE;
3321 }
3322 if (fmode & FREAD)
3323 mode |= VREAD;
3324 if (fmode & O_APPEND)
3325 mode |= VAPPEND;
3326#ifdef MAC
3327 error = mac_check_vnode_open(td->td_ucred, vp, mode);
3328 if (error)
3329 goto bad;
3330#endif
3331 if (mode) {
3332 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
3333 if (error)
3334 goto bad;
3335 }
3336 if (fmode & O_TRUNC) {
3337 VOP_UNLOCK(vp, 0, td); /* XXX */
3338 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
3339 vrele(vp);
3340 return (error);
3341 }
3342 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3344#ifdef MAC
3345 error = mac_check_vnode_write(td->td_ucred, vp);
3346 if (error == 0) {
3347#endif
3348 VATTR_NULL(vap);
3349 vap->va_size = 0;
3350 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
3351#ifdef MAC
3352 }
3353#endif
3354 vn_finished_write(mp);
3355 if (error)
3356 goto bad;
3357 }
3358 error = VOP_OPEN(vp, fmode, td->td_ucred, td);
3359 if (error)
3360 goto bad;
3361 /*
3362 * Make sure that a VM object is created for VMIO support.
3363 */
3364 if (vn_canvmio(vp) == TRUE) {
3365 if ((error = vfs_object_create(vp, td, td->td_ucred)) != 0)
3366 goto bad;
3367 }
3368 if (fmode & FWRITE)
3369 vp->v_writecount++;
3370
3371 /*
3372 * end of vn_open code
3373 */
3374
3375 if ((error = falloc(td, &nfp, &indx)) != 0) {
3376 if (fmode & FWRITE)
3377 vp->v_writecount--;
3378 goto bad;
3379 }
3380 fp = nfp;
3381
3382 /*
3383 * Hold an extra reference to avoid having fp ripped out
3384 * from under us while we block in the lock op
3385 */
3386 fhold(fp);
3387 nfp->f_data = vp;
3388 nfp->f_flag = fmode & FMASK;
3389 nfp->f_ops = &vnops;
3390 nfp->f_type = DTYPE_VNODE;
3391 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3392 lf.l_whence = SEEK_SET;
3393 lf.l_start = 0;
3394 lf.l_len = 0;
3395 if (fmode & O_EXLOCK)
3396 lf.l_type = F_WRLCK;
3397 else
3398 lf.l_type = F_RDLCK;
3399 type = F_FLOCK;
3400 if ((fmode & FNONBLOCK) == 0)
3401 type |= F_WAIT;
3402 VOP_UNLOCK(vp, 0, td);
3403 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
3404 type)) != 0) {
3405 /*
3406 * The lock request failed. Normally close the
3407 * descriptor but handle the case where someone might
3408 * have dup()d or close()d it when we weren't looking.
3409 */
3410 FILEDESC_LOCK(fdp);
3411 if (fdp->fd_ofiles[indx] == fp) {
3412 fdp->fd_ofiles[indx] = NULL;
3413 FILEDESC_UNLOCK(fdp);
3414 fdrop(fp, td);
3415 } else
3416 FILEDESC_UNLOCK(fdp);
3417 /*
3418 * release our private reference
3419 */
3420 fdrop(fp, td);
3421 return(error);
3422 }
3423 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3424 fp->f_flag |= FHASLOCK;
3425 }
3426 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3427 vfs_object_create(vp, td, td->td_ucred);
3428
3429 VOP_UNLOCK(vp, 0, td);
3430 fdrop(fp, td);
3431 td->td_retval[0] = indx;
3432 return (0);
3433
3434bad:
3435 vput(vp);
3436 return (error);
3437}
3438
3439/*
3440 * Stat an (NFS) file handle.
3441 */
3442#ifndef _SYS_SYSPROTO_H_
3443struct fhstat_args {
3444 struct fhandle *u_fhp;
3445 struct stat *sb;
3446};
3447#endif
3448int
3449fhstat(td, uap)
3450 struct thread *td;
3451 register struct fhstat_args /* {
3452 syscallarg(struct fhandle *) u_fhp;
3453 syscallarg(struct stat *) sb;
3454 } */ *uap;
3455{
3456 struct stat sb;
3457 fhandle_t fh;
3458 struct mount *mp;
3459 struct vnode *vp;
3460 int error;
3461
3462 /*
3463 * Must be super user
3464 */
3465 error = suser(td);
3466 if (error)
3467 return (error);
3468
3469 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3470 if (error)
3471 return (error);
3472
3473 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3474 return (ESTALE);
3475 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3476 return (error);
3477 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
3478 vput(vp);
3479 if (error)
3480 return (error);
3481 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3482 return (error);
3483}
3484
3485/*
3486 * Implement fstatfs() for (NFS) file handles.
3487 */
3488#ifndef _SYS_SYSPROTO_H_
3489struct fhstatfs_args {
3490 struct fhandle *u_fhp;
3491 struct statfs *buf;
3492};
3493#endif
3494int
3495fhstatfs(td, uap)
3496 struct thread *td;
3497 struct fhstatfs_args /* {
3498 syscallarg(struct fhandle) *u_fhp;
3499 syscallarg(struct statfs) *buf;
3500 } */ *uap;
3501{
3502 struct statfs *sp;
3503 struct mount *mp;
3504 struct vnode *vp;
3505 struct statfs sb;
3506 fhandle_t fh;
3507 int error;
3508
3509 /*
3510 * Must be super user
3511 */
3512 error = suser(td);
3513 if (error)
3514 return (error);
3515
3516 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3517 return (error);
3518
3519 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3520 return (ESTALE);
3521 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3522 return (error);
3523 mp = vp->v_mount;
3524 sp = &mp->mnt_stat;
3525 vput(vp);
3526#ifdef MAC
3527 error = mac_check_mount_stat(td->td_ucred, mp);
3528 if (error)
3529 return (error);
3530#endif
3531 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3532 return (error);
3533 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3534 if (suser(td)) {
3535 bcopy(sp, &sb, sizeof(sb));
3536 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3537 sp = &sb;
3538 }
3539 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3540}
3541
3542/*
3543 * Syscall to push extended attribute configuration information into the
3544 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3545 * a command (int cmd), and attribute name and misc data. For now, the
3546 * attribute name is left in userspace for consumption by the VFS_op.
3547 * It will probably be changed to be copied into sysspace by the
3548 * syscall in the future, once issues with various consumers of the
3549 * attribute code have raised their hands.
3550 *
3551 * Currently this is used only by UFS Extended Attributes.
3552 */
3553int
3554extattrctl(td, uap)
3555 struct thread *td;
3556 struct extattrctl_args /* {
3557 syscallarg(const char *) path;
3558 syscallarg(int) cmd;
3559 syscallarg(const char *) filename;
3560 syscallarg(int) attrnamespace;
3561 syscallarg(const char *) attrname;
3562 } */ *uap;
3563{
3564 struct vnode *filename_vp;
3565 struct nameidata nd;
3566 struct mount *mp, *mp_writable;
3567 char attrname[EXTATTR_MAXNAMELEN];
3568 int error;
3569
3570 /*
3571 * uap->attrname is not always defined. We check again later when we
3572 * invoke the VFS call so as to pass in NULL there if needed.
3573 */
3574 if (uap->attrname != NULL) {
3575 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
3576 NULL);
3577 if (error)
3578 return (error);
3579 }
3580
3581 /*
3582 * uap->filename is not always defined. If it is, grab a vnode lock,
3583 * which VFS_EXTATTRCTL() will later release.
3584 */
3585 filename_vp = NULL;
3586 if (uap->filename != NULL) {
3587 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3588 uap->filename, td);
3589 if ((error = namei(&nd)) != 0)
3590 return (error);
3591 filename_vp = nd.ni_vp;
3592 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
3593 }
3594
3595 /* uap->path is always defined. */
3596 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
3597 if ((error = namei(&nd)) != 0) {
3598 if (filename_vp != NULL)
3599 vput(filename_vp);
3600 return (error);
3601 }
3602 mp = nd.ni_vp->v_mount;
3603 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
3604 NDFREE(&nd, 0);
3605 if (error) {
3606 if (filename_vp != NULL)
3607 vput(filename_vp);
3608 return (error);
3609 }
3610
3611 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
3612 uap->attrname != NULL ? attrname : NULL, td);
3613
3614 vn_finished_write(mp_writable);
3615 /*
3616 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
3617 * filename_vp, so vrele it if it is defined.
3618 */
3619 if (filename_vp != NULL)
3620 vrele(filename_vp);
3621
3622 return (error);
3623}
3624
3625/*-
3626 * Set a named extended attribute on a file or directory
3627 *
3628 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
3629 * kernelspace string pointer "attrname", userspace buffer
3630 * pointer "data", buffer length "nbytes", thread "td".
3631 * Returns: 0 on success, an error number otherwise
3632 * Locks: none
3633 * References: vp must be a valid reference for the duration of the call
3634 */
3635static int
3636extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3637 void *data, size_t nbytes, struct thread *td)
3638{
3639 struct mount *mp;
3640 struct uio auio;
3641 struct iovec aiov;
3642 ssize_t cnt;
3643 int error;
3644
3645 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3646 return (error);
3647 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3648 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3649
3650 aiov.iov_base = data;
3651 aiov.iov_len = nbytes;
3652 auio.uio_iov = &aiov;
3653 auio.uio_iovcnt = 1;
3654 auio.uio_offset = 0;
3655 if (nbytes > INT_MAX) {
3656 error = EINVAL;
3657 goto done;
3658 }
3659 auio.uio_resid = nbytes;
3660 auio.uio_rw = UIO_WRITE;
3661 auio.uio_segflg = UIO_USERSPACE;
3662 auio.uio_td = td;
3663 cnt = nbytes;
3664
3665#ifdef MAC
3666 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
3667 attrname, &auio);
3668 if (error)
3669 goto done;
3670#endif
3671
3672 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
3673 td->td_ucred, td);
3674 cnt -= auio.uio_resid;
3675 td->td_retval[0] = cnt;
3676
3677done:
3678 VOP_UNLOCK(vp, 0, td);
3679 vn_finished_write(mp);
3680 return (error);
3681}
3682
3683int
3684extattr_set_file(td, uap)
3685 struct thread *td;
3686 struct extattr_set_file_args /* {
3687 syscallarg(const char *) path;
3688 syscallarg(int) attrnamespace;
3689 syscallarg(const char *) attrname;
3690 syscallarg(void *) data;
3691 syscallarg(size_t) nbytes;
3692 } */ *uap;
3693{
3694 struct nameidata nd;
3695 char attrname[EXTATTR_MAXNAMELEN];
3696 int error;
3697
3698 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3699 if (error)
3700 return (error);
3701
3702 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
3703 if ((error = namei(&nd)) != 0)
3704 return (error);
3705 NDFREE(&nd, NDF_ONLY_PNBUF);
3706
3707 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
3708 uap->data, uap->nbytes, td);
3709
3710 vrele(nd.ni_vp);
3711 return (error);
3712}
3713
3714int
3715extattr_set_fd(td, uap)
3716 struct thread *td;
3717 struct extattr_set_fd_args /* {
3718 syscallarg(int) fd;
3719 syscallarg(int) attrnamespace;
3720 syscallarg(const char *) attrname;
3721 syscallarg(void *) data;
3722 syscallarg(size_t) nbytes;
3723 } */ *uap;
3724{
3725 struct file *fp;
3726 char attrname[EXTATTR_MAXNAMELEN];
3727 int error;
3728
3729 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3730 if (error)
3731 return (error);
3732
3733 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3734 return (error);
3735
3736 error = extattr_set_vp((struct vnode *)fp->f_data, uap->attrnamespace,
3737 attrname, uap->data, uap->nbytes, td);
3738 fdrop(fp, td);
3739
3740 return (error);
3741}
3742
3743/*-
3744 * Get a named extended attribute on a file or directory
3745 *
3746 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
3747 * kernelspace string pointer "attrname", userspace buffer
3748 * pointer "data", buffer length "nbytes", thread "td".
3749 * Returns: 0 on success, an error number otherwise
3750 * Locks: none
3751 * References: vp must be a valid reference for the duration of the call
3752 */
3753static int
3754extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3755 void *data, size_t nbytes, struct thread *td)
3756{
3757 struct uio auio, *auiop;
3758 struct iovec aiov;
3759 ssize_t cnt;
3760 size_t size, *sizep;
3761 int error;
3762
3763 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
3764 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3765
3766 /*
3767 * Slightly unusual semantics: if the user provides a NULL data
3768 * pointer, they don't want to receive the data, just the
3769 * maximum read length.
3770 */
3771 auiop = NULL;
3772 sizep = NULL;
3773 cnt = 0;
3774 if (data != NULL) {
3775 aiov.iov_base = data;
3776 aiov.iov_len = nbytes;
3777 auio.uio_iov = &aiov;
3778 auio.uio_offset = 0;
3779 if (nbytes > INT_MAX) {
3780 error = EINVAL;
3781 goto done;
3782 }
3783 auio.uio_resid = nbytes;
3784 auio.uio_rw = UIO_READ;
3785 auio.uio_segflg = UIO_USERSPACE;
3786 auio.uio_td = td;
3787 auiop = &auio;
3788 cnt = nbytes;
3789 } else
3790 sizep = &size;
3791
3792#ifdef MAC
3793 error = mac_check_vnode_getextattr(td->td_ucred, vp, attrnamespace,
3794 attrname, &auio);
3795 if (error)
3796 goto done;
3797#endif
3798
3799 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
3800 td->td_ucred, td);
3801
3802 if (auiop != NULL) {
3803 cnt -= auio.uio_resid;
3804 td->td_retval[0] = cnt;
3805 } else
3806 td->td_retval[0] = size;
3807
3808done:
3809 VOP_UNLOCK(vp, 0, td);
3810 return (error);
3811}
3812
3813int
3814extattr_get_file(td, uap)
3815 struct thread *td;
3816 struct extattr_get_file_args /* {
3817 syscallarg(const char *) path;
3818 syscallarg(int) attrnamespace;
3819 syscallarg(const char *) attrname;
3820 syscallarg(void *) data;
3821 syscallarg(size_t) nbytes;
3822 } */ *uap;
3823{
3824 struct nameidata nd;
3825 char attrname[EXTATTR_MAXNAMELEN];
3826 int error;
3827
3828 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3829 if (error)
3830 return (error);
3831
3832 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
3833 if ((error = namei(&nd)) != 0)
3834 return (error);
3835 NDFREE(&nd, NDF_ONLY_PNBUF);
3836
3837 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
3838 uap->data, uap->nbytes, td);
3839
3840 vrele(nd.ni_vp);
3841 return (error);
3842}
3843
3844int
3845extattr_get_fd(td, uap)
3846 struct thread *td;
3847 struct extattr_get_fd_args /* {
3848 syscallarg(int) fd;
3849 syscallarg(int) attrnamespace;
3850 syscallarg(const char *) attrname;
3851 syscallarg(void *) data;
3852 syscallarg(size_t) nbytes;
3853 } */ *uap;
3854{
3855 struct file *fp;
3856 char attrname[EXTATTR_MAXNAMELEN];
3857 int error;
3858
3859 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3860 if (error)
3861 return (error);
3862
3863 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3864 return (error);
3865
3866 error = extattr_get_vp((struct vnode *)fp->f_data, uap->attrnamespace,
3867 attrname, uap->data, uap->nbytes, td);
3868
3869 fdrop(fp, td);
3870 return (error);
3871}
3872
3873/*
3874 * extattr_delete_vp(): Delete a named extended attribute on a file or
3875 * directory
3876 *
3877 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
3878 * kernelspace string pointer "attrname", proc "p"
3879 * Returns: 0 on success, an error number otherwise
3880 * Locks: none
3881 * References: vp must be a valid reference for the duration of the call
3882 */
3883static int
3884extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3885 struct thread *td)
3886{
3887 struct mount *mp;
3888 int error;
3889
3890 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3891 return (error);
3892 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3893 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3894
3895#ifdef MAC
3896 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
3897 attrname, NULL);
3898#endif
3899
3900 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, td->td_ucred,
3901 td);
3902
3903 VOP_UNLOCK(vp, 0, td);
3904 vn_finished_write(mp);
3905 return (error);
3906}
3907
3908int
3909extattr_delete_file(td, uap)
3910 struct thread *td;
3911 struct extattr_delete_file_args /* {
3912 syscallarg(const char *) path;
3913 syscallarg(int) attrnamespace;
3914 syscallarg(const char *) attrname;
3915 } */ *uap;
3916{
3917 struct nameidata nd;
3918 char attrname[EXTATTR_MAXNAMELEN];
3919 int error;
3920
3921 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3922 if (error)
3923 return(error);
3924
3925 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
3926 if ((error = namei(&nd)) != 0)
3927 return(error);
3928 NDFREE(&nd, NDF_ONLY_PNBUF);
3929
3930 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
3931
3932 vrele(nd.ni_vp);
3933 return(error);
3934}
3935
3936int
3937extattr_delete_fd(td, uap)
3938 struct thread *td;
3939 struct extattr_delete_fd_args /* {
3940 syscallarg(int) fd;
3941 syscallarg(int) attrnamespace;
3942 syscallarg(const char *) attrname;
3943 } */ *uap;
3944{
3945 struct file *fp;
3946 struct vnode *vp;
3947 char attrname[EXTATTR_MAXNAMELEN];
3948 int error;
3949
3950 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
3951 if (error)
3952 return (error);
3953
3954 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3955 return (error);
3956 vp = (struct vnode *)fp->f_data;
3957
3958 error = extattr_delete_vp(vp, uap->attrnamespace, attrname, td);
3959
3960 fdrop(fp, td);
3961 return (error);
3962}