2 * Copyright (c) 1995-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)vfs_syscalls.c 8.41 (Berkeley) 6/15/95
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/namei.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/mount_internal.h>
83 #include <sys/proc_internal.h>
84 #include <sys/kauth.h>
85 #include <sys/uio_internal.h>
86 #include <sys/malloc.h>
88 #include <sys/dirent.h>
90 #include <sys/sysctl.h>
92 #include <sys/quota.h>
93 #include <sys/kdebug.h>
94 #include <sys/fsevents.h>
95 #include <sys/imgsrc.h>
96 #include <sys/sysproto.h>
97 #include <sys/sysctl.h>
98 #include <sys/xattr.h>
99 #include <sys/fcntl.h>
100 #include <sys/fsctl.h>
101 #include <sys/ubc_internal.h>
102 #include <sys/disk.h>
103 #include <sys/content_protection.h>
104 #include <sys/clonefile.h>
105 #include <sys/snapshot.h>
106 #include <sys/priv.h>
107 #include <sys/fsgetpath.h>
108 #include <machine/cons.h>
109 #include <machine/limits.h>
110 #include <miscfs/specfs/specdev.h>
112 #include <vfs/vfs_disk_conditioner.h>
114 #include <security/audit/audit.h>
115 #include <bsm/audit_kevents.h>
117 #include <mach/mach_types.h>
118 #include <kern/kern_types.h>
119 #include <kern/kalloc.h>
120 #include <kern/task.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_protos.h>
125 #include <libkern/OSAtomic.h>
126 #include <pexpert/pexpert.h>
127 #include <IOKit/IOBSD.h>
130 #include <kern/host.h>
131 #include <kern/ipc_misc.h>
132 #include <mach/host_priv.h>
133 #include <mach/vfs_nspace.h>
137 #include <miscfs/routefs/routefs.h>
141 #include <security/mac.h>
142 #include <security/mac_framework.h>
146 #define GET_PATH(x) \
147 (x) = get_pathbuff();
148 #define RELEASE_PATH(x) \
151 #define GET_PATH(x) \
152 MALLOC_ZONE((x), char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
153 #define RELEASE_PATH(x) \
154 FREE_ZONE((x), MAXPATHLEN, M_NAMEI);
155 #endif /* CONFIG_FSE */
157 #ifndef HFS_GET_BOOT_INFO
158 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
161 #ifndef HFS_SET_BOOT_INFO
162 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
165 #ifndef APFSIOC_REVERT_TO_SNAPSHOT
166 #define APFSIOC_REVERT_TO_SNAPSHOT _IOW('J', 1, u_int64_t)
169 extern void disk_conditioner_unmount(mount_t mp
);
171 /* struct for checkdirs iteration */
176 /* callback for checkdirs iteration */
177 static int checkdirs_callback(proc_t p
, void * arg
);
179 static int change_dir(struct nameidata
*ndp
, vfs_context_t ctx
);
180 static int checkdirs(vnode_t olddp
, vfs_context_t ctx
);
181 void enablequotas(struct mount
*mp
, vfs_context_t ctx
);
182 static int getfsstat_callback(mount_t mp
, void * arg
);
183 static int getutimes(user_addr_t usrtvp
, struct timespec
*tsp
);
184 static int setutimes(vfs_context_t ctx
, vnode_t vp
, const struct timespec
*ts
, int nullflag
);
185 static int sync_callback(mount_t
, void *);
186 static int munge_statfs(struct mount
*mp
, struct vfsstatfs
*sfsp
,
187 user_addr_t bufp
, int *sizep
, boolean_t is_64_bit
,
188 boolean_t partial_copy
);
189 static int fsync_common(proc_t p
, struct fsync_args
*uap
, int flags
);
190 static int mount_common(char *fstypename
, vnode_t pvp
, vnode_t vp
,
191 struct componentname
*cnp
, user_addr_t fsmountargs
,
192 int flags
, uint32_t internal_flags
, char *labelstr
, boolean_t kernelmount
,
194 void vfs_notify_mount(vnode_t pdvp
);
196 int prepare_coveredvp(vnode_t vp
, vfs_context_t ctx
, struct componentname
*cnp
, const char *fsname
, boolean_t skip_auth
);
198 struct fd_vn_data
* fg_vn_data_alloc(void);
201 * Max retries for ENOENT returns from vn_authorize_{rmdir, unlink, rename}
202 * Concurrent lookups (or lookups by ids) on hard links can cause the
203 * vn_getpath (which does not re-enter the filesystem as vn_getpath_fsenter
204 * does) to return ENOENT as the path cannot be returned from the name cache
205 * alone. We have no option but to retry and hope to get one namei->reverse path
206 * generation done without an intervening lookup, lookup by id on the hard link
207 * item. This is only an issue for MAC hooks which cannot reenter the filesystem
208 * which currently are the MAC hooks for rename, unlink and rmdir.
210 #define MAX_AUTHORIZE_ENOENT_RETRIES 1024
212 static int rmdirat_internal(vfs_context_t
, int, user_addr_t
, enum uio_seg
,
215 static int fsgetpath_internal(vfs_context_t
, int, uint64_t, vm_size_t
, caddr_t
, uint32_t options
, int *);
217 #ifdef CONFIG_IMGSRC_ACCESS
218 static int authorize_devpath_and_update_mntfromname(mount_t mp
, user_addr_t devpath
, vnode_t
*devvpp
, vfs_context_t ctx
);
219 static int place_mount_and_checkdirs(mount_t mp
, vnode_t vp
, vfs_context_t ctx
);
220 static void undo_place_on_covered_vp(mount_t mp
, vnode_t vp
);
221 static int mount_begin_update(mount_t mp
, vfs_context_t ctx
, int flags
);
222 static void mount_end_update(mount_t mp
);
223 static int relocate_imageboot_source(vnode_t pvp
, vnode_t vp
, struct componentname
*cnp
, const char *fsname
, vfs_context_t ctx
, boolean_t is64bit
, user_addr_t fsmountargs
, boolean_t by_index
);
224 #endif /* CONFIG_IMGSRC_ACCESS */
226 #if CONFIG_LOCKERBOOT
227 int mount_locker_protoboot(const char *fsname
, const char *mntpoint
,
228 const char *pbdevpath
);
232 #if CONFIG_MNT_ROOTSNAP
233 static int snapshot_root(int dirfd
, user_addr_t name
, uint32_t flags
, vfs_context_t ctx
);
235 static int snapshot_root(int dirfd
, user_addr_t name
, uint32_t flags
, vfs_context_t ctx
) __attribute__((unused
));
238 int (*union_dircheckp
)(struct vnode
**, struct fileproc
*, vfs_context_t
);
241 int sync_internal(void);
244 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
246 extern lck_grp_t
*fd_vn_lck_grp
;
247 extern lck_grp_attr_t
*fd_vn_lck_grp_attr
;
248 extern lck_attr_t
*fd_vn_lck_attr
;
251 * incremented each time a mount or unmount operation occurs
252 * used to invalidate the cached value of the rootvp in the
253 * mount structure utilized by cache_lookup_path
255 uint32_t mount_generation
= 0;
257 /* counts number of mount and unmount operations */
258 unsigned int vfs_nummntops
= 0;
260 extern const struct fileops vnops
;
261 #if CONFIG_APPLEDOUBLE
262 extern errno_t
rmdir_remove_orphaned_appleDouble(vnode_t
, vfs_context_t
, int *);
263 #endif /* CONFIG_APPLEDOUBLE */
266 * Virtual File System System Calls
269 #if NFSCLIENT || DEVFS || ROUTEFS
271 * Private in-kernel mounting spi (NFS only, not exported)
275 vfs_iskernelmount(mount_t mp
)
277 return (mp
->mnt_kern_flag
& MNTK_KERNEL_MOUNT
) ? TRUE
: FALSE
;
282 kernel_mount(char *fstype
, vnode_t pvp
, vnode_t vp
, const char *path
,
283 void *data
, __unused
size_t datalen
, int syscall_flags
, uint32_t kern_flags
, vfs_context_t ctx
)
289 NDINIT(&nd
, LOOKUP
, OP_MOUNT
, FOLLOW
| AUDITVNPATH1
| WANTPARENT
,
290 UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
293 * Get the vnode to be covered if it's not supplied
298 if (kern_flags
& (KERNEL_MOUNT_SNAPSHOT
| KERNEL_MOUNT_VMVOL
| KERNEL_MOUNT_DATAVOL
)) {
299 printf("failed to locate mount-on path: %s ", path
);
307 char *pnbuf
= CAST_DOWN(char *, path
);
309 nd
.ni_cnd
.cn_pnbuf
= pnbuf
;
310 nd
.ni_cnd
.cn_pnlen
= strlen(pnbuf
) + 1;
314 error
= mount_common(fstype
, pvp
, vp
, &nd
.ni_cnd
, CAST_USER_ADDR_T(data
),
315 syscall_flags
, kern_flags
, NULL
, TRUE
, ctx
);
325 #endif /* NFSCLIENT || DEVFS */
328 * Mount a file system.
332 mount(proc_t p
, struct mount_args
*uap
, __unused
int32_t *retval
)
334 struct __mac_mount_args muap
;
336 muap
.type
= uap
->type
;
337 muap
.path
= uap
->path
;
338 muap
.flags
= uap
->flags
;
339 muap
.data
= uap
->data
;
340 muap
.mac_p
= USER_ADDR_NULL
;
341 return __mac_mount(p
, &muap
, retval
);
345 fmount(__unused proc_t p
, struct fmount_args
*uap
, __unused
int32_t *retval
)
347 struct componentname cn
;
348 vfs_context_t ctx
= vfs_context_current();
351 int flags
= uap
->flags
;
352 char fstypename
[MFSNAMELEN
];
353 char *labelstr
= NULL
; /* regular mount call always sets it to NULL for __mac_mount() */
357 AUDIT_ARG(fd
, uap
->fd
);
358 AUDIT_ARG(fflags
, flags
);
359 /* fstypename will get audited by mount_common */
361 /* Sanity check the flags */
362 if (flags
& (MNT_IMGSRC_BY_INDEX
| MNT_ROOTFS
)) {
366 if (flags
& MNT_UNION
) {
370 error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, &dummy
);
375 if ((error
= file_vnode(uap
->fd
, &vp
)) != 0) {
379 if ((error
= vnode_getwithref(vp
)) != 0) {
384 pvp
= vnode_getparent(vp
);
391 memset(&cn
, 0, sizeof(struct componentname
));
392 MALLOC(cn
.cn_pnbuf
, char *, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
393 cn
.cn_pnlen
= MAXPATHLEN
;
395 if ((error
= vn_getpath(vp
, cn
.cn_pnbuf
, &cn
.cn_pnlen
)) != 0) {
396 FREE(cn
.cn_pnbuf
, M_TEMP
);
403 error
= mount_common(fstypename
, pvp
, vp
, &cn
, uap
->data
, flags
, 0, labelstr
, FALSE
, ctx
);
405 FREE(cn
.cn_pnbuf
, M_TEMP
);
414 vfs_notify_mount(vnode_t pdvp
)
416 vfs_event_signal(NULL
, VQ_MOUNT
, (intptr_t)NULL
);
417 lock_vnode_and_post(pdvp
, NOTE_WRITE
);
422 * Mount a file system taking into account MAC label behavior.
423 * See mount(2) man page for more information
425 * Parameters: p Process requesting the mount
426 * uap User argument descriptor (see below)
429 * Indirect: uap->type Filesystem type
430 * uap->path Path to mount
431 * uap->data Mount arguments
432 * uap->mac_p MAC info
433 * uap->flags Mount flags
439 boolean_t root_fs_upgrade_try
= FALSE
;
442 __mac_mount(struct proc
*p
, register struct __mac_mount_args
*uap
, __unused
int32_t *retval
)
446 int need_nameidone
= 0;
447 vfs_context_t ctx
= vfs_context_current();
448 char fstypename
[MFSNAMELEN
];
451 char *labelstr
= NULL
;
452 int flags
= uap
->flags
;
454 #if CONFIG_IMGSRC_ACCESS || CONFIG_MACF
455 boolean_t is_64bit
= IS_64BIT_PROCESS(p
);
460 * Get the fs type name from user space
462 error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, &dummy
);
468 * Get the vnode to be covered
470 NDINIT(&nd
, LOOKUP
, OP_MOUNT
, FOLLOW
| AUDITVNPATH1
| WANTPARENT
,
471 UIO_USERSPACE
, uap
->path
, ctx
);
480 #ifdef CONFIG_IMGSRC_ACCESS
481 /* Mounting image source cannot be batched with other operations */
482 if (flags
== MNT_IMGSRC_BY_INDEX
) {
483 error
= relocate_imageboot_source(pvp
, vp
, &nd
.ni_cnd
, fstypename
,
484 ctx
, is_64bit
, uap
->data
, (flags
== MNT_IMGSRC_BY_INDEX
));
487 #endif /* CONFIG_IMGSRC_ACCESS */
491 * Get the label string (if any) from user space
493 if (uap
->mac_p
!= USER_ADDR_NULL
) {
498 struct user64_mac mac64
;
499 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
500 mac
.m_buflen
= mac64
.m_buflen
;
501 mac
.m_string
= mac64
.m_string
;
503 struct user32_mac mac32
;
504 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
505 mac
.m_buflen
= mac32
.m_buflen
;
506 mac
.m_string
= mac32
.m_string
;
511 if ((mac
.m_buflen
> MAC_MAX_LABEL_BUF_LEN
) ||
512 (mac
.m_buflen
< 2)) {
516 MALLOC(labelstr
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
517 error
= copyinstr(mac
.m_string
, labelstr
, mac
.m_buflen
, &ulen
);
521 AUDIT_ARG(mac_string
, labelstr
);
523 #endif /* CONFIG_MACF */
525 AUDIT_ARG(fflags
, flags
);
528 if (flags
& MNT_UNION
) {
529 /* No union mounts on release kernels */
535 if ((vp
->v_flag
& VROOT
) &&
536 (vp
->v_mount
->mnt_flag
& MNT_ROOTFS
)) {
537 if (!(flags
& MNT_UNION
)) {
541 * For a union mount on '/', treat it as fresh
542 * mount instead of update.
543 * Otherwise, union mouting on '/' used to panic the
544 * system before, since mnt_vnodecovered was found to
545 * be NULL for '/' which is required for unionlookup
546 * after it gets ENOENT on union mount.
548 flags
= (flags
& ~(MNT_UPDATE
));
552 if ((flags
& MNT_RDONLY
) == 0) {
553 /* Release kernels are not allowed to mount "/" as rw */
559 * See 7392553 for more details on why this check exists.
560 * Suffice to say: If this check is ON and something tries
561 * to mount the rootFS RW, we'll turn off the codesign
562 * bitmap optimization.
564 #if CHECK_CS_VALIDATION_BITMAP
565 if ((flags
& MNT_RDONLY
) == 0) {
566 root_fs_upgrade_try
= TRUE
;
571 error
= mount_common(fstypename
, pvp
, vp
, &nd
.ni_cnd
, uap
->data
, flags
, 0,
572 labelstr
, FALSE
, ctx
);
578 FREE(labelstr
, M_MACTEMP
);
580 #endif /* CONFIG_MACF */
588 if (need_nameidone
) {
596 * common mount implementation (final stage of mounting)
599 * fstypename file system type (ie it's vfs name)
600 * pvp parent of covered vnode
602 * cnp component name (ie path) of covered vnode
603 * flags generic mount flags
604 * fsmountargs file system specific data
605 * labelstr optional MAC label
606 * kernelmount TRUE for mounts initiated from inside the kernel
607 * ctx caller's context
610 mount_common(char *fstypename
, vnode_t pvp
, vnode_t vp
,
611 struct componentname
*cnp
, user_addr_t fsmountargs
, int flags
, uint32_t internal_flags
,
612 char *labelstr
, boolean_t kernelmount
, vfs_context_t ctx
)
615 #pragma unused(labelstr)
617 struct vnode
*devvp
= NULLVP
;
618 struct vnode
*device_vnode
= NULLVP
;
623 struct vfstable
*vfsp
= (struct vfstable
*)0;
624 struct proc
*p
= vfs_context_proc(ctx
);
626 user_addr_t devpath
= USER_ADDR_NULL
;
629 boolean_t vfsp_ref
= FALSE
;
630 boolean_t is_rwlock_locked
= FALSE
;
631 boolean_t did_rele
= FALSE
;
632 boolean_t have_usecount
= FALSE
;
634 #if CONFIG_ROSV_STARTUP || CONFIG_MOUNT_VM
635 /* Check for mutually-exclusive flag bits */
636 uint32_t checkflags
= (internal_flags
& (KERNEL_MOUNT_DATAVOL
| KERNEL_MOUNT_VMVOL
));
638 while (checkflags
!= 0) {
639 checkflags
&= (checkflags
- 1);
644 //not allowed to request multiple mount-by-role flags
651 * Process an update for an existing mount
653 if (flags
& MNT_UPDATE
) {
654 if ((vp
->v_flag
& VROOT
) == 0) {
660 /* unmount in progress return error */
662 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
668 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
669 is_rwlock_locked
= TRUE
;
671 * We only allow the filesystem to be reloaded if it
672 * is currently mounted read-only.
674 if ((flags
& MNT_RELOAD
) &&
675 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
681 * If content protection is enabled, update mounts are not
682 * allowed to turn it off.
684 if ((mp
->mnt_flag
& MNT_CPROTECT
) &&
685 ((flags
& MNT_CPROTECT
) == 0)) {
691 * can't turn off MNT_REMOVABLE either but it may be an unexpected
692 * failure to return an error for this so we'll just silently
693 * add it if it is not passed in.
695 if ((mp
->mnt_flag
& MNT_REMOVABLE
) &&
696 ((flags
& MNT_REMOVABLE
) == 0)) {
697 flags
|= MNT_REMOVABLE
;
700 #ifdef CONFIG_IMGSRC_ACCESS
701 /* Can't downgrade the backer of the root FS */
702 if ((mp
->mnt_kern_flag
& MNTK_BACKS_ROOT
) &&
703 (!vfs_isrdonly(mp
)) && (flags
& MNT_RDONLY
)) {
707 #endif /* CONFIG_IMGSRC_ACCESS */
710 * Only root, or the user that did the original mount is
711 * permitted to update it.
713 if (mp
->mnt_vfsstat
.f_owner
!= kauth_cred_getuid(vfs_context_ucred(ctx
)) &&
714 (error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
))) {
718 error
= mac_mount_check_remount(ctx
, mp
);
724 * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV,
725 * and MNT_NOEXEC if mount point is already MNT_NOEXEC.
727 if ((!kernelmount
) && suser(vfs_context_ucred(ctx
), NULL
)) {
728 flags
|= MNT_NOSUID
| MNT_NODEV
;
729 if (mp
->mnt_flag
& MNT_NOEXEC
) {
737 mp
->mnt_flag
|= flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
739 vfsp
= mp
->mnt_vtable
;
744 * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV, and
745 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
747 if ((!kernelmount
) && suser(vfs_context_ucred(ctx
), NULL
)) {
748 flags
|= MNT_NOSUID
| MNT_NODEV
;
749 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
754 /* XXXAUDIT: Should we capture the type on the error path as well? */
755 AUDIT_ARG(text
, fstypename
);
757 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
758 if (!strncmp(vfsp
->vfc_name
, fstypename
, MFSNAMELEN
)) {
759 vfsp
->vfc_refcount
++;
771 * VFC_VFSLOCALARGS is not currently supported for kernel mounts,
772 * except in ROSV configs.
774 if (kernelmount
&& (vfsp
->vfc_vfsflags
& VFC_VFSLOCALARGS
) &&
775 ((internal_flags
& (KERNEL_MOUNT_DATAVOL
| KERNEL_MOUNT_VMVOL
)) == 0)) {
776 error
= EINVAL
; /* unsupported request */
780 error
= prepare_coveredvp(vp
, ctx
, cnp
, fstypename
, ((internal_flags
& KERNEL_MOUNT_NOAUTH
) != 0));
786 * Allocate and initialize the filesystem (mount_t)
788 MALLOC_ZONE(mp
, struct mount
*, (u_int32_t
)sizeof(struct mount
),
790 bzero((char *)mp
, (u_int32_t
)sizeof(struct mount
));
793 /* Initialize the default IO constraints */
794 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
795 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
796 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
797 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
798 mp
->mnt_devblocksize
= DEV_BSIZE
;
799 mp
->mnt_alignmentmask
= PAGE_MASK
;
800 mp
->mnt_ioqueue_depth
= MNT_DEFAULT_IOQUEUE_DEPTH
;
803 mp
->mnt_realrootvp
= NULLVP
;
804 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
806 TAILQ_INIT(&mp
->mnt_vnodelist
);
807 TAILQ_INIT(&mp
->mnt_workerqueue
);
808 TAILQ_INIT(&mp
->mnt_newvnodes
);
810 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
811 is_rwlock_locked
= TRUE
;
812 mp
->mnt_op
= vfsp
->vfc_vfsops
;
813 mp
->mnt_vtable
= vfsp
;
814 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
815 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
816 strlcpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
818 int pathlen
= MAXPATHLEN
;
820 if (vn_getpath_ext(vp
, pvp
, mp
->mnt_vfsstat
.f_mntonname
, &pathlen
, VN_GETPATH_FSENTER
)) {
821 strlcpy(mp
->mnt_vfsstat
.f_mntonname
, cnp
->cn_pnbuf
, MAXPATHLEN
);
824 mp
->mnt_vnodecovered
= vp
;
825 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(vfs_context_ucred(ctx
));
826 mp
->mnt_throttle_mask
= LOWPRI_MAX_NUM_DEV
- 1;
827 mp
->mnt_devbsdunit
= 0;
829 /* XXX 3762912 hack to support HFS filesystem 'owner' - filesystem may update later */
830 vfs_setowner(mp
, KAUTH_UID_NONE
, KAUTH_GID_NONE
);
832 #if NFSCLIENT || DEVFS || ROUTEFS
834 mp
->mnt_kern_flag
|= MNTK_KERNEL_MOUNT
;
836 if ((internal_flags
& KERNEL_MOUNT_PERMIT_UNMOUNT
) != 0) {
837 mp
->mnt_kern_flag
|= MNTK_PERMIT_UNMOUNT
;
839 #endif /* NFSCLIENT || DEVFS */
844 * Set the mount level flags.
846 if (flags
& MNT_RDONLY
) {
847 mp
->mnt_flag
|= MNT_RDONLY
;
848 } else if (mp
->mnt_flag
& MNT_RDONLY
) {
849 // disallow read/write upgrades of file systems that
850 // had the TYPENAME_OVERRIDE feature set.
851 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
855 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
857 mp
->mnt_flag
&= ~(MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
858 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
|
859 MNT_UNKNOWNPERMISSIONS
| MNT_DONTBROWSE
|
860 MNT_AUTOMOUNTED
| MNT_DEFWRITE
| MNT_NOATIME
| MNT_STRICTATIME
|
861 MNT_QUARANTINE
| MNT_CPROTECT
);
866 * On release builds of iOS based platforms, always enforce NOSUID on
867 * all mounts. We do this here because we can catch update mounts as well as
868 * non-update mounts in this case.
870 mp
->mnt_flag
|= (MNT_NOSUID
);
874 mp
->mnt_flag
|= flags
& (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
875 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
|
876 MNT_UNKNOWNPERMISSIONS
| MNT_DONTBROWSE
|
877 MNT_AUTOMOUNTED
| MNT_DEFWRITE
| MNT_NOATIME
| MNT_STRICTATIME
|
878 MNT_QUARANTINE
| MNT_CPROTECT
);
881 if (flags
& MNT_MULTILABEL
) {
882 if (vfsp
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) {
886 mp
->mnt_flag
|= MNT_MULTILABEL
;
890 * Process device path for local file systems if requested
892 if (vfsp
->vfc_vfsflags
& VFC_VFSLOCALARGS
&&
893 !(internal_flags
& (KERNEL_MOUNT_SNAPSHOT
| KERNEL_MOUNT_DATAVOL
| KERNEL_MOUNT_VMVOL
))) {
894 //snapshot, vm, datavolume mounts are special
895 if (vfs_context_is64bit(ctx
)) {
896 if ((error
= copyin(fsmountargs
, (caddr_t
)&devpath
, sizeof(devpath
)))) {
899 fsmountargs
+= sizeof(devpath
);
902 if ((error
= copyin(fsmountargs
, (caddr_t
)&tmp
, sizeof(tmp
)))) {
905 /* munge into LP64 addr */
906 devpath
= CAST_USER_ADDR_T(tmp
);
907 fsmountargs
+= sizeof(tmp
);
910 /* Lookup device and authorize access to it */
914 NDINIT(&nd
, LOOKUP
, OP_MOUNT
, FOLLOW
, UIO_USERSPACE
, devpath
, ctx
);
915 if ((error
= namei(&nd
))) {
919 strlcpy(mp
->mnt_vfsstat
.f_mntfromname
, nd
.ni_cnd
.cn_pnbuf
, MAXPATHLEN
);
924 if (devvp
->v_type
!= VBLK
) {
928 if (major(devvp
->v_rdev
) >= nblkdev
) {
933 * If mount by non-root, then verify that user has necessary
934 * permissions on the device.
936 if (suser(vfs_context_ucred(ctx
), NULL
) != 0) {
937 mode_t accessmode
= KAUTH_VNODE_READ_DATA
;
939 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
940 accessmode
|= KAUTH_VNODE_WRITE_DATA
;
942 if ((error
= vnode_authorize(devvp
, NULL
, accessmode
, ctx
)) != 0) {
947 /* On first mount, preflight and open device */
948 if (devpath
&& ((flags
& MNT_UPDATE
) == 0)) {
949 if ((error
= vnode_ref(devvp
))) {
953 * Disallow multiple mounts of the same device.
954 * Disallow mounting of a device that is currently in use
955 * (except for root, which might share swap device for miniroot).
956 * Flush out any old buffers remaining from a previous use.
958 if ((error
= vfs_mountedon(devvp
))) {
962 if (vcount(devvp
) > 1 && !(vfs_flags(mp
) & MNT_ROOTFS
)) {
966 if ((error
= VNOP_FSYNC(devvp
, MNT_WAIT
, ctx
))) {
970 if ((error
= buf_invalidateblks(devvp
, BUF_WRITE_DATA
, 0, 0))) {
974 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
976 error
= mac_vnode_check_open(ctx
,
978 ronly
? FREAD
: FREAD
| FWRITE
);
983 if ((error
= VNOP_OPEN(devvp
, ronly
? FREAD
: FREAD
| FWRITE
, ctx
))) {
987 mp
->mnt_devvp
= devvp
;
988 device_vnode
= devvp
;
989 } else if ((mp
->mnt_flag
& MNT_RDONLY
) &&
990 (mp
->mnt_kern_flag
& MNTK_WANTRDWR
) &&
991 (device_vnode
= mp
->mnt_devvp
)) {
995 * If upgrade to read-write by non-root, then verify
996 * that user has necessary permissions on the device.
998 vnode_getalways(device_vnode
);
1000 if (suser(vfs_context_ucred(ctx
), NULL
) &&
1001 (error
= vnode_authorize(device_vnode
, NULL
,
1002 KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
,
1004 vnode_put(device_vnode
);
1008 /* Tell the device that we're upgrading */
1009 dev
= (dev_t
)device_vnode
->v_rdev
;
1012 if ((u_int
)maj
>= (u_int
)nblkdev
) {
1013 panic("Volume mounted on a device with invalid major number.");
1016 error
= bdevsw
[maj
].d_open(dev
, FREAD
| FWRITE
, S_IFBLK
, p
);
1017 vnode_put(device_vnode
);
1018 device_vnode
= NULLVP
;
1023 } // localargs && !(snapshot | data | vm)
1026 if ((flags
& MNT_UPDATE
) == 0) {
1027 mac_mount_label_init(mp
);
1028 mac_mount_label_associate(ctx
, mp
);
1031 if ((flags
& MNT_UPDATE
) != 0) {
1032 error
= mac_mount_check_label_update(ctx
, mp
);
1040 * Mount the filesystem. We already asserted that internal_flags
1041 * cannot have more than one mount-by-role bit set.
1043 if (internal_flags
& KERNEL_MOUNT_SNAPSHOT
) {
1044 error
= VFS_IOCTL(mp
, VFSIOC_MOUNT_SNAPSHOT
,
1045 (caddr_t
)fsmountargs
, 0, ctx
);
1046 } else if (internal_flags
& KERNEL_MOUNT_DATAVOL
) {
1047 #if CONFIG_ROSV_STARTUP
1048 struct mount
*origin_mp
= (struct mount
*)fsmountargs
;
1049 fs_role_mount_args_t frma
= {origin_mp
, VFS_DATA_ROLE
};
1050 error
= VFS_IOCTL(mp
, VFSIOC_MOUNT_BYROLE
, (caddr_t
)&frma
, 0, ctx
);
1052 printf("MOUNT-BY-ROLE (%d) failed! (%d)", VFS_DATA_ROLE
, error
);
1054 /* Mark volume associated with system volume */
1055 mp
->mnt_kern_flag
|= MNTK_SYSTEM
;
1057 /* Attempt to acquire the mnt_devvp and set it up */
1058 struct vnode
*mp_devvp
= NULL
;
1059 if (mp
->mnt_vfsstat
.f_mntfromname
[0] != 0) {
1060 errno_t lerr
= vnode_lookup(mp
->mnt_vfsstat
.f_mntfromname
,
1061 0, &mp_devvp
, vfs_context_kernel());
1063 mp
->mnt_devvp
= mp_devvp
;
1064 //vnode_lookup took an iocount, need to drop it.
1065 vnode_put(mp_devvp
);
1066 // now set `device_vnode` to the devvp that was acquired.
1067 // this is needed in order to ensure vfs_init_io_attributes is invoked.
1068 // note that though the iocount above was dropped, the mount acquires
1069 // an implicit reference against the device.
1070 device_vnode
= mp_devvp
;
1077 } else if (internal_flags
& KERNEL_MOUNT_VMVOL
) {
1079 struct mount
*origin_mp
= (struct mount
*)fsmountargs
;
1080 fs_role_mount_args_t frma
= {origin_mp
, VFS_VM_ROLE
};
1081 error
= VFS_IOCTL(mp
, VFSIOC_MOUNT_BYROLE
, (caddr_t
)&frma
, 0, ctx
);
1083 printf("MOUNT-BY-ROLE (%d) failed! (%d)", VFS_VM_ROLE
, error
);
1085 /* Mark volume associated with system volume and a swap mount */
1086 mp
->mnt_kern_flag
|= (MNTK_SYSTEM
| MNTK_SWAP_MOUNT
);
1087 /* Attempt to acquire the mnt_devvp and set it up */
1088 struct vnode
*mp_devvp
= NULL
;
1089 if (mp
->mnt_vfsstat
.f_mntfromname
[0] != 0) {
1090 errno_t lerr
= vnode_lookup(mp
->mnt_vfsstat
.f_mntfromname
,
1091 0, &mp_devvp
, vfs_context_kernel());
1093 mp
->mnt_devvp
= mp_devvp
;
1094 //vnode_lookup took an iocount, need to drop it.
1095 vnode_put(mp_devvp
);
1097 // now set `device_vnode` to the devvp that was acquired.
1098 // note that though the iocount above was dropped, the mount acquires
1099 // an implicit reference against the device.
1100 device_vnode
= mp_devvp
;
1108 error
= VFS_MOUNT(mp
, device_vnode
, fsmountargs
, ctx
);
1111 if (flags
& MNT_UPDATE
) {
1112 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
) {
1113 mp
->mnt_flag
&= ~MNT_RDONLY
;
1116 (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
1117 mp
->mnt_kern_flag
&= ~MNTK_WANTRDWR
;
1119 mp
->mnt_flag
= flag
; /* restore flag value */
1121 vfs_event_signal(NULL
, VQ_UPDATE
, (intptr_t)NULL
);
1122 lck_rw_done(&mp
->mnt_rwlock
);
1123 is_rwlock_locked
= FALSE
;
1125 enablequotas(mp
, ctx
);
1131 * Put the new filesystem on the mount list after root.
1134 struct vfs_attr vfsattr
;
1136 error
= mac_mount_check_mount_late(ctx
, mp
);
1141 if (vfs_flags(mp
) & MNT_MULTILABEL
) {
1142 error
= VFS_ROOT(mp
, &rvp
, ctx
);
1144 printf("%s() VFS_ROOT returned %d\n", __func__
, error
);
1147 error
= vnode_label(mp
, NULL
, rvp
, NULL
, 0, ctx
);
1149 * drop reference provided by VFS_ROOT
1159 vnode_lock_spin(vp
);
1160 CLR(vp
->v_flag
, VMOUNT
);
1161 vp
->v_mountedhere
= mp
;
1165 * taking the name_cache_lock exclusively will
1166 * insure that everyone is out of the fast path who
1167 * might be trying to use a now stale copy of
1168 * vp->v_mountedhere->mnt_realrootvp
1169 * bumping mount_generation causes the cached values
1174 name_cache_unlock();
1176 error
= vnode_ref(vp
);
1181 have_usecount
= TRUE
;
1183 error
= checkdirs(vp
, ctx
);
1185 /* Unmount the filesystem as cdir/rdirs cannot be updated */
1189 * there is no cleanup code here so I have made it void
1190 * we need to revisit this
1192 (void)VFS_START(mp
, 0, ctx
);
1194 if (mount_list_add(mp
) != 0) {
1196 * The system is shutting down trying to umount
1197 * everything, so fail with a plausible errno.
1202 lck_rw_done(&mp
->mnt_rwlock
);
1203 is_rwlock_locked
= FALSE
;
1205 /* Check if this mounted file system supports EAs or named streams. */
1206 /* Skip WebDAV file systems for now since they hang in VFS_GETATTR here. */
1207 VFSATTR_INIT(&vfsattr
);
1208 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
1209 if (strncmp(mp
->mnt_vfsstat
.f_fstypename
, "webdav", sizeof("webdav")) != 0 &&
1210 vfs_getattr(mp
, &vfsattr
, ctx
) == 0 &&
1211 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
1212 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
1213 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
1214 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1217 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
) &&
1218 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
)) {
1219 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1222 /* Check if this file system supports path from id lookups. */
1223 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
) &&
1224 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
)) {
1225 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
1226 } else if (mp
->mnt_flag
& MNT_DOVOLFS
) {
1227 /* Legacy MNT_DOVOLFS flag also implies path from id lookups. */
1228 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
1231 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_DIR_HARDLINKS
) &&
1232 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_DIR_HARDLINKS
)) {
1233 mp
->mnt_kern_flag
|= MNTK_DIR_HARDLINKS
;
1236 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
) {
1237 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
1239 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSPREFLIGHT
) {
1240 mp
->mnt_kern_flag
|= MNTK_UNMOUNT_PREFLIGHT
;
1242 /* increment the operations count */
1243 OSAddAtomic(1, &vfs_nummntops
);
1244 enablequotas(mp
, ctx
);
1247 device_vnode
->v_specflags
|= SI_MOUNTEDON
;
1250 * cache the IO attributes for the underlying physical media...
1251 * an error return indicates the underlying driver doesn't
1252 * support all the queries necessary... however, reasonable
1253 * defaults will have been set, so no reason to bail or care
1255 vfs_init_io_attributes(device_vnode
, mp
);
1258 /* Now that mount is setup, notify the listeners */
1259 vfs_notify_mount(pvp
);
1260 IOBSDMountChange(mp
, kIOMountChangeMount
);
1262 /* If we fail a fresh mount, there should be no vnodes left hooked into the mountpoint. */
1263 if (mp
->mnt_vnodelist
.tqh_first
!= NULL
) {
1264 panic("mount_common(): mount of %s filesystem failed with %d, but vnode list is not empty.",
1265 mp
->mnt_vtable
->vfc_name
, error
);
1268 vnode_lock_spin(vp
);
1269 CLR(vp
->v_flag
, VMOUNT
);
1272 mp
->mnt_vtable
->vfc_refcount
--;
1273 mount_list_unlock();
1276 vnode_rele(device_vnode
);
1277 VNOP_CLOSE(device_vnode
, ronly
? FREAD
: FREAD
| FWRITE
, ctx
);
1279 lck_rw_done(&mp
->mnt_rwlock
);
1280 is_rwlock_locked
= FALSE
;
1283 * if we get here, we have a mount structure that needs to be freed,
1284 * but since the coveredvp hasn't yet been updated to point at it,
1285 * no need to worry about other threads holding a crossref on this mp
1286 * so it's ok to just free it
1288 mount_lock_destroy(mp
);
1290 mac_mount_label_destroy(mp
);
1292 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
1296 * drop I/O count on the device vp if there was one
1298 if (devpath
&& devvp
) {
1304 /* Error condition exits */
1306 (void)VFS_UNMOUNT(mp
, MNT_FORCE
, ctx
);
1309 * If the mount has been placed on the covered vp,
1310 * it may have been discovered by now, so we have
1311 * to treat this just like an unmount
1313 mount_lock_spin(mp
);
1314 mp
->mnt_lflag
|= MNT_LDEAD
;
1317 if (device_vnode
!= NULLVP
) {
1318 vnode_rele(device_vnode
);
1319 VNOP_CLOSE(device_vnode
, mp
->mnt_flag
& MNT_RDONLY
? FREAD
: FREAD
| FWRITE
,
1324 vnode_lock_spin(vp
);
1327 vp
->v_mountedhere
= (mount_t
) 0;
1331 if (have_usecount
) {
1335 if (devpath
&& ((flags
& MNT_UPDATE
) == 0) && (!did_rele
)) {
1339 if (devpath
&& devvp
) {
1343 /* Release mnt_rwlock only when it was taken */
1344 if (is_rwlock_locked
== TRUE
) {
1345 lck_rw_done(&mp
->mnt_rwlock
);
1349 if (mp
->mnt_crossref
) {
1350 mount_dropcrossref(mp
, vp
, 0);
1352 mount_lock_destroy(mp
);
1354 mac_mount_label_destroy(mp
);
1356 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
1361 vfsp
->vfc_refcount
--;
1362 mount_list_unlock();
1369 * Flush in-core data, check for competing mount attempts,
1373 prepare_coveredvp(vnode_t vp
, vfs_context_t ctx
, struct componentname
*cnp
, const char *fsname
, boolean_t skip_auth
)
1376 #pragma unused(cnp,fsname)
1378 struct vnode_attr va
;
1383 * If the user is not root, ensure that they own the directory
1384 * onto which we are attempting to mount.
1387 VATTR_WANTED(&va
, va_uid
);
1388 if ((error
= vnode_getattr(vp
, &va
, ctx
)) ||
1389 (va
.va_uid
!= kauth_cred_getuid(vfs_context_ucred(ctx
)) &&
1390 (!vfs_context_issuser(ctx
)))) {
1396 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
))) {
1400 if ((error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
1404 if (vp
->v_type
!= VDIR
) {
1409 if (ISSET(vp
->v_flag
, VMOUNT
) && (vp
->v_mountedhere
!= NULL
)) {
1415 error
= mac_mount_check_mount(ctx
, vp
,
1422 vnode_lock_spin(vp
);
1423 SET(vp
->v_flag
, VMOUNT
);
1430 #if CONFIG_IMGSRC_ACCESS
1432 #define DEBUG_IMGSRC 0
1435 #define IMGSRC_DEBUG(args...) printf("imgsrc: " args)
1437 #define IMGSRC_DEBUG(args...) do { } while(0)
1441 authorize_devpath_and_update_mntfromname(mount_t mp
, user_addr_t devpath
, vnode_t
*devvpp
, vfs_context_t ctx
)
1443 struct nameidata nd
;
1444 vnode_t vp
, realdevvp
;
1447 enum uio_seg uio
= UIO_USERSPACE
;
1449 if (ctx
== vfs_context_kernel()) {
1453 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
, uio
, devpath
, ctx
);
1454 if ((error
= namei(&nd
))) {
1455 IMGSRC_DEBUG("namei() failed with %d\n", error
);
1461 if (!vnode_isblk(vp
)) {
1462 IMGSRC_DEBUG("Not block device.\n");
1467 realdevvp
= mp
->mnt_devvp
;
1468 if (realdevvp
== NULLVP
) {
1469 IMGSRC_DEBUG("No device backs the mount.\n");
1474 error
= vnode_getwithref(realdevvp
);
1476 IMGSRC_DEBUG("Coudn't get iocount on device.\n");
1480 if (vnode_specrdev(vp
) != vnode_specrdev(realdevvp
)) {
1481 IMGSRC_DEBUG("Wrong dev_t.\n");
1486 strlcpy(mp
->mnt_vfsstat
.f_mntfromname
, nd
.ni_cnd
.cn_pnbuf
, MAXPATHLEN
);
1489 * If mount by non-root, then verify that user has necessary
1490 * permissions on the device.
1492 if (!vfs_context_issuser(ctx
)) {
1493 accessmode
= KAUTH_VNODE_READ_DATA
;
1494 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
1495 accessmode
|= KAUTH_VNODE_WRITE_DATA
;
1497 if ((error
= vnode_authorize(vp
, NULL
, accessmode
, ctx
)) != 0) {
1498 IMGSRC_DEBUG("Access denied.\n");
1506 vnode_put(realdevvp
);
1519 * Clear VMOUNT, set v_mountedhere, and mnt_vnodecovered, ref the vnode,
1520 * and call checkdirs()
1523 place_mount_and_checkdirs(mount_t mp
, vnode_t vp
, vfs_context_t ctx
)
1527 mp
->mnt_vnodecovered
= vp
; /* XXX This is normally only set at init-time ... */
1529 IMGSRC_DEBUG("placing: fsname = %s, vp = %s\n",
1530 mp
->mnt_vtable
->vfc_name
, vnode_getname(vp
));
1532 vnode_lock_spin(vp
);
1533 CLR(vp
->v_flag
, VMOUNT
);
1534 vp
->v_mountedhere
= mp
;
1538 * taking the name_cache_lock exclusively will
1539 * insure that everyone is out of the fast path who
1540 * might be trying to use a now stale copy of
1541 * vp->v_mountedhere->mnt_realrootvp
1542 * bumping mount_generation causes the cached values
1547 name_cache_unlock();
1549 error
= vnode_ref(vp
);
1554 error
= checkdirs(vp
, ctx
);
1556 /* Unmount the filesystem as cdir/rdirs cannot be updated */
1563 mp
->mnt_vnodecovered
= NULLVP
;
1569 undo_place_on_covered_vp(mount_t mp
, vnode_t vp
)
1572 vnode_lock_spin(vp
);
1573 vp
->v_mountedhere
= (mount_t
)NULL
;
1576 mp
->mnt_vnodecovered
= NULLVP
;
1580 mount_begin_update(mount_t mp
, vfs_context_t ctx
, int flags
)
1584 /* unmount in progress return error */
1585 mount_lock_spin(mp
);
1586 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
1591 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
1594 * We only allow the filesystem to be reloaded if it
1595 * is currently mounted read-only.
1597 if ((flags
& MNT_RELOAD
) &&
1598 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
1604 * Only root, or the user that did the original mount is
1605 * permitted to update it.
1607 if (mp
->mnt_vfsstat
.f_owner
!= kauth_cred_getuid(vfs_context_ucred(ctx
)) &&
1608 (!vfs_context_issuser(ctx
))) {
1613 error
= mac_mount_check_remount(ctx
, mp
);
1621 lck_rw_done(&mp
->mnt_rwlock
);
1628 mount_end_update(mount_t mp
)
1630 lck_rw_done(&mp
->mnt_rwlock
);
1634 get_imgsrc_rootvnode(uint32_t height
, vnode_t
*rvpp
)
1638 if (height
>= MAX_IMAGEBOOT_NESTING
) {
1642 vp
= imgsrc_rootvnodes
[height
];
1643 if ((vp
!= NULLVP
) && (vnode_get(vp
) == 0)) {
1652 relocate_imageboot_source(vnode_t pvp
, vnode_t vp
,
1653 struct componentname
*cnp
, const char *fsname
, vfs_context_t ctx
,
1654 boolean_t is64bit
, user_addr_t fsmountargs
, boolean_t by_index
)
1658 boolean_t placed
= FALSE
;
1659 struct vfstable
*vfsp
;
1660 user_addr_t devpath
;
1661 char *old_mntonname
;
1667 /* If we didn't imageboot, nothing to move */
1668 if (imgsrc_rootvnodes
[0] == NULLVP
) {
1672 /* Only root can do this */
1673 if (!vfs_context_issuser(ctx
)) {
1677 IMGSRC_DEBUG("looking for root vnode.\n");
1680 * Get root vnode of filesystem we're moving.
1684 struct user64_mnt_imgsrc_args mia64
;
1685 error
= copyin(fsmountargs
, &mia64
, sizeof(mia64
));
1687 IMGSRC_DEBUG("Failed to copy in arguments.\n");
1691 height
= mia64
.mi_height
;
1692 flags
= mia64
.mi_flags
;
1693 devpath
= mia64
.mi_devpath
;
1695 struct user32_mnt_imgsrc_args mia32
;
1696 error
= copyin(fsmountargs
, &mia32
, sizeof(mia32
));
1698 IMGSRC_DEBUG("Failed to copy in arguments.\n");
1702 height
= mia32
.mi_height
;
1703 flags
= mia32
.mi_flags
;
1704 devpath
= mia32
.mi_devpath
;
1708 * For binary compatibility--assumes one level of nesting.
1711 if ((error
= copyin(fsmountargs
, (caddr_t
)&devpath
, sizeof(devpath
)))) {
1716 if ((error
= copyin(fsmountargs
, (caddr_t
)&tmp
, sizeof(tmp
)))) {
1720 /* munge into LP64 addr */
1721 devpath
= CAST_USER_ADDR_T(tmp
);
1729 IMGSRC_DEBUG("%s: Got nonzero flags.\n", __FUNCTION__
);
1733 error
= get_imgsrc_rootvnode(height
, &rvp
);
1735 IMGSRC_DEBUG("getting old root vnode failed with %d\n", error
);
1739 IMGSRC_DEBUG("got old root vnode\n");
1741 MALLOC(old_mntonname
, char*, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
1743 /* Can only move once */
1744 mp
= vnode_mount(rvp
);
1745 if ((mp
->mnt_kern_flag
& MNTK_HAS_MOVED
) == MNTK_HAS_MOVED
) {
1746 IMGSRC_DEBUG("Already moved.\n");
1751 IMGSRC_DEBUG("moving rvp: fsname = %s\n", mp
->mnt_vtable
->vfc_name
);
1752 IMGSRC_DEBUG("Starting updated.\n");
1754 /* Get exclusive rwlock on mount, authorize update on mp */
1755 error
= mount_begin_update(mp
, ctx
, 0);
1757 IMGSRC_DEBUG("Starting updated failed with %d\n", error
);
1762 * It can only be moved once. Flag is set under the rwlock,
1763 * so we're now safe to proceed.
1765 if ((mp
->mnt_kern_flag
& MNTK_HAS_MOVED
) == MNTK_HAS_MOVED
) {
1766 IMGSRC_DEBUG("Already moved [2]\n");
1770 IMGSRC_DEBUG("Preparing coveredvp.\n");
1772 /* Mark covered vnode as mount in progress, authorize placing mount on top */
1773 error
= prepare_coveredvp(vp
, ctx
, cnp
, fsname
, FALSE
);
1775 IMGSRC_DEBUG("Preparing coveredvp failed with %d.\n", error
);
1779 IMGSRC_DEBUG("Covered vp OK.\n");
1781 /* Sanity check the name caller has provided */
1782 vfsp
= mp
->mnt_vtable
;
1783 if (strncmp(vfsp
->vfc_name
, fsname
, MFSNAMELEN
) != 0) {
1784 IMGSRC_DEBUG("Wrong fs name: actual = %s, expected = %s\n",
1785 vfsp
->vfc_name
, fsname
);
1790 /* Check the device vnode and update mount-from name, for local filesystems */
1791 if (vfsp
->vfc_vfsflags
& VFC_VFSLOCALARGS
) {
1792 IMGSRC_DEBUG("Local, doing device validation.\n");
1794 if (devpath
!= USER_ADDR_NULL
) {
1795 error
= authorize_devpath_and_update_mntfromname(mp
, devpath
, &devvp
, ctx
);
1797 IMGSRC_DEBUG("authorize_devpath_and_update_mntfromname() failed.\n");
1806 * Place mp on top of vnode, ref the vnode, call checkdirs(),
1807 * and increment the name cache's mount generation
1810 IMGSRC_DEBUG("About to call place_mount_and_checkdirs().\n");
1811 error
= place_mount_and_checkdirs(mp
, vp
, ctx
);
1818 strlcpy(old_mntonname
, mp
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
1819 strlcpy(mp
->mnt_vfsstat
.f_mntonname
, cnp
->cn_pnbuf
, MAXPATHLEN
);
1821 /* Forbid future moves */
1823 mp
->mnt_kern_flag
|= MNTK_HAS_MOVED
;
1826 /* Finally, add to mount list, completely ready to go */
1827 if (mount_list_add(mp
) != 0) {
1829 * The system is shutting down trying to umount
1830 * everything, so fail with a plausible errno.
1836 mount_end_update(mp
);
1838 FREE(old_mntonname
, M_TEMP
);
1840 vfs_notify_mount(pvp
);
1844 strlcpy(mp
->mnt_vfsstat
.f_mntonname
, old_mntonname
, MAXPATHLEN
);
1847 mp
->mnt_kern_flag
&= ~(MNTK_HAS_MOVED
);
1852 * Placing the mp on the vnode clears VMOUNT,
1853 * so cleanup is different after that point
1856 /* Rele the vp, clear VMOUNT and v_mountedhere */
1857 undo_place_on_covered_vp(mp
, vp
);
1859 vnode_lock_spin(vp
);
1860 CLR(vp
->v_flag
, VMOUNT
);
1864 mount_end_update(mp
);
1868 FREE(old_mntonname
, M_TEMP
);
1872 #if CONFIG_LOCKERBOOT
1875 mount_locker_protoboot(const char *fsname
, const char *mntpoint
,
1876 const char *pbdevpath
)
1879 struct nameidata nd
;
1880 boolean_t cleanup_nd
= FALSE
;
1881 vfs_context_t ctx
= vfs_context_kernel();
1882 boolean_t is64
= TRUE
;
1883 boolean_t by_index
= TRUE
;
1884 struct user64_mnt_imgsrc_args mia64
= {
1887 .mi_devpath
= CAST_USER_ADDR_T(pbdevpath
),
1889 user_addr_t mia64addr
= CAST_USER_ADDR_T(&mia64
);
1891 NDINIT(&nd
, LOOKUP
, OP_MOUNT
, FOLLOW
| AUDITVNPATH1
| WANTPARENT
,
1892 UIO_SYSSPACE
, CAST_USER_ADDR_T(mntpoint
), ctx
);
1895 IMGSRC_DEBUG("namei: %d\n", error
);
1900 error
= relocate_imageboot_source(nd
.ni_dvp
, nd
.ni_vp
,
1901 &nd
.ni_cnd
, fsname
, ctx
, is64
, mia64addr
, by_index
);
1905 int stashed
= error
;
1907 error
= vnode_put(nd
.ni_vp
);
1909 panic("vnode_put() returned non-zero: %d", error
);
1913 error
= vnode_put(nd
.ni_dvp
);
1915 panic("vnode_put() returned non-zero: %d", error
);
1924 #endif /* CONFIG_LOCKERBOOT */
1925 #endif /* CONFIG_IMGSRC_ACCESS */
1928 enablequotas(struct mount
*mp
, vfs_context_t ctx
)
1930 struct nameidata qnd
;
1932 char qfpath
[MAXPATHLEN
];
1933 const char *qfname
= QUOTAFILENAME
;
1934 const char *qfopsname
= QUOTAOPSNAME
;
1935 const char *qfextension
[] = INITQFNAMES
;
1937 /* XXX Shoulkd be an MNTK_ flag, instead of strncmp()'s */
1938 if (strncmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs", sizeof("hfs")) != 0) {
1942 * Enable filesystem disk quotas if necessary.
1943 * We ignore errors as this should not interfere with final mount
1945 for (type
= 0; type
< MAXQUOTAS
; type
++) {
1946 snprintf(qfpath
, sizeof(qfpath
), "%s/%s.%s", mp
->mnt_vfsstat
.f_mntonname
, qfopsname
, qfextension
[type
]);
1947 NDINIT(&qnd
, LOOKUP
, OP_MOUNT
, FOLLOW
, UIO_SYSSPACE
,
1948 CAST_USER_ADDR_T(qfpath
), ctx
);
1949 if (namei(&qnd
) != 0) {
1950 continue; /* option file to trigger quotas is not present */
1952 vnode_put(qnd
.ni_vp
);
1954 snprintf(qfpath
, sizeof(qfpath
), "%s/%s.%s", mp
->mnt_vfsstat
.f_mntonname
, qfname
, qfextension
[type
]);
1956 (void) VFS_QUOTACTL(mp
, QCMD(Q_QUOTAON
, type
), 0, qfpath
, ctx
);
1963 checkdirs_callback(proc_t p
, void * arg
)
1965 struct cdirargs
* cdrp
= (struct cdirargs
*)arg
;
1966 vnode_t olddp
= cdrp
->olddp
;
1967 vnode_t newdp
= cdrp
->newdp
;
1968 struct filedesc
*fdp
;
1972 int cdir_changed
= 0;
1973 int rdir_changed
= 0;
1976 * XXX Also needs to iterate each thread in the process to see if it
1977 * XXX is using a per-thread current working directory, and, if so,
1978 * XXX update that as well.
1983 if (fdp
== (struct filedesc
*)0) {
1985 return PROC_RETURNED
;
1987 fdp_cvp
= fdp
->fd_cdir
;
1988 fdp_rvp
= fdp
->fd_rdir
;
1991 if (fdp_cvp
== olddp
) {
1998 if (fdp_rvp
== olddp
) {
2005 if (cdir_changed
|| rdir_changed
) {
2007 fdp
->fd_cdir
= fdp_cvp
;
2008 fdp
->fd_rdir
= fdp_rvp
;
2011 return PROC_RETURNED
;
2017 * Scan all active processes to see if any of them have a current
2018 * or root directory onto which the new filesystem has just been
2019 * mounted. If so, replace them with the new mount point.
2022 checkdirs(vnode_t olddp
, vfs_context_t ctx
)
2027 struct cdirargs cdr
;
2029 if (olddp
->v_usecount
== 1) {
2032 err
= VFS_ROOT(olddp
->v_mountedhere
, &newdp
, ctx
);
2036 panic("mount: lost mount: error %d", err
);
2043 /* do not block for exec/fork trans as the vp in cwd & rootdir are not changing */
2044 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
, checkdirs_callback
, (void *)&cdr
, NULL
, NULL
);
2046 if (rootvnode
== olddp
) {
2058 * Unmount a file system.
2060 * Note: unmount takes a path to the vnode mounted on as argument,
2061 * not special file (as before).
2065 unmount(__unused proc_t p
, struct unmount_args
*uap
, __unused
int32_t *retval
)
2070 struct nameidata nd
;
2071 vfs_context_t ctx
= vfs_context_current();
2073 NDINIT(&nd
, LOOKUP
, OP_UNMOUNT
, FOLLOW
| AUDITVNPATH1
,
2074 UIO_USERSPACE
, uap
->path
, ctx
);
2084 error
= mac_mount_check_umount(ctx
, mp
);
2091 * Must be the root of the filesystem
2093 if ((vp
->v_flag
& VROOT
) == 0) {
2099 /* safedounmount consumes the mount ref */
2100 return safedounmount(mp
, uap
->flags
, ctx
);
2104 vfs_unmountbyfsid(fsid_t
*fsid
, int flags
, vfs_context_t ctx
)
2108 mp
= mount_list_lookupby_fsid(fsid
, 0, 1);
2109 if (mp
== (mount_t
)0) {
2114 /* safedounmount consumes the mount ref */
2115 return safedounmount(mp
, flags
, ctx
);
2120 * The mount struct comes with a mount ref which will be consumed.
2121 * Do the actual file system unmount, prevent some common foot shooting.
2124 safedounmount(struct mount
*mp
, int flags
, vfs_context_t ctx
)
2127 proc_t p
= vfs_context_proc(ctx
);
2130 * If the file system is not responding and MNT_NOBLOCK
2131 * is set and not a forced unmount then return EBUSY.
2133 if ((mp
->mnt_kern_flag
& MNT_LNOTRESP
) &&
2134 (flags
& MNT_NOBLOCK
) && ((flags
& MNT_FORCE
) == 0)) {
2140 * Skip authorization if the mount is tagged as permissive and
2141 * this is not a forced-unmount attempt.
2143 if (!(((mp
->mnt_kern_flag
& MNTK_PERMIT_UNMOUNT
) != 0) && ((flags
& MNT_FORCE
) == 0))) {
2145 * Only root, or the user that did the original mount is
2146 * permitted to unmount this filesystem.
2148 if ((mp
->mnt_vfsstat
.f_owner
!= kauth_cred_getuid(kauth_cred_get())) &&
2149 (error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
2154 * Don't allow unmounting the root file system (or the associated VM or DATA mounts) .
2156 if ((mp
->mnt_flag
& MNT_ROOTFS
) || (mp
->mnt_kern_flag
& MNTK_SYSTEM
)) {
2157 error
= EBUSY
; /* the root (or associated volumes) is always busy */
2161 #ifdef CONFIG_IMGSRC_ACCESS
2162 if (mp
->mnt_kern_flag
& MNTK_BACKS_ROOT
) {
2166 #endif /* CONFIG_IMGSRC_ACCESS */
2168 return dounmount(mp
, flags
, 1, ctx
);
2176 * Do the actual file system unmount.
2179 dounmount(struct mount
*mp
, int flags
, int withref
, vfs_context_t ctx
)
2181 vnode_t coveredvp
= (vnode_t
)0;
2184 int forcedunmount
= 0;
2186 struct vnode
*devvp
= NULLVP
;
2188 proc_t p
= vfs_context_proc(ctx
);
2190 int pflags_save
= 0;
2191 #endif /* CONFIG_TRIGGERS */
2194 if (!(flags
& MNT_FORCE
)) {
2195 fsevent_unmount(mp
, ctx
); /* has to come first! */
2202 * If already an unmount in progress just return EBUSY.
2203 * Even a forced unmount cannot override.
2205 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
2213 if (flags
& MNT_FORCE
) {
2215 mp
->mnt_lflag
|= MNT_LFORCE
;
2219 if (flags
& MNT_NOBLOCK
&& p
!= kernproc
) {
2220 pflags_save
= OSBitOrAtomic(P_NOREMOTEHANG
, &p
->p_flag
);
2224 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
2225 mp
->mnt_lflag
|= MNT_LUNMOUNT
;
2226 mp
->mnt_flag
&= ~MNT_ASYNC
;
2228 * anyone currently in the fast path that
2229 * trips over the cached rootvp will be
2230 * dumped out and forced into the slow path
2231 * to regenerate a new cached value
2233 mp
->mnt_realrootvp
= NULLVP
;
2236 if (forcedunmount
&& (flags
& MNT_LNOSUB
) == 0) {
2238 * Force unmount any mounts in this filesystem.
2239 * If any unmounts fail - just leave them dangling.
2242 (void) dounmount_submounts(mp
, flags
| MNT_LNOSUB
, ctx
);
2246 * taking the name_cache_lock exclusively will
2247 * insure that everyone is out of the fast path who
2248 * might be trying to use a now stale copy of
2249 * vp->v_mountedhere->mnt_realrootvp
2250 * bumping mount_generation causes the cached values
2255 name_cache_unlock();
2258 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
2263 if (forcedunmount
== 0) {
2264 ubc_umount(mp
); /* release cached vnodes */
2265 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
2266 error
= VFS_SYNC(mp
, MNT_WAIT
, ctx
);
2269 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
2270 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
2271 mp
->mnt_lflag
&= ~MNT_LFORCE
;
2277 IOBSDMountChange(mp
, kIOMountChangeUnmount
);
2280 vfs_nested_trigger_unmounts(mp
, flags
, ctx
);
2283 if (forcedunmount
) {
2284 lflags
|= FORCECLOSE
;
2286 error
= vflush(mp
, NULLVP
, SKIPSWAP
| SKIPSYSTEM
| SKIPROOT
| lflags
);
2287 if ((forcedunmount
== 0) && error
) {
2289 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
2290 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
2291 mp
->mnt_lflag
&= ~MNT_LFORCE
;
2295 /* make sure there are no one in the mount iterations or lookup */
2296 mount_iterdrain(mp
);
2298 error
= VFS_UNMOUNT(mp
, flags
, ctx
);
2300 mount_iterreset(mp
);
2302 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
2303 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
2304 mp
->mnt_lflag
&= ~MNT_LFORCE
;
2308 /* increment the operations count */
2310 OSAddAtomic(1, &vfs_nummntops
);
2313 if (mp
->mnt_devvp
&& mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSLOCALARGS
) {
2314 /* hold an io reference and drop the usecount before close */
2315 devvp
= mp
->mnt_devvp
;
2316 vnode_getalways(devvp
);
2318 VNOP_CLOSE(devvp
, mp
->mnt_flag
& MNT_RDONLY
? FREAD
: FREAD
| FWRITE
,
2320 vnode_clearmountedon(devvp
);
2323 lck_rw_done(&mp
->mnt_rwlock
);
2324 mount_list_remove(mp
);
2325 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
2327 /* mark the mount point hook in the vp but not drop the ref yet */
2328 if ((coveredvp
= mp
->mnt_vnodecovered
) != NULLVP
) {
2330 * The covered vnode needs special handling. Trying to get an
2331 * iocount must not block here as this may lead to deadlocks
2332 * if the Filesystem to which the covered vnode belongs is
2333 * undergoing forced unmounts. Since we hold a usecount, the
2334 * vnode cannot be reused (it can, however, still be terminated)
2336 vnode_getalways(coveredvp
);
2337 vnode_lock_spin(coveredvp
);
2340 coveredvp
->v_mountedhere
= (struct mount
*)0;
2341 CLR(coveredvp
->v_flag
, VMOUNT
);
2343 vnode_unlock(coveredvp
);
2344 vnode_put(coveredvp
);
2348 mp
->mnt_vtable
->vfc_refcount
--;
2349 mount_list_unlock();
2351 cache_purgevfs(mp
); /* remove cache entries for this file sys */
2352 vfs_event_signal(NULL
, VQ_UNMOUNT
, (intptr_t)NULL
);
2354 mp
->mnt_lflag
|= MNT_LDEAD
;
2356 if (mp
->mnt_lflag
& MNT_LWAIT
) {
2358 * do the wakeup here
2359 * in case we block in mount_refdrain
2360 * which will drop the mount lock
2361 * and allow anyone blocked in vfs_busy
2362 * to wakeup and see the LDEAD state
2364 mp
->mnt_lflag
&= ~MNT_LWAIT
;
2365 wakeup((caddr_t
)mp
);
2369 /* free disk_conditioner_info structure for this mount */
2370 disk_conditioner_unmount(mp
);
2373 if (mp
->mnt_lflag
& MNT_LWAIT
) {
2374 mp
->mnt_lflag
&= ~MNT_LWAIT
;
2379 if (flags
& MNT_NOBLOCK
&& p
!= kernproc
) {
2380 // Restore P_NOREMOTEHANG bit to its previous value
2381 if ((pflags_save
& P_NOREMOTEHANG
) == 0) {
2382 OSBitAndAtomic(~((uint32_t) P_NOREMOTEHANG
), &p
->p_flag
);
2387 * Callback and context are set together under the mount lock, and
2388 * never cleared, so we're safe to examine them here, drop the lock,
2391 if (mp
->mnt_triggercallback
!= NULL
) {
2394 mp
->mnt_triggercallback(mp
, VTC_RELEASE
, mp
->mnt_triggerdata
, ctx
);
2395 } else if (did_vflush
) {
2396 mp
->mnt_triggercallback(mp
, VTC_REPLACE
, mp
->mnt_triggerdata
, ctx
);
2403 #endif /* CONFIG_TRIGGERS */
2405 lck_rw_done(&mp
->mnt_rwlock
);
2408 wakeup((caddr_t
)mp
);
2412 if ((coveredvp
!= NULLVP
)) {
2413 vnode_t pvp
= NULLVP
;
2416 * The covered vnode needs special handling. Trying to
2417 * get an iocount must not block here as this may lead
2418 * to deadlocks if the Filesystem to which the covered
2419 * vnode belongs is undergoing forced unmounts. Since we
2420 * hold a usecount, the vnode cannot be reused
2421 * (it can, however, still be terminated).
2423 vnode_getalways(coveredvp
);
2425 mount_dropcrossref(mp
, coveredvp
, 0);
2427 * We'll _try_ to detect if this really needs to be
2428 * done. The coveredvp can only be in termination (or
2429 * terminated) if the coveredvp's mount point is in a
2430 * forced unmount (or has been) since we still hold the
2433 if (!vnode_isrecycled(coveredvp
)) {
2434 pvp
= vnode_getparent(coveredvp
);
2436 if (coveredvp
->v_resolve
) {
2437 vnode_trigger_rearm(coveredvp
, ctx
);
2442 vnode_rele(coveredvp
);
2443 vnode_put(coveredvp
);
2447 lock_vnode_and_post(pvp
, NOTE_WRITE
);
2450 } else if (mp
->mnt_flag
& MNT_ROOTFS
) {
2451 mount_lock_destroy(mp
);
2453 mac_mount_label_destroy(mp
);
2455 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
2457 panic("dounmount: no coveredvp");
2464 * Unmount any mounts in this filesystem.
2467 dounmount_submounts(struct mount
*mp
, int flags
, vfs_context_t ctx
)
2470 fsid_t
*fsids
, fsid
;
2472 int count
= 0, i
, m
= 0;
2477 // Get an array to hold the submounts fsids.
2478 TAILQ_FOREACH(smp
, &mountlist
, mnt_list
)
2480 fsids_sz
= count
* sizeof(fsid_t
);
2481 MALLOC(fsids
, fsid_t
*, fsids_sz
, M_TEMP
, M_NOWAIT
);
2482 if (fsids
== NULL
) {
2483 mount_list_unlock();
2486 fsids
[0] = mp
->mnt_vfsstat
.f_fsid
; // Prime the pump
2489 * Fill the array with submount fsids.
2490 * Since mounts are always added to the tail of the mount list, the
2491 * list is always in mount order.
2492 * For each mount check if the mounted-on vnode belongs to a
2493 * mount that's already added to our array of mounts to be unmounted.
2495 for (smp
= TAILQ_NEXT(mp
, mnt_list
); smp
; smp
= TAILQ_NEXT(smp
, mnt_list
)) {
2496 vp
= smp
->mnt_vnodecovered
;
2500 fsid
= vnode_mount(vp
)->mnt_vfsstat
.f_fsid
; // Underlying fsid
2501 for (i
= 0; i
<= m
; i
++) {
2502 if (fsids
[i
].val
[0] == fsid
.val
[0] &&
2503 fsids
[i
].val
[1] == fsid
.val
[1]) {
2504 fsids
[++m
] = smp
->mnt_vfsstat
.f_fsid
;
2509 mount_list_unlock();
2511 // Unmount the submounts in reverse order. Ignore errors.
2512 for (i
= m
; i
> 0; i
--) {
2513 smp
= mount_list_lookupby_fsid(&fsids
[i
], 0, 1);
2516 mount_iterdrop(smp
);
2517 (void) dounmount(smp
, flags
, 1, ctx
);
2522 FREE(fsids
, M_TEMP
);
2527 mount_dropcrossref(mount_t mp
, vnode_t dp
, int need_put
)
2532 if (mp
->mnt_crossref
< 0) {
2533 panic("mount cross refs -ve");
2536 if ((mp
!= dp
->v_mountedhere
) && (mp
->mnt_crossref
== 0)) {
2538 vnode_put_locked(dp
);
2542 mount_lock_destroy(mp
);
2544 mac_mount_label_destroy(mp
);
2546 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
2550 vnode_put_locked(dp
);
2557 * Sync each mounted filesystem.
2563 int print_vmpage_stat
= 0;
2566 * sync_callback: simple wrapper that calls VFS_SYNC() on volumes
2567 * mounted read-write with the passed waitfor value.
2569 * Parameters: mp mount-point descriptor per mounted file-system instance.
2570 * arg user argument (please see below)
2572 * User argument is a pointer to 32 bit unsigned integer which describes the
2573 * type of waitfor value to set for calling VFS_SYNC(). If user argument is
2574 * passed as NULL, VFS_SYNC() is called with MNT_NOWAIT set as the default
2577 * Returns: VFS_RETURNED
2580 sync_callback(mount_t mp
, void *arg
)
2582 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
2583 int asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
2584 unsigned waitfor
= MNT_NOWAIT
;
2587 waitfor
= *(uint32_t*)arg
;
2590 /* Sanity check for flags - these are the only valid combinations for the flag bits*/
2591 if (waitfor
!= MNT_WAIT
&&
2592 waitfor
!= (MNT_WAIT
| MNT_VOLUME
) &&
2593 waitfor
!= MNT_NOWAIT
&&
2594 waitfor
!= (MNT_NOWAIT
| MNT_VOLUME
) &&
2595 waitfor
!= MNT_DWAIT
&&
2596 waitfor
!= (MNT_DWAIT
| MNT_VOLUME
)) {
2597 panic("Passed inappropriate waitfor %u to "
2598 "sync_callback()", waitfor
);
2601 mp
->mnt_flag
&= ~MNT_ASYNC
;
2602 (void)VFS_SYNC(mp
, waitfor
, vfs_context_kernel());
2604 mp
->mnt_flag
|= MNT_ASYNC
;
2608 return VFS_RETURNED
;
2613 sync(__unused proc_t p
, __unused
struct sync_args
*uap
, __unused
int32_t *retval
)
2615 vfs_iterate(LK_NOWAIT
, sync_callback
, NULL
);
2617 if (print_vmpage_stat
) {
2618 vm_countdirtypages();
2625 #endif /* DIAGNOSTIC */
2631 SYNC_ONLY_RELIABLE_MEDIA
= 1,
2632 SYNC_ONLY_UNRELIABLE_MEDIA
= 2
2636 sync_internal_callback(mount_t mp
, void *arg
)
2639 int is_reliable
= !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
) &&
2640 (mp
->mnt_flag
& MNT_LOCAL
);
2641 sync_type_t sync_type
= *((sync_type_t
*)arg
);
2643 if ((sync_type
== SYNC_ONLY_RELIABLE_MEDIA
) && !is_reliable
) {
2644 return VFS_RETURNED
;
2645 } else if ((sync_type
== SYNC_ONLY_UNRELIABLE_MEDIA
) && is_reliable
) {
2646 return VFS_RETURNED
;
2650 (void)sync_callback(mp
, NULL
);
2652 return VFS_RETURNED
;
2655 int sync_thread_state
= 0;
2656 int sync_timeout_seconds
= 5;
2658 #define SYNC_THREAD_RUN 0x0001
2659 #define SYNC_THREAD_RUNNING 0x0002
2662 sync_thread(__unused
void *arg
, __unused wait_result_t wr
)
2664 sync_type_t sync_type
;
2666 lck_mtx_lock(sync_mtx_lck
);
2667 while (sync_thread_state
& SYNC_THREAD_RUN
) {
2668 sync_thread_state
&= ~SYNC_THREAD_RUN
;
2669 lck_mtx_unlock(sync_mtx_lck
);
2671 sync_type
= SYNC_ONLY_RELIABLE_MEDIA
;
2672 vfs_iterate(LK_NOWAIT
, sync_internal_callback
, &sync_type
);
2673 sync_type
= SYNC_ONLY_UNRELIABLE_MEDIA
;
2674 vfs_iterate(LK_NOWAIT
, sync_internal_callback
, &sync_type
);
2676 lck_mtx_lock(sync_mtx_lck
);
2679 * This wakeup _has_ to be issued before the lock is released otherwise
2680 * we may end up waking up a thread in sync_internal which is
2681 * expecting a wakeup from a thread it just created and not from this
2682 * thread which is about to exit.
2684 wakeup(&sync_thread_state
);
2685 sync_thread_state
&= ~SYNC_THREAD_RUNNING
;
2686 lck_mtx_unlock(sync_mtx_lck
);
2688 if (print_vmpage_stat
) {
2689 vm_countdirtypages();
2696 #endif /* DIAGNOSTIC */
2699 struct timeval sync_timeout_last_print
= {.tv_sec
= 0, .tv_usec
= 0};
2702 * An in-kernel sync for power management to call.
2703 * This function always returns within sync_timeout seconds.
2705 __private_extern__
int
2710 int thread_created
= FALSE
;
2711 struct timespec ts
= {.tv_sec
= sync_timeout_seconds
, .tv_nsec
= 0};
2713 lck_mtx_lock(sync_mtx_lck
);
2714 sync_thread_state
|= SYNC_THREAD_RUN
;
2715 if (!(sync_thread_state
& SYNC_THREAD_RUNNING
)) {
2718 sync_thread_state
|= SYNC_THREAD_RUNNING
;
2719 kr
= kernel_thread_start(sync_thread
, NULL
, &thd
);
2720 if (kr
!= KERN_SUCCESS
) {
2721 sync_thread_state
&= ~SYNC_THREAD_RUNNING
;
2722 lck_mtx_unlock(sync_mtx_lck
);
2723 printf("sync_thread failed\n");
2726 thread_created
= TRUE
;
2729 error
= msleep((caddr_t
)&sync_thread_state
, sync_mtx_lck
,
2730 (PVFS
| PDROP
| PCATCH
), "sync_thread", &ts
);
2735 if (now
.tv_sec
- sync_timeout_last_print
.tv_sec
> 120) {
2736 printf("sync timed out: %d sec\n", sync_timeout_seconds
);
2737 sync_timeout_last_print
.tv_sec
= now
.tv_sec
;
2741 if (thread_created
) {
2742 thread_deallocate(thd
);
2746 } /* end of sync_internal call */
2749 * Change filesystem quotas.
2753 quotactl(proc_t p
, struct quotactl_args
*uap
, __unused
int32_t *retval
)
2756 int error
, quota_cmd
, quota_status
= 0;
2759 struct nameidata nd
;
2760 vfs_context_t ctx
= vfs_context_current();
2761 struct dqblk my_dqblk
= {};
2763 AUDIT_ARG(uid
, uap
->uid
);
2764 AUDIT_ARG(cmd
, uap
->cmd
);
2765 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
2771 mp
= nd
.ni_vp
->v_mount
;
2772 vnode_put(nd
.ni_vp
);
2775 /* copyin any data we will need for downstream code */
2776 quota_cmd
= uap
->cmd
>> SUBCMDSHIFT
;
2778 switch (quota_cmd
) {
2780 /* uap->arg specifies a file from which to take the quotas */
2781 fnamelen
= MAXPATHLEN
;
2782 datap
= kalloc(MAXPATHLEN
);
2783 error
= copyinstr(uap
->arg
, datap
, MAXPATHLEN
, &fnamelen
);
2786 /* uap->arg is a pointer to a dqblk structure. */
2787 datap
= (caddr_t
) &my_dqblk
;
2791 /* uap->arg is a pointer to a dqblk structure. */
2792 datap
= (caddr_t
) &my_dqblk
;
2793 if (proc_is64bit(p
)) {
2794 struct user_dqblk my_dqblk64
;
2795 error
= copyin(uap
->arg
, (caddr_t
)&my_dqblk64
, sizeof(my_dqblk64
));
2797 munge_dqblk(&my_dqblk
, &my_dqblk64
, FALSE
);
2800 error
= copyin(uap
->arg
, (caddr_t
)&my_dqblk
, sizeof(my_dqblk
));
2804 /* uap->arg is a pointer to an integer */
2805 datap
= (caddr_t
) "a_status
;
2813 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
, datap
, ctx
);
2816 switch (quota_cmd
) {
2818 if (datap
!= NULL
) {
2819 kfree(datap
, MAXPATHLEN
);
2823 /* uap->arg is a pointer to a dqblk structure we need to copy out to */
2825 if (proc_is64bit(p
)) {
2826 struct user_dqblk my_dqblk64
;
2828 memset(&my_dqblk64
, 0, sizeof(my_dqblk64
));
2829 munge_dqblk(&my_dqblk
, &my_dqblk64
, TRUE
);
2830 error
= copyout((caddr_t
)&my_dqblk64
, uap
->arg
, sizeof(my_dqblk64
));
2832 error
= copyout(datap
, uap
->arg
, sizeof(struct dqblk
));
2837 /* uap->arg is a pointer to an integer */
2839 error
= copyout(datap
, uap
->arg
, sizeof(quota_status
));
2850 quotactl(__unused proc_t p
, __unused
struct quotactl_args
*uap
, __unused
int32_t *retval
)
2857 * Get filesystem statistics.
2859 * Returns: 0 Success
2861 * vfs_update_vfsstat:???
2862 * munge_statfs:EFAULT
2866 statfs(__unused proc_t p
, struct statfs_args
*uap
, __unused
int32_t *retval
)
2869 struct vfsstatfs
*sp
;
2871 struct nameidata nd
;
2872 vfs_context_t ctx
= vfs_context_current();
2875 NDINIT(&nd
, LOOKUP
, OP_STATFS
, FOLLOW
| AUDITVNPATH1
,
2876 UIO_USERSPACE
, uap
->path
, ctx
);
2883 sp
= &mp
->mnt_vfsstat
;
2887 error
= mac_mount_check_stat(ctx
, mp
);
2894 error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
);
2900 error
= munge_statfs(mp
, sp
, uap
->buf
, NULL
, IS_64BIT_PROCESS(p
), TRUE
);
2906 * Get filesystem statistics.
2910 fstatfs(__unused proc_t p
, struct fstatfs_args
*uap
, __unused
int32_t *retval
)
2914 struct vfsstatfs
*sp
;
2917 AUDIT_ARG(fd
, uap
->fd
);
2919 if ((error
= file_vnode(uap
->fd
, &vp
))) {
2923 error
= vnode_getwithref(vp
);
2929 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2938 error
= mac_mount_check_stat(vfs_context_current(), mp
);
2944 sp
= &mp
->mnt_vfsstat
;
2945 if ((error
= vfs_update_vfsstat(mp
, vfs_context_current(), VFS_USER_EVENT
)) != 0) {
2949 error
= munge_statfs(mp
, sp
, uap
->buf
, NULL
, IS_64BIT_PROCESS(p
), TRUE
);
2959 vfs_get_statfs64(struct mount
*mp
, struct statfs64
*sfs
)
2961 struct vfsstatfs
*vsfs
= &mp
->mnt_vfsstat
;
2963 bzero(sfs
, sizeof(*sfs
));
2965 sfs
->f_bsize
= vsfs
->f_bsize
;
2966 sfs
->f_iosize
= (int32_t)vsfs
->f_iosize
;
2967 sfs
->f_blocks
= vsfs
->f_blocks
;
2968 sfs
->f_bfree
= vsfs
->f_bfree
;
2969 sfs
->f_bavail
= vsfs
->f_bavail
;
2970 sfs
->f_files
= vsfs
->f_files
;
2971 sfs
->f_ffree
= vsfs
->f_ffree
;
2972 sfs
->f_fsid
= vsfs
->f_fsid
;
2973 sfs
->f_owner
= vsfs
->f_owner
;
2974 sfs
->f_type
= mp
->mnt_vtable
->vfc_typenum
;
2975 sfs
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
2976 sfs
->f_fssubtype
= vsfs
->f_fssubtype
;
2977 sfs
->f_flags_ext
= ((mp
->mnt_kern_flag
& MNTK_SYSTEM
) && !(mp
->mnt_kern_flag
& MNTK_SWAP_MOUNT
) && !(mp
->mnt_flag
& MNT_ROOTFS
)) ? MNT_EXT_ROOT_DATA_VOL
: 0;
2978 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
2979 strlcpy(&sfs
->f_fstypename
[0], &mp
->fstypename_override
[0], MFSTYPENAMELEN
);
2981 strlcpy(&sfs
->f_fstypename
[0], &vsfs
->f_fstypename
[0], MFSTYPENAMELEN
);
2983 strlcpy(&sfs
->f_mntonname
[0], &vsfs
->f_mntonname
[0], MAXPATHLEN
);
2984 strlcpy(&sfs
->f_mntfromname
[0], &vsfs
->f_mntfromname
[0], MAXPATHLEN
);
2988 * Get file system statistics in 64-bit mode
2991 statfs64(__unused
struct proc
*p
, struct statfs64_args
*uap
, __unused
int32_t *retval
)
2995 struct nameidata nd
;
2996 struct statfs64 sfs
;
2997 vfs_context_t ctxp
= vfs_context_current();
3000 NDINIT(&nd
, LOOKUP
, OP_STATFS
, FOLLOW
| AUDITVNPATH1
,
3001 UIO_USERSPACE
, uap
->path
, ctxp
);
3011 error
= mac_mount_check_stat(ctxp
, mp
);
3018 error
= vfs_update_vfsstat(mp
, ctxp
, VFS_USER_EVENT
);
3024 vfs_get_statfs64(mp
, &sfs
);
3025 if ((mp
->mnt_kern_flag
& MNTK_SYSTEM
) && !(mp
->mnt_kern_flag
& MNTK_SWAP_MOUNT
) && !(mp
->mnt_flag
& MNT_ROOTFS
) &&
3026 (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
)) {
3027 /* This process does not want to see a seperate data volume mountpoint */
3028 strlcpy(&sfs
.f_mntonname
[0], "/", sizeof("/"));
3030 error
= copyout(&sfs
, uap
->buf
, sizeof(sfs
));
3037 * Get file system statistics in 64-bit mode
3040 fstatfs64(__unused
struct proc
*p
, struct fstatfs64_args
*uap
, __unused
int32_t *retval
)
3044 struct statfs64 sfs
;
3047 AUDIT_ARG(fd
, uap
->fd
);
3049 if ((error
= file_vnode(uap
->fd
, &vp
))) {
3053 error
= vnode_getwithref(vp
);
3059 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
3068 error
= mac_mount_check_stat(vfs_context_current(), mp
);
3074 if ((error
= vfs_update_vfsstat(mp
, vfs_context_current(), VFS_USER_EVENT
)) != 0) {
3078 vfs_get_statfs64(mp
, &sfs
);
3079 if ((mp
->mnt_kern_flag
& MNTK_SYSTEM
) && !(mp
->mnt_kern_flag
& MNTK_SWAP_MOUNT
) && !(mp
->mnt_flag
& MNT_ROOTFS
) &&
3080 (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME
)) {
3081 /* This process does not want to see a seperate data volume mountpoint */
3082 strlcpy(&sfs
.f_mntonname
[0], "/", sizeof("/"));
3084 error
= copyout(&sfs
, uap
->buf
, sizeof(sfs
));
3093 struct getfsstat_struct
{
3104 getfsstat_callback(mount_t mp
, void * arg
)
3106 struct getfsstat_struct
*fstp
= (struct getfsstat_struct
*)arg
;
3107 struct vfsstatfs
*sp
;
3109 vfs_context_t ctx
= vfs_context_current();
3111 if (fstp
->sfsp
&& fstp
->count
< fstp
->maxcount
) {
3113 error
= mac_mount_check_stat(ctx
, mp
);
3115 fstp
->error
= error
;
3116 return VFS_RETURNED_DONE
;
3119 sp
= &mp
->mnt_vfsstat
;
3121 * If MNT_NOWAIT is specified, do not refresh the
3122 * fsstat cache. MNT_WAIT/MNT_DWAIT overrides MNT_NOWAIT.
3124 if ((mp
->mnt_lflag
& MNT_LDEAD
) ||
3125 (((fstp
->flags
& MNT_NOWAIT
) == 0 || (fstp
->flags
& (MNT_WAIT
| MNT_DWAIT
))) &&
3126 (!(mp
->mnt_lflag
& MNT_LUNMOUNT
)) &&
3127 (error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
)))) {
3128 KAUTH_DEBUG("vfs_update_vfsstat returned %d", error
);
3129 return VFS_RETURNED
;
3133 * Need to handle LP64 version of struct statfs
3135 error
= munge_statfs(mp
, sp
, fstp
->sfsp
, &my_size
, IS_64BIT_PROCESS(vfs_context_proc(ctx
)), FALSE
);
3137 fstp
->error
= error
;
3138 return VFS_RETURNED_DONE
;
3140 fstp
->sfsp
+= my_size
;
3144 error
= mac_mount_label_get(mp
, *fstp
->mp
);
3146 fstp
->error
= error
;
3147 return VFS_RETURNED_DONE
;
3154 return VFS_RETURNED
;
3158 * Get statistics on all filesystems.
3161 getfsstat(__unused proc_t p
, struct getfsstat_args
*uap
, int *retval
)
3163 struct __mac_getfsstat_args muap
;
3165 muap
.buf
= uap
->buf
;
3166 muap
.bufsize
= uap
->bufsize
;
3167 muap
.mac
= USER_ADDR_NULL
;
3169 muap
.flags
= uap
->flags
;
3171 return __mac_getfsstat(p
, &muap
, retval
);
3175 * __mac_getfsstat: Get MAC-related file system statistics
3177 * Parameters: p (ignored)
3178 * uap User argument descriptor (see below)
3179 * retval Count of file system statistics (N stats)
3181 * Indirect: uap->bufsize Buffer size
3182 * uap->macsize MAC info size
3183 * uap->buf Buffer where information will be returned
3185 * uap->flags File system flags
3188 * Returns: 0 Success
3193 __mac_getfsstat(__unused proc_t p
, struct __mac_getfsstat_args
*uap
, int *retval
)
3197 size_t count
, maxcount
, bufsize
, macsize
;
3198 struct getfsstat_struct fst
;
3200 if ((unsigned)uap
->bufsize
> INT_MAX
|| (unsigned)uap
->macsize
> INT_MAX
) {
3204 bufsize
= (size_t) uap
->bufsize
;
3205 macsize
= (size_t) uap
->macsize
;
3207 if (IS_64BIT_PROCESS(p
)) {
3208 maxcount
= bufsize
/ sizeof(struct user64_statfs
);
3210 maxcount
= bufsize
/ sizeof(struct user32_statfs
);
3218 if (uap
->mac
!= USER_ADDR_NULL
) {
3223 count
= (macsize
/ (IS_64BIT_PROCESS(p
) ? 8 : 4));
3224 if (count
!= maxcount
) {
3228 /* Copy in the array */
3229 MALLOC(mp0
, u_int32_t
*, macsize
, M_MACTEMP
, M_WAITOK
);
3234 error
= copyin(uap
->mac
, mp0
, macsize
);
3236 FREE(mp0
, M_MACTEMP
);
3240 /* Normalize to an array of user_addr_t */
3241 MALLOC(mp
, user_addr_t
*, count
* sizeof(user_addr_t
), M_MACTEMP
, M_WAITOK
);
3243 FREE(mp0
, M_MACTEMP
);
3247 for (i
= 0; i
< count
; i
++) {
3248 if (IS_64BIT_PROCESS(p
)) {
3249 mp
[i
] = ((user_addr_t
*)mp0
)[i
];
3251 mp
[i
] = (user_addr_t
)mp0
[i
];
3254 FREE(mp0
, M_MACTEMP
);
3261 fst
.flags
= uap
->flags
;
3264 fst
.maxcount
= maxcount
;
3267 vfs_iterate(VFS_ITERATE_NOSKIP_UNMOUNT
, getfsstat_callback
, &fst
);
3270 FREE(mp
, M_MACTEMP
);
3274 KAUTH_DEBUG("ERROR - %s gets %d", p
->p_comm
, fst
.error
);
3278 if (fst
.sfsp
&& fst
.count
> fst
.maxcount
) {
3279 *retval
= fst
.maxcount
;
3281 *retval
= fst
.count
;
3287 getfsstat64_callback(mount_t mp
, void * arg
)
3289 struct getfsstat_struct
*fstp
= (struct getfsstat_struct
*)arg
;
3290 struct vfsstatfs
*sp
;
3291 struct statfs64 sfs
;
3294 if (fstp
->sfsp
&& fstp
->count
< fstp
->maxcount
) {
3296 error
= mac_mount_check_stat(vfs_context_current(), mp
);
3298 fstp
->error
= error
;
3299 return VFS_RETURNED_DONE
;
3302 sp
= &mp
->mnt_vfsstat
;
3304 * If MNT_NOWAIT is specified, do not refresh the fsstat
3305 * cache. MNT_WAIT overrides MNT_NOWAIT.
3307 * We treat MNT_DWAIT as MNT_WAIT for all instances of
3308 * getfsstat, since the constants are out of the same
3311 if ((mp
->mnt_lflag
& MNT_LDEAD
) ||
3312 ((((fstp
->flags
& MNT_NOWAIT
) == 0) || (fstp
->flags
& (MNT_WAIT
| MNT_DWAIT
))) &&
3313 (!(mp
->mnt_lflag
& MNT_LUNMOUNT
)) &&
3314 (error
= vfs_update_vfsstat(mp
, vfs_context_current(), VFS_USER_EVENT
)))) {
3315 KAUTH_DEBUG("vfs_update_vfsstat returned %d", error
);
3316 return VFS_RETURNED
;
3319 vfs_get_statfs64(mp
, &sfs
);
3320 error
= copyout(&sfs
, fstp
->sfsp
, sizeof(sfs
));
3322 fstp
->error
= error
;
3323 return VFS_RETURNED_DONE
;
3325 fstp
->sfsp
+= sizeof(sfs
);
3328 return VFS_RETURNED
;
3332 * Get statistics on all file systems in 64 bit mode.
3335 getfsstat64(__unused proc_t p
, struct getfsstat64_args
*uap
, int *retval
)
3338 int count
, maxcount
;
3339 struct getfsstat_struct fst
;
3341 maxcount
= uap
->bufsize
/ sizeof(struct statfs64
);
3347 fst
.flags
= uap
->flags
;
3350 fst
.maxcount
= maxcount
;
3352 vfs_iterate(VFS_ITERATE_NOSKIP_UNMOUNT
, getfsstat64_callback
, &fst
);
3355 KAUTH_DEBUG("ERROR - %s gets %d", p
->p_comm
, fst
.error
);
3359 if (fst
.sfsp
&& fst
.count
> fst
.maxcount
) {
3360 *retval
= fst
.maxcount
;
3362 *retval
= fst
.count
;
3369 * gets the associated vnode with the file descriptor passed.
3373 * ctx - vfs context of caller
3374 * fd - file descriptor for which vnode is required.
3375 * vpp - Pointer to pointer to vnode to be returned.
3377 * The vnode is returned with an iocount so any vnode obtained
3378 * by this call needs a vnode_put
3382 vnode_getfromfd(vfs_context_t ctx
, int fd
, vnode_t
*vpp
)
3386 struct fileproc
*fp
;
3387 proc_t p
= vfs_context_proc(ctx
);
3391 error
= fp_getfvp(p
, fd
, &fp
, &vp
);
3396 error
= vnode_getwithref(vp
);
3398 (void)fp_drop(p
, fd
, fp
, 0);
3402 (void)fp_drop(p
, fd
, fp
, 0);
3408 * Wrapper function around namei to start lookup from a directory
3409 * specified by a file descriptor ni_dirfd.
3411 * In addition to all the errors returned by namei, this call can
3412 * return ENOTDIR if the file descriptor does not refer to a directory.
3413 * and EBADF if the file descriptor is not valid.
3416 nameiat(struct nameidata
*ndp
, int dirfd
)
3418 if ((dirfd
!= AT_FDCWD
) &&
3419 !(ndp
->ni_flag
& NAMEI_CONTLOOKUP
) &&
3420 !(ndp
->ni_cnd
.cn_flags
& USEDVP
)) {
3424 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
3425 error
= copyin(ndp
->ni_dirp
, &c
, sizeof(char));
3430 c
= *((char *)(ndp
->ni_dirp
));
3436 error
= vnode_getfromfd(ndp
->ni_cnd
.cn_context
, dirfd
,
3442 if (vnode_vtype(dvp_at
) != VDIR
) {
3447 ndp
->ni_dvp
= dvp_at
;
3448 ndp
->ni_cnd
.cn_flags
|= USEDVP
;
3450 ndp
->ni_cnd
.cn_flags
&= ~USEDVP
;
3460 * Change current working directory to a given file descriptor.
3464 common_fchdir(proc_t p
, struct fchdir_args
*uap
, int per_thread
)
3466 struct filedesc
*fdp
= p
->p_fd
;
3472 vfs_context_t ctx
= vfs_context_current();
3474 AUDIT_ARG(fd
, uap
->fd
);
3475 if (per_thread
&& uap
->fd
== -1) {
3477 * Switching back from per-thread to per process CWD; verify we
3478 * in fact have one before proceeding. The only success case
3479 * for this code path is to return 0 preemptively after zapping
3480 * the thread structure contents.
3482 thread_t th
= vfs_context_thread(ctx
);
3484 uthread_t uth
= get_bsdthread_info(th
);
3486 uth
->uu_cdir
= NULLVP
;
3487 if (tvp
!= NULLVP
) {
3495 if ((error
= file_vnode(uap
->fd
, &vp
))) {
3498 if ((error
= vnode_getwithref(vp
))) {
3503 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3505 if (vp
->v_type
!= VDIR
) {
3511 error
= mac_vnode_check_chdir(ctx
, vp
);
3516 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
3521 while (!error
&& (mp
= vp
->v_mountedhere
) != NULL
) {
3522 if (vfs_busy(mp
, LK_NOWAIT
)) {
3526 error
= VFS_ROOT(mp
, &tdp
, ctx
);
3537 if ((error
= vnode_ref(vp
))) {
3543 thread_t th
= vfs_context_thread(ctx
);
3545 uthread_t uth
= get_bsdthread_info(th
);
3548 OSBitOrAtomic(P_THCWD
, &p
->p_flag
);
3574 fchdir(proc_t p
, struct fchdir_args
*uap
, __unused
int32_t *retval
)
3576 return common_fchdir(p
, uap
, 0);
3580 __pthread_fchdir(proc_t p
, struct __pthread_fchdir_args
*uap
, __unused
int32_t *retval
)
3582 return common_fchdir(p
, (void *)uap
, 1);
3587 * Change current working directory (".").
3589 * Returns: 0 Success
3590 * change_dir:ENOTDIR
3592 * vnode_ref:ENOENT No such file or directory
3596 chdir_internal(proc_t p
, vfs_context_t ctx
, struct nameidata
*ndp
, int per_thread
)
3598 struct filedesc
*fdp
= p
->p_fd
;
3602 error
= change_dir(ndp
, ctx
);
3606 if ((error
= vnode_ref(ndp
->ni_vp
))) {
3607 vnode_put(ndp
->ni_vp
);
3611 * drop the iocount we picked up in change_dir
3613 vnode_put(ndp
->ni_vp
);
3616 thread_t th
= vfs_context_thread(ctx
);
3618 uthread_t uth
= get_bsdthread_info(th
);
3620 uth
->uu_cdir
= ndp
->ni_vp
;
3621 OSBitOrAtomic(P_THCWD
, &p
->p_flag
);
3623 vnode_rele(ndp
->ni_vp
);
3629 fdp
->fd_cdir
= ndp
->ni_vp
;
3642 * Change current working directory (".").
3644 * Returns: 0 Success
3645 * chdir_internal:ENOTDIR
3646 * chdir_internal:ENOENT No such file or directory
3647 * chdir_internal:???
3651 common_chdir(proc_t p
, struct chdir_args
*uap
, int per_thread
)
3653 struct nameidata nd
;
3654 vfs_context_t ctx
= vfs_context_current();
3656 NDINIT(&nd
, LOOKUP
, OP_CHDIR
, FOLLOW
| AUDITVNPATH1
,
3657 UIO_USERSPACE
, uap
->path
, ctx
);
3659 return chdir_internal(p
, ctx
, &nd
, per_thread
);
3666 * Change current working directory (".") for the entire process
3668 * Parameters: p Process requesting the call
3669 * uap User argument descriptor (see below)
3672 * Indirect parameters: uap->path Directory path
3674 * Returns: 0 Success
3675 * common_chdir: ENOTDIR
3676 * common_chdir: ENOENT No such file or directory
3681 chdir(proc_t p
, struct chdir_args
*uap
, __unused
int32_t *retval
)
3683 return common_chdir(p
, (void *)uap
, 0);
3689 * Change current working directory (".") for a single thread
3691 * Parameters: p Process requesting the call
3692 * uap User argument descriptor (see below)
3695 * Indirect parameters: uap->path Directory path
3697 * Returns: 0 Success
3698 * common_chdir: ENOTDIR
3699 * common_chdir: ENOENT No such file or directory
3704 __pthread_chdir(proc_t p
, struct __pthread_chdir_args
*uap
, __unused
int32_t *retval
)
3706 return common_chdir(p
, (void *)uap
, 1);
3711 * Change notion of root (``/'') directory.
3715 chroot(proc_t p
, struct chroot_args
*uap
, __unused
int32_t *retval
)
3717 struct filedesc
*fdp
= p
->p_fd
;
3719 struct nameidata nd
;
3721 vfs_context_t ctx
= vfs_context_current();
3723 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
3727 NDINIT(&nd
, LOOKUP
, OP_CHROOT
, FOLLOW
| AUDITVNPATH1
,
3728 UIO_USERSPACE
, uap
->path
, ctx
);
3729 error
= change_dir(&nd
, ctx
);
3735 error
= mac_vnode_check_chroot(ctx
, nd
.ni_vp
,
3738 vnode_put(nd
.ni_vp
);
3743 if ((error
= vnode_ref(nd
.ni_vp
))) {
3744 vnode_put(nd
.ni_vp
);
3747 vnode_put(nd
.ni_vp
);
3751 fdp
->fd_rdir
= nd
.ni_vp
;
3752 fdp
->fd_flags
|= FD_CHROOT
;
3763 * Common routine for chroot and chdir.
3765 * Returns: 0 Success
3766 * ENOTDIR Not a directory
3767 * namei:??? [anything namei can return]
3768 * vnode_authorize:??? [anything vnode_authorize can return]
3771 change_dir(struct nameidata
*ndp
, vfs_context_t ctx
)
3776 if ((error
= namei(ndp
))) {
3782 if (vp
->v_type
!= VDIR
) {
3788 error
= mac_vnode_check_chdir(ctx
, vp
);
3795 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
3805 * Free the vnode data (for directories) associated with the file glob.
3808 fg_vn_data_alloc(void)
3810 struct fd_vn_data
*fvdata
;
3812 /* Allocate per fd vnode data */
3813 MALLOC(fvdata
, struct fd_vn_data
*, (sizeof(struct fd_vn_data
)),
3814 M_FD_VN_DATA
, M_WAITOK
| M_ZERO
);
3815 lck_mtx_init(&fvdata
->fv_lock
, fd_vn_lck_grp
, fd_vn_lck_attr
);
3820 * Free the vnode data (for directories) associated with the file glob.
3823 fg_vn_data_free(void *fgvndata
)
3825 struct fd_vn_data
*fvdata
= (struct fd_vn_data
*)fgvndata
;
3827 if (fvdata
->fv_buf
) {
3828 FREE(fvdata
->fv_buf
, M_FD_DIRBUF
);
3830 lck_mtx_destroy(&fvdata
->fv_lock
, fd_vn_lck_grp
);
3831 FREE(fvdata
, M_FD_VN_DATA
);
3835 * Check permissions, allocate an open file structure,
3836 * and call the device open routine if any.
3838 * Returns: 0 Success
3849 * XXX Need to implement uid, gid
3852 open1(vfs_context_t ctx
, struct nameidata
*ndp
, int uflags
,
3853 struct vnode_attr
*vap
, fp_allocfn_t fp_zalloc
, void *cra
,
3856 proc_t p
= vfs_context_proc(ctx
);
3857 uthread_t uu
= get_bsdthread_info(vfs_context_thread(ctx
));
3858 struct fileproc
*fp
;
3861 int type
, indx
, error
;
3863 struct vfs_context context
;
3867 if ((oflags
& O_ACCMODE
) == O_ACCMODE
) {
3871 flags
= FFLAGS(uflags
);
3872 CLR(flags
, FENCRYPTED
);
3873 CLR(flags
, FUNENCRYPTED
);
3875 AUDIT_ARG(fflags
, oflags
);
3876 AUDIT_ARG(mode
, vap
->va_mode
);
3878 if ((error
= falloc_withalloc(p
,
3879 &fp
, &indx
, ctx
, fp_zalloc
, cra
)) != 0) {
3882 uu
->uu_dupfd
= -indx
- 1;
3884 if ((error
= vn_open_auth(ndp
, &flags
, vap
))) {
3885 if ((error
== ENODEV
|| error
== ENXIO
) && (uu
->uu_dupfd
>= 0)) { /* XXX from fdopen */
3886 if ((error
= dupfdopen(p
->p_fd
, indx
, uu
->uu_dupfd
, flags
, error
)) == 0) {
3887 fp_drop(p
, indx
, NULL
, 0);
3892 if (error
== ERESTART
) {
3895 fp_free(p
, indx
, fp
);
3901 fp
->f_fglob
->fg_flag
= flags
& (FMASK
| O_EVTONLY
| FENCRYPTED
| FUNENCRYPTED
);
3902 fp
->f_fglob
->fg_ops
= &vnops
;
3903 fp
->f_fglob
->fg_data
= (caddr_t
)vp
;
3905 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
3906 lf
.l_whence
= SEEK_SET
;
3909 if (flags
& O_EXLOCK
) {
3910 lf
.l_type
= F_WRLCK
;
3912 lf
.l_type
= F_RDLCK
;
3915 if ((flags
& FNONBLOCK
) == 0) {
3919 error
= mac_file_check_lock(vfs_context_ucred(ctx
), fp
->f_fglob
,
3925 if ((error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, type
, ctx
, NULL
))) {
3928 fp
->f_fglob
->fg_flag
|= FHASLOCK
;
3931 /* try to truncate by setting the size attribute */
3932 if ((flags
& O_TRUNC
) && ((error
= vnode_setsize(vp
, (off_t
)0, 0, ctx
)) != 0)) {
3937 * For directories we hold some additional information in the fd.
3939 if (vnode_vtype(vp
) == VDIR
) {
3940 fp
->f_fglob
->fg_vn_data
= fg_vn_data_alloc();
3942 fp
->f_fglob
->fg_vn_data
= NULL
;
3948 * The first terminal open (without a O_NOCTTY) by a session leader
3949 * results in it being set as the controlling terminal.
3951 if (vnode_istty(vp
) && !(p
->p_flag
& P_CONTROLT
) &&
3952 !(flags
& O_NOCTTY
)) {
3955 (void)(*fp
->f_fglob
->fg_ops
->fo_ioctl
)(fp
, (int)TIOCSCTTY
,
3956 (caddr_t
)&tmp
, ctx
);
3960 if (flags
& O_CLOEXEC
) {
3961 *fdflags(p
, indx
) |= UF_EXCLOSE
;
3963 if (flags
& O_CLOFORK
) {
3964 *fdflags(p
, indx
) |= UF_FORKCLOSE
;
3966 procfdtbl_releasefd(p
, indx
, NULL
);
3968 #if CONFIG_SECLUDED_MEMORY
3969 if (secluded_for_filecache
&&
3970 FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_VNODE
&&
3971 vnode_vtype(vp
) == VREG
) {
3972 memory_object_control_t moc
;
3974 moc
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
3976 if (moc
== MEMORY_OBJECT_CONTROL_NULL
) {
3977 /* nothing to do... */
3978 } else if (fp
->f_fglob
->fg_flag
& FWRITE
) {
3979 /* writable -> no longer eligible for secluded pages */
3980 memory_object_mark_eligible_for_secluded(moc
,
3982 } else if (secluded_for_filecache
== 1) {
3983 char pathname
[32] = { 0, };
3985 /* XXX FBDP: better way to detect /Applications/ ? */
3986 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
3987 (void)copyinstr(ndp
->ni_dirp
,
3992 copystr(CAST_DOWN(void *, ndp
->ni_dirp
),
3997 pathname
[sizeof(pathname
) - 1] = '\0';
3998 if (strncmp(pathname
,
4000 strlen("/Applications/")) == 0 &&
4002 "/Applications/Camera.app/",
4003 strlen("/Applications/Camera.app/")) != 0) {
4006 * AND from "/Applications/"
4007 * AND not from "/Applications/Camera.app/"
4008 * ==> eligible for secluded
4010 memory_object_mark_eligible_for_secluded(moc
,
4013 } else if (secluded_for_filecache
== 2) {
4015 #define DYLD_SHARED_CACHE_NAME "dyld_shared_cache_arm64"
4017 #define DYLD_SHARED_CACHE_NAME "dyld_shared_cache_armv7"
4019 /* not implemented... */
4021 size_t len
= strlen(vp
->v_name
);
4022 if (!strncmp(vp
->v_name
, DYLD_SHARED_CACHE_NAME
, len
) ||
4023 !strncmp(vp
->v_name
, "dyld", len
) ||
4024 !strncmp(vp
->v_name
, "launchd", len
) ||
4025 !strncmp(vp
->v_name
, "Camera", len
) ||
4026 !strncmp(vp
->v_name
, "mediaserverd", len
) ||
4027 !strncmp(vp
->v_name
, "SpringBoard", len
) ||
4028 !strncmp(vp
->v_name
, "backboardd", len
)) {
4030 * This file matters when launching Camera:
4031 * do not store its contents in the secluded
4032 * pool that will be drained on Camera launch.
4034 memory_object_mark_eligible_for_secluded(moc
,
4039 #endif /* CONFIG_SECLUDED_MEMORY */
4041 fp_drop(p
, indx
, fp
, 1);
4048 context
= *vfs_context_current();
4049 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
4051 if ((fp
->f_fglob
->fg_flag
& FHASLOCK
) &&
4052 (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_VNODE
)) {
4053 lf
.l_whence
= SEEK_SET
;
4056 lf
.l_type
= F_UNLCK
;
4059 vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
4062 vn_close(vp
, fp
->f_fglob
->fg_flag
, &context
);
4064 fp_free(p
, indx
, fp
);
4070 * While most of the *at syscall handlers can call nameiat() which
4071 * is a wrapper around namei, the use of namei and initialisation
4072 * of nameidata are far removed and in different functions - namei
4073 * gets called in vn_open_auth for open1. So we'll just do here what
4077 open1at(vfs_context_t ctx
, struct nameidata
*ndp
, int uflags
,
4078 struct vnode_attr
*vap
, fp_allocfn_t fp_zalloc
, void *cra
, int32_t *retval
,
4081 if ((dirfd
!= AT_FDCWD
) && !(ndp
->ni_cnd
.cn_flags
& USEDVP
)) {
4085 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
4086 error
= copyin(ndp
->ni_dirp
, &c
, sizeof(char));
4091 c
= *((char *)(ndp
->ni_dirp
));
4097 error
= vnode_getfromfd(ndp
->ni_cnd
.cn_context
, dirfd
,
4103 if (vnode_vtype(dvp_at
) != VDIR
) {
4108 ndp
->ni_dvp
= dvp_at
;
4109 ndp
->ni_cnd
.cn_flags
|= USEDVP
;
4110 error
= open1(ctx
, ndp
, uflags
, vap
, fp_zalloc
, cra
,
4117 return open1(ctx
, ndp
, uflags
, vap
, fp_zalloc
, cra
, retval
);
4121 * open_extended: open a file given a path name; with extended argument list (including extended security (ACL)).
4123 * Parameters: p Process requesting the open
4124 * uap User argument descriptor (see below)
4125 * retval Pointer to an area to receive the
4126 * return calue from the system call
4128 * Indirect: uap->path Path to open (same as 'open')
4129 * uap->flags Flags to open (same as 'open'
4130 * uap->uid UID to set, if creating
4131 * uap->gid GID to set, if creating
4132 * uap->mode File mode, if creating (same as 'open')
4133 * uap->xsecurity ACL to set, if creating
4135 * Returns: 0 Success
4138 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
4140 * XXX: We should enummerate the possible errno values here, and where
4141 * in the code they originated.
4144 open_extended(proc_t p
, struct open_extended_args
*uap
, int32_t *retval
)
4146 struct filedesc
*fdp
= p
->p_fd
;
4148 kauth_filesec_t xsecdst
;
4149 struct vnode_attr va
;
4150 struct nameidata nd
;
4153 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
4156 if ((uap
->xsecurity
!= USER_ADDR_NULL
) &&
4157 ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)) {
4162 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
4163 VATTR_SET(&va
, va_mode
, cmode
);
4164 if (uap
->uid
!= KAUTH_UID_NONE
) {
4165 VATTR_SET(&va
, va_uid
, uap
->uid
);
4167 if (uap
->gid
!= KAUTH_GID_NONE
) {
4168 VATTR_SET(&va
, va_gid
, uap
->gid
);
4170 if (xsecdst
!= NULL
) {
4171 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
4174 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
4175 uap
->path
, vfs_context_current());
4177 ciferror
= open1(vfs_context_current(), &nd
, uap
->flags
, &va
,
4178 fileproc_alloc_init
, NULL
, retval
);
4179 if (xsecdst
!= NULL
) {
4180 kauth_filesec_free(xsecdst
);
4187 * Go through the data-protected atomically controlled open (2)
4189 * int open_dprotected_np(user_addr_t path, int flags, int class, int dpflags, int mode)
4192 open_dprotected_np(__unused proc_t p
, struct open_dprotected_np_args
*uap
, int32_t *retval
)
4194 int flags
= uap
->flags
;
4195 int class = uap
->class;
4196 int dpflags
= uap
->dpflags
;
4199 * Follow the same path as normal open(2)
4200 * Look up the item if it exists, and acquire the vnode.
4202 struct filedesc
*fdp
= p
->p_fd
;
4203 struct vnode_attr va
;
4204 struct nameidata nd
;
4209 /* Mask off all but regular access permissions */
4210 cmode
= ((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
4211 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
4213 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
4214 uap
->path
, vfs_context_current());
4217 * Initialize the extra fields in vnode_attr to pass down our
4219 * 1. target cprotect class.
4220 * 2. set a flag to mark it as requiring open-raw-encrypted semantics.
4222 if (flags
& O_CREAT
) {
4223 /* lower level kernel code validates that the class is valid before applying it. */
4224 if (class != PROTECTION_CLASS_DEFAULT
) {
4226 * PROTECTION_CLASS_DEFAULT implies that we make the class for this
4227 * file behave the same as open (2)
4229 VATTR_SET(&va
, va_dataprotect_class
, class);
4233 if (dpflags
& (O_DP_GETRAWENCRYPTED
| O_DP_GETRAWUNENCRYPTED
)) {
4234 if (flags
& (O_RDWR
| O_WRONLY
)) {
4235 /* Not allowed to write raw encrypted bytes */
4238 if (uap
->dpflags
& O_DP_GETRAWENCRYPTED
) {
4239 VATTR_SET(&va
, va_dataprotect_flags
, VA_DP_RAWENCRYPTED
);
4241 if (uap
->dpflags
& O_DP_GETRAWUNENCRYPTED
) {
4242 VATTR_SET(&va
, va_dataprotect_flags
, VA_DP_RAWUNENCRYPTED
);
4246 error
= open1(vfs_context_current(), &nd
, uap
->flags
, &va
,
4247 fileproc_alloc_init
, NULL
, retval
);
4253 openat_internal(vfs_context_t ctx
, user_addr_t path
, int flags
, int mode
,
4254 int fd
, enum uio_seg segflg
, int *retval
)
4256 struct filedesc
*fdp
= (vfs_context_proc(ctx
))->p_fd
;
4257 struct vnode_attr va
;
4258 struct nameidata nd
;
4262 /* Mask off all but regular access permissions */
4263 cmode
= ((mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
4264 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
4266 NDINIT(&nd
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
,
4269 return open1at(ctx
, &nd
, flags
, &va
, fileproc_alloc_init
, NULL
,
4274 open(proc_t p
, struct open_args
*uap
, int32_t *retval
)
4276 __pthread_testcancel(1);
4277 return open_nocancel(p
, (struct open_nocancel_args
*)uap
, retval
);
4281 open_nocancel(__unused proc_t p
, struct open_nocancel_args
*uap
,
4284 return openat_internal(vfs_context_current(), uap
->path
, uap
->flags
,
4285 uap
->mode
, AT_FDCWD
, UIO_USERSPACE
, retval
);
4289 openat_nocancel(__unused proc_t p
, struct openat_nocancel_args
*uap
,
4292 return openat_internal(vfs_context_current(), uap
->path
, uap
->flags
,
4293 uap
->mode
, uap
->fd
, UIO_USERSPACE
, retval
);
4297 openat(proc_t p
, struct openat_args
*uap
, int32_t *retval
)
4299 __pthread_testcancel(1);
4300 return openat_nocancel(p
, (struct openat_nocancel_args
*)uap
, retval
);
4304 * openbyid_np: open a file given a file system id and a file system object id
4305 * the hfs file system object id is an fsobj_id_t {uint32, uint32}
4306 * file systems that don't support object ids it is a node id (uint64_t).
4308 * Parameters: p Process requesting the open
4309 * uap User argument descriptor (see below)
4310 * retval Pointer to an area to receive the
4311 * return calue from the system call
4313 * Indirect: uap->path Path to open (same as 'open')
4315 * uap->fsid id of target file system
4316 * uap->objid id of target file system object
4317 * uap->flags Flags to open (same as 'open')
4319 * Returns: 0 Success
4323 * XXX: We should enummerate the possible errno values here, and where
4324 * in the code they originated.
4327 openbyid_np(__unused proc_t p
, struct openbyid_np_args
*uap
, int *retval
)
4333 int buflen
= MAXPATHLEN
;
4335 vfs_context_t ctx
= vfs_context_current();
4337 if ((error
= priv_check_cred(vfs_context_ucred(ctx
), PRIV_VFS_OPEN_BY_ID
, 0))) {
4341 if ((error
= copyin(uap
->fsid
, (caddr_t
)&fsid
, sizeof(fsid
)))) {
4345 /*uap->obj is an fsobj_id_t defined as struct {uint32_t, uint32_t} */
4346 if ((error
= copyin(uap
->objid
, (caddr_t
)&objid
, sizeof(uint64_t)))) {
4350 AUDIT_ARG(value32
, fsid
.val
[0]);
4351 AUDIT_ARG(value64
, objid
);
4353 /*resolve path from fsis, objid*/
4355 MALLOC(buf
, char *, buflen
+ 1, M_TEMP
, M_WAITOK
);
4360 error
= fsgetpath_internal( ctx
, fsid
.val
[0], objid
, buflen
,
4361 buf
, FSOPT_ISREALFSID
, &pathlen
);
4367 } while (error
== ENOSPC
&& (buflen
+= MAXPATHLEN
));
4375 error
= openat_internal(
4376 ctx
, (user_addr_t
)buf
, uap
->oflags
, 0, AT_FDCWD
, UIO_SYSSPACE
, retval
);
4385 * Create a special file.
4387 static int mkfifo1(vfs_context_t ctx
, user_addr_t upath
, struct vnode_attr
*vap
);
4390 mknod(proc_t p
, struct mknod_args
*uap
, __unused
int32_t *retval
)
4392 struct vnode_attr va
;
4393 vfs_context_t ctx
= vfs_context_current();
4395 struct nameidata nd
;
4399 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
4400 VATTR_SET(&va
, va_rdev
, uap
->dev
);
4402 /* If it's a mknod() of a FIFO, call mkfifo1() instead */
4403 if ((uap
->mode
& S_IFMT
) == S_IFIFO
) {
4404 return mkfifo1(ctx
, uap
->path
, &va
);
4407 AUDIT_ARG(mode
, uap
->mode
);
4408 AUDIT_ARG(value32
, uap
->dev
);
4410 if ((error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
))) {
4413 NDINIT(&nd
, CREATE
, OP_MKNOD
, LOCKPARENT
| AUDITVNPATH1
,
4414 UIO_USERSPACE
, uap
->path
, ctx
);
4427 switch (uap
->mode
& S_IFMT
) {
4429 VATTR_SET(&va
, va_type
, VCHR
);
4432 VATTR_SET(&va
, va_type
, VBLK
);
4440 error
= mac_vnode_check_create(ctx
,
4441 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
4447 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0) {
4451 if ((error
= vn_create(dvp
, &vp
, &nd
, &va
, 0, 0, NULL
, ctx
)) != 0) {
4456 int update_flags
= 0;
4458 // Make sure the name & parent pointers are hooked up
4459 if (vp
->v_name
== NULL
) {
4460 update_flags
|= VNODE_UPDATE_NAME
;
4462 if (vp
->v_parent
== NULLVP
) {
4463 update_flags
|= VNODE_UPDATE_PARENT
;
4467 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
4471 add_fsevent(FSE_CREATE_FILE
, ctx
,
4479 * nameidone has to happen before we vnode_put(dvp)
4480 * since it may need to release the fs_nodelock on the dvp
4493 * Create a named pipe.
4495 * Returns: 0 Success
4498 * vnode_authorize:???
4502 mkfifo1(vfs_context_t ctx
, user_addr_t upath
, struct vnode_attr
*vap
)
4506 struct nameidata nd
;
4508 NDINIT(&nd
, CREATE
, OP_MKFIFO
, LOCKPARENT
| AUDITVNPATH1
,
4509 UIO_USERSPACE
, upath
, ctx
);
4517 /* check that this is a new file and authorize addition */
4522 VATTR_SET(vap
, va_type
, VFIFO
);
4524 if ((error
= vn_authorize_create(dvp
, &nd
.ni_cnd
, vap
, ctx
, NULL
)) != 0) {
4528 error
= vn_create(dvp
, &vp
, &nd
, vap
, 0, 0, NULL
, ctx
);
4531 * nameidone has to happen before we vnode_put(dvp)
4532 * since it may need to release the fs_nodelock on the dvp
4546 * mkfifo_extended: Create a named pipe; with extended argument list (including extended security (ACL)).
4548 * Parameters: p Process requesting the open
4549 * uap User argument descriptor (see below)
4552 * Indirect: uap->path Path to fifo (same as 'mkfifo')
4553 * uap->uid UID to set
4554 * uap->gid GID to set
4555 * uap->mode File mode to set (same as 'mkfifo')
4556 * uap->xsecurity ACL to set, if creating
4558 * Returns: 0 Success
4561 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
4563 * XXX: We should enummerate the possible errno values here, and where
4564 * in the code they originated.
4567 mkfifo_extended(proc_t p
, struct mkfifo_extended_args
*uap
, __unused
int32_t *retval
)
4570 kauth_filesec_t xsecdst
;
4571 struct vnode_attr va
;
4573 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
4575 xsecdst
= KAUTH_FILESEC_NONE
;
4576 if (uap
->xsecurity
!= USER_ADDR_NULL
) {
4577 if ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0) {
4583 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
4584 if (uap
->uid
!= KAUTH_UID_NONE
) {
4585 VATTR_SET(&va
, va_uid
, uap
->uid
);
4587 if (uap
->gid
!= KAUTH_GID_NONE
) {
4588 VATTR_SET(&va
, va_gid
, uap
->gid
);
4590 if (xsecdst
!= KAUTH_FILESEC_NONE
) {
4591 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
4594 ciferror
= mkfifo1(vfs_context_current(), uap
->path
, &va
);
4596 if (xsecdst
!= KAUTH_FILESEC_NONE
) {
4597 kauth_filesec_free(xsecdst
);
4604 mkfifo(proc_t p
, struct mkfifo_args
*uap
, __unused
int32_t *retval
)
4606 struct vnode_attr va
;
4609 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
4611 return mkfifo1(vfs_context_current(), uap
->path
, &va
);
4616 my_strrchr(char *p
, int ch
)
4620 for (save
= NULL
;; ++p
) {
4631 extern int safe_getpath_new(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
, int firmlink
);
4632 extern int safe_getpath(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
);
4633 extern int safe_getpath_no_firmlink(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
);
4636 safe_getpath_new(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
, int firmlink
)
4638 int ret
, len
= _len
;
4640 *truncated_path
= 0;
4643 ret
= vn_getpath(dvp
, path
, &len
);
4645 ret
= vn_getpath_no_firmlink(dvp
, path
, &len
);
4647 if (ret
== 0 && len
< (MAXPATHLEN
- 1)) {
4649 path
[len
- 1] = '/';
4650 len
+= strlcpy(&path
[len
], leafname
, MAXPATHLEN
- len
) + 1;
4651 if (len
> MAXPATHLEN
) {
4654 // the string got truncated!
4655 *truncated_path
= 1;
4656 ptr
= my_strrchr(path
, '/');
4658 *ptr
= '\0'; // chop off the string at the last directory component
4660 len
= strlen(path
) + 1;
4663 } else if (ret
== 0) {
4664 *truncated_path
= 1;
4665 } else if (ret
!= 0) {
4666 struct vnode
*mydvp
= dvp
;
4668 if (ret
!= ENOSPC
) {
4669 printf("safe_getpath: failed to get the path for vp %p (%s) : err %d\n",
4670 dvp
, dvp
->v_name
? dvp
->v_name
: "no-name", ret
);
4672 *truncated_path
= 1;
4675 if (mydvp
->v_parent
!= NULL
) {
4676 mydvp
= mydvp
->v_parent
;
4677 } else if (mydvp
->v_mount
) {
4678 strlcpy(path
, mydvp
->v_mount
->mnt_vfsstat
.f_mntonname
, _len
);
4681 // no parent and no mount point? only thing is to punt and say "/" changed
4682 strlcpy(path
, "/", _len
);
4687 if (mydvp
== NULL
) {
4693 ret
= vn_getpath(mydvp
, path
, &len
);
4695 ret
= vn_getpath_no_firmlink(mydvp
, path
, &len
);
4697 } while (ret
== ENOSPC
);
4704 safe_getpath(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
)
4706 return safe_getpath_new(dvp
, leafname
, path
, _len
, truncated_path
, 1);
4710 safe_getpath_no_firmlink(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
)
4712 return safe_getpath_new(dvp
, leafname
, path
, _len
, truncated_path
, 0);
4716 * Make a hard file link.
4718 * Returns: 0 Success
4723 * vnode_authorize:???
4728 linkat_internal(vfs_context_t ctx
, int fd1
, user_addr_t path
, int fd2
,
4729 user_addr_t link
, int flag
, enum uio_seg segflg
)
4731 vnode_t vp
, pvp
, dvp
, lvp
;
4732 struct nameidata nd
;
4738 int need_event
, has_listeners
, need_kpath2
;
4739 char *target_path
= NULL
;
4742 vp
= dvp
= lvp
= NULLVP
;
4744 /* look up the object we are linking to */
4745 follow
= (flag
& AT_SYMLINK_FOLLOW
) ? FOLLOW
: NOFOLLOW
;
4746 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, AUDITVNPATH1
| follow
,
4749 error
= nameiat(&nd
, fd1
);
4758 * Normally, linking to directories is not supported.
4759 * However, some file systems may have limited support.
4761 if (vp
->v_type
== VDIR
) {
4762 if (!ISSET(vp
->v_mount
->mnt_kern_flag
, MNTK_DIR_HARDLINKS
)) {
4763 error
= EPERM
; /* POSIX */
4767 /* Linking to a directory requires ownership. */
4768 if (!kauth_cred_issuser(vfs_context_ucred(ctx
))) {
4769 struct vnode_attr dva
;
4772 VATTR_WANTED(&dva
, va_uid
);
4773 if (vnode_getattr(vp
, &dva
, ctx
) != 0 ||
4774 !VATTR_IS_SUPPORTED(&dva
, va_uid
) ||
4775 (dva
.va_uid
!= kauth_cred_getuid(vfs_context_ucred(ctx
)))) {
4782 /* lookup the target node */
4786 nd
.ni_cnd
.cn_nameiop
= CREATE
;
4787 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| AUDITVNPATH2
| CN_NBMOUNTLOOK
;
4789 error
= nameiat(&nd
, fd2
);
4797 if ((error
= mac_vnode_check_link(ctx
, dvp
, vp
, &nd
.ni_cnd
)) != 0) {
4802 /* or to anything that kauth doesn't want us to (eg. immutable items) */
4803 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_LINKTARGET
, ctx
)) != 0) {
4807 /* target node must not exist */
4808 if (lvp
!= NULLVP
) {
4812 /* cannot link across mountpoints */
4813 if (vnode_mount(vp
) != vnode_mount(dvp
)) {
4818 /* authorize creation of the target note */
4819 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0) {
4823 /* and finally make the link */
4824 error
= VNOP_LINK(vp
, dvp
, &nd
.ni_cnd
, ctx
);
4830 (void)mac_vnode_notify_link(ctx
, vp
, dvp
, &nd
.ni_cnd
);
4834 need_event
= need_fsevent(FSE_CREATE_FILE
, dvp
);
4838 has_listeners
= kauth_authorize_fileop_has_listeners();
4842 if (AUDIT_RECORD_EXISTS()) {
4847 if (need_event
|| has_listeners
|| need_kpath2
) {
4848 char *link_to_path
= NULL
;
4849 int len
, link_name_len
;
4851 /* build the path to the new link file */
4852 GET_PATH(target_path
);
4853 if (target_path
== NULL
) {
4858 len
= safe_getpath(dvp
, nd
.ni_cnd
.cn_nameptr
, target_path
, MAXPATHLEN
, &truncated
);
4860 AUDIT_ARG(kpath
, target_path
, ARG_KPATH2
);
4862 if (has_listeners
) {
4863 /* build the path to file we are linking to */
4864 GET_PATH(link_to_path
);
4865 if (link_to_path
== NULL
) {
4870 link_name_len
= MAXPATHLEN
;
4871 if (vn_getpath(vp
, link_to_path
, &link_name_len
) == 0) {
4873 * Call out to allow 3rd party notification of rename.
4874 * Ignore result of kauth_authorize_fileop call.
4876 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_LINK
,
4877 (uintptr_t)link_to_path
,
4878 (uintptr_t)target_path
);
4880 if (link_to_path
!= NULL
) {
4881 RELEASE_PATH(link_to_path
);
4886 /* construct fsevent */
4887 if (get_fse_info(vp
, &finfo
, ctx
) == 0) {
4889 finfo
.mode
|= FSE_TRUNCATED_PATH
;
4892 // build the path to the destination of the link
4893 add_fsevent(FSE_CREATE_FILE
, ctx
,
4894 FSE_ARG_STRING
, len
, target_path
,
4895 FSE_ARG_FINFO
, &finfo
,
4900 // need an iocount on pvp in this case
4901 if (pvp
&& pvp
!= dvp
) {
4902 error
= vnode_get(pvp
);
4909 add_fsevent(FSE_STAT_CHANGED
, ctx
,
4910 FSE_ARG_VNODE
, pvp
, FSE_ARG_DONE
);
4912 if (pvp
&& pvp
!= dvp
) {
4920 * nameidone has to happen before we vnode_put(dvp)
4921 * since it may need to release the fs_nodelock on the dvp
4924 if (target_path
!= NULL
) {
4925 RELEASE_PATH(target_path
);
4939 link(__unused proc_t p
, struct link_args
*uap
, __unused
int32_t *retval
)
4941 return linkat_internal(vfs_context_current(), AT_FDCWD
, uap
->path
,
4942 AT_FDCWD
, uap
->link
, AT_SYMLINK_FOLLOW
, UIO_USERSPACE
);
4946 linkat(__unused proc_t p
, struct linkat_args
*uap
, __unused
int32_t *retval
)
4948 if (uap
->flag
& ~AT_SYMLINK_FOLLOW
) {
4952 return linkat_internal(vfs_context_current(), uap
->fd1
, uap
->path
,
4953 uap
->fd2
, uap
->link
, uap
->flag
, UIO_USERSPACE
);
4957 * Make a symbolic link.
4959 * We could add support for ACLs here too...
4963 symlinkat_internal(vfs_context_t ctx
, user_addr_t path_data
, int fd
,
4964 user_addr_t link
, enum uio_seg segflg
)
4966 struct vnode_attr va
;
4969 struct nameidata nd
;
4975 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
4976 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
4977 error
= copyinstr(path_data
, path
, MAXPATHLEN
, &dummy
);
4979 path
= (char *)path_data
;
4984 AUDIT_ARG(text
, path
); /* This is the link string */
4986 NDINIT(&nd
, CREATE
, OP_SYMLINK
, LOCKPARENT
| AUDITVNPATH1
,
4989 error
= nameiat(&nd
, fd
);
4996 p
= vfs_context_proc(ctx
);
4998 VATTR_SET(&va
, va_type
, VLNK
);
4999 VATTR_SET(&va
, va_mode
, ACCESSPERMS
& ~p
->p_fd
->fd_cmask
);
5002 error
= mac_vnode_check_create(ctx
,
5003 dvp
, &nd
.ni_cnd
, &va
);
5016 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
);
5018 /* get default ownership, etc. */
5020 error
= vnode_authattr_new(dvp
, &va
, 0, ctx
);
5023 error
= VNOP_SYMLINK(dvp
, &vp
, &nd
.ni_cnd
, &va
, path
, ctx
);
5027 if (error
== 0 && vp
) {
5028 error
= vnode_label(vnode_mount(vp
), dvp
, vp
, &nd
.ni_cnd
, VNODE_LABEL_CREATE
, ctx
);
5032 /* do fallback attribute handling */
5033 if (error
== 0 && vp
) {
5034 error
= vnode_setattr_fallback(vp
, &va
, ctx
);
5038 int update_flags
= 0;
5040 /*check if a new vnode was created, else try to get one*/
5042 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
5044 nd
.ni_op
= OP_LOOKUP
;
5046 nd
.ni_cnd
.cn_flags
= 0;
5047 error
= nameiat(&nd
, fd
);
5055 #if 0 /* XXX - kauth_todo - is KAUTH_FILEOP_SYMLINK needed? */
5056 /* call out to allow 3rd party notification of rename.
5057 * Ignore result of kauth_authorize_fileop call.
5059 if (kauth_authorize_fileop_has_listeners() &&
5061 char *new_link_path
= NULL
;
5064 /* build the path to the new link file */
5065 new_link_path
= get_pathbuff();
5067 vn_getpath(dvp
, new_link_path
, &len
);
5068 if ((len
+ 1 + nd
.ni_cnd
.cn_namelen
+ 1) < MAXPATHLEN
) {
5069 new_link_path
[len
- 1] = '/';
5070 strlcpy(&new_link_path
[len
], nd
.ni_cnd
.cn_nameptr
, MAXPATHLEN
- len
);
5073 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_SYMLINK
,
5074 (uintptr_t)path
, (uintptr_t)new_link_path
);
5075 if (new_link_path
!= NULL
) {
5076 release_pathbuff(new_link_path
);
5080 // Make sure the name & parent pointers are hooked up
5081 if (vp
->v_name
== NULL
) {
5082 update_flags
|= VNODE_UPDATE_NAME
;
5084 if (vp
->v_parent
== NULLVP
) {
5085 update_flags
|= VNODE_UPDATE_PARENT
;
5089 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
5093 add_fsevent(FSE_CREATE_FILE
, ctx
,
5101 * nameidone has to happen before we vnode_put(dvp)
5102 * since it may need to release the fs_nodelock on the dvp
5111 if (path
&& (path
!= (char *)path_data
)) {
5112 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
5119 symlink(__unused proc_t p
, struct symlink_args
*uap
, __unused
int32_t *retval
)
5121 return symlinkat_internal(vfs_context_current(), uap
->path
, AT_FDCWD
,
5122 uap
->link
, UIO_USERSPACE
);
5126 symlinkat(__unused proc_t p
, struct symlinkat_args
*uap
,
5127 __unused
int32_t *retval
)
5129 return symlinkat_internal(vfs_context_current(), uap
->path1
, uap
->fd
,
5130 uap
->path2
, UIO_USERSPACE
);
5134 * Delete a whiteout from the filesystem.
5135 * No longer supported.
5138 undelete(__unused proc_t p
, __unused
struct undelete_args
*uap
, __unused
int32_t *retval
)
5144 * Delete a name from the filesystem.
5148 unlinkat_internal(vfs_context_t ctx
, int fd
, vnode_t start_dvp
,
5149 user_addr_t path_arg
, enum uio_seg segflg
, int unlink_flags
)
5151 struct nameidata nd
;
5154 struct componentname
*cnp
;
5156 char *no_firmlink_path
= NULL
;
5158 int len_no_firmlink_path
= 0;
5161 struct vnode_attr va
;
5167 int truncated_no_firmlink_path
;
5169 struct vnode_attr
*vap
;
5171 int retry_count
= 0;
5174 cn_flags
= LOCKPARENT
;
5175 if (!(unlink_flags
& VNODE_REMOVE_NO_AUDIT_PATH
)) {
5176 cn_flags
|= AUDITVNPATH1
;
5178 /* If a starting dvp is passed, it trumps any fd passed. */
5184 /* unlink or delete is allowed on rsrc forks and named streams */
5185 cn_flags
|= CN_ALLOWRSRCFORK
;
5194 truncated_no_firmlink_path
= 0;
5197 NDINIT(&nd
, DELETE
, OP_UNLINK
, cn_flags
, segflg
, path_arg
, ctx
);
5199 nd
.ni_dvp
= start_dvp
;
5200 nd
.ni_flag
|= NAMEI_COMPOUNDREMOVE
;
5204 error
= nameiat(&nd
, fd
);
5213 /* With Carbon delete semantics, busy files cannot be deleted */
5214 if (unlink_flags
& VNODE_REMOVE_NODELETEBUSY
) {
5215 flags
|= VNODE_REMOVE_NODELETEBUSY
;
5218 /* Skip any potential upcalls if told to. */
5219 if (unlink_flags
& VNODE_REMOVE_SKIP_NAMESPACE_EVENT
) {
5220 flags
|= VNODE_REMOVE_SKIP_NAMESPACE_EVENT
;
5224 batched
= vnode_compound_remove_available(vp
);
5226 * The root of a mounted filesystem cannot be deleted.
5228 if ((vp
->v_flag
& VROOT
) || (dvp
->v_mount
!= vp
->v_mount
)) {
5233 #if DEVELOPMENT || DEBUG
5235 * XXX VSWAP: Check for entitlements or special flag here
5236 * so we can restrict access appropriately.
5238 #else /* DEVELOPMENT || DEBUG */
5240 if (vnode_isswap(vp
) && (ctx
!= vfs_context_kernel())) {
5244 #endif /* DEVELOPMENT || DEBUG */
5247 error
= vn_authorize_unlink(dvp
, vp
, cnp
, ctx
, NULL
);
5249 if (error
== ENOENT
) {
5250 if (retry_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
5261 if (!vnode_compound_remove_available(dvp
)) {
5262 panic("No vp, but no compound remove?");
5267 need_event
= need_fsevent(FSE_DELETE
, dvp
);
5270 if ((vp
->v_flag
& VISHARDLINK
) == 0) {
5271 /* XXX need to get these data in batched VNOP */
5272 get_fse_info(vp
, &finfo
, ctx
);
5275 error
= vfs_get_notify_attributes(&va
);
5284 has_listeners
= kauth_authorize_fileop_has_listeners();
5285 if (need_event
|| has_listeners
) {
5293 len_path
= safe_getpath(dvp
, nd
.ni_cnd
.cn_nameptr
, path
, MAXPATHLEN
, &truncated_path
);
5294 if (no_firmlink_path
== NULL
) {
5295 GET_PATH(no_firmlink_path
);
5296 if (no_firmlink_path
== NULL
) {
5301 len_no_firmlink_path
= safe_getpath_no_firmlink(dvp
, nd
.ni_cnd
.cn_nameptr
, no_firmlink_path
, MAXPATHLEN
, &truncated_no_firmlink_path
);
5305 if (nd
.ni_cnd
.cn_flags
& CN_WANTSRSRCFORK
) {
5306 error
= vnode_removenamedstream(dvp
, vp
, XATTR_RESOURCEFORK_NAME
, 0, ctx
);
5310 error
= vn_remove(dvp
, &nd
.ni_vp
, &nd
, flags
, vap
, ctx
);
5312 if (error
== EKEEPLOOKING
) {
5314 panic("EKEEPLOOKING, but not a filesystem that supports compound VNOPs?");
5317 if ((nd
.ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
5318 panic("EKEEPLOOKING, but continue flag not set?");
5321 if (vnode_isdir(vp
)) {
5325 goto continue_lookup
;
5326 } else if (error
== ENOENT
&& batched
) {
5327 if (retry_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
5329 * For compound VNOPs, the authorization callback may
5330 * return ENOENT in case of racing hardlink lookups
5331 * hitting the name cache, redrive the lookup.
5341 * Call out to allow 3rd party notification of delete.
5342 * Ignore result of kauth_authorize_fileop call.
5345 if (has_listeners
) {
5346 kauth_authorize_fileop(vfs_context_ucred(ctx
),
5347 KAUTH_FILEOP_DELETE
,
5352 if (vp
->v_flag
& VISHARDLINK
) {
5354 // if a hardlink gets deleted we want to blow away the
5355 // v_parent link because the path that got us to this
5356 // instance of the link is no longer valid. this will
5357 // force the next call to get the path to ask the file
5358 // system instead of just following the v_parent link.
5360 vnode_update_identity(vp
, NULL
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
5365 if (vp
->v_flag
& VISHARDLINK
) {
5366 get_fse_info(vp
, &finfo
, ctx
);
5368 vnode_get_fse_info_from_vap(vp
, &finfo
, vap
);
5370 if (truncated_path
) {
5371 finfo
.mode
|= FSE_TRUNCATED_PATH
;
5373 add_fsevent(FSE_DELETE
, ctx
,
5374 FSE_ARG_STRING
, len_no_firmlink_path
, no_firmlink_path
,
5375 FSE_ARG_FINFO
, &finfo
,
5387 if (no_firmlink_path
!= NULL
) {
5388 RELEASE_PATH(no_firmlink_path
);
5389 no_firmlink_path
= NULL
;
5392 /* recycle the deleted rsrc fork vnode to force a reclaim, which
5393 * will cause its shadow file to go away if necessary.
5395 if (vp
&& (vnode_isnamedstream(vp
)) &&
5396 (vp
->v_parent
!= NULLVP
) &&
5397 vnode_isshadow(vp
)) {
5402 * nameidone has to happen before we vnode_put(dvp)
5403 * since it may need to release the fs_nodelock on the dvp
5419 unlink1(vfs_context_t ctx
, vnode_t start_dvp
, user_addr_t path_arg
,
5420 enum uio_seg segflg
, int unlink_flags
)
5422 return unlinkat_internal(ctx
, AT_FDCWD
, start_dvp
, path_arg
, segflg
,
5427 * Delete a name from the filesystem using Carbon semantics.
5430 delete(__unused proc_t p
, struct delete_args
*uap
, __unused
int32_t *retval
)
5432 return unlinkat_internal(vfs_context_current(), AT_FDCWD
, NULLVP
,
5433 uap
->path
, UIO_USERSPACE
, VNODE_REMOVE_NODELETEBUSY
);
5437 * Delete a name from the filesystem using POSIX semantics.
5440 unlink(__unused proc_t p
, struct unlink_args
*uap
, __unused
int32_t *retval
)
5442 return unlinkat_internal(vfs_context_current(), AT_FDCWD
, NULLVP
,
5443 uap
->path
, UIO_USERSPACE
, 0);
5447 unlinkat(__unused proc_t p
, struct unlinkat_args
*uap
, __unused
int32_t *retval
)
5449 if (uap
->flag
& ~(AT_REMOVEDIR
| AT_REMOVEDIR_DATALESS
)) {
5453 if (uap
->flag
& (AT_REMOVEDIR
| AT_REMOVEDIR_DATALESS
)) {
5454 int unlink_flags
= 0;
5456 if (uap
->flag
& AT_REMOVEDIR_DATALESS
) {
5457 unlink_flags
|= VNODE_REMOVE_DATALESS_DIR
;
5459 return rmdirat_internal(vfs_context_current(), uap
->fd
,
5460 uap
->path
, UIO_USERSPACE
, unlink_flags
);
5462 return unlinkat_internal(vfs_context_current(), uap
->fd
,
5463 NULLVP
, uap
->path
, UIO_USERSPACE
, 0);
5468 * Reposition read/write file offset.
5471 lseek(proc_t p
, struct lseek_args
*uap
, off_t
*retval
)
5473 struct fileproc
*fp
;
5475 struct vfs_context
*ctx
;
5476 off_t offset
= uap
->offset
, file_size
;
5479 if ((error
= fp_getfvp(p
, uap
->fd
, &fp
, &vp
))) {
5480 if (error
== ENOTSUP
) {
5485 if (vnode_isfifo(vp
)) {
5491 ctx
= vfs_context_current();
5493 if (uap
->whence
== L_INCR
&& uap
->offset
== 0) {
5494 error
= mac_file_check_get_offset(vfs_context_ucred(ctx
),
5497 error
= mac_file_check_change_offset(vfs_context_ucred(ctx
),
5505 if ((error
= vnode_getwithref(vp
))) {
5510 switch (uap
->whence
) {
5512 offset
+= fp
->f_fglob
->fg_offset
;
5515 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0) {
5518 offset
+= file_size
;
5523 error
= VNOP_IOCTL(vp
, FSIOC_FIOSEEKHOLE
, (caddr_t
)&offset
, 0, ctx
);
5526 error
= VNOP_IOCTL(vp
, FSIOC_FIOSEEKDATA
, (caddr_t
)&offset
, 0, ctx
);
5532 if (uap
->offset
> 0 && offset
< 0) {
5533 /* Incremented/relative move past max size */
5537 * Allow negative offsets on character devices, per
5538 * POSIX 1003.1-2001. Most likely for writing disk
5541 if (offset
< 0 && vp
->v_type
!= VCHR
) {
5542 /* Decremented/relative move before start */
5546 fp
->f_fglob
->fg_offset
= offset
;
5547 *retval
= fp
->f_fglob
->fg_offset
;
5553 * An lseek can affect whether data is "available to read." Use
5554 * hint of NOTE_NONE so no EVFILT_VNODE events fire
5556 post_event_if_success(vp
, error
, NOTE_NONE
);
5557 (void)vnode_put(vp
);
5564 * Check access permissions.
5566 * Returns: 0 Success
5567 * vnode_authorize:???
5570 access1(vnode_t vp
, vnode_t dvp
, int uflags
, vfs_context_t ctx
)
5572 kauth_action_t action
;
5576 * If just the regular access bits, convert them to something
5577 * that vnode_authorize will understand.
5579 if (!(uflags
& _ACCESS_EXTENDED_MASK
)) {
5581 if (uflags
& R_OK
) {
5582 action
|= KAUTH_VNODE_READ_DATA
; /* aka KAUTH_VNODE_LIST_DIRECTORY */
5584 if (uflags
& W_OK
) {
5585 if (vnode_isdir(vp
)) {
5586 action
|= KAUTH_VNODE_ADD_FILE
|
5587 KAUTH_VNODE_ADD_SUBDIRECTORY
;
5588 /* might want delete rights here too */
5590 action
|= KAUTH_VNODE_WRITE_DATA
;
5593 if (uflags
& X_OK
) {
5594 if (vnode_isdir(vp
)) {
5595 action
|= KAUTH_VNODE_SEARCH
;
5597 action
|= KAUTH_VNODE_EXECUTE
;
5601 /* take advantage of definition of uflags */
5602 action
= uflags
>> 8;
5606 error
= mac_vnode_check_access(ctx
, vp
, uflags
);
5612 /* action == 0 means only check for existence */
5614 error
= vnode_authorize(vp
, dvp
, action
| KAUTH_VNODE_ACCESS
, ctx
);
5625 * access_extended: Check access permissions in bulk.
5627 * Description: uap->entries Pointer to an array of accessx
5628 * descriptor structs, plus one or
5629 * more NULL terminated strings (see
5630 * "Notes" section below).
5631 * uap->size Size of the area pointed to by
5633 * uap->results Pointer to the results array.
5635 * Returns: 0 Success
5636 * ENOMEM Insufficient memory
5637 * EINVAL Invalid arguments
5638 * namei:EFAULT Bad address
5639 * namei:ENAMETOOLONG Filename too long
5640 * namei:ENOENT No such file or directory
5641 * namei:ELOOP Too many levels of symbolic links
5642 * namei:EBADF Bad file descriptor
5643 * namei:ENOTDIR Not a directory
5648 * uap->results Array contents modified
5650 * Notes: The uap->entries are structured as an arbitrary length array
5651 * of accessx descriptors, followed by one or more NULL terminated
5654 * struct accessx_descriptor[0]
5656 * struct accessx_descriptor[n]
5657 * char name_data[0];
5659 * We determine the entry count by walking the buffer containing
5660 * the uap->entries argument descriptor. For each descriptor we
5661 * see, the valid values for the offset ad_name_offset will be
5662 * in the byte range:
5664 * [ uap->entries + sizeof(struct accessx_descriptor) ]
5666 * [ uap->entries + uap->size - 2 ]
5668 * since we must have at least one string, and the string must
5669 * be at least one character plus the NULL terminator in length.
5671 * XXX: Need to support the check-as uid argument
5674 access_extended(__unused proc_t p
, struct access_extended_args
*uap
, __unused
int32_t *retval
)
5676 struct accessx_descriptor
*input
= NULL
;
5677 errno_t
*result
= NULL
;
5680 unsigned int desc_max
, desc_actual
, i
, j
;
5681 struct vfs_context context
;
5682 struct nameidata nd
;
5686 #define ACCESSX_MAX_DESCR_ON_STACK 10
5687 struct accessx_descriptor stack_input
[ACCESSX_MAX_DESCR_ON_STACK
];
5689 context
.vc_ucred
= NULL
;
5692 * Validate parameters; if valid, copy the descriptor array and string
5693 * arguments into local memory. Before proceeding, the following
5694 * conditions must have been met:
5696 * o The total size is not permitted to exceed ACCESSX_MAX_TABLESIZE
5697 * o There must be sufficient room in the request for at least one
5698 * descriptor and a one yte NUL terminated string.
5699 * o The allocation of local storage must not fail.
5701 if (uap
->size
> ACCESSX_MAX_TABLESIZE
) {
5704 if (uap
->size
< (sizeof(struct accessx_descriptor
) + 2)) {
5707 if (uap
->size
<= sizeof(stack_input
)) {
5708 input
= stack_input
;
5710 MALLOC(input
, struct accessx_descriptor
*, uap
->size
, M_TEMP
, M_WAITOK
);
5711 if (input
== NULL
) {
5716 error
= copyin(uap
->entries
, input
, uap
->size
);
5721 AUDIT_ARG(opaque
, input
, uap
->size
);
5724 * Force NUL termination of the copyin buffer to avoid nami() running
5725 * off the end. If the caller passes us bogus data, they may get a
5728 ((char *)input
)[uap
->size
- 1] = 0;
5731 * Access is defined as checking against the process' real identity,
5732 * even if operations are checking the effective identity. This
5733 * requires that we use a local vfs context.
5735 context
.vc_ucred
= kauth_cred_copy_real(kauth_cred_get());
5736 context
.vc_thread
= current_thread();
5739 * Find out how many entries we have, so we can allocate the result
5740 * array by walking the list and adjusting the count downward by the
5741 * earliest string offset we see.
5743 desc_max
= (uap
->size
- 2) / sizeof(struct accessx_descriptor
);
5744 desc_actual
= desc_max
;
5745 for (i
= 0; i
< desc_actual
; i
++) {
5747 * Take the offset to the name string for this entry and
5748 * convert to an input array index, which would be one off
5749 * the end of the array if this entry was the lowest-addressed
5752 j
= input
[i
].ad_name_offset
/ sizeof(struct accessx_descriptor
);
5755 * An offset greater than the max allowable offset is an error.
5756 * It is also an error for any valid entry to point
5757 * to a location prior to the end of the current entry, if
5758 * it's not a reference to the string of the previous entry.
5760 if (j
> desc_max
|| (j
!= 0 && j
<= i
)) {
5765 /* Also do not let ad_name_offset point to something beyond the size of the input */
5766 if (input
[i
].ad_name_offset
>= uap
->size
) {
5772 * An offset of 0 means use the previous descriptor's offset;
5773 * this is used to chain multiple requests for the same file
5774 * to avoid multiple lookups.
5777 /* This is not valid for the first entry */
5786 * If the offset of the string for this descriptor is before
5787 * what we believe is the current actual last descriptor,
5788 * then we need to adjust our estimate downward; this permits
5789 * the string table following the last descriptor to be out
5790 * of order relative to the descriptor list.
5792 if (j
< desc_actual
) {
5798 * We limit the actual number of descriptors we are willing to process
5799 * to a hard maximum of ACCESSX_MAX_DESCRIPTORS. If the number being
5800 * requested does not exceed this limit,
5802 if (desc_actual
> ACCESSX_MAX_DESCRIPTORS
) {
5806 MALLOC(result
, errno_t
*, desc_actual
* sizeof(errno_t
), M_TEMP
, M_WAITOK
| M_ZERO
);
5807 if (result
== NULL
) {
5813 * Do the work by iterating over the descriptor entries we know to
5814 * at least appear to contain valid data.
5817 for (i
= 0; i
< desc_actual
; i
++) {
5819 * If the ad_name_offset is 0, then we use the previous
5820 * results to make the check; otherwise, we are looking up
5823 if (input
[i
].ad_name_offset
!= 0) {
5824 /* discard old vnodes */
5835 * Scan forward in the descriptor list to see if we
5836 * need the parent vnode. We will need it if we are
5837 * deleting, since we must have rights to remove
5838 * entries in the parent directory, as well as the
5839 * rights to delete the object itself.
5841 wantdelete
= input
[i
].ad_flags
& _DELETE_OK
;
5842 for (j
= i
+ 1; (j
< desc_actual
) && (input
[j
].ad_name_offset
== 0); j
++) {
5843 if (input
[j
].ad_flags
& _DELETE_OK
) {
5848 niopts
= FOLLOW
| AUDITVNPATH1
;
5850 /* need parent for vnode_authorize for deletion test */
5852 niopts
|= WANTPARENT
;
5856 NDINIT(&nd
, LOOKUP
, OP_ACCESS
, niopts
, UIO_SYSSPACE
,
5857 CAST_USER_ADDR_T(((const char *)input
) + input
[i
].ad_name_offset
),
5870 * Handle lookup errors.
5880 /* run this access check */
5881 result
[i
] = access1(vp
, dvp
, input
[i
].ad_flags
, &context
);
5884 /* fatal lookup error */
5890 AUDIT_ARG(data
, result
, sizeof(errno_t
), desc_actual
);
5892 /* copy out results */
5893 error
= copyout(result
, uap
->results
, desc_actual
* sizeof(errno_t
));
5896 if (input
&& input
!= stack_input
) {
5897 FREE(input
, M_TEMP
);
5900 FREE(result
, M_TEMP
);
5908 if (IS_VALID_CRED(context
.vc_ucred
)) {
5909 kauth_cred_unref(&context
.vc_ucred
);
5916 * Returns: 0 Success
5917 * namei:EFAULT Bad address
5918 * namei:ENAMETOOLONG Filename too long
5919 * namei:ENOENT No such file or directory
5920 * namei:ELOOP Too many levels of symbolic links
5921 * namei:EBADF Bad file descriptor
5922 * namei:ENOTDIR Not a directory
5927 faccessat_internal(vfs_context_t ctx
, int fd
, user_addr_t path
, int amode
,
5928 int flag
, enum uio_seg segflg
)
5931 struct nameidata nd
;
5933 struct vfs_context context
;
5935 int is_namedstream
= 0;
5939 * Unless the AT_EACCESS option is used, Access is defined as checking
5940 * against the process' real identity, even if operations are checking
5941 * the effective identity. So we need to tweak the credential
5942 * in the context for that case.
5944 if (!(flag
& AT_EACCESS
)) {
5945 context
.vc_ucred
= kauth_cred_copy_real(kauth_cred_get());
5947 context
.vc_ucred
= ctx
->vc_ucred
;
5949 context
.vc_thread
= ctx
->vc_thread
;
5952 niopts
= (flag
& AT_SYMLINK_NOFOLLOW
? NOFOLLOW
: FOLLOW
) | AUDITVNPATH1
;
5953 /* need parent for vnode_authorize for deletion test */
5954 if (amode
& _DELETE_OK
) {
5955 niopts
|= WANTPARENT
;
5957 NDINIT(&nd
, LOOKUP
, OP_ACCESS
, niopts
, segflg
,
5961 /* access(F_OK) calls are allowed for resource forks. */
5962 if (amode
== F_OK
) {
5963 nd
.ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
5966 error
= nameiat(&nd
, fd
);
5972 /* Grab reference on the shadow stream file vnode to
5973 * force an inactive on release which will mark it
5976 if (vnode_isnamedstream(nd
.ni_vp
) &&
5977 (nd
.ni_vp
->v_parent
!= NULLVP
) &&
5978 vnode_isshadow(nd
.ni_vp
)) {
5980 vnode_ref(nd
.ni_vp
);
5984 error
= access1(nd
.ni_vp
, nd
.ni_dvp
, amode
, &context
);
5987 if (is_namedstream
) {
5988 vnode_rele(nd
.ni_vp
);
5992 vnode_put(nd
.ni_vp
);
5993 if (amode
& _DELETE_OK
) {
5994 vnode_put(nd
.ni_dvp
);
5999 if (!(flag
& AT_EACCESS
)) {
6000 kauth_cred_unref(&context
.vc_ucred
);
6006 access(__unused proc_t p
, struct access_args
*uap
, __unused
int32_t *retval
)
6008 return faccessat_internal(vfs_context_current(), AT_FDCWD
,
6009 uap
->path
, uap
->flags
, 0, UIO_USERSPACE
);
6013 faccessat(__unused proc_t p
, struct faccessat_args
*uap
,
6014 __unused
int32_t *retval
)
6016 if (uap
->flag
& ~(AT_EACCESS
| AT_SYMLINK_NOFOLLOW
)) {
6020 return faccessat_internal(vfs_context_current(), uap
->fd
,
6021 uap
->path
, uap
->amode
, uap
->flag
, UIO_USERSPACE
);
6025 * Returns: 0 Success
6032 fstatat_internal(vfs_context_t ctx
, user_addr_t path
, user_addr_t ub
,
6033 user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
,
6034 enum uio_seg segflg
, int fd
, int flag
)
6036 struct nameidata nd
;
6043 struct user64_stat user64_sb
;
6044 struct user32_stat user32_sb
;
6045 struct user64_stat64 user64_sb64
;
6046 struct user32_stat64 user32_sb64
;
6050 kauth_filesec_t fsec
;
6051 size_t xsecurity_bufsize
;
6053 struct fileproc
*fp
= NULL
;
6054 int needsrealdev
= 0;
6056 follow
= (flag
& AT_SYMLINK_NOFOLLOW
) ? NOFOLLOW
: FOLLOW
;
6057 NDINIT(&nd
, LOOKUP
, OP_GETATTR
, follow
| AUDITVNPATH1
,
6061 int is_namedstream
= 0;
6062 /* stat calls are allowed for resource forks. */
6063 nd
.ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
6066 if (flag
& AT_FDONLY
) {
6069 error
= fp_getfvp(vfs_context_proc(ctx
), fd
, &fp
, &fvp
);
6073 if ((error
= vnode_getwithref(fvp
))) {
6079 error
= nameiat(&nd
, fd
);
6084 fsec
= KAUTH_FILESEC_NONE
;
6086 statptr
= (void *)&source
;
6089 /* Grab reference on the shadow stream file vnode to
6090 * force an inactive on release which will mark it
6093 if (vnode_isnamedstream(nd
.ni_vp
) &&
6094 (nd
.ni_vp
->v_parent
!= NULLVP
) &&
6095 vnode_isshadow(nd
.ni_vp
)) {
6097 vnode_ref(nd
.ni_vp
);
6101 needsrealdev
= flag
& AT_REALDEV
? 1 : 0;
6102 if (fp
&& (xsecurity
== USER_ADDR_NULL
)) {
6104 * If the caller has the file open, and is not
6105 * requesting extended security information, we are
6106 * going to let them get the basic stat information.
6108 error
= vn_stat_noauth(nd
.ni_vp
, statptr
, NULL
, isstat64
, needsrealdev
, ctx
,
6109 fp
->f_fglob
->fg_cred
);
6111 error
= vn_stat(nd
.ni_vp
, statptr
, (xsecurity
!= USER_ADDR_NULL
? &fsec
: NULL
),
6112 isstat64
, needsrealdev
, ctx
);
6116 if (is_namedstream
) {
6117 vnode_rele(nd
.ni_vp
);
6120 vnode_put(nd
.ni_vp
);
6130 /* Zap spare fields */
6131 if (isstat64
!= 0) {
6132 source
.sb64
.st_lspare
= 0;
6133 source
.sb64
.st_qspare
[0] = 0LL;
6134 source
.sb64
.st_qspare
[1] = 0LL;
6135 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
6136 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
6137 my_size
= sizeof(dest
.user64_sb64
);
6138 sbp
= (caddr_t
)&dest
.user64_sb64
;
6140 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
6141 my_size
= sizeof(dest
.user32_sb64
);
6142 sbp
= (caddr_t
)&dest
.user32_sb64
;
6145 * Check if we raced (post lookup) against the last unlink of a file.
6147 if ((source
.sb64
.st_nlink
== 0) && S_ISREG(source
.sb64
.st_mode
)) {
6148 source
.sb64
.st_nlink
= 1;
6151 source
.sb
.st_lspare
= 0;
6152 source
.sb
.st_qspare
[0] = 0LL;
6153 source
.sb
.st_qspare
[1] = 0LL;
6154 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
6155 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
6156 my_size
= sizeof(dest
.user64_sb
);
6157 sbp
= (caddr_t
)&dest
.user64_sb
;
6159 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
6160 my_size
= sizeof(dest
.user32_sb
);
6161 sbp
= (caddr_t
)&dest
.user32_sb
;
6165 * Check if we raced (post lookup) against the last unlink of a file.
6167 if ((source
.sb
.st_nlink
== 0) && S_ISREG(source
.sb
.st_mode
)) {
6168 source
.sb
.st_nlink
= 1;
6171 if ((error
= copyout(sbp
, ub
, my_size
)) != 0) {
6175 /* caller wants extended security information? */
6176 if (xsecurity
!= USER_ADDR_NULL
) {
6177 /* did we get any? */
6178 if (fsec
== KAUTH_FILESEC_NONE
) {
6179 if (susize(xsecurity_size
, 0) != 0) {
6184 /* find the user buffer size */
6185 xsecurity_bufsize
= fusize(xsecurity_size
);
6187 /* copy out the actual data size */
6188 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
6193 /* if the caller supplied enough room, copy out to it */
6194 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
)) {
6195 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
6200 if (fsec
!= KAUTH_FILESEC_NONE
) {
6201 kauth_filesec_free(fsec
);
6207 * stat_extended: Get file status; with extended security (ACL).
6209 * Parameters: p (ignored)
6210 * uap User argument descriptor (see below)
6213 * Indirect: uap->path Path of file to get status from
6214 * uap->ub User buffer (holds file status info)
6215 * uap->xsecurity ACL to get (extended security)
6216 * uap->xsecurity_size Size of ACL
6218 * Returns: 0 Success
6223 stat_extended(__unused proc_t p
, struct stat_extended_args
*uap
,
6224 __unused
int32_t *retval
)
6226 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6227 uap
->xsecurity
, uap
->xsecurity_size
, 0, UIO_USERSPACE
, AT_FDCWD
,
6232 * Returns: 0 Success
6233 * fstatat_internal:??? [see fstatat_internal() in this file]
6236 stat(__unused proc_t p
, struct stat_args
*uap
, __unused
int32_t *retval
)
6238 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6239 0, 0, 0, UIO_USERSPACE
, AT_FDCWD
, 0);
6243 stat64(__unused proc_t p
, struct stat64_args
*uap
, __unused
int32_t *retval
)
6245 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6246 0, 0, 1, UIO_USERSPACE
, AT_FDCWD
, 0);
6250 * stat64_extended: Get file status; can handle large inode numbers; with extended security (ACL).
6252 * Parameters: p (ignored)
6253 * uap User argument descriptor (see below)
6256 * Indirect: uap->path Path of file to get status from
6257 * uap->ub User buffer (holds file status info)
6258 * uap->xsecurity ACL to get (extended security)
6259 * uap->xsecurity_size Size of ACL
6261 * Returns: 0 Success
6266 stat64_extended(__unused proc_t p
, struct stat64_extended_args
*uap
, __unused
int32_t *retval
)
6268 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6269 uap
->xsecurity
, uap
->xsecurity_size
, 1, UIO_USERSPACE
, AT_FDCWD
,
6274 * lstat_extended: Get file status; does not follow links; with extended security (ACL).
6276 * Parameters: p (ignored)
6277 * uap User argument descriptor (see below)
6280 * Indirect: uap->path Path of file to get status from
6281 * uap->ub User buffer (holds file status info)
6282 * uap->xsecurity ACL to get (extended security)
6283 * uap->xsecurity_size Size of ACL
6285 * Returns: 0 Success
6290 lstat_extended(__unused proc_t p
, struct lstat_extended_args
*uap
, __unused
int32_t *retval
)
6292 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6293 uap
->xsecurity
, uap
->xsecurity_size
, 0, UIO_USERSPACE
, AT_FDCWD
,
6294 AT_SYMLINK_NOFOLLOW
);
6298 * Get file status; this version does not follow links.
6301 lstat(__unused proc_t p
, struct lstat_args
*uap
, __unused
int32_t *retval
)
6303 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6304 0, 0, 0, UIO_USERSPACE
, AT_FDCWD
, AT_SYMLINK_NOFOLLOW
);
6308 lstat64(__unused proc_t p
, struct lstat64_args
*uap
, __unused
int32_t *retval
)
6310 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6311 0, 0, 1, UIO_USERSPACE
, AT_FDCWD
, AT_SYMLINK_NOFOLLOW
);
6315 * lstat64_extended: Get file status; can handle large inode numbers; does not
6316 * follow links; with extended security (ACL).
6318 * Parameters: p (ignored)
6319 * uap User argument descriptor (see below)
6322 * Indirect: uap->path Path of file to get status from
6323 * uap->ub User buffer (holds file status info)
6324 * uap->xsecurity ACL to get (extended security)
6325 * uap->xsecurity_size Size of ACL
6327 * Returns: 0 Success
6332 lstat64_extended(__unused proc_t p
, struct lstat64_extended_args
*uap
, __unused
int32_t *retval
)
6334 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6335 uap
->xsecurity
, uap
->xsecurity_size
, 1, UIO_USERSPACE
, AT_FDCWD
,
6336 AT_SYMLINK_NOFOLLOW
);
6340 fstatat(__unused proc_t p
, struct fstatat_args
*uap
, __unused
int32_t *retval
)
6342 if (uap
->flag
& ~(AT_SYMLINK_NOFOLLOW
| AT_REALDEV
| AT_FDONLY
)) {
6346 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6347 0, 0, 0, UIO_USERSPACE
, uap
->fd
, uap
->flag
);
6351 fstatat64(__unused proc_t p
, struct fstatat64_args
*uap
,
6352 __unused
int32_t *retval
)
6354 if (uap
->flag
& ~(AT_SYMLINK_NOFOLLOW
| AT_REALDEV
| AT_FDONLY
)) {
6358 return fstatat_internal(vfs_context_current(), uap
->path
, uap
->ub
,
6359 0, 0, 1, UIO_USERSPACE
, uap
->fd
, uap
->flag
);
6363 * Get configurable pathname variables.
6365 * Returns: 0 Success
6369 * Notes: Global implementation constants are intended to be
6370 * implemented in this function directly; all other constants
6371 * are per-FS implementation, and therefore must be handled in
6372 * each respective FS, instead.
6374 * XXX We implement some things globally right now that should actually be
6375 * XXX per-FS; we will need to deal with this at some point.
6379 pathconf(__unused proc_t p
, struct pathconf_args
*uap
, int32_t *retval
)
6382 struct nameidata nd
;
6383 vfs_context_t ctx
= vfs_context_current();
6385 NDINIT(&nd
, LOOKUP
, OP_PATHCONF
, FOLLOW
| AUDITVNPATH1
,
6386 UIO_USERSPACE
, uap
->path
, ctx
);
6392 error
= vn_pathconf(nd
.ni_vp
, uap
->name
, retval
, ctx
);
6394 vnode_put(nd
.ni_vp
);
6400 * Return target name of a symbolic link.
6404 readlinkat_internal(vfs_context_t ctx
, int fd
, user_addr_t path
,
6405 enum uio_seg seg
, user_addr_t buf
, size_t bufsize
, enum uio_seg bufseg
,
6411 struct nameidata nd
;
6412 char uio_buf
[UIO_SIZEOF(1)];
6414 NDINIT(&nd
, LOOKUP
, OP_READLINK
, NOFOLLOW
| AUDITVNPATH1
,
6417 error
= nameiat(&nd
, fd
);
6425 auio
= uio_createwithbuffer(1, 0, bufseg
, UIO_READ
,
6426 &uio_buf
[0], sizeof(uio_buf
));
6427 uio_addiov(auio
, buf
, bufsize
);
6428 if (vp
->v_type
!= VLNK
) {
6432 error
= mac_vnode_check_readlink(ctx
, vp
);
6435 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
,
6439 error
= VNOP_READLINK(vp
, auio
, ctx
);
6444 *retval
= bufsize
- (int)uio_resid(auio
);
6449 readlink(proc_t p
, struct readlink_args
*uap
, int32_t *retval
)
6451 enum uio_seg procseg
;
6453 procseg
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6454 return readlinkat_internal(vfs_context_current(), AT_FDCWD
,
6455 CAST_USER_ADDR_T(uap
->path
), procseg
, CAST_USER_ADDR_T(uap
->buf
),
6456 uap
->count
, procseg
, retval
);
6460 readlinkat(proc_t p
, struct readlinkat_args
*uap
, int32_t *retval
)
6462 enum uio_seg procseg
;
6464 procseg
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6465 return readlinkat_internal(vfs_context_current(), uap
->fd
, uap
->path
,
6466 procseg
, uap
->buf
, uap
->bufsize
, procseg
, retval
);
6470 * Change file flags, the deep inner layer.
6473 chflags0(vnode_t vp
, struct vnode_attr
*va
,
6474 int (*setattr
)(vnode_t
, void *, vfs_context_t
),
6475 void *arg
, vfs_context_t ctx
)
6477 kauth_action_t action
= 0;
6481 error
= mac_vnode_check_setflags(ctx
, vp
, va
->va_flags
);
6487 /* request authorisation, disregard immutability */
6488 if ((error
= vnode_authattr(vp
, va
, &action
, ctx
)) != 0) {
6492 * Request that the auth layer disregard those file flags it's allowed to when
6493 * authorizing this operation; we need to do this in order to be able to
6494 * clear immutable flags.
6496 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
| KAUTH_VNODE_NOIMMUTABLE
, ctx
)) != 0)) {
6499 error
= (*setattr
)(vp
, arg
, ctx
);
6503 mac_vnode_notify_setflags(ctx
, vp
, va
->va_flags
);
6512 * Change file flags.
6514 * NOTE: this will vnode_put() `vp'
6517 chflags1(vnode_t vp
, int flags
, vfs_context_t ctx
)
6519 struct vnode_attr va
;
6523 VATTR_SET(&va
, va_flags
, flags
);
6525 error
= chflags0(vp
, &va
, (void *)vnode_setattr
, &va
, ctx
);
6528 if ((error
== 0) && !VATTR_IS_SUPPORTED(&va
, va_flags
)) {
6536 * Change flags of a file given a path name.
6540 chflags(__unused proc_t p
, struct chflags_args
*uap
, __unused
int32_t *retval
)
6543 vfs_context_t ctx
= vfs_context_current();
6545 struct nameidata nd
;
6547 AUDIT_ARG(fflags
, uap
->flags
);
6548 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, FOLLOW
| AUDITVNPATH1
,
6549 UIO_USERSPACE
, uap
->path
, ctx
);
6557 /* we don't vnode_put() here because chflags1 does internally */
6558 error
= chflags1(vp
, uap
->flags
, ctx
);
6564 * Change flags of a file given a file descriptor.
6568 fchflags(__unused proc_t p
, struct fchflags_args
*uap
, __unused
int32_t *retval
)
6573 AUDIT_ARG(fd
, uap
->fd
);
6574 AUDIT_ARG(fflags
, uap
->flags
);
6575 if ((error
= file_vnode(uap
->fd
, &vp
))) {
6579 if ((error
= vnode_getwithref(vp
))) {
6584 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
6586 /* we don't vnode_put() here because chflags1 does internally */
6587 error
= chflags1(vp
, uap
->flags
, vfs_context_current());
6594 * Change security information on a filesystem object.
6596 * Returns: 0 Success
6597 * EPERM Operation not permitted
6598 * vnode_authattr:??? [anything vnode_authattr can return]
6599 * vnode_authorize:??? [anything vnode_authorize can return]
6600 * vnode_setattr:??? [anything vnode_setattr can return]
6602 * Notes: If vnode_authattr or vnode_authorize return EACCES, it will be
6603 * translated to EPERM before being returned.
6606 chmod_vnode(vfs_context_t ctx
, vnode_t vp
, struct vnode_attr
*vap
)
6608 kauth_action_t action
;
6611 AUDIT_ARG(mode
, vap
->va_mode
);
6612 /* XXX audit new args */
6615 /* chmod calls are not allowed for resource forks. */
6616 if (vp
->v_flag
& VISNAMEDSTREAM
) {
6622 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
6623 (error
= mac_vnode_check_setmode(ctx
, vp
, (mode_t
)vap
->va_mode
)) != 0) {
6627 if (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
)) {
6628 if ((error
= mac_vnode_check_setowner(ctx
, vp
,
6629 VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: -1,
6630 VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: -1))) {
6635 if (VATTR_IS_ACTIVE(vap
, va_acl
) &&
6636 (error
= mac_vnode_check_setacl(ctx
, vp
, vap
->va_acl
))) {
6641 /* make sure that the caller is allowed to set this security information */
6642 if (((error
= vnode_authattr(vp
, vap
, &action
, ctx
)) != 0) ||
6643 ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
6644 if (error
== EACCES
) {
6650 if ((error
= vnode_setattr(vp
, vap
, ctx
)) != 0) {
6655 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
6656 mac_vnode_notify_setmode(ctx
, vp
, (mode_t
)vap
->va_mode
);
6659 if (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
)) {
6660 mac_vnode_notify_setowner(ctx
, vp
,
6661 VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: -1,
6662 VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: -1);
6665 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
6666 mac_vnode_notify_setacl(ctx
, vp
, vap
->va_acl
);
6675 * Change mode of a file given a path name.
6677 * Returns: 0 Success
6678 * namei:??? [anything namei can return]
6679 * chmod_vnode:??? [anything chmod_vnode can return]
6682 chmodat(vfs_context_t ctx
, user_addr_t path
, struct vnode_attr
*vap
,
6683 int fd
, int flag
, enum uio_seg segflg
)
6685 struct nameidata nd
;
6688 follow
= (flag
& AT_SYMLINK_NOFOLLOW
) ? NOFOLLOW
: FOLLOW
;
6689 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, follow
| AUDITVNPATH1
,
6691 if ((error
= nameiat(&nd
, fd
))) {
6694 error
= chmod_vnode(ctx
, nd
.ni_vp
, vap
);
6695 vnode_put(nd
.ni_vp
);
6701 * chmod_extended: Change the mode of a file given a path name; with extended
6702 * argument list (including extended security (ACL)).
6704 * Parameters: p Process requesting the open
6705 * uap User argument descriptor (see below)
6708 * Indirect: uap->path Path to object (same as 'chmod')
6709 * uap->uid UID to set
6710 * uap->gid GID to set
6711 * uap->mode File mode to set (same as 'chmod')
6712 * uap->xsecurity ACL to set (or delete)
6714 * Returns: 0 Success
6717 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
6719 * XXX: We should enummerate the possible errno values here, and where
6720 * in the code they originated.
6723 chmod_extended(__unused proc_t p
, struct chmod_extended_args
*uap
, __unused
int32_t *retval
)
6726 struct vnode_attr va
;
6727 kauth_filesec_t xsecdst
;
6729 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
6732 if (uap
->mode
!= -1) {
6733 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
6735 if (uap
->uid
!= KAUTH_UID_NONE
) {
6736 VATTR_SET(&va
, va_uid
, uap
->uid
);
6738 if (uap
->gid
!= KAUTH_GID_NONE
) {
6739 VATTR_SET(&va
, va_gid
, uap
->gid
);
6743 switch (uap
->xsecurity
) {
6744 /* explicit remove request */
6745 case CAST_USER_ADDR_T((void *)1): /* _FILESEC_REMOVE_ACL */
6746 VATTR_SET(&va
, va_acl
, NULL
);
6749 case USER_ADDR_NULL
:
6752 if ((error
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0) {
6755 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
6756 KAUTH_DEBUG("CHMOD - setting ACL with %d entries", va
.va_acl
->acl_entrycount
);
6759 error
= chmodat(vfs_context_current(), uap
->path
, &va
, AT_FDCWD
, 0,
6762 if (xsecdst
!= NULL
) {
6763 kauth_filesec_free(xsecdst
);
6769 * Returns: 0 Success
6770 * chmodat:??? [anything chmodat can return]
6773 fchmodat_internal(vfs_context_t ctx
, user_addr_t path
, int mode
, int fd
,
6774 int flag
, enum uio_seg segflg
)
6776 struct vnode_attr va
;
6779 VATTR_SET(&va
, va_mode
, mode
& ALLPERMS
);
6781 return chmodat(ctx
, path
, &va
, fd
, flag
, segflg
);
6785 chmod(__unused proc_t p
, struct chmod_args
*uap
, __unused
int32_t *retval
)
6787 return fchmodat_internal(vfs_context_current(), uap
->path
, uap
->mode
,
6788 AT_FDCWD
, 0, UIO_USERSPACE
);
6792 fchmodat(__unused proc_t p
, struct fchmodat_args
*uap
, __unused
int32_t *retval
)
6794 if (uap
->flag
& ~AT_SYMLINK_NOFOLLOW
) {
6798 return fchmodat_internal(vfs_context_current(), uap
->path
, uap
->mode
,
6799 uap
->fd
, uap
->flag
, UIO_USERSPACE
);
6803 * Change mode of a file given a file descriptor.
6806 fchmod1(__unused proc_t p
, int fd
, struct vnode_attr
*vap
)
6813 if ((error
= file_vnode(fd
, &vp
)) != 0) {
6816 if ((error
= vnode_getwithref(vp
)) != 0) {
6820 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
6822 error
= chmod_vnode(vfs_context_current(), vp
, vap
);
6823 (void)vnode_put(vp
);
6830 * fchmod_extended: Change mode of a file given a file descriptor; with
6831 * extended argument list (including extended security (ACL)).
6833 * Parameters: p Process requesting to change file mode
6834 * uap User argument descriptor (see below)
6837 * Indirect: uap->mode File mode to set (same as 'chmod')
6838 * uap->uid UID to set
6839 * uap->gid GID to set
6840 * uap->xsecurity ACL to set (or delete)
6841 * uap->fd File descriptor of file to change mode
6843 * Returns: 0 Success
6848 fchmod_extended(proc_t p
, struct fchmod_extended_args
*uap
, __unused
int32_t *retval
)
6851 struct vnode_attr va
;
6852 kauth_filesec_t xsecdst
;
6854 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
6857 if (uap
->mode
!= -1) {
6858 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
6860 if (uap
->uid
!= KAUTH_UID_NONE
) {
6861 VATTR_SET(&va
, va_uid
, uap
->uid
);
6863 if (uap
->gid
!= KAUTH_GID_NONE
) {
6864 VATTR_SET(&va
, va_gid
, uap
->gid
);
6868 switch (uap
->xsecurity
) {
6869 case USER_ADDR_NULL
:
6870 VATTR_SET(&va
, va_acl
, NULL
);
6872 case CAST_USER_ADDR_T((void *)1): /* _FILESEC_REMOVE_ACL */
6873 VATTR_SET(&va
, va_acl
, NULL
);
6876 case CAST_USER_ADDR_T(-1):
6879 if ((error
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0) {
6882 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
6885 error
= fchmod1(p
, uap
->fd
, &va
);
6888 switch (uap
->xsecurity
) {
6889 case USER_ADDR_NULL
:
6890 case CAST_USER_ADDR_T(-1):
6893 if (xsecdst
!= NULL
) {
6894 kauth_filesec_free(xsecdst
);
6901 fchmod(proc_t p
, struct fchmod_args
*uap
, __unused
int32_t *retval
)
6903 struct vnode_attr va
;
6906 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
6908 return fchmod1(p
, uap
->fd
, &va
);
6913 * Set ownership given a path name.
6917 fchownat_internal(vfs_context_t ctx
, int fd
, user_addr_t path
, uid_t uid
,
6918 gid_t gid
, int flag
, enum uio_seg segflg
)
6921 struct vnode_attr va
;
6923 struct nameidata nd
;
6925 kauth_action_t action
;
6927 AUDIT_ARG(owner
, uid
, gid
);
6929 follow
= (flag
& AT_SYMLINK_NOFOLLOW
) ? NOFOLLOW
: FOLLOW
;
6930 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, follow
| AUDITVNPATH1
, segflg
,
6932 error
= nameiat(&nd
, fd
);
6941 if (uid
!= (uid_t
)VNOVAL
) {
6942 VATTR_SET(&va
, va_uid
, uid
);
6944 if (gid
!= (gid_t
)VNOVAL
) {
6945 VATTR_SET(&va
, va_gid
, gid
);
6949 error
= mac_vnode_check_setowner(ctx
, vp
, uid
, gid
);
6955 /* preflight and authorize attribute changes */
6956 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0) {
6959 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
6962 error
= vnode_setattr(vp
, &va
, ctx
);
6966 mac_vnode_notify_setowner(ctx
, vp
, uid
, gid
);
6972 * EACCES is only allowed from namei(); permissions failure should
6973 * return EPERM, so we need to translate the error code.
6975 if (error
== EACCES
) {
6984 chown(__unused proc_t p
, struct chown_args
*uap
, __unused
int32_t *retval
)
6986 return fchownat_internal(vfs_context_current(), AT_FDCWD
, uap
->path
,
6987 uap
->uid
, uap
->gid
, 0, UIO_USERSPACE
);
6991 lchown(__unused proc_t p
, struct lchown_args
*uap
, __unused
int32_t *retval
)
6993 return fchownat_internal(vfs_context_current(), AT_FDCWD
, uap
->path
,
6994 uap
->owner
, uap
->group
, AT_SYMLINK_NOFOLLOW
, UIO_USERSPACE
);
6998 fchownat(__unused proc_t p
, struct fchownat_args
*uap
, __unused
int32_t *retval
)
7000 if (uap
->flag
& ~AT_SYMLINK_NOFOLLOW
) {
7004 return fchownat_internal(vfs_context_current(), uap
->fd
, uap
->path
,
7005 uap
->uid
, uap
->gid
, uap
->flag
, UIO_USERSPACE
);
7009 * Set ownership given a file descriptor.
7013 fchown(__unused proc_t p
, struct fchown_args
*uap
, __unused
int32_t *retval
)
7015 struct vnode_attr va
;
7016 vfs_context_t ctx
= vfs_context_current();
7019 kauth_action_t action
;
7021 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
7022 AUDIT_ARG(fd
, uap
->fd
);
7024 if ((error
= file_vnode(uap
->fd
, &vp
))) {
7028 if ((error
= vnode_getwithref(vp
))) {
7032 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
7035 if (uap
->uid
!= VNOVAL
) {
7036 VATTR_SET(&va
, va_uid
, uap
->uid
);
7038 if (uap
->gid
!= VNOVAL
) {
7039 VATTR_SET(&va
, va_gid
, uap
->gid
);
7043 /* chown calls are not allowed for resource forks. */
7044 if (vp
->v_flag
& VISNAMEDSTREAM
) {
7051 error
= mac_vnode_check_setowner(ctx
, vp
, uap
->uid
, uap
->gid
);
7057 /* preflight and authorize attribute changes */
7058 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0) {
7061 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
7062 if (error
== EACCES
) {
7067 error
= vnode_setattr(vp
, &va
, ctx
);
7071 mac_vnode_notify_setowner(ctx
, vp
, uap
->uid
, uap
->gid
);
7076 (void)vnode_put(vp
);
7082 getutimes(user_addr_t usrtvp
, struct timespec
*tsp
)
7086 if (usrtvp
== USER_ADDR_NULL
) {
7087 struct timeval old_tv
;
7088 /* XXX Y2038 bug because of microtime argument */
7090 TIMEVAL_TO_TIMESPEC(&old_tv
, &tsp
[0]);
7093 if (IS_64BIT_PROCESS(current_proc())) {
7094 struct user64_timeval tv
[2];
7095 error
= copyin(usrtvp
, (void *)tv
, sizeof(tv
));
7099 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
7100 TIMEVAL_TO_TIMESPEC(&tv
[1], &tsp
[1]);
7102 struct user32_timeval tv
[2];
7103 error
= copyin(usrtvp
, (void *)tv
, sizeof(tv
));
7107 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
7108 TIMEVAL_TO_TIMESPEC(&tv
[1], &tsp
[1]);
7115 setutimes(vfs_context_t ctx
, vnode_t vp
, const struct timespec
*ts
,
7119 struct vnode_attr va
;
7120 kauth_action_t action
;
7122 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
7125 VATTR_SET(&va
, va_access_time
, ts
[0]);
7126 VATTR_SET(&va
, va_modify_time
, ts
[1]);
7128 va
.va_vaflags
|= VA_UTIMES_NULL
;
7132 /* utimes calls are not allowed for resource forks. */
7133 if (vp
->v_flag
& VISNAMEDSTREAM
) {
7140 error
= mac_vnode_check_setutimes(ctx
, vp
, ts
[0], ts
[1]);
7145 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0) {
7146 if (!nullflag
&& error
== EACCES
) {
7152 /* since we may not need to auth anything, check here */
7153 if ((action
!= 0) && ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
7154 if (!nullflag
&& error
== EACCES
) {
7159 error
= vnode_setattr(vp
, &va
, ctx
);
7163 mac_vnode_notify_setutimes(ctx
, vp
, ts
[0], ts
[1]);
7172 * Set the access and modification times of a file.
7176 utimes(__unused proc_t p
, struct utimes_args
*uap
, __unused
int32_t *retval
)
7178 struct timespec ts
[2];
7181 struct nameidata nd
;
7182 vfs_context_t ctx
= vfs_context_current();
7185 * AUDIT: Needed to change the order of operations to do the
7186 * name lookup first because auditing wants the path.
7188 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, FOLLOW
| AUDITVNPATH1
,
7189 UIO_USERSPACE
, uap
->path
, ctx
);
7197 * Fetch the user-supplied time. If usrtvp is USER_ADDR_NULL, we fetch
7198 * the current time instead.
7201 if ((error
= getutimes(usrtvp
, ts
)) != 0) {
7205 error
= setutimes(ctx
, nd
.ni_vp
, ts
, usrtvp
== USER_ADDR_NULL
);
7208 vnode_put(nd
.ni_vp
);
7213 * Set the access and modification times of a file.
7217 futimes(__unused proc_t p
, struct futimes_args
*uap
, __unused
int32_t *retval
)
7219 struct timespec ts
[2];
7224 AUDIT_ARG(fd
, uap
->fd
);
7226 if ((error
= getutimes(usrtvp
, ts
)) != 0) {
7229 if ((error
= file_vnode(uap
->fd
, &vp
)) != 0) {
7232 if ((error
= vnode_getwithref(vp
))) {
7237 error
= setutimes(vfs_context_current(), vp
, ts
, usrtvp
== 0);
7244 * Truncate a file given its path name.
7248 truncate(__unused proc_t p
, struct truncate_args
*uap
, __unused
int32_t *retval
)
7251 struct vnode_attr va
;
7252 vfs_context_t ctx
= vfs_context_current();
7254 struct nameidata nd
;
7255 kauth_action_t action
;
7257 if (uap
->length
< 0) {
7260 NDINIT(&nd
, LOOKUP
, OP_TRUNCATE
, FOLLOW
| AUDITVNPATH1
,
7261 UIO_USERSPACE
, uap
->path
, ctx
);
7262 if ((error
= namei(&nd
))) {
7270 VATTR_SET(&va
, va_data_size
, uap
->length
);
7273 error
= mac_vnode_check_truncate(ctx
, NOCRED
, vp
);
7279 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0) {
7282 if ((action
!= 0) && ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
7285 error
= vnode_setattr(vp
, &va
, ctx
);
7289 mac_vnode_notify_truncate(ctx
, NOCRED
, vp
);
7299 * Truncate a file given a file descriptor.
7303 ftruncate(proc_t p
, struct ftruncate_args
*uap
, int32_t *retval
)
7305 vfs_context_t ctx
= vfs_context_current();
7306 struct vnode_attr va
;
7308 struct fileproc
*fp
;
7312 AUDIT_ARG(fd
, uap
->fd
);
7313 if (uap
->length
< 0) {
7317 if ((error
= fp_lookup(p
, fd
, &fp
, 0))) {
7321 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
7323 error
= pshm_truncate(p
, fp
, uap
->fd
, uap
->length
, retval
);
7332 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
7334 if ((fp
->f_fglob
->fg_flag
& FWRITE
) == 0) {
7335 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
7340 if ((error
= vnode_getwithref(vp
)) != 0) {
7344 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
7347 error
= mac_vnode_check_truncate(ctx
,
7348 fp
->f_fglob
->fg_cred
, vp
);
7350 (void)vnode_put(vp
);
7355 VATTR_SET(&va
, va_data_size
, uap
->length
);
7356 error
= vnode_setattr(vp
, &va
, ctx
);
7360 mac_vnode_notify_truncate(ctx
, fp
->f_fglob
->fg_cred
, vp
);
7364 (void)vnode_put(vp
);
7372 * Sync an open file with synchronized I/O _file_ integrity completion
7376 fsync(proc_t p
, struct fsync_args
*uap
, __unused
int32_t *retval
)
7378 __pthread_testcancel(1);
7379 return fsync_common(p
, uap
, MNT_WAIT
);
7384 * Sync an open file with synchronized I/O _file_ integrity completion
7386 * Notes: This is a legacy support function that does not test for
7387 * thread cancellation points.
7391 fsync_nocancel(proc_t p
, struct fsync_nocancel_args
*uap
, __unused
int32_t *retval
)
7393 return fsync_common(p
, (struct fsync_args
*)uap
, MNT_WAIT
);
7398 * Sync an open file with synchronized I/O _data_ integrity completion
7402 fdatasync(proc_t p
, struct fdatasync_args
*uap
, __unused
int32_t *retval
)
7404 __pthread_testcancel(1);
7405 return fsync_common(p
, (struct fsync_args
*)uap
, MNT_DWAIT
);
7412 * Common fsync code to support both synchronized I/O file integrity completion
7413 * (normal fsync) and synchronized I/O data integrity completion (fdatasync).
7415 * If 'flags' is MNT_DWAIT, the caller is requesting data integrity, which
7416 * will only guarantee that the file data contents are retrievable. If
7417 * 'flags' is MNT_WAIT, the caller is rewuesting file integrity, which also
7418 * includes additional metadata unnecessary for retrieving the file data
7419 * contents, such as atime, mtime, ctime, etc., also be committed to stable
7422 * Parameters: p The process
7423 * uap->fd The descriptor to synchronize
7424 * flags The data integrity flags
7426 * Returns: int Success
7427 * fp_getfvp:EBADF Bad file descriptor
7428 * fp_getfvp:ENOTSUP fd does not refer to a vnode
7429 * VNOP_FSYNC:??? unspecified
7431 * Notes: We use struct fsync_args because it is a short name, and all
7432 * caller argument structures are otherwise identical.
7435 fsync_common(proc_t p
, struct fsync_args
*uap
, int flags
)
7438 struct fileproc
*fp
;
7439 vfs_context_t ctx
= vfs_context_current();
7442 AUDIT_ARG(fd
, uap
->fd
);
7444 if ((error
= fp_getfvp(p
, uap
->fd
, &fp
, &vp
))) {
7447 if ((error
= vnode_getwithref(vp
))) {
7452 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
7454 error
= VNOP_FSYNC(vp
, flags
, ctx
);
7457 /* Sync resource fork shadow file if necessary. */
7459 (vp
->v_flag
& VISNAMEDSTREAM
) &&
7460 (vp
->v_parent
!= NULLVP
) &&
7461 vnode_isshadow(vp
) &&
7462 (fp
->f_flags
& FP_WRITTEN
)) {
7463 (void) vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
7467 (void)vnode_put(vp
);
7473 * Duplicate files. Source must be a file, target must be a file or
7476 * XXX Copyfile authorisation checking is woefully inadequate, and will not
7477 * perform inheritance correctly.
7481 copyfile(__unused proc_t p
, struct copyfile_args
*uap
, __unused
int32_t *retval
)
7483 vnode_t tvp
, fvp
, tdvp
, sdvp
;
7484 struct nameidata fromnd
, tond
;
7486 vfs_context_t ctx
= vfs_context_current();
7488 struct filedesc
*fdp
= (vfs_context_proc(ctx
))->p_fd
;
7489 struct vnode_attr va
;
7492 /* Check that the flags are valid. */
7494 if (uap
->flags
& ~CPF_MASK
) {
7498 NDINIT(&fromnd
, LOOKUP
, OP_COPYFILE
, AUDITVNPATH1
,
7499 UIO_USERSPACE
, uap
->from
, ctx
);
7500 if ((error
= namei(&fromnd
))) {
7505 NDINIT(&tond
, CREATE
, OP_LINK
,
7506 LOCKPARENT
| LOCKLEAF
| NOCACHE
| SAVESTART
| AUDITVNPATH2
| CN_NBMOUNTLOOK
,
7507 UIO_USERSPACE
, uap
->to
, ctx
);
7508 if ((error
= namei(&tond
))) {
7515 if (!(uap
->flags
& CPF_OVERWRITE
)) {
7521 if (fvp
->v_type
== VDIR
|| (tvp
&& tvp
->v_type
== VDIR
)) {
7526 /* This calls existing MAC hooks for open */
7527 if ((error
= vn_authorize_open_existing(fvp
, &fromnd
.ni_cnd
, FREAD
, ctx
,
7534 * See unlinkat_internal for an explanation of the potential
7535 * ENOENT from the MAC hook but the gist is that the MAC hook
7536 * can fail because vn_getpath isn't able to return the full
7537 * path. We choose to ignore this failure.
7539 error
= vn_authorize_unlink(tdvp
, tvp
, &tond
.ni_cnd
, ctx
, NULL
);
7540 if (error
&& error
!= ENOENT
) {
7548 VATTR_SET(&va
, va_type
, fvp
->v_type
);
7549 /* Mask off all but regular access permissions */
7550 VATTR_SET(&va
, va_mode
,
7551 ((((uap
->mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
) & ACCESSPERMS
));
7552 error
= mac_vnode_check_create(ctx
, tdvp
, &tond
.ni_cnd
, &va
);
7556 #endif /* CONFIG_MACF */
7558 if ((error
= vnode_authorize(tdvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0) {
7566 * If source is the same as the destination (that is the
7567 * same inode number) then there is nothing to do.
7568 * (fixed to have POSIX semantics - CSM 3/2/98)
7574 error
= VNOP_COPYFILE(fvp
, tdvp
, tvp
, &tond
.ni_cnd
, uap
->mode
, uap
->flags
, ctx
);
7577 sdvp
= tond
.ni_startdir
;
7579 * nameidone has to happen before we vnode_put(tdvp)
7580 * since it may need to release the fs_nodelock on the tdvp
7600 #define CLONE_SNAPSHOT_FALLBACKS_ENABLED 1
7603 * Helper function for doing clones. The caller is expected to provide an
7604 * iocounted source vnode and release it.
7607 clonefile_internal(vnode_t fvp
, boolean_t data_read_authorised
, int dst_dirfd
,
7608 user_addr_t dst
, uint32_t flags
, vfs_context_t ctx
)
7611 struct nameidata tond
;
7614 boolean_t free_src_acl
;
7615 boolean_t attr_cleanup
;
7617 kauth_action_t action
;
7618 struct componentname
*cnp
;
7620 struct vnode_attr va
;
7621 struct vnode_attr nva
;
7622 uint32_t vnop_flags
;
7624 v_type
= vnode_vtype(fvp
);
7629 action
= KAUTH_VNODE_ADD_FILE
;
7632 if (vnode_isvroot(fvp
) || vnode_ismount(fvp
) ||
7633 fvp
->v_mountedhere
) {
7636 action
= KAUTH_VNODE_ADD_SUBDIRECTORY
;
7642 AUDIT_ARG(fd2
, dst_dirfd
);
7643 AUDIT_ARG(value32
, flags
);
7645 follow
= (flags
& CLONE_NOFOLLOW
) ? NOFOLLOW
: FOLLOW
;
7646 NDINIT(&tond
, CREATE
, OP_LINK
, follow
| WANTPARENT
| AUDITVNPATH2
,
7647 UIO_USERSPACE
, dst
, ctx
);
7648 if ((error
= nameiat(&tond
, dst_dirfd
))) {
7655 free_src_acl
= FALSE
;
7656 attr_cleanup
= FALSE
;
7663 if (vnode_mount(tdvp
) != vnode_mount(fvp
)) {
7669 if ((error
= mac_vnode_check_clone(ctx
, tdvp
, fvp
, cnp
))) {
7673 if ((error
= vnode_authorize(tdvp
, NULL
, action
, ctx
))) {
7677 action
= KAUTH_VNODE_GENERIC_READ_BITS
;
7678 if (data_read_authorised
) {
7679 action
&= ~KAUTH_VNODE_READ_DATA
;
7681 if ((error
= vnode_authorize(fvp
, NULL
, action
, ctx
))) {
7686 * certain attributes may need to be changed from the source, we ask for
7690 VATTR_WANTED(&va
, va_uid
);
7691 VATTR_WANTED(&va
, va_gid
);
7692 VATTR_WANTED(&va
, va_mode
);
7693 VATTR_WANTED(&va
, va_flags
);
7694 VATTR_WANTED(&va
, va_acl
);
7696 if ((error
= vnode_getattr(fvp
, &va
, ctx
)) != 0) {
7701 VATTR_SET(&nva
, va_type
, v_type
);
7702 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && va
.va_acl
!= NULL
) {
7703 VATTR_SET(&nva
, va_acl
, va
.va_acl
);
7704 free_src_acl
= TRUE
;
7707 /* Handle ACL inheritance, initialize vap. */
7708 if (v_type
== VLNK
) {
7709 error
= vnode_authattr_new(tdvp
, &nva
, 0, ctx
);
7711 error
= vn_attribute_prepare(tdvp
, &nva
, &defaulted
, ctx
);
7715 attr_cleanup
= TRUE
;
7718 vnop_flags
= VNODE_CLONEFILE_DEFAULT
;
7720 * We've got initial values for all security parameters,
7721 * If we are superuser, then we can change owners to be the
7722 * same as the source. Both superuser and the owner have default
7723 * WRITE_SECURITY privileges so all other fields can be taken
7724 * from source as well.
7726 if (!(flags
& CLONE_NOOWNERCOPY
) && vfs_context_issuser(ctx
)) {
7727 if (VATTR_IS_SUPPORTED(&va
, va_uid
)) {
7728 VATTR_SET(&nva
, va_uid
, va
.va_uid
);
7730 if (VATTR_IS_SUPPORTED(&va
, va_gid
)) {
7731 VATTR_SET(&nva
, va_gid
, va
.va_gid
);
7734 vnop_flags
|= VNODE_CLONEFILE_NOOWNERCOPY
;
7737 if (VATTR_IS_SUPPORTED(&va
, va_mode
)) {
7738 VATTR_SET(&nva
, va_mode
, va
.va_mode
);
7740 if (VATTR_IS_SUPPORTED(&va
, va_flags
)) {
7741 VATTR_SET(&nva
, va_flags
,
7742 ((va
.va_flags
& ~(UF_DATAVAULT
| SF_RESTRICTED
)) | /* Turn off from source */
7743 (nva
.va_flags
& (UF_DATAVAULT
| SF_RESTRICTED
))));
7746 error
= VNOP_CLONEFILE(fvp
, tdvp
, &tvp
, cnp
, &nva
, vnop_flags
, ctx
);
7748 if (!error
&& tvp
) {
7749 int update_flags
= 0;
7752 #endif /* CONFIG_FSE */
7755 (void)vnode_label(vnode_mount(tvp
), tdvp
, tvp
, cnp
,
7756 VNODE_LABEL_CREATE
, ctx
);
7759 * If some of the requested attributes weren't handled by the
7760 * VNOP, use our fallback code.
7762 if (!VATTR_ALL_SUPPORTED(&va
)) {
7763 (void)vnode_setattr_fallback(tvp
, &nva
, ctx
);
7766 // Make sure the name & parent pointers are hooked up
7767 if (tvp
->v_name
== NULL
) {
7768 update_flags
|= VNODE_UPDATE_NAME
;
7770 if (tvp
->v_parent
== NULLVP
) {
7771 update_flags
|= VNODE_UPDATE_PARENT
;
7775 (void)vnode_update_identity(tvp
, tdvp
, cnp
->cn_nameptr
,
7776 cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
7780 switch (vnode_vtype(tvp
)) {
7784 fsevent
= FSE_CREATE_FILE
;
7787 fsevent
= FSE_CREATE_DIR
;
7793 if (need_fsevent(fsevent
, tvp
)) {
7795 * The following is a sequence of three explicit events.
7796 * A pair of FSE_CLONE events representing the source and destination
7797 * followed by an FSE_CREATE_[FILE | DIR] for the destination.
7798 * fseventsd may coalesce the destination clone and create events
7799 * into a single event resulting in the following sequence for a client
7801 * FSE_CLONE | FSE_CREATE (dst)
7803 add_fsevent(FSE_CLONE
, ctx
, FSE_ARG_VNODE
, fvp
, FSE_ARG_VNODE
, tvp
,
7805 add_fsevent(fsevent
, ctx
, FSE_ARG_VNODE
, tvp
,
7808 #endif /* CONFIG_FSE */
7813 vn_attribute_cleanup(&nva
, defaulted
);
7815 if (free_src_acl
&& va
.va_acl
) {
7816 kauth_acl_free(va
.va_acl
);
7827 * clone files or directories, target must not exist.
7831 clonefileat(__unused proc_t p
, struct clonefileat_args
*uap
,
7832 __unused
int32_t *retval
)
7835 struct nameidata fromnd
;
7838 vfs_context_t ctx
= vfs_context_current();
7840 /* Check that the flags are valid. */
7841 if (uap
->flags
& ~(CLONE_NOFOLLOW
| CLONE_NOOWNERCOPY
)) {
7845 AUDIT_ARG(fd
, uap
->src_dirfd
);
7847 follow
= (uap
->flags
& CLONE_NOFOLLOW
) ? NOFOLLOW
: FOLLOW
;
7848 NDINIT(&fromnd
, LOOKUP
, OP_COPYFILE
, follow
| AUDITVNPATH1
,
7849 UIO_USERSPACE
, uap
->src
, ctx
);
7850 if ((error
= nameiat(&fromnd
, uap
->src_dirfd
))) {
7857 error
= clonefile_internal(fvp
, FALSE
, uap
->dst_dirfd
, uap
->dst
,
7865 fclonefileat(__unused proc_t p
, struct fclonefileat_args
*uap
,
7866 __unused
int32_t *retval
)
7869 struct fileproc
*fp
;
7871 vfs_context_t ctx
= vfs_context_current();
7873 /* Check that the flags are valid. */
7874 if (uap
->flags
& ~(CLONE_NOFOLLOW
| CLONE_NOOWNERCOPY
)) {
7878 AUDIT_ARG(fd
, uap
->src_fd
);
7879 error
= fp_getfvp(p
, uap
->src_fd
, &fp
, &fvp
);
7884 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
7885 AUDIT_ARG(vnpath_withref
, fvp
, ARG_VNODE1
);
7890 if ((error
= vnode_getwithref(fvp
))) {
7894 AUDIT_ARG(vnpath
, fvp
, ARG_VNODE1
);
7896 error
= clonefile_internal(fvp
, TRUE
, uap
->dst_dirfd
, uap
->dst
,
7901 file_drop(uap
->src_fd
);
7906 rename_submounts_callback(mount_t mp
, void *arg
)
7909 mount_t pmp
= (mount_t
)arg
;
7910 int prefix_len
= strlen(pmp
->mnt_vfsstat
.f_mntonname
);
7912 if (strncmp(mp
->mnt_vfsstat
.f_mntonname
, pmp
->mnt_vfsstat
.f_mntonname
, prefix_len
) != 0) {
7916 if (mp
->mnt_vfsstat
.f_mntonname
[prefix_len
] != '/') {
7920 if ((error
= vfs_busy(mp
, LK_NOWAIT
))) {
7921 printf("vfs_busy failed with %d for %s\n", error
, mp
->mnt_vfsstat
.f_mntonname
);
7925 int pathlen
= MAXPATHLEN
;
7926 if ((error
= vn_getpath_ext(mp
->mnt_vnodecovered
, NULL
, mp
->mnt_vfsstat
.f_mntonname
, &pathlen
, VN_GETPATH_FSENTER
))) {
7927 printf("vn_getpath_ext failed with %d for mnt_vnodecovered of %s\n", error
, mp
->mnt_vfsstat
.f_mntonname
);
7936 * Rename files. Source and destination must either both be directories,
7937 * or both not be directories. If target is a directory, it must be empty.
7941 renameat_internal(vfs_context_t ctx
, int fromfd
, user_addr_t from
,
7942 int tofd
, user_addr_t to
, int segflg
, vfs_rename_flags_t flags
)
7944 if (flags
& ~VFS_RENAME_FLAGS_MASK
) {
7948 if (ISSET(flags
, VFS_RENAME_SWAP
) && ISSET(flags
, VFS_RENAME_EXCL
)) {
7954 struct nameidata
*fromnd
, *tond
;
7962 const char *oname
= NULL
;
7963 char *from_name
= NULL
, *to_name
= NULL
;
7964 char *from_name_no_firmlink
= NULL
, *to_name_no_firmlink
= NULL
;
7965 int from_len
= 0, to_len
= 0;
7966 int from_len_no_firmlink
= 0, to_len_no_firmlink
= 0;
7967 int holding_mntlock
;
7968 mount_t locked_mp
= NULL
;
7969 vnode_t oparent
= NULLVP
;
7971 fse_info from_finfo
, to_finfo
;
7973 int from_truncated
= 0, to_truncated
= 0;
7974 int from_truncated_no_firmlink
= 0, to_truncated_no_firmlink
= 0;
7976 struct vnode_attr
*fvap
, *tvap
;
7978 /* carving out a chunk for structs that are too big to be on stack. */
7980 struct nameidata from_node
, to_node
;
7981 struct vnode_attr fv_attr
, tv_attr
;
7983 MALLOC(__rename_data
, void *, sizeof(*__rename_data
), M_TEMP
, M_WAITOK
);
7984 fromnd
= &__rename_data
->from_node
;
7985 tond
= &__rename_data
->to_node
;
7987 holding_mntlock
= 0;
7996 NDINIT(fromnd
, DELETE
, OP_UNLINK
, WANTPARENT
| AUDITVNPATH1
,
7998 fromnd
->ni_flag
= NAMEI_COMPOUNDRENAME
;
8000 NDINIT(tond
, RENAME
, OP_RENAME
, WANTPARENT
| AUDITVNPATH2
| CN_NBMOUNTLOOK
,
8002 tond
->ni_flag
= NAMEI_COMPOUNDRENAME
;
8005 if ((fromnd
->ni_flag
& NAMEI_CONTLOOKUP
) != 0 || !continuing
) {
8006 if ((error
= nameiat(fromnd
, fromfd
))) {
8009 fdvp
= fromnd
->ni_dvp
;
8010 fvp
= fromnd
->ni_vp
;
8012 if (fvp
&& fvp
->v_type
== VDIR
) {
8013 tond
->ni_cnd
.cn_flags
|= WILLBEDIR
;
8017 if ((tond
->ni_flag
& NAMEI_CONTLOOKUP
) != 0 || !continuing
) {
8018 if ((error
= nameiat(tond
, tofd
))) {
8020 * Translate error code for rename("dir1", "dir2/.").
8022 if (error
== EISDIR
&& fvp
->v_type
== VDIR
) {
8027 tdvp
= tond
->ni_dvp
;
8031 #if DEVELOPMENT || DEBUG
8033 * XXX VSWAP: Check for entitlements or special flag here
8034 * so we can restrict access appropriately.
8036 #else /* DEVELOPMENT || DEBUG */
8038 if (fromnd
->ni_vp
&& vnode_isswap(fromnd
->ni_vp
) && (ctx
!= vfs_context_kernel())) {
8043 if (tond
->ni_vp
&& vnode_isswap(tond
->ni_vp
) && (ctx
!= vfs_context_kernel())) {
8047 #endif /* DEVELOPMENT || DEBUG */
8049 if (!tvp
&& ISSET(flags
, VFS_RENAME_SWAP
)) {
8054 if (tvp
&& ISSET(flags
, VFS_RENAME_EXCL
)) {
8059 batched
= vnode_compound_rename_available(fdvp
);
8062 need_event
= need_fsevent(FSE_RENAME
, fdvp
);
8065 get_fse_info(fvp
, &from_finfo
, ctx
);
8067 error
= vfs_get_notify_attributes(&__rename_data
->fv_attr
);
8072 fvap
= &__rename_data
->fv_attr
;
8076 get_fse_info(tvp
, &to_finfo
, ctx
);
8077 } else if (batched
) {
8078 error
= vfs_get_notify_attributes(&__rename_data
->tv_attr
);
8083 tvap
= &__rename_data
->tv_attr
;
8088 #endif /* CONFIG_FSE */
8090 has_listeners
= kauth_authorize_fileop_has_listeners();
8094 if (AUDIT_RECORD_EXISTS()) {
8099 if (need_event
|| has_listeners
) {
8100 if (from_name
== NULL
) {
8101 GET_PATH(from_name
);
8102 if (from_name
== NULL
) {
8108 from_len
= safe_getpath(fdvp
, fromnd
->ni_cnd
.cn_nameptr
, from_name
, MAXPATHLEN
, &from_truncated
);
8110 if (from_name_no_firmlink
== NULL
) {
8111 GET_PATH(from_name_no_firmlink
);
8112 if (from_name_no_firmlink
== NULL
) {
8118 from_len_no_firmlink
= safe_getpath_no_firmlink(fdvp
, fromnd
->ni_cnd
.cn_nameptr
, from_name_no_firmlink
, MAXPATHLEN
, &from_truncated_no_firmlink
);
8121 if (need_event
|| need_kpath2
|| has_listeners
) {
8122 if (to_name
== NULL
) {
8124 if (to_name
== NULL
) {
8130 to_len
= safe_getpath(tdvp
, tond
->ni_cnd
.cn_nameptr
, to_name
, MAXPATHLEN
, &to_truncated
);
8132 if (to_name_no_firmlink
== NULL
) {
8133 GET_PATH(to_name_no_firmlink
);
8134 if (to_name_no_firmlink
== NULL
) {
8140 to_len_no_firmlink
= safe_getpath_no_firmlink(tdvp
, tond
->ni_cnd
.cn_nameptr
, to_name_no_firmlink
, MAXPATHLEN
, &to_truncated_no_firmlink
);
8141 if (to_name
&& need_kpath2
) {
8142 AUDIT_ARG(kpath
, to_name
, ARG_KPATH2
);
8147 * Claim: this check will never reject a valid rename.
8148 * For success, either fvp must be on the same mount as tdvp, or fvp must sit atop a vnode on the same mount as tdvp.
8149 * Suppose fdvp and tdvp are not on the same mount.
8150 * If fvp is on the same mount as tdvp, then fvp is not on the same mount as fdvp, so fvp is the root of its filesystem. If fvp is the root,
8151 * then you can't move it to within another dir on the same mountpoint.
8152 * If fvp sits atop a vnode on the same mount as fdvp, then that vnode must be part of the same mount as fdvp, which is a contradiction.
8154 * If this check passes, then we are safe to pass these vnodes to the same FS.
8156 if (fdvp
->v_mount
!= tdvp
->v_mount
) {
8160 goto skipped_lookup
;
8164 error
= vn_authorize_renamex_with_paths(fdvp
, fvp
, &fromnd
->ni_cnd
, from_name
, tdvp
, tvp
, &tond
->ni_cnd
, to_name
, ctx
, flags
, NULL
);
8166 if (error
== ENOENT
) {
8167 if (retry_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
8169 * We encountered a race where after doing the namei, tvp stops
8170 * being valid. If so, simply re-drive the rename call from the
8182 * If the source and destination are the same (i.e. they're
8183 * links to the same vnode) and the target file system is
8184 * case sensitive, then there is nothing to do.
8186 * XXX Come back to this.
8192 * Note: if _PC_CASE_SENSITIVE selector isn't supported,
8193 * then assume that this file system is case sensitive.
8195 if (VNOP_PATHCONF(fvp
, _PC_CASE_SENSITIVE
, &pathconf_val
, ctx
) != 0 ||
8196 pathconf_val
!= 0) {
8202 * Allow the renaming of mount points.
8203 * - target must not exist
8204 * - target must reside in the same directory as source
8205 * - union mounts cannot be renamed
8206 * - "/" cannot be renamed
8208 * XXX Handle this in VFS after a continued lookup (if we missed
8209 * in the cache to start off)
8211 * N.B. If RENAME_SWAP is being used, then @tvp != NULL and so
8212 * we'll skip past here. The file system is responsible for
8213 * checking that @tvp is not a descendent of @fvp and vice versa
8214 * so it should always return EINVAL if either @tvp or @fvp is the
8217 if ((fvp
->v_flag
& VROOT
) &&
8218 (fvp
->v_type
== VDIR
) &&
8220 (fvp
->v_mountedhere
== NULL
) &&
8222 ((fvp
->v_mount
->mnt_flag
& (MNT_UNION
| MNT_ROOTFS
)) == 0) &&
8223 ((fvp
->v_mount
->mnt_kern_flag
& MNTK_SYSTEM
) == 0) &&
8224 (fvp
->v_mount
->mnt_vnodecovered
!= NULLVP
)) {
8227 /* switch fvp to the covered vnode */
8228 coveredvp
= fvp
->v_mount
->mnt_vnodecovered
;
8229 if ((vnode_getwithref(coveredvp
))) {
8239 * Check for cross-device rename.
8241 if ((fvp
->v_mount
!= tdvp
->v_mount
) ||
8242 (tvp
&& (fvp
->v_mount
!= tvp
->v_mount
))) {
8248 * If source is the same as the destination (that is the
8249 * same inode number) then there is nothing to do...
8250 * EXCEPT if the underlying file system supports case
8251 * insensitivity and is case preserving. In this case
8252 * the file system needs to handle the special case of
8253 * getting the same vnode as target (fvp) and source (tvp).
8255 * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE
8256 * and _PC_CASE_PRESERVING can have this exception, and they need to
8257 * handle the special case of getting the same vnode as target and
8258 * source. NOTE: Then the target is unlocked going into vnop_rename,
8259 * so not to cause locking problems. There is a single reference on tvp.
8261 * NOTE - that fvp == tvp also occurs if they are hard linked and
8262 * that correct behaviour then is just to return success without doing
8265 * XXX filesystem should take care of this itself, perhaps...
8267 if (fvp
== tvp
&& fdvp
== tdvp
) {
8268 if (fromnd
->ni_cnd
.cn_namelen
== tond
->ni_cnd
.cn_namelen
&&
8269 !bcmp(fromnd
->ni_cnd
.cn_nameptr
, tond
->ni_cnd
.cn_nameptr
,
8270 fromnd
->ni_cnd
.cn_namelen
)) {
8275 if (holding_mntlock
&& fvp
->v_mount
!= locked_mp
) {
8277 * we're holding a reference and lock
8278 * on locked_mp, but it no longer matches
8279 * what we want to do... so drop our hold
8281 mount_unlock_renames(locked_mp
);
8282 mount_drop(locked_mp
, 0);
8283 holding_mntlock
= 0;
8285 if (tdvp
!= fdvp
&& fvp
->v_type
== VDIR
) {
8287 * serialize renames that re-shape
8288 * the tree... if holding_mntlock is
8289 * set, then we're ready to go...
8291 * first need to drop the iocounts
8292 * we picked up, second take the
8293 * lock to serialize the access,
8294 * then finally start the lookup
8295 * process over with the lock held
8297 if (!holding_mntlock
) {
8299 * need to grab a reference on
8300 * the mount point before we
8301 * drop all the iocounts... once
8302 * the iocounts are gone, the mount
8305 locked_mp
= fvp
->v_mount
;
8306 mount_ref(locked_mp
, 0);
8309 * nameidone has to happen before we vnode_put(tvp)
8310 * since it may need to release the fs_nodelock on the tvp
8320 * nameidone has to happen before we vnode_put(fdvp)
8321 * since it may need to release the fs_nodelock on the fvp
8328 mount_lock_renames(locked_mp
);
8329 holding_mntlock
= 1;
8335 * when we dropped the iocounts to take
8336 * the lock, we allowed the identity of
8337 * the various vnodes to change... if they did,
8338 * we may no longer be dealing with a rename
8339 * that reshapes the tree... once we're holding
8340 * the iocounts, the vnodes can't change type
8341 * so we're free to drop the lock at this point
8344 if (holding_mntlock
) {
8345 mount_unlock_renames(locked_mp
);
8346 mount_drop(locked_mp
, 0);
8347 holding_mntlock
= 0;
8351 // save these off so we can later verify that fvp is the same
8352 oname
= fvp
->v_name
;
8353 oparent
= fvp
->v_parent
;
8356 error
= vn_rename(fdvp
, &fvp
, &fromnd
->ni_cnd
, fvap
,
8357 tdvp
, &tvp
, &tond
->ni_cnd
, tvap
,
8360 if (holding_mntlock
) {
8362 * we can drop our serialization
8365 mount_unlock_renames(locked_mp
);
8366 mount_drop(locked_mp
, 0);
8367 holding_mntlock
= 0;
8370 if (error
== EDATALESS
) {
8372 * If we've been here before, something has gone
8373 * horribly wrong and we should just get out lest
8374 * we spiral around the drain forever.
8376 if (flags
& VFS_RENAME_DATALESS
) {
8382 * The object we're renaming is dataless (or has a
8383 * dataless descendent) and requires materialization
8384 * before the rename occurs. But we're holding the
8385 * mount point's rename lock, so it's not safe to
8388 * In this case, we release the lock, perform the
8389 * materialization, and start the whole thing over.
8391 error
= vnode_materialize_dataless_file(fvp
,
8392 NAMESPACE_HANDLER_RENAME_OP
);
8396 * The next time around we need to tell the
8397 * file system that the materializtaion has
8400 flags
|= VFS_RENAME_DATALESS
;
8405 if (error
== EKEEPLOOKING
) {
8406 if ((fromnd
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
8407 if ((tond
->ni_flag
& NAMEI_CONTLOOKUP
) == 0) {
8408 panic("EKEEPLOOKING without NAMEI_CONTLOOKUP on either ndp?");
8412 fromnd
->ni_vp
= fvp
;
8415 goto continue_lookup
;
8419 * We may encounter a race in the VNOP where the destination didn't
8420 * exist when we did the namei, but it does by the time we go and
8421 * try to create the entry. In this case, we should re-drive this rename
8422 * call from the top again. Currently, only HFS bubbles out ERECYCLE,
8423 * but other filesystems susceptible to this race could return it, too.
8425 if (error
== ERECYCLE
) {
8430 * For compound VNOPs, the authorization callback may return
8431 * ENOENT in case of racing hardlink lookups hitting the name
8432 * cache, redrive the lookup.
8434 if (batched
&& error
== ENOENT
) {
8435 if (retry_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
8444 /* call out to allow 3rd party notification of rename.
8445 * Ignore result of kauth_authorize_fileop call.
8447 kauth_authorize_fileop(vfs_context_ucred(ctx
),
8448 KAUTH_FILEOP_RENAME
,
8449 (uintptr_t)from_name
, (uintptr_t)to_name
);
8450 if (flags
& VFS_RENAME_SWAP
) {
8451 kauth_authorize_fileop(vfs_context_ucred(ctx
),
8452 KAUTH_FILEOP_RENAME
,
8453 (uintptr_t)to_name
, (uintptr_t)from_name
);
8457 if (from_name
!= NULL
&& to_name
!= NULL
) {
8458 if (from_truncated
|| to_truncated
) {
8459 // set it here since only the from_finfo gets reported up to user space
8460 from_finfo
.mode
|= FSE_TRUNCATED_PATH
;
8464 vnode_get_fse_info_from_vap(tvp
, &to_finfo
, tvap
);
8467 vnode_get_fse_info_from_vap(fvp
, &from_finfo
, fvap
);
8471 add_fsevent(FSE_RENAME
, ctx
,
8472 FSE_ARG_STRING
, from_len_no_firmlink
, from_name_no_firmlink
,
8473 FSE_ARG_FINFO
, &from_finfo
,
8474 FSE_ARG_STRING
, to_len_no_firmlink
, to_name_no_firmlink
,
8475 FSE_ARG_FINFO
, &to_finfo
,
8477 if (flags
& VFS_RENAME_SWAP
) {
8479 * Strictly speaking, swap is the equivalent of
8480 * *three* renames. FSEvents clients should only take
8481 * the events as a hint, so we only bother reporting
8484 add_fsevent(FSE_RENAME
, ctx
,
8485 FSE_ARG_STRING
, to_len_no_firmlink
, to_name_no_firmlink
,
8486 FSE_ARG_FINFO
, &to_finfo
,
8487 FSE_ARG_STRING
, from_len_no_firmlink
, from_name_no_firmlink
,
8488 FSE_ARG_FINFO
, &from_finfo
,
8492 add_fsevent(FSE_RENAME
, ctx
,
8493 FSE_ARG_STRING
, from_len_no_firmlink
, from_name_no_firmlink
,
8494 FSE_ARG_FINFO
, &from_finfo
,
8495 FSE_ARG_STRING
, to_len_no_firmlink
, to_name_no_firmlink
,
8499 #endif /* CONFIG_FSE */
8502 * update filesystem's mount point data
8505 char *cp
, *pathend
, *mpname
;
8511 mp
= fvp
->v_mountedhere
;
8513 if (vfs_busy(mp
, LK_NOWAIT
)) {
8517 MALLOC_ZONE(tobuf
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
8519 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
8520 error
= copyinstr(to
, tobuf
, MAXPATHLEN
, &len
);
8522 error
= copystr((void *)to
, tobuf
, MAXPATHLEN
, &len
);
8525 /* find current mount point prefix */
8526 pathend
= &mp
->mnt_vfsstat
.f_mntonname
[0];
8527 for (cp
= pathend
; *cp
!= '\0'; ++cp
) {
8532 /* find last component of target name */
8533 for (mpname
= cp
= tobuf
; *cp
!= '\0'; ++cp
) {
8539 /* Update f_mntonname of sub mounts */
8540 vfs_iterate(0, rename_submounts_callback
, (void *)mp
);
8542 /* append name to prefix */
8543 maxlen
= MAXPATHLEN
- (pathend
- mp
->mnt_vfsstat
.f_mntonname
);
8544 bzero(pathend
, maxlen
);
8546 strlcpy(pathend
, mpname
, maxlen
);
8548 FREE_ZONE(tobuf
, MAXPATHLEN
, M_NAMEI
);
8552 vfs_event_signal(NULL
, VQ_UPDATE
, (intptr_t)NULL
);
8555 * fix up name & parent pointers. note that we first
8556 * check that fvp has the same name/parent pointers it
8557 * had before the rename call... this is a 'weak' check
8560 * XXX oparent and oname may not be set in the compound vnop case
8562 if (batched
|| (oname
== fvp
->v_name
&& oparent
== fvp
->v_parent
)) {
8565 update_flags
= VNODE_UPDATE_NAME
;
8568 update_flags
|= VNODE_UPDATE_PARENT
;
8571 vnode_update_identity(fvp
, tdvp
, tond
->ni_cnd
.cn_nameptr
, tond
->ni_cnd
.cn_namelen
, tond
->ni_cnd
.cn_hash
, update_flags
);
8574 if (to_name
!= NULL
) {
8575 RELEASE_PATH(to_name
);
8578 if (to_name_no_firmlink
!= NULL
) {
8579 RELEASE_PATH(to_name_no_firmlink
);
8580 to_name_no_firmlink
= NULL
;
8582 if (from_name
!= NULL
) {
8583 RELEASE_PATH(from_name
);
8586 if (from_name_no_firmlink
!= NULL
) {
8587 RELEASE_PATH(from_name_no_firmlink
);
8588 from_name_no_firmlink
= NULL
;
8590 if (holding_mntlock
) {
8591 mount_unlock_renames(locked_mp
);
8592 mount_drop(locked_mp
, 0);
8593 holding_mntlock
= 0;
8597 * nameidone has to happen before we vnode_put(tdvp)
8598 * since it may need to release the fs_nodelock on the tdvp
8609 * nameidone has to happen before we vnode_put(fdvp)
8610 * since it may need to release the fs_nodelock on the fdvp
8621 * If things changed after we did the namei, then we will re-drive
8622 * this rename call from the top.
8629 FREE(__rename_data
, M_TEMP
);
8634 rename(__unused proc_t p
, struct rename_args
*uap
, __unused
int32_t *retval
)
8636 return renameat_internal(vfs_context_current(), AT_FDCWD
, uap
->from
,
8637 AT_FDCWD
, uap
->to
, UIO_USERSPACE
, 0);
8641 renameatx_np(__unused proc_t p
, struct renameatx_np_args
*uap
, __unused
int32_t *retval
)
8643 return renameat_internal(
8644 vfs_context_current(),
8645 uap
->fromfd
, uap
->from
,
8647 UIO_USERSPACE
, uap
->flags
);
8651 renameat(__unused proc_t p
, struct renameat_args
*uap
, __unused
int32_t *retval
)
8653 return renameat_internal(vfs_context_current(), uap
->fromfd
, uap
->from
,
8654 uap
->tofd
, uap
->to
, UIO_USERSPACE
, 0);
8658 * Make a directory file.
8660 * Returns: 0 Success
8663 * vnode_authorize:???
8668 mkdir1at(vfs_context_t ctx
, user_addr_t path
, struct vnode_attr
*vap
, int fd
,
8669 enum uio_seg segflg
)
8673 int update_flags
= 0;
8675 struct nameidata nd
;
8677 AUDIT_ARG(mode
, vap
->va_mode
);
8678 NDINIT(&nd
, CREATE
, OP_MKDIR
, LOCKPARENT
| AUDITVNPATH1
, segflg
,
8680 nd
.ni_cnd
.cn_flags
|= WILLBEDIR
;
8681 nd
.ni_flag
= NAMEI_COMPOUNDMKDIR
;
8684 error
= nameiat(&nd
, fd
);
8696 batched
= vnode_compound_mkdir_available(dvp
);
8698 VATTR_SET(vap
, va_type
, VDIR
);
8702 * Don't authorize in VFS for compound VNOP.... mkdir -p today assumes that it will
8703 * only get EXISTS or EISDIR for existing path components, and not that it could see
8704 * EACCESS/EPERM--so if we authorize for mkdir on "/" for "mkdir -p /tmp/foo/bar/baz"
8705 * it will fail in a spurious manner. Need to figure out if this is valid behavior.
8707 if ((error
= vn_authorize_mkdir(dvp
, &nd
.ni_cnd
, vap
, ctx
, NULL
)) != 0) {
8708 if (error
== EACCES
|| error
== EPERM
) {
8716 * Try a lookup without "NAMEI_COMPOUNDVNOP" to make sure we return EEXIST
8717 * rather than EACCESS if the target exists.
8719 NDINIT(&nd
, LOOKUP
, OP_MKDIR
, AUDITVNPATH1
, segflg
,
8721 error2
= nameiat(&nd
, fd
);
8735 * make the directory
8737 if ((error
= vn_create(dvp
, &vp
, &nd
, vap
, 0, 0, NULL
, ctx
)) != 0) {
8738 if (error
== EKEEPLOOKING
) {
8740 goto continue_lookup
;
8746 // Make sure the name & parent pointers are hooked up
8747 if (vp
->v_name
== NULL
) {
8748 update_flags
|= VNODE_UPDATE_NAME
;
8750 if (vp
->v_parent
== NULLVP
) {
8751 update_flags
|= VNODE_UPDATE_PARENT
;
8755 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
8759 add_fsevent(FSE_CREATE_DIR
, ctx
, FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
8764 * nameidone has to happen before we vnode_put(dvp)
8765 * since it may need to release the fs_nodelock on the dvp
8780 * mkdir_extended: Create a directory; with extended security (ACL).
8782 * Parameters: p Process requesting to create the directory
8783 * uap User argument descriptor (see below)
8786 * Indirect: uap->path Path of directory to create
8787 * uap->mode Access permissions to set
8788 * uap->xsecurity ACL to set
8790 * Returns: 0 Success
8795 mkdir_extended(proc_t p
, struct mkdir_extended_args
*uap
, __unused
int32_t *retval
)
8798 kauth_filesec_t xsecdst
;
8799 struct vnode_attr va
;
8801 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
8804 if ((uap
->xsecurity
!= USER_ADDR_NULL
) &&
8805 ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)) {
8810 VATTR_SET(&va
, va_mode
, (uap
->mode
& ACCESSPERMS
) & ~p
->p_fd
->fd_cmask
);
8811 if (xsecdst
!= NULL
) {
8812 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
8815 ciferror
= mkdir1at(vfs_context_current(), uap
->path
, &va
, AT_FDCWD
,
8817 if (xsecdst
!= NULL
) {
8818 kauth_filesec_free(xsecdst
);
8824 mkdir(proc_t p
, struct mkdir_args
*uap
, __unused
int32_t *retval
)
8826 struct vnode_attr va
;
8829 VATTR_SET(&va
, va_mode
, (uap
->mode
& ACCESSPERMS
) & ~p
->p_fd
->fd_cmask
);
8831 return mkdir1at(vfs_context_current(), uap
->path
, &va
, AT_FDCWD
,
8836 mkdirat(proc_t p
, struct mkdirat_args
*uap
, __unused
int32_t *retval
)
8838 struct vnode_attr va
;
8841 VATTR_SET(&va
, va_mode
, (uap
->mode
& ACCESSPERMS
) & ~p
->p_fd
->fd_cmask
);
8843 return mkdir1at(vfs_context_current(), uap
->path
, &va
, uap
->fd
,
8848 rmdirat_internal(vfs_context_t ctx
, int fd
, user_addr_t dirpath
,
8849 enum uio_seg segflg
, int unlink_flags
)
8853 struct nameidata nd
;
8855 char *no_firmlink_path
= NULL
;
8857 int len_no_firmlink_path
= 0;
8858 int has_listeners
= 0;
8860 int truncated_path
= 0;
8861 int truncated_no_firmlink_path
= 0;
8863 struct vnode_attr va
;
8864 #endif /* CONFIG_FSE */
8865 struct vnode_attr
*vap
= NULL
;
8866 int restart_count
= 0;
8872 * This loop exists to restart rmdir in the unlikely case that two
8873 * processes are simultaneously trying to remove the same directory
8874 * containing orphaned appleDouble files.
8877 NDINIT(&nd
, DELETE
, OP_RMDIR
, LOCKPARENT
| AUDITVNPATH1
,
8878 segflg
, dirpath
, ctx
);
8879 nd
.ni_flag
= NAMEI_COMPOUNDRMDIR
;
8884 error
= nameiat(&nd
, fd
);
8893 batched
= vnode_compound_rmdir_available(vp
);
8895 if (vp
->v_flag
& VROOT
) {
8897 * The root of a mounted filesystem cannot be deleted.
8903 #if DEVELOPMENT || DEBUG
8905 * XXX VSWAP: Check for entitlements or special flag here
8906 * so we can restrict access appropriately.
8908 #else /* DEVELOPMENT || DEBUG */
8910 if (vnode_isswap(vp
) && (ctx
!= vfs_context_kernel())) {
8914 #endif /* DEVELOPMENT || DEBUG */
8917 * Removed a check here; we used to abort if vp's vid
8918 * was not the same as what we'd seen the last time around.
8919 * I do not think that check was valid, because if we retry
8920 * and all dirents are gone, the directory could legitimately
8921 * be recycled but still be present in a situation where we would
8922 * have had permission to delete. Therefore, we won't make
8923 * an effort to preserve that check now that we may not have a
8928 error
= vn_authorize_rmdir(dvp
, vp
, &nd
.ni_cnd
, ctx
, NULL
);
8930 if (error
== ENOENT
) {
8931 if (restart_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
8942 if (!vnode_compound_rmdir_available(dvp
)) {
8943 panic("No error, but no compound rmdir?");
8950 need_event
= need_fsevent(FSE_DELETE
, dvp
);
8953 get_fse_info(vp
, &finfo
, ctx
);
8955 error
= vfs_get_notify_attributes(&va
);
8964 has_listeners
= kauth_authorize_fileop_has_listeners();
8965 if (need_event
|| has_listeners
) {
8974 len_path
= safe_getpath(dvp
, nd
.ni_cnd
.cn_nameptr
, path
, MAXPATHLEN
, &truncated_path
);
8976 if (no_firmlink_path
== NULL
) {
8977 GET_PATH(no_firmlink_path
);
8978 if (no_firmlink_path
== NULL
) {
8984 len_no_firmlink_path
= safe_getpath_no_firmlink(dvp
, nd
.ni_cnd
.cn_nameptr
, no_firmlink_path
, MAXPATHLEN
, &truncated_no_firmlink_path
);
8986 if (truncated_no_firmlink_path
) {
8987 finfo
.mode
|= FSE_TRUNCATED_PATH
;
8992 error
= vn_rmdir(dvp
, &vp
, &nd
, vap
, ctx
);
8995 /* Couldn't find a vnode */
8999 if (error
== EKEEPLOOKING
) {
9000 goto continue_lookup
;
9001 } else if (batched
&& error
== ENOENT
) {
9002 if (restart_count
< MAX_AUTHORIZE_ENOENT_RETRIES
) {
9004 * For compound VNOPs, the authorization callback
9005 * may return ENOENT in case of racing hard link lookups
9006 * redrive the lookup.
9015 * XXX There's no provision for passing flags
9016 * to VNOP_RMDIR(). So, if vn_rmdir() fails
9017 * because it's not empty, then we try again
9018 * with VNOP_REMOVE(), passing in a special
9019 * flag that clever file systems will know
9022 if (error
== ENOTEMPTY
&&
9023 (unlink_flags
& VNODE_REMOVE_DATALESS_DIR
) != 0) {
9025 * If this fails, we want to keep the original
9028 if (vn_remove(dvp
, &vp
, &nd
,
9029 VNODE_REMOVE_DATALESS_DIR
, vap
, ctx
) == 0) {
9034 #if CONFIG_APPLEDOUBLE
9036 * Special case to remove orphaned AppleDouble
9037 * files. I don't like putting this in the kernel,
9038 * but carbon does not like putting this in carbon either,
9041 if (error
== ENOTEMPTY
) {
9042 int ad_error
= rmdir_remove_orphaned_appleDouble(vp
, ctx
, &restart_flag
);
9043 if (ad_error
== EBUSY
) {
9050 * Assuming everything went well, we will try the RMDIR again
9053 error
= vn_rmdir(dvp
, &vp
, &nd
, vap
, ctx
);
9056 #endif /* CONFIG_APPLEDOUBLE */
9058 * Call out to allow 3rd party notification of delete.
9059 * Ignore result of kauth_authorize_fileop call.
9062 if (has_listeners
) {
9063 kauth_authorize_fileop(vfs_context_ucred(ctx
),
9064 KAUTH_FILEOP_DELETE
,
9069 if (vp
->v_flag
& VISHARDLINK
) {
9070 // see the comment in unlink1() about why we update
9071 // the parent of a hard link when it is removed
9072 vnode_update_identity(vp
, NULL
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
9078 vnode_get_fse_info_from_vap(vp
, &finfo
, vap
);
9080 add_fsevent(FSE_DELETE
, ctx
,
9081 FSE_ARG_STRING
, len_no_firmlink_path
, no_firmlink_path
,
9082 FSE_ARG_FINFO
, &finfo
,
9094 if (no_firmlink_path
!= NULL
) {
9095 RELEASE_PATH(no_firmlink_path
);
9096 no_firmlink_path
= NULL
;
9100 * nameidone has to happen before we vnode_put(dvp)
9101 * since it may need to release the fs_nodelock on the dvp
9110 if (restart_flag
== 0) {
9111 wakeup_one((caddr_t
)vp
);
9114 tsleep(vp
, PVFS
, "rm AD", 1);
9115 } while (restart_flag
!= 0);
9121 * Remove a directory file.
9125 rmdir(__unused proc_t p
, struct rmdir_args
*uap
, __unused
int32_t *retval
)
9127 return rmdirat_internal(vfs_context_current(), AT_FDCWD
,
9128 CAST_USER_ADDR_T(uap
->path
), UIO_USERSPACE
, 0);
9131 /* Get direntry length padded to 8 byte alignment */
9132 #define DIRENT64_LEN(namlen) \
9133 ((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 7) & ~7)
9135 /* Get dirent length padded to 4 byte alignment */
9136 #define DIRENT_LEN(namelen) \
9137 ((sizeof(struct dirent) + (namelen + 1) - (__DARWIN_MAXNAMLEN + 1) + 3) & ~3)
9139 /* Get the end of this dirent */
9140 #define DIRENT_END(dep) \
9141 (((char *)(dep)) + (dep)->d_reclen - 1)
9144 vnode_readdir64(struct vnode
*vp
, struct uio
*uio
, int flags
, int *eofflag
,
9145 int *numdirent
, vfs_context_t ctxp
)
9147 /* Check if fs natively supports VNODE_READDIR_EXTENDED */
9148 if ((vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSREADDIR_EXTENDED
) &&
9149 ((vp
->v_mount
->mnt_kern_flag
& MNTK_DENY_READDIREXT
) == 0)) {
9150 return VNOP_READDIR(vp
, uio
, flags
, eofflag
, numdirent
, ctxp
);
9155 struct direntry
*entry64
;
9161 * We're here because the underlying file system does not
9162 * support direnties or we mounted denying support so we must
9163 * fall back to dirents and convert them to direntries.
9165 * Our kernel buffer needs to be smaller since re-packing will
9166 * expand each dirent. The worse case (when the name length
9167 * is 3 or less) corresponds to a struct direntry size of 32
9168 * bytes (8-byte aligned) and a struct dirent size of 12 bytes
9169 * (4-byte aligned). So having a buffer that is 3/8 the size
9170 * will prevent us from reading more than we can pack.
9172 * Since this buffer is wired memory, we will limit the
9173 * buffer size to a maximum of 32K. We would really like to
9174 * use 32K in the MIN(), but we use magic number 87371 to
9175 * prevent uio_resid() * 3 / 8 from overflowing.
9177 bufsize
= 3 * MIN((user_size_t
)uio_resid(uio
), 87371u) / 8;
9178 MALLOC(bufptr
, void *, bufsize
, M_TEMP
, M_WAITOK
);
9179 if (bufptr
== NULL
) {
9183 auio
= uio_create(1, 0, UIO_SYSSPACE
, UIO_READ
);
9184 uio_addiov(auio
, (uintptr_t)bufptr
, bufsize
);
9185 auio
->uio_offset
= uio
->uio_offset
;
9187 error
= VNOP_READDIR(vp
, auio
, 0, eofflag
, numdirent
, ctxp
);
9189 dep
= (struct dirent
*)bufptr
;
9190 bytesread
= bufsize
- uio_resid(auio
);
9192 MALLOC(entry64
, struct direntry
*, sizeof(struct direntry
),
9195 * Convert all the entries and copy them out to user's buffer.
9197 while (error
== 0 && (char *)dep
< ((char *)bufptr
+ bytesread
)) {
9198 size_t enbufsize
= DIRENT64_LEN(dep
->d_namlen
);
9200 if (DIRENT_END(dep
) > ((char *)bufptr
+ bytesread
) ||
9201 DIRENT_LEN(dep
->d_namlen
) > dep
->d_reclen
) {
9202 printf("%s: %s: Bad dirent recived from directory %s\n", __func__
,
9203 vp
->v_mount
->mnt_vfsstat
.f_mntonname
,
9204 vp
->v_name
? vp
->v_name
: "<unknown>");
9209 bzero(entry64
, enbufsize
);
9210 /* Convert a dirent to a dirent64. */
9211 entry64
->d_ino
= dep
->d_ino
;
9212 entry64
->d_seekoff
= 0;
9213 entry64
->d_reclen
= enbufsize
;
9214 entry64
->d_namlen
= dep
->d_namlen
;
9215 entry64
->d_type
= dep
->d_type
;
9216 bcopy(dep
->d_name
, entry64
->d_name
, dep
->d_namlen
+ 1);
9218 /* Move to next entry. */
9219 dep
= (struct dirent
*)((char *)dep
+ dep
->d_reclen
);
9221 /* Copy entry64 to user's buffer. */
9222 error
= uiomove((caddr_t
)entry64
, entry64
->d_reclen
, uio
);
9225 /* Update the real offset using the offset we got from VNOP_READDIR. */
9227 uio
->uio_offset
= auio
->uio_offset
;
9230 FREE(bufptr
, M_TEMP
);
9231 FREE(entry64
, M_TEMP
);
9236 #define GETDIRENTRIES_MAXBUFSIZE (128 * 1024 * 1024U)
9239 * Read a block of directory entries in a file system independent format.
9242 getdirentries_common(int fd
, user_addr_t bufp
, user_size_t bufsize
, ssize_t
*bytesread
,
9243 off_t
*offset
, int *eofflag
, int flags
)
9246 struct vfs_context context
= *vfs_context_current(); /* local copy */
9247 struct fileproc
*fp
;
9249 int spacetype
= proc_is64bit(vfs_context_proc(&context
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
9251 int error
, numdirent
;
9252 char uio_buf
[UIO_SIZEOF(1)];
9254 error
= fp_getfvp(vfs_context_proc(&context
), fd
, &fp
, &vp
);
9258 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
9259 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
9264 if (bufsize
> GETDIRENTRIES_MAXBUFSIZE
) {
9265 bufsize
= GETDIRENTRIES_MAXBUFSIZE
;
9269 error
= mac_file_check_change_offset(vfs_context_ucred(&context
), fp
->f_fglob
);
9274 if ((error
= vnode_getwithref(vp
))) {
9277 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
9280 if (vp
->v_type
!= VDIR
) {
9281 (void)vnode_put(vp
);
9287 error
= mac_vnode_check_readdir(&context
, vp
);
9289 (void)vnode_put(vp
);
9294 loff
= fp
->f_fglob
->fg_offset
;
9295 auio
= uio_createwithbuffer(1, loff
, spacetype
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
9296 uio_addiov(auio
, bufp
, bufsize
);
9298 if (flags
& VNODE_READDIR_EXTENDED
) {
9299 error
= vnode_readdir64(vp
, auio
, flags
, eofflag
, &numdirent
, &context
);
9300 fp
->f_fglob
->fg_offset
= uio_offset(auio
);
9302 error
= VNOP_READDIR(vp
, auio
, 0, eofflag
, &numdirent
, &context
);
9303 fp
->f_fglob
->fg_offset
= uio_offset(auio
);
9306 (void)vnode_put(vp
);
9310 if ((user_ssize_t
)bufsize
== uio_resid(auio
)) {
9311 if (union_dircheckp
) {
9312 error
= union_dircheckp(&vp
, fp
, &context
);
9317 (void)vnode_put(vp
);
9322 if ((vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
9323 struct vnode
*tvp
= vp
;
9324 if (lookup_traverse_union(tvp
, &vp
, &context
) == 0) {
9326 fp
->f_fglob
->fg_data
= (caddr_t
) vp
;
9327 fp
->f_fglob
->fg_offset
= 0;
9341 *bytesread
= bufsize
- uio_resid(auio
);
9349 getdirentries(__unused
struct proc
*p
, struct getdirentries_args
*uap
, int32_t *retval
)
9355 AUDIT_ARG(fd
, uap
->fd
);
9356 error
= getdirentries_common(uap
->fd
, uap
->buf
, uap
->count
,
9357 &bytesread
, &offset
, &eofflag
, 0);
9360 if (proc_is64bit(p
)) {
9361 user64_long_t base
= (user64_long_t
)offset
;
9362 error
= copyout((caddr_t
)&base
, uap
->basep
, sizeof(user64_long_t
));
9364 user32_long_t base
= (user32_long_t
)offset
;
9365 error
= copyout((caddr_t
)&base
, uap
->basep
, sizeof(user32_long_t
));
9367 *retval
= bytesread
;
9373 getdirentries64(__unused
struct proc
*p
, struct getdirentries64_args
*uap
, user_ssize_t
*retval
)
9378 user_size_t bufsize
;
9380 AUDIT_ARG(fd
, uap
->fd
);
9383 * If the buffer is at least GETDIRENTRIES64_EXTENDED_BUFSIZE large,
9384 * then the kernel carves out the last 4 bytes to return extended
9385 * information to userspace (namely whether we reached EOF with this call).
9387 if (uap
->bufsize
>= GETDIRENTRIES64_EXTENDED_BUFSIZE
) {
9388 bufsize
= uap
->bufsize
- sizeof(getdirentries64_flags_t
);
9390 bufsize
= uap
->bufsize
;
9393 error
= getdirentries_common(uap
->fd
, uap
->buf
, bufsize
,
9394 &bytesread
, &offset
, &eofflag
, VNODE_READDIR_EXTENDED
);
9397 *retval
= bytesread
;
9398 error
= copyout((caddr_t
)&offset
, uap
->position
, sizeof(off_t
));
9400 if (error
== 0 && uap
->bufsize
>= GETDIRENTRIES64_EXTENDED_BUFSIZE
) {
9401 getdirentries64_flags_t flags
= 0;
9403 flags
|= GETDIRENTRIES64_EOF
;
9405 error
= copyout(&flags
, (user_addr_t
)uap
->buf
+ bufsize
,
9414 * Set the mode mask for creation of filesystem nodes.
9415 * XXX implement xsecurity
9417 #define UMASK_NOXSECURITY (void *)1 /* leave existing xsecurity alone */
9419 umask1(proc_t p
, int newmask
, __unused kauth_filesec_t fsec
, int32_t *retval
)
9421 struct filedesc
*fdp
;
9423 AUDIT_ARG(mask
, newmask
);
9426 *retval
= fdp
->fd_cmask
;
9427 fdp
->fd_cmask
= newmask
& ALLPERMS
;
9433 * umask_extended: Set the mode mask for creation of filesystem nodes; with extended security (ACL).
9435 * Parameters: p Process requesting to set the umask
9436 * uap User argument descriptor (see below)
9437 * retval umask of the process (parameter p)
9439 * Indirect: uap->newmask umask to set
9440 * uap->xsecurity ACL to set
9442 * Returns: 0 Success
9447 umask_extended(proc_t p
, struct umask_extended_args
*uap
, int32_t *retval
)
9450 kauth_filesec_t xsecdst
;
9452 xsecdst
= KAUTH_FILESEC_NONE
;
9453 if (uap
->xsecurity
!= USER_ADDR_NULL
) {
9454 if ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0) {
9458 xsecdst
= KAUTH_FILESEC_NONE
;
9461 ciferror
= umask1(p
, uap
->newmask
, xsecdst
, retval
);
9463 if (xsecdst
!= KAUTH_FILESEC_NONE
) {
9464 kauth_filesec_free(xsecdst
);
9470 umask(proc_t p
, struct umask_args
*uap
, int32_t *retval
)
9472 return umask1(p
, uap
->newmask
, UMASK_NOXSECURITY
, retval
);
9476 * Void all references to file by ripping underlying filesystem
9481 revoke(proc_t p
, struct revoke_args
*uap
, __unused
int32_t *retval
)
9484 struct vnode_attr va
;
9485 vfs_context_t ctx
= vfs_context_current();
9487 struct nameidata nd
;
9489 NDINIT(&nd
, LOOKUP
, OP_REVOKE
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
9499 if (!(vnode_ischr(vp
) || vnode_isblk(vp
))) {
9504 if (vnode_isblk(vp
) && vnode_ismountedon(vp
)) {
9510 error
= mac_vnode_check_revoke(ctx
, vp
);
9517 VATTR_WANTED(&va
, va_uid
);
9518 if ((error
= vnode_getattr(vp
, &va
, ctx
))) {
9521 if (kauth_cred_getuid(vfs_context_ucred(ctx
)) != va
.va_uid
&&
9522 (error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
))) {
9525 if (vp
->v_usecount
> 0 || (vnode_isaliased(vp
))) {
9526 VNOP_REVOKE(vp
, REVOKEALL
, ctx
);
9535 * HFS/HFS PlUS SPECIFIC SYSTEM CALLS
9536 * The following system calls are designed to support features
9537 * which are specific to the HFS & HFS Plus volume formats
9542 * Obtain attribute information on objects in a directory while enumerating
9547 getdirentriesattr(proc_t p
, struct getdirentriesattr_args
*uap
, int32_t *retval
)
9550 struct fileproc
*fp
;
9552 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
9553 uint32_t count
= 0, savecount
= 0;
9554 uint32_t newstate
= 0;
9557 struct attrlist attributelist
;
9558 vfs_context_t ctx
= vfs_context_current();
9560 char uio_buf
[UIO_SIZEOF(1)];
9561 kauth_action_t action
;
9565 /* Get the attributes into kernel space */
9566 if ((error
= copyin(uap
->alist
, (caddr_t
)&attributelist
, sizeof(attributelist
)))) {
9569 if ((error
= copyin(uap
->count
, (caddr_t
)&count
, sizeof(count
)))) {
9573 if ((error
= fp_getfvp(p
, fd
, &fp
, &vp
))) {
9576 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
9577 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
9584 error
= mac_file_check_change_offset(vfs_context_ucred(ctx
),
9592 if ((error
= vnode_getwithref(vp
))) {
9596 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
9599 if (vp
->v_type
!= VDIR
) {
9600 (void)vnode_put(vp
);
9606 error
= mac_vnode_check_readdir(ctx
, vp
);
9608 (void)vnode_put(vp
);
9613 /* set up the uio structure which will contain the users return buffer */
9614 loff
= fp
->f_fglob
->fg_offset
;
9615 auio
= uio_createwithbuffer(1, loff
, spacetype
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
9616 uio_addiov(auio
, uap
->buffer
, uap
->buffersize
);
9619 * If the only item requested is file names, we can let that past with
9620 * just LIST_DIRECTORY. If they want any other attributes, that means
9621 * they need SEARCH as well.
9623 action
= KAUTH_VNODE_LIST_DIRECTORY
;
9624 if ((attributelist
.commonattr
& ~ATTR_CMN_NAME
) ||
9625 attributelist
.fileattr
|| attributelist
.dirattr
) {
9626 action
|= KAUTH_VNODE_SEARCH
;
9629 if ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) == 0) {
9630 /* Believe it or not, uap->options only has 32-bits of valid
9631 * info, so truncate before extending again */
9633 error
= VNOP_READDIRATTR(vp
, &attributelist
, auio
, count
,
9634 (u_long
)(uint32_t)uap
->options
, &newstate
, &eofflag
, &count
, ctx
);
9638 (void) vnode_put(vp
);
9643 * If we've got the last entry of a directory in a union mount
9644 * then reset the eofflag and pretend there's still more to come.
9645 * The next call will again set eofflag and the buffer will be empty,
9646 * so traverse to the underlying directory and do the directory
9649 if (eofflag
&& vp
->v_mount
->mnt_flag
& MNT_UNION
) {
9650 if (uio_resid(auio
) < (user_ssize_t
) uap
->buffersize
) { // Got some entries
9652 } else { // Empty buffer
9653 struct vnode
*tvp
= vp
;
9654 if (lookup_traverse_union(tvp
, &vp
, ctx
) == 0) {
9655 vnode_ref_ext(vp
, fp
->f_fglob
->fg_flag
& O_EVTONLY
, 0);
9656 fp
->f_fglob
->fg_data
= (caddr_t
) vp
;
9657 fp
->f_fglob
->fg_offset
= 0; // reset index for new dir
9659 vnode_rele_internal(tvp
, fp
->f_fglob
->fg_flag
& O_EVTONLY
, 0, 0);
9667 (void)vnode_put(vp
);
9672 fp
->f_fglob
->fg_offset
= uio_offset(auio
); /* should be multiple of dirent, not variable */
9674 if ((error
= copyout((caddr_t
) &count
, uap
->count
, sizeof(count
)))) {
9677 if ((error
= copyout((caddr_t
) &newstate
, uap
->newstate
, sizeof(newstate
)))) {
9680 if ((error
= copyout((caddr_t
) &loff
, uap
->basep
, sizeof(loff
)))) {
9684 *retval
= eofflag
; /* similar to getdirentries */
9688 return error
; /* return error earlier, an retval of 0 or 1 now */
9689 } /* end of getdirentriesattr system call */
9692 * Exchange data between two files
9697 exchangedata(__unused proc_t p
, struct exchangedata_args
*uap
, __unused
int32_t *retval
)
9699 struct nameidata fnd
, snd
;
9700 vfs_context_t ctx
= vfs_context_current();
9704 u_int32_t nameiflags
;
9707 int flen
= 0, slen
= 0;
9708 int from_truncated
= 0, to_truncated
= 0;
9710 fse_info f_finfo
, s_finfo
;
9714 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) {
9715 nameiflags
|= FOLLOW
;
9718 NDINIT(&fnd
, LOOKUP
, OP_EXCHANGEDATA
, nameiflags
| AUDITVNPATH1
,
9719 UIO_USERSPACE
, uap
->path1
, ctx
);
9721 error
= namei(&fnd
);
9729 NDINIT(&snd
, LOOKUP
, OP_EXCHANGEDATA
, CN_NBMOUNTLOOK
| nameiflags
| AUDITVNPATH2
,
9730 UIO_USERSPACE
, uap
->path2
, ctx
);
9732 error
= namei(&snd
);
9741 * if the files are the same, return an inval error
9749 * if the files are on different volumes, return an error
9751 if (svp
->v_mount
!= fvp
->v_mount
) {
9756 /* If they're not files, return an error */
9757 if ((vnode_isreg(fvp
) == 0) || (vnode_isreg(svp
) == 0)) {
9763 error
= mac_vnode_check_exchangedata(ctx
,
9769 if (((error
= vnode_authorize(fvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, ctx
)) != 0) ||
9770 ((error
= vnode_authorize(svp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, ctx
)) != 0)) {
9776 need_fsevent(FSE_EXCHANGE
, fvp
) ||
9778 kauth_authorize_fileop_has_listeners()) {
9781 if (fpath
== NULL
|| spath
== NULL
) {
9786 flen
= safe_getpath(fvp
, NULL
, fpath
, MAXPATHLEN
, &from_truncated
);
9787 slen
= safe_getpath(svp
, NULL
, spath
, MAXPATHLEN
, &to_truncated
);
9790 get_fse_info(fvp
, &f_finfo
, ctx
);
9791 get_fse_info(svp
, &s_finfo
, ctx
);
9792 if (from_truncated
|| to_truncated
) {
9793 // set it here since only the f_finfo gets reported up to user space
9794 f_finfo
.mode
|= FSE_TRUNCATED_PATH
;
9798 /* Ok, make the call */
9799 error
= VNOP_EXCHANGE(fvp
, svp
, 0, ctx
);
9802 const char *tmpname
;
9804 if (fpath
!= NULL
&& spath
!= NULL
) {
9805 /* call out to allow 3rd party notification of exchangedata.
9806 * Ignore result of kauth_authorize_fileop call.
9808 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_EXCHANGE
,
9809 (uintptr_t)fpath
, (uintptr_t)spath
);
9813 tmpname
= fvp
->v_name
;
9814 fvp
->v_name
= svp
->v_name
;
9815 svp
->v_name
= tmpname
;
9817 if (fvp
->v_parent
!= svp
->v_parent
) {
9820 tmp
= fvp
->v_parent
;
9821 fvp
->v_parent
= svp
->v_parent
;
9822 svp
->v_parent
= tmp
;
9824 name_cache_unlock();
9827 if (fpath
!= NULL
&& spath
!= NULL
) {
9828 add_fsevent(FSE_EXCHANGE
, ctx
,
9829 FSE_ARG_STRING
, flen
, fpath
,
9830 FSE_ARG_FINFO
, &f_finfo
,
9831 FSE_ARG_STRING
, slen
, spath
,
9832 FSE_ARG_FINFO
, &s_finfo
,
9839 if (fpath
!= NULL
) {
9840 RELEASE_PATH(fpath
);
9842 if (spath
!= NULL
) {
9843 RELEASE_PATH(spath
);
9852 * Return (in MB) the amount of freespace on the given vnode's volume.
9854 uint32_t freespace_mb(vnode_t vp
);
9857 freespace_mb(vnode_t vp
)
9859 vfs_update_vfsstat(vp
->v_mount
, vfs_context_current(), VFS_USER_EVENT
);
9860 return ((uint64_t)vp
->v_mount
->mnt_vfsstat
.f_bavail
*
9861 vp
->v_mount
->mnt_vfsstat
.f_bsize
) >> 20;
9869 searchfs(proc_t p
, struct searchfs_args
*uap
, __unused
int32_t *retval
)
9874 struct nameidata nd
;
9875 struct user64_fssearchblock searchblock
;
9876 struct searchstate
*state
;
9877 struct attrlist
*returnattrs
;
9878 struct timeval timelimit
;
9879 void *searchparams1
, *searchparams2
;
9881 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
9882 uint32_t nummatches
;
9884 uint32_t nameiflags
;
9885 vfs_context_t ctx
= vfs_context_current();
9886 char uio_buf
[UIO_SIZEOF(1)];
9888 /* Start by copying in fsearchblock parameter list */
9889 if (IS_64BIT_PROCESS(p
)) {
9890 error
= copyin(uap
->searchblock
, (caddr_t
) &searchblock
, sizeof(searchblock
));
9891 timelimit
.tv_sec
= searchblock
.timelimit
.tv_sec
;
9892 timelimit
.tv_usec
= searchblock
.timelimit
.tv_usec
;
9894 struct user32_fssearchblock tmp_searchblock
;
9896 error
= copyin(uap
->searchblock
, (caddr_t
) &tmp_searchblock
, sizeof(tmp_searchblock
));
9897 // munge into 64-bit version
9898 searchblock
.returnattrs
= CAST_USER_ADDR_T(tmp_searchblock
.returnattrs
);
9899 searchblock
.returnbuffer
= CAST_USER_ADDR_T(tmp_searchblock
.returnbuffer
);
9900 searchblock
.returnbuffersize
= tmp_searchblock
.returnbuffersize
;
9901 searchblock
.maxmatches
= tmp_searchblock
.maxmatches
;
9903 * These casts are safe. We will promote the tv_sec into a 64 bit long if necessary
9904 * from a 32 bit long, and tv_usec is already a signed 32 bit int.
9906 timelimit
.tv_sec
= (__darwin_time_t
) tmp_searchblock
.timelimit
.tv_sec
;
9907 timelimit
.tv_usec
= (__darwin_useconds_t
) tmp_searchblock
.timelimit
.tv_usec
;
9908 searchblock
.searchparams1
= CAST_USER_ADDR_T(tmp_searchblock
.searchparams1
);
9909 searchblock
.sizeofsearchparams1
= tmp_searchblock
.sizeofsearchparams1
;
9910 searchblock
.searchparams2
= CAST_USER_ADDR_T(tmp_searchblock
.searchparams2
);
9911 searchblock
.sizeofsearchparams2
= tmp_searchblock
.sizeofsearchparams2
;
9912 searchblock
.searchattrs
= tmp_searchblock
.searchattrs
;
9918 /* Do a sanity check on sizeofsearchparams1 and sizeofsearchparams2.
9920 if (searchblock
.sizeofsearchparams1
> SEARCHFS_MAX_SEARCHPARMS
||
9921 searchblock
.sizeofsearchparams2
> SEARCHFS_MAX_SEARCHPARMS
) {
9925 /* Now malloc a big bunch of space to hold the search parameters, the attrlists and the search state. */
9926 /* It all has to do into local memory and it's not that big so we might as well put it all together. */
9927 /* Searchparams1 shall be first so we might as well use that to hold the base address of the allocated*/
9930 /* NOTE: we allocate an extra 8 bytes to account for the difference in size of the searchstate */
9931 /* due to the changes in rdar://problem/12438273. That way if a 3rd party file system */
9932 /* assumes the size is still 556 bytes it will continue to work */
9934 mallocsize
= searchblock
.sizeofsearchparams1
+ searchblock
.sizeofsearchparams2
+
9935 sizeof(struct attrlist
) + sizeof(struct searchstate
) + (2 * sizeof(uint32_t));
9937 MALLOC(searchparams1
, void *, mallocsize
, M_TEMP
, M_WAITOK
);
9939 /* Now set up the various pointers to the correct place in our newly allocated memory */
9941 searchparams2
= (void *) (((caddr_t
) searchparams1
) + searchblock
.sizeofsearchparams1
);
9942 returnattrs
= (struct attrlist
*) (((caddr_t
) searchparams2
) + searchblock
.sizeofsearchparams2
);
9943 state
= (struct searchstate
*) (((caddr_t
) returnattrs
) + sizeof(struct attrlist
));
9945 /* Now copy in the stuff given our local variables. */
9947 if ((error
= copyin(searchblock
.searchparams1
, searchparams1
, searchblock
.sizeofsearchparams1
))) {
9951 if ((error
= copyin(searchblock
.searchparams2
, searchparams2
, searchblock
.sizeofsearchparams2
))) {
9955 if ((error
= copyin(searchblock
.returnattrs
, (caddr_t
) returnattrs
, sizeof(struct attrlist
)))) {
9959 if ((error
= copyin(uap
->state
, (caddr_t
) state
, sizeof(struct searchstate
)))) {
9964 * When searching a union mount, need to set the
9965 * start flag at the first call on each layer to
9966 * reset state for the new volume.
9968 if (uap
->options
& SRCHFS_START
) {
9969 state
->ss_union_layer
= 0;
9971 uap
->options
|= state
->ss_union_flags
;
9973 state
->ss_union_flags
= 0;
9976 * Because searchparams1 and searchparams2 may contain an ATTR_CMN_NAME search parameter,
9977 * which is passed in with an attrreference_t, we need to inspect the buffer manually here.
9978 * The KPI does not provide us the ability to pass in the length of the buffers searchparams1
9979 * and searchparams2. To obviate the need for all searchfs-supporting filesystems to
9980 * validate the user-supplied data offset of the attrreference_t, we'll do it here.
9983 if (searchblock
.searchattrs
.commonattr
& ATTR_CMN_NAME
) {
9984 attrreference_t
* string_ref
;
9985 u_int32_t
* start_length
;
9986 user64_size_t param_length
;
9988 /* validate searchparams1 */
9989 param_length
= searchblock
.sizeofsearchparams1
;
9990 /* skip the word that specifies length of the buffer */
9991 start_length
= (u_int32_t
*) searchparams1
;
9992 start_length
= start_length
+ 1;
9993 string_ref
= (attrreference_t
*) start_length
;
9995 /* ensure no negative offsets or too big offsets */
9996 if (string_ref
->attr_dataoffset
< 0) {
10000 if (string_ref
->attr_length
> MAXPATHLEN
) {
10005 /* Check for pointer overflow in the string ref */
10006 if (((char*) string_ref
+ string_ref
->attr_dataoffset
) < (char*) string_ref
) {
10011 if (((char*) string_ref
+ string_ref
->attr_dataoffset
) > ((char*)searchparams1
+ param_length
)) {
10015 if (((char*)string_ref
+ string_ref
->attr_dataoffset
+ string_ref
->attr_length
) > ((char*)searchparams1
+ param_length
)) {
10021 /* set up the uio structure which will contain the users return buffer */
10022 auio
= uio_createwithbuffer(1, 0, spacetype
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
10023 uio_addiov(auio
, searchblock
.returnbuffer
, searchblock
.returnbuffersize
);
10026 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) {
10027 nameiflags
|= FOLLOW
;
10029 NDINIT(&nd
, LOOKUP
, OP_SEARCHFS
, nameiflags
| AUDITVNPATH1
,
10030 UIO_USERSPACE
, uap
->path
, ctx
);
10032 error
= namei(&nd
);
10040 * Switch to the root vnode for the volume
10042 error
= VFS_ROOT(vnode_mount(vp
), &tvp
, ctx
);
10050 * If it's a union mount, the path lookup takes
10051 * us to the top layer. But we may need to descend
10052 * to a lower layer. For non-union mounts the layer
10055 for (i
= 0; i
< (int) state
->ss_union_layer
; i
++) {
10056 if ((vp
->v_mount
->mnt_flag
& MNT_UNION
) == 0) {
10060 vp
= vp
->v_mount
->mnt_vnodecovered
;
10066 error
= vnode_getwithref(vp
);
10074 error
= mac_vnode_check_searchfs(ctx
, vp
, &searchblock
.searchattrs
);
10083 * If searchblock.maxmatches == 0, then skip the search. This has happened
10084 * before and sometimes the underlying code doesnt deal with it well.
10086 if (searchblock
.maxmatches
== 0) {
10092 * Allright, we have everything we need, so lets make that call.
10094 * We keep special track of the return value from the file system:
10095 * EAGAIN is an acceptable error condition that shouldn't keep us
10096 * from copying out any results...
10099 fserror
= VNOP_SEARCHFS(vp
,
10102 &searchblock
.searchattrs
,
10103 (u_long
)searchblock
.maxmatches
,
10107 (u_long
)uap
->scriptcode
,
10108 (u_long
)uap
->options
,
10110 (struct searchstate
*) &state
->ss_fsstate
,
10114 * If it's a union mount we need to be called again
10115 * to search the mounted-on filesystem.
10117 if ((vp
->v_mount
->mnt_flag
& MNT_UNION
) && fserror
== 0) {
10118 state
->ss_union_flags
= SRCHFS_START
;
10119 state
->ss_union_layer
++; // search next layer down
10127 /* Now copy out the stuff that needs copying out. That means the number of matches, the
10128 * search state. Everything was already put into he return buffer by the vop call. */
10130 if ((error
= copyout((caddr_t
) state
, uap
->state
, sizeof(struct searchstate
))) != 0) {
10134 if ((error
= suulong(uap
->nummatches
, (uint64_t)nummatches
)) != 0) {
10142 FREE(searchparams1
, M_TEMP
);
10145 } /* end of searchfs system call */
10147 #else /* CONFIG_SEARCHFS */
10150 searchfs(__unused proc_t p
, __unused
struct searchfs_args
*uap
, __unused
int32_t *retval
)
10155 #endif /* CONFIG_SEARCHFS */
10158 #if CONFIG_DATALESS_FILES
10161 * === Namespace Resolver Up-call Mechanism ===
10163 * When I/O is performed to a dataless file or directory (read, write,
10164 * lookup-in, etc.), the file system performs an upcall to the namespace
10165 * resolver (filecoordinationd) to materialize the object.
10167 * We need multiple up-calls to be in flight at once, and we need these
10168 * up-calls to be interruptible, thus the following implementation:
10170 * => The nspace_resolver_request represents the in-kernel request state.
10171 * It contains a request ID, storage space for the errno code returned
10172 * by filecoordinationd, and flags.
10174 * => The request ID is simply a global monotonically incrementing 32-bit
10175 * number. Outstanding requests are stored in a hash table, and the
10176 * hash function is extremely simple.
10178 * => When an upcall is to be made to filecoordinationd, a request structure
10179 * is allocated on the stack (it is small, and needs to live only during
10180 * the duration of the call to resolve_nspace_item_ext()). It is
10181 * initialized and inserted into the table. Some backpressure from
10182 * filecoordinationd is applied by limiting the numnber of entries that
10183 * can be inserted into the table (and thus limiting the number of
10184 * outstanding requests issued to filecoordinationd); waiting for an
10185 * available slot is interruptible.
10187 * => Once the request has been inserted into the table, the up-call is made
10188 * to filecoordinationd via a MiG-generated stub. The up-call returns
10189 * immediately and filecoordinationd processes the request asynchronously.
10191 * => The caller now waits for the request to complete. Tnis is achieved by
10192 * sleeping on the address of the request structure and waiting for
10193 * filecoordinationd to mark the request structure as complete. This
10194 * is an interruptible sleep call; if interrupted, the request structure
10195 * is removed from the table and EINTR is returned to the caller. If
10196 * this occurs, an advisory up-call is made to filecoordinationd with
10197 * the request ID to indicate that the request can be aborted or
10198 * de-prioritized at the discretion of filecoordinationd.
10200 * => When filecoordinationd has completed the request, it signals completion
10201 * by writing to the vfs.nspace.complete sysctl node. Only a process
10202 * decorated as a namespace resolver can write to this sysctl node. The
10203 * value is a request ID / errno tuple passed as an array of 2 uint32_t's.
10204 * The request ID is looked up in the table, and if the request is found,
10205 * the error code is stored in the request structure and a wakeup()
10206 * issued on the address of the request structure. If the request is not
10207 * found, we simply drop the completion notification, assuming that the
10208 * caller was interrupted.
10210 * => When the waiting thread wakes up, it extracts the error code from the
10211 * request structure, removes the request from the table, and returns the
10212 * error code to the calling function. Fini!
10215 struct nspace_resolver_request
{
10216 LIST_ENTRY(nspace_resolver_request
) r_hashlink
;
10218 int r_resolver_error
;
10222 #define RRF_COMPLETE 0x0001
10225 next_nspace_req_id(void)
10227 static uint32_t next_req_id
;
10229 return OSAddAtomic(1, &next_req_id
);
10232 #define NSPACE_RESOLVER_REQ_HASHSIZE 32 /* XXX tune */
10233 #define NSPACE_RESOLVER_MAX_OUTSTANDING 256 /* XXX tune */
10235 static LIST_HEAD(nspace_resolver_requesthead
,
10236 nspace_resolver_request
) * nspace_resolver_request_hashtbl
;
10237 static u_long nspace_resolver_request_hashmask
;
10238 static u_int nspace_resolver_request_count
;
10239 static bool nspace_resolver_request_wait_slot
;
10240 static lck_grp_t
*nspace_resolver_request_lck_grp
;
10241 static lck_mtx_t nspace_resolver_request_hash_mutex
;
10243 #define NSPACE_REQ_LOCK() \
10244 lck_mtx_lock(&nspace_resolver_request_hash_mutex)
10245 #define NSPACE_REQ_UNLOCK() \
10246 lck_mtx_unlock(&nspace_resolver_request_hash_mutex)
10248 #define NSPACE_RESOLVER_HASH(req_id) \
10249 (&nspace_resolver_request_hashtbl[(req_id) & \
10250 nspace_resolver_request_hashmask])
10252 static struct nspace_resolver_request
*
10253 nspace_resolver_req_lookup(uint32_t req_id
)
10255 struct nspace_resolver_requesthead
*bucket
;
10256 struct nspace_resolver_request
*req
;
10258 bucket
= NSPACE_RESOLVER_HASH(req_id
);
10259 LIST_FOREACH(req
, bucket
, r_hashlink
) {
10260 if (req
->r_req_id
== req_id
) {
10269 nspace_resolver_req_add(struct nspace_resolver_request
*req
)
10271 struct nspace_resolver_requesthead
*bucket
;
10274 while (nspace_resolver_request_count
>=
10275 NSPACE_RESOLVER_MAX_OUTSTANDING
) {
10276 nspace_resolver_request_wait_slot
= true;
10277 error
= msleep(&nspace_resolver_request_count
,
10278 &nspace_resolver_request_hash_mutex
,
10279 PVFS
| PCATCH
, "nspacerq", NULL
);
10285 bucket
= NSPACE_RESOLVER_HASH(req
->r_req_id
);
10287 assert(nspace_resolver_req_lookup(req
->r_req_id
) == NULL
);
10288 #endif /* DIAGNOSTIC */
10289 LIST_INSERT_HEAD(bucket
, req
, r_hashlink
);
10290 nspace_resolver_request_count
++;
10296 nspace_resolver_req_remove(struct nspace_resolver_request
*req
)
10298 struct nspace_resolver_requesthead
*bucket
;
10300 bucket
= NSPACE_RESOLVER_HASH(req
->r_req_id
);
10302 assert(nspace_resolver_req_lookup(req
->r_req_id
) != NULL
);
10303 #endif /* DIAGNOSTIC */
10304 LIST_REMOVE(req
, r_hashlink
);
10305 nspace_resolver_request_count
--;
10307 if (nspace_resolver_request_wait_slot
) {
10308 nspace_resolver_request_wait_slot
= false;
10309 wakeup(&nspace_resolver_request_count
);
10314 nspace_resolver_req_cancel(uint32_t req_id
)
10319 // Failures here aren't fatal -- the cancellation message
10320 // sent to the resolver is merely advisory.
10322 kr
= host_get_filecoordinationd_port(host_priv_self(), &mp
);
10323 if (kr
!= KERN_SUCCESS
|| !IPC_PORT_VALID(mp
)) {
10327 kr
= send_nspace_resolve_cancel(mp
, req_id
);
10328 if (kr
!= KERN_SUCCESS
) {
10329 os_log_error(OS_LOG_DEFAULT
,
10330 "NSPACE send_nspace_resolve_cancel failure: %d", kr
);
10333 ipc_port_release_send(mp
);
10337 nspace_resolver_req_wait(struct nspace_resolver_request
*req
)
10339 bool send_cancel_message
= false;
10344 while ((req
->r_flags
& RRF_COMPLETE
) == 0) {
10345 error
= msleep(req
, &nspace_resolver_request_hash_mutex
,
10346 PVFS
| PCATCH
, "nspace", NULL
);
10347 if (error
&& error
!= ERESTART
) {
10348 req
->r_resolver_error
= (error
== EINTR
) ? EINTR
:
10350 send_cancel_message
= true;
10355 nspace_resolver_req_remove(req
);
10357 NSPACE_REQ_UNLOCK();
10359 if (send_cancel_message
) {
10360 nspace_resolver_req_cancel(req
->r_req_id
);
10363 return req
->r_resolver_error
;
10367 nspace_resolver_req_mark_complete(
10368 struct nspace_resolver_request
*req
,
10369 int resolver_error
)
10371 req
->r_resolver_error
= resolver_error
;
10372 req
->r_flags
|= RRF_COMPLETE
;
10377 nspace_resolver_req_completed(uint32_t req_id
, int resolver_error
)
10379 struct nspace_resolver_request
*req
;
10383 // If we don't find the request corresponding to our req_id,
10384 // just drop the completion signal on the floor; it's likely
10385 // that the requester interrupted with a signal.
10387 req
= nspace_resolver_req_lookup(req_id
);
10389 nspace_resolver_req_mark_complete(req
, resolver_error
);
10392 NSPACE_REQ_UNLOCK();
10395 static struct proc
*nspace_resolver_proc
;
10398 nspace_resolver_get_proc_state(struct proc
*p
, int *is_resolver
)
10400 *is_resolver
= ((p
->p_lflag
& P_LNSPACE_RESOLVER
) &&
10401 p
== nspace_resolver_proc
) ? 1 : 0;
10406 nspace_resolver_set_proc_state(struct proc
*p
, int is_resolver
)
10408 vfs_context_t ctx
= vfs_context_current();
10412 // The system filecoordinationd runs as uid == 0. This also
10413 // has the nice side-effect of filtering out filecoordinationd
10414 // running in the simulator.
10416 if (!vfs_context_issuser(ctx
)) {
10420 error
= priv_check_cred(vfs_context_ucred(ctx
),
10421 PRIV_VFS_DATALESS_RESOLVER
, 0);
10429 if (nspace_resolver_proc
== NULL
) {
10431 p
->p_lflag
|= P_LNSPACE_RESOLVER
;
10433 nspace_resolver_proc
= p
;
10438 NSPACE_REQ_UNLOCK();
10440 // This is basically just like the exit case.
10441 // nspace_resolver_exited() will verify that the
10442 // process is the resolver, and will clear the
10444 nspace_resolver_exited(p
);
10451 nspace_materialization_get_proc_state(struct proc
*p
, int *is_prevented
)
10453 if ((p
->p_lflag
& P_LNSPACE_RESOLVER
) != 0 ||
10454 (p
->p_vfs_iopolicy
&
10455 P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
) == 0) {
10464 nspace_materialization_set_proc_state(struct proc
*p
, int is_prevented
)
10466 if (p
->p_lflag
& P_LNSPACE_RESOLVER
) {
10467 return is_prevented
? 0 : EBUSY
;
10470 if (is_prevented
) {
10471 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
), &p
->p_vfs_iopolicy
);
10473 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
, &p
->p_vfs_iopolicy
);
10479 nspace_materialization_get_thread_state(int *is_prevented
)
10481 uthread_t ut
= get_bsdthread_info(current_thread());
10483 *is_prevented
= (ut
->uu_flag
& UT_NSPACE_NODATALESSFAULTS
) ? 1 : 0;
10488 nspace_materialization_set_thread_state(int is_prevented
)
10490 uthread_t ut
= get_bsdthread_info(current_thread());
10492 if (is_prevented
) {
10493 ut
->uu_flag
|= UT_NSPACE_NODATALESSFAULTS
;
10495 ut
->uu_flag
&= ~UT_NSPACE_NODATALESSFAULTS
;
10501 nspace_materialization_is_prevented(void)
10503 proc_t p
= current_proc();
10504 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
10505 vfs_context_t ctx
= vfs_context_current();
10508 * Kernel context ==> return EDEADLK, as we would with any random
10509 * process decorated as no-materialize.
10511 if (ctx
== vfs_context_kernel()) {
10516 * If the process has the dataless-manipulation entitlement,
10517 * materialization is prevented, and depending on the kind
10518 * of file system operation, things get to proceed as if the
10519 * object is not dataless.
10521 if (vfs_context_is_dataless_manipulator(ctx
)) {
10522 return EJUSTRETURN
;
10526 * Per-thread decorations override any process-wide decorations.
10527 * (Foundation uses this, and this overrides even the dataless-
10528 * manipulation entitlement so as to make API contracts consistent.)
10531 if (ut
->uu_flag
& UT_NSPACE_NODATALESSFAULTS
) {
10534 if (ut
->uu_flag
& UT_NSPACE_FORCEDATALESSFAULTS
) {
10540 * If the process's iopolicy specifies that dataless files
10541 * can be materialized, then we let it go ahead.
10543 if (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES
) {
10548 * The default behavior is to not materialize dataless files;
10549 * return to the caller that deadlock was detected.
10554 /* the vfs.nspace branch */
10555 SYSCTL_NODE(_vfs
, OID_AUTO
, nspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
, NULL
, "vfs nspace hinge");
10558 sysctl_nspace_resolver(__unused
struct sysctl_oid
*oidp
,
10559 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
10561 struct proc
*p
= req
->p
;
10562 int new_value
, old_value
, changed
= 0;
10565 error
= nspace_resolver_get_proc_state(p
, &old_value
);
10570 error
= sysctl_io_number(req
, old_value
, sizeof(int), &new_value
,
10572 if (error
== 0 && changed
) {
10573 error
= nspace_resolver_set_proc_state(p
, new_value
);
10578 /* decorate this process as the dataless file resolver */
10579 SYSCTL_PROC(_vfs_nspace
, OID_AUTO
, resolver
,
10580 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
,
10581 0, 0, sysctl_nspace_resolver
, "I", "");
10584 sysctl_nspace_prevent_materialization(__unused
struct sysctl_oid
*oidp
,
10585 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
10587 struct proc
*p
= req
->p
;
10588 int new_value
, old_value
, changed
= 0;
10591 error
= nspace_materialization_get_proc_state(p
, &old_value
);
10596 error
= sysctl_io_number(req
, old_value
, sizeof(int), &new_value
,
10598 if (error
== 0 && changed
) {
10599 error
= nspace_materialization_set_proc_state(p
, new_value
);
10604 /* decorate this process as not wanting to materialize dataless files */
10605 SYSCTL_PROC(_vfs_nspace
, OID_AUTO
, prevent_materialization
,
10606 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
,
10607 0, 0, sysctl_nspace_prevent_materialization
, "I", "");
10610 sysctl_nspace_thread_prevent_materialization(__unused
struct sysctl_oid
*oidp
,
10611 __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
10613 int new_value
, old_value
, changed
= 0;
10616 error
= nspace_materialization_get_thread_state(&old_value
);
10621 error
= sysctl_io_number(req
, old_value
, sizeof(int), &new_value
,
10623 if (error
== 0 && changed
) {
10624 error
= nspace_materialization_set_thread_state(new_value
);
10629 /* decorate this thread as not wanting to materialize dataless files */
10630 SYSCTL_PROC(_vfs_nspace
, OID_AUTO
, thread_prevent_materialization
,
10631 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
,
10632 0, 0, sysctl_nspace_thread_prevent_materialization
, "I", "");
10635 sysctl_nspace_complete(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
10636 __unused
int arg2
, struct sysctl_req
*req
)
10638 struct proc
*p
= req
->p
;
10639 uint32_t req_status
[2] = { 0, 0 };
10640 int error
, is_resolver
, changed
= 0;
10642 error
= nspace_resolver_get_proc_state(p
, &is_resolver
);
10647 if (!is_resolver
) {
10651 error
= sysctl_io_opaque(req
, req_status
, sizeof(req_status
),
10658 * req_status[0] is the req_id
10660 * req_status[1] is the errno
10662 if (error
== 0 && changed
) {
10663 nspace_resolver_req_completed(req_status
[0],
10664 (int)req_status
[1]);
10669 /* Resolver reports completed reqs here. */
10670 SYSCTL_PROC(_vfs_nspace
, OID_AUTO
, complete
,
10671 CTLTYPE_OPAQUE
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_LOCKED
,
10672 0, 0, sysctl_nspace_complete
, "-", "");
10674 #endif /* CONFIG_DATALESS_FILES */
10676 #if CONFIG_DATALESS_FILES
10677 #define __no_dataless_unused /* nothing */
10679 #define __no_dataless_unused __unused
10683 nspace_resolver_init(void)
10685 #if CONFIG_DATALESS_FILES
10686 nspace_resolver_request_lck_grp
=
10687 lck_grp_alloc_init("file namespace resolver", NULL
);
10689 lck_mtx_init(&nspace_resolver_request_hash_mutex
,
10690 nspace_resolver_request_lck_grp
, NULL
);
10692 nspace_resolver_request_hashtbl
=
10693 hashinit(NSPACE_RESOLVER_REQ_HASHSIZE
,
10694 M_VNODE
/* XXX */, &nspace_resolver_request_hashmask
);
10695 #endif /* CONFIG_DATALESS_FILES */
10699 nspace_resolver_exited(struct proc
*p __no_dataless_unused
)
10701 #if CONFIG_DATALESS_FILES
10702 struct nspace_resolver_requesthead
*bucket
;
10703 struct nspace_resolver_request
*req
;
10708 if ((p
->p_lflag
& P_LNSPACE_RESOLVER
) &&
10709 p
== nspace_resolver_proc
) {
10710 for (idx
= 0; idx
<= nspace_resolver_request_hashmask
; idx
++) {
10711 bucket
= &nspace_resolver_request_hashtbl
[idx
];
10712 LIST_FOREACH(req
, bucket
, r_hashlink
) {
10713 nspace_resolver_req_mark_complete(req
,
10717 nspace_resolver_proc
= NULL
;
10720 NSPACE_REQ_UNLOCK();
10721 #endif /* CONFIG_DATALESS_FILES */
10725 resolve_nspace_item(struct vnode
*vp
, uint64_t op
)
10727 return resolve_nspace_item_ext(vp
, op
, NULL
);
10730 #define DATALESS_RESOLVER_ENTITLEMENT \
10731 "com.apple.private.vfs.dataless-resolver"
10732 #define DATALESS_MANIPULATION_ENTITLEMENT \
10733 "com.apple.private.vfs.dataless-manipulation"
10736 * Return TRUE if the vfs context is associated with a process entitled
10737 * for dataless manipulation.
10739 * XXX Arguably belongs in vfs_subr.c, but is here because of the
10740 * complication around CONFIG_DATALESS_FILES.
10743 vfs_context_is_dataless_manipulator(vfs_context_t ctx __unused
)
10745 #if CONFIG_DATALESS_FILES
10746 assert(ctx
->vc_thread
== current_thread());
10747 task_t
const task
= current_task();
10748 return IOTaskHasEntitlement(task
, DATALESS_MANIPULATION_ENTITLEMENT
) ||
10749 IOTaskHasEntitlement(task
, DATALESS_RESOLVER_ENTITLEMENT
);
10752 #endif /* CONFIG_DATALESS_FILES */
10756 resolve_nspace_item_ext(
10757 struct vnode
*vp __no_dataless_unused
,
10758 uint64_t op __no_dataless_unused
,
10759 void *arg __unused
)
10761 #if CONFIG_DATALESS_FILES
10767 struct nspace_resolver_request req
;
10769 // only allow namespace events on regular files, directories and symlinks.
10770 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VDIR
&& vp
->v_type
!= VLNK
) {
10775 // if this is a snapshot event and the vnode is on a
10776 // disk image just pretend nothing happened since any
10777 // change to the disk image will cause the disk image
10778 // itself to get backed up and this avoids multi-way
10779 // deadlocks between the snapshot handler and the ever
10780 // popular diskimages-helper process. the variable
10781 // nspace_allow_virtual_devs allows this behavior to
10782 // be overridden (for use by the Mobile TimeMachine
10783 // testing infrastructure which uses disk images)
10785 if (op
& NAMESPACE_HANDLER_SNAPSHOT_EVENT
) {
10786 os_log_debug(OS_LOG_DEFAULT
, "NSPACE SNAPSHOT not handled");
10790 error
= nspace_materialization_is_prevented();
10792 os_log_debug(OS_LOG_DEFAULT
,
10793 "NSPACE process/thread is decorated as no-materialization");
10797 kr
= host_get_filecoordinationd_port(host_priv_self(), &mp
);
10798 if (kr
!= KERN_SUCCESS
|| !IPC_PORT_VALID(mp
)) {
10799 os_log_error(OS_LOG_DEFAULT
, "NSPACE no port");
10800 // Treat this like being unable to access the backing
10805 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
10806 if (path
== NULL
) {
10808 goto out_release_port
;
10810 path_len
= MAXPATHLEN
;
10812 error
= vn_getpath(vp
, path
, &path_len
);
10814 int xxx_rdar44371223
; /* XXX Mig bug */
10815 req
.r_req_id
= next_nspace_req_id();
10816 req
.r_resolver_error
= 0;
10820 error
= nspace_resolver_req_add(&req
);
10821 NSPACE_REQ_UNLOCK();
10823 goto out_release_port
;
10826 os_log_debug(OS_LOG_DEFAULT
, "NSPACE resolve_path call");
10827 kr
= send_nspace_resolve_path(mp
, req
.r_req_id
,
10828 current_proc()->p_pid
, (uint32_t)(op
& 0xffffffff),
10829 path
, &xxx_rdar44371223
);
10830 if (kr
!= KERN_SUCCESS
) {
10831 // Also treat this like being unable to access
10832 // the backing store server.
10833 os_log_error(OS_LOG_DEFAULT
,
10834 "NSPACE resolve_path failure: %d", kr
);
10838 nspace_resolver_req_remove(&req
);
10839 NSPACE_REQ_UNLOCK();
10840 goto out_release_port
;
10843 // Give back the memory we allocated earlier while
10844 // we wait; we no longer need it.
10845 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
10848 // Request has been submitted to the resolver.
10849 // Now (interruptibly) wait for completion.
10850 // Upon requrn, the request will have been removed
10851 // from the lookup table.
10852 error
= nspace_resolver_req_wait(&req
);
10856 if (path
!= NULL
) {
10857 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
10859 ipc_port_release_send(mp
);
10864 #endif /* CONFIG_DATALESS_FILES */
10868 nspace_snapshot_event(__unused vnode_t vp
, __unused
time_t ctime
,
10869 __unused
uint64_t op_type
, __unused
void *arg
)
10876 build_volfs_path(struct vnode
*vp
, char *path
, int *len
)
10878 struct vnode_attr va
;
10882 VATTR_WANTED(&va
, va_fsid
);
10883 VATTR_WANTED(&va
, va_fileid
);
10885 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
10886 *len
= snprintf(path
, *len
, "/non/existent/path/because/vnode_getattr/failed") + 1;
10889 *len
= snprintf(path
, *len
, "/.vol/%d/%lld", (dev_t
)va
.va_fsid
, va
.va_fileid
) + 1;
10897 static unsigned long
10898 fsctl_bogus_command_compat(unsigned long cmd
)
10901 case IOCBASECMD(FSIOC_SYNC_VOLUME
):
10902 return FSIOC_SYNC_VOLUME
;
10903 case IOCBASECMD(FSIOC_ROUTEFS_SETROUTEID
):
10904 return FSIOC_ROUTEFS_SETROUTEID
;
10905 case IOCBASECMD(FSIOC_SET_PACKAGE_EXTS
):
10906 return FSIOC_SET_PACKAGE_EXTS
;
10907 case IOCBASECMD(FSIOC_SET_FSTYPENAME_OVERRIDE
):
10908 return FSIOC_SET_FSTYPENAME_OVERRIDE
;
10909 case IOCBASECMD(DISK_CONDITIONER_IOC_GET
):
10910 return DISK_CONDITIONER_IOC_GET
;
10911 case IOCBASECMD(DISK_CONDITIONER_IOC_SET
):
10912 return DISK_CONDITIONER_IOC_SET
;
10913 case IOCBASECMD(FSIOC_FIOSEEKHOLE
):
10914 return FSIOC_FIOSEEKHOLE
;
10915 case IOCBASECMD(FSIOC_FIOSEEKDATA
):
10916 return FSIOC_FIOSEEKDATA
;
10917 case IOCBASECMD(SPOTLIGHT_IOC_GET_MOUNT_TIME
):
10918 return SPOTLIGHT_IOC_GET_MOUNT_TIME
;
10919 case IOCBASECMD(SPOTLIGHT_IOC_GET_LAST_MTIME
):
10920 return SPOTLIGHT_IOC_GET_LAST_MTIME
;
10927 cas_bsdflags_setattr(vnode_t vp
, void *arg
, vfs_context_t ctx
)
10929 return VNOP_IOCTL(vp
, FSIOC_CAS_BSDFLAGS
, arg
, FWRITE
, ctx
);
10933 * Make a filesystem-specific control call:
10937 fsctl_internal(proc_t p
, vnode_t
*arg_vp
, u_long cmd
, user_addr_t udata
, u_long options
, vfs_context_t ctx
)
10942 #define STK_PARAMS 128
10943 char stkbuf
[STK_PARAMS
] = {0};
10944 caddr_t data
, memp
;
10945 vnode_t vp
= *arg_vp
;
10947 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
10951 cmd
= fsctl_bogus_command_compat(cmd
);
10953 size
= IOCPARM_LEN(cmd
);
10954 if (size
> IOCPARM_MAX
) {
10958 is64bit
= proc_is64bit(p
);
10962 if (size
> sizeof(stkbuf
)) {
10963 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
10971 if (cmd
& IOC_IN
) {
10973 error
= copyin(udata
, data
, size
);
10982 *(user_addr_t
*)data
= udata
;
10984 *(uint32_t *)data
= (uint32_t)udata
;
10987 } else if ((cmd
& IOC_OUT
) && size
) {
10989 * Zero the buffer so the user always
10990 * gets back something deterministic.
10993 } else if (cmd
& IOC_VOID
) {
10995 *(user_addr_t
*)data
= udata
;
10997 *(uint32_t *)data
= (uint32_t)udata
;
11001 /* Check to see if it's a generic command */
11003 case FSIOC_SYNC_VOLUME
: {
11004 struct vfs_attr vfa
;
11005 mount_t mp
= vp
->v_mount
;
11009 /* record vid of vp so we can drop it below. */
11010 uint32_t vvid
= vp
->v_id
;
11013 * Then grab mount_iterref so that we can release the vnode.
11014 * Without this, a thread may call vnode_iterate_prepare then
11015 * get into a deadlock because we've never released the root vp
11017 error
= mount_iterref(mp
, 0);
11024 if (*(uint32_t*)data
& FSCTL_SYNC_WAIT
) {
11029 * If the filessytem supports multiple filesytems in a
11030 * partition (For eg APFS volumes in a container, it knows
11031 * that the waitfor argument to VFS_SYNC are flags.
11033 VFSATTR_INIT(&vfa
);
11034 VFSATTR_WANTED(&vfa
, f_capabilities
);
11035 if ((vfs_getattr(mp
, &vfa
, vfs_context_current()) == 0) &&
11036 VFSATTR_IS_SUPPORTED(&vfa
, f_capabilities
) &&
11037 ((vfa
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_SHARED_SPACE
)) &&
11038 ((vfa
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_SHARED_SPACE
))) {
11042 /* issue the sync for this volume */
11043 (void)sync_callback(mp
, &arg
);
11046 * Then release the mount_iterref once we're done syncing; it's not
11047 * needed for the VNOP_IOCTL below
11049 mount_iterdrop(mp
);
11051 if (arg
& FSCTL_SYNC_FULLSYNC
) {
11052 /* re-obtain vnode iocount on the root vp, if possible */
11053 error
= vnode_getwithvid(vp
, vvid
);
11055 error
= VNOP_IOCTL(vp
, F_FULLFSYNC
, (caddr_t
)NULL
, 0, ctx
);
11059 /* mark the argument VP as having been released */
11064 case FSIOC_ROUTEFS_SETROUTEID
: {
11066 char routepath
[MAXPATHLEN
];
11069 if ((error
= suser(kauth_cred_get(), &(current_proc()->p_acflag
)))) {
11072 bzero(routepath
, MAXPATHLEN
);
11073 error
= copyinstr(udata
, &routepath
[0], MAXPATHLEN
, &len
);
11077 error
= routefs_kernel_mount(routepath
);
11085 case FSIOC_SET_PACKAGE_EXTS
: {
11086 user_addr_t ext_strings
;
11087 uint32_t num_entries
;
11088 uint32_t max_width
;
11090 if ((error
= priv_check_cred(kauth_cred_get(), PRIV_PACKAGE_EXTENSIONS
, 0))) {
11094 if ((is64bit
&& size
!= sizeof(user64_package_ext_info
))
11095 || (is64bit
== 0 && size
!= sizeof(user32_package_ext_info
))) {
11096 // either you're 64-bit and passed a 64-bit struct or
11097 // you're 32-bit and passed a 32-bit struct. otherwise
11104 ext_strings
= ((user64_package_ext_info
*)data
)->strings
;
11105 num_entries
= ((user64_package_ext_info
*)data
)->num_entries
;
11106 max_width
= ((user64_package_ext_info
*)data
)->max_width
;
11108 ext_strings
= CAST_USER_ADDR_T(((user32_package_ext_info
*)data
)->strings
);
11109 num_entries
= ((user32_package_ext_info
*)data
)->num_entries
;
11110 max_width
= ((user32_package_ext_info
*)data
)->max_width
;
11112 error
= set_package_extensions_table(ext_strings
, num_entries
, max_width
);
11116 case FSIOC_SET_FSTYPENAME_OVERRIDE
:
11118 if ((error
= suser(kauth_cred_get(), &(current_proc()->p_acflag
)))) {
11122 mount_lock(vp
->v_mount
);
11123 if (data
[0] != 0) {
11124 strlcpy(&vp
->v_mount
->fstypename_override
[0], data
, MFSTYPENAMELEN
);
11125 vp
->v_mount
->mnt_kern_flag
|= MNTK_TYPENAME_OVERRIDE
;
11126 if (vfs_isrdonly(vp
->v_mount
) && strcmp(vp
->v_mount
->fstypename_override
, "mtmfs") == 0) {
11127 vp
->v_mount
->mnt_kern_flag
|= MNTK_EXTENDED_SECURITY
;
11128 vp
->v_mount
->mnt_kern_flag
&= ~MNTK_AUTH_OPAQUE
;
11131 if (strcmp(vp
->v_mount
->fstypename_override
, "mtmfs") == 0) {
11132 vp
->v_mount
->mnt_kern_flag
&= ~MNTK_EXTENDED_SECURITY
;
11134 vp
->v_mount
->mnt_kern_flag
&= ~MNTK_TYPENAME_OVERRIDE
;
11135 vp
->v_mount
->fstypename_override
[0] = '\0';
11137 mount_unlock(vp
->v_mount
);
11142 case DISK_CONDITIONER_IOC_GET
: {
11143 error
= disk_conditioner_get_info(vp
->v_mount
, (disk_conditioner_info
*)data
);
11147 case DISK_CONDITIONER_IOC_SET
: {
11148 error
= disk_conditioner_set_info(vp
->v_mount
, (disk_conditioner_info
*)data
);
11152 case FSIOC_CAS_BSDFLAGS
: {
11153 struct fsioc_cas_bsdflags
*cas
= (struct fsioc_cas_bsdflags
*)data
;
11154 struct vnode_attr va
;
11157 VATTR_SET(&va
, va_flags
, cas
->new_flags
);
11159 error
= chflags0(vp
, &va
, cas_bsdflags_setattr
, cas
, ctx
);
11163 case FSIOC_FD_ONLY_OPEN_ONCE
: {
11164 if (vnode_usecount(vp
) > 1) {
11173 /* other, known commands shouldn't be passed down here */
11176 case F_TRIM_ACTIVE_FILE
:
11178 case F_TRANSCODEKEY
:
11179 case F_GETPROTECTIONLEVEL
:
11180 case F_GETDEFAULTPROTLEVEL
:
11181 case F_MAKECOMPRESSED
:
11182 case F_SET_GREEDY_MODE
:
11183 case F_SETSTATICCONTENT
:
11185 case F_SETBACKINGSTORE
:
11186 case F_GETPATH_MTMINFO
:
11187 case APFSIOC_REVERT_TO_SNAPSHOT
:
11188 case FSIOC_FIOSEEKHOLE
:
11189 case FSIOC_FIOSEEKDATA
:
11190 case HFS_GET_BOOT_INFO
:
11191 case HFS_SET_BOOT_INFO
:
11195 case F_BARRIERFSYNC
:
11201 /* Invoke the filesystem-specific code */
11202 error
= VNOP_IOCTL(vp
, cmd
, data
, options
, ctx
);
11204 } /* end switch stmt */
11207 * if no errors, copy any data to user. Size was
11208 * already set and checked above.
11210 if (error
== 0 && (cmd
& IOC_OUT
) && size
) {
11211 error
= copyout(data
, udata
, size
);
11224 fsctl(proc_t p
, struct fsctl_args
*uap
, __unused
int32_t *retval
)
11227 struct nameidata nd
;
11230 vfs_context_t ctx
= vfs_context_current();
11232 AUDIT_ARG(cmd
, uap
->cmd
);
11233 AUDIT_ARG(value32
, uap
->options
);
11234 /* Get the vnode for the file we are getting info on: */
11237 // if we come through fsctl() then the file is by definition not open.
11238 // therefore for the FSIOC_FD_ONLY_OPEN_ONCE selector we return an error
11239 // lest the caller mistakenly thinks the only open is their own (but in
11240 // reality it's someone elses).
11242 if (uap
->cmd
== FSIOC_FD_ONLY_OPEN_ONCE
) {
11245 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) {
11246 nameiflags
|= FOLLOW
;
11248 if (uap
->cmd
== FSIOC_FIRMLINK_CTL
) {
11249 nameiflags
|= (CN_FIRMLINK_NOFOLLOW
| NOCACHE
);
11251 NDINIT(&nd
, LOOKUP
, OP_FSCTL
, nameiflags
| AUDITVNPATH1
,
11252 UIO_USERSPACE
, uap
->path
, ctx
);
11253 if ((error
= namei(&nd
))) {
11260 error
= mac_mount_check_fsctl(ctx
, vnode_mount(vp
), uap
->cmd
);
11266 error
= fsctl_internal(p
, &vp
, uap
->cmd
, (user_addr_t
)uap
->data
, uap
->options
, ctx
);
11276 ffsctl(proc_t p
, struct ffsctl_args
*uap
, __unused
int32_t *retval
)
11280 vfs_context_t ctx
= vfs_context_current();
11283 AUDIT_ARG(fd
, uap
->fd
);
11284 AUDIT_ARG(cmd
, uap
->cmd
);
11285 AUDIT_ARG(value32
, uap
->options
);
11287 /* Get the vnode for the file we are getting info on: */
11288 if ((error
= file_vnode(uap
->fd
, &vp
))) {
11292 if ((error
= vnode_getwithref(vp
))) {
11298 if ((error
= mac_mount_check_fsctl(ctx
, vnode_mount(vp
), uap
->cmd
))) {
11305 error
= fsctl_internal(p
, &vp
, uap
->cmd
, (user_addr_t
)uap
->data
, uap
->options
, ctx
);
11309 /*validate vp; fsctl_internal() can drop iocount and reset vp to NULL*/
11316 /* end of fsctl system call */
11319 * Retrieve the data of an extended attribute.
11322 getxattr(proc_t p
, struct getxattr_args
*uap
, user_ssize_t
*retval
)
11325 struct nameidata nd
;
11326 char attrname
[XATTR_MAXNAMELEN
+ 1];
11327 vfs_context_t ctx
= vfs_context_current();
11329 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11330 size_t attrsize
= 0;
11332 u_int32_t nameiflags
;
11334 char uio_buf
[UIO_SIZEOF(1)];
11336 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11340 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
11341 NDINIT(&nd
, LOOKUP
, OP_GETXATTR
, nameiflags
, spacetype
, uap
->path
, ctx
);
11342 if ((error
= namei(&nd
))) {
11348 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11352 if (xattr_protected(attrname
)) {
11353 if (!vfs_context_issuser(ctx
) || strcmp(attrname
, "com.apple.system.Security") != 0) {
11359 * the specific check for 0xffffffff is a hack to preserve
11360 * binaray compatibilty in K64 with applications that discovered
11361 * that passing in a buf pointer and a size of -1 resulted in
11362 * just the size of the indicated extended attribute being returned.
11363 * this isn't part of the documented behavior, but because of the
11364 * original implemtation's check for "uap->size > 0", this behavior
11365 * was allowed. In K32 that check turned into a signed comparison
11366 * even though uap->size is unsigned... in K64, we blow by that
11367 * check because uap->size is unsigned and doesn't get sign smeared
11368 * in the munger for a 32 bit user app. we also need to add a
11369 * check to limit the maximum size of the buffer being passed in...
11370 * unfortunately, the underlying fileystems seem to just malloc
11371 * the requested size even if the actual extended attribute is tiny.
11372 * because that malloc is for kernel wired memory, we have to put a
11373 * sane limit on it.
11375 * U32 running on K64 will yield 0x00000000ffffffff for uap->size
11376 * U64 running on K64 will yield -1 (64 bits wide)
11377 * U32/U64 running on K32 will yield -1 (32 bits wide)
11379 if (uap
->size
== 0xffffffff || uap
->size
== (size_t)-1) {
11384 if (uap
->size
> (size_t)XATTR_MAXSIZE
) {
11385 uap
->size
= XATTR_MAXSIZE
;
11388 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_READ
,
11389 &uio_buf
[0], sizeof(uio_buf
));
11390 uio_addiov(auio
, uap
->value
, uap
->size
);
11393 error
= vn_getxattr(vp
, attrname
, auio
, &attrsize
, uap
->options
, ctx
);
11398 *retval
= uap
->size
- uio_resid(auio
);
11400 *retval
= (user_ssize_t
)attrsize
;
11407 * Retrieve the data of an extended attribute.
11410 fgetxattr(proc_t p
, struct fgetxattr_args
*uap
, user_ssize_t
*retval
)
11413 char attrname
[XATTR_MAXNAMELEN
+ 1];
11415 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11416 size_t attrsize
= 0;
11419 char uio_buf
[UIO_SIZEOF(1)];
11421 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11425 if ((error
= file_vnode(uap
->fd
, &vp
))) {
11428 if ((error
= vnode_getwithref(vp
))) {
11429 file_drop(uap
->fd
);
11432 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11436 if (xattr_protected(attrname
)) {
11440 if (uap
->value
&& uap
->size
> 0) {
11441 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_READ
,
11442 &uio_buf
[0], sizeof(uio_buf
));
11443 uio_addiov(auio
, uap
->value
, uap
->size
);
11446 error
= vn_getxattr(vp
, attrname
, auio
, &attrsize
, uap
->options
, vfs_context_current());
11448 (void)vnode_put(vp
);
11449 file_drop(uap
->fd
);
11452 *retval
= uap
->size
- uio_resid(auio
);
11454 *retval
= (user_ssize_t
)attrsize
;
11460 * Set the data of an extended attribute.
11463 setxattr(proc_t p
, struct setxattr_args
*uap
, int *retval
)
11466 struct nameidata nd
;
11467 char attrname
[XATTR_MAXNAMELEN
+ 1];
11468 vfs_context_t ctx
= vfs_context_current();
11470 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11472 u_int32_t nameiflags
;
11474 char uio_buf
[UIO_SIZEOF(1)];
11476 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11480 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11482 if (error
== EPERM
) {
11483 /* if the string won't fit in attrname, copyinstr emits EPERM */
11484 return ENAMETOOLONG
;
11486 /* Otherwise return the default error from copyinstr to detect ERANGE, etc */
11489 if (xattr_protected(attrname
)) {
11492 if (uap
->size
!= 0 && uap
->value
== 0) {
11496 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
11497 NDINIT(&nd
, LOOKUP
, OP_SETXATTR
, nameiflags
, spacetype
, uap
->path
, ctx
);
11498 if ((error
= namei(&nd
))) {
11504 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_WRITE
,
11505 &uio_buf
[0], sizeof(uio_buf
));
11506 uio_addiov(auio
, uap
->value
, uap
->size
);
11508 error
= vn_setxattr(vp
, attrname
, auio
, uap
->options
, ctx
);
11511 add_fsevent(FSE_XATTR_MODIFIED
, ctx
,
11522 * Set the data of an extended attribute.
11525 fsetxattr(proc_t p
, struct fsetxattr_args
*uap
, int *retval
)
11528 char attrname
[XATTR_MAXNAMELEN
+ 1];
11530 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11533 char uio_buf
[UIO_SIZEOF(1)];
11535 vfs_context_t ctx
= vfs_context_current();
11538 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11542 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11544 if (error
== EPERM
) {
11545 /* if the string won't fit in attrname, copyinstr emits EPERM */
11546 return ENAMETOOLONG
;
11548 /* Otherwise return the default error from copyinstr to detect ERANGE, etc */
11551 if (xattr_protected(attrname
)) {
11554 if (uap
->size
!= 0 && uap
->value
== 0) {
11557 if ((error
= file_vnode(uap
->fd
, &vp
))) {
11560 if ((error
= vnode_getwithref(vp
))) {
11561 file_drop(uap
->fd
);
11564 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_WRITE
,
11565 &uio_buf
[0], sizeof(uio_buf
));
11566 uio_addiov(auio
, uap
->value
, uap
->size
);
11568 error
= vn_setxattr(vp
, attrname
, auio
, uap
->options
, vfs_context_current());
11571 add_fsevent(FSE_XATTR_MODIFIED
, ctx
,
11577 file_drop(uap
->fd
);
11583 * Remove an extended attribute.
11584 * XXX Code duplication here.
11587 removexattr(proc_t p
, struct removexattr_args
*uap
, int *retval
)
11590 struct nameidata nd
;
11591 char attrname
[XATTR_MAXNAMELEN
+ 1];
11592 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11593 vfs_context_t ctx
= vfs_context_current();
11595 u_int32_t nameiflags
;
11598 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11602 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11606 if (xattr_protected(attrname
)) {
11609 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
11610 NDINIT(&nd
, LOOKUP
, OP_REMOVEXATTR
, nameiflags
, spacetype
, uap
->path
, ctx
);
11611 if ((error
= namei(&nd
))) {
11617 error
= vn_removexattr(vp
, attrname
, uap
->options
, ctx
);
11620 add_fsevent(FSE_XATTR_REMOVED
, ctx
,
11631 * Remove an extended attribute.
11632 * XXX Code duplication here.
11635 fremovexattr(__unused proc_t p
, struct fremovexattr_args
*uap
, int *retval
)
11638 char attrname
[XATTR_MAXNAMELEN
+ 1];
11642 vfs_context_t ctx
= vfs_context_current();
11645 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11649 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
11653 if (xattr_protected(attrname
)) {
11656 if ((error
= file_vnode(uap
->fd
, &vp
))) {
11659 if ((error
= vnode_getwithref(vp
))) {
11660 file_drop(uap
->fd
);
11664 error
= vn_removexattr(vp
, attrname
, uap
->options
, vfs_context_current());
11667 add_fsevent(FSE_XATTR_REMOVED
, ctx
,
11673 file_drop(uap
->fd
);
11679 * Retrieve the list of extended attribute names.
11680 * XXX Code duplication here.
11683 listxattr(proc_t p
, struct listxattr_args
*uap
, user_ssize_t
*retval
)
11686 struct nameidata nd
;
11687 vfs_context_t ctx
= vfs_context_current();
11689 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11690 size_t attrsize
= 0;
11691 u_int32_t nameiflags
;
11693 char uio_buf
[UIO_SIZEOF(1)];
11695 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11699 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
11700 NDINIT(&nd
, LOOKUP
, OP_LISTXATTR
, nameiflags
, spacetype
, uap
->path
, ctx
);
11701 if ((error
= namei(&nd
))) {
11706 if (uap
->namebuf
!= 0 && uap
->bufsize
> 0) {
11707 auio
= uio_createwithbuffer(1, 0, spacetype
, UIO_READ
,
11708 &uio_buf
[0], sizeof(uio_buf
));
11709 uio_addiov(auio
, uap
->namebuf
, uap
->bufsize
);
11712 error
= vn_listxattr(vp
, auio
, &attrsize
, uap
->options
, ctx
);
11716 *retval
= (user_ssize_t
)uap
->bufsize
- uio_resid(auio
);
11718 *retval
= (user_ssize_t
)attrsize
;
11724 * Retrieve the list of extended attribute names.
11725 * XXX Code duplication here.
11728 flistxattr(proc_t p
, struct flistxattr_args
*uap
, user_ssize_t
*retval
)
11732 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
11733 size_t attrsize
= 0;
11735 char uio_buf
[UIO_SIZEOF(1)];
11737 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
)) {
11741 if ((error
= file_vnode(uap
->fd
, &vp
))) {
11744 if ((error
= vnode_getwithref(vp
))) {
11745 file_drop(uap
->fd
);
11748 if (uap
->namebuf
!= 0 && uap
->bufsize
> 0) {
11749 auio
= uio_createwithbuffer(1, 0, spacetype
,
11750 UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
11751 uio_addiov(auio
, uap
->namebuf
, uap
->bufsize
);
11754 error
= vn_listxattr(vp
, auio
, &attrsize
, uap
->options
, vfs_context_current());
11757 file_drop(uap
->fd
);
11759 *retval
= (user_ssize_t
)uap
->bufsize
- uio_resid(auio
);
11761 *retval
= (user_ssize_t
)attrsize
;
11767 fsgetpath_internal(vfs_context_t ctx
, int volfs_id
, uint64_t objid
,
11768 vm_size_t bufsize
, caddr_t buf
, uint32_t options
, int *pathlen
)
11771 struct mount
*mp
= NULL
;
11775 /* maximum number of times to retry build_path */
11776 unsigned int retries
= 0x10;
11778 if (bufsize
> PAGE_SIZE
) {
11787 if ((mp
= mount_lookupby_volfsid(volfs_id
, 1)) == NULL
) {
11788 error
= ENOTSUP
; /* unexpected failure */
11794 struct vfs_attr vfsattr
;
11795 int use_vfs_root
= TRUE
;
11797 VFSATTR_INIT(&vfsattr
);
11798 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
11799 if (!(options
& FSOPT_ISREALFSID
) &&
11800 vfs_getattr(mp
, &vfsattr
, vfs_context_kernel()) == 0 &&
11801 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
11802 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
) &&
11803 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
)) {
11804 use_vfs_root
= FALSE
;
11808 if (use_vfs_root
) {
11809 error
= VFS_ROOT(mp
, &vp
, ctx
);
11811 error
= VFS_VGET(mp
, objid
, &vp
, ctx
);
11814 error
= VFS_VGET(mp
, (ino64_t
)objid
, &vp
, ctx
);
11817 if (error
== ENOENT
&& (mp
->mnt_flag
& MNT_UNION
)) {
11819 * If the fileid isn't found and we're in a union
11820 * mount volume, then see if the fileid is in the
11821 * mounted-on volume.
11823 struct mount
*tmp
= mp
;
11824 mp
= vnode_mount(tmp
->mnt_vnodecovered
);
11826 if (vfs_busy(mp
, LK_NOWAIT
) == 0) {
11838 error
= mac_vnode_check_fsgetpath(ctx
, vp
);
11845 /* Obtain the absolute path to this vnode. */
11846 bpflags
= vfs_context_suser(ctx
) ? BUILDPATH_CHECKACCESS
: 0;
11847 if (options
& FSOPT_NOFIRMLINKPATH
) {
11848 bpflags
|= BUILDPATH_NO_FIRMLINK
;
11850 bpflags
|= BUILDPATH_CHECK_MOVED
;
11851 error
= build_path(vp
, buf
, bufsize
, &length
, bpflags
, ctx
);
11855 /* there was a race building the path, try a few more times */
11856 if (error
== EAGAIN
) {
11867 AUDIT_ARG(text
, buf
);
11869 if (kdebug_enable
) {
11870 long dbg_parms
[NUMPARMS
];
11873 dbg_namelen
= (int)sizeof(dbg_parms
);
11875 if (length
< dbg_namelen
) {
11876 memcpy((char *)dbg_parms
, buf
, length
);
11877 memset((char *)dbg_parms
+ length
, 0, dbg_namelen
- length
);
11879 dbg_namelen
= length
;
11881 memcpy((char *)dbg_parms
, buf
+ (length
- dbg_namelen
), dbg_namelen
);
11884 kdebug_vfs_lookup(dbg_parms
, dbg_namelen
, (void *)vp
,
11885 KDBG_VFS_LOOKUP_FLAG_LOOKUP
);
11888 *pathlen
= (user_ssize_t
)length
; /* may be superseded by error */
11895 * Obtain the full pathname of a file system object by id.
11898 fsgetpath_extended(user_addr_t buf
, int bufsize
, user_addr_t user_fsid
, uint64_t objid
,
11899 uint32_t options
, user_ssize_t
*retval
)
11901 vfs_context_t ctx
= vfs_context_current();
11907 if (options
& ~(FSOPT_NOFIRMLINKPATH
| FSOPT_ISREALFSID
)) {
11911 if ((error
= copyin(user_fsid
, (caddr_t
)&fsid
, sizeof(fsid
)))) {
11914 AUDIT_ARG(value32
, fsid
.val
[0]);
11915 AUDIT_ARG(value64
, objid
);
11916 /* Restrict output buffer size for now. */
11918 if (bufsize
> PAGE_SIZE
|| bufsize
<= 0) {
11921 MALLOC(realpath
, char *, bufsize
, M_TEMP
, M_WAITOK
| M_ZERO
);
11922 if (realpath
== NULL
) {
11926 error
= fsgetpath_internal(ctx
, fsid
.val
[0], objid
, bufsize
, realpath
,
11933 error
= copyout((caddr_t
)realpath
, buf
, length
);
11935 *retval
= (user_ssize_t
)length
; /* may be superseded by error */
11938 FREE(realpath
, M_TEMP
);
11944 fsgetpath(__unused proc_t p
, struct fsgetpath_args
*uap
, user_ssize_t
*retval
)
11946 return fsgetpath_extended(uap
->buf
, uap
->bufsize
, uap
->fsid
, uap
->objid
,
11951 fsgetpath_ext(__unused proc_t p
, struct fsgetpath_ext_args
*uap
, user_ssize_t
*retval
)
11953 return fsgetpath_extended(uap
->buf
, uap
->bufsize
, uap
->fsid
, uap
->objid
,
11954 uap
->options
, retval
);
11958 * Common routine to handle various flavors of statfs data heading out
11961 * Returns: 0 Success
11965 munge_statfs(struct mount
*mp
, struct vfsstatfs
*sfsp
,
11966 user_addr_t bufp
, int *sizep
, boolean_t is_64_bit
,
11967 boolean_t partial_copy
)
11970 int my_size
, copy_size
;
11973 struct user64_statfs sfs
;
11974 my_size
= copy_size
= sizeof(sfs
);
11975 bzero(&sfs
, my_size
);
11976 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
11977 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
11978 sfs
.f_reserved1
= (short)sfsp
->f_fssubtype
;
11979 sfs
.f_bsize
= (user64_long_t
)sfsp
->f_bsize
;
11980 sfs
.f_iosize
= (user64_long_t
)sfsp
->f_iosize
;
11981 sfs
.f_blocks
= (user64_long_t
)sfsp
->f_blocks
;
11982 sfs
.f_bfree
= (user64_long_t
)sfsp
->f_bfree
;
11983 sfs
.f_bavail
= (user64_long_t
)sfsp
->f_bavail
;
11984 sfs
.f_files
= (user64_long_t
)sfsp
->f_files
;
11985 sfs
.f_ffree
= (user64_long_t
)sfsp
->f_ffree
;
11986 sfs
.f_fsid
= sfsp
->f_fsid
;
11987 sfs
.f_owner
= sfsp
->f_owner
;
11988 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
11989 strlcpy(&sfs
.f_fstypename
[0], &mp
->fstypename_override
[0], MFSNAMELEN
);
11991 strlcpy(&sfs
.f_fstypename
[0], &sfsp
->f_fstypename
[0], MFSNAMELEN
);
11993 strlcpy(&sfs
.f_mntonname
[0], &sfsp
->f_mntonname
[0], MNAMELEN
);
11994 strlcpy(&sfs
.f_mntfromname
[0], &sfsp
->f_mntfromname
[0], MNAMELEN
);
11996 if (partial_copy
) {
11997 copy_size
-= (sizeof(sfs
.f_reserved3
) + sizeof(sfs
.f_reserved4
));
11999 error
= copyout((caddr_t
)&sfs
, bufp
, copy_size
);
12001 struct user32_statfs sfs
;
12003 my_size
= copy_size
= sizeof(sfs
);
12004 bzero(&sfs
, my_size
);
12006 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
12007 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
12008 sfs
.f_reserved1
= (short)sfsp
->f_fssubtype
;
12011 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
12012 * have to fudge the numbers here in that case. We inflate the blocksize in order
12013 * to reflect the filesystem size as best we can.
12015 if ((sfsp
->f_blocks
> INT_MAX
)
12016 /* Hack for 4061702 . I think the real fix is for Carbon to
12017 * look for some volume capability and not depend on hidden
12018 * semantics agreed between a FS and carbon.
12019 * f_blocks, f_bfree, and f_bavail set to -1 is the trigger
12020 * for Carbon to set bNoVolumeSizes volume attribute.
12021 * Without this the webdavfs files cannot be copied onto
12022 * disk as they look huge. This change should not affect
12023 * XSAN as they should not setting these to -1..
12025 && (sfsp
->f_blocks
!= 0xffffffffffffffffULL
)
12026 && (sfsp
->f_bfree
!= 0xffffffffffffffffULL
)
12027 && (sfsp
->f_bavail
!= 0xffffffffffffffffULL
)) {
12031 * Work out how far we have to shift the block count down to make it fit.
12032 * Note that it's possible to have to shift so far that the resulting
12033 * blocksize would be unreportably large. At that point, we will clip
12034 * any values that don't fit.
12036 * For safety's sake, we also ensure that f_iosize is never reported as
12037 * being smaller than f_bsize.
12039 for (shift
= 0; shift
< 32; shift
++) {
12040 if ((sfsp
->f_blocks
>> shift
) <= INT_MAX
) {
12043 if ((sfsp
->f_bsize
<< (shift
+ 1)) > INT_MAX
) {
12047 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > INT_MAX) ? INT_MAX : ((x) >> (s)))
12048 sfs
.f_blocks
= (user32_long_t
)__SHIFT_OR_CLIP(sfsp
->f_blocks
, shift
);
12049 sfs
.f_bfree
= (user32_long_t
)__SHIFT_OR_CLIP(sfsp
->f_bfree
, shift
);
12050 sfs
.f_bavail
= (user32_long_t
)__SHIFT_OR_CLIP(sfsp
->f_bavail
, shift
);
12051 #undef __SHIFT_OR_CLIP
12052 sfs
.f_bsize
= (user32_long_t
)(sfsp
->f_bsize
<< shift
);
12053 sfs
.f_iosize
= lmax(sfsp
->f_iosize
, sfsp
->f_bsize
);
12055 /* filesystem is small enough to be reported honestly */
12056 sfs
.f_bsize
= (user32_long_t
)sfsp
->f_bsize
;
12057 sfs
.f_iosize
= (user32_long_t
)sfsp
->f_iosize
;
12058 sfs
.f_blocks
= (user32_long_t
)sfsp
->f_blocks
;
12059 sfs
.f_bfree
= (user32_long_t
)sfsp
->f_bfree
;
12060 sfs
.f_bavail
= (user32_long_t
)sfsp
->f_bavail
;
12062 sfs
.f_files
= (user32_long_t
)sfsp
->f_files
;
12063 sfs
.f_ffree
= (user32_long_t
)sfsp
->f_ffree
;
12064 sfs
.f_fsid
= sfsp
->f_fsid
;
12065 sfs
.f_owner
= sfsp
->f_owner
;
12066 if (mp
->mnt_kern_flag
& MNTK_TYPENAME_OVERRIDE
) {
12067 strlcpy(&sfs
.f_fstypename
[0], &mp
->fstypename_override
[0], MFSNAMELEN
);
12069 strlcpy(&sfs
.f_fstypename
[0], &sfsp
->f_fstypename
[0], MFSNAMELEN
);
12071 strlcpy(&sfs
.f_mntonname
[0], &sfsp
->f_mntonname
[0], MNAMELEN
);
12072 strlcpy(&sfs
.f_mntfromname
[0], &sfsp
->f_mntfromname
[0], MNAMELEN
);
12074 if (partial_copy
) {
12075 copy_size
-= (sizeof(sfs
.f_reserved3
) + sizeof(sfs
.f_reserved4
));
12077 error
= copyout((caddr_t
)&sfs
, bufp
, copy_size
);
12080 if (sizep
!= NULL
) {
12087 * copy stat structure into user_stat structure.
12090 munge_user64_stat(struct stat
*sbp
, struct user64_stat
*usbp
)
12092 bzero(usbp
, sizeof(*usbp
));
12094 usbp
->st_dev
= sbp
->st_dev
;
12095 usbp
->st_ino
= sbp
->st_ino
;
12096 usbp
->st_mode
= sbp
->st_mode
;
12097 usbp
->st_nlink
= sbp
->st_nlink
;
12098 usbp
->st_uid
= sbp
->st_uid
;
12099 usbp
->st_gid
= sbp
->st_gid
;
12100 usbp
->st_rdev
= sbp
->st_rdev
;
12101 #ifndef _POSIX_C_SOURCE
12102 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
12103 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
12104 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
12105 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
12106 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
12107 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
12109 usbp
->st_atime
= sbp
->st_atime
;
12110 usbp
->st_atimensec
= sbp
->st_atimensec
;
12111 usbp
->st_mtime
= sbp
->st_mtime
;
12112 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
12113 usbp
->st_ctime
= sbp
->st_ctime
;
12114 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
12116 usbp
->st_size
= sbp
->st_size
;
12117 usbp
->st_blocks
= sbp
->st_blocks
;
12118 usbp
->st_blksize
= sbp
->st_blksize
;
12119 usbp
->st_flags
= sbp
->st_flags
;
12120 usbp
->st_gen
= sbp
->st_gen
;
12121 usbp
->st_lspare
= sbp
->st_lspare
;
12122 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
12123 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];
12127 munge_user32_stat(struct stat
*sbp
, struct user32_stat
*usbp
)
12129 bzero(usbp
, sizeof(*usbp
));
12131 usbp
->st_dev
= sbp
->st_dev
;
12132 usbp
->st_ino
= sbp
->st_ino
;
12133 usbp
->st_mode
= sbp
->st_mode
;
12134 usbp
->st_nlink
= sbp
->st_nlink
;
12135 usbp
->st_uid
= sbp
->st_uid
;
12136 usbp
->st_gid
= sbp
->st_gid
;
12137 usbp
->st_rdev
= sbp
->st_rdev
;
12138 #ifndef _POSIX_C_SOURCE
12139 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
12140 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
12141 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
12142 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
12143 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
12144 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
12146 usbp
->st_atime
= sbp
->st_atime
;
12147 usbp
->st_atimensec
= sbp
->st_atimensec
;
12148 usbp
->st_mtime
= sbp
->st_mtime
;
12149 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
12150 usbp
->st_ctime
= sbp
->st_ctime
;
12151 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
12153 usbp
->st_size
= sbp
->st_size
;
12154 usbp
->st_blocks
= sbp
->st_blocks
;
12155 usbp
->st_blksize
= sbp
->st_blksize
;
12156 usbp
->st_flags
= sbp
->st_flags
;
12157 usbp
->st_gen
= sbp
->st_gen
;
12158 usbp
->st_lspare
= sbp
->st_lspare
;
12159 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
12160 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];
12164 * copy stat64 structure into user_stat64 structure.
12167 munge_user64_stat64(struct stat64
*sbp
, struct user64_stat64
*usbp
)
12169 bzero(usbp
, sizeof(*usbp
));
12171 usbp
->st_dev
= sbp
->st_dev
;
12172 usbp
->st_ino
= sbp
->st_ino
;
12173 usbp
->st_mode
= sbp
->st_mode
;
12174 usbp
->st_nlink
= sbp
->st_nlink
;
12175 usbp
->st_uid
= sbp
->st_uid
;
12176 usbp
->st_gid
= sbp
->st_gid
;
12177 usbp
->st_rdev
= sbp
->st_rdev
;
12178 #ifndef _POSIX_C_SOURCE
12179 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
12180 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
12181 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
12182 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
12183 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
12184 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
12185 usbp
->st_birthtimespec
.tv_sec
= sbp
->st_birthtimespec
.tv_sec
;
12186 usbp
->st_birthtimespec
.tv_nsec
= sbp
->st_birthtimespec
.tv_nsec
;
12188 usbp
->st_atime
= sbp
->st_atime
;
12189 usbp
->st_atimensec
= sbp
->st_atimensec
;
12190 usbp
->st_mtime
= sbp
->st_mtime
;
12191 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
12192 usbp
->st_ctime
= sbp
->st_ctime
;
12193 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
12194 usbp
->st_birthtime
= sbp
->st_birthtime
;
12195 usbp
->st_birthtimensec
= sbp
->st_birthtimensec
;
12197 usbp
->st_size
= sbp
->st_size
;
12198 usbp
->st_blocks
= sbp
->st_blocks
;
12199 usbp
->st_blksize
= sbp
->st_blksize
;
12200 usbp
->st_flags
= sbp
->st_flags
;
12201 usbp
->st_gen
= sbp
->st_gen
;
12202 usbp
->st_lspare
= sbp
->st_lspare
;
12203 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
12204 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];
12208 munge_user32_stat64(struct stat64
*sbp
, struct user32_stat64
*usbp
)
12210 bzero(usbp
, sizeof(*usbp
));
12212 usbp
->st_dev
= sbp
->st_dev
;
12213 usbp
->st_ino
= sbp
->st_ino
;
12214 usbp
->st_mode
= sbp
->st_mode
;
12215 usbp
->st_nlink
= sbp
->st_nlink
;
12216 usbp
->st_uid
= sbp
->st_uid
;
12217 usbp
->st_gid
= sbp
->st_gid
;
12218 usbp
->st_rdev
= sbp
->st_rdev
;
12219 #ifndef _POSIX_C_SOURCE
12220 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
12221 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
12222 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
12223 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
12224 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
12225 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
12226 usbp
->st_birthtimespec
.tv_sec
= sbp
->st_birthtimespec
.tv_sec
;
12227 usbp
->st_birthtimespec
.tv_nsec
= sbp
->st_birthtimespec
.tv_nsec
;
12229 usbp
->st_atime
= sbp
->st_atime
;
12230 usbp
->st_atimensec
= sbp
->st_atimensec
;
12231 usbp
->st_mtime
= sbp
->st_mtime
;
12232 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
12233 usbp
->st_ctime
= sbp
->st_ctime
;
12234 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
12235 usbp
->st_birthtime
= sbp
->st_birthtime
;
12236 usbp
->st_birthtimensec
= sbp
->st_birthtimensec
;
12238 usbp
->st_size
= sbp
->st_size
;
12239 usbp
->st_blocks
= sbp
->st_blocks
;
12240 usbp
->st_blksize
= sbp
->st_blksize
;
12241 usbp
->st_flags
= sbp
->st_flags
;
12242 usbp
->st_gen
= sbp
->st_gen
;
12243 usbp
->st_lspare
= sbp
->st_lspare
;
12244 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
12245 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];
12249 * Purge buffer cache for simulating cold starts
12252 vnode_purge_callback(struct vnode
*vp
, __unused
void *cargs
)
12254 ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
/* off_t *resid_off */, UBC_PUSHALL
| UBC_INVALIDATE
);
12256 return VNODE_RETURNED
;
12260 vfs_purge_callback(mount_t mp
, __unused
void * arg
)
12262 vnode_iterate(mp
, VNODE_WAIT
| VNODE_ITERATE_ALL
, vnode_purge_callback
, NULL
);
12264 return VFS_RETURNED
;
12268 vfs_purge(__unused
struct proc
*p
, __unused
struct vfs_purge_args
*uap
, __unused
int32_t *retval
)
12270 if (!kauth_cred_issuser(kauth_cred_get())) {
12274 vfs_iterate(0 /* flags */, vfs_purge_callback
, NULL
);
12280 * gets the vnode associated with the (unnamed) snapshot directory
12281 * for a Filesystem. The snapshot directory vnode is returned with
12282 * an iocount on it.
12285 vnode_get_snapdir(vnode_t rvp
, vnode_t
*sdvpp
, vfs_context_t ctx
)
12287 return VFS_VGET_SNAPDIR(vnode_mount(rvp
), sdvpp
, ctx
);
12291 * Get the snapshot vnode.
12293 * If successful, the call returns with an iocount on *rvpp ,*sdvpp and
12294 * needs nameidone() on ndp.
12296 * If the snapshot vnode exists it is returned in ndp->ni_vp.
12298 * If it returns with an error, *rvpp, *sdvpp are NULL and nameidone() is
12302 vnode_get_snapshot(int dirfd
, vnode_t
*rvpp
, vnode_t
*sdvpp
,
12303 user_addr_t name
, struct nameidata
*ndp
, int32_t op
,
12304 #if !CONFIG_TRIGGERS
12307 enum path_operation pathop
,
12313 struct vfs_attr vfa
;
12318 error
= vnode_getfromfd(ctx
, dirfd
, rvpp
);
12323 if (!vnode_isvroot(*rvpp
)) {
12328 /* Make sure the filesystem supports snapshots */
12329 VFSATTR_INIT(&vfa
);
12330 VFSATTR_WANTED(&vfa
, f_capabilities
);
12331 if ((vfs_getattr(vnode_mount(*rvpp
), &vfa
, ctx
) != 0) ||
12332 !VFSATTR_IS_SUPPORTED(&vfa
, f_capabilities
) ||
12333 !((vfa
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] &
12334 VOL_CAP_INT_SNAPSHOT
)) ||
12335 !((vfa
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] &
12336 VOL_CAP_INT_SNAPSHOT
))) {
12341 error
= vnode_get_snapdir(*rvpp
, sdvpp
, ctx
);
12346 MALLOC(name_buf
, caddr_t
, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
12347 error
= copyinstr(name
, name_buf
, MAXPATHLEN
, &name_len
);
12353 * Some sanity checks- name can't be empty, "." or ".." or have slashes.
12354 * (the length returned by copyinstr includes the terminating NUL)
12356 if ((name_len
== 1) || (name_len
== 2 && name_buf
[0] == '.') ||
12357 (name_len
== 3 && name_buf
[0] == '.' && name_buf
[1] == '.')) {
12361 for (i
= 0; i
< (int)name_len
&& name_buf
[i
] != '/'; i
++) {
12364 if (i
< (int)name_len
) {
12370 if (op
== CREATE
) {
12371 error
= mac_mount_check_snapshot_create(ctx
, vnode_mount(*rvpp
),
12373 } else if (op
== DELETE
) {
12374 error
= mac_mount_check_snapshot_delete(ctx
, vnode_mount(*rvpp
),
12382 /* Check if the snapshot already exists ... */
12383 NDINIT(ndp
, op
, pathop
, USEDVP
| NOCACHE
| AUDITVNPATH1
,
12384 UIO_SYSSPACE
, CAST_USER_ADDR_T(name_buf
), ctx
);
12385 ndp
->ni_dvp
= *sdvpp
;
12387 error
= namei(ndp
);
12389 FREE(name_buf
, M_TEMP
);
12405 * create a filesystem snapshot (for supporting filesystems)
12407 * A much simplified version of openat(dirfd, name, O_CREAT | O_EXCL)
12408 * We get to the (unnamed) snapshot directory vnode and create the vnode
12409 * for the snapshot in it.
12413 * a) Passed in name for snapshot cannot have slashes.
12414 * b) name can't be "." or ".."
12416 * Since this requires superuser privileges, vnode_authorize calls are not
12420 snapshot_create(int dirfd
, user_addr_t name
, __unused
uint32_t flags
,
12423 vnode_t rvp
, snapdvp
;
12425 struct nameidata namend
;
12427 error
= vnode_get_snapshot(dirfd
, &rvp
, &snapdvp
, name
, &namend
, CREATE
,
12433 if (namend
.ni_vp
) {
12434 vnode_put(namend
.ni_vp
);
12437 struct vnode_attr va
;
12438 vnode_t vp
= NULLVP
;
12441 VATTR_SET(&va
, va_type
, VREG
);
12442 VATTR_SET(&va
, va_mode
, 0);
12444 error
= vn_create(snapdvp
, &vp
, &namend
, &va
,
12445 VN_CREATE_NOAUTH
| VN_CREATE_NOINHERIT
, 0, NULL
, ctx
);
12446 if (!error
&& vp
) {
12451 nameidone(&namend
);
12452 vnode_put(snapdvp
);
12458 * Delete a Filesystem snapshot
12460 * get the vnode for the unnamed snapshot directory and the snapshot and
12461 * delete the snapshot.
12464 snapshot_delete(int dirfd
, user_addr_t name
, __unused
uint32_t flags
,
12467 vnode_t rvp
, snapdvp
;
12469 struct nameidata namend
;
12471 error
= vnode_get_snapshot(dirfd
, &rvp
, &snapdvp
, name
, &namend
, DELETE
,
12477 error
= VNOP_REMOVE(snapdvp
, namend
.ni_vp
, &namend
.ni_cnd
,
12478 VNODE_REMOVE_SKIP_NAMESPACE_EVENT
, ctx
);
12480 vnode_put(namend
.ni_vp
);
12481 nameidone(&namend
);
12482 vnode_put(snapdvp
);
12489 * Revert a filesystem to a snapshot
12491 * Marks the filesystem to revert to the given snapshot on next mount.
12494 snapshot_revert(int dirfd
, user_addr_t name
, __unused
uint32_t flags
,
12500 struct fs_snapshot_revert_args revert_data
;
12501 struct componentname cnp
;
12505 error
= vnode_getfromfd(ctx
, dirfd
, &rvp
);
12509 mp
= vnode_mount(rvp
);
12511 MALLOC(name_buf
, caddr_t
, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
12512 error
= copyinstr(name
, name_buf
, MAXPATHLEN
, &name_len
);
12514 FREE(name_buf
, M_TEMP
);
12520 error
= mac_mount_check_snapshot_revert(ctx
, mp
, name_buf
);
12522 FREE(name_buf
, M_TEMP
);
12529 * Grab mount_iterref so that we can release the vnode,
12530 * since VFSIOC_REVERT_SNAPSHOT could conceivably cause a sync.
12532 error
= mount_iterref(mp
, 0);
12535 FREE(name_buf
, M_TEMP
);
12539 memset(&cnp
, 0, sizeof(cnp
));
12540 cnp
.cn_pnbuf
= (char *)name_buf
;
12541 cnp
.cn_nameiop
= LOOKUP
;
12542 cnp
.cn_flags
= ISLASTCN
| HASBUF
;
12543 cnp
.cn_pnlen
= MAXPATHLEN
;
12544 cnp
.cn_nameptr
= cnp
.cn_pnbuf
;
12545 cnp
.cn_namelen
= (int)name_len
;
12546 revert_data
.sr_cnp
= &cnp
;
12548 error
= VFS_IOCTL(mp
, VFSIOC_REVERT_SNAPSHOT
, (caddr_t
)&revert_data
, 0, ctx
);
12549 mount_iterdrop(mp
);
12550 FREE(name_buf
, M_TEMP
);
12553 /* If there was any error, try again using VNOP_IOCTL */
12556 struct nameidata namend
;
12558 error
= vnode_get_snapshot(dirfd
, &rvp
, &snapdvp
, name
, &namend
, LOOKUP
,
12565 error
= VNOP_IOCTL(namend
.ni_vp
, APFSIOC_REVERT_TO_SNAPSHOT
, (caddr_t
) NULL
,
12568 vnode_put(namend
.ni_vp
);
12569 nameidone(&namend
);
12570 vnode_put(snapdvp
);
12578 * rename a Filesystem snapshot
12580 * get the vnode for the unnamed snapshot directory and the snapshot and
12581 * rename the snapshot. This is a very specialised (and simple) case of
12582 * rename(2) (which has to deal with a lot more complications). It differs
12583 * slightly from rename(2) in that EEXIST is returned if the new name exists.
12586 snapshot_rename(int dirfd
, user_addr_t old
, user_addr_t
new,
12587 __unused
uint32_t flags
, vfs_context_t ctx
)
12589 vnode_t rvp
, snapdvp
;
12591 caddr_t newname_buf
;
12594 struct nameidata
*fromnd
, *tond
;
12595 /* carving out a chunk for structs that are too big to be on stack. */
12597 struct nameidata from_node
;
12598 struct nameidata to_node
;
12601 MALLOC(__rename_data
, void *, sizeof(*__rename_data
), M_TEMP
, M_WAITOK
);
12602 fromnd
= &__rename_data
->from_node
;
12603 tond
= &__rename_data
->to_node
;
12605 error
= vnode_get_snapshot(dirfd
, &rvp
, &snapdvp
, old
, fromnd
, DELETE
,
12610 fvp
= fromnd
->ni_vp
;
12612 MALLOC(newname_buf
, caddr_t
, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
12613 error
= copyinstr(new, newname_buf
, MAXPATHLEN
, &name_len
);
12619 * Some sanity checks- new name can't be empty, "." or ".." or have
12621 * (the length returned by copyinstr includes the terminating NUL)
12623 * The FS rename VNOP is suppossed to handle this but we'll pick it
12626 if ((name_len
== 1) || (name_len
== 2 && newname_buf
[0] == '.') ||
12627 (name_len
== 3 && newname_buf
[0] == '.' && newname_buf
[1] == '.')) {
12631 for (i
= 0; i
< (int)name_len
&& newname_buf
[i
] != '/'; i
++) {
12634 if (i
< (int)name_len
) {
12640 error
= mac_mount_check_snapshot_create(ctx
, vnode_mount(rvp
),
12647 NDINIT(tond
, RENAME
, OP_RENAME
, USEDVP
| NOCACHE
| AUDITVNPATH2
,
12648 UIO_SYSSPACE
, CAST_USER_ADDR_T(newname_buf
), ctx
);
12649 tond
->ni_dvp
= snapdvp
;
12651 error
= namei(tond
);
12654 } else if (tond
->ni_vp
) {
12656 * snapshot rename behaves differently than rename(2) - if the
12657 * new name exists, EEXIST is returned.
12659 vnode_put(tond
->ni_vp
);
12664 error
= VNOP_RENAME(snapdvp
, fvp
, &fromnd
->ni_cnd
, snapdvp
, NULLVP
,
12665 &tond
->ni_cnd
, ctx
);
12670 FREE(newname_buf
, M_TEMP
);
12672 vnode_put(snapdvp
);
12676 FREE(__rename_data
, M_TEMP
);
12681 * Mount a Filesystem snapshot
12683 * get the vnode for the unnamed snapshot directory and the snapshot and
12684 * mount the snapshot.
12687 snapshot_mount(int dirfd
, user_addr_t name
, user_addr_t directory
,
12688 __unused user_addr_t mnt_data
, __unused
uint32_t flags
, vfs_context_t ctx
)
12690 vnode_t rvp
, snapdvp
, snapvp
, vp
, pvp
;
12692 struct nameidata
*snapndp
, *dirndp
;
12693 /* carving out a chunk for structs that are too big to be on stack. */
12695 struct nameidata snapnd
;
12696 struct nameidata dirnd
;
12697 } * __snapshot_mount_data
;
12699 MALLOC(__snapshot_mount_data
, void *, sizeof(*__snapshot_mount_data
),
12701 snapndp
= &__snapshot_mount_data
->snapnd
;
12702 dirndp
= &__snapshot_mount_data
->dirnd
;
12704 error
= vnode_get_snapshot(dirfd
, &rvp
, &snapdvp
, name
, snapndp
, LOOKUP
,
12710 snapvp
= snapndp
->ni_vp
;
12711 if (!vnode_mount(rvp
) || (vnode_mount(rvp
) == dead_mountp
)) {
12716 /* Get the vnode to be covered */
12717 NDINIT(dirndp
, LOOKUP
, OP_MOUNT
, FOLLOW
| AUDITVNPATH1
| WANTPARENT
,
12718 UIO_USERSPACE
, directory
, ctx
);
12719 error
= namei(dirndp
);
12724 vp
= dirndp
->ni_vp
;
12725 pvp
= dirndp
->ni_dvp
;
12727 if ((vp
->v_flag
& VROOT
) && (vp
->v_mount
->mnt_flag
& MNT_ROOTFS
)) {
12730 mount_t mp
= vnode_mount(rvp
);
12731 struct fs_snapshot_mount_args smnt_data
;
12733 smnt_data
.sm_mp
= mp
;
12734 smnt_data
.sm_cnp
= &snapndp
->ni_cnd
;
12735 error
= mount_common(mp
->mnt_vfsstat
.f_fstypename
, pvp
, vp
,
12736 &dirndp
->ni_cnd
, CAST_USER_ADDR_T(&smnt_data
), flags
& MNT_DONTBROWSE
,
12737 KERNEL_MOUNT_SNAPSHOT
, NULL
, FALSE
, ctx
);
12745 vnode_put(snapdvp
);
12747 nameidone(snapndp
);
12749 FREE(__snapshot_mount_data
, M_TEMP
);
12754 * Root from a snapshot of the filesystem
12756 * Marks the filesystem to root from the given snapshot on next boot.
12759 snapshot_root(int dirfd
, user_addr_t name
, __unused
uint32_t flags
,
12765 struct fs_snapshot_root_args root_data
;
12766 struct componentname cnp
;
12770 error
= vnode_getfromfd(ctx
, dirfd
, &rvp
);
12774 mp
= vnode_mount(rvp
);
12776 MALLOC(name_buf
, caddr_t
, MAXPATHLEN
, M_TEMP
, M_WAITOK
);
12777 error
= copyinstr(name
, name_buf
, MAXPATHLEN
, &name_len
);
12779 FREE(name_buf
, M_TEMP
);
12784 // XXX MAC checks ?
12787 * Grab mount_iterref so that we can release the vnode,
12788 * since VFSIOC_ROOT_SNAPSHOT could conceivably cause a sync.
12790 error
= mount_iterref(mp
, 0);
12793 FREE(name_buf
, M_TEMP
);
12797 memset(&cnp
, 0, sizeof(cnp
));
12798 cnp
.cn_pnbuf
= (char *)name_buf
;
12799 cnp
.cn_nameiop
= LOOKUP
;
12800 cnp
.cn_flags
= ISLASTCN
| HASBUF
;
12801 cnp
.cn_pnlen
= MAXPATHLEN
;
12802 cnp
.cn_nameptr
= cnp
.cn_pnbuf
;
12803 cnp
.cn_namelen
= (int)name_len
;
12804 root_data
.sr_cnp
= &cnp
;
12806 error
= VFS_IOCTL(mp
, VFSIOC_ROOT_SNAPSHOT
, (caddr_t
)&root_data
, 0, ctx
);
12808 mount_iterdrop(mp
);
12809 FREE(name_buf
, M_TEMP
);
12815 * FS snapshot operations dispatcher
12818 fs_snapshot(__unused proc_t p
, struct fs_snapshot_args
*uap
,
12819 __unused
int32_t *retval
)
12822 vfs_context_t ctx
= vfs_context_current();
12824 AUDIT_ARG(fd
, uap
->dirfd
);
12825 AUDIT_ARG(value32
, uap
->op
);
12827 error
= priv_check_cred(vfs_context_ucred(ctx
), PRIV_VFS_SNAPSHOT
, 0);
12833 * Enforce user authorization for snapshot modification operations
12835 if ((uap
->op
!= SNAPSHOT_OP_MOUNT
) &&
12836 (uap
->op
!= SNAPSHOT_OP_ROOT
)) {
12837 vnode_t dvp
= NULLVP
;
12838 vnode_t devvp
= NULLVP
;
12841 error
= vnode_getfromfd(ctx
, uap
->dirfd
, &dvp
);
12845 mp
= vnode_mount(dvp
);
12846 devvp
= mp
->mnt_devvp
;
12848 /* get an iocount on devvp */
12849 if (devvp
== NULLVP
) {
12850 error
= vnode_lookup(mp
->mnt_vfsstat
.f_mntfromname
, 0, &devvp
, ctx
);
12851 /* for mounts which arent block devices */
12852 if (error
== ENOENT
) {
12856 error
= vnode_getwithref(devvp
);
12864 if ((vfs_context_issuser(ctx
) == 0) &&
12865 (vnode_authorize(devvp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
) != 0)) {
12877 case SNAPSHOT_OP_CREATE
:
12878 error
= snapshot_create(uap
->dirfd
, uap
->name1
, uap
->flags
, ctx
);
12880 case SNAPSHOT_OP_DELETE
:
12881 error
= snapshot_delete(uap
->dirfd
, uap
->name1
, uap
->flags
, ctx
);
12883 case SNAPSHOT_OP_RENAME
:
12884 error
= snapshot_rename(uap
->dirfd
, uap
->name1
, uap
->name2
,
12887 case SNAPSHOT_OP_MOUNT
:
12888 error
= snapshot_mount(uap
->dirfd
, uap
->name1
, uap
->name2
,
12889 uap
->data
, uap
->flags
, ctx
);
12891 case SNAPSHOT_OP_REVERT
:
12892 error
= snapshot_revert(uap
->dirfd
, uap
->name1
, uap
->flags
, ctx
);
12894 #if CONFIG_MNT_ROOTSNAP
12895 case SNAPSHOT_OP_ROOT
:
12896 error
= snapshot_root(uap
->dirfd
, uap
->name1
, uap
->flags
, ctx
);
12898 #endif /* CONFIG_MNT_ROOTSNAP */