2 * Copyright (c) 2000-2017 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 #include <libkern/OSAtomic.h>
31 #include <sys/systm.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/dirent.h>
37 #include <sys/mount.h>
38 #include <sys/vnode_if.h>
39 #include <sys/malloc.h>
41 #include <sys/paths.h>
42 #include <sys/quota.h>
45 #include <sys/kauth.h>
46 #include <sys/fsctl.h>
47 #include <sys/xattr.h>
48 #include <sys/decmpfs.h>
50 #include <sys/doc_tombstone.h>
51 #include <sys/namei.h>
53 #include <sys/fsevents.h>
55 #include <miscfs/specfs/specdev.h>
56 #include <miscfs/fifofs/fifo.h>
57 #include <vfs/vfs_support.h>
59 #include <sys/kdebug.h>
60 #include <sys/sysctl.h>
64 #include "hfs_catalog.h"
65 #include "hfs_cnode.h"
67 #include "hfs_mount.h"
68 #include "hfs_quota.h"
69 #include "hfs_endian.h"
70 #include "hfs_kdebug.h"
71 #include "hfs_cprotect.h"
73 #if HFS_CONFIG_KEY_ROLL
74 #include "hfs_key_roll.h"
77 #include "BTreesInternal.h"
78 #include "FileMgrInternal.h"
80 /* Global vfs data structures for hfs */
83 * Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is
84 * 'no'). At some point this might need to move into VFS and we might
85 * need to provide an API to get at it, but for now, this is only used
88 int always_do_fullfsync
= 0;
89 SYSCTL_DECL(_vfs_generic
);
90 HFS_SYSCTL(INT
, _vfs_generic
, OID_AUTO
, always_do_fullfsync
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &always_do_fullfsync
, 0, "always F_FULLFSYNC when fsync is called")
92 int hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
,
93 struct componentname
*cnp
, struct vnode_attr
*vap
,
95 int hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
);
96 int hfs_metasync_all(struct hfsmount
*hfsmp
);
98 int hfs_removedir(struct vnode
*, struct vnode
*, struct componentname
*,
100 int hfs_removefile(struct vnode
*, struct vnode
*, struct componentname
*,
101 int, int, int, struct vnode
*, int);
103 /* Used here and in cnode teardown -- for symlinks */
104 int hfs_removefile_callback(struct buf
*bp
, void *hfsmp
);
107 HFS_MOVE_DATA_INCLUDE_RSRC
= 1,
109 typedef uint32_t hfs_move_data_options_t
;
111 static int hfs_move_data(cnode_t
*from_cp
, cnode_t
*to_cp
,
112 hfs_move_data_options_t options
);
113 static int hfs_move_fork(filefork_t
*srcfork
, cnode_t
*src
,
114 filefork_t
*dstfork
, cnode_t
*dst
);
117 static int hfs_exchangedata_getxattr (struct vnode
*vp
, uint32_t name_selector
, void **buffer
, size_t *xattr_size
);
118 static int hfs_exchangedata_setxattr (struct hfsmount
*hfsmp
, uint32_t fileid
,
119 uint32_t name_selector
, void *buffer
, size_t xattr_size
);
121 enum XATTR_NAME_ENTRIES
{
123 MAX_NUM_XATTR_NAMES
//must be last
127 /* These are special EAs that follow the content in exchangedata(2). */
128 const char *XATTR_NAMES
[MAX_NUM_XATTR_NAMES
] = { "com.apple.quarantine" };
130 #define MAX_EXCHANGE_EA_SIZE 4096
133 static int hfs_move_compressed(cnode_t
*from_vp
, cnode_t
*to_vp
);
136 decmpfs_cnode
* hfs_lazy_init_decmpfs_cnode (struct cnode
*cp
);
139 static int hfsfifo_read(struct vnop_read_args
*);
140 static int hfsfifo_write(struct vnop_write_args
*);
141 static int hfsfifo_close(struct vnop_close_args
*);
143 extern int (**fifo_vnodeop_p
)(void *);
146 int hfs_vnop_close(struct vnop_close_args
*);
147 int hfs_vnop_exchange(struct vnop_exchange_args
*);
148 int hfs_vnop_fsync(struct vnop_fsync_args
*);
149 int hfs_vnop_mkdir(struct vnop_mkdir_args
*);
150 int hfs_vnop_mknod(struct vnop_mknod_args
*);
151 int hfs_vnop_getattr(struct vnop_getattr_args
*);
152 int hfs_vnop_open(struct vnop_open_args
*);
153 int hfs_vnop_readdir(struct vnop_readdir_args
*);
154 int hfs_vnop_rename(struct vnop_rename_args
*);
155 int hfs_vnop_renamex(struct vnop_renamex_args
*);
156 int hfs_vnop_rmdir(struct vnop_rmdir_args
*);
157 int hfs_vnop_symlink(struct vnop_symlink_args
*);
158 int hfs_vnop_setattr(struct vnop_setattr_args
*);
159 int hfs_vnop_readlink(struct vnop_readlink_args
*);
160 int hfs_vnop_pathconf(struct vnop_pathconf_args
*);
161 int hfs_vnop_mmap(struct vnop_mmap_args
*ap
);
162 int hfsspec_read(struct vnop_read_args
*);
163 int hfsspec_write(struct vnop_write_args
*);
164 int hfsspec_close(struct vnop_close_args
*);
166 /* Options for hfs_removedir and hfs_removefile */
167 #define HFSRM_SKIP_RESERVE 0x01
171 /*****************************************************************************
173 * Common Operations on vnodes
175 *****************************************************************************/
178 * Is the given cnode either the .journal or .journal_info_block file on
179 * a volume with an active journal? Many VNOPs use this to deny access
182 * Note: the .journal file on a volume with an external journal still
183 * returns true here, even though it does not actually hold the contents
184 * of the volume's journal.
187 hfs_is_journal_file(struct hfsmount
*hfsmp
, struct cnode
*cp
)
189 if (hfsmp
->jnl
!= NULL
&&
190 (cp
->c_fileid
== hfsmp
->hfs_jnlinfoblkid
||
191 cp
->c_fileid
== hfsmp
->hfs_jnlfileid
)) {
199 * Create a regular file.
202 hfs_vnop_create(struct vnop_create_args
*ap
)
205 * We leave handling of certain race conditions here to the caller
206 * which will have a better understanding of the semantics it
207 * requires. For example, if it turns out that the file exists,
208 * it would be wrong of us to return a reference to the existing
209 * file because the caller might not want that and it would be
210 * misleading to suggest the file had been created when it hadn't
211 * been. Note that our NFS server code does not set the
212 * VA_EXCLUSIVE flag so you cannot assume that callers don't want
213 * EEXIST errors if it's not set. The common case, where users
214 * are calling open with the O_CREAT mode, is handled in VFS; when
215 * we return EEXIST, it will loop and do the look-up again.
217 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
221 * Make device special file.
224 hfs_vnop_mknod(struct vnop_mknod_args
*ap
)
226 struct vnode_attr
*vap
= ap
->a_vap
;
227 struct vnode
*dvp
= ap
->a_dvp
;
228 struct vnode
**vpp
= ap
->a_vpp
;
232 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
236 /* Create the vnode */
237 error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, vap
, ap
->a_context
);
242 cp
->c_touch_acctime
= TRUE
;
243 cp
->c_touch_chgtime
= TRUE
;
244 cp
->c_touch_modtime
= TRUE
;
246 if ((vap
->va_rdev
!= VNOVAL
) &&
247 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
))
248 cp
->c_rdev
= vap
->va_rdev
;
255 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
256 * In the (hopefully rare) case where the data fork vnode is not
257 * present, it will use hfs_vget() to create a new vnode for the
260 * NOTE: If successful and a vnode is returned, the caller is responsible
261 * for releasing the returned vnode with vnode_rele().
264 hfs_ref_data_vp(struct cnode
*cp
, struct vnode
**data_vp
, int skiplock
)
268 if (!data_vp
|| !cp
) /* sanity check incoming parameters */
271 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
273 if (!skiplock
) hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
274 struct vnode
*c_vp
= cp
->c_vp
;
276 /* we already have a data vnode */
278 vref
= vnode_ref(*data_vp
);
279 if (!skiplock
) hfs_unlock(cp
);
285 /* no data fork vnode in the cnode, so ask hfs for one. */
287 if (!cp
->c_rsrc_vp
) {
288 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
290 if (!skiplock
) hfs_unlock(cp
);
294 if (0 == hfs_vget(VTOHFS(cp
->c_rsrc_vp
), cp
->c_cnid
, data_vp
, 1, 0) &&
296 vref
= vnode_ref(*data_vp
);
298 if (!skiplock
) hfs_unlock(cp
);
304 /* there was an error getting the vnode */
306 if (!skiplock
) hfs_unlock(cp
);
311 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
312 * allocating it if necessary; returns NULL if there was an allocation error.
313 * function is non-static so that it can be used from the FCNTL handler.
316 hfs_lazy_init_decmpfs_cnode(struct cnode
*cp
)
319 decmpfs_cnode
*dp
= decmpfs_cnode_alloc();
320 decmpfs_cnode_init(dp
);
321 if (!OSCompareAndSwapPtr(NULL
, dp
, (void * volatile *)&cp
->c_decmp
)) {
322 /* another thread got here first, so free the decmpfs_cnode we allocated */
323 decmpfs_cnode_destroy(dp
);
324 decmpfs_cnode_free(dp
);
332 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
333 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
334 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
335 * or else fills it in via the decmpfs_file_is_compressed() function.
338 hfs_file_is_compressed(struct cnode
*cp
, int skiplock
)
342 /* fast check to see if file is compressed. If flag is clear, just answer no */
343 if (!(cp
->c_bsdflags
& UF_COMPRESSED
)) {
347 decmpfs_cnode
*dp
= hfs_lazy_init_decmpfs_cnode(cp
);
349 /* error allocating a decmpfs cnode, treat the file as uncompressed */
353 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
354 uint32_t decmpfs_state
= decmpfs_cnode_get_vnode_state(dp
);
355 switch(decmpfs_state
) {
356 case FILE_IS_COMPRESSED
:
357 case FILE_IS_CONVERTING
: /* treat decompressing files as if they are compressed */
359 case FILE_IS_NOT_COMPRESSED
:
361 /* otherwise the state is not cached yet */
364 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
365 struct vnode
*data_vp
= NULL
;
366 if (0 == hfs_ref_data_vp(cp
, &data_vp
, skiplock
)) {
368 ret
= decmpfs_file_is_compressed(data_vp
, VTOCMP(data_vp
)); // fill in decmpfs_cnode
375 /* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
376 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
377 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
378 * files size is returned in size (required)
379 * if the indicated file is a directory (or something that doesn't have a data fork), then this call
380 * will return an error and the caller should fall back to treating the item as an uncompressed file
383 hfs_uncompressed_size_of_compressed_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, cnid_t fid
, off_t
*size
, int skiplock
)
386 int putaway
= 0; /* flag to remember if we used hfs_vget() */
389 return EINVAL
; /* no place to put the file size */
393 if (!hfsmp
|| !fid
) { /* make sure we have the required parameters */
396 if (0 != hfs_vget(hfsmp
, fid
, &vp
, skiplock
, 0)) { /* vnode is null, use hfs_vget() to get it */
399 putaway
= 1; /* note that hfs_vget() was used to aquire the vnode */
402 /* this double check for compression (hfs_file_is_compressed)
403 * ensures the cached size is present in case decmpfs hasn't
404 * encountered this node yet.
407 if (hfs_file_is_compressed(VTOC(vp
), skiplock
) ) {
408 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
409 } else if (VTOCMP(vp
)) {
410 uint32_t cmp_type
= decmpfs_cnode_cmp_type(VTOCMP(vp
));
412 if (cmp_type
== DATALESS_CMPFS_TYPE
) {
413 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
415 } else if (cmp_type
>= CMP_MAX
&& VTOC(vp
)->c_datafork
) {
416 // if we don't recognize this type, just use the real data fork size
417 *size
= VTOC(vp
)->c_datafork
->ff_size
;
425 if (putaway
) { /* did we use hfs_vget() to get this vnode? */
426 vnode_put(vp
); /* if so, release it and set it to null */
433 hfs_hides_rsrc(vfs_context_t ctx
, struct cnode
*cp
, int skiplock
)
435 if (ctx
== decmpfs_ctx
)
437 if (!hfs_file_is_compressed(cp
, skiplock
))
439 return decmpfs_hides_rsrc(ctx
, cp
->c_decmp
);
443 hfs_hides_xattr(vfs_context_t ctx
, struct cnode
*cp
, const char *name
, int skiplock
)
445 if (ctx
== decmpfs_ctx
)
447 if (!hfs_file_is_compressed(cp
, skiplock
))
449 return decmpfs_hides_xattr(ctx
, cp
->c_decmp
, name
);
451 #endif /* HFS_COMPRESSION */
454 * Open a file/directory.
457 hfs_vnop_open(struct vnop_open_args
*ap
)
459 struct vnode
*vp
= ap
->a_vp
;
463 static int past_bootup
= 0;
464 struct cnode
*cp
= VTOC(vp
);
465 struct hfsmount
*hfsmp
= VTOHFS(vp
);
468 error
= cp_handle_open(vp
, ap
->a_mode
);
474 if (ap
->a_mode
& FWRITE
) {
476 if ( hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
477 /* opening a compressed file for write, so convert it to decompressed */
478 struct vnode
*data_vp
= NULL
;
479 error
= hfs_ref_data_vp(cp
, &data_vp
, 1); /* 1 == don't take the cnode lock */
482 error
= decmpfs_decompress_file(data_vp
, VTOCMP(data_vp
), -1, 1, 0);
493 if (hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
494 if (VNODE_IS_RSRC(vp
)) {
495 /* opening the resource fork of a compressed file, so nothing to do */
497 /* opening a compressed file for read, make sure it validates */
498 error
= decmpfs_validate_compressed_file(vp
, VTOCMP(vp
));
507 * Files marked append-only must be opened for appending.
509 if ((cp
->c_bsdflags
& APPEND
) && !vnode_isdir(vp
) &&
510 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
513 if (vnode_issystem(vp
))
514 return (EBUSY
); /* file is in use by the kernel */
516 /* Don't allow journal to be opened externally. */
517 if (hfs_is_journal_file(hfsmp
, cp
))
520 bool have_lock
= false;
523 if (ISSET(ap
->a_mode
, FENCRYPTED
) && cp
->c_cpentry
&& vnode_isreg(vp
)) {
524 bool have_trunc_lock
= false;
526 #if HFS_CONFIG_KEY_ROLL
530 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
532 hfs_unlock_truncate(cp
, 0);
538 if (cp
->c_cpentry
->cp_raw_open_count
+ 1
539 < cp
->c_cpentry
->cp_raw_open_count
) {
540 // Overflow; too many raw opens on this file
543 hfs_unlock_truncate(cp
, 0);
547 #if HFS_CONFIG_KEY_ROLL
548 if (cp_should_auto_roll(hfsmp
, cp
->c_cpentry
)) {
549 if (!have_trunc_lock
) {
551 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, 0);
552 have_trunc_lock
= true;
556 error
= hfs_key_roll_start(cp
);
559 hfs_unlock_truncate(cp
, 0);
566 hfs_unlock_truncate(cp
, 0);
568 ++cp
->c_cpentry
->cp_raw_open_count
;
572 if (ISSET(hfsmp
->hfs_flags
, HFS_READ_ONLY
)
575 || vnode_isnamedstream(vp
)
577 || !hfsmp
->jnl
|| vnode_isinuse(vp
, 0)) {
587 if (!have_lock
&& (error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
591 /* If we're going to write to the file, initialize quotas. */
592 if ((ap
->a_mode
& FWRITE
) && (hfsmp
->hfs_flags
& HFS_QUOTAS
))
593 (void)hfs_getinoquota(cp
);
597 * On the first (non-busy) open of a fragmented
598 * file attempt to de-frag it, if it's less than hfs_defrag_max bytes.
599 * That field is initially set to 20MB.
603 fp
->ff_extents
[7].blockCount
!= 0 &&
604 fp
->ff_size
<= hfsmp
->hfs_defrag_max
) {
609 * Wait until system bootup is done (3 min).
610 * And don't relocate a file that's been modified
611 * within the past minute -- this can lead to
615 if (hfsmp
->hfs_defrag_nowait
) {
616 /* If this is toggled, then issue the defrag if appropriate */
623 if (tv
.tv_sec
> (60*3)) {
629 if ((now
.tv_sec
- cp
->c_mtime
) > 60) {
633 if (past_bootup
&& no_mods
) {
634 (void) hfs_relocate(vp
, hfsmp
->nextAllocation
+ 4096,
635 vfs_context_ucred(ap
->a_context
),
636 vfs_context_proc(ap
->a_context
));
647 * Close a file/directory.
650 hfs_vnop_close(struct vnop_close_args
*ap
)
652 register struct vnode
*vp
= ap
->a_vp
;
653 register struct cnode
*cp
;
654 struct proc
*p
= vfs_context_proc(ap
->a_context
);
655 struct hfsmount
*hfsmp
;
657 int tooktrunclock
= 0;
660 if ( hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0)
666 if (cp
->c_cpentry
&& ISSET(ap
->a_fflag
, FENCRYPTED
) && vnode_isreg(vp
)) {
667 hfs_assert(cp
->c_cpentry
->cp_raw_open_count
> 0);
668 --cp
->c_cpentry
->cp_raw_open_count
;
673 * If the rsrc fork is a named stream, it can cause the data fork to
674 * stay around, preventing de-allocation of these blocks.
675 * Do checks for truncation on close. Purge extra extents if they exist.
676 * Make sure the vp is not a directory, and that it has a resource fork,
677 * and that resource fork is also a named stream.
680 if ((vnode_vtype(vp
) == VREG
) && (cp
->c_rsrc_vp
)
681 && (vnode_isnamedstream(cp
->c_rsrc_vp
))) {
684 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
686 * If there are extra blocks and there are only 2 refs on
687 * this vp (ourselves + rsrc fork holding ref on us), go ahead
688 * and try to truncate.
690 if ((blks
< VTOF(vp
)->ff_blocks
) && (!vnode_isinuse(vp
, 2))) {
691 // release cnode lock; must acquire truncate lock BEFORE cnode lock
694 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
697 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0) {
698 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
699 // bail out if we can't re-acquire cnode lock
702 // now re-test to make sure it's still valid
704 knownrefs
= 1 + vnode_isnamedstream(cp
->c_rsrc_vp
);
705 if (!vnode_isinuse(vp
, knownrefs
)){
706 // now we can truncate the file, if necessary
707 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
708 if (blks
< VTOF(vp
)->ff_blocks
){
709 (void) hfs_truncate(vp
, VTOF(vp
)->ff_size
, IO_NDELAY
,
718 // if we froze the fs and we're exiting, then "thaw" the fs
719 if (hfsmp
->hfs_freeze_state
== HFS_FROZEN
720 && hfsmp
->hfs_freezing_proc
== p
&& proc_exiting(p
)) {
724 busy
= vnode_isinuse(vp
, 1);
727 hfs_touchtimes(VTOHFS(vp
), cp
);
729 if (vnode_isdir(vp
)) {
730 hfs_reldirhints(cp
, busy
);
731 } else if (vnode_issystem(vp
) && !busy
) {
736 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
740 if (ap
->a_fflag
& FWASWRITTEN
) {
741 hfs_sync_ejectable(hfsmp
);
747 static bool hfs_should_generate_document_id(hfsmount_t
*hfsmp
, cnode_t
*cp
)
749 return (!ISSET(hfsmp
->hfs_flags
, HFS_READ_ONLY
)
750 && ISSET(cp
->c_bsdflags
, UF_TRACKED
)
751 && cp
->c_desc
.cd_cnid
!= kHFSRootFolderID
752 && (S_ISDIR(cp
->c_mode
) || S_ISREG(cp
->c_mode
) || S_ISLNK(cp
->c_mode
)));
756 * Get basic attributes.
759 hfs_vnop_getattr(struct vnop_getattr_args
*ap
)
761 #define VNODE_ATTR_TIMES \
762 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
763 #define VNODE_ATTR_AUTH \
764 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
765 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
767 struct vnode
*vp
= ap
->a_vp
;
768 struct vnode_attr
*vap
= ap
->a_vap
;
769 struct vnode
*rvp
= NULLVP
;
770 struct hfsmount
*hfsmp
;
778 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
781 off_t uncompressed_size
= -1;
782 if (VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_alloc
) || VATTR_IS_ACTIVE(vap
, va_data_alloc
) || VATTR_IS_ACTIVE(vap
, va_total_size
)) {
783 /* we only care about whether the file is compressed if asked for the uncompressed size */
784 if (VNODE_IS_RSRC(vp
)) {
785 /* if it's a resource fork, decmpfs may want us to hide the size */
786 hide_size
= hfs_hides_rsrc(ap
->a_context
, cp
, 0);
788 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
789 compressed
= hfs_file_is_compressed(cp
, 0);
791 if ((VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_size
))) {
792 // if it's compressed
793 if (compressed
|| (!VNODE_IS_RSRC(vp
) && cp
->c_decmp
&& decmpfs_cnode_cmp_type(cp
->c_decmp
) >= CMP_MAX
)) {
794 if (0 != hfs_uncompressed_size_of_compressed_file(NULL
, vp
, 0, &uncompressed_size
, 0)) {
795 /* failed to get the uncompressed size, we'll check for this later */
796 uncompressed_size
= -1;
798 // fake that it's compressed
807 * Shortcut for vnode_authorize path. Each of the attributes
808 * in this set is updated atomically so we don't need to take
809 * the cnode lock to access them.
811 if ((vap
->va_active
& ~VNODE_ATTR_AUTH
) == 0) {
812 /* Make sure file still exists. */
813 if (cp
->c_flag
& C_NOEXISTS
)
816 vap
->va_uid
= cp
->c_uid
;
817 vap
->va_gid
= cp
->c_gid
;
818 vap
->va_mode
= cp
->c_mode
;
819 vap
->va_flags
= cp
->c_bsdflags
;
820 vap
->va_supported
|= VNODE_ATTR_AUTH
& ~VNODE_ATTR_va_acl
;
822 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
823 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
824 VATTR_SET_SUPPORTED(vap
, va_acl
);
831 v_type
= vnode_vtype(vp
);
833 if (VATTR_IS_ACTIVE(vap
, va_document_id
)) {
834 uint32_t document_id
;
836 if (cp
->c_desc
.cd_cnid
== kHFSRootFolderID
)
837 document_id
= kHFSRootFolderID
;
840 * This is safe without a lock because we're just reading
841 * a 32 bit aligned integer which should be atomic on all
842 * platforms we support.
844 document_id
= hfs_get_document_id(cp
);
846 if (!document_id
&& hfs_should_generate_document_id(hfsmp
, cp
)) {
847 uint32_t new_document_id
;
849 error
= hfs_generate_document_id(hfsmp
, &new_document_id
);
853 error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
857 bool want_docid_fsevent
= false;
859 // Need to check again now that we have the lock
860 document_id
= hfs_get_document_id(cp
);
861 if (!document_id
&& hfs_should_generate_document_id(hfsmp
, cp
)) {
862 cp
->c_attr
.ca_finderextendeddirinfo
.document_id
= document_id
= new_document_id
;
863 want_docid_fsevent
= true;
864 SET(cp
->c_flag
, C_MODIFIED
);
869 if (want_docid_fsevent
) {
870 add_fsevent(FSE_DOCID_CHANGED
, ap
->a_context
,
871 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
872 FSE_ARG_INO
, (ino64_t
)0, // src inode #
873 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
874 FSE_ARG_INT32
, document_id
,
877 if (need_fsevent(FSE_STAT_CHANGED
, vp
)) {
878 add_fsevent(FSE_STAT_CHANGED
, ap
->a_context
,
879 FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
885 vap
->va_document_id
= document_id
;
886 VATTR_SET_SUPPORTED(vap
, va_document_id
);
890 * If time attributes are requested and we have cnode times
891 * that require updating, then acquire an exclusive lock on
892 * the cnode before updating the times. Otherwise we can
893 * just acquire a shared lock.
895 if ((vap
->va_active
& VNODE_ATTR_TIMES
) &&
896 (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
897 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
899 hfs_touchtimes(hfsmp
, cp
);
901 // downgrade to a shared lock since that's all we need from here on out
902 cp
->c_lockowner
= HFS_SHARED_OWNER
;
903 lck_rw_lock_exclusive_to_shared(&cp
->c_rwlock
);
905 } else if ((error
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
909 if (v_type
== VDIR
) {
910 data_size
= (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
;
912 if (VATTR_IS_ACTIVE(vap
, va_nlink
)) {
916 * For directories, the va_nlink is esentially a count
917 * of the ".." references to a directory plus the "."
918 * reference and the directory itself. So for HFS+ this
919 * becomes the sub-directory count plus two.
921 * In the absence of a sub-directory count we use the
922 * directory's item count. This will be too high in
923 * most cases since it also includes files.
925 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
926 (cp
->c_attr
.ca_recflags
& kHFSHasFolderCountMask
))
927 nlink
= cp
->c_attr
.ca_dircount
; /* implied ".." entries */
929 nlink
= cp
->c_entries
;
931 /* Account for ourself and our "." entry */
933 /* Hide our private directories. */
934 if (cp
->c_cnid
== kHFSRootFolderID
) {
935 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0) {
938 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0) {
942 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)nlink
);
944 if (VATTR_IS_ACTIVE(vap
, va_nchildren
)) {
947 entries
= cp
->c_entries
;
948 /* Hide our private files and directories. */
949 if (cp
->c_cnid
== kHFSRootFolderID
) {
950 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
952 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
954 if (hfsmp
->jnl
|| ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
955 entries
-= 2; /* hide the journal files */
957 VATTR_RETURN(vap
, va_nchildren
, entries
);
960 * The va_dirlinkcount is the count of real directory hard links.
961 * (i.e. its not the sum of the implied "." and ".." references)
963 if (VATTR_IS_ACTIVE(vap
, va_dirlinkcount
)) {
964 VATTR_RETURN(vap
, va_dirlinkcount
, (uint32_t)cp
->c_linkcount
);
967 data_size
= VCTOF(vp
, cp
)->ff_size
;
969 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)cp
->c_linkcount
);
970 if (VATTR_IS_ACTIVE(vap
, va_data_alloc
)) {
975 VATTR_RETURN(vap
, va_data_alloc
, 0);
976 } else if (compressed
) {
977 /* for compressed files, we report all allocated blocks as belonging to the data fork */
978 blocks
= cp
->c_blocks
;
979 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
984 blocks
= VCTOF(vp
, cp
)->ff_blocks
;
985 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
990 /* conditional because 64-bit arithmetic can be expensive */
991 if (VATTR_IS_ACTIVE(vap
, va_total_size
)) {
992 if (v_type
== VDIR
) {
993 VATTR_RETURN(vap
, va_total_size
, (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
);
995 u_int64_t total_size
= ~0ULL;
999 /* we're hiding the size of this file, so just return 0 */
1001 } else if (compressed
) {
1002 if (uncompressed_size
== -1) {
1004 * We failed to get the uncompressed size above,
1005 * so we'll fall back to the standard path below
1006 * since total_size is still -1
1009 /* use the uncompressed size we fetched above */
1010 total_size
= uncompressed_size
;
1014 if (total_size
== ~0ULL) {
1015 if (cp
->c_datafork
) {
1016 total_size
= cp
->c_datafork
->ff_size
;
1019 if (cp
->c_blocks
- VTOF(vp
)->ff_blocks
) {
1020 /* We deal with rsrc fork vnode iocount at the end of the function */
1021 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
);
1024 * Note that we call hfs_vgetrsrc with error_on_unlinked
1025 * set to FALSE. This is because we may be invoked via
1026 * fstat() on an open-unlinked file descriptor and we must
1027 * continue to support access to the rsrc fork until it disappears.
1028 * The code at the end of this function will be
1029 * responsible for releasing the iocount generated by
1030 * hfs_vgetrsrc. This is because we can't drop the iocount
1031 * without unlocking the cnode first.
1037 if (rcp
&& rcp
->c_rsrcfork
) {
1038 total_size
+= rcp
->c_rsrcfork
->ff_size
;
1043 VATTR_RETURN(vap
, va_total_size
, total_size
);
1046 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
)) {
1047 if (v_type
== VDIR
) {
1048 VATTR_RETURN(vap
, va_total_alloc
, 0);
1050 VATTR_RETURN(vap
, va_total_alloc
, (u_int64_t
)cp
->c_blocks
* (u_int64_t
)hfsmp
->blockSize
);
1055 * If the VFS wants extended security data, and we know that we
1056 * don't have any (because it never told us it was setting any)
1057 * then we can return the supported bit and no data. If we do
1058 * have extended security, we can just leave the bit alone and
1059 * the VFS will use the fallback path to fetch it.
1061 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
1062 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
1063 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
1064 VATTR_SET_SUPPORTED(vap
, va_acl
);
1068 vap
->va_access_time
.tv_sec
= cp
->c_atime
;
1069 vap
->va_access_time
.tv_nsec
= 0;
1070 vap
->va_create_time
.tv_sec
= cp
->c_itime
;
1071 vap
->va_create_time
.tv_nsec
= 0;
1072 vap
->va_modify_time
.tv_sec
= cp
->c_mtime
;
1073 vap
->va_modify_time
.tv_nsec
= 0;
1074 vap
->va_change_time
.tv_sec
= cp
->c_ctime
;
1075 vap
->va_change_time
.tv_nsec
= 0;
1076 vap
->va_backup_time
.tv_sec
= cp
->c_btime
;
1077 vap
->va_backup_time
.tv_nsec
= 0;
1079 /* See if we need to emit the date added field to the user */
1080 if (VATTR_IS_ACTIVE(vap
, va_addedtime
)) {
1081 u_int32_t dateadded
= hfs_get_dateadded (cp
);
1083 vap
->va_addedtime
.tv_sec
= dateadded
;
1084 vap
->va_addedtime
.tv_nsec
= 0;
1085 VATTR_SET_SUPPORTED (vap
, va_addedtime
);
1089 /* XXX is this really a good 'optimal I/O size'? */
1090 vap
->va_iosize
= hfsmp
->hfs_logBlockSize
;
1091 vap
->va_uid
= cp
->c_uid
;
1092 vap
->va_gid
= cp
->c_gid
;
1093 vap
->va_mode
= cp
->c_mode
;
1094 vap
->va_flags
= cp
->c_bsdflags
;
1097 * Exporting file IDs from HFS Plus:
1099 * For "normal" files the c_fileid is the same value as the
1100 * c_cnid. But for hard link files, they are different - the
1101 * c_cnid belongs to the active directory entry (ie the link)
1102 * and the c_fileid is for the actual inode (ie the data file).
1104 * The stat call (getattr) uses va_fileid and the Carbon APIs,
1105 * which are hardlink-ignorant, will ask for va_linkid.
1107 vap
->va_fileid
= (u_int64_t
)cp
->c_fileid
;
1109 * We need to use the origin cache for both hardlinked files
1110 * and directories. Hardlinked directories have multiple cnids
1111 * and parents (one per link). Hardlinked files also have their
1112 * own parents and link IDs separate from the indirect inode number.
1113 * If we don't use the cache, we could end up vending the wrong ID
1114 * because the cnode will only reflect the link that was looked up most recently.
1116 if (cp
->c_flag
& C_HARDLINK
) {
1117 vap
->va_linkid
= (u_int64_t
)hfs_currentcnid(cp
);
1118 vap
->va_parentid
= (u_int64_t
)hfs_currentparent(cp
, /* have_lock: */ true);
1120 vap
->va_linkid
= (u_int64_t
)cp
->c_cnid
;
1121 vap
->va_parentid
= (u_int64_t
)cp
->c_parentcnid
;
1124 vap
->va_fsid
= hfsmp
->hfs_raw_dev
;
1125 if (VATTR_IS_ACTIVE(vap
, va_devid
)) {
1126 VATTR_RETURN(vap
, va_devid
, hfsmp
->hfs_raw_dev
);
1128 vap
->va_filerev
= 0;
1129 vap
->va_encoding
= cp
->c_encoding
;
1130 vap
->va_rdev
= (v_type
== VBLK
|| v_type
== VCHR
) ? cp
->c_rdev
: 0;
1132 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1134 vap
->va_data_size
= 0;
1135 else if (compressed
) {
1136 if (uncompressed_size
== -1) {
1137 /* failed to get the uncompressed size above, so just return data_size */
1138 vap
->va_data_size
= data_size
;
1140 /* use the uncompressed size we fetched above */
1141 vap
->va_data_size
= uncompressed_size
;
1144 vap
->va_data_size
= data_size
;
1145 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1148 vap
->va_data_size
= data_size
;
1149 vap
->va_supported
|= VNODE_ATTR_va_data_size
;
1153 if (VATTR_IS_ACTIVE(vap
, va_dataprotect_class
)) {
1154 vap
->va_dataprotect_class
= cp
->c_cpentry
? CP_CLASS(cp
->c_cpentry
->cp_pclass
) : 0;
1155 VATTR_SET_SUPPORTED(vap
, va_dataprotect_class
);
1158 if (VATTR_IS_ACTIVE(vap
, va_write_gencount
)) {
1159 if (ubc_is_mapped_writable(vp
)) {
1161 * Return 0 to the caller to indicate the file may be
1162 * changing. There is no need for us to increment the
1163 * generation counter here because it gets done as part of
1164 * page-out and also when the file is unmapped (to account
1165 * for changes we might not have seen).
1167 vap
->va_write_gencount
= 0;
1169 vap
->va_write_gencount
= hfs_get_gencount(cp
);
1172 VATTR_SET_SUPPORTED(vap
, va_write_gencount
);
1175 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
1176 vap
->va_supported
|= VNODE_ATTR_va_access_time
|
1177 VNODE_ATTR_va_create_time
| VNODE_ATTR_va_modify_time
|
1178 VNODE_ATTR_va_change_time
| VNODE_ATTR_va_backup_time
|
1179 VNODE_ATTR_va_iosize
| VNODE_ATTR_va_uid
|
1180 VNODE_ATTR_va_gid
| VNODE_ATTR_va_mode
|
1181 VNODE_ATTR_va_flags
|VNODE_ATTR_va_fileid
|
1182 VNODE_ATTR_va_linkid
| VNODE_ATTR_va_parentid
|
1183 VNODE_ATTR_va_fsid
| VNODE_ATTR_va_filerev
|
1184 VNODE_ATTR_va_encoding
| VNODE_ATTR_va_rdev
;
1186 /* If this is the root, let VFS to find out the mount name, which
1187 * may be different from the real name. Otherwise, we need to take care
1188 * for hardlinked files, which need to be looked up, if necessary
1190 if (VATTR_IS_ACTIVE(vap
, va_name
) && (cp
->c_cnid
!= kHFSRootFolderID
)) {
1191 struct cat_desc linkdesc
;
1193 int uselinkdesc
= 0;
1194 cnid_t nextlinkid
= 0;
1195 cnid_t prevlinkid
= 0;
1197 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
1198 * here because the info. for the link ID requested by getattrlist may be
1199 * different than what's currently in the cnode. This is because the cnode
1200 * will be filled in with the information for the most recent link ID that went
1201 * through namei/lookup(). If there are competing lookups for hardlinks that point
1202 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
1203 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
1206 if ((cp
->c_flag
& C_HARDLINK
) &&
1207 ((cp
->c_desc
.cd_namelen
== 0) || (vap
->va_linkid
!= cp
->c_cnid
))) {
1209 * If we have no name and our link ID is the raw inode number, then we may
1210 * have an open-unlinked file. Go to the next link in this case.
1212 if ((cp
->c_desc
.cd_namelen
== 0) && (vap
->va_linkid
== cp
->c_fileid
)) {
1213 if ((error
= hfs_lookup_siblinglinks(hfsmp
, vap
->va_linkid
, &prevlinkid
, &nextlinkid
))){
1218 /* just use link obtained from vap above */
1219 nextlinkid
= vap
->va_linkid
;
1222 /* We need to probe the catalog for the descriptor corresponding to the link ID
1223 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
1224 * for the cnode here, so we can't just update the descriptor. Instead,
1225 * we should just store the descriptor's value locally and then use it to pass
1226 * out the name value as needed below.
1229 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1230 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
1231 hfs_systemfile_unlock(hfsmp
, lockflags
);
1238 /* By this point, we've either patched up the name above and the c_desc
1239 * points to the correct data, or it already did, in which case we just proceed
1240 * by copying the name into the vap. Note that we will never set va_name to
1241 * supported if nextlinkid is never initialized. This could happen in the degenerate
1242 * case above involving the raw inode number, where it has no nextlinkid. In this case
1243 * we will simply not mark the name bit as supported.
1246 strlcpy(vap
->va_name
, (const char*) linkdesc
.cd_nameptr
, MAXPATHLEN
);
1247 VATTR_SET_SUPPORTED(vap
, va_name
);
1248 cat_releasedesc(&linkdesc
);
1250 else if (cp
->c_desc
.cd_namelen
) {
1251 strlcpy(vap
->va_name
, (const char*) cp
->c_desc
.cd_nameptr
, MAXPATHLEN
);
1252 VATTR_SET_SUPPORTED(vap
, va_name
);
1259 * We need to vnode_put the rsrc fork vnode only *after* we've released
1260 * the cnode lock, since vnode_put can trigger an inactive call, which
1261 * will go back into HFS and try to acquire a cnode lock.
1271 hfs_vnop_setattr(struct vnop_setattr_args
*ap
)
1273 struct vnode_attr
*vap
= ap
->a_vap
;
1274 struct vnode
*vp
= ap
->a_vp
;
1275 struct cnode
*cp
= NULL
;
1276 struct hfsmount
*hfsmp
;
1277 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
1278 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1284 orig_ctime
= VTOC(vp
)->c_ctime
;
1287 int decmpfs_reset_state
= 0;
1289 we call decmpfs_update_attributes even if the file is not compressed
1290 because we want to update the incoming flags if the xattrs are invalid
1292 error
= decmpfs_update_attributes(vp
, vap
);
1297 // if this is not a size-changing setattr and it is not just
1298 // an atime update, then check for a snapshot.
1300 if (!VATTR_IS_ACTIVE(vap
, va_data_size
) && !(vap
->va_active
== VNODE_ATTR_va_access_time
)) {
1301 nspace_snapshot_event(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_MOD
, NSPACE_REARM_NO_ARG
);
1306 * All metadata changes should be allowed except a size-changing setattr, which
1307 * has effects on file content and requires calling into cp_handle_vnop
1308 * to have content protection check.
1310 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1311 if ((error
= cp_handle_vnop(vp
, CP_WRITE_ACCESS
, 0)) != 0) {
1315 #endif /* CONFIG_PROTECT */
1319 /* Don't allow modification of the journal. */
1320 if (hfs_is_journal_file(hfsmp
, VTOC(vp
))) {
1325 // Check if we'll need a document_id and if so, get it before we lock the
1326 // the cnode to avoid any possible deadlock with the root vnode which has
1327 // to get locked to get the document id
1329 u_int32_t document_id
=0;
1330 if (VATTR_IS_ACTIVE(vap
, va_flags
) && (vap
->va_flags
& UF_TRACKED
) && !(VTOC(vp
)->c_bsdflags
& UF_TRACKED
)) {
1331 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&(VTOC(vp
)->c_attr
.ca_finderinfo
) + 16);
1333 // If the document_id is not set, get a new one. It will be set
1334 // on the file down below once we hold the cnode lock.
1336 if (fip
->document_id
== 0) {
1337 if (hfs_generate_document_id(hfsmp
, &document_id
) != 0) {
1345 * File size change request.
1346 * We are guaranteed that this is not a directory, and that
1347 * the filesystem object is writeable.
1349 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
1351 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1352 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1353 if (!vnode_isreg(vp
)) {
1354 if (vnode_isdir(vp
)) {
1357 //otherwise return EINVAL
1362 /* keep the compressed state locked until we're done truncating the file */
1363 decmpfs_cnode
*dp
= VTOCMP(vp
);
1366 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1367 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1368 * on this file while it's truncating
1370 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1372 /* failed to allocate a decmpfs_cnode */
1373 return ENOMEM
; /* what should this be? */
1377 nspace_snapshot_event(vp
, orig_ctime
, vap
->va_data_size
== 0 ? NAMESPACE_HANDLER_TRUNCATE_OP
|NAMESPACE_HANDLER_DELETE_OP
: NAMESPACE_HANDLER_TRUNCATE_OP
, NULL
);
1379 decmpfs_lock_compressed_data(dp
, 1);
1380 if (hfs_file_is_compressed(VTOC(vp
), 1)) {
1381 error
= decmpfs_decompress_file(vp
, dp
, -1/*vap->va_data_size*/, 0, 1);
1383 decmpfs_unlock_compressed_data(dp
, 1);
1389 // Take truncate lock
1390 hfs_lock_truncate(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1392 // hfs_truncate will deal with the cnode lock
1393 error
= hfs_truncate(vp
, vap
->va_data_size
, vap
->va_vaflags
& 0xffff,
1396 hfs_unlock_truncate(VTOC(vp
), HFS_LOCK_DEFAULT
);
1398 decmpfs_unlock_compressed_data(dp
, 1);
1404 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
1410 * If it is just an access time update request by itself
1411 * we know the request is from kernel level code, and we
1412 * can delay it without being as worried about consistency.
1413 * This change speeds up mmaps, in the rare case that they
1414 * get caught behind a sync.
1417 if (vap
->va_active
== VNODE_ATTR_va_access_time
) {
1418 cp
->c_touch_acctime
=TRUE
;
1425 * Owner/group change request.
1426 * We are guaranteed that the new owner/group is valid and legal.
1428 VATTR_SET_SUPPORTED(vap
, va_uid
);
1429 VATTR_SET_SUPPORTED(vap
, va_gid
);
1430 nuid
= VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uid_t
)VNOVAL
;
1431 ngid
= VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (gid_t
)VNOVAL
;
1432 if (((nuid
!= (uid_t
)VNOVAL
) || (ngid
!= (gid_t
)VNOVAL
)) &&
1433 ((error
= hfs_chown(vp
, nuid
, ngid
, cred
, p
)) != 0))
1437 * Mode change request.
1438 * We are guaranteed that the mode value is valid and that in
1439 * conjunction with the owner and group, this change is legal.
1441 VATTR_SET_SUPPORTED(vap
, va_mode
);
1442 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
1443 ((error
= hfs_chmod(vp
, (int)vap
->va_mode
, cred
, p
)) != 0))
1447 * File flags change.
1448 * We are guaranteed that only flags allowed to change given the
1449 * current securelevel are being changed.
1451 VATTR_SET_SUPPORTED(vap
, va_flags
);
1452 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1456 if ((cp
->c_bsdflags
^ vap
->va_flags
) & UF_COMPRESSED
) {
1458 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1459 * but we don't want to actually do the update until we've released the cnode lock down below
1460 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1461 * turn off the flag and look at the "raw" file for debugging purposes
1463 decmpfs_reset_state
= 1;
1466 if ((vap
->va_flags
& UF_TRACKED
) && !(cp
->c_bsdflags
& UF_TRACKED
)) {
1467 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
1470 // we're marking this item UF_TRACKED. if the document_id is
1471 // not set, get a new one and put it on the file.
1473 if (fip
->document_id
== 0) {
1474 if (document_id
!= 0) {
1475 // printf("SETATTR: assigning doc-id %d to %s (ino %d)\n", document_id, vp->v_name, cp->c_desc.cd_cnid);
1476 fip
->document_id
= (uint32_t)document_id
;
1477 add_fsevent(FSE_DOCID_CHANGED
, ap
->a_context
,
1478 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
1479 FSE_ARG_INO
, (ino64_t
)0, // src inode #
1480 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
1481 FSE_ARG_INT32
, document_id
,
1484 // printf("hfs: could not acquire a new document_id for %s (ino %d)\n", vp->v_name, cp->c_desc.cd_cnid);
1488 } else if (!(vap
->va_flags
& UF_TRACKED
) && (cp
->c_bsdflags
& UF_TRACKED
)) {
1490 // UF_TRACKED is being cleared so clear the document_id
1492 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
1493 if (fip
->document_id
) {
1494 // printf("SETATTR: clearing doc-id %d from %s (ino %d)\n", fip->document_id, vp->v_name, cp->c_desc.cd_cnid);
1495 add_fsevent(FSE_DOCID_CHANGED
, ap
->a_context
,
1496 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
1497 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // src inode #
1498 FSE_ARG_INO
, (ino64_t
)0, // dst inode #
1499 FSE_ARG_INT32
, fip
->document_id
, // document id
1501 fip
->document_id
= 0;
1502 cp
->c_bsdflags
&= ~UF_TRACKED
;
1506 cp
->c_bsdflags
= vap
->va_flags
;
1507 cp
->c_flag
|= C_MODIFIED
;
1508 cp
->c_touch_chgtime
= TRUE
;
1512 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1514 * The fdFlags for files and frFlags for folders are both 8 bytes
1515 * into the userInfo (the first 16 bytes of the Finder Info). They
1516 * are both 16-bit fields.
1518 fdFlags
= (u_int16_t
*) &cp
->c_finderinfo
[8];
1519 if (vap
->va_flags
& UF_HIDDEN
)
1520 *fdFlags
|= OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1522 *fdFlags
&= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1526 * Timestamp updates.
1528 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1529 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1530 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1531 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1532 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1533 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
1534 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1535 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1536 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
1537 if (VATTR_IS_ACTIVE(vap
, va_create_time
))
1538 cp
->c_itime
= vap
->va_create_time
.tv_sec
;
1539 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1540 cp
->c_atime
= vap
->va_access_time
.tv_sec
;
1541 cp
->c_touch_acctime
= FALSE
;
1543 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1544 cp
->c_mtime
= vap
->va_modify_time
.tv_sec
;
1545 cp
->c_touch_modtime
= FALSE
;
1546 cp
->c_touch_chgtime
= TRUE
;
1548 hfs_clear_might_be_dirty_flag(cp
);
1551 * The utimes system call can reset the modification
1552 * time but it doesn't know about HFS create times.
1553 * So we need to ensure that the creation time is
1554 * always at least as old as the modification time.
1556 if ((VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) &&
1557 (cp
->c_cnid
!= kHFSRootFolderID
) &&
1558 !VATTR_IS_ACTIVE(vap
, va_create_time
) &&
1559 (cp
->c_mtime
< cp
->c_itime
)) {
1560 cp
->c_itime
= cp
->c_mtime
;
1563 if (VATTR_IS_ACTIVE(vap
, va_backup_time
))
1564 cp
->c_btime
= vap
->va_backup_time
.tv_sec
;
1565 cp
->c_flag
|= C_MINOR_MOD
;
1568 // Set the date added time
1569 VATTR_SET_SUPPORTED(vap
, va_addedtime
);
1570 if (VATTR_IS_ACTIVE(vap
, va_addedtime
)) {
1571 hfs_write_dateadded(&cp
->c_attr
, vap
->va_addedtime
.tv_sec
);
1572 cp
->c_flag
&= ~C_NEEDS_DATEADDED
;
1573 cp
->c_touch_chgtime
= true;
1577 * Set name encoding.
1579 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1580 if (VATTR_IS_ACTIVE(vap
, va_encoding
)) {
1581 cp
->c_encoding
= vap
->va_encoding
;
1582 cp
->c_flag
|= C_MODIFIED
;
1583 hfs_setencodingbits(hfsmp
, cp
->c_encoding
);
1586 if ((error
= hfs_update(vp
, 0)) != 0)
1591 /* Purge origin cache for cnode, since caller now has correct link ID for it
1592 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1594 if ((cp
->c_flag
& C_HARDLINK
) && (vnode_vtype(vp
) != VDIR
)){
1595 hfs_relorigin(cp
, 0);
1600 if (decmpfs_reset_state
) {
1602 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1603 * but don't do it while holding the hfs cnode lock
1605 decmpfs_cnode
*dp
= VTOCMP(vp
);
1608 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1609 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1610 * on this file if it's locked
1612 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1614 /* failed to allocate a decmpfs_cnode */
1615 return ENOMEM
; /* what should this be? */
1618 decmpfs_cnode_set_vnode_state(dp
, FILE_TYPE_UNKNOWN
, 0);
1624 VATTR_SET_SUPPORTED(vap
, va_dataprotect_class
);
1625 if (!error
&& VATTR_IS_ACTIVE(vap
, va_dataprotect_class
))
1626 error
= cp_vnode_setclass(vp
, vap
->va_dataprotect_class
);
1634 * Change the mode on a file.
1635 * cnode must be locked before calling.
1638 hfs_chmod(struct vnode
*vp
, int mode
, __unused kauth_cred_t cred
, __unused
struct proc
*p
)
1640 register struct cnode
*cp
= VTOC(vp
);
1642 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1645 // Don't allow modification of the journal or journal_info_block
1646 if (hfs_is_journal_file(VTOHFS(vp
), cp
)) {
1650 #if OVERRIDE_UNKNOWN_PERMISSIONS
1651 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
) {
1656 mode_t new_mode
= (cp
->c_mode
& ~ALLPERMS
) | (mode
& ALLPERMS
);
1657 if (new_mode
!= cp
->c_mode
) {
1658 cp
->c_mode
= new_mode
;
1659 cp
->c_flag
|= C_MINOR_MOD
;
1661 cp
->c_touch_chgtime
= TRUE
;
1667 hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
)
1669 struct cnode
*cp
= VTOC(vp
);
1674 * Disallow write attempts on read-only file systems;
1675 * unless the file is a socket, fifo, or a block or
1676 * character device resident on the file system.
1678 switch (vnode_vtype(vp
)) {
1682 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
)
1689 /* If immutable bit set, nobody gets to write it. */
1690 if (considerFlags
&& (cp
->c_bsdflags
& IMMUTABLE
))
1693 /* Otherwise, user id 0 always gets access. */
1694 if (!suser(cred
, NULL
))
1697 /* Otherwise, check the owner. */
1698 if ((retval
= hfs_owner_rights(VTOHFS(vp
), cp
->c_uid
, cred
, p
, false)) == 0)
1699 return ((cp
->c_mode
& S_IWUSR
) == S_IWUSR
? 0 : EACCES
);
1701 /* Otherwise, check the groups. */
1702 if (kauth_cred_ismember_gid(cred
, cp
->c_gid
, &is_member
) == 0 && is_member
) {
1703 return ((cp
->c_mode
& S_IWGRP
) == S_IWGRP
? 0 : EACCES
);
1706 /* Otherwise, check everyone else. */
1707 return ((cp
->c_mode
& S_IWOTH
) == S_IWOTH
? 0 : EACCES
);
1712 * Perform chown operation on cnode cp;
1713 * code must be locked prior to call.
1717 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, __unused kauth_cred_t cred
,
1718 __unused
struct proc
*p
)
1720 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
1721 __unused
struct proc
*p
)
1724 register struct cnode
*cp
= VTOC(vp
);
1733 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1736 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
)
1739 if (uid
== (uid_t
)VNOVAL
)
1741 if (gid
== (gid_t
)VNOVAL
)
1744 #if 0 /* we are guaranteed that this is already the case */
1746 * If we don't own the file, are trying to change the owner
1747 * of the file, or are not a member of the target group,
1748 * the caller must be superuser or the call fails.
1750 if ((kauth_cred_getuid(cred
) != cp
->c_uid
|| uid
!= cp
->c_uid
||
1751 (gid
!= cp
->c_gid
&&
1752 (kauth_cred_ismember_gid(cred
, gid
, &is_member
) || !is_member
))) &&
1753 (error
= suser(cred
, 0)))
1760 if (ouid
== uid
&& ogid
== gid
) {
1761 // No change, just set change time
1762 cp
->c_touch_chgtime
= TRUE
;
1767 if ((error
= hfs_getinoquota(cp
)))
1770 dqrele(cp
->c_dquot
[USRQUOTA
]);
1771 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1774 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1775 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1779 * Eventually need to account for (fake) a block per directory
1780 * if (vnode_isdir(vp))
1781 * change = VTOHFS(vp)->blockSize;
1785 change
= (int64_t)(cp
->c_blocks
) * (int64_t)VTOVCB(vp
)->blockSize
;
1786 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
);
1787 (void) hfs_chkiq(cp
, -1, cred
, CHOWN
);
1788 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1789 dqrele(cp
->c_dquot
[i
]);
1790 cp
->c_dquot
[i
] = NODQUOT
;
1796 if ((error
= hfs_getinoquota(cp
)) == 0) {
1798 dqrele(cp
->c_dquot
[USRQUOTA
]);
1799 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1802 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1803 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1805 if ((error
= hfs_chkdq(cp
, change
, cred
, CHOWN
)) == 0) {
1806 if ((error
= hfs_chkiq(cp
, 1, cred
, CHOWN
)) == 0)
1809 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
|FORCE
);
1811 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1812 dqrele(cp
->c_dquot
[i
]);
1813 cp
->c_dquot
[i
] = NODQUOT
;
1818 if (hfs_getinoquota(cp
) == 0) {
1820 dqrele(cp
->c_dquot
[USRQUOTA
]);
1821 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1824 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1825 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1827 (void) hfs_chkdq(cp
, change
, cred
, FORCE
|CHOWN
);
1828 (void) hfs_chkiq(cp
, 1, cred
, FORCE
|CHOWN
);
1829 (void) hfs_getinoquota(cp
);
1833 if (hfs_getinoquota(cp
))
1834 panic("hfs_chown: lost quota");
1838 * Without quotas, we could probably make this a minor
1841 cp
->c_flag
|= C_MODIFIED
;
1844 According to the SUSv3 Standard, chown() shall mark
1845 for update the st_ctime field of the file.
1846 (No exceptions mentioned)
1848 cp
->c_touch_chgtime
= TRUE
;
1854 * Flush the resource fork if it exists. vp is the data fork and has
1857 static int hfs_flush_rsrc(vnode_t vp
, vfs_context_t ctx
)
1859 cnode_t
*cp
= VTOC(vp
);
1861 hfs_lock(cp
, HFS_SHARED_LOCK
, 0);
1863 vnode_t rvp
= cp
->c_rsrc_vp
;
1870 int vid
= vnode_vid(rvp
);
1874 int error
= vnode_getwithvid(rvp
, vid
);
1877 return error
== ENOENT
? 0 : error
;
1879 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, 0);
1880 hfs_lock_always(cp
, HFS_EXCLUSIVE_LOCK
);
1881 hfs_filedone(rvp
, ctx
, HFS_FILE_DONE_NO_SYNC
);
1883 hfs_unlock_truncate(cp
, 0);
1885 error
= ubc_msync(rvp
, 0, ubc_getsize(rvp
), NULL
,
1886 UBC_PUSHALL
| UBC_SYNC
);
1892 #endif // HFS_COMPRESSION
1895 /* Helper Functions for exchangedata(2) */
1898 * hfs_exchangedata_getxattr
1900 * vp: vnode to extract the EA for
1901 * name_selector: the index into the array of EA name entries.
1902 * buffer: address for output buffer to store the output EA
1903 * NOTE: This function will allocate the buffer, it is the caller's responsibility to free it.
1904 * xattr_size: output argument; will return the size of the EA, to correspond with the buffer.
1906 * Return: 0 on success.
1907 * errno on error. If we return any error, the buffer is guaranteed to be NULL.
1909 * Assumes CNODE lock held on cnode for 'vp'
1912 int hfs_exchangedata_getxattr (struct vnode
*vp
, uint32_t name_selector
, void **buffer
, size_t *xattr_size
) {
1913 void *xattr_rawdata
= NULL
;
1914 void *extracted_xattr
= NULL
;
1916 size_t memsize
= MAX_EXCHANGE_EA_SIZE
;
1919 struct hfsmount
*hfsmp
= NULL
;
1921 /* Sanity check inputs */
1922 if (name_selector
> MAX_NUM_XATTR_NAMES
) {
1926 if (buffer
== NULL
|| xattr_size
== NULL
) {
1932 //allocate 4k memory to hold the EA. We don't use this for "large" EAs, and the default
1933 //EA B-tree size should produce inline attributes of size < 4K
1934 xattr_rawdata
= hfs_malloc (MAX_EXCHANGE_EA_SIZE
);
1935 if (!xattr_rawdata
) {
1939 //now create the UIO
1940 uio
= uio_create (1, 0, UIO_SYSSPACE
, UIO_READ
);
1942 hfs_free (xattr_rawdata
, memsize
);
1945 uio_addiov(uio
, CAST_USER_ADDR_T(xattr_rawdata
), memsize
);
1948 struct vnop_getxattr_args vga
= {
1950 .a_name
= XATTR_NAMES
[name_selector
],
1954 // this takes care of grabbing the systemfile locks for us.
1955 error
= hfs_getxattr_internal (VTOC(vp
), &vga
, hfsmp
, 0);
1959 * We could have gotten a variety of errors back from the XATTR tree:
1960 * is it too big? (bigger than 4k?) == ERANGE
1961 * was the EA not found? == ENOATTR
1964 hfs_free (xattr_rawdata
, memsize
);
1971 //upon success, a_size/attrsize now contains the actua/exported EA size
1972 extracted_xattr
= hfs_malloc (attrsize
);
1973 memcpy (extracted_xattr
, xattr_rawdata
, attrsize
);
1974 hfs_free (xattr_rawdata
, memsize
);
1976 *xattr_size
= attrsize
;
1977 *buffer
= extracted_xattr
;
1984 * hfs_exchangedata_setxattr
1986 * Note: This function takes fileIDs in as inputs, because exchangedata does
1987 * swizzly things with the two cnodes (See big block comment in hfs_vnop_exchange)
1988 * so we operate with FileIDs more or less directly on the XATTR b-tree.
1991 * hfsmp: the mount we're working on
1992 * fileid: the fileID of the EA to store into the tree.
1993 * name_selector: selector into the EA name array.
1994 * buffer: pointer to the memory of the EA to write.
1995 * xattr_size: size of the EA to write.
1997 * Returns 0 on success
2000 * Assumes that a transaction has already begun when this is called
2004 int hfs_exchangedata_setxattr (struct hfsmount
*hfsmp
, uint32_t fileid
,
2005 uint32_t name_selector
, void *buffer
, size_t xattr_size
) {
2010 /* Sanity check arguments */
2011 if (name_selector
> MAX_NUM_XATTR_NAMES
) {
2015 if (buffer
== NULL
|| xattr_size
== 0 || fileid
< kHFSFirstUserCatalogNodeID
) {
2019 // is the size too big?
2020 if (xattr_size
> hfsmp
->hfs_max_inline_attrsize
) {
2024 /* setup the arguments to setxattr*/
2025 struct vnop_setxattr_args vsa
= {
2028 .a_name
= XATTR_NAMES
[name_selector
],
2029 .a_uio
= NULL
, // we use the data_ptr argument to setxattr_internal instead
2031 .a_context
= NULL
// no context needed, only done from within exchangedata
2035 * Since we must be in a transaction to guard the exchangedata operation, this will start
2036 * a nested transaction within the exchangedata one.
2038 error
= hfs_setxattr_internal (NULL
, (caddr_t
) buffer
, xattr_size
, &vsa
, hfsmp
, fileid
);
2045 * hfs_vnop_exchange:
2048 * 'from' vnode/cnode
2054 * hfs_vnop_exchange is used to service the exchangedata(2) system call.
2055 * Per the requirements of that system call, this function "swaps" some
2056 * of the information that lives in one catalog record for some that
2057 * lives in another. Note that not everything is swapped; in particular,
2058 * the extent information stored in each cnode is kept local to that
2059 * cnode. This allows existing file descriptor references to continue
2060 * to operate on the same content, regardless of the location in the
2061 * namespace that the file may have moved to. See inline comments
2062 * in the function for more information.
2065 hfs_vnop_exchange(struct vnop_exchange_args
*ap
)
2067 struct vnode
*from_vp
= ap
->a_fvp
;
2068 struct vnode
*to_vp
= ap
->a_tvp
;
2069 struct cnode
*from_cp
;
2070 struct cnode
*to_cp
;
2071 struct hfsmount
*hfsmp
;
2072 struct cat_desc tempdesc
;
2073 struct cat_attr tempattr
;
2074 const unsigned char *from_nameptr
;
2075 const unsigned char *to_nameptr
;
2076 char from_iname
[32];
2078 uint32_t to_flag_special
;
2079 uint32_t from_flag_special
;
2081 uint16_t to_recflags_special
;
2082 uint16_t from_recflags_special
;
2087 int error
= 0, started_tr
= 0, got_cookie
= 0;
2088 cat_cookie_t cookie
;
2089 time_t orig_from_ctime
, orig_to_ctime
;
2090 bool have_cnode_locks
= false, have_from_trunc_lock
= false, have_to_trunc_lock
= false;
2092 /* For the quarantine EA */
2093 void *from_xattr
= NULL
;
2094 void *to_xattr
= NULL
;
2095 size_t from_attrsize
= 0;
2096 size_t to_attrsize
= 0;
2100 * VFS does the following checks:
2101 * 1. Validate that both are files.
2102 * 2. Validate that both are on the same mount.
2103 * 3. Validate that they're not the same vnode.
2106 from_cp
= VTOC(from_vp
);
2107 to_cp
= VTOC(to_vp
);
2108 hfsmp
= VTOHFS(from_vp
);
2110 orig_from_ctime
= from_cp
->c_ctime
;
2111 orig_to_ctime
= to_cp
->c_ctime
;
2115 * Do not allow exchangedata/F_MOVEDATAEXTENTS on data-protected filesystems
2116 * because the EAs will not be swapped. As a result, the persistent keys would not
2117 * match and the files will be garbage.
2119 if (cp_fs_protected (vnode_mount(from_vp
))) {
2125 if (!ISSET(ap
->a_options
, FSOPT_EXCHANGE_DATA_ONLY
)) {
2126 if ( hfs_file_is_compressed(from_cp
, 0) ) {
2127 if ( 0 != ( error
= decmpfs_decompress_file(from_vp
, VTOCMP(from_vp
), -1, 0, 1) ) ) {
2132 if ( hfs_file_is_compressed(to_cp
, 0) ) {
2133 if ( 0 != ( error
= decmpfs_decompress_file(to_vp
, VTOCMP(to_vp
), -1, 0, 1) ) ) {
2138 #endif // HFS_COMPRESSION
2140 // Resource forks cannot be exchanged.
2141 if (VNODE_IS_RSRC(from_vp
) || VNODE_IS_RSRC(to_vp
))
2145 * Normally, we want to notify the user handlers about the event,
2146 * except if it's a handler driving the event.
2148 if ((ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) == 0) {
2149 nspace_snapshot_event(from_vp
, orig_from_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
2150 nspace_snapshot_event(to_vp
, orig_to_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
2153 * This is currently used by mtmd so we should tidy up the
2154 * file now because the data won't be used again in the
2157 hfs_lock_truncate(from_cp
, HFS_EXCLUSIVE_LOCK
, 0);
2158 hfs_lock_always(from_cp
, HFS_EXCLUSIVE_LOCK
);
2159 hfs_filedone(from_vp
, ap
->a_context
, HFS_FILE_DONE_NO_SYNC
);
2160 hfs_unlock(from_cp
);
2161 hfs_unlock_truncate(from_cp
, 0);
2163 // Flush all the data from the source file
2164 error
= ubc_msync(from_vp
, 0, ubc_getsize(from_vp
), NULL
,
2165 UBC_PUSHALL
| UBC_SYNC
);
2171 * If this is a compressed file, we need to do the same for
2172 * the resource fork.
2174 if (ISSET(from_cp
->c_bsdflags
, UF_COMPRESSED
)) {
2175 error
= hfs_flush_rsrc(from_vp
, ap
->a_context
);
2182 * We're doing a data-swap so we need to take the truncate
2183 * lock exclusively. We need an exclusive lock because we
2184 * will be completely truncating the source file and we must
2185 * make sure nobody else sneaks in and trys to issue I/O
2186 * whilst we don't have the cnode lock.
2188 * After taking the truncate lock we do a quick check to
2189 * verify there are no other references (including mmap
2190 * references), but we must remember that this does not stop
2191 * anybody coming in later and taking a reference. We will
2192 * have the truncate lock exclusively so that will prevent
2193 * them from issuing any I/O.
2196 if (to_cp
< from_cp
) {
2197 hfs_lock_truncate(to_cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2198 have_to_trunc_lock
= true;
2201 hfs_lock_truncate(from_cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2202 have_from_trunc_lock
= true;
2205 * Do an early check to verify the source is not in use by
2206 * anyone. We should be called from an FD opened as F_EVTONLY
2207 * so that doesn't count as a reference.
2209 if (vnode_isinuse(from_vp
, 0)) {
2214 if (to_cp
>= from_cp
) {
2215 hfs_lock_truncate(to_cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2216 have_to_trunc_lock
= true;
2220 if ((error
= hfs_lockpair(from_cp
, to_cp
, HFS_EXCLUSIVE_LOCK
)))
2222 have_cnode_locks
= true;
2224 // Don't allow modification of the journal or journal_info_block
2225 if (hfs_is_journal_file(hfsmp
, from_cp
) ||
2226 hfs_is_journal_file(hfsmp
, to_cp
)) {
2232 * If doing a data move, then call the underlying function.
2234 if (ISSET(ap
->a_options
, FSOPT_EXCHANGE_DATA_ONLY
)) {
2236 if (ISSET(from_cp
->c_bsdflags
, UF_COMPRESSED
)) {
2237 error
= hfs_move_compressed(from_cp
, to_cp
);
2242 error
= hfs_move_data(from_cp
, to_cp
, 0);
2247 * If we're doing a normal exchangedata, then get the source/dst quarantine
2248 * EAs as needed. We do it here before we start the transaction.
2251 //get the EA for the 'from' vnode if it exists.
2252 error
= hfs_exchangedata_getxattr (from_vp
, quarantine
, &from_xattr
, &from_attrsize
);
2254 if (error
== ENOATTR
) {
2255 //it's OK for the quarantine EA to not exist
2264 //get the EA from the 'to' vnode if it exists
2265 error
= hfs_exchangedata_getxattr (to_vp
, quarantine
, &to_xattr
, &to_attrsize
);
2267 if (error
== ENOATTR
) {
2268 //it's OK for the quarantine EA to not exist
2277 /* Start a transaction; we have to do all of this atomically */
2278 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2284 * Reserve some space in the Catalog file.
2286 if ((error
= cat_preflight(hfsmp
, CAT_EXCHANGE
, &cookie
, vfs_context_proc(ap
->a_context
)))) {
2291 /* The backend code always tries to delete the virtual
2292 * extent id for exchanging files so we need to lock
2293 * the extents b-tree.
2295 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2297 /* Account for the location of the catalog objects. */
2298 if (from_cp
->c_flag
& C_HARDLINK
) {
2299 MAKE_INODE_NAME(from_iname
, sizeof(from_iname
),
2300 from_cp
->c_attr
.ca_linkref
);
2301 from_nameptr
= (unsigned char *)from_iname
;
2302 from_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
2303 from_cp
->c_hint
= 0;
2305 from_nameptr
= from_cp
->c_desc
.cd_nameptr
;
2306 from_parid
= from_cp
->c_parentcnid
;
2308 if (to_cp
->c_flag
& C_HARDLINK
) {
2309 MAKE_INODE_NAME(to_iname
, sizeof(to_iname
),
2310 to_cp
->c_attr
.ca_linkref
);
2311 to_nameptr
= (unsigned char *)to_iname
;
2312 to_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
2315 to_nameptr
= to_cp
->c_desc
.cd_nameptr
;
2316 to_parid
= to_cp
->c_parentcnid
;
2320 * ExchangeFileIDs swaps the on-disk, or in-BTree extent information
2321 * attached to two different file IDs. It also swaps the extent
2322 * information that may live in the extents-overflow B-Tree.
2324 * We do this in a transaction as this may require a lot of B-Tree nodes
2325 * to do completely, particularly if one of the files in question
2326 * has a lot of extents.
2328 * For example, assume "file1" has fileID 50, and "file2" has fileID 52.
2329 * For the on-disk records, which are assumed to be synced, we will
2330 * first swap the resident inline-8 extents as part of the catalog records.
2331 * Then we will swap any extents overflow records for each file.
2333 * When ExchangeFileIDs returns successfully, "file1" will have fileID 52,
2334 * and "file2" will have fileID 50. However, note that this is only
2335 * approximately half of the work that exchangedata(2) will need to
2336 * accomplish. In other words, we swap "too much" of the information
2337 * because if we only called ExchangeFileIDs, both the fileID and extent
2338 * information would be the invariants of this operation. We don't
2339 * actually want that; we want to conclude with "file1" having
2340 * file ID 50, and "file2" having fileID 52.
2342 * The remainder of hfs_vnop_exchange will swap the file ID and other cnode
2343 * data back to the proper ownership, while still allowing the cnode to remain
2344 * pointing at the same set of extents that it did originally.
2346 error
= ExchangeFileIDs(hfsmp
, from_nameptr
, to_nameptr
, from_parid
,
2347 to_parid
, from_cp
->c_hint
, to_cp
->c_hint
);
2348 hfs_systemfile_unlock(hfsmp
, lockflags
);
2350 if (error
!= E_NONE
) {
2351 error
= MacToVFSError(error
);
2356 * Now, we have to swap the quarantine EA.
2358 * Ordinarily, we would not have to swap/exchange any extended attributes,
2359 * since they are keyed by the file ID, and this function is supposed
2360 * to manipulate the main data stream/fork only.
2362 * However, we want the quarantine EA to follow the file content.
2365 int from_xattr_status
= 0;
2369 * We've crossed a point of no return here, because if we
2370 * have successfully swapped the file content above, we need to continue here
2371 * to swap the rest of the cnode content, which is not subject to failure.
2372 * Failing the whole function because the xattr swap will result in perceived
2373 * data loss to the caller, so we swallow the error case here.
2375 from_xattr_status
= hfs_removexattr_by_id (hfsmp
, from_cp
->c_fileid
, XATTR_NAMES
[quarantine
]);
2376 if (from_xattr_status
== 0) {
2377 int xattr_lockflags
;
2380 * Check to see if we need to remove the xattr bit from the catalog record flags while
2381 * 'from_cp' still tracks with its original file ID. Once the cnodes' contents are swapped
2382 * and they are ready to be re-hashed, we will OR in the bit if we know that we moved the
2383 * EA to the counterpart.
2385 xattr_lockflags
= hfs_systemfile_lock (hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
2386 remaining_eas
= file_attribute_exist (hfsmp
, from_cp
->c_fileid
);
2387 if (remaining_eas
== 0) {
2388 from_cp
->c_attr
.ca_recflags
&= ~kHFSHasAttributesMask
;
2389 //the cnode will be pushed out to disk LATER on.
2391 hfs_systemfile_unlock (hfsmp
, xattr_lockflags
);
2396 //and the same for to_xattr
2398 int xattr_status
= hfs_removexattr_by_id (hfsmp
, to_cp
->c_fileid
, XATTR_NAMES
[quarantine
]);
2400 if (xattr_status
== 0) {
2401 int xattr_lockflags
;
2404 * Check to see if we need to remove the xattr bit from the catalog record flags while
2405 * 'to_cp' still tracks with its original file ID. Once the cnodes' contents are swapped
2406 * and they are ready to be re-hashed, we will OR in the bit if we know that we moved the
2407 * EA to the counterpart.
2409 xattr_lockflags
= hfs_systemfile_lock (hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
2410 remaining_eas
= file_attribute_exist (hfsmp
, from_cp
->c_fileid
);
2411 if (remaining_eas
== 0) {
2412 to_cp
->c_attr
.ca_recflags
&= ~kHFSHasAttributesMask
;
2413 //the cnode will be pushed out to disk LATER on.
2415 hfs_systemfile_unlock (hfsmp
, xattr_lockflags
);
2417 /* Now move the EA to the counterparty fileID. We piggyback on the larger transaction here */
2418 hfs_exchangedata_setxattr (hfsmp
, from_cp
->c_fileid
, quarantine
, to_xattr
, to_attrsize
);
2422 if (from_xattr
&& from_xattr_status
== 0) {
2424 * if the from EA got removed properly, then attach it to the 'to' file. We do it at this point
2425 * to ensure that it got removed properly above before re-setting it again.
2427 hfs_exchangedata_setxattr (hfsmp
, to_cp
->c_fileid
, quarantine
, from_xattr
, from_attrsize
);
2431 /* Purge the vnodes from the name cache */
2433 cache_purge(from_vp
);
2437 /* Bump both source and destination write counts before any swaps. */
2439 hfs_incr_gencount (from_cp
);
2440 hfs_incr_gencount (to_cp
);
2443 /* Save a copy of "from" attributes before swapping. */
2444 bcopy(&from_cp
->c_desc
, &tempdesc
, sizeof(struct cat_desc
));
2445 bcopy(&from_cp
->c_attr
, &tempattr
, sizeof(struct cat_attr
));
2447 /* Save whether or not each cnode is a hardlink or has EAs */
2448 from_flag_special
= from_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
2449 from_recflags_special
= (from_cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
);
2451 to_flag_special
= to_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
2452 to_recflags_special
= (to_cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
);
2454 /* Drop the special bits from each cnode */
2455 from_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
2456 to_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
2457 from_cp
->c_attr
.ca_recflags
&= ~(kHFSHasAttributesMask
);
2458 to_cp
->c_attr
.ca_recflags
&= ~(kHFSHasAttributesMask
);
2461 * Now complete the in-memory portion of the copy.
2463 * ExchangeFileIDs swaps the on-disk records involved. We complete the
2464 * operation by swapping the in-memory contents of the two files here.
2465 * We swap the cnode descriptors, which contain name, BSD attributes,
2466 * timestamps, etc, about the file.
2468 * NOTE: We do *NOT* swap the fileforks of the two cnodes. We have
2469 * already swapped the on-disk extent information. As long as we swap the
2470 * IDs, the in-line resident 8 extents that live in the filefork data
2471 * structure will point to the right data for the new file ID if we leave
2474 * As a result, any file descriptor that points to a particular
2475 * vnode (even though it should change names), will continue
2476 * to point to the same content.
2479 /* Copy the "to" -> "from" cnode */
2480 bcopy(&to_cp
->c_desc
, &from_cp
->c_desc
, sizeof(struct cat_desc
));
2482 from_cp
->c_hint
= 0;
2484 * If 'to' was a hardlink, then we copied over its link ID/CNID/(namespace ID)
2485 * when we bcopy'd the descriptor above. However, the cnode attributes
2486 * are not bcopied. As a result, make sure to swap the file IDs of each item.
2488 * Further, other hardlink attributes must be moved along in this swap:
2489 * the linkcount, the linkref, and the firstlink all need to move
2490 * along with the file IDs. See note below regarding the flags and
2491 * what moves vs. what does not.
2494 * linkcount == total # of hardlinks.
2495 * linkref == the indirect inode pointer.
2496 * firstlink == the first hardlink in the chain (written to the raw inode).
2497 * These three are tied to the fileID and must move along with the rest of the data.
2499 from_cp
->c_fileid
= to_cp
->c_attr
.ca_fileid
;
2501 from_cp
->c_itime
= to_cp
->c_itime
;
2502 from_cp
->c_btime
= to_cp
->c_btime
;
2503 from_cp
->c_atime
= to_cp
->c_atime
;
2504 from_cp
->c_ctime
= to_cp
->c_ctime
;
2505 from_cp
->c_gid
= to_cp
->c_gid
;
2506 from_cp
->c_uid
= to_cp
->c_uid
;
2507 from_cp
->c_bsdflags
= to_cp
->c_bsdflags
;
2508 from_cp
->c_mode
= to_cp
->c_mode
;
2509 from_cp
->c_linkcount
= to_cp
->c_linkcount
;
2510 from_cp
->c_attr
.ca_linkref
= to_cp
->c_attr
.ca_linkref
;
2511 from_cp
->c_attr
.ca_firstlink
= to_cp
->c_attr
.ca_firstlink
;
2514 * The cnode flags need to stay with the cnode and not get transferred
2515 * over along with everything else because they describe the content; they are
2516 * not attributes that reflect changes specific to the file ID. In general,
2517 * fields that are tied to the file ID are the ones that will move.
2519 * This reflects the fact that the file may have borrowed blocks, dirty metadata,
2520 * or other extents, which may not yet have been written to the catalog. If
2521 * they were, they would have been transferred above in the ExchangeFileIDs call above...
2523 * The flags that are special are:
2524 * C_HARDLINK, C_HASXATTRS
2526 * and the c_attr recflag:
2527 * kHFSHasAttributesMask
2529 * These flags move with the item and file ID in the namespace since their
2530 * state is tied to that of the file ID.
2532 * So to transfer the flags, we have to take the following steps
2533 * 1) Store in a localvar whether or not the special bits are set.
2534 * 2) Drop the special bits from the current flags
2535 * 3) swap the special flag bits to their destination
2537 from_cp
->c_flag
|= to_flag_special
| C_MODIFIED
;
2538 from_cp
->c_attr
.ca_recflags
= to_cp
->c_attr
.ca_recflags
;
2539 from_cp
->c_attr
.ca_recflags
|= to_recflags_special
;
2543 * This is counter-intuitive and part of the complexity of exchangedata.
2544 * if 'from_cp' originally had a quarantine EA, then ensure that the cnode
2545 * pointed to by 'from_cp' CONTINUES to keep the "has EAs" bit. This is because
2546 * the cnode is about to be re-hashed with a new ID, but the file CONTENT
2547 * (i.e. the file fork) stayed put. And we want the quarantine EA to follow
2548 * the content. The check above is correct.
2550 from_cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
2553 bcopy(to_cp
->c_finderinfo
, from_cp
->c_finderinfo
, 32);
2556 /* Copy the "from" -> "to" cnode */
2557 bcopy(&tempdesc
, &to_cp
->c_desc
, sizeof(struct cat_desc
));
2560 * Pull the file ID from the tempattr we copied above. We can't assume
2561 * it is the same as the CNID.
2563 to_cp
->c_fileid
= tempattr
.ca_fileid
;
2564 to_cp
->c_itime
= tempattr
.ca_itime
;
2565 to_cp
->c_btime
= tempattr
.ca_btime
;
2566 to_cp
->c_atime
= tempattr
.ca_atime
;
2567 to_cp
->c_ctime
= tempattr
.ca_ctime
;
2568 to_cp
->c_gid
= tempattr
.ca_gid
;
2569 to_cp
->c_uid
= tempattr
.ca_uid
;
2570 to_cp
->c_bsdflags
= tempattr
.ca_flags
;
2571 to_cp
->c_mode
= tempattr
.ca_mode
;
2572 to_cp
->c_linkcount
= tempattr
.ca_linkcount
;
2573 to_cp
->c_attr
.ca_linkref
= tempattr
.ca_linkref
;
2574 to_cp
->c_attr
.ca_firstlink
= tempattr
.ca_firstlink
;
2577 * Only OR in the "from" flags into our cnode flags below.
2578 * Leave the rest of the flags alone.
2580 to_cp
->c_flag
|= from_flag_special
| C_MODIFIED
;
2581 to_cp
->c_attr
.ca_recflags
= tempattr
.ca_recflags
;
2582 to_cp
->c_attr
.ca_recflags
|= from_recflags_special
;
2587 * This is counter-intuitive and part of the complexity of exchangedata.
2588 * if 'to_cp' originally had a quarantine EA, then ensure that the cnode
2589 * pointed to by 'to_cp' CONTINUES to keep the "has EAs" bit. This is because
2590 * the cnode is about to be re-hashed with a new ID, but the file CONTENT
2591 * (i.e. the file fork) stayed put. And we want the quarantine EA to follow
2592 * the content. The check above is correct.
2594 to_cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
2597 bcopy(tempattr
.ca_finderinfo
, to_cp
->c_finderinfo
, 32);
2600 /* Rehash the cnodes using their new file IDs */
2601 hfs_chash_rehash(hfsmp
, from_cp
, to_cp
);
2604 * When a file moves out of "Cleanup At Startup"
2605 * we can drop its NODUMP status.
2607 if ((from_cp
->c_bsdflags
& UF_NODUMP
) &&
2608 (from_cp
->c_parentcnid
!= to_cp
->c_parentcnid
)) {
2609 from_cp
->c_bsdflags
&= ~UF_NODUMP
;
2610 from_cp
->c_touch_chgtime
= TRUE
;
2612 if ((to_cp
->c_bsdflags
& UF_NODUMP
) &&
2613 (to_cp
->c_parentcnid
!= from_cp
->c_parentcnid
)) {
2614 to_cp
->c_bsdflags
&= ~UF_NODUMP
;
2615 to_cp
->c_touch_chgtime
= TRUE
;
2620 cat_postflight(hfsmp
, &cookie
, vfs_context_proc(ap
->a_context
));
2623 hfs_end_transaction(hfsmp
);
2626 if (have_cnode_locks
)
2627 hfs_unlockpair(from_cp
, to_cp
);
2629 if (have_from_trunc_lock
)
2630 hfs_unlock_truncate(from_cp
, 0);
2632 if (have_to_trunc_lock
)
2633 hfs_unlock_truncate(to_cp
, 0);
2635 /* Free the memory used by the EAs */
2637 hfs_free (from_xattr
, from_attrsize
);
2642 hfs_free (to_xattr
, to_attrsize
);
2651 * This function is used specifically for the case when a namespace
2652 * handler is trying to steal data before it's deleted. Note that we
2653 * don't bother deleting the xattr from the source because it will get
2654 * deleted a short time later anyway.
2656 * cnodes must be locked
2658 static int hfs_move_compressed(cnode_t
*from_cp
, cnode_t
*to_cp
)
2663 CLR(from_cp
->c_bsdflags
, UF_COMPRESSED
);
2664 SET(from_cp
->c_flag
, C_MODIFIED
);
2666 ret
= hfs_move_data(from_cp
, to_cp
, HFS_MOVE_DATA_INCLUDE_RSRC
);
2671 * Transfer the xattr that decmpfs uses. Ideally, this code
2672 * should be with the other decmpfs code but it's file system
2673 * agnostic and this path is currently, and likely to remain, HFS+
2674 * specific. It's easier and more performant if we implement it
2679 data
= hfs_malloc(size
= MAX_DECMPFS_XATTR_SIZE
);
2681 ret
= hfs_xattr_read(from_cp
->c_vp
, DECMPFS_XATTR_NAME
, data
, &size
);
2685 ret
= hfs_xattr_write(to_cp
->c_vp
, DECMPFS_XATTR_NAME
, data
, size
);
2689 SET(to_cp
->c_bsdflags
, UF_COMPRESSED
);
2690 SET(to_cp
->c_flag
, C_MODIFIED
);
2693 hfs_free(data
, MAX_DECMPFS_XATTR_SIZE
);
2697 #endif // HFS_COMPRESSION
2700 hfs_vnop_mmap(struct vnop_mmap_args
*ap
)
2702 struct vnode
*vp
= ap
->a_vp
;
2703 cnode_t
*cp
= VTOC(vp
);
2706 if (VNODE_IS_RSRC(vp
)) {
2707 /* allow pageins of the resource fork */
2709 int compressed
= hfs_file_is_compressed(cp
, 1); /* 1 == don't take the cnode lock */
2710 time_t orig_ctime
= cp
->c_ctime
;
2712 if (!compressed
&& (cp
->c_bsdflags
& UF_COMPRESSED
)) {
2713 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
2719 if (ap
->a_fflags
& PROT_WRITE
) {
2720 nspace_snapshot_event(vp
, orig_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
2725 error
= cp_handle_vnop(vp
, (ap
->a_fflags
& PROT_WRITE
2726 ? CP_WRITE_ACCESS
: 0) | CP_READ_ACCESS
, 0);
2732 // NOTE: we return ENOTSUP because we want the cluster layer
2733 // to actually do all the real work.
2738 static errno_t
hfs_vnop_mnomap(struct vnop_mnomap_args
*ap
)
2740 vnode_t vp
= ap
->a_vp
;
2743 * Whilst the file was mapped, there may not have been any
2744 * page-outs so we need to increment the generation counter now.
2745 * Unfortunately this may lead to a change in the generation
2746 * counter when no actual change has been made, but there is
2747 * little we can do about that with our current architecture.
2749 if (ubc_is_mapped_writable(vp
)) {
2750 cnode_t
*cp
= VTOC(vp
);
2751 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2752 hfs_incr_gencount(cp
);
2755 * We don't want to set the modification time here since a
2756 * change to that is not acceptable if no changes were made.
2757 * Instead we set a flag so that if we get any page-outs we
2758 * know to update the modification time. It's possible that
2759 * they weren't actually because of changes made whilst the
2760 * file was mapped but that's not easy to fix now.
2762 SET(cp
->c_flag
, C_MIGHT_BE_DIRTY_FROM_MAPPING
);
2771 * Mark the resource fork as needing a ubc_setsize when we drop the
2774 static void hfs_rsrc_setsize(cnode_t
*cp
)
2777 * We need to take an iocount if we don't have one. vnode_get
2778 * will return ENOENT if the vnode is terminating which is what we
2779 * want as it's not safe to call ubc_setsize in that case.
2781 if (cp
->c_rsrc_vp
&& !vnode_get(cp
->c_rsrc_vp
)) {
2782 // Shouldn't happen, but better safe...
2783 if (ISSET(cp
->c_flag
, C_NEED_RVNODE_PUT
))
2784 vnode_put(cp
->c_rsrc_vp
);
2785 SET(cp
->c_flag
, C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
);
2792 * This is a non-symmetric variant of exchangedata. In this function,
2793 * the contents of the data fork (and optionally the resource fork)
2794 * are moved from from_cp to to_cp.
2796 * The cnodes must be locked.
2798 * The cnode pointed to by 'to_cp' *must* be empty prior to invoking
2799 * this function. We impose this restriction because we may not be
2800 * able to fully delete the entire file's contents in a single
2801 * transaction, particularly if it has a lot of extents. In the
2802 * normal file deletion codepath, the file is screened for two
2803 * conditions: 1) bigger than 400MB, and 2) more than 8 extents. If
2804 * so, the file is relocated to the hidden directory and the deletion
2805 * is broken up into multiple truncates. We can't do that here
2806 * because both files need to exist in the namespace. The main reason
2807 * this is imposed is that we may have to touch a whole lot of bitmap
2808 * blocks if there are many extents.
2810 * Any data written to 'from_cp' after this call completes is not
2811 * guaranteed to be moved.
2814 * cnode_t *from_cp : source file
2815 * cnode_t *to_cp : destination file; must be empty
2819 * EBUSY - File has been deleted or is in use
2820 * EFBIG - Destination file was not empty
2821 * EIO - An I/O error
2823 * other - Other errors that can be returned from called functions
2825 int hfs_move_data(cnode_t
*from_cp
, cnode_t
*to_cp
,
2826 hfs_move_data_options_t options
)
2828 hfsmount_t
*hfsmp
= VTOHFS(from_cp
->c_vp
);
2831 bool return_EIO_on_error
= false;
2832 const bool include_rsrc
= ISSET(options
, HFS_MOVE_DATA_INCLUDE_RSRC
);
2834 /* Verify that neither source/dest file is open-unlinked */
2835 if (ISSET(from_cp
->c_flag
, C_DELETED
| C_NOEXISTS
)
2836 || ISSET(to_cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
2841 * Verify the source file is not in use by anyone besides us.
2843 * This function is typically invoked by a namespace handler
2844 * process responding to a temporarily stalled system call.
2845 * The FD that it is working off of is opened O_EVTONLY, so
2846 * it really has no active usecounts (the kusecount from O_EVTONLY
2847 * is subtracted from the total usecounts).
2849 * As a result, we shouldn't have any active usecounts against
2850 * this vnode when we go to check it below.
2852 if (vnode_isinuse(from_cp
->c_vp
, 0))
2855 if (include_rsrc
&& from_cp
->c_rsrc_vp
) {
2856 if (vnode_isinuse(from_cp
->c_rsrc_vp
, 0))
2860 * In the code below, if the destination file doesn't have a
2861 * c_rsrcfork then we don't create it which means we we cannot
2862 * transfer the ff_invalidranges and cf_vblocks fields. These
2863 * shouldn't be set because we flush the resource fork before
2864 * calling this function but there is a tiny window when we
2865 * did not have any locks...
2867 if (!to_cp
->c_rsrcfork
2868 && (!TAILQ_EMPTY(&from_cp
->c_rsrcfork
->ff_invalidranges
)
2869 || from_cp
->c_rsrcfork
->ff_unallocblocks
)) {
2871 * The file isn't really busy now but something did slip
2872 * in and tinker with the file while we didn't have any
2873 * locks, so this is the most meaningful return code for
2880 // Check the destination file is empty
2881 if (to_cp
->c_datafork
->ff_blocks
2882 || to_cp
->c_datafork
->ff_size
2885 || (to_cp
->c_rsrcfork
&& to_cp
->c_rsrcfork
->ff_size
)))) {
2889 if ((error
= hfs_start_transaction (hfsmp
)))
2892 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
,
2893 HFS_EXCLUSIVE_LOCK
);
2895 // filefork_t is 128 bytes which should be OK
2896 filefork_t rfork_buf
, *from_rfork
= NULL
;
2899 from_rfork
= from_cp
->c_rsrcfork
;
2902 * Creating resource fork vnodes is expensive, so just get get
2903 * the fork data if we need it.
2905 if (!from_rfork
&& hfs_has_rsrc(from_cp
)) {
2906 from_rfork
= &rfork_buf
;
2908 from_rfork
->ff_cp
= from_cp
;
2909 TAILQ_INIT(&from_rfork
->ff_invalidranges
);
2911 error
= cat_idlookup(hfsmp
, from_cp
->c_fileid
, 0, 1, NULL
, NULL
,
2912 &from_rfork
->ff_data
);
2920 * From here on, any failures mean that we might be leaving things
2921 * in a weird or inconsistent state. Ideally, we should back out
2922 * all the changes, but to do that properly we need to fix
2923 * MoveData. We'll save fixing that for another time. For now,
2924 * just return EIO in all cases to the caller so that they know.
2926 return_EIO_on_error
= true;
2928 bool data_overflow_extents
= overflow_extents(from_cp
->c_datafork
);
2930 // Move the data fork
2931 if ((error
= hfs_move_fork (from_cp
->c_datafork
, from_cp
,
2932 to_cp
->c_datafork
, to_cp
))) {
2936 SET(from_cp
->c_flag
, C_NEED_DATA_SETSIZE
);
2937 SET(to_cp
->c_flag
, C_NEED_DATA_SETSIZE
);
2939 // We move the resource fork later
2942 * Note that because all we're doing is moving the extents around,
2943 * we can probably do this in a single transaction: Each extent
2944 * record (group of 8) is 64 bytes. A extent overflow B-Tree node
2945 * is typically 4k. This means each node can hold roughly ~60
2946 * extent records == (480 extents).
2948 * If a file was massively fragmented and had 20k extents, this
2949 * means we'd roughly touch 20k/480 == 41 to 42 nodes, plus the
2950 * index nodes, for half of the operation. (inserting or
2951 * deleting). So if we're manipulating 80-100 nodes, this is
2952 * basically 320k of data to write to the journal in a bad case.
2954 if (data_overflow_extents
) {
2955 if ((error
= MoveData(hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 0)))
2959 if (from_rfork
&& overflow_extents(from_rfork
)) {
2960 if ((error
= MoveData(hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 1)))
2965 from_cp
->c_touch_acctime
= TRUE
;
2966 from_cp
->c_touch_chgtime
= TRUE
;
2967 from_cp
->c_touch_modtime
= TRUE
;
2968 hfs_touchtimes(hfsmp
, from_cp
);
2970 to_cp
->c_touch_acctime
= TRUE
;
2971 to_cp
->c_touch_chgtime
= TRUE
;
2972 to_cp
->c_touch_modtime
= TRUE
;
2973 hfs_touchtimes(hfsmp
, to_cp
);
2975 struct cat_fork dfork_buf
;
2976 const struct cat_fork
*dfork
, *rfork
;
2978 dfork
= hfs_prepare_fork_for_update(to_cp
->c_datafork
, NULL
,
2979 &dfork_buf
, hfsmp
->blockSize
);
2980 rfork
= hfs_prepare_fork_for_update(from_rfork
, NULL
,
2981 &rfork_buf
.ff_data
, hfsmp
->blockSize
);
2983 // Update the catalog nodes, to_cp first
2984 if ((error
= cat_update(hfsmp
, &to_cp
->c_desc
, &to_cp
->c_attr
,
2989 CLR(to_cp
->c_flag
, C_MODIFIED
| C_MINOR_MOD
);
2991 // Update in-memory resource fork data here
2994 uint32_t moving
= from_rfork
->ff_blocks
+ from_rfork
->ff_unallocblocks
;
2996 from_cp
->c_blocks
-= moving
;
2997 to_cp
->c_blocks
+= moving
;
2999 // Update to_cp's resource data if it has it
3000 filefork_t
*to_rfork
= to_cp
->c_rsrcfork
;
3002 TAILQ_SWAP(&to_rfork
->ff_invalidranges
,
3003 &from_rfork
->ff_invalidranges
, rl_entry
, rl_link
);
3004 to_rfork
->ff_data
= from_rfork
->ff_data
;
3006 // Deal with ubc_setsize
3007 hfs_rsrc_setsize(to_cp
);
3010 // Wipe out the resource fork in from_cp
3011 rl_init(&from_rfork
->ff_invalidranges
);
3012 bzero(&from_rfork
->ff_data
, sizeof(from_rfork
->ff_data
));
3014 // Deal with ubc_setsize
3015 hfs_rsrc_setsize(from_cp
);
3018 // Currently unnecessary, but might be useful in future...
3019 dfork
= hfs_prepare_fork_for_update(from_cp
->c_datafork
, NULL
, &dfork_buf
,
3021 rfork
= hfs_prepare_fork_for_update(from_rfork
, NULL
, &rfork_buf
.ff_data
,
3025 if ((error
= cat_update(hfsmp
, &from_cp
->c_desc
, &from_cp
->c_attr
,
3030 CLR(from_cp
->c_flag
, C_MODIFIED
| C_MINOR_MOD
);
3034 hfs_systemfile_unlock(hfsmp
, lockflags
);
3035 hfs_end_transaction(hfsmp
);
3038 if (error
&& error
!= EIO
&& return_EIO_on_error
) {
3039 printf("hfs_move_data: encountered error %d\n", error
);
3047 * Move all of the catalog and runtime data in srcfork to dstfork.
3049 * This allows us to maintain the invalid ranges across the move data
3050 * operation so we don't need to force all of the pending IO right
3051 * now. In addition, we move all non overflow-extent extents into the
3054 * The destination fork must be empty and should have been checked
3055 * prior to calling this.
3057 static int hfs_move_fork(filefork_t
*srcfork
, cnode_t
*src_cp
,
3058 filefork_t
*dstfork
, cnode_t
*dst_cp
)
3060 // Move the invalid ranges
3061 TAILQ_SWAP(&dstfork
->ff_invalidranges
, &srcfork
->ff_invalidranges
,
3063 rl_remove_all(&srcfork
->ff_invalidranges
);
3065 // Move the fork data (copy whole structure)
3066 dstfork
->ff_data
= srcfork
->ff_data
;
3067 bzero(&srcfork
->ff_data
, sizeof(srcfork
->ff_data
));
3070 src_cp
->c_blocks
-= dstfork
->ff_blocks
+ dstfork
->ff_unallocblocks
;
3071 dst_cp
->c_blocks
+= dstfork
->ff_blocks
+ dstfork
->ff_unallocblocks
;
3077 * cnode must be locked
3080 hfs_fsync(struct vnode
*vp
, int waitfor
, hfs_fsync_mode_t fsyncmode
, struct proc
*p
)
3082 struct cnode
*cp
= VTOC(vp
);
3083 struct filefork
*fp
= NULL
;
3085 struct hfsmount
*hfsmp
= VTOHFS(vp
);
3087 int waitdata
; /* attributes necessary for data retrieval */
3088 int wait
; /* all other attributes (e.g. atime, etc.) */
3089 int took_trunc_lock
= 0;
3090 int fsync_default
= 1;
3093 * Applications which only care about data integrity rather than full
3094 * file integrity may opt out of (delay) expensive metadata update
3095 * operations as a performance optimization.
3097 wait
= (waitfor
== MNT_WAIT
);
3098 waitdata
= (waitfor
== MNT_DWAIT
) | wait
;
3100 if (always_do_fullfsync
)
3101 fsyncmode
= HFS_FSYNC_FULL
;
3102 if (fsyncmode
!= HFS_FSYNC
)
3105 /* HFS directories don't have any data blocks. */
3106 if (vnode_isdir(vp
))
3111 * For system files flush the B-tree header and
3112 * for regular files write out any clusters
3114 if (vnode_issystem(vp
)) {
3115 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
3117 if (hfsmp
->jnl
== NULL
) {
3118 BTFlushPath(VTOF(vp
));
3123 hfs_lock_truncate(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
3124 took_trunc_lock
= 1;
3126 if (fp
->ff_unallocblocks
!= 0) {
3127 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3129 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
3132 /* Don't hold cnode lock when calling into cluster layer. */
3133 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
3135 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
3138 * When MNT_WAIT is requested and the zero fill timeout
3139 * has expired then we must explicitly zero out any areas
3140 * that are currently marked invalid (holes).
3142 * Files with NODUMP can bypass zero filling here.
3144 if (fp
&& (((cp
->c_flag
& C_ALWAYS_ZEROFILL
) && !TAILQ_EMPTY(&fp
->ff_invalidranges
)) ||
3145 ((wait
|| (cp
->c_flag
& C_ZFWANTSYNC
)) &&
3146 ((cp
->c_bsdflags
& UF_NODUMP
) == 0) &&
3147 (vnode_issystem(vp
) ==0) &&
3148 cp
->c_zftimeout
!= 0))) {
3151 if ((cp
->c_flag
& C_ALWAYS_ZEROFILL
) == 0 && fsync_default
&& tv
.tv_sec
< (long)cp
->c_zftimeout
) {
3152 /* Remember that a force sync was requested. */
3153 cp
->c_flag
|= C_ZFWANTSYNC
;
3156 if (!TAILQ_EMPTY(&fp
->ff_invalidranges
)) {
3157 if (!took_trunc_lock
|| (cp
->c_truncatelockowner
== HFS_SHARED_OWNER
)) {
3159 if (took_trunc_lock
) {
3160 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3162 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
3163 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
3164 took_trunc_lock
= 1;
3166 hfs_flush_invalid_ranges(vp
);
3168 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
3169 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
3173 if (took_trunc_lock
) {
3174 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3175 took_trunc_lock
= 0;
3179 buf_flushdirtyblks(vp
, waitdata
, 0, "hfs_fsync");
3180 else if (fsync_default
&& vnode_islnk(vp
)
3181 && vnode_hasdirtyblks(vp
) && vnode_isrecycled(vp
)) {
3183 * If it's a symlink that's dirty and is about to be recycled,
3184 * we need to flush the journal.
3190 if (vnode_isreg(vp
) && vnode_issystem(vp
)) {
3191 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
3193 BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
3195 cp
->c_touch_acctime
= FALSE
;
3196 cp
->c_touch_chgtime
= FALSE
;
3197 cp
->c_touch_modtime
= FALSE
;
3198 } else if (!vnode_isswap(vp
)) {
3199 retval
= hfs_update(vp
, HFS_UPDATE_FORCE
);
3202 * When MNT_WAIT is requested push out the catalog record for
3203 * this file. If they asked for a full fsync, we can skip this
3204 * because the journal_flush or hfs_metasync_all will push out
3205 * all of the metadata changes.
3207 if ((retval
== 0) && wait
&& fsync_default
&& cp
->c_hint
&&
3208 !ISSET(cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
3209 hfs_metasync(VTOHFS(vp
), (daddr64_t
)cp
->c_hint
, p
);
3213 * If this was a full fsync, make sure all metadata
3214 * changes get to stable storage.
3216 if (!fsync_default
) {
3218 if (fsyncmode
== HFS_FSYNC_FULL
)
3219 hfs_flush(hfsmp
, HFS_FLUSH_FULL
);
3221 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_BARRIER
);
3223 retval
= hfs_metasync_all(hfsmp
);
3224 /* XXX need to pass context! */
3225 hfs_flush(hfsmp
, HFS_FLUSH_CACHE
);
3230 if (!hfs_is_dirty(cp
) && !ISSET(cp
->c_flag
, C_DELETED
))
3231 vnode_cleardirty(vp
);
3237 /* Sync an hfs catalog b-tree node */
3239 hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
)
3245 vp
= HFSTOVCB(hfsmp
)->catalogRefNum
;
3247 // XXXdbg - don't need to do this on a journaled volume
3252 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3254 * Look for a matching node that has been delayed
3255 * but is not part of a set (B_LOCKED).
3257 * BLK_ONLYVALID causes buf_getblk to return a
3258 * buf_t for the daddr64_t specified only if it's
3259 * currently resident in the cache... the size
3260 * parameter to buf_getblk is ignored when this flag
3263 bp
= buf_getblk(vp
, node
, 0, 0, 0, BLK_META
| BLK_ONLYVALID
);
3266 if ((buf_flags(bp
) & (B_LOCKED
| B_DELWRI
)) == B_DELWRI
)
3267 (void) VNOP_BWRITE(bp
);
3272 hfs_systemfile_unlock(hfsmp
, lockflags
);
3279 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
3280 * without a journal. Note that the volume bitmap does not get written;
3281 * we rely on fsck_hfs to fix that up (which it can do without any loss
3285 hfs_metasync_all(struct hfsmount
*hfsmp
)
3289 /* Lock all of the B-trees so we get a mutually consistent state */
3290 lockflags
= hfs_systemfile_lock(hfsmp
,
3291 SFL_CATALOG
|SFL_EXTENTS
|SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
3293 /* Sync each of the B-trees */
3294 if (hfsmp
->hfs_catalog_vp
)
3295 hfs_btsync(hfsmp
->hfs_catalog_vp
, 0);
3296 if (hfsmp
->hfs_extents_vp
)
3297 hfs_btsync(hfsmp
->hfs_extents_vp
, 0);
3298 if (hfsmp
->hfs_attribute_vp
)
3299 hfs_btsync(hfsmp
->hfs_attribute_vp
, 0);
3301 /* Wait for all of the writes to complete */
3302 if (hfsmp
->hfs_catalog_vp
)
3303 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_metasync_all");
3304 if (hfsmp
->hfs_extents_vp
)
3305 vnode_waitforwrites(hfsmp
->hfs_extents_vp
, 0, 0, 0, "hfs_metasync_all");
3306 if (hfsmp
->hfs_attribute_vp
)
3307 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs_metasync_all");
3309 hfs_systemfile_unlock(hfsmp
, lockflags
);
3317 hfs_btsync_callback(struct buf
*bp
, __unused
void *dummy
)
3319 buf_clearflags(bp
, B_LOCKED
);
3320 (void) buf_bawrite(bp
);
3322 return(BUF_CLAIMED
);
3327 hfs_btsync(struct vnode
*vp
, int sync_transaction
)
3329 struct cnode
*cp
= VTOC(vp
);
3333 if (sync_transaction
)
3334 flags
|= BUF_SKIP_NONLOCKED
;
3336 * Flush all dirty buffers associated with b-tree.
3338 buf_iterate(vp
, hfs_btsync_callback
, flags
, 0);
3341 if (vnode_issystem(vp
) && (VTOF(vp
)->fcbBTCBPtr
!= NULL
))
3342 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
3343 cp
->c_touch_acctime
= FALSE
;
3344 cp
->c_touch_chgtime
= FALSE
;
3345 cp
->c_touch_modtime
= FALSE
;
3351 * Remove a directory.
3354 hfs_vnop_rmdir(struct vnop_rmdir_args
*ap
)
3356 struct vnode
*dvp
= ap
->a_dvp
;
3357 struct vnode
*vp
= ap
->a_vp
;
3358 struct cnode
*dcp
= VTOC(dvp
);
3359 struct cnode
*cp
= VTOC(vp
);
3363 orig_ctime
= VTOC(vp
)->c_ctime
;
3365 if (!S_ISDIR(cp
->c_mode
)) {
3372 nspace_snapshot_event(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
3375 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
3379 /* Check for a race with rmdir on the parent directory */
3380 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
3381 hfs_unlockpair (dcp
, cp
);
3386 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
3388 if ((cp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
3391 hfs_unlockpair(dcp
, cp
);
3393 if (hfs_generate_document_id(VTOHFS(vp
), &newid
) == 0) {
3394 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3395 ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
3396 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
3397 FSE_ARG_DEV
, VTOHFS(vp
)->hfs_raw_dev
,
3398 FSE_ARG_INO
, (ino64_t
)0, // src inode #
3399 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
3400 FSE_ARG_INT32
, newid
,
3403 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rm...
3404 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3408 error
= hfs_removedir(dvp
, vp
, ap
->a_cnp
, 0, 0);
3410 hfs_unlockpair(dcp
, cp
);
3416 * Remove a directory
3418 * Both dvp and vp cnodes are locked
3421 hfs_removedir(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
3422 int skip_reserve
, int only_unlink
)
3426 struct hfsmount
* hfsmp
;
3427 struct cat_desc desc
;
3429 int error
= 0, started_tr
= 0;
3436 return (EINVAL
); /* cannot remove "." */
3438 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3441 if (cp
->c_entries
!= 0) {
3446 * If the directory is open or in use (e.g. opendir() or current working
3447 * directory for some process); wait for inactive/reclaim to actually
3448 * remove cnode from the catalog. Both inactive and reclaim codepaths are capable
3449 * of removing open-unlinked directories from the catalog, as well as getting rid
3450 * of EAs still on the element. So change only_unlink to true, so that it will get
3453 * Otherwise, we can get into a weird old mess where the directory has C_DELETED,
3454 * but it really means C_NOEXISTS because the item was actually removed from the
3455 * catalog. Then when we try to remove the entry from the catalog later on, it won't
3456 * really be there anymore.
3458 if (vnode_isinuse(vp
, 0)) {
3462 /* Deal with directory hardlinks */
3463 if (cp
->c_flag
& C_HARDLINK
) {
3465 * Note that if we have a directory which was a hardlink at any point,
3466 * its actual directory data is stored in the directory inode in the hidden
3467 * directory rather than the leaf element(s) present in the namespace.
3469 * If there are still other hardlinks to this directory,
3470 * then we'll just eliminate this particular link and the vnode will still exist.
3471 * If this is the last link to an empty directory, then we'll open-unlink the
3472 * directory and it will be only tagged with C_DELETED (as opposed to C_NOEXISTS).
3474 * We could also return EBUSY here.
3477 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
3481 * In a few cases, we may want to allow the directory to persist in an
3482 * open-unlinked state. If the directory is being open-unlinked (still has usecount
3483 * references), or if it has EAs, or if it was being deleted as part of a rename,
3484 * then we go ahead and move it to the hidden directory.
3486 * If the directory is being open-unlinked, then we want to keep the catalog entry
3487 * alive so that future EA calls and fchmod/fstat etc. do not cause issues later.
3489 * If the directory had EAs, then we want to use the open-unlink trick so that the
3490 * EA removal is not done in one giant transaction. Otherwise, it could cause a panic
3491 * due to overflowing the journal.
3493 * Finally, if it was deleted as part of a rename, we move it to the hidden directory
3494 * in order to maintain rename atomicity.
3496 * Note that the allow_dirs argument to hfs_removefile specifies that it is
3497 * supposed to handle directories for this case.
3500 if (((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3501 ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0)) ||
3502 (only_unlink
!= 0)) {
3504 int ret
= hfs_removefile(dvp
, vp
, cnp
, 0, 0, 1, NULL
, only_unlink
);
3506 * Even though hfs_vnop_rename calls vnode_recycle for us on tvp we call
3507 * it here just in case we were invoked by rmdir() on a directory that had
3508 * EAs. To ensure that we start reclaiming the space as soon as possible,
3509 * we call vnode_recycle on the directory.
3517 dcp
->c_flag
|= C_DIR_MODIFICATION
;
3520 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3521 (void)hfs_getinoquota(cp
);
3523 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3529 * Verify the directory is empty (and valid).
3530 * (Rmdir ".." won't be valid since
3531 * ".." will contain a reference to
3532 * the current directory and thus be
3535 if ((dcp
->c_bsdflags
& APPEND
) || (cp
->c_bsdflags
& (IMMUTABLE
| APPEND
))) {
3540 /* Remove the entry from the namei cache: */
3544 * Protect against a race with rename by using the component
3545 * name passed in and parent id from dvp (instead of using
3546 * the cp->c_desc which may have changed).
3548 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
3549 desc
.cd_namelen
= cnp
->cn_namelen
;
3550 desc
.cd_parentcnid
= dcp
->c_fileid
;
3551 desc
.cd_cnid
= cp
->c_cnid
;
3552 desc
.cd_flags
= CD_ISDIR
;
3553 desc
.cd_encoding
= cp
->c_encoding
;
3556 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
3561 /* Remove entry from catalog */
3562 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3564 if (!skip_reserve
) {
3566 * Reserve some space in the Catalog file.
3568 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
3569 hfs_systemfile_unlock(hfsmp
, lockflags
);
3574 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
3578 // if skip_reserve == 1 then we're being called from hfs_vnop_rename() and thus
3579 // we don't need to touch the document_id as it's handled by the rename code.
3580 // otherwise it's a normal remove and we need to save the document id in the
3581 // per thread struct and clear it from the cnode.
3583 struct doc_tombstone
*ut
;
3584 ut
= doc_tombstone_get();
3585 if (!skip_reserve
&& (cp
->c_bsdflags
& UF_TRACKED
)
3586 && doc_tombstone_should_save(ut
, vp
, cnp
)) {
3588 uint32_t doc_id
= hfs_get_document_id(cp
);
3590 // this event is more of a "pending-delete"
3591 if (ut
->t_lastop_document_id
) {
3592 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
3593 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
3594 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // src inode #
3595 FSE_ARG_INO
, (ino64_t
)0, // dst inode #
3596 FSE_ARG_INT32
, doc_id
,
3600 doc_tombstone_save(dvp
, vp
, cnp
, doc_id
, cp
->c_fileid
);
3602 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
3604 // clear this so it's never returned again
3605 fip
->document_id
= 0;
3606 cp
->c_bsdflags
&= ~UF_TRACKED
;
3609 /* The parent lost a child */
3610 if (dcp
->c_entries
> 0)
3612 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
3613 dcp
->c_dirchangecnt
++;
3614 hfs_incr_gencount(dcp
);
3616 dcp
->c_touch_chgtime
= TRUE
;
3617 dcp
->c_touch_modtime
= TRUE
;
3618 dcp
->c_flag
|= C_MODIFIED
;
3620 hfs_update(dcp
->c_vp
, 0);
3623 hfs_systemfile_unlock(hfsmp
, lockflags
);
3629 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3630 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
3633 hfs_volupdate(hfsmp
, VOL_RMDIR
, (dcp
->c_cnid
== kHFSRootFolderID
));
3635 /* Mark C_NOEXISTS since the catalog entry is now gone */
3636 cp
->c_flag
|= C_NOEXISTS
;
3639 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3640 wakeup((caddr_t
)&dcp
->c_flag
);
3643 hfs_end_transaction(hfsmp
);
3651 * Remove a file or link.
3654 hfs_vnop_remove(struct vnop_remove_args
*ap
)
3656 struct vnode
*dvp
= ap
->a_dvp
;
3657 struct vnode
*vp
= ap
->a_vp
;
3658 struct cnode
*dcp
= VTOC(dvp
);
3660 struct vnode
*rvp
= NULL
;
3661 int error
=0, recycle_rsrc
=0;
3662 int recycle_vnode
= 0;
3663 uint32_t rsrc_vid
= 0;
3670 orig_ctime
= VTOC(vp
)->c_ctime
;
3671 if (!vnode_isnamedstream(vp
) && ((ap
->a_flags
& VNODE_REMOVE_SKIP_NAMESPACE_EVENT
) == 0)) {
3672 error
= nspace_snapshot_event(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
3674 // XXXdbg - decide on a policy for handling namespace handler failures!
3675 // for now we just let them proceed.
3684 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
3686 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
3687 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3694 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
3696 if ((cp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
3699 hfs_unlockpair(dcp
, cp
);
3701 if (hfs_generate_document_id(VTOHFS(vp
), &newid
) == 0) {
3702 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3703 ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
3704 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
3705 FSE_ARG_DEV
, VTOHFS(vp
)->hfs_raw_dev
,
3706 FSE_ARG_INO
, (ino64_t
)0, // src inode #
3707 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
3708 FSE_ARG_INT32
, newid
,
3711 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rm...
3712 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3717 * Lazily respond to determining if there is a valid resource fork
3718 * vnode attached to 'cp' if it is a regular file or symlink.
3719 * If the vnode does not exist, then we may proceed without having to
3722 * If, however, it does exist, then we need to acquire an iocount on the
3723 * vnode after acquiring its vid. This ensures that if we have to do I/O
3724 * against it, it can't get recycled from underneath us in the middle
3727 * Note: this function may be invoked for directory hardlinks, so just skip these
3728 * steps if 'vp' is a directory.
3731 enum vtype vtype
= vnode_vtype(vp
);
3732 if ((vtype
== VLNK
) || (vtype
== VREG
)) {
3733 if ((cp
->c_rsrc_vp
) && (rvp
== NULL
)) {
3734 /* We need to acquire the rsrc vnode */
3735 rvp
= cp
->c_rsrc_vp
;
3736 rsrc_vid
= vnode_vid (rvp
);
3738 /* Unlock everything to acquire iocount on the rsrc vnode */
3739 hfs_unlock_truncate (cp
, HFS_LOCK_DEFAULT
);
3740 hfs_unlockpair (dcp
, cp
);
3741 /* Use the vid to maintain identity on rvp */
3742 if (vnode_getwithvid(rvp
, rsrc_vid
)) {
3744 * If this fails, then it was recycled or
3745 * reclaimed in the interim. Reset fields and
3756 * Check to see if we raced rmdir for the parent directory
3757 * hfs_removefile already checks for a race on vp/cp
3759 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
3764 error
= hfs_removefile(dvp
, vp
, ap
->a_cnp
, ap
->a_flags
, 0, 0, NULL
, 0);
3767 * If the remove succeeded in deleting the file, then we may need to mark
3768 * the resource fork for recycle so that it is reclaimed as quickly
3769 * as possible. If it were not recycled quickly, then this resource fork
3770 * vnode could keep a v_parent reference on the data fork, which prevents it
3771 * from going through reclaim (by giving it extra usecounts), except in the force-
3774 * However, a caveat: we need to continue to supply resource fork
3775 * access to open-unlinked files even if the resource fork is not open. This is
3776 * a requirement for the compressed files work. Luckily, hfs_vgetrsrc will handle
3777 * this already if the data fork has been re-parented to the hidden directory.
3779 * As a result, all we really need to do here is mark the resource fork vnode
3780 * for recycle. If it goes out of core, it can be brought in again if needed.
3781 * If the cnode was instead marked C_NOEXISTS, then there wouldn't be any
3785 hfs_hotfile_deleted(vp
);
3791 * If the target was actually removed from the catalog schedule it for
3792 * full reclamation/inactivation. We hold an iocount on it so it should just
3793 * get marked with MARKTERM
3795 if (cp
->c_flag
& C_NOEXISTS
) {
3802 * Drop the truncate lock before unlocking the cnode
3803 * (which can potentially perform a vnode_put and
3804 * recycle the vnode which in turn might require the
3808 hfs_unlockpair(dcp
, cp
);
3809 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3812 /* inactive or reclaim on rvp will clean up the blocks from the rsrc fork */
3815 if (recycle_vnode
) {
3820 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
3829 hfs_removefile_callback(struct buf
*bp
, void *hfsmp
) {
3831 if ( !(buf_flags(bp
) & B_META
))
3832 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp
);
3834 * it's part of the current transaction, kill it.
3836 journal_kill_block(((struct hfsmount
*)hfsmp
)->jnl
, bp
);
3838 return (BUF_CLAIMED
);
3844 * Similar to hfs_vnop_remove except there are additional options.
3845 * This function may be used to remove directories if they have
3846 * lots of EA's -- note the 'allow_dirs' argument.
3848 * This function is able to delete blocks & fork data for the resource
3849 * fork even if it does not exist in core (and have a backing vnode).
3850 * It should infer the correct behavior based on the number of blocks
3851 * in the cnode and whether or not the resource fork pointer exists or
3852 * not. As a result, one only need pass in the 'vp' corresponding to the
3853 * data fork of this file (or main vnode in the case of a directory).
3854 * Passing in a resource fork will result in an error.
3856 * Because we do not create any vnodes in this function, we are not at
3857 * risk of deadlocking against ourselves by double-locking.
3859 * Requires cnode and truncate locks to be held.
3862 hfs_removefile(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
3863 int flags
, int skip_reserve
, int allow_dirs
,
3864 __unused
struct vnode
*rvp
, int only_unlink
)
3868 struct vnode
*rsrc_vp
= NULL
;
3869 struct hfsmount
*hfsmp
;
3870 struct cat_desc desc
;
3872 int dataforkbusy
= 0;
3873 int rsrcforkbusy
= 0;
3877 int isbigfile
= 0, defer_remove
=0, isdir
=0;
3884 /* Check if we lost a race post lookup. */
3885 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3889 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
3893 /* Make sure a remove is permitted */
3894 if (VNODE_IS_RSRC(vp
)) {
3899 * We know it's a data fork.
3900 * Probe the cnode to see if we have a valid resource fork
3903 rsrc_vp
= cp
->c_rsrc_vp
;
3906 /* Don't allow deleting the journal or journal_info_block. */
3907 if (hfs_is_journal_file(hfsmp
, cp
)) {
3912 * Hard links require special handling.
3914 if (cp
->c_flag
& C_HARDLINK
) {
3915 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) && vnode_isinuse(vp
, 0)) {
3918 /* A directory hard link with a link count of one is
3919 * treated as a regular directory. Therefore it should
3920 * only be removed using rmdir().
3922 if ((vnode_isdir(vp
) == 1) && (cp
->c_linkcount
== 1) &&
3923 (allow_dirs
== 0)) {
3926 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
3930 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
3931 if (vnode_isdir(vp
)) {
3932 if (allow_dirs
== 0)
3933 return (EPERM
); /* POSIX */
3936 /* Sanity check the parent ids. */
3937 if ((cp
->c_parentcnid
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
3938 (cp
->c_parentcnid
!= dcp
->c_fileid
)) {
3942 dcp
->c_flag
|= C_DIR_MODIFICATION
;
3944 // this guy is going away so mark him as such
3945 cp
->c_flag
|= C_DELETED
;
3948 /* Remove our entry from the namei cache. */
3952 * If the caller was operating on a file (as opposed to a
3953 * directory with EAs), then we need to figure out
3954 * whether or not it has a valid resource fork vnode.
3956 * If there was a valid resource fork vnode, then we need
3957 * to use hfs_truncate to eliminate its data. If there is
3958 * no vnode, then we hold the cnode lock which would
3959 * prevent it from being created. As a result,
3960 * we can use the data deletion functions which do not
3961 * require that a cnode/vnode pair exist.
3964 /* Check if this file is being used. */
3966 dataforkbusy
= vnode_isinuse(vp
, 0);
3968 * At this point, we know that 'vp' points to the
3969 * a data fork because we checked it up front. And if
3970 * there is no rsrc fork, rsrc_vp will be NULL.
3972 if (rsrc_vp
&& (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
3973 rsrcforkbusy
= vnode_isinuse(rsrc_vp
, 0);
3977 /* Check if we have to break the deletion into multiple pieces. */
3979 isbigfile
= cp
->c_datafork
->ff_size
>= HFS_BIGFILE_SIZE
;
3981 /* Check if the file has xattrs. If it does we'll have to delete them in
3982 individual transactions in case there are too many */
3983 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3984 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
3988 /* If we are explicitly told to only unlink item and move to hidden dir, then do it */
3994 * Carbon semantics prohibit deleting busy files.
3995 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
3997 if (dataforkbusy
|| rsrcforkbusy
) {
3998 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) ||
3999 (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0)) {
4006 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
4007 (void)hfs_getinoquota(cp
);
4011 * Do a ubc_setsize to indicate we need to wipe contents if:
4012 * 1) item is a regular file.
4013 * 2) Neither fork is busy AND we are not told to unlink this.
4015 * We need to check for the defer_remove since it can be set without
4016 * having a busy data or rsrc fork
4018 if (isdir
== 0 && (!dataforkbusy
|| !rsrcforkbusy
) && (defer_remove
== 0)) {
4020 * A ubc_setsize can cause a pagein so defer it
4021 * until after the cnode lock is dropped. The
4022 * cnode lock cannot be dropped/reacquired here
4023 * since we might already hold the journal lock.
4025 if (!dataforkbusy
&& cp
->c_datafork
->ff_blocks
&& !isbigfile
) {
4026 cp
->c_flag
|= C_NEED_DATA_SETSIZE
;
4028 if (!rsrcforkbusy
&& rsrc_vp
) {
4029 cp
->c_flag
|= C_NEED_RSRC_SETSIZE
;
4033 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4038 // XXXdbg - if we're journaled, kill any dirty symlink buffers
4039 if (hfsmp
->jnl
&& vnode_islnk(vp
) && (defer_remove
== 0)) {
4040 buf_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
4044 * Prepare to truncate any non-busy forks. Busy forks will
4045 * get truncated when their vnode goes inactive.
4046 * Note that we will only enter this region if we
4047 * can avoid creating an open-unlinked file. If
4048 * either region is busy, we will have to create an open
4051 * Since we are deleting the file, we need to stagger the runtime
4052 * modifications to do things in such a way that a crash won't
4053 * result in us getting overlapped extents or any other
4054 * bad inconsistencies. As such, we call prepare_release_storage
4055 * which updates the UBC, updates quota information, and releases
4056 * any loaned blocks that belong to this file. No actual
4057 * truncation or bitmap manipulation is done until *AFTER*
4058 * the catalog record is removed.
4060 if (isdir
== 0 && (!dataforkbusy
&& !rsrcforkbusy
) && (only_unlink
== 0)) {
4062 if (!dataforkbusy
&& !isbigfile
&& cp
->c_datafork
->ff_blocks
!= 0) {
4064 error
= hfs_prepare_release_storage (hfsmp
, vp
);
4072 * If the resource fork vnode does not exist, we can skip this step.
4074 if (!rsrcforkbusy
&& rsrc_vp
) {
4075 error
= hfs_prepare_release_storage (hfsmp
, rsrc_vp
);
4084 * Protect against a race with rename by using the component
4085 * name passed in and parent id from dvp (instead of using
4086 * the cp->c_desc which may have changed). Also, be aware that
4087 * because we allow directories to be passed in, we need to special case
4088 * this temporary descriptor in case we were handed a directory.
4091 desc
.cd_flags
= CD_ISDIR
;
4096 desc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
4097 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
4098 desc
.cd_namelen
= cnp
->cn_namelen
;
4099 desc
.cd_parentcnid
= dcp
->c_fileid
;
4100 desc
.cd_hint
= cp
->c_desc
.cd_hint
;
4101 desc
.cd_cnid
= cp
->c_cnid
;
4105 * There are two cases to consider:
4106 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
4107 * 2. File is not in use ==> remove the file
4109 * We can get a directory in case 1 because it may have had lots of attributes,
4110 * which need to get removed here.
4112 if (dataforkbusy
|| rsrcforkbusy
|| isbigfile
|| defer_remove
) {
4114 struct cat_desc to_desc
;
4115 struct cat_desc todir_desc
;
4118 * Orphan this file or directory (move to hidden directory).
4119 * Again, we need to take care that we treat directories as directories,
4120 * and files as files. Because directories with attributes can be passed in
4121 * check to make sure that we have a directory or a file before filling in the
4122 * temporary descriptor's flags. We keep orphaned directories AND files in
4123 * the FILE_HARDLINKS private directory since we're generalizing over all
4124 * orphaned filesystem objects.
4126 bzero(&todir_desc
, sizeof(todir_desc
));
4127 todir_desc
.cd_parentcnid
= 2;
4129 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
4130 bzero(&to_desc
, sizeof(to_desc
));
4131 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
4132 to_desc
.cd_namelen
= strlen(delname
);
4133 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
4135 to_desc
.cd_flags
= CD_ISDIR
;
4138 to_desc
.cd_flags
= 0;
4140 to_desc
.cd_cnid
= cp
->c_cnid
;
4142 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4143 if (!skip_reserve
) {
4144 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, NULL
, 0))) {
4145 hfs_systemfile_unlock(hfsmp
, lockflags
);
4150 error
= cat_rename(hfsmp
, &desc
, &todir_desc
,
4151 &to_desc
, (struct cat_desc
*)NULL
);
4154 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
4156 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
4158 (void) cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
4159 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
4161 /* Update the parent directory */
4162 if (dcp
->c_entries
> 0)
4165 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
4167 dcp
->c_dirchangecnt
++;
4168 hfs_incr_gencount(dcp
);
4170 dcp
->c_ctime
= tv
.tv_sec
;
4171 dcp
->c_mtime
= tv
.tv_sec
;
4172 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
4174 /* Update the file or directory's state */
4175 cp
->c_flag
|= C_DELETED
;
4176 cp
->c_ctime
= tv
.tv_sec
;
4178 (void) cat_update(hfsmp
, &to_desc
, &cp
->c_attr
, NULL
, NULL
);
4180 hfs_systemfile_unlock(hfsmp
, lockflags
);
4187 * Nobody is using this item; we can safely remove everything.
4189 struct filefork
*temp_rsrc_fork
= NULL
;
4192 int blksize
= hfsmp
->blockSize
;
4194 u_int32_t fileid
= cp
->c_fileid
;
4197 * Figure out if we need to read the resource fork data into
4198 * core before wiping out the catalog record.
4200 * 1) Must not be a directory
4201 * 2) cnode's c_rsrcfork ptr must be NULL.
4202 * 3) rsrc fork must have actual blocks
4204 if ((isdir
== 0) && (cp
->c_rsrcfork
== NULL
) &&
4205 (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
4207 * The resource fork vnode & filefork did not exist.
4208 * Create a temporary one for use in this function only.
4210 temp_rsrc_fork
= hfs_zalloc(HFS_FILEFORK_ZONE
);
4211 bzero(temp_rsrc_fork
, sizeof(struct filefork
));
4212 temp_rsrc_fork
->ff_cp
= cp
;
4213 rl_init(&temp_rsrc_fork
->ff_invalidranges
);
4216 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4218 /* Look up the resource fork first, if necessary */
4219 if (temp_rsrc_fork
) {
4220 error
= cat_lookup (hfsmp
, &desc
, 1, 0, (struct cat_desc
*) NULL
,
4221 (struct cat_attr
*) NULL
, &temp_rsrc_fork
->ff_data
, NULL
);
4223 hfs_zfree(temp_rsrc_fork
, HFS_FILEFORK_ZONE
);
4224 hfs_systemfile_unlock (hfsmp
, lockflags
);
4229 if (!skip_reserve
) {
4230 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
4231 if (temp_rsrc_fork
) {
4232 hfs_zfree(temp_rsrc_fork
, HFS_FILEFORK_ZONE
);
4234 hfs_systemfile_unlock(hfsmp
, lockflags
);
4239 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
4241 if (error
&& error
!= ENXIO
&& error
!= ENOENT
) {
4242 printf("hfs_removefile: deleting file %s (id=%d) vol=%s err=%d\n",
4243 cp
->c_desc
.cd_nameptr
, cp
->c_attr
.ca_fileid
, hfsmp
->vcbVN
, error
);
4247 /* Update the parent directory */
4248 if (dcp
->c_entries
> 0)
4250 dcp
->c_dirchangecnt
++;
4251 hfs_incr_gencount(dcp
);
4253 dcp
->c_ctime
= tv
.tv_sec
;
4254 dcp
->c_mtime
= tv
.tv_sec
;
4255 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
4257 hfs_systemfile_unlock(hfsmp
, lockflags
);
4260 if (temp_rsrc_fork
) {
4261 hfs_zfree(temp_rsrc_fork
, HFS_FILEFORK_ZONE
);
4267 * Now that we've wiped out the catalog record, the file effectively doesn't
4268 * exist anymore. So update the quota records to reflect the loss of the
4269 * data fork and the resource fork.
4272 if (cp
->c_datafork
->ff_blocks
> 0) {
4273 savedbytes
= ((off_t
)cp
->c_datafork
->ff_blocks
* (off_t
)blksize
);
4274 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
4278 * We may have just deleted the catalog record for a resource fork even
4279 * though it did not exist in core as a vnode. However, just because there
4280 * was a resource fork pointer in the cnode does not mean that it had any blocks.
4282 if (temp_rsrc_fork
|| cp
->c_rsrcfork
) {
4283 if (cp
->c_rsrcfork
) {
4284 if (cp
->c_rsrcfork
->ff_blocks
> 0) {
4285 savedbytes
= ((off_t
)cp
->c_rsrcfork
->ff_blocks
* (off_t
)blksize
);
4286 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
4290 /* we must have used a temporary fork */
4291 savedbytes
= ((off_t
)temp_rsrc_fork
->ff_blocks
* (off_t
)blksize
);
4292 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
4296 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
4297 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
4301 if (vnode_islnk(vp
) && cp
->c_datafork
->ff_symlinkptr
) {
4302 hfs_free(cp
->c_datafork
->ff_symlinkptr
, cp
->c_datafork
->ff_size
);
4303 cp
->c_datafork
->ff_symlinkptr
= NULL
;
4307 * If we didn't get any errors deleting the catalog entry, then go ahead
4308 * and release the backing store now. The filefork pointers are still valid.
4310 if (temp_rsrc_fork
) {
4311 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, temp_rsrc_fork
, fileid
);
4314 /* if cp->c_rsrcfork == NULL, hfs_release_storage will skip over it. */
4315 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, cp
->c_rsrcfork
, fileid
);
4319 * If we encountered an error updating the extents and bitmap,
4320 * mark the volume inconsistent. At this point, the catalog record has
4321 * already been deleted, so we can't recover it at this point. We need
4322 * to proceed and update the volume header and mark the cnode C_NOEXISTS.
4323 * The subsequent fsck should be able to recover the free space for us.
4325 hfs_mark_inconsistent(hfsmp
, HFS_OP_INCOMPLETE
);
4328 /* reset update_vh to 0, since hfs_release_storage should have done it for us */
4332 /* Get rid of the temporary rsrc fork */
4333 if (temp_rsrc_fork
) {
4334 hfs_zfree(temp_rsrc_fork
, HFS_FILEFORK_ZONE
);
4337 cp
->c_flag
|= C_NOEXISTS
;
4338 cp
->c_flag
&= ~C_DELETED
;
4340 cp
->c_touch_chgtime
= TRUE
;
4344 * We must never get a directory if we're in this else block. We could
4345 * accidentally drop the number of files in the volume header if we did.
4347 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
4352 // if skip_reserve == 1 then we're being called from hfs_vnop_rename() and thus
4353 // we don't need to touch the document_id as it's handled by the rename code.
4354 // otherwise it's a normal remove and we need to save the document id in the
4355 // per thread struct and clear it from the cnode.
4357 if (!error
&& !skip_reserve
&& (cp
->c_bsdflags
& UF_TRACKED
)
4358 && cp
->c_linkcount
<= 1) {
4359 struct doc_tombstone
*ut
;
4360 ut
= doc_tombstone_get();
4361 if (doc_tombstone_should_save(ut
, vp
, cnp
)) {
4362 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4363 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4364 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // src inode #
4365 FSE_ARG_INO
, (ino64_t
)0, // dst inode #
4366 FSE_ARG_INT32
, hfs_get_document_id(cp
), // document id
4369 doc_tombstone_save(dvp
, vp
, cnp
, hfs_get_document_id(cp
),
4372 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
4374 fip
->document_id
= 0;
4375 cp
->c_bsdflags
&= ~UF_TRACKED
;
4380 * All done with this cnode's descriptor...
4382 * Note: all future catalog calls for this cnode must be by
4383 * fileid only. This is OK for HFS (which doesn't have file
4384 * thread records) since HFS doesn't support the removal of
4387 cat_releasedesc(&cp
->c_desc
);
4391 cp
->c_flag
&= ~C_DELETED
;
4396 * If we bailed out earlier, we may need to update the volume header
4397 * to deal with the borrowed blocks accounting.
4399 hfs_volupdate (hfsmp
, VOL_UPDATE
, 0);
4403 hfs_end_transaction(hfsmp
);
4406 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4407 wakeup((caddr_t
)&dcp
->c_flag
);
4414 replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
)
4416 // fixes 4348457 and 4463138
4417 if (&cp
->c_desc
== cdp
) {
4421 /* First release allocated name buffer */
4422 if (cp
->c_desc
.cd_flags
& CD_HASBUF
&& cp
->c_desc
.cd_nameptr
!= 0) {
4423 const u_int8_t
*name
= cp
->c_desc
.cd_nameptr
;
4425 cp
->c_desc
.cd_nameptr
= 0;
4426 cp
->c_desc
.cd_namelen
= 0;
4427 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
4428 vfs_removename((const char *)name
);
4430 bcopy(cdp
, &cp
->c_desc
, sizeof(cp
->c_desc
));
4432 /* Cnode now owns the name buffer */
4433 cdp
->cd_nameptr
= 0;
4434 cdp
->cd_namelen
= 0;
4435 cdp
->cd_flags
&= ~CD_HASBUF
;
4441 * Just forwards the arguments from VNOP_RENAME into those of
4442 * VNOP_RENAMEX but zeros out the flags word.
4444 int hfs_vnop_rename (struct vnop_rename_args
*args
) {
4445 struct vnop_renamex_args vrx
;
4447 vrx
.a_desc
= args
->a_desc
; // we aren't using it to switch into the vnop array, so fine as is.
4448 vrx
.a_fdvp
= args
->a_fdvp
;
4449 vrx
.a_fvp
= args
->a_fvp
;
4450 vrx
.a_fcnp
= args
->a_fcnp
;
4451 vrx
.a_tdvp
= args
->a_tdvp
;
4452 vrx
.a_tvp
= args
->a_tvp
;
4453 vrx
.a_tcnp
= args
->a_tcnp
;
4454 vrx
.a_vap
= NULL
; // not used
4455 vrx
.a_flags
= 0; //zero out the flags.
4456 vrx
.a_context
= args
->a_context
;
4458 return hfs_vnop_renamex (&vrx
);
4466 * The VFS layer guarantees that:
4467 * - source and destination will either both be directories, or
4468 * both not be directories.
4469 * - all the vnodes are from the same file system
4471 * When the target is a directory, HFS must ensure that its empty.
4473 * Note that this function requires up to 6 vnodes in order to work properly
4474 * if it is operating on files (and not on directories). This is because only
4475 * files can have resource forks, and we now require iocounts to be held on the
4476 * vnodes corresponding to the resource forks (if applicable) as well as
4477 * the files or directories undergoing rename. The problem with not holding
4478 * iocounts on the resource fork vnodes is that it can lead to a deadlock
4479 * situation: The rsrc fork of the source file may be recycled and reclaimed
4480 * in order to provide a vnode for the destination file's rsrc fork. Since
4481 * data and rsrc forks share the same cnode, we'd eventually try to lock the
4482 * source file's cnode in order to sync its rsrc fork to disk, but it's already
4483 * been locked. By taking the rsrc fork vnodes up front we ensure that they
4484 * cannot be recycled, and that the situation mentioned above cannot happen.
4487 hfs_vnop_renamex(struct vnop_renamex_args
*ap
)
4489 struct vnode
*tvp
= ap
->a_tvp
;
4490 struct vnode
*tdvp
= ap
->a_tdvp
;
4491 struct vnode
*fvp
= ap
->a_fvp
;
4492 struct vnode
*fdvp
= ap
->a_fdvp
;
4494 * Note that we only need locals for the target/destination's
4495 * resource fork vnode (and only if necessary). We don't care if the
4496 * source has a resource fork vnode or not.
4498 struct vnode
*tvp_rsrc
= NULLVP
;
4499 uint32_t tvp_rsrc_vid
= 0;
4500 struct componentname
*tcnp
= ap
->a_tcnp
;
4501 struct componentname
*fcnp
= ap
->a_fcnp
;
4502 struct proc
*p
= vfs_context_proc(ap
->a_context
);
4507 struct cnode
*error_cnode
;
4508 struct cat_desc from_desc
;
4509 struct cat_desc to_desc
;
4510 struct cat_desc out_desc
;
4511 struct hfsmount
*hfsmp
;
4512 cat_cookie_t cookie
;
4513 int tvp_deleted
= 0;
4514 int started_tr
= 0, got_cookie
= 0;
4515 int took_trunc_lock
= 0;
4518 time_t orig_from_ctime
, orig_to_ctime
;
4519 int emit_rename
= 1;
4520 int emit_delete
= 1;
4523 vnode_t old_doc_vp
= NULL
;
4524 int rename_exclusive
= 0;
4526 orig_from_ctime
= VTOC(fvp
)->c_ctime
;
4527 if (tvp
&& VTOC(tvp
)) {
4528 orig_to_ctime
= VTOC(tvp
)->c_ctime
;
4533 hfsmp
= VTOHFS(tdvp
);
4535 /* Check the flags first, so we can avoid grabbing locks if necessary */
4537 /* These are the only flags we support for now */
4538 if ((ap
->a_flags
& (VFS_RENAME_EXCL
)) == 0) {
4542 /* The rename flags are mutually exclusive for HFS+ */
4543 switch (ap
->a_flags
& VFS_RENAME_FLAGS_MASK
) {
4544 case VFS_RENAME_EXCL
:
4545 rename_exclusive
= true;
4553 * Do special case checks here. If fvp == tvp then we need to check the
4554 * cnode with locks held.
4557 int is_hardlink
= 0;
4559 * In this case, we do *NOT* ever emit a DELETE event.
4560 * We may not necessarily emit a RENAME event
4563 if ((error
= hfs_lock(VTOC(fvp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
4566 /* Check to see if the item is a hardlink or not */
4567 is_hardlink
= (VTOC(fvp
)->c_flag
& C_HARDLINK
);
4568 hfs_unlock (VTOC(fvp
));
4571 * If the item is not a hardlink, then case sensitivity must be off, otherwise
4572 * two names should not resolve to the same cnode unless they were case variants.
4577 * Hardlinks are a little trickier. We only want to emit a rename event
4578 * if the item is a hardlink, the parent directories are the same, case sensitivity
4579 * is off, and the case folded names are the same. See the fvp == tvp case below for more
4583 if ((fdvp
== tdvp
) && ((hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) == 0)) {
4584 if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4585 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
4586 /* Then in this case only it is ok to emit a rename */
4593 /* c_bsdflags should only be assessed while holding the cnode lock.
4594 * This is not done consistently throughout the code and can result
4595 * in race. This will be fixed via rdar://12181064
4597 if (VTOC(fvp
)->c_bsdflags
& UF_TRACKED
) {
4600 nspace_snapshot_event(fvp
, orig_from_ctime
, NAMESPACE_HANDLER_RENAME_OP
, NULL
);
4603 if (tvp
&& VTOC(tvp
)) {
4605 nspace_snapshot_event(tvp
, orig_to_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
4610 /* When tvp exists, take the truncate lock for hfs_removefile(). */
4611 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
4612 hfs_lock_truncate(VTOC(tvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4613 took_trunc_lock
= 1;
4617 error
= hfs_lockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
,
4618 HFS_EXCLUSIVE_LOCK
, &error_cnode
);
4620 if (took_trunc_lock
) {
4621 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4622 took_trunc_lock
= 0;
4626 * We hit an error path. If we were trying to re-acquire the locks
4627 * after coming through here once, we might have already obtained
4628 * an iocount on tvp's resource fork vnode. Drop that before dealing
4629 * with the failure. Note this is safe -- since we are in an
4630 * error handling path, we can't be holding the cnode locks.
4633 vnode_put (tvp_rsrc
);
4639 * tvp might no longer exist. If the cause of the lock failure
4640 * was tvp, then we can try again with tvp/tcp set to NULL.
4641 * This is ok because the vfs syscall will vnode_put the vnodes
4642 * after we return from hfs_vnop_rename.
4644 if ((error
== ENOENT
) && (tvp
!= NULL
) && (error_cnode
== VTOC(tvp
))) {
4650 /* If we want to reintroduce notifications for failed renames, this
4651 is the place to do it. */
4659 tcp
= tvp
? VTOC(tvp
) : NULL
;
4663 * If caller requested an exclusive rename (VFS_RENAME_EXCL) and 'tcp' exists
4664 * then we must fail the operation.
4666 if (tcp
&& rename_exclusive
) {
4672 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
4675 if ((fcp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
4678 hfs_unlockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
);
4681 if (hfs_generate_document_id(hfsmp
, &newid
) == 0) {
4682 hfs_lock(fcp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4683 ((struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
4684 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4685 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4686 FSE_ARG_INO
, (ino64_t
)0, // src inode #
4687 FSE_ARG_INO
, (ino64_t
)fcp
->c_fileid
, // dst inode #
4688 FSE_ARG_INT32
, newid
,
4692 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rename...
4696 // check if we're going to need to fix tcp as well. if we aren't, go back relock
4697 // everything. otherwise continue on and fix up tcp as well before relocking.
4699 if (tcp
== NULL
|| !(tcp
->c_bsdflags
& UF_TRACKED
) || ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
!= 0) {
4705 // same thing for tcp if it's set
4707 if (tcp
&& (tcp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
4711 hfs_unlockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
);
4715 if (hfs_generate_document_id(hfsmp
, &newid
) == 0) {
4716 hfs_lock(tcp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4717 ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
4718 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4719 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4720 FSE_ARG_INO
, (ino64_t
)0, // src inode #
4721 FSE_ARG_INO
, (ino64_t
)tcp
->c_fileid
, // dst inode #
4722 FSE_ARG_INT32
, newid
,
4726 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rename...
4729 // go back up and relock everything. next time through the if statement won't be true
4730 // and we'll skip over this block of code.
4737 * Acquire iocounts on the destination's resource fork vnode
4738 * if necessary. If dst/src are files and the dst has a resource
4739 * fork vnode, then we need to try and acquire an iocount on the rsrc vnode.
4740 * If it does not exist, then we don't care and can skip it.
4742 if ((vnode_isreg(fvp
)) || (vnode_islnk(fvp
))) {
4743 if ((tvp
) && (tcp
->c_rsrc_vp
) && (tvp_rsrc
== NULL
)) {
4744 tvp_rsrc
= tcp
->c_rsrc_vp
;
4746 * We can look at the vid here because we're holding the
4747 * cnode lock on the underlying cnode for this rsrc vnode.
4749 tvp_rsrc_vid
= vnode_vid (tvp_rsrc
);
4751 /* Unlock everything to acquire iocount on this rsrc vnode */
4752 if (took_trunc_lock
) {
4753 hfs_unlock_truncate (VTOC(tvp
), HFS_LOCK_DEFAULT
);
4754 took_trunc_lock
= 0;
4756 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4758 if (vnode_getwithvid (tvp_rsrc
, tvp_rsrc_vid
)) {
4759 /* iocount acquisition failed. Reset fields and start over.. */
4769 /* Ensure we didn't race src or dst parent directories with rmdir. */
4770 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4775 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4781 /* Check for a race against unlink. The hfs_valid_cnode checks validate
4782 * the parent/child relationship with fdcp and tdcp, as well as the
4783 * component name of the target cnodes.
4785 if ((fcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, fdvp
, fcnp
, fcp
->c_fileid
, NULL
, &error
)) {
4790 if (tcp
&& ((tcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, tdvp
, tcnp
, tcp
->c_fileid
, NULL
, &error
))) {
4792 // hmm, the destination vnode isn't valid any more.
4793 // in this case we can just drop him and pretend he
4794 // never existed in the first place.
4796 if (took_trunc_lock
) {
4797 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4798 took_trunc_lock
= 0;
4802 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4807 // retry the locking with tvp null'ed out
4811 fdcp
->c_flag
|= C_DIR_MODIFICATION
;
4813 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
4817 * Disallow renaming of a directory hard link if the source and
4818 * destination parent directories are different, or a directory whose
4819 * descendant is a directory hard link and the one of the ancestors
4820 * of the destination directory is a directory hard link.
4822 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
4823 if (fcp
->c_flag
& C_HARDLINK
) {
4827 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
4828 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4829 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
4831 hfs_systemfile_unlock(hfsmp
, lockflags
);
4834 hfs_systemfile_unlock(hfsmp
, lockflags
);
4839 * The following edge case is caught here:
4840 * (to cannot be a descendent of from)
4853 if (tdcp
->c_parentcnid
== fcp
->c_fileid
) {
4859 * The following two edge cases are caught here:
4860 * (note tvp is not empty)
4873 if (tvp
&& vnode_isdir(tvp
) && (tcp
->c_entries
!= 0) && fvp
!= tvp
) {
4879 * The following edge case is caught here:
4880 * (the from child and parent are the same)
4893 * Make sure "from" vnode and its parent are changeable.
4895 if ((fcp
->c_bsdflags
& (IMMUTABLE
| APPEND
)) || (fdcp
->c_bsdflags
& APPEND
)) {
4901 * If the destination parent directory is "sticky", then the
4902 * user must own the parent directory, or the destination of
4903 * the rename, otherwise the destination may not be changed
4904 * (except by root). This implements append-only directories.
4906 * Note that checks for immutable and write access are done
4907 * by the call to hfs_removefile.
4909 if (tvp
&& (tdcp
->c_mode
& S_ISTXT
) &&
4910 (suser(vfs_context_ucred(ap
->a_context
), NULL
)) &&
4911 (kauth_cred_getuid(vfs_context_ucred(ap
->a_context
)) != tdcp
->c_uid
) &&
4912 (hfs_owner_rights(hfsmp
, tcp
->c_uid
, vfs_context_ucred(ap
->a_context
), p
, false)) ) {
4917 /* Don't allow modification of the journal or journal_info_block */
4918 if (hfs_is_journal_file(hfsmp
, fcp
) ||
4919 (tcp
&& hfs_is_journal_file(hfsmp
, tcp
))) {
4926 (void)hfs_getinoquota(tcp
);
4928 /* Preflighting done, take fvp out of the name space. */
4931 #if CONFIG_SECLUDED_RENAME
4933 * Check for "secure" rename that imposes additional restrictions on the
4934 * source vnode. We wait until here to check in order to prevent a race
4935 * with other threads that manage to look up fvp, but their open or link
4936 * is blocked by our locks. At this point, with fvp out of the name cache,
4937 * and holding the lock on fdvp, no other thread can find fvp.
4939 * TODO: Do we need to limit these checks to regular files only?
4941 if (fcnp
->cn_flags
& CN_SECLUDE_RENAME
) {
4942 if (vnode_isdir(fvp
)) {
4948 * Neither fork of source may be open or memory mapped.
4949 * We also don't want it in use by any other system call.
4950 * The file must not have hard links.
4952 * We can't simply use vnode_isinuse() because that does not
4953 * count opens with O_EVTONLY. We don't want a malicious
4954 * process using O_EVTONLY to subvert a secluded rename.
4956 if (fcp
->c_linkcount
!= 1) {
4961 if (fcp
->c_rsrc_vp
&& (vnode_usecount(fcp
->c_rsrc_vp
) > 0 ||
4962 vnode_iocount(fcp
->c_rsrc_vp
) > 0)) {
4963 /* Resource fork is in use (including O_EVTONLY) */
4967 if (fcp
->c_vp
&& (vnode_usecount(fcp
->c_vp
) > (fcp
->c_rsrc_vp
? 1 : 0) ||
4968 vnode_iocount(fcp
->c_vp
) > 1)) {
4970 * Data fork is in use, including O_EVTONLY, but not
4971 * including a reference from the resource fork.
4979 bzero(&from_desc
, sizeof(from_desc
));
4980 from_desc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
4981 from_desc
.cd_namelen
= fcnp
->cn_namelen
;
4982 from_desc
.cd_parentcnid
= fdcp
->c_fileid
;
4983 from_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4984 from_desc
.cd_cnid
= fcp
->c_cnid
;
4986 bzero(&to_desc
, sizeof(to_desc
));
4987 to_desc
.cd_nameptr
= (const u_int8_t
*)tcnp
->cn_nameptr
;
4988 to_desc
.cd_namelen
= tcnp
->cn_namelen
;
4989 to_desc
.cd_parentcnid
= tdcp
->c_fileid
;
4990 to_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4991 to_desc
.cd_cnid
= fcp
->c_cnid
;
4993 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4998 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
4999 * inside a journal transaction and without holding a cnode lock.
5000 * As setting of this bit depends on being in journal transaction for
5001 * concurrency, check this bit again after we start journal transaction for rename
5002 * to ensure that this directory does not have any descendant that
5003 * is a directory hard link.
5005 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
5006 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
5007 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5008 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
5010 hfs_systemfile_unlock(hfsmp
, lockflags
);
5013 hfs_systemfile_unlock(hfsmp
, lockflags
);
5017 // if it's a hardlink then re-lookup the name so
5018 // that we get the correct cnid in from_desc (see
5019 // the comment in hfs_removefile for more details)
5021 if (fcp
->c_flag
& C_HARDLINK
) {
5022 struct cat_desc tmpdesc
;
5025 tmpdesc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
5026 tmpdesc
.cd_namelen
= fcnp
->cn_namelen
;
5027 tmpdesc
.cd_parentcnid
= fdcp
->c_fileid
;
5028 tmpdesc
.cd_hint
= fdcp
->c_childhint
;
5029 tmpdesc
.cd_flags
= fcp
->c_desc
.cd_flags
& CD_ISDIR
;
5030 tmpdesc
.cd_encoding
= 0;
5032 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5034 if (cat_lookup(hfsmp
, &tmpdesc
, 0, 0, NULL
, NULL
, NULL
, &real_cnid
) != 0) {
5035 hfs_systemfile_unlock(hfsmp
, lockflags
);
5039 // use the real cnid instead of whatever happened to be there
5040 from_desc
.cd_cnid
= real_cnid
;
5041 hfs_systemfile_unlock(hfsmp
, lockflags
);
5045 * Reserve some space in the Catalog file.
5047 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
+ CAT_DELETE
, &cookie
, p
))) {
5053 * If the destination exists then it may need to be removed.
5055 * Due to HFS's locking system, we should always move the
5056 * existing 'tvp' element to the hidden directory in hfs_vnop_rename.
5057 * Because the VNOP_LOOKUP call enters and exits the filesystem independently
5058 * of the actual vnop that it was trying to do (stat, link, readlink),
5059 * we must release the cnode lock of that element during the interim to
5060 * do MAC checking, vnode authorization, and other calls. In that time,
5061 * the item can be deleted (or renamed over). However, only in the rename
5062 * case is it inappropriate to return ENOENT from any of those calls. Either
5063 * the call should return information about the old element (stale), or get
5064 * information about the newer element that we are about to write in its place.
5066 * HFS lookup has been modified to detect a rename and re-drive its
5067 * lookup internally. For other calls that have already succeeded in
5068 * their lookup call and are waiting to acquire the cnode lock in order
5069 * to proceed, that cnode lock will not fail due to the cnode being marked
5070 * C_NOEXISTS, because it won't have been marked as such. It will only
5071 * have C_DELETED. Thus, they will simply act on the stale open-unlinked
5072 * element. All future callers will get the new element.
5074 * To implement this behavior, we pass the "only_unlink" argument to
5075 * hfs_removefile and hfs_removedir. This will result in the vnode acting
5076 * as though it is open-unlinked. Additionally, when we are done moving the
5077 * element to the hidden directory, we vnode_recycle the target so that it is
5078 * reclaimed as soon as possible. Reclaim and inactive are both
5079 * capable of clearing out unused blocks for an open-unlinked file or dir.
5083 // if the destination has a document id, we need to preserve it
5086 uint32_t document_id
;
5087 struct FndrExtendedDirInfo
*ffip
= (struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16);
5088 struct FndrExtendedDirInfo
*tfip
= (struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16);
5090 if (ffip
->document_id
&& tfip
->document_id
) {
5091 // both documents are tracked. only save a tombstone from tcp and do nothing else.
5092 doc_tombstone_save(tdvp
, tvp
, tcnp
, hfs_get_document_id(tcp
),
5095 struct doc_tombstone
*ut
;
5096 ut
= doc_tombstone_get();
5098 document_id
= tfip
->document_id
;
5099 tfip
->document_id
= 0;
5101 if (document_id
!= 0) {
5102 // clear UF_TRACKED as well since tcp is now no longer tracked
5103 tcp
->c_bsdflags
&= ~UF_TRACKED
;
5104 (void) cat_update(hfsmp
, &tcp
->c_desc
, &tcp
->c_attr
, NULL
, NULL
);
5107 if (ffip
->document_id
== 0 && document_id
!= 0) {
5108 // printf("RENAME: preserving doc-id %d onto %s (from ino %d, to ino %d)\n", document_id, tcp->c_desc.cd_nameptr, tcp->c_desc.cd_cnid, fcp->c_desc.cd_cnid);
5109 fcp
->c_bsdflags
|= UF_TRACKED
;
5110 ffip
->document_id
= document_id
;
5112 (void) cat_update(hfsmp
, &fcp
->c_desc
, &fcp
->c_attr
, NULL
, NULL
);
5113 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
5114 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
5115 FSE_ARG_INO
, (ino64_t
)tcp
->c_fileid
, // src inode #
5116 FSE_ARG_INO
, (ino64_t
)fcp
->c_fileid
, // dst inode #
5117 FSE_ARG_INT32
, (uint32_t)ffip
->document_id
,
5120 else if ((fcp
->c_bsdflags
& UF_TRACKED
) && doc_tombstone_should_save(ut
, fvp
, fcnp
)) {
5122 if (ut
->t_lastop_document_id
) {
5123 doc_tombstone_clear(ut
, NULL
);
5125 doc_tombstone_save(fdvp
, fvp
, fcnp
,
5126 hfs_get_document_id(fcp
), fcp
->c_fileid
);
5128 //printf("RENAME: (dest-exists): saving tombstone doc-id %lld @ %s (ino %d)\n",
5129 // ut->t_lastop_document_id, ut->t_lastop_filename, fcp->c_desc.cd_cnid);
5135 * When fvp matches tvp they could be case variants
5136 * or matching hard links.
5139 if (!(fcp
->c_flag
& C_HARDLINK
)) {
5141 * If they're not hardlinks, then fvp == tvp must mean we
5142 * are using case-insensitive HFS because case-sensitive would
5143 * not use the same vnode for both. In this case we just update
5144 * the catalog for: a -> A
5146 goto skip_rm
; /* simple case variant */
5149 /* For all cases below, we must be using hardlinks */
5150 else if ((fdvp
!= tdvp
) ||
5151 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)) {
5153 * If the parent directories are not the same, AND the two items
5154 * are hardlinks, posix says to do nothing:
5155 * dir1/fred <-> dir2/bob and the op was mv dir1/fred -> dir2/bob
5156 * We just return 0 in this case.
5158 * If case sensitivity is on, and we are using hardlinks
5159 * then renaming is supposed to do nothing.
5160 * dir1/fred <-> dir2/FRED, and op == mv dir1/fred -> dir2/FRED
5162 goto out
; /* matching hardlinks, nothing to do */
5164 } else if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
5165 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
5167 * If we get here, then the following must be true:
5168 * a) We are running case-insensitive HFS+.
5169 * b) Both paths 'fvp' and 'tvp' are in the same parent directory.
5170 * c) the two names are case-variants of each other.
5172 * In this case, we are really only dealing with a single catalog record
5173 * whose name is being updated.
5175 * op is dir1/fred -> dir1/FRED
5177 * We need to special case the name matching, because if
5178 * dir1/fred <-> dir1/bob were the two links, and the
5179 * op was dir1/fred -> dir1/bob
5180 * That would fail/do nothing.
5182 goto skip_rm
; /* case-variant hardlink in the same dir */
5184 goto out
; /* matching hardlink, nothing to do */
5189 if (vnode_isdir(tvp
)) {
5191 * hfs_removedir will eventually call hfs_removefile on the directory
5192 * we're working on, because only hfs_removefile does the renaming of the
5193 * item to the hidden directory. The directory will stay around in the
5194 * hidden directory with C_DELETED until it gets an inactive or a reclaim.
5195 * That way, we can destroy all of the EAs as needed and allow new ones to be
5198 error
= hfs_removedir(tdvp
, tvp
, tcnp
, HFSRM_SKIP_RESERVE
, 1);
5201 error
= hfs_removefile(tdvp
, tvp
, tcnp
, 0, HFSRM_SKIP_RESERVE
, 0, NULL
, 1);
5204 * If the destination file had a resource fork vnode, then we need to get rid of
5205 * its blocks when there are no more references to it. Because the call to
5206 * hfs_removefile above always open-unlinks things, we need to force an inactive/reclaim
5207 * on the resource fork vnode, in order to prevent block leaks. Otherwise,
5208 * the resource fork vnode could prevent the data fork vnode from going out of scope
5209 * because it holds a v_parent reference on it. So we mark it for termination
5210 * with a call to vnode_recycle. hfs_vnop_reclaim has been modified so that it
5211 * can clean up the blocks of open-unlinked files and resource forks.
5213 * We can safely call vnode_recycle on the resource fork because we took an iocount
5214 * reference on it at the beginning of the function.
5217 if ((error
== 0) && (tcp
->c_flag
& C_DELETED
) && (tvp_rsrc
)) {
5218 vnode_recycle(tvp_rsrc
);
5228 /* Mark 'tcp' as being deleted due to a rename */
5229 tcp
->c_flag
|= C_RENAMED
;
5232 * Aggressively mark tvp/tcp for termination to ensure that we recover all blocks
5233 * as quickly as possible.
5237 struct doc_tombstone
*ut
;
5238 ut
= doc_tombstone_get();
5241 // There is nothing at the destination. If the file being renamed is
5242 // tracked, save a "tombstone" of the document_id. If the file is
5243 // not a tracked file, then see if it needs to inherit a tombstone.
5245 // NOTE: we do not save a tombstone if the file being renamed begins
5246 // with "atmp" which is done to work-around AutoCad's bizarre
5247 // 5-step un-safe save behavior
5249 if (fcp
->c_bsdflags
& UF_TRACKED
) {
5250 if (doc_tombstone_should_save(ut
, fvp
, fcnp
)) {
5251 doc_tombstone_save(fdvp
, fvp
, fcnp
, hfs_get_document_id(fcp
),
5254 //printf("RENAME: (no dest): saving tombstone doc-id %lld @ %s (ino %d)\n",
5255 // ut->t_lastop_document_id, ut->t_lastop_filename, fcp->c_desc.cd_cnid);
5257 // intentionally do nothing
5259 } else if ( ut
->t_lastop_document_id
!= 0
5260 && tdvp
== ut
->t_lastop_parent
5261 && vnode_vid(tdvp
) == ut
->t_lastop_parent_vid
5262 && strcmp((char *)ut
->t_lastop_filename
, (char *)tcnp
->cn_nameptr
) == 0) {
5264 //printf("RENAME: %s (ino %d) inheriting doc-id %lld\n", tcnp->cn_nameptr, fcp->c_desc.cd_cnid, ut->t_lastop_document_id);
5265 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16);
5266 fcp
->c_bsdflags
|= UF_TRACKED
;
5267 fip
->document_id
= ut
->t_lastop_document_id
;
5268 cat_update(hfsmp
, &fcp
->c_desc
, &fcp
->c_attr
, NULL
, NULL
);
5270 doc_tombstone_clear(ut
, &old_doc_vp
);
5271 } else if (ut
->t_lastop_document_id
&& doc_tombstone_should_save(ut
, fvp
, fcnp
) && doc_tombstone_should_save(ut
, tvp
, tcnp
)) {
5272 // no match, clear the tombstone
5273 //printf("RENAME: clearing the tombstone %lld @ %s\n", ut->t_lastop_document_id, ut->t_lastop_filename);
5274 doc_tombstone_clear(ut
, NULL
);
5280 * All done with tvp and fvp.
5282 * We also jump to this point if there was no destination observed during lookup and namei.
5283 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
5284 * competing thread from racing us and creating a file or dir at the destination of this rename
5285 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
5286 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
5287 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
5288 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
5289 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
5290 * will be swallowed and it will restart the operation.
5293 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
5294 error
= cat_rename(hfsmp
, &from_desc
, &tdcp
->c_desc
, &to_desc
, &out_desc
);
5295 hfs_systemfile_unlock(hfsmp
, lockflags
);
5298 if (error
== EEXIST
) {
5304 /* Invalidate negative cache entries in the destination directory */
5305 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
5306 cache_purge_negatives(tdvp
);
5307 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
5310 /* Update cnode's catalog descriptor */
5311 replace_desc(fcp
, &out_desc
);
5312 fcp
->c_parentcnid
= tdcp
->c_fileid
;
5316 * Now indicate this cnode needs to have date-added written to the
5317 * finderinfo, but only if moving to a different directory, or if
5318 * it doesn't already have it.
5320 if (fdvp
!= tdvp
|| !ISSET(fcp
->c_attr
.ca_recflags
, kHFSHasDateAddedMask
))
5321 fcp
->c_flag
|= C_NEEDS_DATEADDED
;
5323 (void) hfs_update (fvp
, 0);
5325 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_RMDIR
: VOL_RMFILE
,
5326 (fdcp
->c_cnid
== kHFSRootFolderID
));
5327 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_MKDIR
: VOL_MKFILE
,
5328 (tdcp
->c_cnid
== kHFSRootFolderID
));
5330 /* Update both parent directories. */
5332 if (vnode_isdir(fvp
)) {
5333 /* If the source directory has directory hard link
5334 * descendants, set the kHFSHasChildLinkBit in the
5335 * destination parent hierarchy
5337 if ((fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) &&
5338 !(tdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
)) {
5340 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
5342 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
5344 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp
->c_cnid
);
5348 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
5349 DEC_FOLDERCOUNT(hfsmp
, fdcp
->c_attr
);
5352 tdcp
->c_dirchangecnt
++;
5353 tdcp
->c_flag
|= C_MODIFIED
;
5354 hfs_incr_gencount(tdcp
);
5356 if (fdcp
->c_entries
> 0)
5358 fdcp
->c_dirchangecnt
++;
5359 fdcp
->c_flag
|= C_MODIFIED
;
5360 fdcp
->c_touch_chgtime
= TRUE
;
5361 fdcp
->c_touch_modtime
= TRUE
;
5363 if (ISSET(fcp
->c_flag
, C_HARDLINK
)) {
5364 hfs_relorigin(fcp
, fdcp
->c_fileid
);
5365 if (fdcp
->c_fileid
!= fdcp
->c_cnid
)
5366 hfs_relorigin(fcp
, fdcp
->c_cnid
);
5369 (void) hfs_update(fdvp
, 0);
5371 hfs_incr_gencount(fdcp
);
5373 tdcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
5374 tdcp
->c_touch_chgtime
= TRUE
;
5375 tdcp
->c_touch_modtime
= TRUE
;
5377 (void) hfs_update(tdvp
, 0);
5379 /* Update the vnode's name now that the rename has completed. */
5380 vnode_update_identity(fvp
, tdvp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
,
5381 tcnp
->cn_hash
, (VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
));
5384 * At this point, we may have a resource fork vnode attached to the
5385 * 'from' vnode. If it exists, we will want to update its name, because
5386 * it contains the old name + _PATH_RSRCFORKSPEC. ("/..namedfork/rsrc").
5388 * Note that the only thing we need to update here is the name attached to
5389 * the vnode, since a resource fork vnode does not have a separate resource
5390 * cnode -- it's still 'fcp'.
5392 if (fcp
->c_rsrc_vp
) {
5393 char* rsrc_path
= NULL
;
5396 /* Create a new temporary buffer that's going to hold the new name */
5397 rsrc_path
= hfs_malloc(MAXPATHLEN
);
5398 len
= snprintf (rsrc_path
, MAXPATHLEN
, "%s%s", tcnp
->cn_nameptr
, _PATH_RSRCFORKSPEC
);
5399 len
= MIN(len
, MAXPATHLEN
);
5402 * vnode_update_identity will do the following for us:
5403 * 1) release reference on the existing rsrc vnode's name.
5404 * 2) copy/insert new name into the name cache
5405 * 3) attach the new name to the resource vnode
5406 * 4) update the vnode's vid
5408 vnode_update_identity (fcp
->c_rsrc_vp
, fvp
, rsrc_path
, len
, 0, (VNODE_UPDATE_NAME
| VNODE_UPDATE_CACHE
));
5410 /* Free the memory associated with the resource fork's name */
5411 hfs_free(rsrc_path
, MAXPATHLEN
);
5415 cat_postflight(hfsmp
, &cookie
, p
);
5418 hfs_end_transaction(hfsmp
);
5421 fdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
5422 wakeup((caddr_t
)&fdcp
->c_flag
);
5424 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
5425 wakeup((caddr_t
)&tdcp
->c_flag
);
5428 const ino64_t file_id
= fcp
->c_fileid
;
5430 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
5432 if (took_trunc_lock
) {
5433 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
5436 /* Now vnode_put the resource forks vnodes if necessary */
5438 vnode_put(tvp_rsrc
);
5442 /* After tvp is removed the only acceptable error is EIO */
5443 if (error
&& tvp_deleted
)
5446 /* If we want to reintroduce notifications for renames, this is the
5450 cnode_t
*ocp
= VTOC(old_doc_vp
);
5451 hfs_lock_always(ocp
, HFS_EXCLUSIVE_LOCK
);
5452 struct FndrExtendedFileInfo
*ofip
= (struct FndrExtendedFileInfo
*)((char *)&ocp
->c_attr
.ca_finderinfo
+ 16);
5454 const uint32_t doc_id
= ofip
->document_id
;
5455 const ino64_t old_file_id
= ocp
->c_fileid
;
5457 // printf("clearing doc-id from ino %d\n", ocp->c_desc.cd_cnid);
5458 ofip
->document_id
= 0;
5459 ocp
->c_bsdflags
&= ~UF_TRACKED
;
5460 ocp
->c_flag
|= C_MODIFIED
;
5463 vnode_put(old_doc_vp
);
5465 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
5466 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
5467 FSE_ARG_INO
, old_file_id
, // src inode #
5468 FSE_ARG_INO
, file_id
, // dst inode #
5469 FSE_ARG_INT32
, doc_id
,
5481 hfs_vnop_mkdir(struct vnop_mkdir_args
*ap
)
5483 /***** HACK ALERT ********/
5484 ap
->a_cnp
->cn_flags
|= MAKEENTRY
;
5485 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
5490 * Create a symbolic link.
5493 hfs_vnop_symlink(struct vnop_symlink_args
*ap
)
5495 struct vnode
**vpp
= ap
->a_vpp
;
5496 struct vnode
*dvp
= ap
->a_dvp
;
5497 struct vnode
*vp
= NULL
;
5498 struct cnode
*cp
= NULL
;
5499 struct hfsmount
*hfsmp
;
5500 struct filefork
*fp
;
5501 struct buf
*bp
= NULL
;
5507 /* HFS standard disks don't support symbolic links */
5508 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
)
5511 /* Check for empty target name */
5512 if (ap
->a_target
[0] == 0)
5515 hfsmp
= VTOHFS(dvp
);
5517 len
= strlen(ap
->a_target
);
5518 if (len
> MAXPATHLEN
)
5519 return (ENAMETOOLONG
);
5521 /* Check for free space */
5522 if (((u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
) < len
) {
5526 /* Create the vnode */
5527 ap
->a_vap
->va_mode
|= S_IFLNK
;
5528 if ((error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
))) {
5532 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
5538 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
5543 (void)hfs_getinoquota(cp
);
5546 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5552 * Allocate space for the link.
5554 * Since we're already inside a transaction,
5556 * Don't need truncate lock since a symlink is treated as a system file.
5558 error
= hfs_truncate(vp
, len
, IO_NOZEROFILL
, 0, ap
->a_context
);
5560 /* On errors, remove the symlink file */
5563 * End the transaction so we don't re-take the cnode lock
5564 * below while inside a transaction (lock order violation).
5566 hfs_end_transaction(hfsmp
);
5568 /* hfs_removefile() requires holding the truncate lock */
5570 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
5571 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
5573 if (hfs_start_transaction(hfsmp
) != 0) {
5575 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
5579 (void) hfs_removefile(dvp
, vp
, ap
->a_cnp
, 0, 0, 0, NULL
, 0);
5580 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
5584 /* Write the link to disk */
5585 bp
= buf_getblk(vp
, (daddr64_t
)0, roundup((int)fp
->ff_size
, hfsmp
->hfs_physical_block_size
),
5588 journal_modify_block_start(hfsmp
->jnl
, bp
);
5590 datap
= (char *)buf_dataptr(bp
);
5591 bzero(datap
, buf_size(bp
));
5592 bcopy(ap
->a_target
, datap
, len
);
5595 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
5601 hfs_end_transaction(hfsmp
);
5602 if ((cp
!= NULL
) && (vp
!= NULL
)) {
5615 /* structures to hold a "." or ".." directory entry */
5616 struct hfs_stddotentry
{
5617 u_int32_t d_fileno
; /* unique file number */
5618 u_int16_t d_reclen
; /* length of this structure */
5619 u_int8_t d_type
; /* dirent file type */
5620 u_int8_t d_namlen
; /* len of filename */
5621 char d_name
[4]; /* "." or ".." */
5624 struct hfs_extdotentry
{
5625 u_int64_t d_fileno
; /* unique file number */
5626 u_int64_t d_seekoff
; /* seek offset (optional, used by servers) */
5627 u_int16_t d_reclen
; /* length of this structure */
5628 u_int16_t d_namlen
; /* len of filename */
5629 u_int8_t d_type
; /* dirent file type */
5630 u_char d_name
[3]; /* "." or ".." */
5634 struct hfs_stddotentry std
;
5635 struct hfs_extdotentry ext
;
5639 * hfs_vnop_readdir reads directory entries into the buffer pointed
5640 * to by uio, in a filesystem independent format. Up to uio_resid
5641 * bytes of data can be transferred. The data in the buffer is a
5642 * series of packed dirent structures where each one contains the
5643 * following entries:
5645 * u_int32_t d_fileno; // file number of entry
5646 * u_int16_t d_reclen; // length of this record
5647 * u_int8_t d_type; // file type
5648 * u_int8_t d_namlen; // length of string in d_name
5649 * char d_name[MAXNAMELEN+1]; // null terminated file name
5651 * The current position (uio_offset) refers to the next block of
5652 * entries. The offset can only be set to a value previously
5653 * returned by hfs_vnop_readdir or zero. This offset does not have
5654 * to match the number of bytes returned (in uio_resid).
5656 * In fact, the offset used by HFS is essentially an index (26 bits)
5657 * with a tag (6 bits). The tag is for associating the next request
5658 * with the current request. This enables us to have multiple threads
5659 * reading the directory while the directory is also being modified.
5661 * Each tag/index pair is tied to a unique directory hint. The hint
5662 * contains information (filename) needed to build the catalog b-tree
5663 * key for finding the next set of entries.
5665 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
5666 * do NOT synthesize entries for "." and "..".
5669 hfs_vnop_readdir(struct vnop_readdir_args
*ap
)
5671 struct vnode
*vp
= ap
->a_vp
;
5672 uio_t uio
= ap
->a_uio
;
5673 struct cnode
*cp
= VTOC(vp
);
5674 struct hfsmount
*hfsmp
= VTOHFS(vp
);
5675 directoryhint_t
*dirhint
= NULL
;
5676 directoryhint_t localhint
;
5681 user_addr_t user_start
= 0;
5682 user_size_t user_len
= 0;
5683 user_size_t user_original_resid
= 0;
5690 cnid_t cnid_hint
= 0;
5691 int bump_valence
= 0;
5694 startoffset
= offset
= uio_offset(uio
);
5695 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5696 nfs_cookies
= extended
&& (ap
->a_flags
& VNODE_READDIR_REQSEEKOFF
);
5698 /* Sanity check the uio data. */
5699 if (uio_iovcnt(uio
) > 1)
5702 if (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
) {
5703 int compressed
= hfs_file_is_compressed(VTOC(vp
), 0); /* 0 == take the cnode lock */
5704 if (VTOCMP(vp
) != NULL
&& !compressed
) {
5705 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
5713 // We have to lock the user's buffer here so that we won't
5714 // fault on it after we've acquired a shared lock on the
5715 // catalog file. The issue is that you can get a 3-way
5716 // deadlock if someone else starts a transaction and then
5717 // tries to lock the catalog file but can't because we're
5718 // here and we can't service our page fault because VM is
5719 // blocked trying to start a transaction as a result of
5720 // trying to free up pages for our page fault. It's messy
5721 // but it does happen on dual-processors that are paging
5722 // heavily (see radar 3082639 for more info). By locking
5723 // the buffer up-front we prevent ourselves from faulting
5724 // while holding the shared catalog file lock.
5726 // Fortunately this and hfs_search() are the only two places
5727 // currently (10/30/02) that can fault on user data with a
5728 // shared lock on the catalog file.
5730 if (hfsmp
->jnl
&& uio_isuserspace(uio
)) {
5731 user_start
= uio_curriovbase(uio
);
5732 user_len
= uio_curriovlen(uio
);
5734 /* Bounds check the user buffer */
5735 if (user_len
> (256 * 1024)) {
5736 /* only allow the user to wire down at most 256k */
5737 user_len
= (256 * 1024);
5738 user_original_resid
= uio_resid(uio
);
5739 uio_setresid (uio
, (user_ssize_t
)(256 * 1024));
5742 if ((error
= vslock(user_start
, user_len
)) != 0) {
5743 if (user_original_resid
> 0) {
5744 uio_setresid(uio
, user_original_resid
);
5745 user_original_resid
= 0;
5751 /* Note that the dirhint calls require an exclusive lock. */
5752 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
5754 if (user_original_resid
> 0) {
5755 uio_setresid(uio
, user_original_resid
);
5756 user_original_resid
= 0;
5758 vsunlock(user_start
, user_len
, TRUE
);
5763 /* Pick up cnid hint (if any). */
5765 cnid_hint
= (cnid_t
)(uio_offset(uio
) >> 32);
5766 uio_setoffset(uio
, uio_offset(uio
) & 0x00000000ffffffffLL
);
5767 if (cnid_hint
== INT_MAX
) { /* searching pass the last item */
5773 * Synthesize entries for "." and "..", unless the directory has
5774 * been deleted, but not closed yet (lazy delete in progress).
5776 if (offset
== 0 && !(cp
->c_flag
& C_DELETED
)) {
5781 * We could use a union of the two types of dot entries (HFS / HFS+)
5782 * but it makes static analysis of this code difficult. The problem is that
5783 * the HFS standard dot entry is smaller than the HFS+ one, and we also ideally
5784 * want the uiomove to operate on a two-element adjacent array. If we used the
5785 * array of unions, we would have to do two separate uiomoves because the memory
5786 * for the hfs standard dot entries would not be adjacent to one another.
5787 * So just allocate the entries on the stack in separate cases.
5791 hfs_dotentry_t dotentry
[2];
5794 struct hfs_extdotentry
*entry
= &dotentry
[0].ext
;
5796 entry
->d_fileno
= cp
->c_cnid
;
5797 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
5798 entry
->d_type
= DT_DIR
;
5799 entry
->d_namlen
= 1;
5800 entry
->d_name
[0] = '.';
5801 entry
->d_name
[1] = '\0';
5802 entry
->d_name
[2] = '\0';
5803 entry
->d_seekoff
= 1;
5806 entry
->d_fileno
= cp
->c_parentcnid
;
5807 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
5808 entry
->d_type
= DT_DIR
;
5809 entry
->d_namlen
= 2;
5810 entry
->d_name
[0] = '.';
5811 entry
->d_name
[1] = '.';
5812 entry
->d_name
[2] = '\0';
5813 entry
->d_seekoff
= 2;
5814 uiosize
= 2 * sizeof(struct hfs_extdotentry
);
5816 if ((error
= uiomove((caddr_t
)dotentry
, uiosize
, uio
))) {
5821 struct hfs_stddotentry hfs_std_dotentries
[2];
5824 struct hfs_stddotentry
*entry
= &hfs_std_dotentries
[0];
5826 entry
->d_fileno
= cp
->c_cnid
;
5827 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
5828 entry
->d_type
= DT_DIR
;
5829 entry
->d_namlen
= 1;
5830 *(int *)&entry
->d_name
[0] = 0;
5831 entry
->d_name
[0] = '.';
5834 entry
->d_fileno
= cp
->c_parentcnid
;
5835 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
5836 entry
->d_type
= DT_DIR
;
5837 entry
->d_namlen
= 2;
5838 *(int *)&entry
->d_name
[0] = 0;
5839 entry
->d_name
[0] = '.';
5840 entry
->d_name
[1] = '.';
5841 uiosize
= 2 * sizeof(struct hfs_stddotentry
);
5843 if ((error
= uiomove((caddr_t
)hfs_std_dotentries
, uiosize
, uio
))) {
5852 * Intentionally avoid checking the valence here. If we
5853 * have FS corruption that reports the valence is 0, even though it
5854 * has contents, we might artificially skip over iterating
5858 /* Convert offset into a catalog directory index. */
5859 index
= (offset
& HFS_INDEX_MASK
) - 2;
5860 tag
= offset
& ~HFS_INDEX_MASK
;
5862 /* Lock catalog during cat_findname and cat_getdirentries. */
5863 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5865 /* When called from NFS, try and resolve a cnid hint. */
5866 if (nfs_cookies
&& cnid_hint
!= 0) {
5867 if (cat_findname(hfsmp
, cnid_hint
, &localhint
.dh_desc
) == 0) {
5868 if ( localhint
.dh_desc
.cd_parentcnid
== cp
->c_fileid
) {
5869 localhint
.dh_index
= index
- 1;
5870 localhint
.dh_time
= 0;
5871 bzero(&localhint
.dh_link
, sizeof(localhint
.dh_link
));
5872 dirhint
= &localhint
; /* don't forget to release the descriptor */
5874 cat_releasedesc(&localhint
.dh_desc
);
5879 /* Get a directory hint (cnode must be locked exclusive) */
5880 if (dirhint
== NULL
) {
5881 dirhint
= hfs_getdirhint(cp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, 0);
5883 /* Hide tag from catalog layer. */
5884 dirhint
->dh_index
&= HFS_INDEX_MASK
;
5885 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
5886 dirhint
->dh_index
= -1;
5891 dirhint
->dh_threadhint
= cp
->c_dirthreadhint
;
5895 * If we have a non-zero index, there is a possibility that during the last
5896 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
5897 * then we don't want to return any new entries for the caller. Just return 0
5898 * items, mark the eofflag, and bail out. Because we won't have done any work, the
5899 * code at the end of the function will release the dirhint for us.
5901 * Don't forget to unlock the catalog lock on the way out, too.
5903 if (dirhint
->dh_desc
.cd_flags
& CD_EOF
) {
5906 uio_setoffset(uio
, startoffset
);
5907 if (user_original_resid
> 0) {
5908 uio_setresid(uio
, user_original_resid
);
5909 user_original_resid
= 0;
5911 hfs_systemfile_unlock (hfsmp
, lockflags
);
5917 /* Pack the buffer with dirent entries. */
5918 error
= cat_getdirentries(hfsmp
, cp
->c_entries
, dirhint
, uio
, ap
->a_flags
, &items
, &eofflag
);
5920 if (user_original_resid
> 0) {
5921 user_original_resid
= user_original_resid
- ((user_ssize_t
)256*1024 - uio_resid(uio
));
5922 uio_setresid(uio
, user_original_resid
);
5923 user_original_resid
= 0;
5926 if (index
== 0 && error
== 0) {
5927 cp
->c_dirthreadhint
= dirhint
->dh_threadhint
;
5930 hfs_systemfile_unlock(hfsmp
, lockflags
);
5936 /* Get index to the next item */
5939 if (items
>= (int)cp
->c_entries
) {
5944 * Detect valence FS corruption.
5946 * We are holding the cnode lock exclusive, so there should not be
5947 * anybody modifying the valence field of this cnode. If we enter
5948 * this block, that means we observed filesystem corruption, because
5949 * this directory reported a valence of 0, yet we found at least one
5950 * item. In this case, we need to minimally self-heal this
5951 * directory to prevent userland from tripping over a directory
5952 * that appears empty (getattr of valence reports 0), but actually
5955 * We'll force the cnode update at the end of the function after
5956 * completing all of the normal getdirentries steps.
5958 if ((cp
->c_entries
== 0) && (items
> 0)) {
5959 /* disk corruption */
5961 /* Mark the cnode as dirty. */
5962 cp
->c_flag
|= C_MODIFIED
;
5963 printf("hfs_vnop_readdir: repairing valence to non-zero! \n");
5968 /* Convert catalog directory index back into an offset. */
5970 tag
= (++cp
->c_dirhinttag
) << HFS_INDEX_BITS
;
5971 uio_setoffset(uio
, (index
+ 2) | tag
);
5972 dirhint
->dh_index
|= tag
;
5975 cp
->c_touch_acctime
= TRUE
;
5977 if (ap
->a_numdirent
) {
5978 if (startoffset
== 0)
5980 *ap
->a_numdirent
= items
;
5985 if (user_original_resid
> 0) {
5986 uio_setresid(uio
, user_original_resid
);
5987 user_original_resid
= 0;
5989 vsunlock(user_start
, user_len
, TRUE
);
5991 /* If we didn't do anything then go ahead and dump the hint. */
5992 if ((dirhint
!= NULL
) &&
5993 (dirhint
!= &localhint
) &&
5994 (uio_offset(uio
) == startoffset
)) {
5995 hfs_reldirhint(cp
, dirhint
);
5998 if (ap
->a_eofflag
) {
5999 *ap
->a_eofflag
= eofflag
;
6001 if (dirhint
== &localhint
) {
6002 cat_releasedesc(&localhint
.dh_desc
);
6006 /* force the update before dropping the cnode lock*/
6017 * Read contents of a symbolic link.
6020 hfs_vnop_readlink(struct vnop_readlink_args
*ap
)
6022 struct vnode
*vp
= ap
->a_vp
;
6024 struct filefork
*fp
;
6027 if (!vnode_islnk(vp
))
6030 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
6035 /* Zero length sym links are not allowed */
6036 if (fp
->ff_size
== 0 || fp
->ff_size
> MAXPATHLEN
) {
6041 /* Cache the path so we don't waste buffer cache resources */
6042 if (fp
->ff_symlinkptr
== NULL
) {
6043 struct buf
*bp
= NULL
;
6045 fp
->ff_symlinkptr
= hfs_malloc(fp
->ff_size
);
6046 error
= (int)buf_meta_bread(vp
, (daddr64_t
)0,
6047 roundup((int)fp
->ff_size
, VTOHFS(vp
)->hfs_physical_block_size
),
6048 vfs_context_ucred(ap
->a_context
), &bp
);
6052 if (fp
->ff_symlinkptr
) {
6053 hfs_free(fp
->ff_symlinkptr
, fp
->ff_size
);
6054 fp
->ff_symlinkptr
= NULL
;
6058 bcopy((char *)buf_dataptr(bp
), fp
->ff_symlinkptr
, (size_t)fp
->ff_size
);
6060 if (VTOHFS(vp
)->jnl
&& (buf_flags(bp
) & B_LOCKED
) == 0) {
6061 buf_markinvalid(bp
); /* data no longer needed */
6065 error
= uiomove((caddr_t
)fp
->ff_symlinkptr
, (int)fp
->ff_size
, ap
->a_uio
);
6068 * Keep track blocks read
6070 if ((VTOHFS(vp
)->hfc_stage
== HFC_RECORDING
) && (error
== 0)) {
6073 * If this file hasn't been seen since the start of
6074 * the current sampling period then start over.
6076 if (cp
->c_atime
< VTOHFS(vp
)->hfc_timebase
)
6077 VTOF(vp
)->ff_bytesread
= fp
->ff_size
;
6079 VTOF(vp
)->ff_bytesread
+= fp
->ff_size
;
6081 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
6082 // cp->c_touch_acctime = TRUE;
6092 * Get configurable pathname variables.
6095 hfs_vnop_pathconf(struct vnop_pathconf_args
*ap
)
6099 int std_hfs
= (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
);
6102 switch (ap
->a_name
) {
6110 *ap
->a_retval
= HFS_LINK_MAX
;
6116 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
6120 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
6124 *ap
->a_retval
= PATH_MAX
; /* 1024 */
6127 *ap
->a_retval
= PIPE_BUF
;
6129 case _PC_CHOWN_RESTRICTED
:
6130 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
6133 *ap
->a_retval
= 200112; /* _POSIX_NO_TRUNC */
6135 case _PC_NAME_CHARS_MAX
:
6138 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
6142 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
6145 case _PC_CASE_SENSITIVE
:
6146 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_CASE_SENSITIVE
)
6151 case _PC_CASE_PRESERVING
:
6154 case _PC_FILESIZEBITS
:
6155 /* number of bits to store max file size */
6165 case _PC_XATTR_SIZE_BITS
:
6166 /* Number of bits to store maximum extended attribute size */
6167 *ap
->a_retval
= HFS_XATTR_SIZE_BITS
;
6177 * Prepares a fork for cat_update by making sure ff_size and ff_blocks
6178 * are no bigger than the valid data on disk thus reducing the chance
6179 * of exposing uninitialised data in the event of a non clean unmount.
6180 * fork_buf is where to put the temporary copy if required. (It can
6183 const struct cat_fork
*
6184 hfs_prepare_fork_for_update(filefork_t
*ff
,
6185 const struct cat_fork
*cf
,
6186 struct cat_fork
*cf_buf
,
6187 uint32_t block_size
)
6195 cf_buf
= &ff
->ff_data
;
6197 off_t max_size
= ff
->ff_size
;
6199 // Check first invalid range
6200 if (!TAILQ_EMPTY(&ff
->ff_invalidranges
))
6201 max_size
= TAILQ_FIRST(&ff
->ff_invalidranges
)->rl_start
;
6203 if (!ff
->ff_unallocblocks
&& ff
->ff_size
<= max_size
)
6204 return cf
; // Nothing to do
6206 if (ff
->ff_blocks
< ff
->ff_unallocblocks
) {
6207 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
6208 ff
->ff_blocks
, ff
->ff_unallocblocks
);
6211 struct cat_fork
*out
= cf_buf
;
6214 bcopy(cf
, out
, sizeof(*cf
));
6216 // Adjust cf_blocks for cf_vblocks
6217 out
->cf_blocks
-= out
->cf_vblocks
;
6220 * Here we trim the size with the updated cf_blocks. This is
6221 * probably unnecessary now because the invalid ranges should
6222 * catch this (but that wasn't always the case).
6224 off_t alloc_bytes
= hfs_blk_to_bytes(out
->cf_blocks
, block_size
);
6225 if (out
->cf_size
> alloc_bytes
)
6226 out
->cf_size
= alloc_bytes
;
6228 // Trim cf_size to first invalid range
6229 if (out
->cf_size
> max_size
)
6230 out
->cf_size
= max_size
;
6236 * Update a cnode's on-disk metadata.
6238 * The cnode must be locked exclusive. See declaration for possible
6242 hfs_update(struct vnode
*vp
, int options
)
6244 struct cnode
*cp
= VTOC(vp
);
6246 const struct cat_fork
*dataforkp
= NULL
;
6247 const struct cat_fork
*rsrcforkp
= NULL
;
6248 struct cat_fork datafork
;
6249 struct cat_fork rsrcfork
;
6250 struct hfsmount
*hfsmp
;
6253 uint32_t tstate
= 0;
6255 if (ISSET(cp
->c_flag
, C_NOEXISTS
))
6261 if (((vnode_issystem(vp
) && (cp
->c_cnid
< kHFSFirstUserCatalogNodeID
))) ||
6262 hfsmp
->hfs_catalog_vp
== NULL
){
6265 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (cp
->c_mode
== 0)) {
6266 CLR(cp
->c_flag
, C_MODIFIED
| C_MINOR_MOD
| C_NEEDS_DATEADDED
);
6267 cp
->c_touch_acctime
= 0;
6268 cp
->c_touch_chgtime
= 0;
6269 cp
->c_touch_modtime
= 0;
6272 if (kdebug_enable
) {
6273 if (cp
->c_touch_acctime
|| cp
->c_atime
!= cp
->c_attr
.ca_atimeondisk
)
6274 tstate
|= DBG_HFS_UPDATE_ACCTIME
;
6275 if (cp
->c_touch_modtime
)
6276 tstate
|= DBG_HFS_UPDATE_MODTIME
;
6277 if (cp
->c_touch_chgtime
)
6278 tstate
|= DBG_HFS_UPDATE_CHGTIME
;
6280 if (cp
->c_flag
& C_MODIFIED
)
6281 tstate
|= DBG_HFS_UPDATE_MODIFIED
;
6282 if (ISSET(options
, HFS_UPDATE_FORCE
))
6283 tstate
|= DBG_HFS_UPDATE_FORCE
;
6284 if (cp
->c_flag
& C_NEEDS_DATEADDED
)
6285 tstate
|= DBG_HFS_UPDATE_DATEADDED
;
6286 if (cp
->c_flag
& C_MINOR_MOD
)
6287 tstate
|= DBG_HFS_UPDATE_MINOR
;
6289 hfs_touchtimes(hfsmp
, cp
);
6291 if (!ISSET(cp
->c_flag
, C_MODIFIED
| C_MINOR_MOD
)
6292 && !hfs_should_save_atime(cp
)) {
6293 // Nothing to update
6297 KDBG(HFSDBG_UPDATE
| DBG_FUNC_START
, kdebug_vnode(vp
), tstate
);
6299 bool check_txn
= false;
6301 if (!ISSET(options
, HFS_UPDATE_FORCE
) && !ISSET(cp
->c_flag
, C_MODIFIED
)) {
6303 * This must be a minor modification. If the current
6304 * transaction already has an update for this node, then we
6305 * bundle in the modification.
6308 && journal_current_txn(hfsmp
->jnl
) == cp
->c_update_txn
) {
6311 tstate
|= DBG_HFS_UPDATE_SKIPPED
;
6317 if ((error
= hfs_start_transaction(hfsmp
)) != 0)
6321 && journal_current_txn(hfsmp
->jnl
) != cp
->c_update_txn
) {
6322 hfs_end_transaction(hfsmp
);
6323 tstate
|= DBG_HFS_UPDATE_SKIPPED
;
6329 dataforkp
= &cp
->c_datafork
->ff_data
;
6331 rsrcforkp
= &cp
->c_rsrcfork
->ff_data
;
6334 * Modify the values passed to cat_update based on whether or not
6335 * the file has invalid ranges or borrowed blocks.
6337 dataforkp
= hfs_prepare_fork_for_update(cp
->c_datafork
, NULL
, &datafork
, hfsmp
->blockSize
);
6338 rsrcforkp
= hfs_prepare_fork_for_update(cp
->c_rsrcfork
, NULL
, &rsrcfork
, hfsmp
->blockSize
);
6340 if (__builtin_expect(kdebug_enable
& KDEBUG_TRACE
, 0)) {
6341 long dbg_parms
[NUMPARMS
];
6344 dbg_namelen
= NUMPARMS
* sizeof(long);
6345 vn_getpath(vp
, (char *)dbg_parms
, &dbg_namelen
);
6347 if (dbg_namelen
< (int)sizeof(dbg_parms
))
6348 memset((char *)dbg_parms
+ dbg_namelen
, 0, sizeof(dbg_parms
) - dbg_namelen
);
6350 kdebug_lookup_gen_events(dbg_parms
, dbg_namelen
, (void *)vp
, TRUE
);
6354 * Lock the Catalog b-tree file.
6356 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
6358 error
= cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, dataforkp
, rsrcforkp
);
6361 cp
->c_update_txn
= journal_current_txn(hfsmp
->jnl
);
6363 hfs_systemfile_unlock(hfsmp
, lockflags
);
6365 CLR(cp
->c_flag
, C_MODIFIED
| C_MINOR_MOD
);
6367 hfs_end_transaction(hfsmp
);
6371 KDBG(HFSDBG_UPDATE
| DBG_FUNC_END
, kdebug_vnode(vp
), tstate
, error
);
6377 * Allocate a new node
6380 hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
,
6381 struct vnode_attr
*vap
, vfs_context_t ctx
)
6383 struct cnode
*cp
= NULL
;
6384 struct cnode
*dcp
= NULL
;
6386 struct hfsmount
*hfsmp
;
6387 struct cat_desc in_desc
, out_desc
;
6388 struct cat_attr attr
;
6391 int error
, started_tr
= 0;
6392 enum vtype vnodetype
;
6394 int newvnode_flags
= 0;
6395 u_int32_t gnv_flags
= 0;
6396 int protectable_target
= 0;
6398 vnode_t old_doc_vp
= NULL
;
6401 struct cprotect
*entry
= NULL
;
6402 int32_t cp_class
= -1;
6405 * By default, it's OK for AKS to overrride our target class preferences.
6407 uint32_t keywrap_flags
= CP_KEYWRAP_DIFFCLASS
;
6409 if (VATTR_IS_ACTIVE(vap
, va_dataprotect_class
)) {
6410 cp_class
= (int32_t)vap
->va_dataprotect_class
;
6412 * Since the user specifically requested this target class be used,
6413 * we want to fail this creation operation if we cannot wrap to their
6414 * target class. The CP_KEYWRAP_DIFFCLASS bit says that it is OK to
6415 * use a different class than the one specified, so we turn that off
6418 keywrap_flags
&= ~CP_KEYWRAP_DIFFCLASS
;
6420 int protected_mount
= 0;
6424 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
6427 /* set the cnode pointer only after successfully acquiring lock */
6430 /* Don't allow creation of new entries in open-unlinked directories */
6431 if ((error
= hfs_checkdeleted(dcp
))) {
6436 dcp
->c_flag
|= C_DIR_MODIFICATION
;
6438 hfsmp
= VTOHFS(dvp
);
6442 out_desc
.cd_flags
= 0;
6443 out_desc
.cd_nameptr
= NULL
;
6445 vnodetype
= vap
->va_type
;
6446 if (vnodetype
== VNON
)
6448 mode
= MAKEIMODE(vnodetype
, vap
->va_mode
);
6450 if (S_ISDIR (mode
) || S_ISREG (mode
)) {
6451 protectable_target
= 1;
6455 /* Check if were out of usable disk space. */
6456 if ((hfs_freeblks(hfsmp
, 1) == 0) && (vfs_context_suser(ctx
) != 0)) {
6463 /* Setup the default attributes */
6464 bzero(&attr
, sizeof(attr
));
6465 attr
.ca_mode
= mode
;
6466 attr
.ca_linkcount
= 1;
6467 if (VATTR_IS_ACTIVE(vap
, va_rdev
)) {
6468 attr
.ca_rdev
= vap
->va_rdev
;
6470 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
6471 VATTR_SET_SUPPORTED(vap
, va_create_time
);
6472 attr
.ca_itime
= vap
->va_create_time
.tv_sec
;
6474 attr
.ca_itime
= tv
.tv_sec
;
6477 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
6478 attr
.ca_itime
+= 3600; /* Same as what hfs_update does */
6481 attr
.ca_atime
= attr
.ca_ctime
= attr
.ca_mtime
= attr
.ca_itime
;
6482 attr
.ca_atimeondisk
= attr
.ca_atime
;
6483 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
6484 VATTR_SET_SUPPORTED(vap
, va_flags
);
6485 attr
.ca_flags
= vap
->va_flags
;
6489 * HFS+ only: all files get ThreadExists
6490 * HFSX only: dirs get HasFolderCount
6493 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
))
6496 if (vnodetype
== VDIR
) {
6497 if (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
)
6498 attr
.ca_recflags
= kHFSHasFolderCountMask
;
6500 attr
.ca_recflags
= kHFSThreadExistsMask
;
6505 if (cp_fs_protected(hfsmp
->hfs_mp
)) {
6506 protected_mount
= 1;
6509 * On a content-protected HFS+/HFSX filesystem, files and directories
6510 * cannot be created without atomically setting/creating the EA that
6511 * contains the protection class metadata and keys at the same time, in
6512 * the same transaction. As a result, pre-set the "EAs exist" flag
6513 * on the cat_attr for protectable catalog record creations. This will
6514 * cause the cnode creation routine in hfs_getnewvnode to mark the cnode
6517 if ((protected_mount
) && (protectable_target
)) {
6518 attr
.ca_recflags
|= kHFSHasAttributesMask
;
6519 /* delay entering in the namecache */
6526 * Add the date added to the item. See above, as
6527 * all of the dates are set to the itime.
6529 hfs_write_dateadded (&attr
, attr
.ca_atime
);
6531 /* Initialize the gen counter to 1 */
6532 hfs_write_gencount(&attr
, (uint32_t)1);
6534 attr
.ca_uid
= vap
->va_uid
;
6535 attr
.ca_gid
= vap
->va_gid
;
6536 VATTR_SET_SUPPORTED(vap
, va_mode
);
6537 VATTR_SET_SUPPORTED(vap
, va_uid
);
6538 VATTR_SET_SUPPORTED(vap
, va_gid
);
6541 /* check to see if this node's creation would cause us to go over
6542 * quota. If so, abort this operation.
6544 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
6545 if ((error
= hfs_quotacheck(hfsmp
, 1, attr
.ca_uid
, attr
.ca_gid
,
6546 vfs_context_ucred(ctx
)))) {
6553 /* Tag symlinks with a type and creator. */
6554 if (vnodetype
== VLNK
) {
6555 struct FndrFileInfo
*fip
;
6557 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
6558 fip
->fdType
= SWAP_BE32(kSymLinkFileType
);
6559 fip
->fdCreator
= SWAP_BE32(kSymLinkCreator
);
6562 /* Setup the descriptor */
6563 in_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
6564 in_desc
.cd_namelen
= cnp
->cn_namelen
;
6565 in_desc
.cd_parentcnid
= dcp
->c_fileid
;
6566 in_desc
.cd_flags
= S_ISDIR(mode
) ? CD_ISDIR
: 0;
6567 in_desc
.cd_hint
= dcp
->c_childhint
;
6568 in_desc
.cd_encoding
= 0;
6572 * To preserve file creation atomicity with regards to the content protection EA,
6573 * we must create the file in the catalog and then write out its EA in the same
6576 * We only denote the target class in this EA; key generation is not completed
6577 * until the file has been inserted into the catalog and will be done
6578 * in a separate transaction.
6580 if ((protected_mount
) && (protectable_target
)) {
6581 error
= cp_setup_newentry(hfsmp
, dcp
, cp_class
, attr
.ca_mode
, &entry
);
6588 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
6593 // have to also lock the attribute file because cat_create() needs
6594 // to check that any fileID it wants to use does not have orphaned
6595 // attributes in it.
6596 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
6599 /* Reserve some space in the Catalog file. */
6600 if ((error
= cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0))) {
6601 hfs_systemfile_unlock(hfsmp
, lockflags
);
6605 if ((error
= cat_acquire_cnid(hfsmp
, &new_id
))) {
6606 hfs_systemfile_unlock (hfsmp
, lockflags
);
6610 error
= cat_create(hfsmp
, new_id
, &in_desc
, &attr
, &out_desc
);
6612 /* Update the parent directory */
6613 dcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
6616 if (vnodetype
== VDIR
) {
6617 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
6619 dcp
->c_dirchangecnt
++;
6620 hfs_incr_gencount(dcp
);
6622 dcp
->c_touch_chgtime
= dcp
->c_touch_modtime
= true;
6623 dcp
->c_flag
|= C_MODIFIED
;
6625 hfs_update(dcp
->c_vp
, 0);
6629 * If we are creating a content protected file, now is when
6630 * we create the EA. We must create it in the same transaction
6631 * that creates the file. We can also guarantee that the file
6632 * MUST exist because we are still holding the catalog lock
6635 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
6636 error
= cp_setxattr (NULL
, entry
, hfsmp
, attr
.ca_fileid
, XATTR_CREATE
);
6641 * If we fail the EA creation, then we need to delete the file.
6642 * Luckily, we are still holding all of the right locks.
6644 delete_err
= cat_delete (hfsmp
, &out_desc
, &attr
);
6645 if (delete_err
== 0) {
6646 /* Update the parent directory */
6647 if (dcp
->c_entries
> 0)
6649 dcp
->c_dirchangecnt
++;
6650 dcp
->c_ctime
= tv
.tv_sec
;
6651 dcp
->c_mtime
= tv
.tv_sec
;
6652 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
6655 /* Emit EINVAL if we fail to create EA*/
6661 hfs_systemfile_unlock(hfsmp
, lockflags
);
6665 uint32_t txn
= hfsmp
->jnl
? journal_current_txn(hfsmp
->jnl
) : 0;
6667 /* Invalidate negative cache entries in the directory */
6668 if (dcp
->c_flag
& C_NEG_ENTRIES
) {
6669 cache_purge_negatives(dvp
);
6670 dcp
->c_flag
&= ~C_NEG_ENTRIES
;
6673 hfs_volupdate(hfsmp
, vnodetype
== VDIR
? VOL_MKDIR
: VOL_MKFILE
,
6674 (dcp
->c_cnid
== kHFSRootFolderID
));
6677 // have to end the transaction here before we call hfs_getnewvnode()
6678 // because that can cause us to try and reclaim a vnode on a different
6679 // file system which could cause us to start a transaction which can
6680 // deadlock with someone on that other file system (since we could be
6681 // holding two transaction locks as well as various vnodes and we did
6682 // not obtain the locks on them in the proper order).
6684 // NOTE: this means that if the quota check fails or we have to update
6685 // the change time on a block-special device that those changes
6686 // will happen as part of independent transactions.
6689 hfs_end_transaction(hfsmp
);
6695 * At this point, we must have encountered success with writing the EA.
6696 * Destroy our temporary cprotect (which had no keys).
6699 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
6700 cp_entry_destroy (hfsmp
, entry
);
6704 gnv_flags
|= GNV_CREATE
;
6706 gnv_flags
|= GNV_NOCACHE
;
6710 * Create a vnode for the object just created.
6712 * NOTE: Maintaining the cnode lock on the parent directory is important,
6713 * as it prevents race conditions where other threads want to look up entries
6714 * in the directory and/or add things as we are in the process of creating
6715 * the vnode below. However, this has the potential for causing a
6716 * double lock panic when dealing with shadow files on a HFS boot partition.
6717 * The panic could occur if we are not cleaning up after ourselves properly
6718 * when done with a shadow file or in the error cases. The error would occur if we
6719 * try to create a new vnode, and then end up reclaiming another shadow vnode to
6720 * create the new one. However, if everything is working properly, this should
6721 * be a non-issue as we would never enter that reclaim codepath.
6723 * The cnode is locked on successful return.
6725 error
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &out_desc
, gnv_flags
, &attr
,
6726 NULL
, &tvp
, &newvnode_flags
);
6732 cp
->c_update_txn
= txn
;
6734 struct doc_tombstone
*ut
;
6735 ut
= doc_tombstone_get();
6736 if ( ut
->t_lastop_document_id
!= 0
6737 && ut
->t_lastop_parent
== dvp
6738 && ut
->t_lastop_parent_vid
== vnode_vid(dvp
)
6739 && strcmp((char *)ut
->t_lastop_filename
, (const char *)cp
->c_desc
.cd_nameptr
) == 0) {
6740 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
6742 //printf("CREATE: preserving doc-id %lld on %s\n", ut->t_lastop_document_id, ut->t_lastop_filename);
6743 fip
->document_id
= (uint32_t)(ut
->t_lastop_document_id
& 0xffffffff);
6745 cp
->c_bsdflags
|= UF_TRACKED
;
6746 cp
->c_flag
|= C_MODIFIED
;
6748 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
6749 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
6751 (void) cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, NULL
, NULL
);
6753 hfs_systemfile_unlock (hfsmp
, lockflags
);
6754 (void) hfs_end_transaction(hfsmp
);
6757 doc_tombstone_clear(ut
, &old_doc_vp
);
6758 } else if (ut
->t_lastop_document_id
!= 0) {
6759 int len
= cnp
->cn_namelen
;
6761 len
= strlen(cnp
->cn_nameptr
);
6764 if (doc_tombstone_should_ignore_name(cnp
->cn_nameptr
, cnp
->cn_namelen
)) {
6765 // printf("CREATE: not clearing tombstone because %s is a temp name.\n", cnp->cn_nameptr);
6767 // Clear the tombstone because the thread is not recreating the same path
6768 // printf("CREATE: clearing tombstone because %s is NOT a temp name.\n", cnp->cn_nameptr);
6769 doc_tombstone_clear(ut
, NULL
);
6773 if ((hfsmp
->hfs_flags
& HFS_CS_HOTFILE_PIN
) && (vnode_isfastdevicecandidate(dvp
) && !vnode_isautocandidate(dvp
))) {
6775 //printf("hfs: flagging %s (fileid: %d) as VFASTDEVCANDIDATE (dvp name: %s)\n",
6776 // cnp->cn_nameptr ? cnp->cn_nameptr : "<NONAME>",
6778 // dvp->v_name ? dvp->v_name : "no-dir-name");
6781 // On new files we set the FastDevCandidate flag so that
6782 // any new blocks allocated to it will be pinned.
6784 cp
->c_attr
.ca_recflags
|= kHFSFastDevCandidateMask
;
6785 vnode_setfastdevicecandidate(tvp
);
6788 // properly inherit auto-cached flags
6790 if (vnode_isautocandidate(dvp
)) {
6791 cp
->c_attr
.ca_recflags
|= kHFSAutoCandidateMask
;
6792 vnode_setautocandidate(tvp
);
6797 // We also want to add it to the hotfile adoption list so
6798 // that it will eventually land in the hotfile btree
6800 (void) hfs_addhotfile(tvp
);
6807 * Now that we have a vnode-in-hand, generate keys for this namespace item.
6808 * If we fail to create the keys, then attempt to delete the item from the
6809 * namespace. If we can't delete the item, that's not desirable but also not fatal..
6810 * All of the places which deal with restoring/unwrapping keys must also be
6811 * prepared to encounter an entry that does not have keys.
6813 if ((protectable_target
) && (protected_mount
)) {
6814 struct cprotect
*keyed_entry
= NULL
;
6816 if (cp
->c_cpentry
== NULL
) {
6817 panic ("hfs_makenode: no cpentry for cnode (%p)", cp
);
6820 error
= cp_generate_keys (hfsmp
, cp
, CP_CLASS(cp
->c_cpentry
->cp_pclass
), keywrap_flags
, &keyed_entry
);
6823 * Upon success, the keys were generated and written out.
6824 * Update the cp pointer in the cnode.
6826 cp_replace_entry (hfsmp
, cp
, keyed_entry
);
6828 cache_enter (dvp
, tvp
, cnp
);
6832 /* If key creation OR the setxattr failed, emit EPERM to userland */
6836 * Beware! This slightly violates the lock ordering for the
6837 * cnode/vnode 'tvp'. Ordinarily, you must acquire the truncate lock
6838 * which guards file size changes before acquiring the normal cnode lock
6839 * and calling hfs_removefile on an item.
6841 * However, in this case, we are still holding the directory lock so
6842 * 'tvp' is not lookup-able and it was a newly created vnode so it
6843 * cannot have any content yet. The only reason we are initiating
6844 * the removefile is because we could not generate content protection keys
6845 * for this namespace item. Note also that we pass a '1' in the allow_dirs
6846 * argument for hfs_removefile because we may be creating a directory here.
6848 * All this to say that while it is technically a violation it is
6849 * impossible to race with another thread for this cnode so it is safe.
6851 int err
= hfs_removefile (dvp
, tvp
, cnp
, 0, 0, 1, NULL
, 0);
6853 printf("hfs_makenode: removefile failed (%d) for CP entry %p\n", err
, tvp
);
6856 /* Release the cnode lock and mark the vnode for termination */
6858 err
= vnode_recycle (tvp
);
6860 printf("hfs_makenode: vnode_recycle failed (%d) for CP entry %p\n", err
, tvp
);
6863 /* Drop the iocount on the new vnode to force reclamation/recycling */
6873 * Once we create this vnode, we need to initialize its quota data
6874 * structures, if necessary. We know that it is OK to just go ahead and
6875 * initialize because we've already validated earlier (through the hfs_quotacheck
6876 * function) to see if creating this cnode/vnode would cause us to go over quota.
6878 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
6880 /* cp could have been zeroed earlier */
6881 (void) hfs_getinoquota(cp
);
6887 cat_releasedesc(&out_desc
);
6891 * We may have jumped here in error-handling various situations above.
6892 * If we haven't already dumped the temporary CP used to initialize
6893 * the file atomically, then free it now. cp_entry_destroy should null
6894 * out the pointer if it was called already.
6897 cp_entry_destroy (hfsmp
, entry
);
6903 * Make sure we release cnode lock on dcp.
6906 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
6907 wakeup((caddr_t
)&dcp
->c_flag
);
6911 ino64_t file_id
= 0;
6912 if (error
== 0 && cp
!= NULL
) {
6913 file_id
= cp
->c_fileid
;
6917 hfs_end_transaction(hfsmp
);
6922 cnode_t
*ocp
= VTOC(old_doc_vp
);
6923 hfs_lock_always(ocp
, HFS_EXCLUSIVE_LOCK
);
6924 struct FndrExtendedFileInfo
*ofip
= (struct FndrExtendedFileInfo
*)((char *)&ocp
->c_attr
.ca_finderinfo
+ 16);
6926 const uint32_t doc_id
= ofip
->document_id
;
6927 const ino64_t old_file_id
= ocp
->c_fileid
;
6929 // printf("clearing doc-id from ino %d\n", ocp->c_desc.cd_cnid);
6930 ofip
->document_id
= 0;
6931 ocp
->c_bsdflags
&= ~UF_TRACKED
;
6932 ocp
->c_flag
|= C_MODIFIED
;
6935 vnode_put(old_doc_vp
);
6937 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
6938 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
6939 FSE_ARG_INO
, old_file_id
, // src inode #
6940 FSE_ARG_INO
, file_id
, // dst inode #
6941 FSE_ARG_INT32
, doc_id
,
6950 * hfs_vgetrsrc acquires a resource fork vnode corresponding to the
6951 * cnode that is found in 'vp'. The cnode should be locked upon entry
6952 * and will be returned locked, but it may be dropped temporarily.
6954 * If the resource fork vnode does not exist, HFS will attempt to acquire an
6955 * empty (uninitialized) vnode from VFS so as to avoid deadlocks with
6956 * jetsam. If we let the normal getnewvnode code produce the vnode for us
6957 * we would be doing so while holding the cnode lock of our cnode.
6959 * On success, *rvpp wlll hold the resource fork vnode with an
6960 * iocount. *Don't* forget the vnode_put.
6963 hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
, struct vnode
**rvpp
)
6965 struct vnode
*rvp
= NULLVP
;
6966 struct vnode
*empty_rvp
= NULLVP
;
6967 struct vnode
*dvp
= NULLVP
;
6968 struct cnode
*cp
= VTOC(vp
);
6972 if (vnode_vtype(vp
) == VDIR
) {
6977 /* Attempt to use existing vnode */
6978 if ((rvp
= cp
->c_rsrc_vp
)) {
6979 vid
= vnode_vid(rvp
);
6981 // vnode_getwithvid can block so we need to drop the cnode lock
6984 error
= vnode_getwithvid(rvp
, vid
);
6986 hfs_lock_always(cp
, HFS_EXCLUSIVE_LOCK
);
6989 * When our lock was relinquished, the resource fork
6990 * could have been recycled. Check for this and try
6993 if (error
== ENOENT
)
6997 const char * name
= (const char *)VTOC(vp
)->c_desc
.cd_nameptr
;
7000 printf("hfs_vgetrsrc: couldn't get resource"
7001 " fork for %s, vol=%s, err=%d\n", name
, hfsmp
->vcbVN
, error
);
7005 struct cat_fork rsrcfork
;
7006 struct componentname cn
;
7007 struct cat_desc
*descptr
= NULL
;
7008 struct cat_desc to_desc
;
7011 int newvnode_flags
= 0;
7014 * In this case, we don't currently see a resource fork vnode attached
7015 * to this cnode. In most cases, we were called from a read-only VNOP
7016 * like getattr, so it should be safe to drop the cnode lock and then
7019 * Here, we drop the lock so that we can acquire an empty/husk
7020 * vnode so that we don't deadlock against jetsam.
7022 * It does not currently appear possible to hold the truncate lock via
7023 * FS re-entrancy when we get to this point. (8/2014)
7027 error
= vnode_create_empty (&empty_rvp
);
7029 hfs_lock_always (cp
, HFS_EXCLUSIVE_LOCK
);
7032 /* If acquiring the 'empty' vnode failed, then nothing to clean up */
7037 * We could have raced with another thread here while we dropped our cnode
7038 * lock. See if the cnode now has a resource fork vnode and restart if appropriate.
7040 * Note: We just released the cnode lock, so there is a possibility that the
7041 * cnode that we just acquired has been deleted or even removed from disk
7042 * completely, though this is unlikely. If the file is open-unlinked, the
7043 * check below will resolve it for us. If it has been completely
7044 * removed (even from the catalog!), then when we examine the catalog
7045 * directly, below, while holding the catalog lock, we will not find the
7046 * item and we can fail out properly.
7048 if (cp
->c_rsrc_vp
) {
7049 /* Drop the empty vnode before restarting */
7050 vnode_put (empty_rvp
);
7057 * hfs_vgetsrc may be invoked for a cnode that has already been marked
7058 * C_DELETED. This is because we need to continue to provide rsrc
7059 * fork access to open-unlinked files. In this case, build a fake descriptor
7060 * like in hfs_removefile. If we don't do this, buildkey will fail in
7061 * cat_lookup because this cnode has no name in its descriptor.
7063 if ((cp
->c_flag
& C_DELETED
) && (cp
->c_desc
.cd_namelen
== 0)) {
7064 bzero (&to_desc
, sizeof(to_desc
));
7065 bzero (delname
, 32);
7066 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
7067 to_desc
.cd_nameptr
= (const u_int8_t
*) delname
;
7068 to_desc
.cd_namelen
= strlen(delname
);
7069 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
7070 to_desc
.cd_flags
= 0;
7071 to_desc
.cd_cnid
= cp
->c_cnid
;
7076 descptr
= &cp
->c_desc
;
7080 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
7083 * We call cat_idlookup (instead of cat_lookup) below because we can't
7084 * trust the descriptor in the provided cnode for lookups at this point.
7085 * Between the time of the original lookup of this vnode and now, the
7086 * descriptor could have gotten swapped or replaced. If this occurred,
7087 * the parent/name combo originally desired may not necessarily be provided
7088 * if we use the descriptor. Even worse, if the vnode represents
7089 * a hardlink, we could have removed one of the links from the namespace
7090 * but left the descriptor alone, since hfs_unlink does not invalidate
7091 * the descriptor in the cnode if other links still point to the inode.
7093 * Consider the following (slightly contrived) scenario:
7094 * /tmp/a <--> /tmp/b (hardlinks).
7095 * 1. Thread A: open rsrc fork on /tmp/b.
7096 * 1a. Thread A: does lookup, goes out to lunch right before calling getnamedstream.
7097 * 2. Thread B does 'mv /foo/b /tmp/b'
7098 * 2. Thread B succeeds.
7099 * 3. Thread A comes back and wants rsrc fork info for /tmp/b.
7101 * Even though the hardlink backing /tmp/b is now eliminated, the descriptor
7102 * is not removed/updated during the unlink process. So, if you were to
7103 * do a lookup on /tmp/b, you'd acquire an entirely different record's resource
7106 * As a result, we use the fileid, which should be invariant for the lifetime
7107 * of the cnode (possibly barring calls to exchangedata).
7109 * Addendum: We can't do the above for HFS standard since we aren't guaranteed to
7110 * have thread records for files. They were only required for directories. So
7111 * we need to do the lookup with the catalog name. This is OK since hardlinks were
7112 * never allowed on HFS standard.
7115 /* Get resource fork data */
7117 if (ISSET(hfsmp
->hfs_flags
, HFS_STANDARD
)) {
7119 * HFS standard only:
7121 * Get the resource fork for this item with a cat_lookup call, but do not
7122 * force a case lookup since HFS standard is case-insensitive only. We
7123 * don't want the descriptor; just the fork data here. If we tried to
7124 * do a ID lookup (via thread record -> catalog record), then we might fail
7125 * prematurely since, as noted above, thread records were not strictly required
7128 error
= cat_lookup (hfsmp
, descptr
, 1, 0, (struct cat_desc
*)NULL
,
7129 (struct cat_attr
*)NULL
, &rsrcfork
, NULL
);
7133 error
= cat_idlookup (hfsmp
, cp
->c_fileid
, 0, 1, NULL
, NULL
, &rsrcfork
);
7136 hfs_systemfile_unlock(hfsmp
, lockflags
);
7138 /* Drop our 'empty' vnode ! */
7139 vnode_put (empty_rvp
);
7143 * Supply hfs_getnewvnode with a component name.
7146 if (descptr
->cd_nameptr
) {
7147 void *buf
= hfs_malloc(MAXPATHLEN
);
7149 cn
= (struct componentname
){
7150 .cn_nameiop
= LOOKUP
,
7151 .cn_flags
= ISLASTCN
,
7152 .cn_pnlen
= MAXPATHLEN
,
7155 .cn_namelen
= snprintf(buf
, MAXPATHLEN
,
7156 "%s%s", descptr
->cd_nameptr
,
7160 // Should never happen because cn.cn_nameptr won't ever be long...
7161 if (cn
.cn_namelen
>= MAXPATHLEN
) {
7162 hfs_free(buf
, MAXPATHLEN
);
7163 /* Drop our 'empty' vnode ! */
7164 vnode_put (empty_rvp
);
7165 return ENAMETOOLONG
;
7169 dvp
= vnode_getparent(vp
);
7172 * We are about to call hfs_getnewvnode and pass in the vnode that we acquired
7173 * earlier when we were not holding any locks. The semantics of GNV_USE_VP require that
7174 * either hfs_getnewvnode consume the vnode and vend it back to us, properly initialized,
7175 * or it will consume/dispose of it properly if it errors out.
7179 error
= hfs_getnewvnode(hfsmp
, dvp
, cn
.cn_pnbuf
? &cn
: NULL
,
7180 descptr
, (GNV_WANTRSRC
| GNV_SKIPLOCK
| GNV_USE_VP
),
7181 &cp
->c_attr
, &rsrcfork
, &rvp
, &newvnode_flags
);
7185 hfs_free(cn
.cn_pnbuf
, MAXPATHLEN
);
7188 } /* End 'else' for rsrc fork not existing */
7195 * Wrapper for special device reads
7198 hfsspec_read(struct vnop_read_args
*ap
)
7203 cnode_t
*cp
= VTOC(ap
->a_vp
);
7206 cp
->c_touch_acctime
= TRUE
;
7208 return spec_read(ap
);
7212 * Wrapper for special device writes
7215 hfsspec_write(struct vnop_write_args
*ap
)
7218 * Set update and change flags.
7220 cnode_t
*cp
= VTOC(ap
->a_vp
);
7223 cp
->c_touch_chgtime
= TRUE
;
7224 cp
->c_touch_modtime
= TRUE
;
7227 return spec_write(ap
);
7231 * Wrapper for special device close
7233 * Update the times on the cnode then do device close.
7236 hfsspec_close(struct vnop_close_args
*ap
)
7238 struct vnode
*vp
= ap
->a_vp
;
7239 cnode_t
*cp
= VTOC(vp
);
7241 if (cp
&& vnode_isinuse(ap
->a_vp
, 0)) {
7242 if (hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
7243 hfs_touchtimes(VTOHFS(vp
), cp
);
7247 return spec_close(ap
);
7252 * Wrapper for fifo reads
7255 hfsfifo_read(struct vnop_read_args
*ap
)
7260 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
7261 return fifo_read(ap
);
7265 * Wrapper for fifo writes
7268 hfsfifo_write(struct vnop_write_args
*ap
)
7271 * Set update and change flags.
7273 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
7274 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
7275 return fifo_write(ap
);
7279 * Wrapper for fifo close
7281 * Update the times on the cnode then do device close.
7284 hfsfifo_close(struct vnop_close_args
*ap
)
7286 struct vnode
*vp
= ap
->a_vp
;
7289 if (vnode_isinuse(ap
->a_vp
, 1)) {
7290 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
7292 hfs_touchtimes(VTOHFS(vp
), cp
);
7296 return fifo_close(ap
);
7303 * Getter for the document_id
7304 * the document_id is stored in FndrExtendedFileInfo/FndrExtendedDirInfo
7307 hfs_get_document_id_internal(const uint8_t *finderinfo
, mode_t mode
)
7309 const uint8_t *finfo
= NULL
;
7310 u_int32_t doc_id
= 0;
7312 /* overlay the FinderInfo to the correct pointer, and advance */
7313 finfo
= finderinfo
+ 16;
7315 if (S_ISDIR(mode
) || S_ISREG(mode
)) {
7316 const struct FndrExtendedFileInfo
*extinfo
= (const struct FndrExtendedFileInfo
*)finfo
;
7317 doc_id
= extinfo
->document_id
;
7324 /* getter(s) for document id */
7326 hfs_get_document_id(struct cnode
*cp
)
7328 return (hfs_get_document_id_internal((u_int8_t
*)cp
->c_finderinfo
,
7329 cp
->c_attr
.ca_mode
));
7332 /* If you have finderinfo and mode, you can use this */
7334 hfs_get_document_id_from_blob(const uint8_t *finderinfo
, mode_t mode
)
7336 return (hfs_get_document_id_internal(finderinfo
, mode
));
7340 * Synchronize a file's in-core state with that on disk.
7343 hfs_vnop_fsync(struct vnop_fsync_args
*ap
)
7345 struct vnode
* vp
= ap
->a_vp
;
7348 /* Note: We check hfs flags instead of vfs mount flag because during
7349 * read-write update, hfs marks itself read-write much earlier than
7350 * the vfs, and hence won't result in skipping of certain writes like
7351 * zero'ing out of unused nodes, creation of hotfiles btree, etc.
7353 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
) {
7358 * No need to call cp_handle_vnop to resolve fsync(). Any dirty data
7359 * should have caused the keys to be unwrapped at the time the data was
7360 * put into the UBC, either at mmap/pagein/read-write. If we did manage
7361 * to let this by, then strategy will auto-resolve for us.
7363 * We also need to allow ENOENT lock errors since unlink
7364 * system call can call VNOP_FSYNC during vclean.
7366 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
7370 error
= hfs_fsync(vp
, ap
->a_waitfor
, 0, vfs_context_proc(ap
->a_context
));
7372 hfs_unlock(VTOC(vp
));
7376 int (**hfs_vnodeop_p
)(void *);
7378 #define VOPFUNC int (*)(void *)
7382 int (**hfs_std_vnodeop_p
) (void *);
7383 static int hfs_readonly_op (__unused
void* ap
) { return (EROFS
); }
7386 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
7387 * is for use with HFS standard to block out operations that would modify the file system
7390 struct vnodeopv_entry_desc hfs_standard_vnodeop_entries
[] = {
7391 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
7392 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
7393 { &vnop_create_desc
, (VOPFUNC
)hfs_readonly_op
}, /* create (READONLY) */
7394 { &vnop_mknod_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mknod (READONLY) */
7395 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
7396 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
7397 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
7398 { &vnop_setattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* setattr */
7399 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
7400 { &vnop_write_desc
, (VOPFUNC
)hfs_readonly_op
}, /* write (READONLY) */
7401 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
7402 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
7403 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
7404 { &vnop_exchange_desc
, (VOPFUNC
)hfs_readonly_op
}, /* exchange (READONLY)*/
7405 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
7406 { &vnop_fsync_desc
, (VOPFUNC
)hfs_readonly_op
}, /* fsync (READONLY) */
7407 { &vnop_remove_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove (READONLY) */
7408 { &vnop_link_desc
, (VOPFUNC
)hfs_readonly_op
}, /* link ( READONLLY) */
7409 { &vnop_rename_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rename (READONLY)*/
7410 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mkdir (READONLY) */
7411 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rmdir (READONLY) */
7412 { &vnop_symlink_desc
, (VOPFUNC
)hfs_readonly_op
}, /* symlink (READONLY) */
7413 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
7414 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
7415 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
7416 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
7417 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
7418 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
7419 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
7420 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
7421 { &vnop_allocate_desc
, (VOPFUNC
)hfs_readonly_op
}, /* allocate (READONLY) */
7423 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
7425 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
7427 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_readonly_op
}, /* bwrite (READONLY) */
7428 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
7429 { &vnop_pageout_desc
,(VOPFUNC
) hfs_readonly_op
}, /* pageout (READONLY) */
7430 { &vnop_copyfile_desc
, (VOPFUNC
)hfs_readonly_op
}, /* copyfile (READONLY)*/
7431 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7432 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7433 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
7434 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7435 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* set xattr (READONLY) */
7436 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove xattr (READONLY) */
7437 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7439 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
7440 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
7441 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
7443 { &vnop_getattrlistbulk_desc
, (VOPFUNC
)hfs_vnop_getattrlistbulk
}, /* getattrlistbulk */
7444 { NULL
, (VOPFUNC
)NULL
}
7447 struct vnodeopv_desc hfs_std_vnodeop_opv_desc
=
7448 { &hfs_std_vnodeop_p
, hfs_standard_vnodeop_entries
};
7451 /* VNOP table for HFS+ */
7452 struct vnodeopv_entry_desc hfs_vnodeop_entries
[] = {
7453 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
7454 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
7455 { &vnop_create_desc
, (VOPFUNC
)hfs_vnop_create
}, /* create */
7456 { &vnop_mknod_desc
, (VOPFUNC
)hfs_vnop_mknod
}, /* mknod */
7457 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
7458 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
7459 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
7460 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
7461 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
7462 { &vnop_write_desc
, (VOPFUNC
)hfs_vnop_write
}, /* write */
7463 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
7464 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
7465 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
7466 { &vnop_exchange_desc
, (VOPFUNC
)hfs_vnop_exchange
}, /* exchange */
7467 { &vnop_mmap_desc
, (VOPFUNC
)hfs_vnop_mmap
}, /* mmap */
7468 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
7469 { &vnop_remove_desc
, (VOPFUNC
)hfs_vnop_remove
}, /* remove */
7470 { &vnop_link_desc
, (VOPFUNC
)hfs_vnop_link
}, /* link */
7471 { &vnop_rename_desc
, (VOPFUNC
)hfs_vnop_rename
}, /* rename */
7472 { &vnop_renamex_desc
, (VOPFUNC
)hfs_vnop_renamex
}, /* renamex (with flags) */
7473 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_vnop_mkdir
}, /* mkdir */
7474 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_vnop_rmdir
}, /* rmdir */
7475 { &vnop_symlink_desc
, (VOPFUNC
)hfs_vnop_symlink
}, /* symlink */
7476 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
7477 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
7478 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
7479 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
7480 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
7481 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
7482 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
7483 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
7484 { &vnop_allocate_desc
, (VOPFUNC
)hfs_vnop_allocate
}, /* allocate */
7486 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
7488 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
7490 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
}, /* bwrite */
7491 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
7492 { &vnop_pageout_desc
,(VOPFUNC
) hfs_vnop_pageout
}, /* pageout */
7493 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
7494 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7495 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7496 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
7497 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7498 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
7499 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
7500 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7502 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
7503 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_vnop_makenamedstream
},
7504 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_vnop_removenamedstream
},
7506 { &vnop_getattrlistbulk_desc
, (VOPFUNC
)hfs_vnop_getattrlistbulk
}, /* getattrlistbulk */
7507 { &vnop_mnomap_desc
, (VOPFUNC
)hfs_vnop_mnomap
},
7508 { NULL
, (VOPFUNC
)NULL
}
7511 struct vnodeopv_desc hfs_vnodeop_opv_desc
=
7512 { &hfs_vnodeop_p
, hfs_vnodeop_entries
};
7515 /* Spec Op vnop table for HFS+ */
7516 int (**hfs_specop_p
)(void *);
7517 struct vnodeopv_entry_desc hfs_specop_entries
[] = {
7518 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
7519 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
7520 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
7521 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
7522 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
7523 { &vnop_close_desc
, (VOPFUNC
)hfsspec_close
}, /* close */
7524 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
7525 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
7526 { &vnop_read_desc
, (VOPFUNC
)hfsspec_read
}, /* read */
7527 { &vnop_write_desc
, (VOPFUNC
)hfsspec_write
}, /* write */
7528 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
7529 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
7530 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
7531 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
7532 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
7533 { &vnop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
7534 { &vnop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
7535 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
7536 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
7537 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
7538 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
7539 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
7540 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
7541 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
7542 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
7543 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
7544 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
7545 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
7546 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
7547 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
7548 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
7549 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
7550 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7551 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7552 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7553 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
7554 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
7555 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7556 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
7558 struct vnodeopv_desc hfs_specop_opv_desc
=
7559 { &hfs_specop_p
, hfs_specop_entries
};
7562 /* HFS+ FIFO VNOP table */
7563 int (**hfs_fifoop_p
)(void *);
7564 struct vnodeopv_entry_desc hfs_fifoop_entries
[] = {
7565 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
7566 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
7567 { &vnop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
7568 { &vnop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
7569 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
7570 { &vnop_close_desc
, (VOPFUNC
)hfsfifo_close
}, /* close */
7571 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
7572 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
7573 { &vnop_read_desc
, (VOPFUNC
)hfsfifo_read
}, /* read */
7574 { &vnop_write_desc
, (VOPFUNC
)hfsfifo_write
}, /* write */
7575 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
7576 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
7577 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
7578 { &vnop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
7579 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
7580 { &vnop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
7581 { &vnop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
7582 { &vnop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
7583 { &vnop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
7584 { &vnop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
7585 { &vnop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
7586 { &vnop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
7587 { &vnop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
7588 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
7589 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
7590 { &vnop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
7591 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
7592 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
7593 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
7594 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
7595 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
7596 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
7597 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7598 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7599 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
7600 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7601 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
7602 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
7603 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7604 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
7606 struct vnodeopv_desc hfs_fifoop_opv_desc
=
7607 { &hfs_fifoop_p
, hfs_fifoop_entries
};