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 mp
->mnt_pbuf_count
= nswbuf_kva
/ NSWBUF_SPLIT
;
340 vfsp
->vfc_refcount
++;
341 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
342 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
343 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
344 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
345 lwkt_gettoken(&mp
->mnt_token
);
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap
->flags
& MNT_RDONLY
)
354 mp
->mnt_flag
|= MNT_RDONLY
;
355 else if (mp
->mnt_flag
& MNT_RDONLY
)
356 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
357 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
358 MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_NOATIME
|
359 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
360 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
|
362 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
363 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_ASYNC
| MNT_FORCE
|
364 MNT_NOSYMFOLLOW
| MNT_IGNORE
| MNT_TRIM
|
365 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
|
368 * Mount the filesystem.
369 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
372 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
373 if (mp
->mnt_flag
& MNT_UPDATE
) {
374 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
375 mp
->mnt_flag
&= ~MNT_RDONLY
;
376 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
377 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
380 mp
->mnt_kern_flag
= flag2
;
382 lwkt_reltoken(&mp
->mnt_token
);
388 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
407 cache_unlock(&mp
->mnt_ncmountpt
);
410 mp
->mnt_ncmounton
= nch
; /* inherits ref */
412 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
414 cache_ismounting(mp
);
415 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
417 mountlist_insert(mp
, MNTINS_LAST
);
419 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
420 error
= vfs_allocate_syncvnode(mp
);
421 lwkt_reltoken(&mp
->mnt_token
);
423 error
= VFS_START(mp
, 0);
425 KNOTE(&fs_klist
, VQ_MOUNT
);
427 vn_syncer_thr_stop(mp
);
428 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
429 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
430 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
431 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
432 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
433 mp
->mnt_vfc
->vfc_refcount
--;
434 lwkt_reltoken(&mp
->mnt_token
);
445 * Scan all active processes to see if any of them have a current
446 * or root directory onto which the new filesystem has just been
447 * mounted. If so, replace them with the new mount point.
449 * Both old_nch and new_nch are ref'd on call but not locked.
450 * new_nch must be temporarily locked so it can be associated with the
451 * vnode representing the root of the mount point.
453 struct checkdirs_info
{
454 struct nchandle old_nch
;
455 struct nchandle new_nch
;
456 struct vnode
*old_vp
;
457 struct vnode
*new_vp
;
460 static int checkdirs_callback(struct proc
*p
, void *data
);
463 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
465 struct checkdirs_info info
;
471 * If the old mount point's vnode has a usecount of 1, it is not
472 * being held as a descriptor anywhere.
474 olddp
= old_nch
->ncp
->nc_vp
;
475 if (olddp
== NULL
|| VREFCNT(olddp
) == 1)
479 * Force the root vnode of the new mount point to be resolved
480 * so we can update any matching processes.
483 if (VFS_ROOT(mp
, &newdp
))
484 panic("mount: lost mount");
487 vn_lock(newdp
, LK_EXCLUSIVE
| LK_RETRY
);
488 cache_setunresolved(new_nch
);
489 cache_setvp(new_nch
, newdp
);
490 cache_unlock(new_nch
);
493 * Special handling of the root node
495 if (rootvnode
== olddp
) {
497 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
501 * Pass newdp separately so the callback does not have to access
502 * it via new_nch->ncp->nc_vp.
504 info
.old_nch
= *old_nch
;
505 info
.new_nch
= *new_nch
;
507 allproc_scan(checkdirs_callback
, &info
);
512 * NOTE: callback is not MP safe because the scanned process's filedesc
513 * structure can be ripped out from under us, amoung other things.
516 checkdirs_callback(struct proc
*p
, void *data
)
518 struct checkdirs_info
*info
= data
;
519 struct filedesc
*fdp
;
520 struct nchandle ncdrop1
;
521 struct nchandle ncdrop2
;
522 struct vnode
*vprele1
;
523 struct vnode
*vprele2
;
525 if ((fdp
= p
->p_fd
) != NULL
) {
526 cache_zero(&ncdrop1
);
527 cache_zero(&ncdrop2
);
532 * MPUNSAFE - XXX fdp can be pulled out from under a
535 * A shared filedesc is ok, we don't have to copy it
536 * because we are making this change globally.
538 spin_lock(&fdp
->fd_spin
);
539 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
540 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
541 vprele1
= fdp
->fd_cdir
;
543 fdp
->fd_cdir
= info
->new_vp
;
544 ncdrop1
= fdp
->fd_ncdir
;
545 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
547 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
548 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
549 vprele2
= fdp
->fd_rdir
;
551 fdp
->fd_rdir
= info
->new_vp
;
552 ncdrop2
= fdp
->fd_nrdir
;
553 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
555 spin_unlock(&fdp
->fd_spin
);
557 cache_drop(&ncdrop1
);
559 cache_drop(&ncdrop2
);
569 * Unmount a file system.
571 * Note: unmount takes a path to the vnode mounted on as argument,
572 * not special file (as before).
574 * umount_args(char *path, int flags)
579 sys_unmount(struct unmount_args
*uap
)
581 struct thread
*td
= curthread
;
582 struct proc
*p __debugvar
= td
->td_proc
;
583 struct mount
*mp
= NULL
;
584 struct nlookupdata nd
;
589 if (td
->td_ucred
->cr_prison
!= NULL
) {
593 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
596 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
598 error
= nlookup(&nd
);
602 mp
= nd
.nl_nch
.mount
;
605 * Only root, or the user that did the original mount is
606 * permitted to unmount this filesystem.
608 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
609 (error
= priv_check(td
, PRIV_ROOT
)))
613 * Don't allow unmounting the root file system.
615 if (mp
->mnt_flag
& MNT_ROOTFS
) {
621 * Must be the root of the filesystem
623 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
631 error
= dounmount(mp
, uap
->flags
);
638 * Do the actual file system unmount.
641 dounmount_interlock(struct mount
*mp
)
643 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
645 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
650 unmount_allproc_cb(struct proc
*p
, void *arg
)
654 if (p
->p_textnch
.ncp
== NULL
)
657 mp
= (struct mount
*)arg
;
658 if (p
->p_textnch
.mount
== mp
)
659 cache_drop(&p
->p_textnch
);
665 dounmount(struct mount
*mp
, int flags
)
667 struct namecache
*ncp
;
676 lwkt_gettoken(&mp
->mnt_token
);
678 * Exclusive access for unmounting purposes
680 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
684 * Allow filesystems to detect that a forced unmount is in progress.
686 if (flags
& MNT_FORCE
)
687 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
688 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_TIMELOCK
);
689 error
= lockmgr(&mp
->mnt_lock
, lflags
);
691 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
692 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
693 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
699 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
700 vfs_setpublicfs(NULL
, NULL
, NULL
);
702 vfs_msync(mp
, MNT_WAIT
);
703 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
704 mp
->mnt_flag
&=~ MNT_ASYNC
;
707 * If this filesystem isn't aliasing other filesystems,
708 * try to invalidate any remaining namecache entries and
709 * check the count afterwords.
711 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
712 cache_lock(&mp
->mnt_ncmountpt
);
713 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
714 cache_unlock(&mp
->mnt_ncmountpt
);
716 cache_clearmntcache();
717 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
718 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
719 allproc_scan(&unmount_allproc_cb
, mp
);
722 cache_clearmntcache();
723 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
724 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
726 if ((flags
& MNT_FORCE
) == 0) {
728 mount_warning(mp
, "Cannot unmount: "
734 mount_warning(mp
, "Forced unmount: "
745 * Decomission our special mnt_syncer vnode. This also stops
746 * the vnlru code. If we are unable to unmount we recommission
749 * Then sync the filesystem.
751 if ((vp
= mp
->mnt_syncer
) != NULL
) {
752 mp
->mnt_syncer
= NULL
;
753 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
756 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
757 VFS_SYNC(mp
, MNT_WAIT
);
760 * nchandle records ref the mount structure. Expect a count of 1
761 * (our mount->mnt_ncmountpt).
763 * Scans can get temporary refs on a mountpoint (thought really
764 * heavy duty stuff like cache_findmount() do not).
766 if (mp
->mnt_refs
!= 1)
767 cache_clearmntcache();
768 for (retry
= 0; retry
< 10 && mp
->mnt_refs
!= 1; ++retry
) {
769 cache_unmounting(mp
);
770 tsleep(&mp
->mnt_refs
, 0, "mntbsy", hz
/ 10 + 1);
771 cache_clearmntcache();
773 if (mp
->mnt_refs
!= 1) {
774 if ((flags
& MNT_FORCE
) == 0) {
775 mount_warning(mp
, "Cannot unmount: "
776 "%d mount refs still present",
780 mount_warning(mp
, "Forced unmount: "
781 "%d mount refs still present",
788 * So far so good, sync the filesystem once more and
789 * call the VFS unmount code if the sync succeeds.
792 if (mp
->mnt_flag
& MNT_RDONLY
) {
793 error
= VFS_UNMOUNT(mp
, flags
);
795 error
= VFS_SYNC(mp
, MNT_WAIT
);
797 (error
== EOPNOTSUPP
) || /* No sync */
798 (flags
& MNT_FORCE
)) {
799 error
= VFS_UNMOUNT(mp
, flags
);
805 * If an error occurred we can still recover, restoring the
806 * syncer vnode and misc flags.
809 if (mp
->mnt_syncer
== NULL
)
810 vfs_allocate_syncvnode(mp
);
811 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
812 mp
->mnt_flag
|= async_flag
;
813 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
814 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
815 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
821 * Clean up any journals still associated with the mount after
822 * filesystem activity has ceased.
824 journal_remove_all_journals(mp
,
825 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
827 mountlist_remove(mp
);
830 * Remove any installed vnode ops here so the individual VFSs don't
833 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
834 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
835 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
836 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
837 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
839 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
840 nch
= mp
->mnt_ncmountpt
;
841 cache_zero(&mp
->mnt_ncmountpt
);
842 cache_clrmountpt(&nch
);
845 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
846 cache_unmounting(mp
);
847 nch
= mp
->mnt_ncmounton
;
848 cache_zero(&mp
->mnt_ncmounton
);
849 cache_clrmountpt(&nch
);
853 mp
->mnt_vfc
->vfc_refcount
--;
854 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
855 panic("unmount: dangling vnode");
856 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
857 if (mp
->mnt_kern_flag
& MNTK_MWAIT
) {
858 mp
->mnt_kern_flag
&= ~MNTK_MWAIT
;
863 * If we reach here and freeok != 0 we must free the mount.
864 * If refs > 1 cycle and wait, just in case someone tried
865 * to busy the mount after we decided to do the unmount.
868 if (mp
->mnt_refs
> 1)
869 cache_clearmntcache();
870 while (mp
->mnt_refs
> 1) {
871 cache_unmounting(mp
);
873 tsleep(&mp
->mnt_refs
, 0, "umntrwait", hz
/ 10 + 1);
874 cache_clearmntcache();
876 lwkt_reltoken(&mp
->mnt_token
);
881 KNOTE(&fs_klist
, VQ_UNMOUNT
);
884 lwkt_reltoken(&mp
->mnt_token
);
890 mount_warning(struct mount
*mp
, const char *ctl
, ...)
897 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, NULL
,
898 &ptr
, &buf
, 0) == 0) {
899 kprintf("unmount(%s): ", ptr
);
904 kprintf("unmount(%p", mp
);
905 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
906 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
915 * Shim cache_fullpath() to handle the case where a process is chrooted into
916 * a subdirectory of a mount. In this case if the root mount matches the
917 * process root directory's mount we have to specify the process's root
918 * directory instead of the mount point, because the mount point might
919 * be above the root directory.
923 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
925 struct nchandle
*nch
;
927 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
928 nch
= &p
->p_fd
->fd_nrdir
;
930 nch
= &mp
->mnt_ncmountpt
;
931 return(cache_fullpath(p
, nch
, NULL
, rb
, fb
, 0));
935 * Sync each mounted filesystem.
939 static int syncprt
= 0;
940 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
943 static int sync_callback(struct mount
*mp
, void *data
);
946 sys_sync(struct sync_args
*uap
)
948 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
954 sync_callback(struct mount
*mp
, void *data __unused
)
958 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
959 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
960 mp
->mnt_flag
&= ~MNT_ASYNC
;
961 vfs_msync(mp
, MNT_NOWAIT
);
962 VFS_SYNC(mp
, MNT_NOWAIT
);
963 mp
->mnt_flag
|= asyncflag
;
968 /* XXX PRISON: could be per prison flag */
969 static int prison_quotas
;
971 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
975 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
977 * Change filesystem quotas.
982 sys_quotactl(struct quotactl_args
*uap
)
984 struct nlookupdata nd
;
991 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
996 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
998 error
= nlookup(&nd
);
1000 mp
= nd
.nl_nch
.mount
;
1001 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
1002 uap
->arg
, nd
.nl_cred
);
1011 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1012 * void *buf, int buflen)
1014 * This function operates on a mount point and executes the specified
1015 * operation using the specified control data, and possibly returns data.
1017 * The actual number of bytes stored in the result buffer is returned, 0
1018 * if none, otherwise an error is returned.
1023 sys_mountctl(struct mountctl_args
*uap
)
1025 struct thread
*td
= curthread
;
1026 struct proc
*p
= td
->td_proc
;
1034 * Sanity and permissions checks. We must be root.
1037 if (td
->td_ucred
->cr_prison
!= NULL
)
1039 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
1040 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
1044 * Argument length checks
1046 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
1048 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
1050 if (uap
->path
== NULL
)
1054 * Allocate the necessary buffers and copyin data
1056 path
= objcache_get(namei_oc
, M_WAITOK
);
1057 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
1062 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1063 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
1068 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
1071 * Validate the descriptor
1074 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1084 * Execute the internal kernel function and clean up.
1087 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1091 if (error
== 0 && uap
->sysmsg_result
> 0)
1092 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1095 objcache_put(namei_oc
, path
);
1104 * Execute a mount control operation by resolving the path to a mount point
1105 * and calling vop_mountctl().
1107 * Use the mount point from the nch instead of the vnode so nullfs mounts
1108 * can properly spike the VOP.
1111 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1112 const void *ctl
, int ctllen
,
1113 void *buf
, int buflen
, int *res
)
1116 struct nlookupdata nd
;
1117 struct nchandle nch
;
1123 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1126 error
= nlookup(&nd
);
1131 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1138 * Yes, all this is needed to use the nch.mount below, because
1139 * we must maintain a ref on the mount to avoid ripouts (e.g.
1140 * due to heavy mount/unmount use by synth or poudriere).
1143 cache_zero(&nd
.nl_nch
);
1151 * Must be the root of the filesystem
1153 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1158 if (mp
== NULL
|| mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1159 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1165 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1174 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1176 struct thread
*td
= curthread
;
1177 struct proc
*p
= td
->td_proc
;
1180 char *fullpath
, *freepath
;
1183 if ((error
= nlookup(nd
)) != 0)
1185 mp
= nd
->nl_nch
.mount
;
1187 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1190 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1193 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1194 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1195 kfree(freepath
, M_TEMP
);
1197 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1198 bcopy(sp
, buf
, sizeof(*buf
));
1199 /* Only root should have access to the fsid's. */
1200 if (priv_check(td
, PRIV_ROOT
))
1201 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1206 * statfs_args(char *path, struct statfs *buf)
1208 * Get filesystem statistics.
1211 sys_statfs(struct statfs_args
*uap
)
1213 struct nlookupdata nd
;
1217 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1219 error
= kern_statfs(&nd
, &buf
);
1222 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1227 kern_fstatfs(int fd
, struct statfs
*buf
)
1229 struct thread
*td
= curthread
;
1230 struct proc
*p
= td
->td_proc
;
1234 char *fullpath
, *freepath
;
1238 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1242 * Try to use mount info from any overlays rather than the
1243 * mount info for the underlying vnode, otherwise we will
1244 * fail when operating on null-mounted paths inside a chroot.
1246 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1247 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1252 if (fp
->f_cred
== NULL
) {
1257 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1260 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1262 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1263 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1264 kfree(freepath
, M_TEMP
);
1266 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1267 bcopy(sp
, buf
, sizeof(*buf
));
1269 /* Only root should have access to the fsid's. */
1270 if (priv_check(td
, PRIV_ROOT
))
1271 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1279 * fstatfs_args(int fd, struct statfs *buf)
1281 * Get filesystem statistics.
1284 sys_fstatfs(struct fstatfs_args
*uap
)
1289 error
= kern_fstatfs(uap
->fd
, &buf
);
1292 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1297 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1303 if ((error
= nlookup(nd
)) != 0)
1305 mp
= nd
->nl_nch
.mount
;
1306 sp
= &mp
->mnt_vstat
;
1307 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1311 if (mp
->mnt_flag
& MNT_RDONLY
)
1312 sp
->f_flag
|= ST_RDONLY
;
1313 if (mp
->mnt_flag
& MNT_NOSUID
)
1314 sp
->f_flag
|= ST_NOSUID
;
1315 bcopy(sp
, buf
, sizeof(*buf
));
1320 * statfs_args(char *path, struct statfs *buf)
1322 * Get filesystem statistics.
1325 sys_statvfs(struct statvfs_args
*uap
)
1327 struct nlookupdata nd
;
1331 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1333 error
= kern_statvfs(&nd
, &buf
);
1336 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1341 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1343 struct thread
*td
= curthread
;
1344 struct proc
*p
= td
->td_proc
;
1351 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1353 if ((mp
= fp
->f_nchandle
.mount
) == NULL
)
1354 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1359 if (fp
->f_cred
== NULL
) {
1363 sp
= &mp
->mnt_vstat
;
1364 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1368 if (mp
->mnt_flag
& MNT_RDONLY
)
1369 sp
->f_flag
|= ST_RDONLY
;
1370 if (mp
->mnt_flag
& MNT_NOSUID
)
1371 sp
->f_flag
|= ST_NOSUID
;
1373 bcopy(sp
, buf
, sizeof(*buf
));
1381 * fstatfs_args(int fd, struct statfs *buf)
1383 * Get filesystem statistics.
1386 sys_fstatvfs(struct fstatvfs_args
*uap
)
1391 error
= kern_fstatvfs(uap
->fd
, &buf
);
1394 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1399 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1401 * Get statistics on all filesystems.
1404 struct getfsstat_info
{
1405 struct statfs
*sfsp
;
1413 static int getfsstat_callback(struct mount
*, void *);
1416 sys_getfsstat(struct getfsstat_args
*uap
)
1418 struct thread
*td
= curthread
;
1419 struct getfsstat_info info
;
1421 bzero(&info
, sizeof(info
));
1423 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1424 info
.sfsp
= uap
->buf
;
1426 info
.flags
= uap
->flags
;
1429 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1430 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1431 uap
->sysmsg_result
= info
.maxcount
;
1433 uap
->sysmsg_result
= info
.count
;
1434 return (info
.error
);
1438 getfsstat_callback(struct mount
*mp
, void *data
)
1440 struct getfsstat_info
*info
= data
;
1446 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1447 if (info
->td
->td_proc
&&
1448 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1454 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1455 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1456 * overrides MNT_WAIT.
1458 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1459 (info
->flags
& MNT_WAIT
)) &&
1460 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1463 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1465 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1467 info
->error
= error
;
1470 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1471 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1472 kfree(freepath
, M_TEMP
);
1474 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1476 info
->error
= error
;
1486 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1487 long bufsize, int flags)
1489 * Get statistics on all filesystems.
1492 struct getvfsstat_info
{
1493 struct statfs
*sfsp
;
1494 struct statvfs
*vsfsp
;
1502 static int getvfsstat_callback(struct mount
*, void *);
1505 sys_getvfsstat(struct getvfsstat_args
*uap
)
1507 struct thread
*td
= curthread
;
1508 struct getvfsstat_info info
;
1510 bzero(&info
, sizeof(info
));
1512 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1513 info
.sfsp
= uap
->buf
;
1514 info
.vsfsp
= uap
->vbuf
;
1516 info
.flags
= uap
->flags
;
1519 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1520 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1521 uap
->sysmsg_result
= info
.maxcount
;
1523 uap
->sysmsg_result
= info
.count
;
1524 return (info
.error
);
1528 getvfsstat_callback(struct mount
*mp
, void *data
)
1530 struct getvfsstat_info
*info
= data
;
1532 struct statvfs
*vsp
;
1537 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1538 if (info
->td
->td_proc
&&
1539 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1543 vsp
= &mp
->mnt_vstat
;
1546 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1547 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1548 * overrides MNT_WAIT.
1550 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1551 (info
->flags
& MNT_WAIT
)) &&
1552 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1555 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1557 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1558 (info
->flags
& MNT_WAIT
)) &&
1559 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1563 if (mp
->mnt_flag
& MNT_RDONLY
)
1564 vsp
->f_flag
|= ST_RDONLY
;
1565 if (mp
->mnt_flag
& MNT_NOSUID
)
1566 vsp
->f_flag
|= ST_NOSUID
;
1568 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1570 info
->error
= error
;
1573 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1574 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1575 kfree(freepath
, M_TEMP
);
1577 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1579 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1581 info
->error
= error
;
1593 * fchdir_args(int fd)
1595 * Change current working directory to a given file descriptor.
1598 sys_fchdir(struct fchdir_args
*uap
)
1600 struct thread
*td
= curthread
;
1601 struct proc
*p
= td
->td_proc
;
1602 struct filedesc
*fdp
= p
->p_fd
;
1603 struct vnode
*vp
, *ovp
;
1606 struct nchandle nch
, onch
, tnch
;
1609 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1611 lwkt_gettoken(&p
->p_token
);
1612 vp
= (struct vnode
*)fp
->f_data
;
1614 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
1615 if (fp
->f_nchandle
.ncp
== NULL
)
1618 error
= checkvp_chdir(vp
, td
);
1623 cache_copy(&fp
->f_nchandle
, &nch
);
1626 * If the ncp has become a mount point, traverse through
1630 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1631 (mp
= cache_findmount(&nch
)) != NULL
1633 error
= nlookup_mp(mp
, &tnch
);
1635 cache_unlock(&tnch
); /* leave ref intact */
1637 vp
= tnch
.ncp
->nc_vp
;
1638 error
= vget(vp
, LK_SHARED
);
1639 KKASSERT(error
== 0);
1643 cache_dropmount(mp
);
1646 spin_lock(&fdp
->fd_spin
);
1648 onch
= fdp
->fd_ncdir
;
1650 fdp
->fd_ncdir
= nch
;
1651 spin_unlock(&fdp
->fd_spin
);
1652 vn_unlock(vp
); /* leave ref intact */
1661 lwkt_reltoken(&p
->p_token
);
1666 kern_chdir(struct nlookupdata
*nd
)
1668 struct thread
*td
= curthread
;
1669 struct proc
*p
= td
->td_proc
;
1670 struct filedesc
*fdp
= p
->p_fd
;
1671 struct vnode
*vp
, *ovp
;
1672 struct nchandle onch
;
1675 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1676 if ((error
= nlookup(nd
)) != 0)
1678 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1680 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1683 lwkt_gettoken(&p
->p_token
);
1684 error
= checkvp_chdir(vp
, td
);
1687 spin_lock(&fdp
->fd_spin
);
1689 onch
= fdp
->fd_ncdir
;
1690 fdp
->fd_ncdir
= nd
->nl_nch
;
1692 spin_unlock(&fdp
->fd_spin
);
1693 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1696 cache_zero(&nd
->nl_nch
);
1700 lwkt_reltoken(&p
->p_token
);
1705 * chdir_args(char *path)
1707 * Change current working directory (``.'').
1710 sys_chdir(struct chdir_args
*uap
)
1712 struct nlookupdata nd
;
1715 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1717 error
= kern_chdir(&nd
);
1723 * Helper function for raised chroot(2) security function: Refuse if
1724 * any filedescriptors are open directories.
1727 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1734 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1735 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1737 vp
= (struct vnode
*)fp
->f_data
;
1738 if (vp
->v_type
!= VDIR
) {
1749 * This sysctl determines if we will allow a process to chroot(2) if it
1750 * has a directory open:
1751 * 0: disallowed for all processes.
1752 * 1: allowed for processes that were not already chroot(2)'ed.
1753 * 2: allowed for all processes.
1756 static int chroot_allow_open_directories
= 1;
1758 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1759 &chroot_allow_open_directories
, 0, "");
1762 * chroot to the specified namecache entry. We obtain the vp from the
1763 * namecache data. The passed ncp must be locked and referenced and will
1764 * remain locked and referenced on return.
1767 kern_chroot(struct nchandle
*nch
)
1769 struct thread
*td
= curthread
;
1770 struct proc
*p
= td
->td_proc
;
1771 struct filedesc
*fdp
= p
->p_fd
;
1776 * Only privileged user can chroot
1778 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1783 * Disallow open directory descriptors (fchdir() breakouts).
1785 if (chroot_allow_open_directories
== 0 ||
1786 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1787 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1790 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1793 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1797 * Check the validity of vp as a directory to change to and
1798 * associate it with rdir/jdir.
1800 error
= checkvp_chdir(vp
, td
);
1801 vn_unlock(vp
); /* leave reference intact */
1803 vrele(fdp
->fd_rdir
);
1804 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1805 cache_drop(&fdp
->fd_nrdir
);
1806 cache_copy(nch
, &fdp
->fd_nrdir
);
1807 if (fdp
->fd_jdir
== NULL
) {
1810 cache_copy(nch
, &fdp
->fd_njdir
);
1819 * chroot_args(char *path)
1821 * Change notion of root (``/'') directory.
1824 sys_chroot(struct chroot_args
*uap
)
1826 struct thread
*td __debugvar
= curthread
;
1827 struct nlookupdata nd
;
1830 KKASSERT(td
->td_proc
);
1831 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1833 nd
.nl_flags
|= NLC_EXEC
;
1834 error
= nlookup(&nd
);
1836 error
= kern_chroot(&nd
.nl_nch
);
1843 sys_chroot_kernel(struct chroot_kernel_args
*uap
)
1845 struct thread
*td
= curthread
;
1846 struct nlookupdata nd
;
1847 struct nchandle
*nch
;
1851 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1855 error
= nlookup(&nd
);
1861 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1865 if ((vp
= nch
->ncp
->nc_vp
) == NULL
) {
1870 if ((error
= cache_vref(nch
, nd
.nl_cred
, &vp
)) != 0)
1873 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap
->path
);
1875 vfs_cache_setroot(vp
, cache_hold(nch
));
1885 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1886 * determine whether it is legal to chdir to the vnode. The vnode's state
1887 * is not changed by this call.
1890 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1894 if (vp
->v_type
!= VDIR
)
1897 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1902 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1904 struct thread
*td
= curthread
;
1905 struct proc
*p
= td
->td_proc
;
1906 struct lwp
*lp
= td
->td_lwp
;
1907 struct filedesc
*fdp
= p
->p_fd
;
1912 int type
, indx
, error
= 0;
1915 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1917 flags
= FFLAGS(oflags
);
1918 error
= falloc(lp
, &nfp
, NULL
);
1922 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1925 * XXX p_dupfd is a real mess. It allows a device to return a
1926 * file descriptor to be duplicated rather then doing the open
1932 * Call vn_open() to do the lookup and assign the vnode to the
1933 * file pointer. vn_open() does not change the ref count on fp
1934 * and the vnode, on success, will be inherited by the file pointer
1937 * Request a shared lock on the vnode if possible.
1939 nd
->nl_flags
|= NLC_LOCKVP
;
1940 if ((flags
& (O_CREAT
|O_TRUNC
)) == 0)
1941 nd
->nl_flags
|= NLC_SHAREDLOCK
;
1943 error
= vn_open(nd
, fp
, flags
, cmode
);
1948 * handle special fdopen() case. bleh. dupfdopen() is
1949 * responsible for dropping the old contents of ofiles[indx]
1952 * Note that fsetfd() will add a ref to fp which represents
1953 * the fd_files[] assignment. We must still drop our
1956 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1957 if (fdalloc(p
, 0, &indx
) == 0) {
1958 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1961 fdrop(fp
); /* our ref */
1964 fsetfd(fdp
, NULL
, indx
);
1967 fdrop(fp
); /* our ref */
1968 if (error
== ERESTART
)
1974 * ref the vnode for ourselves so it can't be ripped out from under
1975 * is. XXX need an ND flag to request that the vnode be returned
1978 * Reserve a file descriptor but do not assign it until the open
1981 vp
= (struct vnode
*)fp
->f_data
;
1983 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1990 * If no error occurs the vp will have been assigned to the file
1995 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1996 lf
.l_whence
= SEEK_SET
;
1999 if (flags
& O_EXLOCK
)
2000 lf
.l_type
= F_WRLCK
;
2002 lf
.l_type
= F_RDLCK
;
2003 if (flags
& FNONBLOCK
)
2008 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
2010 * lock request failed. Clean up the reserved
2014 fsetfd(fdp
, NULL
, indx
);
2018 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
2022 * Assert that all regular file vnodes were created with a object.
2024 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
2025 ("open: regular file has no backing object after vn_open"));
2031 * release our private reference, leaving the one associated with the
2032 * descriptor table intact.
2034 if (oflags
& O_CLOEXEC
)
2035 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
2036 fsetfd(fdp
, fp
, indx
);
2043 * open_args(char *path, int flags, int mode)
2045 * Check permissions, allocate an open file structure,
2046 * and call the device open routine if any.
2049 sys_open(struct open_args
*uap
)
2051 struct nlookupdata nd
;
2054 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2056 error
= kern_open(&nd
, uap
->flags
,
2057 uap
->mode
, &uap
->sysmsg_result
);
2064 * openat_args(int fd, char *path, int flags, int mode)
2067 sys_openat(struct openat_args
*uap
)
2069 struct nlookupdata nd
;
2073 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2075 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
2076 &uap
->sysmsg_result
);
2078 nlookup_done_at(&nd
, fp
);
2083 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
2085 struct thread
*td
= curthread
;
2086 struct proc
*p
= td
->td_proc
;
2095 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2096 vattr
.va_rmajor
= rmajor
;
2097 vattr
.va_rminor
= rminor
;
2099 switch (mode
& S_IFMT
) {
2100 case S_IFMT
: /* used by badsect to flag bad sectors */
2101 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
2102 vattr
.va_type
= VBAD
;
2105 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2106 vattr
.va_type
= VCHR
;
2109 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
2110 vattr
.va_type
= VBLK
;
2113 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
2116 case S_IFDIR
: /* special directories support for HAMMER */
2117 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
2118 vattr
.va_type
= VDIR
;
2129 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2130 if ((error
= nlookup(nd
)) != 0)
2132 if (nd
->nl_nch
.ncp
->nc_vp
)
2134 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2138 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2139 nd
->nl_cred
, NAMEI_CREATE
);
2142 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2143 &vp
, nd
->nl_cred
, &vattr
);
2151 * mknod_args(char *path, int mode, int dev)
2153 * Create a special file.
2156 sys_mknod(struct mknod_args
*uap
)
2158 struct nlookupdata nd
;
2161 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2163 error
= kern_mknod(&nd
, uap
->mode
,
2164 umajor(uap
->dev
), uminor(uap
->dev
));
2171 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2173 * Create a special file. The path is relative to the directory associated
2177 sys_mknodat(struct mknodat_args
*uap
)
2179 struct nlookupdata nd
;
2183 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2185 error
= kern_mknod(&nd
, uap
->mode
,
2186 umajor(uap
->dev
), uminor(uap
->dev
));
2188 nlookup_done_at(&nd
, fp
);
2193 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2195 struct thread
*td
= curthread
;
2196 struct proc
*p
= td
->td_proc
;
2203 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2204 if ((error
= nlookup(nd
)) != 0)
2206 if (nd
->nl_nch
.ncp
->nc_vp
)
2208 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2212 vattr
.va_type
= VFIFO
;
2213 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2215 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2222 * mkfifo_args(char *path, int mode)
2224 * Create a named pipe.
2227 sys_mkfifo(struct mkfifo_args
*uap
)
2229 struct nlookupdata nd
;
2232 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2234 error
= kern_mkfifo(&nd
, uap
->mode
);
2240 * mkfifoat_args(int fd, char *path, mode_t mode)
2242 * Create a named pipe. The path is relative to the directory associated
2246 sys_mkfifoat(struct mkfifoat_args
*uap
)
2248 struct nlookupdata nd
;
2252 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2254 error
= kern_mkfifo(&nd
, uap
->mode
);
2255 nlookup_done_at(&nd
, fp
);
2259 static int hardlink_check_uid
= 0;
2260 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2261 &hardlink_check_uid
, 0,
2262 "Unprivileged processes cannot create hard links to files owned by other "
2264 static int hardlink_check_gid
= 0;
2265 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2266 &hardlink_check_gid
, 0,
2267 "Unprivileged processes cannot create hard links to files owned by other "
2271 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2277 * Shortcut if disabled
2279 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2283 * Privileged user can always hardlink
2285 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2289 * Otherwise only if the originating file is owned by the
2290 * same user or group. Note that any group is allowed if
2291 * the file is owned by the caller.
2293 error
= VOP_GETATTR(vp
, &va
);
2297 if (hardlink_check_uid
) {
2298 if (cred
->cr_uid
!= va
.va_uid
)
2302 if (hardlink_check_gid
) {
2303 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2311 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2313 struct thread
*td
= curthread
;
2318 * Lookup the source and obtained a locked vnode.
2320 * You may only hardlink a file which you have write permission
2321 * on or which you own.
2323 * XXX relookup on vget failure / race ?
2326 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2327 if ((error
= nlookup(nd
)) != 0)
2329 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2330 KKASSERT(vp
!= NULL
);
2331 if (vp
->v_type
== VDIR
)
2332 return (EPERM
); /* POSIX */
2333 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2335 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2339 * Unlock the source so we can lookup the target without deadlocking
2340 * (XXX vp is locked already, possible other deadlock?). The target
2343 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2344 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2345 cache_unlock(&nd
->nl_nch
);
2348 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2349 if ((error
= nlookup(linknd
)) != 0) {
2353 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2357 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
2364 * Finally run the new API VOP.
2366 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2368 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2369 vp
, linknd
->nl_cred
);
2376 * link_args(char *path, char *link)
2378 * Make a hard file link.
2381 sys_link(struct link_args
*uap
)
2383 struct nlookupdata nd
, linknd
;
2386 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2388 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2390 error
= kern_link(&nd
, &linknd
);
2391 nlookup_done(&linknd
);
2398 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2400 * Make a hard file link. The path1 argument is relative to the directory
2401 * associated with fd1, and similarly the path2 argument is relative to
2402 * the directory associated with fd2.
2405 sys_linkat(struct linkat_args
*uap
)
2407 struct nlookupdata nd
, linknd
;
2408 struct file
*fp1
, *fp2
;
2411 error
= nlookup_init_at(&nd
, &fp1
, uap
->fd1
, uap
->path1
, UIO_USERSPACE
,
2412 (uap
->flags
& AT_SYMLINK_FOLLOW
) ? NLC_FOLLOW
: 0);
2414 error
= nlookup_init_at(&linknd
, &fp2
, uap
->fd2
,
2415 uap
->path2
, UIO_USERSPACE
, 0);
2417 error
= kern_link(&nd
, &linknd
);
2418 nlookup_done_at(&linknd
, fp2
);
2420 nlookup_done_at(&nd
, fp1
);
2425 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2433 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2434 if ((error
= nlookup(nd
)) != 0)
2436 if (nd
->nl_nch
.ncp
->nc_vp
)
2438 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2442 vattr
.va_mode
= mode
;
2443 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2450 * symlink(char *path, char *link)
2452 * Make a symbolic link.
2455 sys_symlink(struct symlink_args
*uap
)
2457 struct thread
*td
= curthread
;
2458 struct nlookupdata nd
;
2463 path
= objcache_get(namei_oc
, M_WAITOK
);
2464 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2466 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2468 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2469 error
= kern_symlink(&nd
, path
, mode
);
2473 objcache_put(namei_oc
, path
);
2478 * symlinkat_args(char *path1, int fd, char *path2)
2480 * Make a symbolic link. The path2 argument is relative to the directory
2481 * associated with fd.
2484 sys_symlinkat(struct symlinkat_args
*uap
)
2486 struct thread
*td
= curthread
;
2487 struct nlookupdata nd
;
2493 path1
= objcache_get(namei_oc
, M_WAITOK
);
2494 error
= copyinstr(uap
->path1
, path1
, MAXPATHLEN
, NULL
);
2496 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path2
,
2499 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2500 error
= kern_symlink(&nd
, path1
, mode
);
2502 nlookup_done_at(&nd
, fp
);
2504 objcache_put(namei_oc
, path1
);
2509 * undelete_args(char *path)
2511 * Delete a whiteout from the filesystem.
2514 sys_undelete(struct undelete_args
*uap
)
2516 struct nlookupdata nd
;
2519 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2521 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2523 error
= nlookup(&nd
);
2525 error
= ncp_writechk(&nd
.nl_nch
);
2527 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2535 kern_unlink(struct nlookupdata
*nd
)
2540 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2541 if ((error
= nlookup(nd
)) != 0)
2543 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2545 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2550 * unlink_args(char *path)
2552 * Delete a name from the filesystem.
2555 sys_unlink(struct unlink_args
*uap
)
2557 struct nlookupdata nd
;
2560 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2562 error
= kern_unlink(&nd
);
2569 * unlinkat_args(int fd, char *path, int flags)
2571 * Delete the file or directory entry pointed to by fd/path.
2574 sys_unlinkat(struct unlinkat_args
*uap
)
2576 struct nlookupdata nd
;
2580 if (uap
->flags
& ~AT_REMOVEDIR
)
2583 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2585 if (uap
->flags
& AT_REMOVEDIR
)
2586 error
= kern_rmdir(&nd
);
2588 error
= kern_unlink(&nd
);
2590 nlookup_done_at(&nd
, fp
);
2595 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2597 struct thread
*td
= curthread
;
2598 struct proc
*p
= td
->td_proc
;
2605 fp
= holdfp(p
->p_fd
, fd
, -1);
2608 if (fp
->f_type
!= DTYPE_VNODE
) {
2612 vp
= (struct vnode
*)fp
->f_data
;
2616 spin_lock(&fp
->f_spin
);
2617 new_offset
= fp
->f_offset
+ offset
;
2621 error
= VOP_GETATTR(vp
, &vattr
);
2622 spin_lock(&fp
->f_spin
);
2623 new_offset
= offset
+ vattr
.va_size
;
2626 new_offset
= offset
;
2628 spin_lock(&fp
->f_spin
);
2633 spin_lock(&fp
->f_spin
);
2638 * Validate the seek position. Negative offsets are not allowed
2639 * for regular files or directories.
2641 * Normally we would also not want to allow negative offsets for
2642 * character and block-special devices. However kvm addresses
2643 * on 64 bit architectures might appear to be negative and must
2647 if (new_offset
< 0 &&
2648 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2651 fp
->f_offset
= new_offset
;
2654 *res
= fp
->f_offset
;
2655 spin_unlock(&fp
->f_spin
);
2662 * lseek_args(int fd, int pad, off_t offset, int whence)
2664 * Reposition read/write file offset.
2667 sys_lseek(struct lseek_args
*uap
)
2671 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2672 &uap
->sysmsg_offset
);
2678 * Check if current process can access given file. amode is a bitmask of *_OK
2679 * access bits. flags is a bitmask of AT_* flags.
2682 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2687 if (flags
& ~AT_EACCESS
)
2689 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2690 if ((error
= nlookup(nd
)) != 0)
2693 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2697 /* Flags == 0 means only check for existence. */
2706 if ((mode
& VWRITE
) == 0 ||
2707 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2708 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2711 * If the file handle is stale we have to re-resolve the
2712 * entry with the ncp held exclusively. This is a hack
2715 if (error
== ESTALE
) {
2717 cache_unlock(&nd
->nl_nch
);
2718 cache_lock(&nd
->nl_nch
);
2719 cache_setunresolved(&nd
->nl_nch
);
2720 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2733 * access_args(char *path, int flags)
2735 * Check access permissions.
2738 sys_access(struct access_args
*uap
)
2740 struct nlookupdata nd
;
2743 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2745 error
= kern_access(&nd
, uap
->flags
, 0);
2752 * eaccess_args(char *path, int flags)
2754 * Check access permissions.
2757 sys_eaccess(struct eaccess_args
*uap
)
2759 struct nlookupdata nd
;
2762 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2764 error
= kern_access(&nd
, uap
->flags
, AT_EACCESS
);
2771 * faccessat_args(int fd, char *path, int amode, int flags)
2773 * Check access permissions.
2776 sys_faccessat(struct faccessat_args
*uap
)
2778 struct nlookupdata nd
;
2782 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2785 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2786 nlookup_done_at(&nd
, fp
);
2791 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2796 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2797 if ((error
= nlookup(nd
)) != 0)
2800 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2803 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2805 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2808 * If the file handle is stale we have to re-resolve the
2809 * entry with the ncp held exclusively. This is a hack
2812 if (error
== ESTALE
) {
2814 cache_unlock(&nd
->nl_nch
);
2815 cache_lock(&nd
->nl_nch
);
2816 cache_setunresolved(&nd
->nl_nch
);
2817 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2827 * stat_args(char *path, struct stat *ub)
2829 * Get file status; this version follows links.
2832 sys_stat(struct stat_args
*uap
)
2834 struct nlookupdata nd
;
2838 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2840 error
= kern_stat(&nd
, &st
);
2842 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2849 * lstat_args(char *path, struct stat *ub)
2851 * Get file status; this version does not follow links.
2854 sys_lstat(struct lstat_args
*uap
)
2856 struct nlookupdata nd
;
2860 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2862 error
= kern_stat(&nd
, &st
);
2864 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2871 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2873 * Get status of file pointed to by fd/path.
2876 sys_fstatat(struct fstatat_args
*uap
)
2878 struct nlookupdata nd
;
2884 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2887 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2889 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2890 UIO_USERSPACE
, flags
);
2892 error
= kern_stat(&nd
, &st
);
2894 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2896 nlookup_done_at(&nd
, fp
);
2901 kern_pathconf(char *path
, int name
, int flags
, register_t
*sysmsg_regp
)
2903 struct nlookupdata nd
;
2908 error
= nlookup_init(&nd
, path
, UIO_USERSPACE
, flags
);
2910 error
= nlookup(&nd
);
2912 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2915 error
= VOP_PATHCONF(vp
, name
, sysmsg_regp
);
2922 * pathconf_Args(char *path, int name)
2924 * Get configurable pathname variables.
2927 sys_pathconf(struct pathconf_args
*uap
)
2929 return (kern_pathconf(uap
->path
, uap
->name
, NLC_FOLLOW
,
2934 * lpathconf_Args(char *path, int name)
2936 * Get configurable pathname variables, but don't follow symlinks.
2939 sys_lpathconf(struct lpathconf_args
*uap
)
2941 return (kern_pathconf(uap
->path
, uap
->name
, 0, &uap
->sysmsg_reg
));
2946 * kern_readlink isn't properly split yet. There is a copyin burried
2947 * in VOP_READLINK().
2950 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2952 struct thread
*td
= curthread
;
2958 nd
->nl_flags
|= NLC_SHAREDLOCK
;
2959 if ((error
= nlookup(nd
)) != 0)
2961 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_SHARED
, &vp
);
2964 if (vp
->v_type
!= VLNK
) {
2967 aiov
.iov_base
= buf
;
2968 aiov
.iov_len
= count
;
2969 auio
.uio_iov
= &aiov
;
2970 auio
.uio_iovcnt
= 1;
2971 auio
.uio_offset
= 0;
2972 auio
.uio_rw
= UIO_READ
;
2973 auio
.uio_segflg
= UIO_USERSPACE
;
2975 auio
.uio_resid
= count
;
2976 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
2979 *res
= count
- auio
.uio_resid
;
2984 * readlink_args(char *path, char *buf, int count)
2986 * Return target name of a symbolic link.
2989 sys_readlink(struct readlink_args
*uap
)
2991 struct nlookupdata nd
;
2994 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2996 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2997 &uap
->sysmsg_result
);
3004 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3006 * Return target name of a symbolic link. The path is relative to the
3007 * directory associated with fd.
3010 sys_readlinkat(struct readlinkat_args
*uap
)
3012 struct nlookupdata nd
;
3016 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
3018 error
= kern_readlink(&nd
, uap
->buf
, uap
->bufsize
,
3019 &uap
->sysmsg_result
);
3021 nlookup_done_at(&nd
, fp
);
3026 setfflags(struct vnode
*vp
, int flags
)
3028 struct thread
*td
= curthread
;
3033 * Prevent non-root users from setting flags on devices. When
3034 * a device is reused, users can retain ownership of the device
3035 * if they are allowed to set flags and programs assume that
3036 * chown can't fail when done as root.
3038 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
3039 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
3043 * note: vget is required for any operation that might mod the vnode
3044 * so VINACTIVE is properly cleared.
3046 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3048 vattr
.va_flags
= flags
;
3049 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3056 * chflags(char *path, int flags)
3058 * Change flags of a file given a path name.
3061 sys_chflags(struct chflags_args
*uap
)
3063 struct nlookupdata nd
;
3068 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3070 error
= nlookup(&nd
);
3072 error
= ncp_writechk(&nd
.nl_nch
);
3074 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3077 error
= setfflags(vp
, uap
->flags
);
3084 * lchflags(char *path, int flags)
3086 * Change flags of a file given a path name, but don't follow symlinks.
3089 sys_lchflags(struct lchflags_args
*uap
)
3091 struct nlookupdata nd
;
3096 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3098 error
= nlookup(&nd
);
3100 error
= ncp_writechk(&nd
.nl_nch
);
3102 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3105 error
= setfflags(vp
, uap
->flags
);
3112 * fchflags_args(int fd, int flags)
3114 * Change flags of a file given a file descriptor.
3117 sys_fchflags(struct fchflags_args
*uap
)
3119 struct thread
*td
= curthread
;
3120 struct proc
*p
= td
->td_proc
;
3124 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3126 if (fp
->f_nchandle
.ncp
)
3127 error
= ncp_writechk(&fp
->f_nchandle
);
3129 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
3135 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3136 * change flags given a pathname relative to a filedescriptor
3138 int sys_chflagsat(struct chflagsat_args
*uap
)
3140 struct nlookupdata nd
;
3146 if (uap
->atflags
& ~AT_SYMLINK_NOFOLLOW
)
3149 lookupflags
= (uap
->atflags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3152 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, lookupflags
);
3154 error
= nlookup(&nd
);
3156 error
= ncp_writechk(&nd
.nl_nch
);
3158 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3159 nlookup_done_at(&nd
, fp
);
3161 error
= setfflags(vp
, uap
->flags
);
3169 setfmode(struct vnode
*vp
, int mode
)
3171 struct thread
*td
= curthread
;
3176 * note: vget is required for any operation that might mod the vnode
3177 * so VINACTIVE is properly cleared.
3179 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3181 vattr
.va_mode
= mode
& ALLPERMS
;
3182 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3183 cache_inval_wxok(vp
);
3190 kern_chmod(struct nlookupdata
*nd
, int mode
)
3195 if ((error
= nlookup(nd
)) != 0)
3197 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3199 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3200 error
= setfmode(vp
, mode
);
3206 * chmod_args(char *path, int mode)
3208 * Change mode of a file given path name.
3211 sys_chmod(struct chmod_args
*uap
)
3213 struct nlookupdata nd
;
3216 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3218 error
= kern_chmod(&nd
, uap
->mode
);
3224 * lchmod_args(char *path, int mode)
3226 * Change mode of a file given path name (don't follow links.)
3229 sys_lchmod(struct lchmod_args
*uap
)
3231 struct nlookupdata nd
;
3234 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3236 error
= kern_chmod(&nd
, uap
->mode
);
3242 * fchmod_args(int fd, int mode)
3244 * Change mode of a file given a file descriptor.
3247 sys_fchmod(struct fchmod_args
*uap
)
3249 struct thread
*td
= curthread
;
3250 struct proc
*p
= td
->td_proc
;
3254 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3256 if (fp
->f_nchandle
.ncp
)
3257 error
= ncp_writechk(&fp
->f_nchandle
);
3259 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3265 * fchmodat_args(char *path, int mode)
3267 * Change mode of a file pointed to by fd/path.
3270 sys_fchmodat(struct fchmodat_args
*uap
)
3272 struct nlookupdata nd
;
3277 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3279 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3281 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3282 UIO_USERSPACE
, flags
);
3284 error
= kern_chmod(&nd
, uap
->mode
);
3285 nlookup_done_at(&nd
, fp
);
3290 setfown(struct mount
*mp
, struct vnode
*vp
, uid_t uid
, gid_t gid
)
3292 struct thread
*td
= curthread
;
3300 * note: vget is required for any operation that might mod the vnode
3301 * so VINACTIVE is properly cleared.
3303 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3304 if ((error
= VOP_GETATTR(vp
, &vattr
)) != 0)
3306 o_uid
= vattr
.va_uid
;
3307 o_gid
= vattr
.va_gid
;
3308 size
= vattr
.va_size
;
3313 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3322 VFS_ACCOUNT(mp
, o_uid
, o_gid
, -size
);
3323 VFS_ACCOUNT(mp
, uid
, gid
, size
);
3330 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3335 if ((error
= nlookup(nd
)) != 0)
3337 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3339 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3340 error
= setfown(nd
->nl_nch
.mount
, vp
, uid
, gid
);
3346 * chown(char *path, int uid, int gid)
3348 * Set ownership given a path name.
3351 sys_chown(struct chown_args
*uap
)
3353 struct nlookupdata nd
;
3356 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3358 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3364 * lchown_args(char *path, int uid, int gid)
3366 * Set ownership given a path name, do not cross symlinks.
3369 sys_lchown(struct lchown_args
*uap
)
3371 struct nlookupdata nd
;
3374 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3376 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3382 * fchown_args(int fd, int uid, int gid)
3384 * Set ownership given a file descriptor.
3387 sys_fchown(struct fchown_args
*uap
)
3389 struct thread
*td
= curthread
;
3390 struct proc
*p
= td
->td_proc
;
3394 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3396 if (fp
->f_nchandle
.ncp
)
3397 error
= ncp_writechk(&fp
->f_nchandle
);
3399 error
= setfown(p
->p_fd
->fd_ncdir
.mount
,
3400 (struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3406 * fchownat(int fd, char *path, int uid, int gid, int flags)
3408 * Set ownership of file pointed to by fd/path.
3411 sys_fchownat(struct fchownat_args
*uap
)
3413 struct nlookupdata nd
;
3418 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3420 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3422 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3423 UIO_USERSPACE
, flags
);
3425 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3426 nlookup_done_at(&nd
, fp
);
3432 getutimes(struct timeval
*tvp
, struct timespec
*tsp
)
3434 struct timeval tv
[2];
3439 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3442 if ((error
= itimerfix(tvp
)) != 0)
3444 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3445 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3451 getutimens(const struct timespec
*ts
, struct timespec
*newts
, int *nullflag
)
3453 struct timespec tsnow
;
3467 if (newts
[0].tv_nsec
== UTIME_OMIT
&& newts
[1].tv_nsec
== UTIME_OMIT
)
3469 if (newts
[0].tv_nsec
== UTIME_NOW
&& newts
[1].tv_nsec
== UTIME_NOW
)
3472 if (newts
[0].tv_nsec
== UTIME_OMIT
)
3473 newts
[0].tv_sec
= VNOVAL
;
3474 else if (newts
[0].tv_nsec
== UTIME_NOW
)
3476 else if ((error
= itimespecfix(&newts
[0])) != 0)
3479 if (newts
[1].tv_nsec
== UTIME_OMIT
)
3480 newts
[1].tv_sec
= VNOVAL
;
3481 else if (newts
[1].tv_nsec
== UTIME_NOW
)
3483 else if ((error
= itimespecfix(&newts
[1])) != 0)
3490 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3491 const struct timespec
*ts
, int nullflag
)
3493 struct thread
*td
= curthread
;
3497 vattr
->va_atime
= ts
[0];
3498 vattr
->va_mtime
= ts
[1];
3500 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3501 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3507 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3509 struct timespec ts
[2];
3513 if ((error
= getutimes(tptr
, ts
)) != 0)
3516 error
= kern_utimensat(nd
, tptr
? ts
: NULL
, 0);
3521 * utimes_args(char *path, struct timeval *tptr)
3523 * Set the access and modification times of a file.
3526 sys_utimes(struct utimes_args
*uap
)
3528 struct timeval tv
[2];
3529 struct nlookupdata nd
;
3533 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3537 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3539 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3545 * lutimes_args(char *path, struct timeval *tptr)
3547 * Set the access and modification times of a file.
3550 sys_lutimes(struct lutimes_args
*uap
)
3552 struct timeval tv
[2];
3553 struct nlookupdata nd
;
3557 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3561 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3563 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3569 * Set utimes on a file descriptor. The creds used to open the
3570 * file are used to determine whether the operation is allowed
3574 kern_futimens(int fd
, struct timespec
*ts
)
3576 struct thread
*td
= curthread
;
3577 struct proc
*p
= td
->td_proc
;
3578 struct timespec newts
[2];
3585 error
= getutimens(ts
, newts
, &nullflag
);
3588 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3590 if (fp
->f_nchandle
.ncp
)
3591 error
= ncp_writechk(&fp
->f_nchandle
);
3594 error
= vget(vp
, LK_EXCLUSIVE
);
3596 error
= VOP_GETATTR(vp
, &vattr
);
3598 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3602 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3612 * futimens_args(int fd, struct timespec *ts)
3614 * Set the access and modification times of a file.
3617 sys_futimens(struct futimens_args
*uap
)
3619 struct timespec ts
[2];
3623 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3627 error
= kern_futimens(uap
->fd
, uap
->ts
? ts
: NULL
);
3632 kern_futimes(int fd
, struct timeval
*tptr
)
3634 struct timespec ts
[2];
3638 if ((error
= getutimes(tptr
, ts
)) != 0)
3641 error
= kern_futimens(fd
, tptr
? ts
: NULL
);
3646 * futimes_args(int fd, struct timeval *tptr)
3648 * Set the access and modification times of a file.
3651 sys_futimes(struct futimes_args
*uap
)
3653 struct timeval tv
[2];
3657 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3661 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3666 kern_utimensat(struct nlookupdata
*nd
, const struct timespec
*ts
, int flags
)
3668 struct timespec newts
[2];
3674 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
3677 error
= getutimens(ts
, newts
, &nullflag
);
3681 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3682 if ((error
= nlookup(nd
)) != 0)
3684 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3686 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3688 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3689 error
= vget(vp
, LK_EXCLUSIVE
);
3691 error
= setutimes(vp
, &vattr
, newts
, nullflag
);
3700 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3702 * Set file access and modification times of a file.
3705 sys_utimensat(struct utimensat_args
*uap
)
3707 struct timespec ts
[2];
3708 struct nlookupdata nd
;
3714 error
= copyin(uap
->ts
, ts
, sizeof(ts
));
3719 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3720 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3721 UIO_USERSPACE
, flags
);
3723 error
= kern_utimensat(&nd
, uap
->ts
? ts
: NULL
, uap
->flags
);
3724 nlookup_done_at(&nd
, fp
);
3729 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3736 uint64_t old_size
= 0;
3740 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3741 if ((error
= nlookup(nd
)) != 0)
3743 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3745 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3747 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
| LK_FAILRECLAIM
);
3752 if (vp
->v_type
== VDIR
) {
3756 if (vfs_quota_enabled
) {
3757 error
= VOP_GETATTR(vp
, &vattr
);
3758 KASSERT(error
== 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3761 old_size
= vattr
.va_size
;
3764 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3766 vattr
.va_size
= length
;
3767 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3768 VFS_ACCOUNT(nd
->nl_nch
.mount
, uid
, gid
, length
- old_size
);
3776 * truncate(char *path, int pad, off_t length)
3778 * Truncate a file given its path name.
3781 sys_truncate(struct truncate_args
*uap
)
3783 struct nlookupdata nd
;
3786 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3788 error
= kern_truncate(&nd
, uap
->length
);
3794 kern_ftruncate(int fd
, off_t length
)
3796 struct thread
*td
= curthread
;
3797 struct proc
*p
= td
->td_proc
;
3804 uint64_t old_size
= 0;
3809 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3811 if (fp
->f_nchandle
.ncp
) {
3812 error
= ncp_writechk(&fp
->f_nchandle
);
3816 if ((fp
->f_flag
& FWRITE
) == 0) {
3820 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3824 vp
= (struct vnode
*)fp
->f_data
;
3825 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3826 if (vp
->v_type
== VDIR
) {
3832 if (vfs_quota_enabled
) {
3833 error
= VOP_GETATTR(vp
, &vattr
);
3834 KASSERT(error
== 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3837 old_size
= vattr
.va_size
;
3840 if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3842 vattr
.va_size
= length
;
3843 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3845 VFS_ACCOUNT(mp
, uid
, gid
, length
- old_size
);
3854 * ftruncate_args(int fd, int pad, off_t length)
3856 * Truncate a file given a file descriptor.
3859 sys_ftruncate(struct ftruncate_args
*uap
)
3863 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3871 * Sync an open file.
3874 sys_fsync(struct fsync_args
*uap
)
3876 struct thread
*td
= curthread
;
3877 struct proc
*p
= td
->td_proc
;
3883 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3885 vp
= (struct vnode
*)fp
->f_data
;
3886 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3887 if ((obj
= vp
->v_object
) != NULL
) {
3888 if (vp
->v_mount
== NULL
||
3889 (vp
->v_mount
->mnt_kern_flag
& MNTK_NOMSYNC
) == 0) {
3890 vm_object_page_clean(obj
, 0, 0, 0);
3893 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3894 if (error
== 0 && vp
->v_mount
)
3895 error
= buf_fsync(vp
);
3903 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3905 struct nchandle fnchd
;
3906 struct nchandle tnchd
;
3907 struct namecache
*ncp
;
3916 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3917 if ((error
= nlookup(fromnd
)) != 0)
3919 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3921 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3925 * unlock the source nch so we can lookup the target nch without
3926 * deadlocking. The target may or may not exist so we do not check
3927 * for a target vp like kern_mkdir() and other creation functions do.
3929 * The source and target directories are ref'd and rechecked after
3930 * everything is relocked to determine if the source or target file
3933 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3934 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3936 fncp_gen
= fromnd
->nl_nch
.ncp
->nc_generation
;
3938 cache_unlock(&fromnd
->nl_nch
);
3940 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3941 if ((error
= nlookup(tond
)) != 0) {
3945 tncp_gen
= tond
->nl_nch
.ncp
->nc_generation
;
3947 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3951 tnchd
.mount
= tond
->nl_nch
.mount
;
3955 * If the source and target are the same there is nothing to do
3957 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
3964 * Mount points cannot be renamed or overwritten
3966 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
3975 * Relock the source ncp. cache_relock() will deal with any
3976 * deadlocks against the already-locked tond and will also
3977 * make sure both are resolved.
3979 * NOTE AFTER RELOCKING: The source or target ncp may have become
3980 * invalid while they were unlocked, nc_vp and nc_mount could
3983 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
3984 &tond
->nl_nch
, tond
->nl_cred
);
3985 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
3988 * If the namecache generation changed for either fromnd or tond,
3991 if (fromnd
->nl_nch
.ncp
->nc_generation
!= fncp_gen
||
3992 tond
->nl_nch
.ncp
->nc_generation
!= tncp_gen
) {
3993 kprintf("kern_rename: retry due to gen on: "
3994 "\"%s\" -> \"%s\"\n",
3995 fromnd
->nl_nch
.ncp
->nc_name
,
3996 tond
->nl_nch
.ncp
->nc_name
);
4003 * If either fromnd or tond are marked destroyed a ripout occured
4004 * out from under us and we must retry.
4006 if ((fromnd
->nl_nch
.ncp
->nc_flag
& (NCF_DESTROYED
| NCF_UNRESOLVED
)) ||
4007 fromnd
->nl_nch
.ncp
->nc_vp
== NULL
||
4008 (tond
->nl_nch
.ncp
->nc_flag
& NCF_DESTROYED
)) {
4009 kprintf("kern_rename: retry due to ripout on: "
4010 "\"%s\" -> \"%s\"\n",
4011 fromnd
->nl_nch
.ncp
->nc_name
,
4012 tond
->nl_nch
.ncp
->nc_name
);
4019 * Make sure the parent directories linkages are the same.
4020 * XXX shouldn't be needed any more w/ generation check above.
4022 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
4023 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
4030 * Both the source and target must be within the same filesystem and
4031 * in the same filesystem as their parent directories within the
4032 * namecache topology.
4034 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4037 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
4038 mp
!= tond
->nl_nch
.mount
) {
4045 * Make sure the mount point is writable
4047 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
4054 * If the target exists and either the source or target is a directory,
4055 * then both must be directories.
4057 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4060 if (tond
->nl_nch
.ncp
->nc_vp
) {
4061 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
4063 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4064 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
4066 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
4072 * You cannot rename a source into itself or a subdirectory of itself.
4073 * We check this by travsersing the target directory upwards looking
4074 * for a match against the source.
4079 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
4080 if (fromnd
->nl_nch
.ncp
== ncp
) {
4091 * Even though the namespaces are different, they may still represent
4092 * hardlinks to the same file. The filesystem might have a hard time
4093 * with this so we issue a NREMOVE of the source instead of a NRENAME
4094 * when we detect the situation.
4097 fdvp
= fromnd
->nl_dvp
;
4098 tdvp
= tond
->nl_dvp
;
4099 if (fdvp
== NULL
|| tdvp
== NULL
) {
4101 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
4102 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
4105 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
4106 fdvp
, tdvp
, tond
->nl_cred
);
4113 * rename_args(char *from, char *to)
4115 * Rename files. Source and destination must either both be directories,
4116 * or both not be directories. If target is a directory, it must be empty.
4119 sys_rename(struct rename_args
*uap
)
4121 struct nlookupdata fromnd
, tond
;
4125 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
4127 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
4129 error
= kern_rename(&fromnd
, &tond
);
4130 nlookup_done(&tond
);
4132 nlookup_done(&fromnd
);
4133 } while (error
== EAGAIN
);
4138 * renameat_args(int oldfd, char *old, int newfd, char *new)
4140 * Rename files using paths relative to the directories associated with
4141 * oldfd and newfd. Source and destination must either both be directories,
4142 * or both not be directories. If target is a directory, it must be empty.
4145 sys_renameat(struct renameat_args
*uap
)
4147 struct nlookupdata oldnd
, newnd
;
4148 struct file
*oldfp
, *newfp
;
4152 error
= nlookup_init_at(&oldnd
, &oldfp
,
4153 uap
->oldfd
, uap
->old
,
4156 error
= nlookup_init_at(&newnd
, &newfp
,
4157 uap
->newfd
, uap
->new,
4160 error
= kern_rename(&oldnd
, &newnd
);
4161 nlookup_done_at(&newnd
, newfp
);
4163 nlookup_done_at(&oldnd
, oldfp
);
4164 } while (error
== EAGAIN
);
4169 kern_mkdir(struct nlookupdata
*nd
, int mode
)
4171 struct thread
*td
= curthread
;
4172 struct proc
*p
= td
->td_proc
;
4178 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
4179 if ((error
= nlookup(nd
)) != 0)
4182 if (nd
->nl_nch
.ncp
->nc_vp
)
4184 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4187 vattr
.va_type
= VDIR
;
4188 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
4191 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
4198 * mkdir_args(char *path, int mode)
4200 * Make a directory file.
4203 sys_mkdir(struct mkdir_args
*uap
)
4205 struct nlookupdata nd
;
4208 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4210 error
= kern_mkdir(&nd
, uap
->mode
);
4216 * mkdirat_args(int fd, char *path, mode_t mode)
4218 * Make a directory file. The path is relative to the directory associated
4222 sys_mkdirat(struct mkdirat_args
*uap
)
4224 struct nlookupdata nd
;
4228 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
4230 error
= kern_mkdir(&nd
, uap
->mode
);
4231 nlookup_done_at(&nd
, fp
);
4236 kern_rmdir(struct nlookupdata
*nd
)
4241 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
4242 if ((error
= nlookup(nd
)) != 0)
4246 * Do not allow directories representing mount points to be
4247 * deleted, even if empty. Check write perms on mount point
4248 * in case the vnode is aliased (aka nullfs).
4250 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
4252 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
4254 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
4259 * rmdir_args(char *path)
4261 * Remove a directory file.
4264 sys_rmdir(struct rmdir_args
*uap
)
4266 struct nlookupdata nd
;
4269 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
4271 error
= kern_rmdir(&nd
);
4277 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
4278 enum uio_seg direction
)
4280 struct thread
*td
= curthread
;
4281 struct proc
*p
= td
->td_proc
;
4289 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
4291 if ((fp
->f_flag
& FREAD
) == 0) {
4295 vp
= (struct vnode
*)fp
->f_data
;
4296 if (vp
->v_type
!= VDIR
) {
4300 aiov
.iov_base
= buf
;
4301 aiov
.iov_len
= count
;
4302 auio
.uio_iov
= &aiov
;
4303 auio
.uio_iovcnt
= 1;
4304 auio
.uio_rw
= UIO_READ
;
4305 auio
.uio_segflg
= direction
;
4307 auio
.uio_resid
= count
;
4308 loff
= auio
.uio_offset
= fp
->f_offset
;
4309 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
4310 fp
->f_offset
= auio
.uio_offset
;
4315 * WARNING! *basep may not be wide enough to accomodate the
4316 * seek offset. XXX should we hack this to return the upper 32 bits
4317 * for offsets greater then 4G?
4320 *basep
= (long)loff
;
4322 *res
= count
- auio
.uio_resid
;
4329 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4331 * Read a block of directory entries in a file system independent format.
4334 sys_getdirentries(struct getdirentries_args
*uap
)
4339 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
4340 &uap
->sysmsg_result
, UIO_USERSPACE
);
4342 if (error
== 0 && uap
->basep
)
4343 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
4348 * getdents_args(int fd, char *buf, size_t count)
4351 sys_getdents(struct getdents_args
*uap
)
4355 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
4356 &uap
->sysmsg_result
, UIO_USERSPACE
);
4362 * Set the mode mask for creation of filesystem nodes.
4364 * umask(int newmask)
4367 sys_umask(struct umask_args
*uap
)
4369 struct thread
*td
= curthread
;
4370 struct proc
*p
= td
->td_proc
;
4371 struct filedesc
*fdp
;
4374 uap
->sysmsg_result
= fdp
->fd_cmask
;
4375 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
4380 * revoke(char *path)
4382 * Void all references to file by ripping underlying filesystem
4386 sys_revoke(struct revoke_args
*uap
)
4388 struct nlookupdata nd
;
4395 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4397 error
= nlookup(&nd
);
4399 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4400 cred
= crhold(nd
.nl_cred
);
4404 error
= VOP_GETATTR(vp
, &vattr
);
4405 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4406 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4407 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4409 error
= vrevoke(vp
, cred
);
4410 } else if (error
== 0) {
4411 error
= vrevoke(vp
, cred
);
4421 * getfh_args(char *fname, fhandle_t *fhp)
4423 * Get (NFS) file handle
4425 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4426 * mount. This allows nullfs mounts to be explicitly exported.
4428 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4430 * nullfs mounts of subdirectories are not safe. That is, it will
4431 * work, but you do not really have protection against access to
4432 * the related parent directories.
4435 sys_getfh(struct getfh_args
*uap
)
4437 struct thread
*td
= curthread
;
4438 struct nlookupdata nd
;
4445 * Must be super user
4447 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4451 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4453 error
= nlookup(&nd
);
4455 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4456 mp
= nd
.nl_nch
.mount
;
4459 bzero(&fh
, sizeof(fh
));
4460 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4461 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4464 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4470 * fhopen_args(const struct fhandle *u_fhp, int flags)
4472 * syscall for the rpc.lockd to use to translate a NFS file handle into
4473 * an open descriptor.
4475 * warning: do not remove the priv_check() call or this becomes one giant
4479 sys_fhopen(struct fhopen_args
*uap
)
4481 struct thread
*td
= curthread
;
4482 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4487 struct vattr
*vap
= &vat
;
4489 int fmode
, mode
, error
= 0, type
;
4495 * Must be super user
4497 error
= priv_check(td
, PRIV_ROOT
);
4501 fmode
= FFLAGS(uap
->flags
);
4504 * Why not allow a non-read/write open for our lockd?
4506 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4508 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4513 * Find the mount point
4515 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4520 /* now give me my vnode, it gets returned to me locked */
4521 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4525 * from now on we have to make sure not
4526 * to forget about the vnode
4527 * any error that causes an abort must vput(vp)
4528 * just set error = err and 'goto bad;'.
4534 if (vp
->v_type
== VLNK
) {
4538 if (vp
->v_type
== VSOCK
) {
4543 if (fmode
& (FWRITE
| O_TRUNC
)) {
4544 if (vp
->v_type
== VDIR
) {
4548 error
= vn_writechk(vp
, NULL
);
4556 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4560 if (fmode
& O_TRUNC
) {
4561 vn_unlock(vp
); /* XXX */
4562 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4565 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4571 * VOP_OPEN needs the file pointer so it can potentially override
4574 * WARNING! no f_nchandle will be associated when fhopen()ing a
4577 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4581 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4584 * setting f_ops this way prevents VOP_CLOSE from being
4585 * called or fdrop() releasing the vp from v_data. Since
4586 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4588 fp
->f_ops
= &badfileops
;
4594 * The fp is given its own reference, we still have our ref and lock.
4596 * Assert that all regular files must be created with a VM object.
4598 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4599 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4604 * The open was successful. Handle any locking requirements.
4606 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4607 lf
.l_whence
= SEEK_SET
;
4610 if (fmode
& O_EXLOCK
)
4611 lf
.l_type
= F_WRLCK
;
4613 lf
.l_type
= F_RDLCK
;
4614 if (fmode
& FNONBLOCK
)
4619 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4621 * release our private reference.
4623 fsetfd(fdp
, NULL
, indx
);
4628 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4629 atomic_set_int(&fp
->f_flag
, FHASLOCK
); /* race ok */
4633 * Clean up. Associate the file pointer with the previously
4634 * reserved descriptor and return it.
4637 if (uap
->flags
& O_CLOEXEC
)
4638 fdp
->fd_files
[indx
].fileflags
|= UF_EXCLOSE
;
4639 fsetfd(fdp
, fp
, indx
);
4641 uap
->sysmsg_result
= indx
;
4645 fsetfd(fdp
, NULL
, indx
);
4654 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4657 sys_fhstat(struct fhstat_args
*uap
)
4659 struct thread
*td
= curthread
;
4667 * Must be super user
4669 error
= priv_check(td
, PRIV_ROOT
);
4673 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4677 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4680 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4681 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4686 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4691 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4694 sys_fhstatfs(struct fhstatfs_args
*uap
)
4696 struct thread
*td
= curthread
;
4697 struct proc
*p
= td
->td_proc
;
4702 char *fullpath
, *freepath
;
4707 * Must be super user
4709 if ((error
= priv_check(td
, PRIV_ROOT
)))
4712 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4715 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4719 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4724 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4729 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4732 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4735 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4736 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4737 kfree(freepath
, M_TEMP
);
4739 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4740 if (priv_check(td
, PRIV_ROOT
)) {
4741 bcopy(sp
, &sb
, sizeof(sb
));
4742 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4745 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4751 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4754 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4756 struct thread
*td
= curthread
;
4757 struct proc
*p
= td
->td_proc
;
4765 * Must be super user
4767 if ((error
= priv_check(td
, PRIV_ROOT
)))
4770 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4773 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4777 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4782 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4785 sp
= &mp
->mnt_vstat
;
4787 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4791 if (mp
->mnt_flag
& MNT_RDONLY
)
4792 sp
->f_flag
|= ST_RDONLY
;
4793 if (mp
->mnt_flag
& MNT_NOSUID
)
4794 sp
->f_flag
|= ST_NOSUID
;
4795 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4802 * Syscall to push extended attribute configuration information into the
4803 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4804 * a command (int cmd), and attribute name and misc data. For now, the
4805 * attribute name is left in userspace for consumption by the VFS_op.
4806 * It will probably be changed to be copied into sysspace by the
4807 * syscall in the future, once issues with various consumers of the
4808 * attribute code have raised their hands.
4810 * Currently this is used only by UFS Extended Attributes.
4813 sys_extattrctl(struct extattrctl_args
*uap
)
4815 struct nlookupdata nd
;
4817 char attrname
[EXTATTR_MAXNAMELEN
];
4825 if (error
== 0 && uap
->filename
) {
4826 error
= nlookup_init(&nd
, uap
->filename
, UIO_USERSPACE
,
4829 error
= nlookup(&nd
);
4831 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
4835 if (error
== 0 && uap
->attrname
) {
4836 error
= copyinstr(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
,
4841 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4843 error
= nlookup(&nd
);
4845 error
= ncp_writechk(&nd
.nl_nch
);
4847 error
= VFS_EXTATTRCTL(nd
.nl_nch
.mount
, uap
->cmd
, vp
,
4849 uap
->attrname
, nd
.nl_cred
);
4858 * Syscall to get a named extended attribute on a file or directory.
4861 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4863 char attrname
[EXTATTR_MAXNAMELEN
];
4864 struct nlookupdata nd
;
4870 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4876 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4878 error
= nlookup(&nd
);
4880 error
= ncp_writechk(&nd
.nl_nch
);
4882 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4888 bzero(&auio
, sizeof(auio
));
4889 aiov
.iov_base
= uap
->data
;
4890 aiov
.iov_len
= uap
->nbytes
;
4891 auio
.uio_iov
= &aiov
;
4892 auio
.uio_iovcnt
= 1;
4893 auio
.uio_offset
= 0;
4894 auio
.uio_resid
= uap
->nbytes
;
4895 auio
.uio_rw
= UIO_WRITE
;
4896 auio
.uio_td
= curthread
;
4898 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4907 * Syscall to get a named extended attribute on a file or directory.
4910 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4912 char attrname
[EXTATTR_MAXNAMELEN
];
4913 struct nlookupdata nd
;
4919 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4925 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4927 error
= nlookup(&nd
);
4929 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_SHARED
, &vp
);
4935 bzero(&auio
, sizeof(auio
));
4936 aiov
.iov_base
= uap
->data
;
4937 aiov
.iov_len
= uap
->nbytes
;
4938 auio
.uio_iov
= &aiov
;
4939 auio
.uio_iovcnt
= 1;
4940 auio
.uio_offset
= 0;
4941 auio
.uio_resid
= uap
->nbytes
;
4942 auio
.uio_rw
= UIO_READ
;
4943 auio
.uio_td
= curthread
;
4945 error
= VOP_GETEXTATTR(vp
, uap
->attrnamespace
, attrname
,
4947 uap
->sysmsg_result
= uap
->nbytes
- auio
.uio_resid
;
4955 * Syscall to delete a named extended attribute from a file or directory.
4956 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4959 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
4961 char attrname
[EXTATTR_MAXNAMELEN
];
4962 struct nlookupdata nd
;
4966 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4970 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4972 error
= nlookup(&nd
);
4974 error
= ncp_writechk(&nd
.nl_nch
);
4976 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4978 error
= VOP_SETEXTATTR(vp
, uap
->attrnamespace
,
4979 attrname
, NULL
, nd
.nl_cred
);
4988 * Determine if the mount is visible to the process.
4991 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
4993 struct nchandle nch
;
4996 * Traverse from the mount point upwards. If we hit the process
4997 * root then the mount point is visible to the process.
4999 nch
= mp
->mnt_ncmountpt
;
5001 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
5002 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
5005 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
5006 nch
= nch
.mount
->mnt_ncmounton
;
5008 nch
.ncp
= nch
.ncp
->nc_parent
;
5013 * If the mount point is not visible to the process, but the
5014 * process root is in a subdirectory of the mount, return
5017 if (p
->p_fd
->fd_nrdir
.mount
== mp
)