1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
6 * Created by Or Haimovich on 20/3/18.
9 #include "lf_hfs_cnode.h"
11 #include "lf_hfs_vfsops.h"
12 #include "lf_hfs_chash.h"
13 #include "lf_hfs_vfsutils.h"
14 #include "lf_hfs_vnops.h"
15 #include "lf_hfs_logger.h"
16 #include "lf_hfs_utils.h"
17 #include "lf_hfs_btrees_internal.h"
18 #include "lf_hfs_readwrite_ops.h"
19 #include "lf_hfs_utils.h"
21 #include "lf_hfs_xattr.h"
22 #include "lf_hfs_link.h"
23 #include "lf_hfs_generic_buf.h"
26 hfs_reclaim_cnode(struct cnode
*cp
)
29 * If the descriptor has a name then release it
31 if ((cp
->c_desc
.cd_flags
& CD_HASBUF
) && (cp
->c_desc
.cd_nameptr
!= 0))
33 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
34 cp
->c_desc
.cd_namelen
= 0;
35 hfs_free((void*)cp
->c_desc
.cd_nameptr
);
36 cp
->c_desc
.cd_nameptr
= NULL
;
40 * We only call this function if we are in hfs_vnop_reclaim and
41 * attempting to reclaim a cnode with only one live fork. Because the vnode
42 * went through reclaim, any future attempts to use this item will have to
43 * go through lookup again, which will need to create a new vnode. Thus,
44 * destroying the locks below is safe.
47 lf_lck_rw_destroy(&cp
->c_rwlock
);
48 lf_cond_destroy(&cp
->c_cacsh_cond
);
49 lf_lck_rw_destroy(&cp
->c_truncatelock
);
55 * hfs_getnewvnode - get new default vnode
57 * The vnode is returned with an iocount and the cnode locked.
58 * The cnode of the parent vnode 'dvp' may or may not be locked, depending on
59 * the circumstances. The cnode in question (if acquiring the resource fork),
60 * may also already be locked at the time we enter this function.
62 * Note that there are both input and output flag arguments to this function.
63 * If one of the input flags (specifically, GNV_USE_VP), is set, then
64 * hfs_getnewvnode will use the parameter *vpp, which is traditionally only
65 * an output parameter, as both an input and output parameter. It will use
66 * the vnode provided in the output, and pass it to vnode_create with the
67 * proper flavor so that a new vnode is _NOT_ created on our behalf when
68 * we dispatch to VFS. This may be important in various HFS vnode creation
69 * routines, such a create or get-resource-fork, because we risk deadlock if
72 * Deadlock potential exists if jetsam is synchronously invoked while we are waiting
73 * for a vnode to be recycled in order to give it the identity we want. If jetsam
74 * happens to target a process for termination that is blocked in-kernel, waiting to
75 * acquire the cnode lock on our parent 'dvp', while our current thread has it locked,
76 * neither side will make forward progress and the watchdog timer will eventually fire.
77 * To prevent this, a caller of hfs_getnewvnode may choose to proactively force
78 * any necessary vnode reclamation/recycling while it is not holding any locks and
79 * thus not prone to deadlock. If this is the case, GNV_USE_VP will be set and
80 * the parameter will be used as described above.
83 * In circumstances when GNV_USE_VP is set, this function _MUST_ clean up and either consume
84 * or dispose of the provided vnode. We funnel all errors to a single return value so that
85 * if provided_vp is still non-NULL, then we will dispose of the vnode. This will occur in
86 * all error cases of this function -- anywhere we zero/NULL out the *vpp parameter. It may
87 * also occur if the current thread raced with another to create the same vnode, and we
88 * find the entry already present in the cnode hash.
92 hfs_getnewvnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, struct cat_desc
*descp
, int flags
, struct cat_attr
*attrp
, struct cat_fork
*forkp
, struct vnode
**vpp
, int *out_flags
)
94 struct mount
*mp
= HFSTOVFS(hfsmp
);
95 struct vnode
*vp
= NULL
;
97 struct vnode
*tvp
= NULL
;
98 struct cnode
*cp
= NULL
;
99 struct filefork
*fp
= NULL
;
100 struct vnode
*provided_vp
= NULL
;
101 struct vnode_fsparam vfsp
= {0};
102 enum vtype vtype
= IFTOVT(attrp
->ca_mode
);
105 int issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
106 int wantrsrc
= flags
& GNV_WANTRSRC
;;
107 int need_update_identity
= 0;
109 /* Zero out the out_flags */
112 if (flags
& GNV_USE_VP
)
114 /* Store the provided VP for later use */
118 /* Zero out the vpp regardless of provided input */
121 if (attrp
->ca_fileid
== 0)
128 if ( (vtype
== VBAD
) ||
129 ( (vtype
!= VDIR
&& forkp
&&
130 ( (attrp
->ca_blocks
< forkp
->cf_blocks
) || (howmany((uint64_t)forkp
->cf_size
, hfsmp
->blockSize
) > forkp
->cf_blocks
) ||
131 ( (vtype
== VLNK
) && ((uint64_t)forkp
->cf_size
> MAXPATHLEN
) ) ) ) ) )
133 /* Mark the FS as corrupt and bail out */
134 hfs_mark_inconsistent(hfsmp
, HFS_INCONSISTENCY_DETECTED
);
140 * Get a cnode (new or existing)
142 cp
= hfs_chash_getcnode(hfsmp
, attrp
->ca_fileid
, vpp
, wantrsrc
, (flags
& GNV_SKIPLOCK
), out_flags
, &hflags
);
145 * If the id is no longer valid for lookups we'll get back a NULL cp.
154 * We may have been provided a vnode via
155 * GNV_USE_VP. In this case, we have raced with
156 * a 2nd thread to create the target vnode. The provided
157 * vnode that was passed in will be dealt with at the
158 * end of the function, as we don't zero out the field
159 * until we're ready to pass responsibility to VFS.
164 * If we get a cnode/vnode pair out of hfs_chash_getcnode, then update the
165 * descriptor in the cnode as needed if the cnode represents a hardlink.
166 * We want the caller to get the most up-to-date copy of the descriptor
167 * as possible. However, we only do anything here if there was a valid vnode.
168 * If there isn't a vnode, then the cnode is brand new and needs to be initialized
169 * as it doesn't have a descriptor or cat_attr yet.
171 * If we are about to replace the descriptor with the user-supplied one, then validate
172 * that the descriptor correctly acknowledges this item is a hardlink. We could be
173 * subject to a race where the calling thread invoked cat_lookup, got a valid lookup
174 * result but the file was not yet a hardlink. With sufficient delay between there
175 * and here, we might accidentally copy in the raw inode ID into the descriptor in the
176 * call below. If the descriptor's CNID is the same as the fileID then it must
177 * not yet have been a hardlink when the lookup occurred.
180 if (!(cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)))
183 // If the bytes of the filename in the descp do not match the bytes in the
184 // cnp (and we're not looking up the resource fork), then we want to update
185 // the vnode identity to contain the bytes that HFS stores so that when an
186 // fsevent gets generated, it has the correct filename. otherwise daemons
187 // that match filenames produced by fsevents with filenames they have stored
188 // elsewhere (e.g. bladerunner, backupd, mds), the filenames will not match.
189 // See: <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
192 if (*vpp
&& cnp
&& cnp
->cn_nameptr
&& descp
&& descp
->cd_nameptr
&& strncmp((const char *)cnp
->cn_nameptr
, (const char *)descp
->cd_nameptr
, descp
->cd_namelen
) != 0)
194 vnode_update_identity (*vpp
, dvp
, (const char *)descp
->cd_nameptr
, descp
->cd_namelen
, 0, VNODE_UPDATE_NAME
);
197 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0)
199 /* If cnode is uninitialized, its c_attr will be zeroed out; cnids wont match. */
200 if ((descp
->cd_cnid
== cp
->c_attr
.ca_fileid
) && (attrp
->ca_linkcount
!= cp
->c_attr
.ca_linkcount
))
203 if ((flags
& GNV_SKIPLOCK
) == 0)
206 * Then we took the lock. Drop it before calling
207 * vnode_put, which may invoke hfs_vnop_inactive and need to take
208 * the cnode lock again.
214 * Emit ERECYCLE and GNV_CAT_ATTRCHANGED to
215 * force a re-drive in the lookup routine.
216 * Drop the iocount on the vnode obtained from
217 * chash_getcnode if needed.
226 * If we raced with VNOP_RECLAIM for this vnode, the hash code could
227 * have observed it after the c_vp or c_rsrc_vp fields had been torn down;
228 * the hash code peeks at those fields without holding the cnode lock because
229 * it needs to be fast. As a result, we may have set H_ATTACH in the chash
230 * call above. Since we're bailing out, unset whatever flags we just set, and
231 * wake up all waiters for this cnode.
235 hfs_chashwakeup(hfsmp
, cp
, hflags
);
238 *out_flags
= GNV_CAT_ATTRCHANGED
;
245 * Otherwise, CNID != fileid. Go ahead and copy in the new descriptor.
247 * Replacing the descriptor here is fine because we looked up the item without
248 * a vnode in hand before. If a vnode existed, its identity must be attached to this
249 * item. We are not susceptible to the lookup fastpath issue at this point.
251 replace_desc(cp
, descp
);
254 * This item was a hardlink, and its name needed to be updated. By replacing the
255 * descriptor above, we've now updated the cnode's internal representation of
256 * its link ID/CNID, parent ID, and its name. However, VFS must now be alerted
257 * to the fact that this vnode now has a new parent, since we cannot guarantee
258 * that the new link lived in the same directory as the alternative name for
261 if ((*vpp
!= NULL
) && (cnp
|| cp
->c_desc
.cd_nameptr
))
263 /* we could be requesting the rsrc of a hardlink file... */
264 if (cp
->c_desc
.cd_nameptr
)
266 // Update the identity with what we have stored on disk as the name of this file.
267 vnode_update_identity (*vpp
, dvp
, (const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
, 0, (VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
));
271 vnode_update_identity (*vpp
, dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, (VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
));
279 * At this point, we have performed hardlink and open-unlinked checks
280 * above. We have now validated the state of the vnode that was given back
281 * to us from the cnode hash code and find it safe to return.
290 * If this is a new cnode then initialize it.
292 if (ISSET(cp
->c_hflag
, H_ALLOC
))
294 lf_lck_rw_init(&cp
->c_truncatelock
);
296 /* Make sure its still valid (ie exists on disk). */
297 if (!(flags
& GNV_CREATE
))
300 if (!hfs_valid_cnode (hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
, attrp
, &error
))
302 hfs_chash_abort(hfsmp
, cp
);
303 if ((flags
& GNV_SKIPLOCK
) == 0)
308 hfs_reclaim_cnode(cp
);
311 * If we hit this case, that means that the entry was there in the catalog when
312 * we did a cat_lookup earlier. Think hfs_lookup. However, in between the time
313 * that we checked the catalog and the time we went to get a vnode/cnode for it,
314 * it had been removed from the namespace and the vnode totally reclaimed. As a result,
315 * it's not there in the catalog during the check in hfs_valid_cnode and we bubble out
316 * an ENOENT. To indicate to the caller that they should really double-check the
317 * entry (it could have been renamed over and gotten a new fileid), we mark a bit
318 * in the output flags.
322 *out_flags
= GNV_CAT_DELETED
;
328 * Also, we need to protect the cat_attr acquired during hfs_lookup and passed into
329 * this function as an argument because the catalog may have changed w.r.t hardlink
330 * link counts and the firstlink field. If that validation check fails, then let
331 * lookup re-drive itself to get valid/consistent data with the same failure condition below.
333 if (error
== ERECYCLE
)
335 *out_flags
= GNV_CAT_ATTRCHANGED
;
341 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
342 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
344 /* The name was inherited so clear descriptor state... */
345 descp
->cd_nameptr
= NULL
;
346 descp
->cd_namelen
= 0;
347 descp
->cd_flags
&= ~CD_HASBUF
;
350 if ( (vtype
== VREG
|| vtype
== VDIR
|| vtype
== VSOCK
|| vtype
== VFIFO
) &&
351 (descp
->cd_cnid
!= attrp
->ca_fileid
|| ISSET(attrp
->ca_recflags
, kHFSHasLinkChainMask
) ) )
353 cp
->c_flag
|= C_HARDLINK
;
357 * Fix-up dir link counts.
359 * Earlier versions of Leopard used ca_linkcount for posix
360 * nlink support (effectively the sub-directory count + 2).
361 * That is now accomplished using the ca_dircount field with
362 * the corresponding kHFSHasFolderCountMask flag.
364 * For directories the ca_linkcount is the true link count,
365 * tracking the number of actual hardlinks to a directory.
367 * We only do this if the mount has HFS_FOLDERCOUNT set;
368 * at the moment, we only set that for HFSX volumes.
370 if ( (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) && (vtype
== VDIR
) &&
371 (!(attrp
->ca_recflags
& kHFSHasFolderCountMask
)) && (cp
->c_attr
.ca_linkcount
> 1) )
373 if (cp
->c_attr
.ca_entries
== 0)
375 cp
->c_attr
.ca_dircount
= 0;
379 cp
->c_attr
.ca_dircount
= cp
->c_attr
.ca_linkcount
- 2;
382 cp
->c_attr
.ca_linkcount
= 1;
383 cp
->c_attr
.ca_recflags
|= kHFSHasFolderCountMask
;
384 if ( !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) )
386 cp
->c_flag
|= C_MODIFIED
;
390 /* Mark the output flag that we're vending a new cnode */
391 *out_flags
|= GNV_NEW_CNODE
;
396 if (cp
->c_vp
!= NULL
)
398 LFHFS_LOG(LEVEL_ERROR
, "hfs_getnewvnode: orphaned vnode (data)");
406 * Allocate and initialize a file fork...
408 fp
= hfs_malloc(sizeof(struct filefork
));
414 memset(fp
,0,sizeof(struct filefork
));
419 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
423 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
425 rl_init(&fp
->ff_invalidranges
);
426 fp
->ff_sysfileinfo
= 0;
430 if (cp
->c_rsrcfork
!= NULL
)
432 LFHFS_LOG(LEVEL_ERROR
, "hfs_getnewvnode: orphaned rsrc fork");
435 if (cp
->c_rsrc_vp
!= NULL
)
437 LFHFS_LOG(LEVEL_ERROR
, "hfs_getnewvnode: orphaned vnode (rsrc)");
441 cvpp
= &cp
->c_rsrc_vp
;
442 if ( (tvp
= cp
->c_vp
) != NULL
)
444 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
449 if (cp
->c_datafork
!= NULL
)
451 LFHFS_LOG(LEVEL_ERROR
, "hfs_getnewvnode: orphaned data fork");
454 if (cp
->c_vp
!= NULL
)
456 LFHFS_LOG(LEVEL_ERROR
, "hfs_getnewvnode: orphaned vnode (data)");
462 if ( (tvp
= cp
->c_rsrc_vp
) != NULL
)
464 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
470 vfsp
.vnfs_vtype
= vtype
;
471 vfsp
.vnfs_str
= "hfs";
472 if ((cp
->c_flag
& C_HARDLINK
) && (vtype
== VDIR
))
474 vfsp
.vnfs_dvp
= NULL
; /* no parent for me! */
475 vfsp
.vnfs_cnp
= NULL
; /* no name for me! */
482 vfsp
.vnfs_cnp
= hfs_malloc(sizeof(struct componentname
));
483 if (vfsp
.vnfs_cnp
== NULL
)
493 memcpy((void*) vfsp
.vnfs_cnp
, (void*)cnp
, sizeof(struct componentname
));
494 vfsp
.vnfs_cnp
->cn_nameptr
= lf_hfs_utils_allocate_and_copy_string( (char*) cnp
->cn_nameptr
, cnp
->cn_namelen
);
497 // Incase of ScanID of hardlinks, take the filename from the cnode
498 if (cp
&& cp
->c_desc
.cd_nameptr
) {
499 vfsp
.vnfs_cnp
= hfs_malloc(sizeof(struct componentname
));
500 if (vfsp
.vnfs_cnp
== NULL
) {
501 if (fp
) hfs_free(fp
);
505 bzero(vfsp
.vnfs_cnp
, sizeof(struct componentname
));
506 vfsp
.vnfs_cnp
->cn_nameptr
= lf_hfs_utils_allocate_and_copy_string( (char*) cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
);
507 vfsp
.vnfs_cnp
->cn_namelen
= cp
->c_desc
.cd_namelen
;
512 vfsp
.vnfs_fsnode
= cp
;
517 vfsp
.vnfs_filesize
= forkp
->cf_size
;
521 vfsp
.vnfs_filesize
= 0;
524 if (cnp
&& cnp
->cn_nameptr
&& cp
->c_desc
.cd_nameptr
&& strncmp((const char *)cnp
->cn_nameptr
, (const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) != 0)
527 // We don't want VFS to add an entry for this vnode because the name in the
528 // cnp does not match the bytes stored on disk for this file. Instead we'll
529 // update the identity later after the vnode is created and we'll do so with
530 // the correct bytes for this filename. For more details, see:
531 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
533 need_update_identity
= 1;
537 /* Tag system files */
538 vfsp
.vnfs_marksystem
= issystemfile
;
540 /* Tag root directory */
541 if (descp
->cd_cnid
== kHFSRootFolderID
)
543 vfsp
.vnfs_markroot
= 1;
547 vfsp
.vnfs_markroot
= 0;
551 * If provided_vp was non-NULL, then it is an already-allocated (but not
552 * initialized) vnode. We simply need to initialize it to this identity.
553 * If it was NULL, then assume that we need to call vnode_create with the
554 * normal arguments/types.
560 * After we assign the value of provided_vp into 'vp' (so that it can be
561 * mutated safely by vnode_initialize), we can NULL it out. At this point, the disposal
562 * and handling of the provided vnode will be the responsibility of VFS, which will
563 * clean it up and vnode_put it properly if vnode_initialize fails.
566 retval
= vnode_initialize (sizeof(struct vnode_fsparam
), &vfsp
, &vp
);
567 /* See error handling below for resolving provided_vp */
571 /* Do a standard vnode_create */
572 retval
= vnode_create (sizeof(struct vnode_fsparam
), &vfsp
, &vp
);
576 * We used a local variable to hold the result of vnode_create/vnode_initialize so that
577 * on error cases in vnode_create we won't accidentally harm the cnode's fields
582 /* Clean up if we encountered an error */
584 if (fp
== cp
->c_datafork
)
585 cp
->c_datafork
= NULL
;
587 cp
->c_rsrcfork
= NULL
;
592 * If this is a newly created cnode or a vnode reclaim
593 * occurred during the attachment, then cleanup the cnode.
595 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
))
597 hfs_chash_abort(hfsmp
, cp
);
599 if ((flags
& GNV_SKIPLOCK
) == 0)
603 hfs_reclaim_cnode(cp
);
607 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_ATTACH
);
608 if ((flags
& GNV_SKIPLOCK
) == 0)
617 /* If no error, then assign the value into the cnode's fields */
620 if (cp
->c_flag
& C_HARDLINK
)
622 //TBD - this set is for vfs -> since we have the C_HARDLINK
623 // currently disable this set.
624 //vnode_setmultipath(vp);
627 if (vp
&& need_update_identity
)
630 // As above, update the name of the vnode if the bytes stored in hfs do not match
631 // the bytes in the cnp. See this radar:
632 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
635 vnode_update_identity (vp
, dvp
, (const char *)cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
, 0, VNODE_UPDATE_NAME
);
638 * Tag resource fork vnodes as needing an VNOP_INACTIVE
639 * so that any deferred removes (open unlinked files)
640 * have the chance to process the resource fork.
642 if (vp
&& VNODE_IS_RSRC(vp
))
646 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_ATTACH
);
648 SET_NODE_AS_VALID(vp
);
655 /* Release our empty vnode if it was not used */
656 vnode_rele (provided_vp
);
662 * Check ordering of two cnodes. Return true if they are are in-order.
665 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
669 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
671 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
674 * Locking order is cnode address order.
680 * Acquire 4 cnode locks.
681 * - locked in cnode address order (lesser address first).
682 * - all or none of the locks are taken
683 * - only one lock taken per cnode (dup cnodes are skipped)
684 * - some of the cnode pointers may be null
687 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
688 struct cnode
*cp4
, enum hfs_locktype locktype
, struct cnode
**error_cnode
)
692 struct cnode
* list
[4];
700 if (hfs_isordered(cp1
, cp2
))
702 a
[0] = cp1
; a
[1] = cp2
;
705 a
[0] = cp2
; a
[1] = cp1
;
707 if (hfs_isordered(cp3
, cp4
)) {
708 b
[0] = cp3
; b
[1] = cp4
;
710 b
[0] = cp4
; b
[1] = cp3
;
712 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
713 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
716 * Build the lock list, skipping over duplicates
718 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
719 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
720 if (k
== 0 || tmp
!= list
[k
-1])
725 * Now we can lock using list[0 - k].
726 * Skip over NULL entries.
728 for (i
= 0; i
< k
; ++i
) {
730 if ((error
= hfs_lock(list
[i
], locktype
, HFS_LOCK_DEFAULT
))) {
731 /* Only stuff error_cnode if requested */
733 *error_cnode
= list
[i
];
735 /* Drop any locks we acquired. */
747 * Unlock a group of cnodes.
750 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
752 struct cnode
* list
[4];
760 for (i
= 0; i
< k
; ++i
) {
769 for (i
= 0; i
< k
; ++i
) {
778 for (i
= 0; i
< k
; ++i
) {
788 * N.B. If you add any failure cases, *make* sure hfs_lock_always works
791 hfs_lock(struct cnode
*cp
, enum hfs_locktype locktype
, enum hfs_lockflags flags
)
793 pthread_t thread
= pthread_self();
795 if (cp
->c_lockowner
== thread
)
798 * Only the extents and bitmap files support lock recursion
799 * here. The other system files support lock recursion in
800 * hfs_systemfile_lock. Eventually, we should change to
801 * handle recursion solely in hfs_systemfile_lock.
803 if ((cp
->c_fileid
== kHFSExtentsFileID
) || (cp
->c_fileid
== kHFSAllocationFileID
))
805 cp
->c_syslockcount
++;
809 LFHFS_LOG(LEVEL_ERROR
, "hfs_lock: locking against myself!");
813 else if (locktype
== HFS_SHARED_LOCK
)
815 lf_lck_rw_lock_shared(&cp
->c_rwlock
);
816 cp
->c_lockowner
= HFS_SHARED_OWNER
;
818 else if (locktype
== HFS_TRY_EXCLUSIVE_LOCK
)
820 if (!lf_lck_rw_try_lock(&cp
->c_rwlock
, LCK_RW_TYPE_EXCLUSIVE
))
822 cp
->c_lockowner
= thread
;
824 /* Only the extents and bitmap files support lock recursion. */
825 if ((cp
->c_fileid
== kHFSExtentsFileID
) || (cp
->c_fileid
== kHFSAllocationFileID
))
827 cp
->c_syslockcount
= 1;
836 { /* HFS_EXCLUSIVE_LOCK */
837 lf_lck_rw_lock_exclusive(&cp
->c_rwlock
);
838 cp
->c_lockowner
= thread
;
839 /* Only the extents and bitmap files support lock recursion. */
840 if ((cp
->c_fileid
== kHFSExtentsFileID
) || (cp
->c_fileid
== kHFSAllocationFileID
))
842 cp
->c_syslockcount
= 1;
847 * Skip cnodes for regular files that no longer exist
848 * (marked deleted, catalog entry gone).
850 if (((flags
& HFS_LOCK_ALLOW_NOEXISTS
) == 0) && ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) && (cp
->c_flag
& C_NOEXISTS
))
862 hfs_unlock(struct cnode
*cp
)
864 u_int32_t c_flag
= 0;
867 * Only the extents and bitmap file's support lock recursion.
869 if ((cp
->c_fileid
== kHFSExtentsFileID
) || (cp
->c_fileid
== kHFSAllocationFileID
))
871 if (--cp
->c_syslockcount
> 0)
877 pthread_t thread
= pthread_self();
879 if (cp
->c_lockowner
== thread
)
883 // If we have the truncate lock, we must defer the puts
884 if (cp
->c_truncatelockowner
== thread
)
886 if (ISSET(c_flag
, C_NEED_DVNODE_PUT
)
887 && !cp
->c_need_dvnode_put_after_truncate_unlock
)
889 CLR(c_flag
, C_NEED_DVNODE_PUT
);
890 cp
->c_need_dvnode_put_after_truncate_unlock
= true;
892 if (ISSET(c_flag
, C_NEED_RVNODE_PUT
)
893 && !cp
->c_need_rvnode_put_after_truncate_unlock
)
895 CLR(c_flag
, C_NEED_RVNODE_PUT
);
896 cp
->c_need_rvnode_put_after_truncate_unlock
= true;
900 CLR(cp
->c_flag
, (C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
| C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
));
902 cp
->c_lockowner
= NULL
;
903 lf_lck_rw_unlock_exclusive(&cp
->c_rwlock
);
907 cp
->c_lockowner
= NULL
;
908 lf_lck_rw_unlock_shared(&cp
->c_rwlock
);
915 * This function is used to validate data that is stored in-core against what is contained
916 * in the catalog. Common uses include validating that the parent-child relationship still exist
917 * for a specific directory entry (guaranteeing it has not been renamed into a different spot) at
918 * the point of the check.
921 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
, struct cat_attr
*cattr
, int *error
)
923 struct cat_attr attr
;
924 struct cat_desc cndesc
;
927 /* System files are always valid */
928 if (cnid
< kHFSFirstUserCatalogNodeID
)
934 /* XXX optimization: check write count in dvp */
935 int lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
940 struct cat_fork fork
;
941 bzero(&cndesc
, sizeof(cndesc
));
942 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
943 cndesc
.cd_namelen
= cnp
->cn_namelen
;
944 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_fileid
;
945 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
948 * We have to be careful when calling cat_lookup. The result argument
949 * 'attr' may get different results based on whether or not you ask
950 * for the filefork to be supplied as output. This is because cat_lookupbykey
951 * will attempt to do basic validation/smoke tests against the resident
952 * extents if there are no overflow extent records, but it needs someplace
953 * in memory to store the on-disk fork structures.
955 * Since hfs_lookup calls cat_lookup with a filefork argument, we should
956 * do the same here, to verify that block count differences are not
957 * due to calling the function with different styles. cat_lookupbykey
958 * will request the volume be fsck'd if there is true on-disk corruption
959 * where the number of blocks does not match the number generated by
960 * summing the number of blocks in the resident extents.
962 lookup
= cat_lookup (hfsmp
, &cndesc
, 0, NULL
, &attr
, &fork
, NULL
);
964 if ((lookup
== 0) && (cnid
== attr
.ca_fileid
))
974 * In hfs_getnewvnode, we may encounter a time-of-check vs. time-of-vnode creation
975 * race. Specifically, if there is no vnode/cnode pair for the directory entry
976 * being looked up, we have to go to the catalog. But since we don't hold any locks (aside
977 * from the dvp in 'shared' mode) there is nothing to protect us against the catalog record
978 * changing in between the time we do the cat_lookup there and the time we re-grab the
979 * catalog lock above to do another cat_lookup.
981 * However, we need to check more than just the CNID and parent-child name relationships above.
982 * Hardlinks can suffer the same race in the following scenario: Suppose we do a
983 * cat_lookup, and find a leaf record and a raw inode for a hardlink. Now, we have
984 * the cat_attr in hand (passed in above). But in between then and now, the vnode was
985 * created by a competing hfs_getnewvnode call, and is manipulated and reclaimed before we get
986 * a chance to do anything. This is possible if there are a lot of threads thrashing around
987 * with the cnode hash. In this case, if we don't check/validate the cat_attr in-hand, we will
988 * blindly stuff it into the cnode, which will make the in-core data inconsistent with what is
989 * on disk. So validate the cat_attr below, if required. This race cannot happen if the cnode/vnode
990 * already exists, as it does in the case of rename and delete.
992 if (stillvalid
&& cattr
!= NULL
)
994 if (cattr
->ca_linkcount
!= attr
.ca_linkcount
)
1001 if (cattr
->ca_union1
.cau_linkref
!= attr
.ca_union1
.cau_linkref
)
1008 if (cattr
->ca_union3
.cau_firstlink
!= attr
.ca_union3
.cau_firstlink
)
1014 if (cattr
->ca_union2
.cau_blocks
!= attr
.ca_union2
.cau_blocks
)
1024 if (cat_idlookup(hfsmp
, cnid
, 0, 0, NULL
, NULL
, NULL
) == 0)
1036 hfs_systemfile_unlock(hfsmp
, lockflags
);
1038 return (stillvalid
);
1042 * Protect a cnode against a truncation.
1044 * Used mainly by read/write since they don't hold the
1045 * cnode lock across calls to the cluster layer.
1047 * The process doing a truncation must take the lock
1048 * exclusive. The read/write processes can take it
1049 * shared. The locktype argument is the same as supplied to
1053 hfs_lock_truncate(struct cnode
*cp
, enum hfs_locktype locktype
, enum hfs_lockflags flags
)
1055 pthread_t thread
= pthread_self();
1057 if (cp
->c_truncatelockowner
== thread
) {
1059 * Ignore grabbing the lock if it the current thread already
1060 * holds exclusive lock.
1062 * This is needed on the hfs_vnop_pagein path where we need to ensure
1063 * the file does not change sizes while we are paging in. However,
1064 * we may already hold the lock exclusive due to another
1065 * VNOP from earlier in the call stack. So if we already hold
1066 * the truncate lock exclusive, allow it to proceed, but ONLY if
1067 * it's in the recursive case.
1069 if ((flags
& HFS_LOCK_SKIP_IF_EXCLUSIVE
) == 0)
1071 LFHFS_LOG(LEVEL_ERROR
, "hfs_lock_truncate: cnode %p locked!", cp
);
1074 } else if (locktype
== HFS_SHARED_LOCK
) {
1075 lf_lck_rw_lock_shared(&cp
->c_truncatelock
);
1076 cp
->c_truncatelockowner
= HFS_SHARED_OWNER
;
1077 } else { /* HFS_EXCLUSIVE_LOCK */
1078 lf_lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1079 cp
->c_truncatelockowner
= thread
;
1084 * Unlock the truncate lock, which protects against size changes.
1086 * If HFS_LOCK_SKIP_IF_EXCLUSIVE flag was set, it means that a previous
1087 * hfs_lock_truncate() might have skipped grabbing a lock because
1088 * the current thread was already holding the lock exclusive and
1089 * we may need to return from this function without actually unlocking
1090 * the truncate lock.
1093 hfs_unlock_truncate(struct cnode
*cp
, enum hfs_lockflags flags
)
1095 pthread_t thread
= pthread_self();
1098 * If HFS_LOCK_SKIP_IF_EXCLUSIVE is set in the flags AND the current
1099 * lock owner of the truncate lock is our current thread, then
1100 * we must have skipped taking the lock earlier by in
1101 * hfs_lock_truncate() by setting HFS_LOCK_SKIP_IF_EXCLUSIVE in the
1102 * flags (as the current thread was current lock owner).
1104 * If HFS_LOCK_SKIP_IF_EXCLUSIVE is not set (most of the time) then
1105 * we check the lockowner field to infer whether the lock was taken
1106 * exclusively or shared in order to know what underlying lock
1109 if (flags
& HFS_LOCK_SKIP_IF_EXCLUSIVE
) {
1110 if (cp
->c_truncatelockowner
== thread
) {
1115 /* HFS_LOCK_EXCLUSIVE */
1116 if (thread
== cp
->c_truncatelockowner
) {
1117 // vnode_t vp = NULL, rvp = NULL;
1120 * If there are pending set sizes, the cnode lock should be dropped
1123 hfs_assert(!(cp
->c_lockowner
== thread
1124 && ISSET(cp
->c_flag
, C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
)));
1126 // if (cp->c_need_dvnode_put_after_truncate_unlock) {
1128 // cp->c_need_dvnode_put_after_truncate_unlock = false;
1130 // if (cp->c_need_rvnode_put_after_truncate_unlock) {
1131 // rvp = cp->c_rsrc_vp;
1132 // cp->c_need_rvnode_put_after_truncate_unlock = false;
1135 cp
->c_truncatelockowner
= NULL
;
1136 lf_lck_rw_unlock_exclusive(&cp
->c_truncatelock
);
1138 // // Do the puts now
1144 { /* HFS_LOCK_SHARED */
1145 lf_lck_rw_unlock_shared(&cp
->c_truncatelock
);
1150 * Lock a pair of cnodes.
1153 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfs_locktype locktype
)
1155 struct cnode
*first
, *last
;
1159 * If cnodes match then just lock one.
1163 return hfs_lock(cp1
, locktype
, HFS_LOCK_DEFAULT
);
1167 * Lock in cnode address order.
1180 if ( (error
= hfs_lock(first
, locktype
, HFS_LOCK_DEFAULT
)))
1184 if ( (error
= hfs_lock(last
, locktype
, HFS_LOCK_DEFAULT
)))
1193 * Unlock a pair of cnodes.
1196 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1204 * Increase the gen count by 1; if it wraps around to 0, increment by
1205 * two. The cnode *must* be locked exclusively by the caller.
1207 * You may think holding the lock is unnecessary because we only need
1208 * to change the counter, but consider this sequence of events: thread
1209 * A calls hfs_incr_gencount and the generation counter is 2 upon
1210 * entry. A context switch occurs and thread B increments the counter
1211 * to 3, thread C now gets the generation counter (for whatever
1212 * purpose), and then another thread makes another change and the
1213 * generation counter is incremented again---it's now 4. Now thread A
1214 * continues and it sets the generation counter back to 3. So you can
1215 * see, thread C would miss the change that caused the generation
1216 * counter to increment to 4 and for this reason the cnode *must*
1217 * always be locked exclusively.
1219 uint32_t hfs_incr_gencount (struct cnode
*cp
)
1221 u_int8_t
*finfo
= NULL
;
1222 u_int32_t gcount
= 0;
1224 /* overlay the FinderInfo to the correct pointer, and advance */
1225 finfo
= (u_int8_t
*)cp
->c_finderinfo
;
1229 * FinderInfo is written out in big endian... make sure to convert it to host
1230 * native before we use it.
1232 * NOTE: the write_gen_counter is stored in the same location in both the
1233 * FndrExtendedFileInfo and FndrExtendedDirInfo structs (it's the
1234 * last 32-bit word) so it is safe to have one code path here.
1236 if (S_ISDIR(cp
->c_attr
.ca_mode
) || S_ISREG(cp
->c_attr
.ca_mode
))
1238 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1239 gcount
= extinfo
->write_gen_counter
;
1241 /* Was it zero to begin with (file originated in 10.8 or earlier?) */
1250 /* Did it wrap around ? */
1255 extinfo
->write_gen_counter
= OSSwapHostToBigInt32 (gcount
);
1257 SET(cp
->c_flag
, C_MINOR_MOD
);
1267 void hfs_write_gencount (struct cat_attr
*attrp
, uint32_t gencount
)
1269 u_int8_t
*finfo
= NULL
;
1271 /* overlay the FinderInfo to the correct pointer, and advance */
1272 finfo
= (u_int8_t
*)attrp
->ca_finderinfo
;
1276 * Make sure to write it out as big endian, since that's how
1277 * finder info is defined.
1279 * Generation count is only supported for files.
1281 if (S_ISREG(attrp
->ca_mode
)) {
1282 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1283 extinfo
->write_gen_counter
= OSSwapHostToBigInt32(gencount
);
1286 /* If it were neither directory/file, then we'd bail out */
1290 void hfs_clear_might_be_dirty_flag(cnode_t
*cp
)
1293 * If we're about to touch both mtime and ctime, we can clear the
1294 * C_MIGHT_BE_DIRTY_FROM_MAPPING since we can guarantee that
1295 * subsequent page-outs can only be for data made dirty before
1298 CLR(cp
->c_flag
, C_MIGHT_BE_DIRTY_FROM_MAPPING
);
1302 * Touch cnode times based on c_touch_xxx flags
1304 * cnode must be locked exclusive
1306 * This will also update the volume modify time
1309 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
1312 if (ISSET(hfsmp
->hfs_flags
, HFS_READ_ONLY
) || ISSET(cp
->c_flag
, C_NOEXISTS
)) {
1313 cp
->c_touch_acctime
= FALSE
;
1314 cp
->c_touch_chgtime
= FALSE
;
1315 cp
->c_touch_modtime
= FALSE
;
1316 CLR(cp
->c_flag
, C_NEEDS_DATEADDED
);
1320 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
||
1321 cp
->c_touch_modtime
|| (cp
->c_flag
& C_NEEDS_DATEADDED
)) {
1325 if (cp
->c_touch_modtime
&& cp
->c_touch_chgtime
)
1326 hfs_clear_might_be_dirty_flag(cp
);
1330 if (cp
->c_touch_acctime
) {
1332 * When the access time is the only thing changing, we
1333 * won't necessarily write it to disk immediately. We
1334 * only do the atime update at vnode recycle time, when
1335 * fsync is called or when there's another reason to write
1338 cp
->c_atime
= tv
.tv_sec
;
1339 cp
->c_touch_acctime
= FALSE
;
1341 if (cp
->c_touch_modtime
) {
1342 cp
->c_touch_modtime
= FALSE
;
1343 time_t new_time
= tv
.tv_sec
;
1344 if (cp
->c_mtime
!= new_time
) {
1345 cp
->c_mtime
= new_time
;
1346 cp
->c_flag
|= C_MINOR_MOD
;
1350 if (cp
->c_touch_chgtime
) {
1351 cp
->c_touch_chgtime
= FALSE
;
1352 if (cp
->c_ctime
!= tv
.tv_sec
) {
1353 cp
->c_ctime
= tv
.tv_sec
;
1354 cp
->c_flag
|= C_MINOR_MOD
;
1359 if (cp
->c_flag
& C_NEEDS_DATEADDED
) {
1360 hfs_write_dateadded (&(cp
->c_attr
), tv
.tv_sec
);
1361 cp
->c_flag
|= C_MINOR_MOD
;
1362 /* untwiddle the bit */
1363 cp
->c_flag
&= ~C_NEEDS_DATEADDED
;
1367 /* Touch the volume modtime if needed */
1369 hfs_note_header_minor_change(hfsmp
);
1370 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
1376 * Per HI and Finder requirements, HFS should add in the
1377 * date/time that a particular directory entry was added
1378 * to the containing directory.
1379 * This is stored in the extended Finder Info for the
1382 * Note that this field is also set explicitly in the hfs_vnop_setxattr code.
1383 * We must ignore user attempts to set this part of the finderinfo, and
1384 * so we need to save a local copy of the date added, write in the user
1385 * finderinfo, then stuff the value back in.
1387 void hfs_write_dateadded (struct cat_attr
*attrp
, uint64_t dateadded
)
1389 u_int8_t
*finfo
= NULL
;
1391 /* overlay the FinderInfo to the correct pointer, and advance */
1392 finfo
= (u_int8_t
*)attrp
->ca_finderinfo
;
1396 * Make sure to write it out as big endian, since that's how
1397 * finder info is defined.
1399 * NOTE: This is a Unix-epoch timestamp, not a HFS/Traditional Mac timestamp.
1401 if (S_ISREG(attrp
->ca_mode
)) {
1402 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1403 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
1404 attrp
->ca_recflags
|= kHFSHasDateAddedMask
;
1406 else if (S_ISDIR(attrp
->ca_mode
)) {
1407 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1408 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
1409 attrp
->ca_recflags
|= kHFSHasDateAddedMask
;
1411 /* If it were neither directory/file, then we'd bail out */
1416 hfs_get_dateadded_internal(const uint8_t *finderinfo
, mode_t mode
)
1418 const uint8_t *finfo
= NULL
;
1419 u_int32_t dateadded
= 0;
1421 /* overlay the FinderInfo to the correct pointer, and advance */
1422 finfo
= finderinfo
+ 16;
1425 * FinderInfo is written out in big endian... make sure to convert it to host
1426 * native before we use it.
1428 if (S_ISREG(mode
)) {
1429 const struct FndrExtendedFileInfo
*extinfo
= (const struct FndrExtendedFileInfo
*)finfo
;
1430 dateadded
= OSSwapBigToHostInt32 (extinfo
->date_added
);
1432 else if (S_ISDIR(mode
)) {
1433 const struct FndrExtendedDirInfo
*extinfo
= (const struct FndrExtendedDirInfo
*)finfo
;
1434 dateadded
= OSSwapBigToHostInt32 (extinfo
->date_added
);
1441 hfs_get_dateadded(struct cnode
*cp
)
1443 if ((cp
->c_attr
.ca_recflags
& kHFSHasDateAddedMask
) == 0) {
1444 /* Date added was never set. Return 0. */
1448 return (hfs_get_dateadded_internal((u_int8_t
*)cp
->c_finderinfo
,
1449 cp
->c_attr
.ca_mode
));
1453 hfs_cnode_isinuse(struct cnode
*cp
, uint32_t uRefCount
)
1455 return (cp
->uOpenLookupRefCount
> uRefCount
);
1459 * hfs_cnode_teardown
1461 * This is an internal function that is invoked from both hfs_vnop_inactive
1462 * and hfs_vnop_reclaim. As VNOP_INACTIVE is not necessarily called from vnodes
1463 * being recycled and reclaimed, it is important that we do any post-processing
1464 * necessary for the cnode in both places. Important tasks include things such as
1465 * releasing the blocks from an open-unlinked file when all references to it have dropped,
1466 * and handling resource forks separately from data forks.
1468 * Note that we take only the vnode as an argument here (rather than the cnode).
1469 * Recall that each cnode supports two forks (rsrc/data), and we can always get the right
1470 * cnode from either of the vnodes, but the reverse is not true -- we can't determine which
1471 * vnode we need to reclaim if only the cnode is supplied.
1473 * This function is idempotent and safe to call from both hfs_vnop_inactive and hfs_vnop_reclaim
1474 * if both are invoked right after the other. In the second call, most of this function's if()
1475 * conditions will fail, since they apply generally to cnodes still marked with C_DELETED.
1476 * As a quick check to see if this function is necessary, determine if the cnode is already
1477 * marked C_NOEXISTS. If it is, then it is safe to skip this function. The only tasks that
1478 * remain for cnodes marked in such a fashion is to teardown their fork references and
1479 * release all directory hints and hardlink origins. However, both of those are done
1480 * in hfs_vnop_reclaim. hfs_update, by definition, is not necessary if the cnode's catalog
1481 * entry is no longer there.
1483 * 'reclaim' argument specifies whether or not we were called from hfs_vnop_reclaim. If we are
1484 * invoked from hfs_vnop_reclaim, we can not call functions that cluster_push since the UBC info
1485 * is totally gone by that point.
1487 * Assumes that both truncate and cnode locks for 'cp' are held.
1490 hfs_cnode_teardown (struct vnode
*vp
, int reclaim
)
1493 enum vtype v_type
= vp
->sFSParams
.vnfs_vtype
;
1494 struct cnode
* cp
= VTOC(vp
);
1496 bool started_tr
= false;
1497 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1499 cat_cookie_t cookie
;
1500 int cat_reserve
= 0;
1504 if (cp
->c_datafork
) {
1507 if (cp
->c_rsrcfork
) {
1512 * Remove any directory hints or cached origins
1514 if (v_type
== VDIR
) {
1515 hfs_reldirhints(cp
, 0);
1517 if (cp
->c_flag
& C_HARDLINK
) {
1521 * -- Handle open unlinked files --
1523 * If the vnode is in use, it means a force unmount is in progress
1524 * in which case we defer cleaning up until either we come back
1525 * through here via hfs_vnop_reclaim, at which point the UBC
1526 * information will have been torn down and the vnode might no
1527 * longer be in use, or if it's still in use, it will get cleaned
1528 * up when next remounted.
1530 if (ISSET(cp
->c_flag
, C_DELETED
) && !hfs_cnode_isinuse(cp
, 0)) {
1532 * This check is slightly complicated. We should only truncate data
1533 * in very specific cases for open-unlinked files. This is because
1534 * we want to ensure that the resource fork continues to be available
1535 * if the caller has the data fork open. However, this is not symmetric;
1536 * someone who has the resource fork open need not be able to access the data
1537 * fork once the data fork has gone inactive.
1539 * If we're the last fork, then we have cleaning up to do.
1541 * A) last fork, and vp == c_vp
1542 * Truncate away own fork data. If rsrc fork is not in core, truncate it too.
1544 * B) last fork, and vp == c_rsrc_vp
1545 * Truncate ourselves, assume data fork has been cleaned due to C).
1547 * If we're not the last fork, then things are a little different:
1549 * C) not the last fork, vp == c_vp
1550 * Truncate ourselves. Once the file has gone out of the namespace,
1551 * it cannot be further opened. Further access to the rsrc fork may
1552 * continue, however.
1554 * D) not the last fork, vp == c_rsrc_vp
1555 * Don't enter the block below, just clean up vnode and push it out of core.
1558 if ((v_type
== VREG
|| v_type
== VLNK
) &&
1559 ((forkcount
== 1) || (!VNODE_IS_RSRC(vp
)))) {
1561 /* Truncate away our own fork data. (Case A, B, C above) */
1562 if (VTOF(vp
) && VTOF(vp
)->ff_blocks
!= 0) {
1566 * Encapsulate the entire change (including truncating the link) in
1567 * nested transactions if we are modifying a symlink, because we know that its
1568 * file length will be at most 4k, and we can fit both the truncation and
1569 * any relevant bitmap changes into a single journal transaction. We also want
1570 * the kill_block code to execute in the same transaction so that any dirty symlink
1571 * blocks will not be written. Otherwise, rely on
1572 * hfs_truncate doing its own transactions to ensure that we don't blow up
1575 if (!started_tr
&& (v_type
== VLNK
)) {
1576 if (hfs_start_transaction(hfsmp
) != 0) {
1586 * At this point, we have decided that this cnode is
1587 * suitable for full removal. We are about to deallocate
1588 * its blocks and remove its entry from the catalog.
1589 * If it was a symlink, then it's possible that the operation
1590 * which created it is still in the current transaction group
1591 * due to coalescing. Take action here to kill the data blocks
1592 * of the symlink out of the journal before moving to
1593 * deallocate the blocks. We need to be in the middle of
1594 * a transaction before calling buf_iterate like this.
1596 * Note: we have to kill any potential symlink buffers out of
1597 * the journal prior to deallocating their blocks. This is so
1598 * that we don't race with another thread that may be doing an
1599 * an allocation concurrently and pick up these blocks. It could
1600 * generate I/O against them which could go out ahead of our journal
1604 if (hfsmp
->jnl
&& vnode_islnk(vp
)) {
1605 lf_hfs_generic_buf_write_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
1609 * This truncate call (and the one below) is fine from VNOP_RECLAIM's
1610 * context because we're only removing blocks, not zero-filling new
1611 * ones. The C_DELETED check above makes things much simpler.
1613 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 0);
1619 /* (SYMLINKS ONLY): Close/End our transaction after truncating the file record */
1621 hfs_end_transaction(hfsmp
);
1628 * Truncate away the resource fork, if we represent the data fork and
1629 * it is the last fork. That means, by definition, the rsrc fork is not in
1630 * core. To avoid bringing a vnode into core for the sole purpose of deleting the
1631 * data in the resource fork, we call cat_lookup directly, then hfs_release_storage
1632 * to get rid of the resource fork's data. Note that because we are holding the
1633 * cnode lock, it is impossible for a competing thread to create the resource fork
1634 * vnode from underneath us while we do this.
1636 * This is invoked via case A above only.
1638 if ((cp
->c_blocks
> 0) && (forkcount
== 1) && (vp
!= cp
->c_rsrc_vp
)) {
1639 struct cat_lookup_buffer
*lookup_rsrc
= NULL
;
1640 struct cat_desc
*desc_ptr
= NULL
;
1642 lookup_rsrc
= hfs_mallocz(sizeof(struct cat_lookup_buffer
));
1644 if (cp
->c_desc
.cd_namelen
== 0) {
1645 /* Initialize the rsrc descriptor for lookup if necessary*/
1646 MAKE_DELETED_NAME (lookup_rsrc
->lookup_name
, HFS_TEMPLOOKUP_NAMELEN
, cp
->c_fileid
);
1648 lookup_rsrc
->lookup_desc
.cd_nameptr
= (const uint8_t*) lookup_rsrc
->lookup_name
;
1649 lookup_rsrc
->lookup_desc
.cd_namelen
= strlen (lookup_rsrc
->lookup_name
);
1650 lookup_rsrc
->lookup_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1651 lookup_rsrc
->lookup_desc
.cd_cnid
= cp
->c_cnid
;
1653 desc_ptr
= &lookup_rsrc
->lookup_desc
;
1656 desc_ptr
= &cp
->c_desc
;
1659 lockflags
= hfs_systemfile_lock (hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1661 error
= cat_lookup (hfsmp
, desc_ptr
, 1, (struct cat_desc
*) NULL
, (struct cat_attr
*) NULL
, &lookup_rsrc
->lookup_fork
.ff_data
, NULL
);
1663 hfs_systemfile_unlock (hfsmp
, lockflags
);
1666 hfs_free(lookup_rsrc
);
1671 * Make the filefork in our temporary struct look like a real
1672 * filefork. Fill in the cp, sysfileinfo and rangelist fields..
1674 rl_init (&lookup_rsrc
->lookup_fork
.ff_invalidranges
);
1675 lookup_rsrc
->lookup_fork
.ff_cp
= cp
;
1678 * If there were no errors, then we have the catalog's fork information
1679 * for the resource fork in question. Go ahead and delete the data in it now.
1682 error
= hfs_release_storage (hfsmp
, NULL
, &lookup_rsrc
->lookup_fork
, cp
->c_fileid
);
1683 hfs_free(lookup_rsrc
);
1690 * This fileid's resource fork extents have now been fully deleted on-disk
1691 * and this CNID is no longer valid. At this point, we should be able to
1692 * zero out cp->c_blocks to indicate there is no data left in this file.
1699 * If we represent the last fork (or none in the case of a dir),
1700 * and the cnode has become open-unlinked...
1702 * We check c_blocks here because it is possible in the force
1703 * unmount case for the data fork to be in use but the resource
1704 * fork to not be in use in which case we will truncate the
1705 * resource fork, but not the data fork. It will get cleaned
1706 * up upon next mount.
1708 if (forkcount
<= 1 && !cp
->c_blocks
) {
1710 * If it has EA's, then we need to get rid of them.
1712 * Note that this must happen outside of any other transactions
1713 * because it starts/ends its own transactions and grabs its
1714 * own locks. This is to prevent a file with a lot of attributes
1715 * from creating a transaction that is too large (which panics).
1717 if (ISSET(cp
->c_attr
.ca_recflags
, kHFSHasAttributesMask
))
1719 ea_error
= hfs_removeallattr(hfsmp
, cp
->c_fileid
, &started_tr
);
1725 * Remove the cnode's catalog entry and release all blocks it
1726 * may have been using.
1730 * Mark cnode in transit so that no one can get this
1731 * cnode from cnode hash.
1733 // hfs_chash_mark_in_transit(hfsmp, cp);
1734 // XXXdbg - remove the cnode from the hash table since it's deleted
1735 // otherwise someone could go to sleep on the cnode and not
1736 // be woken up until this vnode gets recycled which could be
1737 // a very long time...
1738 hfs_chashremove(hfsmp
, cp
);
1740 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
1744 if (hfs_start_transaction(hfsmp
) != 0) {
1752 * Reserve some space in the Catalog file.
1754 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
))) {
1759 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1761 if (cp
->c_blocks
> 0) {
1762 LFHFS_LOG(LEVEL_ERROR
, "hfs_inactive: deleting non-empty%sfile %d, "
1763 "blks %d\n", VNODE_IS_RSRC(vp
) ? " rsrc " : " ",
1764 (int)cp
->c_fileid
, (int)cp
->c_blocks
);
1768 // release the name pointer in the descriptor so that
1769 // cat_delete() will use the file-id to do the deletion.
1770 // in the case of hard links this is imperative (in the
1771 // case of regular files the fileid and cnid are the
1772 // same so it doesn't matter).
1774 cat_releasedesc(&cp
->c_desc
);
1777 * The descriptor name may be zero,
1778 * in which case the fileid is used.
1780 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
1782 if (error
&& truncated
&& (error
!= ENXIO
)) {
1783 LFHFS_LOG(LEVEL_ERROR
, "hfs_inactive: couldn't delete a truncated file!");
1786 /* Update HFS Private Data dir */
1788 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
--;
1789 if (vnode_isdir(vp
)) {
1790 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
1792 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
1793 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
1796 hfs_systemfile_unlock(hfsmp
, lockflags
);
1802 /* Already set C_NOEXISTS at the beginning of this block */
1803 cp
->c_flag
&= ~C_DELETED
;
1804 cp
->c_touch_chgtime
= TRUE
;
1805 cp
->c_touch_modtime
= TRUE
;
1808 hfs_volupdate(hfsmp
, (v_type
== VDIR
) ? VOL_RMDIR
: VOL_RMFILE
, 0);
1810 } // if <open unlinked>
1812 hfs_update(vp
, reclaim
? HFS_UPDATE_FORCE
: 0);
1815 * Since we are about to finish what might be an inactive call, propagate
1816 * any remaining modified or touch bits from the cnode to the vnode. This
1817 * serves as a hint to vnode recycling that we shouldn't recycle this vnode
1820 * For now, if the node *only* has a dirty atime, we don't mark
1821 * the vnode as dirty. VFS's asynchronous recycling can actually
1822 * lead to worse performance than having it synchronous. When VFS
1823 * is fixed to be more performant, we can be more honest about
1824 * marking vnodes as dirty when it's only the atime that's dirty.
1826 #if LF_HFS_FULL_VNODE_SUPPORT
1827 //TBD - need to decide how we mark a file as dirty
1828 if (hfs_is_dirty(cp
) == HFS_DIRTY
|| ISSET(cp
->c_flag
, C_DELETED
)) {
1831 vnode_cleardirty(vp
);
1837 cat_postflight(hfsmp
, &cookie
);
1840 hfs_end_transaction(hfsmp
);
1848 hfs_fork_release(struct cnode
* cp
, struct vnode
*vp
, bool bIsRsc
, int* piErr
)
1850 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1851 struct filefork
*fp
= NULL
;
1852 struct filefork
*altfp
= NULL
;
1853 int reclaim_cnode
= 0;
1856 * Sync to disk any remaining data in the cnode/vnode. This includes
1857 * a call to hfs_update if the cnode has outbound data.
1859 * If C_NOEXISTS is set on the cnode, then there's nothing teardown needs to do
1860 * because the catalog entry for this cnode is already gone.
1862 INVALIDATE_NODE(vp
);
1864 if (!ISSET(cp
->c_flag
, C_NOEXISTS
)) {
1865 *piErr
= hfs_cnode_teardown(vp
, 1);
1872 if (vp
->sFSParams
.vnfs_cnp
)
1874 if (vp
->sFSParams
.vnfs_cnp
->cn_nameptr
)
1875 hfs_free(vp
->sFSParams
.vnfs_cnp
->cn_nameptr
);
1876 hfs_free(vp
->sFSParams
.vnfs_cnp
);
1881 fp
= cp
->c_datafork
;
1882 altfp
= cp
->c_rsrcfork
;
1884 cp
->c_datafork
= NULL
;
1887 fp
= cp
->c_rsrcfork
;
1888 altfp
= cp
->c_datafork
;
1890 cp
->c_rsrcfork
= NULL
;
1891 cp
->c_rsrc_vp
= NULL
;
1895 * On the last fork, remove the cnode from its hash chain.
1897 if (altfp
== NULL
) {
1898 /* If we can't remove it then the cnode must persist! */
1899 if (hfs_chashremove(hfsmp
, cp
) == 0)
1902 * Remove any directory hints
1904 if (vnode_isdir(vp
)) {
1905 hfs_reldirhints(cp
, 0);
1908 if(cp
->c_flag
& C_HARDLINK
) {
1913 /* Release the file fork and related data */
1916 /* Dump cached symlink data */
1917 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
1918 hfs_free(fp
->ff_symlinkptr
);
1920 rl_remove_all(&fp
->ff_invalidranges
);
1924 return reclaim_cnode
;
1929 * Reclaim a cnode so that it can be used for other purposes.
1932 hfs_vnop_reclaim(struct vnode
*vp
)
1934 struct cnode
* cp
= VTOC(vp
);
1935 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1936 struct vnode
*altvp
= NULL
;
1937 int reclaim_cnode
= 0;
1941 * We don't take the truncate lock since by the time reclaim comes along,
1942 * all dirty pages have been synced and nobody should be competing
1943 * with us for this thread.
1945 hfs_chash_mark_in_transit(hfsmp
, cp
);
1947 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1948 lf_hfs_generic_buf_cache_LockBufCache();
1950 //In case we have other open lookups
1951 //We need to decrease the counter and exit
1952 if (cp
->uOpenLookupRefCount
> 1)
1954 hfs_chash_lower_OpenLookupCounter(cp
);
1955 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_TRANSIT
);
1956 lf_hfs_generic_buf_cache_UnLockBufCache();
1961 if (cp
->uOpenLookupRefCount
== 0) assert(0);
1963 hfs_chash_lower_OpenLookupCounter(cp
);
1964 lf_hfs_generic_buf_cache_remove_vnode(vp
);
1966 lf_hfs_generic_buf_cache_UnLockBufCache();
1969 * Find file fork for this vnode (if any)
1970 * Also check if another fork is active
1972 if (cp
->c_vp
== vp
) {
1974 reclaim_cnode
= hfs_fork_release(cp
, vp
, false, &err
);
1975 if (err
) return err
;
1977 if (!reclaim_cnode
&& cp
->c_rsrc_vp
!= NULL
)
1979 altvp
= cp
->c_rsrc_vp
;
1980 reclaim_cnode
= hfs_fork_release(cp
, altvp
, true, &err
);
1981 if (err
) return err
;
1983 } else if (cp
->c_rsrc_vp
== vp
) {
1984 reclaim_cnode
= hfs_fork_release(cp
, vp
, true, &err
);
1985 if (err
) return err
;
1987 if (!reclaim_cnode
&& cp
->c_vp
!= NULL
)
1990 reclaim_cnode
= hfs_fork_release(cp
, altvp
, false, &err
);
1991 if (err
) return err
;
1994 LFHFS_LOG(LEVEL_ERROR
, "hfs_vnop_reclaim: vp points to wrong cnode (vp=%p cp->c_vp=%p cp->c_rsrc_vp=%p)\n", vp
, cp
->c_vp
, cp
->c_rsrc_vp
);
1999 * If there was only one active fork then we can release the cnode.
2001 if (reclaim_cnode
) {
2003 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
);
2004 hfs_reclaim_cnode(cp
);
2009 * cnode in use. If it is a directory, it could have
2010 * no live forks. Just release the lock.