2 * Copyright (c) 1995-2008 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/sysproto.h>
96 #include <sys/xattr.h>
97 #include <sys/ubc_internal.h>
99 #include <machine/cons.h>
100 #include <machine/limits.h>
101 #include <miscfs/specfs/specdev.h>
102 #include <miscfs/union/union.h>
104 #include <bsm/audit_kernel.h>
105 #include <bsm/audit_kevents.h>
107 #include <mach/mach_types.h>
108 #include <kern/kern_types.h>
109 #include <kern/kalloc.h>
111 #include <vm/vm_pageout.h>
113 #include <libkern/OSAtomic.h>
116 #include <security/mac.h>
117 #include <security/mac_framework.h>
121 #define GET_PATH(x) \
122 (x) = get_pathbuff();
123 #define RELEASE_PATH(x) \
126 #define GET_PATH(x) \
127 MALLOC_ZONE((x), char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
128 #define RELEASE_PATH(x) \
129 FREE_ZONE((x), MAXPATHLEN, M_NAMEI);
130 #endif /* CONFIG_FSE */
132 /* struct for checkdirs iteration */
137 /* callback for checkdirs iteration */
138 static int checkdirs_callback(proc_t p
, void * arg
);
140 static int change_dir(struct nameidata
*ndp
, vfs_context_t ctx
);
141 static int checkdirs(vnode_t olddp
, vfs_context_t ctx
);
142 void enablequotas(struct mount
*mp
, vfs_context_t ctx
);
143 static int getfsstat_callback(mount_t mp
, void * arg
);
144 static int getutimes(user_addr_t usrtvp
, struct timespec
*tsp
);
145 static int setutimes(vfs_context_t ctx
, vnode_t vp
, const struct timespec
*ts
, int nullflag
);
146 static int sync_callback(mount_t
, void *);
147 static int munge_statfs(struct mount
*mp
, struct vfsstatfs
*sfsp
,
148 user_addr_t bufp
, int *sizep
, boolean_t is_64_bit
,
149 boolean_t partial_copy
);
150 static int statfs64_common(struct mount
*mp
, struct vfsstatfs
*sfsp
, user_addr_t bufp
);
151 int (*union_dircheckp
)(struct vnode
**, struct fileproc
*, vfs_context_t
);
154 int sync_internal(void);
157 int open1(vfs_context_t
, struct nameidata
*, int, struct vnode_attr
*, register_t
*);
160 int unlink1(vfs_context_t
, struct nameidata
*, int);
163 #ifdef __APPLE_API_OBSOLETE
165 int fd
; /* file descriptor of the target file */
166 struct vstat
*vsb
; /* vstat structure for returned info */
169 const char *path
; /* pathname of the target file */
170 struct vstat
*vsb
; /* vstat structure for returned info */
172 struct mkcomplex_args
{
173 const char *path
; /* pathname of the file to be created */
174 mode_t mode
; /* access mode for the newly created file */
175 u_long type
; /* format of the complex file */
178 const char *path
; /* pathname of the target file */
179 struct vstat
*vsb
; /* vstat structure for returned info */
182 int fstatv(proc_t p
, struct fstatv_args
*uap
, register_t
*retval
);
183 int lstatv(proc_t p
, struct lstatv_args
*uap
, register_t
*retval
);
184 int mkcomplex(proc_t p
, struct mkcomplex_args
*uap
, register_t
*retval
);
185 int statv(proc_t p
, struct statv_args
*uap
, register_t
*retval
);
187 #endif /* __APPLE_API_OBSOLETE */
190 * incremented each time a mount or unmount operation occurs
191 * used to invalidate the cached value of the rootvp in the
192 * mount structure utilized by cache_lookup_path
194 int mount_generation
= 0;
196 /* counts number of mount and unmount operations */
197 unsigned int vfs_nummntops
=0;
199 extern struct fileops vnops
;
200 extern errno_t
rmdir_remove_orphaned_appleDouble(vnode_t
, vfs_context_t
, int *);
204 * Virtual File System System Calls
208 * Mount a file system.
212 mount(proc_t p
, struct mount_args
*uap
, __unused register_t
*retval
)
214 struct __mac_mount_args muap
;
216 muap
.type
= uap
->type
;
217 muap
.path
= uap
->path
;
218 muap
.flags
= uap
->flags
;
219 muap
.data
= uap
->data
;
220 muap
.mac_p
= USER_ADDR_NULL
;
221 return (__mac_mount(p
, &muap
, retval
));
225 __mac_mount(struct proc
*p
, register struct __mac_mount_args
*uap
, __unused register_t
*retval
)
228 struct vnode
*devvp
= NULLVP
;
229 struct vnode
*device_vnode
= NULLVP
;
234 struct vfstable
*vfsp
= (struct vfstable
*)0;
236 struct vnode_attr va
;
237 vfs_context_t ctx
= vfs_context_current();
239 struct nameidata nd1
;
240 char fstypename
[MFSNAMELEN
];
242 user_addr_t devpath
= USER_ADDR_NULL
;
243 user_addr_t fsmountargs
= uap
->data
;
248 boolean_t is_rwlock_locked
= FALSE
;
250 AUDIT_ARG(fflags
, uap
->flags
);
252 is_64bit
= proc_is64bit(p
);
255 * Get vnode to be covered
257 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| FOLLOW
| AUDITVNPATH1
,
258 UIO_USERSPACE
, uap
->path
, ctx
);
264 if ((vp
->v_flag
& VROOT
) &&
265 (vp
->v_mount
->mnt_flag
& MNT_ROOTFS
))
266 uap
->flags
|= MNT_UPDATE
;
268 error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, &dummy
);
272 if (uap
->flags
& MNT_UPDATE
) {
273 if ((vp
->v_flag
& VROOT
) == 0) {
279 /* unmount in progress return error */
281 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
287 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
288 is_rwlock_locked
= TRUE
;
290 * We only allow the filesystem to be reloaded if it
291 * is currently mounted read-only.
293 if ((uap
->flags
& MNT_RELOAD
) &&
294 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
299 * Only root, or the user that did the original mount is
300 * permitted to update it.
302 if (mp
->mnt_vfsstat
.f_owner
!= kauth_cred_getuid(vfs_context_ucred(ctx
)) &&
303 (error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
))) {
307 error
= mac_mount_check_remount(ctx
, mp
);
309 lck_rw_done(&mp
->mnt_rwlock
);
314 * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV,
315 * and MNT_NOEXEC if mount point is already MNT_NOEXEC.
317 if (suser(vfs_context_ucred(ctx
), NULL
)) {
318 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
319 if (mp
->mnt_flag
& MNT_NOEXEC
)
320 uap
->flags
|= MNT_NOEXEC
;
325 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
327 vfsp
= mp
->mnt_vtable
;
331 * If the user is not root, ensure that they own the directory
332 * onto which we are attempting to mount.
335 VATTR_WANTED(&va
, va_uid
);
336 if ((error
= vnode_getattr(vp
, &va
, ctx
)) ||
337 (va
.va_uid
!= kauth_cred_getuid(vfs_context_ucred(ctx
)) &&
338 (error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
)))) {
342 * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV, and
343 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
345 if (suser(vfs_context_ucred(ctx
), NULL
)) {
346 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
347 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
)
348 uap
->flags
|= MNT_NOEXEC
;
350 if ( (error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
)) )
353 if ( (error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0)) )
356 if (vp
->v_type
!= VDIR
) {
361 /* XXXAUDIT: Should we capture the type on the error path as well? */
362 AUDIT_ARG(text
, fstypename
);
364 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
365 if (!strncmp(vfsp
->vfc_name
, fstypename
, MFSNAMELEN
))
373 error
= mac_mount_check_mount(ctx
, vp
,
374 &nd
.ni_cnd
, vfsp
->vfc_name
);
378 if (ISSET(vp
->v_flag
, VMOUNT
) && (vp
->v_mountedhere
!= NULL
)) {
383 SET(vp
->v_flag
, VMOUNT
);
387 * Allocate and initialize the filesystem.
389 MALLOC_ZONE(mp
, struct mount
*, (u_long
)sizeof(struct mount
),
391 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
394 /* Initialize the default IO constraints */
395 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
396 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
397 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
398 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
399 mp
->mnt_devblocksize
= DEV_BSIZE
;
400 mp
->mnt_alignmentmask
= PAGE_MASK
;
402 mp
->mnt_realrootvp
= NULLVP
;
403 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
405 TAILQ_INIT(&mp
->mnt_vnodelist
);
406 TAILQ_INIT(&mp
->mnt_workerqueue
);
407 TAILQ_INIT(&mp
->mnt_newvnodes
);
409 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
410 is_rwlock_locked
= TRUE
;
411 mp
->mnt_op
= vfsp
->vfc_vfsops
;
412 mp
->mnt_vtable
= vfsp
;
414 vfsp
->vfc_refcount
++;
416 //mp->mnt_stat.f_type = vfsp->vfc_typenum;
417 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
418 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSTYPENAMELEN
);
419 strncpy(mp
->mnt_vfsstat
.f_mntonname
, nd
.ni_cnd
.cn_pnbuf
, MAXPATHLEN
);
420 mp
->mnt_vnodecovered
= vp
;
421 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(vfs_context_ucred(ctx
));
422 mp
->mnt_devbsdunit
= LOWPRI_MAX_NUM_DEV
- 1;
424 /* XXX 3762912 hack to support HFS filesystem 'owner' - filesystem may update later */
425 vfs_setowner(mp
, KAUTH_UID_NONE
, KAUTH_GID_NONE
);
429 * Set the mount level flags.
431 if (uap
->flags
& MNT_RDONLY
)
432 mp
->mnt_flag
|= MNT_RDONLY
;
433 else if (mp
->mnt_flag
& MNT_RDONLY
)
434 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
435 mp
->mnt_flag
&= ~(MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
436 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
|
437 MNT_UNKNOWNPERMISSIONS
| MNT_DONTBROWSE
| MNT_AUTOMOUNTED
|
438 MNT_DEFWRITE
| MNT_NOATIME
| MNT_QUARANTINE
);
439 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
440 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
|
441 MNT_UNKNOWNPERMISSIONS
| MNT_DONTBROWSE
| MNT_AUTOMOUNTED
|
442 MNT_DEFWRITE
| MNT_NOATIME
| MNT_QUARANTINE
);
445 if (uap
->flags
& MNT_MULTILABEL
) {
446 if (vfsp
->vfc_vfsflags
& VFC_VFSNOMACLABEL
) {
450 mp
->mnt_flag
|= MNT_MULTILABEL
;
454 if (vfsp
->vfc_vfsflags
& VFC_VFSLOCALARGS
) {
456 if ( (error
= copyin(fsmountargs
, (caddr_t
)&devpath
, sizeof(devpath
))) )
458 fsmountargs
+= sizeof(devpath
);
461 if ( (error
= copyin(fsmountargs
, (caddr_t
)&tmp
, sizeof(tmp
))) )
463 /* munge into LP64 addr */
464 devpath
= CAST_USER_ADDR_T(tmp
);
465 fsmountargs
+= sizeof(tmp
);
468 /* if it is not update and device name needs to be parsed */
470 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, devpath
, ctx
);
471 if ( (error
= namei(&nd1
)) )
474 strncpy(mp
->mnt_vfsstat
.f_mntfromname
, nd1
.ni_cnd
.cn_pnbuf
, MAXPATHLEN
);
479 if (devvp
->v_type
!= VBLK
) {
483 if (major(devvp
->v_rdev
) >= nblkdev
) {
488 * If mount by non-root, then verify that user has necessary
489 * permissions on the device.
491 if (suser(vfs_context_ucred(ctx
), NULL
) != 0) {
492 accessmode
= KAUTH_VNODE_READ_DATA
;
493 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
494 accessmode
|= KAUTH_VNODE_WRITE_DATA
;
495 if ((error
= vnode_authorize(devvp
, NULL
, accessmode
, ctx
)) != 0)
499 if (devpath
&& ((uap
->flags
& MNT_UPDATE
) == 0)) {
500 if ( (error
= vnode_ref(devvp
)) )
503 * Disallow multiple mounts of the same device.
504 * Disallow mounting of a device that is currently in use
505 * (except for root, which might share swap device for miniroot).
506 * Flush out any old buffers remaining from a previous use.
508 if ( (error
= vfs_mountedon(devvp
)) )
511 if (vcount(devvp
) > 1 && !(vfs_flags(mp
) & MNT_ROOTFS
)) {
515 if ( (error
= VNOP_FSYNC(devvp
, MNT_WAIT
, ctx
)) ) {
519 if ( (error
= buf_invalidateblks(devvp
, BUF_WRITE_DATA
, 0, 0)) )
522 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
524 error
= mac_vnode_check_open(ctx
,
526 ronly
? FREAD
: FREAD
|FWRITE
);
530 if ( (error
= VNOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, ctx
)) )
533 mp
->mnt_devvp
= devvp
;
534 device_vnode
= devvp
;
536 if ((mp
->mnt_flag
& MNT_RDONLY
) && (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
538 * If upgrade to read-write by non-root, then verify
539 * that user has necessary permissions on the device.
541 device_vnode
= mp
->mnt_devvp
;
542 if (device_vnode
&& suser(vfs_context_ucred(ctx
), NULL
)) {
543 if ((error
= vnode_authorize(device_vnode
, NULL
,
544 KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, ctx
)) != 0)
548 device_vnode
= NULLVP
;
552 if ((uap
->flags
& MNT_UPDATE
) == 0) {
553 mac_mount_label_init(mp
);
554 mac_mount_label_associate(ctx
, mp
);
556 if (uap
->mac_p
!= USER_ADDR_NULL
) {
558 char *labelstr
= NULL
;
561 if ((uap
->flags
& MNT_UPDATE
) != 0) {
562 error
= mac_mount_check_label_update(
568 error
= copyin(uap
->mac_p
, &mac
, sizeof(mac
));
571 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
572 mac
.m_buflen
= mac32
.m_buflen
;
573 mac
.m_string
= CAST_USER_ADDR_T(mac32
.m_string
);
577 if ((mac
.m_buflen
> MAC_MAX_LABEL_BUF_LEN
) ||
578 (mac
.m_buflen
< 2)) {
582 MALLOC(labelstr
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
583 error
= copyinstr(mac
.m_string
, labelstr
, mac
.m_buflen
, &ulen
);
585 FREE(labelstr
, M_MACTEMP
);
588 AUDIT_ARG(mac_string
, labelstr
);
589 error
= mac_mount_label_internalize(mp
->mnt_mntlabel
, labelstr
);
590 FREE(labelstr
, M_MACTEMP
);
595 if (device_vnode
!= NULL
) {
596 VNOP_IOCTL(device_vnode
, DKIOCGETBSDUNIT
, (caddr_t
)&mp
->mnt_devbsdunit
, 0, NULL
);
597 mp
->mnt_devbsdunit
%= LOWPRI_MAX_NUM_DEV
;
601 * Mount the filesystem.
603 error
= VFS_MOUNT(mp
, device_vnode
, fsmountargs
, ctx
);
605 if (uap
->flags
& MNT_UPDATE
) {
606 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
607 mp
->mnt_flag
&= ~MNT_RDONLY
;
609 (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
610 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
613 vfs_event_signal(NULL
, VQ_UPDATE
, (intptr_t)NULL
);
614 lck_rw_done(&mp
->mnt_rwlock
);
615 is_rwlock_locked
= FALSE
;
617 enablequotas(mp
, ctx
);
621 * Put the new filesystem on the mount list after root.
624 struct vfs_attr vfsattr
;
626 if (vfs_flags(mp
) & MNT_MULTILABEL
) {
627 error
= VFS_ROOT(mp
, &rvp
, ctx
);
629 printf("%s() VFS_ROOT returned %d\n", __func__
, error
);
633 /* VFS_ROOT provides reference so needref = 0 */
634 error
= vnode_label(mp
, NULL
, rvp
, NULL
, 0, ctx
);
641 CLR(vp
->v_flag
, VMOUNT
);
642 vp
->v_mountedhere
= mp
;
646 * taking the name_cache_lock exclusively will
647 * insure that everyone is out of the fast path who
648 * might be trying to use a now stale copy of
649 * vp->v_mountedhere->mnt_realrootvp
650 * bumping mount_generation causes the cached values
659 error
= checkdirs(vp
, ctx
);
661 /* Unmount the filesystem as cdir/rdirs cannot be updated */
665 * there is no cleanup code here so I have made it void
666 * we need to revisit this
668 (void)VFS_START(mp
, 0, ctx
);
671 lck_rw_done(&mp
->mnt_rwlock
);
672 is_rwlock_locked
= FALSE
;
674 /* Check if this mounted file system supports EAs or named streams. */
675 /* Skip WebDAV file systems for now since they hang in VFS_GETATTR here. */
676 VFSATTR_INIT(&vfsattr
);
677 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
678 if (strncmp(mp
->mnt_vfsstat
.f_fstypename
, "webdav", sizeof("webdav")) != 0 &&
679 vfs_getattr(mp
, &vfsattr
, ctx
) == 0 &&
680 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
681 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
) &&
682 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_EXTENDED_ATTR
)) {
683 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
686 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
) &&
687 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] & VOL_CAP_INT_NAMEDSTREAMS
)) {
688 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
691 /* Check if this file system supports path from id lookups. */
692 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
) &&
693 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_PATH_FROM_ID
)) {
694 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
695 } else if (mp
->mnt_flag
& MNT_DOVOLFS
) {
696 /* Legacy MNT_DOVOLFS flag also implies path from id lookups. */
697 mp
->mnt_kern_flag
|= MNTK_PATH_FROM_ID
;
700 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSNATIVEXATTR
) {
701 mp
->mnt_kern_flag
|= MNTK_EXTENDED_ATTRS
;
703 if (mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSPREFLIGHT
) {
704 mp
->mnt_kern_flag
|= MNTK_UNMOUNT_PREFLIGHT
;
706 /* increment the operations count */
707 OSAddAtomic(1, (SInt32
*)&vfs_nummntops
);
708 enablequotas(mp
, ctx
);
711 device_vnode
->v_specflags
|= SI_MOUNTEDON
;
714 * cache the IO attributes for the underlying physical media...
715 * an error return indicates the underlying driver doesn't
716 * support all the queries necessary... however, reasonable
717 * defaults will have been set, so no reason to bail or care
719 vfs_init_io_attributes(device_vnode
, mp
);
722 /* Now that mount is setup, notify the listeners */
723 vfs_event_signal(NULL
, VQ_MOUNT
, (intptr_t)NULL
);
726 CLR(vp
->v_flag
, VMOUNT
);
729 mp
->mnt_vtable
->vfc_refcount
--;
733 VNOP_CLOSE(device_vnode
, ronly
? FREAD
: FREAD
|FWRITE
, ctx
);
734 vnode_rele(device_vnode
);
736 lck_rw_done(&mp
->mnt_rwlock
);
737 is_rwlock_locked
= FALSE
;
738 mount_lock_destroy(mp
);
740 mac_mount_label_destroy(mp
);
742 FREE_ZONE((caddr_t
)mp
, sizeof (struct mount
), M_MOUNT
);
747 * drop I/O count on covered 'vp' and
748 * on the device vp if there was one
750 if (devpath
&& devvp
)
756 (void)VFS_UNMOUNT(mp
, MNT_FORCE
, ctx
);
757 if (device_vnode
!= NULLVP
) {
758 VNOP_CLOSE(device_vnode
, mp
->mnt_flag
& MNT_RDONLY
? FREAD
: FREAD
|FWRITE
,
763 vp
->v_mountedhere
= (mount_t
) 0;
767 if (devpath
&& ((uap
->flags
& MNT_UPDATE
) == 0))
770 if (devpath
&& devvp
)
773 /* Release mnt_rwlock only when it was taken */
774 if (is_rwlock_locked
== TRUE
) {
775 lck_rw_done(&mp
->mnt_rwlock
);
779 mac_mount_label_destroy(mp
);
782 vfsp
->vfc_refcount
--;
784 FREE_ZONE((caddr_t
)mp
, sizeof (struct mount
), M_MOUNT
);
793 enablequotas(struct mount
*mp
, vfs_context_t ctx
)
795 struct nameidata qnd
;
797 char qfpath
[MAXPATHLEN
];
798 const char *qfname
= QUOTAFILENAME
;
799 const char *qfopsname
= QUOTAOPSNAME
;
800 const char *qfextension
[] = INITQFNAMES
;
802 /* XXX Shoulkd be an MNTK_ flag, instead of strncmp()'s */
803 if ((strncmp(mp
->mnt_vfsstat
.f_fstypename
, "hfs", sizeof("hfs")) != 0 )
804 && (strncmp( mp
->mnt_vfsstat
.f_fstypename
, "ufs", sizeof("ufs")) != 0))
808 * Enable filesystem disk quotas if necessary.
809 * We ignore errors as this should not interfere with final mount
811 for (type
=0; type
< MAXQUOTAS
; type
++) {
812 snprintf(qfpath
, sizeof(qfpath
), "%s/%s.%s", mp
->mnt_vfsstat
.f_mntonname
, qfopsname
, qfextension
[type
]);
813 NDINIT(&qnd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(qfpath
), ctx
);
814 if (namei(&qnd
) != 0)
815 continue; /* option file to trigger quotas is not present */
816 vnode_put(qnd
.ni_vp
);
818 snprintf(qfpath
, sizeof(qfpath
), "%s/%s.%s", mp
->mnt_vfsstat
.f_mntonname
, qfname
, qfextension
[type
]);
820 (void) VFS_QUOTACTL(mp
, QCMD(Q_QUOTAON
, type
), 0, qfpath
, ctx
);
827 checkdirs_callback(proc_t p
, void * arg
)
829 struct cdirargs
* cdrp
= (struct cdirargs
* )arg
;
830 vnode_t olddp
= cdrp
->olddp
;
831 vnode_t newdp
= cdrp
->newdp
;
832 struct filedesc
*fdp
;
836 int cdir_changed
= 0;
837 int rdir_changed
= 0;
840 * XXX Also needs to iterate each thread in the process to see if it
841 * XXX is using a per-thread current working directory, and, if so,
842 * XXX update that as well.
847 if (fdp
== (struct filedesc
*)0) {
849 return(PROC_RETURNED
);
851 fdp_cvp
= fdp
->fd_cdir
;
852 fdp_rvp
= fdp
->fd_rdir
;
855 if (fdp_cvp
== olddp
) {
862 if (fdp_rvp
== olddp
) {
869 if (cdir_changed
|| rdir_changed
) {
871 fdp
->fd_cdir
= fdp_cvp
;
872 fdp
->fd_rdir
= fdp_rvp
;
875 return(PROC_RETURNED
);
881 * Scan all active processes to see if any of them have a current
882 * or root directory onto which the new filesystem has just been
883 * mounted. If so, replace them with the new mount point.
886 checkdirs(vnode_t olddp
, vfs_context_t ctx
)
892 struct uthread
* uth
= get_bsdthread_info(current_thread());
894 if (olddp
->v_usecount
== 1)
896 if (uth
!= (struct uthread
*)0)
897 uth
->uu_notrigger
= 1;
898 err
= VFS_ROOT(olddp
->v_mountedhere
, &newdp
, ctx
);
899 if (uth
!= (struct uthread
*)0)
900 uth
->uu_notrigger
= 0;
904 panic("mount: lost mount: error %d", err
);
911 /* do not block for exec/fork trans as the vp in cwd & rootdir are not changing */
912 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
, checkdirs_callback
, (void *)&cdr
, NULL
, NULL
);
914 if (rootvnode
== olddp
) {
926 * Unmount a file system.
928 * Note: unmount takes a path to the vnode mounted on as argument,
929 * not special file (as before).
933 unmount(__unused proc_t p
, struct unmount_args
*uap
, __unused register_t
*retval
)
939 vfs_context_t ctx
= vfs_context_current();
941 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| FOLLOW
| AUDITVNPATH1
,
942 UIO_USERSPACE
, uap
->path
, ctx
);
951 error
= mac_mount_check_umount(ctx
, mp
);
958 * Must be the root of the filesystem
960 if ((vp
->v_flag
& VROOT
) == 0) {
966 /* safedounmount consumes the mount ref */
967 return (safedounmount(mp
, uap
->flags
, ctx
));
971 vfs_unmountbyfsid(fsid_t
* fsid
, int flags
, vfs_context_t ctx
)
975 mp
= mount_list_lookupby_fsid(fsid
, 0, 1);
976 if (mp
== (mount_t
)0) {
981 /* safedounmount consumes the mount ref */
982 return(safedounmount(mp
, flags
, ctx
));
987 * The mount struct comes with a mount ref which will be consumed.
988 * Do the actual file system unmount, prevent some common foot shooting.
991 safedounmount(struct mount
*mp
, int flags
, vfs_context_t ctx
)
994 proc_t p
= vfs_context_proc(ctx
);
997 * Only root, or the user that did the original mount is
998 * permitted to unmount this filesystem.
1000 if ((mp
->mnt_vfsstat
.f_owner
!= kauth_cred_getuid(kauth_cred_get())) &&
1001 (error
= suser(kauth_cred_get(), &p
->p_acflag
)))
1005 * Don't allow unmounting the root file system.
1007 if (mp
->mnt_flag
& MNT_ROOTFS
) {
1008 error
= EBUSY
; /* the root is always busy */
1012 return (dounmount(mp
, flags
, 1, ctx
));
1020 * Do the actual file system unmount.
1023 dounmount(struct mount
*mp
, int flags
, int withref
, vfs_context_t ctx
)
1025 vnode_t coveredvp
= (vnode_t
)0;
1028 int forcedunmount
= 0;
1030 struct vnode
*devvp
= NULLVP
;
1032 if (flags
& MNT_FORCE
)
1035 /* XXX post jaguar fix LK_DRAIN - then clean this up */
1036 if ((flags
& MNT_FORCE
)) {
1037 mp
->mnt_kern_flag
|= MNTK_FRCUNMOUNT
;
1038 mp
->mnt_lflag
|= MNT_LFORCE
;
1040 if (mp
->mnt_lflag
& MNT_LUNMOUNT
) {
1041 mp
->mnt_lflag
|= MNT_LWAIT
;
1044 msleep((caddr_t
)mp
, &mp
->mnt_mlock
, (PVFS
| PDROP
), "dounmount", NULL
);
1046 * The prior unmount attempt has probably succeeded.
1047 * Do not dereference mp here - returning EBUSY is safest.
1051 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
1052 mp
->mnt_lflag
|= MNT_LUNMOUNT
;
1053 mp
->mnt_flag
&=~ MNT_ASYNC
;
1055 * anyone currently in the fast path that
1056 * trips over the cached rootvp will be
1057 * dumped out and forced into the slow path
1058 * to regenerate a new cached value
1060 mp
->mnt_realrootvp
= NULLVP
;
1064 * taking the name_cache_lock exclusively will
1065 * insure that everyone is out of the fast path who
1066 * might be trying to use a now stale copy of
1067 * vp->v_mountedhere->mnt_realrootvp
1068 * bumping mount_generation causes the cached values
1073 name_cache_unlock();
1076 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
1080 fsevent_unmount(mp
); /* has to come first! */
1083 if (forcedunmount
== 0) {
1084 ubc_umount(mp
); /* release cached vnodes */
1085 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
1086 error
= VFS_SYNC(mp
, MNT_WAIT
, ctx
);
1089 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
1090 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
1091 mp
->mnt_lflag
&= ~MNT_LFORCE
;
1098 lflags
|= FORCECLOSE
;
1099 error
= vflush(mp
, NULLVP
, SKIPSWAP
| SKIPSYSTEM
| SKIPROOT
| lflags
);
1100 if ((forcedunmount
== 0) && error
) {
1102 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
1103 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
1104 mp
->mnt_lflag
&= ~MNT_LFORCE
;
1108 /* make sure there are no one in the mount iterations or lookup */
1109 mount_iterdrain(mp
);
1111 error
= VFS_UNMOUNT(mp
, flags
, ctx
);
1113 mount_iterreset(mp
);
1115 mp
->mnt_kern_flag
&= ~MNTK_UNMOUNT
;
1116 mp
->mnt_lflag
&= ~MNT_LUNMOUNT
;
1117 mp
->mnt_lflag
&= ~MNT_LFORCE
;
1121 /* increment the operations count */
1123 OSAddAtomic(1, (SInt32
*)&vfs_nummntops
);
1125 if ( mp
->mnt_devvp
&& mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSLOCALARGS
) {
1126 /* hold an io reference and drop the usecount before close */
1127 devvp
= mp
->mnt_devvp
;
1128 vnode_clearmountedon(devvp
);
1129 vnode_getalways(devvp
);
1131 VNOP_CLOSE(devvp
, mp
->mnt_flag
& MNT_RDONLY
? FREAD
: FREAD
|FWRITE
,
1135 lck_rw_done(&mp
->mnt_rwlock
);
1136 mount_list_remove(mp
);
1137 lck_rw_lock_exclusive(&mp
->mnt_rwlock
);
1139 /* mark the mount point hook in the vp but not drop the ref yet */
1140 if ((coveredvp
= mp
->mnt_vnodecovered
) != NULLVP
) {
1141 vnode_getwithref(coveredvp
);
1142 vnode_lock_spin(coveredvp
);
1143 coveredvp
->v_mountedhere
= (struct mount
*)0;
1144 vnode_unlock(coveredvp
);
1145 vnode_put(coveredvp
);
1149 mp
->mnt_vtable
->vfc_refcount
--;
1150 mount_list_unlock();
1152 cache_purgevfs(mp
); /* remove cache entries for this file sys */
1153 vfs_event_signal(NULL
, VQ_UNMOUNT
, (intptr_t)NULL
);
1155 mp
->mnt_lflag
|= MNT_LDEAD
;
1157 if (mp
->mnt_lflag
& MNT_LWAIT
) {
1159 * do the wakeup here
1160 * in case we block in mount_refdrain
1161 * which will drop the mount lock
1162 * and allow anyone blocked in vfs_busy
1163 * to wakeup and see the LDEAD state
1165 mp
->mnt_lflag
&= ~MNT_LWAIT
;
1166 wakeup((caddr_t
)mp
);
1170 if (mp
->mnt_lflag
& MNT_LWAIT
) {
1171 mp
->mnt_lflag
&= ~MNT_LWAIT
;
1175 lck_rw_done(&mp
->mnt_rwlock
);
1178 wakeup((caddr_t
)mp
);
1180 if ((coveredvp
!= NULLVP
)) {
1181 vnode_getwithref(coveredvp
);
1182 vnode_rele(coveredvp
);
1183 vnode_lock_spin(coveredvp
);
1184 if(mp
->mnt_crossref
== 0) {
1185 vnode_unlock(coveredvp
);
1186 mount_lock_destroy(mp
);
1188 mac_mount_label_destroy(mp
);
1190 FREE_ZONE((caddr_t
)mp
, sizeof (struct mount
), M_MOUNT
);
1192 coveredvp
->v_lflag
|= VL_MOUNTDEAD
;
1193 vnode_unlock(coveredvp
);
1195 vnode_put(coveredvp
);
1196 } else if (mp
->mnt_flag
& MNT_ROOTFS
) {
1197 mount_lock_destroy(mp
);
1199 mac_mount_label_destroy(mp
);
1201 FREE_ZONE((caddr_t
)mp
, sizeof (struct mount
), M_MOUNT
);
1203 panic("dounmount: no coveredvp");
1209 mount_dropcrossref(mount_t mp
, vnode_t dp
, int need_put
)
1213 if (mp
->mnt_crossref
< 0)
1214 panic("mount cross refs -ve");
1215 if (((dp
->v_lflag
& VL_MOUNTDEAD
) == VL_MOUNTDEAD
) && (mp
->mnt_crossref
== 0)) {
1216 dp
->v_lflag
&= ~VL_MOUNTDEAD
;
1218 vnode_put_locked(dp
);
1220 mount_lock_destroy(mp
);
1222 mac_mount_label_destroy(mp
);
1224 FREE_ZONE((caddr_t
)mp
, sizeof (struct mount
), M_MOUNT
);
1228 vnode_put_locked(dp
);
1234 * Sync each mounted filesystem.
1238 struct ctldebug debug0
= { "syncprt", &syncprt
};
1241 int print_vmpage_stat
=0;
1244 sync_callback(mount_t mp
, __unused
void * arg
)
1248 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
1249 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
1250 mp
->mnt_flag
&= ~MNT_ASYNC
;
1251 VFS_SYNC(mp
, MNT_NOWAIT
, vfs_context_current());
1253 mp
->mnt_flag
|= MNT_ASYNC
;
1255 return(VFS_RETURNED
);
1259 extern unsigned int vp_pagein
, vp_pgodirty
, vp_pgoclean
;
1260 extern unsigned int dp_pgins
, dp_pgouts
;
1264 sync(__unused proc_t p
, __unused
struct sync_args
*uap
, __unused register_t
*retval
)
1267 vfs_iterate(LK_NOWAIT
, sync_callback
, (void *)0);
1269 if(print_vmpage_stat
) {
1270 vm_countdirtypages();
1271 printf("VP: %d: %d: %d: %d: %d\n", vp_pgodirty
, vp_pgoclean
, vp_pagein
,
1272 dp_pgins
, dp_pgouts
);
1278 #endif /* DIAGNOSTIC */
1283 * Change filesystem quotas.
1286 static int quotactl_funneled(proc_t p
, struct quotactl_args
*uap
, register_t
*retval
);
1289 quotactl(proc_t p
, struct quotactl_args
*uap
, register_t
*retval
)
1291 boolean_t funnel_state
;
1294 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1295 error
= quotactl_funneled(p
, uap
, retval
);
1296 thread_funnel_set(kernel_flock
, funnel_state
);
1301 quotactl_funneled(proc_t p
, struct quotactl_args
*uap
, __unused register_t
*retval
)
1304 int error
, quota_cmd
, quota_status
;
1307 struct nameidata nd
;
1308 vfs_context_t ctx
= vfs_context_current();
1309 struct dqblk my_dqblk
;
1311 AUDIT_ARG(uid
, uap
->uid
, 0, 0, 0);
1312 AUDIT_ARG(cmd
, uap
->cmd
);
1313 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
1314 UIO_USERSPACE
, uap
->path
, ctx
);
1318 mp
= nd
.ni_vp
->v_mount
;
1319 vnode_put(nd
.ni_vp
);
1322 /* copyin any data we will need for downstream code */
1323 quota_cmd
= uap
->cmd
>> SUBCMDSHIFT
;
1325 switch (quota_cmd
) {
1327 /* uap->arg specifies a file from which to take the quotas */
1328 fnamelen
= MAXPATHLEN
;
1329 datap
= kalloc(MAXPATHLEN
);
1330 error
= copyinstr(uap
->arg
, datap
, MAXPATHLEN
, &fnamelen
);
1333 /* uap->arg is a pointer to a dqblk structure. */
1334 datap
= (caddr_t
) &my_dqblk
;
1338 /* uap->arg is a pointer to a dqblk structure. */
1339 datap
= (caddr_t
) &my_dqblk
;
1340 if (proc_is64bit(p
)) {
1341 struct user_dqblk my_dqblk64
;
1342 error
= copyin(uap
->arg
, (caddr_t
)&my_dqblk64
, sizeof (my_dqblk64
));
1344 munge_dqblk(&my_dqblk
, &my_dqblk64
, FALSE
);
1348 error
= copyin(uap
->arg
, (caddr_t
)&my_dqblk
, sizeof (my_dqblk
));
1352 /* uap->arg is a pointer to an integer */
1353 datap
= (caddr_t
) "a_status
;
1361 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
, datap
, ctx
);
1364 switch (quota_cmd
) {
1367 kfree(datap
, MAXPATHLEN
);
1370 /* uap->arg is a pointer to a dqblk structure we need to copy out to */
1372 if (proc_is64bit(p
)) {
1373 struct user_dqblk my_dqblk64
;
1374 munge_dqblk(&my_dqblk
, &my_dqblk64
, TRUE
);
1375 error
= copyout((caddr_t
)&my_dqblk64
, uap
->arg
, sizeof (my_dqblk64
));
1378 error
= copyout(datap
, uap
->arg
, sizeof (struct dqblk
));
1383 /* uap->arg is a pointer to an integer */
1385 error
= copyout(datap
, uap
->arg
, sizeof(quota_status
));
1396 quotactl(__unused proc_t p
, __unused
struct quotactl_args
*uap
, __unused register_t
*retval
)
1398 return (EOPNOTSUPP
);
1403 * Get filesystem statistics.
1405 * Returns: 0 Success
1407 * vfs_update_vfsstat:???
1408 * munge_statfs:EFAULT
1412 statfs(__unused proc_t p
, struct statfs_args
*uap
, __unused register_t
*retval
)
1415 struct vfsstatfs
*sp
;
1417 struct nameidata nd
;
1418 vfs_context_t ctx
= vfs_context_current();
1421 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| FOLLOW
| AUDITVNPATH1
,
1422 UIO_USERSPACE
, uap
->path
, ctx
);
1428 sp
= &mp
->mnt_vfsstat
;
1431 error
= vfs_update_vfsstat(mp
, ctx
, VFS_USER_EVENT
);
1436 error
= munge_statfs(mp
, sp
, uap
->buf
, NULL
, IS_64BIT_PROCESS(p
), TRUE
);
1441 * Get filesystem statistics.
1445 fstatfs(__unused proc_t p
, struct fstatfs_args
*uap
, __unused register_t
*retval
)
1449 struct vfsstatfs
*sp
;
1452 AUDIT_ARG(fd
, uap
->fd
);
1454 if ( (error
= file_vnode(uap
->fd
, &vp
)) )
1457 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1464 sp
= &mp
->mnt_vfsstat
;
1465 if ((error
= vfs_update_vfsstat(mp
,vfs_context_current(),VFS_USER_EVENT
)) != 0) {
1471 error
= munge_statfs(mp
, sp
, uap
->buf
, NULL
, IS_64BIT_PROCESS(p
), TRUE
);
1477 * Common routine to handle copying of statfs64 data to user space
1480 statfs64_common(struct mount
*mp
, struct vfsstatfs
*sfsp
, user_addr_t bufp
)
1483 struct statfs64 sfs
;
1485 bzero(&sfs
, sizeof(sfs
));
1487 sfs
.f_bsize
= sfsp
->f_bsize
;
1488 sfs
.f_iosize
= (int32_t)sfsp
->f_iosize
;
1489 sfs
.f_blocks
= sfsp
->f_blocks
;
1490 sfs
.f_bfree
= sfsp
->f_bfree
;
1491 sfs
.f_bavail
= sfsp
->f_bavail
;
1492 sfs
.f_files
= sfsp
->f_files
;
1493 sfs
.f_ffree
= sfsp
->f_ffree
;
1494 sfs
.f_fsid
= sfsp
->f_fsid
;
1495 sfs
.f_owner
= sfsp
->f_owner
;
1496 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
1497 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1498 sfs
.f_fssubtype
= sfsp
->f_fssubtype
;
1499 strlcpy(&sfs
.f_fstypename
[0], &sfsp
->f_fstypename
[0], MFSTYPENAMELEN
);
1500 strlcpy(&sfs
.f_mntonname
[0], &sfsp
->f_mntonname
[0], MAXPATHLEN
);
1501 strlcpy(&sfs
.f_mntfromname
[0], &sfsp
->f_mntfromname
[0], MAXPATHLEN
);
1503 error
= copyout((caddr_t
)&sfs
, bufp
, sizeof(sfs
));
1509 * Get file system statistics in 64-bit mode
1512 statfs64(__unused
struct proc
*p
, struct statfs64_args
*uap
, __unused register_t
*retval
)
1515 struct vfsstatfs
*sp
;
1517 struct nameidata nd
;
1518 vfs_context_t ctxp
= vfs_context_current();
1521 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| FOLLOW
| AUDITVNPATH1
,
1522 UIO_USERSPACE
, uap
->path
, ctxp
);
1528 sp
= &mp
->mnt_vfsstat
;
1531 error
= vfs_update_vfsstat(mp
, ctxp
, VFS_USER_EVENT
);
1536 error
= statfs64_common(mp
, sp
, uap
->buf
);
1542 * Get file system statistics in 64-bit mode
1545 fstatfs64(__unused
struct proc
*p
, struct fstatfs64_args
*uap
, __unused register_t
*retval
)
1549 struct vfsstatfs
*sp
;
1552 AUDIT_ARG(fd
, uap
->fd
);
1554 if ( (error
= file_vnode(uap
->fd
, &vp
)) )
1557 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1564 sp
= &mp
->mnt_vfsstat
;
1565 if ((error
= vfs_update_vfsstat(mp
, vfs_context_current(), VFS_USER_EVENT
)) != 0) {
1571 error
= statfs64_common(mp
, sp
, uap
->buf
);
1576 struct getfsstat_struct
{
1587 getfsstat_callback(mount_t mp
, void * arg
)
1590 struct getfsstat_struct
*fstp
= (struct getfsstat_struct
*)arg
;
1591 struct vfsstatfs
*sp
;
1593 vfs_context_t ctx
= vfs_context_current();
1595 if (fstp
->sfsp
&& fstp
->count
< fstp
->maxcount
) {
1596 sp
= &mp
->mnt_vfsstat
;
1598 * If MNT_NOWAIT is specified, do not refresh the
1599 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
1601 if (((fstp
->flags
& MNT_NOWAIT
) == 0 || (fstp
->flags
& MNT_WAIT
)) &&
1602 (error
= vfs_update_vfsstat(mp
, ctx
,
1604 KAUTH_DEBUG("vfs_update_vfsstat returned %d", error
);
1605 return(VFS_RETURNED
);
1609 * Need to handle LP64 version of struct statfs
1611 error
= munge_statfs(mp
, sp
, fstp
->sfsp
, &my_size
, IS_64BIT_PROCESS(vfs_context_proc(ctx
)), FALSE
);
1613 fstp
->error
= error
;
1614 return(VFS_RETURNED_DONE
);
1616 fstp
->sfsp
+= my_size
;
1619 error
= mac_mount_label_get(mp
, *fstp
->mp
);
1621 fstp
->error
= error
;
1622 return(VFS_RETURNED_DONE
);
1628 return(VFS_RETURNED
);
1632 * Get statistics on all filesystems.
1635 getfsstat(__unused proc_t p
, struct getfsstat_args
*uap
, int *retval
)
1637 struct __mac_getfsstat_args muap
;
1639 muap
.buf
= uap
->buf
;
1640 muap
.bufsize
= uap
->bufsize
;
1641 muap
.mac
= USER_ADDR_NULL
;
1643 muap
.flags
= uap
->flags
;
1645 return (__mac_getfsstat(p
, &muap
, retval
));
1649 __mac_getfsstat(__unused proc_t p
, struct __mac_getfsstat_args
*uap
, int *retval
)
1653 int count
, maxcount
;
1654 struct getfsstat_struct fst
;
1656 if (IS_64BIT_PROCESS(p
)) {
1657 maxcount
= uap
->bufsize
/ sizeof(struct user_statfs
);
1660 maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1668 if (uap
->mac
!= USER_ADDR_NULL
) {
1673 count
= (int)(uap
->macsize
/ (IS_64BIT_PROCESS(p
) ? 8 : 4));
1674 if (count
!= maxcount
)
1677 /* Copy in the array */
1678 MALLOC(mp0
, u_int32_t
*, uap
->macsize
, M_MACTEMP
, M_WAITOK
);
1679 error
= copyin(uap
->mac
, mp0
, uap
->macsize
);
1683 /* Normalize to an array of user_addr_t */
1684 MALLOC(mp
, user_addr_t
*, count
* sizeof(user_addr_t
), M_MACTEMP
, M_WAITOK
);
1685 for (i
= 0; i
< count
; i
++) {
1686 if (IS_64BIT_PROCESS(p
))
1687 mp
[i
] = ((user_addr_t
*)mp0
)[i
];
1689 mp
[i
] = (user_addr_t
)mp0
[i
];
1691 FREE(mp0
, M_MACTEMP
);
1698 fst
.flags
= uap
->flags
;
1701 fst
.maxcount
= maxcount
;
1704 vfs_iterate(0, getfsstat_callback
, &fst
);
1707 FREE(mp
, M_MACTEMP
);
1710 KAUTH_DEBUG("ERROR - %s gets %d", p
->p_comm
, fst
.error
);
1714 if (fst
.sfsp
&& fst
.count
> fst
.maxcount
)
1715 *retval
= fst
.maxcount
;
1717 *retval
= fst
.count
;
1722 getfsstat64_callback(mount_t mp
, void * arg
)
1724 struct getfsstat_struct
*fstp
= (struct getfsstat_struct
*)arg
;
1725 struct vfsstatfs
*sp
;
1728 if (fstp
->sfsp
&& fstp
->count
< fstp
->maxcount
) {
1729 sp
= &mp
->mnt_vfsstat
;
1731 * If MNT_NOWAIT is specified, do not refresh the
1732 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
1734 if (((fstp
->flags
& MNT_NOWAIT
) == 0 || (fstp
->flags
& MNT_WAIT
)) &&
1735 (error
= vfs_update_vfsstat(mp
, vfs_context_current(), VFS_USER_EVENT
))) {
1736 KAUTH_DEBUG("vfs_update_vfsstat returned %d", error
);
1737 return(VFS_RETURNED
);
1740 error
= statfs64_common(mp
, sp
, fstp
->sfsp
);
1742 fstp
->error
= error
;
1743 return(VFS_RETURNED_DONE
);
1745 fstp
->sfsp
+= sizeof(struct statfs64
);
1748 return(VFS_RETURNED
);
1752 * Get statistics on all file systems in 64 bit mode.
1755 getfsstat64(__unused proc_t p
, struct getfsstat64_args
*uap
, int *retval
)
1758 int count
, maxcount
;
1759 struct getfsstat_struct fst
;
1761 maxcount
= uap
->bufsize
/ sizeof(struct statfs64
);
1767 fst
.flags
= uap
->flags
;
1770 fst
.maxcount
= maxcount
;
1772 vfs_iterate(0, getfsstat64_callback
, &fst
);
1775 KAUTH_DEBUG("ERROR - %s gets %d", p
->p_comm
, fst
.error
);
1779 if (fst
.sfsp
&& fst
.count
> fst
.maxcount
)
1780 *retval
= fst
.maxcount
;
1782 *retval
= fst
.count
;
1787 #if COMPAT_GETFSSTAT
1788 ogetfsstat(proc_t p
, struct getfsstat_args
*uap
, register_t
*retval
)
1795 * Change current working directory to a given file descriptor.
1799 common_fchdir(proc_t p
, struct fchdir_args
*uap
, int per_thread
)
1801 struct filedesc
*fdp
= p
->p_fd
;
1807 vfs_context_t ctx
= vfs_context_current();
1809 if (per_thread
&& uap
->fd
== -1) {
1811 * Switching back from per-thread to per process CWD; verify we
1812 * in fact have one before proceeding. The only success case
1813 * for this code path is to return 0 preemptively after zapping
1814 * the thread structure contents.
1816 thread_t th
= vfs_context_thread(ctx
);
1818 uthread_t uth
= get_bsdthread_info(th
);
1820 uth
->uu_cdir
= NULLVP
;
1821 if (tvp
!= NULLVP
) {
1829 if ( (error
= file_vnode(uap
->fd
, &vp
)) )
1831 if ( (error
= vnode_getwithref(vp
)) ) {
1836 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1838 if (vp
->v_type
!= VDIR
) {
1844 error
= mac_vnode_check_chdir(ctx
, vp
);
1848 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
1852 while (!error
&& (mp
= vp
->v_mountedhere
) != NULL
) {
1853 if (vfs_busy(mp
, LK_NOWAIT
)) {
1857 error
= VFS_ROOT(mp
, &tdp
, ctx
);
1866 if ( (error
= vnode_ref(vp
)) )
1871 thread_t th
= vfs_context_thread(ctx
);
1873 uthread_t uth
= get_bsdthread_info(th
);
1876 OSBitOrAtomic(P_THCWD
, (UInt32
*)&p
->p_flag
);
1901 fchdir(proc_t p
, struct fchdir_args
*uap
, __unused register_t
*retval
)
1903 return common_fchdir(p
, uap
, 0);
1907 __pthread_fchdir(proc_t p
, struct __pthread_fchdir_args
*uap
, __unused register_t
*retval
)
1909 return common_fchdir(p
, (void *)uap
, 1);
1913 * Change current working directory (``.'').
1915 * Returns: 0 Success
1916 * change_dir:ENOTDIR
1918 * vnode_ref:ENOENT No such file or directory
1922 common_chdir(proc_t p
, struct chdir_args
*uap
, int per_thread
)
1924 struct filedesc
*fdp
= p
->p_fd
;
1926 struct nameidata nd
;
1928 vfs_context_t ctx
= vfs_context_current();
1930 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
1931 UIO_USERSPACE
, uap
->path
, ctx
);
1932 error
= change_dir(&nd
, ctx
);
1935 if ( (error
= vnode_ref(nd
.ni_vp
)) ) {
1936 vnode_put(nd
.ni_vp
);
1940 * drop the iocount we picked up in change_dir
1942 vnode_put(nd
.ni_vp
);
1945 thread_t th
= vfs_context_thread(ctx
);
1947 uthread_t uth
= get_bsdthread_info(th
);
1949 uth
->uu_cdir
= nd
.ni_vp
;
1950 OSBitOrAtomic(P_THCWD
, (UInt32
*)&p
->p_flag
);
1952 vnode_rele(nd
.ni_vp
);
1958 fdp
->fd_cdir
= nd
.ni_vp
;
1969 chdir(proc_t p
, struct chdir_args
*uap
, __unused register_t
*retval
)
1971 return common_chdir(p
, (void *)uap
, 0);
1975 __pthread_chdir(proc_t p
, struct __pthread_chdir_args
*uap
, __unused register_t
*retval
)
1977 return common_chdir(p
, (void *)uap
, 1);
1982 * Change notion of root (``/'') directory.
1986 chroot(proc_t p
, struct chroot_args
*uap
, __unused register_t
*retval
)
1988 struct filedesc
*fdp
= p
->p_fd
;
1990 struct nameidata nd
;
1992 vfs_context_t ctx
= vfs_context_current();
1994 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
1997 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
1998 UIO_USERSPACE
, uap
->path
, ctx
);
1999 error
= change_dir(&nd
, ctx
);
2004 error
= mac_vnode_check_chroot(ctx
, nd
.ni_vp
,
2007 vnode_put(nd
.ni_vp
);
2012 if ( (error
= vnode_ref(nd
.ni_vp
)) ) {
2013 vnode_put(nd
.ni_vp
);
2016 vnode_put(nd
.ni_vp
);
2020 fdp
->fd_rdir
= nd
.ni_vp
;
2021 fdp
->fd_flags
|= FD_CHROOT
;
2031 * Common routine for chroot and chdir.
2033 * Returns: 0 Success
2034 * ENOTDIR Not a directory
2035 * namei:??? [anything namei can return]
2036 * vnode_authorize:??? [anything vnode_authorize can return]
2039 change_dir(struct nameidata
*ndp
, vfs_context_t ctx
)
2044 if ((error
= namei(ndp
)))
2049 if (vp
->v_type
!= VDIR
) {
2055 error
= mac_vnode_check_chdir(ctx
, vp
);
2062 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
2072 * Check permissions, allocate an open file structure,
2073 * and call the device open routine if any.
2075 * Returns: 0 Success
2086 #warning XXX implement uid, gid
2088 open1(vfs_context_t ctx
, struct nameidata
*ndp
, int uflags
, struct vnode_attr
*vap
, register_t
*retval
)
2090 proc_t p
= vfs_context_proc(ctx
);
2091 uthread_t uu
= get_bsdthread_info(vfs_context_thread(ctx
));
2092 struct filedesc
*fdp
= p
->p_fd
;
2093 struct fileproc
*fp
;
2096 struct fileproc
*nfp
;
2097 int type
, indx
, error
;
2099 int no_controlling_tty
= 0;
2100 int deny_controlling_tty
= 0;
2101 struct session
*sessp
= SESSION_NULL
;
2102 struct vfs_context context
= *vfs_context_current(); /* local copy */
2106 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
2108 flags
= FFLAGS(uflags
);
2110 AUDIT_ARG(fflags
, oflags
);
2111 AUDIT_ARG(mode
, vap
->va_mode
);
2113 if ( (error
= falloc(p
, &nfp
, &indx
, ctx
)) ) {
2117 uu
->uu_dupfd
= -indx
- 1;
2119 if (!(p
->p_flag
& P_CONTROLT
)) {
2120 sessp
= proc_session(p
);
2121 no_controlling_tty
= 1;
2123 * If conditions would warrant getting a controlling tty if
2124 * the device being opened is a tty (see ttyopen in tty.c),
2125 * but the open flags deny it, set a flag in the session to
2128 if (SESS_LEADER(p
, sessp
) &&
2129 sessp
->s_ttyvp
== NULL
&&
2130 (flags
& O_NOCTTY
)) {
2131 session_lock(sessp
);
2132 sessp
->s_flags
|= S_NOCTTY
;
2133 session_unlock(sessp
);
2134 deny_controlling_tty
= 1;
2138 if ((error
= vn_open_auth(ndp
, &flags
, vap
))) {
2139 if ((error
== ENODEV
|| error
== ENXIO
) && (uu
->uu_dupfd
>= 0)){ /* XXX from fdopen */
2140 if ((error
= dupfdopen(fdp
, indx
, uu
->uu_dupfd
, flags
, error
)) == 0) {
2141 fp_drop(p
, indx
, NULL
, 0);
2143 if (deny_controlling_tty
) {
2144 session_lock(sessp
);
2145 sessp
->s_flags
&= ~S_NOCTTY
;
2146 session_unlock(sessp
);
2148 if (sessp
!= SESSION_NULL
)
2149 session_rele(sessp
);
2153 if (error
== ERESTART
)
2155 fp_free(p
, indx
, fp
);
2157 if (deny_controlling_tty
) {
2158 session_lock(sessp
);
2159 sessp
->s_flags
&= ~S_NOCTTY
;
2160 session_unlock(sessp
);
2162 if (sessp
!= SESSION_NULL
)
2163 session_rele(sessp
);
2169 fp
->f_fglob
->fg_flag
= flags
& (FMASK
| O_EVTONLY
);
2170 fp
->f_fglob
->fg_type
= DTYPE_VNODE
;
2171 fp
->f_fglob
->fg_ops
= &vnops
;
2172 fp
->f_fglob
->fg_data
= (caddr_t
)vp
;
2174 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
2175 lf
.l_whence
= SEEK_SET
;
2178 if (flags
& O_EXLOCK
)
2179 lf
.l_type
= F_WRLCK
;
2181 lf
.l_type
= F_RDLCK
;
2183 if ((flags
& FNONBLOCK
) == 0)
2186 error
= mac_file_check_lock(vfs_context_ucred(ctx
), fp
->f_fglob
,
2191 if ((error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, type
, ctx
)))
2193 fp
->f_fglob
->fg_flag
|= FHASLOCK
;
2196 /* try to truncate by setting the size attribute */
2197 if ((flags
& O_TRUNC
) && ((error
= vnode_setsize(vp
, (off_t
)0, 0, ctx
)) != 0))
2201 * If the open flags denied the acquisition of a controlling tty,
2202 * clear the flag in the session structure that prevented the lower
2203 * level code from assigning one.
2205 if (deny_controlling_tty
) {
2206 session_lock(sessp
);
2207 sessp
->s_flags
&= ~S_NOCTTY
;
2208 session_unlock(sessp
);
2212 * If a controlling tty was set by the tty line discipline, then we
2213 * want to set the vp of the tty into the session structure. We have
2214 * a race here because we can't get to the vp for the tp in ttyopen,
2215 * because it's not passed as a parameter in the open path.
2217 if (no_controlling_tty
&& (p
->p_flag
& P_CONTROLT
)) {
2220 session_lock(sessp
);
2221 ttyvp
= sessp
->s_ttyvp
;
2222 sessp
->s_ttyvp
= vp
;
2223 sessp
->s_ttyvid
= vnode_vid(vp
);
2224 session_unlock(sessp
);
2225 if (ttyvp
!= NULLVP
)
2232 procfdtbl_releasefd(p
, indx
, NULL
);
2233 fp_drop(p
, indx
, fp
, 1);
2238 if (sessp
!= SESSION_NULL
)
2239 session_rele(sessp
);
2242 if (deny_controlling_tty
) {
2243 session_lock(sessp
);
2244 sessp
->s_flags
&= ~S_NOCTTY
;
2245 session_unlock(sessp
);
2247 if (sessp
!= SESSION_NULL
)
2248 session_rele(sessp
);
2250 /* Modify local copy (to not damage thread copy) */
2251 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
2253 vn_close(vp
, fp
->f_fglob
->fg_flag
, &context
);
2255 fp_free(p
, indx
, fp
);
2262 * An open system call using an extended argument list compared to the regular
2263 * system call 'open'.
2265 * Parameters: p Process requesting the open
2266 * uap User argument descriptor (see below)
2267 * retval Pointer to an area to receive the
2268 * return calue from the system call
2270 * Indirect: uap->path Path to open (same as 'open')
2271 * uap->flags Flags to open (same as 'open'
2272 * uap->uid UID to set, if creating
2273 * uap->gid GID to set, if creating
2274 * uap->mode File mode, if creating (same as 'open')
2275 * uap->xsecurity ACL to set, if creating
2277 * Returns: 0 Success
2280 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
2282 * XXX: We should enummerate the possible errno values here, and where
2283 * in the code they originated.
2286 open_extended(proc_t p
, struct open_extended_args
*uap
, register_t
*retval
)
2288 struct filedesc
*fdp
= p
->p_fd
;
2290 kauth_filesec_t xsecdst
;
2291 struct vnode_attr va
;
2292 struct nameidata nd
;
2296 if ((uap
->xsecurity
!= USER_ADDR_NULL
) &&
2297 ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0))
2301 cmode
= ((uap
->mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
2302 VATTR_SET(&va
, va_mode
, cmode
);
2303 if (uap
->uid
!= KAUTH_UID_NONE
)
2304 VATTR_SET(&va
, va_uid
, uap
->uid
);
2305 if (uap
->gid
!= KAUTH_GID_NONE
)
2306 VATTR_SET(&va
, va_gid
, uap
->gid
);
2307 if (xsecdst
!= NULL
)
2308 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
2310 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
, uap
->path
, vfs_context_current());
2312 ciferror
= open1(vfs_context_current(), &nd
, uap
->flags
, &va
, retval
);
2313 if (xsecdst
!= NULL
)
2314 kauth_filesec_free(xsecdst
);
2320 open(proc_t p
, struct open_args
*uap
, register_t
*retval
)
2322 __pthread_testcancel(1);
2323 return(open_nocancel(p
, (struct open_nocancel_args
*)uap
, retval
));
2328 open_nocancel(proc_t p
, struct open_nocancel_args
*uap
, register_t
*retval
)
2330 struct filedesc
*fdp
= p
->p_fd
;
2331 struct vnode_attr va
;
2332 struct nameidata nd
;
2336 /* Mask off all but regular access permissions */
2337 cmode
= ((uap
->mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
2338 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
2340 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
, uap
->path
, vfs_context_current());
2342 return(open1(vfs_context_current(), &nd
, uap
->flags
, &va
, retval
));
2347 * Create a special file.
2349 static int mkfifo1(vfs_context_t ctx
, user_addr_t upath
, struct vnode_attr
*vap
);
2352 mknod(proc_t p
, struct mknod_args
*uap
, __unused register_t
*retval
)
2354 struct vnode_attr va
;
2355 vfs_context_t ctx
= vfs_context_current();
2358 struct nameidata nd
;
2362 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
2363 VATTR_SET(&va
, va_rdev
, uap
->dev
);
2365 /* If it's a mknod() of a FIFO, call mkfifo1() instead */
2366 if ((uap
->mode
& S_IFMT
) == S_IFIFO
)
2367 return(mkfifo1(ctx
, uap
->path
, &va
));
2369 AUDIT_ARG(mode
, uap
->mode
);
2370 AUDIT_ARG(dev
, uap
->dev
);
2372 if ((error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
)))
2374 NDINIT(&nd
, CREATE
, LOCKPARENT
| AUDITVNPATH1
,
2375 UIO_USERSPACE
, uap
->path
, ctx
);
2387 switch (uap
->mode
& S_IFMT
) {
2388 case S_IFMT
: /* used by badsect to flag bad sectors */
2389 VATTR_SET(&va
, va_type
, VBAD
);
2392 VATTR_SET(&va
, va_type
, VCHR
);
2395 VATTR_SET(&va
, va_type
, VBLK
);
2407 error
= mac_vnode_check_create(ctx
,
2408 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
2414 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
2418 error
= VNOP_WHITEOUT(dvp
, &nd
.ni_cnd
, CREATE
, ctx
);
2420 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, ctx
);
2426 int update_flags
= 0;
2428 // Make sure the name & parent pointers are hooked up
2429 if (vp
->v_name
== NULL
)
2430 update_flags
|= VNODE_UPDATE_NAME
;
2431 if (vp
->v_parent
== NULLVP
)
2432 update_flags
|= VNODE_UPDATE_PARENT
;
2435 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
2438 add_fsevent(FSE_CREATE_FILE
, ctx
,
2446 * nameidone has to happen before we vnode_put(dvp)
2447 * since it may need to release the fs_nodelock on the dvp
2459 * Create a named pipe.
2461 * Returns: 0 Success
2464 * vnode_authorize:???
2468 mkfifo1(vfs_context_t ctx
, user_addr_t upath
, struct vnode_attr
*vap
)
2472 struct nameidata nd
;
2474 NDINIT(&nd
, CREATE
, LOCKPARENT
| AUDITVNPATH1
,
2475 UIO_USERSPACE
, upath
, ctx
);
2482 /* check that this is a new file and authorize addition */
2487 VATTR_SET(vap
, va_type
, VFIFO
);
2490 error
= mac_vnode_check_create(ctx
, nd
.ni_dvp
,
2497 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
2501 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, vap
, 0, ctx
);
2504 * nameidone has to happen before we vnode_put(dvp)
2505 * since it may need to release the fs_nodelock on the dvp
2518 * A mkfifo system call using an extended argument list compared to the regular
2519 * system call 'mkfifo'.
2521 * Parameters: p Process requesting the open
2522 * uap User argument descriptor (see below)
2525 * Indirect: uap->path Path to fifo (same as 'mkfifo')
2526 * uap->uid UID to set
2527 * uap->gid GID to set
2528 * uap->mode File mode to set (same as 'mkfifo')
2529 * uap->xsecurity ACL to set, if creating
2531 * Returns: 0 Success
2534 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
2536 * XXX: We should enummerate the possible errno values here, and where
2537 * in the code they originated.
2540 mkfifo_extended(proc_t p
, struct mkfifo_extended_args
*uap
, __unused register_t
*retval
)
2543 kauth_filesec_t xsecdst
;
2544 struct vnode_attr va
;
2546 xsecdst
= KAUTH_FILESEC_NONE
;
2547 if (uap
->xsecurity
!= USER_ADDR_NULL
) {
2548 if ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)
2553 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
2554 if (uap
->uid
!= KAUTH_UID_NONE
)
2555 VATTR_SET(&va
, va_uid
, uap
->uid
);
2556 if (uap
->gid
!= KAUTH_GID_NONE
)
2557 VATTR_SET(&va
, va_gid
, uap
->gid
);
2558 if (xsecdst
!= KAUTH_FILESEC_NONE
)
2559 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
2561 ciferror
= mkfifo1(vfs_context_current(), uap
->path
, &va
);
2563 if (xsecdst
!= KAUTH_FILESEC_NONE
)
2564 kauth_filesec_free(xsecdst
);
2570 mkfifo(proc_t p
, struct mkfifo_args
*uap
, __unused register_t
*retval
)
2572 struct vnode_attr va
;
2575 VATTR_SET(&va
, va_mode
, (uap
->mode
& ALLPERMS
) & ~p
->p_fd
->fd_cmask
);
2577 return(mkfifo1(vfs_context_current(), uap
->path
, &va
));
2581 * Make a hard file link.
2583 * Returns: 0 Success
2588 * vnode_authorize:???
2593 link(__unused proc_t p
, struct link_args
*uap
, __unused register_t
*retval
)
2595 vnode_t vp
, dvp
, lvp
;
2596 struct nameidata nd
;
2597 vfs_context_t ctx
= vfs_context_current();
2600 int need_event
, has_listeners
;
2601 char *target_path
= NULL
;
2603 vp
= dvp
= lvp
= NULLVP
;
2605 /* look up the object we are linking to */
2606 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
2607 UIO_USERSPACE
, uap
->path
, ctx
);
2616 * Normally, linking to directories is not supported.
2617 * However, some file systems may have limited support.
2619 if (vp
->v_type
== VDIR
) {
2620 if (!(vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSDIRLINKS
)) {
2621 error
= EPERM
; /* POSIX */
2624 /* Linking to a directory requires ownership. */
2625 if (!kauth_cred_issuser(vfs_context_ucred(ctx
))) {
2626 struct vnode_attr dva
;
2629 VATTR_WANTED(&dva
, va_uid
);
2630 if (vnode_getattr(vp
, &dva
, ctx
) != 0 ||
2631 !VATTR_IS_SUPPORTED(&dva
, va_uid
) ||
2632 (dva
.va_uid
!= kauth_cred_getuid(vfs_context_ucred(ctx
)))) {
2639 /* lookup the target node */
2640 nd
.ni_cnd
.cn_nameiop
= CREATE
;
2641 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| AUDITVNPATH2
| CN_NBMOUNTLOOK
;
2642 nd
.ni_dirp
= uap
->link
;
2650 if ((error
= mac_vnode_check_link(ctx
, dvp
, vp
, &nd
.ni_cnd
)) != 0)
2654 /* or to anything that kauth doesn't want us to (eg. immutable items) */
2655 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_LINKTARGET
, ctx
)) != 0)
2658 /* target node must not exist */
2659 if (lvp
!= NULLVP
) {
2663 /* cannot link across mountpoints */
2664 if (vnode_mount(vp
) != vnode_mount(dvp
)) {
2669 /* authorize creation of the target note */
2670 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
2673 /* and finally make the link */
2674 error
= VNOP_LINK(vp
, dvp
, &nd
.ni_cnd
, ctx
);
2679 need_event
= need_fsevent(FSE_CREATE_FILE
, dvp
);
2683 has_listeners
= kauth_authorize_fileop_has_listeners();
2685 if (need_event
|| has_listeners
) {
2686 char *link_to_path
= NULL
;
2687 int len
, link_name_len
;
2689 /* build the path to the new link file */
2690 GET_PATH(target_path
);
2691 if (target_path
== NULL
) {
2697 vn_getpath(dvp
, target_path
, &len
);
2698 if ((len
+ 1 + nd
.ni_cnd
.cn_namelen
+ 1) < MAXPATHLEN
) {
2699 target_path
[len
-1] = '/';
2700 strlcpy(&target_path
[len
], nd
.ni_cnd
.cn_nameptr
, MAXPATHLEN
-len
);
2701 len
+= nd
.ni_cnd
.cn_namelen
;
2704 if (has_listeners
) {
2705 /* build the path to file we are linking to */
2706 GET_PATH(link_to_path
);
2707 if (link_to_path
== NULL
) {
2712 link_name_len
= MAXPATHLEN
;
2713 vn_getpath(vp
, link_to_path
, &link_name_len
);
2716 * Call out to allow 3rd party notification of rename.
2717 * Ignore result of kauth_authorize_fileop call.
2719 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_LINK
,
2720 (uintptr_t)link_to_path
, (uintptr_t)target_path
);
2721 if (link_to_path
!= NULL
) {
2722 RELEASE_PATH(link_to_path
);
2727 /* construct fsevent */
2728 if (get_fse_info(vp
, &finfo
, ctx
) == 0) {
2729 // build the path to the destination of the link
2730 add_fsevent(FSE_CREATE_FILE
, ctx
,
2731 FSE_ARG_STRING
, len
, target_path
,
2732 FSE_ARG_FINFO
, &finfo
,
2740 * nameidone has to happen before we vnode_put(dvp)
2741 * since it may need to release the fs_nodelock on the dvp
2744 if (target_path
!= NULL
) {
2745 RELEASE_PATH(target_path
);
2757 * Make a symbolic link.
2759 * We could add support for ACLs here too...
2763 symlink(proc_t p
, struct symlink_args
*uap
, __unused register_t
*retval
)
2765 struct vnode_attr va
;
2768 struct nameidata nd
;
2769 vfs_context_t ctx
= vfs_context_current();
2773 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2774 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, &dummy
);
2777 AUDIT_ARG(text
, path
); /* This is the link string */
2779 NDINIT(&nd
, CREATE
, LOCKPARENT
| AUDITVNPATH1
,
2780 UIO_USERSPACE
, uap
->link
, ctx
);
2788 VATTR_SET(&va
, va_type
, VLNK
);
2789 VATTR_SET(&va
, va_mode
, ACCESSPERMS
& ~p
->p_fd
->fd_cmask
);
2791 error
= mac_vnode_check_create(ctx
,
2792 dvp
, &nd
.ni_cnd
, &va
);
2805 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
);
2806 /* get default ownership, etc. */
2808 error
= vnode_authattr_new(dvp
, &va
, 0, ctx
);
2810 error
= VNOP_SYMLINK(dvp
, &vp
, &nd
.ni_cnd
, &va
, path
, ctx
);
2812 /* do fallback attribute handling */
2814 error
= vnode_setattr_fallback(vp
, &va
, ctx
);
2817 int update_flags
= 0;
2820 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
2821 nd
.ni_cnd
.cn_flags
= 0;
2829 #if 0 /* XXX - kauth_todo - is KAUTH_FILEOP_SYMLINK needed? */
2830 /* call out to allow 3rd party notification of rename.
2831 * Ignore result of kauth_authorize_fileop call.
2833 if (kauth_authorize_fileop_has_listeners() &&
2835 char *new_link_path
= NULL
;
2838 /* build the path to the new link file */
2839 new_link_path
= get_pathbuff();
2841 vn_getpath(dvp
, new_link_path
, &len
);
2842 if ((len
+ 1 + nd
.ni_cnd
.cn_namelen
+ 1) < MAXPATHLEN
) {
2843 new_link_path
[len
- 1] = '/';
2844 strlcpy(&new_link_path
[len
], nd
.ni_cnd
.cn_nameptr
, MAXPATHLEN
-len
);
2847 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_SYMLINK
,
2848 (uintptr_t)path
, (uintptr_t)new_link_path
);
2849 if (new_link_path
!= NULL
)
2850 release_pathbuff(new_link_path
);
2853 // Make sure the name & parent pointers are hooked up
2854 if (vp
->v_name
== NULL
)
2855 update_flags
|= VNODE_UPDATE_NAME
;
2856 if (vp
->v_parent
== NULLVP
)
2857 update_flags
|= VNODE_UPDATE_PARENT
;
2860 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
2863 add_fsevent(FSE_CREATE_FILE
, ctx
,
2871 * nameidone has to happen before we vnode_put(dvp)
2872 * since it may need to release the fs_nodelock on the dvp
2880 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2886 * Delete a whiteout from the filesystem.
2889 #warning XXX authorization not implmented for whiteouts
2891 undelete(__unused proc_t p
, struct undelete_args
*uap
, __unused register_t
*retval
)
2894 struct nameidata nd
;
2895 vfs_context_t ctx
= vfs_context_current();
2898 NDINIT(&nd
, DELETE
, LOCKPARENT
|DOWHITEOUT
|AUDITVNPATH1
,
2899 UIO_USERSPACE
, uap
->path
, ctx
);
2906 if (vp
== NULLVP
&& (nd
.ni_cnd
.cn_flags
& ISWHITEOUT
)) {
2907 error
= VNOP_WHITEOUT(dvp
, &nd
.ni_cnd
, DELETE
, ctx
);
2912 * nameidone has to happen before we vnode_put(dvp)
2913 * since it may need to release the fs_nodelock on the dvp
2925 * Delete a name from the filesystem.
2929 unlink1(vfs_context_t ctx
, struct nameidata
*ndp
, int nodelbusy
)
2933 struct componentname
*cnp
;
2939 int has_listeners
= 0;
2941 ndp
->ni_cnd
.cn_flags
|= LOCKPARENT
;
2950 /* With Carbon delete semantics, busy files cannot be deleted */
2952 flags
|= VNODE_REMOVE_NODELETEBUSY
;
2956 * Normally, unlinking of directories is not supported.
2957 * However, some file systems may have limited support.
2959 if ((vp
->v_type
== VDIR
) &&
2960 !(vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSDIRLINKS
)) {
2961 error
= EPERM
; /* POSIX */
2965 * The root of a mounted filesystem cannot be deleted.
2967 if (vp
->v_flag
& VROOT
) {
2974 /* authorize the delete operation */
2977 error
= mac_vnode_check_unlink(ctx
,
2981 error
= vnode_authorize(vp
, ndp
->ni_dvp
, KAUTH_VNODE_DELETE
, ctx
);
2986 need_event
= need_fsevent(FSE_DELETE
, dvp
);
2988 if ((vp
->v_flag
& VISHARDLINK
) == 0) {
2989 get_fse_info(vp
, &finfo
, ctx
);
2993 has_listeners
= kauth_authorize_fileop_has_listeners();
2994 if (need_event
|| has_listeners
) {
3001 vn_getpath(vp
, path
, &len
);
3005 if (ndp
->ni_cnd
.cn_flags
& CN_WANTSRSRCFORK
)
3006 error
= vnode_removenamedstream(dvp
, vp
, XATTR_RESOURCEFORK_NAME
, 0, ctx
);
3009 error
= VNOP_REMOVE(dvp
, vp
, &ndp
->ni_cnd
, flags
, ctx
);
3012 * Call out to allow 3rd party notification of delete.
3013 * Ignore result of kauth_authorize_fileop call.
3016 if (has_listeners
) {
3017 kauth_authorize_fileop(vfs_context_ucred(ctx
),
3018 KAUTH_FILEOP_DELETE
,
3023 if (vp
->v_flag
& VISHARDLINK
) {
3025 // if a hardlink gets deleted we want to blow away the
3026 // v_parent link because the path that got us to this
3027 // instance of the link is no longer valid. this will
3028 // force the next call to get the path to ask the file
3029 // system instead of just following the v_parent link.
3031 vnode_update_identity(vp
, NULL
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
3036 if (vp
->v_flag
& VISHARDLINK
) {
3037 get_fse_info(vp
, &finfo
, ctx
);
3039 add_fsevent(FSE_DELETE
, ctx
,
3040 FSE_ARG_STRING
, len
, path
,
3041 FSE_ARG_FINFO
, &finfo
,
3050 * nameidone has to happen before we vnode_put(dvp)
3051 * since it may need to release the fs_nodelock on the dvp
3061 * Delete a name from the filesystem using POSIX semantics.
3064 unlink(__unused proc_t p
, struct unlink_args
*uap
, __unused register_t
*retval
)
3066 struct nameidata nd
;
3067 vfs_context_t ctx
= vfs_context_current();
3069 NDINIT(&nd
, DELETE
, AUDITVNPATH1
, UIO_USERSPACE
, uap
->path
, ctx
);
3070 return unlink1(ctx
, &nd
, 0);
3074 * Delete a name from the filesystem using Carbon semantics.
3077 delete(__unused proc_t p
, struct delete_args
*uap
, __unused register_t
*retval
)
3079 struct nameidata nd
;
3080 vfs_context_t ctx
= vfs_context_current();
3082 NDINIT(&nd
, DELETE
, AUDITVNPATH1
, UIO_USERSPACE
, uap
->path
, ctx
);
3083 return unlink1(ctx
, &nd
, 1);
3087 * Reposition read/write file offset.
3090 lseek(proc_t p
, struct lseek_args
*uap
, off_t
*retval
)
3092 struct fileproc
*fp
;
3094 struct vfs_context
*ctx
;
3095 off_t offset
= uap
->offset
, file_size
;
3098 if ( (error
= fp_getfvp(p
,uap
->fd
, &fp
, &vp
)) ) {
3099 if (error
== ENOTSUP
)
3103 if (vnode_isfifo(vp
)) {
3109 ctx
= vfs_context_current();
3111 if (uap
->whence
== L_INCR
&& uap
->offset
== 0)
3112 error
= mac_file_check_get_offset(vfs_context_ucred(ctx
),
3115 error
= mac_file_check_change_offset(vfs_context_ucred(ctx
),
3122 if ( (error
= vnode_getwithref(vp
)) ) {
3127 switch (uap
->whence
) {
3129 offset
+= fp
->f_fglob
->fg_offset
;
3132 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
3134 offset
+= file_size
;
3142 if (uap
->offset
> 0 && offset
< 0) {
3143 /* Incremented/relative move past max size */
3147 * Allow negative offsets on character devices, per
3148 * POSIX 1003.1-2001. Most likely for writing disk
3151 if (offset
< 0 && vp
->v_type
!= VCHR
) {
3152 /* Decremented/relative move before start */
3156 fp
->f_fglob
->fg_offset
= offset
;
3157 *retval
= fp
->f_fglob
->fg_offset
;
3161 (void)vnode_put(vp
);
3168 * Check access permissions.
3170 * Returns: 0 Success
3171 * vnode_authorize:???
3174 access1(vnode_t vp
, vnode_t dvp
, int uflags
, vfs_context_t ctx
)
3176 kauth_action_t action
;
3180 * If just the regular access bits, convert them to something
3181 * that vnode_authorize will understand.
3183 if (!(uflags
& _ACCESS_EXTENDED_MASK
)) {
3186 action
|= KAUTH_VNODE_READ_DATA
; /* aka KAUTH_VNODE_LIST_DIRECTORY */
3187 if (uflags
& W_OK
) {
3188 if (vnode_isdir(vp
)) {
3189 action
|= KAUTH_VNODE_ADD_FILE
|
3190 KAUTH_VNODE_ADD_SUBDIRECTORY
;
3191 /* might want delete rights here too */
3193 action
|= KAUTH_VNODE_WRITE_DATA
;
3196 if (uflags
& X_OK
) {
3197 if (vnode_isdir(vp
)) {
3198 action
|= KAUTH_VNODE_SEARCH
;
3200 action
|= KAUTH_VNODE_EXECUTE
;
3204 /* take advantage of definition of uflags */
3205 action
= uflags
>> 8;
3209 error
= mac_vnode_check_access(ctx
, vp
, uflags
);
3214 /* action == 0 means only check for existence */
3216 error
= vnode_authorize(vp
, dvp
, action
| KAUTH_VNODE_ACCESS
, ctx
);
3229 * Description: uap->entries Pointer to argument descriptor
3230 * uap->size Size of the area pointed to by
3232 * uap->results Pointer to the results array
3234 * Returns: 0 Success
3235 * ENOMEM Insufficient memory
3236 * EINVAL Invalid arguments
3237 * namei:EFAULT Bad address
3238 * namei:ENAMETOOLONG Filename too long
3239 * namei:ENOENT No such file or directory
3240 * namei:ELOOP Too many levels of symbolic links
3241 * namei:EBADF Bad file descriptor
3242 * namei:ENOTDIR Not a directory
3247 * uap->results Array contents modified
3249 * Notes: The uap->entries are structured as an arbitrary length array
3250 * of accessx descriptors, followed by one or more NULL terniated
3253 * struct accessx_descriptor[0]
3255 * struct accessx_descriptor[n]
3256 * char name_data[0];
3258 * We determine the entry count by walking the buffer containing
3259 * the uap->entries argument descriptor. For each descrptor we
3260 * see, the valid values for the offset ad_name_offset will be
3261 * in the byte range:
3263 * [ uap->entries + sizeof(struct accessx_descriptor) ]
3265 * [ uap->entries + uap->size - 2 ]
3267 * since we must have at least one string, and the string must
3268 * be at least one character plus the NUL terminator in length.
3270 * XXX: Need to support the check-as uid argument
3273 access_extended(__unused proc_t p
, struct access_extended_args
*uap
, __unused register_t
*retval
)
3275 struct accessx_descriptor
*input
= NULL
;
3276 errno_t
*result
= NULL
;
3279 unsigned int desc_max
, desc_actual
, i
, j
;
3280 struct vfs_context context
;
3281 struct nameidata nd
;
3285 #define ACCESSX_MAX_DESCR_ON_STACK 10
3286 struct accessx_descriptor stack_input
[ACCESSX_MAX_DESCR_ON_STACK
];
3288 context
.vc_ucred
= NULL
;
3291 * Validate parameters; if valid, copy the descriptor array and string
3292 * arguments into local memory. Before proceeding, the following
3293 * conditions must have been met:
3295 * o The total size is not permitted to exceed ACCESSX_MAX_TABLESIZE
3296 * o There must be sufficient room in the request for at least one
3297 * descriptor and a one yte NUL terminated string.
3298 * o The allocation of local storage must not fail.
3300 if (uap
->size
> ACCESSX_MAX_TABLESIZE
)
3302 if (uap
->size
< (sizeof(struct accessx_descriptor
) + 2))
3304 if (uap
->size
<= sizeof (stack_input
)) {
3305 input
= stack_input
;
3307 MALLOC(input
, struct accessx_descriptor
*, uap
->size
, M_TEMP
, M_WAITOK
);
3308 if (input
== NULL
) {
3313 error
= copyin(uap
->entries
, input
, uap
->size
);
3318 * Force NUL termination of the copyin buffer to avoid nami() running
3319 * off the end. If the caller passes us bogus data, they may get a
3322 ((char *)input
)[uap
->size
- 1] = 0;
3325 * Access is defined as checking against the process' real identity,
3326 * even if operations are checking the effective identity. This
3327 * requires that we use a local vfs context.
3329 context
.vc_ucred
= kauth_cred_copy_real(kauth_cred_get());
3330 context
.vc_thread
= current_thread();
3333 * Find out how many entries we have, so we can allocate the result
3334 * array by walking the list and adjusting the count downward by the
3335 * earliest string offset we see.
3337 desc_max
= (uap
->size
- 2) / sizeof(struct accessx_descriptor
);
3338 desc_actual
= desc_max
;
3339 for (i
= 0; i
< desc_actual
; i
++) {
3341 * Take the offset to the name string for this entry and
3342 * convert to an input array index, which would be one off
3343 * the end of the array if this entry was the lowest-addressed
3346 j
= input
[i
].ad_name_offset
/ sizeof(struct accessx_descriptor
);
3349 * An offset greater than the max allowable offset is an error.
3350 * It is also an error for any valid entry to point
3351 * to a location prior to the end of the current entry, if
3352 * it's not a reference to the string of the previous entry.
3354 if (j
> desc_max
|| (j
!= 0 && j
<= i
)) {
3360 * An offset of 0 means use the previous descriptor's offset;
3361 * this is used to chain multiple requests for the same file
3362 * to avoid multiple lookups.
3365 /* This is not valid for the first entry */
3374 * If the offset of the string for this descriptor is before
3375 * what we believe is the current actual last descriptor,
3376 * then we need to adjust our estimate downward; this permits
3377 * the string table following the last descriptor to be out
3378 * of order relative to the descriptor list.
3380 if (j
< desc_actual
)
3385 * We limit the actual number of descriptors we are willing to process
3386 * to a hard maximum of ACCESSX_MAX_DESCRIPTORS. If the number being
3387 * requested does not exceed this limit,
3389 if (desc_actual
> ACCESSX_MAX_DESCRIPTORS
) {
3393 MALLOC(result
, errno_t
*, desc_actual
* sizeof(errno_t
), M_TEMP
, M_WAITOK
);
3394 if (result
== NULL
) {
3400 * Do the work by iterating over the descriptor entries we know to
3401 * at least appear to contain valid data.
3404 for (i
= 0; i
< desc_actual
; i
++) {
3406 * If the ad_name_offset is 0, then we use the previous
3407 * results to make the check; otherwise, we are looking up
3410 if (input
[i
].ad_name_offset
!= 0) {
3411 /* discard old vnodes */
3422 * Scan forward in the descriptor list to see if we
3423 * need the parent vnode. We will need it if we are
3424 * deleting, since we must have rights to remove
3425 * entries in the parent directory, as well as the
3426 * rights to delete the object itself.
3428 wantdelete
= input
[i
].ad_flags
& _DELETE_OK
;
3429 for (j
= i
+ 1; (j
< desc_actual
) && (input
[j
].ad_name_offset
== 0); j
++)
3430 if (input
[j
].ad_flags
& _DELETE_OK
)
3433 niopts
= FOLLOW
| AUDITVNPATH1
;
3435 /* need parent for vnode_authorize for deletion test */
3437 niopts
|= WANTPARENT
;
3440 NDINIT(&nd
, LOOKUP
, niopts
, UIO_SYSSPACE
, CAST_USER_ADDR_T(((const char *)input
) + input
[i
].ad_name_offset
), &context
);
3451 * Handle lookup errors.
3461 /* run this access check */
3462 result
[i
] = access1(vp
, dvp
, input
[i
].ad_flags
, &context
);
3465 /* fatal lookup error */
3471 /* copy out results */
3472 error
= copyout(result
, uap
->results
, desc_actual
* sizeof(errno_t
));
3475 if (input
&& input
!= stack_input
)
3476 FREE(input
, M_TEMP
);
3478 FREE(result
, M_TEMP
);
3483 if (IS_VALID_CRED(context
.vc_ucred
))
3484 kauth_cred_unref(&context
.vc_ucred
);
3490 * Returns: 0 Success
3491 * namei:EFAULT Bad address
3492 * namei:ENAMETOOLONG Filename too long
3493 * namei:ENOENT No such file or directory
3494 * namei:ELOOP Too many levels of symbolic links
3495 * namei:EBADF Bad file descriptor
3496 * namei:ENOTDIR Not a directory
3501 access(__unused proc_t p
, struct access_args
*uap
, __unused register_t
*retval
)
3504 struct nameidata nd
;
3506 struct vfs_context context
;
3509 int is_namedstream
= 0;
3513 * Access is defined as checking against the process'
3514 * real identity, even if operations are checking the
3515 * effective identity. So we need to tweak the credential
3518 context
.vc_ucred
= kauth_cred_copy_real(kauth_cred_get());
3519 context
.vc_thread
= current_thread();
3521 niopts
= FOLLOW
| AUDITVNPATH1
;
3522 /* need parent for vnode_authorize for deletion test */
3523 if (uap
->flags
& _DELETE_OK
)
3524 niopts
|= WANTPARENT
;
3525 NDINIT(&nd
, LOOKUP
, niopts
, UIO_USERSPACE
, uap
->path
, &context
);
3528 /* access(F_OK) calls are allowed for resource forks. */
3529 if (uap
->flags
== F_OK
)
3530 nd
.ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
3537 /* Grab reference on the shadow stream file vnode to
3538 * force an inactive on release which will mark it for
3541 if (vnode_isnamedstream(nd
.ni_vp
) &&
3542 (nd
.ni_vp
->v_parent
!= NULLVP
) &&
3543 ((nd
.ni_vp
->v_parent
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0)) {
3545 vnode_ref(nd
.ni_vp
);
3549 error
= access1(nd
.ni_vp
, nd
.ni_dvp
, uap
->flags
, &context
);
3552 if (is_namedstream
) {
3553 vnode_rele(nd
.ni_vp
);
3557 vnode_put(nd
.ni_vp
);
3558 if (uap
->flags
& _DELETE_OK
)
3559 vnode_put(nd
.ni_dvp
);
3563 kauth_cred_unref(&context
.vc_ucred
);
3569 * Returns: 0 Success
3576 stat2(vfs_context_t ctx
, struct nameidata
*ndp
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3580 struct user_stat user_sb
;
3581 struct user_stat64 user_sb64
;
3584 kauth_filesec_t fsec
;
3585 size_t xsecurity_bufsize
;
3589 int is_namedstream
= 0;
3590 /* stat calls are allowed for resource forks. */
3591 ndp
->ni_cnd
.cn_flags
|= CN_ALLOWRSRCFORK
;
3596 fsec
= KAUTH_FILESEC_NONE
;
3598 statptr
= (void *)&sb64
;
3600 statptr
= (void *)&sb
;
3603 /* Grab reference on the shadow stream file vnode to
3604 * force an inactive on release which will mark it for
3607 if (vnode_isnamedstream(ndp
->ni_vp
) &&
3608 (ndp
->ni_vp
->v_parent
!= NULLVP
) &&
3609 ((ndp
->ni_vp
->v_parent
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0)) {
3611 vnode_ref (ndp
->ni_vp
);
3615 error
= vn_stat(ndp
->ni_vp
, statptr
, (xsecurity
!= USER_ADDR_NULL
? &fsec
: NULL
), isstat64
, ctx
);
3618 if (is_namedstream
) {
3619 vnode_rele (ndp
->ni_vp
);
3623 vnode_put(ndp
->ni_vp
);
3628 /* Zap spare fields */
3629 if (isstat64
!= 0) {
3631 sb64
.st_qspare
[0] = 0LL;
3632 sb64
.st_qspare
[1] = 0LL;
3633 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
3634 munge_stat64(&sb64
, &user_sb64
);
3635 my_size
= sizeof(user_sb64
);
3636 sbp
= (caddr_t
)&user_sb64
;
3638 my_size
= sizeof(sb64
);
3639 sbp
= (caddr_t
)&sb64
;
3642 * Check if we raced (post lookup) against the last unlink of a file.
3644 if ((sb64
.st_nlink
== 0) && S_ISREG(sb64
.st_mode
)) {
3649 sb
.st_qspare
[0] = 0LL;
3650 sb
.st_qspare
[1] = 0LL;
3651 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
3652 munge_stat(&sb
, &user_sb
);
3653 my_size
= sizeof(user_sb
);
3654 sbp
= (caddr_t
)&user_sb
;
3656 my_size
= sizeof(sb
);
3661 * Check if we raced (post lookup) against the last unlink of a file.
3663 if ((sb
.st_nlink
== 0) && S_ISREG(sb
.st_mode
)) {
3667 if ((error
= copyout(sbp
, ub
, my_size
)) != 0)
3670 /* caller wants extended security information? */
3671 if (xsecurity
!= USER_ADDR_NULL
) {
3673 /* did we get any? */
3674 if (fsec
== KAUTH_FILESEC_NONE
) {
3675 if (susize(xsecurity_size
, 0) != 0) {
3680 /* find the user buffer size */
3681 xsecurity_bufsize
= fusize(xsecurity_size
);
3683 /* copy out the actual data size */
3684 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3689 /* if the caller supplied enough room, copy out to it */
3690 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3691 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3695 if (fsec
!= KAUTH_FILESEC_NONE
)
3696 kauth_filesec_free(fsec
);
3701 * Get file status; this version follows links.
3703 * Returns: 0 Success
3704 * stat2:??? [see stat2() in this file]
3707 stat1(user_addr_t path
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3709 struct nameidata nd
;
3710 vfs_context_t ctx
= vfs_context_current();
3712 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| FOLLOW
| AUDITVNPATH1
,
3713 UIO_USERSPACE
, path
, ctx
);
3714 return(stat2(ctx
, &nd
, ub
, xsecurity
, xsecurity_size
, isstat64
));
3718 stat_extended(__unused proc_t p
, struct stat_extended_args
*uap
, __unused register_t
*retval
)
3720 return (stat1(uap
->path
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3724 * Returns: 0 Success
3725 * stat1:??? [see stat1() in this file]
3728 stat(__unused proc_t p
, struct stat_args
*uap
, __unused register_t
*retval
)
3730 return(stat1(uap
->path
, uap
->ub
, 0, 0, 0));
3734 stat64(__unused proc_t p
, struct stat64_args
*uap
, __unused register_t
*retval
)
3736 return(stat1(uap
->path
, uap
->ub
, 0, 0, 1));
3740 stat64_extended(__unused proc_t p
, struct stat64_extended_args
*uap
, __unused register_t
*retval
)
3742 return (stat1(uap
->path
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3745 * Get file status; this version does not follow links.
3748 lstat1(user_addr_t path
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3750 struct nameidata nd
;
3751 vfs_context_t ctx
= vfs_context_current();
3753 NDINIT(&nd
, LOOKUP
, NOTRIGGER
| NOFOLLOW
| AUDITVNPATH1
,
3754 UIO_USERSPACE
, path
, ctx
);
3756 return(stat2(ctx
, &nd
, ub
, xsecurity
, xsecurity_size
, isstat64
));
3760 lstat_extended(__unused proc_t p
, struct lstat_extended_args
*uap
, __unused register_t
*retval
)
3762 return (lstat1(uap
->path
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3766 lstat(__unused proc_t p
, struct lstat_args
*uap
, __unused register_t
*retval
)
3768 return(lstat1(uap
->path
, uap
->ub
, 0, 0, 0));
3771 lstat64(__unused proc_t p
, struct lstat64_args
*uap
, __unused register_t
*retval
)
3773 return(lstat1(uap
->path
, uap
->ub
, 0, 0, 1));
3777 lstat64_extended(__unused proc_t p
, struct lstat64_extended_args
*uap
, __unused register_t
*retval
)
3779 return (lstat1(uap
->path
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3783 * Get configurable pathname variables.
3785 * Returns: 0 Success
3789 * Notes: Global implementation constants are intended to be
3790 * implemented in this function directly; all other constants
3791 * are per-FS implementation, and therefore must be handled in
3792 * each respective FS, instead.
3794 * XXX We implement some things globally right now that should actually be
3795 * XXX per-FS; we will need to deal with this at some point.
3799 pathconf(__unused proc_t p
, struct pathconf_args
*uap
, register_t
*retval
)
3802 struct nameidata nd
;
3803 vfs_context_t ctx
= vfs_context_current();
3805 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
3806 UIO_USERSPACE
, uap
->path
, ctx
);
3811 error
= vn_pathconf(nd
.ni_vp
, uap
->name
, retval
, ctx
);
3813 vnode_put(nd
.ni_vp
);
3819 * Return target name of a symbolic link.
3823 readlink(proc_t p
, struct readlink_args
*uap
, register_t
*retval
)
3827 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
3829 struct nameidata nd
;
3830 vfs_context_t ctx
= vfs_context_current();
3831 char uio_buf
[ UIO_SIZEOF(1) ];
3833 NDINIT(&nd
, LOOKUP
, NOFOLLOW
| AUDITVNPATH1
,
3834 UIO_USERSPACE
, uap
->path
, ctx
);
3842 auio
= uio_createwithbuffer(1, 0, spacetype
, UIO_READ
,
3843 &uio_buf
[0], sizeof(uio_buf
));
3844 uio_addiov(auio
, uap
->buf
, uap
->count
);
3845 if (vp
->v_type
!= VLNK
)
3849 error
= mac_vnode_check_readlink(ctx
,
3853 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, ctx
);
3855 error
= VNOP_READLINK(vp
, auio
, ctx
);
3858 // LP64todo - fix this
3859 *retval
= uap
->count
- (int)uio_resid(auio
);
3864 * Change file flags.
3867 chflags1(vnode_t vp
, int flags
, vfs_context_t ctx
)
3869 struct vnode_attr va
;
3870 kauth_action_t action
;
3874 VATTR_SET(&va
, va_flags
, flags
);
3877 error
= mac_vnode_check_setflags(ctx
, vp
, flags
);
3882 /* request authorisation, disregard immutability */
3883 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0)
3886 * Request that the auth layer disregard those file flags it's allowed to when
3887 * authorizing this operation; we need to do this in order to be able to
3888 * clear immutable flags.
3890 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
| KAUTH_VNODE_NOIMMUTABLE
, ctx
)) != 0))
3892 error
= vnode_setattr(vp
, &va
, ctx
);
3894 if ((error
== 0) && !VATTR_IS_SUPPORTED(&va
, va_flags
)) {
3903 * Change flags of a file given a path name.
3907 chflags(__unused proc_t p
, struct chflags_args
*uap
, __unused register_t
*retval
)
3910 vfs_context_t ctx
= vfs_context_current();
3912 struct nameidata nd
;
3914 AUDIT_ARG(fflags
, uap
->flags
);
3915 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
3916 UIO_USERSPACE
, uap
->path
, ctx
);
3923 error
= chflags1(vp
, uap
->flags
, ctx
);
3929 * Change flags of a file given a file descriptor.
3933 fchflags(__unused proc_t p
, struct fchflags_args
*uap
, __unused register_t
*retval
)
3938 AUDIT_ARG(fd
, uap
->fd
);
3939 AUDIT_ARG(fflags
, uap
->flags
);
3940 if ( (error
= file_vnode(uap
->fd
, &vp
)) )
3943 if ((error
= vnode_getwithref(vp
))) {
3948 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3950 error
= chflags1(vp
, uap
->flags
, vfs_context_current());
3957 * Change security information on a filesystem object.
3959 * Returns: 0 Success
3960 * EPERM Operation not permitted
3961 * vnode_authattr:??? [anything vnode_authattr can return]
3962 * vnode_authorize:??? [anything vnode_authorize can return]
3963 * vnode_setattr:??? [anything vnode_setattr can return]
3965 * Notes: If vnode_authattr or vnode_authorize return EACCES, it will be
3966 * translated to EPERM before being returned.
3969 chmod2(vfs_context_t ctx
, vnode_t vp
, struct vnode_attr
*vap
)
3971 kauth_action_t action
;
3974 AUDIT_ARG(mode
, (mode_t
)vap
->va_mode
);
3975 #warning XXX audit new args
3978 /* chmod calls are not allowed for resource forks. */
3979 if (vp
->v_flag
& VISNAMEDSTREAM
) {
3985 error
= mac_vnode_check_setmode(ctx
, vp
, (mode_t
)vap
->va_mode
);
3990 /* make sure that the caller is allowed to set this security information */
3991 if (((error
= vnode_authattr(vp
, vap
, &action
, ctx
)) != 0) ||
3992 ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
3993 if (error
== EACCES
)
3998 error
= vnode_setattr(vp
, vap
, ctx
);
4005 * Change mode of a file given path name.
4007 * Returns: 0 Success
4008 * namei:??? [anything namei can return]
4009 * chmod2:??? [anything chmod2 can return]
4012 chmod1(vfs_context_t ctx
, user_addr_t path
, struct vnode_attr
*vap
)
4014 struct nameidata nd
;
4017 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
4018 UIO_USERSPACE
, path
, ctx
);
4019 if ((error
= namei(&nd
)))
4021 error
= chmod2(ctx
, nd
.ni_vp
, vap
);
4022 vnode_put(nd
.ni_vp
);
4028 * A chmod system call using an extended argument list compared to the regular
4029 * system call 'mkfifo'.
4031 * Parameters: p Process requesting the open
4032 * uap User argument descriptor (see below)
4035 * Indirect: uap->path Path to object (same as 'chmod')
4036 * uap->uid UID to set
4037 * uap->gid GID to set
4038 * uap->mode File mode to set (same as 'chmod')
4039 * uap->xsecurity ACL to set (or delete)
4041 * Returns: 0 Success
4044 * Notes: The kauth_filesec_t in 'va', if any, is in host byte order.
4046 * XXX: We should enummerate the possible errno values here, and where
4047 * in the code they originated.
4050 chmod_extended(__unused proc_t p
, struct chmod_extended_args
*uap
, __unused register_t
*retval
)
4053 struct vnode_attr va
;
4054 kauth_filesec_t xsecdst
;
4057 if (uap
->mode
!= -1)
4058 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
4059 if (uap
->uid
!= KAUTH_UID_NONE
)
4060 VATTR_SET(&va
, va_uid
, uap
->uid
);
4061 if (uap
->gid
!= KAUTH_GID_NONE
)
4062 VATTR_SET(&va
, va_gid
, uap
->gid
);
4065 switch(uap
->xsecurity
) {
4066 /* explicit remove request */
4067 case CAST_USER_ADDR_T((void *)1): /* _FILESEC_REMOVE_ACL */
4068 VATTR_SET(&va
, va_acl
, NULL
);
4071 case USER_ADDR_NULL
:
4074 if ((error
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)
4076 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
4077 KAUTH_DEBUG("CHMOD - setting ACL with %d entries", va
.va_acl
->acl_entrycount
);
4080 error
= chmod1(vfs_context_current(), uap
->path
, &va
);
4082 if (xsecdst
!= NULL
)
4083 kauth_filesec_free(xsecdst
);
4088 * Returns: 0 Success
4089 * chmod1:??? [anything chmod1 can return]
4092 chmod(__unused proc_t p
, struct chmod_args
*uap
, __unused register_t
*retval
)
4094 struct vnode_attr va
;
4097 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
4099 return(chmod1(vfs_context_current(), uap
->path
, &va
));
4103 * Change mode of a file given a file descriptor.
4106 fchmod1(__unused proc_t p
, int fd
, struct vnode_attr
*vap
)
4113 if ((error
= file_vnode(fd
, &vp
)) != 0)
4115 if ((error
= vnode_getwithref(vp
)) != 0) {
4119 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4121 error
= chmod2(vfs_context_current(), vp
, vap
);
4122 (void)vnode_put(vp
);
4129 fchmod_extended(proc_t p
, struct fchmod_extended_args
*uap
, __unused register_t
*retval
)
4132 struct vnode_attr va
;
4133 kauth_filesec_t xsecdst
;
4136 if (uap
->mode
!= -1)
4137 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
4138 if (uap
->uid
!= KAUTH_UID_NONE
)
4139 VATTR_SET(&va
, va_uid
, uap
->uid
);
4140 if (uap
->gid
!= KAUTH_GID_NONE
)
4141 VATTR_SET(&va
, va_gid
, uap
->gid
);
4144 switch(uap
->xsecurity
) {
4145 case USER_ADDR_NULL
:
4146 VATTR_SET(&va
, va_acl
, NULL
);
4148 case CAST_USER_ADDR_T(-1):
4151 if ((error
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)
4153 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
4156 error
= fchmod1(p
, uap
->fd
, &va
);
4159 switch(uap
->xsecurity
) {
4160 case USER_ADDR_NULL
:
4161 case CAST_USER_ADDR_T(-1):
4164 if (xsecdst
!= NULL
)
4165 kauth_filesec_free(xsecdst
);
4171 fchmod(proc_t p
, struct fchmod_args
*uap
, __unused register_t
*retval
)
4173 struct vnode_attr va
;
4176 VATTR_SET(&va
, va_mode
, uap
->mode
& ALLPERMS
);
4178 return(fchmod1(p
, uap
->fd
, &va
));
4183 * Set ownership given a path name.
4187 chown1(vfs_context_t ctx
, struct chown_args
*uap
, __unused register_t
*retval
, int follow
)
4190 struct vnode_attr va
;
4192 struct nameidata nd
;
4193 kauth_action_t action
;
4195 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
4197 NDINIT(&nd
, LOOKUP
, (follow
? FOLLOW
: 0) | NOTRIGGER
| AUDITVNPATH1
,
4198 UIO_USERSPACE
, uap
->path
, ctx
);
4207 if (uap
->uid
!= VNOVAL
)
4208 VATTR_SET(&va
, va_uid
, uap
->uid
);
4209 if (uap
->gid
!= VNOVAL
)
4210 VATTR_SET(&va
, va_gid
, uap
->gid
);
4213 error
= mac_vnode_check_setowner(ctx
, vp
, uap
->uid
, uap
->gid
);
4218 /* preflight and authorize attribute changes */
4219 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0)
4221 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0))
4223 error
= vnode_setattr(vp
, &va
, ctx
);
4227 * EACCES is only allowed from namei(); permissions failure should
4228 * return EPERM, so we need to translate the error code.
4230 if (error
== EACCES
)
4238 chown(__unused proc_t p
, struct chown_args
*uap
, register_t
*retval
)
4240 return chown1(vfs_context_current(), uap
, retval
, 1);
4244 lchown(__unused proc_t p
, struct lchown_args
*uap
, register_t
*retval
)
4246 /* Argument list identical, but machine generated; cast for chown1() */
4247 return chown1(vfs_context_current(), (struct chown_args
*)uap
, retval
, 0);
4251 * Set ownership given a file descriptor.
4255 fchown(__unused proc_t p
, struct fchown_args
*uap
, __unused register_t
*retval
)
4257 struct vnode_attr va
;
4258 vfs_context_t ctx
= vfs_context_current();
4261 kauth_action_t action
;
4263 AUDIT_ARG(owner
, uap
->uid
, uap
->gid
);
4264 AUDIT_ARG(fd
, uap
->fd
);
4266 if ( (error
= file_vnode(uap
->fd
, &vp
)) )
4269 if ( (error
= vnode_getwithref(vp
)) ) {
4273 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4276 if (uap
->uid
!= VNOVAL
)
4277 VATTR_SET(&va
, va_uid
, uap
->uid
);
4278 if (uap
->gid
!= VNOVAL
)
4279 VATTR_SET(&va
, va_gid
, uap
->gid
);
4282 /* chown calls are not allowed for resource forks. */
4283 if (vp
->v_flag
& VISNAMEDSTREAM
) {
4290 error
= mac_vnode_check_setowner(ctx
, vp
, uap
->uid
, uap
->gid
);
4295 /* preflight and authorize attribute changes */
4296 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0)
4298 if (action
&& ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
4299 if (error
== EACCES
)
4303 error
= vnode_setattr(vp
, &va
, ctx
);
4306 (void)vnode_put(vp
);
4312 getutimes(user_addr_t usrtvp
, struct timespec
*tsp
)
4314 struct user_timeval tv
[2];
4317 if (usrtvp
== USER_ADDR_NULL
) {
4318 struct timeval old_tv
;
4319 /* XXX Y2038 bug because of microtime argument */
4321 TIMEVAL_TO_TIMESPEC(&old_tv
, &tsp
[0]);
4324 if (IS_64BIT_PROCESS(current_proc())) {
4325 error
= copyin(usrtvp
, (void *)tv
, sizeof(tv
));
4327 struct timeval old_tv
[2];
4328 error
= copyin(usrtvp
, (void *)old_tv
, sizeof(old_tv
));
4329 tv
[0].tv_sec
= old_tv
[0].tv_sec
;
4330 tv
[0].tv_usec
= old_tv
[0].tv_usec
;
4331 tv
[1].tv_sec
= old_tv
[1].tv_sec
;
4332 tv
[1].tv_usec
= old_tv
[1].tv_usec
;
4336 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
4337 TIMEVAL_TO_TIMESPEC(&tv
[1], &tsp
[1]);
4343 setutimes(vfs_context_t ctx
, vnode_t vp
, const struct timespec
*ts
,
4347 struct vnode_attr va
;
4348 kauth_action_t action
;
4350 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4353 VATTR_SET(&va
, va_access_time
, ts
[0]);
4354 VATTR_SET(&va
, va_modify_time
, ts
[1]);
4356 va
.va_vaflags
|= VA_UTIMES_NULL
;
4359 /* utimes calls are not allowed for resource forks. */
4360 if (vp
->v_flag
& VISNAMEDSTREAM
) {
4367 error
= mac_vnode_check_setutimes(ctx
, vp
, ts
[0], ts
[1]);
4371 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0) {
4372 if (!nullflag
&& error
== EACCES
)
4377 /* since we may not need to auth anything, check here */
4378 if ((action
!= 0) && ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
4379 if (!nullflag
&& error
== EACCES
)
4383 error
= vnode_setattr(vp
, &va
, ctx
);
4390 * Set the access and modification times of a file.
4394 utimes(__unused proc_t p
, struct utimes_args
*uap
, __unused register_t
*retval
)
4396 struct timespec ts
[2];
4399 struct nameidata nd
;
4400 vfs_context_t ctx
= vfs_context_current();
4403 * AUDIT: Needed to change the order of operations to do the
4404 * name lookup first because auditing wants the path.
4406 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
4407 UIO_USERSPACE
, uap
->path
, ctx
);
4414 * Fetch the user-supplied time. If usrtvp is USER_ADDR_NULL, we fetch
4415 * the current time instead.
4418 if ((error
= getutimes(usrtvp
, ts
)) != 0)
4421 error
= setutimes(ctx
, nd
.ni_vp
, ts
, usrtvp
== USER_ADDR_NULL
);
4424 vnode_put(nd
.ni_vp
);
4429 * Set the access and modification times of a file.
4433 futimes(__unused proc_t p
, struct futimes_args
*uap
, __unused register_t
*retval
)
4435 struct timespec ts
[2];
4440 AUDIT_ARG(fd
, uap
->fd
);
4442 if ((error
= getutimes(usrtvp
, ts
)) != 0)
4444 if ((error
= file_vnode(uap
->fd
, &vp
)) != 0)
4446 if((error
= vnode_getwithref(vp
))) {
4451 error
= setutimes(vfs_context_current(), vp
, ts
, usrtvp
== 0);
4458 * Truncate a file given its path name.
4462 truncate(__unused proc_t p
, struct truncate_args
*uap
, __unused register_t
*retval
)
4465 struct vnode_attr va
;
4466 vfs_context_t ctx
= vfs_context_current();
4468 struct nameidata nd
;
4469 kauth_action_t action
;
4471 if (uap
->length
< 0)
4473 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
4474 UIO_USERSPACE
, uap
->path
, ctx
);
4475 if ((error
= namei(&nd
)))
4482 VATTR_SET(&va
, va_data_size
, uap
->length
);
4485 error
= mac_vnode_check_truncate(ctx
, NOCRED
, vp
);
4490 if ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0)
4492 if ((action
!= 0) && ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0))
4494 error
= vnode_setattr(vp
, &va
, ctx
);
4501 * Truncate a file given a file descriptor.
4505 ftruncate(proc_t p
, struct ftruncate_args
*uap
, register_t
*retval
)
4507 vfs_context_t ctx
= vfs_context_current();
4508 struct vnode_attr va
;
4510 struct fileproc
*fp
;
4514 AUDIT_ARG(fd
, uap
->fd
);
4515 if (uap
->length
< 0)
4518 if ( (error
= fp_lookup(p
,fd
,&fp
,0)) ) {
4522 if (fp
->f_fglob
->fg_type
== DTYPE_PSXSHM
) {
4523 error
= pshm_truncate(p
, fp
, uap
->fd
, uap
->length
, retval
);
4526 if (fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
4531 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
4533 if ((fp
->f_fglob
->fg_flag
& FWRITE
) == 0) {
4534 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
4539 if ((error
= vnode_getwithref(vp
)) != 0) {
4543 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4546 error
= mac_vnode_check_truncate(ctx
,
4547 fp
->f_fglob
->fg_cred
, vp
);
4549 (void)vnode_put(vp
);
4554 VATTR_SET(&va
, va_data_size
, uap
->length
);
4555 error
= vnode_setattr(vp
, &va
, ctx
);
4556 (void)vnode_put(vp
);
4564 * Sync an open file.
4568 fsync(proc_t p
, struct fsync_args
*uap
, register_t
*retval
)
4570 __pthread_testcancel(1);
4571 return(fsync_nocancel(p
, (struct fsync_nocancel_args
*)uap
, retval
));
4575 fsync_nocancel(proc_t p
, struct fsync_nocancel_args
*uap
, __unused register_t
*retval
)
4578 struct fileproc
*fp
;
4579 vfs_context_t ctx
= vfs_context_current();
4582 if ( (error
= fp_getfvp(p
, uap
->fd
, &fp
, &vp
)) )
4584 if ( (error
= vnode_getwithref(vp
)) ) {
4589 error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
4592 /* Sync resource fork shadow file if necessary. */
4594 (vp
->v_flag
& VISNAMEDSTREAM
) &&
4595 (vp
->v_parent
!= NULLVP
) &&
4596 !(vp
->v_parent
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) &&
4597 (fp
->f_flags
& FP_WRITTEN
)) {
4598 (void) vnode_flushnamedstream(vp
->v_parent
, vp
, ctx
);
4602 (void)vnode_put(vp
);
4608 * Duplicate files. Source must be a file, target must be a file or
4611 * XXX Copyfile authorisation checking is woefully inadequate, and will not
4612 * perform inheritance correctly.
4616 copyfile(__unused proc_t p
, struct copyfile_args
*uap
, __unused register_t
*retval
)
4618 vnode_t tvp
, fvp
, tdvp
, sdvp
;
4619 struct nameidata fromnd
, tond
;
4621 vfs_context_t ctx
= vfs_context_current();
4623 /* Check that the flags are valid. */
4625 if (uap
->flags
& ~CPF_MASK
) {
4629 NDINIT(&fromnd
, LOOKUP
, SAVESTART
| AUDITVNPATH1
,
4630 UIO_USERSPACE
, uap
->from
, ctx
);
4631 if ((error
= namei(&fromnd
)))
4635 NDINIT(&tond
, CREATE
, LOCKPARENT
| LOCKLEAF
| NOCACHE
| SAVESTART
| AUDITVNPATH2
| CN_NBMOUNTLOOK
,
4636 UIO_USERSPACE
, uap
->to
, ctx
);
4637 if ((error
= namei(&tond
))) {
4644 if (!(uap
->flags
& CPF_OVERWRITE
)) {
4649 if (fvp
->v_type
== VDIR
|| (tvp
&& tvp
->v_type
== VDIR
)) {
4654 if ((error
= vnode_authorize(tdvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
4660 * If source is the same as the destination (that is the
4661 * same inode number) then there is nothing to do.
4662 * (fixed to have POSIX semantics - CSM 3/2/98)
4667 error
= VNOP_COPYFILE(fvp
, tdvp
, tvp
, &tond
.ni_cnd
, uap
->mode
, uap
->flags
, ctx
);
4669 sdvp
= tond
.ni_startdir
;
4671 * nameidone has to happen before we vnode_put(tdvp)
4672 * since it may need to release the fs_nodelock on the tdvp
4683 if (fromnd
.ni_startdir
)
4684 vnode_put(fromnd
.ni_startdir
);
4694 * Rename files. Source and destination must either both be directories,
4695 * or both not be directories. If target is a directory, it must be empty.
4699 rename(__unused proc_t p
, struct rename_args
*uap
, __unused register_t
*retval
)
4703 struct nameidata fromnd
, tond
;
4704 vfs_context_t ctx
= vfs_context_current();
4710 char *from_name
= NULL
, *to_name
= NULL
;
4711 int from_len
, to_len
;
4712 int holding_mntlock
;
4713 mount_t locked_mp
= NULL
;
4715 fse_info from_finfo
, to_finfo
;
4717 holding_mntlock
= 0;
4724 NDINIT(&fromnd
, DELETE
, WANTPARENT
| AUDITVNPATH1
, UIO_USERSPACE
, uap
->from
, ctx
);
4726 if ( (error
= namei(&fromnd
)) )
4728 fdvp
= fromnd
.ni_dvp
;
4732 error
= mac_vnode_check_rename_from(ctx
, fdvp
, fvp
, &fromnd
.ni_cnd
);
4737 NDINIT(&tond
, RENAME
, WANTPARENT
| AUDITVNPATH2
| CN_NBMOUNTLOOK
, UIO_USERSPACE
, uap
->to
, ctx
);
4738 if (fvp
->v_type
== VDIR
)
4739 tond
.ni_cnd
.cn_flags
|= WILLBEDIR
;
4741 if ( (error
= namei(&tond
)) ) {
4743 * Translate error code for rename("dir1", "dir2/.").
4745 if (error
== EISDIR
&& fvp
->v_type
== VDIR
)
4753 error
= mac_vnode_check_rename_to(ctx
,
4754 tdvp
, tvp
, fdvp
== tdvp
, &tond
.ni_cnd
);
4760 if (fvp
->v_type
== VDIR
&& tvp
->v_type
!= VDIR
) {
4763 } else if (fvp
->v_type
!= VDIR
&& tvp
->v_type
== VDIR
) {
4773 * If the source and destination are the same (i.e. they're
4774 * links to the same vnode) and the target file system is
4775 * case sensitive, then there is nothing to do.
4781 * Note: if _PC_CASE_SENSITIVE selector isn't supported,
4782 * then assume that this file system is case sensitive.
4784 if (VNOP_PATHCONF(fvp
, _PC_CASE_SENSITIVE
, &pathconf_val
, ctx
) != 0 ||
4785 pathconf_val
!= 0) {
4793 * If tvp is a directory and not the same as fdvp, or tdvp is not
4794 * the same as fdvp, the node is moving between directories and we
4795 * need rights to remove from the old and add to the new.
4797 * If tvp already exists and is not a directory, we need to be
4798 * allowed to delete it.
4800 * Note that we do not inherit when renaming.
4802 * XXX This needs to be revisited to implement the deferred-inherit bit
4808 if ((tvp
!= NULL
) && vnode_isdir(tvp
)) {
4811 } else if (tdvp
!= fdvp
) {
4815 * must have delete rights to remove the old name even in
4816 * the simple case of fdvp == tdvp.
4818 * If fvp is a directory, and we are changing it's parent,
4819 * then we also need rights to rewrite its ".." entry as well.
4821 if (vnode_isdir(fvp
)) {
4822 if ((error
= vnode_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
| KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
)) != 0)
4825 if ((error
= vnode_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
, ctx
)) != 0)
4829 /* moving into tdvp or tvp, must have rights to add */
4830 if ((error
= vnode_authorize(((tvp
!= NULL
) && vnode_isdir(tvp
)) ? tvp
: tdvp
,
4832 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
4835 * We could encounter a race where after doing the namei, tvp stops
4836 * being valid. If so, simply re-drive the rename call from the
4839 if (error
== ENOENT
) {
4845 /* node staying in same directory, must be allowed to add new name */
4846 if ((error
= vnode_authorize(fdvp
, NULL
,
4847 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
, ctx
)) != 0)
4850 /* overwriting tvp */
4851 if ((tvp
!= NULL
) && !vnode_isdir(tvp
) &&
4852 ((error
= vnode_authorize(tvp
, tdvp
, KAUTH_VNODE_DELETE
, ctx
)) != 0)) {
4854 * We could encounter a race where after doing the namei, tvp stops
4855 * being valid. If so, simply re-drive the rename call from the
4858 if (error
== ENOENT
) {
4864 /* XXX more checks? */
4867 /* authorization denied */
4872 * Allow the renaming of mount points.
4873 * - target must not exist
4874 * - target must reside in the same directory as source
4875 * - union mounts cannot be renamed
4876 * - "/" cannot be renamed
4878 if ((fvp
->v_flag
& VROOT
) &&
4879 (fvp
->v_type
== VDIR
) &&
4881 (fvp
->v_mountedhere
== NULL
) &&
4883 ((fvp
->v_mount
->mnt_flag
& (MNT_UNION
| MNT_ROOTFS
)) == 0) &&
4884 (fvp
->v_mount
->mnt_vnodecovered
!= NULLVP
)) {
4887 /* switch fvp to the covered vnode */
4888 coveredvp
= fvp
->v_mount
->mnt_vnodecovered
;
4889 if ( (vnode_getwithref(coveredvp
)) ) {
4899 * Check for cross-device rename.
4901 if ((fvp
->v_mount
!= tdvp
->v_mount
) ||
4902 (tvp
&& (fvp
->v_mount
!= tvp
->v_mount
))) {
4907 * Avoid renaming "." and "..".
4909 if (fvp
->v_type
== VDIR
&&
4911 (fromnd
.ni_cnd
.cn_namelen
== 1 && fromnd
.ni_cnd
.cn_nameptr
[0] == '.') ||
4912 ((fromnd
.ni_cnd
.cn_flags
| tond
.ni_cnd
.cn_flags
) & ISDOTDOT
)) ) {
4917 * The following edge case is caught here:
4918 * (to cannot be a descendent of from)
4931 if (tdvp
->v_parent
== fvp
) {
4937 * If source is the same as the destination (that is the
4938 * same inode number) then there is nothing to do...
4939 * EXCEPT if the underlying file system supports case
4940 * insensitivity and is case preserving. In this case
4941 * the file system needs to handle the special case of
4942 * getting the same vnode as target (fvp) and source (tvp).
4944 * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE
4945 * and _PC_CASE_PRESERVING can have this exception, and they need to
4946 * handle the special case of getting the same vnode as target and
4947 * source. NOTE: Then the target is unlocked going into vnop_rename,
4948 * so not to cause locking problems. There is a single reference on tvp.
4950 * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE
4951 * that correct behaviour then is just to remove the source (link)
4953 if (fvp
== tvp
&& fdvp
== tdvp
) {
4954 if (fromnd
.ni_cnd
.cn_namelen
== tond
.ni_cnd
.cn_namelen
&&
4955 !bcmp(fromnd
.ni_cnd
.cn_nameptr
, tond
.ni_cnd
.cn_nameptr
,
4956 fromnd
.ni_cnd
.cn_namelen
)) {
4961 if (holding_mntlock
&& fvp
->v_mount
!= locked_mp
) {
4963 * we're holding a reference and lock
4964 * on locked_mp, but it no longer matches
4965 * what we want to do... so drop our hold
4967 mount_unlock_renames(locked_mp
);
4968 mount_drop(locked_mp
, 0);
4969 holding_mntlock
= 0;
4971 if (tdvp
!= fdvp
&& fvp
->v_type
== VDIR
) {
4973 * serialize renames that re-shape
4974 * the tree... if holding_mntlock is
4975 * set, then we're ready to go...
4977 * first need to drop the iocounts
4978 * we picked up, second take the
4979 * lock to serialize the access,
4980 * then finally start the lookup
4981 * process over with the lock held
4983 if (!holding_mntlock
) {
4985 * need to grab a reference on
4986 * the mount point before we
4987 * drop all the iocounts... once
4988 * the iocounts are gone, the mount
4991 locked_mp
= fvp
->v_mount
;
4992 mount_ref(locked_mp
, 0);
4995 * nameidone has to happen before we vnode_put(tvp)
4996 * since it may need to release the fs_nodelock on the tvp
5005 * nameidone has to happen before we vnode_put(fdvp)
5006 * since it may need to release the fs_nodelock on the fvp
5013 mount_lock_renames(locked_mp
);
5014 holding_mntlock
= 1;
5020 * when we dropped the iocounts to take
5021 * the lock, we allowed the identity of
5022 * the various vnodes to change... if they did,
5023 * we may no longer be dealing with a rename
5024 * that reshapes the tree... once we're holding
5025 * the iocounts, the vnodes can't change type
5026 * so we're free to drop the lock at this point
5029 if (holding_mntlock
) {
5030 mount_unlock_renames(locked_mp
);
5031 mount_drop(locked_mp
, 0);
5032 holding_mntlock
= 0;
5035 // save these off so we can later verify that fvp is the same
5036 oname
= fvp
->v_name
;
5037 oparent
= fvp
->v_parent
;
5040 need_event
= need_fsevent(FSE_RENAME
, fvp
);
5042 get_fse_info(fvp
, &from_finfo
, ctx
);
5045 get_fse_info(tvp
, &to_finfo
, ctx
);
5050 #endif /* CONFIG_FSE */
5052 if (need_event
|| kauth_authorize_fileop_has_listeners()) {
5053 GET_PATH(from_name
);
5054 if (from_name
== NULL
) {
5058 from_len
= MAXPATHLEN
;
5059 vn_getpath(fdvp
, from_name
, &from_len
);
5060 if ((from_len
+ 1 + fromnd
.ni_cnd
.cn_namelen
+ 1) < MAXPATHLEN
) {
5062 from_name
[from_len
-1] = '/';
5066 strlcpy(&from_name
[from_len
], fromnd
.ni_cnd
.cn_nameptr
, MAXPATHLEN
-from_len
);
5067 from_len
+= fromnd
.ni_cnd
.cn_namelen
+ 1;
5068 from_name
[from_len
] = '\0';
5072 if (to_name
== NULL
) {
5077 to_len
= MAXPATHLEN
;
5078 vn_getpath(tdvp
, to_name
, &to_len
);
5079 // if the path is not just "/", then append a "/"
5080 if ((to_len
+ 1 + tond
.ni_cnd
.cn_namelen
+ 1) < MAXPATHLEN
) {
5082 to_name
[to_len
-1] = '/';
5086 strlcpy(&to_name
[to_len
], tond
.ni_cnd
.cn_nameptr
, MAXPATHLEN
-to_len
);
5087 to_len
+= tond
.ni_cnd
.cn_namelen
+ 1;
5088 to_name
[to_len
] = '\0';
5092 error
= VNOP_RENAME(fdvp
, fvp
, &fromnd
.ni_cnd
,
5093 tdvp
, tvp
, &tond
.ni_cnd
,
5096 if (holding_mntlock
) {
5098 * we can drop our serialization
5101 mount_unlock_renames(locked_mp
);
5102 mount_drop(locked_mp
, 0);
5103 holding_mntlock
= 0;
5107 * We may encounter a race in the VNOP where the destination didn't
5108 * exist when we did the namei, but it does by the time we go and
5109 * try to create the entry. In this case, we should re-drive this rename
5110 * call from the top again.
5112 if (error
== EEXIST
) {
5119 /* call out to allow 3rd party notification of rename.
5120 * Ignore result of kauth_authorize_fileop call.
5122 kauth_authorize_fileop(vfs_context_ucred(ctx
),
5123 KAUTH_FILEOP_RENAME
,
5124 (uintptr_t)from_name
, (uintptr_t)to_name
);
5127 if (from_name
!= NULL
&& to_name
!= NULL
) {
5129 add_fsevent(FSE_RENAME
, ctx
,
5130 FSE_ARG_STRING
, from_len
, from_name
,
5131 FSE_ARG_FINFO
, &from_finfo
,
5132 FSE_ARG_STRING
, to_len
, to_name
,
5133 FSE_ARG_FINFO
, &to_finfo
,
5136 add_fsevent(FSE_RENAME
, ctx
,
5137 FSE_ARG_STRING
, from_len
, from_name
,
5138 FSE_ARG_FINFO
, &from_finfo
,
5139 FSE_ARG_STRING
, to_len
, to_name
,
5143 #endif /* CONFIG_FSE */
5146 * update filesystem's mount point data
5149 char *cp
, *pathend
, *mpname
;
5155 mp
= fvp
->v_mountedhere
;
5157 if (vfs_busy(mp
, LK_NOWAIT
)) {
5161 MALLOC_ZONE(tobuf
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
5163 error
= copyinstr(uap
->to
, tobuf
, MAXPATHLEN
, &len
);
5165 /* find current mount point prefix */
5166 pathend
= &mp
->mnt_vfsstat
.f_mntonname
[0];
5167 for (cp
= pathend
; *cp
!= '\0'; ++cp
) {
5171 /* find last component of target name */
5172 for (mpname
= cp
= tobuf
; *cp
!= '\0'; ++cp
) {
5176 /* append name to prefix */
5177 maxlen
= MAXPATHLEN
- (pathend
- mp
->mnt_vfsstat
.f_mntonname
);
5178 bzero(pathend
, maxlen
);
5179 strlcpy(pathend
, mpname
, maxlen
);
5181 FREE_ZONE(tobuf
, MAXPATHLEN
, M_NAMEI
);
5186 * fix up name & parent pointers. note that we first
5187 * check that fvp has the same name/parent pointers it
5188 * had before the rename call... this is a 'weak' check
5191 if (oname
== fvp
->v_name
&& oparent
== fvp
->v_parent
) {
5194 update_flags
= VNODE_UPDATE_NAME
;
5197 update_flags
|= VNODE_UPDATE_PARENT
;
5199 vnode_update_identity(fvp
, tdvp
, tond
.ni_cnd
.cn_nameptr
, tond
.ni_cnd
.cn_namelen
, tond
.ni_cnd
.cn_hash
, update_flags
);
5202 if (to_name
!= NULL
) {
5203 RELEASE_PATH(to_name
);
5206 if (from_name
!= NULL
) {
5207 RELEASE_PATH(from_name
);
5210 if (holding_mntlock
) {
5211 mount_unlock_renames(locked_mp
);
5212 mount_drop(locked_mp
, 0);
5213 holding_mntlock
= 0;
5217 * nameidone has to happen before we vnode_put(tdvp)
5218 * since it may need to release the fs_nodelock on the tdvp
5228 * nameidone has to happen before we vnode_put(fdvp)
5229 * since it may need to release the fs_nodelock on the fdvp
5239 * If things changed after we did the namei, then we will re-drive
5240 * this rename call from the top.
5251 * Make a directory file.
5253 * Returns: 0 Success
5256 * vnode_authorize:???
5261 mkdir1(vfs_context_t ctx
, user_addr_t path
, struct vnode_attr
*vap
)
5265 int update_flags
= 0;
5266 struct nameidata nd
;
5268 AUDIT_ARG(mode
, vap
->va_mode
);
5269 NDINIT(&nd
, CREATE
, LOCKPARENT
| AUDITVNPATH1
,
5270 UIO_USERSPACE
, path
, ctx
);
5271 nd
.ni_cnd
.cn_flags
|= WILLBEDIR
;
5283 VATTR_SET(vap
, va_type
, VDIR
);
5286 error
= mac_vnode_check_create(ctx
,
5287 nd
.ni_dvp
, &nd
.ni_cnd
, vap
);
5292 /* authorize addition of a directory to the parent */
5293 if ((error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
)) != 0)
5297 /* make the directory */
5298 if ((error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, vap
, 0, ctx
)) != 0)
5301 // Make sure the name & parent pointers are hooked up
5302 if (vp
->v_name
== NULL
)
5303 update_flags
|= VNODE_UPDATE_NAME
;
5304 if (vp
->v_parent
== NULLVP
)
5305 update_flags
|= VNODE_UPDATE_PARENT
;
5308 vnode_update_identity(vp
, dvp
, nd
.ni_cnd
.cn_nameptr
, nd
.ni_cnd
.cn_namelen
, nd
.ni_cnd
.cn_hash
, update_flags
);
5311 add_fsevent(FSE_CREATE_DIR
, ctx
, FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
5316 * nameidone has to happen before we vnode_put(dvp)
5317 * since it may need to release the fs_nodelock on the dvp
5330 mkdir_extended(proc_t p
, struct mkdir_extended_args
*uap
, __unused register_t
*retval
)
5333 kauth_filesec_t xsecdst
;
5334 struct vnode_attr va
;
5337 if ((uap
->xsecurity
!= USER_ADDR_NULL
) &&
5338 ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0))
5342 VATTR_SET(&va
, va_mode
, (uap
->mode
& ACCESSPERMS
) & ~p
->p_fd
->fd_cmask
);
5343 if (xsecdst
!= NULL
)
5344 VATTR_SET(&va
, va_acl
, &xsecdst
->fsec_acl
);
5346 ciferror
= mkdir1(vfs_context_current(), uap
->path
, &va
);
5347 if (xsecdst
!= NULL
)
5348 kauth_filesec_free(xsecdst
);
5353 mkdir(proc_t p
, struct mkdir_args
*uap
, __unused register_t
*retval
)
5355 struct vnode_attr va
;
5358 VATTR_SET(&va
, va_mode
, (uap
->mode
& ACCESSPERMS
) & ~p
->p_fd
->fd_cmask
);
5360 return(mkdir1(vfs_context_current(), uap
->path
, &va
));
5364 * Remove a directory file.
5368 rmdir(__unused proc_t p
, struct rmdir_args
*uap
, __unused register_t
*retval
)
5372 struct nameidata nd
;
5373 vfs_context_t ctx
= vfs_context_current();
5375 int restart_flag
, oldvp_id
= -1;
5378 * This loop exists to restart rmdir in the unlikely case that two
5379 * processes are simultaneously trying to remove the same directory
5380 * containing orphaned appleDouble files.
5385 NDINIT(&nd
, DELETE
, LOCKPARENT
| AUDITVNPATH1
,
5386 UIO_USERSPACE
, uap
->path
, ctx
);
5396 * If being restarted check if the new vp
5397 * still has the same v_id.
5399 if (oldvp_id
!= -1 && oldvp_id
!= vp
->v_id
) {
5404 if (vp
->v_type
!= VDIR
) {
5406 * rmdir only deals with directories
5409 } else if (dvp
== vp
) {
5411 * No rmdir "." please.
5414 } else if (vp
->v_flag
& VROOT
) {
5416 * The root of a mounted filesystem cannot be deleted.
5421 error
= mac_vnode_check_unlink(ctx
, dvp
,
5425 error
= vnode_authorize(vp
, nd
.ni_dvp
, KAUTH_VNODE_DELETE
, ctx
);
5431 int has_listeners
= 0;
5435 need_event
= need_fsevent(FSE_DELETE
, dvp
);
5437 get_fse_info(vp
, &finfo
, ctx
);
5440 has_listeners
= kauth_authorize_fileop_has_listeners();
5441 if (need_event
|| has_listeners
) {
5448 vn_getpath(vp
, path
, &len
);
5451 error
= VNOP_RMDIR(dvp
, vp
, &nd
.ni_cnd
, ctx
);
5454 * Special case to remove orphaned AppleDouble
5455 * files. I don't like putting this in the kernel,
5456 * but carbon does not like putting this in carbon either,
5459 if (error
== ENOTEMPTY
) {
5460 error
= rmdir_remove_orphaned_appleDouble(vp
, ctx
, &restart_flag
);
5461 if (error
== EBUSY
) {
5462 oldvp_id
= vp
->v_id
;
5468 * Assuming everything went well, we will try the RMDIR again
5471 error
= VNOP_RMDIR(dvp
, vp
, &nd
.ni_cnd
, ctx
);
5475 * Call out to allow 3rd party notification of delete.
5476 * Ignore result of kauth_authorize_fileop call.
5479 if (has_listeners
) {
5480 kauth_authorize_fileop(vfs_context_ucred(ctx
),
5481 KAUTH_FILEOP_DELETE
,
5486 if (vp
->v_flag
& VISHARDLINK
) {
5487 // see the comment in unlink1() about why we update
5488 // the parent of a hard link when it is removed
5489 vnode_update_identity(vp
, NULL
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
5494 add_fsevent(FSE_DELETE
, ctx
,
5495 FSE_ARG_STRING
, len
, path
,
5496 FSE_ARG_FINFO
, &finfo
,
5507 * nameidone has to happen before we vnode_put(dvp)
5508 * since it may need to release the fs_nodelock on the dvp
5515 if (restart_flag
== 0) {
5516 wakeup_one((caddr_t
)vp
);
5519 tsleep(vp
, PVFS
, "rm AD", 1);
5521 } while (restart_flag
!= 0);
5527 /* Get direntry length padded to 8 byte alignment */
5528 #define DIRENT64_LEN(namlen) \
5529 ((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 7) & ~7)
5532 vnode_readdir64(struct vnode
*vp
, struct uio
*uio
, int flags
, int *eofflag
,
5533 int *numdirent
, vfs_context_t ctxp
)
5535 /* Check if fs natively supports VNODE_READDIR_EXTENDED */
5536 if (vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSREADDIR_EXTENDED
) {
5537 return VNOP_READDIR(vp
, uio
, flags
, eofflag
, numdirent
, ctxp
);
5542 struct direntry entry64
;
5548 * Our kernel buffer needs to be smaller since re-packing
5549 * will expand each dirent. The worse case (when the name
5550 * length is 3) corresponds to a struct direntry size of 32
5551 * bytes (8-byte aligned) and a struct dirent size of 12 bytes
5552 * (4-byte aligned). So having a buffer that is 3/8 the size
5553 * will prevent us from reading more than we can pack.
5555 * Since this buffer is wired memory, we will limit the
5556 * buffer size to a maximum of 32K. We would really like to
5557 * use 32K in the MIN(), but we use magic number 87371 to
5558 * prevent uio_resid() * 3 / 8 from overflowing.
5560 bufsize
= 3 * MIN(uio_resid(uio
), 87371) / 8;
5561 MALLOC(bufptr
, void *, bufsize
, M_TEMP
, M_WAITOK
);
5563 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_READ
);
5564 uio_addiov(auio
, (uintptr_t)bufptr
, bufsize
);
5565 auio
->uio_offset
= uio
->uio_offset
;
5567 error
= VNOP_READDIR(vp
, auio
, 0, eofflag
, numdirent
, ctxp
);
5569 dep
= (struct dirent
*)bufptr
;
5570 bytesread
= bufsize
- uio_resid(auio
);
5573 * Convert all the entries and copy them out to user's buffer.
5575 while (error
== 0 && (char *)dep
< ((char *)bufptr
+ bytesread
)) {
5576 /* Convert a dirent to a dirent64. */
5577 entry64
.d_ino
= dep
->d_ino
;
5578 entry64
.d_seekoff
= 0;
5579 entry64
.d_reclen
= DIRENT64_LEN(dep
->d_namlen
);
5580 entry64
.d_namlen
= dep
->d_namlen
;
5581 entry64
.d_type
= dep
->d_type
;
5582 bcopy(dep
->d_name
, entry64
.d_name
, dep
->d_namlen
+ 1);
5584 /* Move to next entry. */
5585 dep
= (struct dirent
*)((char *)dep
+ dep
->d_reclen
);
5587 /* Copy entry64 to user's buffer. */
5588 error
= uiomove((caddr_t
)&entry64
, entry64
.d_reclen
, uio
);
5591 /* Update the real offset using the offset we got from VNOP_READDIR. */
5593 uio
->uio_offset
= auio
->uio_offset
;
5596 FREE(bufptr
, M_TEMP
);
5602 * Read a block of directory entries in a file system independent format.
5605 getdirentries_common(int fd
, user_addr_t bufp
, user_size_t bufsize
, ssize_t
*bytesread
,
5606 off_t
*offset
, int flags
)
5609 struct vfs_context context
= *vfs_context_current(); /* local copy */
5610 struct fileproc
*fp
;
5612 int spacetype
= proc_is64bit(vfs_context_proc(&context
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
5614 int error
, eofflag
, numdirent
;
5615 char uio_buf
[ UIO_SIZEOF(1) ];
5617 error
= fp_getfvp(vfs_context_proc(&context
), fd
, &fp
, &vp
);
5621 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
5622 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
5628 error
= mac_file_check_change_offset(vfs_context_ucred(&context
), fp
->f_fglob
);
5632 if ( (error
= vnode_getwithref(vp
)) ) {
5635 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5638 if (vp
->v_type
!= VDIR
) {
5639 (void)vnode_put(vp
);
5645 error
= mac_vnode_check_readdir(&context
, vp
);
5647 (void)vnode_put(vp
);
5652 loff
= fp
->f_fglob
->fg_offset
;
5653 auio
= uio_createwithbuffer(1, loff
, spacetype
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
5654 uio_addiov(auio
, bufp
, bufsize
);
5656 if (flags
& VNODE_READDIR_EXTENDED
) {
5657 error
= vnode_readdir64(vp
, auio
, flags
, &eofflag
, &numdirent
, &context
);
5658 fp
->f_fglob
->fg_offset
= uio_offset(auio
);
5660 error
= VNOP_READDIR(vp
, auio
, 0, &eofflag
, &numdirent
, &context
);
5661 fp
->f_fglob
->fg_offset
= uio_offset(auio
);
5664 (void)vnode_put(vp
);
5668 if ((user_ssize_t
)bufsize
== uio_resid(auio
)){
5669 if (union_dircheckp
) {
5670 error
= union_dircheckp(&vp
, fp
, &context
);
5677 if ((vp
->v_flag
& VROOT
) && (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
5678 struct vnode
*tvp
= vp
;
5679 vp
= vp
->v_mount
->mnt_vnodecovered
;
5680 vnode_getwithref(vp
);
5682 fp
->f_fglob
->fg_data
= (caddr_t
) vp
;
5683 fp
->f_fglob
->fg_offset
= 0;
5694 // LP64todo - fix this
5695 *bytesread
= bufsize
- uio_resid(auio
);
5703 getdirentries(__unused
struct proc
*p
, struct getdirentries_args
*uap
, register_t
*retval
)
5710 AUDIT_ARG(fd
, uap
->fd
);
5711 error
= getdirentries_common(uap
->fd
, uap
->buf
, uap
->count
, &bytesread
, &offset
, 0);
5714 loff
= (long)offset
;
5715 error
= copyout((caddr_t
)&loff
, uap
->basep
, sizeof(long));
5716 *retval
= bytesread
;
5722 getdirentries64(__unused
struct proc
*p
, struct getdirentries64_args
*uap
, user_ssize_t
*retval
)
5728 AUDIT_ARG(fd
, uap
->fd
);
5729 error
= getdirentries_common(uap
->fd
, uap
->buf
, uap
->bufsize
, &bytesread
, &offset
, VNODE_READDIR_EXTENDED
);
5732 *retval
= bytesread
;
5733 error
= copyout((caddr_t
)&offset
, uap
->position
, sizeof(off_t
));
5740 * Set the mode mask for creation of filesystem nodes.
5742 #warning XXX implement xsecurity
5744 #define UMASK_NOXSECURITY (void *)1 /* leave existing xsecurity alone */
5746 umask1(proc_t p
, int newmask
, __unused kauth_filesec_t fsec
, register_t
*retval
)
5748 struct filedesc
*fdp
;
5750 AUDIT_ARG(mask
, newmask
);
5753 *retval
= fdp
->fd_cmask
;
5754 fdp
->fd_cmask
= newmask
& ALLPERMS
;
5761 umask_extended(proc_t p
, struct umask_extended_args
*uap
, register_t
*retval
)
5764 kauth_filesec_t xsecdst
;
5766 xsecdst
= KAUTH_FILESEC_NONE
;
5767 if (uap
->xsecurity
!= USER_ADDR_NULL
) {
5768 if ((ciferror
= kauth_copyinfilesec(uap
->xsecurity
, &xsecdst
)) != 0)
5771 xsecdst
= KAUTH_FILESEC_NONE
;
5774 ciferror
= umask1(p
, uap
->newmask
, xsecdst
, retval
);
5776 if (xsecdst
!= KAUTH_FILESEC_NONE
)
5777 kauth_filesec_free(xsecdst
);
5782 umask(proc_t p
, struct umask_args
*uap
, register_t
*retval
)
5784 return(umask1(p
, uap
->newmask
, UMASK_NOXSECURITY
, retval
));
5788 * Void all references to file by ripping underlying filesystem
5793 revoke(proc_t p
, struct revoke_args
*uap
, __unused register_t
*retval
)
5796 struct vnode_attr va
;
5797 vfs_context_t ctx
= vfs_context_current();
5799 struct nameidata nd
;
5801 NDINIT(&nd
, LOOKUP
, FOLLOW
| AUDITVNPATH1
,
5802 UIO_USERSPACE
, uap
->path
, ctx
);
5811 error
= mac_vnode_check_revoke(ctx
, vp
);
5817 VATTR_WANTED(&va
, va_uid
);
5818 if ((error
= vnode_getattr(vp
, &va
, ctx
)))
5820 if (kauth_cred_getuid(vfs_context_ucred(ctx
)) != va
.va_uid
&&
5821 (error
= suser(vfs_context_ucred(ctx
), &p
->p_acflag
)))
5823 if (vp
->v_usecount
> 1 || (vp
->v_flag
& VALIASED
))
5824 VNOP_REVOKE(vp
, REVOKEALL
, ctx
);
5832 * HFS/HFS PlUS SPECIFIC SYSTEM CALLS
5833 * The following system calls are designed to support features
5834 * which are specific to the HFS & HFS Plus volume formats
5837 #ifdef __APPLE_API_OBSOLETE
5839 /************************************************/
5840 /* *** Following calls will be deleted soon *** */
5841 /************************************************/
5844 * Make a complex file. A complex file is one with multiple forks (data streams)
5848 mkcomplex(__unused proc_t p
, __unused
struct mkcomplex_args
*uap
, __unused register_t
*retval
)
5854 * Extended stat call which returns volumeid and vnodeid as well as other info
5858 statv(__unused proc_t p
,
5859 __unused
struct statv_args
*uap
,
5860 __unused register_t
*retval
)
5862 return (ENOTSUP
); /* We'll just return an error for now */
5864 } /* end of statv system call */
5867 * Extended lstat call which returns volumeid and vnodeid as well as other info
5871 lstatv(__unused proc_t p
,
5872 __unused
struct lstatv_args
*uap
,
5873 __unused register_t
*retval
)
5875 return (ENOTSUP
); /* We'll just return an error for now */
5876 } /* end of lstatv system call */
5879 * Extended fstat call which returns volumeid and vnodeid as well as other info
5883 fstatv(__unused proc_t p
,
5884 __unused
struct fstatv_args
*uap
,
5885 __unused register_t
*retval
)
5887 return (ENOTSUP
); /* We'll just return an error for now */
5888 } /* end of fstatv system call */
5891 /************************************************/
5892 /* *** Preceding calls will be deleted soon *** */
5893 /************************************************/
5895 #endif /* __APPLE_API_OBSOLETE */
5898 * Obtain attribute information on objects in a directory while enumerating
5899 * the directory. This call does not yet support union mounted directories.
5901 * 1.union mounted directories.
5906 getdirentriesattr (proc_t p
, struct getdirentriesattr_args
*uap
, register_t
*retval
)
5909 struct fileproc
*fp
;
5911 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
5916 struct attrlist attributelist
;
5917 vfs_context_t ctx
= vfs_context_current();
5919 char uio_buf
[ UIO_SIZEOF(1) ];
5920 kauth_action_t action
;
5924 /* Get the attributes into kernel space */
5925 if ((error
= copyin(uap
->alist
, (caddr_t
)&attributelist
, sizeof(attributelist
)))) {
5928 if ((error
= copyin(uap
->count
, (caddr_t
)&count
, sizeof(count
)))) {
5931 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5934 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
5935 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
5942 error
= mac_file_check_change_offset(vfs_context_ucred(ctx
),
5949 if ( (error
= vnode_getwithref(vp
)) )
5952 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5954 if (vp
->v_type
!= VDIR
) {
5955 (void)vnode_put(vp
);
5961 error
= mac_vnode_check_readdir(ctx
, vp
);
5963 (void)vnode_put(vp
);
5968 /* set up the uio structure which will contain the users return buffer */
5969 loff
= fp
->f_fglob
->fg_offset
;
5970 auio
= uio_createwithbuffer(1, loff
, spacetype
, UIO_READ
,
5971 &uio_buf
[0], sizeof(uio_buf
));
5972 uio_addiov(auio
, uap
->buffer
, uap
->buffersize
);
5975 * If the only item requested is file names, we can let that past with
5976 * just LIST_DIRECTORY. If they want any other attributes, that means
5977 * they need SEARCH as well.
5979 action
= KAUTH_VNODE_LIST_DIRECTORY
;
5980 if ((attributelist
.commonattr
& ~ATTR_CMN_NAME
) ||
5981 attributelist
.fileattr
|| attributelist
.dirattr
)
5982 action
|= KAUTH_VNODE_SEARCH
;
5984 if ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) == 0) {
5985 u_long ulcount
= count
;
5987 error
= VNOP_READDIRATTR(vp
, &attributelist
, auio
,
5989 uap
->options
, (unsigned long *)&newstate
, &eofflag
,
5994 (void)vnode_put(vp
);
5998 fp
->f_fglob
->fg_offset
= uio_offset(auio
); /* should be multiple of dirent, not variable */
6000 if ((error
= copyout((caddr_t
) &count
, uap
->count
, sizeof(count
))))
6002 if ((error
= copyout((caddr_t
) &newstate
, uap
->newstate
, sizeof(newstate
))))
6004 if ((error
= copyout((caddr_t
) &loff
, uap
->basep
, sizeof(loff
))))
6007 *retval
= eofflag
; /* similar to getdirentries */
6011 return (error
); /* return error earlier, an retval of 0 or 1 now */
6013 } /* end of getdirentryattr system call */
6016 * Exchange data between two files
6021 exchangedata (__unused proc_t p
, struct exchangedata_args
*uap
, __unused register_t
*retval
)
6024 struct nameidata fnd
, snd
;
6025 vfs_context_t ctx
= vfs_context_current();
6033 fse_info f_finfo
, s_finfo
;
6036 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) nameiflags
|= FOLLOW
;
6038 NDINIT(&fnd
, LOOKUP
, nameiflags
| AUDITVNPATH1
,
6039 UIO_USERSPACE
, uap
->path1
, ctx
);
6041 error
= namei(&fnd
);
6048 NDINIT(&snd
, LOOKUP
| CN_NBMOUNTLOOK
, nameiflags
| AUDITVNPATH2
,
6049 UIO_USERSPACE
, uap
->path2
, ctx
);
6051 error
= namei(&snd
);
6060 * if the files are the same, return an inval error
6068 * if the files are on different volumes, return an error
6070 if (svp
->v_mount
!= fvp
->v_mount
) {
6076 error
= mac_vnode_check_exchangedata(ctx
,
6081 if (((error
= vnode_authorize(fvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, ctx
)) != 0) ||
6082 ((error
= vnode_authorize(svp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, ctx
)) != 0))
6087 need_fsevent(FSE_EXCHANGE
, fvp
) ||
6089 kauth_authorize_fileop_has_listeners()) {
6092 if (fpath
== NULL
|| spath
== NULL
) {
6098 if (vn_getpath(fvp
, fpath
, &flen
) != 0 || fpath
[0] == '\0') {
6099 printf("exchange: vn_getpath(fvp=%p) failed <<%s>>\n",
6102 if (vn_getpath(svp
, spath
, &slen
) != 0 || spath
[0] == '\0') {
6103 printf("exchange: vn_getpath(svp=%p) failed <<%s>>\n",
6107 get_fse_info(fvp
, &f_finfo
, ctx
);
6108 get_fse_info(svp
, &s_finfo
, ctx
);
6111 /* Ok, make the call */
6112 error
= VNOP_EXCHANGE(fvp
, svp
, 0, ctx
);
6115 const char *tmpname
;
6117 if (fpath
!= NULL
&& spath
!= NULL
) {
6118 /* call out to allow 3rd party notification of exchangedata.
6119 * Ignore result of kauth_authorize_fileop call.
6121 kauth_authorize_fileop(vfs_context_ucred(ctx
), KAUTH_FILEOP_EXCHANGE
,
6122 (uintptr_t)fpath
, (uintptr_t)spath
);
6126 tmpname
= fvp
->v_name
;
6127 fvp
->v_name
= svp
->v_name
;
6128 svp
->v_name
= tmpname
;
6130 if (fvp
->v_parent
!= svp
->v_parent
) {
6133 tmp
= fvp
->v_parent
;
6134 fvp
->v_parent
= svp
->v_parent
;
6135 svp
->v_parent
= tmp
;
6137 name_cache_unlock();
6140 if (fpath
!= NULL
&& spath
!= NULL
) {
6141 add_fsevent(FSE_EXCHANGE
, ctx
,
6142 FSE_ARG_STRING
, flen
, fpath
,
6143 FSE_ARG_FINFO
, &f_finfo
,
6144 FSE_ARG_STRING
, slen
, spath
,
6145 FSE_ARG_FINFO
, &s_finfo
,
6153 RELEASE_PATH(fpath
);
6155 RELEASE_PATH(spath
);
6166 searchfs(proc_t p
, struct searchfs_args
*uap
, __unused register_t
*retval
)
6171 struct nameidata nd
;
6172 struct user_fssearchblock searchblock
;
6173 struct searchstate
*state
;
6174 struct attrlist
*returnattrs
;
6175 void *searchparams1
,*searchparams2
;
6177 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6181 vfs_context_t ctx
= vfs_context_current();
6182 char uio_buf
[ UIO_SIZEOF(1) ];
6184 /* Start by copying in fsearchblock paramater list */
6185 if (IS_64BIT_PROCESS(p
)) {
6186 error
= copyin(uap
->searchblock
, (caddr_t
) &searchblock
, sizeof(searchblock
));
6189 struct fssearchblock tmp_searchblock
;
6190 error
= copyin(uap
->searchblock
, (caddr_t
) &tmp_searchblock
, sizeof(tmp_searchblock
));
6191 // munge into 64-bit version
6192 searchblock
.returnattrs
= CAST_USER_ADDR_T(tmp_searchblock
.returnattrs
);
6193 searchblock
.returnbuffer
= CAST_USER_ADDR_T(tmp_searchblock
.returnbuffer
);
6194 searchblock
.returnbuffersize
= tmp_searchblock
.returnbuffersize
;
6195 searchblock
.maxmatches
= tmp_searchblock
.maxmatches
;
6196 searchblock
.timelimit
.tv_sec
= tmp_searchblock
.timelimit
.tv_sec
;
6197 searchblock
.timelimit
.tv_usec
= tmp_searchblock
.timelimit
.tv_usec
;
6198 searchblock
.searchparams1
= CAST_USER_ADDR_T(tmp_searchblock
.searchparams1
);
6199 searchblock
.sizeofsearchparams1
= tmp_searchblock
.sizeofsearchparams1
;
6200 searchblock
.searchparams2
= CAST_USER_ADDR_T(tmp_searchblock
.searchparams2
);
6201 searchblock
.sizeofsearchparams2
= tmp_searchblock
.sizeofsearchparams2
;
6202 searchblock
.searchattrs
= tmp_searchblock
.searchattrs
;
6207 /* Do a sanity check on sizeofsearchparams1 and sizeofsearchparams2.
6209 if (searchblock
.sizeofsearchparams1
> SEARCHFS_MAX_SEARCHPARMS
||
6210 searchblock
.sizeofsearchparams2
> SEARCHFS_MAX_SEARCHPARMS
)
6213 /* Now malloc a big bunch of space to hold the search parameters, the attrlists and the search state. */
6214 /* It all has to do into local memory and it's not that big so we might as well put it all together. */
6215 /* Searchparams1 shall be first so we might as well use that to hold the base address of the allocated*/
6218 mallocsize
= searchblock
.sizeofsearchparams1
+ searchblock
.sizeofsearchparams2
+
6219 sizeof(struct attrlist
) + sizeof(struct searchstate
);
6221 MALLOC(searchparams1
, void *, mallocsize
, M_TEMP
, M_WAITOK
);
6223 /* Now set up the various pointers to the correct place in our newly allocated memory */
6225 searchparams2
= (void *) (((caddr_t
) searchparams1
) + searchblock
.sizeofsearchparams1
);
6226 returnattrs
= (struct attrlist
*) (((caddr_t
) searchparams2
) + searchblock
.sizeofsearchparams2
);
6227 state
= (struct searchstate
*) (((caddr_t
) returnattrs
) + sizeof (struct attrlist
));
6229 /* Now copy in the stuff given our local variables. */
6231 if ((error
= copyin(searchblock
.searchparams1
, searchparams1
, searchblock
.sizeofsearchparams1
)))
6234 if ((error
= copyin(searchblock
.searchparams2
, searchparams2
, searchblock
.sizeofsearchparams2
)))
6237 if ((error
= copyin(searchblock
.returnattrs
, (caddr_t
) returnattrs
, sizeof(struct attrlist
))))
6240 if ((error
= copyin(uap
->state
, (caddr_t
) state
, sizeof(struct searchstate
))))
6243 /* set up the uio structure which will contain the users return buffer */
6245 auio
= uio_createwithbuffer(1, 0, spacetype
, UIO_READ
,
6246 &uio_buf
[0], sizeof(uio_buf
));
6247 uio_addiov(auio
, searchblock
.returnbuffer
, searchblock
.returnbuffersize
);
6250 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) nameiflags
|= FOLLOW
;
6251 NDINIT(&nd
, LOOKUP
, nameiflags
| AUDITVNPATH1
,
6252 UIO_USERSPACE
, uap
->path
, ctx
);
6263 * If searchblock.maxmatches == 0, then skip the search. This has happened
6264 * before and sometimes the underlyning code doesnt deal with it well.
6266 if (searchblock
.maxmatches
== 0) {
6272 Allright, we have everything we need, so lets make that call.
6274 We keep special track of the return value from the file system:
6275 EAGAIN is an acceptable error condition that shouldn't keep us
6276 from copying out any results...
6279 fserror
= VNOP_SEARCHFS(vp
,
6282 &searchblock
.searchattrs
,
6283 searchblock
.maxmatches
,
6284 &searchblock
.timelimit
,
6297 /* Now copy out the stuff that needs copying out. That means the number of matches, the
6298 search state. Everything was already put into he return buffer by the vop call. */
6300 if ((error
= copyout((caddr_t
) state
, uap
->state
, sizeof(struct searchstate
))) != 0)
6303 if ((error
= suulong(uap
->nummatches
, (uint64_t)nummatches
)) != 0)
6310 FREE(searchparams1
,M_TEMP
);
6315 } /* end of searchfs system call */
6319 * Make a filesystem-specific control call:
6323 fsctl (proc_t p
, struct fsctl_args
*uap
, __unused register_t
*retval
)
6327 struct nameidata nd
;
6329 u_long cmd
= uap
->cmd
;
6331 #define STK_PARAMS 128
6332 char stkbuf
[STK_PARAMS
];
6334 vfs_context_t ctx
= vfs_context_current();
6336 size
= IOCPARM_LEN(cmd
);
6337 if (size
> IOCPARM_MAX
) return (EINVAL
);
6339 is64bit
= proc_is64bit(p
);
6342 if (size
> sizeof (stkbuf
)) {
6343 if ((memp
= (caddr_t
)kalloc(size
)) == 0) return ENOMEM
;
6351 error
= copyin(uap
->data
, data
, size
);
6352 if (error
) goto FSCtl_Exit
;
6355 *(user_addr_t
*)data
= uap
->data
;
6358 *(uint32_t *)data
= (uint32_t)uap
->data
;
6361 } else if ((cmd
& IOC_OUT
) && size
) {
6363 * Zero the buffer so the user always
6364 * gets back something deterministic.
6367 } else if (cmd
& IOC_VOID
) {
6369 *(user_addr_t
*)data
= uap
->data
;
6372 *(uint32_t *)data
= (uint32_t)uap
->data
;
6376 /* Get the vnode for the file we are getting info on: */
6378 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) nameiflags
|= FOLLOW
;
6379 NDINIT(&nd
, LOOKUP
, nameiflags
, UIO_USERSPACE
, uap
->path
, ctx
);
6380 if ((error
= namei(&nd
))) goto FSCtl_Exit
;
6383 error
= mac_mount_check_fsctl(ctx
, vnode_mount(nd
.ni_vp
), cmd
);
6385 vnode_put(nd
.ni_vp
);
6391 /* Invoke the filesystem-specific code */
6392 error
= VNOP_IOCTL(nd
.ni_vp
, IOCBASECMD(cmd
), data
, uap
->options
, ctx
);
6394 vnode_put(nd
.ni_vp
);
6398 * Copy any data to user, size was
6399 * already set and checked above.
6401 if (error
== 0 && (cmd
& IOC_OUT
) && size
)
6402 error
= copyout(data
, uap
->data
, size
);
6405 if (memp
) kfree(memp
, size
);
6409 /* end of fsctl system call */
6412 * An in-kernel sync for power management to call.
6414 __private_extern__
int
6419 struct sync_args data
;
6424 error
= sync(current_proc(), &data
, &retval
[0]);
6428 } /* end of sync_internal call */
6432 * Retrieve the data of an extended attribute.
6435 getxattr(proc_t p
, struct getxattr_args
*uap
, user_ssize_t
*retval
)
6438 struct nameidata nd
;
6439 char attrname
[XATTR_MAXNAMELEN
+1];
6440 vfs_context_t ctx
= vfs_context_current();
6442 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6443 size_t attrsize
= 0;
6447 char uio_buf
[ UIO_SIZEOF(1) ];
6449 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6452 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
6453 NDINIT(&nd
, LOOKUP
, nameiflags
, spacetype
, uap
->path
, ctx
);
6454 if ((error
= namei(&nd
))) {
6460 if ((error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
) != 0)) {
6463 if (xattr_protected(attrname
)) {
6467 if (uap
->value
&& uap
->size
> 0) {
6468 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_READ
,
6469 &uio_buf
[0], sizeof(uio_buf
));
6470 uio_addiov(auio
, uap
->value
, uap
->size
);
6473 error
= vn_getxattr(vp
, attrname
, auio
, &attrsize
, uap
->options
, ctx
);
6478 *retval
= uap
->size
- uio_resid(auio
);
6480 *retval
= (user_ssize_t
)attrsize
;
6487 * Retrieve the data of an extended attribute.
6490 fgetxattr(proc_t p
, struct fgetxattr_args
*uap
, user_ssize_t
*retval
)
6493 char attrname
[XATTR_MAXNAMELEN
+1];
6495 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6496 size_t attrsize
= 0;
6499 char uio_buf
[ UIO_SIZEOF(1) ];
6501 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6504 if ( (error
= file_vnode(uap
->fd
, &vp
)) ) {
6507 if ( (error
= vnode_getwithref(vp
)) ) {
6511 if ((error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
) != 0)) {
6514 if (xattr_protected(attrname
)) {
6518 if (uap
->value
&& uap
->size
> 0) {
6519 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_READ
,
6520 &uio_buf
[0], sizeof(uio_buf
));
6521 uio_addiov(auio
, uap
->value
, uap
->size
);
6524 error
= vn_getxattr(vp
, attrname
, auio
, &attrsize
, uap
->options
, vfs_context_current());
6526 (void)vnode_put(vp
);
6530 *retval
= uap
->size
- uio_resid(auio
);
6532 *retval
= (user_ssize_t
)attrsize
;
6538 * Set the data of an extended attribute.
6541 setxattr(proc_t p
, struct setxattr_args
*uap
, int *retval
)
6544 struct nameidata nd
;
6545 char attrname
[XATTR_MAXNAMELEN
+1];
6546 vfs_context_t ctx
= vfs_context_current();
6548 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6552 char uio_buf
[ UIO_SIZEOF(1) ];
6554 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6557 if ((error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
) != 0)) {
6560 if (xattr_protected(attrname
))
6562 if (uap
->size
!= 0 && uap
->value
== 0) {
6566 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
6567 NDINIT(&nd
, LOOKUP
, nameiflags
, spacetype
, uap
->path
, ctx
);
6568 if ((error
= namei(&nd
))) {
6574 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_WRITE
,
6575 &uio_buf
[0], sizeof(uio_buf
));
6576 uio_addiov(auio
, uap
->value
, uap
->size
);
6578 error
= vn_setxattr(vp
, attrname
, auio
, uap
->options
, ctx
);
6581 add_fsevent(FSE_XATTR_MODIFIED
, ctx
,
6592 * Set the data of an extended attribute.
6595 fsetxattr(proc_t p
, struct fsetxattr_args
*uap
, int *retval
)
6598 char attrname
[XATTR_MAXNAMELEN
+1];
6600 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6603 char uio_buf
[ UIO_SIZEOF(1) ];
6604 vfs_context_t ctx
= vfs_context_current();
6606 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6609 if ((error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
) != 0)) {
6612 if (xattr_protected(attrname
))
6614 if (uap
->size
!= 0 && uap
->value
== 0) {
6617 if ( (error
= file_vnode(uap
->fd
, &vp
)) ) {
6620 if ( (error
= vnode_getwithref(vp
)) ) {
6624 auio
= uio_createwithbuffer(1, uap
->position
, spacetype
, UIO_WRITE
,
6625 &uio_buf
[0], sizeof(uio_buf
));
6626 uio_addiov(auio
, uap
->value
, uap
->size
);
6628 error
= vn_setxattr(vp
, attrname
, auio
, uap
->options
, vfs_context_current());
6631 add_fsevent(FSE_XATTR_MODIFIED
, ctx
,
6643 * Remove an extended attribute.
6645 #warning "code duplication"
6647 removexattr(proc_t p
, struct removexattr_args
*uap
, int *retval
)
6650 struct nameidata nd
;
6651 char attrname
[XATTR_MAXNAMELEN
+1];
6652 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6653 vfs_context_t ctx
= vfs_context_current();
6658 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6661 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
6665 if (xattr_protected(attrname
))
6667 nameiflags
= (uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
;
6668 NDINIT(&nd
, LOOKUP
, nameiflags
, spacetype
, uap
->path
, ctx
);
6669 if ((error
= namei(&nd
))) {
6675 error
= vn_removexattr(vp
, attrname
, uap
->options
, ctx
);
6678 add_fsevent(FSE_XATTR_REMOVED
, ctx
,
6689 * Remove an extended attribute.
6691 #warning "code duplication"
6693 fremovexattr(__unused proc_t p
, struct fremovexattr_args
*uap
, int *retval
)
6696 char attrname
[XATTR_MAXNAMELEN
+1];
6699 vfs_context_t ctx
= vfs_context_current();
6701 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6704 error
= copyinstr(uap
->attrname
, attrname
, sizeof(attrname
), &namelen
);
6708 if (xattr_protected(attrname
))
6710 if ( (error
= file_vnode(uap
->fd
, &vp
)) ) {
6713 if ( (error
= vnode_getwithref(vp
)) ) {
6718 error
= vn_removexattr(vp
, attrname
, uap
->options
, vfs_context_current());
6721 add_fsevent(FSE_XATTR_REMOVED
, ctx
,
6733 * Retrieve the list of extended attribute names.
6735 #warning "code duplication"
6737 listxattr(proc_t p
, struct listxattr_args
*uap
, user_ssize_t
*retval
)
6740 struct nameidata nd
;
6741 vfs_context_t ctx
= vfs_context_current();
6743 int spacetype
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6744 size_t attrsize
= 0;
6747 char uio_buf
[ UIO_SIZEOF(1) ];
6749 if (uap
->options
& (XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6752 nameiflags
= ((uap
->options
& XATTR_NOFOLLOW
) ? 0 : FOLLOW
) | NOTRIGGER
;
6753 NDINIT(&nd
, LOOKUP
, nameiflags
, spacetype
, uap
->path
, ctx
);
6754 if ((error
= namei(&nd
))) {
6759 if (uap
->namebuf
!= 0 && uap
->bufsize
> 0) {
6760 // LP64todo - fix this!
6761 auio
= uio_createwithbuffer(1, 0, spacetype
,
6762 UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
6763 uio_addiov(auio
, uap
->namebuf
, uap
->bufsize
);
6766 error
= vn_listxattr(vp
, auio
, &attrsize
, uap
->options
, ctx
);
6770 *retval
= (user_ssize_t
)uap
->bufsize
- uio_resid(auio
);
6772 *retval
= (user_ssize_t
)attrsize
;
6778 * Retrieve the list of extended attribute names.
6780 #warning "code duplication"
6782 flistxattr(proc_t p
, struct flistxattr_args
*uap
, user_ssize_t
*retval
)
6786 int spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
6787 size_t attrsize
= 0;
6789 char uio_buf
[ UIO_SIZEOF(1) ];
6791 if (uap
->options
& (XATTR_NOFOLLOW
| XATTR_NOSECURITY
| XATTR_NODEFAULT
))
6794 if ( (error
= file_vnode(uap
->fd
, &vp
)) ) {
6797 if ( (error
= vnode_getwithref(vp
)) ) {
6801 if (uap
->namebuf
!= 0 && uap
->bufsize
> 0) {
6802 // LP64todo - fix this!
6803 auio
= uio_createwithbuffer(1, 0, spacetype
,
6804 UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
6805 uio_addiov(auio
, uap
->namebuf
, uap
->bufsize
);
6808 error
= vn_listxattr(vp
, auio
, &attrsize
, uap
->options
, vfs_context_current());
6813 *retval
= (user_ssize_t
)uap
->bufsize
- uio_resid(auio
);
6815 *retval
= (user_ssize_t
)attrsize
;
6821 * Common routine to handle various flavors of statfs data heading out
6824 * Returns: 0 Success
6828 munge_statfs(struct mount
*mp
, struct vfsstatfs
*sfsp
,
6829 user_addr_t bufp
, int *sizep
, boolean_t is_64_bit
,
6830 boolean_t partial_copy
)
6833 int my_size
, copy_size
;
6836 struct user_statfs sfs
;
6837 my_size
= copy_size
= sizeof(sfs
);
6838 bzero(&sfs
, my_size
);
6839 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
6840 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
6841 sfs
.f_reserved1
= (short)sfsp
->f_fssubtype
;
6842 sfs
.f_bsize
= (user_long_t
)sfsp
->f_bsize
;
6843 sfs
.f_iosize
= (user_long_t
)sfsp
->f_iosize
;
6844 sfs
.f_blocks
= (user_long_t
)sfsp
->f_blocks
;
6845 sfs
.f_bfree
= (user_long_t
)sfsp
->f_bfree
;
6846 sfs
.f_bavail
= (user_long_t
)sfsp
->f_bavail
;
6847 sfs
.f_files
= (user_long_t
)sfsp
->f_files
;
6848 sfs
.f_ffree
= (user_long_t
)sfsp
->f_ffree
;
6849 sfs
.f_fsid
= sfsp
->f_fsid
;
6850 sfs
.f_owner
= sfsp
->f_owner
;
6851 strlcpy(&sfs
.f_fstypename
[0], &sfsp
->f_fstypename
[0], MFSNAMELEN
);
6852 strlcpy(&sfs
.f_mntonname
[0], &sfsp
->f_mntonname
[0], MNAMELEN
);
6853 strlcpy(&sfs
.f_mntfromname
[0], &sfsp
->f_mntfromname
[0], MNAMELEN
);
6856 copy_size
-= (sizeof(sfs
.f_reserved3
) + sizeof(sfs
.f_reserved4
));
6858 error
= copyout((caddr_t
)&sfs
, bufp
, copy_size
);
6862 my_size
= copy_size
= sizeof(sfs
);
6863 bzero(&sfs
, my_size
);
6865 sfs
.f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
6866 sfs
.f_type
= mp
->mnt_vtable
->vfc_typenum
;
6867 sfs
.f_reserved1
= (short)sfsp
->f_fssubtype
;
6870 * It's possible for there to be more than 2^^31 blocks in the filesystem, so we
6871 * have to fudge the numbers here in that case. We inflate the blocksize in order
6872 * to reflect the filesystem size as best we can.
6874 if ((sfsp
->f_blocks
> LONG_MAX
)
6875 /* Hack for 4061702 . I think the real fix is for Carbon to
6876 * look for some volume capability and not depend on hidden
6877 * semantics agreed between a FS and carbon.
6878 * f_blocks, f_bfree, and f_bavail set to -1 is the trigger
6879 * for Carbon to set bNoVolumeSizes volume attribute.
6880 * Without this the webdavfs files cannot be copied onto
6881 * disk as they look huge. This change should not affect
6882 * XSAN as they should not setting these to -1..
6884 && (sfsp
->f_blocks
!= 0xffffffffffffffffULL
)
6885 && (sfsp
->f_bfree
!= 0xffffffffffffffffULL
)
6886 && (sfsp
->f_bavail
!= 0xffffffffffffffffULL
)) {
6890 * Work out how far we have to shift the block count down to make it fit.
6891 * Note that it's possible to have to shift so far that the resulting
6892 * blocksize would be unreportably large. At that point, we will clip
6893 * any values that don't fit.
6895 * For safety's sake, we also ensure that f_iosize is never reported as
6896 * being smaller than f_bsize.
6898 for (shift
= 0; shift
< 32; shift
++) {
6899 if ((sfsp
->f_blocks
>> shift
) <= LONG_MAX
)
6901 if ((sfsp
->f_bsize
<< (shift
+ 1)) > LONG_MAX
)
6904 #define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s)))
6905 sfs
.f_blocks
= (long)__SHIFT_OR_CLIP(sfsp
->f_blocks
, shift
);
6906 sfs
.f_bfree
= (long)__SHIFT_OR_CLIP(sfsp
->f_bfree
, shift
);
6907 sfs
.f_bavail
= (long)__SHIFT_OR_CLIP(sfsp
->f_bavail
, shift
);
6908 #undef __SHIFT_OR_CLIP
6909 sfs
.f_bsize
= (long)(sfsp
->f_bsize
<< shift
);
6910 sfs
.f_iosize
= lmax(sfsp
->f_iosize
, sfsp
->f_bsize
);
6912 /* filesystem is small enough to be reported honestly */
6913 sfs
.f_bsize
= (long)sfsp
->f_bsize
;
6914 sfs
.f_iosize
= (long)sfsp
->f_iosize
;
6915 sfs
.f_blocks
= (long)sfsp
->f_blocks
;
6916 sfs
.f_bfree
= (long)sfsp
->f_bfree
;
6917 sfs
.f_bavail
= (long)sfsp
->f_bavail
;
6919 sfs
.f_files
= (long)sfsp
->f_files
;
6920 sfs
.f_ffree
= (long)sfsp
->f_ffree
;
6921 sfs
.f_fsid
= sfsp
->f_fsid
;
6922 sfs
.f_owner
= sfsp
->f_owner
;
6923 strlcpy(&sfs
.f_fstypename
[0], &sfsp
->f_fstypename
[0], MFSNAMELEN
);
6924 strlcpy(&sfs
.f_mntonname
[0], &sfsp
->f_mntonname
[0], MNAMELEN
);
6925 strlcpy(&sfs
.f_mntfromname
[0], &sfsp
->f_mntfromname
[0], MNAMELEN
);
6928 copy_size
-= (sizeof(sfs
.f_reserved3
) + sizeof(sfs
.f_reserved4
));
6930 error
= copyout((caddr_t
)&sfs
, bufp
, copy_size
);
6933 if (sizep
!= NULL
) {
6940 * copy stat structure into user_stat structure.
6942 void munge_stat(struct stat
*sbp
, struct user_stat
*usbp
)
6944 bzero(usbp
, sizeof(struct user_stat
));
6946 usbp
->st_dev
= sbp
->st_dev
;
6947 usbp
->st_ino
= sbp
->st_ino
;
6948 usbp
->st_mode
= sbp
->st_mode
;
6949 usbp
->st_nlink
= sbp
->st_nlink
;
6950 usbp
->st_uid
= sbp
->st_uid
;
6951 usbp
->st_gid
= sbp
->st_gid
;
6952 usbp
->st_rdev
= sbp
->st_rdev
;
6953 #ifndef _POSIX_C_SOURCE
6954 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
6955 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
6956 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
6957 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
6958 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
6959 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
6961 usbp
->st_atime
= sbp
->st_atime
;
6962 usbp
->st_atimensec
= sbp
->st_atimensec
;
6963 usbp
->st_mtime
= sbp
->st_mtime
;
6964 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
6965 usbp
->st_ctime
= sbp
->st_ctime
;
6966 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
6968 usbp
->st_size
= sbp
->st_size
;
6969 usbp
->st_blocks
= sbp
->st_blocks
;
6970 usbp
->st_blksize
= sbp
->st_blksize
;
6971 usbp
->st_flags
= sbp
->st_flags
;
6972 usbp
->st_gen
= sbp
->st_gen
;
6973 usbp
->st_lspare
= sbp
->st_lspare
;
6974 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
6975 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];
6979 * copy stat64 structure into user_stat64 structure.
6981 void munge_stat64(struct stat64
*sbp
, struct user_stat64
*usbp
)
6983 bzero(usbp
, sizeof(struct user_stat
));
6985 usbp
->st_dev
= sbp
->st_dev
;
6986 usbp
->st_ino
= sbp
->st_ino
;
6987 usbp
->st_mode
= sbp
->st_mode
;
6988 usbp
->st_nlink
= sbp
->st_nlink
;
6989 usbp
->st_uid
= sbp
->st_uid
;
6990 usbp
->st_gid
= sbp
->st_gid
;
6991 usbp
->st_rdev
= sbp
->st_rdev
;
6992 #ifndef _POSIX_C_SOURCE
6993 usbp
->st_atimespec
.tv_sec
= sbp
->st_atimespec
.tv_sec
;
6994 usbp
->st_atimespec
.tv_nsec
= sbp
->st_atimespec
.tv_nsec
;
6995 usbp
->st_mtimespec
.tv_sec
= sbp
->st_mtimespec
.tv_sec
;
6996 usbp
->st_mtimespec
.tv_nsec
= sbp
->st_mtimespec
.tv_nsec
;
6997 usbp
->st_ctimespec
.tv_sec
= sbp
->st_ctimespec
.tv_sec
;
6998 usbp
->st_ctimespec
.tv_nsec
= sbp
->st_ctimespec
.tv_nsec
;
6999 usbp
->st_birthtimespec
.tv_sec
= sbp
->st_birthtimespec
.tv_sec
;
7000 usbp
->st_birthtimespec
.tv_nsec
= sbp
->st_birthtimespec
.tv_nsec
;
7002 usbp
->st_atime
= sbp
->st_atime
;
7003 usbp
->st_atimensec
= sbp
->st_atimensec
;
7004 usbp
->st_mtime
= sbp
->st_mtime
;
7005 usbp
->st_mtimensec
= sbp
->st_mtimensec
;
7006 usbp
->st_ctime
= sbp
->st_ctime
;
7007 usbp
->st_ctimensec
= sbp
->st_ctimensec
;
7008 usbp
->st_birthtime
= sbp
->st_birthtime
;
7009 usbp
->st_birthtimensec
= sbp
->st_birthtimensec
;
7011 usbp
->st_size
= sbp
->st_size
;
7012 usbp
->st_blocks
= sbp
->st_blocks
;
7013 usbp
->st_blksize
= sbp
->st_blksize
;
7014 usbp
->st_flags
= sbp
->st_flags
;
7015 usbp
->st_gen
= sbp
->st_gen
;
7016 usbp
->st_lspare
= sbp
->st_lspare
;
7017 usbp
->st_qspare
[0] = sbp
->st_qspare
[0];
7018 usbp
->st_qspare
[1] = sbp
->st_qspare
[1];