2 * Copyright (c) 1995-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/kernel.h>
40 #include <sys/syslog.h>
41 #include <sys/vnode_internal.h>
42 #include <sys/mount_internal.h>
43 #include <sys/proc_internal.h>
44 #include <sys/file_internal.h>
45 #include <sys/kauth.h>
46 #include <sys/uio_internal.h>
47 #include <sys/malloc.h>
49 #include <sys/sysproto.h>
50 #include <sys/xattr.h>
51 #include <sys/fsevents.h>
52 #include <kern/kalloc.h>
53 #include <miscfs/specfs/specdev.h>
54 #include <security/audit/audit.h>
57 #include <security/mac_framework.h>
60 #define ATTR_TIME_SIZE -1
63 * Structure describing the state of an in-progress attrlist operation.
65 struct _attrlist_buf
{
71 attribute_set_t actual
;
72 attribute_set_t valid
;
77 * Attempt to pack a fixed width attribute of size (count) bytes from
78 * source to our attrlist buffer.
81 attrlist_pack_fixed(struct _attrlist_buf
*ab
, void *source
, ssize_t count
)
84 * Use ssize_t for pointer math purposes,
85 * since a ssize_t is a signed long
90 * Compute the amount of remaining space in the attrlist buffer
91 * based on how much we've used for fixed width fields vs. the
92 * start of the attributes.
94 * If we've still got room, then 'fit' will contain the amount of
97 * Note that this math is safe because, in the event that the
98 * fixed-width cursor has moved beyond the end of the buffer,
99 * then, the second input into lmin() below will be negative, and
100 * we will fail the (fit > 0) check below.
102 fit
= lmin(count
, ab
->allocated
- (ab
->fixedcursor
- ab
->base
));
104 /* Copy in as much as we can */
105 bcopy(source
, ab
->fixedcursor
, fit
);
108 /* always move in increments of 4, even if we didn't pack an attribute. */
109 ab
->fixedcursor
+= roundup(count
, 4);
113 * Attempt to pack one (or two) variable width attributes into the attrlist
114 * buffer. If we are trying to pack two variable width attributes, they are treated
115 * as a single variable-width attribute from the POV of the system call caller.
117 * Recall that a variable-width attribute has two components: the fixed-width
118 * attribute that tells the caller where to look, and the actual variable width data.
121 attrlist_pack_variable2(struct _attrlist_buf
*ab
, const void *source
, ssize_t count
,
122 const void *ext
, ssize_t extcount
)
124 /* Use ssize_t's for pointer math ease */
125 struct attrreference ar
;
129 * Pack the fixed-width component to the variable object.
130 * Note that we may be able to pack the fixed width attref, but not
131 * the variable (if there's no room).
133 ar
.attr_dataoffset
= ab
->varcursor
- ab
->fixedcursor
;
134 ar
.attr_length
= count
+ extcount
;
135 attrlist_pack_fixed(ab
, &ar
, sizeof(ar
));
138 * Use an lmin() to do a signed comparison. We use a signed comparison
139 * to detect the 'out of memory' conditions as described above in the
140 * fixed width check above.
142 * Then pack the first variable attribute as space allows. Note that we advance
143 * the variable cursor only if we we had some available space.
145 fit
= lmin(count
, ab
->allocated
- (ab
->varcursor
- ab
->base
));
147 if (source
!= NULL
) {
148 bcopy(source
, ab
->varcursor
, fit
);
150 ab
->varcursor
+= fit
;
153 /* Compute the available space for the second attribute */
154 fit
= lmin(extcount
, ab
->allocated
- (ab
->varcursor
- ab
->base
));
156 /* Copy in data for the second attribute (if needed) if there is room */
158 bcopy(ext
, ab
->varcursor
, fit
);
160 ab
->varcursor
+= fit
;
162 /* always move in increments of 4 */
163 ab
->varcursor
= (char *)roundup((uintptr_t)ab
->varcursor
, 4);
167 * Packing a single variable-width attribute is the same as calling the two, but with
168 * an invalid 2nd attribute.
171 attrlist_pack_variable(struct _attrlist_buf
*ab
, const void *source
, ssize_t count
)
173 attrlist_pack_variable2(ab
, source
, count
, NULL
, 0);
177 * Attempt to pack a string. This is a special case of a variable width attribute.
179 * If "source" is NULL, then an empty string ("") will be packed. If "source" is
180 * not NULL, but "count" is zero, then "source" is assumed to be a NUL-terminated
181 * C-string. If "source" is not NULL and "count" is not zero, then only the first
182 * "count" bytes of "source" will be copied, and a NUL terminator will be added.
184 * If the attrlist buffer doesn't have enough room to hold the entire string (including
185 * NUL terminator), then copy as much as will fit. The attrlist buffer's "varcursor"
186 * will always be updated based on the entire length of the string (including NUL
187 * terminator); this means "varcursor" may end up pointing beyond the end of the
188 * allocated buffer space.
191 attrlist_pack_string(struct _attrlist_buf
*ab
, const char *source
, ssize_t count
)
193 struct attrreference ar
;
197 * Supplied count is character count of string text, excluding trailing nul
198 * which we always supply here.
200 if (source
== NULL
) {
202 } else if (count
== 0) {
203 count
= strlen(source
);
207 * Construct the fixed-width attribute that refers to this string.
209 ar
.attr_dataoffset
= ab
->varcursor
- ab
->fixedcursor
;
210 ar
.attr_length
= count
+ 1;
211 attrlist_pack_fixed(ab
, &ar
, sizeof(ar
));
214 * Now compute how much available memory we have to copy the string text.
216 * space = the number of bytes available in the attribute buffer to hold the
219 * fit = the number of bytes to copy from the start of the string into the
220 * attribute buffer, NOT including the NUL terminator. If the attribute
221 * buffer is large enough, this will be the string's length; otherwise, it
222 * will be equal to "space".
224 space
= ab
->allocated
- (ab
->varcursor
- ab
->base
);
225 fit
= lmin(count
, space
);
230 * If there is space remaining, copy data in, and
231 * accommodate the trailing NUL terminator.
233 * NOTE: if "space" is too small to hold the string and its NUL
234 * terminator (space < fit + 1), then the string value in the attribute
235 * buffer will NOT be NUL terminated!
237 * NOTE 2: bcopy() will do nothing if the length ("fit") is zero.
238 * Therefore, we don't bother checking for that here.
240 bcopy(source
, ab
->varcursor
, fit
);
241 /* is there room for our trailing nul? */
243 ab
->varcursor
[fit
++] = '\0';
244 /* 'fit' now the number of bytes AFTER adding in the NUL */
246 * Zero out any additional bytes we might have as a
247 * result of rounding up.
249 bytes_to_zero
= min((roundup(fit
, 4) - fit
),
252 bzero(&(ab
->varcursor
[fit
]), bytes_to_zero
);
257 * always move in increments of 4 (including the trailing NUL)
259 ab
->varcursor
+= roundup((count
+ 1), 4);
262 #define ATTR_PACK4(AB, V) \
264 if ((AB.allocated - (AB.fixedcursor - AB.base)) >= 4) { \
265 *(uint32_t *)AB.fixedcursor = V; \
266 AB.fixedcursor += 4; \
270 #define ATTR_PACK8(AB, V) \
272 if ((AB.allocated - (AB.fixedcursor - AB.base)) >= 8) { \
273 memcpy(AB.fixedcursor, &V, 8); \
274 AB.fixedcursor += 8; \
278 #define ATTR_PACK(b, v) attrlist_pack_fixed(b, &v, sizeof(v))
279 #define ATTR_PACK_CAST(b, t, v) \
285 #define ATTR_PACK_TIME(b, v, is64) \
288 struct user64_timespec us = {.tv_sec = v.tv_sec, .tv_nsec = v.tv_nsec}; \
291 struct user32_timespec us = {.tv_sec = v.tv_sec, .tv_nsec = v.tv_nsec}; \
298 * Table-driven setup for all valid common/volume attributes.
300 struct getvolattrlist_attrtab
{
303 #define VFSATTR_BIT(b) (VFSATTR_ ## b)
306 static struct getvolattrlist_attrtab getvolattrlist_common_tab
[] = {
307 {.attr
= ATTR_CMN_NAME
, .bits
= 0, .size
= sizeof(struct attrreference
)},
308 {.attr
= ATTR_CMN_DEVID
, .bits
= 0, .size
= sizeof(dev_t
)},
309 {.attr
= ATTR_CMN_FSID
, .bits
= 0, .size
= sizeof(fsid_t
)},
310 {.attr
= ATTR_CMN_OBJTYPE
, .bits
= 0, .size
= sizeof(fsobj_type_t
)},
311 {.attr
= ATTR_CMN_OBJTAG
, .bits
= 0, .size
= sizeof(fsobj_tag_t
)},
312 {.attr
= ATTR_CMN_OBJID
, .bits
= 0, .size
= sizeof(fsobj_id_t
)},
313 {.attr
= ATTR_CMN_OBJPERMANENTID
, .bits
= 0, .size
= sizeof(fsobj_id_t
)},
314 {.attr
= ATTR_CMN_PAROBJID
, .bits
= 0, .size
= sizeof(fsobj_id_t
)},
315 {.attr
= ATTR_CMN_SCRIPT
, .bits
= 0, .size
= sizeof(text_encoding_t
)},
316 {.attr
= ATTR_CMN_CRTIME
, .bits
= VFSATTR_BIT(f_create_time
), .size
= ATTR_TIME_SIZE
},
317 {.attr
= ATTR_CMN_MODTIME
, .bits
= VFSATTR_BIT(f_modify_time
), .size
= ATTR_TIME_SIZE
},
318 {.attr
= ATTR_CMN_CHGTIME
, .bits
= VFSATTR_BIT(f_modify_time
), .size
= ATTR_TIME_SIZE
},
319 {.attr
= ATTR_CMN_ACCTIME
, .bits
= VFSATTR_BIT(f_access_time
), .size
= ATTR_TIME_SIZE
},
320 {.attr
= ATTR_CMN_BKUPTIME
, .bits
= VFSATTR_BIT(f_backup_time
), .size
= ATTR_TIME_SIZE
},
321 {.attr
= ATTR_CMN_FNDRINFO
, .bits
= 0, .size
= 32},
322 {.attr
= ATTR_CMN_OWNERID
, .bits
= 0, .size
= sizeof(uid_t
)},
323 {.attr
= ATTR_CMN_GRPID
, .bits
= 0, .size
= sizeof(gid_t
)},
324 {.attr
= ATTR_CMN_ACCESSMASK
, .bits
= 0, .size
= sizeof(uint32_t)},
325 {.attr
= ATTR_CMN_FLAGS
, .bits
= 0, .size
= sizeof(uint32_t)},
326 {.attr
= ATTR_CMN_USERACCESS
, .bits
= 0, .size
= sizeof(uint32_t)},
327 {.attr
= ATTR_CMN_EXTENDED_SECURITY
, .bits
= 0, .size
= sizeof(struct attrreference
)},
328 {.attr
= ATTR_CMN_UUID
, .bits
= 0, .size
= sizeof(guid_t
)},
329 {.attr
= ATTR_CMN_GRPUUID
, .bits
= 0, .size
= sizeof(guid_t
)},
330 {.attr
= ATTR_CMN_FILEID
, .bits
= 0, .size
= sizeof(uint64_t)},
331 {.attr
= ATTR_CMN_PARENTID
, .bits
= 0, .size
= sizeof(uint64_t)},
332 {.attr
= ATTR_CMN_RETURNED_ATTRS
, .bits
= 0, .size
= sizeof(attribute_set_t
)},
333 {.attr
= ATTR_CMN_ERROR
, .bits
= 0, .size
= sizeof(uint32_t)},
334 {.attr
= 0, .bits
= 0, .size
= 0}
336 #define ATTR_CMN_VOL_INVALID \
337 (ATTR_CMN_EXTENDED_SECURITY | ATTR_CMN_UUID | ATTR_CMN_GRPUUID | \
338 ATTR_CMN_FILEID | ATTR_CMN_PARENTID)
340 static struct getvolattrlist_attrtab getvolattrlist_vol_tab
[] = {
341 {.attr
= ATTR_VOL_FSTYPE
, .bits
= 0, .size
= sizeof(uint32_t)},
342 {.attr
= ATTR_VOL_SIGNATURE
, .bits
= VFSATTR_BIT(f_signature
), .size
= sizeof(uint32_t)},
343 {.attr
= ATTR_VOL_SIZE
, .bits
= VFSATTR_BIT(f_blocks
) | VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
344 {.attr
= ATTR_VOL_SPACEFREE
, .bits
= VFSATTR_BIT(f_bfree
) | VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
345 {.attr
= ATTR_VOL_SPACEAVAIL
, .bits
= VFSATTR_BIT(f_bavail
) | VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
346 {.attr
= ATTR_VOL_MINALLOCATION
, .bits
= VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
347 {.attr
= ATTR_VOL_ALLOCATIONCLUMP
, .bits
= VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
348 {.attr
= ATTR_VOL_IOBLOCKSIZE
, .bits
= VFSATTR_BIT(f_iosize
), .size
= sizeof(uint32_t)},
349 {.attr
= ATTR_VOL_OBJCOUNT
, .bits
= VFSATTR_BIT(f_objcount
), .size
= sizeof(uint32_t)},
350 {.attr
= ATTR_VOL_FILECOUNT
, .bits
= VFSATTR_BIT(f_filecount
), .size
= sizeof(uint32_t)},
351 {.attr
= ATTR_VOL_DIRCOUNT
, .bits
= VFSATTR_BIT(f_dircount
), .size
= sizeof(uint32_t)},
352 {.attr
= ATTR_VOL_MAXOBJCOUNT
, .bits
= VFSATTR_BIT(f_maxobjcount
), .size
= sizeof(uint32_t)},
353 {.attr
= ATTR_VOL_MOUNTPOINT
, .bits
= 0, .size
= sizeof(struct attrreference
)},
354 {.attr
= ATTR_VOL_NAME
, .bits
= VFSATTR_BIT(f_vol_name
), .size
= sizeof(struct attrreference
)},
355 {.attr
= ATTR_VOL_MOUNTFLAGS
, .bits
= 0, .size
= sizeof(uint32_t)},
356 {.attr
= ATTR_VOL_MOUNTEDDEVICE
, .bits
= 0, .size
= sizeof(struct attrreference
)},
357 {.attr
= ATTR_VOL_ENCODINGSUSED
, .bits
= 0, .size
= sizeof(uint64_t)},
358 {.attr
= ATTR_VOL_CAPABILITIES
, .bits
= VFSATTR_BIT(f_capabilities
), .size
= sizeof(vol_capabilities_attr_t
)},
359 {.attr
= ATTR_VOL_UUID
, .bits
= VFSATTR_BIT(f_uuid
), .size
= sizeof(uuid_t
)},
360 {.attr
= ATTR_VOL_QUOTA_SIZE
, .bits
= VFSATTR_BIT(f_quota
) | VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
361 {.attr
= ATTR_VOL_RESERVED_SIZE
, .bits
= VFSATTR_BIT(f_reserved
) | VFSATTR_BIT(f_bsize
), .size
= sizeof(off_t
)},
362 {.attr
= ATTR_VOL_ATTRIBUTES
, .bits
= VFSATTR_BIT(f_attributes
), .size
= sizeof(vol_attributes_attr_t
)},
363 {.attr
= ATTR_VOL_INFO
, .bits
= 0, .size
= 0},
364 {.attr
= 0, .bits
= 0, .size
= 0}
368 getvolattrlist_parsetab(struct getvolattrlist_attrtab
*tab
, attrgroup_t attrs
, struct vfs_attr
*vsp
,
369 ssize_t
*sizep
, int is_64bit
, unsigned int maxiter
)
371 attrgroup_t recognised
;
375 /* is this attribute set? */
376 if (tab
->attr
& attrs
) {
377 recognised
|= tab
->attr
;
378 vsp
->f_active
|= tab
->bits
;
379 if (tab
->size
== ATTR_TIME_SIZE
) {
381 *sizep
+= sizeof(struct user64_timespec
);
383 *sizep
+= sizeof(struct user32_timespec
);
389 } while (((++tab
)->attr
!= 0) && (--maxiter
> 0));
391 /* check to make sure that we recognised all of the passed-in attributes */
392 if (attrs
& ~recognised
) {
399 * Given the attributes listed in alp, configure vap to request
400 * the data from a filesystem.
403 getvolattrlist_setupvfsattr(struct attrlist
*alp
, struct vfs_attr
*vsp
, ssize_t
*sizep
, int is_64bit
)
411 * Parse the above tables.
413 *sizep
= sizeof(uint32_t); /* length count */
414 if (alp
->commonattr
) {
415 if ((alp
->commonattr
& ATTR_CMN_VOL_INVALID
) &&
416 (alp
->commonattr
& ATTR_CMN_RETURNED_ATTRS
) == 0) {
419 if ((error
= getvolattrlist_parsetab(getvolattrlist_common_tab
,
420 alp
->commonattr
, vsp
, sizep
,
422 sizeof(getvolattrlist_common_tab
) / sizeof(getvolattrlist_common_tab
[0]))) != 0) {
427 (error
= getvolattrlist_parsetab(getvolattrlist_vol_tab
, alp
->volattr
, vsp
, sizep
, is_64bit
, sizeof(getvolattrlist_vol_tab
) / sizeof(getvolattrlist_vol_tab
[0]))) != 0) {
435 * Given the attributes listed in asp and those supported
436 * in the vsp, fixup the asp attributes to reflect any
437 * missing attributes from the file system
440 getvolattrlist_fixupattrs(attribute_set_t
*asp
, struct vfs_attr
*vsp
)
442 struct getvolattrlist_attrtab
*tab
;
444 if (asp
->commonattr
) {
445 tab
= getvolattrlist_common_tab
;
447 if ((tab
->attr
& asp
->commonattr
) &&
449 ((tab
->bits
& vsp
->f_supported
) == 0)) {
450 asp
->commonattr
&= ~tab
->attr
;
452 } while ((++tab
)->attr
!= 0);
455 tab
= getvolattrlist_vol_tab
;
457 if ((tab
->attr
& asp
->volattr
) &&
459 ((tab
->bits
& vsp
->f_supported
) == 0)) {
460 asp
->volattr
&= ~tab
->attr
;
462 } while ((++tab
)->attr
!= 0);
467 * Table-driven setup for all valid common/dir/file/fork attributes against files.
469 struct getattrlist_attrtab
{
472 #define VATTR_BIT(b) (VNODE_ATTR_ ## b)
474 kauth_action_t action
;
478 * A zero after the ATTR_ bit indicates that we don't expect the underlying FS to report back with this
479 * information, and we will synthesize it at the VFS level.
481 static struct getattrlist_attrtab getattrlist_common_tab
[] = {
482 {.attr
= ATTR_CMN_NAME
, .bits
= VATTR_BIT(va_name
), .size
= sizeof(struct attrreference
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
483 {.attr
= ATTR_CMN_DEVID
, .bits
= VATTR_BIT(va_fsid
), .size
= sizeof(dev_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
484 {.attr
= ATTR_CMN_FSID
, .bits
= VATTR_BIT(va_fsid64
), .size
= sizeof(fsid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
485 {.attr
= ATTR_CMN_OBJTYPE
, .bits
= 0, .size
= sizeof(fsobj_type_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
486 {.attr
= ATTR_CMN_OBJTAG
, .bits
= 0, .size
= sizeof(fsobj_tag_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
487 {.attr
= ATTR_CMN_OBJID
, .bits
= VATTR_BIT(va_fileid
) | VATTR_BIT(va_linkid
), .size
= sizeof(fsobj_id_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
488 {.attr
= ATTR_CMN_OBJPERMANENTID
, .bits
= VATTR_BIT(va_fileid
) | VATTR_BIT(va_linkid
), .size
= sizeof(fsobj_id_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
489 {.attr
= ATTR_CMN_PAROBJID
, .bits
= VATTR_BIT(va_parentid
), .size
= sizeof(fsobj_id_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
490 {.attr
= ATTR_CMN_SCRIPT
, .bits
= VATTR_BIT(va_encoding
), .size
= sizeof(text_encoding_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
491 {.attr
= ATTR_CMN_CRTIME
, .bits
= VATTR_BIT(va_create_time
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
492 {.attr
= ATTR_CMN_MODTIME
, .bits
= VATTR_BIT(va_modify_time
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
493 {.attr
= ATTR_CMN_CHGTIME
, .bits
= VATTR_BIT(va_change_time
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
494 {.attr
= ATTR_CMN_ACCTIME
, .bits
= VATTR_BIT(va_access_time
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
495 {.attr
= ATTR_CMN_BKUPTIME
, .bits
= VATTR_BIT(va_backup_time
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
496 {.attr
= ATTR_CMN_FNDRINFO
, .bits
= 0, .size
= 32, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
497 {.attr
= ATTR_CMN_OWNERID
, .bits
= VATTR_BIT(va_uid
), .size
= sizeof(uid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
498 {.attr
= ATTR_CMN_GRPID
, .bits
= VATTR_BIT(va_gid
), .size
= sizeof(gid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
499 {.attr
= ATTR_CMN_ACCESSMASK
, .bits
= VATTR_BIT(va_mode
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
500 {.attr
= ATTR_CMN_FLAGS
, .bits
= VATTR_BIT(va_flags
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
501 {.attr
= ATTR_CMN_GEN_COUNT
, .bits
= VATTR_BIT(va_write_gencount
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
502 {.attr
= ATTR_CMN_DOCUMENT_ID
, .bits
= VATTR_BIT(va_document_id
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
503 {.attr
= ATTR_CMN_USERACCESS
, .bits
= 0, .size
= sizeof(uint32_t), .action
= 0},
504 {.attr
= ATTR_CMN_EXTENDED_SECURITY
, .bits
= VATTR_BIT(va_acl
), .size
= sizeof(struct attrreference
), .action
= KAUTH_VNODE_READ_SECURITY
},
505 {.attr
= ATTR_CMN_UUID
, .bits
= VATTR_BIT(va_uuuid
), .size
= sizeof(guid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
506 {.attr
= ATTR_CMN_GRPUUID
, .bits
= VATTR_BIT(va_guuid
), .size
= sizeof(guid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
507 {.attr
= ATTR_CMN_FILEID
, .bits
= VATTR_BIT(va_fileid
), .size
= sizeof(uint64_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
508 {.attr
= ATTR_CMN_PARENTID
, .bits
= VATTR_BIT(va_parentid
), .size
= sizeof(uint64_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
509 {.attr
= ATTR_CMN_FULLPATH
, .bits
= 0, .size
= sizeof(struct attrreference
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
510 {.attr
= ATTR_CMN_ADDEDTIME
, .bits
= VATTR_BIT(va_addedtime
), .size
= ATTR_TIME_SIZE
, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
511 {.attr
= ATTR_CMN_RETURNED_ATTRS
, .bits
= 0, .size
= sizeof(attribute_set_t
), .action
= 0},
512 {.attr
= ATTR_CMN_ERROR
, .bits
= 0, .size
= sizeof(uint32_t), .action
= 0},
513 {.attr
= ATTR_CMN_DATA_PROTECT_FLAGS
, .bits
= VATTR_BIT(va_dataprotect_class
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
514 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
517 static struct getattrlist_attrtab getattrlist_dir_tab
[] = {
518 {.attr
= ATTR_DIR_LINKCOUNT
, .bits
= VATTR_BIT(va_dirlinkcount
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
519 {.attr
= ATTR_DIR_ENTRYCOUNT
, .bits
= VATTR_BIT(va_nchildren
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
520 {.attr
= ATTR_DIR_MOUNTSTATUS
, .bits
= 0, .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
521 {.attr
= ATTR_DIR_ALLOCSIZE
, .bits
= VATTR_BIT(va_total_alloc
) | VATTR_BIT(va_total_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
522 {.attr
= ATTR_DIR_IOBLOCKSIZE
, .bits
= VATTR_BIT(va_iosize
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
523 {.attr
= ATTR_DIR_DATALENGTH
, .bits
= VATTR_BIT(va_total_size
) | VATTR_BIT(va_data_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
524 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
526 static struct getattrlist_attrtab getattrlist_file_tab
[] = {
527 {.attr
= ATTR_FILE_LINKCOUNT
, .bits
= VATTR_BIT(va_nlink
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
528 {.attr
= ATTR_FILE_TOTALSIZE
, .bits
= VATTR_BIT(va_total_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
529 {.attr
= ATTR_FILE_ALLOCSIZE
, .bits
= VATTR_BIT(va_total_alloc
) | VATTR_BIT(va_total_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
530 {.attr
= ATTR_FILE_IOBLOCKSIZE
, .bits
= VATTR_BIT(va_iosize
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
531 {.attr
= ATTR_FILE_CLUMPSIZE
, .bits
= 0, .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
532 {.attr
= ATTR_FILE_DEVTYPE
, .bits
= VATTR_BIT(va_rdev
), .size
= sizeof(dev_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
533 {.attr
= ATTR_FILE_DATALENGTH
, .bits
= VATTR_BIT(va_total_size
) | VATTR_BIT(va_data_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
534 {.attr
= ATTR_FILE_DATAALLOCSIZE
, .bits
= VATTR_BIT(va_total_alloc
) | VATTR_BIT(va_data_alloc
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
535 {.attr
= ATTR_FILE_RSRCLENGTH
, .bits
= 0, .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
536 {.attr
= ATTR_FILE_RSRCALLOCSIZE
, .bits
= 0, .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
537 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
540 //for forkattr bits repurposed as new common attributes
541 static struct getattrlist_attrtab getattrlist_common_extended_tab
[] = {
542 {.attr
= ATTR_CMNEXT_RELPATH
, .bits
= 0, .size
= sizeof(struct attrreference
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
543 {.attr
= ATTR_CMNEXT_PRIVATESIZE
, .bits
= VATTR_BIT(va_private_size
), .size
= sizeof(off_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
544 {.attr
= ATTR_CMNEXT_LINKID
, .bits
= VATTR_BIT(va_fileid
) | VATTR_BIT(va_linkid
), .size
= sizeof(uint64_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
545 {.attr
= ATTR_CMNEXT_NOFIRMLINKPATH
, .bits
= 0, .size
= sizeof(struct attrreference
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
546 {.attr
= ATTR_CMNEXT_REALDEVID
, .bits
= VATTR_BIT(va_devid
), .size
= sizeof(uint32_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
547 {.attr
= ATTR_CMNEXT_REALFSID
, .bits
= VATTR_BIT(va_fsid64
), .size
= sizeof(fsid_t
), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
548 {.attr
= ATTR_CMNEXT_CLONEID
, .bits
= VATTR_BIT(va_clone_id
), .size
= sizeof(uint64_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
549 {.attr
= ATTR_CMNEXT_EXT_FLAGS
, .bits
= VATTR_BIT(va_extflags
), .size
= sizeof(uint64_t), .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
550 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
554 * This table is for attributes which are only set from the getattrlistbulk(2)
555 * call. These attributes have already been set from the common, file and
556 * directory tables but the vattr bits have not been recorded. Since these
557 * vattr bits are only used from the bulk call, we have a seperate table for
559 * The sizes are not returned from here since the sizes have already been
560 * accounted from the common, file and directory tables.
562 static struct getattrlist_attrtab getattrlistbulk_common_tab
[] = {
563 {.attr
= ATTR_CMN_DEVID
, .bits
= VATTR_BIT(va_devid
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
564 {.attr
= ATTR_CMN_FSID
, .bits
= VATTR_BIT(va_fsid64
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
565 {.attr
= ATTR_CMN_OBJTYPE
, .bits
= VATTR_BIT(va_objtype
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
566 {.attr
= ATTR_CMN_OBJTAG
, .bits
= VATTR_BIT(va_objtag
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
567 {.attr
= ATTR_CMN_USERACCESS
, .bits
= VATTR_BIT(va_user_access
), .size
= 0, .action
= 0},
568 {.attr
= ATTR_CMN_FNDRINFO
, .bits
= VATTR_BIT(va_finderinfo
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
569 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
572 static struct getattrlist_attrtab getattrlistbulk_file_tab
[] = {
573 {.attr
= ATTR_FILE_RSRCLENGTH
, .bits
= VATTR_BIT(va_rsrc_length
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
574 {.attr
= ATTR_FILE_RSRCALLOCSIZE
, .bits
= VATTR_BIT(va_rsrc_alloc
), .size
= 0, .action
= KAUTH_VNODE_READ_ATTRIBUTES
},
575 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
578 static struct getattrlist_attrtab getattrlistbulk_common_extended_tab
[] = {
579 /* getattrlist_parsetab() expects > 1 entries */
580 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0},
581 {.attr
= 0, .bits
= 0, .size
= 0, .action
= 0}
585 * The following are attributes that VFS can derive.
587 * A majority of them are the same attributes that are required for stat(2) and statfs(2).
589 #define VFS_DFLT_ATTR_VOL (ATTR_VOL_FSTYPE | ATTR_VOL_SIGNATURE | \
590 ATTR_VOL_SIZE | ATTR_VOL_SPACEFREE | ATTR_VOL_QUOTA_SIZE | ATTR_VOL_RESERVED_SIZE | \
591 ATTR_VOL_SPACEAVAIL | ATTR_VOL_MINALLOCATION | \
592 ATTR_VOL_ALLOCATIONCLUMP | ATTR_VOL_IOBLOCKSIZE | \
593 ATTR_VOL_MOUNTPOINT | ATTR_VOL_MOUNTFLAGS | \
594 ATTR_VOL_MOUNTEDDEVICE | ATTR_VOL_CAPABILITIES | \
595 ATTR_VOL_ATTRIBUTES | ATTR_VOL_ENCODINGSUSED)
597 #define VFS_DFLT_ATTR_CMN (ATTR_CMN_NAME | ATTR_CMN_DEVID | \
598 ATTR_CMN_FSID | ATTR_CMN_OBJTYPE | \
599 ATTR_CMN_OBJTAG | ATTR_CMN_OBJID | \
600 ATTR_CMN_PAROBJID | ATTR_CMN_SCRIPT | \
601 ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | \
602 ATTR_CMN_FNDRINFO | \
603 ATTR_CMN_OWNERID | ATTR_CMN_GRPID | \
604 ATTR_CMN_ACCESSMASK | ATTR_CMN_FLAGS | \
605 ATTR_CMN_USERACCESS | ATTR_CMN_FILEID | \
606 ATTR_CMN_PARENTID | ATTR_CMN_RETURNED_ATTRS | \
607 ATTR_CMN_DOCUMENT_ID | ATTR_CMN_GEN_COUNT | \
608 ATTR_CMN_DATA_PROTECT_FLAGS)
610 #define VFS_DFLT_ATTR_CMN_EXT (ATTR_CMNEXT_PRIVATESIZE | ATTR_CMNEXT_LINKID | \
611 ATTR_CMNEXT_NOFIRMLINKPATH | ATTR_CMNEXT_REALDEVID | \
612 ATTR_CMNEXT_REALFSID | ATTR_CMNEXT_CLONEID | \
613 ATTR_CMNEXT_EXT_FLAGS)
615 #define VFS_DFLT_ATTR_DIR (ATTR_DIR_LINKCOUNT | ATTR_DIR_MOUNTSTATUS)
617 #define VFS_DFLT_ATTR_FILE (ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE | \
618 ATTR_FILE_ALLOCSIZE | ATTR_FILE_IOBLOCKSIZE | \
619 ATTR_FILE_DEVTYPE | ATTR_FILE_DATALENGTH | \
620 ATTR_FILE_DATAALLOCSIZE | ATTR_FILE_RSRCLENGTH | \
621 ATTR_FILE_RSRCALLOCSIZE)
624 getattrlist_parsetab(struct getattrlist_attrtab
*tab
, attrgroup_t attrs
,
625 struct vnode_attr
*vap
, ssize_t
*sizep
, kauth_action_t
*actionp
,
626 int is_64bit
, unsigned int maxiter
)
628 attrgroup_t recognised
;
635 /* is this attribute set? */
636 if (tab
->attr
& attrs
) {
637 recognised
|= tab
->attr
;
639 vap
->va_active
|= tab
->bits
;
642 if (tab
->size
== ATTR_TIME_SIZE
) {
645 struct user64_timespec
);
648 struct user32_timespec
);
655 *actionp
|= tab
->action
;
657 if (attrs
== recognised
) {
658 break; /* all done, get out */
661 } while (((++tab
)->attr
!= 0) && (--maxiter
> 0));
663 /* check to make sure that we recognised all of the passed-in attributes */
664 if (attrs
& ~recognised
) {
671 * Given the attributes listed in alp, configure vap to request
672 * the data from a filesystem.
675 getattrlist_setupvattr(struct attrlist
*alp
, struct vnode_attr
*vap
, ssize_t
*sizep
, kauth_action_t
*actionp
, int is_64bit
, int isdir
, int use_fork
)
680 * Parse the above tables.
682 *sizep
= sizeof(uint32_t); /* length count */
684 if (alp
->commonattr
&&
685 (error
= getattrlist_parsetab(getattrlist_common_tab
, alp
->commonattr
, vap
, sizep
, actionp
, is_64bit
, sizeof(getattrlist_common_tab
) / sizeof(getattrlist_common_tab
[0]))) != 0) {
688 if (isdir
&& alp
->dirattr
&&
689 (error
= getattrlist_parsetab(getattrlist_dir_tab
, alp
->dirattr
, vap
, sizep
, actionp
, is_64bit
, sizeof(getattrlist_dir_tab
) / sizeof(getattrlist_dir_tab
[0]))) != 0) {
692 if (!isdir
&& alp
->fileattr
&&
693 (error
= getattrlist_parsetab(getattrlist_file_tab
, alp
->fileattr
, vap
, sizep
, actionp
, is_64bit
, sizeof(getattrlist_file_tab
) / sizeof(getattrlist_file_tab
[0]))) != 0) {
696 if (use_fork
&& alp
->forkattr
&&
697 (error
= getattrlist_parsetab(getattrlist_common_extended_tab
, alp
->forkattr
, vap
, sizep
, actionp
, is_64bit
, sizeof(getattrlist_common_extended_tab
) / sizeof(getattrlist_common_extended_tab
[0]))) != 0) {
705 * Given the attributes listed in alp, configure vap to request
706 * the data from a filesystem.
709 getattrlist_setupvattr_all(struct attrlist
*alp
, struct vnode_attr
*vap
,
710 enum vtype obj_type
, ssize_t
*fixedsize
, int is_64bit
, int use_fork
)
715 * Parse the above tables.
718 *fixedsize
= sizeof(uint32_t);
720 if (alp
->commonattr
) {
721 error
= getattrlist_parsetab(getattrlist_common_tab
,
722 alp
->commonattr
, vap
, fixedsize
, NULL
, is_64bit
,
723 sizeof(getattrlist_common_tab
) / sizeof(getattrlist_common_tab
[0]));
726 /* Ignore any errrors from the bulk table */
727 (void)getattrlist_parsetab(getattrlistbulk_common_tab
,
728 alp
->commonattr
, vap
, fixedsize
, NULL
, is_64bit
,
729 sizeof(getattrlistbulk_common_tab
) / sizeof(getattrlistbulk_common_tab
[0]));
733 if (!error
&& (obj_type
== VNON
|| obj_type
== VDIR
) && alp
->dirattr
) {
734 error
= getattrlist_parsetab(getattrlist_dir_tab
, alp
->dirattr
,
735 vap
, fixedsize
, NULL
, is_64bit
,
736 sizeof(getattrlist_dir_tab
) / sizeof(getattrlist_dir_tab
[0]));
739 if (!error
&& (obj_type
!= VDIR
) && alp
->fileattr
) {
740 error
= getattrlist_parsetab(getattrlist_file_tab
,
741 alp
->fileattr
, vap
, fixedsize
, NULL
, is_64bit
,
742 sizeof(getattrlist_file_tab
) / sizeof(getattrlist_file_tab
[0]));
745 /*Ignore any errors from the bulk table */
746 (void)getattrlist_parsetab(getattrlistbulk_file_tab
,
747 alp
->fileattr
, vap
, fixedsize
, NULL
, is_64bit
,
748 sizeof(getattrlistbulk_file_tab
) / sizeof(getattrlistbulk_file_tab
[0]));
752 /* fork attributes are like extended common attributes if enabled*/
753 if (!error
&& use_fork
&& alp
->forkattr
) {
754 error
= getattrlist_parsetab(getattrlist_common_extended_tab
,
755 alp
->forkattr
, vap
, fixedsize
, NULL
, is_64bit
,
756 sizeof(getattrlist_common_extended_tab
) / sizeof(getattrlist_common_extended_tab
[0]));
759 (void)getattrlist_parsetab(getattrlistbulk_common_extended_tab
,
760 alp
->forkattr
, vap
, fixedsize
, NULL
, is_64bit
,
761 sizeof(getattrlistbulk_common_extended_tab
) / sizeof(getattrlistbulk_common_extended_tab
[0]));
769 vfs_setup_vattr_from_attrlist(struct attrlist
*alp
, struct vnode_attr
*vap
,
770 enum vtype obj_vtype
, ssize_t
*attrs_fixed_sizep
, vfs_context_t ctx
)
774 // the caller passes us no options, we assume the caller wants the new fork
775 // attr behavior, hence the hardcoded 1
776 return getattrlist_setupvattr_all(alp
, vap
, obj_vtype
,
777 attrs_fixed_sizep
, IS_64BIT_PROCESS(vfs_context_proc(ctx
)), 1);
784 * Given the attributes listed in asp and those supported
785 * in the vap, fixup the asp attributes to reflect any
786 * missing attributes from the file system
789 getattrlist_fixupattrs(attribute_set_t
*asp
, struct vnode_attr
*vap
, int use_fork
)
791 struct getattrlist_attrtab
*tab
;
793 if (asp
->commonattr
) {
794 tab
= getattrlist_common_tab
;
797 * This if() statement is slightly confusing. We're trying to
798 * iterate through all of the bits listed in the array
799 * getattr_common_tab, and see if the filesystem was expected
800 * to support it, and whether or not we need to do anything about this.
802 * This array is full of structs that have 4 fields (attr, bits, size, action).
803 * The first is used to store the ATTR_CMN_* bit that was being requested
804 * from userland. The second stores the VATTR_BIT corresponding to the field
805 * filled in vnode_attr struct. If it is 0, then we don't typically expect
806 * the filesystem to fill in this field. The third is the size of the field,
807 * and the fourth is the type of kauth actions needed.
809 * So, for all of the ATTR_CMN bits listed in this array, we iterate through
810 * them, and check to see if it was both passed down to the filesystem via the
811 * va_active bitfield, and whether or not we expect it to be emitted from
812 * the filesystem. If it wasn't supported, then we un-twiddle the bit and move
813 * on. This is done so that we can uncheck those bits and re-request
814 * a vnode_getattr from the filesystem again.
816 if ((tab
->attr
& asp
->commonattr
) &&
817 (tab
->bits
& vap
->va_active
) &&
818 (tab
->bits
& vap
->va_supported
) == 0) {
819 asp
->commonattr
&= ~tab
->attr
;
821 } while ((++tab
)->attr
!= 0);
824 tab
= getattrlist_dir_tab
;
826 if ((tab
->attr
& asp
->dirattr
) &&
827 (tab
->bits
& vap
->va_active
) &&
828 (vap
->va_supported
& tab
->bits
) == 0) {
829 asp
->dirattr
&= ~tab
->attr
;
831 } while ((++tab
)->attr
!= 0);
834 tab
= getattrlist_file_tab
;
836 if ((tab
->attr
& asp
->fileattr
) &&
837 (tab
->bits
& vap
->va_active
) &&
838 (vap
->va_supported
& tab
->bits
) == 0) {
839 asp
->fileattr
&= ~tab
->attr
;
841 } while ((++tab
)->attr
!= 0);
843 if (use_fork
&& asp
->forkattr
) {
844 tab
= getattrlist_common_extended_tab
;
846 if ((tab
->attr
& asp
->forkattr
) &&
847 (tab
->bits
& vap
->va_active
) &&
848 (vap
->va_supported
& tab
->bits
) == 0) {
849 asp
->forkattr
&= ~tab
->attr
;
851 } while ((++tab
)->attr
!= 0);
856 setattrlist_setfinderinfo(vnode_t vp
, char *fndrinfo
, struct vfs_context
*ctx
)
859 char uio_buf
[UIO_SIZEOF(1)];
862 if ((auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
, uio_buf
, sizeof(uio_buf
))) == NULL
) {
865 uio_addiov(auio
, CAST_USER_ADDR_T(fndrinfo
), 32);
866 error
= vn_setxattr(vp
, XATTR_FINDERINFO_NAME
, auio
, XATTR_NOSECURITY
, ctx
);
871 if (error
== 0 && need_fsevent(FSE_FINDER_INFO_CHANGED
, vp
)) {
872 add_fsevent(FSE_FINDER_INFO_CHANGED
, ctx
, FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
880 * Find something resembling a terminal component name in the mountedonname for vp
884 getattrlist_findnamecomp(const char *mn
, const char **np
, ssize_t
*nl
)
890 * We're looking for the last sequence of non / characters, but
891 * not including any trailing / characters.
896 for (cp
= mn
; *cp
!= 0; cp
++) {
898 /* start of run of chars */
904 /* end of run of chars */
911 /* need to close run? */
919 getvolattrlist(vfs_context_t ctx
, vnode_t vp
, struct attrlist
*alp
,
920 user_addr_t attributeBuffer
, size_t bufferSize
, uint64_t options
,
921 enum uio_seg segflg
, int is_64bit
)
924 struct vnode_attr va
;
925 struct _attrlist_buf ab
;
927 ssize_t fixedsize
, varsize
;
928 const char *cnp
= NULL
; /* protected by ATTR_CMN_NAME */
929 ssize_t cnl
= 0; /* protected by ATTR_CMN_NAME */
934 vnode_t root_vp
= NULL
;
939 vs
.f_vol_name
= NULL
;
943 /* Check for special packing semantics */
944 return_valid
= (alp
->commonattr
& ATTR_CMN_RETURNED_ATTRS
);
945 pack_invalid
= (options
& FSOPT_PACK_INVAL_ATTRS
);
947 /* FSOPT_PACK_INVAL_ATTRS requires ATTR_CMN_RETURNED_ATTRS */
952 /* Keep invalid attrs from being uninitialized */
953 bzero(&vs
, sizeof(vs
));
954 /* Generate a valid mask for post processing */
955 bcopy(&alp
->commonattr
, &ab
.valid
, sizeof(attribute_set_t
));
958 /* If we do not have root vnode, look it up and substitute it in */
959 if (!vnode_isvroot(vp
)) {
961 error
= VFS_ROOT(mnt
, &root_vp
, ctx
);
963 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: volume attributes requested on non-root vnode, but got an error getting root.");
969 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: volume attributes requested on non-root vnode, but no backpointer to mount.");
975 * Set up the vfs_attr structure and call the filesystem.
977 if ((error
= getvolattrlist_setupvfsattr(alp
, &vs
, &fixedsize
, is_64bit
)) != 0) {
978 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: setup for request failed");
981 if (vs
.f_active
!= 0) {
982 /* If we're going to ask for f_vol_name, allocate a buffer to point it at */
983 if (VFSATTR_IS_ACTIVE(&vs
, f_vol_name
)) {
984 vs
.f_vol_name
= (char *) kalloc(MAXPATHLEN
);
985 if (vs
.f_vol_name
== NULL
) {
987 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not allocate f_vol_name buffer");
990 vs
.f_vol_name
[0] = '\0';
993 VFS_DEBUG(ctx
, vp
, "ATTRLIST - calling to get %016llx with supported %016llx", vs
.f_active
, vs
.f_supported
);
994 if ((error
= vfs_getattr(mnt
, &vs
, ctx
)) != 0) {
995 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: filesystem returned %d", error
);
999 error
= mac_mount_check_getattr(ctx
, mnt
, &vs
);
1001 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: MAC framework returned %d", error
);
1006 * Did we ask for something the filesystem doesn't support?
1008 if (!VFSATTR_ALL_SUPPORTED(&vs
)) {
1009 /* default value for volume subtype */
1010 if (VFSATTR_IS_ACTIVE(&vs
, f_fssubtype
)
1011 && !VFSATTR_IS_SUPPORTED(&vs
, f_fssubtype
)) {
1012 VFSATTR_RETURN(&vs
, f_fssubtype
, 0);
1016 * If the file system didn't supply f_signature, then
1017 * default it to 'BD', which is the generic signature
1018 * that most Carbon file systems should return.
1020 if (VFSATTR_IS_ACTIVE(&vs
, f_signature
)
1021 && !VFSATTR_IS_SUPPORTED(&vs
, f_signature
)) {
1022 VFSATTR_RETURN(&vs
, f_signature
, 0x4244);
1025 /* default for block size */
1026 if (VFSATTR_IS_ACTIVE(&vs
, f_bsize
)
1027 && !VFSATTR_IS_SUPPORTED(&vs
, f_bsize
)) {
1028 VFSATTR_RETURN(&vs
, f_bsize
, mnt
->mnt_devblocksize
);
1031 /* default value for volume f_attributes */
1032 if (VFSATTR_IS_ACTIVE(&vs
, f_attributes
)
1033 && !VFSATTR_IS_SUPPORTED(&vs
, f_attributes
)) {
1034 vol_attributes_attr_t
*attrp
= &vs
.f_attributes
;
1036 attrp
->validattr
.commonattr
= VFS_DFLT_ATTR_CMN
;
1037 attrp
->validattr
.volattr
= VFS_DFLT_ATTR_VOL
;
1038 attrp
->validattr
.dirattr
= VFS_DFLT_ATTR_DIR
;
1039 attrp
->validattr
.fileattr
= VFS_DFLT_ATTR_FILE
;
1040 attrp
->validattr
.forkattr
= VFS_DFLT_ATTR_CMN_EXT
;
1042 attrp
->nativeattr
.commonattr
= 0;
1043 attrp
->nativeattr
.volattr
= 0;
1044 attrp
->nativeattr
.dirattr
= 0;
1045 attrp
->nativeattr
.fileattr
= 0;
1046 attrp
->nativeattr
.forkattr
= 0;
1047 VFSATTR_SET_SUPPORTED(&vs
, f_attributes
);
1050 /* default value for volume f_capabilities */
1051 if (VFSATTR_IS_ACTIVE(&vs
, f_capabilities
)) {
1052 /* getattrlist is always supported now. */
1053 if (!VFSATTR_IS_SUPPORTED(&vs
, f_capabilities
)) {
1054 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] = 0;
1055 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] = VOL_CAP_INT_ATTRLIST
;
1056 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
1057 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
1059 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] = 0;
1060 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] = VOL_CAP_INT_ATTRLIST
;
1061 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
1062 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
1063 VFSATTR_SET_SUPPORTED(&vs
, f_capabilities
);
1065 /* OR in VOL_CAP_INT_ATTRLIST if f_capabilities is supported */
1066 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] |= VOL_CAP_INT_ATTRLIST
;
1067 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] |= VOL_CAP_INT_ATTRLIST
;
1071 /* check to see if our fixups were enough */
1072 if (!VFSATTR_ALL_SUPPORTED(&vs
)) {
1075 /* Fix up valid mask for post processing */
1076 getvolattrlist_fixupattrs(&ab
.valid
, &vs
);
1078 /* Force packing of everything asked for */
1079 vs
.f_supported
= vs
.f_active
;
1081 /* Adjust the requested attributes */
1082 getvolattrlist_fixupattrs((attribute_set_t
*)&alp
->commonattr
, &vs
);
1093 * Some fields require data from the root vp
1095 if (alp
->commonattr
& (ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
| ATTR_CMN_ACCESSMASK
| ATTR_CMN_FLAGS
| ATTR_CMN_SCRIPT
)) {
1096 VATTR_WANTED(&va
, va_uid
);
1097 VATTR_WANTED(&va
, va_gid
);
1098 VATTR_WANTED(&va
, va_mode
);
1099 VATTR_WANTED(&va
, va_flags
);
1100 VATTR_WANTED(&va
, va_encoding
);
1102 if ((error
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
1103 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not fetch attributes from root vnode", vp
);
1107 error
= mac_vnode_check_getattr(ctx
, NOCRED
, vp
, &va
);
1109 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: MAC framework returned %d for root vnode", error
);
1113 if (VATTR_IS_ACTIVE(&va
, va_encoding
) &&
1114 !VATTR_IS_SUPPORTED(&va
, va_encoding
)) {
1115 if (!return_valid
|| pack_invalid
) {
1116 /* use kTextEncodingMacUnicode */
1117 VATTR_RETURN(&va
, va_encoding
, 0x7e);
1119 /* don't use a default */
1120 alp
->commonattr
&= ~ATTR_CMN_SCRIPT
;
1126 * Compute variable-size buffer requirements.
1129 if (alp
->commonattr
& ATTR_CMN_NAME
) {
1130 if (vp
->v_mount
->mnt_vfsstat
.f_mntonname
[1] == 0x00 &&
1131 vp
->v_mount
->mnt_vfsstat
.f_mntonname
[0] == '/') {
1132 /* special case for boot volume. Use root name when it's
1133 * available (which is the volume name) or just the mount on
1134 * name of "/". we must do this for binary compatibility with
1135 * pre Tiger code. returning nothing for the boot volume name
1136 * breaks installers - 3961058
1138 cnp
= vnode_getname(vp
);
1140 /* just use "/" as name */
1141 cnp
= &vp
->v_mount
->mnt_vfsstat
.f_mntonname
[0];
1147 getattrlist_findnamecomp(vp
->v_mount
->mnt_vfsstat
.f_mntonname
, &cnp
, &cnl
);
1149 if (alp
->commonattr
& ATTR_CMN_NAME
) {
1150 varsize
+= roundup(cnl
+ 1, 4);
1153 if (alp
->volattr
& ATTR_VOL_MOUNTPOINT
) {
1154 varsize
+= roundup(strlen(mnt
->mnt_vfsstat
.f_mntonname
) + 1, 4);
1156 if (alp
->volattr
& ATTR_VOL_NAME
) {
1157 vs
.f_vol_name
[MAXPATHLEN
- 1] = '\0'; /* Ensure nul-termination */
1158 varsize
+= roundup(strlen(vs
.f_vol_name
) + 1, 4);
1160 if (alp
->volattr
& ATTR_VOL_MOUNTEDDEVICE
) {
1161 varsize
+= roundup(strlen(mnt
->mnt_vfsstat
.f_mntfromname
) + 1, 4);
1165 * Allocate a target buffer for attribute results.
1166 * Note that since we won't ever copy out more than the caller requested,
1167 * we never need to allocate more than they offer.
1169 ab
.allocated
= fixedsize
+ varsize
;
1170 if (((size_t)ab
.allocated
) > ATTR_MAX_BUFFER
) {
1172 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: buffer size too large (%d limit %d)", ab
.allocated
, ATTR_MAX_BUFFER
);
1177 (ab
.allocated
< (ssize_t
)(sizeof(uint32_t) + sizeof(attribute_set_t
))) &&
1178 !(options
& FSOPT_REPORT_FULLSIZE
)) {
1179 uint32_t num_bytes_valid
= sizeof(uint32_t);
1181 * Not enough to return anything and we don't have to report
1182 * how much space is needed. Get out now.
1183 * N.B. - We have only been called after having verified that
1184 * attributeBuffer is at least sizeof(uint32_t);
1186 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
1187 error
= copyout(&num_bytes_valid
,
1188 CAST_USER_ADDR_T(attributeBuffer
), num_bytes_valid
);
1190 bcopy(&num_bytes_valid
, (void *)attributeBuffer
,
1191 (size_t)num_bytes_valid
);
1196 MALLOC(ab
.base
, char *, ab
.allocated
, M_TEMP
, M_ZERO
| M_WAITOK
);
1197 if (ab
.base
== NULL
) {
1199 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not allocate %d for copy buffer", ab
.allocated
);
1204 * Pack results into the destination buffer.
1206 ab
.fixedcursor
= ab
.base
+ sizeof(uint32_t);
1208 ab
.fixedcursor
+= sizeof(attribute_set_t
);
1209 bzero(&ab
.actual
, sizeof(ab
.actual
));
1211 ab
.varcursor
= ab
.base
+ fixedsize
;
1212 ab
.needed
= fixedsize
+ varsize
;
1214 /* common attributes **************************************************/
1215 if (alp
->commonattr
& ATTR_CMN_ERROR
) {
1217 ab
.actual
.commonattr
|= ATTR_CMN_ERROR
;
1219 if (alp
->commonattr
& ATTR_CMN_NAME
) {
1220 attrlist_pack_string(&ab
, cnp
, cnl
);
1221 ab
.actual
.commonattr
|= ATTR_CMN_NAME
;
1223 if (alp
->commonattr
& ATTR_CMN_DEVID
) {
1224 ATTR_PACK4(ab
, mnt
->mnt_vfsstat
.f_fsid
.val
[0]);
1225 ab
.actual
.commonattr
|= ATTR_CMN_DEVID
;
1227 if (alp
->commonattr
& ATTR_CMN_FSID
) {
1228 ATTR_PACK8(ab
, mnt
->mnt_vfsstat
.f_fsid
);
1229 ab
.actual
.commonattr
|= ATTR_CMN_FSID
;
1231 if (alp
->commonattr
& ATTR_CMN_OBJTYPE
) {
1232 if (!return_valid
|| pack_invalid
) {
1236 if (alp
->commonattr
& ATTR_CMN_OBJTAG
) {
1237 ATTR_PACK4(ab
, vp
->v_tag
);
1238 ab
.actual
.commonattr
|= ATTR_CMN_OBJTAG
;
1240 if (alp
->commonattr
& ATTR_CMN_OBJID
) {
1241 if (!return_valid
|| pack_invalid
) {
1242 fsobj_id_t f
= {0, 0};
1246 if (alp
->commonattr
& ATTR_CMN_OBJPERMANENTID
) {
1247 if (!return_valid
|| pack_invalid
) {
1248 fsobj_id_t f
= {0, 0};
1252 if (alp
->commonattr
& ATTR_CMN_PAROBJID
) {
1253 if (!return_valid
|| pack_invalid
) {
1254 fsobj_id_t f
= {0, 0};
1258 /* note that this returns the encoding for the volume name, not the node name */
1259 if (alp
->commonattr
& ATTR_CMN_SCRIPT
) {
1260 ATTR_PACK4(ab
, va
.va_encoding
);
1261 ab
.actual
.commonattr
|= ATTR_CMN_SCRIPT
;
1263 if (alp
->commonattr
& ATTR_CMN_CRTIME
) {
1264 ATTR_PACK_TIME(ab
, vs
.f_create_time
, is_64bit
);
1265 ab
.actual
.commonattr
|= ATTR_CMN_CRTIME
;
1267 if (alp
->commonattr
& ATTR_CMN_MODTIME
) {
1268 ATTR_PACK_TIME(ab
, vs
.f_modify_time
, is_64bit
);
1269 ab
.actual
.commonattr
|= ATTR_CMN_MODTIME
;
1271 if (alp
->commonattr
& ATTR_CMN_CHGTIME
) {
1272 if (!return_valid
|| pack_invalid
) {
1273 ATTR_PACK_TIME(ab
, vs
.f_modify_time
, is_64bit
);
1276 if (alp
->commonattr
& ATTR_CMN_ACCTIME
) {
1277 ATTR_PACK_TIME(ab
, vs
.f_access_time
, is_64bit
);
1278 ab
.actual
.commonattr
|= ATTR_CMN_ACCTIME
;
1280 if (alp
->commonattr
& ATTR_CMN_BKUPTIME
) {
1281 ATTR_PACK_TIME(ab
, vs
.f_backup_time
, is_64bit
);
1282 ab
.actual
.commonattr
|= ATTR_CMN_BKUPTIME
;
1284 if (alp
->commonattr
& ATTR_CMN_FNDRINFO
) {
1287 * This attribute isn't really Finder Info, at least for HFS.
1289 if (vp
->v_tag
== VT_HFS
) {
1290 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
1291 error
= VNOP_IOCTL(vp
, HFS_GET_BOOT_INFO
, (caddr_t
)&f
, 0, ctx
);
1293 attrlist_pack_fixed(&ab
, f
, sizeof(f
));
1294 ab
.actual
.commonattr
|= ATTR_CMN_FNDRINFO
;
1295 } else if (!return_valid
) {
1298 } else if (!return_valid
|| pack_invalid
) {
1299 /* XXX we could at least pass out the volume UUID here */
1300 bzero(&f
, sizeof(f
));
1301 attrlist_pack_fixed(&ab
, f
, sizeof(f
));
1304 if (alp
->commonattr
& ATTR_CMN_OWNERID
) {
1305 ATTR_PACK4(ab
, va
.va_uid
);
1306 ab
.actual
.commonattr
|= ATTR_CMN_OWNERID
;
1308 if (alp
->commonattr
& ATTR_CMN_GRPID
) {
1309 ATTR_PACK4(ab
, va
.va_gid
);
1310 ab
.actual
.commonattr
|= ATTR_CMN_GRPID
;
1312 if (alp
->commonattr
& ATTR_CMN_ACCESSMASK
) {
1313 ATTR_PACK_CAST(&ab
, uint32_t, va
.va_mode
);
1314 ab
.actual
.commonattr
|= ATTR_CMN_ACCESSMASK
;
1316 if (alp
->commonattr
& ATTR_CMN_FLAGS
) {
1317 ATTR_PACK4(ab
, va
.va_flags
);
1318 ab
.actual
.commonattr
|= ATTR_CMN_FLAGS
;
1320 if (alp
->commonattr
& ATTR_CMN_USERACCESS
) { /* XXX this is expensive and also duplicate work */
1322 if (vnode_isdir(vp
)) {
1323 if (vnode_authorize(vp
, NULL
,
1324 KAUTH_VNODE_ACCESS
| KAUTH_VNODE_ADD_FILE
| KAUTH_VNODE_ADD_SUBDIRECTORY
| KAUTH_VNODE_DELETE_CHILD
, ctx
) == 0) {
1327 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_LIST_DIRECTORY
, ctx
) == 0) {
1330 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_SEARCH
, ctx
) == 0) {
1334 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
, ctx
) == 0) {
1337 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_READ_DATA
, ctx
) == 0) {
1340 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_EXECUTE
, ctx
) == 0) {
1346 * Rather than MAC preceding DAC, in this case we want
1347 * the smallest set of permissions granted by both MAC & DAC
1348 * checks. We won't add back any permissions.
1351 if (mac_vnode_check_access(ctx
, vp
, W_OK
) != 0) {
1356 if (mac_vnode_check_access(ctx
, vp
, R_OK
) != 0) {
1361 if (mac_vnode_check_access(ctx
, vp
, X_OK
) != 0) {
1366 KAUTH_DEBUG("ATTRLIST - returning user access %x", perms
);
1367 ATTR_PACK4(ab
, perms
);
1368 ab
.actual
.commonattr
|= ATTR_CMN_USERACCESS
;
1371 * The following common volume attributes are only
1372 * packed when the pack_invalid mode is enabled.
1377 if (alp
->commonattr
& ATTR_CMN_EXTENDED_SECURITY
) {
1378 attrlist_pack_variable(&ab
, NULL
, 0);
1380 if (alp
->commonattr
& ATTR_CMN_UUID
) {
1381 ATTR_PACK(&ab
, kauth_null_guid
);
1383 if (alp
->commonattr
& ATTR_CMN_GRPUUID
) {
1384 ATTR_PACK(&ab
, kauth_null_guid
);
1386 if (alp
->commonattr
& ATTR_CMN_FILEID
) {
1387 ATTR_PACK8(ab
, fid
);
1389 if (alp
->commonattr
& ATTR_CMN_PARENTID
) {
1390 ATTR_PACK8(ab
, fid
);
1394 /* volume attributes **************************************************/
1396 if (alp
->volattr
& ATTR_VOL_FSTYPE
) {
1397 ATTR_PACK_CAST(&ab
, uint32_t, vfs_typenum(mnt
));
1398 ab
.actual
.volattr
|= ATTR_VOL_FSTYPE
;
1400 if (alp
->volattr
& ATTR_VOL_SIGNATURE
) {
1401 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_signature
);
1402 ab
.actual
.volattr
|= ATTR_VOL_SIGNATURE
;
1404 if (alp
->volattr
& ATTR_VOL_SIZE
) {
1405 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
* vs
.f_blocks
);
1406 ab
.actual
.volattr
|= ATTR_VOL_SIZE
;
1408 if (alp
->volattr
& ATTR_VOL_SPACEFREE
) {
1409 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
* vs
.f_bfree
);
1410 ab
.actual
.volattr
|= ATTR_VOL_SPACEFREE
;
1412 if (alp
->volattr
& ATTR_VOL_SPACEAVAIL
) {
1413 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
* vs
.f_bavail
);
1414 ab
.actual
.volattr
|= ATTR_VOL_SPACEAVAIL
;
1416 if (alp
->volattr
& ATTR_VOL_MINALLOCATION
) {
1417 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
);
1418 ab
.actual
.volattr
|= ATTR_VOL_MINALLOCATION
;
1420 if (alp
->volattr
& ATTR_VOL_ALLOCATIONCLUMP
) {
1421 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
); /* not strictly true */
1422 ab
.actual
.volattr
|= ATTR_VOL_ALLOCATIONCLUMP
;
1424 if (alp
->volattr
& ATTR_VOL_IOBLOCKSIZE
) {
1425 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_iosize
);
1426 ab
.actual
.volattr
|= ATTR_VOL_IOBLOCKSIZE
;
1428 if (alp
->volattr
& ATTR_VOL_OBJCOUNT
) {
1429 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_objcount
);
1430 ab
.actual
.volattr
|= ATTR_VOL_OBJCOUNT
;
1432 if (alp
->volattr
& ATTR_VOL_FILECOUNT
) {
1433 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_filecount
);
1434 ab
.actual
.volattr
|= ATTR_VOL_FILECOUNT
;
1436 if (alp
->volattr
& ATTR_VOL_DIRCOUNT
) {
1437 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_dircount
);
1438 ab
.actual
.volattr
|= ATTR_VOL_DIRCOUNT
;
1440 if (alp
->volattr
& ATTR_VOL_MAXOBJCOUNT
) {
1441 ATTR_PACK_CAST(&ab
, uint32_t, vs
.f_maxobjcount
);
1442 ab
.actual
.volattr
|= ATTR_VOL_MAXOBJCOUNT
;
1444 if (alp
->volattr
& ATTR_VOL_MOUNTPOINT
) {
1445 attrlist_pack_string(&ab
, mnt
->mnt_vfsstat
.f_mntonname
, 0);
1446 ab
.actual
.volattr
|= ATTR_VOL_MOUNTPOINT
;
1448 if (alp
->volattr
& ATTR_VOL_NAME
) {
1449 attrlist_pack_string(&ab
, vs
.f_vol_name
, 0);
1450 ab
.actual
.volattr
|= ATTR_VOL_NAME
;
1452 if (alp
->volattr
& ATTR_VOL_MOUNTFLAGS
) {
1453 ATTR_PACK_CAST(&ab
, uint32_t, mnt
->mnt_flag
);
1454 ab
.actual
.volattr
|= ATTR_VOL_MOUNTFLAGS
;
1456 if (alp
->volattr
& ATTR_VOL_MOUNTEDDEVICE
) {
1457 attrlist_pack_string(&ab
, mnt
->mnt_vfsstat
.f_mntfromname
, 0);
1458 ab
.actual
.volattr
|= ATTR_VOL_MOUNTEDDEVICE
;
1460 if (alp
->volattr
& ATTR_VOL_ENCODINGSUSED
) {
1461 if (!return_valid
|| pack_invalid
) {
1462 ATTR_PACK_CAST(&ab
, uint64_t, ~0LL); /* return all encodings */
1465 if (alp
->volattr
& ATTR_VOL_CAPABILITIES
) {
1466 /* fix up volume capabilities */
1467 if (vfs_extendedsecurity(mnt
)) {
1468 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] |= VOL_CAP_INT_EXTENDED_SECURITY
;
1470 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] &= ~VOL_CAP_INT_EXTENDED_SECURITY
;
1472 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] |= VOL_CAP_INT_EXTENDED_SECURITY
;
1475 * if the filesystem doesn't mark either VOL_CAP_FMT_NO_IMMUTABLE_FILES
1476 * or VOL_CAP_FMT_NO_PERMISSIONS as valid, assume they're not supported
1478 if (!(vs
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_NO_IMMUTABLE_FILES
)) {
1479 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] &= ~VOL_CAP_FMT_NO_IMMUTABLE_FILES
;
1480 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] |= VOL_CAP_FMT_NO_IMMUTABLE_FILES
;
1483 if (!(vs
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_NO_PERMISSIONS
)) {
1484 vs
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] &= ~VOL_CAP_FMT_NO_PERMISSIONS
;
1485 vs
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] |= VOL_CAP_FMT_NO_PERMISSIONS
;
1488 ATTR_PACK(&ab
, vs
.f_capabilities
);
1489 ab
.actual
.volattr
|= ATTR_VOL_CAPABILITIES
;
1491 if (alp
->volattr
& ATTR_VOL_UUID
) {
1492 ATTR_PACK(&ab
, vs
.f_uuid
);
1493 ab
.actual
.volattr
|= ATTR_VOL_UUID
;
1495 if (alp
->volattr
& ATTR_VOL_QUOTA_SIZE
) {
1496 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
* vs
.f_quota
);
1497 ab
.actual
.volattr
|= ATTR_VOL_QUOTA_SIZE
;
1499 if (alp
->volattr
& ATTR_VOL_RESERVED_SIZE
) {
1500 ATTR_PACK_CAST(&ab
, off_t
, vs
.f_bsize
* vs
.f_reserved
);
1501 ab
.actual
.volattr
|= ATTR_VOL_RESERVED_SIZE
;
1503 if (alp
->volattr
& ATTR_VOL_ATTRIBUTES
) {
1504 /* fix up volume attribute information */
1506 vs
.f_attributes
.validattr
.commonattr
|= VFS_DFLT_ATTR_CMN
;
1507 vs
.f_attributes
.validattr
.volattr
|= VFS_DFLT_ATTR_VOL
;
1508 vs
.f_attributes
.validattr
.dirattr
|= VFS_DFLT_ATTR_DIR
;
1509 vs
.f_attributes
.validattr
.fileattr
|= VFS_DFLT_ATTR_FILE
;
1511 if (vfs_extendedsecurity(mnt
)) {
1512 vs
.f_attributes
.validattr
.commonattr
|= (ATTR_CMN_EXTENDED_SECURITY
| ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
);
1514 vs
.f_attributes
.validattr
.commonattr
&= ~(ATTR_CMN_EXTENDED_SECURITY
| ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
);
1515 vs
.f_attributes
.nativeattr
.commonattr
&= ~(ATTR_CMN_EXTENDED_SECURITY
| ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
);
1517 ATTR_PACK(&ab
, vs
.f_attributes
);
1518 ab
.actual
.volattr
|= ATTR_VOL_ATTRIBUTES
;
1522 if (!return_valid
&& (ab
.fixedcursor
- ab
.base
) != fixedsize
) {
1523 panic("packed field size mismatch; allocated %ld but packed %ld for common %08x vol %08x",
1524 fixedsize
, (long) (ab
.fixedcursor
- ab
.base
), alp
->commonattr
, alp
->volattr
);
1526 if (!return_valid
&& ab
.varcursor
!= (ab
.base
+ ab
.needed
)) {
1527 panic("packed variable field size mismatch; used %ld but expected %ld", (long) (ab
.varcursor
- ab
.base
), ab
.needed
);
1531 * In the compatible case, we report the smaller of the required and returned sizes.
1532 * If the FSOPT_REPORT_FULLSIZE option is supplied, we report the full (required) size
1533 * of the result buffer, even if we copied less out. The caller knows how big a buffer
1534 * they gave us, so they can always check for truncation themselves.
1536 *(uint32_t *)ab
.base
= (options
& FSOPT_REPORT_FULLSIZE
) ? ab
.needed
: imin(bufferSize
, ab
.needed
);
1538 /* Return attribute set output if requested. */
1540 (ab
.allocated
>= (ssize_t
)(sizeof(uint32_t) + sizeof(ab
.actual
)))) {
1541 ab
.actual
.commonattr
|= ATTR_CMN_RETURNED_ATTRS
;
1543 /* Only report the attributes that are valid */
1544 ab
.actual
.commonattr
&= ab
.valid
.commonattr
;
1545 ab
.actual
.volattr
&= ab
.valid
.volattr
;
1547 bcopy(&ab
.actual
, ab
.base
+ sizeof(uint32_t), sizeof(ab
.actual
));
1550 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
1551 error
= copyout(ab
.base
, CAST_USER_ADDR_T(attributeBuffer
),
1552 ulmin(bufferSize
, ab
.needed
));
1554 bcopy(ab
.base
, (void *)attributeBuffer
, (size_t)ulmin(bufferSize
, ab
.needed
));
1558 if (vs
.f_vol_name
!= NULL
) {
1559 kfree(vs
.f_vol_name
, MAXPATHLEN
);
1564 if (ab
.base
!= NULL
) {
1565 FREE(ab
.base
, M_TEMP
);
1567 VFS_DEBUG(ctx
, vp
, "ATTRLIST - returning %d", error
);
1569 if (root_vp
!= NULL
) {
1576 * Pack ATTR_COMMON attributes into a user buffer.
1577 * alp is a pointer to the bitmap of attributes required.
1578 * abp is the state of the attribute filling operation.
1579 * The attribute data (along with some other fields that are required
1583 attr_pack_common(vfs_context_t ctx
, mount_t mp
, vnode_t vp
, struct attrlist
*alp
,
1584 struct _attrlist_buf
*abp
, struct vnode_attr
*vap
, int proc_is64
,
1585 const char *cnp
, ssize_t cnl
, const char *fullpathptr
,
1586 ssize_t fullpathlen
, int return_valid
, int pack_invalid
, int vtype
,
1592 if ((alp
->commonattr
& ATTR_CMN_ERROR
) &&
1593 (!return_valid
|| pack_invalid
)) {
1594 ATTR_PACK4((*abp
), 0);
1595 abp
->actual
.commonattr
|= ATTR_CMN_ERROR
;
1597 if (alp
->commonattr
& ATTR_CMN_NAME
) {
1598 attrlist_pack_string(abp
, cnp
, cnl
);
1599 abp
->actual
.commonattr
|= ATTR_CMN_NAME
;
1601 if (alp
->commonattr
& ATTR_CMN_DEVID
) {
1602 if (mp
) { /* caller needs real devid */
1604 mp
->mnt_vfsstat
.f_fsid
.val
[0]);
1605 abp
->actual
.commonattr
|= ATTR_CMN_DEVID
;
1606 } else if (VATTR_IS_ACTIVE(vap
, va_fsid
) && VATTR_IS_SUPPORTED(vap
, va_fsid
)) {
1607 ATTR_PACK4((*abp
), vap
->va_fsid
);
1608 abp
->actual
.commonattr
|= ATTR_CMN_DEVID
;
1611 vp
->v_mount
->mnt_vfsstat
.f_fsid
.val
[0]);
1612 abp
->actual
.commonattr
|= ATTR_CMN_DEVID
;
1613 } else if (VATTR_IS_SUPPORTED(vap
, va_devid
)) {
1614 ATTR_PACK4((*abp
), vap
->va_devid
);
1615 abp
->actual
.commonattr
|= ATTR_CMN_DEVID
;
1616 } else if (!return_valid
|| pack_invalid
) {
1617 ATTR_PACK4((*abp
), 0);
1620 if (alp
->commonattr
& ATTR_CMN_FSID
) {
1621 if (mp
) { /* caller needs real fsid */
1623 mp
->mnt_vfsstat
.f_fsid
);
1624 abp
->actual
.commonattr
|= ATTR_CMN_FSID
;
1625 } else if (VATTR_IS_SUPPORTED(vap
, va_fsid64
)) {
1626 ATTR_PACK8((*abp
), vap
->va_fsid64
);
1627 abp
->actual
.commonattr
|= ATTR_CMN_FSID
;
1630 vp
->v_mount
->mnt_vfsstat
.f_fsid
);
1631 abp
->actual
.commonattr
|= ATTR_CMN_FSID
;
1632 } else if (!return_valid
|| pack_invalid
) {
1633 fsid_t fsid
= {{0}};
1634 ATTR_PACK8((*abp
), fsid
);
1637 if (alp
->commonattr
& ATTR_CMN_OBJTYPE
) {
1639 ATTR_PACK4((*abp
), vtype
);
1640 abp
->actual
.commonattr
|= ATTR_CMN_OBJTYPE
;
1641 } else if (VATTR_IS_SUPPORTED(vap
, va_objtype
)) {
1642 ATTR_PACK4((*abp
), vap
->va_objtype
);
1643 abp
->actual
.commonattr
|= ATTR_CMN_OBJTYPE
;
1644 } else if (!return_valid
|| pack_invalid
) {
1645 ATTR_PACK4((*abp
), 0);
1648 if (alp
->commonattr
& ATTR_CMN_OBJTAG
) {
1650 ATTR_PACK4((*abp
), vp
->v_tag
);
1651 abp
->actual
.commonattr
|= ATTR_CMN_OBJTAG
;
1652 } else if (VATTR_IS_SUPPORTED(vap
, va_objtag
)) {
1653 ATTR_PACK4((*abp
), vap
->va_objtag
);
1654 abp
->actual
.commonattr
|= ATTR_CMN_OBJTAG
;
1655 } else if (!return_valid
|| pack_invalid
) {
1656 ATTR_PACK4((*abp
), 0);
1659 if (alp
->commonattr
& ATTR_CMN_OBJID
) {
1661 * Carbon can't deal with us reporting the target ID
1662 * for links. So we ask the filesystem to give us the
1663 * source ID as well, and if it gives us one, we use
1666 if (vap
->va_vaflags
& VA_64BITOBJIDS
) {
1667 if (VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
1668 ATTR_PACK8((*abp
), vap
->va_linkid
);
1670 ATTR_PACK8((*abp
), vap
->va_fileid
);
1674 if (VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
1675 f
.fid_objno
= (uint32_t)vap
->va_linkid
;
1677 f
.fid_objno
= (uint32_t)vap
->va_fileid
;
1679 f
.fid_generation
= 0;
1680 ATTR_PACK8((*abp
), f
);
1682 abp
->actual
.commonattr
|= ATTR_CMN_OBJID
;
1684 if (alp
->commonattr
& ATTR_CMN_OBJPERMANENTID
) {
1686 * Carbon can't deal with us reporting the target ID
1687 * for links. So we ask the filesystem to give us the
1688 * source ID as well, and if it gives us one, we use
1691 if (vap
->va_vaflags
& VA_64BITOBJIDS
) {
1692 if (VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
1693 ATTR_PACK8((*abp
), vap
->va_linkid
);
1695 ATTR_PACK8((*abp
), vap
->va_fileid
);
1699 if (VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
1700 f
.fid_objno
= (uint32_t)vap
->va_linkid
;
1702 f
.fid_objno
= (uint32_t)vap
->va_fileid
;
1704 f
.fid_generation
= 0;
1705 ATTR_PACK8((*abp
), f
);
1707 abp
->actual
.commonattr
|= ATTR_CMN_OBJPERMANENTID
;
1709 if (alp
->commonattr
& ATTR_CMN_PAROBJID
) {
1710 if (vap
->va_vaflags
& VA_64BITOBJIDS
) {
1711 ATTR_PACK8((*abp
), vap
->va_parentid
);
1714 f
.fid_objno
= (uint32_t)vap
->va_parentid
;
1715 f
.fid_generation
= 0;
1716 ATTR_PACK8((*abp
), f
);
1718 abp
->actual
.commonattr
|= ATTR_CMN_PAROBJID
;
1720 if (alp
->commonattr
& ATTR_CMN_SCRIPT
) {
1721 if (VATTR_IS_SUPPORTED(vap
, va_encoding
)) {
1722 ATTR_PACK4((*abp
), vap
->va_encoding
);
1723 abp
->actual
.commonattr
|= ATTR_CMN_SCRIPT
;
1724 } else if (!return_valid
|| pack_invalid
) {
1725 ATTR_PACK4((*abp
), 0x7e);
1728 if (alp
->commonattr
& ATTR_CMN_CRTIME
) {
1729 ATTR_PACK_TIME((*abp
), vap
->va_create_time
, proc_is64
);
1730 abp
->actual
.commonattr
|= ATTR_CMN_CRTIME
;
1732 if (alp
->commonattr
& ATTR_CMN_MODTIME
) {
1733 ATTR_PACK_TIME((*abp
), vap
->va_modify_time
, proc_is64
);
1734 abp
->actual
.commonattr
|= ATTR_CMN_MODTIME
;
1736 if (alp
->commonattr
& ATTR_CMN_CHGTIME
) {
1737 ATTR_PACK_TIME((*abp
), vap
->va_change_time
, proc_is64
);
1738 abp
->actual
.commonattr
|= ATTR_CMN_CHGTIME
;
1740 if (alp
->commonattr
& ATTR_CMN_ACCTIME
) {
1741 ATTR_PACK_TIME((*abp
), vap
->va_access_time
, proc_is64
);
1742 abp
->actual
.commonattr
|= ATTR_CMN_ACCTIME
;
1744 if (alp
->commonattr
& ATTR_CMN_BKUPTIME
) {
1745 ATTR_PACK_TIME((*abp
), vap
->va_backup_time
, proc_is64
);
1746 abp
->actual
.commonattr
|= ATTR_CMN_BKUPTIME
;
1749 * They are requesting user access, we should obtain this before getting
1750 * the finder info. For some network file systems this is a performance
1753 if (alp
->commonattr
& ATTR_CMN_USERACCESS
) { /* this is expensive */
1754 if (vp
&& !is_bulk
) {
1755 if (vtype
== VDIR
) {
1756 if (vnode_authorize(vp
, NULL
,
1757 KAUTH_VNODE_ACCESS
| KAUTH_VNODE_ADD_FILE
|
1758 KAUTH_VNODE_ADD_SUBDIRECTORY
|
1759 KAUTH_VNODE_DELETE_CHILD
, ctx
) == 0) {
1763 if (vnode_authorize(vp
, NULL
,
1764 KAUTH_VNODE_ACCESS
|
1765 KAUTH_VNODE_LIST_DIRECTORY
, ctx
) == 0) {
1769 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
|
1770 KAUTH_VNODE_SEARCH
, ctx
) == 0) {
1774 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
|
1775 KAUTH_VNODE_WRITE_DATA
, ctx
) == 0) {
1779 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_READ_DATA
, ctx
) == 0) {
1782 if (vnode_authorize(vp
, NULL
, KAUTH_VNODE_ACCESS
| KAUTH_VNODE_EXECUTE
, ctx
) == 0) {
1786 } else if (is_bulk
&&
1787 VATTR_IS_SUPPORTED(vap
, va_user_access
)) {
1788 perms
= vap
->va_user_access
;
1791 if (alp
->commonattr
& ATTR_CMN_FNDRINFO
) {
1795 if (vp
&& !is_bulk
) {
1797 char uio_buf
[UIO_SIZEOF(1)];
1799 if ((auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
,
1800 UIO_READ
, uio_buf
, sizeof(uio_buf
))) == NULL
) {
1804 uio_addiov(auio
, CAST_USER_ADDR_T(abp
->fixedcursor
),
1806 /* fisize may be reset to 0 after this call */
1807 error
= vn_getxattr(vp
, XATTR_FINDERINFO_NAME
, auio
,
1808 &fisize
, XATTR_NOSECURITY
, ctx
);
1812 * Default to zeros if its not available,
1813 * unless ATTR_CMN_RETURNED_ATTRS was requested.
1816 (!return_valid
|| pack_invalid
) &&
1817 ((error
== ENOATTR
) || (error
== ENOENT
) ||
1818 (error
== ENOTSUP
) || (error
== EPERM
))) {
1819 VFS_DEBUG(ctx
, vp
, "ATTRLIST - No system.finderinfo attribute, returning zeroes");
1820 bzero(abp
->fixedcursor
, 32);
1825 abp
->fixedcursor
+= 32;
1826 abp
->actual
.commonattr
|= ATTR_CMN_FNDRINFO
;
1827 } else if (!return_valid
) {
1831 * If we can inform the caller that we can't
1832 * return this attribute, reset error and
1833 * continue with the rest of the attributes.
1837 } else if (VATTR_IS_SUPPORTED(vap
, va_finderinfo
)) {
1838 bcopy(&vap
->va_finderinfo
[0], abp
->fixedcursor
, fisize
);
1839 abp
->fixedcursor
+= fisize
;
1840 abp
->actual
.commonattr
|= ATTR_CMN_FNDRINFO
;
1841 } else if (!return_valid
|| pack_invalid
) {
1842 bzero(abp
->fixedcursor
, fisize
);
1843 abp
->fixedcursor
+= fisize
;
1846 if (alp
->commonattr
& ATTR_CMN_OWNERID
) {
1847 ATTR_PACK4((*abp
), vap
->va_uid
);
1848 abp
->actual
.commonattr
|= ATTR_CMN_OWNERID
;
1850 if (alp
->commonattr
& ATTR_CMN_GRPID
) {
1851 ATTR_PACK4((*abp
), vap
->va_gid
);
1852 abp
->actual
.commonattr
|= ATTR_CMN_GRPID
;
1854 if (alp
->commonattr
& ATTR_CMN_ACCESSMASK
) {
1855 ATTR_PACK4((*abp
), vap
->va_mode
);
1856 abp
->actual
.commonattr
|= ATTR_CMN_ACCESSMASK
;
1858 if (alp
->commonattr
& ATTR_CMN_FLAGS
) {
1859 ATTR_PACK4((*abp
), vap
->va_flags
);
1860 abp
->actual
.commonattr
|= ATTR_CMN_FLAGS
;
1862 if (alp
->commonattr
& ATTR_CMN_GEN_COUNT
) {
1863 if (VATTR_IS_SUPPORTED(vap
, va_write_gencount
)) {
1864 ATTR_PACK4((*abp
), vap
->va_write_gencount
);
1865 abp
->actual
.commonattr
|= ATTR_CMN_GEN_COUNT
;
1866 } else if (!return_valid
|| pack_invalid
) {
1867 ATTR_PACK4((*abp
), 0);
1871 if (alp
->commonattr
& ATTR_CMN_DOCUMENT_ID
) {
1872 if (VATTR_IS_SUPPORTED(vap
, va_document_id
)) {
1873 ATTR_PACK4((*abp
), vap
->va_document_id
);
1874 abp
->actual
.commonattr
|= ATTR_CMN_DOCUMENT_ID
;
1875 } else if (!return_valid
|| pack_invalid
) {
1876 ATTR_PACK4((*abp
), 0);
1879 /* We already obtain the user access, so just fill in the buffer here */
1880 if (alp
->commonattr
& ATTR_CMN_USERACCESS
) {
1882 if (!is_bulk
&& vp
) {
1884 * Rather than MAC preceding DAC, in this case we want
1885 * the smallest set of permissions granted by both MAC &
1886 * DAC checks. We won't add back any permissions.
1889 if (mac_vnode_check_access(ctx
, vp
, W_OK
) != 0) {
1894 if (mac_vnode_check_access(ctx
, vp
, R_OK
) != 0) {
1899 if (mac_vnode_check_access(ctx
, vp
, X_OK
) != 0) {
1905 VFS_DEBUG(ctx
, vp
, "ATTRLIST - granting perms %d", perms
);
1906 if (!is_bulk
&& vp
) {
1907 ATTR_PACK4((*abp
), perms
);
1908 abp
->actual
.commonattr
|= ATTR_CMN_USERACCESS
;
1909 } else if (is_bulk
&& VATTR_IS_SUPPORTED(vap
, va_user_access
)) {
1910 ATTR_PACK4((*abp
), perms
);
1911 abp
->actual
.commonattr
|= ATTR_CMN_USERACCESS
;
1912 } else if (!return_valid
|| pack_invalid
) {
1913 ATTR_PACK4((*abp
), 0);
1916 if (alp
->commonattr
& ATTR_CMN_EXTENDED_SECURITY
) {
1917 if (VATTR_IS_SUPPORTED(vap
, va_acl
) && (vap
->va_acl
!= NULL
)) {
1918 struct kauth_filesec fsec
;
1920 * We want to return a kauth_filesec (for now), but all we have is a kauth_acl.
1922 fsec
.fsec_magic
= KAUTH_FILESEC_MAGIC
;
1923 fsec
.fsec_owner
= kauth_null_guid
;
1924 fsec
.fsec_group
= kauth_null_guid
;
1925 attrlist_pack_variable2(abp
, &fsec
, __offsetof(struct kauth_filesec
, fsec_acl
), vap
->va_acl
, KAUTH_ACL_COPYSIZE(vap
->va_acl
));
1926 abp
->actual
.commonattr
|= ATTR_CMN_EXTENDED_SECURITY
;
1927 } else if (!return_valid
|| pack_invalid
) {
1928 attrlist_pack_variable(abp
, NULL
, 0);
1931 if (alp
->commonattr
& ATTR_CMN_UUID
) {
1932 if (VATTR_IS_SUPPORTED(vap
, va_uuuid
)) {
1933 ATTR_PACK(abp
, vap
->va_uuuid
);
1934 abp
->actual
.commonattr
|= ATTR_CMN_UUID
;
1935 } else if (!return_valid
|| pack_invalid
) {
1936 ATTR_PACK(abp
, kauth_null_guid
);
1939 if (alp
->commonattr
& ATTR_CMN_GRPUUID
) {
1940 if (VATTR_IS_SUPPORTED(vap
, va_guuid
)) {
1941 ATTR_PACK(abp
, vap
->va_guuid
);
1942 abp
->actual
.commonattr
|= ATTR_CMN_GRPUUID
;
1943 } else if (!return_valid
|| pack_invalid
) {
1944 ATTR_PACK(abp
, kauth_null_guid
);
1947 if (alp
->commonattr
& ATTR_CMN_FILEID
) {
1948 ATTR_PACK8((*abp
), vap
->va_fileid
);
1949 abp
->actual
.commonattr
|= ATTR_CMN_FILEID
;
1951 if (alp
->commonattr
& ATTR_CMN_PARENTID
) {
1952 ATTR_PACK8((*abp
), vap
->va_parentid
);
1953 abp
->actual
.commonattr
|= ATTR_CMN_PARENTID
;
1956 if (alp
->commonattr
& ATTR_CMN_FULLPATH
) {
1958 attrlist_pack_string(abp
, fullpathptr
, fullpathlen
);
1959 abp
->actual
.commonattr
|= ATTR_CMN_FULLPATH
;
1963 if (alp
->commonattr
& ATTR_CMN_ADDEDTIME
) {
1964 if (VATTR_IS_SUPPORTED(vap
, va_addedtime
)) {
1965 ATTR_PACK_TIME((*abp
), vap
->va_addedtime
, proc_is64
);
1966 abp
->actual
.commonattr
|= ATTR_CMN_ADDEDTIME
;
1967 } else if (!return_valid
|| pack_invalid
) {
1968 struct timespec zerotime
= {.tv_sec
= 0, .tv_nsec
= 0};
1970 ATTR_PACK_TIME((*abp
), zerotime
, proc_is64
);
1973 if (alp
->commonattr
& ATTR_CMN_DATA_PROTECT_FLAGS
) {
1974 if (VATTR_IS_SUPPORTED(vap
, va_dataprotect_class
)) {
1975 ATTR_PACK4((*abp
), vap
->va_dataprotect_class
);
1976 abp
->actual
.commonattr
|= ATTR_CMN_DATA_PROTECT_FLAGS
;
1977 } else if (!return_valid
|| pack_invalid
) {
1978 ATTR_PACK4((*abp
), 0);
1986 attr_pack_dir(struct vnode
*vp
, struct attrlist
*alp
, struct _attrlist_buf
*abp
,
1987 struct vnode_attr
*vap
, int return_valid
, int pack_invalid
)
1989 if (alp
->dirattr
& ATTR_DIR_LINKCOUNT
) { /* full count of entries */
1990 ATTR_PACK4((*abp
), (uint32_t)vap
->va_dirlinkcount
);
1991 abp
->actual
.dirattr
|= ATTR_DIR_LINKCOUNT
;
1993 if (alp
->dirattr
& ATTR_DIR_ENTRYCOUNT
) {
1994 ATTR_PACK4((*abp
), (uint32_t)vap
->va_nchildren
);
1995 abp
->actual
.dirattr
|= ATTR_DIR_ENTRYCOUNT
;
1997 if (alp
->dirattr
& ATTR_DIR_MOUNTSTATUS
) {
2002 * The vnode that is passed down may either be a
2003 * top level vnode of a mount stack or a mounted
2004 * on vnode. In either case, the directory should
2005 * be reported as a mount point.
2007 if ((vp
->v_flag
& VROOT
) || vnode_mountedhere(vp
)) {
2008 mntstat
= DIR_MNTSTATUS_MNTPOINT
;
2014 * Report back on active vnode triggers
2015 * that can directly trigger a mount
2017 if (vp
->v_resolve
&&
2018 !(vp
->v_resolve
->vr_flags
& VNT_NO_DIRECT_MOUNT
)) {
2019 mntstat
|= DIR_MNTSTATUS_TRIGGER
;
2026 ATTR_PACK4((*abp
), mntstat
);
2027 abp
->actual
.dirattr
|= ATTR_DIR_MOUNTSTATUS
;
2029 if (alp
->dirattr
& ATTR_DIR_ALLOCSIZE
) {
2030 if (VATTR_IS_SUPPORTED(vap
, va_data_alloc
)) {
2031 ATTR_PACK8((*abp
), vap
->va_data_alloc
);
2032 abp
->actual
.dirattr
|= ATTR_DIR_ALLOCSIZE
;
2033 } else if (VATTR_IS_SUPPORTED(vap
, va_total_alloc
)) {
2034 ATTR_PACK8((*abp
), vap
->va_total_alloc
);
2035 abp
->actual
.dirattr
|= ATTR_DIR_ALLOCSIZE
;
2036 } else if (!return_valid
|| pack_invalid
) {
2037 uint64_t zero_val
= 0;
2038 ATTR_PACK8((*abp
), zero_val
);
2041 if (alp
->dirattr
& ATTR_DIR_IOBLOCKSIZE
) {
2042 if (VATTR_IS_SUPPORTED(vap
, va_iosize
)) {
2043 ATTR_PACK4((*abp
), vap
->va_iosize
);
2044 abp
->actual
.dirattr
|= ATTR_DIR_IOBLOCKSIZE
;
2045 } else if (!return_valid
|| pack_invalid
) {
2046 ATTR_PACK4((*abp
), 0);
2050 * If the filesystem does not support datalength
2051 * or dataallocsize, then we infer that totalsize and
2052 * totalalloc are substitutes.
2054 if (alp
->dirattr
& ATTR_DIR_DATALENGTH
) {
2055 if (VATTR_IS_SUPPORTED(vap
, va_data_size
)) {
2056 ATTR_PACK8((*abp
), vap
->va_data_size
);
2057 abp
->actual
.dirattr
|= ATTR_DIR_DATALENGTH
;
2058 } else if (VATTR_IS_SUPPORTED(vap
, va_total_size
)) {
2059 ATTR_PACK8((*abp
), vap
->va_total_size
);
2060 abp
->actual
.dirattr
|= ATTR_DIR_DATALENGTH
;
2061 } else if (!return_valid
|| pack_invalid
) {
2062 uint64_t zero_val
= 0;
2063 ATTR_PACK8((*abp
), zero_val
);
2071 * The is_bulk parameter differentiates whether the function is called from
2072 * getattrlist or getattrlistbulk. When coming in from getattrlistbulk,
2073 * the corresponding va_* values are expected to be the values filled and no
2074 * attempt is made to retrieve them by calling back into the filesystem.
2077 attr_pack_file(vfs_context_t ctx
, struct vnode
*vp
, struct attrlist
*alp
,
2078 struct _attrlist_buf
*abp
, struct vnode_attr
*vap
, int return_valid
,
2079 int pack_invalid
, int is_bulk
)
2082 uint64_t rlength
= 0;
2083 uint64_t ralloc
= 0;
2087 * Pre-fetch the rsrc attributes now so we only get them once.
2088 * Fetch the resource fork size/allocation via xattr interface
2090 if (vp
&& !is_bulk
&&
2091 (alp
->fileattr
& (ATTR_FILE_TOTALSIZE
| ATTR_FILE_ALLOCSIZE
|
2092 ATTR_FILE_RSRCLENGTH
| ATTR_FILE_RSRCALLOCSIZE
))) {
2093 error
= vn_getxattr(vp
, XATTR_RESOURCEFORK_NAME
, NULL
,
2094 &rsize
, XATTR_NOSECURITY
, ctx
);
2096 if ((error
== ENOENT
) || (error
== ENOATTR
) ||
2097 (error
== ENOTSUP
) || (error
== EPERM
) ||
2098 (error
== EACCES
)) {
2107 if (alp
->fileattr
& (ATTR_FILE_RSRCALLOCSIZE
|
2108 ATTR_FILE_ALLOCSIZE
)) {
2111 blksize
= vp
->v_mount
->mnt_vfsstat
.f_bsize
;
2116 ralloc
= roundup(rsize
, blksize
);
2120 if (alp
->fileattr
& ATTR_FILE_LINKCOUNT
) {
2121 ATTR_PACK4((*abp
), (uint32_t)vap
->va_nlink
);
2122 abp
->actual
.fileattr
|= ATTR_FILE_LINKCOUNT
;
2125 * Note the following caveats for the TOTALSIZE and ALLOCSIZE attributes:
2126 * We infer that if the filesystem does not support va_data_size or va_data_alloc
2127 * it must not know about alternate forks. So when we need to gather
2128 * the total size or total alloc, it's OK to substitute the total size for
2129 * the data size below. This is because it is likely a flat filesystem and we must
2130 * be using AD files to store the rsrc fork and EAs.
2132 * Additionally, note that getattrlist is barred from being called on
2133 * resource fork paths. (Search for CN_ALLOWRSRCFORK). So if the filesystem does
2134 * support va_data_size, it is guaranteed to represent the data fork's size. This
2135 * is an important distinction to make because when we call vnode_getattr on
2136 * an HFS resource fork vnode, to get the size, it will vend out the resource
2137 * fork's size (it only gets the size of the passed-in vnode).
2139 if (alp
->fileattr
& ATTR_FILE_TOTALSIZE
) {
2141 uint64_t totalsize
= rlength
;
2143 if (VATTR_IS_SUPPORTED(vap
, va_data_size
)) {
2144 totalsize
+= vap
->va_data_size
;
2145 } else if (VATTR_IS_SUPPORTED(vap
, va_total_size
)) {
2146 totalsize
+= vap
->va_total_size
;
2149 ATTR_PACK8((*abp
), totalsize
);
2150 abp
->actual
.fileattr
|= ATTR_FILE_TOTALSIZE
;
2151 } else if (VATTR_IS_SUPPORTED(vap
, va_total_size
)) {
2152 ATTR_PACK8((*abp
), vap
->va_total_size
);
2153 abp
->actual
.fileattr
|= ATTR_FILE_TOTALSIZE
;
2154 } else if (!return_valid
|| pack_invalid
) {
2155 uint64_t zero_val
= 0;
2157 ATTR_PACK8((*abp
), zero_val
);
2160 if (alp
->fileattr
& ATTR_FILE_ALLOCSIZE
) {
2162 uint64_t totalalloc
= ralloc
;
2165 * If data_alloc is supported, then it must represent the
2168 if (VATTR_IS_SUPPORTED(vap
, va_data_alloc
)) {
2169 totalalloc
+= vap
->va_data_alloc
;
2170 } else if (VATTR_IS_SUPPORTED(vap
, va_total_alloc
)) {
2171 totalalloc
+= vap
->va_total_alloc
;
2174 ATTR_PACK8((*abp
), totalalloc
);
2175 abp
->actual
.fileattr
|= ATTR_FILE_ALLOCSIZE
;
2176 } else if (VATTR_IS_SUPPORTED(vap
, va_total_alloc
)) {
2177 ATTR_PACK8((*abp
), vap
->va_total_alloc
);
2178 abp
->actual
.fileattr
|= ATTR_FILE_ALLOCSIZE
;
2179 } else if (!return_valid
|| pack_invalid
) {
2180 uint64_t zero_val
= 0;
2182 ATTR_PACK8((*abp
), zero_val
);
2185 if (alp
->fileattr
& ATTR_FILE_IOBLOCKSIZE
) {
2186 if (VATTR_IS_SUPPORTED(vap
, va_iosize
)) {
2187 ATTR_PACK4((*abp
), vap
->va_iosize
);
2188 abp
->actual
.fileattr
|= ATTR_FILE_IOBLOCKSIZE
;
2189 } else if (!return_valid
|| pack_invalid
) {
2190 ATTR_PACK4((*abp
), 0);
2193 if (alp
->fileattr
& ATTR_FILE_CLUMPSIZE
) {
2194 if (!return_valid
|| pack_invalid
) {
2195 ATTR_PACK4((*abp
), 0); /* this value is deprecated */
2196 abp
->actual
.fileattr
|= ATTR_FILE_CLUMPSIZE
;
2199 if (alp
->fileattr
& ATTR_FILE_DEVTYPE
) {
2200 if (vp
&& (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
2203 if (vp
->v_specinfo
!= NULL
) {
2204 dev
= vp
->v_specinfo
->si_rdev
;
2205 } else if (VATTR_IS_SUPPORTED(vap
, va_rdev
)) {
2210 ATTR_PACK4((*abp
), dev
);
2211 abp
->actual
.fileattr
|= ATTR_FILE_DEVTYPE
;
2213 ATTR_PACK4((*abp
), 0);
2214 abp
->actual
.fileattr
|= ATTR_FILE_DEVTYPE
;
2215 } else if (VATTR_IS_SUPPORTED(vap
, va_rdev
)) {
2216 ATTR_PACK4((*abp
), vap
->va_rdev
);
2217 abp
->actual
.fileattr
|= ATTR_FILE_DEVTYPE
;
2218 } else if (!return_valid
|| pack_invalid
) {
2219 ATTR_PACK4((*abp
), 0);
2223 * If the filesystem does not support datalength
2224 * or dataallocsize, then we infer that totalsize and
2225 * totalalloc are substitutes.
2227 if (alp
->fileattr
& ATTR_FILE_DATALENGTH
) {
2228 if (VATTR_IS_SUPPORTED(vap
, va_data_size
)) {
2229 ATTR_PACK8((*abp
), vap
->va_data_size
);
2230 abp
->actual
.fileattr
|= ATTR_FILE_DATALENGTH
;
2231 } else if (VATTR_IS_SUPPORTED(vap
, va_total_size
)) {
2232 ATTR_PACK8((*abp
), vap
->va_total_size
);
2233 abp
->actual
.fileattr
|= ATTR_FILE_DATALENGTH
;
2234 } else if (!return_valid
|| pack_invalid
) {
2235 uint64_t zero_val
= 0;
2236 ATTR_PACK8((*abp
), zero_val
);
2239 if (alp
->fileattr
& ATTR_FILE_DATAALLOCSIZE
) {
2240 if (VATTR_IS_SUPPORTED(vap
, va_data_alloc
)) {
2241 ATTR_PACK8((*abp
), vap
->va_data_alloc
);
2242 abp
->actual
.fileattr
|= ATTR_FILE_DATAALLOCSIZE
;
2243 } else if (VATTR_IS_SUPPORTED(vap
, va_total_alloc
)) {
2244 ATTR_PACK8((*abp
), vap
->va_total_alloc
);
2245 abp
->actual
.fileattr
|= ATTR_FILE_DATAALLOCSIZE
;
2246 } else if (!return_valid
|| pack_invalid
) {
2247 uint64_t zero_val
= 0;
2248 ATTR_PACK8((*abp
), zero_val
);
2251 /* already got the resource fork size/allocation above */
2252 if (alp
->fileattr
& ATTR_FILE_RSRCLENGTH
) {
2254 ATTR_PACK8((*abp
), rlength
);
2255 abp
->actual
.fileattr
|= ATTR_FILE_RSRCLENGTH
;
2256 } else if (VATTR_IS_SUPPORTED(vap
, va_rsrc_length
)) {
2257 ATTR_PACK8((*abp
), vap
->va_rsrc_length
);
2258 abp
->actual
.fileattr
|= ATTR_FILE_RSRCLENGTH
;
2259 } else if (!return_valid
|| pack_invalid
) {
2260 uint64_t zero_val
= 0;
2262 ATTR_PACK8((*abp
), zero_val
);
2265 if (alp
->fileattr
& ATTR_FILE_RSRCALLOCSIZE
) {
2267 ATTR_PACK8((*abp
), ralloc
);
2268 abp
->actual
.fileattr
|= ATTR_FILE_RSRCALLOCSIZE
;
2269 } else if (VATTR_IS_SUPPORTED(vap
, va_rsrc_alloc
)) {
2270 ATTR_PACK8((*abp
), vap
->va_rsrc_alloc
);
2271 abp
->actual
.fileattr
|= ATTR_FILE_RSRCALLOCSIZE
;
2272 } else if (!return_valid
|| pack_invalid
) {
2273 uint64_t zero_val
= 0;
2275 ATTR_PACK8((*abp
), zero_val
);
2283 * Pack FORKATTR attributes into a user buffer.
2284 * alp is a pointer to the bitmap of attributes required.
2285 * abp is the state of the attribute filling operation.
2286 * The attribute data (along with some other fields that are required
2290 attr_pack_common_extended(mount_t mp
, struct vnode
*vp
, struct attrlist
*alp
,
2291 struct _attrlist_buf
*abp
, const char *relpathptr
, ssize_t relpathlen
,
2292 const char *REALpathptr
, ssize_t REALpathlen
,
2293 struct vnode_attr
*vap
, int return_valid
, int pack_invalid
)
2295 if (vp
&& (alp
->forkattr
& ATTR_CMNEXT_RELPATH
)) {
2296 attrlist_pack_string(abp
, relpathptr
, relpathlen
);
2297 abp
->actual
.forkattr
|= ATTR_CMNEXT_RELPATH
;
2300 if (alp
->forkattr
& ATTR_CMNEXT_PRIVATESIZE
) {
2301 if (VATTR_IS_SUPPORTED(vap
, va_private_size
)) {
2302 ATTR_PACK8((*abp
), vap
->va_private_size
);
2303 abp
->actual
.forkattr
|= ATTR_CMNEXT_PRIVATESIZE
;
2304 } else if (!return_valid
|| pack_invalid
) {
2305 uint64_t zero_val
= 0;
2306 ATTR_PACK8((*abp
), zero_val
);
2310 if (alp
->forkattr
& ATTR_CMNEXT_LINKID
) {
2313 if (VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
2314 linkid
= vap
->va_linkid
;
2316 linkid
= vap
->va_fileid
;
2319 ATTR_PACK8((*abp
), linkid
);
2320 abp
->actual
.forkattr
|= ATTR_CMNEXT_LINKID
;
2323 if (vp
&& (alp
->forkattr
& ATTR_CMNEXT_NOFIRMLINKPATH
)) {
2324 attrlist_pack_string(abp
, REALpathptr
, REALpathlen
);
2325 abp
->actual
.forkattr
|= ATTR_CMNEXT_NOFIRMLINKPATH
;
2328 if (alp
->forkattr
& ATTR_CMNEXT_REALDEVID
) {
2331 mp
->mnt_vfsstat
.f_fsid
.val
[0]);
2332 abp
->actual
.forkattr
|= ATTR_CMNEXT_REALDEVID
;
2335 vp
->v_mount
->mnt_vfsstat
.f_fsid
.val
[0]);
2336 abp
->actual
.forkattr
|= ATTR_CMNEXT_REALDEVID
;
2337 } else if (VATTR_IS_SUPPORTED(vap
, va_fsid
)) {
2338 ATTR_PACK4((*abp
), vap
->va_fsid
);
2339 abp
->actual
.forkattr
|= ATTR_CMN_DEVID
;
2340 } else if (!return_valid
|| pack_invalid
) {
2341 ATTR_PACK4((*abp
), 0);
2345 if (alp
->forkattr
& ATTR_CMNEXT_REALFSID
) {
2348 mp
->mnt_vfsstat
.f_fsid
);
2349 abp
->actual
.forkattr
|= ATTR_CMNEXT_REALFSID
;
2352 vp
->v_mount
->mnt_vfsstat
.f_fsid
);
2353 abp
->actual
.forkattr
|= ATTR_CMNEXT_REALFSID
;
2354 } else if (VATTR_IS_SUPPORTED(vap
, va_fsid64
)) {
2355 ATTR_PACK8((*abp
), vap
->va_fsid64
);
2356 abp
->actual
.forkattr
|= ATTR_CMN_FSID
;
2357 } else if (!return_valid
|| pack_invalid
) {
2358 fsid_t fsid
= {{0}};
2360 ATTR_PACK8((*abp
), fsid
);
2364 if (alp
->forkattr
& ATTR_CMNEXT_CLONEID
) {
2365 if (VATTR_IS_SUPPORTED(vap
, va_clone_id
)) {
2366 ATTR_PACK8((*abp
), vap
->va_clone_id
);
2367 abp
->actual
.forkattr
|= ATTR_CMNEXT_CLONEID
;
2368 } else if (!return_valid
|| pack_invalid
) {
2369 uint64_t zero_val
= 0;
2370 ATTR_PACK8((*abp
), zero_val
);
2374 if (alp
->forkattr
& ATTR_CMNEXT_EXT_FLAGS
) {
2375 if (VATTR_IS_SUPPORTED(vap
, va_extflags
)) {
2376 ATTR_PACK8((*abp
), vap
->va_extflags
);
2377 abp
->actual
.forkattr
|= ATTR_CMNEXT_EXT_FLAGS
;
2378 } else if (!return_valid
|| pack_invalid
) {
2379 uint64_t zero_val
= 0;
2380 ATTR_PACK8((*abp
), zero_val
);
2388 vattr_get_alt_data(vnode_t vp
, struct attrlist
*alp
, struct vnode_attr
*vap
,
2389 int return_valid
, int is_bulk
,
2390 #if !CONFIG_FIRMLINKS
2393 int is_realdev
, vfs_context_t ctx
)
2396 * There are a couple of special cases.
2397 * If we are after object IDs, we can make do with va_fileid.
2399 if ((alp
->commonattr
&
2400 (ATTR_CMN_OBJID
| ATTR_CMN_OBJPERMANENTID
| ATTR_CMN_FILEID
)) &&
2401 !VATTR_IS_SUPPORTED(vap
, va_linkid
)) {
2402 /* forget we wanted this */
2403 VATTR_CLEAR_ACTIVE(vap
, va_linkid
);
2407 * A filesystem may not support va_fsid64. If it is not available, then we'll
2408 * synthesize it from the mount.
2410 if ((alp
->commonattr
& ATTR_CMN_FSID
) && !VATTR_IS_SUPPORTED(vap
, va_fsid64
)) {
2411 VATTR_CLEAR_ACTIVE(vap
, va_fsid64
);
2415 if ((alp
->commonattr
& ATTR_CMN_FSID
) && !VATTR_IS_SUPPORTED(vap
, va_fsid
)) {
2416 VATTR_CLEAR_ACTIVE(vap
, va_fsid
);
2419 /* We request the fsid64 for the devid */
2420 if ((alp
->commonattr
& ATTR_CMN_DEVID
) && !VATTR_IS_SUPPORTED(vap
, va_fsid
)) {
2421 VATTR_CLEAR_ACTIVE(vap
, va_fsid
);
2426 * Many filesystems don't know their parent object id.
2427 * If necessary, attempt to derive it from the vnode.
2429 if ((alp
->commonattr
& (ATTR_CMN_PAROBJID
| ATTR_CMN_PARENTID
)) && vp
) {
2432 #if CONFIG_FIRMLINKS
2433 /* If this is a firmlink target, we get the fileid of the firmlink parent. */
2434 if (!is_realdev
&& (vp
->v_flag
& VFMLINKTARGET
) && ((dvp
= vp
->v_fmlink
) != NULL
) && (vnode_get(dvp
) == 0)) {
2435 struct vnode_attr lva
;
2438 VATTR_WANTED(&lva
, va_parentid
);
2439 VATTR_WANTED(&lva
, va_fsid
);
2440 if (vnode_getattr(dvp
, &lva
, ctx
) == 0 &&
2441 VATTR_IS_SUPPORTED(&lva
, va_parentid
) &&
2442 VATTR_IS_SUPPORTED(&lva
, va_fsid
) &&
2443 (lva
.va_fsid
== (uint32_t)vp
->v_mount
->mnt_vfsstat
.f_fsid
.val
[0])) {
2444 vap
->va_parentid
= lva
.va_parentid
;
2445 VATTR_SET_SUPPORTED(vap
, va_parentid
);
2449 #endif /* CONFIG_FIRMLINKS */
2450 if (!VATTR_IS_SUPPORTED(vap
, va_parentid
) && !is_bulk
) {
2451 if ((dvp
= vnode_getparent(vp
)) != NULLVP
) {
2452 struct vnode_attr lva
;
2455 VATTR_WANTED(&lva
, va_fileid
);
2456 if (vnode_getattr(dvp
, &lva
, ctx
) == 0 &&
2457 VATTR_IS_SUPPORTED(vap
, va_fileid
)) {
2458 vap
->va_parentid
= lva
.va_fileid
;
2459 VATTR_SET_SUPPORTED(vap
, va_parentid
);
2467 * And we can report datasize/alloc from total.
2469 if ((alp
->fileattr
& ATTR_FILE_DATALENGTH
) &&
2470 !VATTR_IS_SUPPORTED(vap
, va_data_size
)) {
2471 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
2474 if ((alp
->fileattr
& ATTR_FILE_DATAALLOCSIZE
) &&
2475 !VATTR_IS_SUPPORTED(vap
, va_data_alloc
)) {
2476 VATTR_CLEAR_ACTIVE(vap
, va_data_alloc
);
2480 * If we don't have an encoding, go with UTF-8
2482 if ((alp
->commonattr
& ATTR_CMN_SCRIPT
) &&
2483 !VATTR_IS_SUPPORTED(vap
, va_encoding
) && !return_valid
) {
2484 VATTR_RETURN(vap
, va_encoding
,
2485 0x7e /* kTextEncodingMacUnicode */);
2489 * If we don't have a name, we'll get one from the vnode or
2492 if ((alp
->commonattr
& ATTR_CMN_NAME
) &&
2493 !VATTR_IS_SUPPORTED(vap
, va_name
)) {
2494 VATTR_CLEAR_ACTIVE(vap
, va_name
);
2497 /* If va_dirlinkcount isn't supported use a default of 1. */
2498 if ((alp
->dirattr
& ATTR_DIR_LINKCOUNT
) &&
2499 !VATTR_IS_SUPPORTED(vap
, va_dirlinkcount
)) {
2500 VATTR_RETURN(vap
, va_dirlinkcount
, 1);
2504 struct _attrlist_paths
{
2506 ssize_t
*fullpathlenp
;
2508 ssize_t
*relpathlenp
;
2510 ssize_t
*REALpathlenp
;
2514 calc_varsize(vnode_t vp
, struct attrlist
*alp
, struct vnode_attr
*vap
,
2515 ssize_t
*varsizep
, struct _attrlist_paths
*pathsp
, const char **vnamep
,
2516 const char **cnpp
, ssize_t
*cnlp
)
2520 *varsizep
= 0; /* length count */
2521 /* We may need to fix up the name attribute if requested */
2522 if (alp
->commonattr
& ATTR_CMN_NAME
) {
2523 if (VATTR_IS_SUPPORTED(vap
, va_name
)) {
2524 vap
->va_name
[MAXPATHLEN
- 1] = '\0'; /* Ensure nul-termination */
2525 *cnpp
= vap
->va_name
;
2526 *cnlp
= strlen(*cnpp
);
2528 /* Filesystem did not support getting the name */
2529 if (vnode_isvroot(vp
)) {
2530 if (vp
->v_mount
->mnt_vfsstat
.f_mntonname
[1] == 0x00 &&
2531 vp
->v_mount
->mnt_vfsstat
.f_mntonname
[0] == '/') {
2532 /* special case for boot volume. Use root name when it's
2533 * available (which is the volume name) or just the mount on
2534 * name of "/". we must do this for binary compatibility with
2535 * pre Tiger code. returning nothing for the boot volume name
2536 * breaks installers - 3961058
2538 *cnpp
= *vnamep
= vnode_getname(vp
);
2539 if (*cnpp
== NULL
) {
2540 /* just use "/" as name */
2541 *cnpp
= &vp
->v_mount
->mnt_vfsstat
.f_mntonname
[0];
2543 *cnlp
= strlen(*cnpp
);
2545 getattrlist_findnamecomp(vp
->v_mount
->mnt_vfsstat
.f_mntonname
, cnpp
, cnlp
);
2548 *cnpp
= *vnamep
= vnode_getname(vp
);
2550 if (*cnpp
!= NULL
) {
2551 *cnlp
= strlen(*cnpp
);
2557 *varsizep
+= roundup(*cnlp
+ 1, 4);
2561 * Compute the full path to this vnode, if necessary. This attribute is almost certainly
2562 * not supported by any filesystem, so build the path to this vnode at this time.
2564 if (vp
&& (alp
->commonattr
& ATTR_CMN_FULLPATH
)) {
2565 int len
= MAXPATHLEN
;
2568 /* call build_path making sure NOT to use the cache-only behavior */
2569 err
= build_path(vp
, pathsp
->fullpathptr
, len
, &len
, 0, vfs_context_current());
2574 if (pathsp
->fullpathptr
) {
2575 *(pathsp
->fullpathlenp
) = strlen(pathsp
->fullpathptr
);
2577 *(pathsp
->fullpathlenp
) = 0;
2579 *varsizep
+= roundup(((*(pathsp
->fullpathlenp
)) + 1), 4);
2583 * Compute this vnode's volume relative path.
2585 if (vp
&& (alp
->forkattr
& ATTR_CMNEXT_RELPATH
)) {
2589 /* call build_path making sure NOT to use the cache-only behavior */
2590 err
= build_path(vp
, pathsp
->relpathptr
, MAXPATHLEN
, &len
, BUILDPATH_VOLUME_RELATIVE
, vfs_context_current());
2596 //`len' includes trailing null
2597 *(pathsp
->relpathlenp
) = len
- 1;
2598 *varsizep
+= roundup(len
, 4);
2602 * Compute this vnode's real (firmlink free) path.
2604 if (vp
&& (alp
->forkattr
& ATTR_CMNEXT_NOFIRMLINKPATH
)) {
2608 /* call build_path making sure NOT to use the cache-only behavior */
2609 err
= build_path(vp
, pathsp
->REALpathptr
, MAXPATHLEN
, &len
, BUILDPATH_NO_FIRMLINK
, vfs_context_current());
2615 //`len' includes trailing null
2616 *(pathsp
->REALpathlenp
) = len
- 1;
2617 *varsizep
+= roundup(len
, 4);
2621 * We have a kauth_acl_t but we will be returning a kauth_filesec_t.
2623 * XXX This needs to change at some point; since the blob is opaque in
2624 * user-space this is OK.
2626 if ((alp
->commonattr
& ATTR_CMN_EXTENDED_SECURITY
) &&
2627 VATTR_IS_SUPPORTED(vap
, va_acl
) &&
2628 (vap
->va_acl
!= NULL
)) {
2630 * Since we have a kauth_acl_t (not a kauth_filesec_t), we have to check against
2631 * KAUTH_FILESEC_NOACL ourselves
2633 if (vap
->va_acl
->acl_entrycount
== KAUTH_FILESEC_NOACL
) {
2634 *varsizep
+= roundup((KAUTH_FILESEC_SIZE(0)), 4);
2636 *varsizep
+= roundup((KAUTH_FILESEC_SIZE(vap
->va_acl
->acl_entrycount
)), 4);
2645 vfs_attr_pack_internal(mount_t mp
, vnode_t vp
, uio_t auio
, struct attrlist
*alp
,
2646 uint64_t options
, struct vnode_attr
*vap
, __unused
void *fndesc
,
2647 vfs_context_t ctx
, int is_bulk
, enum vtype vtype
, ssize_t fixedsize
)
2649 struct _attrlist_buf ab
;
2650 struct _attrlist_paths apaths
= {.fullpathptr
= NULL
, .fullpathlenp
= NULL
,
2651 .relpathptr
= NULL
, .relpathlenp
= NULL
,
2652 .REALpathptr
= NULL
, .REALpathlenp
= NULL
};
2656 const char *vname
= NULL
;
2660 ssize_t fullpathlen
;
2664 ssize_t REALpathlen
;
2670 int alloc_local_buf
;
2671 const int use_fork
= options
& FSOPT_ATTR_CMN_EXTENDED
;
2673 proc_is64
= proc_is64bit(vfs_context_proc(ctx
));
2684 alloc_local_buf
= 0;
2686 buf_size
= (ssize_t
)uio_resid(auio
);
2687 if ((buf_size
<= 0) || (uio_iovcnt(auio
) > 1)) {
2692 /* Check for special packing semantics */
2693 return_valid
= (alp
->commonattr
& ATTR_CMN_RETURNED_ATTRS
) ? 1 : 0;
2694 pack_invalid
= (options
& FSOPT_PACK_INVAL_ATTRS
) ? 1 : 0;
2695 is_realdev
= options
& FSOPT_RETURN_REALDEV
? 1 : 0;
2698 /* Generate a valid mask for post processing */
2699 bcopy(&(alp
->commonattr
), &ab
.valid
, sizeof(attribute_set_t
));
2702 /* did we ask for something the filesystem doesn't support? */
2703 if (vap
->va_active
&&
2704 (!VATTR_ALL_SUPPORTED(vap
)
2705 #if CONFIG_FIRMLINKS
2706 /* For firmlink targets we have to overide what the FS returned for parentid */
2708 (!is_realdev
&& vp
&& (vp
->v_flag
& VFMLINKTARGET
) && vp
->v_fmlink
&&
2709 (alp
->commonattr
& (ATTR_CMN_PAROBJID
| ATTR_CMN_PARENTID
)))
2712 // this disables the selectors that were not supported by the filesystem
2713 vattr_get_alt_data(vp
, alp
, vap
, return_valid
, is_bulk
, is_realdev
,
2717 if (!VATTR_ALL_SUPPORTED(vap
)) {
2718 if (return_valid
&& pack_invalid
) {
2719 /* Fix up valid mask for post processing */
2720 getattrlist_fixupattrs(&ab
.valid
, vap
, use_fork
);
2722 /* Force packing of everything asked for */
2723 vap
->va_supported
= vap
->va_active
;
2724 } else if (return_valid
) {
2725 /* Adjust the requested attributes */
2726 getattrlist_fixupattrs(
2727 (attribute_set_t
*)&(alp
->commonattr
), vap
, use_fork
);
2738 //if a path is requested, allocate a temporary buffer to build it
2739 if (vp
&& (alp
->commonattr
& (ATTR_CMN_FULLPATH
))) {
2740 fullpathptr
= (char*) kalloc(MAXPATHLEN
);
2741 if (fullpathptr
== NULL
) {
2743 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: cannot allocate fullpath buffer");
2746 bzero(fullpathptr
, MAXPATHLEN
);
2747 apaths
.fullpathptr
= fullpathptr
;
2748 apaths
.fullpathlenp
= &fullpathlen
;
2751 // only interpret fork attributes if they're used as new common attributes
2752 if (vp
&& use_fork
) {
2753 if (alp
->forkattr
& (ATTR_CMNEXT_RELPATH
)) {
2754 relpathptr
= (char*) kalloc(MAXPATHLEN
);
2755 if (relpathptr
== NULL
) {
2757 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: cannot allocate relpath buffer");
2760 bzero(relpathptr
, MAXPATHLEN
);
2761 apaths
.relpathptr
= relpathptr
;
2762 apaths
.relpathlenp
= &relpathlen
;
2765 if (alp
->forkattr
& (ATTR_CMNEXT_NOFIRMLINKPATH
)) {
2766 REALpathptr
= (char*) kalloc(MAXPATHLEN
);
2767 if (REALpathptr
== NULL
) {
2769 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: cannot allocate canonpath buffer");
2772 bzero(REALpathptr
, MAXPATHLEN
);
2773 apaths
.REALpathptr
= REALpathptr
;
2774 apaths
.REALpathlenp
= &REALpathlen
;
2779 * Compute variable-space requirements.
2781 error
= calc_varsize(vp
, alp
, vap
, &varsize
, &apaths
, &vname
, &cnp
, &cnl
);
2787 * Allocate a target buffer for attribute results.
2789 * Note that we won't ever copy out more than the caller requested, even though
2790 * we might have to allocate more than they offer so that the diagnostic checks
2791 * don't result in a panic if the caller's buffer is too small.
2793 ab
.allocated
= fixedsize
+ varsize
;
2794 /* Cast 'allocated' to an unsigned to verify allocation size */
2795 if (((size_t)ab
.allocated
) > ATTR_MAX_BUFFER
) {
2797 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: buffer size too large (%d limit %d)", ab
.allocated
, ATTR_MAX_BUFFER
);
2802 * Special handling for bulk calls, align to 8 (and only if enough
2806 if (buf_size
< ab
.allocated
) {
2811 newlen
= (ab
.allocated
+ 7) & ~0x07;
2812 /* Align only if enough space for alignment */
2813 if (newlen
<= (uint32_t)buf_size
) {
2814 ab
.allocated
= newlen
;
2820 * See if we can reuse buffer passed in i.e. it is a kernel buffer
2823 if (uio_isuserspace(auio
) || (buf_size
< ab
.allocated
)) {
2824 MALLOC(ab
.base
, char *, ab
.allocated
, M_TEMP
,
2826 alloc_local_buf
= 1;
2829 * In case this is a kernel buffer and sufficiently
2830 * big, this function will try to use that buffer
2831 * instead of allocating another buffer and bcopy'ing
2834 * The calculation below figures out where to start
2835 * writing in the buffer and once all the data has been
2836 * filled in, uio_resid is updated to reflect the usage
2839 * uio_offset cannot be used here to determine the
2840 * starting location as uio_offset could be set to a
2841 * value which has nothing to do the location
2844 ab
.base
= (char *)uio_curriovbase(auio
) +
2845 ((ssize_t
)uio_curriovlen(auio
) - buf_size
);
2846 bzero(ab
.base
, ab
.allocated
);
2849 if (ab
.base
== NULL
) {
2851 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not allocate %d for copy buffer", ab
.allocated
);
2856 /* set the S_IFMT bits for the mode */
2857 if (alp
->commonattr
& ATTR_CMN_ACCESSMASK
) {
2859 switch (vp
->v_type
) {
2861 vap
->va_mode
|= S_IFREG
;
2864 vap
->va_mode
|= S_IFDIR
;
2867 vap
->va_mode
|= S_IFBLK
;
2870 vap
->va_mode
|= S_IFCHR
;
2873 vap
->va_mode
|= S_IFLNK
;
2876 vap
->va_mode
|= S_IFSOCK
;
2879 vap
->va_mode
|= S_IFIFO
;
2889 * Pack results into the destination buffer.
2891 ab
.fixedcursor
= ab
.base
+ sizeof(uint32_t);
2893 ab
.fixedcursor
+= sizeof(attribute_set_t
);
2894 bzero(&ab
.actual
, sizeof(ab
.actual
));
2896 ab
.varcursor
= ab
.base
+ fixedsize
;
2897 ab
.needed
= ab
.allocated
;
2899 /* common attributes ************************************************/
2900 error
= attr_pack_common(ctx
, (options
& FSOPT_RETURN_REALDEV
? mp
: NULL
),
2901 vp
, alp
, &ab
, vap
, proc_is64
, cnp
, cnl
, fullpathptr
, fullpathlen
,
2902 return_valid
, pack_invalid
, vtype
, is_bulk
);
2904 /* directory attributes *********************************************/
2905 if (!error
&& alp
->dirattr
&& (vtype
== VDIR
)) {
2906 error
= attr_pack_dir(vp
, alp
, &ab
, vap
, return_valid
, pack_invalid
);
2909 /* file attributes **************************************************/
2910 if (!error
&& alp
->fileattr
&& (vtype
!= VDIR
)) {
2911 error
= attr_pack_file(ctx
, vp
, alp
, &ab
, vap
, return_valid
,
2912 pack_invalid
, is_bulk
);
2915 /* common extended attributes *****************************************/
2916 if (!error
&& use_fork
) {
2917 error
= attr_pack_common_extended(mp
, vp
, alp
, &ab
, relpathptr
, relpathlen
,
2918 REALpathptr
, REALpathlen
, vap
, return_valid
, pack_invalid
);
2926 if (!return_valid
&& (ab
.fixedcursor
- ab
.base
) != fixedsize
) {
2927 panic("packed field size mismatch; allocated %ld but packed %ld for common %08x vol %08x",
2928 fixedsize
, (long) (ab
.fixedcursor
- ab
.base
), alp
->commonattr
, alp
->volattr
);
2930 if (!return_valid
&& ab
.varcursor
!= (ab
.base
+ ab
.needed
)) {
2931 panic("packed variable field size mismatch; used %ld but expected %ld", (long) (ab
.varcursor
- ab
.base
), ab
.needed
);
2935 * In the compatible case, we report the smaller of the required and returned sizes.
2936 * If the FSOPT_REPORT_FULLSIZE option is supplied, we report the full (required) size
2937 * of the result buffer, even if we copied less out. The caller knows how big a buffer
2938 * they gave us, so they can always check for truncation themselves.
2940 *(uint32_t *)ab
.base
= (options
& FSOPT_REPORT_FULLSIZE
) ? ab
.needed
: imin(ab
.allocated
, ab
.needed
);
2942 /* Return attribute set output if requested. */
2944 ab
.actual
.commonattr
|= ATTR_CMN_RETURNED_ATTRS
;
2946 /* Only report the attributes that are valid */
2947 ab
.actual
.commonattr
&= ab
.valid
.commonattr
;
2948 ab
.actual
.dirattr
&= ab
.valid
.dirattr
;
2949 ab
.actual
.fileattr
&= ab
.valid
.fileattr
;
2951 bcopy(&ab
.actual
, ab
.base
+ sizeof(uint32_t), sizeof(ab
.actual
));
2954 copy_size
= imin(buf_size
, ab
.allocated
);
2956 /* Only actually copyout as much out as the user buffer can hold */
2957 if (alloc_local_buf
) {
2958 error
= uiomove(ab
.base
, copy_size
, auio
);
2960 off_t orig_offset
= uio_offset(auio
);
2963 * The buffer in the uio struct was used directly
2964 * (i.e. it was a kernel buffer and big enough
2965 * to hold the data required) in order to avoid
2966 * un-needed allocation and copies.
2968 * At this point, update the resid value to what it
2969 * would be if this was the result of a uiomove. The
2970 * offset is also incremented, though it may not
2971 * mean anything to the caller but that is what
2972 * uiomove does as well.
2974 uio_setresid(auio
, buf_size
- copy_size
);
2975 uio_setoffset(auio
, orig_offset
+ (off_t
)copy_size
);
2980 vnode_putname(vname
);
2983 kfree(fullpathptr
, MAXPATHLEN
);
2986 kfree(relpathptr
, MAXPATHLEN
);
2989 kfree(REALpathptr
, MAXPATHLEN
);
2991 if (ab
.base
!= NULL
&& alloc_local_buf
) {
2992 FREE(ab
.base
, M_TEMP
);
2998 vfs_attr_pack_ext(mount_t mp
, vnode_t vp
, uio_t uio
, struct attrlist
*alp
, uint64_t options
,
2999 struct vnode_attr
*vap
, __unused
void *fndesc
, vfs_context_t ctx
)
3003 uint64_t orig_active
;
3004 struct attrlist orig_al
;
3008 v_type
= vnode_vtype(vp
);
3010 v_type
= vap
->va_objtype
;
3014 orig_active
= vap
->va_active
;
3017 error
= getattrlist_setupvattr_all(alp
, vap
, v_type
, &fixedsize
,
3018 proc_is64bit(vfs_context_proc(ctx
)), options
& FSOPT_ATTR_CMN_EXTENDED
);
3022 "ATTRLIST - ERROR: setup for request failed");
3026 error
= vfs_attr_pack_internal(mp
, vp
, uio
, alp
,
3027 options
| FSOPT_REPORT_FULLSIZE
, vap
, NULL
, ctx
, 1, v_type
,
3030 VATTR_CLEAR_SUPPORTED_ALL(vap
);
3031 vap
->va_active
= orig_active
;
3038 vfs_attr_pack(vnode_t vp
, uio_t uio
, struct attrlist
*alp
, uint64_t options
,
3039 struct vnode_attr
*vap
, __unused
void *fndesc
, vfs_context_t ctx
)
3041 return vfs_attr_pack_ext(NULL
, vp
, uio
, alp
, options
, vap
, fndesc
, ctx
);
3045 * Obtain attribute information about a filesystem object.
3047 * Note: The alt_name parameter can be used by the caller to pass in the vnode
3048 * name obtained from some authoritative source (eg. readdir vnop); where
3049 * filesystems' getattr vnops do not support ATTR_CMN_NAME, the alt_name will be
3050 * used as the ATTR_CMN_NAME attribute returned in vnode_attr.va_name.
3054 getattrlist_internal(vfs_context_t ctx
, vnode_t vp
, struct attrlist
*alp
,
3055 user_addr_t attributeBuffer
, size_t bufferSize
, uint64_t options
,
3056 enum uio_seg segflg
, char* authoritative_name
, struct ucred
*file_cred
)
3058 struct vnode_attr va
;
3059 kauth_action_t action
;
3068 char uio_buf
[UIO_SIZEOF(1)];
3069 // must be true for fork attributes to be used as new common attributes
3070 const int use_fork
= (options
& FSOPT_ATTR_CMN_EXTENDED
) != 0;
3072 if (bufferSize
< sizeof(uint32_t)) {
3076 proc_is64
= proc_is64bit(vfs_context_proc(ctx
));
3078 if (segflg
== UIO_USERSPACE
) {
3080 segflg
= UIO_USERSPACE64
;
3082 segflg
= UIO_USERSPACE32
;
3085 auio
= uio_createwithbuffer(1, 0, segflg
, UIO_READ
,
3086 &uio_buf
[0], sizeof(uio_buf
));
3087 uio_addiov(auio
, attributeBuffer
, bufferSize
);
3092 if (alp
->bitmapcount
!= ATTR_BIT_MAP_COUNT
) {
3097 VFS_DEBUG(ctx
, vp
, "%p ATTRLIST - %s request common %08x vol %08x file %08x dir %08x fork %08x %sfollow on '%s'",
3098 vp
, vfs_context_proc(ctx
)->p_comm
, alp
->commonattr
, alp
->volattr
, alp
->fileattr
, alp
->dirattr
, alp
->forkattr
,
3099 (options
& FSOPT_NOFOLLOW
) ? "no":"", vp
->v_name
);
3102 error
= mac_vnode_check_getattrlist(ctx
, vp
, alp
);
3109 * It is legal to request volume or file attributes, but not both.
3111 * 26903449 fork attributes can also be requested, but only if they're
3112 * interpreted as new, common attributes
3115 if (alp
->fileattr
|| alp
->dirattr
|| (alp
->forkattr
&& !use_fork
)) {
3117 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: mixed volume/file/directory attributes");
3120 /* handle volume attribute request */
3121 error
= getvolattrlist(ctx
, vp
, alp
, attributeBuffer
,
3122 bufferSize
, options
, segflg
, proc_is64
);
3127 * ATTR_CMN_GEN_COUNT and ATTR_CMN_DOCUMENT_ID reuse the bits
3128 * originally allocated to ATTR_CMN_NAMEDATTRCOUNT and
3129 * ATTR_CMN_NAMEDATTRLIST.
3131 if ((alp
->commonattr
& (ATTR_CMN_GEN_COUNT
| ATTR_CMN_DOCUMENT_ID
)) &&
3132 !(options
& FSOPT_ATTR_CMN_EXTENDED
)) {
3137 /* common extended attributes require FSOPT_ATTR_CMN_EXTENDED option */
3138 if (!(use_fork
) && (alp
->forkattr
& ATTR_CMNEXT_VALIDMASK
)) {
3143 /* FSOPT_ATTR_CMN_EXTENDED requires forkattrs are not referenced */
3144 if ((options
& FSOPT_ATTR_CMN_EXTENDED
) && (alp
->forkattr
& (ATTR_FORK_VALIDMASK
))) {
3149 /* Check for special packing semantics */
3150 return_valid
= (alp
->commonattr
& ATTR_CMN_RETURNED_ATTRS
) ? 1 : 0;
3151 pack_invalid
= (options
& FSOPT_PACK_INVAL_ATTRS
) ? 1 : 0;
3153 /* FSOPT_PACK_INVAL_ATTRS requires ATTR_CMN_RETURNED_ATTRS */
3154 if (!return_valid
|| (alp
->forkattr
&& !use_fork
)) {
3158 /* Keep invalid attrs from being uninitialized */
3159 bzero(&va
, sizeof(va
));
3162 /* Pick up the vnode type. If the FS is bad and changes vnode types on us, we
3163 * will have a valid snapshot that we can work from here.
3168 * Set up the vnode_attr structure and authorise.
3170 if ((error
= getattrlist_setupvattr(alp
, &va
, &fixedsize
, &action
, proc_is64
, (vtype
== VDIR
), use_fork
)) != 0) {
3171 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: setup for request failed");
3174 if ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0) {
3175 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: authorisation failed/denied");
3180 if (va
.va_active
!= 0) {
3181 uint64_t va_active
= va
.va_active
;
3184 * If we're going to ask for va_name, allocate a buffer to point it at
3186 if (VATTR_IS_ACTIVE(&va
, va_name
)) {
3187 MALLOC_ZONE(va_name
, char *, MAXPATHLEN
, M_NAMEI
,
3189 if (va_name
== NULL
) {
3191 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: cannot allocate va_name buffer");
3195 * If we have an authoritative_name, prefer that name.
3197 * N.B. Since authoritative_name implies this is coming from getattrlistbulk,
3198 * we know the name is authoritative. For /dev/fd, we want to use the file
3199 * descriptor as the name not the underlying name of the associate vnode in a
3200 * particular file system.
3202 if (authoritative_name
) {
3203 /* Don't ask the file system */
3204 VATTR_CLEAR_ACTIVE(&va
, va_name
);
3205 strlcpy(va_name
, authoritative_name
, MAXPATHLEN
);
3209 va
.va_name
= authoritative_name
? NULL
: va_name
;
3211 if (options
& FSOPT_RETURN_REALDEV
) {
3212 va
.va_vaflags
|= VA_REALFSID
;
3216 * Call the filesystem.
3218 if ((error
= vnode_getattr(vp
, &va
, ctx
)) != 0) {
3219 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: filesystem returned %d", error
);
3224 * Give MAC polices a chance to reject or filter the
3225 * attributes returned by the filesystem. Note that MAC
3226 * policies are consulted *after* calling the filesystem
3227 * because filesystems can return more attributes than
3228 * were requested so policies wouldn't be authoritative
3229 * is consulted beforehand. This also gives policies an
3230 * opportunity to change the values of attributes
3233 error
= mac_vnode_check_getattr(ctx
, file_cred
, vp
, &va
);
3235 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: MAC framework returned %d", error
);
3243 * It we ask for the name, i.e., vname is non null and
3244 * we have an authoritative name, then reset va_name is
3245 * active and if needed set va_name is supported.
3247 * A (buggy) filesystem may change fields which belong
3248 * to us. We try to deal with that here as well.
3250 va
.va_active
= va_active
;
3251 if (authoritative_name
&& va_name
) {
3252 VATTR_SET_ACTIVE(&va
, va_name
);
3253 if (!(VATTR_IS_SUPPORTED(&va
, va_name
))) {
3254 VATTR_SET_SUPPORTED(&va
, va_name
);
3257 va
.va_name
= va_name
;
3260 error
= vfs_attr_pack_internal(vp
->v_mount
, vp
, auio
, alp
, options
, &va
, NULL
, ctx
,
3261 0, vtype
, fixedsize
);
3265 FREE_ZONE(va_name
, MAXPATHLEN
, M_NAMEI
);
3267 if (VATTR_IS_SUPPORTED(&va
, va_acl
) && (va
.va_acl
!= NULL
)) {
3268 kauth_acl_free(va
.va_acl
);
3271 VFS_DEBUG(ctx
, vp
, "ATTRLIST - returning %d", error
);
3276 fgetattrlist(proc_t p
, struct fgetattrlist_args
*uap
, __unused
int32_t *retval
)
3282 struct fileproc
*fp
;
3284 ctx
= vfs_context_current();
3289 if ((error
= file_vnode(uap
->fd
, &vp
)) != 0) {
3293 if ((error
= fp_lookup(p
, uap
->fd
, &fp
, 0)) != 0 ||
3294 (error
= vnode_getwithref(vp
)) != 0) {
3300 * Fetch the attribute request.
3302 error
= copyin(uap
->alist
, &al
, sizeof(al
));
3307 /* Default to using the vnode's name. */
3308 error
= getattrlist_internal(ctx
, vp
, &al
, uap
->attributeBuffer
,
3309 uap
->bufferSize
, uap
->options
,
3310 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: \
3311 UIO_USERSPACE32
), NULL
,
3312 fp
->f_fglob
->fg_cred
);
3316 fp_drop(p
, uap
->fd
, fp
, 0);
3327 getattrlistat_internal(vfs_context_t ctx
, user_addr_t path
,
3328 struct attrlist
*alp
, user_addr_t attributeBuffer
, size_t bufferSize
,
3329 uint64_t options
, enum uio_seg segflg
, enum uio_seg pathsegflg
, int fd
)
3331 struct nameidata nd
;
3340 if (!(options
& FSOPT_NOFOLLOW
)) {
3341 nameiflags
|= FOLLOW
;
3344 nameiflags
|= AUDITVNPATH1
;
3345 NDINIT(&nd
, LOOKUP
, OP_GETATTR
, nameiflags
, pathsegflg
,
3348 error
= nameiat(&nd
, fd
);
3356 error
= getattrlist_internal(ctx
, vp
, alp
, attributeBuffer
,
3357 bufferSize
, options
, segflg
, NULL
, NOCRED
);
3359 /* Retain the namei reference until the getattrlist completes. */
3366 getattrlist(proc_t p
, struct getattrlist_args
*uap
, __unused
int32_t *retval
)
3368 enum uio_seg segflg
;
3372 segflg
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
3375 * Fetch the attribute request.
3377 error
= copyin(uap
->alist
, &al
, sizeof(al
));
3382 return getattrlistat_internal(vfs_context_current(),
3383 CAST_USER_ADDR_T(uap
->path
), &al
,
3384 CAST_USER_ADDR_T(uap
->attributeBuffer
), uap
->bufferSize
,
3385 (uint64_t)uap
->options
, segflg
, segflg
, AT_FDCWD
);
3389 getattrlistat(proc_t p
, struct getattrlistat_args
*uap
, __unused
int32_t *retval
)
3391 enum uio_seg segflg
;
3395 segflg
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
3398 * Fetch the attribute request.
3400 error
= copyin(uap
->alist
, &al
, sizeof(al
));
3405 return getattrlistat_internal(vfs_context_current(),
3406 CAST_USER_ADDR_T(uap
->path
), &al
,
3407 CAST_USER_ADDR_T(uap
->attributeBuffer
), uap
->bufferSize
,
3408 (uint64_t)uap
->options
, segflg
, segflg
, uap
->fd
);
3412 * This refills the per-fd direntries cache by issuing a VNOP_READDIR.
3413 * It attempts to try and find a size the filesystem responds to, so
3414 * it first tries 1 direntry sized buffer and going from 1 to 2 to 4
3415 * direntry sized buffers to readdir. If the filesystem does not respond
3416 * to 4 * direntry it returns the error by the filesystem (if any) and sets
3419 * This function also tries again if the last "refill" returned an EOF
3420 * to try and get any additional entries if they were added after the last
3424 refill_fd_direntries(vfs_context_t ctx
, vnode_t dvp
, struct fd_vn_data
*fvd
,
3428 char uio_buf
[UIO_SIZEOF(1)];
3436 * If the last readdir returned EOF, don't try again.
3438 if (fvd
->fv_eofflag
) {
3441 FREE(fvd
->fv_buf
, M_FD_DIRBUF
);
3450 * If there is a cached allocation size of the dirbuf that should be
3451 * allocated, use that. Otherwise start with a allocation size of
3452 * FV_DIRBUF_START_SIZ. This start size may need to be increased if the
3453 * filesystem doesn't respond to the initial size.
3456 if (fvd
->fv_offset
&& fvd
->fv_bufallocsiz
) {
3457 rdirbufsiz
= fvd
->fv_bufallocsiz
;
3459 rdirbufsiz
= FV_DIRBUF_START_SIZ
;
3464 rdir_uio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3465 &uio_buf
[0], sizeof(uio_buf
));
3469 * Don't explicitly zero out this buffer since this is
3470 * not copied out to user space.
3473 MALLOC(fvd
->fv_buf
, caddr_t
, rdirbufsiz
, M_FD_DIRBUF
, M_WAITOK
);
3474 fvd
->fv_bufdone
= 0;
3477 uio_reset(rdir_uio
, fvd
->fv_eoff
, UIO_SYSSPACE
, UIO_READ
);
3478 uio_addiov(rdir_uio
, CAST_USER_ADDR_T(fvd
->fv_buf
), rdirbufsiz
);
3481 * Some filesystems do not set nentries or eofflag...
3485 error
= vnode_readdir64(dvp
, rdir_uio
, VNODE_READDIR_EXTENDED
,
3486 &eofflag
, &nentries
, ctx
);
3488 rdirbufused
= rdirbufsiz
- (size_t)uio_resid(rdir_uio
);
3490 if (!error
&& (rdirbufused
> 0) && (rdirbufused
<= rdirbufsiz
)) {
3492 fvd
->fv_soff
= fvd
->fv_eoff
;
3493 fvd
->fv_eoff
= uio_offset(rdir_uio
);
3494 /* Save eofflag state but don't return EOF for this time.*/
3495 fvd
->fv_eofflag
= eofflag
;
3497 /* Reset buffer parameters */
3498 fvd
->fv_bufsiz
= rdirbufused
;
3499 fvd
->fv_bufdone
= 0;
3500 bzero(fvd
->fv_buf
+ rdirbufused
, rdirbufsiz
- rdirbufused
);
3501 /* Cache allocation size the Filesystem responded to */
3502 fvd
->fv_bufallocsiz
= rdirbufsiz
;
3503 } else if (!eofflag
&& (rdirbufsiz
< FV_DIRBUF_MAX_SIZ
)) {
3505 * Some Filesystems have higher requirements for the
3506 * smallest buffer size they will respond to for a
3507 * directory listing. Start (relatively) small but increase
3508 * it upto FV_DIRBUF_MAX_SIZ. Most should be good with
3509 * 1*direntry. Cache the size found so that this does not need
3510 * need to be done every time. This also means that an error
3511 * from VNOP_READDIR is ignored until at least FV_DIRBUF_MAX_SIZ
3512 * has been attempted.
3514 FREE(fvd
->fv_buf
, M_FD_DIRBUF
);
3516 rdirbufsiz
= 2 * rdirbufsiz
;
3517 fvd
->fv_bufallocsiz
= 0;
3519 } else if (!error
) {
3521 * The Filesystem did not set eofflag but also did not
3522 * return any entries (or an error). It is presumed that
3523 * EOF has been reached.
3525 fvd
->fv_eofflag
= eofflag
= 1;
3529 * If the filesystem returned an error and it had previously returned
3530 * EOF, ignore the error and set EOF.
3532 if (error
&& fvd
->fv_eofflag
) {
3538 * If either the directory has either hit EOF or an error, now is a good
3539 * time to free up directory entry buffer.
3541 if ((error
|| eofflag
) && fvd
->fv_buf
) {
3542 FREE(fvd
->fv_buf
, M_FD_DIRBUF
);
3546 *eofflagp
= eofflag
;
3552 * gets the current direntry. To advance to the next direntry this has to be
3553 * paired with a direntry_done.
3555 * Since directories have restrictions on where directory enumeration
3556 * can restart from, entries are first read into* a per fd diectory entry
3557 * "cache" and entries provided from that cache.
3560 get_direntry(vfs_context_t ctx
, vnode_t dvp
, struct fd_vn_data
*fvd
,
3561 int *eofflagp
, struct direntry
**dpp
)
3569 if (!fvd
->fv_bufsiz
) {
3570 error
= refill_fd_direntries(ctx
, dvp
, fvd
, &eofflag
);
3575 *eofflagp
= eofflag
;
3580 *dpp
= (struct direntry
*)(fvd
->fv_buf
+ fvd
->fv_bufdone
);
3585 * Advances to the next direntry.
3588 direntry_done(struct fd_vn_data
*fvd
)
3590 struct direntry
*dp
;
3592 dp
= (struct direntry
*)(fvd
->fv_buf
+ fvd
->fv_bufdone
);
3594 fvd
->fv_bufdone
+= dp
->d_reclen
;
3595 if (fvd
->fv_bufdone
> fvd
->fv_bufsiz
) {
3596 fvd
->fv_bufdone
= fvd
->fv_bufsiz
;
3599 fvd
->fv_bufdone
= fvd
->fv_bufsiz
;
3603 * If we're at the end the fd direntries cache, reset the
3606 if (fvd
->fv_bufdone
== fvd
->fv_bufsiz
) {
3607 fvd
->fv_bufdone
= 0;
3613 * A stripped down version of getattrlist_internal to fill in only select
3614 * attributes in case of an error from getattrlist_internal.
3616 * It always returns at least ATTR_BULK_REQUIRED i.e. the name (but may also
3617 * return some other attributes which can be obtained from the vnode).
3619 * It does not change the value of the passed in attrlist.
3621 * The objective of this function is to fill in an "error entry", i.e.
3622 * an entry with ATTR_CMN_RETURNED_ATTRS & ATTR_CMN_NAME. If the caller
3623 * has also asked for ATTR_CMN_ERROR, it is filled in as well.
3626 * vp - vnode pointer
3627 * alp - pointer to attrlist struct.
3628 * options - options passed to getattrlistbulk(2)
3629 * kern_attr_buf - Kernel buffer to fill data (assumes offset 0 in
3631 * kern_attr_buf_siz - Size of buffer.
3632 * needs_error_attr - Whether the caller asked for ATTR_CMN_ERROR
3633 * error_attr - This value is used to fill ATTR_CMN_ERROR (if the user
3634 * has requested it in the attribute list.
3635 * namebuf - This is used to fill in the name.
3636 * ctx - vfs context of caller.
3639 get_error_attributes(vnode_t vp
, struct attrlist
*alp
, uint64_t options
,
3640 user_addr_t kern_attr_buf
, size_t kern_attr_buf_siz
, int error_attr
,
3641 caddr_t namebuf
, vfs_context_t ctx
)
3644 struct _attrlist_buf ab
;
3646 kauth_action_t action
;
3648 int needs_error_attr
= (alp
->commonattr
& ATTR_CMN_ERROR
);
3651 * To calculate fixed size required, in the FSOPT_PACK_INVAL_ATTRS case,
3652 * the fixedsize should include space for all the attributes asked by
3653 * the user. Only ATTR_BULK_REQUIRED (and ATTR_CMN_ERROR) will be filled
3654 * and will be valid. All other attributes are zeroed out later.
3656 * ATTR_CMN_RETURNED_ATTRS, ATTR_CMN_ERROR and ATTR_CMN_NAME
3657 * (the only valid ones being returned from here) happen to be
3658 * the first three attributes by order as well.
3661 if (!(options
& FSOPT_PACK_INVAL_ATTRS
)) {
3663 * In this case the fixedsize only needs to be only for the
3664 * attributes being actually returned.
3666 al
.commonattr
= ATTR_BULK_REQUIRED
;
3667 if (needs_error_attr
) {
3668 al
.commonattr
|= ATTR_CMN_ERROR
;
3675 * Passing NULL for the vnode_attr pointer is valid for
3676 * getattrlist_setupvattr. All that is required is the size.
3679 (void)getattrlist_setupvattr(&al
, NULL
, (ssize_t
*)&fsiz
,
3680 &action
, proc_is64bit(vfs_context_proc(ctx
)),
3681 (vnode_vtype(vp
) == VDIR
), (options
& FSOPT_ATTR_CMN_EXTENDED
));
3683 namelen
= strlen(namebuf
);
3685 vsiz
= ((vsiz
+ 3) & ~0x03);
3687 bzero(&ab
, sizeof(ab
));
3688 ab
.base
= (char *)kern_attr_buf
;
3689 ab
.needed
= fsiz
+ vsiz
;
3691 /* Fill in the size needed */
3692 *((uint32_t *)ab
.base
) = ab
.needed
;
3693 if (ab
.needed
> (ssize_t
)kern_attr_buf_siz
) {
3698 * Setup to pack results into the destination buffer.
3700 ab
.fixedcursor
= ab
.base
+ sizeof(uint32_t);
3702 * Zero out buffer, ab.fixedbuffer starts after the first uint32_t
3703 * which gives the length. This ensures everything that we don't
3704 * fill in explicitly later is zeroed out correctly.
3706 bzero(ab
.fixedcursor
, fsiz
);
3708 * variable size data should start after all the fixed
3711 ab
.varcursor
= ab
.base
+ fsiz
;
3713 * Initialise the value for ATTR_CMN_RETURNED_ATTRS and leave space
3714 * Leave space for filling in its value here at the end.
3716 bzero(&ab
.actual
, sizeof(ab
.actual
));
3717 ab
.fixedcursor
+= sizeof(attribute_set_t
);
3719 ab
.allocated
= ab
.needed
;
3721 /* Fill ATTR_CMN_ERROR (if asked for) */
3722 if (needs_error_attr
) {
3723 ATTR_PACK4(ab
, error_attr
);
3724 ab
.actual
.commonattr
|= ATTR_CMN_ERROR
;
3728 * Fill ATTR_CMN_NAME, The attrrefrence is packed at this location
3729 * but the actual string itself is packed after fixedsize which set
3730 * to different lengths based on whether FSOPT_PACK_INVAL_ATTRS
3733 attrlist_pack_string(&ab
, namebuf
, namelen
);
3736 * Now Fill in ATTR_CMN_RETURNED_ATTR. This copies to a
3737 * location after the count i.e. before ATTR_CMN_ERROR and
3740 ab
.actual
.commonattr
|= ATTR_CMN_NAME
| ATTR_CMN_RETURNED_ATTRS
;
3741 bcopy(&ab
.actual
, ab
.base
+ sizeof(uint32_t), sizeof(ab
.actual
));
3747 * This is the buffer size required to return at least 1 entry. We need space
3748 * for the length, for ATTR_CMN_RETURNED_ATTRS and ATTR_CMN_NAME. Assuming the
3749 * smallest filename of a single byte we get
3752 #define MIN_BUF_SIZE_REQUIRED (sizeof(uint32_t) + sizeof(attribute_set_t) +\
3753 sizeof(attrreference_t))
3756 * Read directory entries and get attributes filled in for each directory
3759 readdirattr(vnode_t dvp
, struct fd_vn_data
*fvd
, uio_t auio
,
3760 struct attrlist
*alp
, uint64_t options
, int *count
, int *eofflagp
,
3763 caddr_t kern_attr_buf
;
3764 size_t kern_attr_buf_siz
;
3765 caddr_t max_path_name_buf
= NULL
;
3771 if (uio_iovcnt(auio
) > 1) {
3776 * We fill in a kernel buffer for the attributes and uiomove each
3777 * entry's attributes (as returned by getattrlist_internal)
3779 kern_attr_buf_siz
= uio_resid(auio
);
3780 if (kern_attr_buf_siz
> ATTR_MAX_BUFFER
) {
3781 kern_attr_buf_siz
= ATTR_MAX_BUFFER
;
3782 } else if (kern_attr_buf_siz
== 0) {
3787 MALLOC(kern_attr_buf
, caddr_t
, kern_attr_buf_siz
, M_TEMP
, M_WAITOK
);
3789 while (uio_resid(auio
) > (user_ssize_t
)MIN_BUF_SIZE_REQUIRED
) {
3790 struct direntry
*dp
;
3791 user_addr_t name_buffer
;
3792 struct nameidata nd
;
3801 * get_direntry returns the current direntry and does not
3802 * advance. A move to the next direntry only happens if
3803 * direntry_done is called.
3805 error
= get_direntry(ctx
, dvp
, fvd
, eofflagp
, &dp
);
3806 if (error
|| (*eofflagp
) || !dp
) {
3811 * skip "." and ".." (and a bunch of other invalid conditions.)
3813 if (!dp
->d_reclen
|| dp
->d_ino
== 0 || dp
->d_namlen
== 0 ||
3814 (dp
->d_namlen
== 1 && dp
->d_name
[0] == '.') ||
3815 (dp
->d_namlen
== 2 && dp
->d_name
[0] == '.' &&
3816 dp
->d_name
[1] == '.')) {
3822 * try to deal with not-null terminated filenames.
3824 if (dp
->d_name
[dp
->d_namlen
] != '\0') {
3825 if (!max_path_name_buf
) {
3826 MALLOC(max_path_name_buf
, caddr_t
, MAXPATHLEN
,
3829 bcopy(dp
->d_name
, max_path_name_buf
, dp
->d_namlen
);
3830 max_path_name_buf
[dp
->d_namlen
] = '\0';
3831 name_buffer
= CAST_USER_ADDR_T(max_path_name_buf
);
3833 name_buffer
= CAST_USER_ADDR_T(&(dp
->d_name
));
3837 * We have an iocount on the directory already.
3839 * Note that we supply NOCROSSMOUNT to the namei call as we attempt to acquire
3840 * a vnode for this particular entry. This is because the native call will
3841 * (likely) attempt to emit attributes based on its own metadata in order to avoid
3842 * creating vnodes where posssible. If the native call is not going to walk
3843 * up the vnode mounted-on chain in order to find the top-most mount point, then we
3844 * should not either in this emulated readdir+getattrlist() approach. We
3845 * will be responsible for setting DIR_MNTSTATUS_MNTPOINT on that directory that
3846 * contains a mount point.
3848 NDINIT(&nd
, LOOKUP
, OP_GETATTR
, (AUDITVNPATH1
| USEDVP
| NOCROSSMOUNT
),
3849 UIO_SYSSPACE
, CAST_USER_ADDR_T(name_buffer
), ctx
);
3863 * getattrlist_internal can change the values of the
3864 * the required attribute list. Copy the current values
3865 * and use that one instead.
3869 error
= getattrlist_internal(ctx
, vp
, &al
,
3870 CAST_USER_ADDR_T(kern_attr_buf
), kern_attr_buf_siz
,
3871 options
| FSOPT_REPORT_FULLSIZE
, UIO_SYSSPACE
,
3872 CAST_DOWN_EXPLICIT(char *, name_buffer
),
3878 get_error_attributes(vp
, alp
, options
,
3879 CAST_USER_ADDR_T(kern_attr_buf
),
3880 kern_attr_buf_siz
, error
, (caddr_t
)name_buffer
,
3885 /* Done with vnode now */
3889 * Because FSOPT_REPORT_FULLSIZE was set, the first 4 bytes
3890 * of the buffer returned by getattrlist contains the size
3891 * (even if the provided buffer isn't sufficiently big). Use
3892 * that to check if we've run out of buffer space.
3894 * resid is a signed type, and the size of the buffer etc
3895 * are unsigned types. It is theoretically possible for
3896 * resid to be < 0 and in which case we would be assigning
3897 * an out of bounds value to bytes_left (which is unsigned)
3898 * uiomove takes care to not ever set resid to < 0, so it
3899 * is safe to do this here.
3901 bytes_left
= (size_t)((user_size_t
)uio_resid(auio
));
3902 entlen
= (size_t)(*((uint32_t *)(kern_attr_buf
)));
3903 if (!entlen
|| (entlen
> bytes_left
)) {
3908 * Will the pad bytes fit as well ? If they can't be, still use
3909 * this entry but this will be the last entry returned.
3911 pad_bytes
= ((entlen
+ 7) & ~0x07) - entlen
;
3913 if (pad_bytes
&& (entlen
+ pad_bytes
<= bytes_left
)) {
3915 * While entlen can never be > ATTR_MAX_BUFFER,
3916 * (entlen + pad_bytes) can be, handle that and
3917 * zero out the pad bytes. N.B. - Only zero
3918 * out information in the kernel buffer that is
3919 * going to be uiomove'ed out.
3921 if (entlen
+ pad_bytes
<= kern_attr_buf_siz
) {
3922 /* This is the normal case. */
3923 bzero(kern_attr_buf
+ entlen
, pad_bytes
);
3925 bzero(kern_attr_buf
+ entlen
,
3926 kern_attr_buf_siz
- entlen
);
3928 * Pad bytes left over, change the resid value
3929 * manually. We only got in here because
3930 * bytes_left >= entlen + pad_bytes so
3931 * new_resid (which is a signed type) is
3934 new_resid
= (ssize_t
)(bytes_left
-
3935 (entlen
+ pad_bytes
));
3937 entlen
+= pad_bytes
;
3939 *((uint32_t *)kern_attr_buf
) = (uint32_t)entlen
;
3940 error
= uiomove(kern_attr_buf
, min(entlen
, kern_attr_buf_siz
),
3948 uio_setresid(auio
, (user_ssize_t
)new_resid
);
3952 * At this point, the directory entry has been consumed, proceed
3959 if (max_path_name_buf
) {
3960 FREE(max_path_name_buf
, M_TEMP
);
3964 * At this point, kern_attr_buf is always allocated
3966 FREE(kern_attr_buf
, M_TEMP
);
3969 * Always set the offset to the last succesful offset
3970 * returned by VNOP_READDIR.
3972 uio_setoffset(auio
, fvd
->fv_eoff
);
3978 * int getattrlistbulk(int dirfd, struct attrlist *alist, void *attributeBuffer,
3979 * size_t bufferSize, uint64_t options)
3981 * Gets directory entries alongwith their attributes in the same way
3982 * getattrlist does for a single file system object.
3984 * On non error returns, retval will hold the count of entries returned.
3987 getattrlistbulk(proc_t p
, struct getattrlistbulk_args
*uap
, int32_t *retval
)
3990 vnode_t dvp
= NULLVP
;
3991 struct fileproc
*fp
;
3992 struct fd_vn_data
*fvdata
;
3995 enum uio_seg segflg
;
3998 char uio_buf
[UIO_SIZEOF(1)];
3999 kauth_action_t action
;
4006 error
= fp_getfvp(p
, uap
->dirfd
, &fp
, &dvp
);
4014 ctx
= vfs_context_current();
4015 ut
= get_bsdthread_info(current_thread());
4016 segflg
= IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
4018 if ((fp
->f_fglob
->fg_flag
& FREAD
) == 0) {
4020 * AUDIT_ARG(vnpath_withref, dvp, ARG_VNODE1);
4027 if ((error
= vnode_getwithref(dvp
))) {
4032 if (uap
->options
& FSOPT_LIST_SNAPSHOT
) {
4035 if (!vnode_isvroot(dvp
)) {
4040 /* switch directory to snapshot directory */
4041 error
= vnode_get_snapdir(dvp
, &snapdvp
, ctx
);
4049 if (dvp
->v_type
!= VDIR
) {
4055 error
= mac_file_check_change_offset(vfs_context_ucred(ctx
),
4062 * XXX : Audit Support
4063 * AUDIT_ARG(vnpath, dvp, ARG_VNODE1);
4066 options
= uap
->options
| FSOPT_ATTR_CMN_EXTENDED
;
4068 if ((error
= copyin(CAST_USER_ADDR_T(uap
->alist
), &al
,
4069 sizeof(struct attrlist
)))) {
4074 ((al
.commonattr
& ATTR_BULK_REQUIRED
) != ATTR_BULK_REQUIRED
)) {
4080 error
= mac_vnode_check_readdir(ctx
, dvp
);
4087 * If the only item requested is file names, we can let that past with
4088 * just LIST_DIRECTORY. If they want any other attributes, that means
4089 * they need SEARCH as well.
4091 action
= KAUTH_VNODE_LIST_DIRECTORY
;
4092 if ((al
.commonattr
& ~ATTR_CMN_NAME
) || al
.fileattr
|| al
.dirattr
) {
4093 action
|= KAUTH_VNODE_SEARCH
;
4096 error
= vnode_authorize(dvp
, NULL
, action
, ctx
);
4101 fvdata
= (struct fd_vn_data
*)fp
->f_fglob
->fg_vn_data
;
4103 panic("Directory expected to have fg_vn_data");
4109 * getattrlistbulk(2) maintains its offset in fv_offset. However
4110 * if the offset in the file glob is set (or reset) to 0, the directory
4111 * traversal needs to be restarted (Any existing state in the
4112 * directory buffer is removed as well).
4114 if (!fp
->f_fglob
->fg_offset
) {
4115 fvdata
->fv_offset
= 0;
4116 if (fvdata
->fv_buf
) {
4117 FREE(fvdata
->fv_buf
, M_FD_DIRBUF
);
4119 fvdata
->fv_buf
= NULL
;
4120 fvdata
->fv_bufsiz
= 0;
4121 fvdata
->fv_bufdone
= 0;
4122 fvdata
->fv_soff
= 0;
4123 fvdata
->fv_eoff
= 0;
4124 fvdata
->fv_eofflag
= 0;
4127 auio
= uio_createwithbuffer(1, fvdata
->fv_offset
, segflg
, UIO_READ
,
4128 &uio_buf
[0], sizeof(uio_buf
));
4129 uio_addiov(auio
, uap
->attributeBuffer
, (user_size_t
)uap
->bufferSize
);
4132 * For "expensive" operations in which the native VNOP implementations
4133 * end up having to do just as much (if not more) work than the default
4134 * implementation, fall back to the default implementation.
4135 * The VNOP helper functions depend on the filesystem providing the
4136 * object type, if the caller has not requested ATTR_CMN_OBJTYPE, fall
4137 * back to the default implementation.
4139 if ((al
.commonattr
&
4140 (ATTR_CMN_UUID
| ATTR_CMN_GRPUUID
| ATTR_CMN_EXTENDED_SECURITY
)) ||
4141 !(al
.commonattr
& ATTR_CMN_OBJTYPE
)) {
4144 struct vnode_attr va
;
4147 if (fvdata
->fv_eofflag
&& !fvdata
->fv_buf
) {
4149 * If the last successful VNOP_GETATTRLISTBULK or
4150 * VNOP_READDIR returned EOF, don't try again.
4160 MALLOC(va_name
, char *, MAXPATHLEN
, M_TEMP
,
4162 va
.va_name
= va_name
;
4164 (void)getattrlist_setupvattr_all(&al
, &va
, VNON
, NULL
,
4165 IS_64BIT_PROCESS(p
), (uap
->options
& FSOPT_ATTR_CMN_EXTENDED
));
4168 * Set UT_KERN_RAGE_VNODES to cause all vnodes created by the
4169 * filesystem to be rapidly aged.
4171 ut
->uu_flag
|= UT_KERN_RAGE_VNODES
;
4172 error
= VNOP_GETATTRLISTBULK(dvp
, &al
, &va
, auio
, NULL
,
4173 options
, &eofflag
, &count
, ctx
);
4174 ut
->uu_flag
&= ~UT_KERN_RAGE_VNODES
;
4176 FREE(va_name
, M_TEMP
);
4179 * cache state of eofflag.
4182 fvdata
->fv_eofflag
= eofflag
;
4188 * If the Filessytem does not natively support getattrlistbulk,
4189 * do the default implementation.
4191 if (error
== ENOTSUP
) {
4195 ut
->uu_flag
|= UT_KERN_RAGE_VNODES
;
4196 error
= readdirattr(dvp
, fvdata
, auio
, &al
, options
,
4197 &count
, &eofflag
, ctx
);
4198 ut
->uu_flag
&= ~UT_KERN_RAGE_VNODES
;
4202 fvdata
->fv_offset
= uio_offset(auio
);
4203 fp
->f_fglob
->fg_offset
= fvdata
->fv_offset
;
4206 } else if (!error
&& !eofflag
) {
4208 * This just means the buffer was too small to fit even a
4220 file_drop(uap
->dirfd
);
4226 attrlist_unpack_fixed(char **cursor
, char *end
, void *buf
, ssize_t size
)
4228 /* make sure we have enough source data */
4229 if ((*cursor
) + size
> end
) {
4233 bcopy(*cursor
, buf
, size
);
4238 #define ATTR_UNPACK(v) do {if ((error = attrlist_unpack_fixed(&cursor, bufend, &v, sizeof(v))) != 0) goto out;} while(0);
4239 #define ATTR_UNPACK_CAST(t, v) do { t _f; ATTR_UNPACK(_f); v = _f;} while(0)
4240 #define ATTR_UNPACK_TIME(v, is64) \
4243 struct user64_timespec us; \
4245 v.tv_sec = us.tv_sec; \
4246 v.tv_nsec = us.tv_nsec; \
4248 struct user32_timespec us; \
4250 v.tv_sec = us.tv_sec; \
4251 v.tv_nsec = us.tv_nsec; \
4260 setattrlist_internal(vnode_t vp
, struct setattrlist_args
*uap
, proc_t p
, vfs_context_t ctx
)
4263 struct vnode_attr va
;
4264 struct attrreference ar
;
4265 kauth_action_t action
;
4266 char *user_buf
, *cursor
, *bufend
, *fndrinfo
, *cp
, *volname
;
4267 int proc_is64
, error
;
4269 kauth_filesec_t rfsec
;
4275 proc_is64
= proc_is64bit(p
);
4279 * Fetch the attribute set and validate.
4281 if ((error
= copyin(uap
->alist
, (caddr_t
) &al
, sizeof(al
)))) {
4284 if (al
.bitmapcount
!= ATTR_BIT_MAP_COUNT
) {
4289 #if DEVELOPMENT || DEBUG
4291 * XXX VSWAP: Check for entitlements or special flag here
4292 * so we can restrict access appropriately.
4294 #else /* DEVELOPMENT || DEBUG */
4296 if (vnode_isswap(vp
) && (ctx
!= vfs_context_kernel())) {
4300 #endif /* DEVELOPMENT || DEBUG */
4302 VFS_DEBUG(ctx
, vp
, "%p ATTRLIST - %s set common %08x vol %08x file %08x dir %08x fork %08x %sfollow on '%s'",
4303 vp
, p
->p_comm
, al
.commonattr
, al
.volattr
, al
.fileattr
, al
.dirattr
, al
.forkattr
,
4304 (uap
->options
& FSOPT_NOFOLLOW
) ? "no":"", vp
->v_name
);
4307 if ((al
.volattr
& ~ATTR_VOL_SETMASK
) ||
4308 (al
.commonattr
& ~ATTR_CMN_VOLSETMASK
) ||
4312 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: attempt to set invalid volume attributes");
4316 if ((al
.commonattr
& ~ATTR_CMN_SETMASK
) ||
4317 (al
.fileattr
& ~ATTR_FILE_SETMASK
) ||
4318 (al
.dirattr
& ~ATTR_DIR_SETMASK
) ||
4319 (al
.forkattr
& ~ATTR_FORK_SETMASK
)) {
4321 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: attempt to set invalid file/folder attributes");
4327 * If the caller's bitmaps indicate that there are no attributes to set,
4328 * then exit early. In particular, we want to avoid the MALLOC below
4329 * since the caller's bufferSize could be zero, and MALLOC of zero bytes
4330 * returns a NULL pointer, which would cause setattrlist to return ENOMEM.
4332 if (al
.commonattr
== 0 &&
4333 (al
.volattr
& ~ATTR_VOL_INFO
) == 0 &&
4342 * Make the naive assumption that the caller has supplied a reasonable buffer
4343 * size. We could be more careful by pulling in the fixed-size region, checking
4344 * the attrref structures, then pulling in the variable section.
4345 * We need to reconsider this for handling large ACLs, as they should probably be
4346 * brought directly into a buffer. Multiple copyins will make this slower though.
4348 * We could also map the user buffer if it is larger than some sensible mimimum.
4350 if (uap
->bufferSize
> ATTR_MAX_BUFFER
) {
4351 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: buffer size %d too large", uap
->bufferSize
);
4355 MALLOC(user_buf
, char *, uap
->bufferSize
, M_TEMP
, M_WAITOK
);
4356 if (user_buf
== NULL
) {
4357 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not allocate %d bytes for buffer", uap
->bufferSize
);
4361 if ((error
= copyin(uap
->attributeBuffer
, user_buf
, uap
->bufferSize
)) != 0) {
4362 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: buffer copyin failed");
4365 VFS_DEBUG(ctx
, vp
, "ATTRLIST - copied in %d bytes of user attributes to %p", uap
->bufferSize
, user_buf
);
4368 error
= mac_vnode_check_setattrlist(ctx
, vp
, &al
);
4375 * Unpack the argument buffer.
4378 bufend
= cursor
+ uap
->bufferSize
;
4381 if (al
.commonattr
& ATTR_CMN_SCRIPT
) {
4382 ATTR_UNPACK(va
.va_encoding
);
4383 VATTR_SET_ACTIVE(&va
, va_encoding
);
4385 if (al
.commonattr
& ATTR_CMN_CRTIME
) {
4386 ATTR_UNPACK_TIME(va
.va_create_time
, proc_is64
);
4387 VATTR_SET_ACTIVE(&va
, va_create_time
);
4389 if (al
.commonattr
& ATTR_CMN_MODTIME
) {
4390 ATTR_UNPACK_TIME(va
.va_modify_time
, proc_is64
);
4391 VATTR_SET_ACTIVE(&va
, va_modify_time
);
4393 if (al
.commonattr
& ATTR_CMN_CHGTIME
) {
4394 ATTR_UNPACK_TIME(va
.va_change_time
, proc_is64
);
4395 al
.commonattr
&= ~ATTR_CMN_CHGTIME
;
4396 /*quietly ignore change time; advisory in man page*/
4398 if (al
.commonattr
& ATTR_CMN_ACCTIME
) {
4399 ATTR_UNPACK_TIME(va
.va_access_time
, proc_is64
);
4400 VATTR_SET_ACTIVE(&va
, va_access_time
);
4402 if (al
.commonattr
& ATTR_CMN_BKUPTIME
) {
4403 ATTR_UNPACK_TIME(va
.va_backup_time
, proc_is64
);
4404 VATTR_SET_ACTIVE(&va
, va_backup_time
);
4406 if (al
.commonattr
& ATTR_CMN_FNDRINFO
) {
4407 if ((cursor
+ 32) > bufend
) {
4409 VFS_DEBUG(ctx
, vp
, "ATTRLIST - not enough data supplied for FINDERINFO");
4415 if (al
.commonattr
& ATTR_CMN_OWNERID
) {
4416 ATTR_UNPACK(va
.va_uid
);
4417 VATTR_SET_ACTIVE(&va
, va_uid
);
4419 if (al
.commonattr
& ATTR_CMN_GRPID
) {
4420 ATTR_UNPACK(va
.va_gid
);
4421 VATTR_SET_ACTIVE(&va
, va_gid
);
4423 if (al
.commonattr
& ATTR_CMN_ACCESSMASK
) {
4424 ATTR_UNPACK_CAST(uint32_t, va
.va_mode
);
4425 VATTR_SET_ACTIVE(&va
, va_mode
);
4427 if (al
.commonattr
& ATTR_CMN_FLAGS
) {
4428 ATTR_UNPACK(va
.va_flags
);
4429 VATTR_SET_ACTIVE(&va
, va_flags
);
4431 if ((error
= mac_vnode_check_setflags(ctx
, vp
, va
.va_flags
)) != 0) {
4436 if (al
.commonattr
& ATTR_CMN_EXTENDED_SECURITY
) {
4438 * We are (for now) passed a kauth_filesec_t, but all we want from
4443 if (ar
.attr_dataoffset
< 0) {
4444 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: bad offset supplied", ar
.attr_dataoffset
);
4449 cp
+= ar
.attr_dataoffset
;
4450 rfsec
= (kauth_filesec_t
)cp
;
4451 if (((((char *)rfsec
) + KAUTH_FILESEC_SIZE(0)) > bufend
) || /* no space for acl */
4452 (rfsec
->fsec_magic
!= KAUTH_FILESEC_MAGIC
) || /* bad magic */
4453 (KAUTH_FILESEC_COPYSIZE(rfsec
) != ar
.attr_length
) || /* size does not match */
4454 ((cp
+ KAUTH_FILESEC_COPYSIZE(rfsec
)) > bufend
)) { /* ACEs overrun buffer */
4456 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: bad ACL supplied", ar
.attr_length
);
4459 nace
= rfsec
->fsec_entrycount
;
4460 if (nace
== KAUTH_FILESEC_NOACL
) {
4463 if (nace
> KAUTH_ACL_MAX_ENTRIES
) { /* ACL size invalid */
4465 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: bad ACL supplied");
4468 nace
= rfsec
->fsec_acl
.acl_entrycount
;
4469 if (nace
== KAUTH_FILESEC_NOACL
) {
4471 VATTR_SET(&va
, va_acl
, NULL
);
4473 if (nace
> KAUTH_ACL_MAX_ENTRIES
) { /* ACL size invalid */
4475 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: supplied ACL is too large");
4478 VATTR_SET(&va
, va_acl
, &rfsec
->fsec_acl
);
4481 if (al
.commonattr
& ATTR_CMN_UUID
) {
4482 ATTR_UNPACK(va
.va_uuuid
);
4483 VATTR_SET_ACTIVE(&va
, va_uuuid
);
4485 if (al
.commonattr
& ATTR_CMN_GRPUUID
) {
4486 ATTR_UNPACK(va
.va_guuid
);
4487 VATTR_SET_ACTIVE(&va
, va_guuid
);
4489 if (al
.commonattr
& ATTR_CMN_ADDEDTIME
) {
4490 ATTR_UNPACK_TIME(va
.va_addedtime
, proc_is64
);
4491 VATTR_SET_ACTIVE(&va
, va_addedtime
);
4493 /* Support setattrlist of data protection class */
4494 if (al
.commonattr
& ATTR_CMN_DATA_PROTECT_FLAGS
) {
4495 ATTR_UNPACK(va
.va_dataprotect_class
);
4496 VATTR_SET_ACTIVE(&va
, va_dataprotect_class
);
4500 if (al
.volattr
& ATTR_VOL_INFO
) {
4501 if (al
.volattr
& ATTR_VOL_NAME
) {
4504 /* attr_length cannot be 0! */
4505 if ((ar
.attr_dataoffset
< 0) || (ar
.attr_length
== 0) ||
4506 (ar
.attr_length
> uap
->bufferSize
) ||
4507 (uap
->bufferSize
- ar
.attr_length
< (unsigned)ar
.attr_dataoffset
)) {
4508 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: bad offset supplied (2) ", ar
.attr_dataoffset
);
4513 if (volname
>= bufend
- ar
.attr_dataoffset
- ar
.attr_length
) {
4515 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: volume name too big for caller buffer");
4518 volname
+= ar
.attr_dataoffset
;
4519 /* guarantee NUL termination */
4520 volname
[ar
.attr_length
- 1] = 0;
4525 if (al
.fileattr
& ATTR_FILE_DEVTYPE
) {
4526 /* XXX does it actually make any sense to change this? */
4528 VFS_DEBUG(ctx
, vp
, "ATTRLIST - XXX device type change not implemented");
4533 * Validate and authorize.
4536 if ((va
.va_active
!= 0LL) && ((error
= vnode_authattr(vp
, &va
, &action
, ctx
)) != 0)) {
4537 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: attribute changes refused: %d", error
);
4541 * We can auth file Finder Info here. HFS volume FinderInfo is really boot data,
4542 * and will be auth'ed by the FS.
4544 if (fndrinfo
!= NULL
) {
4545 if (al
.volattr
& ATTR_VOL_INFO
) {
4546 if (vp
->v_tag
!= VT_HFS
) {
4551 action
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
4555 if ((action
!= 0) && ((error
= vnode_authorize(vp
, NULL
, action
, ctx
)) != 0)) {
4556 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: authorization failed");
4561 * When we're setting both the access mask and the finder info, then
4562 * check if were about to remove write access for the owner. Since
4563 * vnode_setattr and vn_setxattr invoke two separate vnops, we need
4564 * to consider their ordering.
4566 * If were about to remove write access for the owner we'll set the
4567 * Finder Info here before vnode_setattr. Otherwise we'll set it
4568 * after vnode_setattr since it may be adding owner write access.
4570 if ((fndrinfo
!= NULL
) && !(al
.volattr
& ATTR_VOL_INFO
) &&
4571 (al
.commonattr
& ATTR_CMN_ACCESSMASK
) && !(va
.va_mode
& S_IWUSR
)) {
4572 if ((error
= setattrlist_setfinderinfo(vp
, fndrinfo
, ctx
)) != 0) {
4575 fndrinfo
= NULL
; /* it was set here so skip setting below */
4579 * Write the attributes if we have any.
4581 if ((va
.va_active
!= 0LL) && ((error
= vnode_setattr(vp
, &va
, ctx
)) != 0)) {
4582 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: filesystem returned %d", error
);
4587 mac_vnode_notify_setattrlist(ctx
, vp
, &al
);
4588 if (VATTR_IS_ACTIVE(&va
, va_flags
)) {
4589 mac_vnode_notify_setflags(ctx
, vp
, va
.va_flags
);
4594 * Write the Finder Info if we have any.
4596 if (fndrinfo
!= NULL
) {
4597 if (al
.volattr
& ATTR_VOL_INFO
) {
4598 if (vp
->v_tag
== VT_HFS
) {
4599 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
4600 error
= VNOP_IOCTL(vp
, HFS_SET_BOOT_INFO
, (caddr_t
)fndrinfo
, 0, ctx
);
4605 /* XXX should never get here */
4607 } else if ((error
= setattrlist_setfinderinfo(vp
, fndrinfo
, ctx
)) != 0) {
4613 * Set the volume name, if we have one
4615 if (volname
!= NULL
) {
4620 vs
.f_vol_name
= volname
; /* References the setattrlist buffer directly */
4621 VFSATTR_WANTED(&vs
, f_vol_name
);
4624 error
= mac_mount_check_setattr(ctx
, vp
->v_mount
, &vs
);
4630 if ((error
= vfs_setattr(vp
->v_mount
, &vs
, ctx
)) != 0) {
4631 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: setting volume name failed");
4635 if (!VFSATTR_ALL_SUPPORTED(&vs
)) {
4637 VFS_DEBUG(ctx
, vp
, "ATTRLIST - ERROR: could not set volume name");
4642 /* all done and successful */
4645 if (user_buf
!= NULL
) {
4646 FREE(user_buf
, M_TEMP
);
4648 VFS_DEBUG(ctx
, vp
, "ATTRLIST - set returning %d", error
);
4653 setattrlist(proc_t p
, struct setattrlist_args
*uap
, __unused
int32_t *retval
)
4655 struct vfs_context
*ctx
;
4656 struct nameidata nd
;
4661 ctx
= vfs_context_current();
4666 nameiflags
= AUDITVNPATH1
;
4667 if ((uap
->options
& FSOPT_NOFOLLOW
) == 0) {
4668 nameiflags
|= FOLLOW
;
4670 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, nameiflags
, UIO_USERSPACE
, uap
->path
, ctx
);
4671 if ((error
= namei(&nd
)) != 0) {
4677 error
= setattrlist_internal(vp
, uap
, p
, ctx
);
4686 setattrlistat(proc_t p
, struct setattrlistat_args
*uap
, __unused
int32_t *retval
)
4688 struct setattrlist_args ap
;
4689 struct vfs_context
*ctx
;
4690 struct nameidata nd
;
4691 vnode_t vp
= NULLVP
;
4692 uint32_t nameiflags
;
4695 ctx
= vfs_context_current();
4697 AUDIT_ARG(fd
, uap
->fd
);
4701 nameiflags
= AUDITVNPATH1
;
4702 if (!(uap
->options
& FSOPT_NOFOLLOW
)) {
4703 nameiflags
|= FOLLOW
;
4705 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, nameiflags
, UIO_USERSPACE
, uap
->path
, ctx
);
4706 if ((error
= nameiat(&nd
, uap
->fd
)) != 0) {
4713 ap
.alist
= uap
->alist
;
4714 ap
.attributeBuffer
= uap
->attributeBuffer
;
4715 ap
.bufferSize
= uap
->bufferSize
;
4716 ap
.options
= uap
->options
;
4718 error
= setattrlist_internal(vp
, &ap
, p
, ctx
);
4727 fsetattrlist(proc_t p
, struct fsetattrlist_args
*uap
, __unused
int32_t *retval
)
4729 struct vfs_context
*ctx
;
4732 struct setattrlist_args ap
;
4734 ctx
= vfs_context_current();
4736 if ((error
= file_vnode(uap
->fd
, &vp
)) != 0) {
4740 if ((error
= vnode_getwithref(vp
)) != 0) {
4746 ap
.alist
= uap
->alist
;
4747 ap
.attributeBuffer
= uap
->attributeBuffer
;
4748 ap
.bufferSize
= uap
->bufferSize
;
4749 ap
.options
= uap
->options
;
4751 error
= setattrlist_internal(vp
, &ap
, p
, ctx
);