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 $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
74 #include <sys/mplock2.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount
*mp
, const char *ctl
, ...)
87 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
88 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
89 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
90 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
91 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
92 static int getutimes (const struct timeval
*, struct timespec
*);
93 static int setfown (struct mount
*, struct vnode
*, uid_t
, gid_t
);
94 static int setfmode (struct vnode
*, int);
95 static int setfflags (struct vnode
*, int);
96 static int setutimes (struct vnode
*, struct vattr
*,
97 const struct timespec
*, int);
98 static int usermount
= 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp
) (struct thread
*, struct vnode
**, struct file
*);
102 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0,
103 "Allow non-root users to mount filesystems");
106 * Virtual File System System Calls
110 * Mount a file system.
112 * mount_args(char *type, char *path, int flags, caddr_t data)
117 sys_mount(struct mount_args
*uap
)
119 struct thread
*td
= curthread
;
122 struct mount
*mp
, *nullmp
;
123 struct vfsconf
*vfsp
;
124 int error
, flag
= 0, flag2
= 0;
127 struct nlookupdata nd
;
128 char fstypename
[MFSNAMELEN
];
137 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
141 * Do not allow NFS export by non-root users.
143 if (uap
->flags
& MNT_EXPORTED
) {
144 error
= priv_check(td
, PRIV_ROOT
);
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td
, PRIV_ROOT
))
152 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
155 * Lookup the requested path and extract the nch and vnode.
157 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
159 if ((error
= nlookup(&nd
)) == 0) {
160 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
175 nullmp
= nd
.nl_nch
.mount
;
178 * Extract the locked+refd ncp and cleanup the nd structure
181 cache_zero(&nd
.nl_nch
);
184 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
185 (mp
= cache_findmount(&nch
)) != NULL
) {
194 * now we have the locked ref'd nch and unreferenced vnode.
197 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
204 * Extract the file system type. We need to know this early, to take
205 * appropriate actions if we are dealing with a nullfs.
207 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
214 * Now we have an unlocked ref'd nch and a locked ref'd vp
216 if (uap
->flags
& MNT_UPDATE
) {
217 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
224 if (strncmp(fstypename
, "null", 5) == 0) {
232 flag2
= mp
->mnt_kern_flag
;
234 * We only allow the filesystem to be reloaded if it
235 * is currently mounted read-only.
237 if ((uap
->flags
& MNT_RELOAD
) &&
238 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
241 error
= EOPNOTSUPP
; /* Needs translation */
245 * Only root, or the user that did the original mount is
246 * permitted to update it.
248 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
249 (error
= priv_check(td
, PRIV_ROOT
))) {
254 if (vfs_busy(mp
, LK_NOWAIT
)) {
260 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
267 vsetflags(vp
, VMOUNT
);
269 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
274 * If the user is not root, ensure that they own the directory
275 * onto which we are attempting to mount.
277 if ((error
= VOP_GETATTR(vp
, &va
)) ||
278 (va
.va_uid
!= cred
->cr_uid
&& (error
= priv_check(td
, PRIV_ROOT
)))) {
283 if ((error
= vinvalbuf(vp
, V_SAVE
, 0, 0)) != 0) {
288 if (vp
->v_type
!= VDIR
) {
294 if (vp
->v_mount
->mnt_kern_flag
& MNTK_NOSTKMNT
) {
300 vfsp
= vfsconf_find_by_name(fstypename
);
304 /* Only load modules for root (very important!) */
305 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0) {
310 error
= linker_load_file(fstypename
, &lf
);
311 if (error
|| lf
== NULL
) {
319 /* lookup again, see if the VFS was loaded */
320 vfsp
= vfsconf_find_by_name(fstypename
);
323 linker_file_unload(lf
);
330 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
336 vsetflags(vp
, VMOUNT
);
339 * Allocate and initialize the filesystem.
341 mp
= kmalloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
343 vfs_busy(mp
, LK_NOWAIT
);
344 mp
->mnt_op
= vfsp
->vfc_vfsops
;
346 vfsp
->vfc_refcount
++;
347 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
348 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
349 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
350 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
354 * Set the mount level flags.
356 if (uap
->flags
& MNT_RDONLY
)
357 mp
->mnt_flag
|= MNT_RDONLY
;
358 else if (mp
->mnt_flag
& MNT_RDONLY
)
359 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
360 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
361 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_NOATIME
|
362 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
363 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
364 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
365 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_FORCE
|
366 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
367 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
369 * Mount the filesystem.
370 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
373 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
374 if (mp
->mnt_flag
& MNT_UPDATE
) {
375 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
376 mp
->mnt_flag
&= ~MNT_RDONLY
;
377 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
378 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
381 mp
->mnt_kern_flag
= flag2
;
384 vclrflags(vp
, VMOUNT
);
389 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
407 cache_unlock(&mp
->mnt_ncmountpt
);
409 mp
->mnt_ncmounton
= nch
; /* inherits ref */
410 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
412 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
413 vclrflags(vp
, VMOUNT
);
414 mountlist_insert(mp
, MNTINS_LAST
);
416 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
417 error
= vfs_allocate_syncvnode(mp
);
419 error
= VFS_START(mp
, 0);
422 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
423 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
424 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
425 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
426 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
427 vclrflags(vp
, VMOUNT
);
428 mp
->mnt_vfc
->vfc_refcount
--;
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * The passed ncp is ref'd and locked (from the mount code) and
445 * must be associated with the vnode representing the root of the
448 struct checkdirs_info
{
449 struct nchandle old_nch
;
450 struct nchandle new_nch
;
451 struct vnode
*old_vp
;
452 struct vnode
*new_vp
;
455 static int checkdirs_callback(struct proc
*p
, void *data
);
458 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
460 struct checkdirs_info info
;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp
= old_nch
->ncp
->nc_vp
;
470 if (olddp
== NULL
|| olddp
->v_sysref
.refcnt
== 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp
, &newdp
))
479 panic("mount: lost mount");
480 cache_setunresolved(new_nch
);
481 cache_setvp(new_nch
, newdp
);
484 * Special handling of the root node
486 if (rootvnode
== olddp
) {
488 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
492 * Pass newdp separately so the callback does not have to access
493 * it via new_nch->ncp->nc_vp.
495 info
.old_nch
= *old_nch
;
496 info
.new_nch
= *new_nch
;
498 allproc_scan(checkdirs_callback
, &info
);
503 * NOTE: callback is not MP safe because the scanned process's filedesc
504 * structure can be ripped out from under us, amoung other things.
507 checkdirs_callback(struct proc
*p
, void *data
)
509 struct checkdirs_info
*info
= data
;
510 struct filedesc
*fdp
;
511 struct nchandle ncdrop1
;
512 struct nchandle ncdrop2
;
513 struct vnode
*vprele1
;
514 struct vnode
*vprele2
;
516 if ((fdp
= p
->p_fd
) != NULL
) {
517 cache_zero(&ncdrop1
);
518 cache_zero(&ncdrop2
);
523 * MPUNSAFE - XXX fdp can be pulled out from under a
526 * A shared filedesc is ok, we don't have to copy it
527 * because we are making this change globally.
529 spin_lock(&fdp
->fd_spin
);
530 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
531 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
532 vprele1
= fdp
->fd_cdir
;
534 fdp
->fd_cdir
= info
->new_vp
;
535 ncdrop1
= fdp
->fd_ncdir
;
536 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
538 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
539 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
540 vprele2
= fdp
->fd_rdir
;
542 fdp
->fd_rdir
= info
->new_vp
;
543 ncdrop2
= fdp
->fd_nrdir
;
544 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
546 spin_unlock(&fdp
->fd_spin
);
548 cache_drop(&ncdrop1
);
550 cache_drop(&ncdrop2
);
560 * Unmount a file system.
562 * Note: unmount takes a path to the vnode mounted on as argument,
563 * not special file (as before).
565 * umount_args(char *path, int flags)
570 sys_unmount(struct unmount_args
*uap
)
572 struct thread
*td
= curthread
;
573 struct proc
*p __debugvar
= td
->td_proc
;
574 struct mount
*mp
= NULL
;
575 struct nlookupdata nd
;
580 if (td
->td_ucred
->cr_prison
!= NULL
) {
584 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
587 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
589 error
= nlookup(&nd
);
593 mp
= nd
.nl_nch
.mount
;
596 * Only root, or the user that did the original mount is
597 * permitted to unmount this filesystem.
599 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
600 (error
= priv_check(td
, PRIV_ROOT
)))
604 * Don't allow unmounting the root file system.
606 if (mp
->mnt_flag
& MNT_ROOTFS
) {
612 * Must be the root of the filesystem
614 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
622 error
= dounmount(mp
, uap
->flags
);
629 * Do the actual file system unmount.
632 dounmount_interlock(struct mount
*mp
)
634 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
636 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
641 unmount_allproc_cb(struct proc
*p
, void *arg
)
645 if (p
->p_textnch
.ncp
== NULL
)
648 mp
= (struct mount
*)arg
;
649 if (p
->p_textnch
.mount
== mp
)
650 cache_drop(&p
->p_textnch
);
656 dounmount(struct mount
*mp
, int flags
)
658 struct namecache
*ncp
;
666 lwkt_gettoken(&mntvnode_token
);
668 * Exclusive access for unmounting purposes
670 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
674 * Allow filesystems to detect that a forced unmount is in progress.
676 if (flags
& MNT_FORCE
)
677 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
678 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_NOWAIT
);
679 error
= lockmgr(&mp
->mnt_lock
, lflags
);
681 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
682 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
687 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
688 vfs_setpublicfs(NULL
, NULL
, NULL
);
690 vfs_msync(mp
, MNT_WAIT
);
691 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
692 mp
->mnt_flag
&=~ MNT_ASYNC
;
695 * If this filesystem isn't aliasing other filesystems,
696 * try to invalidate any remaining namecache entries and
697 * check the count afterwords.
699 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
700 cache_lock(&mp
->mnt_ncmountpt
);
701 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
702 cache_unlock(&mp
->mnt_ncmountpt
);
704 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
705 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
706 allproc_scan(&unmount_allproc_cb
, mp
);
709 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
710 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
712 if ((flags
& MNT_FORCE
) == 0) {
714 mount_warning(mp
, "Cannot unmount: "
720 mount_warning(mp
, "Forced unmount: "
731 * nchandle records ref the mount structure. Expect a count of 1
732 * (our mount->mnt_ncmountpt).
734 if (mp
->mnt_refs
!= 1) {
735 if ((flags
& MNT_FORCE
) == 0) {
736 mount_warning(mp
, "Cannot unmount: "
737 "%d process references still "
738 "present", mp
->mnt_refs
);
741 mount_warning(mp
, "Forced unmount: "
742 "%d process references still "
743 "present", mp
->mnt_refs
);
749 * Decomission our special mnt_syncer vnode. This also stops
750 * the vnlru code. If we are unable to unmount we recommission
754 if ((vp
= mp
->mnt_syncer
) != NULL
) {
755 mp
->mnt_syncer
= NULL
;
758 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
759 (error
= VFS_SYNC(mp
, MNT_WAIT
)) == 0) ||
760 (flags
& MNT_FORCE
)) {
761 error
= VFS_UNMOUNT(mp
, flags
);
765 if (mp
->mnt_syncer
== NULL
)
766 vfs_allocate_syncvnode(mp
);
767 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
768 mp
->mnt_flag
|= async_flag
;
769 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
770 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
775 * Clean up any journals still associated with the mount after
776 * filesystem activity has ceased.
778 journal_remove_all_journals(mp
,
779 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
781 mountlist_remove(mp
);
784 * Remove any installed vnode ops here so the individual VFSs don't
787 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
788 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
789 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
790 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
791 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
793 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
794 nch
= mp
->mnt_ncmountpt
;
795 cache_zero(&mp
->mnt_ncmountpt
);
796 cache_clrmountpt(&nch
);
799 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
800 nch
= mp
->mnt_ncmounton
;
801 cache_zero(&mp
->mnt_ncmounton
);
802 cache_clrmountpt(&nch
);
806 mp
->mnt_vfc
->vfc_refcount
--;
807 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
808 panic("unmount: dangling vnode");
809 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
810 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
816 lwkt_reltoken(&mntvnode_token
);
822 mount_warning(struct mount
*mp
, const char *ctl
, ...)
829 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, NULL
,
830 &ptr
, &buf
, 0) == 0) {
831 kprintf("unmount(%s): ", ptr
);
836 kprintf("unmount(%p", mp
);
837 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
838 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
847 * Shim cache_fullpath() to handle the case where a process is chrooted into
848 * a subdirectory of a mount. In this case if the root mount matches the
849 * process root directory's mount we have to specify the process's root
850 * directory instead of the mount point, because the mount point might
851 * be above the root directory.
855 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
857 struct nchandle
*nch
;
859 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
860 nch
= &p
->p_fd
->fd_nrdir
;
862 nch
= &mp
->mnt_ncmountpt
;
863 return(cache_fullpath(p
, nch
, NULL
, rb
, fb
, 0));
867 * Sync each mounted filesystem.
871 static int syncprt
= 0;
872 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
875 static int sync_callback(struct mount
*mp
, void *data
);
878 sys_sync(struct sync_args
*uap
)
880 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
883 * print out buffer pool stat information on each sync() call.
893 sync_callback(struct mount
*mp
, void *data __unused
)
897 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
898 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
899 mp
->mnt_flag
&= ~MNT_ASYNC
;
900 vfs_msync(mp
, MNT_NOWAIT
);
901 VFS_SYNC(mp
, MNT_NOWAIT
);
902 mp
->mnt_flag
|= asyncflag
;
907 /* XXX PRISON: could be per prison flag */
908 static int prison_quotas
;
910 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
914 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
916 * Change filesystem quotas.
921 sys_quotactl(struct quotactl_args
*uap
)
923 struct nlookupdata nd
;
930 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
935 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
937 error
= nlookup(&nd
);
939 mp
= nd
.nl_nch
.mount
;
940 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
941 uap
->arg
, nd
.nl_cred
);
950 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
951 * void *buf, int buflen)
953 * This function operates on a mount point and executes the specified
954 * operation using the specified control data, and possibly returns data.
956 * The actual number of bytes stored in the result buffer is returned, 0
957 * if none, otherwise an error is returned.
962 sys_mountctl(struct mountctl_args
*uap
)
964 struct thread
*td
= curthread
;
965 struct proc
*p
= td
->td_proc
;
973 * Sanity and permissions checks. We must be root.
976 if (td
->td_ucred
->cr_prison
!= NULL
)
978 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
979 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
983 * Argument length checks
985 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
987 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
989 if (uap
->path
== NULL
)
993 * Allocate the necessary buffers and copyin data
995 path
= objcache_get(namei_oc
, M_WAITOK
);
996 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1001 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1002 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1007 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1010 * Validate the descriptor
1013 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1023 * Execute the internal kernel function and clean up.
1026 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1030 if (error
== 0 && uap
->sysmsg_result
> 0)
1031 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1034 objcache_put(namei_oc
, path
);
1043 * Execute a mount control operation by resolving the path to a mount point
1044 * and calling vop_mountctl().
1046 * Use the mount point from the nch instead of the vnode so nullfs mounts
1047 * can properly spike the VOP.
1050 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1051 const void *ctl
, int ctllen
,
1052 void *buf
, int buflen
, int *res
)
1056 struct nlookupdata nd
;
1061 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1063 error
= nlookup(&nd
);
1065 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1066 mp
= nd
.nl_nch
.mount
;
1073 * Must be the root of the filesystem
1075 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1079 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1086 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1088 struct thread
*td
= curthread
;
1089 struct proc
*p
= td
->td_proc
;
1092 char *fullpath
, *freepath
;
1095 if ((error
= nlookup(nd
)) != 0)
1097 mp
= nd
->nl_nch
.mount
;
1099 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1102 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1105 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1106 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1107 kfree(freepath
, M_TEMP
);
1109 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1110 bcopy(sp
, buf
, sizeof(*buf
));
1111 /* Only root should have access to the fsid's. */
1112 if (priv_check(td
, PRIV_ROOT
))
1113 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1118 * statfs_args(char *path, struct statfs *buf)
1120 * Get filesystem statistics.
1123 sys_statfs(struct statfs_args
*uap
)
1125 struct nlookupdata nd
;
1129 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1131 error
= kern_statfs(&nd
, &buf
);
1134 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1139 kern_fstatfs(int fd
, struct statfs
*buf
)
1141 struct thread
*td
= curthread
;
1142 struct proc
*p
= td
->td_proc
;
1146 char *fullpath
, *freepath
;
1150 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1154 * Try to use mount info from any overlays rather than the
1155 * mount info for the underlying vnode, otherwise we will
1156 * fail when operating on null-mounted paths inside a chroot.
1158 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1159 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1164 if (fp
->f_cred
== NULL
) {
1169 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1172 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1174 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1175 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1176 kfree(freepath
, M_TEMP
);
1178 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1179 bcopy(sp
, buf
, sizeof(*buf
));
1181 /* Only root should have access to the fsid's. */
1182 if (priv_check(td
, PRIV_ROOT
))
1183 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1191 * fstatfs_args(int fd, struct statfs *buf)
1193 * Get filesystem statistics.
1196 sys_fstatfs(struct fstatfs_args
*uap
)
1201 error
= kern_fstatfs(uap
->fd
, &buf
);
1204 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1209 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1215 if ((error
= nlookup(nd
)) != 0)
1217 mp
= nd
->nl_nch
.mount
;
1218 sp
= &mp
->mnt_vstat
;
1219 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1223 if (mp
->mnt_flag
& MNT_RDONLY
)
1224 sp
->f_flag
|= ST_RDONLY
;
1225 if (mp
->mnt_flag
& MNT_NOSUID
)
1226 sp
->f_flag
|= ST_NOSUID
;
1227 bcopy(sp
, buf
, sizeof(*buf
));
1232 * statfs_args(char *path, struct statfs *buf)
1234 * Get filesystem statistics.
1237 sys_statvfs(struct statvfs_args
*uap
)
1239 struct nlookupdata nd
;
1243 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1245 error
= kern_statvfs(&nd
, &buf
);
1248 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1253 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1255 struct thread
*td
= curthread
;
1256 struct proc
*p
= td
->td_proc
;
1263 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1265 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1266 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1271 if (fp
->f_cred
== NULL
) {
1275 sp
= &mp
->mnt_vstat
;
1276 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1280 if (mp
->mnt_flag
& MNT_RDONLY
)
1281 sp
->f_flag
|= ST_RDONLY
;
1282 if (mp
->mnt_flag
& MNT_NOSUID
)
1283 sp
->f_flag
|= ST_NOSUID
;
1285 bcopy(sp
, buf
, sizeof(*buf
));
1293 * fstatfs_args(int fd, struct statfs *buf)
1295 * Get filesystem statistics.
1298 sys_fstatvfs(struct fstatvfs_args
*uap
)
1303 error
= kern_fstatvfs(uap
->fd
, &buf
);
1306 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1311 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1313 * Get statistics on all filesystems.
1316 struct getfsstat_info
{
1317 struct statfs
*sfsp
;
1325 static int getfsstat_callback(struct mount
*, void *);
1328 sys_getfsstat(struct getfsstat_args
*uap
)
1330 struct thread
*td
= curthread
;
1331 struct getfsstat_info info
;
1333 bzero(&info
, sizeof(info
));
1335 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1336 info
.sfsp
= uap
->buf
;
1338 info
.flags
= uap
->flags
;
1341 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1342 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1343 uap
->sysmsg_result
= info
.maxcount
;
1345 uap
->sysmsg_result
= info
.count
;
1346 return (info
.error
);
1350 getfsstat_callback(struct mount
*mp
, void *data
)
1352 struct getfsstat_info
*info
= data
;
1358 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1359 if (info
->td
->td_proc
&&
1360 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1366 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1367 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1368 * overrides MNT_WAIT.
1370 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1371 (info
->flags
& MNT_WAIT
)) &&
1372 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1375 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1377 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1379 info
->error
= error
;
1382 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1383 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1384 kfree(freepath
, M_TEMP
);
1386 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1388 info
->error
= error
;
1398 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1399 long bufsize, int flags)
1401 * Get statistics on all filesystems.
1404 struct getvfsstat_info
{
1405 struct statfs
*sfsp
;
1406 struct statvfs
*vsfsp
;
1414 static int getvfsstat_callback(struct mount
*, void *);
1417 sys_getvfsstat(struct getvfsstat_args
*uap
)
1419 struct thread
*td
= curthread
;
1420 struct getvfsstat_info info
;
1422 bzero(&info
, sizeof(info
));
1424 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1425 info
.sfsp
= uap
->buf
;
1426 info
.vsfsp
= uap
->vbuf
;
1428 info
.flags
= uap
->flags
;
1431 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1432 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1433 uap
->sysmsg_result
= info
.maxcount
;
1435 uap
->sysmsg_result
= info
.count
;
1436 return (info
.error
);
1440 getvfsstat_callback(struct mount
*mp
, void *data
)
1442 struct getvfsstat_info
*info
= data
;
1444 struct statvfs
*vsp
;
1449 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1450 if (info
->td
->td_proc
&&
1451 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1455 vsp
= &mp
->mnt_vstat
;
1458 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1459 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1460 * overrides MNT_WAIT.
1462 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1463 (info
->flags
& MNT_WAIT
)) &&
1464 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1467 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1469 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1470 (info
->flags
& MNT_WAIT
)) &&
1471 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1475 if (mp
->mnt_flag
& MNT_RDONLY
)
1476 vsp
->f_flag
|= ST_RDONLY
;
1477 if (mp
->mnt_flag
& MNT_NOSUID
)
1478 vsp
->f_flag
|= ST_NOSUID
;
1480 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1482 info
->error
= error
;
1485 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1486 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1487 kfree(freepath
, M_TEMP
);
1489 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1491 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1493 info
->error
= error
;
1505 * fchdir_args(int fd)
1507 * Change current working directory to a given file descriptor.
1510 sys_fchdir(struct fchdir_args
*uap
)
1512 struct thread
*td
= curthread
;
1513 struct proc
*p
= td
->td_proc
;
1514 struct filedesc
*fdp
= p
->p_fd
;
1515 struct vnode
*vp
, *ovp
;
1518 struct nchandle nch
, onch
, tnch
;
1521 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1523 lwkt_gettoken(&p
->p_token
);
1524 vp
= (struct vnode
*)fp
->f_data
;
1526 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1527 if (fp
->f_nchandle
.ncp
== NULL
)
1530 error
= checkvp_chdir(vp
, td
);
1535 cache_copy(&fp
->f_nchandle
, &nch
);
1538 * If the ncp has become a mount point, traverse through
1542 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1543 (mp
= cache_findmount(&nch
)) != NULL
1545 error
= nlookup_mp(mp
, &tnch
);
1547 cache_unlock(&tnch
); /* leave ref intact */
1549 vp
= tnch
.ncp
->nc_vp
;
1550 error
= vget(vp
, LK_SHARED
);
1551 KKASSERT(error
== 0);
1555 cache_dropmount(mp
);
1559 onch
= fdp
->fd_ncdir
;
1560 vn_unlock(vp
); /* leave ref intact */
1562 fdp
->fd_ncdir
= nch
;
1571 lwkt_reltoken(&p
->p_token
);
1576 kern_chdir(struct nlookupdata
*nd
)
1578 struct thread
*td
= curthread
;
1579 struct proc
*p
= td
->td_proc
;
1580 struct filedesc
*fdp
= p
->p_fd
;
1581 struct vnode
*vp
, *ovp
;
1582 struct nchandle onch
;
1585 if ((error
= nlookup(nd
)) != 0)
1587 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1589 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1592 lwkt_gettoken(&p
->p_token
);
1593 error
= checkvp_chdir(vp
, td
);
1597 onch
= fdp
->fd_ncdir
;
1598 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1599 fdp
->fd_ncdir
= nd
->nl_nch
;
1603 cache_zero(&nd
->nl_nch
);
1607 lwkt_reltoken(&p
->p_token
);
1612 * chdir_args(char *path)
1614 * Change current working directory (``.'').
1617 sys_chdir(struct chdir_args
*uap
)
1619 struct nlookupdata nd
;
1622 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1624 error
= kern_chdir(&nd
);
1630 * Helper function for raised chroot(2) security function: Refuse if
1631 * any filedescriptors are open directories.
1634 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1641 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1642 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1644 vp
= (struct vnode
*)fp
->f_data
;
1645 if (vp
->v_type
!= VDIR
) {
1656 * This sysctl determines if we will allow a process to chroot(2) if it
1657 * has a directory open:
1658 * 0: disallowed for all processes.
1659 * 1: allowed for processes that were not already chroot(2)'ed.
1660 * 2: allowed for all processes.
1663 static int chroot_allow_open_directories
= 1;
1665 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1666 &chroot_allow_open_directories
, 0, "");
1669 * chroot to the specified namecache entry. We obtain the vp from the
1670 * namecache data. The passed ncp must be locked and referenced and will
1671 * remain locked and referenced on return.
1674 kern_chroot(struct nchandle
*nch
)
1676 struct thread
*td
= curthread
;
1677 struct proc
*p
= td
->td_proc
;
1678 struct filedesc
*fdp
= p
->p_fd
;
1683 * Only privileged user can chroot
1685 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1690 * Disallow open directory descriptors (fchdir() breakouts).
1692 if (chroot_allow_open_directories
== 0 ||
1693 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1694 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1697 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1700 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1704 * Check the validity of vp as a directory to change to and
1705 * associate it with rdir/jdir.
1707 error
= checkvp_chdir(vp
, td
);
1708 vn_unlock(vp
); /* leave reference intact */
1710 vrele(fdp
->fd_rdir
);
1711 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1712 cache_drop(&fdp
->fd_nrdir
);
1713 cache_copy(nch
, &fdp
->fd_nrdir
);
1714 if (fdp
->fd_jdir
== NULL
) {
1717 cache_copy(nch
, &fdp
->fd_njdir
);
1726 * chroot_args(char *path)
1728 * Change notion of root (``/'') directory.
1731 sys_chroot(struct chroot_args
*uap
)
1733 struct thread
*td __debugvar
= curthread
;
1734 struct nlookupdata nd
;
1737 KKASSERT(td
->td_proc
);
1738 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1740 nd
.nl_flags
|= NLC_EXEC
;
1741 error
= nlookup(&nd
);
1743 error
= kern_chroot(&nd
.nl_nch
);
1750 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1752 struct thread
*td
= curthread
;
1753 struct nlookupdata nd
;
1754 struct nchandle
*nch
;
1758 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1762 error
= nlookup(&nd
);
1768 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1772 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1777 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1780 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1782 vfs_cache_setroot(vp
, cache_hold(nch
));
1792 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1793 * determine whether it is legal to chdir to the vnode. The vnode's state
1794 * is not changed by this call.
1797 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1801 if (vp
->v_type
!= VDIR
)
1804 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1809 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1811 struct thread
*td
= curthread
;
1812 struct proc
*p
= td
->td_proc
;
1813 struct lwp
*lp
= td
->td_lwp
;
1814 struct filedesc
*fdp
= p
->p_fd
;
1819 int type
, indx
, error
= 0;
1822 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1824 flags
= FFLAGS(oflags
);
1825 error
= falloc(lp
, &nfp
, NULL
);
1829 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1832 * XXX p_dupfd is a real mess. It allows a device to return a
1833 * file descriptor to be duplicated rather then doing the open
1839 * Call vn_open() to do the lookup and assign the vnode to the
1840 * file pointer. vn_open() does not change the ref count on fp
1841 * and the vnode, on success, will be inherited by the file pointer
1844 nd
->nl_flags
|= NLC_LOCKVP
;
1845 error
= vn_open(nd
, fp
, flags
, cmode
);
1849 * handle special fdopen() case. bleh. dupfdopen() is
1850 * responsible for dropping the old contents of ofiles[indx]
1853 * Note that fsetfd() will add a ref to fp which represents
1854 * the fd_files[] assignment. We must still drop our
1857 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1858 if (fdalloc(p
, 0, &indx
) == 0) {
1859 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1862 fdrop(fp
); /* our ref */
1865 fsetfd(fdp
, NULL
, indx
);
1868 fdrop(fp
); /* our ref */
1869 if (error
== ERESTART
)
1875 * ref the vnode for ourselves so it can't be ripped out from under
1876 * is. XXX need an ND flag to request that the vnode be returned
1879 * Reserve a file descriptor but do not assign it until the open
1882 vp
= (struct vnode
*)fp
->f_data
;
1884 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1891 * If no error occurs the vp will have been assigned to the file
1896 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1897 lf
.l_whence
= SEEK_SET
;
1900 if (flags
& O_EXLOCK
)
1901 lf
.l_type
= F_WRLCK
;
1903 lf
.l_type
= F_RDLCK
;
1904 if (flags
& FNONBLOCK
)
1909 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1911 * lock request failed. Clean up the reserved
1915 fsetfd(fdp
, NULL
, indx
);
1919 fp
->f_flag
|= FHASLOCK
;
1923 * Assert that all regular file vnodes were created with a object.
1925 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1926 ("open: regular file has no backing object after vn_open"));
1932 * release our private reference, leaving the one associated with the
1933 * descriptor table intact.
1935 fsetfd(fdp
, fp
, indx
);
1938 if (oflags
& O_CLOEXEC
)
1939 error
= fsetfdflags(fdp
, *res
, UF_EXCLOSE
);
1944 * open_args(char *path, int flags, int mode)
1946 * Check permissions, allocate an open file structure,
1947 * and call the device open routine if any.
1950 sys_open(struct open_args
*uap
)
1952 struct nlookupdata nd
;
1955 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1957 error
= kern_open(&nd
, uap
->flags
,
1958 uap
->mode
, &uap
->sysmsg_result
);
1965 * openat_args(int fd, char *path, int flags, int mode)
1968 sys_openat(struct openat_args
*uap
)
1970 struct nlookupdata nd
;
1974 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
1976 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
1977 &uap
->sysmsg_result
);
1979 nlookup_done_at(&nd
, fp
);
1984 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
1986 struct thread
*td
= curthread
;
1987 struct proc
*p
= td
->td_proc
;
1996 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1997 vattr
.va_rmajor
= rmajor
;
1998 vattr
.va_rminor
= rminor
;
2000 switch (mode
& S_IFMT
) {
2001 case S_IFMT
: /* used by badsect to flag bad sectors */
2002 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2003 vattr
.va_type
= VBAD
;
2006 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2007 vattr
.va_type
= VCHR
;
2010 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2011 vattr
.va_type
= VBLK
;
2014 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2017 case S_IFDIR
: /* special directories support for HAMMER */
2018 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2019 vattr
.va_type
= VDIR
;
2030 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2031 if ((error
= nlookup(nd
)) != 0)
2033 if (nd
->nl_nch
.ncp
->nc_vp
)
2035 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2039 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2040 nd
->nl_cred
, NAMEI_CREATE
);
2043 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2044 &vp
, nd
->nl_cred
, &vattr
);
2052 * mknod_args(char *path, int mode, int dev)
2054 * Create a special file.
2057 sys_mknod(struct mknod_args
*uap
)
2059 struct nlookupdata nd
;
2062 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2064 error
= kern_mknod(&nd
, uap
->mode
,
2065 umajor(uap
->dev
), uminor(uap
->dev
));
2072 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2074 * Create a special file. The path is relative to the directory associated
2078 sys_mknodat(struct mknodat_args
*uap
)
2080 struct nlookupdata nd
;
2084 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2086 error
= kern_mknod(&nd
, uap
->mode
,
2087 umajor(uap
->dev
), uminor(uap
->dev
));
2089 nlookup_done_at(&nd
, fp
);
2094 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2096 struct thread
*td
= curthread
;
2097 struct proc
*p
= td
->td_proc
;
2104 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2105 if ((error
= nlookup(nd
)) != 0)
2107 if (nd
->nl_nch
.ncp
->nc_vp
)
2109 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2113 vattr
.va_type
= VFIFO
;
2114 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2116 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2123 * mkfifo_args(char *path, int mode)
2125 * Create a named pipe.
2128 sys_mkfifo(struct mkfifo_args
*uap
)
2130 struct nlookupdata nd
;
2133 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2135 error
= kern_mkfifo(&nd
, uap
->mode
);
2141 * mkfifoat_args(int fd, char *path, mode_t mode)
2143 * Create a named pipe. The path is relative to the directory associated
2147 sys_mkfifoat(struct mkfifoat_args
*uap
)
2149 struct nlookupdata nd
;
2153 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2155 error
= kern_mkfifo(&nd
, uap
->mode
);
2156 nlookup_done_at(&nd
, fp
);
2160 static int hardlink_check_uid
= 0;
2161 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2162 &hardlink_check_uid
, 0,
2163 "Unprivileged processes cannot create hard links to files owned by other "
2165 static int hardlink_check_gid
= 0;
2166 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2167 &hardlink_check_gid
, 0,
2168 "Unprivileged processes cannot create hard links to files owned by other "
2172 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2178 * Shortcut if disabled
2180 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2184 * Privileged user can always hardlink
2186 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2190 * Otherwise only if the originating file is owned by the
2191 * same user or group. Note that any group is allowed if
2192 * the file is owned by the caller.
2194 error
= VOP_GETATTR(vp
, &va
);
2198 if (hardlink_check_uid
) {
2199 if (cred
->cr_uid
!= va
.va_uid
)
2203 if (hardlink_check_gid
) {
2204 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2212 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2214 struct thread
*td
= curthread
;
2219 * Lookup the source and obtained a locked vnode.
2221 * You may only hardlink a file which you have write permission
2222 * on or which you own.
2224 * XXX relookup on vget failure / race ?
2227 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2228 if ((error
= nlookup(nd
)) != 0)
2230 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2231 KKASSERT(vp
!= NULL
);
2232 if (vp
->v_type
== VDIR
)
2233 return (EPERM
); /* POSIX */
2234 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2236 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2240 * Unlock the source so we can lookup the target without deadlocking
2241 * (XXX vp is locked already, possible other deadlock?). The target
2244 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2245 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2246 cache_unlock(&nd
->nl_nch
);
2249 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2250 if ((error
= nlookup(linknd
)) != 0) {
2254 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2258 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
2264 * Finally run the new API VOP.
2266 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2268 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2269 vp
, linknd
->nl_cred
);
2276 * link_args(char *path, char *link)
2278 * Make a hard file link.
2281 sys_link(struct link_args
*uap
)
2283 struct nlookupdata nd
, linknd
;
2286 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2288 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2290 error
= kern_link(&nd
, &linknd
);
2291 nlookup_done(&linknd
);
2298 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2300 * Make a hard file link. The path1 argument is relative to the directory
2301 * associated with fd1, and similarly the path2 argument is relative to
2302 * the directory associated with fd2.
2305 sys_linkat(struct linkat_args
*uap
)
2307 struct nlookupdata nd
, linknd
;
2308 struct file
*fp1
, *fp2
;
2311 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2312 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2314 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2315 uap
->path2
, UIO_USERSPACE
, 0);
2317 error
= kern_link(&nd
, &linknd
);
2318 nlookup_done_at(&linknd
, fp2
);
2320 nlookup_done_at(&nd
, fp1
);
2325 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2333 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2334 if ((error
= nlookup(nd
)) != 0)
2336 if (nd
->nl_nch
.ncp
->nc_vp
)
2338 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2342 vattr
.va_mode
= mode
;
2343 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2350 * symlink(char *path, char *link)
2352 * Make a symbolic link.
2355 sys_symlink(struct symlink_args
*uap
)
2357 struct thread
*td
= curthread
;
2358 struct nlookupdata nd
;
2363 path
= objcache_get(namei_oc
, M_WAITOK
);
2364 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2366 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2368 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2369 error
= kern_symlink(&nd
, path
, mode
);
2373 objcache_put(namei_oc
, path
);
2378 * symlinkat_args(char *path1, int fd, char *path2)
2380 * Make a symbolic link. The path2 argument is relative to the directory
2381 * associated with fd.
2384 sys_symlinkat(struct symlinkat_args
*uap
)
2386 struct thread
*td
= curthread
;
2387 struct nlookupdata nd
;
2393 path1
= objcache_get(namei_oc
, M_WAITOK
);
2394 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2396 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2399 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2400 error
= kern_symlink(&nd
, path1
, mode
);
2402 nlookup_done_at(&nd
, fp
);
2404 objcache_put(namei_oc
, path1
);
2409 * undelete_args(char *path)
2411 * Delete a whiteout from the filesystem.
2414 sys_undelete(struct undelete_args
*uap
)
2416 struct nlookupdata nd
;
2419 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2421 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2423 error
= nlookup(&nd
);
2425 error
= ncp_writechk(&nd
.nl_nch
);
2427 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2435 kern_unlink(struct nlookupdata
*nd
)
2440 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2441 if ((error
= nlookup(nd
)) != 0)
2443 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2445 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2450 * unlink_args(char *path)
2452 * Delete a name from the filesystem.
2455 sys_unlink(struct unlink_args
*uap
)
2457 struct nlookupdata nd
;
2460 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2462 error
= kern_unlink(&nd
);
2469 * unlinkat_args(int fd, char *path, int flags)
2471 * Delete the file or directory entry pointed to by fd/path.
2474 sys_unlinkat(struct unlinkat_args
*uap
)
2476 struct nlookupdata nd
;
2480 if (uap
->flags
& ~AT_REMOVEDIR
)
2483 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2485 if (uap
->flags
& AT_REMOVEDIR
)
2486 error
= kern_rmdir(&nd
);
2488 error
= kern_unlink(&nd
);
2490 nlookup_done_at(&nd
, fp
);
2495 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2497 struct thread
*td
= curthread
;
2498 struct proc
*p
= td
->td_proc
;
2505 fp
= holdfp(p
->p_fd
, fd
, -1);
2508 if (fp
->f_type
!= DTYPE_VNODE
) {
2512 vp
= (struct vnode
*)fp
->f_data
;
2516 spin_lock(&fp
->f_spin
);
2517 new_offset
= fp
->f_offset
+ offset
;
2521 error
= VOP_GETATTR(vp
, &vattr
);
2522 spin_lock(&fp
->f_spin
);
2523 new_offset
= offset
+ vattr
.va_size
;
2526 new_offset
= offset
;
2528 spin_lock(&fp
->f_spin
);
2533 spin_lock(&fp
->f_spin
);
2538 * Validate the seek position. Negative offsets are not allowed
2539 * for regular files or directories.
2541 * Normally we would also not want to allow negative offsets for
2542 * character and block-special devices. However kvm addresses
2543 * on 64 bit architectures might appear to be negative and must
2547 if (new_offset
< 0 &&
2548 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2551 fp
->f_offset
= new_offset
;
2554 *res
= fp
->f_offset
;
2555 spin_unlock(&fp
->f_spin
);
2562 * lseek_args(int fd, int pad, off_t offset, int whence)
2564 * Reposition read/write file offset.
2567 sys_lseek(struct lseek_args
*uap
)
2571 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2572 &uap
->sysmsg_offset
);
2578 * Check if current process can access given file. amode is a bitmask of *_OK
2579 * access bits. flags is a bitmask of AT_* flags.
2582 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2587 if (flags
& ~AT_EACCESS
)
2589 if ((error
= nlookup(nd
)) != 0)
2592 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2596 /* Flags == 0 means only check for existence. */
2605 if ((mode
& VWRITE
) == 0 ||
2606 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2607 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2610 * If the file handle is stale we have to re-resolve the
2611 * entry. This is a hack at the moment.
2613 if (error
== ESTALE
) {
2615 cache_setunresolved(&nd
->nl_nch
);
2616 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2629 * access_args(char *path, int flags)
2631 * Check access permissions.
2634 sys_access(struct access_args
*uap
)
2636 struct nlookupdata nd
;
2639 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2641 error
= kern_access(&nd
, uap
->flags
, 0);
2648 * eaccess_args(char *path, int flags)
2650 * Check access permissions.
2653 sys_eaccess(struct eaccess_args
*uap
)
2655 struct nlookupdata nd
;
2658 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2660 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2667 * faccessat_args(int fd, char *path, int amode, int flags)
2669 * Check access permissions.
2672 sys_faccessat(struct faccessat_args
*uap
)
2674 struct nlookupdata nd
;
2678 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2681 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2682 nlookup_done_at(&nd
, fp
);
2688 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2693 if ((error
= nlookup(nd
)) != 0)
2696 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2699 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2701 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2704 * If the file handle is stale we have to re-resolve the entry. This
2705 * is a hack at the moment.
2707 if (error
== ESTALE
) {
2709 cache_setunresolved(&nd
->nl_nch
);
2710 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2720 * stat_args(char *path, struct stat *ub)
2722 * Get file status; this version follows links.
2725 sys_stat(struct stat_args
*uap
)
2727 struct nlookupdata nd
;
2731 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2733 error
= kern_stat(&nd
, &st
);
2735 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2742 * lstat_args(char *path, struct stat *ub)
2744 * Get file status; this version does not follow links.
2747 sys_lstat(struct lstat_args
*uap
)
2749 struct nlookupdata nd
;
2753 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2755 error
= kern_stat(&nd
, &st
);
2757 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2764 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2766 * Get status of file pointed to by fd/path.
2769 sys_fstatat(struct fstatat_args
*uap
)
2771 struct nlookupdata nd
;
2777 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2780 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2782 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2783 UIO_USERSPACE
, flags
);
2785 error
= kern_stat(&nd
, &st
);
2787 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2789 nlookup_done_at(&nd
, fp
);
2794 * pathconf_Args(char *path, int name)
2796 * Get configurable pathname variables.
2799 sys_pathconf(struct pathconf_args
*uap
)
2801 struct nlookupdata nd
;
2806 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2808 error
= nlookup(&nd
);
2810 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2813 error
= VOP_PATHCONF(vp
, uap
->name
, &uap
->sysmsg_reg
);
2821 * kern_readlink isn't properly split yet. There is a copyin burried
2822 * in VOP_READLINK().
2825 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2827 struct thread
*td
= curthread
;
2833 if ((error
= nlookup(nd
)) != 0)
2835 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2838 if (vp
->v_type
!= VLNK
) {
2841 aiov
.iov_base
= buf
;
2842 aiov
.iov_len
= count
;
2843 auio
.uio_iov
= &aiov
;
2844 auio
.uio_iovcnt
= 1;
2845 auio
.uio_offset
= 0;
2846 auio
.uio_rw
= UIO_READ
;
2847 auio
.uio_segflg
= UIO_USERSPACE
;
2849 auio
.uio_resid
= count
;
2850 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
2853 *res
= count
- auio
.uio_resid
;
2858 * readlink_args(char *path, char *buf, int count)
2860 * Return target name of a symbolic link.
2863 sys_readlink(struct readlink_args
*uap
)
2865 struct nlookupdata nd
;
2868 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2870 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2871 &uap
->sysmsg_result
);
2878 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2880 * Return target name of a symbolic link. The path is relative to the
2881 * directory associated with fd.
2884 sys_readlinkat(struct readlinkat_args
*uap
)
2886 struct nlookupdata nd
;
2890 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2892 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
2893 &uap
->sysmsg_result
);
2895 nlookup_done_at(&nd
, fp
);
2900 setfflags(struct vnode
*vp
, int flags
)
2902 struct thread
*td
= curthread
;
2907 * Prevent non-root users from setting flags on devices. When
2908 * a device is reused, users can retain ownership of the device
2909 * if they are allowed to set flags and programs assume that
2910 * chown can't fail when done as root.
2912 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2913 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
2917 * note: vget is required for any operation that might mod the vnode
2918 * so VINACTIVE is properly cleared.
2920 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2922 vattr
.va_flags
= flags
;
2923 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
2930 * chflags(char *path, int flags)
2932 * Change flags of a file given a path name.
2935 sys_chflags(struct chflags_args
*uap
)
2937 struct nlookupdata nd
;
2942 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2944 error
= nlookup(&nd
);
2946 error
= ncp_writechk(&nd
.nl_nch
);
2948 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2951 error
= setfflags(vp
, uap
->flags
);
2958 * lchflags(char *path, int flags)
2960 * Change flags of a file given a path name, but don't follow symlinks.
2963 sys_lchflags(struct lchflags_args
*uap
)
2965 struct nlookupdata nd
;
2970 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2972 error
= nlookup(&nd
);
2974 error
= ncp_writechk(&nd
.nl_nch
);
2976 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2979 error
= setfflags(vp
, uap
->flags
);
2986 * fchflags_args(int fd, int flags)
2988 * Change flags of a file given a file descriptor.
2991 sys_fchflags(struct fchflags_args
*uap
)
2993 struct thread
*td
= curthread
;
2994 struct proc
*p
= td
->td_proc
;
2998 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3000 if (fp
->f_nchandle
.ncp
)
3001 error
= ncp_writechk(&fp
->f_nchandle
);
3003 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3009 setfmode(struct vnode
*vp
, int mode
)
3011 struct thread
*td
= curthread
;
3016 * note: vget is required for any operation that might mod the vnode
3017 * so VINACTIVE is properly cleared.
3019 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3021 vattr
.va_mode
= mode
& ALLPERMS
;
3022 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3029 kern_chmod(struct nlookupdata
*nd
, int mode
)
3034 if ((error
= nlookup(nd
)) != 0)
3036 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3038 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3039 error
= setfmode(vp
, mode
);
3045 * chmod_args(char *path, int mode)
3047 * Change mode of a file given path name.
3050 sys_chmod(struct chmod_args
*uap
)
3052 struct nlookupdata nd
;
3055 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3057 error
= kern_chmod(&nd
, uap
->mode
);
3063 * lchmod_args(char *path, int mode)
3065 * Change mode of a file given path name (don't follow links.)
3068 sys_lchmod(struct lchmod_args
*uap
)
3070 struct nlookupdata nd
;
3073 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3075 error
= kern_chmod(&nd
, uap
->mode
);
3081 * fchmod_args(int fd, int mode)
3083 * Change mode of a file given a file descriptor.
3086 sys_fchmod(struct fchmod_args
*uap
)
3088 struct thread
*td
= curthread
;
3089 struct proc
*p
= td
->td_proc
;
3093 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3095 if (fp
->f_nchandle
.ncp
)
3096 error
= ncp_writechk(&fp
->f_nchandle
);
3098 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3104 * fchmodat_args(char *path, int mode)
3106 * Change mode of a file pointed to by fd/path.
3109 sys_fchmodat(struct fchmodat_args
*uap
)
3111 struct nlookupdata nd
;
3116 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3118 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3120 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3121 UIO_USERSPACE
, flags
);
3123 error
= kern_chmod(&nd
, uap
->mode
);
3124 nlookup_done_at(&nd
, fp
);
3129 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3131 struct thread
*td
= curthread
;
3139 * note: vget is required for any operation that might mod the vnode
3140 * so VINACTIVE is properly cleared.
3142 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3143 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3145 o_uid
= vattr
.va_uid
;
3146 o_gid
= vattr
.va_gid
;
3147 size
= vattr
.va_size
;
3152 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3161 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3162 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3169 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3174 if ((error
= nlookup(nd
)) != 0)
3176 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3178 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3179 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3185 * chown(char *path, int uid, int gid)
3187 * Set ownership given a path name.
3190 sys_chown(struct chown_args
*uap
)
3192 struct nlookupdata nd
;
3195 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3197 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3203 * lchown_args(char *path, int uid, int gid)
3205 * Set ownership given a path name, do not cross symlinks.
3208 sys_lchown(struct lchown_args
*uap
)
3210 struct nlookupdata nd
;
3213 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3215 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3221 * fchown_args(int fd, int uid, int gid)
3223 * Set ownership given a file descriptor.
3226 sys_fchown(struct fchown_args
*uap
)
3228 struct thread
*td
= curthread
;
3229 struct proc
*p
= td
->td_proc
;
3233 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3235 if (fp
->f_nchandle
.ncp
)
3236 error
= ncp_writechk(&fp
->f_nchandle
);
3238 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3239 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3245 * fchownat(int fd, char *path, int uid, int gid, int flags)
3247 * Set ownership of file pointed to by fd/path.
3250 sys_fchownat(struct fchownat_args
*uap
)
3252 struct nlookupdata nd
;
3257 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3259 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3261 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3262 UIO_USERSPACE
, flags
);
3264 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3265 nlookup_done_at(&nd
, fp
);
3271 getutimes(const struct timeval
*tvp
, struct timespec
*tsp
)
3273 struct timeval tv
[2];
3277 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3280 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3281 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3287 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3288 const struct timespec
*ts
, int nullflag
)
3290 struct thread
*td
= curthread
;
3294 vattr
->va_atime
= ts
[0];
3295 vattr
->va_mtime
= ts
[1];
3297 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3298 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3304 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3306 struct timespec ts
[2];
3311 if ((error
= getutimes(tptr
, ts
)) != 0)
3315 * NOTE: utimes() succeeds for the owner even if the file
3316 * is not user-writable.
3318 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3320 if ((error
= nlookup(nd
)) != 0)
3322 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3324 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3328 * note: vget is required for any operation that might mod the vnode
3329 * so VINACTIVE is properly cleared.
3331 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3332 error
= vget(vp
, LK_EXCLUSIVE
);
3334 error
= setutimes(vp
, &vattr
, ts
, (tptr
== NULL
));
3343 * utimes_args(char *path, struct timeval *tptr)
3345 * Set the access and modification times of a file.
3348 sys_utimes(struct utimes_args
*uap
)
3350 struct timeval tv
[2];
3351 struct nlookupdata nd
;
3355 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3359 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3361 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3367 * lutimes_args(char *path, struct timeval *tptr)
3369 * Set the access and modification times of a file.
3372 sys_lutimes(struct lutimes_args
*uap
)
3374 struct timeval tv
[2];
3375 struct nlookupdata nd
;
3379 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3383 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3385 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3391 * Set utimes on a file descriptor. The creds used to open the
3392 * file are used to determine whether the operation is allowed
3396 kern_futimes(int fd
, struct timeval
*tptr
)
3398 struct thread
*td
= curthread
;
3399 struct proc
*p
= td
->td_proc
;
3400 struct timespec ts
[2];
3406 error
= getutimes(tptr
, ts
);
3409 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3411 if (fp
->f_nchandle
.ncp
)
3412 error
= ncp_writechk(&fp
->f_nchandle
);
3415 error
= vget(vp
, LK_EXCLUSIVE
);
3417 error
= VOP_GETATTR(vp
, &vattr
);
3419 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3423 error
= setutimes(vp
, &vattr
, ts
,
3434 * futimes_args(int fd, struct timeval *tptr)
3436 * Set the access and modification times of a file.
3439 sys_futimes(struct futimes_args
*uap
)
3441 struct timeval tv
[2];
3445 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3449 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3455 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3462 uint64_t old_size
= 0;
3466 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3467 if ((error
= nlookup(nd
)) != 0)
3469 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3471 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3473 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
3477 if (vp
->v_type
== VDIR
) {
3481 if (vfs_quota_enabled
) {
3482 error
= VOP_GETATTR(vp
, &vattr
);
3483 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3486 old_size
= vattr
.va_size
;
3489 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3491 vattr
.va_size
= length
;
3492 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3493 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3501 * truncate(char *path, int pad, off_t length)
3503 * Truncate a file given its path name.
3506 sys_truncate(struct truncate_args
*uap
)
3508 struct nlookupdata nd
;
3511 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3513 error
= kern_truncate(&nd
, uap
->length
);
3519 kern_ftruncate(int fd
, off_t length
)
3521 struct thread
*td
= curthread
;
3522 struct proc
*p
= td
->td_proc
;
3529 uint64_t old_size
= 0;
3534 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3536 if (fp
->f_nchandle
.ncp
) {
3537 error
= ncp_writechk(&fp
->f_nchandle
);
3541 if ((fp
->f_flag
& FWRITE
) == 0) {
3545 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3549 vp
= (struct vnode
*)fp
->f_data
;
3550 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3551 if (vp
->v_type
== VDIR
) {
3556 if (vfs_quota_enabled
) {
3557 error
= VOP_GETATTR(vp
, &vattr
);
3558 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3561 old_size
= vattr
.va_size
;
3564 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3566 vattr
.va_size
= length
;
3567 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3569 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3578 * ftruncate_args(int fd, int pad, off_t length)
3580 * Truncate a file given a file descriptor.
3583 sys_ftruncate(struct ftruncate_args
*uap
)
3587 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3595 * Sync an open file.
3598 sys_fsync(struct fsync_args
*uap
)
3600 struct thread
*td
= curthread
;
3601 struct proc
*p
= td
->td_proc
;
3607 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3609 vp
= (struct vnode
*)fp
->f_data
;
3610 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3611 if ((obj
= vp
->v_object
) != NULL
)
3612 vm_object_page_clean(obj
, 0, 0, 0);
3613 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3614 if (error
== 0 && vp
->v_mount
)
3615 error
= buf_fsync(vp
);
3623 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3625 struct nchandle fnchd
;
3626 struct nchandle tnchd
;
3627 struct namecache
*ncp
;
3634 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3635 if ((error
= nlookup(fromnd
)) != 0)
3637 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3639 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3643 * unlock the source nch so we can lookup the target nch without
3644 * deadlocking. The target may or may not exist so we do not check
3645 * for a target vp like kern_mkdir() and other creation functions do.
3647 * The source and target directories are ref'd and rechecked after
3648 * everything is relocked to determine if the source or target file
3651 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3652 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3653 cache_unlock(&fromnd
->nl_nch
);
3655 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3656 if ((error
= nlookup(tond
)) != 0) {
3660 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3664 tnchd
.mount
= tond
->nl_nch
.mount
;
3668 * If the source and target are the same there is nothing to do
3670 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
3677 * Mount points cannot be renamed or overwritten
3679 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
3688 * Relock the source ncp. cache_relock() will deal with any
3689 * deadlocks against the already-locked tond and will also
3690 * make sure both are resolved.
3692 * NOTE AFTER RELOCKING: The source or target ncp may have become
3693 * invalid while they were unlocked, nc_vp and nc_mount could
3696 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
3697 &tond
->nl_nch
, tond
->nl_cred
);
3698 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
3701 * If either fromnd or tond are marked destroyed a ripout occured
3702 * out from under us and we must retry.
3704 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
3705 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
3706 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
3707 kprintf("kern_rename: retry due to ripout on: "
3708 "\"%s\" -> \"%s\"\n",
3709 fromnd
->nl_nch
.ncp
->nc_name
,
3710 tond
->nl_nch
.ncp
->nc_name
);
3717 * make sure the parent directories linkages are the same
3719 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
3720 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
3727 * Both the source and target must be within the same filesystem and
3728 * in the same filesystem as their parent directories within the
3729 * namecache topology.
3731 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3734 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
3735 mp
!= tond
->nl_nch
.mount
) {
3742 * Make sure the mount point is writable
3744 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
3751 * If the target exists and either the source or target is a directory,
3752 * then both must be directories.
3754 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3757 if (tond
->nl_nch
.ncp
->nc_vp
) {
3758 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
3760 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3761 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
3763 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3769 * You cannot rename a source into itself or a subdirectory of itself.
3770 * We check this by travsersing the target directory upwards looking
3771 * for a match against the source.
3776 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
3777 if (fromnd
->nl_nch
.ncp
== ncp
) {
3788 * Even though the namespaces are different, they may still represent
3789 * hardlinks to the same file. The filesystem might have a hard time
3790 * with this so we issue a NREMOVE of the source instead of a NRENAME
3791 * when we detect the situation.
3794 fdvp
= fromnd
->nl_dvp
;
3795 tdvp
= tond
->nl_dvp
;
3796 if (fdvp
== NULL
|| tdvp
== NULL
) {
3798 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
3799 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
3802 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
3803 fdvp
, tdvp
, tond
->nl_cred
);
3810 * rename_args(char *from, char *to)
3812 * Rename files. Source and destination must either both be directories,
3813 * or both not be directories. If target is a directory, it must be empty.
3816 sys_rename(struct rename_args
*uap
)
3818 struct nlookupdata fromnd
, tond
;
3822 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
3824 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
3826 error
= kern_rename(&fromnd
, &tond
);
3827 nlookup_done(&tond
);
3829 nlookup_done(&fromnd
);
3830 } while (error
== EAGAIN
);
3835 * renameat_args(int oldfd, char *old, int newfd, char *new)
3837 * Rename files using paths relative to the directories associated with
3838 * oldfd and newfd. Source and destination must either both be directories,
3839 * or both not be directories. If target is a directory, it must be empty.
3842 sys_renameat(struct renameat_args
*uap
)
3844 struct nlookupdata oldnd
, newnd
;
3845 struct file
*oldfp
, *newfp
;
3849 error
= nlookup_init_at(&oldnd
, &oldfp
,
3850 uap
->oldfd
, uap
->old
,
3853 error
= nlookup_init_at(&newnd
, &newfp
,
3854 uap
->newfd
, uap
->new,
3857 error
= kern_rename(&oldnd
, &newnd
);
3858 nlookup_done_at(&newnd
, newfp
);
3860 nlookup_done_at(&oldnd
, oldfp
);
3861 } while (error
== EAGAIN
);
3866 kern_mkdir(struct nlookupdata
*nd
, int mode
)
3868 struct thread
*td
= curthread
;
3869 struct proc
*p
= td
->td_proc
;
3875 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
3876 if ((error
= nlookup(nd
)) != 0)
3879 if (nd
->nl_nch
.ncp
->nc_vp
)
3881 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3884 vattr
.va_type
= VDIR
;
3885 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
3888 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
3895 * mkdir_args(char *path, int mode)
3897 * Make a directory file.
3900 sys_mkdir(struct mkdir_args
*uap
)
3902 struct nlookupdata nd
;
3905 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3907 error
= kern_mkdir(&nd
, uap
->mode
);
3913 * mkdirat_args(int fd, char *path, mode_t mode)
3915 * Make a directory file. The path is relative to the directory associated
3919 sys_mkdirat(struct mkdirat_args
*uap
)
3921 struct nlookupdata nd
;
3925 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
3927 error
= kern_mkdir(&nd
, uap
->mode
);
3928 nlookup_done_at(&nd
, fp
);
3933 kern_rmdir(struct nlookupdata
*nd
)
3938 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
3939 if ((error
= nlookup(nd
)) != 0)
3943 * Do not allow directories representing mount points to be
3944 * deleted, even if empty. Check write perms on mount point
3945 * in case the vnode is aliased (aka nullfs).
3947 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
3949 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3951 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
3956 * rmdir_args(char *path)
3958 * Remove a directory file.
3961 sys_rmdir(struct rmdir_args
*uap
)
3963 struct nlookupdata nd
;
3966 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3968 error
= kern_rmdir(&nd
);
3974 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
3975 enum uio_seg direction
)
3977 struct thread
*td
= curthread
;
3978 struct proc
*p
= td
->td_proc
;
3986 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3988 if ((fp
->f_flag
& FREAD
) == 0) {
3992 vp
= (struct vnode
*)fp
->f_data
;
3994 if (vp
->v_type
!= VDIR
) {
3998 aiov
.iov_base
= buf
;
3999 aiov
.iov_len
= count
;
4000 auio
.uio_iov
= &aiov
;
4001 auio
.uio_iovcnt
= 1;
4002 auio
.uio_rw
= UIO_READ
;
4003 auio
.uio_segflg
= direction
;
4005 auio
.uio_resid
= count
;
4006 loff
= auio
.uio_offset
= fp
->f_offset
;
4007 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4008 fp
->f_offset
= auio
.uio_offset
;
4011 if (count
== auio
.uio_resid
) {
4012 if (union_dircheckp
) {
4013 error
= union_dircheckp(td
, &vp
, fp
);
4020 if ((vp
->v_flag
& VROOT
) &&
4021 (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
4022 struct vnode
*tvp
= vp
;
4023 vp
= vp
->v_mount
->mnt_vnodecovered
;
4034 * WARNING! *basep may not be wide enough to accomodate the
4035 * seek offset. XXX should we hack this to return the upper 32 bits
4036 * for offsets greater then 4G?
4039 *basep
= (long)loff
;
4041 *res
= count
- auio
.uio_resid
;
4048 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4050 * Read a block of directory entries in a file system independent format.
4053 sys_getdirentries(struct getdirentries_args
*uap
)
4058 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4059 &uap
->sysmsg_result
, UIO_USERSPACE
);
4061 if (error
== 0 && uap
->basep
)
4062 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4067 * getdents_args(int fd, char *buf, size_t count)
4070 sys_getdents(struct getdents_args
*uap
)
4074 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4075 &uap
->sysmsg_result
, UIO_USERSPACE
);
4081 * Set the mode mask for creation of filesystem nodes.
4083 * umask(int newmask)
4086 sys_umask(struct umask_args
*uap
)
4088 struct thread
*td
= curthread
;
4089 struct proc
*p
= td
->td_proc
;
4090 struct filedesc
*fdp
;
4093 uap
->sysmsg_result
= fdp
->fd_cmask
;
4094 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4099 * revoke(char *path)
4101 * Void all references to file by ripping underlying filesystem
4105 sys_revoke(struct revoke_args
*uap
)
4107 struct nlookupdata nd
;
4114 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4116 error
= nlookup(&nd
);
4118 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4119 cred
= crhold(nd
.nl_cred
);
4123 error
= VOP_GETATTR(vp
, &vattr
);
4124 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4125 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4126 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4128 error
= vrevoke(vp
, cred
);
4129 } else if (error
== 0) {
4130 error
= vrevoke(vp
, cred
);
4140 * getfh_args(char *fname, fhandle_t *fhp)
4142 * Get (NFS) file handle
4144 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4145 * mount. This allows nullfs mounts to be explicitly exported.
4147 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4149 * nullfs mounts of subdirectories are not safe. That is, it will
4150 * work, but you do not really have protection against access to
4151 * the related parent directories.
4154 sys_getfh(struct getfh_args
*uap
)
4156 struct thread
*td
= curthread
;
4157 struct nlookupdata nd
;
4164 * Must be super user
4166 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4170 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4172 error
= nlookup(&nd
);
4174 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4175 mp
= nd
.nl_nch
.mount
;
4178 bzero(&fh
, sizeof(fh
));
4179 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4180 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4183 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4189 * fhopen_args(const struct fhandle *u_fhp, int flags)
4191 * syscall for the rpc.lockd to use to translate a NFS file handle into
4192 * an open descriptor.
4194 * warning: do not remove the priv_check() call or this becomes one giant
4198 sys_fhopen(struct fhopen_args
*uap
)
4200 struct thread
*td
= curthread
;
4201 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4206 struct vattr
*vap
= &vat
;
4208 int fmode
, mode
, error
= 0, type
;
4214 * Must be super user
4216 error
= priv_check(td
, PRIV_ROOT
);
4220 fmode
= FFLAGS(uap
->flags
);
4223 * Why not allow a non-read/write open for our lockd?
4225 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4227 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4232 * Find the mount point
4234 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4239 /* now give me my vnode, it gets returned to me locked */
4240 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4244 * from now on we have to make sure not
4245 * to forget about the vnode
4246 * any error that causes an abort must vput(vp)
4247 * just set error = err and 'goto bad;'.
4253 if (vp
->v_type
== VLNK
) {
4257 if (vp
->v_type
== VSOCK
) {
4262 if (fmode
& (FWRITE
| O_TRUNC
)) {
4263 if (vp
->v_type
== VDIR
) {
4267 error
= vn_writechk(vp
, NULL
);
4275 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4279 if (fmode
& O_TRUNC
) {
4280 vn_unlock(vp
); /* XXX */
4281 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4284 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4290 * VOP_OPEN needs the file pointer so it can potentially override
4293 * WARNING! no f_nchandle will be associated when fhopen()ing a
4296 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4300 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4303 * setting f_ops this way prevents VOP_CLOSE from being
4304 * called or fdrop() releasing the vp from v_data. Since
4305 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4307 fp
->f_ops
= &badfileops
;
4313 * The fp is given its own reference, we still have our ref and lock.
4315 * Assert that all regular files must be created with a VM object.
4317 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4318 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4323 * The open was successful. Handle any locking requirements.
4325 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4326 lf
.l_whence
= SEEK_SET
;
4329 if (fmode
& O_EXLOCK
)
4330 lf
.l_type
= F_WRLCK
;
4332 lf
.l_type
= F_RDLCK
;
4333 if (fmode
& FNONBLOCK
)
4338 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4340 * release our private reference.
4342 fsetfd(fdp
, NULL
, indx
);
4347 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4348 fp
->f_flag
|= FHASLOCK
;
4352 * Clean up. Associate the file pointer with the previously
4353 * reserved descriptor and return it.
4356 fsetfd(fdp
, fp
, indx
);
4358 uap
->sysmsg_result
= indx
;
4359 if (uap
->flags
& O_CLOEXEC
)
4360 error
= fsetfdflags(fdp
, indx
, UF_EXCLOSE
);
4364 fsetfd(fdp
, NULL
, indx
);
4373 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4376 sys_fhstat(struct fhstat_args
*uap
)
4378 struct thread
*td
= curthread
;
4386 * Must be super user
4388 error
= priv_check(td
, PRIV_ROOT
);
4392 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4396 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4399 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4400 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4405 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4410 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4413 sys_fhstatfs(struct fhstatfs_args
*uap
)
4415 struct thread
*td
= curthread
;
4416 struct proc
*p
= td
->td_proc
;
4421 char *fullpath
, *freepath
;
4426 * Must be super user
4428 if ((error
= priv_check(td
, PRIV_ROOT
)))
4431 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4434 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4438 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4443 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4448 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4451 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4454 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4455 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4456 kfree(freepath
, M_TEMP
);
4458 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4459 if (priv_check(td
, PRIV_ROOT
)) {
4460 bcopy(sp
, &sb
, sizeof(sb
));
4461 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4464 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4470 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4473 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4475 struct thread
*td
= curthread
;
4476 struct proc
*p
= td
->td_proc
;
4484 * Must be super user
4486 if ((error
= priv_check(td
, PRIV_ROOT
)))
4489 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4492 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4496 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4501 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4504 sp
= &mp
->mnt_vstat
;
4506 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4510 if (mp
->mnt_flag
& MNT_RDONLY
)
4511 sp
->f_flag
|= ST_RDONLY
;
4512 if (mp
->mnt_flag
& MNT_NOSUID
)
4513 sp
->f_flag
|= ST_NOSUID
;
4514 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4521 * Syscall to push extended attribute configuration information into the
4522 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4523 * a command (int cmd), and attribute name and misc data. For now, the
4524 * attribute name is left in userspace for consumption by the VFS_op.
4525 * It will probably be changed to be copied into sysspace by the
4526 * syscall in the future, once issues with various consumers of the
4527 * attribute code have raised their hands.
4529 * Currently this is used only by UFS Extended Attributes.
4532 sys_extattrctl(struct extattrctl_args
*uap
)
4534 struct nlookupdata nd
;
4536 char attrname
[EXTATTR_MAXNAMELEN
];
4544 if (error
== 0 && uap
->filename
) {
4545 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4548 error
= nlookup(&nd
);
4550 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4554 if (error
== 0 && uap
->attrname
) {
4555 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4560 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4562 error
= nlookup(&nd
);
4564 error
= ncp_writechk(&nd
.nl_nch
);
4566 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4568 uap
->attrname
, nd
.nl_cred
);
4577 * Syscall to get a named extended attribute on a file or directory.
4580 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4582 char attrname
[EXTATTR_MAXNAMELEN
];
4583 struct nlookupdata nd
;
4589 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4595 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4597 error
= nlookup(&nd
);
4599 error
= ncp_writechk(&nd
.nl_nch
);
4601 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4607 bzero(&auio
, sizeof(auio
));
4608 aiov
.iov_base
= uap
->data
;
4609 aiov
.iov_len
= uap
->nbytes
;
4610 auio
.uio_iov
= &aiov
;
4611 auio
.uio_iovcnt
= 1;
4612 auio
.uio_offset
= 0;
4613 auio
.uio_resid
= uap
->nbytes
;
4614 auio
.uio_rw
= UIO_WRITE
;
4615 auio
.uio_td
= curthread
;
4617 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4626 * Syscall to get a named extended attribute on a file or directory.
4629 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4631 char attrname
[EXTATTR_MAXNAMELEN
];
4632 struct nlookupdata nd
;
4638 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4644 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4646 error
= nlookup(&nd
);
4648 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4654 bzero(&auio
, sizeof(auio
));
4655 aiov
.iov_base
= uap
->data
;
4656 aiov
.iov_len
= uap
->nbytes
;
4657 auio
.uio_iov
= &aiov
;
4658 auio
.uio_iovcnt
= 1;
4659 auio
.uio_offset
= 0;
4660 auio
.uio_resid
= uap
->nbytes
;
4661 auio
.uio_rw
= UIO_READ
;
4662 auio
.uio_td
= curthread
;
4664 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4666 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
4674 * Syscall to delete a named extended attribute from a file or directory.
4675 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4678 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
4680 char attrname
[EXTATTR_MAXNAMELEN
];
4681 struct nlookupdata nd
;
4685 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4689 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4691 error
= nlookup(&nd
);
4693 error
= ncp_writechk(&nd
.nl_nch
);
4695 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4697 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
4698 attrname
, NULL
, nd
.nl_cred
);
4707 * Determine if the mount is visible to the process.
4710 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
4712 struct nchandle nch
;
4715 * Traverse from the mount point upwards. If we hit the process
4716 * root then the mount point is visible to the process.
4718 nch
= mp
->mnt_ncmountpt
;
4720 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
4721 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
4724 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
4725 nch
= nch
.mount
->mnt_ncmounton
;
4727 nch
.ncp
= nch
.ncp
->nc_parent
;
4732 * If the mount point is not visible to the process, but the
4733 * process root is in a subdirectory of the mount, return
4736 if (p
->p_fd
->fd_nrdir
.mount
== mp
)