2 * Copyright (c) 2000-2013 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 <sys/systm.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/file_internal.h>
33 #include <sys/dirent.h>
36 #include <sys/buf_internal.h>
37 #include <sys/mount.h>
38 #include <sys/vnode_if.h>
39 #include <sys/vnode_internal.h>
40 #include <sys/malloc.h>
42 #include <sys/ubc_internal.h>
43 #include <sys/paths.h>
44 #include <sys/quota.h>
47 #include <sys/kauth.h>
48 #include <sys/uio_internal.h>
49 #include <sys/fsctl.h>
50 #include <sys/cprotect.h>
51 #include <sys/xattr.h>
53 #include <sys/fsevents.h>
54 #include <kern/kalloc.h>
56 #include <miscfs/specfs/specdev.h>
57 #include <miscfs/fifofs/fifo.h>
58 #include <vfs/vfs_support.h>
59 #include <machine/spl.h>
61 #include <sys/kdebug.h>
62 #include <sys/sysctl.h>
65 #include "hfs_catalog.h"
66 #include "hfs_cnode.h"
68 #include "hfs_mount.h"
69 #include "hfs_quota.h"
70 #include "hfs_endian.h"
72 #include "hfscommon/headers/BTreesInternal.h"
73 #include "hfscommon/headers/FileMgrInternal.h"
75 #define KNDETACH_VNLOCKED 0x00000001
77 /* Global vfs data structures for hfs */
79 /* Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is 'no') */
80 int always_do_fullfsync
= 0;
81 SYSCTL_DECL(_vfs_generic
);
82 SYSCTL_INT (_vfs_generic
, OID_AUTO
, always_do_fullfsync
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &always_do_fullfsync
, 0, "always F_FULLFSYNC when fsync is called");
84 int hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
,
85 struct componentname
*cnp
, struct vnode_attr
*vap
,
87 int hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
);
88 int hfs_metasync_all(struct hfsmount
*hfsmp
);
90 int hfs_removedir(struct vnode
*, struct vnode
*, struct componentname
*,
92 int hfs_removefile(struct vnode
*, struct vnode
*, struct componentname
*,
93 int, int, int, struct vnode
*, int);
95 /* Used here and in cnode teardown -- for symlinks */
96 int hfs_removefile_callback(struct buf
*bp
, void *hfsmp
);
98 int hfs_movedata (struct vnode
*, struct vnode
*);
99 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src
,
100 struct filefork
*dstfork
, struct cnode
*dst
);
102 decmpfs_cnode
* hfs_lazy_init_decmpfs_cnode (struct cnode
*cp
);
105 static int hfsfifo_read(struct vnop_read_args
*);
106 static int hfsfifo_write(struct vnop_write_args
*);
107 static int hfsfifo_close(struct vnop_close_args
*);
109 extern int (**fifo_vnodeop_p
)(void *);
112 int hfs_vnop_close(struct vnop_close_args
*);
113 int hfs_vnop_create(struct vnop_create_args
*);
114 int hfs_vnop_exchange(struct vnop_exchange_args
*);
115 int hfs_vnop_fsync(struct vnop_fsync_args
*);
116 int hfs_vnop_mkdir(struct vnop_mkdir_args
*);
117 int hfs_vnop_mknod(struct vnop_mknod_args
*);
118 int hfs_vnop_getattr(struct vnop_getattr_args
*);
119 int hfs_vnop_open(struct vnop_open_args
*);
120 int hfs_vnop_readdir(struct vnop_readdir_args
*);
121 int hfs_vnop_remove(struct vnop_remove_args
*);
122 int hfs_vnop_rename(struct vnop_rename_args
*);
123 int hfs_vnop_rmdir(struct vnop_rmdir_args
*);
124 int hfs_vnop_symlink(struct vnop_symlink_args
*);
125 int hfs_vnop_setattr(struct vnop_setattr_args
*);
126 int hfs_vnop_readlink(struct vnop_readlink_args
*);
127 int hfs_vnop_pathconf(struct vnop_pathconf_args
*);
128 int hfs_vnop_whiteout(struct vnop_whiteout_args
*);
129 int hfs_vnop_mmap(struct vnop_mmap_args
*ap
);
130 int hfsspec_read(struct vnop_read_args
*);
131 int hfsspec_write(struct vnop_write_args
*);
132 int hfsspec_close(struct vnop_close_args
*);
134 /* Options for hfs_removedir and hfs_removefile */
135 #define HFSRM_SKIP_RESERVE 0x01
139 /*****************************************************************************
141 * Common Operations on vnodes
143 *****************************************************************************/
146 * Is the given cnode either the .journal or .journal_info_block file on
147 * a volume with an active journal? Many VNOPs use this to deny access
150 * Note: the .journal file on a volume with an external journal still
151 * returns true here, even though it does not actually hold the contents
152 * of the volume's journal.
155 hfs_is_journal_file(struct hfsmount
*hfsmp
, struct cnode
*cp
)
157 if (hfsmp
->jnl
!= NULL
&&
158 (cp
->c_fileid
== hfsmp
->hfs_jnlinfoblkid
||
159 cp
->c_fileid
== hfsmp
->hfs_jnlfileid
)) {
167 * Create a regular file.
170 hfs_vnop_create(struct vnop_create_args
*ap
)
175 error
= hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
178 * We speculatively skipped the original lookup of the leaf
179 * for CREATE. Since it exists, go get it as long as they
180 * didn't want an exclusive create.
182 if ((error
== EEXIST
) && !(ap
->a_vap
->va_vaflags
& VA_EXCLUSIVE
)) {
183 struct vnop_lookup_args args
;
185 args
.a_desc
= &vnop_lookup_desc
;
186 args
.a_dvp
= ap
->a_dvp
;
187 args
.a_vpp
= ap
->a_vpp
;
188 args
.a_cnp
= ap
->a_cnp
;
189 args
.a_context
= ap
->a_context
;
190 args
.a_cnp
->cn_nameiop
= LOOKUP
;
191 error
= hfs_vnop_lookup(&args
);
193 * We can also race with remove for this file.
195 if (error
== ENOENT
) {
199 /* Make sure it was file. */
200 if ((error
== 0) && !vnode_isreg(*args
.a_vpp
)) {
201 vnode_put(*args
.a_vpp
);
202 *args
.a_vpp
= NULLVP
;
205 args
.a_cnp
->cn_nameiop
= CREATE
;
211 * Make device special file.
214 hfs_vnop_mknod(struct vnop_mknod_args
*ap
)
216 struct vnode_attr
*vap
= ap
->a_vap
;
217 struct vnode
*dvp
= ap
->a_dvp
;
218 struct vnode
**vpp
= ap
->a_vpp
;
222 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
226 /* Create the vnode */
227 error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, vap
, ap
->a_context
);
232 cp
->c_touch_acctime
= TRUE
;
233 cp
->c_touch_chgtime
= TRUE
;
234 cp
->c_touch_modtime
= TRUE
;
236 if ((vap
->va_rdev
!= VNOVAL
) &&
237 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
))
238 cp
->c_rdev
= vap
->va_rdev
;
245 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
246 * In the (hopefully rare) case where the data fork vnode is not
247 * present, it will use hfs_vget() to create a new vnode for the
250 * NOTE: If successful and a vnode is returned, the caller is responsible
251 * for releasing the returned vnode with vnode_rele().
254 hfs_ref_data_vp(struct cnode
*cp
, struct vnode
**data_vp
, int skiplock
)
258 if (!data_vp
|| !cp
) /* sanity check incoming parameters */
261 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
263 if (!skiplock
) hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
264 struct vnode
*c_vp
= cp
->c_vp
;
266 /* we already have a data vnode */
268 vref
= vnode_ref(*data_vp
);
269 if (!skiplock
) hfs_unlock(cp
);
275 /* no data fork vnode in the cnode, so ask hfs for one. */
277 if (!cp
->c_rsrc_vp
) {
278 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
280 if (!skiplock
) hfs_unlock(cp
);
284 if (0 == hfs_vget(VTOHFS(cp
->c_rsrc_vp
), cp
->c_cnid
, data_vp
, 1, 0) &&
286 vref
= vnode_ref(*data_vp
);
288 if (!skiplock
) hfs_unlock(cp
);
294 /* there was an error getting the vnode */
296 if (!skiplock
) hfs_unlock(cp
);
301 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
302 * allocating it if necessary; returns NULL if there was an allocation error.
303 * function is non-static so that it can be used from the FCNTL handler.
306 hfs_lazy_init_decmpfs_cnode(struct cnode
*cp
)
309 decmpfs_cnode
*dp
= NULL
;
310 MALLOC_ZONE(dp
, decmpfs_cnode
*, sizeof(decmpfs_cnode
), M_DECMPFS_CNODE
, M_WAITOK
);
312 /* error allocating a decmpfs cnode */
315 decmpfs_cnode_init(dp
);
316 if (!OSCompareAndSwapPtr(NULL
, dp
, (void * volatile *)&cp
->c_decmp
)) {
317 /* another thread got here first, so free the decmpfs_cnode we allocated */
318 decmpfs_cnode_destroy(dp
);
319 FREE_ZONE(dp
, sizeof(*dp
), M_DECMPFS_CNODE
);
327 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
328 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
329 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
330 * or else fills it in via the decmpfs_file_is_compressed() function.
333 hfs_file_is_compressed(struct cnode
*cp
, int skiplock
)
337 /* fast check to see if file is compressed. If flag is clear, just answer no */
338 if (!(cp
->c_bsdflags
& UF_COMPRESSED
)) {
342 decmpfs_cnode
*dp
= hfs_lazy_init_decmpfs_cnode(cp
);
344 /* error allocating a decmpfs cnode, treat the file as uncompressed */
348 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
349 uint32_t decmpfs_state
= decmpfs_cnode_get_vnode_state(dp
);
350 switch(decmpfs_state
) {
351 case FILE_IS_COMPRESSED
:
352 case FILE_IS_CONVERTING
: /* treat decompressing files as if they are compressed */
354 case FILE_IS_NOT_COMPRESSED
:
356 /* otherwise the state is not cached yet */
359 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
360 struct vnode
*data_vp
= NULL
;
361 if (0 == hfs_ref_data_vp(cp
, &data_vp
, skiplock
)) {
363 ret
= decmpfs_file_is_compressed(data_vp
, VTOCMP(data_vp
)); // fill in decmpfs_cnode
370 /* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
371 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
372 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
373 * files size is returned in size (required)
374 * if the indicated file is a directory (or something that doesn't have a data fork), then this call
375 * will return an error and the caller should fall back to treating the item as an uncompressed file
378 hfs_uncompressed_size_of_compressed_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, cnid_t fid
, off_t
*size
, int skiplock
)
381 int putaway
= 0; /* flag to remember if we used hfs_vget() */
384 return EINVAL
; /* no place to put the file size */
388 if (!hfsmp
|| !fid
) { /* make sure we have the required parameters */
391 if (0 != hfs_vget(hfsmp
, fid
, &vp
, skiplock
, 0)) { /* vnode is null, use hfs_vget() to get it */
394 putaway
= 1; /* note that hfs_vget() was used to aquire the vnode */
397 /* this double check for compression (hfs_file_is_compressed)
398 * ensures the cached size is present in case decmpfs hasn't
399 * encountered this node yet.
402 if (hfs_file_is_compressed(VTOC(vp
), skiplock
) ) {
403 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
405 if (VTOCMP(vp
) && VTOCMP(vp
)->cmp_type
>= CMP_MAX
) {
406 if (VTOCMP(vp
)->cmp_type
!= DATALESS_CMPFS_TYPE
) {
407 // if we don't recognize this type, just use the real data fork size
408 if (VTOC(vp
)->c_datafork
) {
409 *size
= VTOC(vp
)->c_datafork
->ff_size
;
415 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
424 if (putaway
) { /* did we use hfs_vget() to get this vnode? */
425 vnode_put(vp
); /* if so, release it and set it to null */
432 hfs_hides_rsrc(vfs_context_t ctx
, struct cnode
*cp
, int skiplock
)
434 if (ctx
== decmpfs_ctx
)
436 if (!hfs_file_is_compressed(cp
, skiplock
))
438 return decmpfs_hides_rsrc(ctx
, cp
->c_decmp
);
442 hfs_hides_xattr(vfs_context_t ctx
, struct cnode
*cp
, const char *name
, int skiplock
)
444 if (ctx
== decmpfs_ctx
)
446 if (!hfs_file_is_compressed(cp
, skiplock
))
448 return decmpfs_hides_xattr(ctx
, cp
->c_decmp
, name
);
450 #endif /* HFS_COMPRESSION */
454 // This function gets the doc_tombstone structure for the
455 // current thread. If the thread doesn't have one, the
456 // structure is allocated.
458 static struct doc_tombstone
*
459 get_uthread_doc_tombstone(void)
462 ut
= get_bsdthread_info(current_thread());
464 if (ut
->t_tombstone
== NULL
) {
465 ut
->t_tombstone
= kalloc(sizeof(struct doc_tombstone
));
466 if (ut
->t_tombstone
) {
467 memset(ut
->t_tombstone
, 0, sizeof(struct doc_tombstone
));
471 return ut
->t_tombstone
;
475 // This routine clears out the current tombstone for the
476 // current thread and if necessary passes the doc-id of
477 // the tombstone on to the dst_cnode.
479 // If the doc-id transfers to dst_cnode, we also generate
480 // a doc-id changed fsevent. Unlike all the other fsevents,
481 // doc-id changed events can only be generated here in HFS
482 // where we have the necessary info.
485 clear_tombstone_docid(struct doc_tombstone
*ut
, struct hfsmount
*hfsmp
, struct cnode
*dst_cnode
)
487 uint32_t old_id
= ut
->t_lastop_document_id
;
489 ut
->t_lastop_document_id
= 0;
490 ut
->t_lastop_parent
= NULL
;
491 ut
->t_lastop_parent_vid
= 0;
492 ut
->t_lastop_filename
[0] = '\0';
495 // If the lastop item is still the same and needs to be cleared,
498 if (dst_cnode
&& old_id
&& ut
->t_lastop_item
&& vnode_vid(ut
->t_lastop_item
) == ut
->t_lastop_item_vid
) {
500 // clear the document_id from the file that used to have it.
501 // XXXdbg - we need to lock the other vnode and make sure to
502 // update it on disk.
504 struct cnode
*ocp
= VTOC(ut
->t_lastop_item
);
505 struct FndrExtendedFileInfo
*ofip
= (struct FndrExtendedFileInfo
*)((char *)&ocp
->c_attr
.ca_finderinfo
+ 16);
507 // printf("clearing doc-id from ino %d\n", ocp->c_desc.cd_cnid);
508 ofip
->document_id
= 0;
509 ocp
->c_bsdflags
&= ~UF_TRACKED
;
510 ocp
->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
; // mark it dirty
511 /* cat_update(hfsmp, &ocp->c_desc, &ocp->c_attr, NULL, NULL); */
516 if (dst_cnode
&& old_id
) {
517 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&dst_cnode
->c_attr
.ca_finderinfo
+ 16);
519 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
520 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
521 FSE_ARG_INO
, (ino64_t
)ut
->t_lastop_fileid
, // src inode #
522 FSE_ARG_INO
, (ino64_t
)dst_cnode
->c_fileid
, // dst inode #
523 FSE_ARG_INT32
, (uint32_t)fip
->document_id
,
527 // last, clear these now that we're all done
528 ut
->t_lastop_item
= NULL
;
529 ut
->t_lastop_fileid
= 0;
530 ut
->t_lastop_item_vid
= 0;
535 // This function is used to filter out operations on temp
536 // filenames. We have to filter out operations on certain
537 // temp filenames to work-around questionable application
538 // behavior from apps like Autocad that perform unusual
539 // sequences of file system operations for a "safe save".
541 is_ignorable_temp_name(const char *nameptr
, int len
)
544 len
= strlen(nameptr
);
547 if ( strncmp(nameptr
, "atmp", 4) == 0
548 || (len
> 4 && strncmp(nameptr
+len
-4, ".bak", 4) == 0)
549 || (len
> 4 && strncmp(nameptr
+len
-4, ".tmp", 4) == 0)) {
557 // Decide if we need to save a tombstone or not. Normally we always
558 // save a tombstone - but if there already is one and the name we're
559 // given is an ignorable name, then we will not save a tombstone.
562 should_save_docid_tombstone(struct doc_tombstone
*ut
, struct vnode
*vp
, struct componentname
*cnp
)
564 if (cnp
->cn_nameptr
== NULL
) {
568 if (ut
->t_lastop_document_id
&& ut
->t_lastop_item
== vp
&& is_ignorable_temp_name(cnp
->cn_nameptr
, cnp
->cn_namelen
)) {
577 // This function saves a tombstone for the given vnode and name. The
578 // tombstone represents the parent directory and name where the document
579 // used to live and the document-id of that file. This info is recorded
580 // in the doc_tombstone structure hanging off the uthread (which assumes
581 // that all safe-save operations happen on the same thread).
583 // If later on the same parent/name combo comes back into existence then
584 // we'll preserve the doc-id from this vnode onto the new vnode.
587 save_tombstone(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int for_unlink
)
589 struct cnode
*cp
= VTOC(vp
);
590 struct doc_tombstone
*ut
;
591 ut
= get_uthread_doc_tombstone();
593 if (for_unlink
&& vp
->v_type
== VREG
&& cp
->c_linkcount
> 1) {
595 // a regular file that is being unlinked and that is also
596 // hardlinked should not clear the UF_TRACKED state or
597 // mess with the tombstone because somewhere else in the
598 // file system the file is still alive.
603 ut
->t_lastop_parent
= dvp
;
604 ut
->t_lastop_parent_vid
= vnode_vid(dvp
);
605 ut
->t_lastop_fileid
= cp
->c_fileid
;
607 ut
->t_lastop_item
= NULL
;
608 ut
->t_lastop_item_vid
= 0;
610 ut
->t_lastop_item
= vp
;
611 ut
->t_lastop_item_vid
= vnode_vid(vp
);
614 strlcpy((char *)&ut
->t_lastop_filename
[0], cnp
->cn_nameptr
, sizeof(ut
->t_lastop_filename
));
616 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
617 ut
->t_lastop_document_id
= fip
->document_id
;
620 // clear this so it's never returned again
621 fip
->document_id
= 0;
622 cp
->c_bsdflags
&= ~UF_TRACKED
;
624 if (ut
->t_lastop_document_id
) {
625 (void) cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, NULL
, NULL
);
628 // this event is more of a "pending-delete"
629 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
630 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
631 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // src inode #
632 FSE_ARG_INO
, (ino64_t
)0, // dst inode #
633 FSE_ARG_INT32
, ut
->t_lastop_document_id
, // document id
642 * Open a file/directory.
645 hfs_vnop_open(struct vnop_open_args
*ap
)
647 struct vnode
*vp
= ap
->a_vp
;
651 static int past_bootup
= 0;
652 struct cnode
*cp
= VTOC(vp
);
653 struct hfsmount
*hfsmp
= VTOHFS(vp
);
656 if (ap
->a_mode
& FWRITE
) {
658 if ( hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
659 /* opening a compressed file for write, so convert it to decompressed */
660 struct vnode
*data_vp
= NULL
;
661 error
= hfs_ref_data_vp(cp
, &data_vp
, 1); /* 1 == don't take the cnode lock */
664 error
= decmpfs_decompress_file(data_vp
, VTOCMP(data_vp
), -1, 1, 0);
675 if (hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
676 if (VNODE_IS_RSRC(vp
)) {
677 /* opening the resource fork of a compressed file, so nothing to do */
679 /* opening a compressed file for read, make sure it validates */
680 error
= decmpfs_validate_compressed_file(vp
, VTOCMP(vp
));
689 * Files marked append-only must be opened for appending.
691 if ((cp
->c_bsdflags
& APPEND
) && !vnode_isdir(vp
) &&
692 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
695 if (vnode_isreg(vp
) && !UBCINFOEXISTS(vp
))
696 return (EBUSY
); /* file is in use by the kernel */
698 /* Don't allow journal to be opened externally. */
699 if (hfs_is_journal_file(hfsmp
, cp
))
702 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
703 (hfsmp
->jnl
== NULL
) ||
705 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0) || vnode_isnamedstream(vp
)) {
707 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0)) {
712 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
716 /* If we're going to write to the file, initialize quotas. */
717 if ((ap
->a_mode
& FWRITE
) && (hfsmp
->hfs_flags
& HFS_QUOTAS
))
718 (void)hfs_getinoquota(cp
);
722 * On the first (non-busy) open of a fragmented
723 * file attempt to de-frag it (if its less than 20MB).
727 fp
->ff_extents
[7].blockCount
!= 0 &&
728 fp
->ff_size
<= (20 * 1024 * 1024)) {
732 * Wait until system bootup is done (3 min).
733 * And don't relocate a file that's been modified
734 * within the past minute -- this can lead to
740 if (tv
.tv_sec
> (60*3)) {
746 if ((now
.tv_sec
- cp
->c_mtime
) > 60) {
750 if (past_bootup
&& no_mods
) {
751 (void) hfs_relocate(vp
, hfsmp
->nextAllocation
+ 4096,
752 vfs_context_ucred(ap
->a_context
),
753 vfs_context_proc(ap
->a_context
));
764 * Close a file/directory.
768 struct vnop_close_args
/* {
771 vfs_context_t a_context;
774 register struct vnode
*vp
= ap
->a_vp
;
775 register struct cnode
*cp
;
776 struct proc
*p
= vfs_context_proc(ap
->a_context
);
777 struct hfsmount
*hfsmp
;
779 int tooktrunclock
= 0;
782 if ( hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0)
788 * If the rsrc fork is a named stream, it can cause the data fork to
789 * stay around, preventing de-allocation of these blocks.
790 * Do checks for truncation on close. Purge extra extents if they exist.
791 * Make sure the vp is not a directory, and that it has a resource fork,
792 * and that resource fork is also a named stream.
795 if ((vp
->v_type
== VREG
) && (cp
->c_rsrc_vp
)
796 && (vnode_isnamedstream(cp
->c_rsrc_vp
))) {
799 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
801 * If there are extra blocks and there are only 2 refs on
802 * this vp (ourselves + rsrc fork holding ref on us), go ahead
803 * and try to truncate.
805 if ((blks
< VTOF(vp
)->ff_blocks
) && (!vnode_isinuse(vp
, 2))) {
806 // release cnode lock; must acquire truncate lock BEFORE cnode lock
809 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
812 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0) {
813 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
814 // bail out if we can't re-acquire cnode lock
817 // now re-test to make sure it's still valid
819 knownrefs
= 1 + vnode_isnamedstream(cp
->c_rsrc_vp
);
820 if (!vnode_isinuse(vp
, knownrefs
)){
821 // now we can truncate the file, if necessary
822 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
823 if (blks
< VTOF(vp
)->ff_blocks
){
824 (void) hfs_truncate(vp
, VTOF(vp
)->ff_size
, IO_NDELAY
, 0, 0, ap
->a_context
);
832 // if we froze the fs and we're exiting, then "thaw" the fs
833 if (hfsmp
->hfs_freezing_proc
== p
&& proc_exiting(p
)) {
834 hfsmp
->hfs_freezing_proc
= NULL
;
835 hfs_unlock_global (hfsmp
);
836 lck_rw_unlock_exclusive(&hfsmp
->hfs_insync
);
839 busy
= vnode_isinuse(vp
, 1);
842 hfs_touchtimes(VTOHFS(vp
), cp
);
844 if (vnode_isdir(vp
)) {
845 hfs_reldirhints(cp
, busy
);
846 } else if (vnode_issystem(vp
) && !busy
) {
851 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
855 if (ap
->a_fflag
& FWASWRITTEN
) {
856 hfs_sync_ejectable(hfsmp
);
863 * Get basic attributes.
866 hfs_vnop_getattr(struct vnop_getattr_args
*ap
)
868 #define VNODE_ATTR_TIMES \
869 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
870 #define VNODE_ATTR_AUTH \
871 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
872 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
874 struct vnode
*vp
= ap
->a_vp
;
875 struct vnode_attr
*vap
= ap
->a_vap
;
876 struct vnode
*rvp
= NULLVP
;
877 struct hfsmount
*hfsmp
;
885 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
888 off_t uncompressed_size
= -1;
889 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
)) {
890 /* we only care about whether the file is compressed if asked for the uncompressed size */
891 if (VNODE_IS_RSRC(vp
)) {
892 /* if it's a resource fork, decmpfs may want us to hide the size */
893 hide_size
= hfs_hides_rsrc(ap
->a_context
, cp
, 0);
895 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
896 compressed
= hfs_file_is_compressed(cp
, 0);
898 if ((VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_size
))) {
899 // if it's compressed
900 if (compressed
|| (!VNODE_IS_RSRC(vp
) && cp
->c_decmp
&& cp
->c_decmp
->cmp_type
>= CMP_MAX
)) {
901 if (0 != hfs_uncompressed_size_of_compressed_file(NULL
, vp
, 0, &uncompressed_size
, 0)) {
902 /* failed to get the uncompressed size, we'll check for this later */
903 uncompressed_size
= -1;
905 // fake that it's compressed
914 * Shortcut for vnode_authorize path. Each of the attributes
915 * in this set is updated atomically so we don't need to take
916 * the cnode lock to access them.
918 if ((vap
->va_active
& ~VNODE_ATTR_AUTH
) == 0) {
919 /* Make sure file still exists. */
920 if (cp
->c_flag
& C_NOEXISTS
)
923 vap
->va_uid
= cp
->c_uid
;
924 vap
->va_gid
= cp
->c_gid
;
925 vap
->va_mode
= cp
->c_mode
;
926 vap
->va_flags
= cp
->c_bsdflags
;
927 vap
->va_supported
|= VNODE_ATTR_AUTH
& ~VNODE_ATTR_va_acl
;
929 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
930 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
931 VATTR_SET_SUPPORTED(vap
, va_acl
);
938 v_type
= vnode_vtype(vp
);
940 * If time attributes are requested and we have cnode times
941 * that require updating, then acquire an exclusive lock on
942 * the cnode before updating the times. Otherwise we can
943 * just acquire a shared lock.
945 if ((vap
->va_active
& VNODE_ATTR_TIMES
) &&
946 (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
947 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
949 hfs_touchtimes(hfsmp
, cp
);
952 if ((error
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
)))
956 if (v_type
== VDIR
) {
957 data_size
= (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
;
959 if (VATTR_IS_ACTIVE(vap
, va_nlink
)) {
963 * For directories, the va_nlink is esentially a count
964 * of the ".." references to a directory plus the "."
965 * reference and the directory itself. So for HFS+ this
966 * becomes the sub-directory count plus two.
968 * In the absence of a sub-directory count we use the
969 * directory's item count. This will be too high in
970 * most cases since it also includes files.
972 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
973 (cp
->c_attr
.ca_recflags
& kHFSHasFolderCountMask
))
974 nlink
= cp
->c_attr
.ca_dircount
; /* implied ".." entries */
976 nlink
= cp
->c_entries
;
978 /* Account for ourself and our "." entry */
980 /* Hide our private directories. */
981 if (cp
->c_cnid
== kHFSRootFolderID
) {
982 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0) {
985 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0) {
989 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)nlink
);
991 if (VATTR_IS_ACTIVE(vap
, va_nchildren
)) {
994 entries
= cp
->c_entries
;
995 /* Hide our private files and directories. */
996 if (cp
->c_cnid
== kHFSRootFolderID
) {
997 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
999 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
1001 if (hfsmp
->jnl
|| ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
1002 entries
-= 2; /* hide the journal files */
1004 VATTR_RETURN(vap
, va_nchildren
, entries
);
1007 * The va_dirlinkcount is the count of real directory hard links.
1008 * (i.e. its not the sum of the implied "." and ".." references)
1010 if (VATTR_IS_ACTIVE(vap
, va_dirlinkcount
)) {
1011 VATTR_RETURN(vap
, va_dirlinkcount
, (uint32_t)cp
->c_linkcount
);
1013 } else /* !VDIR */ {
1014 data_size
= VCTOF(vp
, cp
)->ff_size
;
1016 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)cp
->c_linkcount
);
1017 if (VATTR_IS_ACTIVE(vap
, va_data_alloc
)) {
1022 VATTR_RETURN(vap
, va_data_alloc
, 0);
1023 } else if (compressed
) {
1024 /* for compressed files, we report all allocated blocks as belonging to the data fork */
1025 blocks
= cp
->c_blocks
;
1026 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
1031 blocks
= VCTOF(vp
, cp
)->ff_blocks
;
1032 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
1037 /* conditional because 64-bit arithmetic can be expensive */
1038 if (VATTR_IS_ACTIVE(vap
, va_total_size
)) {
1039 if (v_type
== VDIR
) {
1040 VATTR_RETURN(vap
, va_total_size
, (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
);
1042 u_int64_t total_size
= ~0ULL;
1046 /* we're hiding the size of this file, so just return 0 */
1048 } else if (compressed
) {
1049 if (uncompressed_size
== -1) {
1051 * We failed to get the uncompressed size above,
1052 * so we'll fall back to the standard path below
1053 * since total_size is still -1
1056 /* use the uncompressed size we fetched above */
1057 total_size
= uncompressed_size
;
1061 if (total_size
== ~0ULL) {
1062 if (cp
->c_datafork
) {
1063 total_size
= cp
->c_datafork
->ff_size
;
1066 if (cp
->c_blocks
- VTOF(vp
)->ff_blocks
) {
1067 /* We deal with rsrc fork vnode iocount at the end of the function */
1068 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
1071 * Note that we call hfs_vgetrsrc with error_on_unlinked
1072 * set to FALSE. This is because we may be invoked via
1073 * fstat() on an open-unlinked file descriptor and we must
1074 * continue to support access to the rsrc fork until it disappears.
1075 * The code at the end of this function will be
1076 * responsible for releasing the iocount generated by
1077 * hfs_vgetrsrc. This is because we can't drop the iocount
1078 * without unlocking the cnode first.
1084 if (rcp
&& rcp
->c_rsrcfork
) {
1085 total_size
+= rcp
->c_rsrcfork
->ff_size
;
1090 VATTR_RETURN(vap
, va_total_size
, total_size
);
1093 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
)) {
1094 if (v_type
== VDIR
) {
1095 VATTR_RETURN(vap
, va_total_alloc
, 0);
1097 VATTR_RETURN(vap
, va_total_alloc
, (u_int64_t
)cp
->c_blocks
* (u_int64_t
)hfsmp
->blockSize
);
1102 * If the VFS wants extended security data, and we know that we
1103 * don't have any (because it never told us it was setting any)
1104 * then we can return the supported bit and no data. If we do
1105 * have extended security, we can just leave the bit alone and
1106 * the VFS will use the fallback path to fetch it.
1108 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
1109 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
1110 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
1111 VATTR_SET_SUPPORTED(vap
, va_acl
);
1114 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1115 /* Access times are lazily updated, get current time if needed */
1116 if (cp
->c_touch_acctime
) {
1120 vap
->va_access_time
.tv_sec
= tv
.tv_sec
;
1122 vap
->va_access_time
.tv_sec
= cp
->c_atime
;
1124 vap
->va_access_time
.tv_nsec
= 0;
1125 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1127 vap
->va_create_time
.tv_sec
= cp
->c_itime
;
1128 vap
->va_create_time
.tv_nsec
= 0;
1129 vap
->va_modify_time
.tv_sec
= cp
->c_mtime
;
1130 vap
->va_modify_time
.tv_nsec
= 0;
1131 vap
->va_change_time
.tv_sec
= cp
->c_ctime
;
1132 vap
->va_change_time
.tv_nsec
= 0;
1133 vap
->va_backup_time
.tv_sec
= cp
->c_btime
;
1134 vap
->va_backup_time
.tv_nsec
= 0;
1136 /* See if we need to emit the date added field to the user */
1137 if (VATTR_IS_ACTIVE(vap
, va_addedtime
)) {
1138 u_int32_t dateadded
= hfs_get_dateadded (cp
);
1140 vap
->va_addedtime
.tv_sec
= dateadded
;
1141 vap
->va_addedtime
.tv_nsec
= 0;
1142 VATTR_SET_SUPPORTED (vap
, va_addedtime
);
1146 /* XXX is this really a good 'optimal I/O size'? */
1147 vap
->va_iosize
= hfsmp
->hfs_logBlockSize
;
1148 vap
->va_uid
= cp
->c_uid
;
1149 vap
->va_gid
= cp
->c_gid
;
1150 vap
->va_mode
= cp
->c_mode
;
1151 vap
->va_flags
= cp
->c_bsdflags
;
1154 * Exporting file IDs from HFS Plus:
1156 * For "normal" files the c_fileid is the same value as the
1157 * c_cnid. But for hard link files, they are different - the
1158 * c_cnid belongs to the active directory entry (ie the link)
1159 * and the c_fileid is for the actual inode (ie the data file).
1161 * The stat call (getattr) uses va_fileid and the Carbon APIs,
1162 * which are hardlink-ignorant, will ask for va_linkid.
1164 vap
->va_fileid
= (u_int64_t
)cp
->c_fileid
;
1166 * We need to use the origin cache for both hardlinked files
1167 * and directories. Hardlinked directories have multiple cnids
1168 * and parents (one per link). Hardlinked files also have their
1169 * own parents and link IDs separate from the indirect inode number.
1170 * If we don't use the cache, we could end up vending the wrong ID
1171 * because the cnode will only reflect the link that was looked up most recently.
1173 if (cp
->c_flag
& C_HARDLINK
) {
1174 vap
->va_linkid
= (u_int64_t
)hfs_currentcnid(cp
);
1175 vap
->va_parentid
= (u_int64_t
)hfs_currentparent(cp
);
1177 vap
->va_linkid
= (u_int64_t
)cp
->c_cnid
;
1178 vap
->va_parentid
= (u_int64_t
)cp
->c_parentcnid
;
1180 vap
->va_fsid
= hfsmp
->hfs_raw_dev
;
1181 vap
->va_filerev
= 0;
1182 vap
->va_encoding
= cp
->c_encoding
;
1183 vap
->va_rdev
= (v_type
== VBLK
|| v_type
== VCHR
) ? cp
->c_rdev
: 0;
1185 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1187 vap
->va_data_size
= 0;
1188 else if (compressed
) {
1189 if (uncompressed_size
== -1) {
1190 /* failed to get the uncompressed size above, so just return data_size */
1191 vap
->va_data_size
= data_size
;
1193 /* use the uncompressed size we fetched above */
1194 vap
->va_data_size
= uncompressed_size
;
1197 vap
->va_data_size
= data_size
;
1198 // vap->va_supported |= VNODE_ATTR_va_data_size;
1199 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1202 vap
->va_data_size
= data_size
;
1203 vap
->va_supported
|= VNODE_ATTR_va_data_size
;
1206 if (VATTR_IS_ACTIVE(vap
, va_gen
)) {
1207 if (UBCINFOEXISTS(vp
) && (vp
->v_ubcinfo
->ui_flags
& UI_ISMAPPED
)) {
1208 /* While file is mmapped the generation count is invalid.
1209 * However, bump the value so that the write-gen counter
1210 * will be different once the file is unmapped (since,
1211 * when unmapped the pageouts may not yet have happened)
1213 if (vp
->v_ubcinfo
->ui_flags
& UI_MAPPEDWRITE
) {
1214 hfs_incr_gencount (cp
);
1218 vap
->va_gen
= hfs_get_gencount(cp
);
1221 VATTR_SET_SUPPORTED(vap
, va_gen
);
1223 if (VATTR_IS_ACTIVE(vap
, va_document_id
)) {
1224 vap
->va_document_id
= hfs_get_document_id(cp
);
1225 VATTR_SET_SUPPORTED(vap
, va_document_id
);
1228 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
1229 vap
->va_supported
|= VNODE_ATTR_va_create_time
| VNODE_ATTR_va_modify_time
|
1230 VNODE_ATTR_va_change_time
| VNODE_ATTR_va_backup_time
|
1231 VNODE_ATTR_va_iosize
| VNODE_ATTR_va_uid
|
1232 VNODE_ATTR_va_gid
| VNODE_ATTR_va_mode
|
1233 VNODE_ATTR_va_flags
|VNODE_ATTR_va_fileid
|
1234 VNODE_ATTR_va_linkid
| VNODE_ATTR_va_parentid
|
1235 VNODE_ATTR_va_fsid
| VNODE_ATTR_va_filerev
|
1236 VNODE_ATTR_va_encoding
| VNODE_ATTR_va_rdev
;
1238 /* If this is the root, let VFS to find out the mount name, which
1239 * may be different from the real name. Otherwise, we need to take care
1240 * for hardlinked files, which need to be looked up, if necessary
1242 if (VATTR_IS_ACTIVE(vap
, va_name
) && (cp
->c_cnid
!= kHFSRootFolderID
)) {
1243 struct cat_desc linkdesc
;
1245 int uselinkdesc
= 0;
1246 cnid_t nextlinkid
= 0;
1247 cnid_t prevlinkid
= 0;
1249 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
1250 * here because the info. for the link ID requested by getattrlist may be
1251 * different than what's currently in the cnode. This is because the cnode
1252 * will be filled in with the information for the most recent link ID that went
1253 * through namei/lookup(). If there are competing lookups for hardlinks that point
1254 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
1255 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
1258 if ((cp
->c_flag
& C_HARDLINK
) &&
1259 ((cp
->c_desc
.cd_namelen
== 0) || (vap
->va_linkid
!= cp
->c_cnid
))) {
1261 * If we have no name and our link ID is the raw inode number, then we may
1262 * have an open-unlinked file. Go to the next link in this case.
1264 if ((cp
->c_desc
.cd_namelen
== 0) && (vap
->va_linkid
== cp
->c_fileid
)) {
1265 if ((error
= hfs_lookup_siblinglinks(hfsmp
, vap
->va_linkid
, &prevlinkid
, &nextlinkid
))){
1270 /* just use link obtained from vap above */
1271 nextlinkid
= vap
->va_linkid
;
1274 /* We need to probe the catalog for the descriptor corresponding to the link ID
1275 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
1276 * for the cnode here, so we can't just update the descriptor. Instead,
1277 * we should just store the descriptor's value locally and then use it to pass
1278 * out the name value as needed below.
1281 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1282 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
1283 hfs_systemfile_unlock(hfsmp
, lockflags
);
1290 /* By this point, we've either patched up the name above and the c_desc
1291 * points to the correct data, or it already did, in which case we just proceed
1292 * by copying the name into the vap. Note that we will never set va_name to
1293 * supported if nextlinkid is never initialized. This could happen in the degenerate
1294 * case above involving the raw inode number, where it has no nextlinkid. In this case
1295 * we will simply not mark the name bit as supported.
1298 strlcpy(vap
->va_name
, (const char*) linkdesc
.cd_nameptr
, MAXPATHLEN
);
1299 VATTR_SET_SUPPORTED(vap
, va_name
);
1300 cat_releasedesc(&linkdesc
);
1302 else if (cp
->c_desc
.cd_namelen
) {
1303 strlcpy(vap
->va_name
, (const char*) cp
->c_desc
.cd_nameptr
, MAXPATHLEN
);
1304 VATTR_SET_SUPPORTED(vap
, va_name
);
1311 * We need to vnode_put the rsrc fork vnode only *after* we've released
1312 * the cnode lock, since vnode_put can trigger an inactive call, which
1313 * will go back into HFS and try to acquire a cnode lock.
1323 hfs_vnop_setattr(ap
)
1324 struct vnop_setattr_args
/* {
1326 struct vnode_attr *a_vap;
1327 vfs_context_t a_context;
1330 struct vnode_attr
*vap
= ap
->a_vap
;
1331 struct vnode
*vp
= ap
->a_vp
;
1332 struct cnode
*cp
= NULL
;
1333 struct hfsmount
*hfsmp
;
1334 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
1335 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1341 orig_ctime
= VTOC(vp
)->c_ctime
;
1344 int decmpfs_reset_state
= 0;
1346 we call decmpfs_update_attributes even if the file is not compressed
1347 because we want to update the incoming flags if the xattrs are invalid
1349 error
= decmpfs_update_attributes(vp
, vap
);
1354 // if this is not a size-changing setattr and it is not just
1355 // an atime update, then check for a snapshot.
1357 if (!VATTR_IS_ACTIVE(vap
, va_data_size
) && !(vap
->va_active
== VNODE_ATTR_va_access_time
)) {
1358 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_MOD
, NSPACE_REARM_NO_ARG
);
1362 if ((error
= cp_handle_vnop(vp
, CP_WRITE_ACCESS
, 0)) != 0) {
1365 #endif /* CONFIG_PROTECT */
1369 /* Don't allow modification of the journal. */
1370 if (hfs_is_journal_file(hfsmp
, VTOC(vp
))) {
1375 // Check if we'll need a document_id and if so, get it before we lock the
1376 // the cnode to avoid any possible deadlock with the root vnode which has
1377 // to get locked to get the document id
1379 u_int32_t document_id
=0;
1380 if (VATTR_IS_ACTIVE(vap
, va_flags
) && (vap
->va_flags
& UF_TRACKED
) && !(VTOC(vp
)->c_bsdflags
& UF_TRACKED
)) {
1381 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&(VTOC(vp
)->c_attr
.ca_finderinfo
) + 16);
1383 // If the document_id is not set, get a new one. It will be set
1384 // on the file down below once we hold the cnode lock.
1386 if (fip
->document_id
== 0) {
1387 if (hfs_generate_document_id(hfsmp
, &document_id
) != 0) {
1395 * File size change request.
1396 * We are guaranteed that this is not a directory, and that
1397 * the filesystem object is writeable.
1399 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
1401 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1402 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && !vnode_islnk(vp
)) {
1404 /* keep the compressed state locked until we're done truncating the file */
1405 decmpfs_cnode
*dp
= VTOCMP(vp
);
1408 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1409 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1410 * on this file while it's truncating
1412 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1414 /* failed to allocate a decmpfs_cnode */
1415 return ENOMEM
; /* what should this be? */
1419 check_for_tracked_file(vp
, orig_ctime
, vap
->va_data_size
== 0 ? NAMESPACE_HANDLER_TRUNCATE_OP
|NAMESPACE_HANDLER_DELETE_OP
: NAMESPACE_HANDLER_TRUNCATE_OP
, NULL
);
1421 decmpfs_lock_compressed_data(dp
, 1);
1422 if (hfs_file_is_compressed(VTOC(vp
), 1)) {
1423 error
= decmpfs_decompress_file(vp
, dp
, -1/*vap->va_data_size*/, 0, 1);
1425 decmpfs_unlock_compressed_data(dp
, 1);
1431 /* Take truncate lock before taking cnode lock. */
1432 hfs_lock_truncate(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1434 /* Perform the ubc_setsize before taking the cnode lock. */
1435 ubc_setsize(vp
, vap
->va_data_size
);
1437 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1438 hfs_unlock_truncate(VTOC(vp
), HFS_LOCK_DEFAULT
);
1440 decmpfs_unlock_compressed_data(dp
, 1);
1446 error
= hfs_truncate(vp
, vap
->va_data_size
, vap
->va_vaflags
& 0xffff, 1, 0, ap
->a_context
);
1448 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1450 decmpfs_unlock_compressed_data(dp
, 1);
1456 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
1462 * If it is just an access time update request by itself
1463 * we know the request is from kernel level code, and we
1464 * can delay it without being as worried about consistency.
1465 * This change speeds up mmaps, in the rare case that they
1466 * get caught behind a sync.
1469 if (vap
->va_active
== VNODE_ATTR_va_access_time
) {
1470 cp
->c_touch_acctime
=TRUE
;
1477 * Owner/group change request.
1478 * We are guaranteed that the new owner/group is valid and legal.
1480 VATTR_SET_SUPPORTED(vap
, va_uid
);
1481 VATTR_SET_SUPPORTED(vap
, va_gid
);
1482 nuid
= VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uid_t
)VNOVAL
;
1483 ngid
= VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (gid_t
)VNOVAL
;
1484 if (((nuid
!= (uid_t
)VNOVAL
) || (ngid
!= (gid_t
)VNOVAL
)) &&
1485 ((error
= hfs_chown(vp
, nuid
, ngid
, cred
, p
)) != 0))
1489 * Mode change request.
1490 * We are guaranteed that the mode value is valid and that in
1491 * conjunction with the owner and group, this change is legal.
1493 VATTR_SET_SUPPORTED(vap
, va_mode
);
1494 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
1495 ((error
= hfs_chmod(vp
, (int)vap
->va_mode
, cred
, p
)) != 0))
1499 * File flags change.
1500 * We are guaranteed that only flags allowed to change given the
1501 * current securelevel are being changed.
1503 VATTR_SET_SUPPORTED(vap
, va_flags
);
1504 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1508 if ((cp
->c_bsdflags
^ vap
->va_flags
) & UF_COMPRESSED
) {
1510 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1511 * but we don't want to actually do the update until we've released the cnode lock down below
1512 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1513 * turn off the flag and look at the "raw" file for debugging purposes
1515 decmpfs_reset_state
= 1;
1518 if ((vap
->va_flags
& UF_TRACKED
) && !(cp
->c_bsdflags
& UF_TRACKED
)) {
1519 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
1522 // we're marking this item UF_TRACKED. if the document_id is
1523 // not set, get a new one and put it on the file.
1525 if (fip
->document_id
== 0) {
1526 if (document_id
!= 0) {
1527 // printf("SETATTR: assigning doc-id %d to %s (ino %d)\n", document_id, vp->v_name, cp->c_desc.cd_cnid);
1528 fip
->document_id
= (uint32_t)document_id
;
1530 add_fsevent(FSE_DOCID_CHANGED
, ap
->a_context
,
1531 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
1532 FSE_ARG_INO
, (ino64_t
)0, // src inode #
1533 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
1534 FSE_ARG_INT32
, document_id
,
1538 // printf("hfs: could not acquire a new document_id for %s (ino %d)\n", vp->v_name, cp->c_desc.cd_cnid);
1542 } else if (!(vap
->va_flags
& UF_TRACKED
) && (cp
->c_bsdflags
& UF_TRACKED
)) {
1544 // UF_TRACKED is being cleared so clear the document_id
1546 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
1547 if (fip
->document_id
) {
1548 // printf("SETATTR: clearing doc-id %d from %s (ino %d)\n", fip->document_id, vp->v_name, cp->c_desc.cd_cnid);
1550 add_fsevent(FSE_DOCID_CHANGED
, ap
->a_context
,
1551 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
1552 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // src inode #
1553 FSE_ARG_INO
, (ino64_t
)0, // dst inode #
1554 FSE_ARG_INT32
, fip
->document_id
, // document id
1557 fip
->document_id
= 0;
1558 cp
->c_bsdflags
&= ~UF_TRACKED
;
1562 cp
->c_bsdflags
= vap
->va_flags
;
1563 cp
->c_touch_chgtime
= TRUE
;
1567 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1569 * The fdFlags for files and frFlags for folders are both 8 bytes
1570 * into the userInfo (the first 16 bytes of the Finder Info). They
1571 * are both 16-bit fields.
1573 fdFlags
= (u_int16_t
*) &cp
->c_finderinfo
[8];
1574 if (vap
->va_flags
& UF_HIDDEN
)
1575 *fdFlags
|= OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1577 *fdFlags
&= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1581 * Timestamp updates.
1583 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1584 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1585 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1586 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1587 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1588 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
1589 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1590 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1591 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
1592 if (VATTR_IS_ACTIVE(vap
, va_create_time
))
1593 cp
->c_itime
= vap
->va_create_time
.tv_sec
;
1594 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1595 cp
->c_atime
= vap
->va_access_time
.tv_sec
;
1596 cp
->c_touch_acctime
= FALSE
;
1598 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1599 cp
->c_mtime
= vap
->va_modify_time
.tv_sec
;
1600 cp
->c_touch_modtime
= FALSE
;
1601 cp
->c_touch_chgtime
= TRUE
;
1604 * The utimes system call can reset the modification
1605 * time but it doesn't know about HFS create times.
1606 * So we need to ensure that the creation time is
1607 * always at least as old as the modification time.
1609 if ((VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) &&
1610 (cp
->c_cnid
!= kHFSRootFolderID
) &&
1611 (cp
->c_mtime
< cp
->c_itime
)) {
1612 cp
->c_itime
= cp
->c_mtime
;
1615 if (VATTR_IS_ACTIVE(vap
, va_backup_time
))
1616 cp
->c_btime
= vap
->va_backup_time
.tv_sec
;
1617 cp
->c_flag
|= C_MODIFIED
;
1621 * Set name encoding.
1623 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1624 if (VATTR_IS_ACTIVE(vap
, va_encoding
)) {
1625 cp
->c_encoding
= vap
->va_encoding
;
1626 hfs_setencodingbits(hfsmp
, cp
->c_encoding
);
1629 if ((error
= hfs_update(vp
, TRUE
)) != 0)
1633 /* Purge origin cache for cnode, since caller now has correct link ID for it
1634 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1636 if ((cp
->c_flag
& C_HARDLINK
) && (vp
->v_type
!= VDIR
)){
1637 hfs_relorigin(cp
, 0);
1642 if (decmpfs_reset_state
) {
1644 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1645 * but don't do it while holding the hfs cnode lock
1647 decmpfs_cnode
*dp
= VTOCMP(vp
);
1650 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1651 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1652 * on this file if it's locked
1654 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1656 /* failed to allocate a decmpfs_cnode */
1657 return ENOMEM
; /* what should this be? */
1660 decmpfs_cnode_set_vnode_state(dp
, FILE_TYPE_UNKNOWN
, 0);
1669 * Change the mode on a file.
1670 * cnode must be locked before calling.
1673 hfs_chmod(struct vnode
*vp
, int mode
, __unused kauth_cred_t cred
, __unused
struct proc
*p
)
1675 register struct cnode
*cp
= VTOC(vp
);
1677 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1680 // Don't allow modification of the journal or journal_info_block
1681 if (hfs_is_journal_file(VTOHFS(vp
), cp
)) {
1685 #if OVERRIDE_UNKNOWN_PERMISSIONS
1686 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
) {
1690 cp
->c_mode
&= ~ALLPERMS
;
1691 cp
->c_mode
|= (mode
& ALLPERMS
);
1692 cp
->c_touch_chgtime
= TRUE
;
1698 hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
)
1700 struct cnode
*cp
= VTOC(vp
);
1705 * Disallow write attempts on read-only file systems;
1706 * unless the file is a socket, fifo, or a block or
1707 * character device resident on the file system.
1709 switch (vnode_vtype(vp
)) {
1713 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
)
1720 /* If immutable bit set, nobody gets to write it. */
1721 if (considerFlags
&& (cp
->c_bsdflags
& IMMUTABLE
))
1724 /* Otherwise, user id 0 always gets access. */
1725 if (!suser(cred
, NULL
))
1728 /* Otherwise, check the owner. */
1729 if ((retval
= hfs_owner_rights(VTOHFS(vp
), cp
->c_uid
, cred
, p
, false)) == 0)
1730 return ((cp
->c_mode
& S_IWUSR
) == S_IWUSR
? 0 : EACCES
);
1732 /* Otherwise, check the groups. */
1733 if (kauth_cred_ismember_gid(cred
, cp
->c_gid
, &is_member
) == 0 && is_member
) {
1734 return ((cp
->c_mode
& S_IWGRP
) == S_IWGRP
? 0 : EACCES
);
1737 /* Otherwise, check everyone else. */
1738 return ((cp
->c_mode
& S_IWOTH
) == S_IWOTH
? 0 : EACCES
);
1743 * Perform chown operation on cnode cp;
1744 * code must be locked prior to call.
1748 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, __unused kauth_cred_t cred
,
1749 __unused
struct proc
*p
)
1751 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
1752 __unused
struct proc
*p
)
1755 register struct cnode
*cp
= VTOC(vp
);
1764 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1767 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
)
1770 if (uid
== (uid_t
)VNOVAL
)
1772 if (gid
== (gid_t
)VNOVAL
)
1775 #if 0 /* we are guaranteed that this is already the case */
1777 * If we don't own the file, are trying to change the owner
1778 * of the file, or are not a member of the target group,
1779 * the caller must be superuser or the call fails.
1781 if ((kauth_cred_getuid(cred
) != cp
->c_uid
|| uid
!= cp
->c_uid
||
1782 (gid
!= cp
->c_gid
&&
1783 (kauth_cred_ismember_gid(cred
, gid
, &is_member
) || !is_member
))) &&
1784 (error
= suser(cred
, 0)))
1791 if ((error
= hfs_getinoquota(cp
)))
1794 dqrele(cp
->c_dquot
[USRQUOTA
]);
1795 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1798 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1799 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1803 * Eventually need to account for (fake) a block per directory
1804 * if (vnode_isdir(vp))
1805 * change = VTOHFS(vp)->blockSize;
1809 change
= (int64_t)(cp
->c_blocks
) * (int64_t)VTOVCB(vp
)->blockSize
;
1810 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
);
1811 (void) hfs_chkiq(cp
, -1, cred
, CHOWN
);
1812 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1813 dqrele(cp
->c_dquot
[i
]);
1814 cp
->c_dquot
[i
] = NODQUOT
;
1820 if ((error
= hfs_getinoquota(cp
)) == 0) {
1822 dqrele(cp
->c_dquot
[USRQUOTA
]);
1823 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1826 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1827 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1829 if ((error
= hfs_chkdq(cp
, change
, cred
, CHOWN
)) == 0) {
1830 if ((error
= hfs_chkiq(cp
, 1, cred
, CHOWN
)) == 0)
1833 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
|FORCE
);
1835 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1836 dqrele(cp
->c_dquot
[i
]);
1837 cp
->c_dquot
[i
] = NODQUOT
;
1842 if (hfs_getinoquota(cp
) == 0) {
1844 dqrele(cp
->c_dquot
[USRQUOTA
]);
1845 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1848 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1849 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1851 (void) hfs_chkdq(cp
, change
, cred
, FORCE
|CHOWN
);
1852 (void) hfs_chkiq(cp
, 1, cred
, FORCE
|CHOWN
);
1853 (void) hfs_getinoquota(cp
);
1857 if (hfs_getinoquota(cp
))
1858 panic("hfs_chown: lost quota");
1863 According to the SUSv3 Standard, chown() shall mark
1864 for update the st_ctime field of the file.
1865 (No exceptions mentioned)
1867 cp
->c_touch_chgtime
= TRUE
;
1873 * hfs_vnop_exchange:
1876 * 'from' vnode/cnode
1882 * hfs_vnop_exchange is used to service the exchangedata(2) system call.
1883 * Per the requirements of that system call, this function "swaps" some
1884 * of the information that lives in one catalog record for some that
1885 * lives in another. Note that not everything is swapped; in particular,
1886 * the extent information stored in each cnode is kept local to that
1887 * cnode. This allows existing file descriptor references to continue
1888 * to operate on the same content, regardless of the location in the
1889 * namespace that the file may have moved to. See inline comments
1890 * in the function for more information.
1893 hfs_vnop_exchange(ap
)
1894 struct vnop_exchange_args
/* {
1895 struct vnode *a_fvp;
1896 struct vnode *a_tvp;
1898 vfs_context_t a_context;
1901 struct vnode
*from_vp
= ap
->a_fvp
;
1902 struct vnode
*to_vp
= ap
->a_tvp
;
1903 struct cnode
*from_cp
;
1904 struct cnode
*to_cp
;
1905 struct hfsmount
*hfsmp
;
1906 struct cat_desc tempdesc
;
1907 struct cat_attr tempattr
;
1908 const unsigned char *from_nameptr
;
1909 const unsigned char *to_nameptr
;
1910 char from_iname
[32];
1912 uint32_t to_flag_special
;
1913 uint32_t from_flag_special
;
1917 int error
= 0, started_tr
= 0, got_cookie
= 0;
1918 cat_cookie_t cookie
;
1919 time_t orig_from_ctime
, orig_to_ctime
;
1922 * VFS does the following checks:
1923 * 1. Validate that both are files.
1924 * 2. Validate that both are on the same mount.
1925 * 3. Validate that they're not the same vnode.
1928 orig_from_ctime
= VTOC(from_vp
)->c_ctime
;
1929 orig_to_ctime
= VTOC(to_vp
)->c_ctime
;
1934 * Do not allow exchangedata/F_MOVEDATAEXTENTS on data-protected filesystems
1935 * because the EAs will not be swapped. As a result, the persistent keys would not
1936 * match and the files will be garbage.
1938 if (cp_fs_protected (vnode_mount(from_vp
))) {
1944 if ( hfs_file_is_compressed(VTOC(from_vp
), 0) ) {
1945 if ( 0 != ( error
= decmpfs_decompress_file(from_vp
, VTOCMP(from_vp
), -1, 0, 1) ) ) {
1950 if ( hfs_file_is_compressed(VTOC(to_vp
), 0) ) {
1951 if ( 0 != ( error
= decmpfs_decompress_file(to_vp
, VTOCMP(to_vp
), -1, 0, 1) ) ) {
1955 #endif // HFS_COMPRESSION
1958 * Normally, we want to notify the user handlers about the event,
1959 * except if it's a handler driving the event.
1961 if ((ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) == 0) {
1962 check_for_tracked_file(from_vp
, orig_from_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1963 check_for_tracked_file(to_vp
, orig_to_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1966 * We're doing a data-swap.
1967 * Take the truncate lock/cnode lock, then verify there are no mmap references.
1968 * Issue a hfs_filedone to flush out all of the remaining state for this file.
1969 * Allow the rest of the codeflow to re-acquire the cnode locks in order.
1972 hfs_lock_truncate (VTOC(from_vp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
1974 if ((error
= hfs_lock(VTOC(from_vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1975 hfs_unlock_truncate (VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1979 /* Verify the source file is not in use by anyone besides us (including mmap refs) */
1980 if (vnode_isinuse(from_vp
, 1)) {
1982 hfs_unlock(VTOC(from_vp
));
1983 hfs_unlock_truncate (VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1987 /* Flush out the data in the source file */
1988 VTOC(from_vp
)->c_flag
|= C_SWAPINPROGRESS
;
1989 error
= hfs_filedone (from_vp
, ap
->a_context
);
1990 VTOC(from_vp
)->c_flag
&= ~C_SWAPINPROGRESS
;
1991 hfs_unlock(VTOC(from_vp
));
1992 hfs_unlock_truncate(VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1999 if ((error
= hfs_lockpair(VTOC(from_vp
), VTOC(to_vp
), HFS_EXCLUSIVE_LOCK
)))
2002 from_cp
= VTOC(from_vp
);
2003 to_cp
= VTOC(to_vp
);
2004 hfsmp
= VTOHFS(from_vp
);
2006 /* Resource forks cannot be exchanged. */
2007 if ( VNODE_IS_RSRC(from_vp
) || VNODE_IS_RSRC(to_vp
)) {
2012 // Don't allow modification of the journal or journal_info_block
2013 if (hfs_is_journal_file(hfsmp
, from_cp
) ||
2014 hfs_is_journal_file(hfsmp
, to_cp
)) {
2020 * Ok, now that all of the pre-flighting is done, call the underlying
2021 * function if needed.
2023 if (ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) {
2024 error
= hfs_movedata(from_vp
, to_vp
);
2029 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2035 * Reserve some space in the Catalog file.
2037 if ((error
= cat_preflight(hfsmp
, CAT_EXCHANGE
, &cookie
, vfs_context_proc(ap
->a_context
)))) {
2042 /* The backend code always tries to delete the virtual
2043 * extent id for exchanging files so we need to lock
2044 * the extents b-tree.
2046 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2048 /* Account for the location of the catalog objects. */
2049 if (from_cp
->c_flag
& C_HARDLINK
) {
2050 MAKE_INODE_NAME(from_iname
, sizeof(from_iname
),
2051 from_cp
->c_attr
.ca_linkref
);
2052 from_nameptr
= (unsigned char *)from_iname
;
2053 from_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
2054 from_cp
->c_hint
= 0;
2056 from_nameptr
= from_cp
->c_desc
.cd_nameptr
;
2057 from_parid
= from_cp
->c_parentcnid
;
2059 if (to_cp
->c_flag
& C_HARDLINK
) {
2060 MAKE_INODE_NAME(to_iname
, sizeof(to_iname
),
2061 to_cp
->c_attr
.ca_linkref
);
2062 to_nameptr
= (unsigned char *)to_iname
;
2063 to_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
2066 to_nameptr
= to_cp
->c_desc
.cd_nameptr
;
2067 to_parid
= to_cp
->c_parentcnid
;
2071 * ExchangeFileIDs swaps the extent information attached to two
2072 * different file IDs. It also swaps the extent information that
2073 * may live in the extents-overflow B-Tree.
2075 * We do this in a transaction as this may require a lot of B-Tree nodes
2076 * to do completely, particularly if one of the files in question
2077 * has a lot of extents.
2079 * For example, assume "file1" has fileID 50, and "file2" has fileID 52.
2080 * For the on-disk records, which are assumed to be synced, we will
2081 * first swap the resident inline-8 extents as part of the catalog records.
2082 * Then we will swap any extents overflow records for each file.
2084 * When this function is done, "file1" will have fileID 52, and "file2" will
2087 error
= ExchangeFileIDs(hfsmp
, from_nameptr
, to_nameptr
, from_parid
,
2088 to_parid
, from_cp
->c_hint
, to_cp
->c_hint
);
2089 hfs_systemfile_unlock(hfsmp
, lockflags
);
2092 * Note that we don't need to exchange any extended attributes
2093 * since the attributes are keyed by file ID.
2096 if (error
!= E_NONE
) {
2097 error
= MacToVFSError(error
);
2101 /* Purge the vnodes from the name cache */
2103 cache_purge(from_vp
);
2107 /* Bump both source and destination write counts before any swaps. */
2109 hfs_incr_gencount (from_cp
);
2110 hfs_incr_gencount (to_cp
);
2114 /* Save a copy of "from" attributes before swapping. */
2115 bcopy(&from_cp
->c_desc
, &tempdesc
, sizeof(struct cat_desc
));
2116 bcopy(&from_cp
->c_attr
, &tempattr
, sizeof(struct cat_attr
));
2118 /* Save whether or not each cnode is a hardlink or has EAs */
2119 from_flag_special
= from_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
2120 to_flag_special
= to_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
2122 /* Drop the special bits from each cnode */
2123 from_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
2124 to_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
2127 * Complete the in-memory portion of the copy.
2129 * ExchangeFileIDs swaps the on-disk records involved. We complete the
2130 * operation by swapping the in-memory contents of the two files here.
2131 * We swap the cnode descriptors, which contain name, BSD attributes,
2132 * timestamps, etc, about the file.
2134 * NOTE: We do *NOT* swap the fileforks of the two cnodes. We have
2135 * already swapped the on-disk extent information. As long as we swap the
2136 * IDs, the in-line resident 8 extents that live in the filefork data
2137 * structure will point to the right data for the new file ID if we leave
2140 * As a result, any file descriptor that points to a particular
2141 * vnode (even though it should change names), will continue
2142 * to point to the same content.
2145 /* Copy the "to" -> "from" cnode */
2146 bcopy(&to_cp
->c_desc
, &from_cp
->c_desc
, sizeof(struct cat_desc
));
2148 from_cp
->c_hint
= 0;
2150 * If 'to' was a hardlink, then we copied over its link ID/CNID/(namespace ID)
2151 * when we bcopy'd the descriptor above. However, the cnode attributes
2152 * are not bcopied. As a result, make sure to swap the file IDs of each item.
2154 * Further, other hardlink attributes must be moved along in this swap:
2155 * the linkcount, the linkref, and the firstlink all need to move
2156 * along with the file IDs. See note below regarding the flags and
2157 * what moves vs. what does not.
2160 * linkcount == total # of hardlinks.
2161 * linkref == the indirect inode pointer.
2162 * firstlink == the first hardlink in the chain (written to the raw inode).
2163 * These three are tied to the fileID and must move along with the rest of the data.
2165 from_cp
->c_fileid
= to_cp
->c_attr
.ca_fileid
;
2167 from_cp
->c_itime
= to_cp
->c_itime
;
2168 from_cp
->c_btime
= to_cp
->c_btime
;
2169 from_cp
->c_atime
= to_cp
->c_atime
;
2170 from_cp
->c_ctime
= to_cp
->c_ctime
;
2171 from_cp
->c_gid
= to_cp
->c_gid
;
2172 from_cp
->c_uid
= to_cp
->c_uid
;
2173 from_cp
->c_bsdflags
= to_cp
->c_bsdflags
;
2174 from_cp
->c_mode
= to_cp
->c_mode
;
2175 from_cp
->c_linkcount
= to_cp
->c_linkcount
;
2176 from_cp
->c_attr
.ca_linkref
= to_cp
->c_attr
.ca_linkref
;
2177 from_cp
->c_attr
.ca_firstlink
= to_cp
->c_attr
.ca_firstlink
;
2180 * The cnode flags need to stay with the cnode and not get transferred
2181 * over along with everything else because they describe the content; they are
2182 * not attributes that reflect changes specific to the file ID. In general,
2183 * fields that are tied to the file ID are the ones that will move.
2185 * This reflects the fact that the file may have borrowed blocks, dirty metadata,
2186 * or other extents, which may not yet have been written to the catalog. If
2187 * they were, they would have been transferred above in the ExchangeFileIDs call above...
2189 * The flags that are special are:
2190 * C_HARDLINK, C_HASXATTRS
2192 * These flags move with the item and file ID in the namespace since their
2193 * state is tied to that of the file ID.
2195 * So to transfer the flags, we have to take the following steps
2196 * 1) Store in a localvar whether or not the special bits are set.
2197 * 2) Drop the special bits from the current flags
2198 * 3) swap the special flag bits to their destination
2200 from_cp
->c_flag
|= to_flag_special
;
2201 from_cp
->c_attr
.ca_recflags
= to_cp
->c_attr
.ca_recflags
;
2202 bcopy(to_cp
->c_finderinfo
, from_cp
->c_finderinfo
, 32);
2205 /* Copy the "from" -> "to" cnode */
2206 bcopy(&tempdesc
, &to_cp
->c_desc
, sizeof(struct cat_desc
));
2209 * Pull the file ID from the tempattr we copied above. We can't assume
2210 * it is the same as the CNID.
2212 to_cp
->c_fileid
= tempattr
.ca_fileid
;
2213 to_cp
->c_itime
= tempattr
.ca_itime
;
2214 to_cp
->c_btime
= tempattr
.ca_btime
;
2215 to_cp
->c_atime
= tempattr
.ca_atime
;
2216 to_cp
->c_ctime
= tempattr
.ca_ctime
;
2217 to_cp
->c_gid
= tempattr
.ca_gid
;
2218 to_cp
->c_uid
= tempattr
.ca_uid
;
2219 to_cp
->c_bsdflags
= tempattr
.ca_flags
;
2220 to_cp
->c_mode
= tempattr
.ca_mode
;
2221 to_cp
->c_linkcount
= tempattr
.ca_linkcount
;
2222 to_cp
->c_attr
.ca_linkref
= tempattr
.ca_linkref
;
2223 to_cp
->c_attr
.ca_firstlink
= tempattr
.ca_firstlink
;
2226 * Only OR in the "from" flags into our cnode flags below.
2227 * Leave the rest of the flags alone.
2229 to_cp
->c_flag
|= from_flag_special
;
2231 to_cp
->c_attr
.ca_recflags
= tempattr
.ca_recflags
;
2232 bcopy(tempattr
.ca_finderinfo
, to_cp
->c_finderinfo
, 32);
2235 /* Rehash the cnodes using their new file IDs */
2236 hfs_chash_rehash(hfsmp
, from_cp
, to_cp
);
2239 * When a file moves out of "Cleanup At Startup"
2240 * we can drop its NODUMP status.
2242 if ((from_cp
->c_bsdflags
& UF_NODUMP
) &&
2243 (from_cp
->c_parentcnid
!= to_cp
->c_parentcnid
)) {
2244 from_cp
->c_bsdflags
&= ~UF_NODUMP
;
2245 from_cp
->c_touch_chgtime
= TRUE
;
2247 if ((to_cp
->c_bsdflags
& UF_NODUMP
) &&
2248 (to_cp
->c_parentcnid
!= from_cp
->c_parentcnid
)) {
2249 to_cp
->c_bsdflags
&= ~UF_NODUMP
;
2250 to_cp
->c_touch_chgtime
= TRUE
;
2255 cat_postflight(hfsmp
, &cookie
, vfs_context_proc(ap
->a_context
));
2258 hfs_end_transaction(hfsmp
);
2261 hfs_unlockpair(from_cp
, to_cp
);
2266 hfs_vnop_mmap(struct vnop_mmap_args
*ap
)
2268 struct vnode
*vp
= ap
->a_vp
;
2271 if (VNODE_IS_RSRC(vp
)) {
2272 /* allow pageins of the resource fork */
2274 int compressed
= hfs_file_is_compressed(VTOC(vp
), 1); /* 1 == don't take the cnode lock */
2275 time_t orig_ctime
= VTOC(vp
)->c_ctime
;
2277 if (!compressed
&& (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
)) {
2278 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
2284 if (ap
->a_fflags
& PROT_WRITE
) {
2285 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
2287 /* even though we're manipulating a cnode field here, we're only monotonically increasing
2288 * the generation counter. The vnode can't be recycled (because we hold a FD in order to cause the
2289 * map to happen). So it's safe to do this without holding the cnode lock. The caller's only
2290 * requirement is that the number has been changed.
2292 struct cnode
*cp
= VTOC(vp
);
2293 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
2294 hfs_incr_gencount(cp
);
2300 // NOTE: we return ENOTSUP because we want the cluster layer
2301 // to actually do all the real work.
2309 * This is a non-symmetric variant of exchangedata. In this function,
2310 * the contents of the fork in from_vp are moved to the fork
2311 * specified by to_vp.
2313 * The cnodes pointed to by 'from_vp' and 'to_vp' must be locked.
2315 * The vnode pointed to by 'to_vp' *must* be empty prior to invoking this function.
2316 * We impose this restriction because we may not be able to fully delete the entire
2317 * file's contents in a single transaction, particularly if it has a lot of extents.
2318 * In the normal file deletion codepath, the file is screened for two conditions:
2319 * 1) bigger than 400MB, and 2) more than 8 extents. If so, the file is relocated to
2320 * the hidden directory and the deletion is broken up into multiple truncates. We can't
2321 * do that here because both files need to exist in the namespace. The main reason this
2322 * is imposed is that we may have to touch a whole lot of bitmap blocks if there are
2325 * Any data written to 'from_vp' after this call completes is not guaranteed
2329 * vnode from_vp: source file
2330 * vnode to_vp: destination file; must be empty
2333 * EFBIG - Destination file was not empty
2338 int hfs_movedata (struct vnode
*from_vp
, struct vnode
*to_vp
) {
2340 struct cnode
*from_cp
;
2341 struct cnode
*to_cp
;
2342 struct hfsmount
*hfsmp
= NULL
;
2346 int overflow_blocks
;
2350 /* Get the HFS pointers */
2351 from_cp
= VTOC(from_vp
);
2352 to_cp
= VTOC(to_vp
);
2353 hfsmp
= VTOHFS(from_vp
);
2355 /* Verify that neither source/dest file is open-unlinked */
2356 if (from_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2361 if (to_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2367 * Verify the source file is not in use by anyone besides us.
2369 * This function is typically invoked by a namespace handler
2370 * process responding to a temporarily stalled system call.
2371 * The FD that it is working off of is opened O_EVTONLY, so
2372 * it really has no active usecounts (the kusecount from O_EVTONLY
2373 * is subtracted from the total usecounts).
2375 * As a result, we shouldn't have any active usecounts against
2376 * this vnode when we go to check it below.
2378 if (vnode_isinuse(from_vp
, 0)) {
2383 if (from_cp
->c_rsrc_vp
== from_vp
) {
2388 * We assume that the destination file is already empty.
2389 * Verify that it is.
2392 if (to_cp
->c_rsrcfork
->ff_size
> 0) {
2398 if (to_cp
->c_datafork
->ff_size
> 0) {
2404 /* If the source has the rsrc open, make sure the destination is also the rsrc */
2406 if (to_vp
!= to_cp
->c_rsrc_vp
) {
2412 /* Verify that both forks are data forks */
2413 if (to_vp
!= to_cp
->c_vp
) {
2420 * See if the source file has overflow extents. If it doesn't, we don't
2421 * need to call into MoveData, and the catalog will be enough.
2424 overflow_blocks
= overflow_extents(from_cp
->c_rsrcfork
);
2427 overflow_blocks
= overflow_extents(from_cp
->c_datafork
);
2430 if ((error
= hfs_start_transaction (hfsmp
)) != 0) {
2435 /* Lock the system files: catalog, extents, attributes */
2436 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2438 /* Copy over any catalog allocation data into the new spot. */
2440 if ((error
= hfs_move_fork (from_cp
->c_rsrcfork
, from_cp
, to_cp
->c_rsrcfork
, to_cp
))){
2441 hfs_systemfile_unlock(hfsmp
, lockflags
);
2446 if ((error
= hfs_move_fork (from_cp
->c_datafork
, from_cp
, to_cp
->c_datafork
, to_cp
))) {
2447 hfs_systemfile_unlock(hfsmp
, lockflags
);
2453 * Note that because all we're doing is moving the extents around, we can
2454 * probably do this in a single transaction: Each extent record (group of 8)
2455 * is 64 bytes. A extent overflow B-Tree node is typically 4k. This means
2456 * each node can hold roughly ~60 extent records == (480 extents).
2458 * If a file was massively fragmented and had 20k extents, this means we'd
2459 * roughly touch 20k/480 == 41 to 42 nodes, plus the index nodes, for half
2460 * of the operation. (inserting or deleting). So if we're manipulating 80-100
2461 * nodes, this is basically 320k of data to write to the journal in
2464 if (overflow_blocks
!= 0) {
2466 error
= MoveData(hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 1);
2469 error
= MoveData (hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 0);
2474 /* Reverse the operation. Copy the fork data back into the source */
2476 hfs_move_fork (to_cp
->c_rsrcfork
, to_cp
, from_cp
->c_rsrcfork
, from_cp
);
2479 hfs_move_fork (to_cp
->c_datafork
, to_cp
, from_cp
->c_datafork
, from_cp
);
2483 struct cat_fork
*src_data
= NULL
;
2484 struct cat_fork
*src_rsrc
= NULL
;
2485 struct cat_fork
*dst_data
= NULL
;
2486 struct cat_fork
*dst_rsrc
= NULL
;
2488 /* Touch the times*/
2489 to_cp
->c_touch_acctime
= TRUE
;
2490 to_cp
->c_touch_chgtime
= TRUE
;
2491 to_cp
->c_touch_modtime
= TRUE
;
2493 from_cp
->c_touch_acctime
= TRUE
;
2494 from_cp
->c_touch_chgtime
= TRUE
;
2495 from_cp
->c_touch_modtime
= TRUE
;
2497 hfs_touchtimes(hfsmp
, to_cp
);
2498 hfs_touchtimes(hfsmp
, from_cp
);
2500 if (from_cp
->c_datafork
) {
2501 src_data
= &from_cp
->c_datafork
->ff_data
;
2503 if (from_cp
->c_rsrcfork
) {
2504 src_rsrc
= &from_cp
->c_rsrcfork
->ff_data
;
2507 if (to_cp
->c_datafork
) {
2508 dst_data
= &to_cp
->c_datafork
->ff_data
;
2510 if (to_cp
->c_rsrcfork
) {
2511 dst_rsrc
= &to_cp
->c_rsrcfork
->ff_data
;
2514 /* Update the catalog nodes */
2515 (void) cat_update(hfsmp
, &from_cp
->c_desc
, &from_cp
->c_attr
,
2516 src_data
, src_rsrc
);
2518 (void) cat_update(hfsmp
, &to_cp
->c_desc
, &to_cp
->c_attr
,
2519 dst_data
, dst_rsrc
);
2522 /* unlock the system files */
2523 hfs_systemfile_unlock(hfsmp
, lockflags
);
2528 hfs_end_transaction(hfsmp
);
2536 * Copy all of the catalog and runtime data in srcfork to dstfork.
2538 * This allows us to maintain the invalid ranges across the movedata operation so
2539 * we don't need to force all of the pending IO right now. In addition, we move all
2540 * non overflow-extent extents into the destination here.
2542 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src_cp
,
2543 struct filefork
*dstfork
, struct cnode
*dst_cp
) {
2544 struct rl_entry
*invalid_range
;
2545 int size
= sizeof(struct HFSPlusExtentDescriptor
);
2546 size
= size
* kHFSPlusExtentDensity
;
2548 /* If the dstfork has any invalid ranges, bail out */
2549 invalid_range
= TAILQ_FIRST(&dstfork
->ff_invalidranges
);
2550 if (invalid_range
!= NULL
) {
2554 if (dstfork
->ff_data
.cf_size
!= 0 || dstfork
->ff_data
.cf_new_size
!= 0) {
2558 /* First copy the invalid ranges */
2559 while ((invalid_range
= TAILQ_FIRST(&srcfork
->ff_invalidranges
))) {
2560 off_t start
= invalid_range
->rl_start
;
2561 off_t end
= invalid_range
->rl_end
;
2563 /* Remove it from the srcfork and add it to dstfork */
2564 rl_remove(start
, end
, &srcfork
->ff_invalidranges
);
2565 rl_add(start
, end
, &dstfork
->ff_invalidranges
);
2569 * Ignore the ff_union. We don't move symlinks or system files.
2570 * Now copy the in-catalog extent information
2572 dstfork
->ff_data
.cf_size
= srcfork
->ff_data
.cf_size
;
2573 dstfork
->ff_data
.cf_new_size
= srcfork
->ff_data
.cf_new_size
;
2574 dstfork
->ff_data
.cf_vblocks
= srcfork
->ff_data
.cf_vblocks
;
2575 dstfork
->ff_data
.cf_blocks
= srcfork
->ff_data
.cf_blocks
;
2577 /* just memcpy the whole array of extents to the new location. */
2578 memcpy (dstfork
->ff_data
.cf_extents
, srcfork
->ff_data
.cf_extents
, size
);
2581 * Copy the cnode attribute data.
2584 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_vblocks
;
2585 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_blocks
;
2587 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_vblocks
;
2588 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_blocks
;
2590 /* Now delete the entries in the source fork */
2591 srcfork
->ff_data
.cf_size
= 0;
2592 srcfork
->ff_data
.cf_new_size
= 0;
2593 srcfork
->ff_data
.cf_union
.cfu_bytesread
= 0;
2594 srcfork
->ff_data
.cf_vblocks
= 0;
2595 srcfork
->ff_data
.cf_blocks
= 0;
2597 /* Zero out the old extents */
2598 bzero (srcfork
->ff_data
.cf_extents
, size
);
2604 * cnode must be locked
2607 hfs_fsync(struct vnode
*vp
, int waitfor
, int fullsync
, struct proc
*p
)
2609 struct cnode
*cp
= VTOC(vp
);
2610 struct filefork
*fp
= NULL
;
2612 struct hfsmount
*hfsmp
= VTOHFS(vp
);
2613 struct rl_entry
*invalid_range
;
2615 int waitdata
; /* attributes necessary for data retrieval */
2616 int wait
; /* all other attributes (e.g. atime, etc.) */
2618 int took_trunc_lock
= 0;
2619 int locked_buffers
= 0;
2622 * Applications which only care about data integrity rather than full
2623 * file integrity may opt out of (delay) expensive metadata update
2624 * operations as a performance optimization.
2626 wait
= (waitfor
== MNT_WAIT
);
2627 waitdata
= (waitfor
== MNT_DWAIT
) | wait
;
2628 if (always_do_fullfsync
)
2631 /* HFS directories don't have any data blocks. */
2632 if (vnode_isdir(vp
))
2637 * For system files flush the B-tree header and
2638 * for regular files write out any clusters
2640 if (vnode_issystem(vp
)) {
2641 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2643 if (hfsmp
->jnl
== NULL
) {
2644 BTFlushPath(VTOF(vp
));
2647 } else if (UBCINFOEXISTS(vp
)) {
2649 hfs_lock_truncate(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
2650 took_trunc_lock
= 1;
2652 if (fp
->ff_unallocblocks
!= 0) {
2653 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2655 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2657 /* Don't hold cnode lock when calling into cluster layer. */
2658 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2660 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2663 * When MNT_WAIT is requested and the zero fill timeout
2664 * has expired then we must explicitly zero out any areas
2665 * that are currently marked invalid (holes).
2667 * Files with NODUMP can bypass zero filling here.
2669 if (fp
&& (((cp
->c_flag
& C_ALWAYS_ZEROFILL
) && !TAILQ_EMPTY(&fp
->ff_invalidranges
)) ||
2670 ((wait
|| (cp
->c_flag
& C_ZFWANTSYNC
)) &&
2671 ((cp
->c_bsdflags
& UF_NODUMP
) == 0) &&
2672 UBCINFOEXISTS(vp
) && (vnode_issystem(vp
) ==0) &&
2673 cp
->c_zftimeout
!= 0))) {
2676 if ((cp
->c_flag
& C_ALWAYS_ZEROFILL
) == 0 && !fullsync
&& tv
.tv_sec
< (long)cp
->c_zftimeout
) {
2677 /* Remember that a force sync was requested. */
2678 cp
->c_flag
|= C_ZFWANTSYNC
;
2681 if (!TAILQ_EMPTY(&fp
->ff_invalidranges
)) {
2682 if (!took_trunc_lock
|| (cp
->c_truncatelockowner
== HFS_SHARED_OWNER
)) {
2684 if (took_trunc_lock
) {
2685 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2687 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2688 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2689 took_trunc_lock
= 1;
2691 while ((invalid_range
= TAILQ_FIRST(&fp
->ff_invalidranges
))) {
2692 off_t start
= invalid_range
->rl_start
;
2693 off_t end
= invalid_range
->rl_end
;
2695 /* The range about to be written must be validated
2696 * first, so that VNOP_BLOCKMAP() will return the
2697 * appropriate mapping for the cluster code:
2699 rl_remove(start
, end
, &fp
->ff_invalidranges
);
2701 /* Don't hold cnode lock when calling into cluster layer. */
2703 (void) cluster_write(vp
, (struct uio
*) 0,
2704 fp
->ff_size
, end
+ 1, start
, (off_t
)0,
2705 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
2706 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2707 cp
->c_flag
|= C_MODIFIED
;
2710 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2711 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2713 cp
->c_flag
&= ~C_ZFWANTSYNC
;
2714 cp
->c_zftimeout
= 0;
2717 if (took_trunc_lock
) {
2718 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2719 took_trunc_lock
= 0;
2722 * if we have a journal and if journal_active() returns != 0 then the
2723 * we shouldn't do anything to a locked block (because it is part
2724 * of a transaction). otherwise we'll just go through the normal
2725 * code path and flush the buffer. note journal_active() can return
2726 * -1 if the journal is invalid -- however we still need to skip any
2727 * locked blocks as they get cleaned up when we finish the transaction
2728 * or close the journal.
2730 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
2732 lockflag
= BUF_SKIP_LOCKED
;
2737 * Flush all dirty buffers associated with a vnode.
2738 * Record how many of them were dirty AND locked (if necessary).
2740 locked_buffers
= buf_flushdirtyblks_skipinfo(vp
, waitdata
, lockflag
, "hfs_fsync");
2741 if ((lockflag
& BUF_SKIP_LOCKED
) && (locked_buffers
) && (vnode_vtype(vp
) == VLNK
)) {
2743 * If there are dirty symlink buffers, then we may need to take action
2744 * to prevent issues later on if we are journaled. If we're fsyncing a
2745 * symlink vnode then we are in one of three cases:
2747 * 1) automatic sync has fired. In this case, we don't want the behavior to change.
2749 * 2) Someone has opened the FD for the symlink (not what it points to)
2750 * and has issued an fsync against it. This should be rare, and we don't
2751 * want the behavior to change.
2753 * 3) We are being called by a vclean which is trying to reclaim this
2754 * symlink vnode. If this is the case, then allowing this fsync to
2755 * proceed WITHOUT flushing the journal could result in the vclean
2756 * invalidating the buffer's blocks before the journal transaction is
2757 * written to disk. To prevent this, we force a journal flush
2758 * if the vnode is in the middle of a recycle (VL_TERMINATE or VL_DEAD is set).
2760 if (vnode_isrecycled(vp
)) {
2766 if (vnode_isreg(vp
) && vnode_issystem(vp
)) {
2767 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2769 BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2771 cp
->c_touch_acctime
= FALSE
;
2772 cp
->c_touch_chgtime
= FALSE
;
2773 cp
->c_touch_modtime
= FALSE
;
2774 } else if ( !(vp
->v_flag
& VSWAP
) ) /* User file */ {
2775 retval
= hfs_update(vp
, wait
);
2778 * When MNT_WAIT is requested push out the catalog record for
2779 * this file. If they asked for a full fsync, we can skip this
2780 * because the journal_flush or hfs_metasync_all will push out
2781 * all of the metadata changes.
2783 if ((retval
== 0) && wait
&& !fullsync
&& cp
->c_hint
&&
2784 !ISSET(cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
2785 hfs_metasync(VTOHFS(vp
), (daddr64_t
)cp
->c_hint
, p
);
2789 * If this was a full fsync, make sure all metadata
2790 * changes get to stable storage.
2794 hfs_journal_flush(hfsmp
, FALSE
);
2796 if (journal_uses_fua(hfsmp
->jnl
)) {
2798 * the journal_flush did NOT issue a sync track cache command,
2799 * and the fullsync indicates we are supposed to flush all cached
2800 * data to the media, so issue the sync track cache command
2803 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2806 retval
= hfs_metasync_all(hfsmp
);
2807 /* XXX need to pass context! */
2808 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2817 /* Sync an hfs catalog b-tree node */
2819 hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
)
2825 vp
= HFSTOVCB(hfsmp
)->catalogRefNum
;
2827 // XXXdbg - don't need to do this on a journaled volume
2832 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
2834 * Look for a matching node that has been delayed
2835 * but is not part of a set (B_LOCKED).
2837 * BLK_ONLYVALID causes buf_getblk to return a
2838 * buf_t for the daddr64_t specified only if it's
2839 * currently resident in the cache... the size
2840 * parameter to buf_getblk is ignored when this flag
2843 bp
= buf_getblk(vp
, node
, 0, 0, 0, BLK_META
| BLK_ONLYVALID
);
2846 if ((buf_flags(bp
) & (B_LOCKED
| B_DELWRI
)) == B_DELWRI
)
2847 (void) VNOP_BWRITE(bp
);
2852 hfs_systemfile_unlock(hfsmp
, lockflags
);
2859 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
2860 * without a journal. Note that the volume bitmap does not get written;
2861 * we rely on fsck_hfs to fix that up (which it can do without any loss
2865 hfs_metasync_all(struct hfsmount
*hfsmp
)
2869 /* Lock all of the B-trees so we get a mutually consistent state */
2870 lockflags
= hfs_systemfile_lock(hfsmp
,
2871 SFL_CATALOG
|SFL_EXTENTS
|SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2873 /* Sync each of the B-trees */
2874 if (hfsmp
->hfs_catalog_vp
)
2875 hfs_btsync(hfsmp
->hfs_catalog_vp
, 0);
2876 if (hfsmp
->hfs_extents_vp
)
2877 hfs_btsync(hfsmp
->hfs_extents_vp
, 0);
2878 if (hfsmp
->hfs_attribute_vp
)
2879 hfs_btsync(hfsmp
->hfs_attribute_vp
, 0);
2881 /* Wait for all of the writes to complete */
2882 if (hfsmp
->hfs_catalog_vp
)
2883 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_metasync_all");
2884 if (hfsmp
->hfs_extents_vp
)
2885 vnode_waitforwrites(hfsmp
->hfs_extents_vp
, 0, 0, 0, "hfs_metasync_all");
2886 if (hfsmp
->hfs_attribute_vp
)
2887 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs_metasync_all");
2889 hfs_systemfile_unlock(hfsmp
, lockflags
);
2897 hfs_btsync_callback(struct buf
*bp
, __unused
void *dummy
)
2899 buf_clearflags(bp
, B_LOCKED
);
2900 (void) buf_bawrite(bp
);
2902 return(BUF_CLAIMED
);
2907 hfs_btsync(struct vnode
*vp
, int sync_transaction
)
2909 struct cnode
*cp
= VTOC(vp
);
2913 if (sync_transaction
)
2914 flags
|= BUF_SKIP_NONLOCKED
;
2916 * Flush all dirty buffers associated with b-tree.
2918 buf_iterate(vp
, hfs_btsync_callback
, flags
, 0);
2921 if (vnode_issystem(vp
) && (VTOF(vp
)->fcbBTCBPtr
!= NULL
))
2922 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2923 cp
->c_touch_acctime
= FALSE
;
2924 cp
->c_touch_chgtime
= FALSE
;
2925 cp
->c_touch_modtime
= FALSE
;
2931 * Remove a directory.
2935 struct vnop_rmdir_args
/* {
2936 struct vnode *a_dvp;
2938 struct componentname *a_cnp;
2939 vfs_context_t a_context;
2942 struct vnode
*dvp
= ap
->a_dvp
;
2943 struct vnode
*vp
= ap
->a_vp
;
2944 struct cnode
*dcp
= VTOC(dvp
);
2945 struct cnode
*cp
= VTOC(vp
);
2949 orig_ctime
= VTOC(vp
)->c_ctime
;
2951 if (!S_ISDIR(cp
->c_mode
)) {
2958 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
2961 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2965 /* Check for a race with rmdir on the parent directory */
2966 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2967 hfs_unlockpair (dcp
, cp
);
2972 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
2974 if ((cp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
2977 hfs_unlockpair(dcp
, cp
);
2979 if (hfs_generate_document_id(VTOHFS(vp
), &newid
) == 0) {
2980 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
2981 ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
2983 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
2984 FSE_ARG_DEV
, VTOHFS(vp
)->hfs_raw_dev
,
2985 FSE_ARG_INO
, (ino64_t
)0, // src inode #
2986 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
2987 FSE_ARG_INT32
, newid
,
2991 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rm...
2992 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
2996 error
= hfs_removedir(dvp
, vp
, ap
->a_cnp
, 0, 0);
2998 hfs_unlockpair(dcp
, cp
);
3004 * Remove a directory
3006 * Both dvp and vp cnodes are locked
3009 hfs_removedir(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
3010 int skip_reserve
, int only_unlink
)
3014 struct hfsmount
* hfsmp
;
3015 struct cat_desc desc
;
3017 int error
= 0, started_tr
= 0;
3024 return (EINVAL
); /* cannot remove "." */
3026 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3029 if (cp
->c_entries
!= 0) {
3034 * If the directory is open or in use (e.g. opendir() or current working
3035 * directory for some process); wait for inactive/reclaim to actually
3036 * remove cnode from the catalog. Both inactive and reclaim codepaths are capable
3037 * of removing open-unlinked directories from the catalog, as well as getting rid
3038 * of EAs still on the element. So change only_unlink to true, so that it will get
3041 * Otherwise, we can get into a weird old mess where the directory has C_DELETED,
3042 * but it really means C_NOEXISTS because the item was actually removed from the
3043 * catalog. Then when we try to remove the entry from the catalog later on, it won't
3044 * really be there anymore.
3046 if (vnode_isinuse(vp
, 0)) {
3050 /* Deal with directory hardlinks */
3051 if (cp
->c_flag
& C_HARDLINK
) {
3053 * Note that if we have a directory which was a hardlink at any point,
3054 * its actual directory data is stored in the directory inode in the hidden
3055 * directory rather than the leaf element(s) present in the namespace.
3057 * If there are still other hardlinks to this directory,
3058 * then we'll just eliminate this particular link and the vnode will still exist.
3059 * If this is the last link to an empty directory, then we'll open-unlink the
3060 * directory and it will be only tagged with C_DELETED (as opposed to C_NOEXISTS).
3062 * We could also return EBUSY here.
3065 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
3069 * In a few cases, we may want to allow the directory to persist in an
3070 * open-unlinked state. If the directory is being open-unlinked (still has usecount
3071 * references), or if it has EAs, or if it was being deleted as part of a rename,
3072 * then we go ahead and move it to the hidden directory.
3074 * If the directory is being open-unlinked, then we want to keep the catalog entry
3075 * alive so that future EA calls and fchmod/fstat etc. do not cause issues later.
3077 * If the directory had EAs, then we want to use the open-unlink trick so that the
3078 * EA removal is not done in one giant transaction. Otherwise, it could cause a panic
3079 * due to overflowing the journal.
3081 * Finally, if it was deleted as part of a rename, we move it to the hidden directory
3082 * in order to maintain rename atomicity.
3084 * Note that the allow_dirs argument to hfs_removefile specifies that it is
3085 * supposed to handle directories for this case.
3088 if (((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3089 ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0)) ||
3090 (only_unlink
!= 0)) {
3092 int ret
= hfs_removefile(dvp
, vp
, cnp
, 0, 0, 1, NULL
, only_unlink
);
3094 * Even though hfs_vnop_rename calls vnode_recycle for us on tvp we call
3095 * it here just in case we were invoked by rmdir() on a directory that had
3096 * EAs. To ensure that we start reclaiming the space as soon as possible,
3097 * we call vnode_recycle on the directory.
3105 dcp
->c_flag
|= C_DIR_MODIFICATION
;
3108 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3109 (void)hfs_getinoquota(cp
);
3111 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3117 * Verify the directory is empty (and valid).
3118 * (Rmdir ".." won't be valid since
3119 * ".." will contain a reference to
3120 * the current directory and thus be
3123 if ((dcp
->c_bsdflags
& APPEND
) || (cp
->c_bsdflags
& (IMMUTABLE
| APPEND
))) {
3128 /* Remove the entry from the namei cache: */
3132 * Protect against a race with rename by using the component
3133 * name passed in and parent id from dvp (instead of using
3134 * the cp->c_desc which may have changed).
3136 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
3137 desc
.cd_namelen
= cnp
->cn_namelen
;
3138 desc
.cd_parentcnid
= dcp
->c_fileid
;
3139 desc
.cd_cnid
= cp
->c_cnid
;
3140 desc
.cd_flags
= CD_ISDIR
;
3141 desc
.cd_encoding
= cp
->c_encoding
;
3144 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
3149 /* Remove entry from catalog */
3150 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3152 if (!skip_reserve
) {
3154 * Reserve some space in the Catalog file.
3156 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
3157 hfs_systemfile_unlock(hfsmp
, lockflags
);
3162 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
3166 // if skip_reserve == 1 then we're being called from hfs_vnop_rename() and thus
3167 // we don't need to touch the document_id as it's handled by the rename code.
3168 // otherwise it's a normal remove and we need to save the document id in the
3169 // per thread struct and clear it from the cnode.
3171 struct doc_tombstone
*ut
;
3172 ut
= get_uthread_doc_tombstone();
3173 if (!skip_reserve
&& (cp
->c_bsdflags
& UF_TRACKED
) && should_save_docid_tombstone(ut
, vp
, cnp
)) {
3175 if (ut
->t_lastop_document_id
) {
3176 clear_tombstone_docid(ut
, hfsmp
, NULL
);
3178 save_tombstone(hfsmp
, dvp
, vp
, cnp
, 1);
3182 /* The parent lost a child */
3183 if (dcp
->c_entries
> 0)
3185 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
3186 dcp
->c_dirchangecnt
++;
3188 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)dcp
->c_finderinfo
+ 16);
3189 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
3191 dcp
->c_touch_chgtime
= TRUE
;
3192 dcp
->c_touch_modtime
= TRUE
;
3193 hfs_touchtimes(hfsmp
, cp
);
3194 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3195 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
3198 hfs_systemfile_unlock(hfsmp
, lockflags
);
3204 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3205 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
3208 hfs_volupdate(hfsmp
, VOL_RMDIR
, (dcp
->c_cnid
== kHFSRootFolderID
));
3210 /* Mark C_NOEXISTS since the catalog entry is now gone */
3211 cp
->c_flag
|= C_NOEXISTS
;
3213 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3214 wakeup((caddr_t
)&dcp
->c_flag
);
3217 hfs_end_transaction(hfsmp
);
3225 * Remove a file or link.
3229 struct vnop_remove_args
/* {
3230 struct vnode *a_dvp;
3232 struct componentname *a_cnp;
3234 vfs_context_t a_context;
3237 struct vnode
*dvp
= ap
->a_dvp
;
3238 struct vnode
*vp
= ap
->a_vp
;
3239 struct cnode
*dcp
= VTOC(dvp
);
3241 struct vnode
*rvp
= NULL
;
3242 int error
=0, recycle_rsrc
=0;
3243 int recycle_vnode
= 0;
3244 uint32_t rsrc_vid
= 0;
3251 orig_ctime
= VTOC(vp
)->c_ctime
;
3252 if (!vnode_isnamedstream(vp
) && ((ap
->a_flags
& VNODE_REMOVE_SKIP_NAMESPACE_EVENT
) == 0)) {
3253 error
= check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
3255 // XXXdbg - decide on a policy for handling namespace handler failures!
3256 // for now we just let them proceed.
3265 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
3267 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
3268 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3275 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
3277 if ((cp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
3280 hfs_unlockpair(dcp
, cp
);
3282 if (hfs_generate_document_id(VTOHFS(vp
), &newid
) == 0) {
3283 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3284 ((struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
3286 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
3287 FSE_ARG_DEV
, VTOHFS(vp
)->hfs_raw_dev
,
3288 FSE_ARG_INO
, (ino64_t
)0, // src inode #
3289 FSE_ARG_INO
, (ino64_t
)cp
->c_fileid
, // dst inode #
3290 FSE_ARG_INT32
, newid
,
3294 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rm...
3295 hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
);
3300 * Lazily respond to determining if there is a valid resource fork
3301 * vnode attached to 'cp' if it is a regular file or symlink.
3302 * If the vnode does not exist, then we may proceed without having to
3305 * If, however, it does exist, then we need to acquire an iocount on the
3306 * vnode after acquiring its vid. This ensures that if we have to do I/O
3307 * against it, it can't get recycled from underneath us in the middle
3310 * Note: this function may be invoked for directory hardlinks, so just skip these
3311 * steps if 'vp' is a directory.
3314 if ((vp
->v_type
== VLNK
) || (vp
->v_type
== VREG
)) {
3315 if ((cp
->c_rsrc_vp
) && (rvp
== NULL
)) {
3316 /* We need to acquire the rsrc vnode */
3317 rvp
= cp
->c_rsrc_vp
;
3318 rsrc_vid
= vnode_vid (rvp
);
3320 /* Unlock everything to acquire iocount on the rsrc vnode */
3321 hfs_unlock_truncate (cp
, HFS_LOCK_DEFAULT
);
3322 hfs_unlockpair (dcp
, cp
);
3323 /* Use the vid to maintain identity on rvp */
3324 if (vnode_getwithvid(rvp
, rsrc_vid
)) {
3326 * If this fails, then it was recycled or
3327 * reclaimed in the interim. Reset fields and
3338 * Check to see if we raced rmdir for the parent directory
3339 * hfs_removefile already checks for a race on vp/cp
3341 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
3346 error
= hfs_removefile(dvp
, vp
, ap
->a_cnp
, ap
->a_flags
, 0, 0, NULL
, 0);
3349 * If the remove succeeded in deleting the file, then we may need to mark
3350 * the resource fork for recycle so that it is reclaimed as quickly
3351 * as possible. If it were not recycled quickly, then this resource fork
3352 * vnode could keep a v_parent reference on the data fork, which prevents it
3353 * from going through reclaim (by giving it extra usecounts), except in the force-
3356 * However, a caveat: we need to continue to supply resource fork
3357 * access to open-unlinked files even if the resource fork is not open. This is
3358 * a requirement for the compressed files work. Luckily, hfs_vgetrsrc will handle
3359 * this already if the data fork has been re-parented to the hidden directory.
3361 * As a result, all we really need to do here is mark the resource fork vnode
3362 * for recycle. If it goes out of core, it can be brought in again if needed.
3363 * If the cnode was instead marked C_NOEXISTS, then there wouldn't be any
3371 * If the target was actually removed from the catalog schedule it for
3372 * full reclamation/inactivation. We hold an iocount on it so it should just
3373 * get marked with MARKTERM
3375 if (cp
->c_flag
& C_NOEXISTS
) {
3382 * Drop the truncate lock before unlocking the cnode
3383 * (which can potentially perform a vnode_put and
3384 * recycle the vnode which in turn might require the
3388 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3389 hfs_unlockpair(dcp
, cp
);
3392 /* inactive or reclaim on rvp will clean up the blocks from the rsrc fork */
3395 if (recycle_vnode
) {
3400 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
3409 hfs_removefile_callback(struct buf
*bp
, void *hfsmp
) {
3411 if ( !(buf_flags(bp
) & B_META
))
3412 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp
);
3414 * it's part of the current transaction, kill it.
3416 journal_kill_block(((struct hfsmount
*)hfsmp
)->jnl
, bp
);
3418 return (BUF_CLAIMED
);
3424 * Similar to hfs_vnop_remove except there are additional options.
3425 * This function may be used to remove directories if they have
3426 * lots of EA's -- note the 'allow_dirs' argument.
3428 * This function is able to delete blocks & fork data for the resource
3429 * fork even if it does not exist in core (and have a backing vnode).
3430 * It should infer the correct behavior based on the number of blocks
3431 * in the cnode and whether or not the resource fork pointer exists or
3432 * not. As a result, one only need pass in the 'vp' corresponding to the
3433 * data fork of this file (or main vnode in the case of a directory).
3434 * Passing in a resource fork will result in an error.
3436 * Because we do not create any vnodes in this function, we are not at
3437 * risk of deadlocking against ourselves by double-locking.
3439 * Requires cnode and truncate locks to be held.
3442 hfs_removefile(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
3443 int flags
, int skip_reserve
, int allow_dirs
,
3444 __unused
struct vnode
*rvp
, int only_unlink
)
3448 struct vnode
*rsrc_vp
= NULL
;
3449 struct hfsmount
*hfsmp
;
3450 struct cat_desc desc
;
3452 int dataforkbusy
= 0;
3453 int rsrcforkbusy
= 0;
3457 int isbigfile
= 0, defer_remove
=0, isdir
=0;
3464 /* Check if we lost a race post lookup. */
3465 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3469 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
3473 /* Make sure a remove is permitted */
3474 if (VNODE_IS_RSRC(vp
)) {
3479 * We know it's a data fork.
3480 * Probe the cnode to see if we have a valid resource fork
3483 rsrc_vp
= cp
->c_rsrc_vp
;
3486 /* Don't allow deleting the journal or journal_info_block. */
3487 if (hfs_is_journal_file(hfsmp
, cp
)) {
3492 * If removing a symlink, then we need to ensure that the
3493 * data blocks for the symlink are not still in-flight or pending.
3494 * If so, we will unlink the symlink here, making its blocks
3495 * available for re-allocation by a subsequent transaction. That is OK, but
3496 * then the I/O for the data blocks could then go out before the journal
3497 * transaction that created it was flushed, leading to I/O ordering issues.
3499 if (vp
->v_type
== VLNK
) {
3501 * This will block if the asynchronous journal flush is in progress.
3502 * If this symlink is not being renamed over and doesn't have any open FDs,
3503 * then we'll remove it from the journal's bufs below in kill_block.
3505 buf_wait_for_shadow_io (vp
, 0);
3509 * Hard links require special handling.
3511 if (cp
->c_flag
& C_HARDLINK
) {
3512 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) && vnode_isinuse(vp
, 0)) {
3515 /* A directory hard link with a link count of one is
3516 * treated as a regular directory. Therefore it should
3517 * only be removed using rmdir().
3519 if ((vnode_isdir(vp
) == 1) && (cp
->c_linkcount
== 1) &&
3520 (allow_dirs
== 0)) {
3523 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
3527 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
3528 if (vnode_isdir(vp
)) {
3529 if (allow_dirs
== 0)
3530 return (EPERM
); /* POSIX */
3533 /* Sanity check the parent ids. */
3534 if ((cp
->c_parentcnid
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
3535 (cp
->c_parentcnid
!= dcp
->c_fileid
)) {
3539 dcp
->c_flag
|= C_DIR_MODIFICATION
;
3541 // this guy is going away so mark him as such
3542 cp
->c_flag
|= C_DELETED
;
3545 /* Remove our entry from the namei cache. */
3549 * If the caller was operating on a file (as opposed to a
3550 * directory with EAs), then we need to figure out
3551 * whether or not it has a valid resource fork vnode.
3553 * If there was a valid resource fork vnode, then we need
3554 * to use hfs_truncate to eliminate its data. If there is
3555 * no vnode, then we hold the cnode lock which would
3556 * prevent it from being created. As a result,
3557 * we can use the data deletion functions which do not
3558 * require that a cnode/vnode pair exist.
3561 /* Check if this file is being used. */
3563 dataforkbusy
= vnode_isinuse(vp
, 0);
3565 * At this point, we know that 'vp' points to the
3566 * a data fork because we checked it up front. And if
3567 * there is no rsrc fork, rsrc_vp will be NULL.
3569 if (rsrc_vp
&& (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
3570 rsrcforkbusy
= vnode_isinuse(rsrc_vp
, 0);
3574 /* Check if we have to break the deletion into multiple pieces. */
3576 isbigfile
= ((cp
->c_datafork
->ff_size
>= HFS_BIGFILE_SIZE
) && overflow_extents(VTOF(vp
)));
3579 /* Check if the file has xattrs. If it does we'll have to delete them in
3580 individual transactions in case there are too many */
3581 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3582 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
3586 /* If we are explicitly told to only unlink item and move to hidden dir, then do it */
3592 * Carbon semantics prohibit deleting busy files.
3593 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
3595 if (dataforkbusy
|| rsrcforkbusy
) {
3596 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) ||
3597 (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0)) {
3604 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3605 (void)hfs_getinoquota(cp
);
3609 * Do a ubc_setsize to indicate we need to wipe contents if:
3610 * 1) item is a regular file.
3611 * 2) Neither fork is busy AND we are not told to unlink this.
3613 * We need to check for the defer_remove since it can be set without
3614 * having a busy data or rsrc fork
3616 if (isdir
== 0 && (!dataforkbusy
|| !rsrcforkbusy
) && (defer_remove
== 0)) {
3618 * A ubc_setsize can cause a pagein so defer it
3619 * until after the cnode lock is dropped. The
3620 * cnode lock cannot be dropped/reacquired here
3621 * since we might already hold the journal lock.
3623 if (!dataforkbusy
&& cp
->c_datafork
->ff_blocks
&& !isbigfile
) {
3624 cp
->c_flag
|= C_NEED_DATA_SETSIZE
;
3626 if (!rsrcforkbusy
&& rsrc_vp
) {
3627 cp
->c_flag
|= C_NEED_RSRC_SETSIZE
;
3631 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3636 // XXXdbg - if we're journaled, kill any dirty symlink buffers
3637 if (hfsmp
->jnl
&& vnode_islnk(vp
) && (defer_remove
== 0)) {
3638 buf_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
3642 * Prepare to truncate any non-busy forks. Busy forks will
3643 * get truncated when their vnode goes inactive.
3644 * Note that we will only enter this region if we
3645 * can avoid creating an open-unlinked file. If
3646 * either region is busy, we will have to create an open
3649 * Since we are deleting the file, we need to stagger the runtime
3650 * modifications to do things in such a way that a crash won't
3651 * result in us getting overlapped extents or any other
3652 * bad inconsistencies. As such, we call prepare_release_storage
3653 * which updates the UBC, updates quota information, and releases
3654 * any loaned blocks that belong to this file. No actual
3655 * truncation or bitmap manipulation is done until *AFTER*
3656 * the catalog record is removed.
3658 if (isdir
== 0 && (!dataforkbusy
&& !rsrcforkbusy
) && (only_unlink
== 0)) {
3660 if (!dataforkbusy
&& !isbigfile
&& cp
->c_datafork
->ff_blocks
!= 0) {
3662 error
= hfs_prepare_release_storage (hfsmp
, vp
);
3670 * If the resource fork vnode does not exist, we can skip this step.
3672 if (!rsrcforkbusy
&& rsrc_vp
) {
3673 error
= hfs_prepare_release_storage (hfsmp
, rsrc_vp
);
3682 * Protect against a race with rename by using the component
3683 * name passed in and parent id from dvp (instead of using
3684 * the cp->c_desc which may have changed). Also, be aware that
3685 * because we allow directories to be passed in, we need to special case
3686 * this temporary descriptor in case we were handed a directory.
3689 desc
.cd_flags
= CD_ISDIR
;
3694 desc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
3695 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
3696 desc
.cd_namelen
= cnp
->cn_namelen
;
3697 desc
.cd_parentcnid
= dcp
->c_fileid
;
3698 desc
.cd_hint
= cp
->c_desc
.cd_hint
;
3699 desc
.cd_cnid
= cp
->c_cnid
;
3703 * There are two cases to consider:
3704 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
3705 * 2. File is not in use ==> remove the file
3707 * We can get a directory in case 1 because it may have had lots of attributes,
3708 * which need to get removed here.
3710 if (dataforkbusy
|| rsrcforkbusy
|| isbigfile
|| defer_remove
) {
3712 struct cat_desc to_desc
;
3713 struct cat_desc todir_desc
;
3716 * Orphan this file or directory (move to hidden directory).
3717 * Again, we need to take care that we treat directories as directories,
3718 * and files as files. Because directories with attributes can be passed in
3719 * check to make sure that we have a directory or a file before filling in the
3720 * temporary descriptor's flags. We keep orphaned directories AND files in
3721 * the FILE_HARDLINKS private directory since we're generalizing over all
3722 * orphaned filesystem objects.
3724 bzero(&todir_desc
, sizeof(todir_desc
));
3725 todir_desc
.cd_parentcnid
= 2;
3727 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
3728 bzero(&to_desc
, sizeof(to_desc
));
3729 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
3730 to_desc
.cd_namelen
= strlen(delname
);
3731 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
3733 to_desc
.cd_flags
= CD_ISDIR
;
3736 to_desc
.cd_flags
= 0;
3738 to_desc
.cd_cnid
= cp
->c_cnid
;
3740 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3741 if (!skip_reserve
) {
3742 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, NULL
, 0))) {
3743 hfs_systemfile_unlock(hfsmp
, lockflags
);
3748 error
= cat_rename(hfsmp
, &desc
, &todir_desc
,
3749 &to_desc
, (struct cat_desc
*)NULL
);
3752 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
3754 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
3756 (void) cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
3757 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
3759 /* Update the parent directory */
3760 if (dcp
->c_entries
> 0)
3763 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
3765 dcp
->c_dirchangecnt
++;
3767 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)dcp
->c_finderinfo
+ 16);
3768 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
3770 dcp
->c_ctime
= tv
.tv_sec
;
3771 dcp
->c_mtime
= tv
.tv_sec
;
3772 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3774 /* Update the file or directory's state */
3775 cp
->c_flag
|= C_DELETED
;
3776 cp
->c_ctime
= tv
.tv_sec
;
3778 (void) cat_update(hfsmp
, &to_desc
, &cp
->c_attr
, NULL
, NULL
);
3780 hfs_systemfile_unlock(hfsmp
, lockflags
);
3787 * Nobody is using this item; we can safely remove everything.
3789 struct filefork
*temp_rsrc_fork
= NULL
;
3792 int blksize
= hfsmp
->blockSize
;
3794 u_int32_t fileid
= cp
->c_fileid
;
3797 * Figure out if we need to read the resource fork data into
3798 * core before wiping out the catalog record.
3800 * 1) Must not be a directory
3801 * 2) cnode's c_rsrcfork ptr must be NULL.
3802 * 3) rsrc fork must have actual blocks
3804 if ((isdir
== 0) && (cp
->c_rsrcfork
== NULL
) &&
3805 (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
3807 * The resource fork vnode & filefork did not exist.
3808 * Create a temporary one for use in this function only.
3810 MALLOC_ZONE (temp_rsrc_fork
, struct filefork
*, sizeof (struct filefork
), M_HFSFORK
, M_WAITOK
);
3811 bzero(temp_rsrc_fork
, sizeof(struct filefork
));
3812 temp_rsrc_fork
->ff_cp
= cp
;
3813 rl_init(&temp_rsrc_fork
->ff_invalidranges
);
3816 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3818 /* Look up the resource fork first, if necessary */
3819 if (temp_rsrc_fork
) {
3820 error
= cat_lookup (hfsmp
, &desc
, 1, 0, (struct cat_desc
*) NULL
,
3821 (struct cat_attr
*) NULL
, &temp_rsrc_fork
->ff_data
, NULL
);
3823 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3824 hfs_systemfile_unlock (hfsmp
, lockflags
);
3829 if (!skip_reserve
) {
3830 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
3831 if (temp_rsrc_fork
) {
3832 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3834 hfs_systemfile_unlock(hfsmp
, lockflags
);
3839 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
3841 if (error
&& error
!= ENXIO
&& error
!= ENOENT
) {
3842 printf("hfs_removefile: deleting file %s (id=%d) vol=%s err=%d\n",
3843 cp
->c_desc
.cd_nameptr
, cp
->c_attr
.ca_fileid
, hfsmp
->vcbVN
, error
);
3847 /* Update the parent directory */
3848 if (dcp
->c_entries
> 0)
3850 dcp
->c_dirchangecnt
++;
3852 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)dcp
->c_finderinfo
+ 16);
3853 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
3855 dcp
->c_ctime
= tv
.tv_sec
;
3856 dcp
->c_mtime
= tv
.tv_sec
;
3857 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3859 hfs_systemfile_unlock(hfsmp
, lockflags
);
3862 if (temp_rsrc_fork
) {
3863 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3869 * Now that we've wiped out the catalog record, the file effectively doesn't
3870 * exist anymore. So update the quota records to reflect the loss of the
3871 * data fork and the resource fork.
3874 if (cp
->c_datafork
->ff_blocks
> 0) {
3875 savedbytes
= ((off_t
)cp
->c_datafork
->ff_blocks
* (off_t
)blksize
);
3876 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3880 * We may have just deleted the catalog record for a resource fork even
3881 * though it did not exist in core as a vnode. However, just because there
3882 * was a resource fork pointer in the cnode does not mean that it had any blocks.
3884 if (temp_rsrc_fork
|| cp
->c_rsrcfork
) {
3885 if (cp
->c_rsrcfork
) {
3886 if (cp
->c_rsrcfork
->ff_blocks
> 0) {
3887 savedbytes
= ((off_t
)cp
->c_rsrcfork
->ff_blocks
* (off_t
)blksize
);
3888 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3892 /* we must have used a temporary fork */
3893 savedbytes
= ((off_t
)temp_rsrc_fork
->ff_blocks
* (off_t
)blksize
);
3894 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3898 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
3899 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
3904 * If we didn't get any errors deleting the catalog entry, then go ahead
3905 * and release the backing store now. The filefork pointers are still valid.
3907 if (temp_rsrc_fork
) {
3908 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, temp_rsrc_fork
, fileid
);
3911 /* if cp->c_rsrcfork == NULL, hfs_release_storage will skip over it. */
3912 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, cp
->c_rsrcfork
, fileid
);
3916 * If we encountered an error updating the extents and bitmap,
3917 * mark the volume inconsistent. At this point, the catalog record has
3918 * already been deleted, so we can't recover it at this point. We need
3919 * to proceed and update the volume header and mark the cnode C_NOEXISTS.
3920 * The subsequent fsck should be able to recover the free space for us.
3922 hfs_mark_volume_inconsistent(hfsmp
);
3925 /* reset update_vh to 0, since hfs_release_storage should have done it for us */
3929 /* Get rid of the temporary rsrc fork */
3930 if (temp_rsrc_fork
) {
3931 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3934 cp
->c_flag
|= C_NOEXISTS
;
3935 cp
->c_flag
&= ~C_DELETED
;
3937 cp
->c_touch_chgtime
= TRUE
; /* XXX needed ? */
3941 * We must never get a directory if we're in this else block. We could
3942 * accidentally drop the number of files in the volume header if we did.
3944 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
3949 // if skip_reserve == 1 then we're being called from hfs_vnop_rename() and thus
3950 // we don't need to touch the document_id as it's handled by the rename code.
3951 // otherwise it's a normal remove and we need to save the document id in the
3952 // per thread struct and clear it from the cnode.
3954 struct doc_tombstone
*ut
;
3955 ut
= get_uthread_doc_tombstone();
3956 if (!error
&& !skip_reserve
&& (cp
->c_bsdflags
& UF_TRACKED
) && should_save_docid_tombstone(ut
, vp
, cnp
)) {
3958 if (ut
->t_lastop_document_id
) {
3959 clear_tombstone_docid(ut
, hfsmp
, NULL
);
3961 save_tombstone(hfsmp
, dvp
, vp
, cnp
, 1);
3967 * All done with this cnode's descriptor...
3969 * Note: all future catalog calls for this cnode must be by
3970 * fileid only. This is OK for HFS (which doesn't have file
3971 * thread records) since HFS doesn't support the removal of
3974 cat_releasedesc(&cp
->c_desc
);
3978 cp
->c_flag
&= ~C_DELETED
;
3983 * If we bailed out earlier, we may need to update the volume header
3984 * to deal with the borrowed blocks accounting.
3986 hfs_volupdate (hfsmp
, VOL_UPDATE
, 0);
3990 hfs_end_transaction(hfsmp
);
3993 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3994 wakeup((caddr_t
)&dcp
->c_flag
);
4000 __private_extern__
void
4001 replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
)
4003 // fixes 4348457 and 4463138
4004 if (&cp
->c_desc
== cdp
) {
4008 /* First release allocated name buffer */
4009 if (cp
->c_desc
.cd_flags
& CD_HASBUF
&& cp
->c_desc
.cd_nameptr
!= 0) {
4010 const u_int8_t
*name
= cp
->c_desc
.cd_nameptr
;
4012 cp
->c_desc
.cd_nameptr
= 0;
4013 cp
->c_desc
.cd_namelen
= 0;
4014 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
4015 vfs_removename((const char *)name
);
4017 bcopy(cdp
, &cp
->c_desc
, sizeof(cp
->c_desc
));
4019 /* Cnode now owns the name buffer */
4020 cdp
->cd_nameptr
= 0;
4021 cdp
->cd_namelen
= 0;
4022 cdp
->cd_flags
&= ~CD_HASBUF
;
4029 * The VFS layer guarantees that:
4030 * - source and destination will either both be directories, or
4031 * both not be directories.
4032 * - all the vnodes are from the same file system
4034 * When the target is a directory, HFS must ensure that its empty.
4036 * Note that this function requires up to 6 vnodes in order to work properly
4037 * if it is operating on files (and not on directories). This is because only
4038 * files can have resource forks, and we now require iocounts to be held on the
4039 * vnodes corresponding to the resource forks (if applicable) as well as
4040 * the files or directories undergoing rename. The problem with not holding
4041 * iocounts on the resource fork vnodes is that it can lead to a deadlock
4042 * situation: The rsrc fork of the source file may be recycled and reclaimed
4043 * in order to provide a vnode for the destination file's rsrc fork. Since
4044 * data and rsrc forks share the same cnode, we'd eventually try to lock the
4045 * source file's cnode in order to sync its rsrc fork to disk, but it's already
4046 * been locked. By taking the rsrc fork vnodes up front we ensure that they
4047 * cannot be recycled, and that the situation mentioned above cannot happen.
4051 struct vnop_rename_args
/* {
4052 struct vnode *a_fdvp;
4053 struct vnode *a_fvp;
4054 struct componentname *a_fcnp;
4055 struct vnode *a_tdvp;
4056 struct vnode *a_tvp;
4057 struct componentname *a_tcnp;
4058 vfs_context_t a_context;
4061 struct vnode
*tvp
= ap
->a_tvp
;
4062 struct vnode
*tdvp
= ap
->a_tdvp
;
4063 struct vnode
*fvp
= ap
->a_fvp
;
4064 struct vnode
*fdvp
= ap
->a_fdvp
;
4066 * Note that we only need locals for the target/destination's
4067 * resource fork vnode (and only if necessary). We don't care if the
4068 * source has a resource fork vnode or not.
4070 struct vnode
*tvp_rsrc
= NULLVP
;
4071 uint32_t tvp_rsrc_vid
= 0;
4072 struct componentname
*tcnp
= ap
->a_tcnp
;
4073 struct componentname
*fcnp
= ap
->a_fcnp
;
4074 struct proc
*p
= vfs_context_proc(ap
->a_context
);
4079 struct cnode
*error_cnode
;
4080 struct cat_desc from_desc
;
4081 struct cat_desc to_desc
;
4082 struct cat_desc out_desc
;
4083 struct hfsmount
*hfsmp
;
4084 cat_cookie_t cookie
;
4085 int tvp_deleted
= 0;
4086 int started_tr
= 0, got_cookie
= 0;
4087 int took_trunc_lock
= 0;
4090 time_t orig_from_ctime
, orig_to_ctime
;
4091 int emit_rename
= 1;
4092 int emit_delete
= 1;
4096 orig_from_ctime
= VTOC(fvp
)->c_ctime
;
4097 if (tvp
&& VTOC(tvp
)) {
4098 orig_to_ctime
= VTOC(tvp
)->c_ctime
;
4103 hfsmp
= VTOHFS(tdvp
);
4105 * Do special case checks here. If fvp == tvp then we need to check the
4106 * cnode with locks held.
4109 int is_hardlink
= 0;
4111 * In this case, we do *NOT* ever emit a DELETE event.
4112 * We may not necessarily emit a RENAME event
4115 if ((error
= hfs_lock(VTOC(fvp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
4118 /* Check to see if the item is a hardlink or not */
4119 is_hardlink
= (VTOC(fvp
)->c_flag
& C_HARDLINK
);
4120 hfs_unlock (VTOC(fvp
));
4123 * If the item is not a hardlink, then case sensitivity must be off, otherwise
4124 * two names should not resolve to the same cnode unless they were case variants.
4129 * Hardlinks are a little trickier. We only want to emit a rename event
4130 * if the item is a hardlink, the parent directories are the same, case sensitivity
4131 * is off, and the case folded names are the same. See the fvp == tvp case below for more
4135 if ((fdvp
== tdvp
) && ((hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) == 0)) {
4136 if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4137 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
4138 /* Then in this case only it is ok to emit a rename */
4145 /* c_bsdflags should only be assessed while holding the cnode lock.
4146 * This is not done consistently throughout the code and can result
4147 * in race. This will be fixed via rdar://12181064
4149 if (VTOC(fvp
)->c_bsdflags
& UF_TRACKED
) {
4152 check_for_tracked_file(fvp
, orig_from_ctime
, NAMESPACE_HANDLER_RENAME_OP
, NULL
);
4155 if (tvp
&& VTOC(tvp
)) {
4157 check_for_tracked_file(tvp
, orig_to_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
4162 /* When tvp exists, take the truncate lock for hfs_removefile(). */
4163 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
4164 hfs_lock_truncate(VTOC(tvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4165 took_trunc_lock
= 1;
4169 error
= hfs_lockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
,
4170 HFS_EXCLUSIVE_LOCK
, &error_cnode
);
4172 if (took_trunc_lock
) {
4173 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4174 took_trunc_lock
= 0;
4178 * We hit an error path. If we were trying to re-acquire the locks
4179 * after coming through here once, we might have already obtained
4180 * an iocount on tvp's resource fork vnode. Drop that before dealing
4181 * with the failure. Note this is safe -- since we are in an
4182 * error handling path, we can't be holding the cnode locks.
4185 vnode_put (tvp_rsrc
);
4191 * tvp might no longer exist. If the cause of the lock failure
4192 * was tvp, then we can try again with tvp/tcp set to NULL.
4193 * This is ok because the vfs syscall will vnode_put the vnodes
4194 * after we return from hfs_vnop_rename.
4196 if ((error
== ENOENT
) && (tvp
!= NULL
) && (error_cnode
== VTOC(tvp
))) {
4202 if (emit_rename
&& is_tracked
) {
4203 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_FAILED_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
4212 tcp
= tvp
? VTOC(tvp
) : NULL
;
4215 // if the item is tracked but doesn't have a document_id, assign one and generate an fsevent for it
4218 if ((fcp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
4221 hfs_unlockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
);
4224 if (hfs_generate_document_id(hfsmp
, &newid
) == 0) {
4225 hfs_lock(fcp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4226 ((struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
4228 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4229 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4230 FSE_ARG_INO
, (ino64_t
)0, // src inode #
4231 FSE_ARG_INO
, (ino64_t
)fcp
->c_fileid
, // dst inode #
4232 FSE_ARG_INT32
, newid
,
4237 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rename...
4241 // check if we're going to need to fix tcp as well. if we aren't, go back relock
4242 // everything. otherwise continue on and fix up tcp as well before relocking.
4244 if (tcp
== NULL
|| !(tcp
->c_bsdflags
& UF_TRACKED
) || ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
!= 0) {
4250 // same thing for tcp if it's set
4252 if (tcp
&& (tcp
->c_bsdflags
& UF_TRACKED
) && ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
== 0) {
4256 hfs_unlockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
);
4260 if (hfs_generate_document_id(hfsmp
, &newid
) == 0) {
4261 hfs_lock(tcp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4262 ((struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16))->document_id
= newid
;
4264 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4265 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4266 FSE_ARG_INO
, (ino64_t
)0, // src inode #
4267 FSE_ARG_INO
, (ino64_t
)tcp
->c_fileid
, // dst inode #
4268 FSE_ARG_INT32
, newid
,
4273 // XXXdbg - couldn't get a new docid... what to do? can't really fail the rename...
4276 // go back up and relock everything. next time through the if statement won't be true
4277 // and we'll skip over this block of code.
4284 * Acquire iocounts on the destination's resource fork vnode
4285 * if necessary. If dst/src are files and the dst has a resource
4286 * fork vnode, then we need to try and acquire an iocount on the rsrc vnode.
4287 * If it does not exist, then we don't care and can skip it.
4289 if ((vnode_isreg(fvp
)) || (vnode_islnk(fvp
))) {
4290 if ((tvp
) && (tcp
->c_rsrc_vp
) && (tvp_rsrc
== NULL
)) {
4291 tvp_rsrc
= tcp
->c_rsrc_vp
;
4293 * We can look at the vid here because we're holding the
4294 * cnode lock on the underlying cnode for this rsrc vnode.
4296 tvp_rsrc_vid
= vnode_vid (tvp_rsrc
);
4298 /* Unlock everything to acquire iocount on this rsrc vnode */
4299 if (took_trunc_lock
) {
4300 hfs_unlock_truncate (VTOC(tvp
), HFS_LOCK_DEFAULT
);
4301 took_trunc_lock
= 0;
4303 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4305 if (vnode_getwithvid (tvp_rsrc
, tvp_rsrc_vid
)) {
4306 /* iocount acquisition failed. Reset fields and start over.. */
4316 /* Ensure we didn't race src or dst parent directories with rmdir. */
4317 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4322 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4328 /* Check for a race against unlink. The hfs_valid_cnode checks validate
4329 * the parent/child relationship with fdcp and tdcp, as well as the
4330 * component name of the target cnodes.
4332 if ((fcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, fdvp
, fcnp
, fcp
->c_fileid
, NULL
, &error
)) {
4337 if (tcp
&& ((tcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, tdvp
, tcnp
, tcp
->c_fileid
, NULL
, &error
))) {
4339 // hmm, the destination vnode isn't valid any more.
4340 // in this case we can just drop him and pretend he
4341 // never existed in the first place.
4343 if (took_trunc_lock
) {
4344 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4345 took_trunc_lock
= 0;
4349 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4354 // retry the locking with tvp null'ed out
4358 fdcp
->c_flag
|= C_DIR_MODIFICATION
;
4360 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
4364 * Disallow renaming of a directory hard link if the source and
4365 * destination parent directories are different, or a directory whose
4366 * descendant is a directory hard link and the one of the ancestors
4367 * of the destination directory is a directory hard link.
4369 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
4370 if (fcp
->c_flag
& C_HARDLINK
) {
4374 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
4375 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4376 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
4378 hfs_systemfile_unlock(hfsmp
, lockflags
);
4381 hfs_systemfile_unlock(hfsmp
, lockflags
);
4386 * The following edge case is caught here:
4387 * (to cannot be a descendent of from)
4400 if (tdcp
->c_parentcnid
== fcp
->c_fileid
) {
4406 * The following two edge cases are caught here:
4407 * (note tvp is not empty)
4420 if (tvp
&& vnode_isdir(tvp
) && (tcp
->c_entries
!= 0) && fvp
!= tvp
) {
4426 * The following edge case is caught here:
4427 * (the from child and parent are the same)
4440 * Make sure "from" vnode and its parent are changeable.
4442 if ((fcp
->c_bsdflags
& (IMMUTABLE
| APPEND
)) || (fdcp
->c_bsdflags
& APPEND
)) {
4448 * If the destination parent directory is "sticky", then the
4449 * user must own the parent directory, or the destination of
4450 * the rename, otherwise the destination may not be changed
4451 * (except by root). This implements append-only directories.
4453 * Note that checks for immutable and write access are done
4454 * by the call to hfs_removefile.
4456 if (tvp
&& (tdcp
->c_mode
& S_ISTXT
) &&
4457 (suser(vfs_context_ucred(tcnp
->cn_context
), NULL
)) &&
4458 (kauth_cred_getuid(vfs_context_ucred(tcnp
->cn_context
)) != tdcp
->c_uid
) &&
4459 (hfs_owner_rights(hfsmp
, tcp
->c_uid
, vfs_context_ucred(tcnp
->cn_context
), p
, false)) ) {
4464 /* Don't allow modification of the journal or journal_info_block */
4465 if (hfs_is_journal_file(hfsmp
, fcp
) ||
4466 (tcp
&& hfs_is_journal_file(hfsmp
, tcp
))) {
4473 (void)hfs_getinoquota(tcp
);
4475 /* Preflighting done, take fvp out of the name space. */
4478 bzero(&from_desc
, sizeof(from_desc
));
4479 from_desc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
4480 from_desc
.cd_namelen
= fcnp
->cn_namelen
;
4481 from_desc
.cd_parentcnid
= fdcp
->c_fileid
;
4482 from_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4483 from_desc
.cd_cnid
= fcp
->c_cnid
;
4485 bzero(&to_desc
, sizeof(to_desc
));
4486 to_desc
.cd_nameptr
= (const u_int8_t
*)tcnp
->cn_nameptr
;
4487 to_desc
.cd_namelen
= tcnp
->cn_namelen
;
4488 to_desc
.cd_parentcnid
= tdcp
->c_fileid
;
4489 to_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4490 to_desc
.cd_cnid
= fcp
->c_cnid
;
4492 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4497 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
4498 * inside a journal transaction and without holding a cnode lock.
4499 * As setting of this bit depends on being in journal transaction for
4500 * concurrency, check this bit again after we start journal transaction for rename
4501 * to ensure that this directory does not have any descendant that
4502 * is a directory hard link.
4504 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
4505 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
4506 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4507 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
4509 hfs_systemfile_unlock(hfsmp
, lockflags
);
4512 hfs_systemfile_unlock(hfsmp
, lockflags
);
4516 // if it's a hardlink then re-lookup the name so
4517 // that we get the correct cnid in from_desc (see
4518 // the comment in hfs_removefile for more details)
4520 if (fcp
->c_flag
& C_HARDLINK
) {
4521 struct cat_desc tmpdesc
;
4524 tmpdesc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
4525 tmpdesc
.cd_namelen
= fcnp
->cn_namelen
;
4526 tmpdesc
.cd_parentcnid
= fdcp
->c_fileid
;
4527 tmpdesc
.cd_hint
= fdcp
->c_childhint
;
4528 tmpdesc
.cd_flags
= fcp
->c_desc
.cd_flags
& CD_ISDIR
;
4529 tmpdesc
.cd_encoding
= 0;
4531 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4533 if (cat_lookup(hfsmp
, &tmpdesc
, 0, 0, NULL
, NULL
, NULL
, &real_cnid
) != 0) {
4534 hfs_systemfile_unlock(hfsmp
, lockflags
);
4538 // use the real cnid instead of whatever happened to be there
4539 from_desc
.cd_cnid
= real_cnid
;
4540 hfs_systemfile_unlock(hfsmp
, lockflags
);
4544 * Reserve some space in the Catalog file.
4546 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
+ CAT_DELETE
, &cookie
, p
))) {
4552 * If the destination exists then it may need to be removed.
4554 * Due to HFS's locking system, we should always move the
4555 * existing 'tvp' element to the hidden directory in hfs_vnop_rename.
4556 * Because the VNOP_LOOKUP call enters and exits the filesystem independently
4557 * of the actual vnop that it was trying to do (stat, link, readlink),
4558 * we must release the cnode lock of that element during the interim to
4559 * do MAC checking, vnode authorization, and other calls. In that time,
4560 * the item can be deleted (or renamed over). However, only in the rename
4561 * case is it inappropriate to return ENOENT from any of those calls. Either
4562 * the call should return information about the old element (stale), or get
4563 * information about the newer element that we are about to write in its place.
4565 * HFS lookup has been modified to detect a rename and re-drive its
4566 * lookup internally. For other calls that have already succeeded in
4567 * their lookup call and are waiting to acquire the cnode lock in order
4568 * to proceed, that cnode lock will not fail due to the cnode being marked
4569 * C_NOEXISTS, because it won't have been marked as such. It will only
4570 * have C_DELETED. Thus, they will simply act on the stale open-unlinked
4571 * element. All future callers will get the new element.
4573 * To implement this behavior, we pass the "only_unlink" argument to
4574 * hfs_removefile and hfs_removedir. This will result in the vnode acting
4575 * as though it is open-unlinked. Additionally, when we are done moving the
4576 * element to the hidden directory, we vnode_recycle the target so that it is
4577 * reclaimed as soon as possible. Reclaim and inactive are both
4578 * capable of clearing out unused blocks for an open-unlinked file or dir.
4582 // if the destination has a document id, we need to preserve it
4585 uint32_t document_id
;
4586 struct FndrExtendedDirInfo
*ffip
= (struct FndrExtendedDirInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16);
4587 struct FndrExtendedDirInfo
*tfip
= (struct FndrExtendedDirInfo
*)((char *)&tcp
->c_attr
.ca_finderinfo
+ 16);
4589 if (ffip
->document_id
&& tfip
->document_id
) {
4590 // both documents are tracked. only save a tombstone from tcp and do nothing else.
4591 save_tombstone(hfsmp
, tdvp
, tvp
, tcnp
, 0);
4593 struct doc_tombstone
*ut
;
4594 ut
= get_uthread_doc_tombstone();
4596 document_id
= tfip
->document_id
;
4597 tfip
->document_id
= 0;
4599 if (document_id
!= 0) {
4600 // clear UF_TRACKED as well since tcp is now no longer tracked
4601 tcp
->c_bsdflags
&= ~UF_TRACKED
;
4602 (void) cat_update(hfsmp
, &tcp
->c_desc
, &tcp
->c_attr
, NULL
, NULL
);
4605 if (ffip
->document_id
== 0 && document_id
!= 0) {
4606 // 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);
4607 fcp
->c_bsdflags
|= UF_TRACKED
;
4608 ffip
->document_id
= document_id
;
4610 (void) cat_update(hfsmp
, &fcp
->c_desc
, &fcp
->c_attr
, NULL
, NULL
);
4612 add_fsevent(FSE_DOCID_CHANGED
, vfs_context_current(),
4613 FSE_ARG_DEV
, hfsmp
->hfs_raw_dev
,
4614 FSE_ARG_INO
, (ino64_t
)tcp
->c_fileid
, // src inode #
4615 FSE_ARG_INO
, (ino64_t
)fcp
->c_fileid
, // dst inode #
4616 FSE_ARG_INT32
, (uint32_t)ffip
->document_id
,
4619 } else if ((fcp
->c_bsdflags
& UF_TRACKED
) && should_save_docid_tombstone(ut
, fvp
, fcnp
)) {
4621 if (ut
->t_lastop_document_id
) {
4622 clear_tombstone_docid(ut
, hfsmp
, NULL
);
4624 save_tombstone(hfsmp
, fdvp
, fvp
, fcnp
, 0);
4626 //printf("RENAME: (dest-exists): saving tombstone doc-id %lld @ %s (ino %d)\n",
4627 // ut->t_lastop_document_id, ut->t_lastop_filename, fcp->c_desc.cd_cnid);
4633 * When fvp matches tvp they could be case variants
4634 * or matching hard links.
4637 if (!(fcp
->c_flag
& C_HARDLINK
)) {
4639 * If they're not hardlinks, then fvp == tvp must mean we
4640 * are using case-insensitive HFS because case-sensitive would
4641 * not use the same vnode for both. In this case we just update
4642 * the catalog for: a -> A
4644 goto skip_rm
; /* simple case variant */
4647 /* For all cases below, we must be using hardlinks */
4648 else if ((fdvp
!= tdvp
) ||
4649 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)) {
4651 * If the parent directories are not the same, AND the two items
4652 * are hardlinks, posix says to do nothing:
4653 * dir1/fred <-> dir2/bob and the op was mv dir1/fred -> dir2/bob
4654 * We just return 0 in this case.
4656 * If case sensitivity is on, and we are using hardlinks
4657 * then renaming is supposed to do nothing.
4658 * dir1/fred <-> dir2/FRED, and op == mv dir1/fred -> dir2/FRED
4660 goto out
; /* matching hardlinks, nothing to do */
4662 } else if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4663 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
4665 * If we get here, then the following must be true:
4666 * a) We are running case-insensitive HFS+.
4667 * b) Both paths 'fvp' and 'tvp' are in the same parent directory.
4668 * c) the two names are case-variants of each other.
4670 * In this case, we are really only dealing with a single catalog record
4671 * whose name is being updated.
4673 * op is dir1/fred -> dir1/FRED
4675 * We need to special case the name matching, because if
4676 * dir1/fred <-> dir1/bob were the two links, and the
4677 * op was dir1/fred -> dir1/bob
4678 * That would fail/do nothing.
4680 goto skip_rm
; /* case-variant hardlink in the same dir */
4682 goto out
; /* matching hardlink, nothing to do */
4687 if (vnode_isdir(tvp
)) {
4689 * hfs_removedir will eventually call hfs_removefile on the directory
4690 * we're working on, because only hfs_removefile does the renaming of the
4691 * item to the hidden directory. The directory will stay around in the
4692 * hidden directory with C_DELETED until it gets an inactive or a reclaim.
4693 * That way, we can destroy all of the EAs as needed and allow new ones to be
4696 error
= hfs_removedir(tdvp
, tvp
, tcnp
, HFSRM_SKIP_RESERVE
, 1);
4699 error
= hfs_removefile(tdvp
, tvp
, tcnp
, 0, HFSRM_SKIP_RESERVE
, 0, NULL
, 1);
4702 * If the destination file had a resource fork vnode, then we need to get rid of
4703 * its blocks when there are no more references to it. Because the call to
4704 * hfs_removefile above always open-unlinks things, we need to force an inactive/reclaim
4705 * on the resource fork vnode, in order to prevent block leaks. Otherwise,
4706 * the resource fork vnode could prevent the data fork vnode from going out of scope
4707 * because it holds a v_parent reference on it. So we mark it for termination
4708 * with a call to vnode_recycle. hfs_vnop_reclaim has been modified so that it
4709 * can clean up the blocks of open-unlinked files and resource forks.
4711 * We can safely call vnode_recycle on the resource fork because we took an iocount
4712 * reference on it at the beginning of the function.
4715 if ((error
== 0) && (tcp
->c_flag
& C_DELETED
) && (tvp_rsrc
)) {
4716 vnode_recycle(tvp_rsrc
);
4726 /* Mark 'tcp' as being deleted due to a rename */
4727 tcp
->c_flag
|= C_RENAMED
;
4730 * Aggressively mark tvp/tcp for termination to ensure that we recover all blocks
4731 * as quickly as possible.
4735 struct doc_tombstone
*ut
;
4736 ut
= get_uthread_doc_tombstone();
4739 // There is nothing at the destination. If the file being renamed is
4740 // tracked, save a "tombstone" of the document_id. If the file is
4741 // not a tracked file, then see if it needs to inherit a tombstone.
4743 // NOTE: we do not save a tombstone if the file being renamed begins
4744 // with "atmp" which is done to work-around AutoCad's bizarre
4745 // 5-step un-safe save behavior
4747 if (fcp
->c_bsdflags
& UF_TRACKED
) {
4748 if (should_save_docid_tombstone(ut
, fvp
, fcnp
)) {
4749 save_tombstone(hfsmp
, fdvp
, fvp
, fcnp
, 0);
4751 //printf("RENAME: (no dest): saving tombstone doc-id %lld @ %s (ino %d)\n",
4752 // ut->t_lastop_document_id, ut->t_lastop_filename, fcp->c_desc.cd_cnid);
4754 // intentionally do nothing
4756 } else if ( ut
->t_lastop_document_id
!= 0
4757 && tdvp
== ut
->t_lastop_parent
4758 && vnode_vid(tdvp
) == ut
->t_lastop_parent_vid
4759 && strcmp((char *)ut
->t_lastop_filename
, (char *)tcnp
->cn_nameptr
) == 0) {
4761 //printf("RENAME: %s (ino %d) inheriting doc-id %lld\n", tcnp->cn_nameptr, fcp->c_desc.cd_cnid, ut->t_lastop_document_id);
4762 struct FndrExtendedFileInfo
*fip
= (struct FndrExtendedFileInfo
*)((char *)&fcp
->c_attr
.ca_finderinfo
+ 16);
4763 fcp
->c_bsdflags
|= UF_TRACKED
;
4764 fip
->document_id
= ut
->t_lastop_document_id
;
4765 cat_update(hfsmp
, &fcp
->c_desc
, &fcp
->c_attr
, NULL
, NULL
);
4767 clear_tombstone_docid(ut
, hfsmp
, fcp
); // will send the docid-changed fsevent
4769 } else if (ut
->t_lastop_document_id
&& should_save_docid_tombstone(ut
, fvp
, fcnp
) && should_save_docid_tombstone(ut
, tvp
, tcnp
)) {
4770 // no match, clear the tombstone
4771 //printf("RENAME: clearing the tombstone %lld @ %s\n", ut->t_lastop_document_id, ut->t_lastop_filename);
4772 clear_tombstone_docid(ut
, hfsmp
, NULL
);
4778 * All done with tvp and fvp.
4780 * We also jump to this point if there was no destination observed during lookup and namei.
4781 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
4782 * competing thread from racing us and creating a file or dir at the destination of this rename
4783 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
4784 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
4785 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
4786 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
4787 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
4788 * will be swallowed and it will restart the operation.
4791 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4792 error
= cat_rename(hfsmp
, &from_desc
, &tdcp
->c_desc
, &to_desc
, &out_desc
);
4793 hfs_systemfile_unlock(hfsmp
, lockflags
);
4796 if (error
== EEXIST
) {
4802 /* Invalidate negative cache entries in the destination directory */
4803 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
4804 cache_purge_negatives(tdvp
);
4805 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
4808 /* Update cnode's catalog descriptor */
4809 replace_desc(fcp
, &out_desc
);
4810 fcp
->c_parentcnid
= tdcp
->c_fileid
;
4813 /* Now indicate this cnode needs to have date-added written to the finderinfo */
4814 fcp
->c_flag
|= C_NEEDS_DATEADDED
;
4815 (void) hfs_update (fvp
, 0);
4818 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_RMDIR
: VOL_RMFILE
,
4819 (fdcp
->c_cnid
== kHFSRootFolderID
));
4820 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_MKDIR
: VOL_MKFILE
,
4821 (tdcp
->c_cnid
== kHFSRootFolderID
));
4823 /* Update both parent directories. */
4825 if (vnode_isdir(fvp
)) {
4826 /* If the source directory has directory hard link
4827 * descendants, set the kHFSHasChildLinkBit in the
4828 * destination parent hierarchy
4830 if ((fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) &&
4831 !(tdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
)) {
4833 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
4835 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
4837 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp
->c_cnid
);
4841 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
4842 DEC_FOLDERCOUNT(hfsmp
, fdcp
->c_attr
);
4845 tdcp
->c_dirchangecnt
++;
4847 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)tdcp
->c_finderinfo
+ 16);
4848 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
4850 if (fdcp
->c_entries
> 0)
4852 fdcp
->c_dirchangecnt
++;
4853 fdcp
->c_touch_chgtime
= TRUE
;
4854 fdcp
->c_touch_modtime
= TRUE
;
4856 fdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4857 (void) hfs_update(fdvp
, 0);
4860 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)fdcp
->c_finderinfo
+ 16);
4861 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
4864 tdcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
4865 tdcp
->c_touch_chgtime
= TRUE
;
4866 tdcp
->c_touch_modtime
= TRUE
;
4868 tdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4869 (void) hfs_update(tdvp
, 0);
4871 /* Update the vnode's name now that the rename has completed. */
4872 vnode_update_identity(fvp
, tdvp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
,
4873 tcnp
->cn_hash
, (VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
));
4876 * At this point, we may have a resource fork vnode attached to the
4877 * 'from' vnode. If it exists, we will want to update its name, because
4878 * it contains the old name + _PATH_RSRCFORKSPEC. ("/..namedfork/rsrc").
4880 * Note that the only thing we need to update here is the name attached to
4881 * the vnode, since a resource fork vnode does not have a separate resource
4882 * cnode -- it's still 'fcp'.
4884 if (fcp
->c_rsrc_vp
) {
4885 char* rsrc_path
= NULL
;
4888 /* Create a new temporary buffer that's going to hold the new name */
4889 MALLOC_ZONE (rsrc_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
4890 len
= snprintf (rsrc_path
, MAXPATHLEN
, "%s%s", tcnp
->cn_nameptr
, _PATH_RSRCFORKSPEC
);
4891 len
= MIN(len
, MAXPATHLEN
);
4894 * vnode_update_identity will do the following for us:
4895 * 1) release reference on the existing rsrc vnode's name.
4896 * 2) copy/insert new name into the name cache
4897 * 3) attach the new name to the resource vnode
4898 * 4) update the vnode's vid
4900 vnode_update_identity (fcp
->c_rsrc_vp
, fvp
, rsrc_path
, len
, 0, (VNODE_UPDATE_NAME
| VNODE_UPDATE_CACHE
));
4902 /* Free the memory associated with the resource fork's name */
4903 FREE_ZONE (rsrc_path
, MAXPATHLEN
, M_NAMEI
);
4907 cat_postflight(hfsmp
, &cookie
, p
);
4910 hfs_end_transaction(hfsmp
);
4913 fdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4914 wakeup((caddr_t
)&fdcp
->c_flag
);
4916 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4917 wakeup((caddr_t
)&tdcp
->c_flag
);
4920 if (took_trunc_lock
) {
4921 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4924 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4926 /* Now vnode_put the resource forks vnodes if necessary */
4928 vnode_put(tvp_rsrc
);
4932 /* After tvp is removed the only acceptable error is EIO */
4933 if (error
&& tvp_deleted
)
4936 if (emit_rename
&& is_tracked
) {
4938 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_FAILED_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
4940 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_SUCCESS_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
4952 hfs_vnop_mkdir(struct vnop_mkdir_args
*ap
)
4954 /***** HACK ALERT ********/
4955 ap
->a_cnp
->cn_flags
|= MAKEENTRY
;
4956 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
4961 * Create a symbolic link.
4964 hfs_vnop_symlink(struct vnop_symlink_args
*ap
)
4966 struct vnode
**vpp
= ap
->a_vpp
;
4967 struct vnode
*dvp
= ap
->a_dvp
;
4968 struct vnode
*vp
= NULL
;
4969 struct cnode
*cp
= NULL
;
4970 struct hfsmount
*hfsmp
;
4971 struct filefork
*fp
;
4972 struct buf
*bp
= NULL
;
4978 /* HFS standard disks don't support symbolic links */
4979 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
)
4982 /* Check for empty target name */
4983 if (ap
->a_target
[0] == 0)
4986 hfsmp
= VTOHFS(dvp
);
4987 len
= strlen(ap
->a_target
);
4989 /* Check for free space */
4990 if (((u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
) < len
) {
4994 /* Create the vnode */
4995 ap
->a_vap
->va_mode
|= S_IFLNK
;
4996 if ((error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
))) {
5000 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
5006 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
5011 (void)hfs_getinoquota(cp
);
5014 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5020 * Allocate space for the link.
5022 * Since we're already inside a transaction,
5023 * tell hfs_truncate to skip the ubc_setsize.
5025 * Don't need truncate lock since a symlink is treated as a system file.
5027 error
= hfs_truncate(vp
, len
, IO_NOZEROFILL
, 1, 0, ap
->a_context
);
5029 /* On errors, remove the symlink file */
5032 * End the transaction so we don't re-take the cnode lock
5033 * below while inside a transaction (lock order violation).
5035 hfs_end_transaction(hfsmp
);
5037 /* hfs_removefile() requires holding the truncate lock */
5039 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
5040 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
5042 if (hfs_start_transaction(hfsmp
) != 0) {
5044 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
5048 (void) hfs_removefile(dvp
, vp
, ap
->a_cnp
, 0, 0, 0, NULL
, 0);
5049 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
5053 /* Write the link to disk */
5054 bp
= buf_getblk(vp
, (daddr64_t
)0, roundup((int)fp
->ff_size
, hfsmp
->hfs_physical_block_size
),
5057 journal_modify_block_start(hfsmp
->jnl
, bp
);
5059 datap
= (char *)buf_dataptr(bp
);
5060 bzero(datap
, buf_size(bp
));
5061 bcopy(ap
->a_target
, datap
, len
);
5064 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
5069 * We defered the ubc_setsize for hfs_truncate
5070 * since we were inside a transaction.
5072 * We don't need to drop the cnode lock here
5073 * since this is a symlink.
5075 ubc_setsize(vp
, len
);
5078 hfs_end_transaction(hfsmp
);
5079 if ((cp
!= NULL
) && (vp
!= NULL
)) {
5092 /* structures to hold a "." or ".." directory entry */
5093 struct hfs_stddotentry
{
5094 u_int32_t d_fileno
; /* unique file number */
5095 u_int16_t d_reclen
; /* length of this structure */
5096 u_int8_t d_type
; /* dirent file type */
5097 u_int8_t d_namlen
; /* len of filename */
5098 char d_name
[4]; /* "." or ".." */
5101 struct hfs_extdotentry
{
5102 u_int64_t d_fileno
; /* unique file number */
5103 u_int64_t d_seekoff
; /* seek offset (optional, used by servers) */
5104 u_int16_t d_reclen
; /* length of this structure */
5105 u_int16_t d_namlen
; /* len of filename */
5106 u_int8_t d_type
; /* dirent file type */
5107 u_char d_name
[3]; /* "." or ".." */
5111 struct hfs_stddotentry std
;
5112 struct hfs_extdotentry ext
;
5116 * hfs_vnop_readdir reads directory entries into the buffer pointed
5117 * to by uio, in a filesystem independent format. Up to uio_resid
5118 * bytes of data can be transferred. The data in the buffer is a
5119 * series of packed dirent structures where each one contains the
5120 * following entries:
5122 * u_int32_t d_fileno; // file number of entry
5123 * u_int16_t d_reclen; // length of this record
5124 * u_int8_t d_type; // file type
5125 * u_int8_t d_namlen; // length of string in d_name
5126 * char d_name[MAXNAMELEN+1]; // null terminated file name
5128 * The current position (uio_offset) refers to the next block of
5129 * entries. The offset can only be set to a value previously
5130 * returned by hfs_vnop_readdir or zero. This offset does not have
5131 * to match the number of bytes returned (in uio_resid).
5133 * In fact, the offset used by HFS is essentially an index (26 bits)
5134 * with a tag (6 bits). The tag is for associating the next request
5135 * with the current request. This enables us to have multiple threads
5136 * reading the directory while the directory is also being modified.
5138 * Each tag/index pair is tied to a unique directory hint. The hint
5139 * contains information (filename) needed to build the catalog b-tree
5140 * key for finding the next set of entries.
5142 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
5143 * do NOT synthesize entries for "." and "..".
5146 hfs_vnop_readdir(ap
)
5147 struct vnop_readdir_args
/* {
5153 vfs_context_t a_context;
5156 struct vnode
*vp
= ap
->a_vp
;
5157 uio_t uio
= ap
->a_uio
;
5159 struct hfsmount
*hfsmp
;
5160 directoryhint_t
*dirhint
= NULL
;
5161 directoryhint_t localhint
;
5166 user_addr_t user_start
= 0;
5167 user_size_t user_len
= 0;
5174 cnid_t cnid_hint
= 0;
5177 startoffset
= offset
= uio_offset(uio
);
5178 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5179 nfs_cookies
= extended
&& (ap
->a_flags
& VNODE_READDIR_REQSEEKOFF
);
5181 /* Sanity check the uio data. */
5182 if (uio_iovcnt(uio
) > 1)
5185 if (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
) {
5186 int compressed
= hfs_file_is_compressed(VTOC(vp
), 0); /* 0 == take the cnode lock */
5187 if (VTOCMP(vp
) != NULL
&& !compressed
) {
5188 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
5198 /* Note that the dirhint calls require an exclusive lock. */
5199 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
5202 /* Pick up cnid hint (if any). */
5204 cnid_hint
= (cnid_t
)(uio_offset(uio
) >> 32);
5205 uio_setoffset(uio
, uio_offset(uio
) & 0x00000000ffffffffLL
);
5206 if (cnid_hint
== INT_MAX
) { /* searching pass the last item */
5212 * Synthesize entries for "." and "..", unless the directory has
5213 * been deleted, but not closed yet (lazy delete in progress).
5215 if (offset
== 0 && !(cp
->c_flag
& C_DELETED
)) {
5216 hfs_dotentry_t dotentry
[2];
5220 struct hfs_extdotentry
*entry
= &dotentry
[0].ext
;
5222 entry
->d_fileno
= cp
->c_cnid
;
5223 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
5224 entry
->d_type
= DT_DIR
;
5225 entry
->d_namlen
= 1;
5226 entry
->d_name
[0] = '.';
5227 entry
->d_name
[1] = '\0';
5228 entry
->d_name
[2] = '\0';
5229 entry
->d_seekoff
= 1;
5232 entry
->d_fileno
= cp
->c_parentcnid
;
5233 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
5234 entry
->d_type
= DT_DIR
;
5235 entry
->d_namlen
= 2;
5236 entry
->d_name
[0] = '.';
5237 entry
->d_name
[1] = '.';
5238 entry
->d_name
[2] = '\0';
5239 entry
->d_seekoff
= 2;
5240 uiosize
= 2 * sizeof(struct hfs_extdotentry
);
5242 struct hfs_stddotentry
*entry
= &dotentry
[0].std
;
5244 entry
->d_fileno
= cp
->c_cnid
;
5245 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
5246 entry
->d_type
= DT_DIR
;
5247 entry
->d_namlen
= 1;
5248 *(int *)&entry
->d_name
[0] = 0;
5249 entry
->d_name
[0] = '.';
5252 entry
->d_fileno
= cp
->c_parentcnid
;
5253 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
5254 entry
->d_type
= DT_DIR
;
5255 entry
->d_namlen
= 2;
5256 *(int *)&entry
->d_name
[0] = 0;
5257 entry
->d_name
[0] = '.';
5258 entry
->d_name
[1] = '.';
5259 uiosize
= 2 * sizeof(struct hfs_stddotentry
);
5261 if ((error
= uiomove((caddr_t
)&dotentry
, uiosize
, uio
))) {
5267 /* If there are no real entries then we're done. */
5268 if (cp
->c_entries
== 0) {
5271 uio_setoffset(uio
, offset
);
5276 // We have to lock the user's buffer here so that we won't
5277 // fault on it after we've acquired a shared lock on the
5278 // catalog file. The issue is that you can get a 3-way
5279 // deadlock if someone else starts a transaction and then
5280 // tries to lock the catalog file but can't because we're
5281 // here and we can't service our page fault because VM is
5282 // blocked trying to start a transaction as a result of
5283 // trying to free up pages for our page fault. It's messy
5284 // but it does happen on dual-processors that are paging
5285 // heavily (see radar 3082639 for more info). By locking
5286 // the buffer up-front we prevent ourselves from faulting
5287 // while holding the shared catalog file lock.
5289 // Fortunately this and hfs_search() are the only two places
5290 // currently (10/30/02) that can fault on user data with a
5291 // shared lock on the catalog file.
5293 if (hfsmp
->jnl
&& uio_isuserspace(uio
)) {
5294 user_start
= uio_curriovbase(uio
);
5295 user_len
= uio_curriovlen(uio
);
5297 if ((error
= vslock(user_start
, user_len
)) != 0) {
5302 /* Convert offset into a catalog directory index. */
5303 index
= (offset
& HFS_INDEX_MASK
) - 2;
5304 tag
= offset
& ~HFS_INDEX_MASK
;
5306 /* Lock catalog during cat_findname and cat_getdirentries. */
5307 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5309 /* When called from NFS, try and resolve a cnid hint. */
5310 if (nfs_cookies
&& cnid_hint
!= 0) {
5311 if (cat_findname(hfsmp
, cnid_hint
, &localhint
.dh_desc
) == 0) {
5312 if ( localhint
.dh_desc
.cd_parentcnid
== cp
->c_fileid
) {
5313 localhint
.dh_index
= index
- 1;
5314 localhint
.dh_time
= 0;
5315 bzero(&localhint
.dh_link
, sizeof(localhint
.dh_link
));
5316 dirhint
= &localhint
; /* don't forget to release the descriptor */
5318 cat_releasedesc(&localhint
.dh_desc
);
5323 /* Get a directory hint (cnode must be locked exclusive) */
5324 if (dirhint
== NULL
) {
5325 dirhint
= hfs_getdirhint(cp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, 0);
5327 /* Hide tag from catalog layer. */
5328 dirhint
->dh_index
&= HFS_INDEX_MASK
;
5329 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
5330 dirhint
->dh_index
= -1;
5335 dirhint
->dh_threadhint
= cp
->c_dirthreadhint
;
5339 * If we have a non-zero index, there is a possibility that during the last
5340 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
5341 * then we don't want to return any new entries for the caller. Just return 0
5342 * items, mark the eofflag, and bail out. Because we won't have done any work, the
5343 * code at the end of the function will release the dirhint for us.
5345 * Don't forget to unlock the catalog lock on the way out, too.
5347 if (dirhint
->dh_desc
.cd_flags
& CD_EOF
) {
5350 uio_setoffset(uio
, startoffset
);
5351 hfs_systemfile_unlock (hfsmp
, lockflags
);
5357 /* Pack the buffer with dirent entries. */
5358 error
= cat_getdirentries(hfsmp
, cp
->c_entries
, dirhint
, uio
, ap
->a_flags
, &items
, &eofflag
);
5360 if (index
== 0 && error
== 0) {
5361 cp
->c_dirthreadhint
= dirhint
->dh_threadhint
;
5364 hfs_systemfile_unlock(hfsmp
, lockflags
);
5370 /* Get index to the next item */
5373 if (items
>= (int)cp
->c_entries
) {
5377 /* Convert catalog directory index back into an offset. */
5379 tag
= (++cp
->c_dirhinttag
) << HFS_INDEX_BITS
;
5380 uio_setoffset(uio
, (index
+ 2) | tag
);
5381 dirhint
->dh_index
|= tag
;
5384 cp
->c_touch_acctime
= TRUE
;
5386 if (ap
->a_numdirent
) {
5387 if (startoffset
== 0)
5389 *ap
->a_numdirent
= items
;
5394 vsunlock(user_start
, user_len
, TRUE
);
5396 /* If we didn't do anything then go ahead and dump the hint. */
5397 if ((dirhint
!= NULL
) &&
5398 (dirhint
!= &localhint
) &&
5399 (uio_offset(uio
) == startoffset
)) {
5400 hfs_reldirhint(cp
, dirhint
);
5403 if (ap
->a_eofflag
) {
5404 *ap
->a_eofflag
= eofflag
;
5406 if (dirhint
== &localhint
) {
5407 cat_releasedesc(&localhint
.dh_desc
);
5415 * Read contents of a symbolic link.
5418 hfs_vnop_readlink(ap
)
5419 struct vnop_readlink_args
/* {
5422 vfs_context_t a_context;
5425 struct vnode
*vp
= ap
->a_vp
;
5427 struct filefork
*fp
;
5430 if (!vnode_islnk(vp
))
5433 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
5438 /* Zero length sym links are not allowed */
5439 if (fp
->ff_size
== 0 || fp
->ff_size
> MAXPATHLEN
) {
5444 /* Cache the path so we don't waste buffer cache resources */
5445 if (fp
->ff_symlinkptr
== NULL
) {
5446 struct buf
*bp
= NULL
;
5448 MALLOC(fp
->ff_symlinkptr
, char *, fp
->ff_size
, M_TEMP
, M_WAITOK
);
5449 if (fp
->ff_symlinkptr
== NULL
) {
5453 error
= (int)buf_meta_bread(vp
, (daddr64_t
)0,
5454 roundup((int)fp
->ff_size
, VTOHFS(vp
)->hfs_physical_block_size
),
5455 vfs_context_ucred(ap
->a_context
), &bp
);
5459 if (fp
->ff_symlinkptr
) {
5460 FREE(fp
->ff_symlinkptr
, M_TEMP
);
5461 fp
->ff_symlinkptr
= NULL
;
5465 bcopy((char *)buf_dataptr(bp
), fp
->ff_symlinkptr
, (size_t)fp
->ff_size
);
5467 if (VTOHFS(vp
)->jnl
&& (buf_flags(bp
) & B_LOCKED
) == 0) {
5468 buf_markinvalid(bp
); /* data no longer needed */
5472 error
= uiomove((caddr_t
)fp
->ff_symlinkptr
, (int)fp
->ff_size
, ap
->a_uio
);
5475 * Keep track blocks read
5477 if ((VTOHFS(vp
)->hfc_stage
== HFC_RECORDING
) && (error
== 0)) {
5480 * If this file hasn't been seen since the start of
5481 * the current sampling period then start over.
5483 if (cp
->c_atime
< VTOHFS(vp
)->hfc_timebase
)
5484 VTOF(vp
)->ff_bytesread
= fp
->ff_size
;
5486 VTOF(vp
)->ff_bytesread
+= fp
->ff_size
;
5488 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
5489 // cp->c_touch_acctime = TRUE;
5499 * Get configurable pathname variables.
5502 hfs_vnop_pathconf(ap
)
5503 struct vnop_pathconf_args
/* {
5507 vfs_context_t a_context;
5511 int std_hfs
= (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
);
5512 switch (ap
->a_name
) {
5515 *ap
->a_retval
= HFS_LINK_MAX
;
5525 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
5529 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
5534 *ap
->a_retval
= PATH_MAX
; /* 1024 */
5537 *ap
->a_retval
= PIPE_BUF
;
5539 case _PC_CHOWN_RESTRICTED
:
5540 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
5543 *ap
->a_retval
= 200112; /* _POSIX_NO_TRUNC */
5545 case _PC_NAME_CHARS_MAX
:
5547 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
5551 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
5555 case _PC_CASE_SENSITIVE
:
5556 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_CASE_SENSITIVE
)
5561 case _PC_CASE_PRESERVING
:
5564 case _PC_FILESIZEBITS
:
5565 /* number of bits to store max file size */
5575 case _PC_XATTR_SIZE_BITS
:
5576 /* Number of bits to store maximum extended attribute size */
5577 *ap
->a_retval
= HFS_XATTR_SIZE_BITS
;
5588 * Update a cnode's on-disk metadata.
5590 * If waitfor is set, then wait for the disk write of
5591 * the node to complete.
5593 * The cnode must be locked exclusive
5596 hfs_update(struct vnode
*vp
, __unused
int waitfor
)
5598 struct cnode
*cp
= VTOC(vp
);
5600 struct cat_fork
*dataforkp
= NULL
;
5601 struct cat_fork
*rsrcforkp
= NULL
;
5602 struct cat_fork datafork
;
5603 struct cat_fork rsrcfork
;
5604 struct hfsmount
*hfsmp
;
5607 uint32_t tstate
= 0;
5612 if (((vnode_issystem(vp
) && (cp
->c_cnid
< kHFSFirstUserCatalogNodeID
))) ||
5613 hfsmp
->hfs_catalog_vp
== NULL
){
5616 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (cp
->c_mode
== 0)) {
5617 cp
->c_flag
&= ~C_MODIFIED
;
5618 cp
->c_touch_acctime
= 0;
5619 cp
->c_touch_chgtime
= 0;
5620 cp
->c_touch_modtime
= 0;
5623 if (kdebug_enable
) {
5624 if (cp
->c_touch_acctime
)
5625 tstate
|= DBG_HFS_UPDATE_ACCTIME
;
5626 if (cp
->c_touch_modtime
)
5627 tstate
|= DBG_HFS_UPDATE_MODTIME
;
5628 if (cp
->c_touch_chgtime
)
5629 tstate
|= DBG_HFS_UPDATE_CHGTIME
;
5631 if (cp
->c_flag
& C_MODIFIED
)
5632 tstate
|= DBG_HFS_UPDATE_MODIFIED
;
5633 if (cp
->c_flag
& C_FORCEUPDATE
)
5634 tstate
|= DBG_HFS_UPDATE_FORCE
;
5635 if (cp
->c_flag
& C_NEEDS_DATEADDED
)
5636 tstate
|= DBG_HFS_UPDATE_DATEADDED
;
5638 hfs_touchtimes(hfsmp
, cp
);
5640 /* Nothing to update. */
5641 if ((cp
->c_flag
& (C_MODIFIED
| C_FORCEUPDATE
)) == 0) {
5646 dataforkp
= &cp
->c_datafork
->ff_data
;
5648 rsrcforkp
= &cp
->c_rsrcfork
->ff_data
;
5651 * For delayed allocations updates are
5652 * postponed until an fsync or the file
5653 * gets written to disk.
5655 * Deleted files can defer meta data updates until inactive.
5657 * If we're ever called with the C_FORCEUPDATE flag though
5658 * we have to do the update.
5660 if (ISSET(cp
->c_flag
, C_FORCEUPDATE
) == 0 &&
5661 (ISSET(cp
->c_flag
, C_DELETED
) ||
5662 (dataforkp
&& cp
->c_datafork
->ff_unallocblocks
) ||
5663 (rsrcforkp
&& cp
->c_rsrcfork
->ff_unallocblocks
))) {
5664 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
5665 cp
->c_flag
|= C_MODIFIED
;
5670 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_START
, vp
, tstate
, 0, 0, 0);
5672 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5674 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_END
, vp
, tstate
, error
, -1, 0);
5679 * Modify the values passed to cat_update based on whether or not
5680 * the file has invalid ranges or borrowed blocks.
5685 /* copy the datafork into a temporary copy so we don't pollute the cnode's */
5686 bcopy(dataforkp
, &datafork
, sizeof(datafork
));
5687 dataforkp
= &datafork
;
5690 * If there are borrowed blocks, ensure that they are subtracted
5691 * from the total block count before writing the cnode entry to disk.
5692 * Only extents that have actually been marked allocated in the bitmap
5693 * should be reflected in the total block count for this fork.
5695 if (cp
->c_datafork
->ff_unallocblocks
!= 0) {
5696 // make sure that we don't assign a negative block count
5697 if (cp
->c_datafork
->ff_blocks
< cp
->c_datafork
->ff_unallocblocks
) {
5698 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
5699 cp
->c_datafork
->ff_blocks
, cp
->c_datafork
->ff_unallocblocks
);
5702 /* Also cap the LEOF to the total number of bytes that are allocated. */
5703 datafork
.cf_blocks
= (cp
->c_datafork
->ff_blocks
- cp
->c_datafork
->ff_unallocblocks
);
5704 datafork
.cf_size
= datafork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
5708 * For files with invalid ranges (holes) the on-disk
5709 * field representing the size of the file (cf_size)
5710 * must be no larger than the start of the first hole.
5711 * However, note that if the first invalid range exists
5712 * solely within borrowed blocks, then our LEOF and block
5713 * count should both be zero. As a result, set it to the
5714 * min of the current cf_size and the start of the first
5715 * invalid range, because it may have already been reduced
5716 * to zero by the borrowed blocks check above.
5718 if (!TAILQ_EMPTY(&cp
->c_datafork
->ff_invalidranges
)) {
5719 numbytes
= TAILQ_FIRST(&cp
->c_datafork
->ff_invalidranges
)->rl_start
;
5720 datafork
.cf_size
= MIN((numbytes
), (datafork
.cf_size
));
5725 * For resource forks with delayed allocations, make sure
5726 * the block count and file size match the number of blocks
5727 * actually allocated to the file on disk.
5729 if (rsrcforkp
&& (cp
->c_rsrcfork
->ff_unallocblocks
!= 0)) {
5730 bcopy(rsrcforkp
, &rsrcfork
, sizeof(rsrcfork
));
5731 rsrcfork
.cf_blocks
= (cp
->c_rsrcfork
->ff_blocks
- cp
->c_rsrcfork
->ff_unallocblocks
);
5732 rsrcfork
.cf_size
= rsrcfork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
5733 rsrcforkp
= &rsrcfork
;
5735 if (kdebug_enable
) {
5736 long dbg_parms
[NUMPARMS
];
5739 dbg_namelen
= NUMPARMS
* sizeof(long);
5740 vn_getpath(vp
, (char *)dbg_parms
, &dbg_namelen
);
5742 if (dbg_namelen
< (int)sizeof(dbg_parms
))
5743 memset((char *)dbg_parms
+ dbg_namelen
, 0, sizeof(dbg_parms
) - dbg_namelen
);
5745 kdebug_lookup_gen_events(dbg_parms
, dbg_namelen
, (void *)vp
, TRUE
);
5749 * Lock the Catalog b-tree file.
5751 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
5753 /* XXX - waitfor is not enforced */
5754 error
= cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, dataforkp
, rsrcforkp
);
5756 hfs_systemfile_unlock(hfsmp
, lockflags
);
5758 /* After the updates are finished, clear the flags */
5759 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
5761 hfs_end_transaction(hfsmp
);
5763 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_END
, vp
, tstate
, error
, 0, 0);
5769 * Allocate a new node
5770 * Note - Function does not create and return a vnode for whiteout creation.
5773 hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
,
5774 struct vnode_attr
*vap
, vfs_context_t ctx
)
5776 struct cnode
*cp
= NULL
;
5777 struct cnode
*dcp
= NULL
;
5779 struct hfsmount
*hfsmp
;
5780 struct cat_desc in_desc
, out_desc
;
5781 struct cat_attr attr
;
5784 int error
, started_tr
= 0;
5785 enum vtype vnodetype
;
5787 int newvnode_flags
= 0;
5788 u_int32_t gnv_flags
= 0;
5789 int protectable_target
= 0;
5793 struct cprotect
*entry
= NULL
;
5794 int32_t cp_class
= -1;
5795 if (VATTR_IS_ACTIVE(vap
, va_dataprotect_class
)) {
5796 cp_class
= (int32_t)vap
->va_dataprotect_class
;
5798 int protected_mount
= 0;
5802 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
5805 /* set the cnode pointer only after successfully acquiring lock */
5808 /* Don't allow creation of new entries in open-unlinked directories */
5809 if ((error
= hfs_checkdeleted(dcp
))) {
5814 dcp
->c_flag
|= C_DIR_MODIFICATION
;
5816 hfsmp
= VTOHFS(dvp
);
5820 out_desc
.cd_flags
= 0;
5821 out_desc
.cd_nameptr
= NULL
;
5823 vnodetype
= vap
->va_type
;
5824 if (vnodetype
== VNON
)
5826 mode
= MAKEIMODE(vnodetype
, vap
->va_mode
);
5828 if (S_ISDIR (mode
) || S_ISREG (mode
)) {
5829 protectable_target
= 1;
5833 /* Check if were out of usable disk space. */
5834 if ((hfs_freeblks(hfsmp
, 1) == 0) && (vfs_context_suser(ctx
) != 0)) {
5841 /* Setup the default attributes */
5842 bzero(&attr
, sizeof(attr
));
5843 attr
.ca_mode
= mode
;
5844 attr
.ca_linkcount
= 1;
5845 if (VATTR_IS_ACTIVE(vap
, va_rdev
)) {
5846 attr
.ca_rdev
= vap
->va_rdev
;
5848 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5849 VATTR_SET_SUPPORTED(vap
, va_create_time
);
5850 attr
.ca_itime
= vap
->va_create_time
.tv_sec
;
5852 attr
.ca_itime
= tv
.tv_sec
;
5855 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
5856 attr
.ca_itime
+= 3600; /* Same as what hfs_update does */
5859 attr
.ca_atime
= attr
.ca_ctime
= attr
.ca_mtime
= attr
.ca_itime
;
5860 attr
.ca_atimeondisk
= attr
.ca_atime
;
5861 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5862 VATTR_SET_SUPPORTED(vap
, va_flags
);
5863 attr
.ca_flags
= vap
->va_flags
;
5867 * HFS+ only: all files get ThreadExists
5868 * HFSX only: dirs get HasFolderCount
5870 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
5871 if (vnodetype
== VDIR
) {
5872 if (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
)
5873 attr
.ca_recflags
= kHFSHasFolderCountMask
;
5875 attr
.ca_recflags
= kHFSThreadExistsMask
;
5880 if (cp_fs_protected(hfsmp
->hfs_mp
)) {
5881 protected_mount
= 1;
5884 * On a content-protected HFS+/HFSX filesystem, files and directories
5885 * cannot be created without atomically setting/creating the EA that
5886 * contains the protection class metadata and keys at the same time, in
5887 * the same transaction. As a result, pre-set the "EAs exist" flag
5888 * on the cat_attr for protectable catalog record creations. This will
5889 * cause the cnode creation routine in hfs_getnewvnode to mark the cnode
5892 if ((protected_mount
) && (protectable_target
)) {
5893 attr
.ca_recflags
|= kHFSHasAttributesMask
;
5894 /* delay entering in the namecache */
5901 * Add the date added to the item. See above, as
5902 * all of the dates are set to the itime.
5904 hfs_write_dateadded (&attr
, attr
.ca_atime
);
5906 /* Initialize the gen counter to 1 */
5907 hfs_write_gencount(&attr
, (uint32_t)1);
5909 attr
.ca_uid
= vap
->va_uid
;
5910 attr
.ca_gid
= vap
->va_gid
;
5911 VATTR_SET_SUPPORTED(vap
, va_mode
);
5912 VATTR_SET_SUPPORTED(vap
, va_uid
);
5913 VATTR_SET_SUPPORTED(vap
, va_gid
);
5916 /* check to see if this node's creation would cause us to go over
5917 * quota. If so, abort this operation.
5919 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
5920 if ((error
= hfs_quotacheck(hfsmp
, 1, attr
.ca_uid
, attr
.ca_gid
,
5921 vfs_context_ucred(ctx
)))) {
5928 /* Tag symlinks with a type and creator. */
5929 if (vnodetype
== VLNK
) {
5930 struct FndrFileInfo
*fip
;
5932 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
5933 fip
->fdType
= SWAP_BE32(kSymLinkFileType
);
5934 fip
->fdCreator
= SWAP_BE32(kSymLinkCreator
);
5936 if (cnp
->cn_flags
& ISWHITEOUT
)
5937 attr
.ca_flags
|= UF_OPAQUE
;
5939 /* Setup the descriptor */
5940 in_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
5941 in_desc
.cd_namelen
= cnp
->cn_namelen
;
5942 in_desc
.cd_parentcnid
= dcp
->c_fileid
;
5943 in_desc
.cd_flags
= S_ISDIR(mode
) ? CD_ISDIR
: 0;
5944 in_desc
.cd_hint
= dcp
->c_childhint
;
5945 in_desc
.cd_encoding
= 0;
5949 * To preserve file creation atomicity with regards to the content protection EA,
5950 * we must create the file in the catalog and then write out its EA in the same
5953 * We only denote the target class in this EA; key generation is not completed
5954 * until the file has been inserted into the catalog and will be done
5955 * in a separate transaction.
5957 if ((protected_mount
) && (protectable_target
)) {
5958 error
= cp_setup_newentry(hfsmp
, dcp
, cp_class
, attr
.ca_mode
, &entry
);
5965 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5970 // have to also lock the attribute file because cat_create() needs
5971 // to check that any fileID it wants to use does not have orphaned
5972 // attributes in it.
5973 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
5976 /* Reserve some space in the Catalog file. */
5977 if ((error
= cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0))) {
5978 hfs_systemfile_unlock(hfsmp
, lockflags
);
5982 if ((error
= cat_acquire_cnid(hfsmp
, &new_id
))) {
5983 hfs_systemfile_unlock (hfsmp
, lockflags
);
5987 error
= cat_create(hfsmp
, new_id
, &in_desc
, &attr
, &out_desc
);
5989 /* Update the parent directory */
5990 dcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
5993 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)dcp
->c_finderinfo
+ 16);
5994 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
5996 if (vnodetype
== VDIR
) {
5997 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
5999 dcp
->c_dirchangecnt
++;
6001 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)dcp
->c_finderinfo
+ 16);
6002 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(OSSwapBigToHostInt32(extinfo
->write_gen_counter
) + 1);
6004 dcp
->c_ctime
= tv
.tv_sec
;
6005 dcp
->c_mtime
= tv
.tv_sec
;
6006 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
6010 * If we are creating a content protected file, now is when
6011 * we create the EA. We must create it in the same transaction
6012 * that creates the file. We can also guarantee that the file
6013 * MUST exist because we are still holding the catalog lock
6016 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
6017 error
= cp_setxattr (NULL
, entry
, hfsmp
, attr
.ca_fileid
, XATTR_CREATE
);
6022 * If we fail the EA creation, then we need to delete the file.
6023 * Luckily, we are still holding all of the right locks.
6025 delete_err
= cat_delete (hfsmp
, &out_desc
, &attr
);
6026 if (delete_err
== 0) {
6027 /* Update the parent directory */
6028 if (dcp
->c_entries
> 0)
6030 dcp
->c_dirchangecnt
++;
6031 dcp
->c_ctime
= tv
.tv_sec
;
6032 dcp
->c_mtime
= tv
.tv_sec
;
6033 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
6036 /* Emit EINVAL if we fail to create EA*/
6042 hfs_systemfile_unlock(hfsmp
, lockflags
);
6046 /* Invalidate negative cache entries in the directory */
6047 if (dcp
->c_flag
& C_NEG_ENTRIES
) {
6048 cache_purge_negatives(dvp
);
6049 dcp
->c_flag
&= ~C_NEG_ENTRIES
;
6052 hfs_volupdate(hfsmp
, vnodetype
== VDIR
? VOL_MKDIR
: VOL_MKFILE
,
6053 (dcp
->c_cnid
== kHFSRootFolderID
));
6056 // have to end the transaction here before we call hfs_getnewvnode()
6057 // because that can cause us to try and reclaim a vnode on a different
6058 // file system which could cause us to start a transaction which can
6059 // deadlock with someone on that other file system (since we could be
6060 // holding two transaction locks as well as various vnodes and we did
6061 // not obtain the locks on them in the proper order).
6063 // NOTE: this means that if the quota check fails or we have to update
6064 // the change time on a block-special device that those changes
6065 // will happen as part of independent transactions.
6068 hfs_end_transaction(hfsmp
);
6074 * At this point, we must have encountered success with writing the EA.
6075 * Destroy our temporary cprotect (which had no keys).
6078 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
6079 cp_entry_destroy (entry
);
6084 /* Do not create vnode for whiteouts */
6085 if (S_ISWHT(mode
)) {
6089 gnv_flags
|= GNV_CREATE
;
6091 gnv_flags
|= GNV_NOCACHE
;
6095 * Create a vnode for the object just created.
6097 * NOTE: Maintaining the cnode lock on the parent directory is important,
6098 * as it prevents race conditions where other threads want to look up entries
6099 * in the directory and/or add things as we are in the process of creating
6100 * the vnode below. However, this has the potential for causing a
6101 * double lock panic when dealing with shadow files on a HFS boot partition.
6102 * The panic could occur if we are not cleaning up after ourselves properly
6103 * when done with a shadow file or in the error cases. The error would occur if we
6104 * try to create a new vnode, and then end up reclaiming another shadow vnode to
6105 * create the new one. However, if everything is working properly, this should
6106 * be a non-issue as we would never enter that reclaim codepath.
6108 * The cnode is locked on successful return.
6110 error
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &out_desc
, gnv_flags
, &attr
,
6111 NULL
, &tvp
, &newvnode_flags
);
6117 struct doc_tombstone
*ut
;
6118 ut
= get_uthread_doc_tombstone();
6119 if ( ut
->t_lastop_document_id
!= 0
6120 && ut
->t_lastop_parent
== dvp
6121 && ut
->t_lastop_parent_vid
== vnode_vid(dvp
)
6122 && strcmp((char *)ut
->t_lastop_filename
, (char *)cp
->c_desc
.cd_nameptr
) == 0) {
6123 struct FndrExtendedDirInfo
*fip
= (struct FndrExtendedDirInfo
*)((char *)&cp
->c_attr
.ca_finderinfo
+ 16);
6125 //printf("CREATE: preserving doc-id %lld on %s\n", ut->t_lastop_document_id, ut->t_lastop_filename);
6126 fip
->document_id
= (uint32_t)(ut
->t_lastop_document_id
& 0xffffffff);
6128 cp
->c_bsdflags
|= UF_TRACKED
;
6129 // mark the cnode dirty
6130 cp
->c_flag
|= C_MODIFIED
| C_FORCEUPDATE
;
6132 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
6133 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
6135 (void) cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, NULL
, NULL
);
6137 hfs_systemfile_unlock (hfsmp
, lockflags
);
6138 (void) hfs_end_transaction(hfsmp
);
6141 clear_tombstone_docid(ut
, hfsmp
, cp
); // will send the docid-changed fsevent
6142 } else if (ut
->t_lastop_document_id
!= 0) {
6143 int len
= cnp
->cn_namelen
;
6145 len
= strlen(cnp
->cn_nameptr
);
6148 if (is_ignorable_temp_name(cnp
->cn_nameptr
, cnp
->cn_namelen
)) {
6149 // printf("CREATE: not clearing tombstone because %s is a temp name.\n", cnp->cn_nameptr);
6151 // Clear the tombstone because the thread is not recreating the same path
6152 // printf("CREATE: clearing tombstone because %s is NOT a temp name.\n", cnp->cn_nameptr);
6153 clear_tombstone_docid(ut
, hfsmp
, NULL
);
6161 * Now that we have a vnode-in-hand, generate keys for this namespace item.
6162 * If we fail to create the keys, then attempt to delete the item from the
6163 * namespace. If we can't delete the item, that's not desirable but also not fatal..
6164 * All of the places which deal with restoring/unwrapping keys must also be
6165 * prepared to encounter an entry that does not have keys.
6167 if ((protectable_target
) && (protected_mount
)) {
6168 struct cprotect
*keyed_entry
= NULL
;
6170 if (cp
->c_cpentry
== NULL
) {
6171 panic ("hfs_makenode: no cpentry for cnode (%p)", cp
);
6174 error
= cp_generate_keys (hfsmp
, cp
, cp
->c_cpentry
->cp_pclass
, &keyed_entry
);
6177 * Upon success, the keys were generated and written out.
6178 * Update the cp pointer in the cnode.
6180 cp_replace_entry (cp
, keyed_entry
);
6182 cache_enter (dvp
, tvp
, cnp
);
6186 /* If key creation OR the setxattr failed, emit EPERM to userland */
6190 * Beware! This slightly violates the lock ordering for the
6191 * cnode/vnode 'tvp'. Ordinarily, you must acquire the truncate lock
6192 * which guards file size changes before acquiring the normal cnode lock
6193 * and calling hfs_removefile on an item.
6195 * However, in this case, we are still holding the directory lock so
6196 * 'tvp' is not lookup-able and it was a newly created vnode so it
6197 * cannot have any content yet. The only reason we are initiating
6198 * the removefile is because we could not generate content protection keys
6199 * for this namespace item. Note also that we pass a '1' in the allow_dirs
6200 * argument for hfs_removefile because we may be creating a directory here.
6202 * All this to say that while it is technically a violation it is
6203 * impossible to race with another thread for this cnode so it is safe.
6205 int err
= hfs_removefile (dvp
, tvp
, cnp
, 0, 0, 1, NULL
, 0);
6207 printf("hfs_makenode: removefile failed (%d) for CP entry %p\n", err
, tvp
);
6210 /* Release the cnode lock and mark the vnode for termination */
6212 err
= vnode_recycle (tvp
);
6214 printf("hfs_makenode: vnode_recycle failed (%d) for CP entry %p\n", err
, tvp
);
6217 /* Drop the iocount on the new vnode to force reclamation/recycling */
6227 * Once we create this vnode, we need to initialize its quota data
6228 * structures, if necessary. We know that it is OK to just go ahead and
6229 * initialize because we've already validated earlier (through the hfs_quotacheck
6230 * function) to see if creating this cnode/vnode would cause us to go over quota.
6232 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
6234 /* cp could have been zeroed earlier */
6235 (void) hfs_getinoquota(cp
);
6241 cat_releasedesc(&out_desc
);
6245 * We may have jumped here in error-handling various situations above.
6246 * If we haven't already dumped the temporary CP used to initialize
6247 * the file atomically, then free it now. cp_entry_destroy should null
6248 * out the pointer if it was called already.
6251 cp_entry_destroy (entry
);
6257 * Make sure we release cnode lock on dcp.
6260 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
6261 wakeup((caddr_t
)&dcp
->c_flag
);
6265 if (error
== 0 && cp
!= NULL
) {
6269 hfs_end_transaction(hfsmp
);
6278 * hfs_vgetrsrc acquires a resource fork vnode corresponding to the cnode that is
6279 * found in 'vp'. The rsrc fork vnode is returned with the cnode locked and iocount
6280 * on the rsrc vnode.
6282 * *rvpp is an output argument for returning the pointer to the resource fork vnode.
6283 * In most cases, the resource fork vnode will not be set if we return an error.
6284 * However, if error_on_unlinked is set, we may have already acquired the resource fork vnode
6285 * before we discover the error (the file has gone open-unlinked). In this case only,
6286 * we may return a vnode in the output argument despite an error.
6288 * If can_drop_lock is set, then it is safe for this function to temporarily drop
6289 * and then re-acquire the cnode lock. We may need to do this, for example, in order to
6290 * acquire an iocount or promote our lock.
6292 * error_on_unlinked is an argument which indicates that we are to return an error if we
6293 * discover that the cnode has gone into an open-unlinked state ( C_DELETED or C_NOEXISTS)
6294 * is set in the cnode flags. This is only necessary if can_drop_lock is true, otherwise
6295 * there's really no reason to double-check for errors on the cnode.
6299 hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
, struct vnode
**rvpp
,
6300 int can_drop_lock
, int error_on_unlinked
)
6303 struct vnode
*dvp
= NULLVP
;
6304 struct cnode
*cp
= VTOC(vp
);
6307 int delete_status
= 0;
6309 if (vnode_vtype(vp
) == VDIR
) {
6314 * Need to check the status of the cnode to validate it hasn't gone
6315 * open-unlinked on us before we can actually do work with it.
6317 delete_status
= hfs_checkdeleted(cp
);
6318 if ((delete_status
) && (error_on_unlinked
)) {
6319 return delete_status
;
6323 /* Attempt to use existing vnode */
6324 if ((rvp
= cp
->c_rsrc_vp
)) {
6325 vid
= vnode_vid(rvp
);
6328 * It is not safe to hold the cnode lock when calling vnode_getwithvid()
6329 * for the alternate fork -- vnode_getwithvid() could deadlock waiting
6330 * for a VL_WANTTERM while another thread has an iocount on the alternate
6331 * fork vnode and is attempting to acquire the common cnode lock.
6333 * But it's also not safe to drop the cnode lock when we're holding
6334 * multiple cnode locks, like during a hfs_removefile() operation
6335 * since we could lock out of order when re-acquiring the cnode lock.
6337 * So we can only drop the lock here if its safe to drop it -- which is
6338 * most of the time with the exception being hfs_removefile().
6343 error
= vnode_getwithvid(rvp
, vid
);
6345 if (can_drop_lock
) {
6346 (void) hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
6349 * When we relinquished our cnode lock, the cnode could have raced
6350 * with a delete and gotten deleted. If the caller did not want
6351 * us to ignore open-unlinked files, then re-check the C_DELETED
6352 * state and see if we need to return an ENOENT here because the item
6353 * got deleted in the intervening time.
6355 if (error_on_unlinked
) {
6356 if ((delete_status
= hfs_checkdeleted(cp
))) {
6358 * If error == 0, this means that we succeeded in acquiring an iocount on the
6359 * rsrc fork vnode. However, if we're in this block of code, that means that we noticed
6360 * that the cnode has gone open-unlinked. In this case, the caller requested that we
6361 * not do any other work and return an errno. The caller will be responsible for
6362 * dropping the iocount we just acquired because we can't do it until we've released
6368 return delete_status
;
6373 * When our lock was relinquished, the resource fork
6374 * could have been recycled. Check for this and try
6377 if (error
== ENOENT
)
6381 const char * name
= (const char *)VTOC(vp
)->c_desc
.cd_nameptr
;
6384 printf("hfs_vgetrsrc: couldn't get resource"
6385 " fork for %s, vol=%s, err=%d\n", name
, hfsmp
->vcbVN
, error
);
6389 struct cat_fork rsrcfork
;
6390 struct componentname cn
;
6391 struct cat_desc
*descptr
= NULL
;
6392 struct cat_desc to_desc
;
6395 int newvnode_flags
= 0;
6398 * Make sure cnode lock is exclusive, if not upgrade it.
6400 * We assume that we were called from a read-only VNOP (getattr)
6401 * and that its safe to have the cnode lock dropped and reacquired.
6403 if (cp
->c_lockowner
!= current_thread()) {
6404 if (!can_drop_lock
) {
6408 * If the upgrade fails we lose the lock and
6409 * have to take the exclusive lock on our own.
6411 if (lck_rw_lock_shared_to_exclusive(&cp
->c_rwlock
) == FALSE
)
6412 lck_rw_lock_exclusive(&cp
->c_rwlock
);
6413 cp
->c_lockowner
= current_thread();
6417 * hfs_vgetsrc may be invoked for a cnode that has already been marked
6418 * C_DELETED. This is because we need to continue to provide rsrc
6419 * fork access to open-unlinked files. In this case, build a fake descriptor
6420 * like in hfs_removefile. If we don't do this, buildkey will fail in
6421 * cat_lookup because this cnode has no name in its descriptor. However,
6422 * only do this if the caller did not specify that they wanted us to
6423 * error out upon encountering open-unlinked files.
6426 if ((error_on_unlinked
) && (can_drop_lock
)) {
6427 if ((error
= hfs_checkdeleted(cp
))) {
6432 if ((cp
->c_flag
& C_DELETED
) && (cp
->c_desc
.cd_namelen
== 0)) {
6433 bzero (&to_desc
, sizeof(to_desc
));
6434 bzero (delname
, 32);
6435 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
6436 to_desc
.cd_nameptr
= (const u_int8_t
*) delname
;
6437 to_desc
.cd_namelen
= strlen(delname
);
6438 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
6439 to_desc
.cd_flags
= 0;
6440 to_desc
.cd_cnid
= cp
->c_cnid
;
6445 descptr
= &cp
->c_desc
;
6449 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
6452 * We call cat_idlookup (instead of cat_lookup) below because we can't
6453 * trust the descriptor in the provided cnode for lookups at this point.
6454 * Between the time of the original lookup of this vnode and now, the
6455 * descriptor could have gotten swapped or replaced. If this occurred,
6456 * the parent/name combo originally desired may not necessarily be provided
6457 * if we use the descriptor. Even worse, if the vnode represents
6458 * a hardlink, we could have removed one of the links from the namespace
6459 * but left the descriptor alone, since hfs_unlink does not invalidate
6460 * the descriptor in the cnode if other links still point to the inode.
6462 * Consider the following (slightly contrived) scenario:
6463 * /tmp/a <--> /tmp/b (hardlinks).
6464 * 1. Thread A: open rsrc fork on /tmp/b.
6465 * 1a. Thread A: does lookup, goes out to lunch right before calling getnamedstream.
6466 * 2. Thread B does 'mv /foo/b /tmp/b'
6467 * 2. Thread B succeeds.
6468 * 3. Thread A comes back and wants rsrc fork info for /tmp/b.
6470 * Even though the hardlink backing /tmp/b is now eliminated, the descriptor
6471 * is not removed/updated during the unlink process. So, if you were to
6472 * do a lookup on /tmp/b, you'd acquire an entirely different record's resource
6475 * As a result, we use the fileid, which should be invariant for the lifetime
6476 * of the cnode (possibly barring calls to exchangedata).
6478 * Addendum: We can't do the above for HFS standard since we aren't guaranteed to
6479 * have thread records for files. They were only required for directories. So
6480 * we need to do the lookup with the catalog name. This is OK since hardlinks were
6481 * never allowed on HFS standard.
6484 /* Get resource fork data */
6485 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) == 0) {
6486 error
= cat_idlookup (hfsmp
, cp
->c_fileid
, 0, 1, NULL
, NULL
, &rsrcfork
);
6491 * HFS standard only:
6493 * Get the resource fork for this item with a cat_lookup call, but do not
6494 * force a case lookup since HFS standard is case-insensitive only. We
6495 * don't want the descriptor; just the fork data here. If we tried to
6496 * do a ID lookup (via thread record -> catalog record), then we might fail
6497 * prematurely since, as noted above, thread records were not strictly required
6500 error
= cat_lookup (hfsmp
, descptr
, 1, 0, (struct cat_desc
*)NULL
,
6501 (struct cat_attr
*)NULL
, &rsrcfork
, NULL
);
6505 hfs_systemfile_unlock(hfsmp
, lockflags
);
6510 * Supply hfs_getnewvnode with a component name.
6513 if (descptr
->cd_nameptr
) {
6514 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
6515 cn
.cn_nameiop
= LOOKUP
;
6516 cn
.cn_flags
= ISLASTCN
| HASBUF
;
6517 cn
.cn_context
= NULL
;
6518 cn
.cn_pnlen
= MAXPATHLEN
;
6519 cn
.cn_nameptr
= cn
.cn_pnbuf
;
6522 cn
.cn_namelen
= snprintf(cn
.cn_nameptr
, MAXPATHLEN
,
6523 "%s%s", descptr
->cd_nameptr
,
6524 _PATH_RSRCFORKSPEC
);
6526 dvp
= vnode_getparent(vp
);
6527 error
= hfs_getnewvnode(hfsmp
, dvp
, cn
.cn_pnbuf
? &cn
: NULL
,
6528 descptr
, GNV_WANTRSRC
| GNV_SKIPLOCK
, &cp
->c_attr
,
6529 &rsrcfork
, &rvp
, &newvnode_flags
);
6533 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
6543 * Wrapper for special device reads
6547 struct vnop_read_args
/* {
6551 vfs_context_t a_context;
6557 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
6558 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6562 * Wrapper for special device writes
6566 struct vnop_write_args
/* {
6570 vfs_context_t a_context;
6574 * Set update and change flags.
6576 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
6577 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
6578 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6582 * Wrapper for special device close
6584 * Update the times on the cnode then do device close.
6588 struct vnop_close_args
/* {
6591 vfs_context_t a_context;
6594 struct vnode
*vp
= ap
->a_vp
;
6597 if (vnode_isinuse(ap
->a_vp
, 0)) {
6598 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
6600 hfs_touchtimes(VTOHFS(vp
), cp
);
6604 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6609 * Wrapper for fifo reads
6613 struct vnop_read_args
/* {
6617 vfs_context_t a_context;
6623 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
6624 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6628 * Wrapper for fifo writes
6632 struct vnop_write_args
/* {
6636 vfs_context_t a_context;
6640 * Set update and change flags.
6642 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
6643 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
6644 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6648 * Wrapper for fifo close
6650 * Update the times on the cnode then do device close.
6654 struct vnop_close_args
/* {
6657 vfs_context_t a_context;
6660 struct vnode
*vp
= ap
->a_vp
;
6663 if (vnode_isinuse(ap
->a_vp
, 1)) {
6664 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
6666 hfs_touchtimes(VTOHFS(vp
), cp
);
6670 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6677 * Getter for the document_id
6678 * the document_id is stored in FndrExtendedFileInfo/FndrExtendedDirInfo
6681 hfs_get_document_id_internal(const uint8_t *finderinfo
, mode_t mode
)
6683 u_int8_t
*finfo
= NULL
;
6684 u_int32_t doc_id
= 0;
6686 /* overlay the FinderInfo to the correct pointer, and advance */
6687 finfo
= ((uint8_t *)finderinfo
) + 16;
6689 if (S_ISDIR(mode
) || S_ISREG(mode
)) {
6690 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
6691 doc_id
= extinfo
->document_id
;
6692 } else if (S_ISDIR(mode
)) {
6693 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)((u_int8_t
*)finderinfo
+ 16);
6694 doc_id
= extinfo
->document_id
;
6701 /* getter(s) for document id */
6703 hfs_get_document_id(struct cnode
*cp
)
6705 return (hfs_get_document_id_internal((u_int8_t
*)cp
->c_finderinfo
,
6706 cp
->c_attr
.ca_mode
));
6709 /* If you have finderinfo and mode, you can use this */
6711 hfs_get_document_id_from_blob(const uint8_t *finderinfo
, mode_t mode
)
6713 return (hfs_get_document_id_internal(finderinfo
, mode
));
6717 * Synchronize a file's in-core state with that on disk.
6721 struct vnop_fsync_args
/* {
6724 vfs_context_t a_context;
6727 struct vnode
* vp
= ap
->a_vp
;
6730 /* Note: We check hfs flags instead of vfs mount flag because during
6731 * read-write update, hfs marks itself read-write much earlier than
6732 * the vfs, and hence won't result in skipping of certain writes like
6733 * zero'ing out of unused nodes, creation of hotfiles btree, etc.
6735 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
) {
6740 if ((error
= cp_handle_vnop(vp
, CP_WRITE_ACCESS
, 0)) != 0) {
6743 #endif /* CONFIG_PROTECT */
6746 * We need to allow ENOENT lock errors since unlink
6747 * systenm call can call VNOP_FSYNC during vclean.
6749 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
6753 error
= hfs_fsync(vp
, ap
->a_waitfor
, 0, vfs_context_proc(ap
->a_context
));
6755 hfs_unlock(VTOC(vp
));
6761 hfs_vnop_whiteout(ap
)
6762 struct vnop_whiteout_args
/* {
6763 struct vnode *a_dvp;
6764 struct componentname *a_cnp;
6766 vfs_context_t a_context;
6770 struct vnode
*vp
= NULL
;
6771 struct vnode_attr va
;
6772 struct vnop_lookup_args lookup_args
;
6773 struct vnop_remove_args remove_args
;
6774 struct hfsmount
*hfsmp
;
6776 hfsmp
= VTOHFS(ap
->a_dvp
);
6777 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
6782 switch (ap
->a_flags
) {
6789 VATTR_SET(&va
, va_type
, VREG
);
6790 VATTR_SET(&va
, va_mode
, S_IFWHT
);
6791 VATTR_SET(&va
, va_uid
, 0);
6792 VATTR_SET(&va
, va_gid
, 0);
6794 error
= hfs_makenode(ap
->a_dvp
, &vp
, ap
->a_cnp
, &va
, ap
->a_context
);
6795 /* No need to release the vnode as no vnode is created for whiteouts */
6799 lookup_args
.a_dvp
= ap
->a_dvp
;
6800 lookup_args
.a_vpp
= &vp
;
6801 lookup_args
.a_cnp
= ap
->a_cnp
;
6802 lookup_args
.a_context
= ap
->a_context
;
6804 error
= hfs_vnop_lookup(&lookup_args
);
6809 remove_args
.a_dvp
= ap
->a_dvp
;
6810 remove_args
.a_vp
= vp
;
6811 remove_args
.a_cnp
= ap
->a_cnp
;
6812 remove_args
.a_flags
= 0;
6813 remove_args
.a_context
= ap
->a_context
;
6815 error
= hfs_vnop_remove(&remove_args
);
6820 panic("hfs_vnop_whiteout: unknown operation (flag = %x)\n", ap
->a_flags
);
6827 int (**hfs_vnodeop_p
)(void *);
6829 #define VOPFUNC int (*)(void *)
6833 int (**hfs_std_vnodeop_p
) (void *);
6834 static int hfs_readonly_op (__unused
void* ap
) { return (EROFS
); }
6837 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
6838 * is for use with HFS standard to block out operations that would modify the file system
6841 struct vnodeopv_entry_desc hfs_standard_vnodeop_entries
[] = {
6842 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6843 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
6844 { &vnop_create_desc
, (VOPFUNC
)hfs_readonly_op
}, /* create (READONLY) */
6845 { &vnop_mknod_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mknod (READONLY) */
6846 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
6847 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
6848 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6849 { &vnop_setattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* setattr */
6850 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
6851 { &vnop_write_desc
, (VOPFUNC
)hfs_readonly_op
}, /* write (READONLY) */
6852 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
6853 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
6854 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
6855 { &vnop_exchange_desc
, (VOPFUNC
)hfs_readonly_op
}, /* exchange (READONLY)*/
6856 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
6857 { &vnop_fsync_desc
, (VOPFUNC
)hfs_readonly_op
}, /* fsync (READONLY) */
6858 { &vnop_remove_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove (READONLY) */
6859 { &vnop_link_desc
, (VOPFUNC
)hfs_readonly_op
}, /* link ( READONLLY) */
6860 { &vnop_rename_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rename (READONLY)*/
6861 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mkdir (READONLY) */
6862 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rmdir (READONLY) */
6863 { &vnop_symlink_desc
, (VOPFUNC
)hfs_readonly_op
}, /* symlink (READONLY) */
6864 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
6865 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
6866 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
6867 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6868 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6869 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
6870 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
6871 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6872 { &vnop_allocate_desc
, (VOPFUNC
)hfs_readonly_op
}, /* allocate (READONLY) */
6874 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
6876 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
6878 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_readonly_op
}, /* bwrite (READONLY) */
6879 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
6880 { &vnop_pageout_desc
,(VOPFUNC
) hfs_readonly_op
}, /* pageout (READONLY) */
6881 { &vnop_copyfile_desc
, (VOPFUNC
)hfs_readonly_op
}, /* copyfile (READONLY)*/
6882 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6883 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6884 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
6885 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6886 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* set xattr (READONLY) */
6887 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove xattr (READONLY) */
6888 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6889 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_readonly_op
}, /* whiteout (READONLY) */
6891 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
6892 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
6893 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
6895 { NULL
, (VOPFUNC
)NULL
}
6898 struct vnodeopv_desc hfs_std_vnodeop_opv_desc
=
6899 { &hfs_std_vnodeop_p
, hfs_standard_vnodeop_entries
};
6902 /* VNOP table for HFS+ */
6903 struct vnodeopv_entry_desc hfs_vnodeop_entries
[] = {
6904 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6905 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
6906 { &vnop_create_desc
, (VOPFUNC
)hfs_vnop_create
}, /* create */
6907 { &vnop_mknod_desc
, (VOPFUNC
)hfs_vnop_mknod
}, /* mknod */
6908 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
6909 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
6910 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6911 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
6912 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
6913 { &vnop_write_desc
, (VOPFUNC
)hfs_vnop_write
}, /* write */
6914 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
6915 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
6916 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
6917 { &vnop_exchange_desc
, (VOPFUNC
)hfs_vnop_exchange
}, /* exchange */
6918 { &vnop_mmap_desc
, (VOPFUNC
)hfs_vnop_mmap
}, /* mmap */
6919 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
6920 { &vnop_remove_desc
, (VOPFUNC
)hfs_vnop_remove
}, /* remove */
6921 { &vnop_link_desc
, (VOPFUNC
)hfs_vnop_link
}, /* link */
6922 { &vnop_rename_desc
, (VOPFUNC
)hfs_vnop_rename
}, /* rename */
6923 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_vnop_mkdir
}, /* mkdir */
6924 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_vnop_rmdir
}, /* rmdir */
6925 { &vnop_symlink_desc
, (VOPFUNC
)hfs_vnop_symlink
}, /* symlink */
6926 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
6927 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
6928 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
6929 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6930 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6931 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
6932 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
6933 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6934 { &vnop_allocate_desc
, (VOPFUNC
)hfs_vnop_allocate
}, /* allocate */
6936 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
6938 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
6940 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
}, /* bwrite */
6941 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
6942 { &vnop_pageout_desc
,(VOPFUNC
) hfs_vnop_pageout
}, /* pageout */
6943 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
6944 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6945 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6946 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
6947 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6948 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
6949 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
6950 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6951 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_vnop_whiteout
},
6953 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
6954 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_vnop_makenamedstream
},
6955 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_vnop_removenamedstream
},
6957 { NULL
, (VOPFUNC
)NULL
}
6960 struct vnodeopv_desc hfs_vnodeop_opv_desc
=
6961 { &hfs_vnodeop_p
, hfs_vnodeop_entries
};
6964 /* Spec Op vnop table for HFS+ */
6965 int (**hfs_specop_p
)(void *);
6966 struct vnodeopv_entry_desc hfs_specop_entries
[] = {
6967 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6968 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
6969 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
6970 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
6971 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
6972 { &vnop_close_desc
, (VOPFUNC
)hfsspec_close
}, /* close */
6973 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6974 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
6975 { &vnop_read_desc
, (VOPFUNC
)hfsspec_read
}, /* read */
6976 { &vnop_write_desc
, (VOPFUNC
)hfsspec_write
}, /* write */
6977 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
6978 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
6979 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
6980 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
6981 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
6982 { &vnop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
6983 { &vnop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
6984 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
6985 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
6986 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
6987 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
6988 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
6989 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
6990 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6991 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6992 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
6993 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
6994 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6995 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
6996 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
6997 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
6998 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
6999 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7000 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7001 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7002 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
7003 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
7004 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7005 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
7007 struct vnodeopv_desc hfs_specop_opv_desc
=
7008 { &hfs_specop_p
, hfs_specop_entries
};
7011 /* HFS+ FIFO VNOP table */
7012 int (**hfs_fifoop_p
)(void *);
7013 struct vnodeopv_entry_desc hfs_fifoop_entries
[] = {
7014 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
7015 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
7016 { &vnop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
7017 { &vnop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
7018 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
7019 { &vnop_close_desc
, (VOPFUNC
)hfsfifo_close
}, /* close */
7020 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
7021 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
7022 { &vnop_read_desc
, (VOPFUNC
)hfsfifo_read
}, /* read */
7023 { &vnop_write_desc
, (VOPFUNC
)hfsfifo_write
}, /* write */
7024 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
7025 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
7026 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
7027 { &vnop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
7028 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
7029 { &vnop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
7030 { &vnop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
7031 { &vnop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
7032 { &vnop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
7033 { &vnop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
7034 { &vnop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
7035 { &vnop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
7036 { &vnop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
7037 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
7038 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
7039 { &vnop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
7040 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
7041 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
7042 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
7043 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
7044 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
7045 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
7046 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
7047 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
7048 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
7049 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
7050 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
7051 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
7052 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
7053 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
7055 struct vnodeopv_desc hfs_fifoop_opv_desc
=
7056 { &hfs_fifoop_p
, hfs_fifoop_entries
};