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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
78 static void mount_warning(struct mount
*mp
, const char *ctl
, ...)
80 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
81 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
82 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
83 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
84 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
85 static int getutimes (struct timeval
*, struct timespec
*);
86 static int getutimens (const struct timespec
*, struct timespec
*, int *);
87 static int setfown (struct mount
*, struct vnode
*, uid_t
, gid_t
);
88 static int setfmode (struct vnode
*, int);
89 static int setfflags (struct vnode
*, int);
90 static int setutimes (struct vnode
*, struct vattr
*,
91 const struct timespec
*, int);
92 static int usermount
= 0; /* if 1, non-root can mount fs. */
94 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0,
95 "Allow non-root users to mount filesystems");
98 * Virtual File System System Calls
102 * Mount a file system.
104 * mount_args(char *type, char *path, int flags, caddr_t data)
109 sys_mount(struct mount_args
*uap
)
111 struct thread
*td
= curthread
;
114 struct mount
*mp
, *nullmp
;
115 struct vfsconf
*vfsp
;
116 int error
, flag
= 0, flag2
= 0;
119 struct nlookupdata nd
;
120 char fstypename
[MFSNAMELEN
];
128 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
132 * Do not allow NFS export by non-root users.
134 if (uap
->flags
& MNT_EXPORTED
) {
135 error
= priv_check(td
, PRIV_ROOT
);
140 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 if (priv_check(td
, PRIV_ROOT
))
143 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
146 * Lookup the requested path and extract the nch and vnode.
148 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
150 if ((error
= nlookup(&nd
)) == 0) {
151 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
161 * If the target filesystem is resolved via a nullfs mount, then
162 * nd.nl_nch.mount will be pointing to the nullfs mount structure
163 * instead of the target file system. We need it in case we are
166 nullmp
= nd
.nl_nch
.mount
;
169 * Extract the locked+refd ncp and cleanup the nd structure
172 cache_zero(&nd
.nl_nch
);
175 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
176 (mp
= cache_findmount(&nch
)) != NULL
) {
185 * now we have the locked ref'd nch and unreferenced vnode.
188 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
195 * Extract the file system type. We need to know this early, to take
196 * appropriate actions if we are dealing with a nullfs.
198 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
205 * Now we have an unlocked ref'd nch and a locked ref'd vp
207 if (uap
->flags
& MNT_UPDATE
) {
208 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
215 if (strncmp(fstypename
, "null", 5) == 0) {
223 flag2
= mp
->mnt_kern_flag
;
225 * We only allow the filesystem to be reloaded if it
226 * is currently mounted read-only.
228 if ((uap
->flags
& MNT_RELOAD
) &&
229 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
232 error
= EOPNOTSUPP
; /* Needs translation */
236 * Only root, or the user that did the original mount is
237 * permitted to update it.
239 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
240 (error
= priv_check(td
, PRIV_ROOT
))) {
245 if (vfs_busy(mp
, LK_NOWAIT
)) {
259 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
260 lwkt_gettoken(&mp
->mnt_token
);
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error
= VOP_GETATTR(vp
, &va
)) ||
271 (va
.va_uid
!= cred
->cr_uid
&&
272 (error
= priv_check(td
, PRIV_ROOT
)))) {
277 if ((error
= vinvalbuf(vp
, V_SAVE
, 0, 0)) != 0) {
282 if (vp
->v_type
!= VDIR
) {
288 if (vp
->v_mount
->mnt_kern_flag
& MNTK_NOSTKMNT
) {
294 vfsp
= vfsconf_find_by_name(fstypename
);
298 /* Only load modules for root (very important!) */
299 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0) {
304 error
= linker_load_file(fstypename
, &lf
);
305 if (error
|| lf
== NULL
) {
313 /* lookup again, see if the VFS was loaded */
314 vfsp
= vfsconf_find_by_name(fstypename
);
317 linker_file_unload(lf
);
332 * Allocate and initialize the filesystem.
334 mp
= kmalloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
336 vfs_busy(mp
, LK_NOWAIT
);
337 mp
->mnt_op
= vfsp
->vfc_vfsops
;
339 mp
->mnt_pbuf_count
= nswbuf_kva
/ NSWBUF_SPLIT
;
340 vfsp
->vfc_refcount
++;
341 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
342 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
343 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
344 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
345 lwkt_gettoken(&mp
->mnt_token
);
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap
->flags
& MNT_RDONLY
)
354 mp
->mnt_flag
|= MNT_RDONLY
;
355 else if (mp
->mnt_flag
& MNT_RDONLY
)
356 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
357 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
358 MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_NOATIME
|
359 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
360 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
|
362 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
363 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_FORCE
|
364 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
365 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
|
369 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
370 * This way the initial VFS_MOUNT() call will also be MPSAFE.
372 if (vfsp
->vfc_flags
& VFCF_MPSAFE
)
373 mp
->mnt_kern_flag
|= MNTK_ALL_MPSAFE
;
376 * Mount the filesystem.
377 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
380 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
381 if (mp
->mnt_flag
& MNT_UPDATE
) {
382 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
383 mp
->mnt_flag
&= ~MNT_RDONLY
;
384 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
385 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
388 mp
->mnt_kern_flag
= flag2
;
390 lwkt_reltoken(&mp
->mnt_token
);
396 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
399 * Put the new filesystem on the mount list after root. The mount
400 * point gets its own mnt_ncmountpt (unless the VFS already set one
401 * up) which represents the root of the mount. The lookup code
402 * detects the mount point going forward and checks the root of
403 * the mount going backwards.
405 * It is not necessary to invalidate or purge the vnode underneath
406 * because elements under the mount will be given their own glue
410 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
412 * Allocate, then unlock, but leave the ref intact.
413 * This is the mnt_refs (1) that we will retain
414 * through to the unmount.
416 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
417 cache_unlock(&mp
->mnt_ncmountpt
);
420 mp
->mnt_ncmounton
= nch
; /* inherits ref */
422 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
424 cache_ismounting(mp
);
425 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
427 mountlist_insert(mp
, MNTINS_LAST
);
429 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
430 error
= vfs_allocate_syncvnode(mp
);
431 lwkt_reltoken(&mp
->mnt_token
);
433 error
= VFS_START(mp
, 0);
435 KNOTE(&fs_klist
, VQ_MOUNT
);
437 vn_syncer_thr_stop(mp
);
438 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
439 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
440 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
441 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
442 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
443 mp
->mnt_vfc
->vfc_refcount
--;
444 lwkt_reltoken(&mp
->mnt_token
);
455 * Scan all active processes to see if any of them have a current
456 * or root directory onto which the new filesystem has just been
457 * mounted. If so, replace them with the new mount point.
459 * Both old_nch and new_nch are ref'd on call but not locked.
460 * new_nch must be temporarily locked so it can be associated with the
461 * vnode representing the root of the mount point.
463 struct checkdirs_info
{
464 struct nchandle old_nch
;
465 struct nchandle new_nch
;
466 struct vnode
*old_vp
;
467 struct vnode
*new_vp
;
470 static int checkdirs_callback(struct proc
*p
, void *data
);
473 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
475 struct checkdirs_info info
;
481 * If the old mount point's vnode has a usecount of 1, it is not
482 * being held as a descriptor anywhere.
484 olddp
= old_nch
->ncp
->nc_vp
;
485 if (olddp
== NULL
|| VREFCNT(olddp
) == 1)
489 * Force the root vnode of the new mount point to be resolved
490 * so we can update any matching processes.
493 if (VFS_ROOT(mp
, &newdp
))
494 panic("mount: lost mount");
497 vn_lock(newdp
, LK_EXCLUSIVE
| LK_RETRY
);
498 cache_setunresolved(new_nch
);
499 cache_setvp(new_nch
, newdp
);
500 cache_unlock(new_nch
);
503 * Special handling of the root node
505 if (rootvnode
== olddp
) {
507 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
511 * Pass newdp separately so the callback does not have to access
512 * it via new_nch->ncp->nc_vp.
514 info
.old_nch
= *old_nch
;
515 info
.new_nch
= *new_nch
;
517 allproc_scan(checkdirs_callback
, &info
, 0);
522 * NOTE: callback is not MP safe because the scanned process's filedesc
523 * structure can be ripped out from under us, amoung other things.
526 checkdirs_callback(struct proc
*p
, void *data
)
528 struct checkdirs_info
*info
= data
;
529 struct filedesc
*fdp
;
530 struct nchandle ncdrop1
;
531 struct nchandle ncdrop2
;
532 struct vnode
*vprele1
;
533 struct vnode
*vprele2
;
535 if ((fdp
= p
->p_fd
) != NULL
) {
536 cache_zero(&ncdrop1
);
537 cache_zero(&ncdrop2
);
542 * MPUNSAFE - XXX fdp can be pulled out from under a
545 * A shared filedesc is ok, we don't have to copy it
546 * because we are making this change globally.
548 spin_lock(&fdp
->fd_spin
);
549 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
550 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
551 vprele1
= fdp
->fd_cdir
;
553 fdp
->fd_cdir
= info
->new_vp
;
554 ncdrop1
= fdp
->fd_ncdir
;
555 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
557 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
558 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
559 vprele2
= fdp
->fd_rdir
;
561 fdp
->fd_rdir
= info
->new_vp
;
562 ncdrop2
= fdp
->fd_nrdir
;
563 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
565 spin_unlock(&fdp
->fd_spin
);
567 cache_drop(&ncdrop1
);
569 cache_drop(&ncdrop2
);
579 * Unmount a file system.
581 * Note: unmount takes a path to the vnode mounted on as argument,
582 * not special file (as before).
584 * umount_args(char *path, int flags)
589 sys_unmount(struct unmount_args
*uap
)
591 struct thread
*td
= curthread
;
592 struct proc
*p __debugvar
= td
->td_proc
;
593 struct mount
*mp
= NULL
;
594 struct nlookupdata nd
;
598 if (td
->td_ucred
->cr_prison
!= NULL
) {
602 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
605 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
607 error
= nlookup(&nd
);
611 mp
= nd
.nl_nch
.mount
;
614 * Only root, or the user that did the original mount is
615 * permitted to unmount this filesystem.
617 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
618 (error
= priv_check(td
, PRIV_ROOT
)))
622 * Don't allow unmounting the root file system.
624 if (mp
->mnt_flag
& MNT_ROOTFS
) {
630 * Must be the root of the filesystem
632 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
638 * If no error try to issue the unmount. We lose our cache
639 * ref when we call nlookup_done so we must hold the mount point
640 * to prevent use-after-free races.
646 error
= dounmount(mp
, uap
->flags
);
656 * Do the actual file system unmount (interlocked against the mountlist
657 * token and mp->mnt_token).
660 dounmount_interlock(struct mount
*mp
)
662 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
664 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
669 unmount_allproc_cb(struct proc
*p
, void *arg
)
673 if (p
->p_textnch
.ncp
== NULL
)
676 mp
= (struct mount
*)arg
;
677 if (p
->p_textnch
.mount
== mp
)
678 cache_drop(&p
->p_textnch
);
684 * The guts of the unmount code. The mount owns one ref and one hold
685 * count. If we successfully interlock the unmount, those refs are ours.
686 * (The ref is from mnt_ncmountpt).
689 dounmount(struct mount
*mp
, int flags
)
691 struct namecache
*ncp
;
700 lwkt_gettoken(&mp
->mnt_token
);
703 * Exclusive access for unmounting purposes.
705 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
709 * We now 'own' the last mp->mnt_refs
711 * Allow filesystems to detect that a forced unmount is in progress.
713 if (flags
& MNT_FORCE
)
714 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
715 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_TIMELOCK
);
716 error
= lockmgr(&mp
->mnt_lock
, lflags
);
718 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
719 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
720 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
726 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
727 vfs_setpublicfs(NULL
, NULL
, NULL
);
729 vfs_msync(mp
, MNT_WAIT
);
730 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
731 mp
->mnt_flag
&=~ MNT_ASYNC
;
734 * If this filesystem isn't aliasing other filesystems,
735 * try to invalidate any remaining namecache entries and
736 * check the count afterwords.
738 * We own the last mnt_refs by owning mnt_ncmountpt.
740 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
741 cache_lock(&mp
->mnt_ncmountpt
);
742 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
743 cache_unlock(&mp
->mnt_ncmountpt
);
745 cache_clearmntcache();
746 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
747 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
748 allproc_scan(&unmount_allproc_cb
, mp
, 0);
751 cache_clearmntcache();
752 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
753 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
755 if ((flags
& MNT_FORCE
) == 0) {
757 mount_warning(mp
, "Cannot unmount: "
763 mount_warning(mp
, "Forced unmount: "
774 * Decomission our special mnt_syncer vnode. This also stops
775 * the vnlru code. If we are unable to unmount we recommission
778 * Then sync the filesystem.
780 if ((vp
= mp
->mnt_syncer
) != NULL
) {
781 mp
->mnt_syncer
= NULL
;
782 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
785 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
786 VFS_SYNC(mp
, MNT_WAIT
);
789 * nchandle records ref the mount structure. Expect a count of 1
790 * (our mount->mnt_ncmountpt).
792 * Scans can get temporary refs on a mountpoint (thought really
793 * heavy duty stuff like cache_findmount() do not).
795 if (mp
->mnt_refs
!= 1)
796 cache_clearmntcache();
797 for (retry
= 0; retry
< 10 && mp
->mnt_refs
!= 1; ++retry
) {
798 cache_unmounting(mp
);
799 tsleep(&mp
->mnt_refs
, 0, "mntbsy", hz
/ 10 + 1);
800 cache_clearmntcache();
802 if (mp
->mnt_refs
!= 1) {
803 if ((flags
& MNT_FORCE
) == 0) {
804 mount_warning(mp
, "Cannot unmount: "
805 "%d mount refs still present",
809 mount_warning(mp
, "Forced unmount: "
810 "%d mount refs still present",
817 * So far so good, sync the filesystem once more and
818 * call the VFS unmount code if the sync succeeds.
821 if (mp
->mnt_flag
& MNT_RDONLY
) {
822 error
= VFS_UNMOUNT(mp
, flags
);
824 error
= VFS_SYNC(mp
, MNT_WAIT
);
826 (error
== EOPNOTSUPP
) || /* No sync */
827 (flags
& MNT_FORCE
)) {
828 error
= VFS_UNMOUNT(mp
, flags
);
834 * If an error occurred we can still recover, restoring the
835 * syncer vnode and misc flags.
838 if (mp
->mnt_syncer
== NULL
)
839 vfs_allocate_syncvnode(mp
);
840 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
841 mp
->mnt_flag
|= async_flag
;
842 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
843 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
844 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
850 * Clean up any journals still associated with the mount after
851 * filesystem activity has ceased.
853 journal_remove_all_journals(mp
,
854 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
856 mountlist_remove(mp
);
859 * Remove any installed vnode ops here so the individual VFSs don't
862 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
864 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
865 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
866 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
867 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
868 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
870 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
871 nch
= mp
->mnt_ncmountpt
;
872 cache_zero(&mp
->mnt_ncmountpt
);
873 cache_clrmountpt(&nch
);
876 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
877 cache_unmounting(mp
);
878 nch
= mp
->mnt_ncmounton
;
879 cache_zero(&mp
->mnt_ncmounton
);
880 cache_clrmountpt(&nch
);
884 mp
->mnt_vfc
->vfc_refcount
--;
885 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
886 panic("unmount: dangling vnode");
891 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
892 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
893 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
898 * If we reach here and freeok != 0 we must free the mount.
899 * mnt_refs should already have dropped to 0, so if it is not
900 * zero we must cycle the caches and wait.
902 * When we are satisfied that the mount has disconnected we can
903 * drop the hold on the mp that represented the mount (though the
904 * caller might actually have another, so the caller's drop may
905 * do the actual free).
908 if (mp
->mnt_refs
> 0)
909 cache_clearmntcache();
910 while (mp
->mnt_refs
> 0) {
911 cache_unmounting(mp
);
913 tsleep(&mp
->mnt_refs
, 0, "umntrwait", hz
/ 10 + 1);
914 cache_clearmntcache();
916 lwkt_reltoken(&mp
->mnt_token
);
921 KNOTE(&fs_klist
, VQ_UNMOUNT
);
924 lwkt_reltoken(&mp
->mnt_token
);
930 mount_warning(struct mount
*mp
, const char *ctl
, ...)
937 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, NULL
,
938 &ptr
, &buf
, 0) == 0) {
939 kprintf("unmount(%s): ", ptr
);
944 kprintf("unmount(%p", mp
);
945 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
946 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
955 * Shim cache_fullpath() to handle the case where a process is chrooted into
956 * a subdirectory of a mount. In this case if the root mount matches the
957 * process root directory's mount we have to specify the process's root
958 * directory instead of the mount point, because the mount point might
959 * be above the root directory.
963 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
965 struct nchandle
*nch
;
967 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
968 nch
= &p
->p_fd
->fd_nrdir
;
970 nch
= &mp
->mnt_ncmountpt
;
971 return(cache_fullpath(p
, nch
, NULL
, rb
, fb
, 0));
975 * Sync each mounted filesystem.
979 static int syncprt
= 0;
980 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
983 static int sync_callback(struct mount
*mp
, void *data
);
986 sys_sync(struct sync_args
*uap
)
988 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
994 sync_callback(struct mount
*mp
, void *data __unused
)
998 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
999 lwkt_gettoken(&mp
->mnt_token
);
1000 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
1001 mp
->mnt_flag
&= ~MNT_ASYNC
;
1002 lwkt_reltoken(&mp
->mnt_token
);
1003 vfs_msync(mp
, MNT_NOWAIT
);
1004 VFS_SYNC(mp
, MNT_NOWAIT
);
1005 lwkt_gettoken(&mp
->mnt_token
);
1006 mp
->mnt_flag
|= asyncflag
;
1007 lwkt_reltoken(&mp
->mnt_token
);
1012 /* XXX PRISON: could be per prison flag */
1013 static int prison_quotas
;
1015 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
1019 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1021 * Change filesystem quotas.
1026 sys_quotactl(struct quotactl_args
*uap
)
1028 struct nlookupdata nd
;
1034 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
1039 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1041 error
= nlookup(&nd
);
1043 mp
= nd
.nl_nch
.mount
;
1044 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
1045 uap
->arg
, nd
.nl_cred
);
1053 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1054 * void *buf, int buflen)
1056 * This function operates on a mount point and executes the specified
1057 * operation using the specified control data, and possibly returns data.
1059 * The actual number of bytes stored in the result buffer is returned, 0
1060 * if none, otherwise an error is returned.
1065 sys_mountctl(struct mountctl_args
*uap
)
1067 struct thread
*td
= curthread
;
1068 struct proc
*p
= td
->td_proc
;
1076 * Sanity and permissions checks. We must be root.
1079 if (td
->td_ucred
->cr_prison
!= NULL
)
1081 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
1082 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
1086 * Argument length checks
1088 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
1090 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
1092 if (uap
->path
== NULL
)
1096 * Allocate the necessary buffers and copyin data
1098 path
= objcache_get(namei_oc
, M_WAITOK
);
1099 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1104 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1105 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1110 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1113 * Validate the descriptor
1116 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1126 * Execute the internal kernel function and clean up.
1128 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1131 if (error
== 0 && uap
->sysmsg_result
> 0)
1132 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1135 objcache_put(namei_oc
, path
);
1144 * Execute a mount control operation by resolving the path to a mount point
1145 * and calling vop_mountctl().
1147 * Use the mount point from the nch instead of the vnode so nullfs mounts
1148 * can properly spike the VOP.
1151 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1152 const void *ctl
, int ctllen
,
1153 void *buf
, int buflen
, int *res
)
1156 struct nlookupdata nd
;
1157 struct nchandle nch
;
1163 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1166 error
= nlookup(&nd
);
1171 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1178 * Yes, all this is needed to use the nch.mount below, because
1179 * we must maintain a ref on the mount to avoid ripouts (e.g.
1180 * due to heavy mount/unmount use by synth or poudriere).
1183 cache_zero(&nd
.nl_nch
);
1191 * Must be the root of the filesystem
1193 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1198 if (mp
== NULL
|| mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1199 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1205 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1214 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1216 struct thread
*td
= curthread
;
1217 struct proc
*p
= td
->td_proc
;
1220 char *fullpath
, *freepath
;
1223 if ((error
= nlookup(nd
)) != 0)
1225 mp
= nd
->nl_nch
.mount
;
1227 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1230 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1233 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1234 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1235 kfree(freepath
, M_TEMP
);
1237 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1238 bcopy(sp
, buf
, sizeof(*buf
));
1239 /* Only root should have access to the fsid's. */
1240 if (priv_check(td
, PRIV_ROOT
))
1241 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1246 * statfs_args(char *path, struct statfs *buf)
1248 * Get filesystem statistics.
1251 sys_statfs(struct statfs_args
*uap
)
1253 struct nlookupdata nd
;
1257 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1259 error
= kern_statfs(&nd
, &buf
);
1262 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1267 kern_fstatfs(int fd
, struct statfs
*buf
)
1269 struct thread
*td
= curthread
;
1270 struct proc
*p
= td
->td_proc
;
1274 char *fullpath
, *freepath
;
1278 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1282 * Try to use mount info from any overlays rather than the
1283 * mount info for the underlying vnode, otherwise we will
1284 * fail when operating on null-mounted paths inside a chroot.
1286 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1287 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1292 if (fp
->f_cred
== NULL
) {
1297 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1300 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1302 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1303 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1304 kfree(freepath
, M_TEMP
);
1306 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1307 bcopy(sp
, buf
, sizeof(*buf
));
1309 /* Only root should have access to the fsid's. */
1310 if (priv_check(td
, PRIV_ROOT
))
1311 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1319 * fstatfs_args(int fd, struct statfs *buf)
1321 * Get filesystem statistics.
1324 sys_fstatfs(struct fstatfs_args
*uap
)
1329 error
= kern_fstatfs(uap
->fd
, &buf
);
1332 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1337 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1343 if ((error
= nlookup(nd
)) != 0)
1345 mp
= nd
->nl_nch
.mount
;
1346 sp
= &mp
->mnt_vstat
;
1347 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1351 if (mp
->mnt_flag
& MNT_RDONLY
)
1352 sp
->f_flag
|= ST_RDONLY
;
1353 if (mp
->mnt_flag
& MNT_NOSUID
)
1354 sp
->f_flag
|= ST_NOSUID
;
1355 bcopy(sp
, buf
, sizeof(*buf
));
1360 * statfs_args(char *path, struct statfs *buf)
1362 * Get filesystem statistics.
1365 sys_statvfs(struct statvfs_args
*uap
)
1367 struct nlookupdata nd
;
1371 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1373 error
= kern_statvfs(&nd
, &buf
);
1376 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1381 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1383 struct thread
*td
= curthread
;
1384 struct proc
*p
= td
->td_proc
;
1391 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1393 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1394 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1399 if (fp
->f_cred
== NULL
) {
1403 sp
= &mp
->mnt_vstat
;
1404 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1408 if (mp
->mnt_flag
& MNT_RDONLY
)
1409 sp
->f_flag
|= ST_RDONLY
;
1410 if (mp
->mnt_flag
& MNT_NOSUID
)
1411 sp
->f_flag
|= ST_NOSUID
;
1413 bcopy(sp
, buf
, sizeof(*buf
));
1421 * fstatfs_args(int fd, struct statfs *buf)
1423 * Get filesystem statistics.
1426 sys_fstatvfs(struct fstatvfs_args
*uap
)
1431 error
= kern_fstatvfs(uap
->fd
, &buf
);
1434 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1439 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1441 * Get statistics on all filesystems.
1444 struct getfsstat_info
{
1445 struct statfs
*sfsp
;
1453 static int getfsstat_callback(struct mount
*, void *);
1456 sys_getfsstat(struct getfsstat_args
*uap
)
1458 struct thread
*td
= curthread
;
1459 struct getfsstat_info info
;
1461 bzero(&info
, sizeof(info
));
1463 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1464 info
.sfsp
= uap
->buf
;
1466 info
.flags
= uap
->flags
;
1469 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1470 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1471 uap
->sysmsg_result
= info
.maxcount
;
1473 uap
->sysmsg_result
= info
.count
;
1474 return (info
.error
);
1478 getfsstat_callback(struct mount
*mp
, void *data
)
1480 struct getfsstat_info
*info
= data
;
1486 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1487 if (info
->td
->td_proc
&&
1488 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1494 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1495 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1496 * overrides MNT_WAIT.
1498 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1499 (info
->flags
& MNT_WAIT
)) &&
1500 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1503 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1505 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1507 info
->error
= error
;
1510 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1511 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1512 kfree(freepath
, M_TEMP
);
1514 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1516 info
->error
= error
;
1526 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1527 long bufsize, int flags)
1529 * Get statistics on all filesystems.
1532 struct getvfsstat_info
{
1533 struct statfs
*sfsp
;
1534 struct statvfs
*vsfsp
;
1542 static int getvfsstat_callback(struct mount
*, void *);
1545 sys_getvfsstat(struct getvfsstat_args
*uap
)
1547 struct thread
*td
= curthread
;
1548 struct getvfsstat_info info
;
1550 bzero(&info
, sizeof(info
));
1552 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1553 info
.sfsp
= uap
->buf
;
1554 info
.vsfsp
= uap
->vbuf
;
1556 info
.flags
= uap
->flags
;
1559 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1560 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1561 uap
->sysmsg_result
= info
.maxcount
;
1563 uap
->sysmsg_result
= info
.count
;
1564 return (info
.error
);
1568 getvfsstat_callback(struct mount
*mp
, void *data
)
1570 struct getvfsstat_info
*info
= data
;
1572 struct statvfs
*vsp
;
1577 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1578 if (info
->td
->td_proc
&&
1579 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1583 vsp
= &mp
->mnt_vstat
;
1586 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1587 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1588 * overrides MNT_WAIT.
1590 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1591 (info
->flags
& MNT_WAIT
)) &&
1592 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1595 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1597 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1598 (info
->flags
& MNT_WAIT
)) &&
1599 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1603 if (mp
->mnt_flag
& MNT_RDONLY
)
1604 vsp
->f_flag
|= ST_RDONLY
;
1605 if (mp
->mnt_flag
& MNT_NOSUID
)
1606 vsp
->f_flag
|= ST_NOSUID
;
1608 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1610 info
->error
= error
;
1613 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1614 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1615 kfree(freepath
, M_TEMP
);
1617 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1619 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1621 info
->error
= error
;
1633 * fchdir_args(int fd)
1635 * Change current working directory to a given file descriptor.
1638 sys_fchdir(struct fchdir_args
*uap
)
1640 struct thread
*td
= curthread
;
1641 struct proc
*p
= td
->td_proc
;
1642 struct filedesc
*fdp
= p
->p_fd
;
1643 struct vnode
*vp
, *ovp
;
1646 struct nchandle nch
, onch
, tnch
;
1649 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1651 lwkt_gettoken(&p
->p_token
);
1652 vp
= (struct vnode
*)fp
->f_data
;
1654 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1655 if (fp
->f_nchandle
.ncp
== NULL
)
1658 error
= checkvp_chdir(vp
, td
);
1663 cache_copy(&fp
->f_nchandle
, &nch
);
1666 * If the ncp has become a mount point, traverse through
1670 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1671 (mp
= cache_findmount(&nch
)) != NULL
1673 error
= nlookup_mp(mp
, &tnch
);
1675 cache_unlock(&tnch
); /* leave ref intact */
1677 vp
= tnch
.ncp
->nc_vp
;
1678 error
= vget(vp
, LK_SHARED
);
1679 KKASSERT(error
== 0);
1683 cache_dropmount(mp
);
1686 spin_lock(&fdp
->fd_spin
);
1688 onch
= fdp
->fd_ncdir
;
1690 fdp
->fd_ncdir
= nch
;
1691 spin_unlock(&fdp
->fd_spin
);
1692 vn_unlock(vp
); /* leave ref intact */
1701 lwkt_reltoken(&p
->p_token
);
1706 kern_chdir(struct nlookupdata
*nd
)
1708 struct thread
*td
= curthread
;
1709 struct proc
*p
= td
->td_proc
;
1710 struct filedesc
*fdp
= p
->p_fd
;
1711 struct vnode
*vp
, *ovp
;
1712 struct nchandle onch
;
1715 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1716 if ((error
= nlookup(nd
)) != 0)
1718 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1720 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1723 lwkt_gettoken(&p
->p_token
);
1724 error
= checkvp_chdir(vp
, td
);
1727 spin_lock(&fdp
->fd_spin
);
1729 onch
= fdp
->fd_ncdir
;
1730 fdp
->fd_ncdir
= nd
->nl_nch
;
1732 spin_unlock(&fdp
->fd_spin
);
1733 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1736 cache_zero(&nd
->nl_nch
);
1740 lwkt_reltoken(&p
->p_token
);
1745 * chdir_args(char *path)
1747 * Change current working directory (``.'').
1750 sys_chdir(struct chdir_args
*uap
)
1752 struct nlookupdata nd
;
1755 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1757 error
= kern_chdir(&nd
);
1763 * Helper function for raised chroot(2) security function: Refuse if
1764 * any filedescriptors are open directories.
1767 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1774 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1775 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1777 vp
= (struct vnode
*)fp
->f_data
;
1778 if (vp
->v_type
!= VDIR
) {
1789 * This sysctl determines if we will allow a process to chroot(2) if it
1790 * has a directory open:
1791 * 0: disallowed for all processes.
1792 * 1: allowed for processes that were not already chroot(2)'ed.
1793 * 2: allowed for all processes.
1796 static int chroot_allow_open_directories
= 1;
1798 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1799 &chroot_allow_open_directories
, 0, "");
1802 * chroot to the specified namecache entry. We obtain the vp from the
1803 * namecache data. The passed ncp must be locked and referenced and will
1804 * remain locked and referenced on return.
1807 kern_chroot(struct nchandle
*nch
)
1809 struct thread
*td
= curthread
;
1810 struct proc
*p
= td
->td_proc
;
1811 struct filedesc
*fdp
= p
->p_fd
;
1816 * Only privileged user can chroot
1818 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1823 * Disallow open directory descriptors (fchdir() breakouts).
1825 if (chroot_allow_open_directories
== 0 ||
1826 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1827 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1830 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1833 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1837 * Check the validity of vp as a directory to change to and
1838 * associate it with rdir/jdir.
1840 error
= checkvp_chdir(vp
, td
);
1841 vn_unlock(vp
); /* leave reference intact */
1843 lwkt_gettoken(&p
->p_token
);
1844 vrele(fdp
->fd_rdir
);
1845 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1846 cache_drop(&fdp
->fd_nrdir
);
1847 cache_copy(nch
, &fdp
->fd_nrdir
);
1848 if (fdp
->fd_jdir
== NULL
) {
1851 cache_copy(nch
, &fdp
->fd_njdir
);
1853 if ((p
->p_flags
& P_DIDCHROOT
) == 0) {
1854 p
->p_flags
|= P_DIDCHROOT
;
1855 if (p
->p_depth
<= 65535 - 32)
1858 lwkt_reltoken(&p
->p_token
);
1866 * chroot_args(char *path)
1868 * Change notion of root (``/'') directory.
1871 sys_chroot(struct chroot_args
*uap
)
1873 struct thread
*td __debugvar
= curthread
;
1874 struct nlookupdata nd
;
1877 KKASSERT(td
->td_proc
);
1878 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1880 nd
.nl_flags
|= NLC_EXEC
;
1881 error
= nlookup(&nd
);
1883 error
= kern_chroot(&nd
.nl_nch
);
1890 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1892 struct thread
*td
= curthread
;
1893 struct nlookupdata nd
;
1894 struct nchandle
*nch
;
1898 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1902 error
= nlookup(&nd
);
1908 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1912 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1917 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1920 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1921 vfs_cache_setroot(vp
, cache_hold(nch
));
1930 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1931 * determine whether it is legal to chdir to the vnode. The vnode's state
1932 * is not changed by this call.
1935 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1939 if (vp
->v_type
!= VDIR
)
1942 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1947 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1949 struct thread
*td
= curthread
;
1950 struct proc
*p
= td
->td_proc
;
1951 struct lwp
*lp
= td
->td_lwp
;
1952 struct filedesc
*fdp
= p
->p_fd
;
1957 int type
, indx
, error
= 0;
1960 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1962 flags
= FFLAGS(oflags
);
1963 error
= falloc(lp
, &nfp
, NULL
);
1967 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1970 * XXX p_dupfd is a real mess. It allows a device to return a
1971 * file descriptor to be duplicated rather then doing the open
1977 * Call vn_open() to do the lookup and assign the vnode to the
1978 * file pointer. vn_open() does not change the ref count on fp
1979 * and the vnode, on success, will be inherited by the file pointer
1982 * Request a shared lock on the vnode if possible.
1984 * Executable binaries can race VTEXT against O_RDWR opens, so
1985 * use an exclusive lock for O_RDWR opens as well.
1987 * NOTE: We need a flag to separate terminal vnode locking from
1988 * parent locking. O_CREAT needs parent locking, but O_TRUNC
1989 * and O_RDWR only need to lock the terminal vnode exclusively.
1991 nd
->nl_flags
|= NLC_LOCKVP
;
1992 if ((flags
& (O_CREAT
|O_TRUNC
|O_RDWR
)) == 0)
1993 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1995 error
= vn_open(nd
, fp
, flags
, cmode
);
2000 * handle special fdopen() case. bleh. dupfdopen() is
2001 * responsible for dropping the old contents of ofiles[indx]
2004 * Note that fsetfd() will add a ref to fp which represents
2005 * the fd_files[] assignment. We must still drop our
2008 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
2009 if (fdalloc(p
, 0, &indx
) == 0) {
2010 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
2013 fdrop(fp
); /* our ref */
2016 fsetfd(fdp
, NULL
, indx
);
2019 fdrop(fp
); /* our ref */
2020 if (error
== ERESTART
)
2026 * ref the vnode for ourselves so it can't be ripped out from under
2027 * is. XXX need an ND flag to request that the vnode be returned
2030 * Reserve a file descriptor but do not assign it until the open
2033 vp
= (struct vnode
*)fp
->f_data
;
2035 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
2042 * If no error occurs the vp will have been assigned to the file
2047 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
2048 lf
.l_whence
= SEEK_SET
;
2051 if (flags
& O_EXLOCK
)
2052 lf
.l_type
= F_WRLCK
;
2054 lf
.l_type
= F_RDLCK
;
2055 if (flags
& FNONBLOCK
)
2060 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
2062 * lock request failed. Clean up the reserved
2066 fsetfd(fdp
, NULL
, indx
);
2070 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
2074 * Assert that all regular file vnodes were created with a object.
2076 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
2077 ("open: regular file has no backing object after vn_open"));
2083 * release our private reference, leaving the one associated with the
2084 * descriptor table intact.
2086 if (oflags
& O_CLOEXEC
)
2087 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
2088 fsetfd(fdp
, fp
, indx
);
2095 * open_args(char *path, int flags, int mode)
2097 * Check permissions, allocate an open file structure,
2098 * and call the device open routine if any.
2101 sys_open(struct open_args
*uap
)
2103 struct nlookupdata nd
;
2106 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2108 error
= kern_open(&nd
, uap
->flags
,
2109 uap
->mode
, &uap
->sysmsg_result
);
2116 * openat_args(int fd, char *path, int flags, int mode)
2119 sys_openat(struct openat_args
*uap
)
2121 struct nlookupdata nd
;
2125 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2127 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2128 &uap
->sysmsg_result
);
2130 nlookup_done_at(&nd
, fp
);
2135 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2137 struct thread
*td
= curthread
;
2138 struct proc
*p
= td
->td_proc
;
2147 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2148 vattr
.va_rmajor
= rmajor
;
2149 vattr
.va_rminor
= rminor
;
2151 switch (mode
& S_IFMT
) {
2152 case S_IFMT
: /* used by badsect to flag bad sectors */
2153 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2154 vattr
.va_type
= VBAD
;
2157 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2158 vattr
.va_type
= VCHR
;
2161 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2162 vattr
.va_type
= VBLK
;
2165 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2168 case S_IFDIR
: /* special directories support for HAMMER */
2169 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2170 vattr
.va_type
= VDIR
;
2181 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2182 if ((error
= nlookup(nd
)) != 0)
2184 if (nd
->nl_nch
.ncp
->nc_vp
)
2186 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2190 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2191 nd
->nl_cred
, NAMEI_CREATE
);
2194 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2195 &vp
, nd
->nl_cred
, &vattr
);
2203 * mknod_args(char *path, int mode, int dev)
2205 * Create a special file.
2208 sys_mknod(struct mknod_args
*uap
)
2210 struct nlookupdata nd
;
2213 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2215 error
= kern_mknod(&nd
, uap
->mode
,
2216 umajor(uap
->dev
), uminor(uap
->dev
));
2223 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2225 * Create a special file. The path is relative to the directory associated
2229 sys_mknodat(struct mknodat_args
*uap
)
2231 struct nlookupdata nd
;
2235 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2237 error
= kern_mknod(&nd
, uap
->mode
,
2238 umajor(uap
->dev
), uminor(uap
->dev
));
2240 nlookup_done_at(&nd
, fp
);
2245 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2247 struct thread
*td
= curthread
;
2248 struct proc
*p
= td
->td_proc
;
2255 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2256 if ((error
= nlookup(nd
)) != 0)
2258 if (nd
->nl_nch
.ncp
->nc_vp
)
2260 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2264 vattr
.va_type
= VFIFO
;
2265 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2267 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2274 * mkfifo_args(char *path, int mode)
2276 * Create a named pipe.
2279 sys_mkfifo(struct mkfifo_args
*uap
)
2281 struct nlookupdata nd
;
2284 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2286 error
= kern_mkfifo(&nd
, uap
->mode
);
2292 * mkfifoat_args(int fd, char *path, mode_t mode)
2294 * Create a named pipe. The path is relative to the directory associated
2298 sys_mkfifoat(struct mkfifoat_args
*uap
)
2300 struct nlookupdata nd
;
2304 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2306 error
= kern_mkfifo(&nd
, uap
->mode
);
2307 nlookup_done_at(&nd
, fp
);
2311 static int hardlink_check_uid
= 0;
2312 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2313 &hardlink_check_uid
, 0,
2314 "Unprivileged processes cannot create hard links to files owned by other "
2316 static int hardlink_check_gid
= 0;
2317 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2318 &hardlink_check_gid
, 0,
2319 "Unprivileged processes cannot create hard links to files owned by other "
2323 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2329 * Shortcut if disabled
2331 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2335 * Privileged user can always hardlink
2337 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2341 * Otherwise only if the originating file is owned by the
2342 * same user or group. Note that any group is allowed if
2343 * the file is owned by the caller.
2345 error
= VOP_GETATTR(vp
, &va
);
2349 if (hardlink_check_uid
) {
2350 if (cred
->cr_uid
!= va
.va_uid
)
2354 if (hardlink_check_gid
) {
2355 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2363 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2365 struct thread
*td
= curthread
;
2370 * Lookup the source and obtained a locked vnode.
2372 * You may only hardlink a file which you have write permission
2373 * on or which you own.
2375 * XXX relookup on vget failure / race ?
2378 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2379 if ((error
= nlookup(nd
)) != 0)
2381 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2382 KKASSERT(vp
!= NULL
);
2383 if (vp
->v_type
== VDIR
)
2384 return (EPERM
); /* POSIX */
2385 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2387 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2391 * Unlock the source so we can lookup the target without deadlocking
2392 * (XXX vp is locked already, possible other deadlock?). The target
2395 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2396 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2397 cache_unlock(&nd
->nl_nch
);
2400 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2401 if ((error
= nlookup(linknd
)) != 0) {
2405 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2409 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2416 * Finally run the new API VOP.
2418 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2420 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2421 vp
, linknd
->nl_cred
);
2428 * link_args(char *path, char *link)
2430 * Make a hard file link.
2433 sys_link(struct link_args
*uap
)
2435 struct nlookupdata nd
, linknd
;
2438 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2440 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2442 error
= kern_link(&nd
, &linknd
);
2443 nlookup_done(&linknd
);
2450 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2452 * Make a hard file link. The path1 argument is relative to the directory
2453 * associated with fd1, and similarly the path2 argument is relative to
2454 * the directory associated with fd2.
2457 sys_linkat(struct linkat_args
*uap
)
2459 struct nlookupdata nd
, linknd
;
2460 struct file
*fp1
, *fp2
;
2463 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2464 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2466 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2467 uap
->path2
, UIO_USERSPACE
, 0);
2469 error
= kern_link(&nd
, &linknd
);
2470 nlookup_done_at(&linknd
, fp2
);
2472 nlookup_done_at(&nd
, fp1
);
2477 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2485 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2486 if ((error
= nlookup(nd
)) != 0)
2488 if (nd
->nl_nch
.ncp
->nc_vp
)
2490 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2494 vattr
.va_mode
= mode
;
2495 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2502 * symlink(char *path, char *link)
2504 * Make a symbolic link.
2507 sys_symlink(struct symlink_args
*uap
)
2509 struct thread
*td
= curthread
;
2510 struct nlookupdata nd
;
2515 path
= objcache_get(namei_oc
, M_WAITOK
);
2516 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2518 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2520 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2521 error
= kern_symlink(&nd
, path
, mode
);
2525 objcache_put(namei_oc
, path
);
2530 * symlinkat_args(char *path1, int fd, char *path2)
2532 * Make a symbolic link. The path2 argument is relative to the directory
2533 * associated with fd.
2536 sys_symlinkat(struct symlinkat_args
*uap
)
2538 struct thread
*td
= curthread
;
2539 struct nlookupdata nd
;
2545 path1
= objcache_get(namei_oc
, M_WAITOK
);
2546 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2548 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2551 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2552 error
= kern_symlink(&nd
, path1
, mode
);
2554 nlookup_done_at(&nd
, fp
);
2556 objcache_put(namei_oc
, path1
);
2561 * undelete_args(char *path)
2563 * Delete a whiteout from the filesystem.
2566 sys_undelete(struct undelete_args
*uap
)
2568 struct nlookupdata nd
;
2571 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2573 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2575 error
= nlookup(&nd
);
2577 error
= ncp_writechk(&nd
.nl_nch
);
2579 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2587 kern_unlink(struct nlookupdata
*nd
)
2592 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2593 if ((error
= nlookup(nd
)) != 0)
2595 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2597 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2602 * unlink_args(char *path)
2604 * Delete a name from the filesystem.
2607 sys_unlink(struct unlink_args
*uap
)
2609 struct nlookupdata nd
;
2612 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2614 error
= kern_unlink(&nd
);
2621 * unlinkat_args(int fd, char *path, int flags)
2623 * Delete the file or directory entry pointed to by fd/path.
2626 sys_unlinkat(struct unlinkat_args
*uap
)
2628 struct nlookupdata nd
;
2632 if (uap
->flags
& ~AT_REMOVEDIR
)
2635 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2637 if (uap
->flags
& AT_REMOVEDIR
)
2638 error
= kern_rmdir(&nd
);
2640 error
= kern_unlink(&nd
);
2642 nlookup_done_at(&nd
, fp
);
2647 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2649 struct thread
*td
= curthread
;
2650 struct proc
*p
= td
->td_proc
;
2657 fp
= holdfp(p
->p_fd
, fd
, -1);
2660 if (fp
->f_type
!= DTYPE_VNODE
) {
2664 vp
= (struct vnode
*)fp
->f_data
;
2668 spin_lock(&fp
->f_spin
);
2669 new_offset
= fp
->f_offset
+ offset
;
2673 error
= VOP_GETATTR(vp
, &vattr
);
2674 spin_lock(&fp
->f_spin
);
2675 new_offset
= offset
+ vattr
.va_size
;
2678 new_offset
= offset
;
2680 spin_lock(&fp
->f_spin
);
2685 spin_lock(&fp
->f_spin
);
2690 * Validate the seek position. Negative offsets are not allowed
2691 * for regular files or directories.
2693 * Normally we would also not want to allow negative offsets for
2694 * character and block-special devices. However kvm addresses
2695 * on 64 bit architectures might appear to be negative and must
2699 if (new_offset
< 0 &&
2700 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2703 fp
->f_offset
= new_offset
;
2706 *res
= fp
->f_offset
;
2707 spin_unlock(&fp
->f_spin
);
2714 * lseek_args(int fd, int pad, off_t offset, int whence)
2716 * Reposition read/write file offset.
2719 sys_lseek(struct lseek_args
*uap
)
2723 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2724 &uap
->sysmsg_offset
);
2730 * Check if current process can access given file. amode is a bitmask of *_OK
2731 * access bits. flags is a bitmask of AT_* flags.
2734 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2739 if (flags
& ~AT_EACCESS
)
2741 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2742 if ((error
= nlookup(nd
)) != 0)
2745 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2749 /* Flags == 0 means only check for existence. */
2758 if ((mode
& VWRITE
) == 0 ||
2759 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2760 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2763 * If the file handle is stale we have to re-resolve the
2764 * entry with the ncp held exclusively. This is a hack
2767 if (error
== ESTALE
) {
2769 cache_unlock(&nd
->nl_nch
);
2770 cache_lock(&nd
->nl_nch
);
2771 cache_setunresolved(&nd
->nl_nch
);
2772 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2785 * access_args(char *path, int flags)
2787 * Check access permissions.
2790 sys_access(struct access_args
*uap
)
2792 struct nlookupdata nd
;
2795 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2797 error
= kern_access(&nd
, uap
->flags
, 0);
2804 * eaccess_args(char *path, int flags)
2806 * Check access permissions.
2809 sys_eaccess(struct eaccess_args
*uap
)
2811 struct nlookupdata nd
;
2814 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2816 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2823 * faccessat_args(int fd, char *path, int amode, int flags)
2825 * Check access permissions.
2828 sys_faccessat(struct faccessat_args
*uap
)
2830 struct nlookupdata nd
;
2834 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2837 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2838 nlookup_done_at(&nd
, fp
);
2843 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2848 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2849 if ((error
= nlookup(nd
)) != 0)
2852 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2855 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2857 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2860 * If the file handle is stale we have to re-resolve the
2861 * entry with the ncp held exclusively. This is a hack
2864 if (error
== ESTALE
) {
2866 cache_unlock(&nd
->nl_nch
);
2867 cache_lock(&nd
->nl_nch
);
2868 cache_setunresolved(&nd
->nl_nch
);
2869 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2879 * stat_args(char *path, struct stat *ub)
2881 * Get file status; this version follows links.
2884 sys_stat(struct stat_args
*uap
)
2886 struct nlookupdata nd
;
2890 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2892 error
= kern_stat(&nd
, &st
);
2894 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2901 * lstat_args(char *path, struct stat *ub)
2903 * Get file status; this version does not follow links.
2906 sys_lstat(struct lstat_args
*uap
)
2908 struct nlookupdata nd
;
2912 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2914 error
= kern_stat(&nd
, &st
);
2916 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2923 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2925 * Get status of file pointed to by fd/path.
2928 sys_fstatat(struct fstatat_args
*uap
)
2930 struct nlookupdata nd
;
2936 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2939 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2941 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2942 UIO_USERSPACE
, flags
);
2944 error
= kern_stat(&nd
, &st
);
2946 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2948 nlookup_done_at(&nd
, fp
);
2953 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2955 struct nlookupdata nd
;
2960 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2962 error
= nlookup(&nd
);
2964 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2967 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2974 * pathconf_Args(char *path, int name)
2976 * Get configurable pathname variables.
2979 sys_pathconf(struct pathconf_args
*uap
)
2981 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2986 * lpathconf_Args(char *path, int name)
2988 * Get configurable pathname variables, but don't follow symlinks.
2991 sys_lpathconf(struct lpathconf_args
*uap
)
2993 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2998 * kern_readlink isn't properly split yet. There is a copyin burried
2999 * in VOP_READLINK().
3002 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
3004 struct thread
*td
= curthread
;
3010 nd
->nl_flags
|= NLC_SHAREDLOCK
;
3011 if ((error
= nlookup(nd
)) != 0)
3013 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
3016 if (vp
->v_type
!= VLNK
) {
3019 aiov
.iov_base
= buf
;
3020 aiov
.iov_len
= count
;
3021 auio
.uio_iov
= &aiov
;
3022 auio
.uio_iovcnt
= 1;
3023 auio
.uio_offset
= 0;
3024 auio
.uio_rw
= UIO_READ
;
3025 auio
.uio_segflg
= UIO_USERSPACE
;
3027 auio
.uio_resid
= count
;
3028 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
3031 *res
= count
- auio
.uio_resid
;
3036 * readlink_args(char *path, char *buf, int count)
3038 * Return target name of a symbolic link.
3041 sys_readlink(struct readlink_args
*uap
)
3043 struct nlookupdata nd
;
3046 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3048 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
3049 &uap
->sysmsg_result
);
3056 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3058 * Return target name of a symbolic link. The path is relative to the
3059 * directory associated with fd.
3062 sys_readlinkat(struct readlinkat_args
*uap
)
3064 struct nlookupdata nd
;
3068 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
3070 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
3071 &uap
->sysmsg_result
);
3073 nlookup_done_at(&nd
, fp
);
3078 setfflags(struct vnode
*vp
, int flags
)
3080 struct thread
*td
= curthread
;
3085 * Prevent non-root users from setting flags on devices. When
3086 * a device is reused, users can retain ownership of the device
3087 * if they are allowed to set flags and programs assume that
3088 * chown can't fail when done as root.
3090 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
3091 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
3095 * note: vget is required for any operation that might mod the vnode
3096 * so VINACTIVE is properly cleared.
3098 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3100 vattr
.va_flags
= flags
;
3101 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3108 * chflags(char *path, int flags)
3110 * Change flags of a file given a path name.
3113 sys_chflags(struct chflags_args
*uap
)
3115 struct nlookupdata nd
;
3120 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3122 error
= nlookup(&nd
);
3124 error
= ncp_writechk(&nd
.nl_nch
);
3126 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3129 error
= setfflags(vp
, uap
->flags
);
3136 * lchflags(char *path, int flags)
3138 * Change flags of a file given a path name, but don't follow symlinks.
3141 sys_lchflags(struct lchflags_args
*uap
)
3143 struct nlookupdata nd
;
3148 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3150 error
= nlookup(&nd
);
3152 error
= ncp_writechk(&nd
.nl_nch
);
3154 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3157 error
= setfflags(vp
, uap
->flags
);
3164 * fchflags_args(int fd, int flags)
3166 * Change flags of a file given a file descriptor.
3169 sys_fchflags(struct fchflags_args
*uap
)
3171 struct thread
*td
= curthread
;
3172 struct proc
*p
= td
->td_proc
;
3176 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3178 if (fp
->f_nchandle
.ncp
)
3179 error
= ncp_writechk(&fp
->f_nchandle
);
3181 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3187 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3188 * change flags given a pathname relative to a filedescriptor
3190 int sys_chflagsat(struct chflagsat_args
*uap
)
3192 struct nlookupdata nd
;
3198 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3201 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3204 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3206 error
= nlookup(&nd
);
3208 error
= ncp_writechk(&nd
.nl_nch
);
3210 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3211 nlookup_done_at(&nd
, fp
);
3213 error
= setfflags(vp
, uap
->flags
);
3221 setfmode(struct vnode
*vp
, int mode
)
3223 struct thread
*td
= curthread
;
3228 * note: vget is required for any operation that might mod the vnode
3229 * so VINACTIVE is properly cleared.
3231 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3233 vattr
.va_mode
= mode
& ALLPERMS
;
3234 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3235 cache_inval_wxok(vp
);
3242 kern_chmod(struct nlookupdata
*nd
, int mode
)
3247 if ((error
= nlookup(nd
)) != 0)
3249 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3251 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3252 error
= setfmode(vp
, mode
);
3258 * chmod_args(char *path, int mode)
3260 * Change mode of a file given path name.
3263 sys_chmod(struct chmod_args
*uap
)
3265 struct nlookupdata nd
;
3268 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3270 error
= kern_chmod(&nd
, uap
->mode
);
3276 * lchmod_args(char *path, int mode)
3278 * Change mode of a file given path name (don't follow links.)
3281 sys_lchmod(struct lchmod_args
*uap
)
3283 struct nlookupdata nd
;
3286 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3288 error
= kern_chmod(&nd
, uap
->mode
);
3294 * fchmod_args(int fd, int mode)
3296 * Change mode of a file given a file descriptor.
3299 sys_fchmod(struct fchmod_args
*uap
)
3301 struct thread
*td
= curthread
;
3302 struct proc
*p
= td
->td_proc
;
3306 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3308 if (fp
->f_nchandle
.ncp
)
3309 error
= ncp_writechk(&fp
->f_nchandle
);
3311 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3317 * fchmodat_args(char *path, int mode)
3319 * Change mode of a file pointed to by fd/path.
3322 sys_fchmodat(struct fchmodat_args
*uap
)
3324 struct nlookupdata nd
;
3329 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3331 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3333 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3334 UIO_USERSPACE
, flags
);
3336 error
= kern_chmod(&nd
, uap
->mode
);
3337 nlookup_done_at(&nd
, fp
);
3342 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3344 struct thread
*td
= curthread
;
3352 * note: vget is required for any operation that might mod the vnode
3353 * so VINACTIVE is properly cleared.
3355 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3356 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3358 o_uid
= vattr
.va_uid
;
3359 o_gid
= vattr
.va_gid
;
3360 size
= vattr
.va_size
;
3365 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3374 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3375 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3382 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3387 if ((error
= nlookup(nd
)) != 0)
3389 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3391 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3392 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3398 * chown(char *path, int uid, int gid)
3400 * Set ownership given a path name.
3403 sys_chown(struct chown_args
*uap
)
3405 struct nlookupdata nd
;
3408 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3410 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3416 * lchown_args(char *path, int uid, int gid)
3418 * Set ownership given a path name, do not cross symlinks.
3421 sys_lchown(struct lchown_args
*uap
)
3423 struct nlookupdata nd
;
3426 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3428 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3434 * fchown_args(int fd, int uid, int gid)
3436 * Set ownership given a file descriptor.
3439 sys_fchown(struct fchown_args
*uap
)
3441 struct thread
*td
= curthread
;
3442 struct proc
*p
= td
->td_proc
;
3446 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3448 if (fp
->f_nchandle
.ncp
)
3449 error
= ncp_writechk(&fp
->f_nchandle
);
3451 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3452 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3458 * fchownat(int fd, char *path, int uid, int gid, int flags)
3460 * Set ownership of file pointed to by fd/path.
3463 sys_fchownat(struct fchownat_args
*uap
)
3465 struct nlookupdata nd
;
3470 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3472 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3474 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3475 UIO_USERSPACE
, flags
);
3477 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3478 nlookup_done_at(&nd
, fp
);
3484 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3486 struct timeval tv
[2];
3491 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3494 if ((error
= itimerfix(tvp
)) != 0)
3496 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3497 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3503 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3505 struct timespec tsnow
;
3519 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3521 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3524 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3525 newts
[0].tv_sec
= VNOVAL
;
3526 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3528 else if ((error
= itimespecfix(&newts
[0])) != 0)
3531 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3532 newts
[1].tv_sec
= VNOVAL
;
3533 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3535 else if ((error
= itimespecfix(&newts
[1])) != 0)
3542 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3543 const struct timespec
*ts
, int nullflag
)
3545 struct thread
*td
= curthread
;
3549 vattr
->va_atime
= ts
[0];
3550 vattr
->va_mtime
= ts
[1];
3552 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3553 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3559 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3561 struct timespec ts
[2];
3565 if ((error
= getutimes(tptr
, ts
)) != 0)
3568 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3573 * utimes_args(char *path, struct timeval *tptr)
3575 * Set the access and modification times of a file.
3578 sys_utimes(struct utimes_args
*uap
)
3580 struct timeval tv
[2];
3581 struct nlookupdata nd
;
3585 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3589 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3591 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3597 * lutimes_args(char *path, struct timeval *tptr)
3599 * Set the access and modification times of a file.
3602 sys_lutimes(struct lutimes_args
*uap
)
3604 struct timeval tv
[2];
3605 struct nlookupdata nd
;
3609 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3613 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3615 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3621 * Set utimes on a file descriptor. The creds used to open the
3622 * file are used to determine whether the operation is allowed
3626 kern_futimens(int fd
, struct timespec
*ts
)
3628 struct thread
*td
= curthread
;
3629 struct proc
*p
= td
->td_proc
;
3630 struct timespec newts
[2];
3637 error
= getutimens(ts
, newts
, &nullflag
);
3640 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3642 if (fp
->f_nchandle
.ncp
)
3643 error
= ncp_writechk(&fp
->f_nchandle
);
3646 error
= vget(vp
, LK_EXCLUSIVE
);
3648 error
= VOP_GETATTR(vp
, &vattr
);
3650 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3654 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3664 * futimens_args(int fd, struct timespec *ts)
3666 * Set the access and modification times of a file.
3669 sys_futimens(struct futimens_args
*uap
)
3671 struct timespec ts
[2];
3675 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3679 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3684 kern_futimes(int fd
, struct timeval
*tptr
)
3686 struct timespec ts
[2];
3690 if ((error
= getutimes(tptr
, ts
)) != 0)
3693 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3698 * futimes_args(int fd, struct timeval *tptr)
3700 * Set the access and modification times of a file.
3703 sys_futimes(struct futimes_args
*uap
)
3705 struct timeval tv
[2];
3709 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3713 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3718 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3720 struct timespec newts
[2];
3726 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3729 error
= getutimens(ts
, newts
, &nullflag
);
3733 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3734 if ((error
= nlookup(nd
)) != 0)
3736 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3738 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3740 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3741 error
= vget(vp
, LK_EXCLUSIVE
);
3743 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3752 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3754 * Set file access and modification times of a file.
3757 sys_utimensat(struct utimensat_args
*uap
)
3759 struct timespec ts
[2];
3760 struct nlookupdata nd
;
3766 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3771 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3772 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3773 UIO_USERSPACE
, flags
);
3775 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3776 nlookup_done_at(&nd
, fp
);
3781 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3788 uint64_t old_size
= 0;
3792 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3793 if ((error
= nlookup(nd
)) != 0)
3795 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3797 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3799 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3804 if (vp
->v_type
== VDIR
) {
3808 if (vfs_quota_enabled
) {
3809 error
= VOP_GETATTR(vp
, &vattr
);
3810 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3813 old_size
= vattr
.va_size
;
3816 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3818 vattr
.va_size
= length
;
3819 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3820 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3828 * truncate(char *path, int pad, off_t length)
3830 * Truncate a file given its path name.
3833 sys_truncate(struct truncate_args
*uap
)
3835 struct nlookupdata nd
;
3838 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3840 error
= kern_truncate(&nd
, uap
->length
);
3846 kern_ftruncate(int fd
, off_t length
)
3848 struct thread
*td
= curthread
;
3849 struct proc
*p
= td
->td_proc
;
3856 uint64_t old_size
= 0;
3861 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3863 if (fp
->f_nchandle
.ncp
) {
3864 error
= ncp_writechk(&fp
->f_nchandle
);
3868 if ((fp
->f_flag
& FWRITE
) == 0) {
3872 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3876 vp
= (struct vnode
*)fp
->f_data
;
3877 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3878 if (vp
->v_type
== VDIR
) {
3884 if (vfs_quota_enabled
) {
3885 error
= VOP_GETATTR(vp
, &vattr
);
3886 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3889 old_size
= vattr
.va_size
;
3892 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3894 vattr
.va_size
= length
;
3895 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3897 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3906 * ftruncate_args(int fd, int pad, off_t length)
3908 * Truncate a file given a file descriptor.
3911 sys_ftruncate(struct ftruncate_args
*uap
)
3915 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3923 * Sync an open file.
3926 sys_fsync(struct fsync_args
*uap
)
3928 struct thread
*td
= curthread
;
3929 struct proc
*p
= td
->td_proc
;
3935 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3937 vp
= (struct vnode
*)fp
->f_data
;
3938 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3939 if ((obj
= vp
->v_object
) != NULL
) {
3940 if (vp
->v_mount
== NULL
||
3941 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3942 vm_object_page_clean(obj
, 0, 0, 0);
3945 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3946 if (error
== 0 && vp
->v_mount
)
3947 error
= buf_fsync(vp
);
3955 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3957 struct nchandle fnchd
;
3958 struct nchandle tnchd
;
3959 struct namecache
*ncp
;
3968 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3969 if ((error
= nlookup(fromnd
)) != 0)
3971 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3973 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3977 * unlock the source nch so we can lookup the target nch without
3978 * deadlocking. The target may or may not exist so we do not check
3979 * for a target vp like kern_mkdir() and other creation functions do.
3981 * The source and target directories are ref'd and rechecked after
3982 * everything is relocked to determine if the source or target file
3985 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3986 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3988 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3990 cache_unlock(&fromnd
->nl_nch
);
3992 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3993 if ((error
= nlookup(tond
)) != 0) {
3997 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3999 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
4003 tnchd
.mount
= tond
->nl_nch
.mount
;
4007 * If the source and target are the same there is nothing to do
4009 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
4016 * Mount points cannot be renamed or overwritten
4018 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
4027 * Relock the source ncp. cache_relock() will deal with any
4028 * deadlocks against the already-locked tond and will also
4029 * make sure both are resolved.
4031 * NOTE AFTER RELOCKING: The source or target ncp may have become
4032 * invalid while they were unlocked, nc_vp and nc_mount could
4035 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
4036 &tond
->nl_nch
, tond
->nl_cred
);
4037 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
4040 * If the namecache generation changed for either fromnd or tond,
4043 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
4044 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
4045 kprintf("kern_rename: retry due to gen on: "
4046 "\"%s\" -> \"%s\"\n",
4047 fromnd
->nl_nch
.ncp
->nc_name
,
4048 tond
->nl_nch
.ncp
->nc_name
);
4055 * If either fromnd or tond are marked destroyed a ripout occured
4056 * out from under us and we must retry.
4058 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
4059 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
4060 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
4061 kprintf("kern_rename: retry due to ripout on: "
4062 "\"%s\" -> \"%s\"\n",
4063 fromnd
->nl_nch
.ncp
->nc_name
,
4064 tond
->nl_nch
.ncp
->nc_name
);
4071 * Make sure the parent directories linkages are the same.
4072 * XXX shouldn't be needed any more w/ generation check above.
4074 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
4075 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
4082 * Both the source and target must be within the same filesystem and
4083 * in the same filesystem as their parent directories within the
4084 * namecache topology.
4086 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4089 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
4090 mp
!= tond
->nl_nch
.mount
) {
4097 * Make sure the mount point is writable
4099 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4106 * If the target exists and either the source or target is a directory,
4107 * then both must be directories.
4109 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4112 if (tond
->nl_nch
.ncp
->nc_vp
) {
4113 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4115 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4116 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4118 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4124 * You cannot rename a source into itself or a subdirectory of itself.
4125 * We check this by travsersing the target directory upwards looking
4126 * for a match against the source.
4131 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4132 if (fromnd
->nl_nch
.ncp
== ncp
) {
4143 * Even though the namespaces are different, they may still represent
4144 * hardlinks to the same file. The filesystem might have a hard time
4145 * with this so we issue a NREMOVE of the source instead of a NRENAME
4146 * when we detect the situation.
4149 fdvp
= fromnd
->nl_dvp
;
4150 tdvp
= tond
->nl_dvp
;
4151 if (fdvp
== NULL
|| tdvp
== NULL
) {
4153 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4154 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4157 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4158 fdvp
, tdvp
, tond
->nl_cred
);
4165 * rename_args(char *from, char *to)
4167 * Rename files. Source and destination must either both be directories,
4168 * or both not be directories. If target is a directory, it must be empty.
4171 sys_rename(struct rename_args
*uap
)
4173 struct nlookupdata fromnd
, tond
;
4177 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4179 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4181 error
= kern_rename(&fromnd
, &tond
);
4182 nlookup_done(&tond
);
4184 nlookup_done(&fromnd
);
4185 } while (error
== EAGAIN
);
4190 * renameat_args(int oldfd, char *old, int newfd, char *new)
4192 * Rename files using paths relative to the directories associated with
4193 * oldfd and newfd. Source and destination must either both be directories,
4194 * or both not be directories. If target is a directory, it must be empty.
4197 sys_renameat(struct renameat_args
*uap
)
4199 struct nlookupdata oldnd
, newnd
;
4200 struct file
*oldfp
, *newfp
;
4204 error
= nlookup_init_at(&oldnd
, &oldfp
,
4205 uap
->oldfd
, uap
->old
,
4208 error
= nlookup_init_at(&newnd
, &newfp
,
4209 uap
->newfd
, uap
->new,
4212 error
= kern_rename(&oldnd
, &newnd
);
4213 nlookup_done_at(&newnd
, newfp
);
4215 nlookup_done_at(&oldnd
, oldfp
);
4216 } while (error
== EAGAIN
);
4221 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4223 struct thread
*td
= curthread
;
4224 struct proc
*p
= td
->td_proc
;
4230 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4231 if ((error
= nlookup(nd
)) != 0)
4234 if (nd
->nl_nch
.ncp
->nc_vp
)
4236 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4239 vattr
.va_type
= VDIR
;
4240 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4243 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4250 * mkdir_args(char *path, int mode)
4252 * Make a directory file.
4255 sys_mkdir(struct mkdir_args
*uap
)
4257 struct nlookupdata nd
;
4260 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4262 error
= kern_mkdir(&nd
, uap
->mode
);
4268 * mkdirat_args(int fd, char *path, mode_t mode)
4270 * Make a directory file. The path is relative to the directory associated
4274 sys_mkdirat(struct mkdirat_args
*uap
)
4276 struct nlookupdata nd
;
4280 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4282 error
= kern_mkdir(&nd
, uap
->mode
);
4283 nlookup_done_at(&nd
, fp
);
4288 kern_rmdir(struct nlookupdata
*nd
)
4293 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4294 if ((error
= nlookup(nd
)) != 0)
4298 * Do not allow directories representing mount points to be
4299 * deleted, even if empty. Check write perms on mount point
4300 * in case the vnode is aliased (aka nullfs).
4302 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4304 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4306 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4311 * rmdir_args(char *path)
4313 * Remove a directory file.
4316 sys_rmdir(struct rmdir_args
*uap
)
4318 struct nlookupdata nd
;
4321 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4323 error
= kern_rmdir(&nd
);
4329 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4330 enum uio_seg direction
)
4332 struct thread
*td
= curthread
;
4333 struct proc
*p
= td
->td_proc
;
4341 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4343 if ((fp
->f_flag
& FREAD
) == 0) {
4347 vp
= (struct vnode
*)fp
->f_data
;
4348 if (vp
->v_type
!= VDIR
) {
4352 aiov
.iov_base
= buf
;
4353 aiov
.iov_len
= count
;
4354 auio
.uio_iov
= &aiov
;
4355 auio
.uio_iovcnt
= 1;
4356 auio
.uio_rw
= UIO_READ
;
4357 auio
.uio_segflg
= direction
;
4359 auio
.uio_resid
= count
;
4360 loff
= auio
.uio_offset
= fp
->f_offset
;
4361 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4362 fp
->f_offset
= auio
.uio_offset
;
4367 * WARNING! *basep may not be wide enough to accomodate the
4368 * seek offset. XXX should we hack this to return the upper 32 bits
4369 * for offsets greater then 4G?
4372 *basep
= (long)loff
;
4374 *res
= count
- auio
.uio_resid
;
4381 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4383 * Read a block of directory entries in a file system independent format.
4386 sys_getdirentries(struct getdirentries_args
*uap
)
4391 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4392 &uap
->sysmsg_result
, UIO_USERSPACE
);
4394 if (error
== 0 && uap
->basep
)
4395 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4400 * getdents_args(int fd, char *buf, size_t count)
4403 sys_getdents(struct getdents_args
*uap
)
4407 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4408 &uap
->sysmsg_result
, UIO_USERSPACE
);
4414 * Set the mode mask for creation of filesystem nodes.
4416 * umask(int newmask)
4419 sys_umask(struct umask_args
*uap
)
4421 struct thread
*td
= curthread
;
4422 struct proc
*p
= td
->td_proc
;
4423 struct filedesc
*fdp
;
4426 uap
->sysmsg_result
= fdp
->fd_cmask
;
4427 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4432 * revoke(char *path)
4434 * Void all references to file by ripping underlying filesystem
4438 sys_revoke(struct revoke_args
*uap
)
4440 struct nlookupdata nd
;
4447 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4449 error
= nlookup(&nd
);
4451 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4452 cred
= crhold(nd
.nl_cred
);
4456 error
= VOP_GETATTR(vp
, &vattr
);
4457 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4458 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4459 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4461 error
= vrevoke(vp
, cred
);
4462 } else if (error
== 0) {
4463 error
= vrevoke(vp
, cred
);
4473 * getfh_args(char *fname, fhandle_t *fhp)
4475 * Get (NFS) file handle
4477 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4478 * mount. This allows nullfs mounts to be explicitly exported.
4480 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4482 * nullfs mounts of subdirectories are not safe. That is, it will
4483 * work, but you do not really have protection against access to
4484 * the related parent directories.
4487 sys_getfh(struct getfh_args
*uap
)
4489 struct thread
*td
= curthread
;
4490 struct nlookupdata nd
;
4497 * Must be super user
4499 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4503 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4505 error
= nlookup(&nd
);
4507 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4508 mp
= nd
.nl_nch
.mount
;
4511 bzero(&fh
, sizeof(fh
));
4512 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4513 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4516 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4522 * fhopen_args(const struct fhandle *u_fhp, int flags)
4524 * syscall for the rpc.lockd to use to translate a NFS file handle into
4525 * an open descriptor.
4527 * warning: do not remove the priv_check() call or this becomes one giant
4531 sys_fhopen(struct fhopen_args
*uap
)
4533 struct thread
*td
= curthread
;
4534 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4539 struct vattr
*vap
= &vat
;
4541 int fmode
, mode
, error
= 0, type
;
4547 * Must be super user
4549 error
= priv_check(td
, PRIV_ROOT
);
4553 fmode
= FFLAGS(uap
->flags
);
4556 * Why not allow a non-read/write open for our lockd?
4558 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4560 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4565 * Find the mount point
4567 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4572 /* now give me my vnode, it gets returned to me locked */
4573 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4577 * from now on we have to make sure not
4578 * to forget about the vnode
4579 * any error that causes an abort must vput(vp)
4580 * just set error = err and 'goto bad;'.
4586 if (vp
->v_type
== VLNK
) {
4590 if (vp
->v_type
== VSOCK
) {
4595 if (fmode
& (FWRITE
| O_TRUNC
)) {
4596 if (vp
->v_type
== VDIR
) {
4600 error
= vn_writechk(vp
, NULL
);
4608 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4612 if (fmode
& O_TRUNC
) {
4613 vn_unlock(vp
); /* XXX */
4614 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4617 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4623 * VOP_OPEN needs the file pointer so it can potentially override
4626 * WARNING! no f_nchandle will be associated when fhopen()ing a
4629 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4633 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4636 * setting f_ops this way prevents VOP_CLOSE from being
4637 * called or fdrop() releasing the vp from v_data. Since
4638 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4640 fp
->f_ops
= &badfileops
;
4646 * The fp is given its own reference, we still have our ref and lock.
4648 * Assert that all regular files must be created with a VM object.
4650 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4651 kprintf("fhopen: regular file did not "
4652 "have VM object: %p\n",
4658 * The open was successful. Handle any locking requirements.
4660 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4661 lf
.l_whence
= SEEK_SET
;
4664 if (fmode
& O_EXLOCK
)
4665 lf
.l_type
= F_WRLCK
;
4667 lf
.l_type
= F_RDLCK
;
4668 if (fmode
& FNONBLOCK
)
4673 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
,
4676 * release our private reference.
4678 fsetfd(fdp
, NULL
, indx
);
4683 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4684 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4688 * Clean up. Associate the file pointer with the previously
4689 * reserved descriptor and return it.
4692 if (uap
->flags
& O_CLOEXEC
)
4693 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4694 fsetfd(fdp
, fp
, indx
);
4696 uap
->sysmsg_result
= indx
;
4702 fsetfd(fdp
, NULL
, indx
);
4713 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4716 sys_fhstat(struct fhstat_args
*uap
)
4718 struct thread
*td
= curthread
;
4726 * Must be super user
4728 error
= priv_check(td
, PRIV_ROOT
);
4732 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4736 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4739 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4740 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4745 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4753 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4756 sys_fhstatfs(struct fhstatfs_args
*uap
)
4758 struct thread
*td
= curthread
;
4759 struct proc
*p
= td
->td_proc
;
4764 char *fullpath
, *freepath
;
4769 * Must be super user
4771 if ((error
= priv_check(td
, PRIV_ROOT
)))
4774 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4777 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4781 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4786 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4791 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4794 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4797 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4798 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4799 kfree(freepath
, M_TEMP
);
4801 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4802 if (priv_check(td
, PRIV_ROOT
)) {
4803 bcopy(sp
, &sb
, sizeof(sb
));
4804 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4807 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4816 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4819 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4821 struct thread
*td
= curthread
;
4822 struct proc
*p
= td
->td_proc
;
4830 * Must be super user
4832 if ((error
= priv_check(td
, PRIV_ROOT
)))
4835 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4838 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4842 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4847 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4850 sp
= &mp
->mnt_vstat
;
4852 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4856 if (mp
->mnt_flag
& MNT_RDONLY
)
4857 sp
->f_flag
|= ST_RDONLY
;
4858 if (mp
->mnt_flag
& MNT_NOSUID
)
4859 sp
->f_flag
|= ST_NOSUID
;
4860 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4869 * Syscall to push extended attribute configuration information into the
4870 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4871 * a command (int cmd), and attribute name and misc data. For now, the
4872 * attribute name is left in userspace for consumption by the VFS_op.
4873 * It will probably be changed to be copied into sysspace by the
4874 * syscall in the future, once issues with various consumers of the
4875 * attribute code have raised their hands.
4877 * Currently this is used only by UFS Extended Attributes.
4880 sys_extattrctl(struct extattrctl_args
*uap
)
4882 struct nlookupdata nd
;
4884 char attrname
[EXTATTR_MAXNAMELEN
];
4892 if (error
== 0 && uap
->filename
) {
4893 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4896 error
= nlookup(&nd
);
4898 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4902 if (error
== 0 && uap
->attrname
) {
4903 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4908 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4910 error
= nlookup(&nd
);
4912 error
= ncp_writechk(&nd
.nl_nch
);
4914 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4916 uap
->attrname
, nd
.nl_cred
);
4925 * Syscall to get a named extended attribute on a file or directory.
4928 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4930 char attrname
[EXTATTR_MAXNAMELEN
];
4931 struct nlookupdata nd
;
4937 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4943 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4945 error
= nlookup(&nd
);
4947 error
= ncp_writechk(&nd
.nl_nch
);
4949 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4955 bzero(&auio
, sizeof(auio
));
4956 aiov
.iov_base
= uap
->data
;
4957 aiov
.iov_len
= uap
->nbytes
;
4958 auio
.uio_iov
= &aiov
;
4959 auio
.uio_iovcnt
= 1;
4960 auio
.uio_offset
= 0;
4961 auio
.uio_resid
= uap
->nbytes
;
4962 auio
.uio_rw
= UIO_WRITE
;
4963 auio
.uio_td
= curthread
;
4965 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4974 * Syscall to get a named extended attribute on a file or directory.
4977 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4979 char attrname
[EXTATTR_MAXNAMELEN
];
4980 struct nlookupdata nd
;
4986 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4992 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4994 error
= nlookup(&nd
);
4996 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
5002 bzero(&auio
, sizeof(auio
));
5003 aiov
.iov_base
= uap
->data
;
5004 aiov
.iov_len
= uap
->nbytes
;
5005 auio
.uio_iov
= &aiov
;
5006 auio
.uio_iovcnt
= 1;
5007 auio
.uio_offset
= 0;
5008 auio
.uio_resid
= uap
->nbytes
;
5009 auio
.uio_rw
= UIO_READ
;
5010 auio
.uio_td
= curthread
;
5012 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
5014 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
5022 * Syscall to delete a named extended attribute from a file or directory.
5023 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5026 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
5028 char attrname
[EXTATTR_MAXNAMELEN
];
5029 struct nlookupdata nd
;
5033 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
5037 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
5039 error
= nlookup(&nd
);
5041 error
= ncp_writechk(&nd
.nl_nch
);
5043 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
5045 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
5046 attrname
, NULL
, nd
.nl_cred
);
5055 * Determine if the mount is visible to the process.
5058 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
5060 struct nchandle nch
;
5063 * Traverse from the mount point upwards. If we hit the process
5064 * root then the mount point is visible to the process.
5066 nch
= mp
->mnt_ncmountpt
;
5068 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
5069 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
5072 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
5073 nch
= nch
.mount
->mnt_ncmounton
;
5075 nch
.ncp
= nch
.ncp
->nc_parent
;
5080 * If the mount point is not visible to the process, but the
5081 * process root is in a subdirectory of the mount, return
5084 if (p
->p_fd
->fd_nrdir
.mount
== mp
)