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>
70 #include <sys/mplock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 static void mount_warning(struct mount
*mp
, const char *ctl
, ...)
81 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
82 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
83 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
84 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
85 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
86 static int getutimes (struct timeval
*, struct timespec
*);
87 static int getutimens (const struct timespec
*, struct timespec
*, int *);
88 static int setfown (struct mount
*, struct vnode
*, uid_t
, gid_t
);
89 static int setfmode (struct vnode
*, int);
90 static int setfflags (struct vnode
*, int);
91 static int setutimes (struct vnode
*, struct vattr
*,
92 const struct timespec
*, int);
93 static int usermount
= 0; /* if 1, non-root can mount fs. */
95 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0,
96 "Allow non-root users to mount filesystems");
99 * Virtual File System System Calls
103 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args
*uap
)
112 struct thread
*td
= curthread
;
115 struct mount
*mp
, *nullmp
;
116 struct vfsconf
*vfsp
;
117 int error
, flag
= 0, flag2
= 0;
120 struct nlookupdata nd
;
121 char fstypename
[MFSNAMELEN
];
129 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
133 * Do not allow NFS export by non-root users.
135 if (uap
->flags
& MNT_EXPORTED
) {
136 error
= priv_check(td
, PRIV_ROOT
);
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
143 if (priv_check(td
, PRIV_ROOT
))
144 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
147 * Lookup the requested path and extract the nch and vnode.
149 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
151 if ((error
= nlookup(&nd
)) == 0) {
152 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
162 * If the target filesystem is resolved via a nullfs mount, then
163 * nd.nl_nch.mount will be pointing to the nullfs mount structure
164 * instead of the target file system. We need it in case we are
167 nullmp
= nd
.nl_nch
.mount
;
170 * Extract the locked+refd ncp and cleanup the nd structure
173 cache_zero(&nd
.nl_nch
);
176 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
177 (mp
= cache_findmount(&nch
)) != NULL
) {
186 * now we have the locked ref'd nch and unreferenced vnode.
189 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
196 * Extract the file system type. We need to know this early, to take
197 * appropriate actions if we are dealing with a nullfs.
199 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
206 * Now we have an unlocked ref'd nch and a locked ref'd vp
208 if (uap
->flags
& MNT_UPDATE
) {
209 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
216 if (strncmp(fstypename
, "null", 5) == 0) {
224 flag2
= mp
->mnt_kern_flag
;
226 * We only allow the filesystem to be reloaded if it
227 * is currently mounted read-only.
229 if ((uap
->flags
& MNT_RELOAD
) &&
230 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
233 error
= EOPNOTSUPP
; /* Needs translation */
237 * Only root, or the user that did the original mount is
238 * permitted to update it.
240 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
241 (error
= priv_check(td
, PRIV_ROOT
))) {
246 if (vfs_busy(mp
, LK_NOWAIT
)) {
260 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
261 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 vfsp
->vfc_refcount
++;
340 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
341 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
342 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
343 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
344 lwkt_gettoken(&mp
->mnt_token
);
348 * (per-mount token acquired at this point)
350 * Set the mount level flags.
352 if (uap
->flags
& MNT_RDONLY
)
353 mp
->mnt_flag
|= MNT_RDONLY
;
354 else if (mp
->mnt_flag
& MNT_RDONLY
)
355 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
356 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
357 MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_NOATIME
|
358 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
359 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
360 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
361 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_FORCE
|
362 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
363 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
365 * Mount the filesystem.
366 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
369 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
370 if (mp
->mnt_flag
& MNT_UPDATE
) {
371 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
372 mp
->mnt_flag
&= ~MNT_RDONLY
;
373 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
374 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
377 mp
->mnt_kern_flag
= flag2
;
379 lwkt_reltoken(&mp
->mnt_token
);
385 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
388 * Put the new filesystem on the mount list after root. The mount
389 * point gets its own mnt_ncmountpt (unless the VFS already set one
390 * up) which represents the root of the mount. The lookup code
391 * detects the mount point going forward and checks the root of
392 * the mount going backwards.
394 * It is not necessary to invalidate or purge the vnode underneath
395 * because elements under the mount will be given their own glue
399 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
401 * allocate, then unlock, but leave the ref intact
403 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
404 cache_unlock(&mp
->mnt_ncmountpt
);
406 mp
->mnt_ncmounton
= nch
; /* inherits ref */
407 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
408 cache_ismounting(mp
);
410 mountlist_insert(mp
, MNTINS_LAST
);
412 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
413 error
= vfs_allocate_syncvnode(mp
);
414 lwkt_reltoken(&mp
->mnt_token
);
416 error
= VFS_START(mp
, 0);
419 vn_syncer_thr_stop(mp
);
420 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
421 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
422 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
423 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
424 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
425 mp
->mnt_vfc
->vfc_refcount
--;
426 lwkt_reltoken(&mp
->mnt_token
);
437 * Scan all active processes to see if any of them have a current
438 * or root directory onto which the new filesystem has just been
439 * mounted. If so, replace them with the new mount point.
441 * Both old_nch and new_nch are ref'd on call but not locked.
442 * new_nch must be temporarily locked so it can be associated with the
443 * vnode representing the root of the mount point.
445 struct checkdirs_info
{
446 struct nchandle old_nch
;
447 struct nchandle new_nch
;
448 struct vnode
*old_vp
;
449 struct vnode
*new_vp
;
452 static int checkdirs_callback(struct proc
*p
, void *data
);
455 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
457 struct checkdirs_info info
;
463 * If the old mount point's vnode has a usecount of 1, it is not
464 * being held as a descriptor anywhere.
466 olddp
= old_nch
->ncp
->nc_vp
;
467 if (olddp
== NULL
|| VREFCNT(olddp
) == 1)
471 * Force the root vnode of the new mount point to be resolved
472 * so we can update any matching processes.
475 if (VFS_ROOT(mp
, &newdp
))
476 panic("mount: lost mount");
479 vn_lock(newdp
, LK_EXCLUSIVE
| LK_RETRY
);
480 cache_setunresolved(new_nch
);
481 cache_setvp(new_nch
, newdp
);
482 cache_unlock(new_nch
);
485 * Special handling of the root node
487 if (rootvnode
== olddp
) {
489 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
493 * Pass newdp separately so the callback does not have to access
494 * it via new_nch->ncp->nc_vp.
496 info
.old_nch
= *old_nch
;
497 info
.new_nch
= *new_nch
;
499 allproc_scan(checkdirs_callback
, &info
);
504 * NOTE: callback is not MP safe because the scanned process's filedesc
505 * structure can be ripped out from under us, amoung other things.
508 checkdirs_callback(struct proc
*p
, void *data
)
510 struct checkdirs_info
*info
= data
;
511 struct filedesc
*fdp
;
512 struct nchandle ncdrop1
;
513 struct nchandle ncdrop2
;
514 struct vnode
*vprele1
;
515 struct vnode
*vprele2
;
517 if ((fdp
= p
->p_fd
) != NULL
) {
518 cache_zero(&ncdrop1
);
519 cache_zero(&ncdrop2
);
524 * MPUNSAFE - XXX fdp can be pulled out from under a
527 * A shared filedesc is ok, we don't have to copy it
528 * because we are making this change globally.
530 spin_lock(&fdp
->fd_spin
);
531 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
532 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
533 vprele1
= fdp
->fd_cdir
;
535 fdp
->fd_cdir
= info
->new_vp
;
536 ncdrop1
= fdp
->fd_ncdir
;
537 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
539 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
540 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
541 vprele2
= fdp
->fd_rdir
;
543 fdp
->fd_rdir
= info
->new_vp
;
544 ncdrop2
= fdp
->fd_nrdir
;
545 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
547 spin_unlock(&fdp
->fd_spin
);
549 cache_drop(&ncdrop1
);
551 cache_drop(&ncdrop2
);
561 * Unmount a file system.
563 * Note: unmount takes a path to the vnode mounted on as argument,
564 * not special file (as before).
566 * umount_args(char *path, int flags)
571 sys_unmount(struct unmount_args
*uap
)
573 struct thread
*td
= curthread
;
574 struct proc
*p __debugvar
= td
->td_proc
;
575 struct mount
*mp
= NULL
;
576 struct nlookupdata nd
;
581 if (td
->td_ucred
->cr_prison
!= NULL
) {
585 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
588 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
590 error
= nlookup(&nd
);
594 mp
= nd
.nl_nch
.mount
;
597 * Only root, or the user that did the original mount is
598 * permitted to unmount this filesystem.
600 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
601 (error
= priv_check(td
, PRIV_ROOT
)))
605 * Don't allow unmounting the root file system.
607 if (mp
->mnt_flag
& MNT_ROOTFS
) {
613 * Must be the root of the filesystem
615 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
623 error
= dounmount(mp
, uap
->flags
);
630 * Do the actual file system unmount.
633 dounmount_interlock(struct mount
*mp
)
635 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
637 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
642 unmount_allproc_cb(struct proc
*p
, void *arg
)
646 if (p
->p_textnch
.ncp
== NULL
)
649 mp
= (struct mount
*)arg
;
650 if (p
->p_textnch
.mount
== mp
)
651 cache_drop(&p
->p_textnch
);
657 dounmount(struct mount
*mp
, int flags
)
659 struct namecache
*ncp
;
668 lwkt_gettoken(&mp
->mnt_token
);
670 * Exclusive access for unmounting purposes
672 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
676 * Allow filesystems to detect that a forced unmount is in progress.
678 if (flags
& MNT_FORCE
)
679 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
680 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_TIMELOCK
);
681 error
= lockmgr(&mp
->mnt_lock
, lflags
);
683 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
684 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
685 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
691 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
692 vfs_setpublicfs(NULL
, NULL
, NULL
);
694 vfs_msync(mp
, MNT_WAIT
);
695 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
696 mp
->mnt_flag
&=~ MNT_ASYNC
;
699 * If this filesystem isn't aliasing other filesystems,
700 * try to invalidate any remaining namecache entries and
701 * check the count afterwords.
703 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
704 cache_lock(&mp
->mnt_ncmountpt
);
705 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
706 cache_unlock(&mp
->mnt_ncmountpt
);
708 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
709 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
710 allproc_scan(&unmount_allproc_cb
, mp
);
713 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
714 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
716 if ((flags
& MNT_FORCE
) == 0) {
718 mount_warning(mp
, "Cannot unmount: "
724 mount_warning(mp
, "Forced unmount: "
735 * Decomission our special mnt_syncer vnode. This also stops
736 * the vnlru code. If we are unable to unmount we recommission
739 * Then sync the filesystem.
741 if ((vp
= mp
->mnt_syncer
) != NULL
) {
742 mp
->mnt_syncer
= NULL
;
743 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
746 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
747 VFS_SYNC(mp
, MNT_WAIT
);
750 * nchandle records ref the mount structure. Expect a count of 1
751 * (our mount->mnt_ncmountpt).
753 * Scans can get temporary refs on a mountpoint (thought really
754 * heavy duty stuff like cache_findmount() do not).
756 for (retry
= 0; retry
< 10 && mp
->mnt_refs
!= 1; ++retry
) {
757 cache_unmounting(mp
);
758 tsleep(&mp
->mnt_refs
, 0, "mntbsy", hz
/ 10 + 1);
760 if (mp
->mnt_refs
!= 1) {
761 if ((flags
& MNT_FORCE
) == 0) {
762 mount_warning(mp
, "Cannot unmount: "
763 "%d mount refs still present",
767 mount_warning(mp
, "Forced unmount: "
768 "%d mount refs still present",
775 * So far so good, sync the filesystem once more and
776 * call the VFS unmount code if the sync succeeds.
779 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
780 (error
= VFS_SYNC(mp
, MNT_WAIT
)) == 0) ||
781 (flags
& MNT_FORCE
)) {
782 error
= VFS_UNMOUNT(mp
, flags
);
787 * If an error occurred we can still recover, restoring the
788 * syncer vnode and misc flags.
791 if (mp
->mnt_syncer
== NULL
)
792 vfs_allocate_syncvnode(mp
);
793 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
794 mp
->mnt_flag
|= async_flag
;
795 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
796 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
797 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
803 * Clean up any journals still associated with the mount after
804 * filesystem activity has ceased.
806 journal_remove_all_journals(mp
,
807 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
809 mountlist_remove(mp
);
812 * Remove any installed vnode ops here so the individual VFSs don't
815 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
816 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
817 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
818 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
819 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
821 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
822 nch
= mp
->mnt_ncmountpt
;
823 cache_zero(&mp
->mnt_ncmountpt
);
824 cache_clrmountpt(&nch
);
827 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
828 cache_unmounting(mp
);
829 nch
= mp
->mnt_ncmounton
;
830 cache_zero(&mp
->mnt_ncmounton
);
831 cache_clrmountpt(&nch
);
835 mp
->mnt_vfc
->vfc_refcount
--;
836 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
837 panic("unmount: dangling vnode");
838 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
839 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
840 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
845 * If we reach here and freeok != 0 we must free the mount.
846 * If refs > 1 cycle and wait, just in case someone tried
847 * to busy the mount after we decided to do the unmount.
850 while (mp
->mnt_refs
> 1) {
851 cache_unmounting(mp
);
853 tsleep(&mp
->mnt_refs
, 0, "umntrwait", hz
/ 10 + 1);
855 lwkt_reltoken(&mp
->mnt_token
);
862 lwkt_reltoken(&mp
->mnt_token
);
868 mount_warning(struct mount
*mp
, const char *ctl
, ...)
875 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, NULL
,
876 &ptr
, &buf
, 0) == 0) {
877 kprintf("unmount(%s): ", ptr
);
882 kprintf("unmount(%p", mp
);
883 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
884 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
893 * Shim cache_fullpath() to handle the case where a process is chrooted into
894 * a subdirectory of a mount. In this case if the root mount matches the
895 * process root directory's mount we have to specify the process's root
896 * directory instead of the mount point, because the mount point might
897 * be above the root directory.
901 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
903 struct nchandle
*nch
;
905 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
906 nch
= &p
->p_fd
->fd_nrdir
;
908 nch
= &mp
->mnt_ncmountpt
;
909 return(cache_fullpath(p
, nch
, NULL
, rb
, fb
, 0));
913 * Sync each mounted filesystem.
917 static int syncprt
= 0;
918 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
921 static int sync_callback(struct mount
*mp
, void *data
);
924 sys_sync(struct sync_args
*uap
)
926 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
932 sync_callback(struct mount
*mp
, void *data __unused
)
936 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
937 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
938 mp
->mnt_flag
&= ~MNT_ASYNC
;
939 vfs_msync(mp
, MNT_NOWAIT
);
940 VFS_SYNC(mp
, MNT_NOWAIT
);
941 mp
->mnt_flag
|= asyncflag
;
946 /* XXX PRISON: could be per prison flag */
947 static int prison_quotas
;
949 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
953 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
955 * Change filesystem quotas.
960 sys_quotactl(struct quotactl_args
*uap
)
962 struct nlookupdata nd
;
969 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
974 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
976 error
= nlookup(&nd
);
978 mp
= nd
.nl_nch
.mount
;
979 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
980 uap
->arg
, nd
.nl_cred
);
989 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
990 * void *buf, int buflen)
992 * This function operates on a mount point and executes the specified
993 * operation using the specified control data, and possibly returns data.
995 * The actual number of bytes stored in the result buffer is returned, 0
996 * if none, otherwise an error is returned.
1001 sys_mountctl(struct mountctl_args
*uap
)
1003 struct thread
*td
= curthread
;
1004 struct proc
*p
= td
->td_proc
;
1012 * Sanity and permissions checks. We must be root.
1015 if (td
->td_ucred
->cr_prison
!= NULL
)
1017 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
1018 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
1022 * Argument length checks
1024 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
1026 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
1028 if (uap
->path
== NULL
)
1032 * Allocate the necessary buffers and copyin data
1034 path
= objcache_get(namei_oc
, M_WAITOK
);
1035 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1040 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1041 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1046 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1049 * Validate the descriptor
1052 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1062 * Execute the internal kernel function and clean up.
1065 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1069 if (error
== 0 && uap
->sysmsg_result
> 0)
1070 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1073 objcache_put(namei_oc
, path
);
1082 * Execute a mount control operation by resolving the path to a mount point
1083 * and calling vop_mountctl().
1085 * Use the mount point from the nch instead of the vnode so nullfs mounts
1086 * can properly spike the VOP.
1089 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1090 const void *ctl
, int ctllen
,
1091 void *buf
, int buflen
, int *res
)
1095 struct nlookupdata nd
;
1100 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1102 error
= nlookup(&nd
);
1104 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1105 mp
= nd
.nl_nch
.mount
;
1112 * Must be the root of the filesystem
1114 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1118 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1125 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1127 struct thread
*td
= curthread
;
1128 struct proc
*p
= td
->td_proc
;
1131 char *fullpath
, *freepath
;
1134 if ((error
= nlookup(nd
)) != 0)
1136 mp
= nd
->nl_nch
.mount
;
1138 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1141 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1144 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1145 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1146 kfree(freepath
, M_TEMP
);
1148 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1149 bcopy(sp
, buf
, sizeof(*buf
));
1150 /* Only root should have access to the fsid's. */
1151 if (priv_check(td
, PRIV_ROOT
))
1152 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1157 * statfs_args(char *path, struct statfs *buf)
1159 * Get filesystem statistics.
1162 sys_statfs(struct statfs_args
*uap
)
1164 struct nlookupdata nd
;
1168 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1170 error
= kern_statfs(&nd
, &buf
);
1173 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1178 kern_fstatfs(int fd
, struct statfs
*buf
)
1180 struct thread
*td
= curthread
;
1181 struct proc
*p
= td
->td_proc
;
1185 char *fullpath
, *freepath
;
1189 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1193 * Try to use mount info from any overlays rather than the
1194 * mount info for the underlying vnode, otherwise we will
1195 * fail when operating on null-mounted paths inside a chroot.
1197 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1198 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1203 if (fp
->f_cred
== NULL
) {
1208 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1211 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1213 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1214 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1215 kfree(freepath
, M_TEMP
);
1217 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1218 bcopy(sp
, buf
, sizeof(*buf
));
1220 /* Only root should have access to the fsid's. */
1221 if (priv_check(td
, PRIV_ROOT
))
1222 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1230 * fstatfs_args(int fd, struct statfs *buf)
1232 * Get filesystem statistics.
1235 sys_fstatfs(struct fstatfs_args
*uap
)
1240 error
= kern_fstatfs(uap
->fd
, &buf
);
1243 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1248 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1254 if ((error
= nlookup(nd
)) != 0)
1256 mp
= nd
->nl_nch
.mount
;
1257 sp
= &mp
->mnt_vstat
;
1258 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1262 if (mp
->mnt_flag
& MNT_RDONLY
)
1263 sp
->f_flag
|= ST_RDONLY
;
1264 if (mp
->mnt_flag
& MNT_NOSUID
)
1265 sp
->f_flag
|= ST_NOSUID
;
1266 bcopy(sp
, buf
, sizeof(*buf
));
1271 * statfs_args(char *path, struct statfs *buf)
1273 * Get filesystem statistics.
1276 sys_statvfs(struct statvfs_args
*uap
)
1278 struct nlookupdata nd
;
1282 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1284 error
= kern_statvfs(&nd
, &buf
);
1287 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1292 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1294 struct thread
*td
= curthread
;
1295 struct proc
*p
= td
->td_proc
;
1302 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1304 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1305 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1310 if (fp
->f_cred
== NULL
) {
1314 sp
= &mp
->mnt_vstat
;
1315 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1319 if (mp
->mnt_flag
& MNT_RDONLY
)
1320 sp
->f_flag
|= ST_RDONLY
;
1321 if (mp
->mnt_flag
& MNT_NOSUID
)
1322 sp
->f_flag
|= ST_NOSUID
;
1324 bcopy(sp
, buf
, sizeof(*buf
));
1332 * fstatfs_args(int fd, struct statfs *buf)
1334 * Get filesystem statistics.
1337 sys_fstatvfs(struct fstatvfs_args
*uap
)
1342 error
= kern_fstatvfs(uap
->fd
, &buf
);
1345 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1350 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1352 * Get statistics on all filesystems.
1355 struct getfsstat_info
{
1356 struct statfs
*sfsp
;
1364 static int getfsstat_callback(struct mount
*, void *);
1367 sys_getfsstat(struct getfsstat_args
*uap
)
1369 struct thread
*td
= curthread
;
1370 struct getfsstat_info info
;
1372 bzero(&info
, sizeof(info
));
1374 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1375 info
.sfsp
= uap
->buf
;
1377 info
.flags
= uap
->flags
;
1380 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1381 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1382 uap
->sysmsg_result
= info
.maxcount
;
1384 uap
->sysmsg_result
= info
.count
;
1385 return (info
.error
);
1389 getfsstat_callback(struct mount
*mp
, void *data
)
1391 struct getfsstat_info
*info
= data
;
1397 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1398 if (info
->td
->td_proc
&&
1399 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1405 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1406 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1407 * overrides MNT_WAIT.
1409 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1410 (info
->flags
& MNT_WAIT
)) &&
1411 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1414 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1416 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1418 info
->error
= error
;
1421 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1422 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1423 kfree(freepath
, M_TEMP
);
1425 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1427 info
->error
= error
;
1437 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1438 long bufsize, int flags)
1440 * Get statistics on all filesystems.
1443 struct getvfsstat_info
{
1444 struct statfs
*sfsp
;
1445 struct statvfs
*vsfsp
;
1453 static int getvfsstat_callback(struct mount
*, void *);
1456 sys_getvfsstat(struct getvfsstat_args
*uap
)
1458 struct thread
*td
= curthread
;
1459 struct getvfsstat_info info
;
1461 bzero(&info
, sizeof(info
));
1463 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1464 info
.sfsp
= uap
->buf
;
1465 info
.vsfsp
= uap
->vbuf
;
1467 info
.flags
= uap
->flags
;
1470 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1471 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1472 uap
->sysmsg_result
= info
.maxcount
;
1474 uap
->sysmsg_result
= info
.count
;
1475 return (info
.error
);
1479 getvfsstat_callback(struct mount
*mp
, void *data
)
1481 struct getvfsstat_info
*info
= data
;
1483 struct statvfs
*vsp
;
1488 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1489 if (info
->td
->td_proc
&&
1490 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1494 vsp
= &mp
->mnt_vstat
;
1497 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1498 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1499 * overrides MNT_WAIT.
1501 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1502 (info
->flags
& MNT_WAIT
)) &&
1503 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1506 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1508 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1509 (info
->flags
& MNT_WAIT
)) &&
1510 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1514 if (mp
->mnt_flag
& MNT_RDONLY
)
1515 vsp
->f_flag
|= ST_RDONLY
;
1516 if (mp
->mnt_flag
& MNT_NOSUID
)
1517 vsp
->f_flag
|= ST_NOSUID
;
1519 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1521 info
->error
= error
;
1524 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1525 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1526 kfree(freepath
, M_TEMP
);
1528 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1530 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1532 info
->error
= error
;
1544 * fchdir_args(int fd)
1546 * Change current working directory to a given file descriptor.
1549 sys_fchdir(struct fchdir_args
*uap
)
1551 struct thread
*td
= curthread
;
1552 struct proc
*p
= td
->td_proc
;
1553 struct filedesc
*fdp
= p
->p_fd
;
1554 struct vnode
*vp
, *ovp
;
1557 struct nchandle nch
, onch
, tnch
;
1560 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1562 lwkt_gettoken(&p
->p_token
);
1563 vp
= (struct vnode
*)fp
->f_data
;
1565 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1566 if (fp
->f_nchandle
.ncp
== NULL
)
1569 error
= checkvp_chdir(vp
, td
);
1574 cache_copy(&fp
->f_nchandle
, &nch
);
1577 * If the ncp has become a mount point, traverse through
1581 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1582 (mp
= cache_findmount(&nch
)) != NULL
1584 error
= nlookup_mp(mp
, &tnch
);
1586 cache_unlock(&tnch
); /* leave ref intact */
1588 vp
= tnch
.ncp
->nc_vp
;
1589 error
= vget(vp
, LK_SHARED
);
1590 KKASSERT(error
== 0);
1594 cache_dropmount(mp
);
1598 onch
= fdp
->fd_ncdir
;
1599 vn_unlock(vp
); /* leave ref intact */
1601 fdp
->fd_ncdir
= nch
;
1610 lwkt_reltoken(&p
->p_token
);
1615 kern_chdir(struct nlookupdata
*nd
)
1617 struct thread
*td
= curthread
;
1618 struct proc
*p
= td
->td_proc
;
1619 struct filedesc
*fdp
= p
->p_fd
;
1620 struct vnode
*vp
, *ovp
;
1621 struct nchandle onch
;
1624 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1625 if ((error
= nlookup(nd
)) != 0)
1627 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1629 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1632 lwkt_gettoken(&p
->p_token
);
1633 error
= checkvp_chdir(vp
, td
);
1637 onch
= fdp
->fd_ncdir
;
1638 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1639 fdp
->fd_ncdir
= nd
->nl_nch
;
1643 cache_zero(&nd
->nl_nch
);
1647 lwkt_reltoken(&p
->p_token
);
1652 * chdir_args(char *path)
1654 * Change current working directory (``.'').
1657 sys_chdir(struct chdir_args
*uap
)
1659 struct nlookupdata nd
;
1662 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1664 error
= kern_chdir(&nd
);
1670 * Helper function for raised chroot(2) security function: Refuse if
1671 * any filedescriptors are open directories.
1674 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1681 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1682 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1684 vp
= (struct vnode
*)fp
->f_data
;
1685 if (vp
->v_type
!= VDIR
) {
1696 * This sysctl determines if we will allow a process to chroot(2) if it
1697 * has a directory open:
1698 * 0: disallowed for all processes.
1699 * 1: allowed for processes that were not already chroot(2)'ed.
1700 * 2: allowed for all processes.
1703 static int chroot_allow_open_directories
= 1;
1705 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1706 &chroot_allow_open_directories
, 0, "");
1709 * chroot to the specified namecache entry. We obtain the vp from the
1710 * namecache data. The passed ncp must be locked and referenced and will
1711 * remain locked and referenced on return.
1714 kern_chroot(struct nchandle
*nch
)
1716 struct thread
*td
= curthread
;
1717 struct proc
*p
= td
->td_proc
;
1718 struct filedesc
*fdp
= p
->p_fd
;
1723 * Only privileged user can chroot
1725 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1730 * Disallow open directory descriptors (fchdir() breakouts).
1732 if (chroot_allow_open_directories
== 0 ||
1733 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1734 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1737 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1740 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1744 * Check the validity of vp as a directory to change to and
1745 * associate it with rdir/jdir.
1747 error
= checkvp_chdir(vp
, td
);
1748 vn_unlock(vp
); /* leave reference intact */
1750 vrele(fdp
->fd_rdir
);
1751 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1752 cache_drop(&fdp
->fd_nrdir
);
1753 cache_copy(nch
, &fdp
->fd_nrdir
);
1754 if (fdp
->fd_jdir
== NULL
) {
1757 cache_copy(nch
, &fdp
->fd_njdir
);
1766 * chroot_args(char *path)
1768 * Change notion of root (``/'') directory.
1771 sys_chroot(struct chroot_args
*uap
)
1773 struct thread
*td __debugvar
= curthread
;
1774 struct nlookupdata nd
;
1777 KKASSERT(td
->td_proc
);
1778 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1780 nd
.nl_flags
|= NLC_EXEC
;
1781 error
= nlookup(&nd
);
1783 error
= kern_chroot(&nd
.nl_nch
);
1790 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1792 struct thread
*td
= curthread
;
1793 struct nlookupdata nd
;
1794 struct nchandle
*nch
;
1798 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1802 error
= nlookup(&nd
);
1808 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1812 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1817 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1820 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1822 vfs_cache_setroot(vp
, cache_hold(nch
));
1832 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1833 * determine whether it is legal to chdir to the vnode. The vnode's state
1834 * is not changed by this call.
1837 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1841 if (vp
->v_type
!= VDIR
)
1844 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1849 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1851 struct thread
*td
= curthread
;
1852 struct proc
*p
= td
->td_proc
;
1853 struct lwp
*lp
= td
->td_lwp
;
1854 struct filedesc
*fdp
= p
->p_fd
;
1859 int type
, indx
, error
= 0;
1862 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1864 flags
= FFLAGS(oflags
);
1865 error
= falloc(lp
, &nfp
, NULL
);
1869 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1872 * XXX p_dupfd is a real mess. It allows a device to return a
1873 * file descriptor to be duplicated rather then doing the open
1879 * Call vn_open() to do the lookup and assign the vnode to the
1880 * file pointer. vn_open() does not change the ref count on fp
1881 * and the vnode, on success, will be inherited by the file pointer
1884 * Request a shared lock on the vnode if possible.
1886 nd
->nl_flags
|= NLC_LOCKVP
;
1887 if ((flags
& (O_CREAT
|O_TRUNC
)) == 0)
1888 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1890 error
= vn_open(nd
, fp
, flags
, cmode
);
1895 * handle special fdopen() case. bleh. dupfdopen() is
1896 * responsible for dropping the old contents of ofiles[indx]
1899 * Note that fsetfd() will add a ref to fp which represents
1900 * the fd_files[] assignment. We must still drop our
1903 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1904 if (fdalloc(p
, 0, &indx
) == 0) {
1905 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1908 fdrop(fp
); /* our ref */
1911 fsetfd(fdp
, NULL
, indx
);
1914 fdrop(fp
); /* our ref */
1915 if (error
== ERESTART
)
1921 * ref the vnode for ourselves so it can't be ripped out from under
1922 * is. XXX need an ND flag to request that the vnode be returned
1925 * Reserve a file descriptor but do not assign it until the open
1928 vp
= (struct vnode
*)fp
->f_data
;
1930 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1937 * If no error occurs the vp will have been assigned to the file
1942 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1943 lf
.l_whence
= SEEK_SET
;
1946 if (flags
& O_EXLOCK
)
1947 lf
.l_type
= F_WRLCK
;
1949 lf
.l_type
= F_RDLCK
;
1950 if (flags
& FNONBLOCK
)
1955 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1957 * lock request failed. Clean up the reserved
1961 fsetfd(fdp
, NULL
, indx
);
1965 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
1969 * Assert that all regular file vnodes were created with a object.
1971 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1972 ("open: regular file has no backing object after vn_open"));
1978 * release our private reference, leaving the one associated with the
1979 * descriptor table intact.
1981 if (oflags
& O_CLOEXEC
)
1982 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
1983 fsetfd(fdp
, fp
, indx
);
1990 * open_args(char *path, int flags, int mode)
1992 * Check permissions, allocate an open file structure,
1993 * and call the device open routine if any.
1996 sys_open(struct open_args
*uap
)
1998 struct nlookupdata nd
;
2001 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2003 error
= kern_open(&nd
, uap
->flags
,
2004 uap
->mode
, &uap
->sysmsg_result
);
2011 * openat_args(int fd, char *path, int flags, int mode)
2014 sys_openat(struct openat_args
*uap
)
2016 struct nlookupdata nd
;
2020 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2022 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2023 &uap
->sysmsg_result
);
2025 nlookup_done_at(&nd
, fp
);
2030 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2032 struct thread
*td
= curthread
;
2033 struct proc
*p
= td
->td_proc
;
2042 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2043 vattr
.va_rmajor
= rmajor
;
2044 vattr
.va_rminor
= rminor
;
2046 switch (mode
& S_IFMT
) {
2047 case S_IFMT
: /* used by badsect to flag bad sectors */
2048 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2049 vattr
.va_type
= VBAD
;
2052 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2053 vattr
.va_type
= VCHR
;
2056 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2057 vattr
.va_type
= VBLK
;
2060 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2063 case S_IFDIR
: /* special directories support for HAMMER */
2064 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2065 vattr
.va_type
= VDIR
;
2076 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2077 if ((error
= nlookup(nd
)) != 0)
2079 if (nd
->nl_nch
.ncp
->nc_vp
)
2081 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2085 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2086 nd
->nl_cred
, NAMEI_CREATE
);
2089 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2090 &vp
, nd
->nl_cred
, &vattr
);
2098 * mknod_args(char *path, int mode, int dev)
2100 * Create a special file.
2103 sys_mknod(struct mknod_args
*uap
)
2105 struct nlookupdata nd
;
2108 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2110 error
= kern_mknod(&nd
, uap
->mode
,
2111 umajor(uap
->dev
), uminor(uap
->dev
));
2118 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2120 * Create a special file. The path is relative to the directory associated
2124 sys_mknodat(struct mknodat_args
*uap
)
2126 struct nlookupdata nd
;
2130 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2132 error
= kern_mknod(&nd
, uap
->mode
,
2133 umajor(uap
->dev
), uminor(uap
->dev
));
2135 nlookup_done_at(&nd
, fp
);
2140 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2142 struct thread
*td
= curthread
;
2143 struct proc
*p
= td
->td_proc
;
2150 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2151 if ((error
= nlookup(nd
)) != 0)
2153 if (nd
->nl_nch
.ncp
->nc_vp
)
2155 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2159 vattr
.va_type
= VFIFO
;
2160 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2162 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2169 * mkfifo_args(char *path, int mode)
2171 * Create a named pipe.
2174 sys_mkfifo(struct mkfifo_args
*uap
)
2176 struct nlookupdata nd
;
2179 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2181 error
= kern_mkfifo(&nd
, uap
->mode
);
2187 * mkfifoat_args(int fd, char *path, mode_t mode)
2189 * Create a named pipe. The path is relative to the directory associated
2193 sys_mkfifoat(struct mkfifoat_args
*uap
)
2195 struct nlookupdata nd
;
2199 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2201 error
= kern_mkfifo(&nd
, uap
->mode
);
2202 nlookup_done_at(&nd
, fp
);
2206 static int hardlink_check_uid
= 0;
2207 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2208 &hardlink_check_uid
, 0,
2209 "Unprivileged processes cannot create hard links to files owned by other "
2211 static int hardlink_check_gid
= 0;
2212 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2213 &hardlink_check_gid
, 0,
2214 "Unprivileged processes cannot create hard links to files owned by other "
2218 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2224 * Shortcut if disabled
2226 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2230 * Privileged user can always hardlink
2232 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2236 * Otherwise only if the originating file is owned by the
2237 * same user or group. Note that any group is allowed if
2238 * the file is owned by the caller.
2240 error
= VOP_GETATTR(vp
, &va
);
2244 if (hardlink_check_uid
) {
2245 if (cred
->cr_uid
!= va
.va_uid
)
2249 if (hardlink_check_gid
) {
2250 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2258 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2260 struct thread
*td
= curthread
;
2265 * Lookup the source and obtained a locked vnode.
2267 * You may only hardlink a file which you have write permission
2268 * on or which you own.
2270 * XXX relookup on vget failure / race ?
2273 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2274 if ((error
= nlookup(nd
)) != 0)
2276 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2277 KKASSERT(vp
!= NULL
);
2278 if (vp
->v_type
== VDIR
)
2279 return (EPERM
); /* POSIX */
2280 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2282 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2286 * Unlock the source so we can lookup the target without deadlocking
2287 * (XXX vp is locked already, possible other deadlock?). The target
2290 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2291 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2292 cache_unlock(&nd
->nl_nch
);
2295 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2296 if ((error
= nlookup(linknd
)) != 0) {
2300 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2304 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2311 * Finally run the new API VOP.
2313 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2315 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2316 vp
, linknd
->nl_cred
);
2323 * link_args(char *path, char *link)
2325 * Make a hard file link.
2328 sys_link(struct link_args
*uap
)
2330 struct nlookupdata nd
, linknd
;
2333 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2335 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2337 error
= kern_link(&nd
, &linknd
);
2338 nlookup_done(&linknd
);
2345 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2347 * Make a hard file link. The path1 argument is relative to the directory
2348 * associated with fd1, and similarly the path2 argument is relative to
2349 * the directory associated with fd2.
2352 sys_linkat(struct linkat_args
*uap
)
2354 struct nlookupdata nd
, linknd
;
2355 struct file
*fp1
, *fp2
;
2358 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2359 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2361 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2362 uap
->path2
, UIO_USERSPACE
, 0);
2364 error
= kern_link(&nd
, &linknd
);
2365 nlookup_done_at(&linknd
, fp2
);
2367 nlookup_done_at(&nd
, fp1
);
2372 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2380 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2381 if ((error
= nlookup(nd
)) != 0)
2383 if (nd
->nl_nch
.ncp
->nc_vp
)
2385 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2389 vattr
.va_mode
= mode
;
2390 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2397 * symlink(char *path, char *link)
2399 * Make a symbolic link.
2402 sys_symlink(struct symlink_args
*uap
)
2404 struct thread
*td
= curthread
;
2405 struct nlookupdata nd
;
2410 path
= objcache_get(namei_oc
, M_WAITOK
);
2411 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2413 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2415 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2416 error
= kern_symlink(&nd
, path
, mode
);
2420 objcache_put(namei_oc
, path
);
2425 * symlinkat_args(char *path1, int fd, char *path2)
2427 * Make a symbolic link. The path2 argument is relative to the directory
2428 * associated with fd.
2431 sys_symlinkat(struct symlinkat_args
*uap
)
2433 struct thread
*td
= curthread
;
2434 struct nlookupdata nd
;
2440 path1
= objcache_get(namei_oc
, M_WAITOK
);
2441 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2443 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2446 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2447 error
= kern_symlink(&nd
, path1
, mode
);
2449 nlookup_done_at(&nd
, fp
);
2451 objcache_put(namei_oc
, path1
);
2456 * undelete_args(char *path)
2458 * Delete a whiteout from the filesystem.
2461 sys_undelete(struct undelete_args
*uap
)
2463 struct nlookupdata nd
;
2466 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2468 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2470 error
= nlookup(&nd
);
2472 error
= ncp_writechk(&nd
.nl_nch
);
2474 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2482 kern_unlink(struct nlookupdata
*nd
)
2487 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2488 if ((error
= nlookup(nd
)) != 0)
2490 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2492 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2497 * unlink_args(char *path)
2499 * Delete a name from the filesystem.
2502 sys_unlink(struct unlink_args
*uap
)
2504 struct nlookupdata nd
;
2507 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2509 error
= kern_unlink(&nd
);
2516 * unlinkat_args(int fd, char *path, int flags)
2518 * Delete the file or directory entry pointed to by fd/path.
2521 sys_unlinkat(struct unlinkat_args
*uap
)
2523 struct nlookupdata nd
;
2527 if (uap
->flags
& ~AT_REMOVEDIR
)
2530 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2532 if (uap
->flags
& AT_REMOVEDIR
)
2533 error
= kern_rmdir(&nd
);
2535 error
= kern_unlink(&nd
);
2537 nlookup_done_at(&nd
, fp
);
2542 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2544 struct thread
*td
= curthread
;
2545 struct proc
*p
= td
->td_proc
;
2552 fp
= holdfp(p
->p_fd
, fd
, -1);
2555 if (fp
->f_type
!= DTYPE_VNODE
) {
2559 vp
= (struct vnode
*)fp
->f_data
;
2563 spin_lock(&fp
->f_spin
);
2564 new_offset
= fp
->f_offset
+ offset
;
2568 error
= VOP_GETATTR(vp
, &vattr
);
2569 spin_lock(&fp
->f_spin
);
2570 new_offset
= offset
+ vattr
.va_size
;
2573 new_offset
= offset
;
2575 spin_lock(&fp
->f_spin
);
2580 spin_lock(&fp
->f_spin
);
2585 * Validate the seek position. Negative offsets are not allowed
2586 * for regular files or directories.
2588 * Normally we would also not want to allow negative offsets for
2589 * character and block-special devices. However kvm addresses
2590 * on 64 bit architectures might appear to be negative and must
2594 if (new_offset
< 0 &&
2595 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2598 fp
->f_offset
= new_offset
;
2601 *res
= fp
->f_offset
;
2602 spin_unlock(&fp
->f_spin
);
2609 * lseek_args(int fd, int pad, off_t offset, int whence)
2611 * Reposition read/write file offset.
2614 sys_lseek(struct lseek_args
*uap
)
2618 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2619 &uap
->sysmsg_offset
);
2625 * Check if current process can access given file. amode is a bitmask of *_OK
2626 * access bits. flags is a bitmask of AT_* flags.
2629 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2634 if (flags
& ~AT_EACCESS
)
2636 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2637 if ((error
= nlookup(nd
)) != 0)
2640 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2644 /* Flags == 0 means only check for existence. */
2653 if ((mode
& VWRITE
) == 0 ||
2654 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2655 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2658 * If the file handle is stale we have to re-resolve the
2659 * entry with the ncp held exclusively. This is a hack
2662 if (error
== ESTALE
) {
2664 cache_unlock(&nd
->nl_nch
);
2665 cache_lock(&nd
->nl_nch
);
2666 cache_setunresolved(&nd
->nl_nch
);
2667 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2680 * access_args(char *path, int flags)
2682 * Check access permissions.
2685 sys_access(struct access_args
*uap
)
2687 struct nlookupdata nd
;
2690 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2692 error
= kern_access(&nd
, uap
->flags
, 0);
2699 * eaccess_args(char *path, int flags)
2701 * Check access permissions.
2704 sys_eaccess(struct eaccess_args
*uap
)
2706 struct nlookupdata nd
;
2709 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2711 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2718 * faccessat_args(int fd, char *path, int amode, int flags)
2720 * Check access permissions.
2723 sys_faccessat(struct faccessat_args
*uap
)
2725 struct nlookupdata nd
;
2729 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2732 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2733 nlookup_done_at(&nd
, fp
);
2738 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2743 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2744 if ((error
= nlookup(nd
)) != 0)
2747 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2750 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2752 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2755 * If the file handle is stale we have to re-resolve the
2756 * entry with the ncp held exclusively. This is a hack
2759 if (error
== ESTALE
) {
2761 cache_unlock(&nd
->nl_nch
);
2762 cache_lock(&nd
->nl_nch
);
2763 cache_setunresolved(&nd
->nl_nch
);
2764 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2774 * stat_args(char *path, struct stat *ub)
2776 * Get file status; this version follows links.
2779 sys_stat(struct stat_args
*uap
)
2781 struct nlookupdata nd
;
2785 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2787 error
= kern_stat(&nd
, &st
);
2789 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2796 * lstat_args(char *path, struct stat *ub)
2798 * Get file status; this version does not follow links.
2801 sys_lstat(struct lstat_args
*uap
)
2803 struct nlookupdata nd
;
2807 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2809 error
= kern_stat(&nd
, &st
);
2811 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2818 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2820 * Get status of file pointed to by fd/path.
2823 sys_fstatat(struct fstatat_args
*uap
)
2825 struct nlookupdata nd
;
2831 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2834 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2836 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2837 UIO_USERSPACE
, flags
);
2839 error
= kern_stat(&nd
, &st
);
2841 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2843 nlookup_done_at(&nd
, fp
);
2848 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2850 struct nlookupdata nd
;
2855 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2857 error
= nlookup(&nd
);
2859 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2862 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2869 * pathconf_Args(char *path, int name)
2871 * Get configurable pathname variables.
2874 sys_pathconf(struct pathconf_args
*uap
)
2876 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2881 * lpathconf_Args(char *path, int name)
2883 * Get configurable pathname variables, but don't follow symlinks.
2886 sys_lpathconf(struct lpathconf_args
*uap
)
2888 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2893 * kern_readlink isn't properly split yet. There is a copyin burried
2894 * in VOP_READLINK().
2897 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2899 struct thread
*td
= curthread
;
2905 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2906 if ((error
= nlookup(nd
)) != 0)
2908 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2911 if (vp
->v_type
!= VLNK
) {
2914 aiov
.iov_base
= buf
;
2915 aiov
.iov_len
= count
;
2916 auio
.uio_iov
= &aiov
;
2917 auio
.uio_iovcnt
= 1;
2918 auio
.uio_offset
= 0;
2919 auio
.uio_rw
= UIO_READ
;
2920 auio
.uio_segflg
= UIO_USERSPACE
;
2922 auio
.uio_resid
= count
;
2923 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
2926 *res
= count
- auio
.uio_resid
;
2931 * readlink_args(char *path, char *buf, int count)
2933 * Return target name of a symbolic link.
2936 sys_readlink(struct readlink_args
*uap
)
2938 struct nlookupdata nd
;
2941 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2943 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2944 &uap
->sysmsg_result
);
2951 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2953 * Return target name of a symbolic link. The path is relative to the
2954 * directory associated with fd.
2957 sys_readlinkat(struct readlinkat_args
*uap
)
2959 struct nlookupdata nd
;
2963 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2965 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
2966 &uap
->sysmsg_result
);
2968 nlookup_done_at(&nd
, fp
);
2973 setfflags(struct vnode
*vp
, int flags
)
2975 struct thread
*td
= curthread
;
2980 * Prevent non-root users from setting flags on devices. When
2981 * a device is reused, users can retain ownership of the device
2982 * if they are allowed to set flags and programs assume that
2983 * chown can't fail when done as root.
2985 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2986 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
2990 * note: vget is required for any operation that might mod the vnode
2991 * so VINACTIVE is properly cleared.
2993 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2995 vattr
.va_flags
= flags
;
2996 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3003 * chflags(char *path, int flags)
3005 * Change flags of a file given a path name.
3008 sys_chflags(struct chflags_args
*uap
)
3010 struct nlookupdata nd
;
3015 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3017 error
= nlookup(&nd
);
3019 error
= ncp_writechk(&nd
.nl_nch
);
3021 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3024 error
= setfflags(vp
, uap
->flags
);
3031 * lchflags(char *path, int flags)
3033 * Change flags of a file given a path name, but don't follow symlinks.
3036 sys_lchflags(struct lchflags_args
*uap
)
3038 struct nlookupdata nd
;
3043 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3045 error
= nlookup(&nd
);
3047 error
= ncp_writechk(&nd
.nl_nch
);
3049 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3052 error
= setfflags(vp
, uap
->flags
);
3059 * fchflags_args(int fd, int flags)
3061 * Change flags of a file given a file descriptor.
3064 sys_fchflags(struct fchflags_args
*uap
)
3066 struct thread
*td
= curthread
;
3067 struct proc
*p
= td
->td_proc
;
3071 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3073 if (fp
->f_nchandle
.ncp
)
3074 error
= ncp_writechk(&fp
->f_nchandle
);
3076 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3082 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3083 * change flags given a pathname relative to a filedescriptor
3085 int sys_chflagsat(struct chflagsat_args
*uap
)
3087 struct nlookupdata nd
;
3093 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3096 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3099 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3101 error
= nlookup(&nd
);
3103 error
= ncp_writechk(&nd
.nl_nch
);
3105 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3106 nlookup_done_at(&nd
, fp
);
3108 error
= setfflags(vp
, uap
->flags
);
3116 setfmode(struct vnode
*vp
, int mode
)
3118 struct thread
*td
= curthread
;
3123 * note: vget is required for any operation that might mod the vnode
3124 * so VINACTIVE is properly cleared.
3126 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3128 vattr
.va_mode
= mode
& ALLPERMS
;
3129 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3136 kern_chmod(struct nlookupdata
*nd
, int mode
)
3141 if ((error
= nlookup(nd
)) != 0)
3143 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3145 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3146 error
= setfmode(vp
, mode
);
3152 * chmod_args(char *path, int mode)
3154 * Change mode of a file given path name.
3157 sys_chmod(struct chmod_args
*uap
)
3159 struct nlookupdata nd
;
3162 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3164 error
= kern_chmod(&nd
, uap
->mode
);
3170 * lchmod_args(char *path, int mode)
3172 * Change mode of a file given path name (don't follow links.)
3175 sys_lchmod(struct lchmod_args
*uap
)
3177 struct nlookupdata nd
;
3180 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3182 error
= kern_chmod(&nd
, uap
->mode
);
3188 * fchmod_args(int fd, int mode)
3190 * Change mode of a file given a file descriptor.
3193 sys_fchmod(struct fchmod_args
*uap
)
3195 struct thread
*td
= curthread
;
3196 struct proc
*p
= td
->td_proc
;
3200 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3202 if (fp
->f_nchandle
.ncp
)
3203 error
= ncp_writechk(&fp
->f_nchandle
);
3205 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3211 * fchmodat_args(char *path, int mode)
3213 * Change mode of a file pointed to by fd/path.
3216 sys_fchmodat(struct fchmodat_args
*uap
)
3218 struct nlookupdata nd
;
3223 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3225 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3227 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3228 UIO_USERSPACE
, flags
);
3230 error
= kern_chmod(&nd
, uap
->mode
);
3231 nlookup_done_at(&nd
, fp
);
3236 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3238 struct thread
*td
= curthread
;
3246 * note: vget is required for any operation that might mod the vnode
3247 * so VINACTIVE is properly cleared.
3249 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3250 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3252 o_uid
= vattr
.va_uid
;
3253 o_gid
= vattr
.va_gid
;
3254 size
= vattr
.va_size
;
3259 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3268 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3269 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3276 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3281 if ((error
= nlookup(nd
)) != 0)
3283 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3285 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3286 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3292 * chown(char *path, int uid, int gid)
3294 * Set ownership given a path name.
3297 sys_chown(struct chown_args
*uap
)
3299 struct nlookupdata nd
;
3302 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3304 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3310 * lchown_args(char *path, int uid, int gid)
3312 * Set ownership given a path name, do not cross symlinks.
3315 sys_lchown(struct lchown_args
*uap
)
3317 struct nlookupdata nd
;
3320 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3322 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3328 * fchown_args(int fd, int uid, int gid)
3330 * Set ownership given a file descriptor.
3333 sys_fchown(struct fchown_args
*uap
)
3335 struct thread
*td
= curthread
;
3336 struct proc
*p
= td
->td_proc
;
3340 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3342 if (fp
->f_nchandle
.ncp
)
3343 error
= ncp_writechk(&fp
->f_nchandle
);
3345 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3346 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3352 * fchownat(int fd, char *path, int uid, int gid, int flags)
3354 * Set ownership of file pointed to by fd/path.
3357 sys_fchownat(struct fchownat_args
*uap
)
3359 struct nlookupdata nd
;
3364 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3366 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3368 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3369 UIO_USERSPACE
, flags
);
3371 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3372 nlookup_done_at(&nd
, fp
);
3378 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3380 struct timeval tv
[2];
3385 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3388 if ((error
= itimerfix(tvp
)) != 0)
3390 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3391 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3397 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3399 struct timespec tsnow
;
3413 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3415 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3418 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3419 newts
[0].tv_sec
= VNOVAL
;
3420 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3422 else if ((error
= itimespecfix(&newts
[0])) != 0)
3425 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3426 newts
[1].tv_sec
= VNOVAL
;
3427 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3429 else if ((error
= itimespecfix(&newts
[1])) != 0)
3436 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3437 const struct timespec
*ts
, int nullflag
)
3439 struct thread
*td
= curthread
;
3443 vattr
->va_atime
= ts
[0];
3444 vattr
->va_mtime
= ts
[1];
3446 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3447 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3453 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3455 struct timespec ts
[2];
3459 if ((error
= getutimes(tptr
, ts
)) != 0)
3462 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3467 * utimes_args(char *path, struct timeval *tptr)
3469 * Set the access and modification times of a file.
3472 sys_utimes(struct utimes_args
*uap
)
3474 struct timeval tv
[2];
3475 struct nlookupdata nd
;
3479 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3483 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3485 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3491 * lutimes_args(char *path, struct timeval *tptr)
3493 * Set the access and modification times of a file.
3496 sys_lutimes(struct lutimes_args
*uap
)
3498 struct timeval tv
[2];
3499 struct nlookupdata nd
;
3503 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3507 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3509 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3515 * Set utimes on a file descriptor. The creds used to open the
3516 * file are used to determine whether the operation is allowed
3520 kern_futimens(int fd
, struct timespec
*ts
)
3522 struct thread
*td
= curthread
;
3523 struct proc
*p
= td
->td_proc
;
3524 struct timespec newts
[2];
3531 error
= getutimens(ts
, newts
, &nullflag
);
3534 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3536 if (fp
->f_nchandle
.ncp
)
3537 error
= ncp_writechk(&fp
->f_nchandle
);
3540 error
= vget(vp
, LK_EXCLUSIVE
);
3542 error
= VOP_GETATTR(vp
, &vattr
);
3544 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3548 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3558 * futimens_args(int fd, struct timespec *ts)
3560 * Set the access and modification times of a file.
3563 sys_futimens(struct futimens_args
*uap
)
3565 struct timespec ts
[2];
3569 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3573 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3578 kern_futimes(int fd
, struct timeval
*tptr
)
3580 struct timespec ts
[2];
3584 if ((error
= getutimes(tptr
, ts
)) != 0)
3587 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3592 * futimes_args(int fd, struct timeval *tptr)
3594 * Set the access and modification times of a file.
3597 sys_futimes(struct futimes_args
*uap
)
3599 struct timeval tv
[2];
3603 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3607 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3612 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3614 struct timespec newts
[2];
3620 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3623 error
= getutimens(ts
, newts
, &nullflag
);
3627 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3628 if ((error
= nlookup(nd
)) != 0)
3630 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3632 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3634 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3635 error
= vget(vp
, LK_EXCLUSIVE
);
3637 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3646 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3648 * Set file access and modification times of a file.
3651 sys_utimensat(struct utimensat_args
*uap
)
3653 struct timespec ts
[2];
3654 struct nlookupdata nd
;
3660 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3665 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3666 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3667 UIO_USERSPACE
, flags
);
3669 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3670 nlookup_done_at(&nd
, fp
);
3675 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3682 uint64_t old_size
= 0;
3686 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3687 if ((error
= nlookup(nd
)) != 0)
3689 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3691 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3693 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3698 if (vp
->v_type
== VDIR
) {
3702 if (vfs_quota_enabled
) {
3703 error
= VOP_GETATTR(vp
, &vattr
);
3704 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3707 old_size
= vattr
.va_size
;
3710 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3712 vattr
.va_size
= length
;
3713 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3714 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3722 * truncate(char *path, int pad, off_t length)
3724 * Truncate a file given its path name.
3727 sys_truncate(struct truncate_args
*uap
)
3729 struct nlookupdata nd
;
3732 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3734 error
= kern_truncate(&nd
, uap
->length
);
3740 kern_ftruncate(int fd
, off_t length
)
3742 struct thread
*td
= curthread
;
3743 struct proc
*p
= td
->td_proc
;
3750 uint64_t old_size
= 0;
3755 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3757 if (fp
->f_nchandle
.ncp
) {
3758 error
= ncp_writechk(&fp
->f_nchandle
);
3762 if ((fp
->f_flag
& FWRITE
) == 0) {
3766 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3770 vp
= (struct vnode
*)fp
->f_data
;
3771 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3772 if (vp
->v_type
== VDIR
) {
3778 if (vfs_quota_enabled
) {
3779 error
= VOP_GETATTR(vp
, &vattr
);
3780 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3783 old_size
= vattr
.va_size
;
3786 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3788 vattr
.va_size
= length
;
3789 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3791 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3800 * ftruncate_args(int fd, int pad, off_t length)
3802 * Truncate a file given a file descriptor.
3805 sys_ftruncate(struct ftruncate_args
*uap
)
3809 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3817 * Sync an open file.
3820 sys_fsync(struct fsync_args
*uap
)
3822 struct thread
*td
= curthread
;
3823 struct proc
*p
= td
->td_proc
;
3829 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3831 vp
= (struct vnode
*)fp
->f_data
;
3832 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3833 if ((obj
= vp
->v_object
) != NULL
) {
3834 if (vp
->v_mount
== NULL
||
3835 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3836 vm_object_page_clean(obj
, 0, 0, 0);
3839 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3840 if (error
== 0 && vp
->v_mount
)
3841 error
= buf_fsync(vp
);
3849 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3851 struct nchandle fnchd
;
3852 struct nchandle tnchd
;
3853 struct namecache
*ncp
;
3862 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3863 if ((error
= nlookup(fromnd
)) != 0)
3865 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3867 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3871 * unlock the source nch so we can lookup the target nch without
3872 * deadlocking. The target may or may not exist so we do not check
3873 * for a target vp like kern_mkdir() and other creation functions do.
3875 * The source and target directories are ref'd and rechecked after
3876 * everything is relocked to determine if the source or target file
3879 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3880 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3882 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3884 cache_unlock(&fromnd
->nl_nch
);
3886 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3887 if ((error
= nlookup(tond
)) != 0) {
3891 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3893 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3897 tnchd
.mount
= tond
->nl_nch
.mount
;
3901 * If the source and target are the same there is nothing to do
3903 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
3910 * Mount points cannot be renamed or overwritten
3912 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
3921 * Relock the source ncp. cache_relock() will deal with any
3922 * deadlocks against the already-locked tond and will also
3923 * make sure both are resolved.
3925 * NOTE AFTER RELOCKING: The source or target ncp may have become
3926 * invalid while they were unlocked, nc_vp and nc_mount could
3929 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
3930 &tond
->nl_nch
, tond
->nl_cred
);
3931 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
3934 * If the namecache generation changed for either fromnd or tond,
3937 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
3938 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
3939 kprintf("kern_rename: retry due to gen on: "
3940 "\"%s\" -> \"%s\"\n",
3941 fromnd
->nl_nch
.ncp
->nc_name
,
3942 tond
->nl_nch
.ncp
->nc_name
);
3949 * If either fromnd or tond are marked destroyed a ripout occured
3950 * out from under us and we must retry.
3952 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
3953 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
3954 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
3955 kprintf("kern_rename: retry due to ripout on: "
3956 "\"%s\" -> \"%s\"\n",
3957 fromnd
->nl_nch
.ncp
->nc_name
,
3958 tond
->nl_nch
.ncp
->nc_name
);
3965 * Make sure the parent directories linkages are the same.
3966 * XXX shouldn't be needed any more w/ generation check above.
3968 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
3969 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
3976 * Both the source and target must be within the same filesystem and
3977 * in the same filesystem as their parent directories within the
3978 * namecache topology.
3980 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3983 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
3984 mp
!= tond
->nl_nch
.mount
) {
3991 * Make sure the mount point is writable
3993 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4000 * If the target exists and either the source or target is a directory,
4001 * then both must be directories.
4003 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4006 if (tond
->nl_nch
.ncp
->nc_vp
) {
4007 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4009 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4010 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4012 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4018 * You cannot rename a source into itself or a subdirectory of itself.
4019 * We check this by travsersing the target directory upwards looking
4020 * for a match against the source.
4025 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4026 if (fromnd
->nl_nch
.ncp
== ncp
) {
4037 * Even though the namespaces are different, they may still represent
4038 * hardlinks to the same file. The filesystem might have a hard time
4039 * with this so we issue a NREMOVE of the source instead of a NRENAME
4040 * when we detect the situation.
4043 fdvp
= fromnd
->nl_dvp
;
4044 tdvp
= tond
->nl_dvp
;
4045 if (fdvp
== NULL
|| tdvp
== NULL
) {
4047 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4048 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4051 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4052 fdvp
, tdvp
, tond
->nl_cred
);
4059 * rename_args(char *from, char *to)
4061 * Rename files. Source and destination must either both be directories,
4062 * or both not be directories. If target is a directory, it must be empty.
4065 sys_rename(struct rename_args
*uap
)
4067 struct nlookupdata fromnd
, tond
;
4071 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4073 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4075 error
= kern_rename(&fromnd
, &tond
);
4076 nlookup_done(&tond
);
4078 nlookup_done(&fromnd
);
4079 } while (error
== EAGAIN
);
4084 * renameat_args(int oldfd, char *old, int newfd, char *new)
4086 * Rename files using paths relative to the directories associated with
4087 * oldfd and newfd. Source and destination must either both be directories,
4088 * or both not be directories. If target is a directory, it must be empty.
4091 sys_renameat(struct renameat_args
*uap
)
4093 struct nlookupdata oldnd
, newnd
;
4094 struct file
*oldfp
, *newfp
;
4098 error
= nlookup_init_at(&oldnd
, &oldfp
,
4099 uap
->oldfd
, uap
->old
,
4102 error
= nlookup_init_at(&newnd
, &newfp
,
4103 uap
->newfd
, uap
->new,
4106 error
= kern_rename(&oldnd
, &newnd
);
4107 nlookup_done_at(&newnd
, newfp
);
4109 nlookup_done_at(&oldnd
, oldfp
);
4110 } while (error
== EAGAIN
);
4115 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4117 struct thread
*td
= curthread
;
4118 struct proc
*p
= td
->td_proc
;
4124 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4125 if ((error
= nlookup(nd
)) != 0)
4128 if (nd
->nl_nch
.ncp
->nc_vp
)
4130 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4133 vattr
.va_type
= VDIR
;
4134 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4137 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4144 * mkdir_args(char *path, int mode)
4146 * Make a directory file.
4149 sys_mkdir(struct mkdir_args
*uap
)
4151 struct nlookupdata nd
;
4154 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4156 error
= kern_mkdir(&nd
, uap
->mode
);
4162 * mkdirat_args(int fd, char *path, mode_t mode)
4164 * Make a directory file. The path is relative to the directory associated
4168 sys_mkdirat(struct mkdirat_args
*uap
)
4170 struct nlookupdata nd
;
4174 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4176 error
= kern_mkdir(&nd
, uap
->mode
);
4177 nlookup_done_at(&nd
, fp
);
4182 kern_rmdir(struct nlookupdata
*nd
)
4187 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4188 if ((error
= nlookup(nd
)) != 0)
4192 * Do not allow directories representing mount points to be
4193 * deleted, even if empty. Check write perms on mount point
4194 * in case the vnode is aliased (aka nullfs).
4196 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4198 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4200 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4205 * rmdir_args(char *path)
4207 * Remove a directory file.
4210 sys_rmdir(struct rmdir_args
*uap
)
4212 struct nlookupdata nd
;
4215 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4217 error
= kern_rmdir(&nd
);
4223 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4224 enum uio_seg direction
)
4226 struct thread
*td
= curthread
;
4227 struct proc
*p
= td
->td_proc
;
4235 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4237 if ((fp
->f_flag
& FREAD
) == 0) {
4241 vp
= (struct vnode
*)fp
->f_data
;
4242 if (vp
->v_type
!= VDIR
) {
4246 aiov
.iov_base
= buf
;
4247 aiov
.iov_len
= count
;
4248 auio
.uio_iov
= &aiov
;
4249 auio
.uio_iovcnt
= 1;
4250 auio
.uio_rw
= UIO_READ
;
4251 auio
.uio_segflg
= direction
;
4253 auio
.uio_resid
= count
;
4254 loff
= auio
.uio_offset
= fp
->f_offset
;
4255 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4256 fp
->f_offset
= auio
.uio_offset
;
4261 * WARNING! *basep may not be wide enough to accomodate the
4262 * seek offset. XXX should we hack this to return the upper 32 bits
4263 * for offsets greater then 4G?
4266 *basep
= (long)loff
;
4268 *res
= count
- auio
.uio_resid
;
4275 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4277 * Read a block of directory entries in a file system independent format.
4280 sys_getdirentries(struct getdirentries_args
*uap
)
4285 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4286 &uap
->sysmsg_result
, UIO_USERSPACE
);
4288 if (error
== 0 && uap
->basep
)
4289 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4294 * getdents_args(int fd, char *buf, size_t count)
4297 sys_getdents(struct getdents_args
*uap
)
4301 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4302 &uap
->sysmsg_result
, UIO_USERSPACE
);
4308 * Set the mode mask for creation of filesystem nodes.
4310 * umask(int newmask)
4313 sys_umask(struct umask_args
*uap
)
4315 struct thread
*td
= curthread
;
4316 struct proc
*p
= td
->td_proc
;
4317 struct filedesc
*fdp
;
4320 uap
->sysmsg_result
= fdp
->fd_cmask
;
4321 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4326 * revoke(char *path)
4328 * Void all references to file by ripping underlying filesystem
4332 sys_revoke(struct revoke_args
*uap
)
4334 struct nlookupdata nd
;
4341 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4343 error
= nlookup(&nd
);
4345 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4346 cred
= crhold(nd
.nl_cred
);
4350 error
= VOP_GETATTR(vp
, &vattr
);
4351 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4352 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4353 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4355 error
= vrevoke(vp
, cred
);
4356 } else if (error
== 0) {
4357 error
= vrevoke(vp
, cred
);
4367 * getfh_args(char *fname, fhandle_t *fhp)
4369 * Get (NFS) file handle
4371 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4372 * mount. This allows nullfs mounts to be explicitly exported.
4374 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4376 * nullfs mounts of subdirectories are not safe. That is, it will
4377 * work, but you do not really have protection against access to
4378 * the related parent directories.
4381 sys_getfh(struct getfh_args
*uap
)
4383 struct thread
*td
= curthread
;
4384 struct nlookupdata nd
;
4391 * Must be super user
4393 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4397 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4399 error
= nlookup(&nd
);
4401 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4402 mp
= nd
.nl_nch
.mount
;
4405 bzero(&fh
, sizeof(fh
));
4406 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4407 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4410 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4416 * fhopen_args(const struct fhandle *u_fhp, int flags)
4418 * syscall for the rpc.lockd to use to translate a NFS file handle into
4419 * an open descriptor.
4421 * warning: do not remove the priv_check() call or this becomes one giant
4425 sys_fhopen(struct fhopen_args
*uap
)
4427 struct thread
*td
= curthread
;
4428 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4433 struct vattr
*vap
= &vat
;
4435 int fmode
, mode
, error
= 0, type
;
4441 * Must be super user
4443 error
= priv_check(td
, PRIV_ROOT
);
4447 fmode
= FFLAGS(uap
->flags
);
4450 * Why not allow a non-read/write open for our lockd?
4452 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4454 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4459 * Find the mount point
4461 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4466 /* now give me my vnode, it gets returned to me locked */
4467 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4471 * from now on we have to make sure not
4472 * to forget about the vnode
4473 * any error that causes an abort must vput(vp)
4474 * just set error = err and 'goto bad;'.
4480 if (vp
->v_type
== VLNK
) {
4484 if (vp
->v_type
== VSOCK
) {
4489 if (fmode
& (FWRITE
| O_TRUNC
)) {
4490 if (vp
->v_type
== VDIR
) {
4494 error
= vn_writechk(vp
, NULL
);
4502 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4506 if (fmode
& O_TRUNC
) {
4507 vn_unlock(vp
); /* XXX */
4508 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4511 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4517 * VOP_OPEN needs the file pointer so it can potentially override
4520 * WARNING! no f_nchandle will be associated when fhopen()ing a
4523 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4527 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4530 * setting f_ops this way prevents VOP_CLOSE from being
4531 * called or fdrop() releasing the vp from v_data. Since
4532 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4534 fp
->f_ops
= &badfileops
;
4540 * The fp is given its own reference, we still have our ref and lock.
4542 * Assert that all regular files must be created with a VM object.
4544 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4545 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4550 * The open was successful. Handle any locking requirements.
4552 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4553 lf
.l_whence
= SEEK_SET
;
4556 if (fmode
& O_EXLOCK
)
4557 lf
.l_type
= F_WRLCK
;
4559 lf
.l_type
= F_RDLCK
;
4560 if (fmode
& FNONBLOCK
)
4565 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4567 * release our private reference.
4569 fsetfd(fdp
, NULL
, indx
);
4574 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4575 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4579 * Clean up. Associate the file pointer with the previously
4580 * reserved descriptor and return it.
4583 if (uap
->flags
& O_CLOEXEC
)
4584 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4585 fsetfd(fdp
, fp
, indx
);
4587 uap
->sysmsg_result
= indx
;
4591 fsetfd(fdp
, NULL
, indx
);
4600 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4603 sys_fhstat(struct fhstat_args
*uap
)
4605 struct thread
*td
= curthread
;
4613 * Must be super user
4615 error
= priv_check(td
, PRIV_ROOT
);
4619 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4623 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4626 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4627 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4632 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4637 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4640 sys_fhstatfs(struct fhstatfs_args
*uap
)
4642 struct thread
*td
= curthread
;
4643 struct proc
*p
= td
->td_proc
;
4648 char *fullpath
, *freepath
;
4653 * Must be super user
4655 if ((error
= priv_check(td
, PRIV_ROOT
)))
4658 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4661 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4665 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4670 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4675 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4678 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4681 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4682 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4683 kfree(freepath
, M_TEMP
);
4685 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4686 if (priv_check(td
, PRIV_ROOT
)) {
4687 bcopy(sp
, &sb
, sizeof(sb
));
4688 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4691 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4697 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4700 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4702 struct thread
*td
= curthread
;
4703 struct proc
*p
= td
->td_proc
;
4711 * Must be super user
4713 if ((error
= priv_check(td
, PRIV_ROOT
)))
4716 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4719 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4723 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4728 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4731 sp
= &mp
->mnt_vstat
;
4733 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4737 if (mp
->mnt_flag
& MNT_RDONLY
)
4738 sp
->f_flag
|= ST_RDONLY
;
4739 if (mp
->mnt_flag
& MNT_NOSUID
)
4740 sp
->f_flag
|= ST_NOSUID
;
4741 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4748 * Syscall to push extended attribute configuration information into the
4749 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4750 * a command (int cmd), and attribute name and misc data. For now, the
4751 * attribute name is left in userspace for consumption by the VFS_op.
4752 * It will probably be changed to be copied into sysspace by the
4753 * syscall in the future, once issues with various consumers of the
4754 * attribute code have raised their hands.
4756 * Currently this is used only by UFS Extended Attributes.
4759 sys_extattrctl(struct extattrctl_args
*uap
)
4761 struct nlookupdata nd
;
4763 char attrname
[EXTATTR_MAXNAMELEN
];
4771 if (error
== 0 && uap
->filename
) {
4772 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4775 error
= nlookup(&nd
);
4777 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4781 if (error
== 0 && uap
->attrname
) {
4782 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4787 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4789 error
= nlookup(&nd
);
4791 error
= ncp_writechk(&nd
.nl_nch
);
4793 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4795 uap
->attrname
, nd
.nl_cred
);
4804 * Syscall to get a named extended attribute on a file or directory.
4807 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4809 char attrname
[EXTATTR_MAXNAMELEN
];
4810 struct nlookupdata nd
;
4816 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4822 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4824 error
= nlookup(&nd
);
4826 error
= ncp_writechk(&nd
.nl_nch
);
4828 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4834 bzero(&auio
, sizeof(auio
));
4835 aiov
.iov_base
= uap
->data
;
4836 aiov
.iov_len
= uap
->nbytes
;
4837 auio
.uio_iov
= &aiov
;
4838 auio
.uio_iovcnt
= 1;
4839 auio
.uio_offset
= 0;
4840 auio
.uio_resid
= uap
->nbytes
;
4841 auio
.uio_rw
= UIO_WRITE
;
4842 auio
.uio_td
= curthread
;
4844 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4853 * Syscall to get a named extended attribute on a file or directory.
4856 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4858 char attrname
[EXTATTR_MAXNAMELEN
];
4859 struct nlookupdata nd
;
4865 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4871 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4873 error
= nlookup(&nd
);
4875 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
4881 bzero(&auio
, sizeof(auio
));
4882 aiov
.iov_base
= uap
->data
;
4883 aiov
.iov_len
= uap
->nbytes
;
4884 auio
.uio_iov
= &aiov
;
4885 auio
.uio_iovcnt
= 1;
4886 auio
.uio_offset
= 0;
4887 auio
.uio_resid
= uap
->nbytes
;
4888 auio
.uio_rw
= UIO_READ
;
4889 auio
.uio_td
= curthread
;
4891 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4893 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
4901 * Syscall to delete a named extended attribute from a file or directory.
4902 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4905 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
4907 char attrname
[EXTATTR_MAXNAMELEN
];
4908 struct nlookupdata nd
;
4912 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4916 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4918 error
= nlookup(&nd
);
4920 error
= ncp_writechk(&nd
.nl_nch
);
4922 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4924 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
4925 attrname
, NULL
, nd
.nl_cred
);
4934 * Determine if the mount is visible to the process.
4937 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
4939 struct nchandle nch
;
4942 * Traverse from the mount point upwards. If we hit the process
4943 * root then the mount point is visible to the process.
4945 nch
= mp
->mnt_ncmountpt
;
4947 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
4948 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
4951 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
4952 nch
= nch
.mount
->mnt_ncmounton
;
4954 nch
.ncp
= nch
.ncp
->nc_parent
;
4959 * If the mount point is not visible to the process, but the
4960 * process root is in a subdirectory of the mount, return
4963 if (p
->p_fd
->fd_nrdir
.mount
== mp
)