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.117 2007/06/26 20:39:33 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/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
68 #include <sys/sysctl.h>
69 #include <sys/file2.h>
70 #include <sys/spinlock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 #include <vfs/union/union.h>
81 static void mount_warning(struct mount
*mp
, const char *ctl
, ...);
82 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
83 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
84 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
85 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
86 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
87 static int getutimes (const struct timeval
*, struct timespec
*);
88 static int setfown (struct vnode
*, uid_t
, gid_t
);
89 static int setfmode (struct vnode
*, int);
90 static int setfflags (struct vnode
*, int);
91 static int setutimes (struct vnode
*, const struct timespec
*, int);
92 static int usermount
= 0; /* if 1, non-root can mount fs. */
94 int (*union_dircheckp
) (struct thread
*, struct vnode
**, struct file
*);
96 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0, "");
99 * Virtual File System System Calls
103 * Mount a file system.
106 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args
*uap
)
112 struct thread
*td
= curthread
;
113 struct proc
*p
= td
->td_proc
;
117 struct vfsconf
*vfsp
;
118 int error
, flag
= 0, flag2
= 0;
121 struct nlookupdata nd
;
122 char fstypename
[MFSNAMELEN
];
123 struct ucred
*cred
= p
->p_ucred
;
126 if (cred
->cr_prison
!= NULL
)
128 if (usermount
== 0 && (error
= suser(td
)))
131 * Do not allow NFS export by non-root users.
133 if (uap
->flags
& MNT_EXPORTED
) {
139 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
145 * Lookup the requested path and extract the nch and vnode.
147 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
149 if ((error
= nlookup(&nd
)) == 0) {
150 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
160 * Extract the locked+refd ncp and cleanup the nd structure
163 cache_zero(&nd
.nl_nch
);
166 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) && cache_findmount(&nch
))
173 * now we have the locked ref'd nch and unreferenced vnode.
176 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
183 * Now we have an unlocked ref'd nch and a locked ref'd vp
185 if (uap
->flags
& MNT_UPDATE
) {
186 if ((vp
->v_flag
& VROOT
) == 0) {
193 flag2
= mp
->mnt_kern_flag
;
195 * We only allow the filesystem to be reloaded if it
196 * is currently mounted read-only.
198 if ((uap
->flags
& MNT_RELOAD
) &&
199 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
202 return (EOPNOTSUPP
); /* Needs translation */
205 * Only root, or the user that did the original mount is
206 * permitted to update it.
208 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
209 (error
= suser(td
))) {
214 if (vfs_busy(mp
, LK_NOWAIT
)) {
219 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
225 vp
->v_flag
|= VMOUNT
;
227 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
232 * If the user is not root, ensure that they own the directory
233 * onto which we are attempting to mount.
235 if ((error
= VOP_GETATTR(vp
, &va
)) ||
236 (va
.va_uid
!= cred
->cr_uid
&& (error
= suser(td
)))) {
241 if ((error
= vinvalbuf(vp
, V_SAVE
, 0, 0)) != 0) {
246 if (vp
->v_type
!= VDIR
) {
251 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
256 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
257 if (!strcmp(vfsp
->vfc_name
, fstypename
))
263 /* Only load modules for root (very important!) */
264 if ((error
= suser(td
)) != 0) {
269 error
= linker_load_file(fstypename
, &lf
);
270 if (error
|| lf
== NULL
) {
278 /* lookup again, see if the VFS was loaded */
279 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
280 if (!strcmp(vfsp
->vfc_name
, fstypename
))
285 linker_file_unload(lf
);
291 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
296 vp
->v_flag
|= VMOUNT
;
299 * Allocate and initialize the filesystem.
301 mp
= kmalloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
302 TAILQ_INIT(&mp
->mnt_nvnodelist
);
303 TAILQ_INIT(&mp
->mnt_reservedvnlist
);
304 TAILQ_INIT(&mp
->mnt_jlist
);
305 mp
->mnt_nvnodelistsize
= 0;
306 lockinit(&mp
->mnt_lock
, "vfslock", 0, 0);
307 vfs_busy(mp
, LK_NOWAIT
);
308 mp
->mnt_op
= vfsp
->vfc_vfsops
;
310 vfsp
->vfc_refcount
++;
311 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
312 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
313 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
314 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
315 mp
->mnt_iosize_max
= DFLTPHYS
;
319 * Set the mount level flags.
321 if (uap
->flags
& MNT_RDONLY
)
322 mp
->mnt_flag
|= MNT_RDONLY
;
323 else if (mp
->mnt_flag
& MNT_RDONLY
)
324 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
325 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
326 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_NOATIME
|
327 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
328 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
329 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
330 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_FORCE
|
331 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
332 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
334 * Mount the filesystem.
335 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
338 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
339 if (mp
->mnt_flag
& MNT_UPDATE
) {
340 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
341 mp
->mnt_flag
&= ~MNT_RDONLY
;
342 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
343 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
346 mp
->mnt_kern_flag
= flag2
;
349 vp
->v_flag
&= ~VMOUNT
;
354 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
356 * Put the new filesystem on the mount list after root. The mount
357 * point gets its own mnt_ncmountpt (unless the VFS already set one
358 * up) which represents the root of the mount. The lookup code
359 * detects the mount point going forward and checks the root of
360 * the mount going backwards.
362 * It is not necessary to invalidate or purge the vnode underneath
363 * because elements under the mount will be given their own glue
367 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
369 * allocate, then unlock, but leave the ref intact
371 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
372 cache_unlock(&mp
->mnt_ncmountpt
);
374 mp
->mnt_ncmounton
= nch
; /* inherits ref */
375 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
377 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
378 vp
->v_flag
&= ~VMOUNT
;
379 mountlist_insert(mp
, MNTINS_LAST
);
380 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
382 error
= vfs_allocate_syncvnode(mp
);
384 error
= VFS_START(mp
, 0);
387 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
388 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
389 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
390 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
391 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
392 vp
->v_flag
&= ~VMOUNT
;
393 mp
->mnt_vfc
->vfc_refcount
--;
403 * Scan all active processes to see if any of them have a current
404 * or root directory onto which the new filesystem has just been
405 * mounted. If so, replace them with the new mount point.
407 * The passed ncp is ref'd and locked (from the mount code) and
408 * must be associated with the vnode representing the root of the
411 struct checkdirs_info
{
412 struct nchandle old_nch
;
413 struct nchandle new_nch
;
414 struct vnode
*old_vp
;
415 struct vnode
*new_vp
;
418 static int checkdirs_callback(struct proc
*p
, void *data
);
421 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
423 struct checkdirs_info info
;
429 * If the old mount point's vnode has a usecount of 1, it is not
430 * being held as a descriptor anywhere.
432 olddp
= old_nch
->ncp
->nc_vp
;
433 if (olddp
== NULL
|| olddp
->v_sysref
.refcnt
== 1)
437 * Force the root vnode of the new mount point to be resolved
438 * so we can update any matching processes.
441 if (VFS_ROOT(mp
, &newdp
))
442 panic("mount: lost mount");
443 cache_setunresolved(new_nch
);
444 cache_setvp(new_nch
, newdp
);
447 * Special handling of the root node
449 if (rootvnode
== olddp
) {
451 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
455 * Pass newdp separately so the callback does not have to access
456 * it via new_nch->ncp->nc_vp.
458 info
.old_nch
= *old_nch
;
459 info
.new_nch
= *new_nch
;
461 allproc_scan(checkdirs_callback
, &info
);
466 * NOTE: callback is not MP safe because the scanned process's filedesc
467 * structure can be ripped out from under us, amoung other things.
470 checkdirs_callback(struct proc
*p
, void *data
)
472 struct checkdirs_info
*info
= data
;
473 struct filedesc
*fdp
;
474 struct nchandle ncdrop1
;
475 struct nchandle ncdrop2
;
476 struct vnode
*vprele1
;
477 struct vnode
*vprele2
;
479 if ((fdp
= p
->p_fd
) != NULL
) {
480 cache_zero(&ncdrop1
);
481 cache_zero(&ncdrop2
);
486 * MPUNSAFE - XXX fdp can be pulled out from under a
489 * A shared filedesc is ok, we don't have to copy it
490 * because we are making this change globally.
492 spin_lock_wr(&fdp
->fd_spin
);
493 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
494 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
495 vprele1
= fdp
->fd_cdir
;
497 fdp
->fd_cdir
= info
->new_vp
;
498 ncdrop1
= fdp
->fd_ncdir
;
499 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
501 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
502 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
503 vprele2
= fdp
->fd_rdir
;
505 fdp
->fd_rdir
= info
->new_vp
;
506 ncdrop2
= fdp
->fd_nrdir
;
507 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
509 spin_unlock_wr(&fdp
->fd_spin
);
511 cache_drop(&ncdrop1
);
513 cache_drop(&ncdrop2
);
523 * Unmount a file system.
525 * Note: unmount takes a path to the vnode mounted on as argument,
526 * not special file (as before).
529 * umount_args(char *path, int flags)
533 sys_unmount(struct unmount_args
*uap
)
535 struct thread
*td
= curthread
;
536 struct proc
*p
= td
->td_proc
;
537 struct mount
*mp
= NULL
;
539 struct nlookupdata nd
;
542 if (p
->p_ucred
->cr_prison
!= NULL
)
544 if (usermount
== 0 && (error
= suser(td
)))
547 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
549 error
= nlookup(&nd
);
553 mp
= nd
.nl_nch
.mount
;
556 * Only root, or the user that did the original mount is
557 * permitted to unmount this filesystem.
559 if ((mp
->mnt_stat
.f_owner
!= p
->p_ucred
->cr_uid
) &&
564 * Don't allow unmounting the root file system.
566 if (mp
->mnt_flag
& MNT_ROOTFS
) {
572 * Must be the root of the filesystem
574 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
583 return (dounmount(mp
, uap
->flags
));
587 * Do the actual file system unmount.
590 dounmount_interlock(struct mount
*mp
)
592 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
594 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
599 dounmount(struct mount
*mp
, int flags
)
601 struct namecache
*ncp
;
609 * Exclusive access for unmounting purposes
611 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
615 * Allow filesystems to detect that a forced unmount is in progress.
617 if (flags
& MNT_FORCE
)
618 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
619 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_NOWAIT
);
620 error
= lockmgr(&mp
->mnt_lock
, lflags
);
622 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
623 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
628 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
629 vfs_setpublicfs(NULL
, NULL
, NULL
);
631 vfs_msync(mp
, MNT_WAIT
);
632 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
633 mp
->mnt_flag
&=~ MNT_ASYNC
;
636 * If this filesystem isn't aliasing other filesystems,
637 * try to invalidate any remaining namecache entries and
638 * check the count afterwords.
640 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
641 cache_lock(&mp
->mnt_ncmountpt
);
642 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
643 cache_unlock(&mp
->mnt_ncmountpt
);
645 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
646 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
648 if ((flags
& MNT_FORCE
) == 0) {
650 mount_warning(mp
, "Cannot unmount: "
656 mount_warning(mp
, "Forced unmount: "
667 * nchandle records ref the mount structure. Expect a count of 1
668 * (our mount->mnt_ncmountpt).
670 if (mp
->mnt_refs
!= 1) {
671 if ((flags
& MNT_FORCE
) == 0) {
672 mount_warning(mp
, "Cannot unmount: "
673 "%d process references still "
674 "present", mp
->mnt_refs
);
677 mount_warning(mp
, "Forced unmount: "
678 "%d process references still "
679 "present", mp
->mnt_refs
);
685 if (mp
->mnt_syncer
!= NULL
)
686 vrele(mp
->mnt_syncer
);
687 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
688 (error
= VFS_SYNC(mp
, MNT_WAIT
)) == 0) ||
689 (flags
& MNT_FORCE
)) {
690 error
= VFS_UNMOUNT(mp
, flags
);
694 if (mp
->mnt_syncer
== NULL
)
695 vfs_allocate_syncvnode(mp
);
696 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
697 mp
->mnt_flag
|= async_flag
;
698 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
699 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
704 * Clean up any journals still associated with the mount after
705 * filesystem activity has ceased.
707 journal_remove_all_journals(mp
,
708 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
710 mountlist_remove(mp
);
713 * Remove any installed vnode ops here so the individual VFSs don't
716 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
717 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
718 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
719 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
720 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
722 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
723 nch
= mp
->mnt_ncmountpt
;
724 cache_zero(&mp
->mnt_ncmountpt
);
725 cache_clrmountpt(&nch
);
728 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
729 nch
= mp
->mnt_ncmounton
;
730 cache_zero(&mp
->mnt_ncmounton
);
731 cache_clrmountpt(&nch
);
735 mp
->mnt_vfc
->vfc_refcount
--;
736 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
737 panic("unmount: dangling vnode");
738 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
739 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
748 mount_warning(struct mount
*mp
, const char *ctl
, ...)
755 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, &ptr
, &buf
) == 0) {
756 kprintf("unmount(%s): ", ptr
);
761 kprintf("unmount(%p): ", mp
);
769 * Shim cache_fullpath() to handle the case where a process is chrooted into
770 * a subdirectory of a mount. In this case if the root mount matches the
771 * process root directory's mount we have to specify the process's root
772 * directory instead of the mount point, because the mount point might
773 * be above the root directory.
777 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
779 struct nchandle
*nch
;
781 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
782 nch
= &p
->p_fd
->fd_nrdir
;
784 nch
= &mp
->mnt_ncmountpt
;
785 return(cache_fullpath(p
, nch
, rb
, fb
));
789 * Sync each mounted filesystem.
793 static int syncprt
= 0;
794 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
797 static int sync_callback(struct mount
*mp
, void *data
);
801 sys_sync(struct sync_args
*uap
)
803 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
806 * print out buffer pool stat information on each sync() call.
816 sync_callback(struct mount
*mp
, void *data __unused
)
820 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
821 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
822 mp
->mnt_flag
&= ~MNT_ASYNC
;
823 vfs_msync(mp
, MNT_NOWAIT
);
824 VFS_SYNC(mp
, MNT_NOWAIT
);
825 mp
->mnt_flag
|= asyncflag
;
830 /* XXX PRISON: could be per prison flag */
831 static int prison_quotas
;
833 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
837 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
839 * Change filesystem quotas.
843 sys_quotactl(struct quotactl_args
*uap
)
845 struct nlookupdata nd
;
853 if (p
->p_ucred
->cr_prison
&& !prison_quotas
)
856 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
858 error
= nlookup(&nd
);
860 mp
= nd
.nl_nch
.mount
;
861 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
862 uap
->arg
, nd
.nl_cred
);
869 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
870 * void *buf, int buflen)
872 * This function operates on a mount point and executes the specified
873 * operation using the specified control data, and possibly returns data.
875 * The actual number of bytes stored in the result buffer is returned, 0
876 * if none, otherwise an error is returned.
880 sys_mountctl(struct mountctl_args
*uap
)
882 struct thread
*td
= curthread
;
883 struct proc
*p
= td
->td_proc
;
891 * Sanity and permissions checks. We must be root.
894 if (p
->p_ucred
->cr_prison
!= NULL
)
896 if ((error
= suser(td
)) != 0)
900 * Argument length checks
902 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
904 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
906 if (uap
->path
== NULL
)
910 * Allocate the necessary buffers and copyin data
912 path
= objcache_get(namei_oc
, M_WAITOK
);
913 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
918 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
919 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
924 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
927 * Validate the descriptor
930 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
940 * Execute the internal kernel function and clean up.
942 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
945 if (error
== 0 && uap
->sysmsg_result
> 0)
946 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
949 objcache_put(namei_oc
, path
);
958 * Execute a mount control operation by resolving the path to a mount point
959 * and calling vop_mountctl().
962 kern_mountctl(const char *path
, int op
, struct file
*fp
,
963 const void *ctl
, int ctllen
,
964 void *buf
, int buflen
, int *res
)
968 struct nlookupdata nd
;
973 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
975 error
= nlookup(&nd
);
977 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
985 * Must be the root of the filesystem
987 if ((vp
->v_flag
& VROOT
) == 0) {
991 error
= vop_mountctl(mp
->mnt_vn_use_ops
, op
, fp
, ctl
, ctllen
,
998 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1000 struct thread
*td
= curthread
;
1001 struct proc
*p
= td
->td_proc
;
1004 char *fullpath
, *freepath
;
1007 if ((error
= nlookup(nd
)) != 0)
1009 mp
= nd
->nl_nch
.mount
;
1011 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1014 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1017 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1018 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1019 kfree(freepath
, M_TEMP
);
1021 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1022 bcopy(sp
, buf
, sizeof(*buf
));
1023 /* Only root should have access to the fsid's. */
1025 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1030 * statfs_args(char *path, struct statfs *buf)
1032 * Get filesystem statistics.
1035 sys_statfs(struct statfs_args
*uap
)
1037 struct nlookupdata nd
;
1041 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1043 error
= kern_statfs(&nd
, &buf
);
1046 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1051 kern_fstatfs(int fd
, struct statfs
*buf
)
1053 struct thread
*td
= curthread
;
1054 struct proc
*p
= td
->td_proc
;
1058 char *fullpath
, *freepath
;
1062 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1064 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1069 if (fp
->f_cred
== NULL
) {
1074 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1077 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1079 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1080 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1081 kfree(freepath
, M_TEMP
);
1083 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1084 bcopy(sp
, buf
, sizeof(*buf
));
1086 /* Only root should have access to the fsid's. */
1088 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1096 * fstatfs_args(int fd, struct statfs *buf)
1098 * Get filesystem statistics.
1101 sys_fstatfs(struct fstatfs_args
*uap
)
1106 error
= kern_fstatfs(uap
->fd
, &buf
);
1109 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1114 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1116 * Get statistics on all filesystems.
1119 struct getfsstat_info
{
1120 struct statfs
*sfsp
;
1128 static int getfsstat_callback(struct mount
*, void *);
1132 sys_getfsstat(struct getfsstat_args
*uap
)
1134 struct thread
*td
= curthread
;
1135 struct proc
*p
= td
->td_proc
;
1136 struct getfsstat_info info
;
1138 bzero(&info
, sizeof(info
));
1140 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1141 info
.sfsp
= uap
->buf
;
1143 info
.flags
= uap
->flags
;
1146 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1147 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1148 uap
->sysmsg_result
= info
.maxcount
;
1150 uap
->sysmsg_result
= info
.count
;
1151 return (info
.error
);
1155 getfsstat_callback(struct mount
*mp
, void *data
)
1157 struct getfsstat_info
*info
= data
;
1163 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1164 if (info
->p
&& !chroot_visible_mnt(mp
, info
->p
))
1169 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1170 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1171 * overrides MNT_WAIT.
1173 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1174 (info
->flags
& MNT_WAIT
)) &&
1175 (error
= VFS_STATFS(mp
, sp
, info
->p
->p_ucred
))) {
1178 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1180 error
= mount_path(info
->p
, mp
, &fullpath
, &freepath
);
1182 info
->error
= error
;
1185 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1186 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1187 kfree(freepath
, M_TEMP
);
1189 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1191 info
->error
= error
;
1201 * fchdir_args(int fd)
1203 * Change current working directory to a given file descriptor.
1207 sys_fchdir(struct fchdir_args
*uap
)
1209 struct thread
*td
= curthread
;
1210 struct proc
*p
= td
->td_proc
;
1211 struct filedesc
*fdp
= p
->p_fd
;
1212 struct vnode
*vp
, *ovp
;
1215 struct nchandle nch
, onch
, tnch
;
1218 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1220 vp
= (struct vnode
*)fp
->f_data
;
1222 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1223 if (vp
->v_type
!= VDIR
|| fp
->f_nchandle
.ncp
== NULL
)
1226 error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
);
1232 cache_copy(&fp
->f_nchandle
, &nch
);
1235 * If the ncp has become a mount point, traverse through
1239 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1240 (mp
= cache_findmount(&nch
)) != NULL
1242 error
= nlookup_mp(mp
, &tnch
);
1244 cache_unlock(&tnch
); /* leave ref intact */
1246 vp
= tnch
.ncp
->nc_vp
;
1247 error
= vget(vp
, LK_SHARED
);
1248 KKASSERT(error
== 0);
1255 onch
= fdp
->fd_ncdir
;
1256 vn_unlock(vp
); /* leave ref intact */
1258 fdp
->fd_ncdir
= nch
;
1270 kern_chdir(struct nlookupdata
*nd
)
1272 struct thread
*td
= curthread
;
1273 struct proc
*p
= td
->td_proc
;
1274 struct filedesc
*fdp
= p
->p_fd
;
1275 struct vnode
*vp
, *ovp
;
1276 struct nchandle onch
;
1279 if ((error
= nlookup(nd
)) != 0)
1281 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1283 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1286 error
= checkvp_chdir(vp
, td
);
1290 onch
= fdp
->fd_ncdir
;
1291 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1292 fdp
->fd_ncdir
= nd
->nl_nch
;
1296 cache_zero(&nd
->nl_nch
);
1304 * chdir_args(char *path)
1306 * Change current working directory (``.'').
1309 sys_chdir(struct chdir_args
*uap
)
1311 struct nlookupdata nd
;
1314 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1316 error
= kern_chdir(&nd
);
1322 * Helper function for raised chroot(2) security function: Refuse if
1323 * any filedescriptors are open directories.
1326 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1333 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1334 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1336 vp
= (struct vnode
*)fp
->f_data
;
1337 if (vp
->v_type
!= VDIR
) {
1348 * This sysctl determines if we will allow a process to chroot(2) if it
1349 * has a directory open:
1350 * 0: disallowed for all processes.
1351 * 1: allowed for processes that were not already chroot(2)'ed.
1352 * 2: allowed for all processes.
1355 static int chroot_allow_open_directories
= 1;
1357 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1358 &chroot_allow_open_directories
, 0, "");
1361 * chroot to the specified namecache entry. We obtain the vp from the
1362 * namecache data. The passed ncp must be locked and referenced and will
1363 * remain locked and referenced on return.
1366 kern_chroot(struct nchandle
*nch
)
1368 struct thread
*td
= curthread
;
1369 struct proc
*p
= td
->td_proc
;
1370 struct filedesc
*fdp
= p
->p_fd
;
1375 * Only root can chroot
1377 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0)
1381 * Disallow open directory descriptors (fchdir() breakouts).
1383 if (chroot_allow_open_directories
== 0 ||
1384 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1385 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1388 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1391 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1395 * Check the validity of vp as a directory to change to and
1396 * associate it with rdir/jdir.
1398 error
= checkvp_chdir(vp
, td
);
1399 vn_unlock(vp
); /* leave reference intact */
1401 vrele(fdp
->fd_rdir
);
1402 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1403 cache_drop(&fdp
->fd_nrdir
);
1404 cache_copy(nch
, &fdp
->fd_nrdir
);
1405 if (fdp
->fd_jdir
== NULL
) {
1408 cache_copy(nch
, &fdp
->fd_njdir
);
1417 * chroot_args(char *path)
1419 * Change notion of root (``/'') directory.
1423 sys_chroot(struct chroot_args
*uap
)
1425 struct thread
*td
= curthread
;
1426 struct nlookupdata nd
;
1429 KKASSERT(td
->td_proc
);
1430 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1435 error
= nlookup(&nd
);
1437 error
= kern_chroot(&nd
.nl_nch
);
1443 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1444 * determine whether it is legal to chdir to the vnode. The vnode's state
1445 * is not changed by this call.
1448 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1452 if (vp
->v_type
!= VDIR
)
1455 error
= VOP_ACCESS(vp
, VEXEC
, td
->td_proc
->p_ucred
);
1460 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1462 struct thread
*td
= curthread
;
1463 struct proc
*p
= td
->td_proc
;
1464 struct lwp
*lp
= td
->td_lwp
;
1465 struct filedesc
*fdp
= p
->p_fd
;
1470 int type
, indx
, error
;
1473 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1475 flags
= FFLAGS(oflags
);
1476 error
= falloc(p
, &nfp
, NULL
);
1480 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) &~ S_ISTXT
;
1483 * XXX p_dupfd is a real mess. It allows a device to return a
1484 * file descriptor to be duplicated rather then doing the open
1490 * Call vn_open() to do the lookup and assign the vnode to the
1491 * file pointer. vn_open() does not change the ref count on fp
1492 * and the vnode, on success, will be inherited by the file pointer
1495 nd
->nl_flags
|= NLC_LOCKVP
;
1496 error
= vn_open(nd
, fp
, flags
, cmode
);
1500 * handle special fdopen() case. bleh. dupfdopen() is
1501 * responsible for dropping the old contents of ofiles[indx]
1504 * Note that fsetfd() will add a ref to fp which represents
1505 * the fd_files[] assignment. We must still drop our
1508 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1509 if (fdalloc(p
, 0, &indx
) == 0) {
1510 error
= dupfdopen(p
, indx
, lp
->lwp_dupfd
, flags
, error
);
1513 fdrop(fp
); /* our ref */
1516 fsetfd(p
, NULL
, indx
);
1519 fdrop(fp
); /* our ref */
1520 if (error
== ERESTART
)
1526 * ref the vnode for ourselves so it can't be ripped out from under
1527 * is. XXX need an ND flag to request that the vnode be returned
1530 * Reserve a file descriptor but do not assign it until the open
1533 vp
= (struct vnode
*)fp
->f_data
;
1535 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1542 * If no error occurs the vp will have been assigned to the file
1547 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1548 lf
.l_whence
= SEEK_SET
;
1551 if (flags
& O_EXLOCK
)
1552 lf
.l_type
= F_WRLCK
;
1554 lf
.l_type
= F_RDLCK
;
1555 if (flags
& FNONBLOCK
)
1560 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1562 * lock request failed. Clean up the reserved
1566 fsetfd(p
, NULL
, indx
);
1570 fp
->f_flag
|= FHASLOCK
;
1574 * Assert that all regular file vnodes were created with a object.
1576 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1577 ("open: regular file has no backing object after vn_open"));
1583 * release our private reference, leaving the one associated with the
1584 * descriptor table intact.
1586 fsetfd(p
, fp
, indx
);
1593 * open_args(char *path, int flags, int mode)
1595 * Check permissions, allocate an open file structure,
1596 * and call the device open routine if any.
1599 sys_open(struct open_args
*uap
)
1601 struct nlookupdata nd
;
1604 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1606 error
= kern_open(&nd
, uap
->flags
,
1607 uap
->mode
, &uap
->sysmsg_result
);
1614 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
1616 struct thread
*td
= curthread
;
1617 struct proc
*p
= td
->td_proc
;
1625 switch (mode
& S_IFMT
) {
1631 error
= suser_cred(p
->p_ucred
, PRISON_ROOT
);
1638 nd
->nl_flags
|= NLC_CREATE
;
1639 if ((error
= nlookup(nd
)) != 0)
1641 if (nd
->nl_nch
.ncp
->nc_vp
)
1643 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1647 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1648 vattr
.va_rmajor
= rmajor
;
1649 vattr
.va_rminor
= rminor
;
1652 switch (mode
& S_IFMT
) {
1653 case S_IFMT
: /* used by badsect to flag bad sectors */
1654 vattr
.va_type
= VBAD
;
1657 vattr
.va_type
= VCHR
;
1660 vattr
.va_type
= VBLK
;
1671 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_cred
, NAMEI_CREATE
);
1674 error
= VOP_NMKNOD(&nd
->nl_nch
, &vp
, nd
->nl_cred
, &vattr
);
1683 * mknod_args(char *path, int mode, int dev)
1685 * Create a special file.
1688 sys_mknod(struct mknod_args
*uap
)
1690 struct nlookupdata nd
;
1693 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1695 error
= kern_mknod(&nd
, uap
->mode
,
1696 umajor(uap
->dev
), uminor(uap
->dev
));
1703 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
1705 struct thread
*td
= curthread
;
1706 struct proc
*p
= td
->td_proc
;
1713 nd
->nl_flags
|= NLC_CREATE
;
1714 if ((error
= nlookup(nd
)) != 0)
1716 if (nd
->nl_nch
.ncp
->nc_vp
)
1718 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1722 vattr
.va_type
= VFIFO
;
1723 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1725 error
= VOP_NMKNOD(&nd
->nl_nch
, &vp
, nd
->nl_cred
, &vattr
);
1732 * mkfifo_args(char *path, int mode)
1734 * Create a named pipe.
1737 sys_mkfifo(struct mkfifo_args
*uap
)
1739 struct nlookupdata nd
;
1742 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1744 error
= kern_mkfifo(&nd
, uap
->mode
);
1749 static int hardlink_check_uid
= 0;
1750 SYSCTL_INT(_kern
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
1751 &hardlink_check_uid
, 0,
1752 "Unprivileged processes cannot create hard links to files owned by other "
1754 static int hardlink_check_gid
= 0;
1755 SYSCTL_INT(_kern
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
1756 &hardlink_check_gid
, 0,
1757 "Unprivileged processes cannot create hard links to files owned by other "
1761 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
1767 * Shortcut if disabled
1769 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
1773 * root cred can always hardlink
1775 if (suser_cred(cred
, PRISON_ROOT
) == 0)
1779 * Otherwise only if the originating file is owned by the
1780 * same user or group. Note that any group is allowed if
1781 * the file is owned by the caller.
1783 error
= VOP_GETATTR(vp
, &va
);
1787 if (hardlink_check_uid
) {
1788 if (cred
->cr_uid
!= va
.va_uid
)
1792 if (hardlink_check_gid
) {
1793 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
1801 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
1803 struct thread
*td
= curthread
;
1808 * Lookup the source and obtained a locked vnode.
1810 * XXX relookup on vget failure / race ?
1813 if ((error
= nlookup(nd
)) != 0)
1815 vp
= nd
->nl_nch
.ncp
->nc_vp
;
1816 KKASSERT(vp
!= NULL
);
1817 if (vp
->v_type
== VDIR
)
1818 return (EPERM
); /* POSIX */
1819 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1821 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
1825 * Unlock the source so we can lookup the target without deadlocking
1826 * (XXX vp is locked already, possible other deadlock?). The target
1829 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
1830 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
1831 cache_unlock(&nd
->nl_nch
);
1833 linknd
->nl_flags
|= NLC_CREATE
;
1834 if ((error
= nlookup(linknd
)) != 0) {
1838 if (linknd
->nl_nch
.ncp
->nc_vp
) {
1844 * Finally run the new API VOP.
1846 error
= can_hardlink(vp
, td
, td
->td_proc
->p_ucred
);
1848 error
= VOP_NLINK(&linknd
->nl_nch
, vp
, linknd
->nl_cred
);
1854 * link_args(char *path, char *link)
1856 * Make a hard file link.
1859 sys_link(struct link_args
*uap
)
1861 struct nlookupdata nd
, linknd
;
1864 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1866 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
1868 error
= kern_link(&nd
, &linknd
);
1869 nlookup_done(&linknd
);
1876 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
1883 nd
->nl_flags
|= NLC_CREATE
;
1884 if ((error
= nlookup(nd
)) != 0)
1886 if (nd
->nl_nch
.ncp
->nc_vp
)
1888 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1891 vattr
.va_mode
= mode
;
1892 error
= VOP_NSYMLINK(&nd
->nl_nch
, &vp
, nd
->nl_cred
, &vattr
, path
);
1899 * symlink(char *path, char *link)
1901 * Make a symbolic link.
1904 sys_symlink(struct symlink_args
*uap
)
1906 struct thread
*td
= curthread
;
1907 struct nlookupdata nd
;
1912 path
= objcache_get(namei_oc
, M_WAITOK
);
1913 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1915 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
1917 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
1918 error
= kern_symlink(&nd
, path
, mode
);
1922 objcache_put(namei_oc
, path
);
1927 * undelete_args(char *path)
1929 * Delete a whiteout from the filesystem.
1933 sys_undelete(struct undelete_args
*uap
)
1935 struct nlookupdata nd
;
1938 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1940 nd
.nl_flags
|= NLC_DELETE
;
1942 error
= nlookup(&nd
);
1944 error
= ncp_writechk(&nd
.nl_nch
);
1946 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_cred
, NAMEI_DELETE
);
1952 kern_unlink(struct nlookupdata
*nd
)
1957 nd
->nl_flags
|= NLC_DELETE
;
1958 if ((error
= nlookup(nd
)) != 0)
1960 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1962 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_cred
);
1967 * unlink_args(char *path)
1969 * Delete a name from the filesystem.
1972 sys_unlink(struct unlink_args
*uap
)
1974 struct nlookupdata nd
;
1977 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1979 error
= kern_unlink(&nd
);
1985 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
1987 struct thread
*td
= curthread
;
1988 struct proc
*p
= td
->td_proc
;
1993 fp
= holdfp(p
->p_fd
, fd
, -1);
1996 if (fp
->f_type
!= DTYPE_VNODE
) {
2003 fp
->f_offset
+= offset
;
2007 error
= VOP_GETATTR((struct vnode
*)fp
->f_data
, &vattr
);
2009 fp
->f_offset
= offset
+ vattr
.va_size
;
2012 fp
->f_offset
= offset
;
2019 *res
= fp
->f_offset
;
2026 * lseek_args(int fd, int pad, off_t offset, int whence)
2028 * Reposition read/write file offset.
2031 sys_lseek(struct lseek_args
*uap
)
2035 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2036 &uap
->sysmsg_offset
);
2042 kern_access(struct nlookupdata
*nd
, int aflags
)
2047 if ((error
= nlookup(nd
)) != 0)
2050 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2054 /* Flags == 0 means only check for existence. */
2063 if ((flags
& VWRITE
) == 0 ||
2064 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2065 error
= VOP_ACCESS(vp
, flags
, nd
->nl_cred
);
2068 * If the file handle is stale we have to re-resolve the
2069 * entry. This is a hack at the moment.
2071 if (error
== ESTALE
) {
2072 cache_setunresolved(&nd
->nl_nch
);
2073 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2086 * access_args(char *path, int flags)
2088 * Check access permissions.
2091 sys_access(struct access_args
*uap
)
2093 struct nlookupdata nd
;
2096 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2098 error
= kern_access(&nd
, uap
->flags
);
2104 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2110 if ((error
= nlookup(nd
)) != 0)
2113 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2117 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2119 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2122 * If the file handle is stale we have to re-resolve the entry. This
2123 * is a hack at the moment.
2125 if (error
== ESTALE
) {
2126 cache_setunresolved(&nd
->nl_nch
);
2127 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2138 * stat_args(char *path, struct stat *ub)
2140 * Get file status; this version follows links.
2143 sys_stat(struct stat_args
*uap
)
2145 struct nlookupdata nd
;
2149 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2151 error
= kern_stat(&nd
, &st
);
2153 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2160 * lstat_args(char *path, struct stat *ub)
2162 * Get file status; this version does not follow links.
2165 sys_lstat(struct lstat_args
*uap
)
2167 struct nlookupdata nd
;
2171 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2173 error
= kern_stat(&nd
, &st
);
2175 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2182 * pathconf_Args(char *path, int name)
2184 * Get configurable pathname variables.
2188 sys_pathconf(struct pathconf_args
*uap
)
2190 struct nlookupdata nd
;
2195 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2197 error
= nlookup(&nd
);
2199 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2202 error
= VOP_PATHCONF(vp
, uap
->name
, uap
->sysmsg_fds
);
2210 * kern_readlink isn't properly split yet. There is a copyin burried
2211 * in VOP_READLINK().
2214 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2216 struct thread
*td
= curthread
;
2217 struct proc
*p
= td
->td_proc
;
2223 if ((error
= nlookup(nd
)) != 0)
2225 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2228 if (vp
->v_type
!= VLNK
) {
2231 aiov
.iov_base
= buf
;
2232 aiov
.iov_len
= count
;
2233 auio
.uio_iov
= &aiov
;
2234 auio
.uio_iovcnt
= 1;
2235 auio
.uio_offset
= 0;
2236 auio
.uio_rw
= UIO_READ
;
2237 auio
.uio_segflg
= UIO_USERSPACE
;
2239 auio
.uio_resid
= count
;
2240 error
= VOP_READLINK(vp
, &auio
, p
->p_ucred
);
2243 *res
= count
- auio
.uio_resid
;
2248 * readlink_args(char *path, char *buf, int count)
2250 * Return target name of a symbolic link.
2253 sys_readlink(struct readlink_args
*uap
)
2255 struct nlookupdata nd
;
2258 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2260 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2261 &uap
->sysmsg_result
);
2268 setfflags(struct vnode
*vp
, int flags
)
2270 struct thread
*td
= curthread
;
2271 struct proc
*p
= td
->td_proc
;
2276 * Prevent non-root users from setting flags on devices. When
2277 * a device is reused, users can retain ownership of the device
2278 * if they are allowed to set flags and programs assume that
2279 * chown can't fail when done as root.
2281 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2282 ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0))
2286 * note: vget is required for any operation that might mod the vnode
2287 * so VINACTIVE is properly cleared.
2289 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2291 vattr
.va_flags
= flags
;
2292 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2299 * chflags(char *path, int flags)
2301 * Change flags of a file given a path name.
2305 sys_chflags(struct chflags_args
*uap
)
2307 struct nlookupdata nd
;
2312 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2313 /* XXX Add NLC flag indicating modifying operation? */
2315 error
= nlookup(&nd
);
2317 error
= ncp_writechk(&nd
.nl_nch
);
2319 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2322 error
= setfflags(vp
, uap
->flags
);
2329 * fchflags_args(int fd, int flags)
2331 * Change flags of a file given a file descriptor.
2335 sys_fchflags(struct fchflags_args
*uap
)
2337 struct thread
*td
= curthread
;
2338 struct proc
*p
= td
->td_proc
;
2342 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2344 if (fp
->f_nchandle
.ncp
)
2345 error
= ncp_writechk(&fp
->f_nchandle
);
2347 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
2353 setfmode(struct vnode
*vp
, int mode
)
2355 struct thread
*td
= curthread
;
2356 struct proc
*p
= td
->td_proc
;
2361 * note: vget is required for any operation that might mod the vnode
2362 * so VINACTIVE is properly cleared.
2364 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2366 vattr
.va_mode
= mode
& ALLPERMS
;
2367 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2374 kern_chmod(struct nlookupdata
*nd
, int mode
)
2379 /* XXX Add NLC flag indicating modifying operation? */
2380 if ((error
= nlookup(nd
)) != 0)
2382 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2384 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
2385 error
= setfmode(vp
, mode
);
2391 * chmod_args(char *path, int mode)
2393 * Change mode of a file given path name.
2397 sys_chmod(struct chmod_args
*uap
)
2399 struct nlookupdata nd
;
2402 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2404 error
= kern_chmod(&nd
, uap
->mode
);
2410 * lchmod_args(char *path, int mode)
2412 * Change mode of a file given path name (don't follow links.)
2416 sys_lchmod(struct lchmod_args
*uap
)
2418 struct nlookupdata nd
;
2421 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2423 error
= kern_chmod(&nd
, uap
->mode
);
2429 * fchmod_args(int fd, int mode)
2431 * Change mode of a file given a file descriptor.
2435 sys_fchmod(struct fchmod_args
*uap
)
2437 struct thread
*td
= curthread
;
2438 struct proc
*p
= td
->td_proc
;
2442 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2444 if (fp
->f_nchandle
.ncp
)
2445 error
= ncp_writechk(&fp
->f_nchandle
);
2447 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
2453 setfown(struct vnode
*vp
, uid_t uid
, gid_t gid
)
2455 struct thread
*td
= curthread
;
2456 struct proc
*p
= td
->td_proc
;
2461 * note: vget is required for any operation that might mod the vnode
2462 * so VINACTIVE is properly cleared.
2464 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2468 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2475 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
2480 /* XXX Add NLC flag indicating modifying operation? */
2481 if ((error
= nlookup(nd
)) != 0)
2483 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2485 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
2486 error
= setfown(vp
, uid
, gid
);
2492 * chown(char *path, int uid, int gid)
2494 * Set ownership given a path name.
2497 sys_chown(struct chown_args
*uap
)
2499 struct nlookupdata nd
;
2502 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2504 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2510 * lchown_args(char *path, int uid, int gid)
2512 * Set ownership given a path name, do not cross symlinks.
2515 sys_lchown(struct lchown_args
*uap
)
2517 struct nlookupdata nd
;
2520 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2522 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2528 * fchown_args(int fd, int uid, int gid)
2530 * Set ownership given a file descriptor.
2534 sys_fchown(struct fchown_args
*uap
)
2536 struct thread
*td
= curthread
;
2537 struct proc
*p
= td
->td_proc
;
2541 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2543 if (fp
->f_nchandle
.ncp
)
2544 error
= ncp_writechk(&fp
->f_nchandle
);
2546 error
= setfown((struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
2552 getutimes(const struct timeval
*tvp
, struct timespec
*tsp
)
2554 struct timeval tv
[2];
2558 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
2561 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
2562 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
2568 setutimes(struct vnode
*vp
, const struct timespec
*ts
, int nullflag
)
2570 struct thread
*td
= curthread
;
2571 struct proc
*p
= td
->td_proc
;
2576 * note: vget is required for any operation that might mod the vnode
2577 * so VINACTIVE is properly cleared.
2579 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2581 vattr
.va_atime
= ts
[0];
2582 vattr
.va_mtime
= ts
[1];
2584 vattr
.va_vaflags
|= VA_UTIMES_NULL
;
2585 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2592 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
2594 struct timespec ts
[2];
2598 if ((error
= getutimes(tptr
, ts
)) != 0)
2600 /* XXX Add NLC flag indicating modifying operation? */
2601 if ((error
= nlookup(nd
)) != 0)
2603 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2605 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2607 error
= setutimes(vp
, ts
, tptr
== NULL
);
2613 * utimes_args(char *path, struct timeval *tptr)
2615 * Set the access and modification times of a file.
2618 sys_utimes(struct utimes_args
*uap
)
2620 struct timeval tv
[2];
2621 struct nlookupdata nd
;
2625 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2629 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2631 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2637 * lutimes_args(char *path, struct timeval *tptr)
2639 * Set the access and modification times of a file.
2642 sys_lutimes(struct lutimes_args
*uap
)
2644 struct timeval tv
[2];
2645 struct nlookupdata nd
;
2649 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2653 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2655 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2661 kern_futimes(int fd
, struct timeval
*tptr
)
2663 struct thread
*td
= curthread
;
2664 struct proc
*p
= td
->td_proc
;
2665 struct timespec ts
[2];
2669 error
= getutimes(tptr
, ts
);
2672 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
2674 if (fp
->f_nchandle
.ncp
)
2675 error
= ncp_writechk(&fp
->f_nchandle
);
2677 error
= setutimes((struct vnode
*)fp
->f_data
, ts
, tptr
== NULL
);
2683 * futimes_args(int fd, struct timeval *tptr)
2685 * Set the access and modification times of a file.
2688 sys_futimes(struct futimes_args
*uap
)
2690 struct timeval tv
[2];
2694 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2699 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
2705 kern_truncate(struct nlookupdata
*nd
, off_t length
)
2713 /* XXX Add NLC flag indicating modifying operation? */
2714 if ((error
= nlookup(nd
)) != 0)
2716 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2718 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2720 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
2724 if (vp
->v_type
== VDIR
) {
2726 } else if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0 &&
2727 (error
= VOP_ACCESS(vp
, VWRITE
, nd
->nl_cred
)) == 0) {
2729 vattr
.va_size
= length
;
2730 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
2737 * truncate(char *path, int pad, off_t length)
2739 * Truncate a file given its path name.
2742 sys_truncate(struct truncate_args
*uap
)
2744 struct nlookupdata nd
;
2747 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2749 error
= kern_truncate(&nd
, uap
->length
);
2755 kern_ftruncate(int fd
, off_t length
)
2757 struct thread
*td
= curthread
;
2758 struct proc
*p
= td
->td_proc
;
2766 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
2768 if (fp
->f_nchandle
.ncp
) {
2769 error
= ncp_writechk(&fp
->f_nchandle
);
2773 if ((fp
->f_flag
& FWRITE
) == 0) {
2777 vp
= (struct vnode
*)fp
->f_data
;
2778 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2779 if (vp
->v_type
== VDIR
) {
2781 } else if ((error
= vn_writechk(vp
, NULL
)) == 0) {
2783 vattr
.va_size
= length
;
2784 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
2793 * ftruncate_args(int fd, int pad, off_t length)
2795 * Truncate a file given a file descriptor.
2798 sys_ftruncate(struct ftruncate_args
*uap
)
2802 error
= kern_ftruncate(uap
->fd
, uap
->length
);
2810 * Sync an open file.
2814 sys_fsync(struct fsync_args
*uap
)
2816 struct thread
*td
= curthread
;
2817 struct proc
*p
= td
->td_proc
;
2823 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2825 vp
= (struct vnode
*)fp
->f_data
;
2826 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2827 if ((obj
= vp
->v_object
) != NULL
)
2828 vm_object_page_clean(obj
, 0, 0, 0);
2829 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
)) == 0 &&
2830 vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SOFTDEP
) &&
2832 error
= (*bioops
.io_fsync
)(vp
);
2840 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
2842 struct nchandle fnchd
;
2843 struct nchandle tnchd
;
2844 struct namecache
*ncp
;
2849 if ((error
= nlookup(fromnd
)) != 0)
2851 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
2853 fnchd
.mount
= fromnd
->nl_nch
.mount
;
2857 * unlock the source nch so we can lookup the target nch without
2858 * deadlocking. The target may or may not exist so we do not check
2859 * for a target vp like kern_mkdir() and other creation functions do.
2861 * The source and target directories are ref'd and rechecked after
2862 * everything is relocked to determine if the source or target file
2865 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
2866 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2867 cache_unlock(&fromnd
->nl_nch
);
2869 tond
->nl_flags
|= NLC_CREATE
;
2870 if ((error
= nlookup(tond
)) != 0) {
2874 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
2878 tnchd
.mount
= tond
->nl_nch
.mount
;
2882 * If the source and target are the same there is nothing to do
2884 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
2891 * Mount points cannot be renamed or overwritten
2893 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
2902 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2903 * may have become invalid while it was unlocked, nc_vp and nc_mount
2906 if (cache_lock_nonblock(&fromnd
->nl_nch
) == 0) {
2907 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2908 } else if (fromnd
->nl_nch
.ncp
> tond
->nl_nch
.ncp
) {
2909 cache_lock(&fromnd
->nl_nch
);
2910 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2912 cache_unlock(&tond
->nl_nch
);
2913 cache_lock(&fromnd
->nl_nch
);
2914 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2915 cache_lock(&tond
->nl_nch
);
2916 cache_resolve(&tond
->nl_nch
, tond
->nl_cred
);
2918 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
2921 * make sure the parent directories linkages are the same
2923 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
2924 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
2931 * Both the source and target must be within the same filesystem and
2932 * in the same filesystem as their parent directories within the
2933 * namecache topology.
2935 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2938 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
2939 mp
!= tond
->nl_nch
.mount
) {
2946 * Make sure the mount point is writable
2948 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
2955 * If the target exists and either the source or target is a directory,
2956 * then both must be directories.
2958 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2961 if (tond
->nl_nch
.ncp
->nc_vp
) {
2962 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
2964 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
2965 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
2967 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
2973 * You cannot rename a source into itself or a subdirectory of itself.
2974 * We check this by travsersing the target directory upwards looking
2975 * for a match against the source.
2978 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
2979 if (fromnd
->nl_nch
.ncp
== ncp
) {
2990 * Even though the namespaces are different, they may still represent
2991 * hardlinks to the same file. The filesystem might have a hard time
2992 * with this so we issue a NREMOVE of the source instead of a NRENAME
2993 * when we detect the situation.
2996 if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
2997 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2999 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
3007 * rename_args(char *from, char *to)
3009 * Rename files. Source and destination must either both be directories,
3010 * or both not be directories. If target is a directory, it must be empty.
3013 sys_rename(struct rename_args
*uap
)
3015 struct nlookupdata fromnd
, tond
;
3018 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
3020 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
3022 error
= kern_rename(&fromnd
, &tond
);
3023 nlookup_done(&tond
);
3025 nlookup_done(&fromnd
);
3030 kern_mkdir(struct nlookupdata
*nd
, int mode
)
3032 struct thread
*td
= curthread
;
3033 struct proc
*p
= td
->td_proc
;
3039 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
;
3040 if ((error
= nlookup(nd
)) != 0)
3043 if (nd
->nl_nch
.ncp
->nc_vp
)
3045 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3049 vattr
.va_type
= VDIR
;
3050 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
3053 error
= VOP_NMKDIR(&nd
->nl_nch
, &vp
, p
->p_ucred
, &vattr
);
3060 * mkdir_args(char *path, int mode)
3062 * Make a directory file.
3066 sys_mkdir(struct mkdir_args
*uap
)
3068 struct nlookupdata nd
;
3071 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3073 error
= kern_mkdir(&nd
, uap
->mode
);
3079 kern_rmdir(struct nlookupdata
*nd
)
3084 nd
->nl_flags
|= NLC_DELETE
;
3085 if ((error
= nlookup(nd
)) != 0)
3089 * Do not allow directories representing mount points to be
3090 * deleted, even if empty. Check write perms on mount point
3091 * in case the vnode is aliased (aka nullfs).
3093 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
3095 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3098 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_cred
);
3103 * rmdir_args(char *path)
3105 * Remove a directory file.
3109 sys_rmdir(struct rmdir_args
*uap
)
3111 struct nlookupdata nd
;
3114 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3116 error
= kern_rmdir(&nd
);
3122 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
3123 enum uio_seg direction
)
3125 struct thread
*td
= curthread
;
3126 struct proc
*p
= td
->td_proc
;
3134 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3136 if ((fp
->f_flag
& FREAD
) == 0) {
3140 vp
= (struct vnode
*)fp
->f_data
;
3142 if (vp
->v_type
!= VDIR
) {
3146 aiov
.iov_base
= buf
;
3147 aiov
.iov_len
= count
;
3148 auio
.uio_iov
= &aiov
;
3149 auio
.uio_iovcnt
= 1;
3150 auio
.uio_rw
= UIO_READ
;
3151 auio
.uio_segflg
= direction
;
3153 auio
.uio_resid
= count
;
3154 loff
= auio
.uio_offset
= fp
->f_offset
;
3155 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
3156 fp
->f_offset
= auio
.uio_offset
;
3159 if (count
== auio
.uio_resid
) {
3160 if (union_dircheckp
) {
3161 error
= union_dircheckp(td
, &vp
, fp
);
3168 if ((vp
->v_flag
& VROOT
) &&
3169 (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
3170 struct vnode
*tvp
= vp
;
3171 vp
= vp
->v_mount
->mnt_vnodecovered
;
3183 *res
= count
- auio
.uio_resid
;
3190 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3192 * Read a block of directory entries in a file system independent format.
3195 sys_getdirentries(struct getdirentries_args
*uap
)
3200 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
3201 &uap
->sysmsg_result
, UIO_USERSPACE
);
3204 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
3209 * getdents_args(int fd, char *buf, size_t count)
3212 sys_getdents(struct getdents_args
*uap
)
3216 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
3217 &uap
->sysmsg_result
, UIO_USERSPACE
);
3223 * umask(int newmask)
3225 * Set the mode mask for creation of filesystem nodes.
3230 sys_umask(struct umask_args
*uap
)
3232 struct thread
*td
= curthread
;
3233 struct proc
*p
= td
->td_proc
;
3234 struct filedesc
*fdp
;
3237 uap
->sysmsg_result
= fdp
->fd_cmask
;
3238 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
3243 * revoke(char *path)
3245 * Void all references to file by ripping underlying filesystem
3250 sys_revoke(struct revoke_args
*uap
)
3252 struct nlookupdata nd
;
3259 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3261 error
= nlookup(&nd
);
3263 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3264 cred
= crhold(nd
.nl_cred
);
3267 if (vp
->v_type
!= VCHR
&& vp
->v_type
!= VBLK
)
3270 error
= VOP_GETATTR(vp
, &vattr
);
3271 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
3272 error
= suser_cred(cred
, PRISON_ROOT
);
3273 if (error
== 0 && count_udev(vp
->v_umajor
, vp
->v_uminor
) > 0) {
3276 VOP_REVOKE(vp
, REVOKEALL
);
3287 * getfh_args(char *fname, fhandle_t *fhp)
3289 * Get (NFS) file handle
3292 sys_getfh(struct getfh_args
*uap
)
3294 struct thread
*td
= curthread
;
3295 struct nlookupdata nd
;
3301 * Must be super user
3303 if ((error
= suser(td
)) != 0)
3307 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
3309 error
= nlookup(&nd
);
3311 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3314 bzero(&fh
, sizeof(fh
));
3315 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
3316 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
3319 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
3325 * fhopen_args(const struct fhandle *u_fhp, int flags)
3327 * syscall for the rpc.lockd to use to translate a NFS file handle into
3328 * an open descriptor.
3330 * warning: do not remove the suser() call or this becomes one giant
3334 sys_fhopen(struct fhopen_args
*uap
)
3336 struct thread
*td
= curthread
;
3337 struct proc
*p
= td
->td_proc
;
3342 struct vattr
*vap
= &vat
;
3344 int fmode
, mode
, error
, type
;
3350 * Must be super user
3356 fmode
= FFLAGS(uap
->flags
);
3357 /* why not allow a non-read/write open for our lockd? */
3358 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
3360 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
3363 /* find the mount point */
3364 mp
= vfs_getvfs(&fhp
.fh_fsid
);
3367 /* now give me my vnode, it gets returned to me locked */
3368 error
= VFS_FHTOVP(mp
, &fhp
.fh_fid
, &vp
);
3372 * from now on we have to make sure not
3373 * to forget about the vnode
3374 * any error that causes an abort must vput(vp)
3375 * just set error = err and 'goto bad;'.
3381 if (vp
->v_type
== VLNK
) {
3385 if (vp
->v_type
== VSOCK
) {
3390 if (fmode
& (FWRITE
| O_TRUNC
)) {
3391 if (vp
->v_type
== VDIR
) {
3395 error
= vn_writechk(vp
, NULL
);
3403 error
= VOP_ACCESS(vp
, mode
, p
->p_ucred
);
3407 if (fmode
& O_TRUNC
) {
3408 vn_unlock(vp
); /* XXX */
3409 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
3412 error
= VOP_SETATTR(vp
, vap
, p
->p_ucred
);
3418 * VOP_OPEN needs the file pointer so it can potentially override
3421 * WARNING! no f_nchandle will be associated when fhopen()ing a
3424 if ((error
= falloc(p
, &nfp
, &indx
)) != 0)
3428 error
= VOP_OPEN(vp
, fmode
, p
->p_ucred
, fp
);
3431 * setting f_ops this way prevents VOP_CLOSE from being
3432 * called or fdrop() releasing the vp from v_data. Since
3433 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3435 fp
->f_ops
= &badfileops
;
3441 * The fp is given its own reference, we still have our ref and lock.
3443 * Assert that all regular files must be created with a VM object.
3445 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
3446 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
3451 * The open was successful. Handle any locking requirements.
3453 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
3454 lf
.l_whence
= SEEK_SET
;
3457 if (fmode
& O_EXLOCK
)
3458 lf
.l_type
= F_WRLCK
;
3460 lf
.l_type
= F_RDLCK
;
3461 if (fmode
& FNONBLOCK
)
3466 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
3468 * release our private reference.
3470 fsetfd(p
, NULL
, indx
);
3475 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3476 fp
->f_flag
|= FHASLOCK
;
3480 * Clean up. Associate the file pointer with the previously
3481 * reserved descriptor and return it.
3484 fsetfd(p
, fp
, indx
);
3486 uap
->sysmsg_result
= indx
;
3490 fsetfd(p
, NULL
, indx
);
3498 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3501 sys_fhstat(struct fhstat_args
*uap
)
3503 struct thread
*td
= curthread
;
3511 * Must be super user
3517 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
3521 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3523 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3525 error
= vn_stat(vp
, &sb
, td
->td_proc
->p_ucred
);
3529 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
3534 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3537 sys_fhstatfs(struct fhstatfs_args
*uap
)
3539 struct thread
*td
= curthread
;
3540 struct proc
*p
= td
->td_proc
;
3545 char *fullpath
, *freepath
;
3550 * Must be super user
3552 if ((error
= suser(td
)))
3555 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
3558 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3561 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
))
3564 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3569 if ((error
= VFS_STATFS(mp
, sp
, p
->p_ucred
)) != 0)
3572 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
3575 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
3576 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
3577 kfree(freepath
, M_TEMP
);
3579 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3581 bcopy(sp
, &sb
, sizeof(sb
));
3582 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
3585 return (copyout(sp
, uap
->buf
, sizeof(*sp
)));
3589 * Syscall to push extended attribute configuration information into the
3590 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3591 * a command (int cmd), and attribute name and misc data. For now, the
3592 * attribute name is left in userspace for consumption by the VFS_op.
3593 * It will probably be changed to be copied into sysspace by the
3594 * syscall in the future, once issues with various consumers of the
3595 * attribute code have raised their hands.
3597 * Currently this is used only by UFS Extended Attributes.
3600 sys_extattrctl(struct extattrctl_args
*uap
)
3602 struct nlookupdata nd
;
3608 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3610 error
= nlookup(&nd
);
3612 mp
= nd
.nl_nch
.mount
;
3613 error
= VFS_EXTATTRCTL(mp
, uap
->cmd
,
3614 uap
->attrname
, uap
->arg
,
3622 * Syscall to set a named extended attribute on a file or directory.
3623 * Accepts attribute name, and a uio structure pointing to the data to set.
3624 * The uio is consumed in the style of writev(). The real work happens
3625 * in VOP_SETEXTATTR().
3628 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
3630 char attrname
[EXTATTR_MAXNAMELEN
];
3631 struct iovec aiov
[UIO_SMALLIOV
];
3632 struct iovec
*needfree
;
3633 struct nlookupdata nd
;
3642 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3647 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3649 error
= nlookup(&nd
);
3651 error
= ncp_writechk(&nd
.nl_nch
);
3653 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3660 iovlen
= uap
->iovcnt
* sizeof(struct iovec
);
3661 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3662 if (uap
->iovcnt
> UIO_MAXIOV
) {
3666 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3672 auio
.uio_iovcnt
= uap
->iovcnt
;
3673 auio
.uio_rw
= UIO_WRITE
;
3674 auio
.uio_segflg
= UIO_USERSPACE
;
3675 auio
.uio_td
= nd
.nl_td
;
3676 auio
.uio_offset
= 0;
3677 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3680 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3681 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3685 auio
.uio_resid
+= iov
->iov_len
;
3688 cnt
= auio
.uio_resid
;
3689 error
= VOP_SETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
3690 cnt
-= auio
.uio_resid
;
3691 uap
->sysmsg_result
= cnt
;
3696 FREE(needfree
, M_IOV
);
3701 * Syscall to get a named extended attribute on a file or directory.
3702 * Accepts attribute name, and a uio structure pointing to a buffer for the
3703 * data. The uio is consumed in the style of readv(). The real work
3704 * happens in VOP_GETEXTATTR();
3707 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
3709 char attrname
[EXTATTR_MAXNAMELEN
];
3710 struct iovec aiov
[UIO_SMALLIOV
];
3711 struct iovec
*needfree
;
3712 struct nlookupdata nd
;
3721 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3726 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3728 error
= nlookup(&nd
);
3730 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3736 iovlen
= uap
->iovcnt
* sizeof (struct iovec
);
3738 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3739 if (uap
->iovcnt
> UIO_MAXIOV
) {
3743 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3749 auio
.uio_iovcnt
= uap
->iovcnt
;
3750 auio
.uio_rw
= UIO_READ
;
3751 auio
.uio_segflg
= UIO_USERSPACE
;
3752 auio
.uio_td
= nd
.nl_td
;
3753 auio
.uio_offset
= 0;
3754 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3757 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3758 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3762 auio
.uio_resid
+= iov
->iov_len
;
3765 cnt
= auio
.uio_resid
;
3766 error
= VOP_GETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
3767 cnt
-= auio
.uio_resid
;
3768 uap
->sysmsg_result
= cnt
;
3773 FREE(needfree
, M_IOV
);
3778 * Syscall to delete a named extended attribute from a file or directory.
3779 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3782 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
3784 char attrname
[EXTATTR_MAXNAMELEN
];
3785 struct nlookupdata nd
;
3789 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3794 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3796 error
= nlookup(&nd
);
3798 error
= ncp_writechk(&nd
.nl_nch
);
3800 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3806 error
= VOP_SETEXTATTR(vp
, attrname
, NULL
, nd
.nl_cred
);
3813 * Determine if the mount is visible to the process.
3816 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
3818 struct nchandle nch
;
3821 * Traverse from the mount point upwards. If we hit the process
3822 * root then the mount point is visible to the process.
3824 nch
= mp
->mnt_ncmountpt
;
3826 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
3827 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
3830 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
3831 nch
= nch
.mount
->mnt_ncmounton
;
3833 nch
.ncp
= nch
.ncp
->nc_parent
;
3838 * If the mount point is not visible to the process, but the
3839 * process root is in a subdirectory of the mount, return
3842 if (p
->p_fd
->fd_nrdir
.mount
== mp
)