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 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
1000 mp
->mnt_flag
&= ~MNT_ASYNC
;
1001 vfs_msync(mp
, MNT_NOWAIT
);
1002 VFS_SYNC(mp
, MNT_NOWAIT
);
1003 mp
->mnt_flag
|= asyncflag
;
1008 /* XXX PRISON: could be per prison flag */
1009 static int prison_quotas
;
1011 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
1015 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1017 * Change filesystem quotas.
1022 sys_quotactl(struct quotactl_args
*uap
)
1024 struct nlookupdata nd
;
1030 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
1035 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1037 error
= nlookup(&nd
);
1039 mp
= nd
.nl_nch
.mount
;
1040 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
1041 uap
->arg
, nd
.nl_cred
);
1049 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1050 * void *buf, int buflen)
1052 * This function operates on a mount point and executes the specified
1053 * operation using the specified control data, and possibly returns data.
1055 * The actual number of bytes stored in the result buffer is returned, 0
1056 * if none, otherwise an error is returned.
1061 sys_mountctl(struct mountctl_args
*uap
)
1063 struct thread
*td
= curthread
;
1064 struct proc
*p
= td
->td_proc
;
1072 * Sanity and permissions checks. We must be root.
1075 if (td
->td_ucred
->cr_prison
!= NULL
)
1077 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
1078 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
1082 * Argument length checks
1084 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
1086 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
1088 if (uap
->path
== NULL
)
1092 * Allocate the necessary buffers and copyin data
1094 path
= objcache_get(namei_oc
, M_WAITOK
);
1095 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1100 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1101 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1106 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1109 * Validate the descriptor
1112 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1122 * Execute the internal kernel function and clean up.
1124 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1127 if (error
== 0 && uap
->sysmsg_result
> 0)
1128 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1131 objcache_put(namei_oc
, path
);
1140 * Execute a mount control operation by resolving the path to a mount point
1141 * and calling vop_mountctl().
1143 * Use the mount point from the nch instead of the vnode so nullfs mounts
1144 * can properly spike the VOP.
1147 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1148 const void *ctl
, int ctllen
,
1149 void *buf
, int buflen
, int *res
)
1152 struct nlookupdata nd
;
1153 struct nchandle nch
;
1159 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1162 error
= nlookup(&nd
);
1167 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1174 * Yes, all this is needed to use the nch.mount below, because
1175 * we must maintain a ref on the mount to avoid ripouts (e.g.
1176 * due to heavy mount/unmount use by synth or poudriere).
1179 cache_zero(&nd
.nl_nch
);
1187 * Must be the root of the filesystem
1189 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1194 if (mp
== NULL
|| mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1195 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1201 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1210 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1212 struct thread
*td
= curthread
;
1213 struct proc
*p
= td
->td_proc
;
1216 char *fullpath
, *freepath
;
1219 if ((error
= nlookup(nd
)) != 0)
1221 mp
= nd
->nl_nch
.mount
;
1223 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1226 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1229 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1230 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1231 kfree(freepath
, M_TEMP
);
1233 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1234 bcopy(sp
, buf
, sizeof(*buf
));
1235 /* Only root should have access to the fsid's. */
1236 if (priv_check(td
, PRIV_ROOT
))
1237 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1242 * statfs_args(char *path, struct statfs *buf)
1244 * Get filesystem statistics.
1247 sys_statfs(struct statfs_args
*uap
)
1249 struct nlookupdata nd
;
1253 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1255 error
= kern_statfs(&nd
, &buf
);
1258 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1263 kern_fstatfs(int fd
, struct statfs
*buf
)
1265 struct thread
*td
= curthread
;
1266 struct proc
*p
= td
->td_proc
;
1270 char *fullpath
, *freepath
;
1274 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1278 * Try to use mount info from any overlays rather than the
1279 * mount info for the underlying vnode, otherwise we will
1280 * fail when operating on null-mounted paths inside a chroot.
1282 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1283 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1288 if (fp
->f_cred
== NULL
) {
1293 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1296 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1298 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1299 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1300 kfree(freepath
, M_TEMP
);
1302 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1303 bcopy(sp
, buf
, sizeof(*buf
));
1305 /* Only root should have access to the fsid's. */
1306 if (priv_check(td
, PRIV_ROOT
))
1307 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1315 * fstatfs_args(int fd, struct statfs *buf)
1317 * Get filesystem statistics.
1320 sys_fstatfs(struct fstatfs_args
*uap
)
1325 error
= kern_fstatfs(uap
->fd
, &buf
);
1328 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1333 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1339 if ((error
= nlookup(nd
)) != 0)
1341 mp
= nd
->nl_nch
.mount
;
1342 sp
= &mp
->mnt_vstat
;
1343 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1347 if (mp
->mnt_flag
& MNT_RDONLY
)
1348 sp
->f_flag
|= ST_RDONLY
;
1349 if (mp
->mnt_flag
& MNT_NOSUID
)
1350 sp
->f_flag
|= ST_NOSUID
;
1351 bcopy(sp
, buf
, sizeof(*buf
));
1356 * statfs_args(char *path, struct statfs *buf)
1358 * Get filesystem statistics.
1361 sys_statvfs(struct statvfs_args
*uap
)
1363 struct nlookupdata nd
;
1367 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1369 error
= kern_statvfs(&nd
, &buf
);
1372 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1377 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1379 struct thread
*td
= curthread
;
1380 struct proc
*p
= td
->td_proc
;
1387 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1389 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1390 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1395 if (fp
->f_cred
== NULL
) {
1399 sp
= &mp
->mnt_vstat
;
1400 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1404 if (mp
->mnt_flag
& MNT_RDONLY
)
1405 sp
->f_flag
|= ST_RDONLY
;
1406 if (mp
->mnt_flag
& MNT_NOSUID
)
1407 sp
->f_flag
|= ST_NOSUID
;
1409 bcopy(sp
, buf
, sizeof(*buf
));
1417 * fstatfs_args(int fd, struct statfs *buf)
1419 * Get filesystem statistics.
1422 sys_fstatvfs(struct fstatvfs_args
*uap
)
1427 error
= kern_fstatvfs(uap
->fd
, &buf
);
1430 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1435 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1437 * Get statistics on all filesystems.
1440 struct getfsstat_info
{
1441 struct statfs
*sfsp
;
1449 static int getfsstat_callback(struct mount
*, void *);
1452 sys_getfsstat(struct getfsstat_args
*uap
)
1454 struct thread
*td
= curthread
;
1455 struct getfsstat_info info
;
1457 bzero(&info
, sizeof(info
));
1459 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1460 info
.sfsp
= uap
->buf
;
1462 info
.flags
= uap
->flags
;
1465 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1466 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1467 uap
->sysmsg_result
= info
.maxcount
;
1469 uap
->sysmsg_result
= info
.count
;
1470 return (info
.error
);
1474 getfsstat_callback(struct mount
*mp
, void *data
)
1476 struct getfsstat_info
*info
= data
;
1482 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1483 if (info
->td
->td_proc
&&
1484 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1490 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1491 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1492 * overrides MNT_WAIT.
1494 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1495 (info
->flags
& MNT_WAIT
)) &&
1496 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1499 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1501 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1503 info
->error
= error
;
1506 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1507 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1508 kfree(freepath
, M_TEMP
);
1510 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1512 info
->error
= error
;
1522 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1523 long bufsize, int flags)
1525 * Get statistics on all filesystems.
1528 struct getvfsstat_info
{
1529 struct statfs
*sfsp
;
1530 struct statvfs
*vsfsp
;
1538 static int getvfsstat_callback(struct mount
*, void *);
1541 sys_getvfsstat(struct getvfsstat_args
*uap
)
1543 struct thread
*td
= curthread
;
1544 struct getvfsstat_info info
;
1546 bzero(&info
, sizeof(info
));
1548 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1549 info
.sfsp
= uap
->buf
;
1550 info
.vsfsp
= uap
->vbuf
;
1552 info
.flags
= uap
->flags
;
1555 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1556 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1557 uap
->sysmsg_result
= info
.maxcount
;
1559 uap
->sysmsg_result
= info
.count
;
1560 return (info
.error
);
1564 getvfsstat_callback(struct mount
*mp
, void *data
)
1566 struct getvfsstat_info
*info
= data
;
1568 struct statvfs
*vsp
;
1573 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1574 if (info
->td
->td_proc
&&
1575 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1579 vsp
= &mp
->mnt_vstat
;
1582 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1583 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1584 * overrides MNT_WAIT.
1586 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1587 (info
->flags
& MNT_WAIT
)) &&
1588 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1591 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1593 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1594 (info
->flags
& MNT_WAIT
)) &&
1595 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1599 if (mp
->mnt_flag
& MNT_RDONLY
)
1600 vsp
->f_flag
|= ST_RDONLY
;
1601 if (mp
->mnt_flag
& MNT_NOSUID
)
1602 vsp
->f_flag
|= ST_NOSUID
;
1604 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1606 info
->error
= error
;
1609 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1610 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1611 kfree(freepath
, M_TEMP
);
1613 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1615 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1617 info
->error
= error
;
1629 * fchdir_args(int fd)
1631 * Change current working directory to a given file descriptor.
1634 sys_fchdir(struct fchdir_args
*uap
)
1636 struct thread
*td
= curthread
;
1637 struct proc
*p
= td
->td_proc
;
1638 struct filedesc
*fdp
= p
->p_fd
;
1639 struct vnode
*vp
, *ovp
;
1642 struct nchandle nch
, onch
, tnch
;
1645 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1647 lwkt_gettoken(&p
->p_token
);
1648 vp
= (struct vnode
*)fp
->f_data
;
1650 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1651 if (fp
->f_nchandle
.ncp
== NULL
)
1654 error
= checkvp_chdir(vp
, td
);
1659 cache_copy(&fp
->f_nchandle
, &nch
);
1662 * If the ncp has become a mount point, traverse through
1666 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1667 (mp
= cache_findmount(&nch
)) != NULL
1669 error
= nlookup_mp(mp
, &tnch
);
1671 cache_unlock(&tnch
); /* leave ref intact */
1673 vp
= tnch
.ncp
->nc_vp
;
1674 error
= vget(vp
, LK_SHARED
);
1675 KKASSERT(error
== 0);
1679 cache_dropmount(mp
);
1682 spin_lock(&fdp
->fd_spin
);
1684 onch
= fdp
->fd_ncdir
;
1686 fdp
->fd_ncdir
= nch
;
1687 spin_unlock(&fdp
->fd_spin
);
1688 vn_unlock(vp
); /* leave ref intact */
1697 lwkt_reltoken(&p
->p_token
);
1702 kern_chdir(struct nlookupdata
*nd
)
1704 struct thread
*td
= curthread
;
1705 struct proc
*p
= td
->td_proc
;
1706 struct filedesc
*fdp
= p
->p_fd
;
1707 struct vnode
*vp
, *ovp
;
1708 struct nchandle onch
;
1711 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1712 if ((error
= nlookup(nd
)) != 0)
1714 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1716 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1719 lwkt_gettoken(&p
->p_token
);
1720 error
= checkvp_chdir(vp
, td
);
1723 spin_lock(&fdp
->fd_spin
);
1725 onch
= fdp
->fd_ncdir
;
1726 fdp
->fd_ncdir
= nd
->nl_nch
;
1728 spin_unlock(&fdp
->fd_spin
);
1729 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1732 cache_zero(&nd
->nl_nch
);
1736 lwkt_reltoken(&p
->p_token
);
1741 * chdir_args(char *path)
1743 * Change current working directory (``.'').
1746 sys_chdir(struct chdir_args
*uap
)
1748 struct nlookupdata nd
;
1751 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1753 error
= kern_chdir(&nd
);
1759 * Helper function for raised chroot(2) security function: Refuse if
1760 * any filedescriptors are open directories.
1763 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1770 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1771 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1773 vp
= (struct vnode
*)fp
->f_data
;
1774 if (vp
->v_type
!= VDIR
) {
1785 * This sysctl determines if we will allow a process to chroot(2) if it
1786 * has a directory open:
1787 * 0: disallowed for all processes.
1788 * 1: allowed for processes that were not already chroot(2)'ed.
1789 * 2: allowed for all processes.
1792 static int chroot_allow_open_directories
= 1;
1794 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1795 &chroot_allow_open_directories
, 0, "");
1798 * chroot to the specified namecache entry. We obtain the vp from the
1799 * namecache data. The passed ncp must be locked and referenced and will
1800 * remain locked and referenced on return.
1803 kern_chroot(struct nchandle
*nch
)
1805 struct thread
*td
= curthread
;
1806 struct proc
*p
= td
->td_proc
;
1807 struct filedesc
*fdp
= p
->p_fd
;
1812 * Only privileged user can chroot
1814 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1819 * Disallow open directory descriptors (fchdir() breakouts).
1821 if (chroot_allow_open_directories
== 0 ||
1822 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1823 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1826 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1829 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1833 * Check the validity of vp as a directory to change to and
1834 * associate it with rdir/jdir.
1836 error
= checkvp_chdir(vp
, td
);
1837 vn_unlock(vp
); /* leave reference intact */
1839 lwkt_gettoken(&p
->p_token
);
1840 vrele(fdp
->fd_rdir
);
1841 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1842 cache_drop(&fdp
->fd_nrdir
);
1843 cache_copy(nch
, &fdp
->fd_nrdir
);
1844 if (fdp
->fd_jdir
== NULL
) {
1847 cache_copy(nch
, &fdp
->fd_njdir
);
1849 if ((p
->p_flags
& P_DIDCHROOT
) == 0) {
1850 p
->p_flags
|= P_DIDCHROOT
;
1851 if (p
->p_depth
<= 65535 - 32)
1854 lwkt_reltoken(&p
->p_token
);
1862 * chroot_args(char *path)
1864 * Change notion of root (``/'') directory.
1867 sys_chroot(struct chroot_args
*uap
)
1869 struct thread
*td __debugvar
= curthread
;
1870 struct nlookupdata nd
;
1873 KKASSERT(td
->td_proc
);
1874 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1876 nd
.nl_flags
|= NLC_EXEC
;
1877 error
= nlookup(&nd
);
1879 error
= kern_chroot(&nd
.nl_nch
);
1886 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1888 struct thread
*td
= curthread
;
1889 struct nlookupdata nd
;
1890 struct nchandle
*nch
;
1894 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1898 error
= nlookup(&nd
);
1904 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1908 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1913 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1916 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1917 vfs_cache_setroot(vp
, cache_hold(nch
));
1926 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1927 * determine whether it is legal to chdir to the vnode. The vnode's state
1928 * is not changed by this call.
1931 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1935 if (vp
->v_type
!= VDIR
)
1938 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1943 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1945 struct thread
*td
= curthread
;
1946 struct proc
*p
= td
->td_proc
;
1947 struct lwp
*lp
= td
->td_lwp
;
1948 struct filedesc
*fdp
= p
->p_fd
;
1953 int type
, indx
, error
= 0;
1956 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1958 flags
= FFLAGS(oflags
);
1959 error
= falloc(lp
, &nfp
, NULL
);
1963 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1966 * XXX p_dupfd is a real mess. It allows a device to return a
1967 * file descriptor to be duplicated rather then doing the open
1973 * Call vn_open() to do the lookup and assign the vnode to the
1974 * file pointer. vn_open() does not change the ref count on fp
1975 * and the vnode, on success, will be inherited by the file pointer
1978 * Request a shared lock on the vnode if possible.
1980 * Executable binaries can race VTEXT against O_RDWR opens, so
1981 * use an exclusive lock for O_RDWR opens as well.
1983 * NOTE: We need a flag to separate terminal vnode locking from
1984 * parent locking. O_CREAT needs parent locking, but O_TRUNC
1985 * and O_RDWR only need to lock the terminal vnode exclusively.
1987 nd
->nl_flags
|= NLC_LOCKVP
;
1988 if ((flags
& (O_CREAT
|O_TRUNC
|O_RDWR
)) == 0)
1989 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1991 error
= vn_open(nd
, fp
, flags
, cmode
);
1996 * handle special fdopen() case. bleh. dupfdopen() is
1997 * responsible for dropping the old contents of ofiles[indx]
2000 * Note that fsetfd() will add a ref to fp which represents
2001 * the fd_files[] assignment. We must still drop our
2004 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
2005 if (fdalloc(p
, 0, &indx
) == 0) {
2006 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
2009 fdrop(fp
); /* our ref */
2012 fsetfd(fdp
, NULL
, indx
);
2015 fdrop(fp
); /* our ref */
2016 if (error
== ERESTART
)
2022 * ref the vnode for ourselves so it can't be ripped out from under
2023 * is. XXX need an ND flag to request that the vnode be returned
2026 * Reserve a file descriptor but do not assign it until the open
2029 vp
= (struct vnode
*)fp
->f_data
;
2031 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
2038 * If no error occurs the vp will have been assigned to the file
2043 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
2044 lf
.l_whence
= SEEK_SET
;
2047 if (flags
& O_EXLOCK
)
2048 lf
.l_type
= F_WRLCK
;
2050 lf
.l_type
= F_RDLCK
;
2051 if (flags
& FNONBLOCK
)
2056 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
2058 * lock request failed. Clean up the reserved
2062 fsetfd(fdp
, NULL
, indx
);
2066 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
2070 * Assert that all regular file vnodes were created with a object.
2072 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
2073 ("open: regular file has no backing object after vn_open"));
2079 * release our private reference, leaving the one associated with the
2080 * descriptor table intact.
2082 if (oflags
& O_CLOEXEC
)
2083 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
2084 fsetfd(fdp
, fp
, indx
);
2091 * open_args(char *path, int flags, int mode)
2093 * Check permissions, allocate an open file structure,
2094 * and call the device open routine if any.
2097 sys_open(struct open_args
*uap
)
2099 struct nlookupdata nd
;
2102 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2104 error
= kern_open(&nd
, uap
->flags
,
2105 uap
->mode
, &uap
->sysmsg_result
);
2112 * openat_args(int fd, char *path, int flags, int mode)
2115 sys_openat(struct openat_args
*uap
)
2117 struct nlookupdata nd
;
2121 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2123 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2124 &uap
->sysmsg_result
);
2126 nlookup_done_at(&nd
, fp
);
2131 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2133 struct thread
*td
= curthread
;
2134 struct proc
*p
= td
->td_proc
;
2143 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2144 vattr
.va_rmajor
= rmajor
;
2145 vattr
.va_rminor
= rminor
;
2147 switch (mode
& S_IFMT
) {
2148 case S_IFMT
: /* used by badsect to flag bad sectors */
2149 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2150 vattr
.va_type
= VBAD
;
2153 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2154 vattr
.va_type
= VCHR
;
2157 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2158 vattr
.va_type
= VBLK
;
2161 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2164 case S_IFDIR
: /* special directories support for HAMMER */
2165 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2166 vattr
.va_type
= VDIR
;
2177 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2178 if ((error
= nlookup(nd
)) != 0)
2180 if (nd
->nl_nch
.ncp
->nc_vp
)
2182 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2186 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2187 nd
->nl_cred
, NAMEI_CREATE
);
2190 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2191 &vp
, nd
->nl_cred
, &vattr
);
2199 * mknod_args(char *path, int mode, int dev)
2201 * Create a special file.
2204 sys_mknod(struct mknod_args
*uap
)
2206 struct nlookupdata nd
;
2209 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2211 error
= kern_mknod(&nd
, uap
->mode
,
2212 umajor(uap
->dev
), uminor(uap
->dev
));
2219 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2221 * Create a special file. The path is relative to the directory associated
2225 sys_mknodat(struct mknodat_args
*uap
)
2227 struct nlookupdata nd
;
2231 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2233 error
= kern_mknod(&nd
, uap
->mode
,
2234 umajor(uap
->dev
), uminor(uap
->dev
));
2236 nlookup_done_at(&nd
, fp
);
2241 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2243 struct thread
*td
= curthread
;
2244 struct proc
*p
= td
->td_proc
;
2251 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2252 if ((error
= nlookup(nd
)) != 0)
2254 if (nd
->nl_nch
.ncp
->nc_vp
)
2256 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2260 vattr
.va_type
= VFIFO
;
2261 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2263 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2270 * mkfifo_args(char *path, int mode)
2272 * Create a named pipe.
2275 sys_mkfifo(struct mkfifo_args
*uap
)
2277 struct nlookupdata nd
;
2280 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2282 error
= kern_mkfifo(&nd
, uap
->mode
);
2288 * mkfifoat_args(int fd, char *path, mode_t mode)
2290 * Create a named pipe. The path is relative to the directory associated
2294 sys_mkfifoat(struct mkfifoat_args
*uap
)
2296 struct nlookupdata nd
;
2300 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2302 error
= kern_mkfifo(&nd
, uap
->mode
);
2303 nlookup_done_at(&nd
, fp
);
2307 static int hardlink_check_uid
= 0;
2308 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2309 &hardlink_check_uid
, 0,
2310 "Unprivileged processes cannot create hard links to files owned by other "
2312 static int hardlink_check_gid
= 0;
2313 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2314 &hardlink_check_gid
, 0,
2315 "Unprivileged processes cannot create hard links to files owned by other "
2319 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2325 * Shortcut if disabled
2327 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2331 * Privileged user can always hardlink
2333 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2337 * Otherwise only if the originating file is owned by the
2338 * same user or group. Note that any group is allowed if
2339 * the file is owned by the caller.
2341 error
= VOP_GETATTR(vp
, &va
);
2345 if (hardlink_check_uid
) {
2346 if (cred
->cr_uid
!= va
.va_uid
)
2350 if (hardlink_check_gid
) {
2351 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2359 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2361 struct thread
*td
= curthread
;
2366 * Lookup the source and obtained a locked vnode.
2368 * You may only hardlink a file which you have write permission
2369 * on or which you own.
2371 * XXX relookup on vget failure / race ?
2374 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2375 if ((error
= nlookup(nd
)) != 0)
2377 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2378 KKASSERT(vp
!= NULL
);
2379 if (vp
->v_type
== VDIR
)
2380 return (EPERM
); /* POSIX */
2381 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2383 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2387 * Unlock the source so we can lookup the target without deadlocking
2388 * (XXX vp is locked already, possible other deadlock?). The target
2391 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2392 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2393 cache_unlock(&nd
->nl_nch
);
2396 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2397 if ((error
= nlookup(linknd
)) != 0) {
2401 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2405 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2412 * Finally run the new API VOP.
2414 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2416 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2417 vp
, linknd
->nl_cred
);
2424 * link_args(char *path, char *link)
2426 * Make a hard file link.
2429 sys_link(struct link_args
*uap
)
2431 struct nlookupdata nd
, linknd
;
2434 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2436 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2438 error
= kern_link(&nd
, &linknd
);
2439 nlookup_done(&linknd
);
2446 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2448 * Make a hard file link. The path1 argument is relative to the directory
2449 * associated with fd1, and similarly the path2 argument is relative to
2450 * the directory associated with fd2.
2453 sys_linkat(struct linkat_args
*uap
)
2455 struct nlookupdata nd
, linknd
;
2456 struct file
*fp1
, *fp2
;
2459 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2460 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2462 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2463 uap
->path2
, UIO_USERSPACE
, 0);
2465 error
= kern_link(&nd
, &linknd
);
2466 nlookup_done_at(&linknd
, fp2
);
2468 nlookup_done_at(&nd
, fp1
);
2473 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2481 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2482 if ((error
= nlookup(nd
)) != 0)
2484 if (nd
->nl_nch
.ncp
->nc_vp
)
2486 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2490 vattr
.va_mode
= mode
;
2491 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2498 * symlink(char *path, char *link)
2500 * Make a symbolic link.
2503 sys_symlink(struct symlink_args
*uap
)
2505 struct thread
*td
= curthread
;
2506 struct nlookupdata nd
;
2511 path
= objcache_get(namei_oc
, M_WAITOK
);
2512 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2514 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2516 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2517 error
= kern_symlink(&nd
, path
, mode
);
2521 objcache_put(namei_oc
, path
);
2526 * symlinkat_args(char *path1, int fd, char *path2)
2528 * Make a symbolic link. The path2 argument is relative to the directory
2529 * associated with fd.
2532 sys_symlinkat(struct symlinkat_args
*uap
)
2534 struct thread
*td
= curthread
;
2535 struct nlookupdata nd
;
2541 path1
= objcache_get(namei_oc
, M_WAITOK
);
2542 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2544 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2547 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2548 error
= kern_symlink(&nd
, path1
, mode
);
2550 nlookup_done_at(&nd
, fp
);
2552 objcache_put(namei_oc
, path1
);
2557 * undelete_args(char *path)
2559 * Delete a whiteout from the filesystem.
2562 sys_undelete(struct undelete_args
*uap
)
2564 struct nlookupdata nd
;
2567 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2569 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2571 error
= nlookup(&nd
);
2573 error
= ncp_writechk(&nd
.nl_nch
);
2575 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2583 kern_unlink(struct nlookupdata
*nd
)
2588 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2589 if ((error
= nlookup(nd
)) != 0)
2591 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2593 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2598 * unlink_args(char *path)
2600 * Delete a name from the filesystem.
2603 sys_unlink(struct unlink_args
*uap
)
2605 struct nlookupdata nd
;
2608 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2610 error
= kern_unlink(&nd
);
2617 * unlinkat_args(int fd, char *path, int flags)
2619 * Delete the file or directory entry pointed to by fd/path.
2622 sys_unlinkat(struct unlinkat_args
*uap
)
2624 struct nlookupdata nd
;
2628 if (uap
->flags
& ~AT_REMOVEDIR
)
2631 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2633 if (uap
->flags
& AT_REMOVEDIR
)
2634 error
= kern_rmdir(&nd
);
2636 error
= kern_unlink(&nd
);
2638 nlookup_done_at(&nd
, fp
);
2643 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2645 struct thread
*td
= curthread
;
2646 struct proc
*p
= td
->td_proc
;
2653 fp
= holdfp(p
->p_fd
, fd
, -1);
2656 if (fp
->f_type
!= DTYPE_VNODE
) {
2660 vp
= (struct vnode
*)fp
->f_data
;
2664 spin_lock(&fp
->f_spin
);
2665 new_offset
= fp
->f_offset
+ offset
;
2669 error
= VOP_GETATTR(vp
, &vattr
);
2670 spin_lock(&fp
->f_spin
);
2671 new_offset
= offset
+ vattr
.va_size
;
2674 new_offset
= offset
;
2676 spin_lock(&fp
->f_spin
);
2681 spin_lock(&fp
->f_spin
);
2686 * Validate the seek position. Negative offsets are not allowed
2687 * for regular files or directories.
2689 * Normally we would also not want to allow negative offsets for
2690 * character and block-special devices. However kvm addresses
2691 * on 64 bit architectures might appear to be negative and must
2695 if (new_offset
< 0 &&
2696 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2699 fp
->f_offset
= new_offset
;
2702 *res
= fp
->f_offset
;
2703 spin_unlock(&fp
->f_spin
);
2710 * lseek_args(int fd, int pad, off_t offset, int whence)
2712 * Reposition read/write file offset.
2715 sys_lseek(struct lseek_args
*uap
)
2719 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2720 &uap
->sysmsg_offset
);
2726 * Check if current process can access given file. amode is a bitmask of *_OK
2727 * access bits. flags is a bitmask of AT_* flags.
2730 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2735 if (flags
& ~AT_EACCESS
)
2737 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2738 if ((error
= nlookup(nd
)) != 0)
2741 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2745 /* Flags == 0 means only check for existence. */
2754 if ((mode
& VWRITE
) == 0 ||
2755 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2756 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2759 * If the file handle is stale we have to re-resolve the
2760 * entry with the ncp held exclusively. This is a hack
2763 if (error
== ESTALE
) {
2765 cache_unlock(&nd
->nl_nch
);
2766 cache_lock(&nd
->nl_nch
);
2767 cache_setunresolved(&nd
->nl_nch
);
2768 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2781 * access_args(char *path, int flags)
2783 * Check access permissions.
2786 sys_access(struct access_args
*uap
)
2788 struct nlookupdata nd
;
2791 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2793 error
= kern_access(&nd
, uap
->flags
, 0);
2800 * eaccess_args(char *path, int flags)
2802 * Check access permissions.
2805 sys_eaccess(struct eaccess_args
*uap
)
2807 struct nlookupdata nd
;
2810 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2812 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2819 * faccessat_args(int fd, char *path, int amode, int flags)
2821 * Check access permissions.
2824 sys_faccessat(struct faccessat_args
*uap
)
2826 struct nlookupdata nd
;
2830 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2833 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2834 nlookup_done_at(&nd
, fp
);
2839 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2844 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2845 if ((error
= nlookup(nd
)) != 0)
2848 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2851 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2853 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2856 * If the file handle is stale we have to re-resolve the
2857 * entry with the ncp held exclusively. This is a hack
2860 if (error
== ESTALE
) {
2862 cache_unlock(&nd
->nl_nch
);
2863 cache_lock(&nd
->nl_nch
);
2864 cache_setunresolved(&nd
->nl_nch
);
2865 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2875 * stat_args(char *path, struct stat *ub)
2877 * Get file status; this version follows links.
2880 sys_stat(struct stat_args
*uap
)
2882 struct nlookupdata nd
;
2886 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2888 error
= kern_stat(&nd
, &st
);
2890 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2897 * lstat_args(char *path, struct stat *ub)
2899 * Get file status; this version does not follow links.
2902 sys_lstat(struct lstat_args
*uap
)
2904 struct nlookupdata nd
;
2908 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2910 error
= kern_stat(&nd
, &st
);
2912 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2919 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2921 * Get status of file pointed to by fd/path.
2924 sys_fstatat(struct fstatat_args
*uap
)
2926 struct nlookupdata nd
;
2932 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2935 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2937 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2938 UIO_USERSPACE
, flags
);
2940 error
= kern_stat(&nd
, &st
);
2942 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2944 nlookup_done_at(&nd
, fp
);
2949 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2951 struct nlookupdata nd
;
2956 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2958 error
= nlookup(&nd
);
2960 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2963 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2970 * pathconf_Args(char *path, int name)
2972 * Get configurable pathname variables.
2975 sys_pathconf(struct pathconf_args
*uap
)
2977 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2982 * lpathconf_Args(char *path, int name)
2984 * Get configurable pathname variables, but don't follow symlinks.
2987 sys_lpathconf(struct lpathconf_args
*uap
)
2989 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2994 * kern_readlink isn't properly split yet. There is a copyin burried
2995 * in VOP_READLINK().
2998 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
3000 struct thread
*td
= curthread
;
3006 nd
->nl_flags
|= NLC_SHAREDLOCK
;
3007 if ((error
= nlookup(nd
)) != 0)
3009 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
3012 if (vp
->v_type
!= VLNK
) {
3015 aiov
.iov_base
= buf
;
3016 aiov
.iov_len
= count
;
3017 auio
.uio_iov
= &aiov
;
3018 auio
.uio_iovcnt
= 1;
3019 auio
.uio_offset
= 0;
3020 auio
.uio_rw
= UIO_READ
;
3021 auio
.uio_segflg
= UIO_USERSPACE
;
3023 auio
.uio_resid
= count
;
3024 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
3027 *res
= count
- auio
.uio_resid
;
3032 * readlink_args(char *path, char *buf, int count)
3034 * Return target name of a symbolic link.
3037 sys_readlink(struct readlink_args
*uap
)
3039 struct nlookupdata nd
;
3042 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3044 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
3045 &uap
->sysmsg_result
);
3052 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3054 * Return target name of a symbolic link. The path is relative to the
3055 * directory associated with fd.
3058 sys_readlinkat(struct readlinkat_args
*uap
)
3060 struct nlookupdata nd
;
3064 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
3066 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
3067 &uap
->sysmsg_result
);
3069 nlookup_done_at(&nd
, fp
);
3074 setfflags(struct vnode
*vp
, int flags
)
3076 struct thread
*td
= curthread
;
3081 * Prevent non-root users from setting flags on devices. When
3082 * a device is reused, users can retain ownership of the device
3083 * if they are allowed to set flags and programs assume that
3084 * chown can't fail when done as root.
3086 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
3087 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
3091 * note: vget is required for any operation that might mod the vnode
3092 * so VINACTIVE is properly cleared.
3094 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3096 vattr
.va_flags
= flags
;
3097 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3104 * chflags(char *path, int flags)
3106 * Change flags of a file given a path name.
3109 sys_chflags(struct chflags_args
*uap
)
3111 struct nlookupdata nd
;
3116 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3118 error
= nlookup(&nd
);
3120 error
= ncp_writechk(&nd
.nl_nch
);
3122 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3125 error
= setfflags(vp
, uap
->flags
);
3132 * lchflags(char *path, int flags)
3134 * Change flags of a file given a path name, but don't follow symlinks.
3137 sys_lchflags(struct lchflags_args
*uap
)
3139 struct nlookupdata nd
;
3144 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3146 error
= nlookup(&nd
);
3148 error
= ncp_writechk(&nd
.nl_nch
);
3150 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3153 error
= setfflags(vp
, uap
->flags
);
3160 * fchflags_args(int fd, int flags)
3162 * Change flags of a file given a file descriptor.
3165 sys_fchflags(struct fchflags_args
*uap
)
3167 struct thread
*td
= curthread
;
3168 struct proc
*p
= td
->td_proc
;
3172 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3174 if (fp
->f_nchandle
.ncp
)
3175 error
= ncp_writechk(&fp
->f_nchandle
);
3177 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3183 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3184 * change flags given a pathname relative to a filedescriptor
3186 int sys_chflagsat(struct chflagsat_args
*uap
)
3188 struct nlookupdata nd
;
3194 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3197 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3200 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3202 error
= nlookup(&nd
);
3204 error
= ncp_writechk(&nd
.nl_nch
);
3206 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3207 nlookup_done_at(&nd
, fp
);
3209 error
= setfflags(vp
, uap
->flags
);
3217 setfmode(struct vnode
*vp
, int mode
)
3219 struct thread
*td
= curthread
;
3224 * note: vget is required for any operation that might mod the vnode
3225 * so VINACTIVE is properly cleared.
3227 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3229 vattr
.va_mode
= mode
& ALLPERMS
;
3230 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3231 cache_inval_wxok(vp
);
3238 kern_chmod(struct nlookupdata
*nd
, int mode
)
3243 if ((error
= nlookup(nd
)) != 0)
3245 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3247 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3248 error
= setfmode(vp
, mode
);
3254 * chmod_args(char *path, int mode)
3256 * Change mode of a file given path name.
3259 sys_chmod(struct chmod_args
*uap
)
3261 struct nlookupdata nd
;
3264 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3266 error
= kern_chmod(&nd
, uap
->mode
);
3272 * lchmod_args(char *path, int mode)
3274 * Change mode of a file given path name (don't follow links.)
3277 sys_lchmod(struct lchmod_args
*uap
)
3279 struct nlookupdata nd
;
3282 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3284 error
= kern_chmod(&nd
, uap
->mode
);
3290 * fchmod_args(int fd, int mode)
3292 * Change mode of a file given a file descriptor.
3295 sys_fchmod(struct fchmod_args
*uap
)
3297 struct thread
*td
= curthread
;
3298 struct proc
*p
= td
->td_proc
;
3302 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3304 if (fp
->f_nchandle
.ncp
)
3305 error
= ncp_writechk(&fp
->f_nchandle
);
3307 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3313 * fchmodat_args(char *path, int mode)
3315 * Change mode of a file pointed to by fd/path.
3318 sys_fchmodat(struct fchmodat_args
*uap
)
3320 struct nlookupdata nd
;
3325 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3327 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3329 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3330 UIO_USERSPACE
, flags
);
3332 error
= kern_chmod(&nd
, uap
->mode
);
3333 nlookup_done_at(&nd
, fp
);
3338 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3340 struct thread
*td
= curthread
;
3348 * note: vget is required for any operation that might mod the vnode
3349 * so VINACTIVE is properly cleared.
3351 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3352 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3354 o_uid
= vattr
.va_uid
;
3355 o_gid
= vattr
.va_gid
;
3356 size
= vattr
.va_size
;
3361 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3370 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3371 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3378 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3383 if ((error
= nlookup(nd
)) != 0)
3385 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3387 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3388 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3394 * chown(char *path, int uid, int gid)
3396 * Set ownership given a path name.
3399 sys_chown(struct chown_args
*uap
)
3401 struct nlookupdata nd
;
3404 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3406 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3412 * lchown_args(char *path, int uid, int gid)
3414 * Set ownership given a path name, do not cross symlinks.
3417 sys_lchown(struct lchown_args
*uap
)
3419 struct nlookupdata nd
;
3422 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3424 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3430 * fchown_args(int fd, int uid, int gid)
3432 * Set ownership given a file descriptor.
3435 sys_fchown(struct fchown_args
*uap
)
3437 struct thread
*td
= curthread
;
3438 struct proc
*p
= td
->td_proc
;
3442 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3444 if (fp
->f_nchandle
.ncp
)
3445 error
= ncp_writechk(&fp
->f_nchandle
);
3447 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3448 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3454 * fchownat(int fd, char *path, int uid, int gid, int flags)
3456 * Set ownership of file pointed to by fd/path.
3459 sys_fchownat(struct fchownat_args
*uap
)
3461 struct nlookupdata nd
;
3466 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3468 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3470 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3471 UIO_USERSPACE
, flags
);
3473 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3474 nlookup_done_at(&nd
, fp
);
3480 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3482 struct timeval tv
[2];
3487 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3490 if ((error
= itimerfix(tvp
)) != 0)
3492 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3493 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3499 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3501 struct timespec tsnow
;
3515 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3517 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3520 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3521 newts
[0].tv_sec
= VNOVAL
;
3522 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3524 else if ((error
= itimespecfix(&newts
[0])) != 0)
3527 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3528 newts
[1].tv_sec
= VNOVAL
;
3529 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3531 else if ((error
= itimespecfix(&newts
[1])) != 0)
3538 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3539 const struct timespec
*ts
, int nullflag
)
3541 struct thread
*td
= curthread
;
3545 vattr
->va_atime
= ts
[0];
3546 vattr
->va_mtime
= ts
[1];
3548 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3549 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3555 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3557 struct timespec ts
[2];
3561 if ((error
= getutimes(tptr
, ts
)) != 0)
3564 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3569 * utimes_args(char *path, struct timeval *tptr)
3571 * Set the access and modification times of a file.
3574 sys_utimes(struct utimes_args
*uap
)
3576 struct timeval tv
[2];
3577 struct nlookupdata nd
;
3581 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3585 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3587 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3593 * lutimes_args(char *path, struct timeval *tptr)
3595 * Set the access and modification times of a file.
3598 sys_lutimes(struct lutimes_args
*uap
)
3600 struct timeval tv
[2];
3601 struct nlookupdata nd
;
3605 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3609 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3611 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3617 * Set utimes on a file descriptor. The creds used to open the
3618 * file are used to determine whether the operation is allowed
3622 kern_futimens(int fd
, struct timespec
*ts
)
3624 struct thread
*td
= curthread
;
3625 struct proc
*p
= td
->td_proc
;
3626 struct timespec newts
[2];
3633 error
= getutimens(ts
, newts
, &nullflag
);
3636 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3638 if (fp
->f_nchandle
.ncp
)
3639 error
= ncp_writechk(&fp
->f_nchandle
);
3642 error
= vget(vp
, LK_EXCLUSIVE
);
3644 error
= VOP_GETATTR(vp
, &vattr
);
3646 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3650 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3660 * futimens_args(int fd, struct timespec *ts)
3662 * Set the access and modification times of a file.
3665 sys_futimens(struct futimens_args
*uap
)
3667 struct timespec ts
[2];
3671 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3675 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3680 kern_futimes(int fd
, struct timeval
*tptr
)
3682 struct timespec ts
[2];
3686 if ((error
= getutimes(tptr
, ts
)) != 0)
3689 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3694 * futimes_args(int fd, struct timeval *tptr)
3696 * Set the access and modification times of a file.
3699 sys_futimes(struct futimes_args
*uap
)
3701 struct timeval tv
[2];
3705 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3709 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3714 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3716 struct timespec newts
[2];
3722 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3725 error
= getutimens(ts
, newts
, &nullflag
);
3729 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3730 if ((error
= nlookup(nd
)) != 0)
3732 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3734 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3736 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3737 error
= vget(vp
, LK_EXCLUSIVE
);
3739 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3748 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3750 * Set file access and modification times of a file.
3753 sys_utimensat(struct utimensat_args
*uap
)
3755 struct timespec ts
[2];
3756 struct nlookupdata nd
;
3762 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3767 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3768 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3769 UIO_USERSPACE
, flags
);
3771 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3772 nlookup_done_at(&nd
, fp
);
3777 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3784 uint64_t old_size
= 0;
3788 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3789 if ((error
= nlookup(nd
)) != 0)
3791 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3793 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3795 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3800 if (vp
->v_type
== VDIR
) {
3804 if (vfs_quota_enabled
) {
3805 error
= VOP_GETATTR(vp
, &vattr
);
3806 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3809 old_size
= vattr
.va_size
;
3812 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3814 vattr
.va_size
= length
;
3815 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3816 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3824 * truncate(char *path, int pad, off_t length)
3826 * Truncate a file given its path name.
3829 sys_truncate(struct truncate_args
*uap
)
3831 struct nlookupdata nd
;
3834 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3836 error
= kern_truncate(&nd
, uap
->length
);
3842 kern_ftruncate(int fd
, off_t length
)
3844 struct thread
*td
= curthread
;
3845 struct proc
*p
= td
->td_proc
;
3852 uint64_t old_size
= 0;
3857 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3859 if (fp
->f_nchandle
.ncp
) {
3860 error
= ncp_writechk(&fp
->f_nchandle
);
3864 if ((fp
->f_flag
& FWRITE
) == 0) {
3868 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3872 vp
= (struct vnode
*)fp
->f_data
;
3873 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3874 if (vp
->v_type
== VDIR
) {
3880 if (vfs_quota_enabled
) {
3881 error
= VOP_GETATTR(vp
, &vattr
);
3882 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3885 old_size
= vattr
.va_size
;
3888 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3890 vattr
.va_size
= length
;
3891 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3893 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3902 * ftruncate_args(int fd, int pad, off_t length)
3904 * Truncate a file given a file descriptor.
3907 sys_ftruncate(struct ftruncate_args
*uap
)
3911 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3919 * Sync an open file.
3922 sys_fsync(struct fsync_args
*uap
)
3924 struct thread
*td
= curthread
;
3925 struct proc
*p
= td
->td_proc
;
3931 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3933 vp
= (struct vnode
*)fp
->f_data
;
3934 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3935 if ((obj
= vp
->v_object
) != NULL
) {
3936 if (vp
->v_mount
== NULL
||
3937 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3938 vm_object_page_clean(obj
, 0, 0, 0);
3941 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3942 if (error
== 0 && vp
->v_mount
)
3943 error
= buf_fsync(vp
);
3951 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3953 struct nchandle fnchd
;
3954 struct nchandle tnchd
;
3955 struct namecache
*ncp
;
3964 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3965 if ((error
= nlookup(fromnd
)) != 0)
3967 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3969 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3973 * unlock the source nch so we can lookup the target nch without
3974 * deadlocking. The target may or may not exist so we do not check
3975 * for a target vp like kern_mkdir() and other creation functions do.
3977 * The source and target directories are ref'd and rechecked after
3978 * everything is relocked to determine if the source or target file
3981 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3982 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3984 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3986 cache_unlock(&fromnd
->nl_nch
);
3988 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3989 if ((error
= nlookup(tond
)) != 0) {
3993 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3995 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3999 tnchd
.mount
= tond
->nl_nch
.mount
;
4003 * If the source and target are the same there is nothing to do
4005 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
4012 * Mount points cannot be renamed or overwritten
4014 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
4023 * Relock the source ncp. cache_relock() will deal with any
4024 * deadlocks against the already-locked tond and will also
4025 * make sure both are resolved.
4027 * NOTE AFTER RELOCKING: The source or target ncp may have become
4028 * invalid while they were unlocked, nc_vp and nc_mount could
4031 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
4032 &tond
->nl_nch
, tond
->nl_cred
);
4033 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
4036 * If the namecache generation changed for either fromnd or tond,
4039 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
4040 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
4041 kprintf("kern_rename: retry due to gen on: "
4042 "\"%s\" -> \"%s\"\n",
4043 fromnd
->nl_nch
.ncp
->nc_name
,
4044 tond
->nl_nch
.ncp
->nc_name
);
4051 * If either fromnd or tond are marked destroyed a ripout occured
4052 * out from under us and we must retry.
4054 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
4055 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
4056 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
4057 kprintf("kern_rename: retry due to ripout on: "
4058 "\"%s\" -> \"%s\"\n",
4059 fromnd
->nl_nch
.ncp
->nc_name
,
4060 tond
->nl_nch
.ncp
->nc_name
);
4067 * Make sure the parent directories linkages are the same.
4068 * XXX shouldn't be needed any more w/ generation check above.
4070 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
4071 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
4078 * Both the source and target must be within the same filesystem and
4079 * in the same filesystem as their parent directories within the
4080 * namecache topology.
4082 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4085 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
4086 mp
!= tond
->nl_nch
.mount
) {
4093 * Make sure the mount point is writable
4095 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4102 * If the target exists and either the source or target is a directory,
4103 * then both must be directories.
4105 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4108 if (tond
->nl_nch
.ncp
->nc_vp
) {
4109 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4111 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4112 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4114 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4120 * You cannot rename a source into itself or a subdirectory of itself.
4121 * We check this by travsersing the target directory upwards looking
4122 * for a match against the source.
4127 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4128 if (fromnd
->nl_nch
.ncp
== ncp
) {
4139 * Even though the namespaces are different, they may still represent
4140 * hardlinks to the same file. The filesystem might have a hard time
4141 * with this so we issue a NREMOVE of the source instead of a NRENAME
4142 * when we detect the situation.
4145 fdvp
= fromnd
->nl_dvp
;
4146 tdvp
= tond
->nl_dvp
;
4147 if (fdvp
== NULL
|| tdvp
== NULL
) {
4149 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4150 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4153 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4154 fdvp
, tdvp
, tond
->nl_cred
);
4161 * rename_args(char *from, char *to)
4163 * Rename files. Source and destination must either both be directories,
4164 * or both not be directories. If target is a directory, it must be empty.
4167 sys_rename(struct rename_args
*uap
)
4169 struct nlookupdata fromnd
, tond
;
4173 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4175 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4177 error
= kern_rename(&fromnd
, &tond
);
4178 nlookup_done(&tond
);
4180 nlookup_done(&fromnd
);
4181 } while (error
== EAGAIN
);
4186 * renameat_args(int oldfd, char *old, int newfd, char *new)
4188 * Rename files using paths relative to the directories associated with
4189 * oldfd and newfd. Source and destination must either both be directories,
4190 * or both not be directories. If target is a directory, it must be empty.
4193 sys_renameat(struct renameat_args
*uap
)
4195 struct nlookupdata oldnd
, newnd
;
4196 struct file
*oldfp
, *newfp
;
4200 error
= nlookup_init_at(&oldnd
, &oldfp
,
4201 uap
->oldfd
, uap
->old
,
4204 error
= nlookup_init_at(&newnd
, &newfp
,
4205 uap
->newfd
, uap
->new,
4208 error
= kern_rename(&oldnd
, &newnd
);
4209 nlookup_done_at(&newnd
, newfp
);
4211 nlookup_done_at(&oldnd
, oldfp
);
4212 } while (error
== EAGAIN
);
4217 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4219 struct thread
*td
= curthread
;
4220 struct proc
*p
= td
->td_proc
;
4226 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4227 if ((error
= nlookup(nd
)) != 0)
4230 if (nd
->nl_nch
.ncp
->nc_vp
)
4232 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4235 vattr
.va_type
= VDIR
;
4236 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4239 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4246 * mkdir_args(char *path, int mode)
4248 * Make a directory file.
4251 sys_mkdir(struct mkdir_args
*uap
)
4253 struct nlookupdata nd
;
4256 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4258 error
= kern_mkdir(&nd
, uap
->mode
);
4264 * mkdirat_args(int fd, char *path, mode_t mode)
4266 * Make a directory file. The path is relative to the directory associated
4270 sys_mkdirat(struct mkdirat_args
*uap
)
4272 struct nlookupdata nd
;
4276 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4278 error
= kern_mkdir(&nd
, uap
->mode
);
4279 nlookup_done_at(&nd
, fp
);
4284 kern_rmdir(struct nlookupdata
*nd
)
4289 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4290 if ((error
= nlookup(nd
)) != 0)
4294 * Do not allow directories representing mount points to be
4295 * deleted, even if empty. Check write perms on mount point
4296 * in case the vnode is aliased (aka nullfs).
4298 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4300 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4302 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4307 * rmdir_args(char *path)
4309 * Remove a directory file.
4312 sys_rmdir(struct rmdir_args
*uap
)
4314 struct nlookupdata nd
;
4317 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4319 error
= kern_rmdir(&nd
);
4325 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4326 enum uio_seg direction
)
4328 struct thread
*td
= curthread
;
4329 struct proc
*p
= td
->td_proc
;
4337 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4339 if ((fp
->f_flag
& FREAD
) == 0) {
4343 vp
= (struct vnode
*)fp
->f_data
;
4344 if (vp
->v_type
!= VDIR
) {
4348 aiov
.iov_base
= buf
;
4349 aiov
.iov_len
= count
;
4350 auio
.uio_iov
= &aiov
;
4351 auio
.uio_iovcnt
= 1;
4352 auio
.uio_rw
= UIO_READ
;
4353 auio
.uio_segflg
= direction
;
4355 auio
.uio_resid
= count
;
4356 loff
= auio
.uio_offset
= fp
->f_offset
;
4357 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4358 fp
->f_offset
= auio
.uio_offset
;
4363 * WARNING! *basep may not be wide enough to accomodate the
4364 * seek offset. XXX should we hack this to return the upper 32 bits
4365 * for offsets greater then 4G?
4368 *basep
= (long)loff
;
4370 *res
= count
- auio
.uio_resid
;
4377 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4379 * Read a block of directory entries in a file system independent format.
4382 sys_getdirentries(struct getdirentries_args
*uap
)
4387 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4388 &uap
->sysmsg_result
, UIO_USERSPACE
);
4390 if (error
== 0 && uap
->basep
)
4391 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4396 * getdents_args(int fd, char *buf, size_t count)
4399 sys_getdents(struct getdents_args
*uap
)
4403 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4404 &uap
->sysmsg_result
, UIO_USERSPACE
);
4410 * Set the mode mask for creation of filesystem nodes.
4412 * umask(int newmask)
4415 sys_umask(struct umask_args
*uap
)
4417 struct thread
*td
= curthread
;
4418 struct proc
*p
= td
->td_proc
;
4419 struct filedesc
*fdp
;
4422 uap
->sysmsg_result
= fdp
->fd_cmask
;
4423 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4428 * revoke(char *path)
4430 * Void all references to file by ripping underlying filesystem
4434 sys_revoke(struct revoke_args
*uap
)
4436 struct nlookupdata nd
;
4443 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4445 error
= nlookup(&nd
);
4447 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4448 cred
= crhold(nd
.nl_cred
);
4452 error
= VOP_GETATTR(vp
, &vattr
);
4453 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4454 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4455 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4457 error
= vrevoke(vp
, cred
);
4458 } else if (error
== 0) {
4459 error
= vrevoke(vp
, cred
);
4469 * getfh_args(char *fname, fhandle_t *fhp)
4471 * Get (NFS) file handle
4473 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4474 * mount. This allows nullfs mounts to be explicitly exported.
4476 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4478 * nullfs mounts of subdirectories are not safe. That is, it will
4479 * work, but you do not really have protection against access to
4480 * the related parent directories.
4483 sys_getfh(struct getfh_args
*uap
)
4485 struct thread
*td
= curthread
;
4486 struct nlookupdata nd
;
4493 * Must be super user
4495 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4499 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4501 error
= nlookup(&nd
);
4503 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4504 mp
= nd
.nl_nch
.mount
;
4507 bzero(&fh
, sizeof(fh
));
4508 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4509 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4512 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4518 * fhopen_args(const struct fhandle *u_fhp, int flags)
4520 * syscall for the rpc.lockd to use to translate a NFS file handle into
4521 * an open descriptor.
4523 * warning: do not remove the priv_check() call or this becomes one giant
4527 sys_fhopen(struct fhopen_args
*uap
)
4529 struct thread
*td
= curthread
;
4530 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4535 struct vattr
*vap
= &vat
;
4537 int fmode
, mode
, error
= 0, type
;
4543 * Must be super user
4545 error
= priv_check(td
, PRIV_ROOT
);
4549 fmode
= FFLAGS(uap
->flags
);
4552 * Why not allow a non-read/write open for our lockd?
4554 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4556 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4561 * Find the mount point
4563 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4568 /* now give me my vnode, it gets returned to me locked */
4569 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4573 * from now on we have to make sure not
4574 * to forget about the vnode
4575 * any error that causes an abort must vput(vp)
4576 * just set error = err and 'goto bad;'.
4582 if (vp
->v_type
== VLNK
) {
4586 if (vp
->v_type
== VSOCK
) {
4591 if (fmode
& (FWRITE
| O_TRUNC
)) {
4592 if (vp
->v_type
== VDIR
) {
4596 error
= vn_writechk(vp
, NULL
);
4604 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4608 if (fmode
& O_TRUNC
) {
4609 vn_unlock(vp
); /* XXX */
4610 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4613 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4619 * VOP_OPEN needs the file pointer so it can potentially override
4622 * WARNING! no f_nchandle will be associated when fhopen()ing a
4625 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4629 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4632 * setting f_ops this way prevents VOP_CLOSE from being
4633 * called or fdrop() releasing the vp from v_data. Since
4634 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4636 fp
->f_ops
= &badfileops
;
4642 * The fp is given its own reference, we still have our ref and lock.
4644 * Assert that all regular files must be created with a VM object.
4646 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4647 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4652 * The open was successful. Handle any locking requirements.
4654 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4655 lf
.l_whence
= SEEK_SET
;
4658 if (fmode
& O_EXLOCK
)
4659 lf
.l_type
= F_WRLCK
;
4661 lf
.l_type
= F_RDLCK
;
4662 if (fmode
& FNONBLOCK
)
4667 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4669 * release our private reference.
4671 fsetfd(fdp
, NULL
, indx
);
4676 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4677 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4681 * Clean up. Associate the file pointer with the previously
4682 * reserved descriptor and return it.
4685 if (uap
->flags
& O_CLOEXEC
)
4686 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4687 fsetfd(fdp
, fp
, indx
);
4689 uap
->sysmsg_result
= indx
;
4693 fsetfd(fdp
, NULL
, indx
);
4702 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4705 sys_fhstat(struct fhstat_args
*uap
)
4707 struct thread
*td
= curthread
;
4715 * Must be super user
4717 error
= priv_check(td
, PRIV_ROOT
);
4721 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4725 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4728 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4729 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4734 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4739 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4742 sys_fhstatfs(struct fhstatfs_args
*uap
)
4744 struct thread
*td
= curthread
;
4745 struct proc
*p
= td
->td_proc
;
4750 char *fullpath
, *freepath
;
4755 * Must be super user
4757 if ((error
= priv_check(td
, PRIV_ROOT
)))
4760 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4763 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4767 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4772 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4777 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4780 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4783 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4784 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4785 kfree(freepath
, M_TEMP
);
4787 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4788 if (priv_check(td
, PRIV_ROOT
)) {
4789 bcopy(sp
, &sb
, sizeof(sb
));
4790 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4793 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4799 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4802 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4804 struct thread
*td
= curthread
;
4805 struct proc
*p
= td
->td_proc
;
4813 * Must be super user
4815 if ((error
= priv_check(td
, PRIV_ROOT
)))
4818 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4821 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4825 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4830 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4833 sp
= &mp
->mnt_vstat
;
4835 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4839 if (mp
->mnt_flag
& MNT_RDONLY
)
4840 sp
->f_flag
|= ST_RDONLY
;
4841 if (mp
->mnt_flag
& MNT_NOSUID
)
4842 sp
->f_flag
|= ST_NOSUID
;
4843 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4850 * Syscall to push extended attribute configuration information into the
4851 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4852 * a command (int cmd), and attribute name and misc data. For now, the
4853 * attribute name is left in userspace for consumption by the VFS_op.
4854 * It will probably be changed to be copied into sysspace by the
4855 * syscall in the future, once issues with various consumers of the
4856 * attribute code have raised their hands.
4858 * Currently this is used only by UFS Extended Attributes.
4861 sys_extattrctl(struct extattrctl_args
*uap
)
4863 struct nlookupdata nd
;
4865 char attrname
[EXTATTR_MAXNAMELEN
];
4873 if (error
== 0 && uap
->filename
) {
4874 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4877 error
= nlookup(&nd
);
4879 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4883 if (error
== 0 && uap
->attrname
) {
4884 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4889 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4891 error
= nlookup(&nd
);
4893 error
= ncp_writechk(&nd
.nl_nch
);
4895 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4897 uap
->attrname
, nd
.nl_cred
);
4906 * Syscall to get a named extended attribute on a file or directory.
4909 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4911 char attrname
[EXTATTR_MAXNAMELEN
];
4912 struct nlookupdata nd
;
4918 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4924 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4926 error
= nlookup(&nd
);
4928 error
= ncp_writechk(&nd
.nl_nch
);
4930 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4936 bzero(&auio
, sizeof(auio
));
4937 aiov
.iov_base
= uap
->data
;
4938 aiov
.iov_len
= uap
->nbytes
;
4939 auio
.uio_iov
= &aiov
;
4940 auio
.uio_iovcnt
= 1;
4941 auio
.uio_offset
= 0;
4942 auio
.uio_resid
= uap
->nbytes
;
4943 auio
.uio_rw
= UIO_WRITE
;
4944 auio
.uio_td
= curthread
;
4946 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4955 * Syscall to get a named extended attribute on a file or directory.
4958 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4960 char attrname
[EXTATTR_MAXNAMELEN
];
4961 struct nlookupdata nd
;
4967 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4973 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4975 error
= nlookup(&nd
);
4977 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
4983 bzero(&auio
, sizeof(auio
));
4984 aiov
.iov_base
= uap
->data
;
4985 aiov
.iov_len
= uap
->nbytes
;
4986 auio
.uio_iov
= &aiov
;
4987 auio
.uio_iovcnt
= 1;
4988 auio
.uio_offset
= 0;
4989 auio
.uio_resid
= uap
->nbytes
;
4990 auio
.uio_rw
= UIO_READ
;
4991 auio
.uio_td
= curthread
;
4993 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4995 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
5003 * Syscall to delete a named extended attribute from a file or directory.
5004 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5007 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
5009 char attrname
[EXTATTR_MAXNAMELEN
];
5010 struct nlookupdata nd
;
5014 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
5018 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
5020 error
= nlookup(&nd
);
5022 error
= ncp_writechk(&nd
.nl_nch
);
5024 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
5026 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
5027 attrname
, NULL
, nd
.nl_cred
);
5036 * Determine if the mount is visible to the process.
5039 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
5041 struct nchandle nch
;
5044 * Traverse from the mount point upwards. If we hit the process
5045 * root then the mount point is visible to the process.
5047 nch
= mp
->mnt_ncmountpt
;
5049 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
5050 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
5053 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
5054 nch
= nch
.mount
->mnt_ncmounton
;
5056 nch
.ncp
= nch
.ncp
->nc_parent
;
5061 * If the mount point is not visible to the process, but the
5062 * process root is in a subdirectory of the mount, return
5065 if (p
->p_fd
->fd_nrdir
.mount
== mp
)