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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.77 2006/03/02 19:07:59 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/kern_syscall.h>
67 #include <machine/limits.h>
68 #include <vfs/union/union.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_zone.h>
73 #include <vm/vm_page.h>
75 #include <sys/file2.h>
77 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
78 static void checkdirs (struct vnode
*olddp
, struct namecache
*ncp
);
79 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
80 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
81 static int getutimes (const struct timeval
*, struct timespec
*);
82 static int setfown (struct vnode
*, uid_t
, gid_t
);
83 static int setfmode (struct vnode
*, int);
84 static int setfflags (struct vnode
*, int);
85 static int setutimes (struct vnode
*, const struct timespec
*, int);
86 static int usermount
= 0; /* if 1, non-root can mount fs. */
88 int (*union_dircheckp
) (struct thread
*, struct vnode
**, struct file
*);
90 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0, "");
93 * Virtual File System System Calls
97 * Mount a file system.
100 * mount_args(char *type, char *path, int flags, caddr_t data)
104 mount(struct mount_args
*uap
)
106 struct thread
*td
= curthread
;
107 struct proc
*p
= td
->td_proc
;
109 struct namecache
*ncp
;
111 struct vfsconf
*vfsp
;
112 int error
, flag
= 0, flag2
= 0;
114 struct nlookupdata nd
;
115 char fstypename
[MFSNAMELEN
];
116 struct nlcomponent nlc
;
119 if (p
->p_ucred
->cr_prison
!= NULL
)
121 if (usermount
== 0 && (error
= suser(td
)))
124 * Do not allow NFS export by non-root users.
126 if (uap
->flags
& MNT_EXPORTED
) {
132 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
135 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
138 * Lookup the requested path and extract the ncp and vnode.
140 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
142 if ((error
= nlookup(&nd
)) == 0) {
143 if (nd
.nl_ncp
->nc_vp
== NULL
)
153 * Extract the locked+refd ncp and cleanup the nd structure
160 * now we have the locked ref'd ncp and unreferenced vnode.
163 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) != 0) {
170 * Now we have an unlocked ref'd ncp and a locked ref'd vp
172 if (uap
->flags
& MNT_UPDATE
) {
173 if ((vp
->v_flag
& VROOT
) == 0) {
180 flag2
= mp
->mnt_kern_flag
;
182 * We only allow the filesystem to be reloaded if it
183 * is currently mounted read-only.
185 if ((uap
->flags
& MNT_RELOAD
) &&
186 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
189 return (EOPNOTSUPP
); /* Needs translation */
192 * Only root, or the user that did the original mount is
193 * permitted to update it.
195 if (mp
->mnt_stat
.f_owner
!= p
->p_ucred
->cr_uid
&&
196 (error
= suser(td
))) {
201 if (vfs_busy(mp
, LK_NOWAIT
, td
)) {
206 if ((vp
->v_flag
& VMOUNT
) != 0 ||
207 vp
->v_mountedhere
!= NULL
) {
213 vp
->v_flag
|= VMOUNT
;
215 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
216 VOP_UNLOCK(vp
, 0, td
);
220 * If the user is not root, ensure that they own the directory
221 * onto which we are attempting to mount.
223 if ((error
= VOP_GETATTR(vp
, &va
, td
)) ||
224 (va
.va_uid
!= p
->p_ucred
->cr_uid
&&
225 (error
= suser(td
)))) {
230 if ((error
= vinvalbuf(vp
, V_SAVE
, td
, 0, 0)) != 0) {
235 if (vp
->v_type
!= VDIR
) {
240 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
245 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
246 if (!strcmp(vfsp
->vfc_name
, fstypename
))
252 /* Only load modules for root (very important!) */
253 if ((error
= suser(td
)) != 0) {
258 error
= linker_load_file(fstypename
, &lf
);
259 if (error
|| lf
== NULL
) {
267 /* lookup again, see if the VFS was loaded */
268 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
269 if (!strcmp(vfsp
->vfc_name
, fstypename
))
274 linker_file_unload(lf
);
280 if ((vp
->v_flag
& VMOUNT
) != 0 ||
281 vp
->v_mountedhere
!= NULL
) {
286 vp
->v_flag
|= VMOUNT
;
289 * Allocate and initialize the filesystem.
291 mp
= malloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
292 TAILQ_INIT(&mp
->mnt_nvnodelist
);
293 TAILQ_INIT(&mp
->mnt_reservedvnlist
);
294 TAILQ_INIT(&mp
->mnt_jlist
);
295 mp
->mnt_nvnodelistsize
= 0;
296 lockinit(&mp
->mnt_lock
, "vfslock", 0, LK_NOPAUSE
);
297 vfs_busy(mp
, LK_NOWAIT
, td
);
298 mp
->mnt_op
= vfsp
->vfc_vfsops
;
300 vfsp
->vfc_refcount
++;
301 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
302 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
303 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
304 mp
->mnt_vnodecovered
= vp
;
305 mp
->mnt_stat
.f_owner
= p
->p_ucred
->cr_uid
;
306 mp
->mnt_iosize_max
= DFLTPHYS
;
307 VOP_UNLOCK(vp
, 0, td
);
310 * Set the mount level flags.
312 if (uap
->flags
& MNT_RDONLY
)
313 mp
->mnt_flag
|= MNT_RDONLY
;
314 else if (mp
->mnt_flag
& MNT_RDONLY
)
315 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
316 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
317 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_NOATIME
|
318 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
319 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
320 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
321 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_FORCE
|
322 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
323 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
325 * Mount the filesystem.
326 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
329 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, td
);
330 if (mp
->mnt_flag
& MNT_UPDATE
) {
331 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
332 mp
->mnt_flag
&= ~MNT_RDONLY
;
333 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
334 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
337 mp
->mnt_kern_flag
= flag2
;
340 vp
->v_flag
&= ~VMOUNT
;
345 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
347 * Put the new filesystem on the mount list after root. The mount
348 * point gets its own mnt_ncp which is a special ncp linking the
349 * vnode-under to the root of the new mount. The lookup code
350 * detects the mount point going forward and detects the special
351 * mnt_ncp via NCP_MOUNTPT going backwards.
353 * It is not necessary to invalidate or purge the vnode underneath
354 * because elements under the mount will be given their own glue
358 nlc
.nlc_nameptr
= "";
360 mp
->mnt_ncp
= cache_nlookup(ncp
, &nlc
);
361 cache_setunresolved(mp
->mnt_ncp
);
362 mp
->mnt_ncp
->nc_flag
|= NCF_MOUNTPT
;
363 mp
->mnt_ncp
->nc_mount
= mp
;
365 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
366 vp
->v_flag
&= ~VMOUNT
;
367 vp
->v_mountedhere
= mp
;
368 mountlist_insert(mp
, MNTINS_LAST
);
369 checkdirs(vp
, mp
->mnt_ncp
);
370 cache_unlock(mp
->mnt_ncp
); /* leave ref intact */
371 VOP_UNLOCK(vp
, 0, td
);
372 error
= vfs_allocate_syncvnode(mp
);
374 if ((error
= VFS_START(mp
, 0, td
)) != 0)
377 vfs_rm_vnodeops(&mp
->mnt_vn_coherency_ops
);
378 vfs_rm_vnodeops(&mp
->mnt_vn_journal_ops
);
379 vfs_rm_vnodeops(&mp
->mnt_vn_norm_ops
);
380 vfs_rm_vnodeops(&mp
->mnt_vn_spec_ops
);
381 vfs_rm_vnodeops(&mp
->mnt_vn_fifo_ops
);
382 vp
->v_flag
&= ~VMOUNT
;
383 mp
->mnt_vfc
->vfc_refcount
--;
393 * Scan all active processes to see if any of them have a current
394 * or root directory onto which the new filesystem has just been
395 * mounted. If so, replace them with the new mount point.
397 * The passed ncp is ref'd and locked (from the mount code) and
398 * must be associated with the vnode representing the root of the
402 checkdirs(struct vnode
*olddp
, struct namecache
*ncp
)
404 struct filedesc
*fdp
;
409 if (olddp
->v_usecount
== 1)
411 mp
= olddp
->v_mountedhere
;
412 if (VFS_ROOT(mp
, &newdp
))
413 panic("mount: lost mount");
414 cache_setvp(ncp
, newdp
);
416 if (rootvnode
== olddp
) {
418 vfs_cache_setroot(newdp
, cache_hold(ncp
));
421 FOREACH_PROC_IN_SYSTEM(p
) {
423 if (fdp
->fd_cdir
== olddp
) {
426 fdp
->fd_cdir
= newdp
;
427 cache_drop(fdp
->fd_ncdir
);
428 fdp
->fd_ncdir
= cache_hold(ncp
);
430 if (fdp
->fd_rdir
== olddp
) {
433 fdp
->fd_rdir
= newdp
;
434 cache_drop(fdp
->fd_nrdir
);
435 fdp
->fd_nrdir
= cache_hold(ncp
);
442 * Unmount a file system.
444 * Note: unmount takes a path to the vnode mounted on as argument,
445 * not special file (as before).
448 * umount_args(char *path, int flags)
452 unmount(struct unmount_args
*uap
)
454 struct thread
*td
= curthread
;
455 struct proc
*p
= td
->td_proc
;
456 struct mount
*mp
= NULL
;
458 struct nlookupdata nd
;
461 if (p
->p_ucred
->cr_prison
!= NULL
)
463 if (usermount
== 0 && (error
= suser(td
)))
466 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
468 error
= nlookup(&nd
);
472 mp
= nd
.nl_ncp
->nc_mount
;
475 * Only root, or the user that did the original mount is
476 * permitted to unmount this filesystem.
478 if ((mp
->mnt_stat
.f_owner
!= p
->p_ucred
->cr_uid
) &&
483 * Don't allow unmounting the root file system.
485 if (mp
->mnt_flag
& MNT_ROOTFS
) {
491 * Must be the root of the filesystem
493 if (! (nd
.nl_ncp
->nc_flag
& NCF_MOUNTPT
)) {
502 return (dounmount(mp
, uap
->flags
, td
));
506 * Do the actual file system unmount.
509 dounmount_interlock(struct mount
*mp
)
511 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
513 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
518 dounmount(struct mount
*mp
, int flags
, struct thread
*td
)
520 struct vnode
*coveredvp
;
525 * Exclusive access for unmounting purposes
527 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
531 * Allow filesystems to detect that a forced unmount is in progress.
533 if (flags
& MNT_FORCE
)
534 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
535 error
= lockmgr(&mp
->mnt_lock
, LK_DRAIN
|
536 ((flags
& MNT_FORCE
) ? 0 : LK_NOWAIT
), NULL
, td
);
538 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
539 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
544 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
545 vfs_setpublicfs(NULL
, NULL
, NULL
);
547 vfs_msync(mp
, MNT_WAIT
);
548 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
549 mp
->mnt_flag
&=~ MNT_ASYNC
;
550 cache_purgevfs(mp
); /* remove cache entries for this file sys */
551 if (mp
->mnt_syncer
!= NULL
)
552 vrele(mp
->mnt_syncer
);
553 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
554 (error
= VFS_SYNC(mp
, MNT_WAIT
, td
)) == 0) ||
556 error
= VFS_UNMOUNT(mp
, flags
, td
);
558 if (mp
->mnt_syncer
== NULL
)
559 vfs_allocate_syncvnode(mp
);
560 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
561 mp
->mnt_flag
|= async_flag
;
562 lockmgr(&mp
->mnt_lock
, LK_RELEASE
| LK_REENABLE
, NULL
, td
);
563 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
568 * Clean up any journals still associated with the mount after
569 * filesystem activity has ceased.
571 journal_remove_all_journals(mp
,
572 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
574 mountlist_remove(mp
);
577 * Remove any installed vnode ops here so the individual VFSs don't
580 vfs_rm_vnodeops(&mp
->mnt_vn_coherency_ops
);
581 vfs_rm_vnodeops(&mp
->mnt_vn_journal_ops
);
582 vfs_rm_vnodeops(&mp
->mnt_vn_norm_ops
);
583 vfs_rm_vnodeops(&mp
->mnt_vn_spec_ops
);
584 vfs_rm_vnodeops(&mp
->mnt_vn_fifo_ops
);
586 if ((coveredvp
= mp
->mnt_vnodecovered
) != NULLVP
) {
587 coveredvp
->v_mountedhere
= NULL
;
589 cache_drop(mp
->mnt_ncp
);
592 mp
->mnt_vfc
->vfc_refcount
--;
593 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
594 panic("unmount: dangling vnode");
595 lockmgr(&mp
->mnt_lock
, LK_RELEASE
, NULL
, td
);
596 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
603 * Sync each mounted filesystem.
607 static int syncprt
= 0;
608 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
611 static int sync_callback(struct mount
*mp
, void *data
);
615 sync(struct sync_args
*uap
)
617 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
620 * print out buffer pool stat information on each sync() call.
630 sync_callback(struct mount
*mp
, void *data __unused
)
634 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
635 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
636 mp
->mnt_flag
&= ~MNT_ASYNC
;
637 vfs_msync(mp
, MNT_NOWAIT
);
638 VFS_SYNC(mp
, MNT_NOWAIT
, curthread
);
639 mp
->mnt_flag
|= asyncflag
;
644 /* XXX PRISON: could be per prison flag */
645 static int prison_quotas
;
647 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
651 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
653 * Change filesystem quotas.
657 quotactl(struct quotactl_args
*uap
)
659 struct nlookupdata nd
;
667 if (p
->p_ucred
->cr_prison
&& !prison_quotas
)
670 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
672 error
= nlookup(&nd
);
674 mp
= nd
.nl_ncp
->nc_mount
;
675 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
683 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
684 * void *buf, int buflen)
686 * This function operates on a mount point and executes the specified
687 * operation using the specified control data, and possibly returns data.
689 * The actual number of bytes stored in the result buffer is returned, 0
690 * if none, otherwise an error is returned.
694 mountctl(struct mountctl_args
*uap
)
696 struct thread
*td
= curthread
;
697 struct proc
*p
= td
->td_proc
;
698 struct filedesc
*fdp
= p
->p_fd
;
706 * Sanity and permissions checks. We must be root.
709 if (p
->p_ucred
->cr_prison
!= NULL
)
711 if ((error
= suser(td
)) != 0)
715 * Argument length checks
717 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
719 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
721 if (uap
->path
== NULL
)
725 * Allocate the necessary buffers and copyin data
727 path
= zalloc(namei_zone
);
728 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
733 ctl
= malloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
734 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
739 buf
= malloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
742 * Validate the descriptor
746 } else if ((u_int
)uap
->fd
>= fdp
->fd_nfiles
||
747 (fp
= fdp
->fd_files
[uap
->fd
].fp
) == NULL
) {
755 * Execute the internal kernel function and clean up.
757 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
760 if (error
== 0 && uap
->sysmsg_result
> 0)
761 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
764 zfree(namei_zone
, path
);
773 * Execute a mount control operation by resolving the path to a mount point
774 * and calling vop_mountctl().
777 kern_mountctl(const char *path
, int op
, struct file
*fp
,
778 const void *ctl
, int ctllen
,
779 void *buf
, int buflen
, int *res
)
783 struct nlookupdata nd
;
788 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
790 error
= nlookup(&nd
);
792 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
800 * Must be the root of the filesystem
802 if ((vp
->v_flag
& VROOT
) == 0) {
806 error
= vop_mountctl(mp
->mnt_vn_use_ops
, op
, fp
, ctl
, ctllen
,
813 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
815 struct thread
*td
= curthread
;
816 struct proc
*p
= td
->td_proc
;
819 char *fullpath
, *freepath
;
822 if ((error
= nlookup(nd
)) != 0)
824 mp
= nd
->nl_ncp
->nc_mount
;
826 if ((error
= VFS_STATFS(mp
, sp
, td
)) != 0)
829 error
= cache_fullpath(p
, mp
->mnt_ncp
, &fullpath
, &freepath
);
832 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
833 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
834 free(freepath
, M_TEMP
);
836 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
837 bcopy(sp
, buf
, sizeof(*buf
));
838 /* Only root should have access to the fsid's. */
840 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
845 * statfs_args(char *path, struct statfs *buf)
847 * Get filesystem statistics.
850 statfs(struct statfs_args
*uap
)
852 struct nlookupdata nd
;
856 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
858 error
= kern_statfs(&nd
, &buf
);
861 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
866 kern_fstatfs(int fd
, struct statfs
*buf
)
868 struct thread
*td
= curthread
;
869 struct proc
*p
= td
->td_proc
;
873 char *fullpath
, *freepath
;
877 error
= getvnode(p
->p_fd
, fd
, &fp
);
880 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
884 error
= VFS_STATFS(mp
, sp
, td
);
888 error
= cache_fullpath(p
, mp
->mnt_ncp
, &fullpath
, &freepath
);
891 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
892 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
893 free(freepath
, M_TEMP
);
895 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
896 bcopy(sp
, buf
, sizeof(*buf
));
898 /* Only root should have access to the fsid's. */
900 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
905 * fstatfs_args(int fd, struct statfs *buf)
907 * Get filesystem statistics.
910 fstatfs(struct fstatfs_args
*uap
)
915 error
= kern_fstatfs(uap
->fd
, &buf
);
918 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
923 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
925 * Get statistics on all filesystems.
928 struct getfsstat_info
{
939 static int getfsstat_callback(struct mount
*, void *);
943 getfsstat(struct getfsstat_args
*uap
)
945 struct thread
*td
= curthread
;
946 struct proc
*p
= td
->td_proc
;
947 struct getfsstat_info info
;
949 bzero(&info
, sizeof(info
));
950 if (p
!= NULL
&& (p
->p_fd
->fd_nrdir
->nc_flag
& NCF_ROOT
) == 0)
951 info
.is_chrooted
= 1;
953 info
.is_chrooted
= 0;
955 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
956 info
.sfsp
= uap
->buf
;
958 info
.flags
= uap
->flags
;
962 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
963 if (info
.sfsp
&& info
.count
> info
.maxcount
)
964 uap
->sysmsg_result
= info
.maxcount
;
966 uap
->sysmsg_result
= info
.count
;
971 getfsstat_callback(struct mount
*mp
, void *data
)
973 struct getfsstat_info
*info
= data
;
979 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
980 if (info
->is_chrooted
&& !chroot_visible_mnt(mp
, info
->p
))
985 * If MNT_NOWAIT or MNT_LAZY is specified, do not
986 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
987 * overrides MNT_WAIT.
989 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
990 (info
->flags
& MNT_WAIT
)) &&
991 (error
= VFS_STATFS(mp
, sp
, info
->td
))) {
994 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
996 error
= cache_fullpath(info
->p
, mp
->mnt_ncp
,
997 &fullpath
, &freepath
);
1002 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1003 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1004 free(freepath
, M_TEMP
);
1006 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1008 info
->error
= error
;
1018 * fchdir_args(int fd)
1020 * Change current working directory to a given file descriptor.
1024 fchdir(struct fchdir_args
*uap
)
1026 struct thread
*td
= curthread
;
1027 struct proc
*p
= td
->td_proc
;
1028 struct filedesc
*fdp
= p
->p_fd
;
1029 struct vnode
*vp
, *ovp
;
1032 struct namecache
*ncp
, *oncp
;
1033 struct namecache
*nct
;
1036 if ((error
= getvnode(fdp
, uap
->fd
, &fp
)) != 0)
1038 vp
= (struct vnode
*)fp
->f_data
;
1040 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
1041 if (vp
->v_type
!= VDIR
|| fp
->f_ncp
== NULL
)
1044 error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
, td
);
1049 ncp
= cache_hold(fp
->f_ncp
);
1050 while (!error
&& (mp
= vp
->v_mountedhere
) != NULL
) {
1051 error
= nlookup_mp(mp
, &nct
);
1053 cache_unlock(nct
); /* leave ref intact */
1056 error
= vget(vp
, LK_SHARED
, td
);
1057 KKASSERT(error
== 0);
1064 oncp
= fdp
->fd_ncdir
;
1065 VOP_UNLOCK(vp
, 0, td
); /* leave ref intact */
1067 fdp
->fd_ncdir
= ncp
;
1078 kern_chdir(struct nlookupdata
*nd
)
1080 struct thread
*td
= curthread
;
1081 struct proc
*p
= td
->td_proc
;
1082 struct filedesc
*fdp
= p
->p_fd
;
1083 struct vnode
*vp
, *ovp
;
1084 struct namecache
*oncp
;
1087 if ((error
= nlookup(nd
)) != 0)
1089 if ((vp
= nd
->nl_ncp
->nc_vp
) == NULL
)
1091 if ((error
= vget(vp
, LK_SHARED
, td
)) != 0)
1094 error
= checkvp_chdir(vp
, td
);
1095 VOP_UNLOCK(vp
, 0, td
);
1098 oncp
= fdp
->fd_ncdir
;
1099 cache_unlock(nd
->nl_ncp
); /* leave reference intact */
1100 fdp
->fd_ncdir
= nd
->nl_ncp
;
1112 * chdir_args(char *path)
1114 * Change current working directory (``.'').
1117 chdir(struct chdir_args
*uap
)
1119 struct nlookupdata nd
;
1122 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1124 error
= kern_chdir(&nd
);
1130 * Helper function for raised chroot(2) security function: Refuse if
1131 * any filedescriptors are open directories.
1134 chroot_refuse_vdir_fds(fdp
)
1135 struct filedesc
*fdp
;
1142 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1143 error
= getvnode(fdp
, fd
, &fp
);
1146 vp
= (struct vnode
*)fp
->f_data
;
1147 if (vp
->v_type
!= VDIR
)
1155 * This sysctl determines if we will allow a process to chroot(2) if it
1156 * has a directory open:
1157 * 0: disallowed for all processes.
1158 * 1: allowed for processes that were not already chroot(2)'ed.
1159 * 2: allowed for all processes.
1162 static int chroot_allow_open_directories
= 1;
1164 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1165 &chroot_allow_open_directories
, 0, "");
1168 * chroot to the specified namecache entry. We obtain the vp from the
1169 * namecache data. The passed ncp must be locked and referenced and will
1170 * remain locked and referenced on return.
1173 kern_chroot(struct namecache
*ncp
)
1175 struct thread
*td
= curthread
;
1176 struct proc
*p
= td
->td_proc
;
1177 struct filedesc
*fdp
= p
->p_fd
;
1182 * Only root can chroot
1184 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0)
1188 * Disallow open directory descriptors (fchdir() breakouts).
1190 if (chroot_allow_open_directories
== 0 ||
1191 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1192 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1195 if ((vp
= ncp
->nc_vp
) == NULL
)
1198 if ((error
= vget(vp
, LK_SHARED
, td
)) != 0)
1202 * Check the validity of vp as a directory to change to and
1203 * associate it with rdir/jdir.
1205 error
= checkvp_chdir(vp
, td
);
1206 VOP_UNLOCK(vp
, 0, td
); /* leave reference intact */
1208 vrele(fdp
->fd_rdir
);
1209 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1210 cache_drop(fdp
->fd_nrdir
);
1211 fdp
->fd_nrdir
= cache_hold(ncp
);
1212 if (fdp
->fd_jdir
== NULL
) {
1215 fdp
->fd_njdir
= cache_hold(ncp
);
1224 * chroot_args(char *path)
1226 * Change notion of root (``/'') directory.
1230 chroot(struct chroot_args
*uap
)
1232 struct thread
*td
= curthread
;
1233 struct nlookupdata nd
;
1236 KKASSERT(td
->td_proc
);
1237 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1242 error
= nlookup(&nd
);
1244 error
= kern_chroot(nd
.nl_ncp
);
1250 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1251 * determine whether it is legal to chdir to the vnode. The vnode's state
1252 * is not changed by this call.
1255 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1259 if (vp
->v_type
!= VDIR
)
1262 error
= VOP_ACCESS(vp
, VEXEC
, td
->td_proc
->p_ucred
, td
);
1267 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1269 struct thread
*td
= curthread
;
1270 struct proc
*p
= td
->td_proc
;
1271 struct lwp
*lp
= td
->td_lwp
;
1272 struct filedesc
*fdp
= p
->p_fd
;
1277 int type
, indx
, error
;
1280 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1282 flags
= FFLAGS(oflags
);
1283 error
= falloc(p
, &nfp
, NULL
);
1287 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) &~ S_ISTXT
;
1290 * XXX p_dupfd is a real mess. It allows a device to return a
1291 * file descriptor to be duplicated rather then doing the open
1297 * Call vn_open() to do the lookup and assign the vnode to the
1298 * file pointer. vn_open() does not change the ref count on fp
1299 * and the vnode, on success, will be inherited by the file pointer
1302 nd
->nl_flags
|= NLC_LOCKVP
;
1303 error
= vn_open(nd
, fp
, flags
, cmode
);
1307 * handle special fdopen() case. bleh. dupfdopen() is
1308 * responsible for dropping the old contents of ofiles[indx]
1311 * Note that if fsetfd() succeeds it will add a ref to fp
1312 * which represents the fd_files[] assignment. We must still
1313 * drop our reference.
1315 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1316 if (fsetfd(p
, fp
, &indx
) == 0) {
1317 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1320 fdrop(fp
, td
); /* our ref */
1323 if (fdp
->fd_files
[indx
].fp
== fp
) {
1324 funsetfd(fdp
, indx
);
1325 fdrop(fp
, td
); /* fd_files[] ref */
1329 fdrop(fp
, td
); /* our ref */
1330 if (error
== ERESTART
)
1336 * ref the vnode for ourselves so it can't be ripped out from under
1337 * is. XXX need an ND flag to request that the vnode be returned
1340 vp
= (struct vnode
*)fp
->f_data
;
1342 if ((error
= fsetfd(p
, fp
, &indx
)) != 0) {
1349 * If no error occurs the vp will have been assigned to the file
1355 * There should be 2 references on the file, one from the descriptor
1356 * table, and one for us.
1358 * Handle the case where someone closed the file (via its file
1359 * descriptor) while we were blocked. The end result should look
1360 * like opening the file succeeded but it was immediately closed.
1362 if (fp
->f_count
== 1) {
1363 KASSERT(fdp
->fd_files
[indx
].fp
!= fp
,
1364 ("Open file descriptor lost all refs"));
1372 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1373 lf
.l_whence
= SEEK_SET
;
1376 if (flags
& O_EXLOCK
)
1377 lf
.l_type
= F_WRLCK
;
1379 lf
.l_type
= F_RDLCK
;
1381 if ((flags
& FNONBLOCK
) == 0)
1384 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1386 * lock request failed. Normally close the descriptor
1387 * but handle the case where someone might have dup()d
1388 * it when we weren't looking. One reference is
1389 * owned by the descriptor array, the other by us.
1392 if (fdp
->fd_files
[indx
].fp
== fp
) {
1393 funsetfd(fdp
, indx
);
1399 fp
->f_flag
|= FHASLOCK
;
1401 /* assert that vn_open created a backing object if one is needed */
1402 KASSERT(!vn_canvmio(vp
) || VOP_GETVOBJECT(vp
, NULL
) == 0,
1403 ("open: vmio vnode has no backing object after vn_open"));
1408 * release our private reference, leaving the one associated with the
1409 * descriptor table intact.
1417 * open_args(char *path, int flags, int mode)
1419 * Check permissions, allocate an open file structure,
1420 * and call the device open routine if any.
1423 open(struct open_args
*uap
)
1425 struct nlookupdata nd
;
1428 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1430 error
= kern_open(&nd
, uap
->flags
,
1431 uap
->mode
, &uap
->sysmsg_result
);
1438 kern_mknod(struct nlookupdata
*nd
, int mode
, int dev
)
1440 struct namecache
*ncp
;
1441 struct thread
*td
= curthread
;
1442 struct proc
*p
= td
->td_proc
;
1450 switch (mode
& S_IFMT
) {
1456 error
= suser_cred(p
->p_ucred
, PRISON_ROOT
);
1463 nd
->nl_flags
|= NLC_CREATE
;
1464 if ((error
= nlookup(nd
)) != 0)
1471 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1472 vattr
.va_rdev
= dev
;
1475 switch (mode
& S_IFMT
) {
1476 case S_IFMT
: /* used by badsect to flag bad sectors */
1477 vattr
.va_type
= VBAD
;
1480 vattr
.va_type
= VCHR
;
1483 vattr
.va_type
= VBLK
;
1494 error
= VOP_NWHITEOUT(ncp
, nd
->nl_cred
, NAMEI_CREATE
);
1497 error
= VOP_NMKNOD(ncp
, &vp
, nd
->nl_cred
, &vattr
);
1506 * mknod_args(char *path, int mode, int dev)
1508 * Create a special file.
1511 mknod(struct mknod_args
*uap
)
1513 struct nlookupdata nd
;
1516 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1518 error
= kern_mknod(&nd
, uap
->mode
, uap
->dev
);
1524 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
1526 struct namecache
*ncp
;
1527 struct thread
*td
= curthread
;
1528 struct proc
*p
= td
->td_proc
;
1535 nd
->nl_flags
|= NLC_CREATE
;
1536 if ((error
= nlookup(nd
)) != 0)
1543 vattr
.va_type
= VFIFO
;
1544 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1546 error
= VOP_NMKNOD(ncp
, &vp
, nd
->nl_cred
, &vattr
);
1553 * mkfifo_args(char *path, int mode)
1555 * Create a named pipe.
1558 mkfifo(struct mkfifo_args
*uap
)
1560 struct nlookupdata nd
;
1563 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1565 error
= kern_mkfifo(&nd
, uap
->mode
);
1570 static int hardlink_check_uid
= 0;
1571 SYSCTL_INT(_kern
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
1572 &hardlink_check_uid
, 0,
1573 "Unprivileged processes cannot create hard links to files owned by other "
1575 static int hardlink_check_gid
= 0;
1576 SYSCTL_INT(_kern
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
1577 &hardlink_check_gid
, 0,
1578 "Unprivileged processes cannot create hard links to files owned by other "
1582 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
1588 * Shortcut if disabled
1590 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
1594 * root cred can always hardlink
1596 if (suser_cred(cred
, PRISON_ROOT
) == 0)
1600 * Otherwise only if the originating file is owned by the
1601 * same user or group. Note that any group is allowed if
1602 * the file is owned by the caller.
1604 error
= VOP_GETATTR(vp
, &va
, td
);
1608 if (hardlink_check_uid
) {
1609 if (cred
->cr_uid
!= va
.va_uid
)
1613 if (hardlink_check_gid
) {
1614 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
1622 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
1624 struct thread
*td
= curthread
;
1629 * Lookup the source and obtained a locked vnode.
1631 * XXX relookup on vget failure / race ?
1634 if ((error
= nlookup(nd
)) != 0)
1636 vp
= nd
->nl_ncp
->nc_vp
;
1637 KKASSERT(vp
!= NULL
);
1638 if (vp
->v_type
== VDIR
)
1639 return (EPERM
); /* POSIX */
1640 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) != 0)
1644 * Unlock the source so we can lookup the target without deadlocking
1645 * (XXX vp is locked already, possible other deadlock?). The target
1648 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
1649 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
1650 cache_unlock(nd
->nl_ncp
);
1652 linknd
->nl_flags
|= NLC_CREATE
;
1653 if ((error
= nlookup(linknd
)) != 0) {
1657 if (linknd
->nl_ncp
->nc_vp
) {
1663 * Finally run the new API VOP.
1665 error
= can_hardlink(vp
, td
, td
->td_proc
->p_ucred
);
1667 error
= VOP_NLINK(linknd
->nl_ncp
, vp
, linknd
->nl_cred
);
1673 * link_args(char *path, char *link)
1675 * Make a hard file link.
1678 link(struct link_args
*uap
)
1680 struct nlookupdata nd
, linknd
;
1683 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1685 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
1687 error
= kern_link(&nd
, &linknd
);
1688 nlookup_done(&linknd
);
1695 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
1697 struct namecache
*ncp
;
1703 nd
->nl_flags
|= NLC_CREATE
;
1704 if ((error
= nlookup(nd
)) != 0)
1711 vattr
.va_mode
= mode
;
1712 error
= VOP_NSYMLINK(ncp
, &vp
, nd
->nl_cred
, &vattr
, path
);
1719 * symlink(char *path, char *link)
1721 * Make a symbolic link.
1724 symlink(struct symlink_args
*uap
)
1726 struct thread
*td
= curthread
;
1727 struct nlookupdata nd
;
1732 path
= zalloc(namei_zone
);
1733 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1735 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
1737 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
1738 error
= kern_symlink(&nd
, path
, mode
);
1742 zfree(namei_zone
, path
);
1747 * undelete_args(char *path)
1749 * Delete a whiteout from the filesystem.
1753 undelete(struct undelete_args
*uap
)
1755 struct nlookupdata nd
;
1758 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1760 nd
.nl_flags
|= NLC_DELETE
;
1762 error
= nlookup(&nd
);
1764 error
= VOP_NWHITEOUT(nd
.nl_ncp
, nd
.nl_cred
, NAMEI_DELETE
);
1770 kern_unlink(struct nlookupdata
*nd
)
1772 struct namecache
*ncp
;
1776 nd
->nl_flags
|= NLC_DELETE
;
1777 if ((error
= nlookup(nd
)) != 0)
1780 error
= VOP_NREMOVE(ncp
, nd
->nl_cred
);
1785 * unlink_args(char *path)
1787 * Delete a name from the filesystem.
1790 unlink(struct unlink_args
*uap
)
1792 struct nlookupdata nd
;
1795 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1797 error
= kern_unlink(&nd
);
1803 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
1805 struct thread
*td
= curthread
;
1806 struct proc
*p
= td
->td_proc
;
1807 struct filedesc
*fdp
= p
->p_fd
;
1812 if ((u_int
)fd
>= fdp
->fd_nfiles
||
1813 (fp
= fdp
->fd_files
[fd
].fp
) == NULL
)
1815 if (fp
->f_type
!= DTYPE_VNODE
)
1819 fp
->f_offset
+= offset
;
1822 error
=VOP_GETATTR((struct vnode
*)fp
->f_data
, &vattr
, td
);
1825 fp
->f_offset
= offset
+ vattr
.va_size
;
1828 fp
->f_offset
= offset
;
1833 *res
= fp
->f_offset
;
1838 * lseek_args(int fd, int pad, off_t offset, int whence)
1840 * Reposition read/write file offset.
1843 lseek(struct lseek_args
*uap
)
1847 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
1848 &uap
->sysmsg_offset
);
1854 kern_access(struct nlookupdata
*nd
, int aflags
)
1856 struct thread
*td
= curthread
;
1860 if ((error
= nlookup(nd
)) != 0)
1863 error
= cache_vget(nd
->nl_ncp
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
1867 /* Flags == 0 means only check for existence. */
1876 if ((flags
& VWRITE
) == 0 || (error
= vn_writechk(vp
)) == 0)
1877 error
= VOP_ACCESS(vp
, flags
, nd
->nl_cred
, td
);
1880 * If the file handle is stale we have to re-resolve the
1881 * entry. This is a hack at the moment.
1883 if (error
== ESTALE
) {
1884 cache_setunresolved(nd
->nl_ncp
);
1885 error
= cache_resolve(nd
->nl_ncp
, nd
->nl_cred
);
1898 * access_args(char *path, int flags)
1900 * Check access permissions.
1903 access(struct access_args
*uap
)
1905 struct nlookupdata nd
;
1908 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1910 error
= kern_access(&nd
, uap
->flags
);
1916 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
1922 if ((error
= nlookup(nd
)) != 0)
1925 if ((vp
= nd
->nl_ncp
->nc_vp
) == NULL
)
1929 if ((error
= vget(vp
, LK_SHARED
, td
)) != 0)
1931 error
= vn_stat(vp
, st
, td
);
1934 * If the file handle is stale we have to re-resolve the entry. This
1935 * is a hack at the moment.
1937 if (error
== ESTALE
) {
1938 cache_setunresolved(nd
->nl_ncp
);
1939 error
= cache_resolve(nd
->nl_ncp
, nd
->nl_cred
);
1950 * stat_args(char *path, struct stat *ub)
1952 * Get file status; this version follows links.
1955 stat(struct stat_args
*uap
)
1957 struct nlookupdata nd
;
1961 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1963 error
= kern_stat(&nd
, &st
);
1965 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
1972 * lstat_args(char *path, struct stat *ub)
1974 * Get file status; this version does not follow links.
1977 lstat(struct lstat_args
*uap
)
1979 struct nlookupdata nd
;
1983 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1985 error
= kern_stat(&nd
, &st
);
1987 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
1994 * pathconf_Args(char *path, int name)
1996 * Get configurable pathname variables.
2000 pathconf(struct pathconf_args
*uap
)
2002 struct nlookupdata nd
;
2007 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2009 error
= nlookup(&nd
);
2011 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2014 error
= VOP_PATHCONF(vp
, uap
->name
, uap
->sysmsg_fds
);
2022 * kern_readlink isn't properly split yet. There is a copyin burried
2023 * in VOP_READLINK().
2026 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2028 struct thread
*td
= curthread
;
2029 struct proc
*p
= td
->td_proc
;
2035 if ((error
= nlookup(nd
)) != 0)
2037 error
= cache_vget(nd
->nl_ncp
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2040 if (vp
->v_type
!= VLNK
) {
2043 aiov
.iov_base
= buf
;
2044 aiov
.iov_len
= count
;
2045 auio
.uio_iov
= &aiov
;
2046 auio
.uio_iovcnt
= 1;
2047 auio
.uio_offset
= 0;
2048 auio
.uio_rw
= UIO_READ
;
2049 auio
.uio_segflg
= UIO_USERSPACE
;
2051 auio
.uio_resid
= count
;
2052 error
= VOP_READLINK(vp
, &auio
, p
->p_ucred
);
2055 *res
= count
- auio
.uio_resid
;
2060 * readlink_args(char *path, char *buf, int count)
2062 * Return target name of a symbolic link.
2065 readlink(struct readlink_args
*uap
)
2067 struct nlookupdata nd
;
2070 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2072 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2073 &uap
->sysmsg_result
);
2080 setfflags(struct vnode
*vp
, int flags
)
2082 struct thread
*td
= curthread
;
2083 struct proc
*p
= td
->td_proc
;
2088 * Prevent non-root users from setting flags on devices. When
2089 * a device is reused, users can retain ownership of the device
2090 * if they are allowed to set flags and programs assume that
2091 * chown can't fail when done as root.
2093 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2094 ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0))
2098 * note: vget is required for any operation that might mod the vnode
2099 * so VINACTIVE is properly cleared.
2101 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
2102 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) == 0) {
2104 vattr
.va_flags
= flags
;
2105 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, td
);
2112 * chflags(char *path, int flags)
2114 * Change flags of a file given a path name.
2118 chflags(struct chflags_args
*uap
)
2120 struct nlookupdata nd
;
2125 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2126 /* XXX Add NLC flag indicating modifying operation? */
2128 error
= nlookup(&nd
);
2130 error
= cache_vref(nd
.nl_ncp
, nd
.nl_cred
, &vp
);
2133 error
= setfflags(vp
, uap
->flags
);
2140 * fchflags_args(int fd, int flags)
2142 * Change flags of a file given a file descriptor.
2146 fchflags(struct fchflags_args
*uap
)
2148 struct thread
*td
= curthread
;
2149 struct proc
*p
= td
->td_proc
;
2153 if ((error
= getvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2155 return setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
2159 setfmode(struct vnode
*vp
, int mode
)
2161 struct thread
*td
= curthread
;
2162 struct proc
*p
= td
->td_proc
;
2167 * note: vget is required for any operation that might mod the vnode
2168 * so VINACTIVE is properly cleared.
2170 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
2171 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) == 0) {
2173 vattr
.va_mode
= mode
& ALLPERMS
;
2174 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, td
);
2181 kern_chmod(struct nlookupdata
*nd
, int mode
)
2186 /* XXX Add NLC flag indicating modifying operation? */
2187 if ((error
= nlookup(nd
)) != 0)
2189 if ((error
= cache_vref(nd
->nl_ncp
, nd
->nl_cred
, &vp
)) != 0)
2191 error
= setfmode(vp
, mode
);
2197 * chmod_args(char *path, int mode)
2199 * Change mode of a file given path name.
2203 chmod(struct chmod_args
*uap
)
2205 struct nlookupdata nd
;
2208 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2210 error
= kern_chmod(&nd
, uap
->mode
);
2216 * lchmod_args(char *path, int mode)
2218 * Change mode of a file given path name (don't follow links.)
2222 lchmod(struct lchmod_args
*uap
)
2224 struct nlookupdata nd
;
2227 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2229 error
= kern_chmod(&nd
, uap
->mode
);
2235 * fchmod_args(int fd, int mode)
2237 * Change mode of a file given a file descriptor.
2241 fchmod(struct fchmod_args
*uap
)
2243 struct thread
*td
= curthread
;
2244 struct proc
*p
= td
->td_proc
;
2248 if ((error
= getvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2250 return setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
2254 setfown(struct vnode
*vp
, uid_t uid
, gid_t gid
)
2256 struct thread
*td
= curthread
;
2257 struct proc
*p
= td
->td_proc
;
2262 * note: vget is required for any operation that might mod the vnode
2263 * so VINACTIVE is properly cleared.
2265 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
2266 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) == 0) {
2270 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, td
);
2277 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
2282 /* XXX Add NLC flag indicating modifying operation? */
2283 if ((error
= nlookup(nd
)) != 0)
2285 if ((error
= cache_vref(nd
->nl_ncp
, nd
->nl_cred
, &vp
)) != 0)
2287 error
= setfown(vp
, uid
, gid
);
2293 * chown(char *path, int uid, int gid)
2295 * Set ownership given a path name.
2298 chown(struct chown_args
*uap
)
2300 struct nlookupdata nd
;
2303 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2305 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2311 * lchown_args(char *path, int uid, int gid)
2313 * Set ownership given a path name, do not cross symlinks.
2316 lchown(struct lchown_args
*uap
)
2318 struct nlookupdata nd
;
2321 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2323 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2329 * fchown_args(int fd, int uid, int gid)
2331 * Set ownership given a file descriptor.
2335 fchown(struct fchown_args
*uap
)
2337 struct thread
*td
= curthread
;
2338 struct proc
*p
= td
->td_proc
;
2342 if ((error
= getvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2344 return setfown((struct vnode
*)fp
->f_data
,
2345 uap
->uid
, uap
->gid
);
2349 getutimes(const struct timeval
*tvp
, struct timespec
*tsp
)
2351 struct timeval tv
[2];
2355 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
2358 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
2359 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
2365 setutimes(struct vnode
*vp
, const struct timespec
*ts
, int nullflag
)
2367 struct thread
*td
= curthread
;
2368 struct proc
*p
= td
->td_proc
;
2373 * note: vget is required for any operation that might mod the vnode
2374 * so VINACTIVE is properly cleared.
2376 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
2377 if ((error
= vget(vp
, LK_EXCLUSIVE
, td
)) == 0) {
2379 vattr
.va_atime
= ts
[0];
2380 vattr
.va_mtime
= ts
[1];
2382 vattr
.va_vaflags
|= VA_UTIMES_NULL
;
2383 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, td
);
2390 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
2392 struct timespec ts
[2];
2396 if ((error
= getutimes(tptr
, ts
)) != 0)
2398 /* XXX Add NLC flag indicating modifying operation? */
2399 if ((error
= nlookup(nd
)) != 0)
2401 if ((error
= cache_vref(nd
->nl_ncp
, nd
->nl_cred
, &vp
)) != 0)
2403 error
= setutimes(vp
, ts
, tptr
== NULL
);
2409 * utimes_args(char *path, struct timeval *tptr)
2411 * Set the access and modification times of a file.
2414 utimes(struct utimes_args
*uap
)
2416 struct timeval tv
[2];
2417 struct nlookupdata nd
;
2421 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2425 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2427 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2433 * lutimes_args(char *path, struct timeval *tptr)
2435 * Set the access and modification times of a file.
2438 lutimes(struct lutimes_args
*uap
)
2440 struct timeval tv
[2];
2441 struct nlookupdata nd
;
2445 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2449 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2451 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2457 kern_futimes(int fd
, struct timeval
*tptr
)
2459 struct thread
*td
= curthread
;
2460 struct proc
*p
= td
->td_proc
;
2461 struct timespec ts
[2];
2465 error
= getutimes(tptr
, ts
);
2468 error
= getvnode(p
->p_fd
, fd
, &fp
);
2471 error
= setutimes((struct vnode
*)fp
->f_data
, ts
, tptr
== NULL
);
2476 * futimes_args(int fd, struct timeval *tptr)
2478 * Set the access and modification times of a file.
2481 futimes(struct futimes_args
*uap
)
2483 struct timeval tv
[2];
2487 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2492 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
2498 kern_truncate(struct nlookupdata
*nd
, off_t length
)
2506 /* XXX Add NLC flag indicating modifying operation? */
2507 if ((error
= nlookup(nd
)) != 0)
2509 if ((error
= cache_vref(nd
->nl_ncp
, nd
->nl_cred
, &vp
)) != 0)
2511 VOP_LEASE(vp
, nd
->nl_td
, nd
->nl_cred
, LEASE_WRITE
);
2512 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, nd
->nl_td
)) != 0) {
2516 if (vp
->v_type
== VDIR
) {
2518 } else if ((error
= vn_writechk(vp
)) == 0 &&
2519 (error
= VOP_ACCESS(vp
, VWRITE
, nd
->nl_cred
, nd
->nl_td
)) == 0) {
2521 vattr
.va_size
= length
;
2522 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
, nd
->nl_td
);
2529 * truncate(char *path, int pad, off_t length)
2531 * Truncate a file given its path name.
2534 truncate(struct truncate_args
*uap
)
2536 struct nlookupdata nd
;
2539 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2541 error
= kern_truncate(&nd
, uap
->length
);
2547 kern_ftruncate(int fd
, off_t length
)
2549 struct thread
*td
= curthread
;
2550 struct proc
*p
= td
->td_proc
;
2558 if ((error
= getvnode(p
->p_fd
, fd
, &fp
)) != 0)
2560 if ((fp
->f_flag
& FWRITE
) == 0)
2562 vp
= (struct vnode
*)fp
->f_data
;
2563 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
2564 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
2565 if (vp
->v_type
== VDIR
)
2567 else if ((error
= vn_writechk(vp
)) == 0) {
2569 vattr
.va_size
= length
;
2570 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
, td
);
2572 VOP_UNLOCK(vp
, 0, td
);
2577 * ftruncate_args(int fd, int pad, off_t length)
2579 * Truncate a file given a file descriptor.
2582 ftruncate(struct ftruncate_args
*uap
)
2586 error
= kern_ftruncate(uap
->fd
, uap
->length
);
2594 * Sync an open file.
2598 fsync(struct fsync_args
*uap
)
2600 struct thread
*td
= curthread
;
2601 struct proc
*p
= td
->td_proc
;
2607 if ((error
= getvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2609 vp
= (struct vnode
*)fp
->f_data
;
2610 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
2611 if (VOP_GETVOBJECT(vp
, &obj
) == 0)
2612 vm_object_page_clean(obj
, 0, 0, 0);
2613 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
, td
)) == 0 &&
2614 vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SOFTDEP
) &&
2616 error
= (*bioops
.io_fsync
)(vp
);
2617 VOP_UNLOCK(vp
, 0, td
);
2622 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
2624 struct namecache
*fncpd
;
2625 struct namecache
*tncpd
;
2626 struct namecache
*ncp
;
2631 if ((error
= nlookup(fromnd
)) != 0)
2633 if ((fncpd
= fromnd
->nl_ncp
->nc_parent
) == NULL
)
2638 * unlock the source ncp so we can lookup the target ncp without
2639 * deadlocking. The target may or may not exist so we do not check
2640 * for a target vp like kern_mkdir() and other creation functions do.
2642 * The source and target directories are ref'd and rechecked after
2643 * everything is relocked to determine if the source or target file
2646 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
2647 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2648 cache_unlock(fromnd
->nl_ncp
);
2650 tond
->nl_flags
|= NLC_CREATE
;
2651 if ((error
= nlookup(tond
)) != 0) {
2655 if ((tncpd
= tond
->nl_ncp
->nc_parent
) == NULL
) {
2662 * If the source and target are the same there is nothing to do
2664 if (fromnd
->nl_ncp
== tond
->nl_ncp
) {
2671 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2672 * may have become invalid while it was unlocked, nc_vp and nc_mount
2675 if (cache_lock_nonblock(fromnd
->nl_ncp
) == 0) {
2676 cache_resolve(fromnd
->nl_ncp
, fromnd
->nl_cred
);
2677 } else if (fromnd
->nl_ncp
> tond
->nl_ncp
) {
2678 cache_lock(fromnd
->nl_ncp
);
2679 cache_resolve(fromnd
->nl_ncp
, fromnd
->nl_cred
);
2681 cache_unlock(tond
->nl_ncp
);
2682 cache_lock(fromnd
->nl_ncp
);
2683 cache_resolve(fromnd
->nl_ncp
, fromnd
->nl_cred
);
2684 cache_lock(tond
->nl_ncp
);
2685 cache_resolve(tond
->nl_ncp
, tond
->nl_cred
);
2687 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
2690 * make sure the parent directories linkages are the same
2692 if (fncpd
!= fromnd
->nl_ncp
->nc_parent
||
2693 tncpd
!= tond
->nl_ncp
->nc_parent
) {
2700 * Both the source and target must be within the same filesystem and
2701 * in the same filesystem as their parent directories within the
2702 * namecache topology.
2704 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2706 mp
= fncpd
->nc_mount
;
2707 if (mp
!= tncpd
->nc_mount
|| mp
!= fromnd
->nl_ncp
->nc_mount
||
2708 mp
!= tond
->nl_ncp
->nc_mount
) {
2715 * If the target exists and either the source or target is a directory,
2716 * then both must be directories.
2718 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2721 if (tond
->nl_ncp
->nc_vp
) {
2722 if (fromnd
->nl_ncp
->nc_vp
== NULL
) {
2724 } else if (fromnd
->nl_ncp
->nc_vp
->v_type
== VDIR
) {
2725 if (tond
->nl_ncp
->nc_vp
->v_type
!= VDIR
)
2727 } else if (tond
->nl_ncp
->nc_vp
->v_type
== VDIR
) {
2733 * You cannot rename a source into itself or a subdirectory of itself.
2734 * We check this by travsersing the target directory upwards looking
2735 * for a match against the source.
2738 for (ncp
= tncpd
; ncp
; ncp
= ncp
->nc_parent
) {
2739 if (fromnd
->nl_ncp
== ncp
) {
2750 * Even though the namespaces are different, they may still represent
2751 * hardlinks to the same file. The filesystem might have a hard time
2752 * with this so we issue a NREMOVE of the source instead of a NRENAME
2753 * when we detect the situation.
2756 if (fromnd
->nl_ncp
->nc_vp
== tond
->nl_ncp
->nc_vp
) {
2757 error
= VOP_NREMOVE(fromnd
->nl_ncp
, fromnd
->nl_cred
);
2759 error
= VOP_NRENAME(fromnd
->nl_ncp
, tond
->nl_ncp
,
2767 * rename_args(char *from, char *to)
2769 * Rename files. Source and destination must either both be directories,
2770 * or both not be directories. If target is a directory, it must be empty.
2773 rename(struct rename_args
*uap
)
2775 struct nlookupdata fromnd
, tond
;
2778 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
2780 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
2782 error
= kern_rename(&fromnd
, &tond
);
2783 nlookup_done(&tond
);
2785 nlookup_done(&fromnd
);
2790 kern_mkdir(struct nlookupdata
*nd
, int mode
)
2792 struct thread
*td
= curthread
;
2793 struct proc
*p
= td
->td_proc
;
2794 struct namecache
*ncp
;
2800 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
;
2801 if ((error
= nlookup(nd
)) != 0)
2809 vattr
.va_type
= VDIR
;
2810 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
2813 error
= VOP_NMKDIR(ncp
, &vp
, p
->p_ucred
, &vattr
);
2820 * mkdir_args(char *path, int mode)
2822 * Make a directory file.
2826 mkdir(struct mkdir_args
*uap
)
2828 struct nlookupdata nd
;
2831 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2833 error
= kern_mkdir(&nd
, uap
->mode
);
2839 kern_rmdir(struct nlookupdata
*nd
)
2841 struct namecache
*ncp
;
2845 nd
->nl_flags
|= NLC_DELETE
;
2846 if ((error
= nlookup(nd
)) != 0)
2850 error
= VOP_NRMDIR(ncp
, nd
->nl_cred
);
2855 * rmdir_args(char *path)
2857 * Remove a directory file.
2861 rmdir(struct rmdir_args
*uap
)
2863 struct nlookupdata nd
;
2866 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2868 error
= kern_rmdir(&nd
);
2874 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
2875 enum uio_seg direction
)
2877 struct thread
*td
= curthread
;
2878 struct proc
*p
= td
->td_proc
;
2886 if ((error
= getvnode(p
->p_fd
, fd
, &fp
)) != 0)
2888 if ((fp
->f_flag
& FREAD
) == 0)
2890 vp
= (struct vnode
*)fp
->f_data
;
2892 if (vp
->v_type
!= VDIR
)
2894 aiov
.iov_base
= buf
;
2895 aiov
.iov_len
= count
;
2896 auio
.uio_iov
= &aiov
;
2897 auio
.uio_iovcnt
= 1;
2898 auio
.uio_rw
= UIO_READ
;
2899 auio
.uio_segflg
= direction
;
2901 auio
.uio_resid
= count
;
2902 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2903 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
2904 loff
= auio
.uio_offset
= fp
->f_offset
;
2905 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
2906 fp
->f_offset
= auio
.uio_offset
;
2907 VOP_UNLOCK(vp
, 0, td
);
2910 if (count
== auio
.uio_resid
) {
2911 if (union_dircheckp
) {
2912 error
= union_dircheckp(td
, &vp
, fp
);
2918 if ((vp
->v_flag
& VROOT
) &&
2919 (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
2920 struct vnode
*tvp
= vp
;
2921 vp
= vp
->v_mount
->mnt_vnodecovered
;
2932 *res
= count
- auio
.uio_resid
;
2937 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2939 * Read a block of directory entries in a file system independent format.
2942 getdirentries(struct getdirentries_args
*uap
)
2947 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
2948 &uap
->sysmsg_result
, UIO_USERSPACE
);
2951 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
2956 * getdents_args(int fd, char *buf, size_t count)
2959 getdents(struct getdents_args
*uap
)
2963 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
2964 &uap
->sysmsg_result
, UIO_USERSPACE
);
2970 * umask(int newmask)
2972 * Set the mode mask for creation of filesystem nodes.
2977 umask(struct umask_args
*uap
)
2979 struct thread
*td
= curthread
;
2980 struct proc
*p
= td
->td_proc
;
2981 struct filedesc
*fdp
;
2984 uap
->sysmsg_result
= fdp
->fd_cmask
;
2985 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
2990 * revoke(char *path)
2992 * Void all references to file by ripping underlying filesystem
2997 revoke(struct revoke_args
*uap
)
2999 struct thread
*td
= curthread
;
3000 struct nlookupdata nd
;
3007 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3009 error
= nlookup(&nd
);
3011 error
= cache_vref(nd
.nl_ncp
, nd
.nl_cred
, &vp
);
3012 cred
= crhold(nd
.nl_cred
);
3015 if (vp
->v_type
!= VCHR
&& vp
->v_type
!= VBLK
)
3018 error
= VOP_GETATTR(vp
, &vattr
, td
);
3019 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
3020 error
= suser_cred(cred
, PRISON_ROOT
);
3021 if (error
== 0 && count_udev(vp
->v_udev
) > 0) {
3022 if ((error
= vx_lock(vp
)) == 0) {
3023 VOP_REVOKE(vp
, REVOKEALL
);
3035 * Convert a user file descriptor to a kernel file entry.
3038 getvnode(struct filedesc
*fdp
, int fd
, struct file
**fpp
)
3042 if ((u_int
)fd
>= fdp
->fd_nfiles
||
3043 (fp
= fdp
->fd_files
[fd
].fp
) == NULL
)
3045 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_FIFO
)
3051 * getfh_args(char *fname, fhandle_t *fhp)
3053 * Get (NFS) file handle
3056 getfh(struct getfh_args
*uap
)
3058 struct thread
*td
= curthread
;
3059 struct nlookupdata nd
;
3065 * Must be super user
3067 if ((error
= suser(td
)) != 0)
3071 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
3073 error
= nlookup(&nd
);
3075 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3078 bzero(&fh
, sizeof(fh
));
3079 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
3080 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
3083 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
3089 * fhopen_args(const struct fhandle *u_fhp, int flags)
3091 * syscall for the rpc.lockd to use to translate a NFS file handle into
3092 * an open descriptor.
3094 * warning: do not remove the suser() call or this becomes one giant
3098 fhopen(struct fhopen_args
*uap
)
3100 struct thread
*td
= curthread
;
3101 struct proc
*p
= td
->td_proc
;
3106 struct vattr
*vap
= &vat
;
3108 struct filedesc
*fdp
= p
->p_fd
;
3109 int fmode
, mode
, error
, type
;
3115 * Must be super user
3121 fmode
= FFLAGS(uap
->flags
);
3122 /* why not allow a non-read/write open for our lockd? */
3123 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
3125 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
3128 /* find the mount point */
3129 mp
= vfs_getvfs(&fhp
.fh_fsid
);
3132 /* now give me my vnode, it gets returned to me locked */
3133 error
= VFS_FHTOVP(mp
, &fhp
.fh_fid
, &vp
);
3137 * from now on we have to make sure not
3138 * to forget about the vnode
3139 * any error that causes an abort must vput(vp)
3140 * just set error = err and 'goto bad;'.
3146 if (vp
->v_type
== VLNK
) {
3150 if (vp
->v_type
== VSOCK
) {
3155 if (fmode
& (FWRITE
| O_TRUNC
)) {
3156 if (vp
->v_type
== VDIR
) {
3160 error
= vn_writechk(vp
);
3168 error
= VOP_ACCESS(vp
, mode
, p
->p_ucred
, td
);
3172 if (fmode
& O_TRUNC
) {
3173 VOP_UNLOCK(vp
, 0, td
); /* XXX */
3174 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
3175 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
); /* XXX */
3178 error
= VOP_SETATTR(vp
, vap
, p
->p_ucred
, td
);
3184 * VOP_OPEN needs the file pointer so it can potentially override
3187 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3190 if ((error
= falloc(p
, &nfp
, NULL
)) != 0)
3194 fp
->f_type
= DTYPE_VNODE
;
3195 fp
->f_flag
= fmode
& FMASK
;
3196 fp
->f_ops
= &vnode_fileops
;
3199 error
= VOP_OPEN(vp
, fmode
, p
->p_ucred
, fp
, td
);
3202 * setting f_ops this way prevents VOP_CLOSE from being
3203 * called or fdrop() releasing the vp from v_data. Since
3204 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3206 fp
->f_ops
= &badfileops
;
3215 * The fp now owns a reference on the vnode. We still have our own
3221 * Make sure that a VM object is created for VMIO support. If this
3222 * fails just fdrop() normally to clean up.
3224 if (vn_canvmio(vp
) == TRUE
) {
3225 if ((error
= vfs_object_create(vp
, td
)) != 0) {
3232 * The open was successful, associate it with a file descriptor.
3234 if ((error
= fsetfd(p
, fp
, &indx
)) != 0) {
3241 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
3242 lf
.l_whence
= SEEK_SET
;
3245 if (fmode
& O_EXLOCK
)
3246 lf
.l_type
= F_WRLCK
;
3248 lf
.l_type
= F_RDLCK
;
3250 if ((fmode
& FNONBLOCK
) == 0)
3252 VOP_UNLOCK(vp
, 0, td
);
3253 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
3255 * lock request failed. Normally close the descriptor
3256 * but handle the case where someone might have dup()d
3257 * or close()d it when we weren't looking.
3259 if (fdp
->fd_files
[indx
].fp
== fp
) {
3260 funsetfd(fdp
, indx
);
3265 * release our private reference.
3271 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
3272 fp
->f_flag
|= FHASLOCK
;
3274 if ((vp
->v_type
== VREG
) && (VOP_GETVOBJECT(vp
, NULL
) != 0))
3275 vfs_object_create(vp
, td
);
3279 uap
->sysmsg_result
= indx
;
3288 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3291 fhstat(struct fhstat_args
*uap
)
3293 struct thread
*td
= curthread
;
3301 * Must be super user
3307 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
3311 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3313 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3315 error
= vn_stat(vp
, &sb
, td
);
3319 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
3324 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3327 fhstatfs(struct fhstatfs_args
*uap
)
3329 struct thread
*td
= curthread
;
3330 struct proc
*p
= td
->td_proc
;
3335 char *fullpath
, *freepath
;
3340 * Must be super user
3342 if ((error
= suser(td
)))
3345 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
3348 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3351 if (p
!= NULL
&& (p
->p_fd
->fd_nrdir
->nc_flag
& NCF_ROOT
) == 0 &&
3352 !chroot_visible_mnt(mp
, p
))
3355 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3360 if ((error
= VFS_STATFS(mp
, sp
, td
)) != 0)
3363 error
= cache_fullpath(p
, mp
->mnt_ncp
, &fullpath
, &freepath
);
3366 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
3367 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
3368 free(freepath
, M_TEMP
);
3370 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3372 bcopy(sp
, &sb
, sizeof(sb
));
3373 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
3376 return (copyout(sp
, uap
->buf
, sizeof(*sp
)));
3380 * Syscall to push extended attribute configuration information into the
3381 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3382 * a command (int cmd), and attribute name and misc data. For now, the
3383 * attribute name is left in userspace for consumption by the VFS_op.
3384 * It will probably be changed to be copied into sysspace by the
3385 * syscall in the future, once issues with various consumers of the
3386 * attribute code have raised their hands.
3388 * Currently this is used only by UFS Extended Attributes.
3391 extattrctl(struct extattrctl_args
*uap
)
3393 struct nlookupdata nd
;
3399 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3401 error
= nlookup(&nd
);
3403 mp
= nd
.nl_ncp
->nc_mount
;
3404 error
= VFS_EXTATTRCTL(mp
, uap
->cmd
,
3405 uap
->attrname
, uap
->arg
,
3413 * Syscall to set a named extended attribute on a file or directory.
3414 * Accepts attribute name, and a uio structure pointing to the data to set.
3415 * The uio is consumed in the style of writev(). The real work happens
3416 * in VOP_SETEXTATTR().
3419 extattr_set_file(struct extattr_set_file_args
*uap
)
3421 char attrname
[EXTATTR_MAXNAMELEN
];
3422 struct iovec aiov
[UIO_SMALLIOV
];
3423 struct iovec
*needfree
;
3424 struct nlookupdata nd
;
3433 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3438 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3440 error
= nlookup(&nd
);
3442 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3449 iovlen
= uap
->iovcnt
* sizeof(struct iovec
);
3450 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3451 if (uap
->iovcnt
> UIO_MAXIOV
) {
3455 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3461 auio
.uio_iovcnt
= uap
->iovcnt
;
3462 auio
.uio_rw
= UIO_WRITE
;
3463 auio
.uio_segflg
= UIO_USERSPACE
;
3464 auio
.uio_td
= nd
.nl_td
;
3465 auio
.uio_offset
= 0;
3466 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3469 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3470 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3474 auio
.uio_resid
+= iov
->iov_len
;
3477 cnt
= auio
.uio_resid
;
3478 error
= VOP_SETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
, nd
.nl_td
);
3479 cnt
-= auio
.uio_resid
;
3480 uap
->sysmsg_result
= cnt
;
3485 FREE(needfree
, M_IOV
);
3490 * Syscall to get a named extended attribute on a file or directory.
3491 * Accepts attribute name, and a uio structure pointing to a buffer for the
3492 * data. The uio is consumed in the style of readv(). The real work
3493 * happens in VOP_GETEXTATTR();
3496 extattr_get_file(struct extattr_get_file_args
*uap
)
3498 char attrname
[EXTATTR_MAXNAMELEN
];
3499 struct iovec aiov
[UIO_SMALLIOV
];
3500 struct iovec
*needfree
;
3501 struct nlookupdata nd
;
3510 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3515 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3517 error
= nlookup(&nd
);
3519 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3525 iovlen
= uap
->iovcnt
* sizeof (struct iovec
);
3527 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3528 if (uap
->iovcnt
> UIO_MAXIOV
) {
3532 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3538 auio
.uio_iovcnt
= uap
->iovcnt
;
3539 auio
.uio_rw
= UIO_READ
;
3540 auio
.uio_segflg
= UIO_USERSPACE
;
3541 auio
.uio_td
= nd
.nl_td
;
3542 auio
.uio_offset
= 0;
3543 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3546 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3547 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3551 auio
.uio_resid
+= iov
->iov_len
;
3554 cnt
= auio
.uio_resid
;
3555 error
= VOP_GETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
, nd
.nl_td
);
3556 cnt
-= auio
.uio_resid
;
3557 uap
->sysmsg_result
= cnt
;
3562 FREE(needfree
, M_IOV
);
3567 * Syscall to delete a named extended attribute from a file or directory.
3568 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3571 extattr_delete_file(struct extattr_delete_file_args
*uap
)
3573 char attrname
[EXTATTR_MAXNAMELEN
];
3574 struct nlookupdata nd
;
3578 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3583 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3585 error
= nlookup(&nd
);
3587 error
= cache_vget(nd
.nl_ncp
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3593 error
= VOP_SETEXTATTR(vp
, attrname
, NULL
, nd
.nl_cred
, nd
.nl_td
);
3600 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
3602 struct namecache
*ncp
;
3604 * First check if this file system is below
3608 while (ncp
!= NULL
&& ncp
!= p
->p_fd
->fd_nrdir
)
3609 ncp
= ncp
->nc_parent
;
3612 * This is not below the chroot path.
3614 * Check if the chroot path is on the same filesystem,
3615 * by determing if we have to cross a mount point
3616 * before reaching mp->mnt_ncp.
3618 ncp
= p
->p_fd
->fd_nrdir
;
3619 while (ncp
!= NULL
&& ncp
!= mp
->mnt_ncp
) {
3620 if (ncp
->nc_flag
& NCF_MOUNTPT
) {
3624 ncp
= ncp
->nc_parent
;
3627 return(ncp
!= NULL
);