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
);
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
);
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 vrele(fdp
->fd_rdir
);
1840 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1841 cache_drop(&fdp
->fd_nrdir
);
1842 cache_copy(nch
, &fdp
->fd_nrdir
);
1843 if (fdp
->fd_jdir
== NULL
) {
1846 cache_copy(nch
, &fdp
->fd_njdir
);
1855 * chroot_args(char *path)
1857 * Change notion of root (``/'') directory.
1860 sys_chroot(struct chroot_args
*uap
)
1862 struct thread
*td __debugvar
= curthread
;
1863 struct nlookupdata nd
;
1866 KKASSERT(td
->td_proc
);
1867 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1869 nd
.nl_flags
|= NLC_EXEC
;
1870 error
= nlookup(&nd
);
1872 error
= kern_chroot(&nd
.nl_nch
);
1879 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1881 struct thread
*td
= curthread
;
1882 struct nlookupdata nd
;
1883 struct nchandle
*nch
;
1887 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1891 error
= nlookup(&nd
);
1897 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1901 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1906 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1909 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1910 vfs_cache_setroot(vp
, cache_hold(nch
));
1919 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1920 * determine whether it is legal to chdir to the vnode. The vnode's state
1921 * is not changed by this call.
1924 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1928 if (vp
->v_type
!= VDIR
)
1931 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1936 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1938 struct thread
*td
= curthread
;
1939 struct proc
*p
= td
->td_proc
;
1940 struct lwp
*lp
= td
->td_lwp
;
1941 struct filedesc
*fdp
= p
->p_fd
;
1946 int type
, indx
, error
= 0;
1949 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1951 flags
= FFLAGS(oflags
);
1952 error
= falloc(lp
, &nfp
, NULL
);
1956 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1959 * XXX p_dupfd is a real mess. It allows a device to return a
1960 * file descriptor to be duplicated rather then doing the open
1966 * Call vn_open() to do the lookup and assign the vnode to the
1967 * file pointer. vn_open() does not change the ref count on fp
1968 * and the vnode, on success, will be inherited by the file pointer
1971 * Request a shared lock on the vnode if possible.
1973 * Executable binaries can race VTEXT against O_RDWR opens, so
1974 * use an exclusive lock for O_RDWR opens as well.
1976 * NOTE: We need a flag to separate terminal vnode locking from
1977 * parent locking. O_CREAT needs parent locking, but O_TRUNC
1978 * and O_RDWR only need to lock the terminal vnode exclusively.
1980 nd
->nl_flags
|= NLC_LOCKVP
;
1981 if ((flags
& (O_CREAT
|O_TRUNC
|O_RDWR
)) == 0)
1982 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1984 error
= vn_open(nd
, fp
, flags
, cmode
);
1989 * handle special fdopen() case. bleh. dupfdopen() is
1990 * responsible for dropping the old contents of ofiles[indx]
1993 * Note that fsetfd() will add a ref to fp which represents
1994 * the fd_files[] assignment. We must still drop our
1997 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1998 if (fdalloc(p
, 0, &indx
) == 0) {
1999 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
2002 fdrop(fp
); /* our ref */
2005 fsetfd(fdp
, NULL
, indx
);
2008 fdrop(fp
); /* our ref */
2009 if (error
== ERESTART
)
2015 * ref the vnode for ourselves so it can't be ripped out from under
2016 * is. XXX need an ND flag to request that the vnode be returned
2019 * Reserve a file descriptor but do not assign it until the open
2022 vp
= (struct vnode
*)fp
->f_data
;
2024 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
2031 * If no error occurs the vp will have been assigned to the file
2036 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
2037 lf
.l_whence
= SEEK_SET
;
2040 if (flags
& O_EXLOCK
)
2041 lf
.l_type
= F_WRLCK
;
2043 lf
.l_type
= F_RDLCK
;
2044 if (flags
& FNONBLOCK
)
2049 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
2051 * lock request failed. Clean up the reserved
2055 fsetfd(fdp
, NULL
, indx
);
2059 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
2063 * Assert that all regular file vnodes were created with a object.
2065 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
2066 ("open: regular file has no backing object after vn_open"));
2072 * release our private reference, leaving the one associated with the
2073 * descriptor table intact.
2075 if (oflags
& O_CLOEXEC
)
2076 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
2077 fsetfd(fdp
, fp
, indx
);
2084 * open_args(char *path, int flags, int mode)
2086 * Check permissions, allocate an open file structure,
2087 * and call the device open routine if any.
2090 sys_open(struct open_args
*uap
)
2092 struct nlookupdata nd
;
2095 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2097 error
= kern_open(&nd
, uap
->flags
,
2098 uap
->mode
, &uap
->sysmsg_result
);
2105 * openat_args(int fd, char *path, int flags, int mode)
2108 sys_openat(struct openat_args
*uap
)
2110 struct nlookupdata nd
;
2114 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2116 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2117 &uap
->sysmsg_result
);
2119 nlookup_done_at(&nd
, fp
);
2124 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2126 struct thread
*td
= curthread
;
2127 struct proc
*p
= td
->td_proc
;
2136 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2137 vattr
.va_rmajor
= rmajor
;
2138 vattr
.va_rminor
= rminor
;
2140 switch (mode
& S_IFMT
) {
2141 case S_IFMT
: /* used by badsect to flag bad sectors */
2142 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2143 vattr
.va_type
= VBAD
;
2146 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2147 vattr
.va_type
= VCHR
;
2150 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2151 vattr
.va_type
= VBLK
;
2154 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2157 case S_IFDIR
: /* special directories support for HAMMER */
2158 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2159 vattr
.va_type
= VDIR
;
2170 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2171 if ((error
= nlookup(nd
)) != 0)
2173 if (nd
->nl_nch
.ncp
->nc_vp
)
2175 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2179 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2180 nd
->nl_cred
, NAMEI_CREATE
);
2183 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2184 &vp
, nd
->nl_cred
, &vattr
);
2192 * mknod_args(char *path, int mode, int dev)
2194 * Create a special file.
2197 sys_mknod(struct mknod_args
*uap
)
2199 struct nlookupdata nd
;
2202 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2204 error
= kern_mknod(&nd
, uap
->mode
,
2205 umajor(uap
->dev
), uminor(uap
->dev
));
2212 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2214 * Create a special file. The path is relative to the directory associated
2218 sys_mknodat(struct mknodat_args
*uap
)
2220 struct nlookupdata nd
;
2224 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2226 error
= kern_mknod(&nd
, uap
->mode
,
2227 umajor(uap
->dev
), uminor(uap
->dev
));
2229 nlookup_done_at(&nd
, fp
);
2234 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2236 struct thread
*td
= curthread
;
2237 struct proc
*p
= td
->td_proc
;
2244 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2245 if ((error
= nlookup(nd
)) != 0)
2247 if (nd
->nl_nch
.ncp
->nc_vp
)
2249 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2253 vattr
.va_type
= VFIFO
;
2254 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2256 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2263 * mkfifo_args(char *path, int mode)
2265 * Create a named pipe.
2268 sys_mkfifo(struct mkfifo_args
*uap
)
2270 struct nlookupdata nd
;
2273 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2275 error
= kern_mkfifo(&nd
, uap
->mode
);
2281 * mkfifoat_args(int fd, char *path, mode_t mode)
2283 * Create a named pipe. The path is relative to the directory associated
2287 sys_mkfifoat(struct mkfifoat_args
*uap
)
2289 struct nlookupdata nd
;
2293 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2295 error
= kern_mkfifo(&nd
, uap
->mode
);
2296 nlookup_done_at(&nd
, fp
);
2300 static int hardlink_check_uid
= 0;
2301 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2302 &hardlink_check_uid
, 0,
2303 "Unprivileged processes cannot create hard links to files owned by other "
2305 static int hardlink_check_gid
= 0;
2306 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2307 &hardlink_check_gid
, 0,
2308 "Unprivileged processes cannot create hard links to files owned by other "
2312 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2318 * Shortcut if disabled
2320 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2324 * Privileged user can always hardlink
2326 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2330 * Otherwise only if the originating file is owned by the
2331 * same user or group. Note that any group is allowed if
2332 * the file is owned by the caller.
2334 error
= VOP_GETATTR(vp
, &va
);
2338 if (hardlink_check_uid
) {
2339 if (cred
->cr_uid
!= va
.va_uid
)
2343 if (hardlink_check_gid
) {
2344 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2352 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2354 struct thread
*td
= curthread
;
2359 * Lookup the source and obtained a locked vnode.
2361 * You may only hardlink a file which you have write permission
2362 * on or which you own.
2364 * XXX relookup on vget failure / race ?
2367 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2368 if ((error
= nlookup(nd
)) != 0)
2370 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2371 KKASSERT(vp
!= NULL
);
2372 if (vp
->v_type
== VDIR
)
2373 return (EPERM
); /* POSIX */
2374 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2376 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2380 * Unlock the source so we can lookup the target without deadlocking
2381 * (XXX vp is locked already, possible other deadlock?). The target
2384 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2385 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2386 cache_unlock(&nd
->nl_nch
);
2389 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2390 if ((error
= nlookup(linknd
)) != 0) {
2394 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2398 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2405 * Finally run the new API VOP.
2407 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2409 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2410 vp
, linknd
->nl_cred
);
2417 * link_args(char *path, char *link)
2419 * Make a hard file link.
2422 sys_link(struct link_args
*uap
)
2424 struct nlookupdata nd
, linknd
;
2427 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2429 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2431 error
= kern_link(&nd
, &linknd
);
2432 nlookup_done(&linknd
);
2439 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2441 * Make a hard file link. The path1 argument is relative to the directory
2442 * associated with fd1, and similarly the path2 argument is relative to
2443 * the directory associated with fd2.
2446 sys_linkat(struct linkat_args
*uap
)
2448 struct nlookupdata nd
, linknd
;
2449 struct file
*fp1
, *fp2
;
2452 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2453 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2455 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2456 uap
->path2
, UIO_USERSPACE
, 0);
2458 error
= kern_link(&nd
, &linknd
);
2459 nlookup_done_at(&linknd
, fp2
);
2461 nlookup_done_at(&nd
, fp1
);
2466 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2474 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2475 if ((error
= nlookup(nd
)) != 0)
2477 if (nd
->nl_nch
.ncp
->nc_vp
)
2479 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2483 vattr
.va_mode
= mode
;
2484 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2491 * symlink(char *path, char *link)
2493 * Make a symbolic link.
2496 sys_symlink(struct symlink_args
*uap
)
2498 struct thread
*td
= curthread
;
2499 struct nlookupdata nd
;
2504 path
= objcache_get(namei_oc
, M_WAITOK
);
2505 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2507 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2509 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2510 error
= kern_symlink(&nd
, path
, mode
);
2514 objcache_put(namei_oc
, path
);
2519 * symlinkat_args(char *path1, int fd, char *path2)
2521 * Make a symbolic link. The path2 argument is relative to the directory
2522 * associated with fd.
2525 sys_symlinkat(struct symlinkat_args
*uap
)
2527 struct thread
*td
= curthread
;
2528 struct nlookupdata nd
;
2534 path1
= objcache_get(namei_oc
, M_WAITOK
);
2535 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2537 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2540 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2541 error
= kern_symlink(&nd
, path1
, mode
);
2543 nlookup_done_at(&nd
, fp
);
2545 objcache_put(namei_oc
, path1
);
2550 * undelete_args(char *path)
2552 * Delete a whiteout from the filesystem.
2555 sys_undelete(struct undelete_args
*uap
)
2557 struct nlookupdata nd
;
2560 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2562 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2564 error
= nlookup(&nd
);
2566 error
= ncp_writechk(&nd
.nl_nch
);
2568 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2576 kern_unlink(struct nlookupdata
*nd
)
2581 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2582 if ((error
= nlookup(nd
)) != 0)
2584 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2586 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2591 * unlink_args(char *path)
2593 * Delete a name from the filesystem.
2596 sys_unlink(struct unlink_args
*uap
)
2598 struct nlookupdata nd
;
2601 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2603 error
= kern_unlink(&nd
);
2610 * unlinkat_args(int fd, char *path, int flags)
2612 * Delete the file or directory entry pointed to by fd/path.
2615 sys_unlinkat(struct unlinkat_args
*uap
)
2617 struct nlookupdata nd
;
2621 if (uap
->flags
& ~AT_REMOVEDIR
)
2624 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2626 if (uap
->flags
& AT_REMOVEDIR
)
2627 error
= kern_rmdir(&nd
);
2629 error
= kern_unlink(&nd
);
2631 nlookup_done_at(&nd
, fp
);
2636 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2638 struct thread
*td
= curthread
;
2639 struct proc
*p
= td
->td_proc
;
2646 fp
= holdfp(p
->p_fd
, fd
, -1);
2649 if (fp
->f_type
!= DTYPE_VNODE
) {
2653 vp
= (struct vnode
*)fp
->f_data
;
2657 spin_lock(&fp
->f_spin
);
2658 new_offset
= fp
->f_offset
+ offset
;
2662 error
= VOP_GETATTR(vp
, &vattr
);
2663 spin_lock(&fp
->f_spin
);
2664 new_offset
= offset
+ vattr
.va_size
;
2667 new_offset
= offset
;
2669 spin_lock(&fp
->f_spin
);
2674 spin_lock(&fp
->f_spin
);
2679 * Validate the seek position. Negative offsets are not allowed
2680 * for regular files or directories.
2682 * Normally we would also not want to allow negative offsets for
2683 * character and block-special devices. However kvm addresses
2684 * on 64 bit architectures might appear to be negative and must
2688 if (new_offset
< 0 &&
2689 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2692 fp
->f_offset
= new_offset
;
2695 *res
= fp
->f_offset
;
2696 spin_unlock(&fp
->f_spin
);
2703 * lseek_args(int fd, int pad, off_t offset, int whence)
2705 * Reposition read/write file offset.
2708 sys_lseek(struct lseek_args
*uap
)
2712 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2713 &uap
->sysmsg_offset
);
2719 * Check if current process can access given file. amode is a bitmask of *_OK
2720 * access bits. flags is a bitmask of AT_* flags.
2723 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2728 if (flags
& ~AT_EACCESS
)
2730 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2731 if ((error
= nlookup(nd
)) != 0)
2734 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2738 /* Flags == 0 means only check for existence. */
2747 if ((mode
& VWRITE
) == 0 ||
2748 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2749 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2752 * If the file handle is stale we have to re-resolve the
2753 * entry with the ncp held exclusively. This is a hack
2756 if (error
== ESTALE
) {
2758 cache_unlock(&nd
->nl_nch
);
2759 cache_lock(&nd
->nl_nch
);
2760 cache_setunresolved(&nd
->nl_nch
);
2761 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2774 * access_args(char *path, int flags)
2776 * Check access permissions.
2779 sys_access(struct access_args
*uap
)
2781 struct nlookupdata nd
;
2784 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2786 error
= kern_access(&nd
, uap
->flags
, 0);
2793 * eaccess_args(char *path, int flags)
2795 * Check access permissions.
2798 sys_eaccess(struct eaccess_args
*uap
)
2800 struct nlookupdata nd
;
2803 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2805 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2812 * faccessat_args(int fd, char *path, int amode, int flags)
2814 * Check access permissions.
2817 sys_faccessat(struct faccessat_args
*uap
)
2819 struct nlookupdata nd
;
2823 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2826 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2827 nlookup_done_at(&nd
, fp
);
2832 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2837 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2838 if ((error
= nlookup(nd
)) != 0)
2841 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2844 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2846 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2849 * If the file handle is stale we have to re-resolve the
2850 * entry with the ncp held exclusively. This is a hack
2853 if (error
== ESTALE
) {
2855 cache_unlock(&nd
->nl_nch
);
2856 cache_lock(&nd
->nl_nch
);
2857 cache_setunresolved(&nd
->nl_nch
);
2858 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2868 * stat_args(char *path, struct stat *ub)
2870 * Get file status; this version follows links.
2873 sys_stat(struct stat_args
*uap
)
2875 struct nlookupdata nd
;
2879 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2881 error
= kern_stat(&nd
, &st
);
2883 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2890 * lstat_args(char *path, struct stat *ub)
2892 * Get file status; this version does not follow links.
2895 sys_lstat(struct lstat_args
*uap
)
2897 struct nlookupdata nd
;
2901 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2903 error
= kern_stat(&nd
, &st
);
2905 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2912 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2914 * Get status of file pointed to by fd/path.
2917 sys_fstatat(struct fstatat_args
*uap
)
2919 struct nlookupdata nd
;
2925 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2928 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2930 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2931 UIO_USERSPACE
, flags
);
2933 error
= kern_stat(&nd
, &st
);
2935 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2937 nlookup_done_at(&nd
, fp
);
2942 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2944 struct nlookupdata nd
;
2949 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2951 error
= nlookup(&nd
);
2953 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2956 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2963 * pathconf_Args(char *path, int name)
2965 * Get configurable pathname variables.
2968 sys_pathconf(struct pathconf_args
*uap
)
2970 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2975 * lpathconf_Args(char *path, int name)
2977 * Get configurable pathname variables, but don't follow symlinks.
2980 sys_lpathconf(struct lpathconf_args
*uap
)
2982 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2987 * kern_readlink isn't properly split yet. There is a copyin burried
2988 * in VOP_READLINK().
2991 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2993 struct thread
*td
= curthread
;
2999 nd
->nl_flags
|= NLC_SHAREDLOCK
;
3000 if ((error
= nlookup(nd
)) != 0)
3002 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
3005 if (vp
->v_type
!= VLNK
) {
3008 aiov
.iov_base
= buf
;
3009 aiov
.iov_len
= count
;
3010 auio
.uio_iov
= &aiov
;
3011 auio
.uio_iovcnt
= 1;
3012 auio
.uio_offset
= 0;
3013 auio
.uio_rw
= UIO_READ
;
3014 auio
.uio_segflg
= UIO_USERSPACE
;
3016 auio
.uio_resid
= count
;
3017 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
3020 *res
= count
- auio
.uio_resid
;
3025 * readlink_args(char *path, char *buf, int count)
3027 * Return target name of a symbolic link.
3030 sys_readlink(struct readlink_args
*uap
)
3032 struct nlookupdata nd
;
3035 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3037 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
3038 &uap
->sysmsg_result
);
3045 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3047 * Return target name of a symbolic link. The path is relative to the
3048 * directory associated with fd.
3051 sys_readlinkat(struct readlinkat_args
*uap
)
3053 struct nlookupdata nd
;
3057 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
3059 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
3060 &uap
->sysmsg_result
);
3062 nlookup_done_at(&nd
, fp
);
3067 setfflags(struct vnode
*vp
, int flags
)
3069 struct thread
*td
= curthread
;
3074 * Prevent non-root users from setting flags on devices. When
3075 * a device is reused, users can retain ownership of the device
3076 * if they are allowed to set flags and programs assume that
3077 * chown can't fail when done as root.
3079 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
3080 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
3084 * note: vget is required for any operation that might mod the vnode
3085 * so VINACTIVE is properly cleared.
3087 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3089 vattr
.va_flags
= flags
;
3090 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3097 * chflags(char *path, int flags)
3099 * Change flags of a file given a path name.
3102 sys_chflags(struct chflags_args
*uap
)
3104 struct nlookupdata nd
;
3109 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3111 error
= nlookup(&nd
);
3113 error
= ncp_writechk(&nd
.nl_nch
);
3115 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3118 error
= setfflags(vp
, uap
->flags
);
3125 * lchflags(char *path, int flags)
3127 * Change flags of a file given a path name, but don't follow symlinks.
3130 sys_lchflags(struct lchflags_args
*uap
)
3132 struct nlookupdata nd
;
3137 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3139 error
= nlookup(&nd
);
3141 error
= ncp_writechk(&nd
.nl_nch
);
3143 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3146 error
= setfflags(vp
, uap
->flags
);
3153 * fchflags_args(int fd, int flags)
3155 * Change flags of a file given a file descriptor.
3158 sys_fchflags(struct fchflags_args
*uap
)
3160 struct thread
*td
= curthread
;
3161 struct proc
*p
= td
->td_proc
;
3165 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3167 if (fp
->f_nchandle
.ncp
)
3168 error
= ncp_writechk(&fp
->f_nchandle
);
3170 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3176 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3177 * change flags given a pathname relative to a filedescriptor
3179 int sys_chflagsat(struct chflagsat_args
*uap
)
3181 struct nlookupdata nd
;
3187 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3190 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3193 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3195 error
= nlookup(&nd
);
3197 error
= ncp_writechk(&nd
.nl_nch
);
3199 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3200 nlookup_done_at(&nd
, fp
);
3202 error
= setfflags(vp
, uap
->flags
);
3210 setfmode(struct vnode
*vp
, int mode
)
3212 struct thread
*td
= curthread
;
3217 * note: vget is required for any operation that might mod the vnode
3218 * so VINACTIVE is properly cleared.
3220 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3222 vattr
.va_mode
= mode
& ALLPERMS
;
3223 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3224 cache_inval_wxok(vp
);
3231 kern_chmod(struct nlookupdata
*nd
, int mode
)
3236 if ((error
= nlookup(nd
)) != 0)
3238 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3240 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3241 error
= setfmode(vp
, mode
);
3247 * chmod_args(char *path, int mode)
3249 * Change mode of a file given path name.
3252 sys_chmod(struct chmod_args
*uap
)
3254 struct nlookupdata nd
;
3257 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3259 error
= kern_chmod(&nd
, uap
->mode
);
3265 * lchmod_args(char *path, int mode)
3267 * Change mode of a file given path name (don't follow links.)
3270 sys_lchmod(struct lchmod_args
*uap
)
3272 struct nlookupdata nd
;
3275 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3277 error
= kern_chmod(&nd
, uap
->mode
);
3283 * fchmod_args(int fd, int mode)
3285 * Change mode of a file given a file descriptor.
3288 sys_fchmod(struct fchmod_args
*uap
)
3290 struct thread
*td
= curthread
;
3291 struct proc
*p
= td
->td_proc
;
3295 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3297 if (fp
->f_nchandle
.ncp
)
3298 error
= ncp_writechk(&fp
->f_nchandle
);
3300 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3306 * fchmodat_args(char *path, int mode)
3308 * Change mode of a file pointed to by fd/path.
3311 sys_fchmodat(struct fchmodat_args
*uap
)
3313 struct nlookupdata nd
;
3318 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3320 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3322 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3323 UIO_USERSPACE
, flags
);
3325 error
= kern_chmod(&nd
, uap
->mode
);
3326 nlookup_done_at(&nd
, fp
);
3331 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3333 struct thread
*td
= curthread
;
3341 * note: vget is required for any operation that might mod the vnode
3342 * so VINACTIVE is properly cleared.
3344 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3345 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3347 o_uid
= vattr
.va_uid
;
3348 o_gid
= vattr
.va_gid
;
3349 size
= vattr
.va_size
;
3354 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3363 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3364 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3371 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3376 if ((error
= nlookup(nd
)) != 0)
3378 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3380 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3381 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3387 * chown(char *path, int uid, int gid)
3389 * Set ownership given a path name.
3392 sys_chown(struct chown_args
*uap
)
3394 struct nlookupdata nd
;
3397 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3399 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3405 * lchown_args(char *path, int uid, int gid)
3407 * Set ownership given a path name, do not cross symlinks.
3410 sys_lchown(struct lchown_args
*uap
)
3412 struct nlookupdata nd
;
3415 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3417 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3423 * fchown_args(int fd, int uid, int gid)
3425 * Set ownership given a file descriptor.
3428 sys_fchown(struct fchown_args
*uap
)
3430 struct thread
*td
= curthread
;
3431 struct proc
*p
= td
->td_proc
;
3435 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3437 if (fp
->f_nchandle
.ncp
)
3438 error
= ncp_writechk(&fp
->f_nchandle
);
3440 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3441 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3447 * fchownat(int fd, char *path, int uid, int gid, int flags)
3449 * Set ownership of file pointed to by fd/path.
3452 sys_fchownat(struct fchownat_args
*uap
)
3454 struct nlookupdata nd
;
3459 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3461 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3463 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3464 UIO_USERSPACE
, flags
);
3466 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3467 nlookup_done_at(&nd
, fp
);
3473 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3475 struct timeval tv
[2];
3480 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3483 if ((error
= itimerfix(tvp
)) != 0)
3485 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3486 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3492 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3494 struct timespec tsnow
;
3508 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3510 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3513 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3514 newts
[0].tv_sec
= VNOVAL
;
3515 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3517 else if ((error
= itimespecfix(&newts
[0])) != 0)
3520 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3521 newts
[1].tv_sec
= VNOVAL
;
3522 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3524 else if ((error
= itimespecfix(&newts
[1])) != 0)
3531 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3532 const struct timespec
*ts
, int nullflag
)
3534 struct thread
*td
= curthread
;
3538 vattr
->va_atime
= ts
[0];
3539 vattr
->va_mtime
= ts
[1];
3541 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3542 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3548 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3550 struct timespec ts
[2];
3554 if ((error
= getutimes(tptr
, ts
)) != 0)
3557 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3562 * utimes_args(char *path, struct timeval *tptr)
3564 * Set the access and modification times of a file.
3567 sys_utimes(struct utimes_args
*uap
)
3569 struct timeval tv
[2];
3570 struct nlookupdata nd
;
3574 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3578 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3580 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3586 * lutimes_args(char *path, struct timeval *tptr)
3588 * Set the access and modification times of a file.
3591 sys_lutimes(struct lutimes_args
*uap
)
3593 struct timeval tv
[2];
3594 struct nlookupdata nd
;
3598 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3602 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3604 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3610 * Set utimes on a file descriptor. The creds used to open the
3611 * file are used to determine whether the operation is allowed
3615 kern_futimens(int fd
, struct timespec
*ts
)
3617 struct thread
*td
= curthread
;
3618 struct proc
*p
= td
->td_proc
;
3619 struct timespec newts
[2];
3626 error
= getutimens(ts
, newts
, &nullflag
);
3629 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3631 if (fp
->f_nchandle
.ncp
)
3632 error
= ncp_writechk(&fp
->f_nchandle
);
3635 error
= vget(vp
, LK_EXCLUSIVE
);
3637 error
= VOP_GETATTR(vp
, &vattr
);
3639 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3643 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3653 * futimens_args(int fd, struct timespec *ts)
3655 * Set the access and modification times of a file.
3658 sys_futimens(struct futimens_args
*uap
)
3660 struct timespec ts
[2];
3664 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3668 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3673 kern_futimes(int fd
, struct timeval
*tptr
)
3675 struct timespec ts
[2];
3679 if ((error
= getutimes(tptr
, ts
)) != 0)
3682 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3687 * futimes_args(int fd, struct timeval *tptr)
3689 * Set the access and modification times of a file.
3692 sys_futimes(struct futimes_args
*uap
)
3694 struct timeval tv
[2];
3698 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3702 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3707 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3709 struct timespec newts
[2];
3715 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3718 error
= getutimens(ts
, newts
, &nullflag
);
3722 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3723 if ((error
= nlookup(nd
)) != 0)
3725 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3727 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3729 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3730 error
= vget(vp
, LK_EXCLUSIVE
);
3732 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3741 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3743 * Set file access and modification times of a file.
3746 sys_utimensat(struct utimensat_args
*uap
)
3748 struct timespec ts
[2];
3749 struct nlookupdata nd
;
3755 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3760 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3761 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3762 UIO_USERSPACE
, flags
);
3764 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3765 nlookup_done_at(&nd
, fp
);
3770 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3777 uint64_t old_size
= 0;
3781 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3782 if ((error
= nlookup(nd
)) != 0)
3784 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3786 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3788 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3793 if (vp
->v_type
== VDIR
) {
3797 if (vfs_quota_enabled
) {
3798 error
= VOP_GETATTR(vp
, &vattr
);
3799 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3802 old_size
= vattr
.va_size
;
3805 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3807 vattr
.va_size
= length
;
3808 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3809 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3817 * truncate(char *path, int pad, off_t length)
3819 * Truncate a file given its path name.
3822 sys_truncate(struct truncate_args
*uap
)
3824 struct nlookupdata nd
;
3827 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3829 error
= kern_truncate(&nd
, uap
->length
);
3835 kern_ftruncate(int fd
, off_t length
)
3837 struct thread
*td
= curthread
;
3838 struct proc
*p
= td
->td_proc
;
3845 uint64_t old_size
= 0;
3850 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3852 if (fp
->f_nchandle
.ncp
) {
3853 error
= ncp_writechk(&fp
->f_nchandle
);
3857 if ((fp
->f_flag
& FWRITE
) == 0) {
3861 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3865 vp
= (struct vnode
*)fp
->f_data
;
3866 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3867 if (vp
->v_type
== VDIR
) {
3873 if (vfs_quota_enabled
) {
3874 error
= VOP_GETATTR(vp
, &vattr
);
3875 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3878 old_size
= vattr
.va_size
;
3881 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3883 vattr
.va_size
= length
;
3884 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3886 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3895 * ftruncate_args(int fd, int pad, off_t length)
3897 * Truncate a file given a file descriptor.
3900 sys_ftruncate(struct ftruncate_args
*uap
)
3904 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3912 * Sync an open file.
3915 sys_fsync(struct fsync_args
*uap
)
3917 struct thread
*td
= curthread
;
3918 struct proc
*p
= td
->td_proc
;
3924 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3926 vp
= (struct vnode
*)fp
->f_data
;
3927 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3928 if ((obj
= vp
->v_object
) != NULL
) {
3929 if (vp
->v_mount
== NULL
||
3930 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3931 vm_object_page_clean(obj
, 0, 0, 0);
3934 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3935 if (error
== 0 && vp
->v_mount
)
3936 error
= buf_fsync(vp
);
3944 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3946 struct nchandle fnchd
;
3947 struct nchandle tnchd
;
3948 struct namecache
*ncp
;
3957 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3958 if ((error
= nlookup(fromnd
)) != 0)
3960 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3962 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3966 * unlock the source nch so we can lookup the target nch without
3967 * deadlocking. The target may or may not exist so we do not check
3968 * for a target vp like kern_mkdir() and other creation functions do.
3970 * The source and target directories are ref'd and rechecked after
3971 * everything is relocked to determine if the source or target file
3974 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3975 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3977 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3979 cache_unlock(&fromnd
->nl_nch
);
3981 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3982 if ((error
= nlookup(tond
)) != 0) {
3986 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3988 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3992 tnchd
.mount
= tond
->nl_nch
.mount
;
3996 * If the source and target are the same there is nothing to do
3998 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
4005 * Mount points cannot be renamed or overwritten
4007 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
4016 * Relock the source ncp. cache_relock() will deal with any
4017 * deadlocks against the already-locked tond and will also
4018 * make sure both are resolved.
4020 * NOTE AFTER RELOCKING: The source or target ncp may have become
4021 * invalid while they were unlocked, nc_vp and nc_mount could
4024 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
4025 &tond
->nl_nch
, tond
->nl_cred
);
4026 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
4029 * If the namecache generation changed for either fromnd or tond,
4032 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
4033 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
4034 kprintf("kern_rename: retry due to gen on: "
4035 "\"%s\" -> \"%s\"\n",
4036 fromnd
->nl_nch
.ncp
->nc_name
,
4037 tond
->nl_nch
.ncp
->nc_name
);
4044 * If either fromnd or tond are marked destroyed a ripout occured
4045 * out from under us and we must retry.
4047 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
4048 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
4049 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
4050 kprintf("kern_rename: retry due to ripout on: "
4051 "\"%s\" -> \"%s\"\n",
4052 fromnd
->nl_nch
.ncp
->nc_name
,
4053 tond
->nl_nch
.ncp
->nc_name
);
4060 * Make sure the parent directories linkages are the same.
4061 * XXX shouldn't be needed any more w/ generation check above.
4063 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
4064 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
4071 * Both the source and target must be within the same filesystem and
4072 * in the same filesystem as their parent directories within the
4073 * namecache topology.
4075 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4078 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
4079 mp
!= tond
->nl_nch
.mount
) {
4086 * Make sure the mount point is writable
4088 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4095 * If the target exists and either the source or target is a directory,
4096 * then both must be directories.
4098 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4101 if (tond
->nl_nch
.ncp
->nc_vp
) {
4102 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4104 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4105 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4107 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4113 * You cannot rename a source into itself or a subdirectory of itself.
4114 * We check this by travsersing the target directory upwards looking
4115 * for a match against the source.
4120 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4121 if (fromnd
->nl_nch
.ncp
== ncp
) {
4132 * Even though the namespaces are different, they may still represent
4133 * hardlinks to the same file. The filesystem might have a hard time
4134 * with this so we issue a NREMOVE of the source instead of a NRENAME
4135 * when we detect the situation.
4138 fdvp
= fromnd
->nl_dvp
;
4139 tdvp
= tond
->nl_dvp
;
4140 if (fdvp
== NULL
|| tdvp
== NULL
) {
4142 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4143 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4146 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4147 fdvp
, tdvp
, tond
->nl_cred
);
4154 * rename_args(char *from, char *to)
4156 * Rename files. Source and destination must either both be directories,
4157 * or both not be directories. If target is a directory, it must be empty.
4160 sys_rename(struct rename_args
*uap
)
4162 struct nlookupdata fromnd
, tond
;
4166 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4168 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4170 error
= kern_rename(&fromnd
, &tond
);
4171 nlookup_done(&tond
);
4173 nlookup_done(&fromnd
);
4174 } while (error
== EAGAIN
);
4179 * renameat_args(int oldfd, char *old, int newfd, char *new)
4181 * Rename files using paths relative to the directories associated with
4182 * oldfd and newfd. Source and destination must either both be directories,
4183 * or both not be directories. If target is a directory, it must be empty.
4186 sys_renameat(struct renameat_args
*uap
)
4188 struct nlookupdata oldnd
, newnd
;
4189 struct file
*oldfp
, *newfp
;
4193 error
= nlookup_init_at(&oldnd
, &oldfp
,
4194 uap
->oldfd
, uap
->old
,
4197 error
= nlookup_init_at(&newnd
, &newfp
,
4198 uap
->newfd
, uap
->new,
4201 error
= kern_rename(&oldnd
, &newnd
);
4202 nlookup_done_at(&newnd
, newfp
);
4204 nlookup_done_at(&oldnd
, oldfp
);
4205 } while (error
== EAGAIN
);
4210 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4212 struct thread
*td
= curthread
;
4213 struct proc
*p
= td
->td_proc
;
4219 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4220 if ((error
= nlookup(nd
)) != 0)
4223 if (nd
->nl_nch
.ncp
->nc_vp
)
4225 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4228 vattr
.va_type
= VDIR
;
4229 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4232 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4239 * mkdir_args(char *path, int mode)
4241 * Make a directory file.
4244 sys_mkdir(struct mkdir_args
*uap
)
4246 struct nlookupdata nd
;
4249 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4251 error
= kern_mkdir(&nd
, uap
->mode
);
4257 * mkdirat_args(int fd, char *path, mode_t mode)
4259 * Make a directory file. The path is relative to the directory associated
4263 sys_mkdirat(struct mkdirat_args
*uap
)
4265 struct nlookupdata nd
;
4269 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4271 error
= kern_mkdir(&nd
, uap
->mode
);
4272 nlookup_done_at(&nd
, fp
);
4277 kern_rmdir(struct nlookupdata
*nd
)
4282 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4283 if ((error
= nlookup(nd
)) != 0)
4287 * Do not allow directories representing mount points to be
4288 * deleted, even if empty. Check write perms on mount point
4289 * in case the vnode is aliased (aka nullfs).
4291 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4293 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4295 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4300 * rmdir_args(char *path)
4302 * Remove a directory file.
4305 sys_rmdir(struct rmdir_args
*uap
)
4307 struct nlookupdata nd
;
4310 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4312 error
= kern_rmdir(&nd
);
4318 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4319 enum uio_seg direction
)
4321 struct thread
*td
= curthread
;
4322 struct proc
*p
= td
->td_proc
;
4330 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4332 if ((fp
->f_flag
& FREAD
) == 0) {
4336 vp
= (struct vnode
*)fp
->f_data
;
4337 if (vp
->v_type
!= VDIR
) {
4341 aiov
.iov_base
= buf
;
4342 aiov
.iov_len
= count
;
4343 auio
.uio_iov
= &aiov
;
4344 auio
.uio_iovcnt
= 1;
4345 auio
.uio_rw
= UIO_READ
;
4346 auio
.uio_segflg
= direction
;
4348 auio
.uio_resid
= count
;
4349 loff
= auio
.uio_offset
= fp
->f_offset
;
4350 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4351 fp
->f_offset
= auio
.uio_offset
;
4356 * WARNING! *basep may not be wide enough to accomodate the
4357 * seek offset. XXX should we hack this to return the upper 32 bits
4358 * for offsets greater then 4G?
4361 *basep
= (long)loff
;
4363 *res
= count
- auio
.uio_resid
;
4370 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4372 * Read a block of directory entries in a file system independent format.
4375 sys_getdirentries(struct getdirentries_args
*uap
)
4380 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4381 &uap
->sysmsg_result
, UIO_USERSPACE
);
4383 if (error
== 0 && uap
->basep
)
4384 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4389 * getdents_args(int fd, char *buf, size_t count)
4392 sys_getdents(struct getdents_args
*uap
)
4396 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4397 &uap
->sysmsg_result
, UIO_USERSPACE
);
4403 * Set the mode mask for creation of filesystem nodes.
4405 * umask(int newmask)
4408 sys_umask(struct umask_args
*uap
)
4410 struct thread
*td
= curthread
;
4411 struct proc
*p
= td
->td_proc
;
4412 struct filedesc
*fdp
;
4415 uap
->sysmsg_result
= fdp
->fd_cmask
;
4416 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4421 * revoke(char *path)
4423 * Void all references to file by ripping underlying filesystem
4427 sys_revoke(struct revoke_args
*uap
)
4429 struct nlookupdata nd
;
4436 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4438 error
= nlookup(&nd
);
4440 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4441 cred
= crhold(nd
.nl_cred
);
4445 error
= VOP_GETATTR(vp
, &vattr
);
4446 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4447 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4448 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4450 error
= vrevoke(vp
, cred
);
4451 } else if (error
== 0) {
4452 error
= vrevoke(vp
, cred
);
4462 * getfh_args(char *fname, fhandle_t *fhp)
4464 * Get (NFS) file handle
4466 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4467 * mount. This allows nullfs mounts to be explicitly exported.
4469 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4471 * nullfs mounts of subdirectories are not safe. That is, it will
4472 * work, but you do not really have protection against access to
4473 * the related parent directories.
4476 sys_getfh(struct getfh_args
*uap
)
4478 struct thread
*td
= curthread
;
4479 struct nlookupdata nd
;
4486 * Must be super user
4488 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4492 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4494 error
= nlookup(&nd
);
4496 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4497 mp
= nd
.nl_nch
.mount
;
4500 bzero(&fh
, sizeof(fh
));
4501 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4502 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4505 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4511 * fhopen_args(const struct fhandle *u_fhp, int flags)
4513 * syscall for the rpc.lockd to use to translate a NFS file handle into
4514 * an open descriptor.
4516 * warning: do not remove the priv_check() call or this becomes one giant
4520 sys_fhopen(struct fhopen_args
*uap
)
4522 struct thread
*td
= curthread
;
4523 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4528 struct vattr
*vap
= &vat
;
4530 int fmode
, mode
, error
= 0, type
;
4536 * Must be super user
4538 error
= priv_check(td
, PRIV_ROOT
);
4542 fmode
= FFLAGS(uap
->flags
);
4545 * Why not allow a non-read/write open for our lockd?
4547 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4549 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4554 * Find the mount point
4556 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4561 /* now give me my vnode, it gets returned to me locked */
4562 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4566 * from now on we have to make sure not
4567 * to forget about the vnode
4568 * any error that causes an abort must vput(vp)
4569 * just set error = err and 'goto bad;'.
4575 if (vp
->v_type
== VLNK
) {
4579 if (vp
->v_type
== VSOCK
) {
4584 if (fmode
& (FWRITE
| O_TRUNC
)) {
4585 if (vp
->v_type
== VDIR
) {
4589 error
= vn_writechk(vp
, NULL
);
4597 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4601 if (fmode
& O_TRUNC
) {
4602 vn_unlock(vp
); /* XXX */
4603 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4606 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4612 * VOP_OPEN needs the file pointer so it can potentially override
4615 * WARNING! no f_nchandle will be associated when fhopen()ing a
4618 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4622 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4625 * setting f_ops this way prevents VOP_CLOSE from being
4626 * called or fdrop() releasing the vp from v_data. Since
4627 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4629 fp
->f_ops
= &badfileops
;
4635 * The fp is given its own reference, we still have our ref and lock.
4637 * Assert that all regular files must be created with a VM object.
4639 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4640 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4645 * The open was successful. Handle any locking requirements.
4647 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4648 lf
.l_whence
= SEEK_SET
;
4651 if (fmode
& O_EXLOCK
)
4652 lf
.l_type
= F_WRLCK
;
4654 lf
.l_type
= F_RDLCK
;
4655 if (fmode
& FNONBLOCK
)
4660 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4662 * release our private reference.
4664 fsetfd(fdp
, NULL
, indx
);
4669 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4670 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4674 * Clean up. Associate the file pointer with the previously
4675 * reserved descriptor and return it.
4678 if (uap
->flags
& O_CLOEXEC
)
4679 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4680 fsetfd(fdp
, fp
, indx
);
4682 uap
->sysmsg_result
= indx
;
4686 fsetfd(fdp
, NULL
, indx
);
4695 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4698 sys_fhstat(struct fhstat_args
*uap
)
4700 struct thread
*td
= curthread
;
4708 * Must be super user
4710 error
= priv_check(td
, PRIV_ROOT
);
4714 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4718 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4721 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4722 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4727 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4732 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4735 sys_fhstatfs(struct fhstatfs_args
*uap
)
4737 struct thread
*td
= curthread
;
4738 struct proc
*p
= td
->td_proc
;
4743 char *fullpath
, *freepath
;
4748 * Must be super user
4750 if ((error
= priv_check(td
, PRIV_ROOT
)))
4753 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4756 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4760 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4765 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4770 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4773 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4776 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4777 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4778 kfree(freepath
, M_TEMP
);
4780 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4781 if (priv_check(td
, PRIV_ROOT
)) {
4782 bcopy(sp
, &sb
, sizeof(sb
));
4783 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4786 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4792 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4795 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4797 struct thread
*td
= curthread
;
4798 struct proc
*p
= td
->td_proc
;
4806 * Must be super user
4808 if ((error
= priv_check(td
, PRIV_ROOT
)))
4811 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4814 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4818 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4823 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4826 sp
= &mp
->mnt_vstat
;
4828 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4832 if (mp
->mnt_flag
& MNT_RDONLY
)
4833 sp
->f_flag
|= ST_RDONLY
;
4834 if (mp
->mnt_flag
& MNT_NOSUID
)
4835 sp
->f_flag
|= ST_NOSUID
;
4836 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4843 * Syscall to push extended attribute configuration information into the
4844 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4845 * a command (int cmd), and attribute name and misc data. For now, the
4846 * attribute name is left in userspace for consumption by the VFS_op.
4847 * It will probably be changed to be copied into sysspace by the
4848 * syscall in the future, once issues with various consumers of the
4849 * attribute code have raised their hands.
4851 * Currently this is used only by UFS Extended Attributes.
4854 sys_extattrctl(struct extattrctl_args
*uap
)
4856 struct nlookupdata nd
;
4858 char attrname
[EXTATTR_MAXNAMELEN
];
4866 if (error
== 0 && uap
->filename
) {
4867 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4870 error
= nlookup(&nd
);
4872 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4876 if (error
== 0 && uap
->attrname
) {
4877 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4882 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4884 error
= nlookup(&nd
);
4886 error
= ncp_writechk(&nd
.nl_nch
);
4888 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4890 uap
->attrname
, nd
.nl_cred
);
4899 * Syscall to get a named extended attribute on a file or directory.
4902 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4904 char attrname
[EXTATTR_MAXNAMELEN
];
4905 struct nlookupdata nd
;
4911 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4917 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4919 error
= nlookup(&nd
);
4921 error
= ncp_writechk(&nd
.nl_nch
);
4923 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4929 bzero(&auio
, sizeof(auio
));
4930 aiov
.iov_base
= uap
->data
;
4931 aiov
.iov_len
= uap
->nbytes
;
4932 auio
.uio_iov
= &aiov
;
4933 auio
.uio_iovcnt
= 1;
4934 auio
.uio_offset
= 0;
4935 auio
.uio_resid
= uap
->nbytes
;
4936 auio
.uio_rw
= UIO_WRITE
;
4937 auio
.uio_td
= curthread
;
4939 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4948 * Syscall to get a named extended attribute on a file or directory.
4951 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4953 char attrname
[EXTATTR_MAXNAMELEN
];
4954 struct nlookupdata nd
;
4960 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4966 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4968 error
= nlookup(&nd
);
4970 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
4976 bzero(&auio
, sizeof(auio
));
4977 aiov
.iov_base
= uap
->data
;
4978 aiov
.iov_len
= uap
->nbytes
;
4979 auio
.uio_iov
= &aiov
;
4980 auio
.uio_iovcnt
= 1;
4981 auio
.uio_offset
= 0;
4982 auio
.uio_resid
= uap
->nbytes
;
4983 auio
.uio_rw
= UIO_READ
;
4984 auio
.uio_td
= curthread
;
4986 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4988 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
4996 * Syscall to delete a named extended attribute from a file or directory.
4997 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5000 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
5002 char attrname
[EXTATTR_MAXNAMELEN
];
5003 struct nlookupdata nd
;
5007 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
5011 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
5013 error
= nlookup(&nd
);
5015 error
= ncp_writechk(&nd
.nl_nch
);
5017 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
5019 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
5020 attrname
, NULL
, nd
.nl_cred
);
5029 * Determine if the mount is visible to the process.
5032 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
5034 struct nchandle nch
;
5037 * Traverse from the mount point upwards. If we hit the process
5038 * root then the mount point is visible to the process.
5040 nch
= mp
->mnt_ncmountpt
;
5042 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
5043 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
5046 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
5047 nch
= nch
.mount
->mnt_ncmounton
;
5049 nch
.ncp
= nch
.ncp
->nc_parent
;
5054 * If the mount point is not visible to the process, but the
5055 * process root is in a subdirectory of the mount, return
5058 if (p
->p_fd
->fd_nrdir
.mount
== mp
)