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
|
361 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
362 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_FORCE
|
363 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
364 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
|
367 * Mount the filesystem.
368 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
371 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
372 if (mp
->mnt_flag
& MNT_UPDATE
) {
373 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
374 mp
->mnt_flag
&= ~MNT_RDONLY
;
375 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
376 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
379 mp
->mnt_kern_flag
= flag2
;
381 lwkt_reltoken(&mp
->mnt_token
);
387 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
390 * Put the new filesystem on the mount list after root. The mount
391 * point gets its own mnt_ncmountpt (unless the VFS already set one
392 * up) which represents the root of the mount. The lookup code
393 * detects the mount point going forward and checks the root of
394 * the mount going backwards.
396 * It is not necessary to invalidate or purge the vnode underneath
397 * because elements under the mount will be given their own glue
401 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
403 * allocate, then unlock, but leave the ref intact
405 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
406 cache_unlock(&mp
->mnt_ncmountpt
);
408 mp
->mnt_ncmounton
= nch
; /* inherits ref */
409 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
410 cache_ismounting(mp
);
412 mountlist_insert(mp
, MNTINS_LAST
);
414 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
415 error
= vfs_allocate_syncvnode(mp
);
416 lwkt_reltoken(&mp
->mnt_token
);
418 error
= VFS_START(mp
, 0);
420 KNOTE(&fs_klist
, VQ_MOUNT
);
422 vn_syncer_thr_stop(mp
);
423 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
424 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
425 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
426 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
427 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
428 mp
->mnt_vfc
->vfc_refcount
--;
429 lwkt_reltoken(&mp
->mnt_token
);
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * Both old_nch and new_nch are ref'd on call but not locked.
445 * new_nch must be temporarily locked so it can be associated with the
446 * vnode representing the root of the mount point.
448 struct checkdirs_info
{
449 struct nchandle old_nch
;
450 struct nchandle new_nch
;
451 struct vnode
*old_vp
;
452 struct vnode
*new_vp
;
455 static int checkdirs_callback(struct proc
*p
, void *data
);
458 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
460 struct checkdirs_info info
;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp
= old_nch
->ncp
->nc_vp
;
470 if (olddp
== NULL
|| VREFCNT(olddp
) == 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp
, &newdp
))
479 panic("mount: lost mount");
482 vn_lock(newdp
, LK_EXCLUSIVE
| LK_RETRY
);
483 cache_setunresolved(new_nch
);
484 cache_setvp(new_nch
, newdp
);
485 cache_unlock(new_nch
);
488 * Special handling of the root node
490 if (rootvnode
== olddp
) {
492 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
496 * Pass newdp separately so the callback does not have to access
497 * it via new_nch->ncp->nc_vp.
499 info
.old_nch
= *old_nch
;
500 info
.new_nch
= *new_nch
;
502 allproc_scan(checkdirs_callback
, &info
);
507 * NOTE: callback is not MP safe because the scanned process's filedesc
508 * structure can be ripped out from under us, amoung other things.
511 checkdirs_callback(struct proc
*p
, void *data
)
513 struct checkdirs_info
*info
= data
;
514 struct filedesc
*fdp
;
515 struct nchandle ncdrop1
;
516 struct nchandle ncdrop2
;
517 struct vnode
*vprele1
;
518 struct vnode
*vprele2
;
520 if ((fdp
= p
->p_fd
) != NULL
) {
521 cache_zero(&ncdrop1
);
522 cache_zero(&ncdrop2
);
527 * MPUNSAFE - XXX fdp can be pulled out from under a
530 * A shared filedesc is ok, we don't have to copy it
531 * because we are making this change globally.
533 spin_lock(&fdp
->fd_spin
);
534 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
535 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
536 vprele1
= fdp
->fd_cdir
;
538 fdp
->fd_cdir
= info
->new_vp
;
539 ncdrop1
= fdp
->fd_ncdir
;
540 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
542 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
543 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
544 vprele2
= fdp
->fd_rdir
;
546 fdp
->fd_rdir
= info
->new_vp
;
547 ncdrop2
= fdp
->fd_nrdir
;
548 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
550 spin_unlock(&fdp
->fd_spin
);
552 cache_drop(&ncdrop1
);
554 cache_drop(&ncdrop2
);
564 * Unmount a file system.
566 * Note: unmount takes a path to the vnode mounted on as argument,
567 * not special file (as before).
569 * umount_args(char *path, int flags)
574 sys_unmount(struct unmount_args
*uap
)
576 struct thread
*td
= curthread
;
577 struct proc
*p __debugvar
= td
->td_proc
;
578 struct mount
*mp
= NULL
;
579 struct nlookupdata nd
;
584 if (td
->td_ucred
->cr_prison
!= NULL
) {
588 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
591 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
593 error
= nlookup(&nd
);
597 mp
= nd
.nl_nch
.mount
;
600 * Only root, or the user that did the original mount is
601 * permitted to unmount this filesystem.
603 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
604 (error
= priv_check(td
, PRIV_ROOT
)))
608 * Don't allow unmounting the root file system.
610 if (mp
->mnt_flag
& MNT_ROOTFS
) {
616 * Must be the root of the filesystem
618 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
626 error
= dounmount(mp
, uap
->flags
);
633 * Do the actual file system unmount.
636 dounmount_interlock(struct mount
*mp
)
638 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
640 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
645 unmount_allproc_cb(struct proc
*p
, void *arg
)
649 if (p
->p_textnch
.ncp
== NULL
)
652 mp
= (struct mount
*)arg
;
653 if (p
->p_textnch
.mount
== mp
)
654 cache_drop(&p
->p_textnch
);
660 dounmount(struct mount
*mp
, int flags
)
662 struct namecache
*ncp
;
671 lwkt_gettoken(&mp
->mnt_token
);
673 * Exclusive access for unmounting purposes
675 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
679 * Allow filesystems to detect that a forced unmount is in progress.
681 if (flags
& MNT_FORCE
)
682 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
683 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_TIMELOCK
);
684 error
= lockmgr(&mp
->mnt_lock
, lflags
);
686 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
687 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
688 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
694 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
695 vfs_setpublicfs(NULL
, NULL
, NULL
);
697 vfs_msync(mp
, MNT_WAIT
);
698 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
699 mp
->mnt_flag
&=~ MNT_ASYNC
;
702 * If this filesystem isn't aliasing other filesystems,
703 * try to invalidate any remaining namecache entries and
704 * check the count afterwords.
706 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
707 cache_lock(&mp
->mnt_ncmountpt
);
708 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
709 cache_unlock(&mp
->mnt_ncmountpt
);
711 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
712 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
713 allproc_scan(&unmount_allproc_cb
, mp
);
716 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
717 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
719 if ((flags
& MNT_FORCE
) == 0) {
721 mount_warning(mp
, "Cannot unmount: "
727 mount_warning(mp
, "Forced unmount: "
738 * Decomission our special mnt_syncer vnode. This also stops
739 * the vnlru code. If we are unable to unmount we recommission
742 * Then sync the filesystem.
744 if ((vp
= mp
->mnt_syncer
) != NULL
) {
745 mp
->mnt_syncer
= NULL
;
746 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
749 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
750 VFS_SYNC(mp
, MNT_WAIT
);
753 * nchandle records ref the mount structure. Expect a count of 1
754 * (our mount->mnt_ncmountpt).
756 * Scans can get temporary refs on a mountpoint (thought really
757 * heavy duty stuff like cache_findmount() do not).
759 for (retry
= 0; retry
< 10 && mp
->mnt_refs
!= 1; ++retry
) {
760 cache_unmounting(mp
);
761 tsleep(&mp
->mnt_refs
, 0, "mntbsy", hz
/ 10 + 1);
763 if (mp
->mnt_refs
!= 1) {
764 if ((flags
& MNT_FORCE
) == 0) {
765 mount_warning(mp
, "Cannot unmount: "
766 "%d mount refs still present",
770 mount_warning(mp
, "Forced unmount: "
771 "%d mount refs still present",
778 * So far so good, sync the filesystem once more and
779 * call the VFS unmount code if the sync succeeds.
782 if (mp
->mnt_flag
& MNT_RDONLY
) {
783 error
= VFS_UNMOUNT(mp
, flags
);
785 error
= VFS_SYNC(mp
, MNT_WAIT
);
787 (error
== EOPNOTSUPP
) || /* No sync */
788 (flags
& MNT_FORCE
)) {
789 error
= VFS_UNMOUNT(mp
, flags
);
795 * If an error occurred we can still recover, restoring the
796 * syncer vnode and misc flags.
799 if (mp
->mnt_syncer
== NULL
)
800 vfs_allocate_syncvnode(mp
);
801 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
802 mp
->mnt_flag
|= async_flag
;
803 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
804 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
805 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
811 * Clean up any journals still associated with the mount after
812 * filesystem activity has ceased.
814 journal_remove_all_journals(mp
,
815 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
817 mountlist_remove(mp
);
820 * Remove any installed vnode ops here so the individual VFSs don't
823 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
824 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
825 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
826 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
827 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
829 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
830 nch
= mp
->mnt_ncmountpt
;
831 cache_zero(&mp
->mnt_ncmountpt
);
832 cache_clrmountpt(&nch
);
835 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
836 cache_unmounting(mp
);
837 nch
= mp
->mnt_ncmounton
;
838 cache_zero(&mp
->mnt_ncmounton
);
839 cache_clrmountpt(&nch
);
843 mp
->mnt_vfc
->vfc_refcount
--;
844 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
845 panic("unmount: dangling vnode");
846 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
847 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
848 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
853 * If we reach here and freeok != 0 we must free the mount.
854 * If refs > 1 cycle and wait, just in case someone tried
855 * to busy the mount after we decided to do the unmount.
858 while (mp
->mnt_refs
> 1) {
859 cache_unmounting(mp
);
861 tsleep(&mp
->mnt_refs
, 0, "umntrwait", hz
/ 10 + 1);
863 lwkt_reltoken(&mp
->mnt_token
);
868 KNOTE(&fs_klist
, VQ_UNMOUNT
);
871 lwkt_reltoken(&mp
->mnt_token
);
877 mount_warning(struct mount
*mp
, const char *ctl
, ...)
884 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, NULL
,
885 &ptr
, &buf
, 0) == 0) {
886 kprintf("unmount(%s): ", ptr
);
891 kprintf("unmount(%p", mp
);
892 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
893 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
902 * Shim cache_fullpath() to handle the case where a process is chrooted into
903 * a subdirectory of a mount. In this case if the root mount matches the
904 * process root directory's mount we have to specify the process's root
905 * directory instead of the mount point, because the mount point might
906 * be above the root directory.
910 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
912 struct nchandle
*nch
;
914 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
915 nch
= &p
->p_fd
->fd_nrdir
;
917 nch
= &mp
->mnt_ncmountpt
;
918 return(cache_fullpath(p
, nch
, NULL
, rb
, fb
, 0));
922 * Sync each mounted filesystem.
926 static int syncprt
= 0;
927 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
930 static int sync_callback(struct mount
*mp
, void *data
);
933 sys_sync(struct sync_args
*uap
)
935 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
941 sync_callback(struct mount
*mp
, void *data __unused
)
945 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
946 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
947 mp
->mnt_flag
&= ~MNT_ASYNC
;
948 vfs_msync(mp
, MNT_NOWAIT
);
949 VFS_SYNC(mp
, MNT_NOWAIT
);
950 mp
->mnt_flag
|= asyncflag
;
955 /* XXX PRISON: could be per prison flag */
956 static int prison_quotas
;
958 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
962 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
964 * Change filesystem quotas.
969 sys_quotactl(struct quotactl_args
*uap
)
971 struct nlookupdata nd
;
978 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
983 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
985 error
= nlookup(&nd
);
987 mp
= nd
.nl_nch
.mount
;
988 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
989 uap
->arg
, nd
.nl_cred
);
998 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
999 * void *buf, int buflen)
1001 * This function operates on a mount point and executes the specified
1002 * operation using the specified control data, and possibly returns data.
1004 * The actual number of bytes stored in the result buffer is returned, 0
1005 * if none, otherwise an error is returned.
1010 sys_mountctl(struct mountctl_args
*uap
)
1012 struct thread
*td
= curthread
;
1013 struct proc
*p
= td
->td_proc
;
1021 * Sanity and permissions checks. We must be root.
1024 if (td
->td_ucred
->cr_prison
!= NULL
)
1026 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
1027 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
1031 * Argument length checks
1033 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
1035 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
1037 if (uap
->path
== NULL
)
1041 * Allocate the necessary buffers and copyin data
1043 path
= objcache_get(namei_oc
, M_WAITOK
);
1044 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1049 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1050 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1055 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1058 * Validate the descriptor
1061 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1071 * Execute the internal kernel function and clean up.
1074 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1078 if (error
== 0 && uap
->sysmsg_result
> 0)
1079 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1082 objcache_put(namei_oc
, path
);
1091 * Execute a mount control operation by resolving the path to a mount point
1092 * and calling vop_mountctl().
1094 * Use the mount point from the nch instead of the vnode so nullfs mounts
1095 * can properly spike the VOP.
1098 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1099 const void *ctl
, int ctllen
,
1100 void *buf
, int buflen
, int *res
)
1104 struct nlookupdata nd
;
1109 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1111 error
= nlookup(&nd
);
1113 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1114 mp
= nd
.nl_nch
.mount
;
1121 * Must be the root of the filesystem
1123 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1127 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1134 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1136 struct thread
*td
= curthread
;
1137 struct proc
*p
= td
->td_proc
;
1140 char *fullpath
, *freepath
;
1143 if ((error
= nlookup(nd
)) != 0)
1145 mp
= nd
->nl_nch
.mount
;
1147 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1150 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1153 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1154 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1155 kfree(freepath
, M_TEMP
);
1157 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1158 bcopy(sp
, buf
, sizeof(*buf
));
1159 /* Only root should have access to the fsid's. */
1160 if (priv_check(td
, PRIV_ROOT
))
1161 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1166 * statfs_args(char *path, struct statfs *buf)
1168 * Get filesystem statistics.
1171 sys_statfs(struct statfs_args
*uap
)
1173 struct nlookupdata nd
;
1177 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1179 error
= kern_statfs(&nd
, &buf
);
1182 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1187 kern_fstatfs(int fd
, struct statfs
*buf
)
1189 struct thread
*td
= curthread
;
1190 struct proc
*p
= td
->td_proc
;
1194 char *fullpath
, *freepath
;
1198 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1202 * Try to use mount info from any overlays rather than the
1203 * mount info for the underlying vnode, otherwise we will
1204 * fail when operating on null-mounted paths inside a chroot.
1206 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1207 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1212 if (fp
->f_cred
== NULL
) {
1217 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1220 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1222 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1223 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1224 kfree(freepath
, M_TEMP
);
1226 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1227 bcopy(sp
, buf
, sizeof(*buf
));
1229 /* Only root should have access to the fsid's. */
1230 if (priv_check(td
, PRIV_ROOT
))
1231 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1239 * fstatfs_args(int fd, struct statfs *buf)
1241 * Get filesystem statistics.
1244 sys_fstatfs(struct fstatfs_args
*uap
)
1249 error
= kern_fstatfs(uap
->fd
, &buf
);
1252 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1257 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1263 if ((error
= nlookup(nd
)) != 0)
1265 mp
= nd
->nl_nch
.mount
;
1266 sp
= &mp
->mnt_vstat
;
1267 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1271 if (mp
->mnt_flag
& MNT_RDONLY
)
1272 sp
->f_flag
|= ST_RDONLY
;
1273 if (mp
->mnt_flag
& MNT_NOSUID
)
1274 sp
->f_flag
|= ST_NOSUID
;
1275 bcopy(sp
, buf
, sizeof(*buf
));
1280 * statfs_args(char *path, struct statfs *buf)
1282 * Get filesystem statistics.
1285 sys_statvfs(struct statvfs_args
*uap
)
1287 struct nlookupdata nd
;
1291 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1293 error
= kern_statvfs(&nd
, &buf
);
1296 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1301 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1303 struct thread
*td
= curthread
;
1304 struct proc
*p
= td
->td_proc
;
1311 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1313 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1314 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1319 if (fp
->f_cred
== NULL
) {
1323 sp
= &mp
->mnt_vstat
;
1324 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1328 if (mp
->mnt_flag
& MNT_RDONLY
)
1329 sp
->f_flag
|= ST_RDONLY
;
1330 if (mp
->mnt_flag
& MNT_NOSUID
)
1331 sp
->f_flag
|= ST_NOSUID
;
1333 bcopy(sp
, buf
, sizeof(*buf
));
1341 * fstatfs_args(int fd, struct statfs *buf)
1343 * Get filesystem statistics.
1346 sys_fstatvfs(struct fstatvfs_args
*uap
)
1351 error
= kern_fstatvfs(uap
->fd
, &buf
);
1354 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1359 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1361 * Get statistics on all filesystems.
1364 struct getfsstat_info
{
1365 struct statfs
*sfsp
;
1373 static int getfsstat_callback(struct mount
*, void *);
1376 sys_getfsstat(struct getfsstat_args
*uap
)
1378 struct thread
*td
= curthread
;
1379 struct getfsstat_info info
;
1381 bzero(&info
, sizeof(info
));
1383 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1384 info
.sfsp
= uap
->buf
;
1386 info
.flags
= uap
->flags
;
1389 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1390 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1391 uap
->sysmsg_result
= info
.maxcount
;
1393 uap
->sysmsg_result
= info
.count
;
1394 return (info
.error
);
1398 getfsstat_callback(struct mount
*mp
, void *data
)
1400 struct getfsstat_info
*info
= data
;
1406 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1407 if (info
->td
->td_proc
&&
1408 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1414 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1415 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1416 * overrides MNT_WAIT.
1418 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1419 (info
->flags
& MNT_WAIT
)) &&
1420 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1423 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1425 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1427 info
->error
= error
;
1430 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1431 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1432 kfree(freepath
, M_TEMP
);
1434 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1436 info
->error
= error
;
1446 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1447 long bufsize, int flags)
1449 * Get statistics on all filesystems.
1452 struct getvfsstat_info
{
1453 struct statfs
*sfsp
;
1454 struct statvfs
*vsfsp
;
1462 static int getvfsstat_callback(struct mount
*, void *);
1465 sys_getvfsstat(struct getvfsstat_args
*uap
)
1467 struct thread
*td
= curthread
;
1468 struct getvfsstat_info info
;
1470 bzero(&info
, sizeof(info
));
1472 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1473 info
.sfsp
= uap
->buf
;
1474 info
.vsfsp
= uap
->vbuf
;
1476 info
.flags
= uap
->flags
;
1479 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1480 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1481 uap
->sysmsg_result
= info
.maxcount
;
1483 uap
->sysmsg_result
= info
.count
;
1484 return (info
.error
);
1488 getvfsstat_callback(struct mount
*mp
, void *data
)
1490 struct getvfsstat_info
*info
= data
;
1492 struct statvfs
*vsp
;
1497 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1498 if (info
->td
->td_proc
&&
1499 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1503 vsp
= &mp
->mnt_vstat
;
1506 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1507 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1508 * overrides MNT_WAIT.
1510 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1511 (info
->flags
& MNT_WAIT
)) &&
1512 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1515 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1517 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1518 (info
->flags
& MNT_WAIT
)) &&
1519 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1523 if (mp
->mnt_flag
& MNT_RDONLY
)
1524 vsp
->f_flag
|= ST_RDONLY
;
1525 if (mp
->mnt_flag
& MNT_NOSUID
)
1526 vsp
->f_flag
|= ST_NOSUID
;
1528 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1530 info
->error
= error
;
1533 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1534 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1535 kfree(freepath
, M_TEMP
);
1537 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1539 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1541 info
->error
= error
;
1553 * fchdir_args(int fd)
1555 * Change current working directory to a given file descriptor.
1558 sys_fchdir(struct fchdir_args
*uap
)
1560 struct thread
*td
= curthread
;
1561 struct proc
*p
= td
->td_proc
;
1562 struct filedesc
*fdp
= p
->p_fd
;
1563 struct vnode
*vp
, *ovp
;
1566 struct nchandle nch
, onch
, tnch
;
1569 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1571 lwkt_gettoken(&p
->p_token
);
1572 vp
= (struct vnode
*)fp
->f_data
;
1574 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1575 if (fp
->f_nchandle
.ncp
== NULL
)
1578 error
= checkvp_chdir(vp
, td
);
1583 cache_copy(&fp
->f_nchandle
, &nch
);
1586 * If the ncp has become a mount point, traverse through
1590 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1591 (mp
= cache_findmount(&nch
)) != NULL
1593 error
= nlookup_mp(mp
, &tnch
);
1595 cache_unlock(&tnch
); /* leave ref intact */
1597 vp
= tnch
.ncp
->nc_vp
;
1598 error
= vget(vp
, LK_SHARED
);
1599 KKASSERT(error
== 0);
1603 cache_dropmount(mp
);
1607 onch
= fdp
->fd_ncdir
;
1608 vn_unlock(vp
); /* leave ref intact */
1610 fdp
->fd_ncdir
= nch
;
1619 lwkt_reltoken(&p
->p_token
);
1624 kern_chdir(struct nlookupdata
*nd
)
1626 struct thread
*td
= curthread
;
1627 struct proc
*p
= td
->td_proc
;
1628 struct filedesc
*fdp
= p
->p_fd
;
1629 struct vnode
*vp
, *ovp
;
1630 struct nchandle onch
;
1633 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1634 if ((error
= nlookup(nd
)) != 0)
1636 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1638 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1641 lwkt_gettoken(&p
->p_token
);
1642 error
= checkvp_chdir(vp
, td
);
1646 onch
= fdp
->fd_ncdir
;
1647 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1648 fdp
->fd_ncdir
= nd
->nl_nch
;
1652 cache_zero(&nd
->nl_nch
);
1656 lwkt_reltoken(&p
->p_token
);
1661 * chdir_args(char *path)
1663 * Change current working directory (``.'').
1666 sys_chdir(struct chdir_args
*uap
)
1668 struct nlookupdata nd
;
1671 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1673 error
= kern_chdir(&nd
);
1679 * Helper function for raised chroot(2) security function: Refuse if
1680 * any filedescriptors are open directories.
1683 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1690 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1691 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1693 vp
= (struct vnode
*)fp
->f_data
;
1694 if (vp
->v_type
!= VDIR
) {
1705 * This sysctl determines if we will allow a process to chroot(2) if it
1706 * has a directory open:
1707 * 0: disallowed for all processes.
1708 * 1: allowed for processes that were not already chroot(2)'ed.
1709 * 2: allowed for all processes.
1712 static int chroot_allow_open_directories
= 1;
1714 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1715 &chroot_allow_open_directories
, 0, "");
1718 * chroot to the specified namecache entry. We obtain the vp from the
1719 * namecache data. The passed ncp must be locked and referenced and will
1720 * remain locked and referenced on return.
1723 kern_chroot(struct nchandle
*nch
)
1725 struct thread
*td
= curthread
;
1726 struct proc
*p
= td
->td_proc
;
1727 struct filedesc
*fdp
= p
->p_fd
;
1732 * Only privileged user can chroot
1734 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1739 * Disallow open directory descriptors (fchdir() breakouts).
1741 if (chroot_allow_open_directories
== 0 ||
1742 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1743 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1746 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1749 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1753 * Check the validity of vp as a directory to change to and
1754 * associate it with rdir/jdir.
1756 error
= checkvp_chdir(vp
, td
);
1757 vn_unlock(vp
); /* leave reference intact */
1759 vrele(fdp
->fd_rdir
);
1760 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1761 cache_drop(&fdp
->fd_nrdir
);
1762 cache_copy(nch
, &fdp
->fd_nrdir
);
1763 if (fdp
->fd_jdir
== NULL
) {
1766 cache_copy(nch
, &fdp
->fd_njdir
);
1775 * chroot_args(char *path)
1777 * Change notion of root (``/'') directory.
1780 sys_chroot(struct chroot_args
*uap
)
1782 struct thread
*td __debugvar
= curthread
;
1783 struct nlookupdata nd
;
1786 KKASSERT(td
->td_proc
);
1787 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1789 nd
.nl_flags
|= NLC_EXEC
;
1790 error
= nlookup(&nd
);
1792 error
= kern_chroot(&nd
.nl_nch
);
1799 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1801 struct thread
*td
= curthread
;
1802 struct nlookupdata nd
;
1803 struct nchandle
*nch
;
1807 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1811 error
= nlookup(&nd
);
1817 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1821 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1826 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1829 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1831 vfs_cache_setroot(vp
, cache_hold(nch
));
1841 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1842 * determine whether it is legal to chdir to the vnode. The vnode's state
1843 * is not changed by this call.
1846 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1850 if (vp
->v_type
!= VDIR
)
1853 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1858 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1860 struct thread
*td
= curthread
;
1861 struct proc
*p
= td
->td_proc
;
1862 struct lwp
*lp
= td
->td_lwp
;
1863 struct filedesc
*fdp
= p
->p_fd
;
1868 int type
, indx
, error
= 0;
1871 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1873 flags
= FFLAGS(oflags
);
1874 error
= falloc(lp
, &nfp
, NULL
);
1878 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1881 * XXX p_dupfd is a real mess. It allows a device to return a
1882 * file descriptor to be duplicated rather then doing the open
1888 * Call vn_open() to do the lookup and assign the vnode to the
1889 * file pointer. vn_open() does not change the ref count on fp
1890 * and the vnode, on success, will be inherited by the file pointer
1893 * Request a shared lock on the vnode if possible.
1895 nd
->nl_flags
|= NLC_LOCKVP
;
1896 if ((flags
& (O_CREAT
|O_TRUNC
)) == 0)
1897 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1899 error
= vn_open(nd
, fp
, flags
, cmode
);
1904 * handle special fdopen() case. bleh. dupfdopen() is
1905 * responsible for dropping the old contents of ofiles[indx]
1908 * Note that fsetfd() will add a ref to fp which represents
1909 * the fd_files[] assignment. We must still drop our
1912 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1913 if (fdalloc(p
, 0, &indx
) == 0) {
1914 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1917 fdrop(fp
); /* our ref */
1920 fsetfd(fdp
, NULL
, indx
);
1923 fdrop(fp
); /* our ref */
1924 if (error
== ERESTART
)
1930 * ref the vnode for ourselves so it can't be ripped out from under
1931 * is. XXX need an ND flag to request that the vnode be returned
1934 * Reserve a file descriptor but do not assign it until the open
1937 vp
= (struct vnode
*)fp
->f_data
;
1939 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1946 * If no error occurs the vp will have been assigned to the file
1951 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1952 lf
.l_whence
= SEEK_SET
;
1955 if (flags
& O_EXLOCK
)
1956 lf
.l_type
= F_WRLCK
;
1958 lf
.l_type
= F_RDLCK
;
1959 if (flags
& FNONBLOCK
)
1964 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1966 * lock request failed. Clean up the reserved
1970 fsetfd(fdp
, NULL
, indx
);
1974 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
1978 * Assert that all regular file vnodes were created with a object.
1980 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1981 ("open: regular file has no backing object after vn_open"));
1987 * release our private reference, leaving the one associated with the
1988 * descriptor table intact.
1990 if (oflags
& O_CLOEXEC
)
1991 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
1992 fsetfd(fdp
, fp
, indx
);
1999 * open_args(char *path, int flags, int mode)
2001 * Check permissions, allocate an open file structure,
2002 * and call the device open routine if any.
2005 sys_open(struct open_args
*uap
)
2007 struct nlookupdata nd
;
2010 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2012 error
= kern_open(&nd
, uap
->flags
,
2013 uap
->mode
, &uap
->sysmsg_result
);
2020 * openat_args(int fd, char *path, int flags, int mode)
2023 sys_openat(struct openat_args
*uap
)
2025 struct nlookupdata nd
;
2029 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2031 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2032 &uap
->sysmsg_result
);
2034 nlookup_done_at(&nd
, fp
);
2039 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2041 struct thread
*td
= curthread
;
2042 struct proc
*p
= td
->td_proc
;
2051 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2052 vattr
.va_rmajor
= rmajor
;
2053 vattr
.va_rminor
= rminor
;
2055 switch (mode
& S_IFMT
) {
2056 case S_IFMT
: /* used by badsect to flag bad sectors */
2057 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2058 vattr
.va_type
= VBAD
;
2061 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2062 vattr
.va_type
= VCHR
;
2065 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2066 vattr
.va_type
= VBLK
;
2069 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2072 case S_IFDIR
: /* special directories support for HAMMER */
2073 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2074 vattr
.va_type
= VDIR
;
2085 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2086 if ((error
= nlookup(nd
)) != 0)
2088 if (nd
->nl_nch
.ncp
->nc_vp
)
2090 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2094 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2095 nd
->nl_cred
, NAMEI_CREATE
);
2098 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2099 &vp
, nd
->nl_cred
, &vattr
);
2107 * mknod_args(char *path, int mode, int dev)
2109 * Create a special file.
2112 sys_mknod(struct mknod_args
*uap
)
2114 struct nlookupdata nd
;
2117 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2119 error
= kern_mknod(&nd
, uap
->mode
,
2120 umajor(uap
->dev
), uminor(uap
->dev
));
2127 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2129 * Create a special file. The path is relative to the directory associated
2133 sys_mknodat(struct mknodat_args
*uap
)
2135 struct nlookupdata nd
;
2139 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2141 error
= kern_mknod(&nd
, uap
->mode
,
2142 umajor(uap
->dev
), uminor(uap
->dev
));
2144 nlookup_done_at(&nd
, fp
);
2149 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2151 struct thread
*td
= curthread
;
2152 struct proc
*p
= td
->td_proc
;
2159 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2160 if ((error
= nlookup(nd
)) != 0)
2162 if (nd
->nl_nch
.ncp
->nc_vp
)
2164 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2168 vattr
.va_type
= VFIFO
;
2169 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2171 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2178 * mkfifo_args(char *path, int mode)
2180 * Create a named pipe.
2183 sys_mkfifo(struct mkfifo_args
*uap
)
2185 struct nlookupdata nd
;
2188 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2190 error
= kern_mkfifo(&nd
, uap
->mode
);
2196 * mkfifoat_args(int fd, char *path, mode_t mode)
2198 * Create a named pipe. The path is relative to the directory associated
2202 sys_mkfifoat(struct mkfifoat_args
*uap
)
2204 struct nlookupdata nd
;
2208 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2210 error
= kern_mkfifo(&nd
, uap
->mode
);
2211 nlookup_done_at(&nd
, fp
);
2215 static int hardlink_check_uid
= 0;
2216 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2217 &hardlink_check_uid
, 0,
2218 "Unprivileged processes cannot create hard links to files owned by other "
2220 static int hardlink_check_gid
= 0;
2221 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2222 &hardlink_check_gid
, 0,
2223 "Unprivileged processes cannot create hard links to files owned by other "
2227 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2233 * Shortcut if disabled
2235 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2239 * Privileged user can always hardlink
2241 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2245 * Otherwise only if the originating file is owned by the
2246 * same user or group. Note that any group is allowed if
2247 * the file is owned by the caller.
2249 error
= VOP_GETATTR(vp
, &va
);
2253 if (hardlink_check_uid
) {
2254 if (cred
->cr_uid
!= va
.va_uid
)
2258 if (hardlink_check_gid
) {
2259 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2267 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2269 struct thread
*td
= curthread
;
2274 * Lookup the source and obtained a locked vnode.
2276 * You may only hardlink a file which you have write permission
2277 * on or which you own.
2279 * XXX relookup on vget failure / race ?
2282 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2283 if ((error
= nlookup(nd
)) != 0)
2285 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2286 KKASSERT(vp
!= NULL
);
2287 if (vp
->v_type
== VDIR
)
2288 return (EPERM
); /* POSIX */
2289 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2291 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2295 * Unlock the source so we can lookup the target without deadlocking
2296 * (XXX vp is locked already, possible other deadlock?). The target
2299 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2300 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2301 cache_unlock(&nd
->nl_nch
);
2304 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2305 if ((error
= nlookup(linknd
)) != 0) {
2309 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2313 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2320 * Finally run the new API VOP.
2322 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2324 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2325 vp
, linknd
->nl_cred
);
2332 * link_args(char *path, char *link)
2334 * Make a hard file link.
2337 sys_link(struct link_args
*uap
)
2339 struct nlookupdata nd
, linknd
;
2342 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2344 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2346 error
= kern_link(&nd
, &linknd
);
2347 nlookup_done(&linknd
);
2354 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2356 * Make a hard file link. The path1 argument is relative to the directory
2357 * associated with fd1, and similarly the path2 argument is relative to
2358 * the directory associated with fd2.
2361 sys_linkat(struct linkat_args
*uap
)
2363 struct nlookupdata nd
, linknd
;
2364 struct file
*fp1
, *fp2
;
2367 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2368 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2370 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2371 uap
->path2
, UIO_USERSPACE
, 0);
2373 error
= kern_link(&nd
, &linknd
);
2374 nlookup_done_at(&linknd
, fp2
);
2376 nlookup_done_at(&nd
, fp1
);
2381 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2389 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2390 if ((error
= nlookup(nd
)) != 0)
2392 if (nd
->nl_nch
.ncp
->nc_vp
)
2394 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2398 vattr
.va_mode
= mode
;
2399 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2406 * symlink(char *path, char *link)
2408 * Make a symbolic link.
2411 sys_symlink(struct symlink_args
*uap
)
2413 struct thread
*td
= curthread
;
2414 struct nlookupdata nd
;
2419 path
= objcache_get(namei_oc
, M_WAITOK
);
2420 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2422 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2424 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2425 error
= kern_symlink(&nd
, path
, mode
);
2429 objcache_put(namei_oc
, path
);
2434 * symlinkat_args(char *path1, int fd, char *path2)
2436 * Make a symbolic link. The path2 argument is relative to the directory
2437 * associated with fd.
2440 sys_symlinkat(struct symlinkat_args
*uap
)
2442 struct thread
*td
= curthread
;
2443 struct nlookupdata nd
;
2449 path1
= objcache_get(namei_oc
, M_WAITOK
);
2450 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2452 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2455 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2456 error
= kern_symlink(&nd
, path1
, mode
);
2458 nlookup_done_at(&nd
, fp
);
2460 objcache_put(namei_oc
, path1
);
2465 * undelete_args(char *path)
2467 * Delete a whiteout from the filesystem.
2470 sys_undelete(struct undelete_args
*uap
)
2472 struct nlookupdata nd
;
2475 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2477 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2479 error
= nlookup(&nd
);
2481 error
= ncp_writechk(&nd
.nl_nch
);
2483 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2491 kern_unlink(struct nlookupdata
*nd
)
2496 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2497 if ((error
= nlookup(nd
)) != 0)
2499 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2501 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2506 * unlink_args(char *path)
2508 * Delete a name from the filesystem.
2511 sys_unlink(struct unlink_args
*uap
)
2513 struct nlookupdata nd
;
2516 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2518 error
= kern_unlink(&nd
);
2525 * unlinkat_args(int fd, char *path, int flags)
2527 * Delete the file or directory entry pointed to by fd/path.
2530 sys_unlinkat(struct unlinkat_args
*uap
)
2532 struct nlookupdata nd
;
2536 if (uap
->flags
& ~AT_REMOVEDIR
)
2539 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2541 if (uap
->flags
& AT_REMOVEDIR
)
2542 error
= kern_rmdir(&nd
);
2544 error
= kern_unlink(&nd
);
2546 nlookup_done_at(&nd
, fp
);
2551 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2553 struct thread
*td
= curthread
;
2554 struct proc
*p
= td
->td_proc
;
2561 fp
= holdfp(p
->p_fd
, fd
, -1);
2564 if (fp
->f_type
!= DTYPE_VNODE
) {
2568 vp
= (struct vnode
*)fp
->f_data
;
2572 spin_lock(&fp
->f_spin
);
2573 new_offset
= fp
->f_offset
+ offset
;
2577 error
= VOP_GETATTR(vp
, &vattr
);
2578 spin_lock(&fp
->f_spin
);
2579 new_offset
= offset
+ vattr
.va_size
;
2582 new_offset
= offset
;
2584 spin_lock(&fp
->f_spin
);
2589 spin_lock(&fp
->f_spin
);
2594 * Validate the seek position. Negative offsets are not allowed
2595 * for regular files or directories.
2597 * Normally we would also not want to allow negative offsets for
2598 * character and block-special devices. However kvm addresses
2599 * on 64 bit architectures might appear to be negative and must
2603 if (new_offset
< 0 &&
2604 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2607 fp
->f_offset
= new_offset
;
2610 *res
= fp
->f_offset
;
2611 spin_unlock(&fp
->f_spin
);
2618 * lseek_args(int fd, int pad, off_t offset, int whence)
2620 * Reposition read/write file offset.
2623 sys_lseek(struct lseek_args
*uap
)
2627 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2628 &uap
->sysmsg_offset
);
2634 * Check if current process can access given file. amode is a bitmask of *_OK
2635 * access bits. flags is a bitmask of AT_* flags.
2638 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2643 if (flags
& ~AT_EACCESS
)
2645 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2646 if ((error
= nlookup(nd
)) != 0)
2649 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2653 /* Flags == 0 means only check for existence. */
2662 if ((mode
& VWRITE
) == 0 ||
2663 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2664 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2667 * If the file handle is stale we have to re-resolve the
2668 * entry with the ncp held exclusively. This is a hack
2671 if (error
== ESTALE
) {
2673 cache_unlock(&nd
->nl_nch
);
2674 cache_lock(&nd
->nl_nch
);
2675 cache_setunresolved(&nd
->nl_nch
);
2676 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2689 * access_args(char *path, int flags)
2691 * Check access permissions.
2694 sys_access(struct access_args
*uap
)
2696 struct nlookupdata nd
;
2699 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2701 error
= kern_access(&nd
, uap
->flags
, 0);
2708 * eaccess_args(char *path, int flags)
2710 * Check access permissions.
2713 sys_eaccess(struct eaccess_args
*uap
)
2715 struct nlookupdata nd
;
2718 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2720 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2727 * faccessat_args(int fd, char *path, int amode, int flags)
2729 * Check access permissions.
2732 sys_faccessat(struct faccessat_args
*uap
)
2734 struct nlookupdata nd
;
2738 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2741 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2742 nlookup_done_at(&nd
, fp
);
2747 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2752 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2753 if ((error
= nlookup(nd
)) != 0)
2756 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2759 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2761 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2764 * If the file handle is stale we have to re-resolve the
2765 * entry with the ncp held exclusively. This is a hack
2768 if (error
== ESTALE
) {
2770 cache_unlock(&nd
->nl_nch
);
2771 cache_lock(&nd
->nl_nch
);
2772 cache_setunresolved(&nd
->nl_nch
);
2773 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2783 * stat_args(char *path, struct stat *ub)
2785 * Get file status; this version follows links.
2788 sys_stat(struct stat_args
*uap
)
2790 struct nlookupdata nd
;
2794 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2796 error
= kern_stat(&nd
, &st
);
2798 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2805 * lstat_args(char *path, struct stat *ub)
2807 * Get file status; this version does not follow links.
2810 sys_lstat(struct lstat_args
*uap
)
2812 struct nlookupdata nd
;
2816 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2818 error
= kern_stat(&nd
, &st
);
2820 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2827 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2829 * Get status of file pointed to by fd/path.
2832 sys_fstatat(struct fstatat_args
*uap
)
2834 struct nlookupdata nd
;
2840 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2843 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2845 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2846 UIO_USERSPACE
, flags
);
2848 error
= kern_stat(&nd
, &st
);
2850 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2852 nlookup_done_at(&nd
, fp
);
2857 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2859 struct nlookupdata nd
;
2864 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2866 error
= nlookup(&nd
);
2868 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2871 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2878 * pathconf_Args(char *path, int name)
2880 * Get configurable pathname variables.
2883 sys_pathconf(struct pathconf_args
*uap
)
2885 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2890 * lpathconf_Args(char *path, int name)
2892 * Get configurable pathname variables, but don't follow symlinks.
2895 sys_lpathconf(struct lpathconf_args
*uap
)
2897 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2902 * kern_readlink isn't properly split yet. There is a copyin burried
2903 * in VOP_READLINK().
2906 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2908 struct thread
*td
= curthread
;
2914 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2915 if ((error
= nlookup(nd
)) != 0)
2917 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2920 if (vp
->v_type
!= VLNK
) {
2923 aiov
.iov_base
= buf
;
2924 aiov
.iov_len
= count
;
2925 auio
.uio_iov
= &aiov
;
2926 auio
.uio_iovcnt
= 1;
2927 auio
.uio_offset
= 0;
2928 auio
.uio_rw
= UIO_READ
;
2929 auio
.uio_segflg
= UIO_USERSPACE
;
2931 auio
.uio_resid
= count
;
2932 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
2935 *res
= count
- auio
.uio_resid
;
2940 * readlink_args(char *path, char *buf, int count)
2942 * Return target name of a symbolic link.
2945 sys_readlink(struct readlink_args
*uap
)
2947 struct nlookupdata nd
;
2950 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2952 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2953 &uap
->sysmsg_result
);
2960 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2962 * Return target name of a symbolic link. The path is relative to the
2963 * directory associated with fd.
2966 sys_readlinkat(struct readlinkat_args
*uap
)
2968 struct nlookupdata nd
;
2972 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2974 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
2975 &uap
->sysmsg_result
);
2977 nlookup_done_at(&nd
, fp
);
2982 setfflags(struct vnode
*vp
, int flags
)
2984 struct thread
*td
= curthread
;
2989 * Prevent non-root users from setting flags on devices. When
2990 * a device is reused, users can retain ownership of the device
2991 * if they are allowed to set flags and programs assume that
2992 * chown can't fail when done as root.
2994 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2995 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
2999 * note: vget is required for any operation that might mod the vnode
3000 * so VINACTIVE is properly cleared.
3002 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3004 vattr
.va_flags
= flags
;
3005 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3012 * chflags(char *path, int flags)
3014 * Change flags of a file given a path name.
3017 sys_chflags(struct chflags_args
*uap
)
3019 struct nlookupdata nd
;
3024 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3026 error
= nlookup(&nd
);
3028 error
= ncp_writechk(&nd
.nl_nch
);
3030 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3033 error
= setfflags(vp
, uap
->flags
);
3040 * lchflags(char *path, int flags)
3042 * Change flags of a file given a path name, but don't follow symlinks.
3045 sys_lchflags(struct lchflags_args
*uap
)
3047 struct nlookupdata nd
;
3052 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3054 error
= nlookup(&nd
);
3056 error
= ncp_writechk(&nd
.nl_nch
);
3058 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3061 error
= setfflags(vp
, uap
->flags
);
3068 * fchflags_args(int fd, int flags)
3070 * Change flags of a file given a file descriptor.
3073 sys_fchflags(struct fchflags_args
*uap
)
3075 struct thread
*td
= curthread
;
3076 struct proc
*p
= td
->td_proc
;
3080 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3082 if (fp
->f_nchandle
.ncp
)
3083 error
= ncp_writechk(&fp
->f_nchandle
);
3085 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3091 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3092 * change flags given a pathname relative to a filedescriptor
3094 int sys_chflagsat(struct chflagsat_args
*uap
)
3096 struct nlookupdata nd
;
3102 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3105 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3108 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3110 error
= nlookup(&nd
);
3112 error
= ncp_writechk(&nd
.nl_nch
);
3114 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3115 nlookup_done_at(&nd
, fp
);
3117 error
= setfflags(vp
, uap
->flags
);
3125 setfmode(struct vnode
*vp
, int mode
)
3127 struct thread
*td
= curthread
;
3132 * note: vget is required for any operation that might mod the vnode
3133 * so VINACTIVE is properly cleared.
3135 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3137 vattr
.va_mode
= mode
& ALLPERMS
;
3138 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3145 kern_chmod(struct nlookupdata
*nd
, int mode
)
3150 if ((error
= nlookup(nd
)) != 0)
3152 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3154 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3155 error
= setfmode(vp
, mode
);
3161 * chmod_args(char *path, int mode)
3163 * Change mode of a file given path name.
3166 sys_chmod(struct chmod_args
*uap
)
3168 struct nlookupdata nd
;
3171 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3173 error
= kern_chmod(&nd
, uap
->mode
);
3179 * lchmod_args(char *path, int mode)
3181 * Change mode of a file given path name (don't follow links.)
3184 sys_lchmod(struct lchmod_args
*uap
)
3186 struct nlookupdata nd
;
3189 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3191 error
= kern_chmod(&nd
, uap
->mode
);
3197 * fchmod_args(int fd, int mode)
3199 * Change mode of a file given a file descriptor.
3202 sys_fchmod(struct fchmod_args
*uap
)
3204 struct thread
*td
= curthread
;
3205 struct proc
*p
= td
->td_proc
;
3209 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3211 if (fp
->f_nchandle
.ncp
)
3212 error
= ncp_writechk(&fp
->f_nchandle
);
3214 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3220 * fchmodat_args(char *path, int mode)
3222 * Change mode of a file pointed to by fd/path.
3225 sys_fchmodat(struct fchmodat_args
*uap
)
3227 struct nlookupdata nd
;
3232 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3234 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3236 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3237 UIO_USERSPACE
, flags
);
3239 error
= kern_chmod(&nd
, uap
->mode
);
3240 nlookup_done_at(&nd
, fp
);
3245 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3247 struct thread
*td
= curthread
;
3255 * note: vget is required for any operation that might mod the vnode
3256 * so VINACTIVE is properly cleared.
3258 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3259 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3261 o_uid
= vattr
.va_uid
;
3262 o_gid
= vattr
.va_gid
;
3263 size
= vattr
.va_size
;
3268 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3277 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3278 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3285 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3290 if ((error
= nlookup(nd
)) != 0)
3292 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3294 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3295 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3301 * chown(char *path, int uid, int gid)
3303 * Set ownership given a path name.
3306 sys_chown(struct chown_args
*uap
)
3308 struct nlookupdata nd
;
3311 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3313 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3319 * lchown_args(char *path, int uid, int gid)
3321 * Set ownership given a path name, do not cross symlinks.
3324 sys_lchown(struct lchown_args
*uap
)
3326 struct nlookupdata nd
;
3329 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3331 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3337 * fchown_args(int fd, int uid, int gid)
3339 * Set ownership given a file descriptor.
3342 sys_fchown(struct fchown_args
*uap
)
3344 struct thread
*td
= curthread
;
3345 struct proc
*p
= td
->td_proc
;
3349 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3351 if (fp
->f_nchandle
.ncp
)
3352 error
= ncp_writechk(&fp
->f_nchandle
);
3354 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3355 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3361 * fchownat(int fd, char *path, int uid, int gid, int flags)
3363 * Set ownership of file pointed to by fd/path.
3366 sys_fchownat(struct fchownat_args
*uap
)
3368 struct nlookupdata nd
;
3373 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3375 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3377 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3378 UIO_USERSPACE
, flags
);
3380 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3381 nlookup_done_at(&nd
, fp
);
3387 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3389 struct timeval tv
[2];
3394 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3397 if ((error
= itimerfix(tvp
)) != 0)
3399 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3400 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3406 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3408 struct timespec tsnow
;
3422 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3424 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3427 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3428 newts
[0].tv_sec
= VNOVAL
;
3429 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3431 else if ((error
= itimespecfix(&newts
[0])) != 0)
3434 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3435 newts
[1].tv_sec
= VNOVAL
;
3436 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3438 else if ((error
= itimespecfix(&newts
[1])) != 0)
3445 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3446 const struct timespec
*ts
, int nullflag
)
3448 struct thread
*td
= curthread
;
3452 vattr
->va_atime
= ts
[0];
3453 vattr
->va_mtime
= ts
[1];
3455 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3456 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3462 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3464 struct timespec ts
[2];
3468 if ((error
= getutimes(tptr
, ts
)) != 0)
3471 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3476 * utimes_args(char *path, struct timeval *tptr)
3478 * Set the access and modification times of a file.
3481 sys_utimes(struct utimes_args
*uap
)
3483 struct timeval tv
[2];
3484 struct nlookupdata nd
;
3488 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3492 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3494 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3500 * lutimes_args(char *path, struct timeval *tptr)
3502 * Set the access and modification times of a file.
3505 sys_lutimes(struct lutimes_args
*uap
)
3507 struct timeval tv
[2];
3508 struct nlookupdata nd
;
3512 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3516 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3518 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3524 * Set utimes on a file descriptor. The creds used to open the
3525 * file are used to determine whether the operation is allowed
3529 kern_futimens(int fd
, struct timespec
*ts
)
3531 struct thread
*td
= curthread
;
3532 struct proc
*p
= td
->td_proc
;
3533 struct timespec newts
[2];
3540 error
= getutimens(ts
, newts
, &nullflag
);
3543 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3545 if (fp
->f_nchandle
.ncp
)
3546 error
= ncp_writechk(&fp
->f_nchandle
);
3549 error
= vget(vp
, LK_EXCLUSIVE
);
3551 error
= VOP_GETATTR(vp
, &vattr
);
3553 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3557 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3567 * futimens_args(int fd, struct timespec *ts)
3569 * Set the access and modification times of a file.
3572 sys_futimens(struct futimens_args
*uap
)
3574 struct timespec ts
[2];
3578 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3582 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3587 kern_futimes(int fd
, struct timeval
*tptr
)
3589 struct timespec ts
[2];
3593 if ((error
= getutimes(tptr
, ts
)) != 0)
3596 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3601 * futimes_args(int fd, struct timeval *tptr)
3603 * Set the access and modification times of a file.
3606 sys_futimes(struct futimes_args
*uap
)
3608 struct timeval tv
[2];
3612 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3616 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3621 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3623 struct timespec newts
[2];
3629 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3632 error
= getutimens(ts
, newts
, &nullflag
);
3636 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3637 if ((error
= nlookup(nd
)) != 0)
3639 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3641 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3643 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3644 error
= vget(vp
, LK_EXCLUSIVE
);
3646 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3655 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3657 * Set file access and modification times of a file.
3660 sys_utimensat(struct utimensat_args
*uap
)
3662 struct timespec ts
[2];
3663 struct nlookupdata nd
;
3669 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3674 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3675 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3676 UIO_USERSPACE
, flags
);
3678 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3679 nlookup_done_at(&nd
, fp
);
3684 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3691 uint64_t old_size
= 0;
3695 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3696 if ((error
= nlookup(nd
)) != 0)
3698 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3700 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3702 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3707 if (vp
->v_type
== VDIR
) {
3711 if (vfs_quota_enabled
) {
3712 error
= VOP_GETATTR(vp
, &vattr
);
3713 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3716 old_size
= vattr
.va_size
;
3719 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3721 vattr
.va_size
= length
;
3722 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3723 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3731 * truncate(char *path, int pad, off_t length)
3733 * Truncate a file given its path name.
3736 sys_truncate(struct truncate_args
*uap
)
3738 struct nlookupdata nd
;
3741 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3743 error
= kern_truncate(&nd
, uap
->length
);
3749 kern_ftruncate(int fd
, off_t length
)
3751 struct thread
*td
= curthread
;
3752 struct proc
*p
= td
->td_proc
;
3759 uint64_t old_size
= 0;
3764 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3766 if (fp
->f_nchandle
.ncp
) {
3767 error
= ncp_writechk(&fp
->f_nchandle
);
3771 if ((fp
->f_flag
& FWRITE
) == 0) {
3775 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3779 vp
= (struct vnode
*)fp
->f_data
;
3780 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3781 if (vp
->v_type
== VDIR
) {
3787 if (vfs_quota_enabled
) {
3788 error
= VOP_GETATTR(vp
, &vattr
);
3789 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3792 old_size
= vattr
.va_size
;
3795 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3797 vattr
.va_size
= length
;
3798 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3800 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3809 * ftruncate_args(int fd, int pad, off_t length)
3811 * Truncate a file given a file descriptor.
3814 sys_ftruncate(struct ftruncate_args
*uap
)
3818 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3826 * Sync an open file.
3829 sys_fsync(struct fsync_args
*uap
)
3831 struct thread
*td
= curthread
;
3832 struct proc
*p
= td
->td_proc
;
3838 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3840 vp
= (struct vnode
*)fp
->f_data
;
3841 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3842 if ((obj
= vp
->v_object
) != NULL
) {
3843 if (vp
->v_mount
== NULL
||
3844 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3845 vm_object_page_clean(obj
, 0, 0, 0);
3848 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3849 if (error
== 0 && vp
->v_mount
)
3850 error
= buf_fsync(vp
);
3858 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3860 struct nchandle fnchd
;
3861 struct nchandle tnchd
;
3862 struct namecache
*ncp
;
3871 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3872 if ((error
= nlookup(fromnd
)) != 0)
3874 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3876 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3880 * unlock the source nch so we can lookup the target nch without
3881 * deadlocking. The target may or may not exist so we do not check
3882 * for a target vp like kern_mkdir() and other creation functions do.
3884 * The source and target directories are ref'd and rechecked after
3885 * everything is relocked to determine if the source or target file
3888 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3889 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3891 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3893 cache_unlock(&fromnd
->nl_nch
);
3895 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3896 if ((error
= nlookup(tond
)) != 0) {
3900 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3902 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3906 tnchd
.mount
= tond
->nl_nch
.mount
;
3910 * If the source and target are the same there is nothing to do
3912 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
3919 * Mount points cannot be renamed or overwritten
3921 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
3930 * Relock the source ncp. cache_relock() will deal with any
3931 * deadlocks against the already-locked tond and will also
3932 * make sure both are resolved.
3934 * NOTE AFTER RELOCKING: The source or target ncp may have become
3935 * invalid while they were unlocked, nc_vp and nc_mount could
3938 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
3939 &tond
->nl_nch
, tond
->nl_cred
);
3940 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
3943 * If the namecache generation changed for either fromnd or tond,
3946 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
3947 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
3948 kprintf("kern_rename: retry due to gen on: "
3949 "\"%s\" -> \"%s\"\n",
3950 fromnd
->nl_nch
.ncp
->nc_name
,
3951 tond
->nl_nch
.ncp
->nc_name
);
3958 * If either fromnd or tond are marked destroyed a ripout occured
3959 * out from under us and we must retry.
3961 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
3962 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
3963 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
3964 kprintf("kern_rename: retry due to ripout on: "
3965 "\"%s\" -> \"%s\"\n",
3966 fromnd
->nl_nch
.ncp
->nc_name
,
3967 tond
->nl_nch
.ncp
->nc_name
);
3974 * Make sure the parent directories linkages are the same.
3975 * XXX shouldn't be needed any more w/ generation check above.
3977 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
3978 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
3985 * Both the source and target must be within the same filesystem and
3986 * in the same filesystem as their parent directories within the
3987 * namecache topology.
3989 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3992 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
3993 mp
!= tond
->nl_nch
.mount
) {
4000 * Make sure the mount point is writable
4002 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4009 * If the target exists and either the source or target is a directory,
4010 * then both must be directories.
4012 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4015 if (tond
->nl_nch
.ncp
->nc_vp
) {
4016 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4018 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4019 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4021 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4027 * You cannot rename a source into itself or a subdirectory of itself.
4028 * We check this by travsersing the target directory upwards looking
4029 * for a match against the source.
4034 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4035 if (fromnd
->nl_nch
.ncp
== ncp
) {
4046 * Even though the namespaces are different, they may still represent
4047 * hardlinks to the same file. The filesystem might have a hard time
4048 * with this so we issue a NREMOVE of the source instead of a NRENAME
4049 * when we detect the situation.
4052 fdvp
= fromnd
->nl_dvp
;
4053 tdvp
= tond
->nl_dvp
;
4054 if (fdvp
== NULL
|| tdvp
== NULL
) {
4056 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4057 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4060 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4061 fdvp
, tdvp
, tond
->nl_cred
);
4068 * rename_args(char *from, char *to)
4070 * Rename files. Source and destination must either both be directories,
4071 * or both not be directories. If target is a directory, it must be empty.
4074 sys_rename(struct rename_args
*uap
)
4076 struct nlookupdata fromnd
, tond
;
4080 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4082 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4084 error
= kern_rename(&fromnd
, &tond
);
4085 nlookup_done(&tond
);
4087 nlookup_done(&fromnd
);
4088 } while (error
== EAGAIN
);
4093 * renameat_args(int oldfd, char *old, int newfd, char *new)
4095 * Rename files using paths relative to the directories associated with
4096 * oldfd and newfd. Source and destination must either both be directories,
4097 * or both not be directories. If target is a directory, it must be empty.
4100 sys_renameat(struct renameat_args
*uap
)
4102 struct nlookupdata oldnd
, newnd
;
4103 struct file
*oldfp
, *newfp
;
4107 error
= nlookup_init_at(&oldnd
, &oldfp
,
4108 uap
->oldfd
, uap
->old
,
4111 error
= nlookup_init_at(&newnd
, &newfp
,
4112 uap
->newfd
, uap
->new,
4115 error
= kern_rename(&oldnd
, &newnd
);
4116 nlookup_done_at(&newnd
, newfp
);
4118 nlookup_done_at(&oldnd
, oldfp
);
4119 } while (error
== EAGAIN
);
4124 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4126 struct thread
*td
= curthread
;
4127 struct proc
*p
= td
->td_proc
;
4133 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4134 if ((error
= nlookup(nd
)) != 0)
4137 if (nd
->nl_nch
.ncp
->nc_vp
)
4139 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4142 vattr
.va_type
= VDIR
;
4143 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4146 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4153 * mkdir_args(char *path, int mode)
4155 * Make a directory file.
4158 sys_mkdir(struct mkdir_args
*uap
)
4160 struct nlookupdata nd
;
4163 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4165 error
= kern_mkdir(&nd
, uap
->mode
);
4171 * mkdirat_args(int fd, char *path, mode_t mode)
4173 * Make a directory file. The path is relative to the directory associated
4177 sys_mkdirat(struct mkdirat_args
*uap
)
4179 struct nlookupdata nd
;
4183 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4185 error
= kern_mkdir(&nd
, uap
->mode
);
4186 nlookup_done_at(&nd
, fp
);
4191 kern_rmdir(struct nlookupdata
*nd
)
4196 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4197 if ((error
= nlookup(nd
)) != 0)
4201 * Do not allow directories representing mount points to be
4202 * deleted, even if empty. Check write perms on mount point
4203 * in case the vnode is aliased (aka nullfs).
4205 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4207 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4209 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4214 * rmdir_args(char *path)
4216 * Remove a directory file.
4219 sys_rmdir(struct rmdir_args
*uap
)
4221 struct nlookupdata nd
;
4224 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4226 error
= kern_rmdir(&nd
);
4232 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4233 enum uio_seg direction
)
4235 struct thread
*td
= curthread
;
4236 struct proc
*p
= td
->td_proc
;
4244 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4246 if ((fp
->f_flag
& FREAD
) == 0) {
4250 vp
= (struct vnode
*)fp
->f_data
;
4251 if (vp
->v_type
!= VDIR
) {
4255 aiov
.iov_base
= buf
;
4256 aiov
.iov_len
= count
;
4257 auio
.uio_iov
= &aiov
;
4258 auio
.uio_iovcnt
= 1;
4259 auio
.uio_rw
= UIO_READ
;
4260 auio
.uio_segflg
= direction
;
4262 auio
.uio_resid
= count
;
4263 loff
= auio
.uio_offset
= fp
->f_offset
;
4264 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4265 fp
->f_offset
= auio
.uio_offset
;
4270 * WARNING! *basep may not be wide enough to accomodate the
4271 * seek offset. XXX should we hack this to return the upper 32 bits
4272 * for offsets greater then 4G?
4275 *basep
= (long)loff
;
4277 *res
= count
- auio
.uio_resid
;
4284 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4286 * Read a block of directory entries in a file system independent format.
4289 sys_getdirentries(struct getdirentries_args
*uap
)
4294 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4295 &uap
->sysmsg_result
, UIO_USERSPACE
);
4297 if (error
== 0 && uap
->basep
)
4298 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4303 * getdents_args(int fd, char *buf, size_t count)
4306 sys_getdents(struct getdents_args
*uap
)
4310 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4311 &uap
->sysmsg_result
, UIO_USERSPACE
);
4317 * Set the mode mask for creation of filesystem nodes.
4319 * umask(int newmask)
4322 sys_umask(struct umask_args
*uap
)
4324 struct thread
*td
= curthread
;
4325 struct proc
*p
= td
->td_proc
;
4326 struct filedesc
*fdp
;
4329 uap
->sysmsg_result
= fdp
->fd_cmask
;
4330 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4335 * revoke(char *path)
4337 * Void all references to file by ripping underlying filesystem
4341 sys_revoke(struct revoke_args
*uap
)
4343 struct nlookupdata nd
;
4350 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4352 error
= nlookup(&nd
);
4354 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4355 cred
= crhold(nd
.nl_cred
);
4359 error
= VOP_GETATTR(vp
, &vattr
);
4360 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4361 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4362 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4364 error
= vrevoke(vp
, cred
);
4365 } else if (error
== 0) {
4366 error
= vrevoke(vp
, cred
);
4376 * getfh_args(char *fname, fhandle_t *fhp)
4378 * Get (NFS) file handle
4380 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4381 * mount. This allows nullfs mounts to be explicitly exported.
4383 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4385 * nullfs mounts of subdirectories are not safe. That is, it will
4386 * work, but you do not really have protection against access to
4387 * the related parent directories.
4390 sys_getfh(struct getfh_args
*uap
)
4392 struct thread
*td
= curthread
;
4393 struct nlookupdata nd
;
4400 * Must be super user
4402 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4406 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4408 error
= nlookup(&nd
);
4410 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4411 mp
= nd
.nl_nch
.mount
;
4414 bzero(&fh
, sizeof(fh
));
4415 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4416 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4419 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4425 * fhopen_args(const struct fhandle *u_fhp, int flags)
4427 * syscall for the rpc.lockd to use to translate a NFS file handle into
4428 * an open descriptor.
4430 * warning: do not remove the priv_check() call or this becomes one giant
4434 sys_fhopen(struct fhopen_args
*uap
)
4436 struct thread
*td
= curthread
;
4437 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4442 struct vattr
*vap
= &vat
;
4444 int fmode
, mode
, error
= 0, type
;
4450 * Must be super user
4452 error
= priv_check(td
, PRIV_ROOT
);
4456 fmode
= FFLAGS(uap
->flags
);
4459 * Why not allow a non-read/write open for our lockd?
4461 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4463 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4468 * Find the mount point
4470 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4475 /* now give me my vnode, it gets returned to me locked */
4476 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4480 * from now on we have to make sure not
4481 * to forget about the vnode
4482 * any error that causes an abort must vput(vp)
4483 * just set error = err and 'goto bad;'.
4489 if (vp
->v_type
== VLNK
) {
4493 if (vp
->v_type
== VSOCK
) {
4498 if (fmode
& (FWRITE
| O_TRUNC
)) {
4499 if (vp
->v_type
== VDIR
) {
4503 error
= vn_writechk(vp
, NULL
);
4511 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4515 if (fmode
& O_TRUNC
) {
4516 vn_unlock(vp
); /* XXX */
4517 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4520 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4526 * VOP_OPEN needs the file pointer so it can potentially override
4529 * WARNING! no f_nchandle will be associated when fhopen()ing a
4532 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4536 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4539 * setting f_ops this way prevents VOP_CLOSE from being
4540 * called or fdrop() releasing the vp from v_data. Since
4541 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4543 fp
->f_ops
= &badfileops
;
4549 * The fp is given its own reference, we still have our ref and lock.
4551 * Assert that all regular files must be created with a VM object.
4553 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4554 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4559 * The open was successful. Handle any locking requirements.
4561 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4562 lf
.l_whence
= SEEK_SET
;
4565 if (fmode
& O_EXLOCK
)
4566 lf
.l_type
= F_WRLCK
;
4568 lf
.l_type
= F_RDLCK
;
4569 if (fmode
& FNONBLOCK
)
4574 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4576 * release our private reference.
4578 fsetfd(fdp
, NULL
, indx
);
4583 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4584 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4588 * Clean up. Associate the file pointer with the previously
4589 * reserved descriptor and return it.
4592 if (uap
->flags
& O_CLOEXEC
)
4593 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4594 fsetfd(fdp
, fp
, indx
);
4596 uap
->sysmsg_result
= indx
;
4600 fsetfd(fdp
, NULL
, indx
);
4609 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4612 sys_fhstat(struct fhstat_args
*uap
)
4614 struct thread
*td
= curthread
;
4622 * Must be super user
4624 error
= priv_check(td
, PRIV_ROOT
);
4628 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4632 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4635 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4636 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4641 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4646 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4649 sys_fhstatfs(struct fhstatfs_args
*uap
)
4651 struct thread
*td
= curthread
;
4652 struct proc
*p
= td
->td_proc
;
4657 char *fullpath
, *freepath
;
4662 * Must be super user
4664 if ((error
= priv_check(td
, PRIV_ROOT
)))
4667 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4670 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4674 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4679 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4684 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4687 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4690 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4691 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4692 kfree(freepath
, M_TEMP
);
4694 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4695 if (priv_check(td
, PRIV_ROOT
)) {
4696 bcopy(sp
, &sb
, sizeof(sb
));
4697 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4700 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4706 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4709 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4711 struct thread
*td
= curthread
;
4712 struct proc
*p
= td
->td_proc
;
4720 * Must be super user
4722 if ((error
= priv_check(td
, PRIV_ROOT
)))
4725 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4728 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4732 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4737 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4740 sp
= &mp
->mnt_vstat
;
4742 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4746 if (mp
->mnt_flag
& MNT_RDONLY
)
4747 sp
->f_flag
|= ST_RDONLY
;
4748 if (mp
->mnt_flag
& MNT_NOSUID
)
4749 sp
->f_flag
|= ST_NOSUID
;
4750 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4757 * Syscall to push extended attribute configuration information into the
4758 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4759 * a command (int cmd), and attribute name and misc data. For now, the
4760 * attribute name is left in userspace for consumption by the VFS_op.
4761 * It will probably be changed to be copied into sysspace by the
4762 * syscall in the future, once issues with various consumers of the
4763 * attribute code have raised their hands.
4765 * Currently this is used only by UFS Extended Attributes.
4768 sys_extattrctl(struct extattrctl_args
*uap
)
4770 struct nlookupdata nd
;
4772 char attrname
[EXTATTR_MAXNAMELEN
];
4780 if (error
== 0 && uap
->filename
) {
4781 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4784 error
= nlookup(&nd
);
4786 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4790 if (error
== 0 && uap
->attrname
) {
4791 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4796 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4798 error
= nlookup(&nd
);
4800 error
= ncp_writechk(&nd
.nl_nch
);
4802 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4804 uap
->attrname
, nd
.nl_cred
);
4813 * Syscall to get a named extended attribute on a file or directory.
4816 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4818 char attrname
[EXTATTR_MAXNAMELEN
];
4819 struct nlookupdata nd
;
4825 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4831 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4833 error
= nlookup(&nd
);
4835 error
= ncp_writechk(&nd
.nl_nch
);
4837 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4843 bzero(&auio
, sizeof(auio
));
4844 aiov
.iov_base
= uap
->data
;
4845 aiov
.iov_len
= uap
->nbytes
;
4846 auio
.uio_iov
= &aiov
;
4847 auio
.uio_iovcnt
= 1;
4848 auio
.uio_offset
= 0;
4849 auio
.uio_resid
= uap
->nbytes
;
4850 auio
.uio_rw
= UIO_WRITE
;
4851 auio
.uio_td
= curthread
;
4853 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4862 * Syscall to get a named extended attribute on a file or directory.
4865 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4867 char attrname
[EXTATTR_MAXNAMELEN
];
4868 struct nlookupdata nd
;
4874 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4880 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4882 error
= nlookup(&nd
);
4884 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
4890 bzero(&auio
, sizeof(auio
));
4891 aiov
.iov_base
= uap
->data
;
4892 aiov
.iov_len
= uap
->nbytes
;
4893 auio
.uio_iov
= &aiov
;
4894 auio
.uio_iovcnt
= 1;
4895 auio
.uio_offset
= 0;
4896 auio
.uio_resid
= uap
->nbytes
;
4897 auio
.uio_rw
= UIO_READ
;
4898 auio
.uio_td
= curthread
;
4900 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4902 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
4910 * Syscall to delete a named extended attribute from a file or directory.
4911 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4914 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
4916 char attrname
[EXTATTR_MAXNAMELEN
];
4917 struct nlookupdata nd
;
4921 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4925 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4927 error
= nlookup(&nd
);
4929 error
= ncp_writechk(&nd
.nl_nch
);
4931 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4933 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
4934 attrname
, NULL
, nd
.nl_cred
);
4943 * Determine if the mount is visible to the process.
4946 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
4948 struct nchandle nch
;
4951 * Traverse from the mount point upwards. If we hit the process
4952 * root then the mount point is visible to the process.
4954 nch
= mp
->mnt_ncmountpt
;
4956 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
4957 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
4960 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
4961 nch
= nch
.mount
->mnt_ncmounton
;
4963 nch
.ncp
= nch
.ncp
->nc_parent
;
4968 * If the mount point is not visible to the process, but the
4969 * process root is in a subdirectory of the mount, return
4972 if (p
->p_fd
->fd_nrdir
.mount
== mp
)