2 * Copyright (c) 2002-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/vnode.h>
32 #include <sys/mount.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
37 #include <sys/quota.h>
38 #include <sys/kdebug.h>
39 #include <libkern/OSByteOrder.h>
41 #include <kern/locks.h>
43 #include <miscfs/specfs/specdev.h>
44 #include <miscfs/fifofs/fifo.h>
47 #include <hfs/hfs_catalog.h>
48 #include <hfs/hfs_cnode.h>
49 #include <hfs/hfs_quota.h>
50 #include <hfs/hfs_format.h>
54 extern lck_attr_t
* hfs_lock_attr
;
55 extern lck_grp_t
* hfs_mutex_group
;
56 extern lck_grp_t
* hfs_rwlock_group
;
58 static void hfs_reclaim_cnode(struct cnode
*);
59 static int hfs_cnode_teardown (struct vnode
*vp
, vfs_context_t ctx
, int reclaim
);
60 static int hfs_isordered(struct cnode
*, struct cnode
*);
62 __inline__
int hfs_checkdeleted (struct cnode
*cp
) {
63 return ((cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ? ENOENT
: 0);
68 * Function used by a special fcntl() that decorates a cnode/vnode that
69 * indicates it is backing another filesystem, like a disk image.
71 * the argument 'val' indicates whether or not to set the bit in the cnode flags
73 * Returns non-zero on failure. 0 on success
75 int hfs_set_backingstore (struct vnode
*vp
, int val
) {
76 struct cnode
*cp
= NULL
;
80 if (!vnode_isreg(vp
) && !vnode_isdir(vp
)) {
85 err
= hfs_lock (cp
, HFS_EXCLUSIVE_LOCK
);
91 cp
->c_flag
|= C_BACKINGSTORE
;
94 cp
->c_flag
&= ~C_BACKINGSTORE
;
97 /* unlock everything */
104 * Function used by a special fcntl() that check to see if a cnode/vnode
105 * indicates it is backing another filesystem, like a disk image.
107 * the argument 'val' is an output argument for whether or not the bit is set
109 * Returns non-zero on failure. 0 on success
112 int hfs_is_backingstore (struct vnode
*vp
, int *val
) {
113 struct cnode
*cp
= NULL
;
116 if (!vnode_isreg(vp
) && !vnode_isdir(vp
)) {
124 err
= hfs_lock (cp
, HFS_SHARED_LOCK
);
129 if (cp
->c_flag
& C_BACKINGSTORE
) {
136 /* unlock everything */
146 * This is an internal function that is invoked from both hfs_vnop_inactive
147 * and hfs_vnop_reclaim. As VNOP_INACTIVE is not necessarily called from vnodes
148 * being recycled and reclaimed, it is important that we do any post-processing
149 * necessary for the cnode in both places. Important tasks include things such as
150 * releasing the blocks from an open-unlinked file when all references to it have dropped,
151 * and handling resource forks separately from data forks.
153 * Note that we take only the vnode as an argument here (rather than the cnode).
154 * Recall that each cnode supports two forks (rsrc/data), and we can always get the right
155 * cnode from either of the vnodes, but the reverse is not true -- we can't determine which
156 * vnode we need to reclaim if only the cnode is supplied.
158 * This function is idempotent and safe to call from both hfs_vnop_inactive and hfs_vnop_reclaim
159 * if both are invoked right after the other. In the second call, most of this function's if()
160 * conditions will fail, since they apply generally to cnodes still marked with C_DELETED.
161 * As a quick check to see if this function is necessary, determine if the cnode is already
162 * marked C_NOEXISTS. If it is, then it is safe to skip this function. The only tasks that
163 * remain for cnodes marked in such a fashion is to teardown their fork references and
164 * release all directory hints and hardlink origins. However, both of those are done
165 * in hfs_vnop_reclaim. hfs_update, by definition, is not necessary if the cnode's catalog
166 * entry is no longer there.
168 * 'reclaim' argument specifies whether or not we were called from hfs_vnop_reclaim. If we are
169 * invoked from hfs_vnop_reclaim, we can not call functions that cluster_push since the UBC info
170 * is totally gone by that point.
172 * Assumes that both truncate and cnode locks for 'cp' are held.
175 int hfs_cnode_teardown (struct vnode
*vp
, vfs_context_t ctx
, int reclaim
) {
182 struct hfsmount
*hfsmp
= VTOHFS(vp
);
183 struct proc
*p
= vfs_context_proc(ctx
);
190 v_type
= vnode_vtype(vp
);
193 if (cp
->c_datafork
) {
196 if (cp
->c_rsrcfork
) {
202 * Skip the call to ubc_setsize if we're being invoked on behalf of reclaim.
203 * The dirty regions would have already been synced to disk, so informing UBC
204 * that they can toss the pages doesn't help anyone at this point.
206 * Note that this is a performance problem if the vnode goes straight to reclaim
207 * (and skips inactive), since there would be no way for anyone to notify the UBC
208 * that all pages in this file are basically useless.
212 * Check whether we are tearing down a cnode with only one remaining fork.
213 * If there are blocks in its filefork, then we need to unlock the cnode
214 * before calling ubc_setsize. The cluster layer may re-enter the filesystem
215 * (i.e. VNOP_BLOCKMAP), and if we retain the cnode lock, we could double-lock
219 if ((v_type
== VREG
|| v_type
== VLNK
) &&
220 (cp
->c_flag
& C_DELETED
) &&
221 (VTOF(vp
)->ff_blocks
!= 0) && (forkcount
== 1)) {
223 /* ubc_setsize just fails if we were to call this from VNOP_RECLAIM */
225 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
230 * Push file data out for normal files that haven't been evicted from
231 * the namespace. We only do this if this function was not called from reclaim,
232 * because by that point the UBC information has been totally torn down.
234 * There should also be no way that a normal file that has NOT been deleted from
235 * the namespace to skip INACTIVE and go straight to RECLAIM. That race only happens
236 * when the file becomes open-unlinked.
238 if ((v_type
== VREG
) &&
239 (!ISSET(cp
->c_flag
, C_DELETED
)) &&
240 (!ISSET(cp
->c_flag
, C_NOEXISTS
)) &&
241 (VTOF(vp
)->ff_blocks
) &&
243 hfs_filedone(vp
, ctx
);
246 * Remove any directory hints or cached origins
248 if (v_type
== VDIR
) {
249 hfs_reldirhints(cp
, 0);
251 if (cp
->c_flag
& C_HARDLINK
) {
256 * This check is slightly complicated. We should only truncate data
257 * in very specific cases for open-unlinked files. This is because
258 * we want to ensure that the resource fork continues to be available
259 * if the caller has the data fork open. However, this is not symmetric;
260 * someone who has the resource fork open need not be able to access the data
261 * fork once the data fork has gone inactive.
263 * If we're the last fork, then we have cleaning up to do.
265 * A) last fork, and vp == c_vp
266 * Truncate away own fork data. If rsrc fork is not in core, truncate it too.
268 * B) last fork, and vp == c_rsrc_vp
269 * Truncate ourselves, assume data fork has been cleaned due to C).
271 * If we're not the last fork, then things are a little different:
273 * C) not the last fork, vp == c_vp
274 * Truncate ourselves. Once the file has gone out of the namespace,
275 * it cannot be further opened. Further access to the rsrc fork may
278 * D) not the last fork, vp == c_rsrc_vp
279 * Don't enter the block below, just clean up vnode and push it out of core.
282 if ((v_type
== VREG
|| v_type
== VLNK
) &&
283 (cp
->c_flag
& C_DELETED
) &&
284 ((forkcount
== 1) || (!VNODE_IS_RSRC(vp
)))) {
286 /* Truncate away our own fork data. (Case A, B, C above) */
287 if (VTOF(vp
)->ff_blocks
!= 0) {
289 * Since we're already inside a transaction,
290 * tell hfs_truncate to skip the ubc_setsize.
292 * This truncate call (and the one below) is fine from VNOP_RECLAIM's
293 * context because we're only removing blocks, not zero-filling new
294 * ones. The C_DELETED check above makes things much simpler.
296 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, 0, ctx
);
304 * Truncate away the resource fork, if we represent the data fork and
305 * it is the last fork. That means, by definition, the rsrc fork is not in
306 * core. So we bring it into core, and then truncate it away.
308 * This is invoked via case A above only.
310 if ((cp
->c_blocks
> 0) && (forkcount
== 1) && (vp
!= cp
->c_rsrc_vp
)) {
311 struct vnode
*rvp
= NULLVP
;
314 * It is safe for us to pass FALSE to the argument can_drop_lock
315 * on this call to hfs_vgetrsrc. We know that the resource fork does not
316 * exist in core, so we'll have to go to the catalog to retrieve its
317 * information. That will attach the resource fork vnode to our cnode.
319 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, FALSE
, FALSE
);
324 * Defer the vnode_put and ubc_setsize on rvp until hfs_unlock().
326 * By bringing the vnode into core above, we may force hfs_vnop_reclaim
327 * to only partially finish if that's what called us. Bringing the
328 * resource fork into core results in a new rsrc vnode that will get
329 * immediately marked for termination below. It will get recycled/reclaimed
330 * as soon as possible, but that could cause another round of inactive and reclaim.
332 cp
->c_flag
|= C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
;
333 error
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 1, 0, ctx
);
339 * Note that the following call to vnode_recycle is safe from within the
340 * context of hfs_vnop_inactive or hfs_vnop_reclaim. It is being invoked
341 * on the RSRC fork vp (which is not our current vnode) As such, we hold
342 * an iocount on it and vnode_recycle will just add the MARKTERM bit at this
345 vnode_recycle(rvp
); /* all done with this vnode */
350 * If we represent the last fork (or none in the case of a dir),
351 * and the cnode has become open-unlinked,
352 * AND it has EA's, then we need to get rid of them.
354 * Note that this must happen outside of any other transactions
355 * because it starts/ends its own transactions and grabs its
356 * own locks. This is to prevent a file with a lot of attributes
357 * from creating a transaction that is too large (which panics).
359 if ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0 &&
360 (cp
->c_flag
& C_DELETED
) &&
363 ea_error
= hfs_removeallattr(hfsmp
, cp
->c_fileid
);
368 * If the cnode represented an open-unlinked file, then now
369 * actually remove the cnode's catalog entry and release all blocks
370 * it may have been using.
372 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
374 * Mark cnode in transit so that no one can get this
375 * cnode from cnode hash.
377 // hfs_chash_mark_in_transit(hfsmp, cp);
378 // XXXdbg - remove the cnode from the hash table since it's deleted
379 // otherwise someone could go to sleep on the cnode and not
380 // be woken up until this vnode gets recycled which could be
381 // a very long time...
382 hfs_chashremove(hfsmp
, cp
);
384 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
387 if (started_tr
== 0) {
388 if (hfs_start_transaction(hfsmp
) != 0) {
396 * Reserve some space in the Catalog file.
398 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
403 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
405 if (cp
->c_blocks
> 0) {
406 printf("hfs_inactive: deleting non-empty%sfile %d, "
407 "blks %d\n", VNODE_IS_RSRC(vp
) ? " rsrc " : " ",
408 (int)cp
->c_fileid
, (int)cp
->c_blocks
);
412 // release the name pointer in the descriptor so that
413 // cat_delete() will use the file-id to do the deletion.
414 // in the case of hard links this is imperative (in the
415 // case of regular files the fileid and cnid are the
416 // same so it doesn't matter).
418 cat_releasedesc(&cp
->c_desc
);
421 * The descriptor name may be zero,
422 * in which case the fileid is used.
424 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
426 if (error
&& truncated
&& (error
!= ENXIO
))
427 printf("hfs_inactive: couldn't delete a truncated file!");
429 /* Update HFS Private Data dir */
431 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
--;
432 if (vnode_isdir(vp
)) {
433 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
435 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
436 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
439 hfs_systemfile_unlock(hfsmp
, lockflags
);
446 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
447 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
450 /* Already set C_NOEXISTS at the beginning of this block */
451 cp
->c_flag
&= ~C_DELETED
;
452 cp
->c_touch_chgtime
= TRUE
;
453 cp
->c_touch_modtime
= TRUE
;
456 hfs_volupdate(hfsmp
, (v_type
== VDIR
) ? VOL_RMDIR
: VOL_RMFILE
, 0);
460 * A file may have had delayed allocations, in which case hfs_update
461 * would not have updated the catalog record (cat_update). We need
462 * to do that now, before we lose our fork data. We also need to
463 * force the update, or hfs_update will again skip the cat_update.
465 * If the file has C_NOEXISTS set, then we can skip the hfs_update call
466 * because the catalog entry has already been removed. There would be no point
467 * to looking up the entry in the catalog to modify it when we already know it's gone
469 if ((!ISSET(cp
->c_flag
, C_NOEXISTS
)) &&
470 ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_acctime
||
471 cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
473 if ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_modtime
){
474 cp
->c_flag
|= C_FORCEUPDATE
;
481 cat_postflight(hfsmp
, &cookie
, p
);
483 // XXXdbg - have to do this because a goto could have come here
485 hfs_end_transaction(hfsmp
);
498 * The last usecount on the vnode has gone away, so we need to tear down
499 * any remaining data still residing in the cnode. If necessary, write out
500 * remaining blocks or delete the cnode's entry in the catalog.
503 hfs_vnop_inactive(struct vnop_inactive_args
*ap
)
505 struct vnode
*vp
= ap
->a_vp
;
507 struct hfsmount
*hfsmp
= VTOHFS(vp
);
508 struct proc
*p
= vfs_context_proc(ap
->a_context
);
510 int took_trunc_lock
= 0;
513 v_type
= vnode_vtype(vp
);
516 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || vnode_issystem(vp
) ||
517 (hfsmp
->hfs_freezing_proc
== p
)) {
523 * For safety, do NOT call vnode_recycle from inside this function. This can cause
524 * problems in the following scenario:
526 * vnode_create -> vnode_reclaim_internal -> vclean -> VNOP_INACTIVE
528 * If we're being invoked as a result of a reclaim that was already in-flight, then we
529 * cannot call vnode_recycle again. Being in reclaim means that there are no usecounts or
530 * iocounts by definition. As a result, if we were to call vnode_recycle, it would immediately
531 * try to re-enter reclaim again and panic.
533 * Currently, there are three things that can cause us (VNOP_INACTIVE) to get called.
534 * 1) last usecount goes away on the vnode (vnode_rele)
535 * 2) last iocount goes away on a vnode that previously had usecounts but didn't have
536 * vnode_recycle called (vnode_put)
537 * 3) vclean by way of reclaim
539 * In this function we would generally want to call vnode_recycle to speed things
540 * along to ensure that we don't leak blocks due to open-unlinked files. However, by
541 * virtue of being in this function already, we can call hfs_cnode_teardown, which
542 * will release blocks held by open-unlinked files, and mark them C_NOEXISTS so that
543 * there's no entry in the catalog and no backing store anymore. If that's the case,
544 * then we really don't care all that much when the vnode actually goes through reclaim.
545 * Further, the HFS VNOPs that manipulated the namespace in order to create the open-
546 * unlinked file in the first place should have already called vnode_recycle on the vnode
547 * to guarantee that it would go through reclaim in a speedy way.
550 if (cp
->c_flag
& C_NOEXISTS
) {
552 * If the cnode has already had its cat entry removed, then
553 * just skip to the end. We don't need to do anything here.
559 if ((v_type
== VREG
|| v_type
== VLNK
)) {
560 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
564 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
567 * Call cnode_teardown to push out dirty blocks to disk, release open-unlinked
568 * files' blocks from being in use, and move the cnode from C_DELETED to C_NOEXISTS.
570 error
= hfs_cnode_teardown (vp
, ap
->a_context
, 0);
573 * Drop the truncate lock before unlocking the cnode
574 * (which can potentially perform a vnode_put and
575 * recycle the vnode which in turn might require the
578 if (took_trunc_lock
) {
579 hfs_unlock_truncate(cp
, 0);
591 * File clean-up (zero fill and shrink peof).
595 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
599 struct hfsmount
*hfsmp
;
600 struct rl_entry
*invalid_range
;
602 u_int32_t blks
, blocksize
;
603 int cluster_flags
= IO_CLOSE
;
604 int cluster_zero_flags
= IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
;
611 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
615 * If we are being invoked from F_SWAPDATAEXTENTS, then we
616 * need to issue synchronous IO; Unless we are sure that all
617 * of the data has been written to the disk, we won't know
618 * that all of the blocks have been allocated properly.
620 if (cp
->c_flag
& C_SWAPINPROGRESS
) {
621 cluster_flags
|= IO_SYNC
;
625 (void) cluster_push(vp
, cluster_flags
);
626 hfs_lock(cp
, HFS_FORCE_LOCK
);
629 * Explicitly zero out the areas of file
630 * that are currently marked invalid.
632 while ((invalid_range
= TAILQ_FIRST(&fp
->ff_invalidranges
))) {
633 off_t start
= invalid_range
->rl_start
;
634 off_t end
= invalid_range
->rl_end
;
636 /* The range about to be written must be validated
637 * first, so that VNOP_BLOCKMAP() will return the
638 * appropriate mapping for the cluster code:
640 rl_remove(start
, end
, &fp
->ff_invalidranges
);
643 (void) cluster_write(vp
, (struct uio
*) 0,
644 leof
, end
+ 1, start
, (off_t
)0, cluster_zero_flags
);
645 hfs_lock(cp
, HFS_FORCE_LOCK
);
646 cp
->c_flag
|= C_MODIFIED
;
648 cp
->c_flag
&= ~C_ZFWANTSYNC
;
650 blocksize
= VTOVCB(vp
)->blockSize
;
651 blks
= leof
/ blocksize
;
652 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
655 * Shrink the peof to the smallest size neccessary to contain the leof.
657 if (blks
< fp
->ff_blocks
)
658 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, 0, context
);
660 (void) cluster_push(vp
, cluster_flags
);
661 hfs_lock(cp
, HFS_FORCE_LOCK
);
664 * If the hfs_truncate didn't happen to flush the vnode's
665 * information out to disk, force it to be updated now that
666 * all invalid ranges have been zero-filled and validated:
668 if (cp
->c_flag
& C_MODIFIED
) {
676 * Reclaim a cnode so that it can be used for other purposes.
679 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
681 struct vnode
*vp
= ap
->a_vp
;
683 struct filefork
*fp
= NULL
;
684 struct filefork
*altfp
= NULL
;
685 struct hfsmount
*hfsmp
= VTOHFS(vp
);
686 vfs_context_t ctx
= ap
->a_context
;
687 int reclaim_cnode
= 0;
691 v_type
= vnode_vtype(vp
);
695 * We don't take the truncate lock since by the time reclaim comes along,
696 * all dirty pages have been synced and nobody should be competing
697 * with us for this thread.
699 (void) hfs_lock (cp
, HFS_FORCE_LOCK
);
702 * Sync to disk any remaining data in the cnode/vnode. This includes
703 * a call to hfs_update if the cnode has outbound data.
705 * If C_NOEXISTS is set on the cnode, then there's nothing teardown needs to do
706 * because the catalog entry for this cnode is already gone.
708 if (!ISSET(cp
->c_flag
, C_NOEXISTS
)) {
709 err
= hfs_cnode_teardown(vp
, ctx
, 1);
713 * Keep track of an inactive hot file.
715 if (!vnode_isdir(vp
) &&
716 !vnode_issystem(vp
) &&
717 !(cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ) {
718 (void) hfs_addhotfile(vp
);
720 vnode_removefsref(vp
);
723 * Find file fork for this vnode (if any)
724 * Also check if another fork is active
726 if (cp
->c_vp
== vp
) {
728 altfp
= cp
->c_rsrcfork
;
730 cp
->c_datafork
= NULL
;
732 } else if (cp
->c_rsrc_vp
== vp
) {
734 altfp
= cp
->c_datafork
;
736 cp
->c_rsrcfork
= NULL
;
737 cp
->c_rsrc_vp
= NULL
;
739 panic("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
);
742 * On the last fork, remove the cnode from its hash chain.
745 /* If we can't remove it then the cnode must persist! */
746 if (hfs_chashremove(hfsmp
, cp
) == 0)
749 * Remove any directory hints
751 if (vnode_isdir(vp
)) {
752 hfs_reldirhints(cp
, 0);
755 if(cp
->c_flag
& C_HARDLINK
) {
759 /* Release the file fork and related data */
761 /* Dump cached symlink data */
762 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
763 FREE(fp
->ff_symlinkptr
, M_TEMP
);
765 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
769 * If there was only one active fork then we can release the cnode.
772 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_TRANSIT
);
773 hfs_reclaim_cnode(cp
);
777 * cnode in use. If it is a directory, it could have
778 * no live forks. Just release the lock.
783 vnode_clearfsnode(vp
);
788 extern int (**hfs_vnodeop_p
) (void *);
789 extern int (**hfs_std_vnodeop_p
) (void *);
790 extern int (**hfs_specop_p
) (void *);
792 extern int (**hfs_fifoop_p
) (void *);
796 * hfs_getnewvnode - get new default vnode
798 * The vnode is returned with an iocount and the cnode locked
802 struct hfsmount
*hfsmp
,
804 struct componentname
*cnp
,
805 struct cat_desc
*descp
,
807 struct cat_attr
*attrp
,
808 struct cat_fork
*forkp
,
812 struct mount
*mp
= HFSTOVFS(hfsmp
);
813 struct vnode
*vp
= NULL
;
815 struct vnode
*tvp
= NULLVP
;
816 struct cnode
*cp
= NULL
;
817 struct filefork
*fp
= NULL
;
818 int hfs_standard
= 0;
823 struct vnode_fsparam vfsp
;
829 hfs_standard
= (hfsmp
->hfs_flags
& HFS_STANDARD
);
831 if (attrp
->ca_fileid
== 0) {
837 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
842 vtype
= IFTOVT(attrp
->ca_mode
);
843 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
844 wantrsrc
= flags
& GNV_WANTRSRC
;
846 /* Zero out the out_flags */
849 #ifdef HFS_CHECK_LOCK_ORDER
851 * The only case were its permissible to hold the parent cnode
852 * lock is during a create operation (hfs_makenode) or when
853 * we don't need the cnode lock (GNV_SKIPLOCK).
856 (flags
& (GNV_CREATE
| GNV_SKIPLOCK
)) == 0 &&
857 VTOC(dvp
)->c_lockowner
== current_thread()) {
858 panic("hfs_getnewvnode: unexpected hold of parent cnode %p", VTOC(dvp
));
860 #endif /* HFS_CHECK_LOCK_ORDER */
863 * Get a cnode (new or existing)
865 cp
= hfs_chash_getcnode(hfsmp
, attrp
->ca_fileid
, vpp
, wantrsrc
,
866 (flags
& GNV_SKIPLOCK
), out_flags
, &hflags
);
869 * If the id is no longer valid for lookups we'll get back a NULL cp.
876 * If we get a cnode/vnode pair out of hfs_chash_getcnode, then update the
877 * descriptor in the cnode as needed if the cnode represents a hardlink.
878 * We want the caller to get the most up-to-date copy of the descriptor
879 * as possible. However, we only do anything here if there was a valid vnode.
880 * If there isn't a vnode, then the cnode is brand new and needs to be initialized
881 * as it doesn't have a descriptor or cat_attr yet.
883 * If we are about to replace the descriptor with the user-supplied one, then validate
884 * that the descriptor correctly acknowledges this item is a hardlink. We could be
885 * subject to a race where the calling thread invoked cat_lookup, got a valid lookup
886 * result but the file was not yet a hardlink. With sufficient delay between there
887 * and here, we might accidentally copy in the raw inode ID into the descriptor in the
888 * call below. If the descriptor's CNID is the same as the fileID then it must
889 * not yet have been a hardlink when the lookup occurred.
892 if (!(hfs_checkdeleted(cp
))) {
893 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
894 /* If cnode is uninitialized, its c_attr will be zeroed out; cnids wont match. */
895 if ((descp
->cd_cnid
== cp
->c_attr
.ca_fileid
) &&
896 (attrp
->ca_linkcount
!= cp
->c_attr
.ca_linkcount
)){
897 if ((flags
& GNV_SKIPLOCK
) == 0) {
899 * Then we took the lock. Drop it before calling
900 * vnode_put, which may invoke hfs_vnop_inactive and need to take
901 * the cnode lock again.
907 * Emit ERECYCLE and GNV_CAT_ATTRCHANGED to
908 * force a re-drive in the lookup routine.
909 * Drop the iocount on the vnode obtained from
910 * chash_getcnode if needed.
918 * If we raced with VNOP_RECLAIM for this vnode, the hash code could
919 * have observed it after the c_vp or c_rsrc_vp fields had been torn down;
920 * the hash code peeks at those fields without holding the cnode lock because
921 * it needs to be fast. As a result, we may have set H_ATTACH in the chash
922 * call above. Since we're bailing out, unset whatever flags we just set, and
923 * wake up all waiters for this cnode.
926 hfs_chashwakeup(hfsmp
, cp
, hflags
);
929 *out_flags
= GNV_CAT_ATTRCHANGED
;
933 /* Otherwise, CNID != fileid. Go ahead and copy in the new descriptor */
934 replace_desc(cp
, descp
);
940 /* Check if we found a matching vnode */
946 * If this is a new cnode then initialize it.
948 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
949 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
954 /* Make sure its still valid (ie exists on disk). */
955 if (!(flags
& GNV_CREATE
)) {
957 if (!hfs_valid_cnode (hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
, attrp
, &error
)) {
958 hfs_chash_abort(hfsmp
, cp
);
959 hfs_reclaim_cnode(cp
);
962 * If we hit this case, that means that the entry was there in the catalog when
963 * we did a cat_lookup earlier. Think hfs_lookup. However, in between the time
964 * that we checked the catalog and the time we went to get a vnode/cnode for it,
965 * it had been removed from the namespace and the vnode totally reclaimed. As a result,
966 * it's not there in the catalog during the check in hfs_valid_cnode and we bubble out
967 * an ENOENT. To indicate to the caller that they should really double-check the
968 * entry (it could have been renamed over and gotten a new fileid), we mark a bit
969 * in the output flags.
971 if (error
== ENOENT
) {
972 *out_flags
= GNV_CAT_DELETED
;
977 * Also, we need to protect the cat_attr acquired during hfs_lookup and passed into
978 * this function as an argument because the catalog may have changed w.r.t hardlink
979 * link counts and the firstlink field. If that validation check fails, then let
980 * lookup re-drive itself to get valid/consistent data with the same failure condition below.
982 if (error
== ERECYCLE
) {
983 *out_flags
= GNV_CAT_ATTRCHANGED
;
988 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
989 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
991 /* The name was inherited so clear descriptor state... */
992 descp
->cd_namelen
= 0;
993 descp
->cd_nameptr
= NULL
;
994 descp
->cd_flags
&= ~CD_HASBUF
;
997 if ((vtype
== VREG
|| vtype
== VDIR
) &&
998 ((descp
->cd_cnid
!= attrp
->ca_fileid
) ||
999 (attrp
->ca_recflags
& kHFSHasLinkChainMask
))) {
1000 cp
->c_flag
|= C_HARDLINK
;
1003 * Fix-up dir link counts.
1005 * Earlier versions of Leopard used ca_linkcount for posix
1006 * nlink support (effectively the sub-directory count + 2).
1007 * That is now accomplished using the ca_dircount field with
1008 * the corresponding kHFSHasFolderCountMask flag.
1010 * For directories the ca_linkcount is the true link count,
1011 * tracking the number of actual hardlinks to a directory.
1013 * We only do this if the mount has HFS_FOLDERCOUNT set;
1014 * at the moment, we only set that for HFSX volumes.
1016 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
1018 !(attrp
->ca_recflags
& kHFSHasFolderCountMask
) &&
1019 (cp
->c_attr
.ca_linkcount
> 1)) {
1020 if (cp
->c_attr
.ca_entries
== 0)
1021 cp
->c_attr
.ca_dircount
= 0;
1023 cp
->c_attr
.ca_dircount
= cp
->c_attr
.ca_linkcount
- 2;
1025 cp
->c_attr
.ca_linkcount
= 1;
1026 cp
->c_attr
.ca_recflags
|= kHFSHasFolderCountMask
;
1027 if ( !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) )
1028 cp
->c_flag
|= C_MODIFIED
;
1031 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
1032 for (i
= 0; i
< MAXQUOTAS
; i
++)
1033 cp
->c_dquot
[i
] = NODQUOT
;
1036 /* Mark the output flag that we're vending a new cnode */
1037 *out_flags
|= GNV_NEW_CNODE
;
1040 if (vtype
== VDIR
) {
1041 if (cp
->c_vp
!= NULL
)
1042 panic("hfs_getnewvnode: orphaned vnode (data)");
1045 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
1046 panic("hfs_getnewvnode: bad ca_blocks (too small)");
1048 * Allocate and initialize a file fork...
1050 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
1051 M_HFSFORK
, M_WAITOK
);
1054 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
1056 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
1057 rl_init(&fp
->ff_invalidranges
);
1058 fp
->ff_sysfileinfo
= 0;
1061 if (cp
->c_rsrcfork
!= NULL
)
1062 panic("hfs_getnewvnode: orphaned rsrc fork");
1063 if (cp
->c_rsrc_vp
!= NULL
)
1064 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
1065 cp
->c_rsrcfork
= fp
;
1066 cvpp
= &cp
->c_rsrc_vp
;
1067 if ( (tvp
= cp
->c_vp
) != NULLVP
)
1068 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
1070 if (cp
->c_datafork
!= NULL
)
1071 panic("hfs_getnewvnode: orphaned data fork");
1072 if (cp
->c_vp
!= NULL
)
1073 panic("hfs_getnewvnode: orphaned vnode (data)");
1074 cp
->c_datafork
= fp
;
1076 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
1077 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
1080 if (tvp
!= NULLVP
) {
1082 * grab an iocount on the vnode we weren't
1083 * interested in (i.e. we want the resource fork
1084 * but the cnode already has the data fork)
1085 * to prevent it from being
1086 * recycled by us when we call vnode_create
1087 * which will result in a deadlock when we
1088 * try to take the cnode lock in hfs_vnop_fsync or
1089 * hfs_vnop_reclaim... vnode_get can be called here
1090 * because we already hold the cnode lock which will
1091 * prevent the vnode from changing identity until
1092 * we drop it.. vnode_get will not block waiting for
1093 * a change of state... however, it will return an
1094 * error if the current iocount == 0 and we've already
1095 * started to terminate the vnode... we don't need/want to
1096 * grab an iocount in the case since we can't cause
1097 * the fileystem to be re-entered on this thread for this vp
1099 * the matching vnode_put will happen in hfs_unlock
1100 * after we've dropped the cnode lock
1102 if ( vnode_get(tvp
) != 0)
1103 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
1106 vfsp
.vnfs_vtype
= vtype
;
1107 vfsp
.vnfs_str
= "hfs";
1108 if ((cp
->c_flag
& C_HARDLINK
) && (vtype
== VDIR
)) {
1109 vfsp
.vnfs_dvp
= NULL
; /* no parent for me! */
1110 vfsp
.vnfs_cnp
= NULL
; /* no name for me! */
1112 vfsp
.vnfs_dvp
= dvp
;
1113 vfsp
.vnfs_cnp
= cnp
;
1115 vfsp
.vnfs_fsnode
= cp
;
1118 * Special Case HFS Standard VNOPs from HFS+, since
1119 * HFS standard is readonly/deprecated as of 10.6
1123 if (vtype
== VFIFO
)
1124 vfsp
.vnfs_vops
= hfs_fifoop_p
;
1127 if (vtype
== VBLK
|| vtype
== VCHR
)
1128 vfsp
.vnfs_vops
= hfs_specop_p
;
1129 else if (hfs_standard
)
1130 vfsp
.vnfs_vops
= hfs_std_vnodeop_p
;
1132 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
1134 if (vtype
== VBLK
|| vtype
== VCHR
)
1135 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
1140 vfsp
.vnfs_filesize
= forkp
->cf_size
;
1142 vfsp
.vnfs_filesize
= 0;
1144 vfsp
.vnfs_flags
= VNFS_ADDFSREF
;
1145 if (dvp
== NULLVP
|| cnp
== NULL
|| !(cnp
->cn_flags
& MAKEENTRY
) || (flags
& GNV_NOCACHE
))
1146 vfsp
.vnfs_flags
|= VNFS_NOCACHE
;
1148 /* Tag system files */
1149 vfsp
.vnfs_marksystem
= issystemfile
;
1151 /* Tag root directory */
1152 if (descp
->cd_cnid
== kHFSRootFolderID
)
1153 vfsp
.vnfs_markroot
= 1;
1155 vfsp
.vnfs_markroot
= 0;
1157 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
1159 if (fp
== cp
->c_datafork
)
1160 cp
->c_datafork
= NULL
;
1162 cp
->c_rsrcfork
= NULL
;
1164 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
1167 * If this is a newly created cnode or a vnode reclaim
1168 * occurred during the attachment, then cleanup the cnode.
1170 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
1171 hfs_chash_abort(hfsmp
, cp
);
1172 hfs_reclaim_cnode(cp
);
1175 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_ATTACH
);
1176 if ((flags
& GNV_SKIPLOCK
) == 0){
1184 vnode_settag(vp
, VT_HFS
);
1185 if (cp
->c_flag
& C_HARDLINK
) {
1186 vnode_setmultipath(vp
);
1189 * Tag resource fork vnodes as needing an VNOP_INACTIVE
1190 * so that any deferred removes (open unlinked files)
1191 * have the chance to process the resource fork.
1193 if (VNODE_IS_RSRC(vp
)) {
1195 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 37)), cp
->c_vp
, cp
->c_rsrc_vp
, 0, 0, 0);
1197 /* Force VL_NEEDINACTIVE on this vnode */
1198 err
= vnode_ref(vp
);
1203 hfs_chashwakeup(hfsmp
, cp
, H_ALLOC
| H_ATTACH
);
1206 * Stop tracking an active hot file.
1208 if (!(flags
& GNV_CREATE
) && (vtype
!= VDIR
) && !issystemfile
) {
1209 (void) hfs_removehotfile(vp
);
1213 if (!issystemfile
&& (*out_flags
& GNV_NEW_CNODE
))
1214 cp_entry_init(cp
, mp
);
1223 hfs_reclaim_cnode(struct cnode
*cp
)
1228 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1229 if (cp
->c_dquot
[i
] != NODQUOT
) {
1230 dqreclaim(cp
->c_dquot
[i
]);
1231 cp
->c_dquot
[i
] = NODQUOT
;
1237 * If the descriptor has a name then release it
1239 if ((cp
->c_desc
.cd_flags
& CD_HASBUF
) && (cp
->c_desc
.cd_nameptr
!= 0)) {
1240 const char *nameptr
;
1242 nameptr
= (const char *) cp
->c_desc
.cd_nameptr
;
1243 cp
->c_desc
.cd_nameptr
= 0;
1244 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
1245 cp
->c_desc
.cd_namelen
= 0;
1246 vfs_removename(nameptr
);
1250 * We only call this function if we are in hfs_vnop_reclaim and
1251 * attempting to reclaim a cnode with only one live fork. Because the vnode
1252 * went through reclaim, any future attempts to use this item will have to
1253 * go through lookup again, which will need to create a new vnode. Thus,
1254 * destroying the locks below (while they were still held during our parent
1255 * function hfs_vnop_reclaim) is safe.
1258 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
1259 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
1262 decmpfs_cnode_destroy(cp
->c_decmp
);
1263 FREE_ZONE(cp
->c_decmp
, sizeof(*(cp
->c_decmp
)), M_DECMPFS_CNODE
);
1267 cp_entry_destroy(cp
);
1271 bzero(cp
, sizeof(struct cnode
));
1272 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
1279 * This function is used to validate data that is stored in-core against what is contained
1280 * in the catalog. Common uses include validating that the parent-child relationship still exist
1281 * for a specific directory entry (guaranteeing it has not been renamed into a different spot) at
1282 * the point of the check.
1285 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
,
1286 cnid_t cnid
, struct cat_attr
*cattr
, int *error
)
1288 struct cat_attr attr
;
1289 struct cat_desc cndesc
;
1293 /* System files are always valid */
1294 if (cnid
< kHFSFirstUserCatalogNodeID
) {
1299 /* XXX optimization: check write count in dvp */
1301 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1305 struct cat_fork fork
;
1307 bzero(&cndesc
, sizeof(cndesc
));
1308 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
1309 cndesc
.cd_namelen
= cnp
->cn_namelen
;
1310 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_fileid
;
1311 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
1314 * We have to be careful when calling cat_lookup. The result argument
1315 * 'attr' may get different results based on whether or not you ask
1316 * for the filefork to be supplied as output. This is because cat_lookupbykey
1317 * will attempt to do basic validation/smoke tests against the resident
1318 * extents if there are no overflow extent records, but it needs someplace
1319 * in memory to store the on-disk fork structures.
1321 * Since hfs_lookup calls cat_lookup with a filefork argument, we should
1322 * do the same here, to verify that block count differences are not
1323 * due to calling the function with different styles. cat_lookupbykey
1324 * will request the volume be fsck'd if there is true on-disk corruption
1325 * where the number of blocks does not match the number generated by
1326 * summing the number of blocks in the resident extents.
1329 lookup
= cat_lookup (hfsmp
, &cndesc
, 0, NULL
, &attr
, &fork
, NULL
);
1330 if ((lookup
== 0) && (cnid
== attr
.ca_fileid
)) {
1339 * In hfs_getnewvnode, we may encounter a time-of-check vs. time-of-vnode creation
1340 * race. Specifically, if there is no vnode/cnode pair for the directory entry
1341 * being looked up, we have to go to the catalog. But since we don't hold any locks (aside
1342 * from the dvp in 'shared' mode) there is nothing to protect us against the catalog record
1343 * changing in between the time we do the cat_lookup there and the time we re-grab the
1344 * catalog lock above to do another cat_lookup.
1346 * However, we need to check more than just the CNID and parent-child name relationships above.
1347 * Hardlinks can suffer the same race in the following scenario: Suppose we do a
1348 * cat_lookup, and find a leaf record and a raw inode for a hardlink. Now, we have
1349 * the cat_attr in hand (passed in above). But in between then and now, the vnode was
1350 * created by a competing hfs_getnewvnode call, and is manipulated and reclaimed before we get
1351 * a chance to do anything. This is possible if there are a lot of threads thrashing around
1352 * with the cnode hash. In this case, if we don't check/validate the cat_attr in-hand, we will
1353 * blindly stuff it into the cnode, which will make the in-core data inconsistent with what is
1354 * on disk. So validate the cat_attr below, if required. This race cannot happen if the cnode/vnode
1355 * already exists, as it does in the case of rename and delete.
1357 if (stillvalid
&& cattr
!= NULL
) {
1358 if (cattr
->ca_linkcount
!= attr
.ca_linkcount
) {
1364 if (cattr
->ca_union1
.cau_linkref
!= attr
.ca_union1
.cau_linkref
) {
1370 if (cattr
->ca_union3
.cau_firstlink
!= attr
.ca_union3
.cau_firstlink
) {
1376 if (cattr
->ca_union2
.cau_blocks
!= attr
.ca_union2
.cau_blocks
) {
1383 if (cat_idlookup(hfsmp
, cnid
, 0, NULL
, NULL
, NULL
) == 0) {
1392 hfs_systemfile_unlock(hfsmp
, lockflags
);
1394 return (stillvalid
);
1398 * Per HI and Finder requirements, HFS should add in the
1399 * date/time that a particular directory entry was added
1400 * to the containing directory.
1401 * This is stored in the extended Finder Info for the
1404 * Note that this field is also set explicitly in the hfs_vnop_setxattr code.
1405 * We must ignore user attempts to set this part of the finderinfo, and
1406 * so we need to save a local copy of the date added, write in the user
1407 * finderinfo, then stuff the value back in.
1409 void hfs_write_dateadded (struct cat_attr
*attrp
, u_int32_t dateadded
) {
1410 u_int8_t
*finfo
= NULL
;
1412 /* overlay the FinderInfo to the correct pointer, and advance */
1413 finfo
= (u_int8_t
*)attrp
->ca_finderinfo
;
1417 * Make sure to write it out as big endian, since that's how
1418 * finder info is defined.
1420 * NOTE: This is a Unix-epoch timestamp, not a HFS/Traditional Mac timestamp.
1422 if (S_ISREG(attrp
->ca_mode
)) {
1423 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1424 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
1425 attrp
->ca_recflags
|= kHFSHasDateAddedMask
;
1427 else if (S_ISDIR(attrp
->ca_mode
)) {
1428 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1429 extinfo
->date_added
= OSSwapHostToBigInt32(dateadded
);
1430 attrp
->ca_recflags
|= kHFSHasDateAddedMask
;
1433 /* If it were neither directory/file, then we'd bail out */
1437 u_int32_t
hfs_get_dateadded (struct cnode
*cp
) {
1438 u_int8_t
*finfo
= NULL
;
1439 u_int32_t dateadded
= 0;
1441 if ((cp
->c_attr
.ca_recflags
& kHFSHasDateAddedMask
) == 0) {
1442 /* Date added was never set. Return 0. */
1447 /* overlay the FinderInfo to the correct pointer, and advance */
1448 finfo
= (u_int8_t
*)cp
->c_finderinfo
;
1452 * FinderInfo is written out in big endian... make sure to convert it to host
1453 * native before we use it.
1455 if (S_ISREG(cp
->c_attr
.ca_mode
)) {
1456 struct FndrExtendedFileInfo
*extinfo
= (struct FndrExtendedFileInfo
*)finfo
;
1457 dateadded
= OSSwapBigToHostInt32 (extinfo
->date_added
);
1459 else if (S_ISDIR(cp
->c_attr
.ca_mode
)) {
1460 struct FndrExtendedDirInfo
*extinfo
= (struct FndrExtendedDirInfo
*)finfo
;
1461 dateadded
= OSSwapBigToHostInt32 (extinfo
->date_added
);
1470 * Touch cnode times based on c_touch_xxx flags
1472 * cnode must be locked exclusive
1474 * This will also update the volume modify time
1477 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
1480 /* don't modify times if volume is read-only */
1481 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1482 cp
->c_touch_acctime
= FALSE
;
1483 cp
->c_touch_chgtime
= FALSE
;
1484 cp
->c_touch_modtime
= FALSE
;
1487 else if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1488 /* HFS Standard doesn't support access times */
1489 cp
->c_touch_acctime
= FALSE
;
1492 ctx
= vfs_context_current();
1494 * Skip access time updates if:
1495 * . MNT_NOATIME is set
1496 * . a file system freeze is in progress
1497 * . a file system resize is in progress
1498 * . the vnode associated with this cnode is marked for rapid aging
1500 if (cp
->c_touch_acctime
) {
1501 if ((vfs_flags(hfsmp
->hfs_mp
) & MNT_NOATIME
) ||
1502 (hfsmp
->hfs_freezing_proc
!= NULL
) ||
1503 (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) ||
1504 (cp
->c_vp
&& ((vnode_israge(cp
->c_vp
) || (vfs_ctx_skipatime(ctx
)))))) {
1506 cp
->c_touch_acctime
= FALSE
;
1509 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
||
1510 cp
->c_touch_modtime
|| (cp
->c_flag
& C_NEEDS_DATEADDED
)) {
1516 if (cp
->c_touch_acctime
) {
1517 cp
->c_atime
= tv
.tv_sec
;
1519 * When the access time is the only thing changing
1520 * then make sure its sufficiently newer before
1521 * committing it to disk.
1523 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
1524 ATIME_ONDISK_ACCURACY
)) {
1525 cp
->c_flag
|= C_MODIFIED
;
1527 cp
->c_touch_acctime
= FALSE
;
1529 if (cp
->c_touch_modtime
) {
1530 cp
->c_mtime
= tv
.tv_sec
;
1531 cp
->c_touch_modtime
= FALSE
;
1532 cp
->c_flag
|= C_MODIFIED
;
1536 * HFS dates that WE set must be adjusted for DST
1538 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
1539 cp
->c_mtime
+= 3600;
1543 if (cp
->c_touch_chgtime
) {
1544 cp
->c_ctime
= tv
.tv_sec
;
1545 cp
->c_touch_chgtime
= FALSE
;
1546 cp
->c_flag
|= C_MODIFIED
;
1550 if (cp
->c_flag
& C_NEEDS_DATEADDED
) {
1551 hfs_write_dateadded (&(cp
->c_attr
), tv
.tv_sec
);
1552 cp
->c_flag
|= C_MODIFIED
;
1553 /* untwiddle the bit */
1554 cp
->c_flag
&= ~C_NEEDS_DATEADDED
;
1558 /* Touch the volume modtime if needed */
1560 MarkVCBDirty(hfsmp
);
1561 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
1570 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
1572 void * thread
= current_thread();
1574 if (cp
->c_lockowner
== thread
) {
1576 * Only the extents and bitmap file's support lock recursion.
1578 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1579 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1580 cp
->c_syslockcount
++;
1582 panic("hfs_lock: locking against myself!");
1584 } else if (locktype
== HFS_SHARED_LOCK
) {
1585 lck_rw_lock_shared(&cp
->c_rwlock
);
1586 cp
->c_lockowner
= HFS_SHARED_OWNER
;
1588 } else /* HFS_EXCLUSIVE_LOCK */ {
1589 lck_rw_lock_exclusive(&cp
->c_rwlock
);
1590 cp
->c_lockowner
= thread
;
1593 * Only the extents and bitmap file's support lock recursion.
1595 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1596 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1597 cp
->c_syslockcount
= 1;
1601 #ifdef HFS_CHECK_LOCK_ORDER
1603 * Regular cnodes (non-system files) cannot be locked
1604 * while holding the journal lock or a system file lock.
1606 if (!(cp
->c_desc
.cd_flags
& CD_ISMETA
) &&
1607 ((cp
->c_fileid
> kHFSFirstUserCatalogNodeID
) || (cp
->c_fileid
== kHFSRootFolderID
))) {
1608 vnode_t vp
= NULLVP
;
1610 /* Find corresponding vnode. */
1611 if (cp
->c_vp
!= NULLVP
&& VTOC(cp
->c_vp
) == cp
) {
1613 } else if (cp
->c_rsrc_vp
!= NULLVP
&& VTOC(cp
->c_rsrc_vp
) == cp
) {
1617 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1619 if (hfsmp
->jnl
&& (journal_owner(hfsmp
->jnl
) == thread
)) {
1620 /* This will eventually be a panic here. */
1621 printf("hfs_lock: bad lock order (cnode after journal)\n");
1623 if (hfsmp
->hfs_catalog_cp
&& hfsmp
->hfs_catalog_cp
->c_lockowner
== thread
) {
1624 panic("hfs_lock: bad lock order (cnode after catalog)");
1626 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== thread
) {
1627 panic("hfs_lock: bad lock order (cnode after attribute)");
1629 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== thread
) {
1630 panic("hfs_lock: bad lock order (cnode after extents)");
1634 #endif /* HFS_CHECK_LOCK_ORDER */
1637 * Skip cnodes that no longer exist (were deleted).
1639 if ((locktype
!= HFS_FORCE_LOCK
) &&
1640 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
1641 (cp
->c_flag
& C_NOEXISTS
)) {
1649 * Lock a pair of cnodes.
1652 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
1654 struct cnode
*first
, *last
;
1658 * If cnodes match then just lock one.
1661 return hfs_lock(cp1
, locktype
);
1665 * Lock in cnode address order.
1675 if ( (error
= hfs_lock(first
, locktype
))) {
1678 if ( (error
= hfs_lock(last
, locktype
))) {
1686 * Check ordering of two cnodes. Return true if they are are in-order.
1689 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
1693 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
1695 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
1698 * Locking order is cnode address order.
1704 * Acquire 4 cnode locks.
1705 * - locked in cnode address order (lesser address first).
1706 * - all or none of the locks are taken
1707 * - only one lock taken per cnode (dup cnodes are skipped)
1708 * - some of the cnode pointers may be null
1711 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
1712 struct cnode
*cp4
, enum hfslocktype locktype
, struct cnode
**error_cnode
)
1714 struct cnode
* a
[3];
1715 struct cnode
* b
[3];
1716 struct cnode
* list
[4];
1721 *error_cnode
= NULL
;
1724 if (hfs_isordered(cp1
, cp2
)) {
1725 a
[0] = cp1
; a
[1] = cp2
;
1727 a
[0] = cp2
; a
[1] = cp1
;
1729 if (hfs_isordered(cp3
, cp4
)) {
1730 b
[0] = cp3
; b
[1] = cp4
;
1732 b
[0] = cp4
; b
[1] = cp3
;
1734 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1735 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1738 * Build the lock list, skipping over duplicates
1740 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
1741 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
1742 if (k
== 0 || tmp
!= list
[k
-1])
1747 * Now we can lock using list[0 - k].
1748 * Skip over NULL entries.
1750 for (i
= 0; i
< k
; ++i
) {
1752 if ((error
= hfs_lock(list
[i
], locktype
))) {
1753 /* Only stuff error_cnode if requested */
1755 *error_cnode
= list
[i
];
1757 /* Drop any locks we acquired. */
1760 hfs_unlock(list
[i
]);
1773 hfs_unlock(struct cnode
*cp
)
1775 vnode_t rvp
= NULLVP
;
1776 vnode_t vp
= NULLVP
;
1781 * Only the extents and bitmap file's support lock recursion.
1783 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1784 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1785 if (--cp
->c_syslockcount
> 0) {
1789 c_flag
= cp
->c_flag
;
1790 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
| C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
);
1792 if (c_flag
& (C_NEED_DVNODE_PUT
| C_NEED_DATA_SETSIZE
)) {
1795 if (c_flag
& (C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
)) {
1796 rvp
= cp
->c_rsrc_vp
;
1799 lockowner
= cp
->c_lockowner
;
1800 if (lockowner
== current_thread()) {
1801 cp
->c_lockowner
= NULL
;
1802 lck_rw_unlock_exclusive(&cp
->c_rwlock
);
1804 lck_rw_unlock_shared(&cp
->c_rwlock
);
1807 /* Perform any vnode post processing after cnode lock is dropped. */
1809 if (c_flag
& C_NEED_DATA_SETSIZE
)
1811 if (c_flag
& C_NEED_DVNODE_PUT
)
1815 if (c_flag
& C_NEED_RSRC_SETSIZE
)
1816 ubc_setsize(rvp
, 0);
1817 if (c_flag
& C_NEED_RVNODE_PUT
)
1823 * Unlock a pair of cnodes.
1826 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1834 * Unlock a group of cnodes.
1837 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1839 struct cnode
* list
[4];
1847 for (i
= 0; i
< k
; ++i
) {
1856 for (i
= 0; i
< k
; ++i
) {
1865 for (i
= 0; i
< k
; ++i
) {
1875 * Protect a cnode against a truncation.
1877 * Used mainly by read/write since they don't hold the
1878 * cnode lock across calls to the cluster layer.
1880 * The process doing a truncation must take the lock
1881 * exclusive. The read/write processes can take it
1882 * shared. The locktype argument is the same as supplied to
1886 hfs_lock_truncate(struct cnode
*cp
, enum hfslocktype locktype
)
1888 void * thread
= current_thread();
1890 if (cp
->c_truncatelockowner
== thread
) {
1892 * Only HFS_RECURSE_TRUNCLOCK is allowed to recurse.
1894 * This is needed on the hfs_vnop_pagein path where we need to ensure
1895 * the file does not change sizes while we are paging in. However,
1896 * we may already hold the lock exclusive due to another
1897 * VNOP from earlier in the call stack. So if we already hold
1898 * the truncate lock exclusive, allow it to proceed, but ONLY if
1899 * it's in the recursive case.
1901 if (locktype
!= HFS_RECURSE_TRUNCLOCK
) {
1902 panic("hfs_lock_truncate: cnode %p locked!", cp
);
1905 /* HFS_RECURSE_TRUNCLOCK takes a shared lock if it is not already locked */
1906 else if ((locktype
== HFS_SHARED_LOCK
) || (locktype
== HFS_RECURSE_TRUNCLOCK
)) {
1907 lck_rw_lock_shared(&cp
->c_truncatelock
);
1908 cp
->c_truncatelockowner
= HFS_SHARED_OWNER
;
1910 else { /* must be an HFS_EXCLUSIVE_LOCK */
1911 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1912 cp
->c_truncatelockowner
= thread
;
1918 * Attempt to get the truncate lock. If it cannot be acquired, error out.
1919 * This function is needed in the degenerate hfs_vnop_pagein during force unmount
1920 * case. To prevent deadlocks while a VM copy object is moving pages, HFS vnop pagein will
1921 * temporarily need to disable V2 semantics.
1923 int hfs_try_trunclock (struct cnode
*cp
, enum hfslocktype locktype
) {
1924 void * thread
= current_thread();
1925 boolean_t didlock
= false;
1927 if (cp
->c_truncatelockowner
== thread
) {
1929 * Only HFS_RECURSE_TRUNCLOCK is allowed to recurse.
1931 * This is needed on the hfs_vnop_pagein path where we need to ensure
1932 * the file does not change sizes while we are paging in. However,
1933 * we may already hold the lock exclusive due to another
1934 * VNOP from earlier in the call stack. So if we already hold
1935 * the truncate lock exclusive, allow it to proceed, but ONLY if
1936 * it's in the recursive case.
1938 if (locktype
!= HFS_RECURSE_TRUNCLOCK
) {
1939 panic("hfs_lock_truncate: cnode %p locked!", cp
);
1942 /* HFS_RECURSE_TRUNCLOCK takes a shared lock if it is not already locked */
1943 else if ((locktype
== HFS_SHARED_LOCK
) || (locktype
== HFS_RECURSE_TRUNCLOCK
)) {
1944 didlock
= lck_rw_try_lock(&cp
->c_truncatelock
, LCK_RW_TYPE_SHARED
);
1946 cp
->c_truncatelockowner
= HFS_SHARED_OWNER
;
1949 else { /* must be an HFS_EXCLUSIVE_LOCK */
1950 didlock
= lck_rw_try_lock (&cp
->c_truncatelock
, LCK_RW_TYPE_EXCLUSIVE
);
1952 cp
->c_truncatelockowner
= thread
;
1961 * Unlock the truncate lock, which protects against size changes.
1963 * The been_recursed argument is used when we may need to return
1964 * from this function without actually unlocking the truncate lock.
1967 hfs_unlock_truncate(struct cnode
*cp
, int been_recursed
)
1969 void *thread
= current_thread();
1972 * If been_recursed is nonzero AND the current lock owner of the
1973 * truncate lock is our current thread, then we must have recursively
1974 * taken the lock earlier on. If the lock were unlocked,
1975 * HFS_RECURSE_TRUNCLOCK took a shared lock and it would fall through
1976 * to the SHARED case below.
1978 * If been_recursed is zero (most of the time) then we check the
1979 * lockowner field to infer whether the lock was taken exclusively or
1980 * shared in order to know what underlying lock routine to call.
1982 if (been_recursed
) {
1983 if (cp
->c_truncatelockowner
== thread
) {
1988 /* HFS_LOCK_EXCLUSIVE */
1989 if (thread
== cp
->c_truncatelockowner
) {
1990 cp
->c_truncatelockowner
= NULL
;
1991 lck_rw_unlock_exclusive(&cp
->c_truncatelock
);
1993 /* HFS_LOCK_SHARED */
1995 lck_rw_unlock_shared(&cp
->c_truncatelock
);