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.124 2008/01/04 12:16:19 matthias 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>
71 #include <sys/file2.h>
72 #include <sys/spinlock2.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
78 #include <machine/limits.h>
79 #include <machine/stdarg.h>
81 #include <vfs/union/union.h>
83 static void mount_warning(struct mount
*mp
, const char *ctl
, ...);
84 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
85 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
86 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
87 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
88 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
89 static int getutimes (const struct timeval
*, struct timespec
*);
90 static int setfown (struct vnode
*, uid_t
, gid_t
);
91 static int setfmode (struct vnode
*, int);
92 static int setfflags (struct vnode
*, int);
93 static int setutimes (struct vnode
*, const struct timespec
*, int);
94 static int usermount
= 0; /* if 1, non-root can mount fs. */
96 int (*union_dircheckp
) (struct thread
*, struct vnode
**, struct file
*);
98 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0, "");
101 * Virtual File System System Calls
105 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
112 sys_mount(struct mount_args
*uap
)
114 struct thread
*td
= curthread
;
115 struct proc
*p
= td
->td_proc
;
119 struct vfsconf
*vfsp
;
120 int error
, flag
= 0, flag2
= 0;
123 struct nlookupdata nd
;
124 char fstypename
[MFSNAMELEN
];
125 struct ucred
*cred
= p
->p_ucred
;
128 if (cred
->cr_prison
!= NULL
)
130 if (usermount
== 0 && (error
= suser(td
)))
133 * Do not allow NFS export by non-root users.
135 if (uap
->flags
& MNT_EXPORTED
) {
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
144 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
147 * Lookup the requested path and extract the nch and vnode.
149 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
151 if ((error
= nlookup(&nd
)) == 0) {
152 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
162 * Extract the locked+refd ncp and cleanup the nd structure
165 cache_zero(&nd
.nl_nch
);
168 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) && cache_findmount(&nch
))
175 * now we have the locked ref'd nch and unreferenced vnode.
178 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
185 * Now we have an unlocked ref'd nch and a locked ref'd vp
187 if (uap
->flags
& MNT_UPDATE
) {
188 if ((vp
->v_flag
& VROOT
) == 0) {
195 flag2
= mp
->mnt_kern_flag
;
197 * We only allow the filesystem to be reloaded if it
198 * is currently mounted read-only.
200 if ((uap
->flags
& MNT_RELOAD
) &&
201 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
204 return (EOPNOTSUPP
); /* Needs translation */
207 * Only root, or the user that did the original mount is
208 * permitted to update it.
210 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
211 (error
= suser(td
))) {
216 if (vfs_busy(mp
, LK_NOWAIT
)) {
221 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
227 vp
->v_flag
|= VMOUNT
;
229 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
234 * If the user is not root, ensure that they own the directory
235 * onto which we are attempting to mount.
237 if ((error
= VOP_GETATTR(vp
, &va
)) ||
238 (va
.va_uid
!= cred
->cr_uid
&& (error
= suser(td
)))) {
243 if ((error
= vinvalbuf(vp
, V_SAVE
, 0, 0)) != 0) {
248 if (vp
->v_type
!= VDIR
) {
253 if (vp
->v_mount
->mnt_kern_flag
& MNTK_NOSTKMNT
) {
258 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
263 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
264 if (!strcmp(vfsp
->vfc_name
, fstypename
))
270 /* Only load modules for root (very important!) */
271 if ((error
= suser(td
)) != 0) {
276 error
= linker_load_file(fstypename
, &lf
);
277 if (error
|| lf
== NULL
) {
285 /* lookup again, see if the VFS was loaded */
286 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
287 if (!strcmp(vfsp
->vfc_name
, fstypename
))
292 linker_file_unload(lf
);
298 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
303 vp
->v_flag
|= VMOUNT
;
306 * Allocate and initialize the filesystem.
308 mp
= kmalloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
309 TAILQ_INIT(&mp
->mnt_nvnodelist
);
310 TAILQ_INIT(&mp
->mnt_reservedvnlist
);
311 TAILQ_INIT(&mp
->mnt_jlist
);
312 mp
->mnt_nvnodelistsize
= 0;
313 lockinit(&mp
->mnt_lock
, "vfslock", 0, 0);
314 vfs_busy(mp
, LK_NOWAIT
);
315 mp
->mnt_op
= vfsp
->vfc_vfsops
;
317 vfsp
->vfc_refcount
++;
318 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
319 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
320 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
321 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
322 mp
->mnt_iosize_max
= DFLTPHYS
;
326 * Set the mount level flags.
328 if (uap
->flags
& MNT_RDONLY
)
329 mp
->mnt_flag
|= MNT_RDONLY
;
330 else if (mp
->mnt_flag
& MNT_RDONLY
)
331 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
332 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
333 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_NOATIME
|
334 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
335 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
336 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
337 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_FORCE
|
338 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
339 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
341 * Mount the filesystem.
342 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
345 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
346 if (mp
->mnt_flag
& MNT_UPDATE
) {
347 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
348 mp
->mnt_flag
&= ~MNT_RDONLY
;
349 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
350 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
353 mp
->mnt_kern_flag
= flag2
;
356 vp
->v_flag
&= ~VMOUNT
;
361 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
363 * Put the new filesystem on the mount list after root. The mount
364 * point gets its own mnt_ncmountpt (unless the VFS already set one
365 * up) which represents the root of the mount. The lookup code
366 * detects the mount point going forward and checks the root of
367 * the mount going backwards.
369 * It is not necessary to invalidate or purge the vnode underneath
370 * because elements under the mount will be given their own glue
374 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
376 * allocate, then unlock, but leave the ref intact
378 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
379 cache_unlock(&mp
->mnt_ncmountpt
);
381 mp
->mnt_ncmounton
= nch
; /* inherits ref */
382 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
384 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
385 vp
->v_flag
&= ~VMOUNT
;
386 mountlist_insert(mp
, MNTINS_LAST
);
387 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
389 error
= vfs_allocate_syncvnode(mp
);
391 error
= VFS_START(mp
, 0);
394 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
395 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
396 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
397 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
398 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
399 vp
->v_flag
&= ~VMOUNT
;
400 mp
->mnt_vfc
->vfc_refcount
--;
410 * Scan all active processes to see if any of them have a current
411 * or root directory onto which the new filesystem has just been
412 * mounted. If so, replace them with the new mount point.
414 * The passed ncp is ref'd and locked (from the mount code) and
415 * must be associated with the vnode representing the root of the
418 struct checkdirs_info
{
419 struct nchandle old_nch
;
420 struct nchandle new_nch
;
421 struct vnode
*old_vp
;
422 struct vnode
*new_vp
;
425 static int checkdirs_callback(struct proc
*p
, void *data
);
428 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
430 struct checkdirs_info info
;
436 * If the old mount point's vnode has a usecount of 1, it is not
437 * being held as a descriptor anywhere.
439 olddp
= old_nch
->ncp
->nc_vp
;
440 if (olddp
== NULL
|| olddp
->v_sysref
.refcnt
== 1)
444 * Force the root vnode of the new mount point to be resolved
445 * so we can update any matching processes.
448 if (VFS_ROOT(mp
, &newdp
))
449 panic("mount: lost mount");
450 cache_setunresolved(new_nch
);
451 cache_setvp(new_nch
, newdp
);
454 * Special handling of the root node
456 if (rootvnode
== olddp
) {
458 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
462 * Pass newdp separately so the callback does not have to access
463 * it via new_nch->ncp->nc_vp.
465 info
.old_nch
= *old_nch
;
466 info
.new_nch
= *new_nch
;
468 allproc_scan(checkdirs_callback
, &info
);
473 * NOTE: callback is not MP safe because the scanned process's filedesc
474 * structure can be ripped out from under us, amoung other things.
477 checkdirs_callback(struct proc
*p
, void *data
)
479 struct checkdirs_info
*info
= data
;
480 struct filedesc
*fdp
;
481 struct nchandle ncdrop1
;
482 struct nchandle ncdrop2
;
483 struct vnode
*vprele1
;
484 struct vnode
*vprele2
;
486 if ((fdp
= p
->p_fd
) != NULL
) {
487 cache_zero(&ncdrop1
);
488 cache_zero(&ncdrop2
);
493 * MPUNSAFE - XXX fdp can be pulled out from under a
496 * A shared filedesc is ok, we don't have to copy it
497 * because we are making this change globally.
499 spin_lock_wr(&fdp
->fd_spin
);
500 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
501 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
502 vprele1
= fdp
->fd_cdir
;
504 fdp
->fd_cdir
= info
->new_vp
;
505 ncdrop1
= fdp
->fd_ncdir
;
506 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
508 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
509 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
510 vprele2
= fdp
->fd_rdir
;
512 fdp
->fd_rdir
= info
->new_vp
;
513 ncdrop2
= fdp
->fd_nrdir
;
514 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
516 spin_unlock_wr(&fdp
->fd_spin
);
518 cache_drop(&ncdrop1
);
520 cache_drop(&ncdrop2
);
530 * Unmount a file system.
532 * Note: unmount takes a path to the vnode mounted on as argument,
533 * not special file (as before).
536 * umount_args(char *path, int flags)
540 sys_unmount(struct unmount_args
*uap
)
542 struct thread
*td
= curthread
;
543 struct proc
*p
= td
->td_proc
;
544 struct mount
*mp
= NULL
;
546 struct nlookupdata nd
;
549 if (p
->p_ucred
->cr_prison
!= NULL
)
551 if (usermount
== 0 && (error
= suser(td
)))
554 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
556 error
= nlookup(&nd
);
560 mp
= nd
.nl_nch
.mount
;
563 * Only root, or the user that did the original mount is
564 * permitted to unmount this filesystem.
566 if ((mp
->mnt_stat
.f_owner
!= p
->p_ucred
->cr_uid
) &&
571 * Don't allow unmounting the root file system.
573 if (mp
->mnt_flag
& MNT_ROOTFS
) {
579 * Must be the root of the filesystem
581 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
590 return (dounmount(mp
, uap
->flags
));
594 * Do the actual file system unmount.
597 dounmount_interlock(struct mount
*mp
)
599 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
601 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
606 dounmount(struct mount
*mp
, int flags
)
608 struct namecache
*ncp
;
616 * Exclusive access for unmounting purposes
618 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
622 * Allow filesystems to detect that a forced unmount is in progress.
624 if (flags
& MNT_FORCE
)
625 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
626 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_NOWAIT
);
627 error
= lockmgr(&mp
->mnt_lock
, lflags
);
629 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
630 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
635 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
636 vfs_setpublicfs(NULL
, NULL
, NULL
);
638 vfs_msync(mp
, MNT_WAIT
);
639 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
640 mp
->mnt_flag
&=~ MNT_ASYNC
;
643 * If this filesystem isn't aliasing other filesystems,
644 * try to invalidate any remaining namecache entries and
645 * check the count afterwords.
647 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
648 cache_lock(&mp
->mnt_ncmountpt
);
649 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
650 cache_unlock(&mp
->mnt_ncmountpt
);
652 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
653 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
655 if ((flags
& MNT_FORCE
) == 0) {
657 mount_warning(mp
, "Cannot unmount: "
663 mount_warning(mp
, "Forced unmount: "
674 * nchandle records ref the mount structure. Expect a count of 1
675 * (our mount->mnt_ncmountpt).
677 if (mp
->mnt_refs
!= 1) {
678 if ((flags
& MNT_FORCE
) == 0) {
679 mount_warning(mp
, "Cannot unmount: "
680 "%d process references still "
681 "present", mp
->mnt_refs
);
684 mount_warning(mp
, "Forced unmount: "
685 "%d process references still "
686 "present", mp
->mnt_refs
);
692 if (mp
->mnt_syncer
!= NULL
)
693 vrele(mp
->mnt_syncer
);
694 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
695 (error
= VFS_SYNC(mp
, MNT_WAIT
)) == 0) ||
696 (flags
& MNT_FORCE
)) {
697 error
= VFS_UNMOUNT(mp
, flags
);
701 if (mp
->mnt_syncer
== NULL
)
702 vfs_allocate_syncvnode(mp
);
703 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
704 mp
->mnt_flag
|= async_flag
;
705 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
706 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
711 * Clean up any journals still associated with the mount after
712 * filesystem activity has ceased.
714 journal_remove_all_journals(mp
,
715 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
717 mountlist_remove(mp
);
720 * Remove any installed vnode ops here so the individual VFSs don't
723 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
724 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
725 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
726 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
727 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
729 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
730 nch
= mp
->mnt_ncmountpt
;
731 cache_zero(&mp
->mnt_ncmountpt
);
732 cache_clrmountpt(&nch
);
735 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
736 nch
= mp
->mnt_ncmounton
;
737 cache_zero(&mp
->mnt_ncmounton
);
738 cache_clrmountpt(&nch
);
742 mp
->mnt_vfc
->vfc_refcount
--;
743 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
744 panic("unmount: dangling vnode");
745 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
746 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
755 mount_warning(struct mount
*mp
, const char *ctl
, ...)
762 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, &ptr
, &buf
) == 0) {
763 kprintf("unmount(%s): ", ptr
);
768 kprintf("unmount(%p", mp
);
769 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
770 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
779 * Shim cache_fullpath() to handle the case where a process is chrooted into
780 * a subdirectory of a mount. In this case if the root mount matches the
781 * process root directory's mount we have to specify the process's root
782 * directory instead of the mount point, because the mount point might
783 * be above the root directory.
787 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
789 struct nchandle
*nch
;
791 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
792 nch
= &p
->p_fd
->fd_nrdir
;
794 nch
= &mp
->mnt_ncmountpt
;
795 return(cache_fullpath(p
, nch
, rb
, fb
));
799 * Sync each mounted filesystem.
803 static int syncprt
= 0;
804 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
807 static int sync_callback(struct mount
*mp
, void *data
);
811 sys_sync(struct sync_args
*uap
)
813 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
816 * print out buffer pool stat information on each sync() call.
826 sync_callback(struct mount
*mp
, void *data __unused
)
830 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
831 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
832 mp
->mnt_flag
&= ~MNT_ASYNC
;
833 vfs_msync(mp
, MNT_NOWAIT
);
834 VFS_SYNC(mp
, MNT_NOWAIT
);
835 mp
->mnt_flag
|= asyncflag
;
840 /* XXX PRISON: could be per prison flag */
841 static int prison_quotas
;
843 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
847 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
849 * Change filesystem quotas.
853 sys_quotactl(struct quotactl_args
*uap
)
855 struct nlookupdata nd
;
863 if (p
->p_ucred
->cr_prison
&& !prison_quotas
)
866 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
868 error
= nlookup(&nd
);
870 mp
= nd
.nl_nch
.mount
;
871 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
872 uap
->arg
, nd
.nl_cred
);
879 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
880 * void *buf, int buflen)
882 * This function operates on a mount point and executes the specified
883 * operation using the specified control data, and possibly returns data.
885 * The actual number of bytes stored in the result buffer is returned, 0
886 * if none, otherwise an error is returned.
890 sys_mountctl(struct mountctl_args
*uap
)
892 struct thread
*td
= curthread
;
893 struct proc
*p
= td
->td_proc
;
901 * Sanity and permissions checks. We must be root.
904 if (p
->p_ucred
->cr_prison
!= NULL
)
906 if ((error
= suser(td
)) != 0)
910 * Argument length checks
912 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
914 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
916 if (uap
->path
== NULL
)
920 * Allocate the necessary buffers and copyin data
922 path
= objcache_get(namei_oc
, M_WAITOK
);
923 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
928 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
929 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
934 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
937 * Validate the descriptor
940 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
950 * Execute the internal kernel function and clean up.
952 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
955 if (error
== 0 && uap
->sysmsg_result
> 0)
956 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
959 objcache_put(namei_oc
, path
);
968 * Execute a mount control operation by resolving the path to a mount point
969 * and calling vop_mountctl().
972 kern_mountctl(const char *path
, int op
, struct file
*fp
,
973 const void *ctl
, int ctllen
,
974 void *buf
, int buflen
, int *res
)
978 struct nlookupdata nd
;
983 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
985 error
= nlookup(&nd
);
987 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
995 * Must be the root of the filesystem
997 if ((vp
->v_flag
& VROOT
) == 0) {
1001 error
= vop_mountctl(mp
->mnt_vn_use_ops
, op
, fp
, ctl
, ctllen
,
1008 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1010 struct thread
*td
= curthread
;
1011 struct proc
*p
= td
->td_proc
;
1014 char *fullpath
, *freepath
;
1017 if ((error
= nlookup(nd
)) != 0)
1019 mp
= nd
->nl_nch
.mount
;
1021 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1024 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1027 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1028 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1029 kfree(freepath
, M_TEMP
);
1031 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1032 bcopy(sp
, buf
, sizeof(*buf
));
1033 /* Only root should have access to the fsid's. */
1035 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1040 * statfs_args(char *path, struct statfs *buf)
1042 * Get filesystem statistics.
1045 sys_statfs(struct statfs_args
*uap
)
1047 struct nlookupdata nd
;
1051 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1053 error
= kern_statfs(&nd
, &buf
);
1056 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1061 kern_fstatfs(int fd
, struct statfs
*buf
)
1063 struct thread
*td
= curthread
;
1064 struct proc
*p
= td
->td_proc
;
1068 char *fullpath
, *freepath
;
1072 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1074 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1079 if (fp
->f_cred
== NULL
) {
1084 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1087 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1089 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1090 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1091 kfree(freepath
, M_TEMP
);
1093 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1094 bcopy(sp
, buf
, sizeof(*buf
));
1096 /* Only root should have access to the fsid's. */
1098 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1106 * fstatfs_args(int fd, struct statfs *buf)
1108 * Get filesystem statistics.
1111 sys_fstatfs(struct fstatfs_args
*uap
)
1116 error
= kern_fstatfs(uap
->fd
, &buf
);
1119 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1124 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1126 * Get statistics on all filesystems.
1129 struct getfsstat_info
{
1130 struct statfs
*sfsp
;
1138 static int getfsstat_callback(struct mount
*, void *);
1142 sys_getfsstat(struct getfsstat_args
*uap
)
1144 struct thread
*td
= curthread
;
1145 struct proc
*p
= td
->td_proc
;
1146 struct getfsstat_info info
;
1148 bzero(&info
, sizeof(info
));
1150 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1151 info
.sfsp
= uap
->buf
;
1153 info
.flags
= uap
->flags
;
1156 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1157 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1158 uap
->sysmsg_result
= info
.maxcount
;
1160 uap
->sysmsg_result
= info
.count
;
1161 return (info
.error
);
1165 getfsstat_callback(struct mount
*mp
, void *data
)
1167 struct getfsstat_info
*info
= data
;
1173 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1174 if (info
->p
&& !chroot_visible_mnt(mp
, info
->p
))
1179 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1180 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1181 * overrides MNT_WAIT.
1183 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1184 (info
->flags
& MNT_WAIT
)) &&
1185 (error
= VFS_STATFS(mp
, sp
, info
->p
->p_ucred
))) {
1188 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1190 error
= mount_path(info
->p
, mp
, &fullpath
, &freepath
);
1192 info
->error
= error
;
1195 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1196 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1197 kfree(freepath
, M_TEMP
);
1199 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1201 info
->error
= error
;
1211 * fchdir_args(int fd)
1213 * Change current working directory to a given file descriptor.
1217 sys_fchdir(struct fchdir_args
*uap
)
1219 struct thread
*td
= curthread
;
1220 struct proc
*p
= td
->td_proc
;
1221 struct filedesc
*fdp
= p
->p_fd
;
1222 struct vnode
*vp
, *ovp
;
1225 struct nchandle nch
, onch
, tnch
;
1228 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1230 vp
= (struct vnode
*)fp
->f_data
;
1232 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1233 if (vp
->v_type
!= VDIR
|| fp
->f_nchandle
.ncp
== NULL
)
1236 error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
);
1242 cache_copy(&fp
->f_nchandle
, &nch
);
1245 * If the ncp has become a mount point, traverse through
1249 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1250 (mp
= cache_findmount(&nch
)) != NULL
1252 error
= nlookup_mp(mp
, &tnch
);
1254 cache_unlock(&tnch
); /* leave ref intact */
1256 vp
= tnch
.ncp
->nc_vp
;
1257 error
= vget(vp
, LK_SHARED
);
1258 KKASSERT(error
== 0);
1265 onch
= fdp
->fd_ncdir
;
1266 vn_unlock(vp
); /* leave ref intact */
1268 fdp
->fd_ncdir
= nch
;
1280 kern_chdir(struct nlookupdata
*nd
)
1282 struct thread
*td
= curthread
;
1283 struct proc
*p
= td
->td_proc
;
1284 struct filedesc
*fdp
= p
->p_fd
;
1285 struct vnode
*vp
, *ovp
;
1286 struct nchandle onch
;
1289 if ((error
= nlookup(nd
)) != 0)
1291 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1293 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1296 error
= checkvp_chdir(vp
, td
);
1300 onch
= fdp
->fd_ncdir
;
1301 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1302 fdp
->fd_ncdir
= nd
->nl_nch
;
1306 cache_zero(&nd
->nl_nch
);
1314 * chdir_args(char *path)
1316 * Change current working directory (``.'').
1319 sys_chdir(struct chdir_args
*uap
)
1321 struct nlookupdata nd
;
1324 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1326 error
= kern_chdir(&nd
);
1332 * Helper function for raised chroot(2) security function: Refuse if
1333 * any filedescriptors are open directories.
1336 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1343 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1344 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1346 vp
= (struct vnode
*)fp
->f_data
;
1347 if (vp
->v_type
!= VDIR
) {
1358 * This sysctl determines if we will allow a process to chroot(2) if it
1359 * has a directory open:
1360 * 0: disallowed for all processes.
1361 * 1: allowed for processes that were not already chroot(2)'ed.
1362 * 2: allowed for all processes.
1365 static int chroot_allow_open_directories
= 1;
1367 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1368 &chroot_allow_open_directories
, 0, "");
1371 * chroot to the specified namecache entry. We obtain the vp from the
1372 * namecache data. The passed ncp must be locked and referenced and will
1373 * remain locked and referenced on return.
1376 kern_chroot(struct nchandle
*nch
)
1378 struct thread
*td
= curthread
;
1379 struct proc
*p
= td
->td_proc
;
1380 struct filedesc
*fdp
= p
->p_fd
;
1385 * Only root can chroot
1387 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0)
1391 * Disallow open directory descriptors (fchdir() breakouts).
1393 if (chroot_allow_open_directories
== 0 ||
1394 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1395 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1398 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1401 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1405 * Check the validity of vp as a directory to change to and
1406 * associate it with rdir/jdir.
1408 error
= checkvp_chdir(vp
, td
);
1409 vn_unlock(vp
); /* leave reference intact */
1411 vrele(fdp
->fd_rdir
);
1412 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1413 cache_drop(&fdp
->fd_nrdir
);
1414 cache_copy(nch
, &fdp
->fd_nrdir
);
1415 if (fdp
->fd_jdir
== NULL
) {
1418 cache_copy(nch
, &fdp
->fd_njdir
);
1427 * chroot_args(char *path)
1429 * Change notion of root (``/'') directory.
1433 sys_chroot(struct chroot_args
*uap
)
1435 struct thread
*td
= curthread
;
1436 struct nlookupdata nd
;
1439 KKASSERT(td
->td_proc
);
1440 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1445 error
= nlookup(&nd
);
1447 error
= kern_chroot(&nd
.nl_nch
);
1453 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1454 * determine whether it is legal to chdir to the vnode. The vnode's state
1455 * is not changed by this call.
1458 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1462 if (vp
->v_type
!= VDIR
)
1465 error
= VOP_ACCESS(vp
, VEXEC
, td
->td_proc
->p_ucred
);
1470 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1472 struct thread
*td
= curthread
;
1473 struct proc
*p
= td
->td_proc
;
1474 struct lwp
*lp
= td
->td_lwp
;
1475 struct filedesc
*fdp
= p
->p_fd
;
1480 int type
, indx
, error
;
1483 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1485 flags
= FFLAGS(oflags
);
1486 error
= falloc(p
, &nfp
, NULL
);
1490 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) &~ S_ISTXT
;
1493 * XXX p_dupfd is a real mess. It allows a device to return a
1494 * file descriptor to be duplicated rather then doing the open
1500 * Call vn_open() to do the lookup and assign the vnode to the
1501 * file pointer. vn_open() does not change the ref count on fp
1502 * and the vnode, on success, will be inherited by the file pointer
1505 nd
->nl_flags
|= NLC_LOCKVP
;
1506 error
= vn_open(nd
, fp
, flags
, cmode
);
1510 * handle special fdopen() case. bleh. dupfdopen() is
1511 * responsible for dropping the old contents of ofiles[indx]
1514 * Note that fsetfd() will add a ref to fp which represents
1515 * the fd_files[] assignment. We must still drop our
1518 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1519 if (fdalloc(p
, 0, &indx
) == 0) {
1520 error
= dupfdopen(p
, indx
, lp
->lwp_dupfd
, flags
, error
);
1523 fdrop(fp
); /* our ref */
1526 fsetfd(p
, NULL
, indx
);
1529 fdrop(fp
); /* our ref */
1530 if (error
== ERESTART
)
1536 * ref the vnode for ourselves so it can't be ripped out from under
1537 * is. XXX need an ND flag to request that the vnode be returned
1540 * Reserve a file descriptor but do not assign it until the open
1543 vp
= (struct vnode
*)fp
->f_data
;
1545 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1552 * If no error occurs the vp will have been assigned to the file
1557 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1558 lf
.l_whence
= SEEK_SET
;
1561 if (flags
& O_EXLOCK
)
1562 lf
.l_type
= F_WRLCK
;
1564 lf
.l_type
= F_RDLCK
;
1565 if (flags
& FNONBLOCK
)
1570 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1572 * lock request failed. Clean up the reserved
1576 fsetfd(p
, NULL
, indx
);
1580 fp
->f_flag
|= FHASLOCK
;
1584 * Assert that all regular file vnodes were created with a object.
1586 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1587 ("open: regular file has no backing object after vn_open"));
1593 * release our private reference, leaving the one associated with the
1594 * descriptor table intact.
1596 fsetfd(p
, fp
, indx
);
1603 * open_args(char *path, int flags, int mode)
1605 * Check permissions, allocate an open file structure,
1606 * and call the device open routine if any.
1609 sys_open(struct open_args
*uap
)
1611 struct nlookupdata nd
;
1614 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1616 error
= kern_open(&nd
, uap
->flags
,
1617 uap
->mode
, &uap
->sysmsg_result
);
1624 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
1626 struct thread
*td
= curthread
;
1627 struct proc
*p
= td
->td_proc
;
1636 switch (mode
& S_IFMT
) {
1642 error
= suser_cred(p
->p_ucred
, PRISON_ROOT
);
1649 nd
->nl_flags
|= NLC_CREATE
;
1650 if ((error
= nlookup(nd
)) != 0)
1652 if (nd
->nl_nch
.ncp
->nc_vp
)
1654 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1656 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
1658 /* vhold(dvp); - DVP can't go away */
1661 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1662 vattr
.va_rmajor
= rmajor
;
1663 vattr
.va_rminor
= rminor
;
1666 switch (mode
& S_IFMT
) {
1667 case S_IFMT
: /* used by badsect to flag bad sectors */
1668 vattr
.va_type
= VBAD
;
1671 vattr
.va_type
= VCHR
;
1674 vattr
.va_type
= VBLK
;
1685 error
= VOP_NWHITEOUT(&nd
->nl_nch
, dvp
, nd
->nl_cred
, NAMEI_CREATE
);
1688 error
= VOP_NMKNOD(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
);
1698 * mknod_args(char *path, int mode, int dev)
1700 * Create a special file.
1703 sys_mknod(struct mknod_args
*uap
)
1705 struct nlookupdata nd
;
1708 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1710 error
= kern_mknod(&nd
, uap
->mode
,
1711 umajor(uap
->dev
), uminor(uap
->dev
));
1718 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
1720 struct thread
*td
= curthread
;
1721 struct proc
*p
= td
->td_proc
;
1729 nd
->nl_flags
|= NLC_CREATE
;
1730 if ((error
= nlookup(nd
)) != 0)
1732 if (nd
->nl_nch
.ncp
->nc_vp
)
1734 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1736 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
1738 /* vhold(dvp); - DVP can't go away */
1741 vattr
.va_type
= VFIFO
;
1742 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1744 error
= VOP_NMKNOD(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
);
1752 * mkfifo_args(char *path, int mode)
1754 * Create a named pipe.
1757 sys_mkfifo(struct mkfifo_args
*uap
)
1759 struct nlookupdata nd
;
1762 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1764 error
= kern_mkfifo(&nd
, uap
->mode
);
1769 static int hardlink_check_uid
= 0;
1770 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
1771 &hardlink_check_uid
, 0,
1772 "Unprivileged processes cannot create hard links to files owned by other "
1774 static int hardlink_check_gid
= 0;
1775 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
1776 &hardlink_check_gid
, 0,
1777 "Unprivileged processes cannot create hard links to files owned by other "
1781 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
1787 * Shortcut if disabled
1789 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
1793 * root cred can always hardlink
1795 if (suser_cred(cred
, PRISON_ROOT
) == 0)
1799 * Otherwise only if the originating file is owned by the
1800 * same user or group. Note that any group is allowed if
1801 * the file is owned by the caller.
1803 error
= VOP_GETATTR(vp
, &va
);
1807 if (hardlink_check_uid
) {
1808 if (cred
->cr_uid
!= va
.va_uid
)
1812 if (hardlink_check_gid
) {
1813 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
1821 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
1823 struct thread
*td
= curthread
;
1829 * Lookup the source and obtained a locked vnode.
1831 * XXX relookup on vget failure / race ?
1834 if ((error
= nlookup(nd
)) != 0)
1836 vp
= nd
->nl_nch
.ncp
->nc_vp
;
1837 KKASSERT(vp
!= NULL
);
1838 if (vp
->v_type
== VDIR
)
1839 return (EPERM
); /* POSIX */
1840 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1842 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
1846 * Unlock the source so we can lookup the target without deadlocking
1847 * (XXX vp is locked already, possible other deadlock?). The target
1850 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
1851 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
1852 cache_unlock(&nd
->nl_nch
);
1854 linknd
->nl_flags
|= NLC_CREATE
;
1855 if ((error
= nlookup(linknd
)) != 0) {
1859 if (linknd
->nl_nch
.ncp
->nc_vp
) {
1863 if ((dvp
= linknd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
) {
1867 /* vhold(dvp); - dvp can't go away */
1870 * Finally run the new API VOP.
1872 error
= can_hardlink(vp
, td
, td
->td_proc
->p_ucred
);
1874 error
= VOP_NLINK(&linknd
->nl_nch
, dvp
, vp
, linknd
->nl_cred
);
1881 * link_args(char *path, char *link)
1883 * Make a hard file link.
1886 sys_link(struct link_args
*uap
)
1888 struct nlookupdata nd
, linknd
;
1891 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1893 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
1895 error
= kern_link(&nd
, &linknd
);
1896 nlookup_done(&linknd
);
1903 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
1911 nd
->nl_flags
|= NLC_CREATE
;
1912 if ((error
= nlookup(nd
)) != 0)
1914 if (nd
->nl_nch
.ncp
->nc_vp
)
1916 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
1918 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
1920 /* vhold(dvp); - dvp can't go away */
1922 vattr
.va_mode
= mode
;
1923 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
1931 * symlink(char *path, char *link)
1933 * Make a symbolic link.
1936 sys_symlink(struct symlink_args
*uap
)
1938 struct thread
*td
= curthread
;
1939 struct nlookupdata nd
;
1944 path
= objcache_get(namei_oc
, M_WAITOK
);
1945 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1947 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
1949 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
1950 error
= kern_symlink(&nd
, path
, mode
);
1954 objcache_put(namei_oc
, path
);
1959 * undelete_args(char *path)
1961 * Delete a whiteout from the filesystem.
1965 sys_undelete(struct undelete_args
*uap
)
1967 struct nlookupdata nd
;
1971 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1973 nd
.nl_flags
|= NLC_DELETE
;
1975 error
= nlookup(&nd
);
1977 error
= ncp_writechk(&nd
.nl_nch
);
1980 if ((dvp
= nd
.nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
1984 /* vhold(dvp); - dvp can't go away */
1985 error
= VOP_NWHITEOUT(&nd
.nl_nch
, dvp
, nd
.nl_cred
, NAMEI_DELETE
);
1993 kern_unlink(struct nlookupdata
*nd
)
1999 nd
->nl_flags
|= NLC_DELETE
;
2000 if ((error
= nlookup(nd
)) != 0)
2002 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2004 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
2006 /* vhold(dvp); - dvp can't go away */
2007 error
= VOP_NREMOVE(&nd
->nl_nch
, dvp
, nd
->nl_cred
);
2013 * unlink_args(char *path)
2015 * Delete a name from the filesystem.
2018 sys_unlink(struct unlink_args
*uap
)
2020 struct nlookupdata nd
;
2023 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2025 error
= kern_unlink(&nd
);
2031 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2033 struct thread
*td
= curthread
;
2034 struct proc
*p
= td
->td_proc
;
2039 fp
= holdfp(p
->p_fd
, fd
, -1);
2042 if (fp
->f_type
!= DTYPE_VNODE
) {
2049 fp
->f_offset
+= offset
;
2053 error
= VOP_GETATTR((struct vnode
*)fp
->f_data
, &vattr
);
2055 fp
->f_offset
= offset
+ vattr
.va_size
;
2058 fp
->f_offset
= offset
;
2065 *res
= fp
->f_offset
;
2072 * lseek_args(int fd, int pad, off_t offset, int whence)
2074 * Reposition read/write file offset.
2077 sys_lseek(struct lseek_args
*uap
)
2081 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2082 &uap
->sysmsg_offset
);
2088 kern_access(struct nlookupdata
*nd
, int aflags
)
2093 if ((error
= nlookup(nd
)) != 0)
2096 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2100 /* Flags == 0 means only check for existence. */
2109 if ((flags
& VWRITE
) == 0 ||
2110 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2111 error
= VOP_ACCESS(vp
, flags
, nd
->nl_cred
);
2114 * If the file handle is stale we have to re-resolve the
2115 * entry. This is a hack at the moment.
2117 if (error
== ESTALE
) {
2119 cache_setunresolved(&nd
->nl_nch
);
2120 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2133 * access_args(char *path, int flags)
2135 * Check access permissions.
2138 sys_access(struct access_args
*uap
)
2140 struct nlookupdata nd
;
2143 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2145 error
= kern_access(&nd
, uap
->flags
);
2151 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2157 if ((error
= nlookup(nd
)) != 0)
2160 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2164 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2166 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2169 * If the file handle is stale we have to re-resolve the entry. This
2170 * is a hack at the moment.
2172 if (error
== ESTALE
) {
2174 cache_setunresolved(&nd
->nl_nch
);
2175 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2185 * stat_args(char *path, struct stat *ub)
2187 * Get file status; this version follows links.
2190 sys_stat(struct stat_args
*uap
)
2192 struct nlookupdata nd
;
2196 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2198 error
= kern_stat(&nd
, &st
);
2200 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2207 * lstat_args(char *path, struct stat *ub)
2209 * Get file status; this version does not follow links.
2212 sys_lstat(struct lstat_args
*uap
)
2214 struct nlookupdata nd
;
2218 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2220 error
= kern_stat(&nd
, &st
);
2222 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2229 * pathconf_Args(char *path, int name)
2231 * Get configurable pathname variables.
2235 sys_pathconf(struct pathconf_args
*uap
)
2237 struct nlookupdata nd
;
2242 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2244 error
= nlookup(&nd
);
2246 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2249 error
= VOP_PATHCONF(vp
, uap
->name
, uap
->sysmsg_fds
);
2257 * kern_readlink isn't properly split yet. There is a copyin burried
2258 * in VOP_READLINK().
2261 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2263 struct thread
*td
= curthread
;
2264 struct proc
*p
= td
->td_proc
;
2270 if ((error
= nlookup(nd
)) != 0)
2272 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2275 if (vp
->v_type
!= VLNK
) {
2278 aiov
.iov_base
= buf
;
2279 aiov
.iov_len
= count
;
2280 auio
.uio_iov
= &aiov
;
2281 auio
.uio_iovcnt
= 1;
2282 auio
.uio_offset
= 0;
2283 auio
.uio_rw
= UIO_READ
;
2284 auio
.uio_segflg
= UIO_USERSPACE
;
2286 auio
.uio_resid
= count
;
2287 error
= VOP_READLINK(vp
, &auio
, p
->p_ucred
);
2290 *res
= count
- auio
.uio_resid
;
2295 * readlink_args(char *path, char *buf, int count)
2297 * Return target name of a symbolic link.
2300 sys_readlink(struct readlink_args
*uap
)
2302 struct nlookupdata nd
;
2305 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2307 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2308 &uap
->sysmsg_result
);
2315 setfflags(struct vnode
*vp
, int flags
)
2317 struct thread
*td
= curthread
;
2318 struct proc
*p
= td
->td_proc
;
2323 * Prevent non-root users from setting flags on devices. When
2324 * a device is reused, users can retain ownership of the device
2325 * if they are allowed to set flags and programs assume that
2326 * chown can't fail when done as root.
2328 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2329 ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)) != 0))
2333 * note: vget is required for any operation that might mod the vnode
2334 * so VINACTIVE is properly cleared.
2336 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2338 vattr
.va_flags
= flags
;
2339 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2346 * chflags(char *path, int flags)
2348 * Change flags of a file given a path name.
2352 sys_chflags(struct chflags_args
*uap
)
2354 struct nlookupdata nd
;
2359 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2360 /* XXX Add NLC flag indicating modifying operation? */
2362 error
= nlookup(&nd
);
2364 error
= ncp_writechk(&nd
.nl_nch
);
2366 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2369 error
= setfflags(vp
, uap
->flags
);
2376 * fchflags_args(int fd, int flags)
2378 * Change flags of a file given a file descriptor.
2382 sys_fchflags(struct fchflags_args
*uap
)
2384 struct thread
*td
= curthread
;
2385 struct proc
*p
= td
->td_proc
;
2389 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2391 if (fp
->f_nchandle
.ncp
)
2392 error
= ncp_writechk(&fp
->f_nchandle
);
2394 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
2400 setfmode(struct vnode
*vp
, int mode
)
2402 struct thread
*td
= curthread
;
2403 struct proc
*p
= td
->td_proc
;
2408 * note: vget is required for any operation that might mod the vnode
2409 * so VINACTIVE is properly cleared.
2411 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2413 vattr
.va_mode
= mode
& ALLPERMS
;
2414 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2421 kern_chmod(struct nlookupdata
*nd
, int mode
)
2426 /* XXX Add NLC flag indicating modifying operation? */
2427 if ((error
= nlookup(nd
)) != 0)
2429 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2431 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
2432 error
= setfmode(vp
, mode
);
2438 * chmod_args(char *path, int mode)
2440 * Change mode of a file given path name.
2444 sys_chmod(struct chmod_args
*uap
)
2446 struct nlookupdata nd
;
2449 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2451 error
= kern_chmod(&nd
, uap
->mode
);
2457 * lchmod_args(char *path, int mode)
2459 * Change mode of a file given path name (don't follow links.)
2463 sys_lchmod(struct lchmod_args
*uap
)
2465 struct nlookupdata nd
;
2468 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2470 error
= kern_chmod(&nd
, uap
->mode
);
2476 * fchmod_args(int fd, int mode)
2478 * Change mode of a file given a file descriptor.
2482 sys_fchmod(struct fchmod_args
*uap
)
2484 struct thread
*td
= curthread
;
2485 struct proc
*p
= td
->td_proc
;
2489 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2491 if (fp
->f_nchandle
.ncp
)
2492 error
= ncp_writechk(&fp
->f_nchandle
);
2494 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
2500 setfown(struct vnode
*vp
, uid_t uid
, gid_t gid
)
2502 struct thread
*td
= curthread
;
2503 struct proc
*p
= td
->td_proc
;
2508 * note: vget is required for any operation that might mod the vnode
2509 * so VINACTIVE is properly cleared.
2511 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2515 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2522 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
2527 /* XXX Add NLC flag indicating modifying operation? */
2528 if ((error
= nlookup(nd
)) != 0)
2530 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2532 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
2533 error
= setfown(vp
, uid
, gid
);
2539 * chown(char *path, int uid, int gid)
2541 * Set ownership given a path name.
2544 sys_chown(struct chown_args
*uap
)
2546 struct nlookupdata nd
;
2549 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2551 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2557 * lchown_args(char *path, int uid, int gid)
2559 * Set ownership given a path name, do not cross symlinks.
2562 sys_lchown(struct lchown_args
*uap
)
2564 struct nlookupdata nd
;
2567 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2569 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
2575 * fchown_args(int fd, int uid, int gid)
2577 * Set ownership given a file descriptor.
2581 sys_fchown(struct fchown_args
*uap
)
2583 struct thread
*td
= curthread
;
2584 struct proc
*p
= td
->td_proc
;
2588 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2590 if (fp
->f_nchandle
.ncp
)
2591 error
= ncp_writechk(&fp
->f_nchandle
);
2593 error
= setfown((struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
2599 getutimes(const struct timeval
*tvp
, struct timespec
*tsp
)
2601 struct timeval tv
[2];
2605 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
2608 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
2609 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
2615 setutimes(struct vnode
*vp
, const struct timespec
*ts
, int nullflag
)
2617 struct thread
*td
= curthread
;
2618 struct proc
*p
= td
->td_proc
;
2623 * note: vget is required for any operation that might mod the vnode
2624 * so VINACTIVE is properly cleared.
2626 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2628 vattr
.va_atime
= ts
[0];
2629 vattr
.va_mtime
= ts
[1];
2631 vattr
.va_vaflags
|= VA_UTIMES_NULL
;
2632 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
);
2639 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
2641 struct timespec ts
[2];
2645 if ((error
= getutimes(tptr
, ts
)) != 0)
2647 /* XXX Add NLC flag indicating modifying operation? */
2648 if ((error
= nlookup(nd
)) != 0)
2650 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2652 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2654 error
= setutimes(vp
, ts
, tptr
== NULL
);
2660 * utimes_args(char *path, struct timeval *tptr)
2662 * Set the access and modification times of a file.
2665 sys_utimes(struct utimes_args
*uap
)
2667 struct timeval tv
[2];
2668 struct nlookupdata nd
;
2672 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2676 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2678 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2684 * lutimes_args(char *path, struct timeval *tptr)
2686 * Set the access and modification times of a file.
2689 sys_lutimes(struct lutimes_args
*uap
)
2691 struct timeval tv
[2];
2692 struct nlookupdata nd
;
2696 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2700 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2702 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
2708 kern_futimes(int fd
, struct timeval
*tptr
)
2710 struct thread
*td
= curthread
;
2711 struct proc
*p
= td
->td_proc
;
2712 struct timespec ts
[2];
2716 error
= getutimes(tptr
, ts
);
2719 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
2721 if (fp
->f_nchandle
.ncp
)
2722 error
= ncp_writechk(&fp
->f_nchandle
);
2724 error
= setutimes((struct vnode
*)fp
->f_data
, ts
, tptr
== NULL
);
2730 * futimes_args(int fd, struct timeval *tptr)
2732 * Set the access and modification times of a file.
2735 sys_futimes(struct futimes_args
*uap
)
2737 struct timeval tv
[2];
2741 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
2746 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
2752 kern_truncate(struct nlookupdata
*nd
, off_t length
)
2760 /* XXX Add NLC flag indicating modifying operation? */
2761 if ((error
= nlookup(nd
)) != 0)
2763 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2765 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2767 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
2771 if (vp
->v_type
== VDIR
) {
2773 } else if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0 &&
2774 (error
= VOP_ACCESS(vp
, VWRITE
, nd
->nl_cred
)) == 0) {
2776 vattr
.va_size
= length
;
2777 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
2784 * truncate(char *path, int pad, off_t length)
2786 * Truncate a file given its path name.
2789 sys_truncate(struct truncate_args
*uap
)
2791 struct nlookupdata nd
;
2794 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2796 error
= kern_truncate(&nd
, uap
->length
);
2802 kern_ftruncate(int fd
, off_t length
)
2804 struct thread
*td
= curthread
;
2805 struct proc
*p
= td
->td_proc
;
2813 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
2815 if (fp
->f_nchandle
.ncp
) {
2816 error
= ncp_writechk(&fp
->f_nchandle
);
2820 if ((fp
->f_flag
& FWRITE
) == 0) {
2824 vp
= (struct vnode
*)fp
->f_data
;
2825 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2826 if (vp
->v_type
== VDIR
) {
2828 } else if ((error
= vn_writechk(vp
, NULL
)) == 0) {
2830 vattr
.va_size
= length
;
2831 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
2840 * ftruncate_args(int fd, int pad, off_t length)
2842 * Truncate a file given a file descriptor.
2845 sys_ftruncate(struct ftruncate_args
*uap
)
2849 error
= kern_ftruncate(uap
->fd
, uap
->length
);
2857 * Sync an open file.
2861 sys_fsync(struct fsync_args
*uap
)
2863 struct thread
*td
= curthread
;
2864 struct proc
*p
= td
->td_proc
;
2870 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2872 vp
= (struct vnode
*)fp
->f_data
;
2873 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2874 if ((obj
= vp
->v_object
) != NULL
)
2875 vm_object_page_clean(obj
, 0, 0, 0);
2876 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
)) == 0 && vp
->v_mount
)
2877 error
= buf_fsync(vp
);
2884 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
2886 struct nchandle fnchd
;
2887 struct nchandle tnchd
;
2888 struct namecache
*ncp
;
2895 if ((error
= nlookup(fromnd
)) != 0)
2897 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
2899 fnchd
.mount
= fromnd
->nl_nch
.mount
;
2903 * unlock the source nch so we can lookup the target nch without
2904 * deadlocking. The target may or may not exist so we do not check
2905 * for a target vp like kern_mkdir() and other creation functions do.
2907 * The source and target directories are ref'd and rechecked after
2908 * everything is relocked to determine if the source or target file
2911 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
2912 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2913 cache_unlock(&fromnd
->nl_nch
);
2915 tond
->nl_flags
|= NLC_CREATE
;
2916 if ((error
= nlookup(tond
)) != 0) {
2920 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
2924 tnchd
.mount
= tond
->nl_nch
.mount
;
2928 * If the source and target are the same there is nothing to do
2930 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
2937 * Mount points cannot be renamed or overwritten
2939 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
2948 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2949 * may have become invalid while it was unlocked, nc_vp and nc_mount
2952 if (cache_lock_nonblock(&fromnd
->nl_nch
) == 0) {
2953 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2954 } else if (fromnd
->nl_nch
.ncp
> tond
->nl_nch
.ncp
) {
2955 cache_lock(&fromnd
->nl_nch
);
2956 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2958 cache_unlock(&tond
->nl_nch
);
2959 cache_lock(&fromnd
->nl_nch
);
2960 cache_resolve(&fromnd
->nl_nch
, fromnd
->nl_cred
);
2961 cache_lock(&tond
->nl_nch
);
2962 cache_resolve(&tond
->nl_nch
, tond
->nl_cred
);
2964 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
2967 * make sure the parent directories linkages are the same
2969 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
2970 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
2977 * Both the source and target must be within the same filesystem and
2978 * in the same filesystem as their parent directories within the
2979 * namecache topology.
2981 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2984 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
2985 mp
!= tond
->nl_nch
.mount
) {
2992 * Make sure the mount point is writable
2994 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
3001 * If the target exists and either the source or target is a directory,
3002 * then both must be directories.
3004 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3007 if (tond
->nl_nch
.ncp
->nc_vp
) {
3008 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
3010 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3011 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
3013 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3019 * You cannot rename a source into itself or a subdirectory of itself.
3020 * We check this by travsersing the target directory upwards looking
3021 * for a match against the source.
3024 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
3025 if (fromnd
->nl_nch
.ncp
== ncp
) {
3036 * Even though the namespaces are different, they may still represent
3037 * hardlinks to the same file. The filesystem might have a hard time
3038 * with this so we issue a NREMOVE of the source instead of a NRENAME
3039 * when we detect the situation.
3042 fdvp
= fromnd
->nl_nch
.ncp
->nc_parent
->nc_vp
;
3043 tdvp
= tond
->nl_nch
.ncp
->nc_parent
->nc_vp
;
3044 if (fdvp
== NULL
|| tdvp
== NULL
) {
3046 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
3047 /* vhold(fdvp); - dvp can't go away */
3048 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
3052 /* vhold(fdvp); - dvp can't go away */
3053 /* vhold(tdvp); - dvp can't go away */
3054 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
3055 fdvp
, tdvp
, tond
->nl_cred
);
3064 * rename_args(char *from, char *to)
3066 * Rename files. Source and destination must either both be directories,
3067 * or both not be directories. If target is a directory, it must be empty.
3070 sys_rename(struct rename_args
*uap
)
3072 struct nlookupdata fromnd
, tond
;
3075 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
3077 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
3079 error
= kern_rename(&fromnd
, &tond
);
3080 nlookup_done(&tond
);
3082 nlookup_done(&fromnd
);
3087 kern_mkdir(struct nlookupdata
*nd
, int mode
)
3089 struct thread
*td
= curthread
;
3090 struct proc
*p
= td
->td_proc
;
3097 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
;
3098 if ((error
= nlookup(nd
)) != 0)
3101 if (nd
->nl_nch
.ncp
->nc_vp
)
3103 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3105 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
3107 /* vhold(dvp); - dvp can't go away */
3109 vattr
.va_type
= VDIR
;
3110 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
3113 error
= VOP_NMKDIR(&nd
->nl_nch
, dvp
, &vp
, p
->p_ucred
, &vattr
);
3121 * mkdir_args(char *path, int mode)
3123 * Make a directory file.
3127 sys_mkdir(struct mkdir_args
*uap
)
3129 struct nlookupdata nd
;
3132 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3134 error
= kern_mkdir(&nd
, uap
->mode
);
3140 kern_rmdir(struct nlookupdata
*nd
)
3146 nd
->nl_flags
|= NLC_DELETE
;
3147 if ((error
= nlookup(nd
)) != 0)
3151 * Do not allow directories representing mount points to be
3152 * deleted, even if empty. Check write perms on mount point
3153 * in case the vnode is aliased (aka nullfs).
3155 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
3157 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3159 if ((dvp
= nd
->nl_nch
.ncp
->nc_parent
->nc_vp
) == NULL
)
3161 /* vhold(dvp); - dvp can't go away */
3162 error
= VOP_NRMDIR(&nd
->nl_nch
, dvp
, nd
->nl_cred
);
3168 * rmdir_args(char *path)
3170 * Remove a directory file.
3174 sys_rmdir(struct rmdir_args
*uap
)
3176 struct nlookupdata nd
;
3179 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3181 error
= kern_rmdir(&nd
);
3187 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
3188 enum uio_seg direction
)
3190 struct thread
*td
= curthread
;
3191 struct proc
*p
= td
->td_proc
;
3199 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3201 if ((fp
->f_flag
& FREAD
) == 0) {
3205 vp
= (struct vnode
*)fp
->f_data
;
3207 if (vp
->v_type
!= VDIR
) {
3211 aiov
.iov_base
= buf
;
3212 aiov
.iov_len
= count
;
3213 auio
.uio_iov
= &aiov
;
3214 auio
.uio_iovcnt
= 1;
3215 auio
.uio_rw
= UIO_READ
;
3216 auio
.uio_segflg
= direction
;
3218 auio
.uio_resid
= count
;
3219 loff
= auio
.uio_offset
= fp
->f_offset
;
3220 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
3221 fp
->f_offset
= auio
.uio_offset
;
3224 if (count
== auio
.uio_resid
) {
3225 if (union_dircheckp
) {
3226 error
= union_dircheckp(td
, &vp
, fp
);
3233 if ((vp
->v_flag
& VROOT
) &&
3234 (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
3235 struct vnode
*tvp
= vp
;
3236 vp
= vp
->v_mount
->mnt_vnodecovered
;
3247 * WARNING! *basep may not be wide enough to accomodate the
3248 * seek offset. XXX should we hack this to return the upper 32 bits
3249 * for offsets greater then 4G?
3252 *basep
= (long)loff
;
3254 *res
= count
- auio
.uio_resid
;
3261 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3263 * Read a block of directory entries in a file system independent format.
3266 sys_getdirentries(struct getdirentries_args
*uap
)
3271 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
3272 &uap
->sysmsg_result
, UIO_USERSPACE
);
3274 if (error
== 0 && uap
->basep
)
3275 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
3280 * getdents_args(int fd, char *buf, size_t count)
3283 sys_getdents(struct getdents_args
*uap
)
3287 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
3288 &uap
->sysmsg_result
, UIO_USERSPACE
);
3294 * umask(int newmask)
3296 * Set the mode mask for creation of filesystem nodes.
3301 sys_umask(struct umask_args
*uap
)
3303 struct thread
*td
= curthread
;
3304 struct proc
*p
= td
->td_proc
;
3305 struct filedesc
*fdp
;
3308 uap
->sysmsg_result
= fdp
->fd_cmask
;
3309 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
3314 * revoke(char *path)
3316 * Void all references to file by ripping underlying filesystem
3321 sys_revoke(struct revoke_args
*uap
)
3323 struct nlookupdata nd
;
3330 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3332 error
= nlookup(&nd
);
3334 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3335 cred
= crhold(nd
.nl_cred
);
3338 if (vp
->v_type
!= VCHR
&& vp
->v_type
!= VBLK
)
3341 error
= VOP_GETATTR(vp
, &vattr
);
3342 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
3343 error
= suser_cred(cred
, PRISON_ROOT
);
3344 if (error
== 0 && count_udev(vp
->v_umajor
, vp
->v_uminor
) > 0) {
3347 VOP_REVOKE(vp
, REVOKEALL
);
3358 * getfh_args(char *fname, fhandle_t *fhp)
3360 * Get (NFS) file handle
3363 sys_getfh(struct getfh_args
*uap
)
3365 struct thread
*td
= curthread
;
3366 struct nlookupdata nd
;
3372 * Must be super user
3374 if ((error
= suser(td
)) != 0)
3378 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
3380 error
= nlookup(&nd
);
3382 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3385 bzero(&fh
, sizeof(fh
));
3386 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
3387 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
3390 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
3396 * fhopen_args(const struct fhandle *u_fhp, int flags)
3398 * syscall for the rpc.lockd to use to translate a NFS file handle into
3399 * an open descriptor.
3401 * warning: do not remove the suser() call or this becomes one giant
3405 sys_fhopen(struct fhopen_args
*uap
)
3407 struct thread
*td
= curthread
;
3408 struct proc
*p
= td
->td_proc
;
3413 struct vattr
*vap
= &vat
;
3415 int fmode
, mode
, error
, type
;
3421 * Must be super user
3427 fmode
= FFLAGS(uap
->flags
);
3428 /* why not allow a non-read/write open for our lockd? */
3429 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
3431 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
3434 /* find the mount point */
3435 mp
= vfs_getvfs(&fhp
.fh_fsid
);
3438 /* now give me my vnode, it gets returned to me locked */
3439 error
= VFS_FHTOVP(mp
, &fhp
.fh_fid
, &vp
);
3443 * from now on we have to make sure not
3444 * to forget about the vnode
3445 * any error that causes an abort must vput(vp)
3446 * just set error = err and 'goto bad;'.
3452 if (vp
->v_type
== VLNK
) {
3456 if (vp
->v_type
== VSOCK
) {
3461 if (fmode
& (FWRITE
| O_TRUNC
)) {
3462 if (vp
->v_type
== VDIR
) {
3466 error
= vn_writechk(vp
, NULL
);
3474 error
= VOP_ACCESS(vp
, mode
, p
->p_ucred
);
3478 if (fmode
& O_TRUNC
) {
3479 vn_unlock(vp
); /* XXX */
3480 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
3483 error
= VOP_SETATTR(vp
, vap
, p
->p_ucred
);
3489 * VOP_OPEN needs the file pointer so it can potentially override
3492 * WARNING! no f_nchandle will be associated when fhopen()ing a
3495 if ((error
= falloc(p
, &nfp
, &indx
)) != 0)
3499 error
= VOP_OPEN(vp
, fmode
, p
->p_ucred
, fp
);
3502 * setting f_ops this way prevents VOP_CLOSE from being
3503 * called or fdrop() releasing the vp from v_data. Since
3504 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3506 fp
->f_ops
= &badfileops
;
3512 * The fp is given its own reference, we still have our ref and lock.
3514 * Assert that all regular files must be created with a VM object.
3516 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
3517 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
3522 * The open was successful. Handle any locking requirements.
3524 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
3525 lf
.l_whence
= SEEK_SET
;
3528 if (fmode
& O_EXLOCK
)
3529 lf
.l_type
= F_WRLCK
;
3531 lf
.l_type
= F_RDLCK
;
3532 if (fmode
& FNONBLOCK
)
3537 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
3539 * release our private reference.
3541 fsetfd(p
, NULL
, indx
);
3546 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3547 fp
->f_flag
|= FHASLOCK
;
3551 * Clean up. Associate the file pointer with the previously
3552 * reserved descriptor and return it.
3555 fsetfd(p
, fp
, indx
);
3557 uap
->sysmsg_result
= indx
;
3561 fsetfd(p
, NULL
, indx
);
3569 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3572 sys_fhstat(struct fhstat_args
*uap
)
3574 struct thread
*td
= curthread
;
3582 * Must be super user
3588 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
3592 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3594 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3596 error
= vn_stat(vp
, &sb
, td
->td_proc
->p_ucred
);
3600 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
3605 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3608 sys_fhstatfs(struct fhstatfs_args
*uap
)
3610 struct thread
*td
= curthread
;
3611 struct proc
*p
= td
->td_proc
;
3616 char *fullpath
, *freepath
;
3621 * Must be super user
3623 if ((error
= suser(td
)))
3626 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
3629 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
3632 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
))
3635 if ((error
= VFS_FHTOVP(mp
, &fh
.fh_fid
, &vp
)))
3640 if ((error
= VFS_STATFS(mp
, sp
, p
->p_ucred
)) != 0)
3643 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
3646 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
3647 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
3648 kfree(freepath
, M_TEMP
);
3650 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
3652 bcopy(sp
, &sb
, sizeof(sb
));
3653 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
3656 return (copyout(sp
, uap
->buf
, sizeof(*sp
)));
3660 * Syscall to push extended attribute configuration information into the
3661 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3662 * a command (int cmd), and attribute name and misc data. For now, the
3663 * attribute name is left in userspace for consumption by the VFS_op.
3664 * It will probably be changed to be copied into sysspace by the
3665 * syscall in the future, once issues with various consumers of the
3666 * attribute code have raised their hands.
3668 * Currently this is used only by UFS Extended Attributes.
3671 sys_extattrctl(struct extattrctl_args
*uap
)
3673 struct nlookupdata nd
;
3679 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3681 error
= nlookup(&nd
);
3683 mp
= nd
.nl_nch
.mount
;
3684 error
= VFS_EXTATTRCTL(mp
, uap
->cmd
,
3685 uap
->attrname
, uap
->arg
,
3693 * Syscall to set a named extended attribute on a file or directory.
3694 * Accepts attribute name, and a uio structure pointing to the data to set.
3695 * The uio is consumed in the style of writev(). The real work happens
3696 * in VOP_SETEXTATTR().
3699 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
3701 char attrname
[EXTATTR_MAXNAMELEN
];
3702 struct iovec aiov
[UIO_SMALLIOV
];
3703 struct iovec
*needfree
;
3704 struct nlookupdata nd
;
3713 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3718 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3720 error
= nlookup(&nd
);
3722 error
= ncp_writechk(&nd
.nl_nch
);
3724 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3731 iovlen
= uap
->iovcnt
* sizeof(struct iovec
);
3732 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3733 if (uap
->iovcnt
> UIO_MAXIOV
) {
3737 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3743 auio
.uio_iovcnt
= uap
->iovcnt
;
3744 auio
.uio_rw
= UIO_WRITE
;
3745 auio
.uio_segflg
= UIO_USERSPACE
;
3746 auio
.uio_td
= nd
.nl_td
;
3747 auio
.uio_offset
= 0;
3748 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3751 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3752 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3756 auio
.uio_resid
+= iov
->iov_len
;
3759 cnt
= auio
.uio_resid
;
3760 error
= VOP_SETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
3761 cnt
-= auio
.uio_resid
;
3762 uap
->sysmsg_result
= cnt
;
3767 FREE(needfree
, M_IOV
);
3772 * Syscall to get a named extended attribute on a file or directory.
3773 * Accepts attribute name, and a uio structure pointing to a buffer for the
3774 * data. The uio is consumed in the style of readv(). The real work
3775 * happens in VOP_GETEXTATTR();
3778 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
3780 char attrname
[EXTATTR_MAXNAMELEN
];
3781 struct iovec aiov
[UIO_SMALLIOV
];
3782 struct iovec
*needfree
;
3783 struct nlookupdata nd
;
3792 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3797 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3799 error
= nlookup(&nd
);
3801 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3807 iovlen
= uap
->iovcnt
* sizeof (struct iovec
);
3809 if (uap
->iovcnt
> UIO_SMALLIOV
) {
3810 if (uap
->iovcnt
> UIO_MAXIOV
) {
3814 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
3820 auio
.uio_iovcnt
= uap
->iovcnt
;
3821 auio
.uio_rw
= UIO_READ
;
3822 auio
.uio_segflg
= UIO_USERSPACE
;
3823 auio
.uio_td
= nd
.nl_td
;
3824 auio
.uio_offset
= 0;
3825 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
3828 for (i
= 0; i
< uap
->iovcnt
; i
++) {
3829 if (iov
->iov_len
> INT_MAX
- auio
.uio_resid
) {
3833 auio
.uio_resid
+= iov
->iov_len
;
3836 cnt
= auio
.uio_resid
;
3837 error
= VOP_GETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
3838 cnt
-= auio
.uio_resid
;
3839 uap
->sysmsg_result
= cnt
;
3844 FREE(needfree
, M_IOV
);
3849 * Syscall to delete a named extended attribute from a file or directory.
3850 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3853 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
3855 char attrname
[EXTATTR_MAXNAMELEN
];
3856 struct nlookupdata nd
;
3860 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
3865 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3867 error
= nlookup(&nd
);
3869 error
= ncp_writechk(&nd
.nl_nch
);
3871 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
3877 error
= VOP_SETEXTATTR(vp
, attrname
, NULL
, nd
.nl_cred
);
3884 * Determine if the mount is visible to the process.
3887 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
3889 struct nchandle nch
;
3892 * Traverse from the mount point upwards. If we hit the process
3893 * root then the mount point is visible to the process.
3895 nch
= mp
->mnt_ncmountpt
;
3897 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
3898 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
3901 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
3902 nch
= nch
.mount
->mnt_ncmounton
;
3904 nch
.ncp
= nch
.ncp
->nc_parent
;
3909 * If the mount point is not visible to the process, but the
3910 * process root is in a subdirectory of the mount, return
3913 if (p
->p_fd
->fd_nrdir
.mount
== mp
)