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>
40 #include <kern/locks.h>
42 #include <miscfs/specfs/specdev.h>
43 #include <miscfs/fifofs/fifo.h>
46 #include <hfs/hfs_catalog.h>
47 #include <hfs/hfs_cnode.h>
48 #include <hfs/hfs_quota.h>
52 extern lck_attr_t
* hfs_lock_attr
;
53 extern lck_grp_t
* hfs_mutex_group
;
54 extern lck_grp_t
* hfs_rwlock_group
;
56 static int hfs_filedone(struct vnode
*vp
, vfs_context_t context
);
58 static void hfs_reclaim_cnode(struct cnode
*);
60 static int hfs_isordered(struct cnode
*, struct cnode
*);
64 * Last reference to an cnode. If necessary, write or delete it.
68 hfs_vnop_inactive(struct vnop_inactive_args
*ap
)
70 struct vnode
*vp
= ap
->a_vp
;
72 struct hfsmount
*hfsmp
= VTOHFS(vp
);
73 struct proc
*p
= vfs_context_proc(ap
->a_context
);
79 int took_trunc_lock
= 0;
85 v_type
= vnode_vtype(vp
);
88 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || vnode_issystem(vp
) ||
89 (hfsmp
->hfs_freezing_proc
== p
)) {
94 * Ignore nodes related to stale file handles.
96 if (cp
->c_mode
== 0) {
101 if ((v_type
== VREG
|| v_type
== VLNK
)) {
102 hfs_lock_truncate(cp
, TRUE
);
106 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
109 * We should lock cnode before checking the flags in the
110 * condition below and should unlock the cnode before calling
111 * ubc_setsize() as cluster code can call other HFS vnops which
112 * will try to acquire the same cnode lock and cause deadlock.
114 if ((v_type
== VREG
|| v_type
== VLNK
) &&
115 (cp
->c_flag
& C_DELETED
) &&
116 (VTOF(vp
)->ff_blocks
!= 0)) {
119 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
122 if (v_type
== VREG
&& !ISSET(cp
->c_flag
, C_DELETED
) && VTOF(vp
)->ff_blocks
) {
123 hfs_filedone(vp
, ap
->a_context
);
126 * Remove any directory hints or cached origins
128 if (v_type
== VDIR
) {
129 hfs_reldirhints(cp
, 0);
130 if (cp
->c_flag
& C_HARDLINK
)
139 /* If needed, get rid of any fork's data for a deleted file */
140 if ((v_type
== VREG
|| v_type
== VLNK
) && (cp
->c_flag
& C_DELETED
)) {
141 if (VTOF(vp
)->ff_blocks
!= 0) {
143 * Since we're already inside a transaction,
144 * tell hfs_truncate to skip the ubc_setsize.
146 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
154 * Check if there's any resource fork blocks that need to
155 * be reclaimed. This covers the case where there is a
156 * resource fork but its not in core.
158 if ((cp
->c_blocks
> 0) && (forkcount
== 1) && (vp
!= cp
->c_rsrc_vp
)) {
159 struct vnode
*rvp
= NULLVP
;
161 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, FALSE
);
165 * Defer the vnode_put and ubc_setsize on rvp until hfs_unlock().
167 cp
->c_flag
|= C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
;
168 error
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
171 vnode_recycle(rvp
); /* all done with this vnode */
175 // If needed, get rid of any xattrs that this file may have.
176 // Note that this must happen outside of any other transactions
177 // because it starts/ends its own transactions and grabs its
178 // own locks. This is to prevent a file with a lot of attributes
179 // from creating a transaction that is too large (which panics).
181 if ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0 && (cp
->c_flag
& C_DELETED
)) {
182 hfs_removeallattr(hfsmp
, cp
->c_fileid
);
186 * Check for a postponed deletion.
187 * (only delete cnode when the last fork goes inactive)
189 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
191 * Mark cnode in transit so that no one can get this
192 * cnode from cnode hash.
194 // hfs_chash_mark_in_transit(cp);
195 // XXXdbg - remove the cnode from the hash table since it's deleted
196 // otherwise someone could go to sleep on the cnode and not
197 // be woken up until this vnode gets recycled which could be
198 // a very long time...
201 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
204 if (started_tr
== 0) {
205 if (hfs_start_transaction(hfsmp
) != 0) {
213 * Reserve some space in the Catalog file.
215 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
220 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
222 if (cp
->c_blocks
> 0) {
223 printf("hfs_inactive: deleting non-empty%sfile %d, "
224 "blks %d\n", VNODE_IS_RSRC(vp
) ? " rsrc " : " ",
225 (int)cp
->c_fileid
, (int)cp
->c_blocks
);
229 // release the name pointer in the descriptor so that
230 // cat_delete() will use the file-id to do the deletion.
231 // in the case of hard links this is imperative (in the
232 // case of regular files the fileid and cnid are the
233 // same so it doesn't matter).
235 cat_releasedesc(&cp
->c_desc
);
238 * The descriptor name may be zero,
239 * in which case the fileid is used.
241 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
243 if (error
&& truncated
&& (error
!= ENXIO
))
244 printf("hfs_inactive: couldn't delete a truncated file!");
246 /* Update HFS Private Data dir */
248 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
--;
249 if (vnode_isdir(vp
)) {
250 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
252 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
253 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
256 hfs_systemfile_unlock(hfsmp
, lockflags
);
262 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
263 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
267 cp
->c_flag
&= ~C_DELETED
;
268 cp
->c_touch_chgtime
= TRUE
;
269 cp
->c_touch_modtime
= TRUE
;
272 hfs_volupdate(hfsmp
, (v_type
== VDIR
) ? VOL_RMDIR
: VOL_RMFILE
, 0);
276 * A file may have had delayed allocations, in which case hfs_update
277 * would not have updated the catalog record (cat_update). We need
278 * to do that now, before we lose our fork data. We also need to
279 * force the update, or hfs_update will again skip the cat_update.
281 if ((cp
->c_flag
& C_MODIFIED
) ||
282 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
283 if ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_modtime
){
284 cp
->c_flag
|= C_FORCEUPDATE
;
290 cat_postflight(hfsmp
, &cookie
, p
);
292 // XXXdbg - have to do this because a goto could have come here
294 hfs_end_transaction(hfsmp
);
301 hfs_unlock_truncate(cp
, TRUE
);
304 * If we are done with the vnode, reclaim it
305 * so that it can be reused immediately.
307 if (cp
->c_mode
== 0 || recycle
)
314 * File clean-up (zero fill and shrink peof).
317 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
321 struct hfsmount
*hfsmp
;
323 u_long blks
, blocksize
;
330 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
334 (void) cluster_push(vp
, IO_CLOSE
);
335 hfs_lock(cp
, HFS_FORCE_LOCK
);
338 * Explicitly zero out the areas of file
339 * that are currently marked invalid.
341 while (!CIRCLEQ_EMPTY(&fp
->ff_invalidranges
)) {
342 struct rl_entry
*invalid_range
= CIRCLEQ_FIRST(&fp
->ff_invalidranges
);
343 off_t start
= invalid_range
->rl_start
;
344 off_t end
= invalid_range
->rl_end
;
346 /* The range about to be written must be validated
347 * first, so that VNOP_BLOCKMAP() will return the
348 * appropriate mapping for the cluster code:
350 rl_remove(start
, end
, &fp
->ff_invalidranges
);
353 (void) cluster_write(vp
, (struct uio
*) 0,
354 leof
, end
+ 1, start
, (off_t
)0,
355 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
356 hfs_lock(cp
, HFS_FORCE_LOCK
);
357 cp
->c_flag
|= C_MODIFIED
;
359 cp
->c_flag
&= ~C_ZFWANTSYNC
;
361 blocksize
= VTOVCB(vp
)->blockSize
;
362 blks
= leof
/ blocksize
;
363 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
366 * Shrink the peof to the smallest size neccessary to contain the leof.
368 if (blks
< fp
->ff_blocks
)
369 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, context
);
371 (void) cluster_push(vp
, IO_CLOSE
);
372 hfs_lock(cp
, HFS_FORCE_LOCK
);
375 * If the hfs_truncate didn't happen to flush the vnode's
376 * information out to disk, force it to be updated now that
377 * all invalid ranges have been zero-filled and validated:
379 if (cp
->c_flag
& C_MODIFIED
) {
387 * Reclaim a cnode so that it can be used for other purposes.
391 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
393 struct vnode
*vp
= ap
->a_vp
;
395 struct filefork
*fp
= NULL
;
396 struct filefork
*altfp
= NULL
;
397 int reclaim_cnode
= 0;
399 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
403 * Check if a deleted resource fork vnode missed a
404 * VNOP_INACTIVE call and requires truncation.
406 if (VNODE_IS_RSRC(vp
) &&
407 (cp
->c_flag
& C_DELETED
) &&
408 (VTOF(vp
)->ff_blocks
!= 0)) {
412 hfs_lock_truncate(cp
, TRUE
);
413 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
415 (void) hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
417 hfs_unlock_truncate(cp
, TRUE
);
420 * A file may have had delayed allocations, in which case hfs_update
421 * would not have updated the catalog record (cat_update). We need
422 * to do that now, before we lose our fork data. We also need to
423 * force the update, or hfs_update will again skip the cat_update.
425 if ((cp
->c_flag
& C_MODIFIED
) ||
426 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
427 if ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_modtime
){
428 cp
->c_flag
|= C_FORCEUPDATE
;
434 * Keep track of an inactive hot file.
436 if (!vnode_isdir(vp
) &&
437 !vnode_issystem(vp
) &&
438 !(cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ) {
439 (void) hfs_addhotfile(vp
);
441 vnode_removefsref(vp
);
444 * Find file fork for this vnode (if any)
445 * Also check if another fork is active
447 if (cp
->c_vp
== vp
) {
449 altfp
= cp
->c_rsrcfork
;
451 cp
->c_datafork
= NULL
;
453 } else if (cp
->c_rsrc_vp
== vp
) {
455 altfp
= cp
->c_datafork
;
457 cp
->c_rsrcfork
= NULL
;
458 cp
->c_rsrc_vp
= NULL
;
460 panic("hfs_vnop_reclaim: vp points to wrong cnode\n");
463 * On the last fork, remove the cnode from its hash chain.
466 /* If we can't remove it then the cnode must persist! */
467 if (hfs_chashremove(cp
) == 0)
470 * Remove any directory hints
472 if (vnode_isdir(vp
)) {
473 hfs_reldirhints(cp
, 0);
476 /* Release the file fork and related data */
478 /* Dump cached symlink data */
479 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
480 FREE(fp
->ff_symlinkptr
, M_TEMP
);
482 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
486 * If there was only one active fork then we can release the cnode.
489 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
490 hfs_reclaim_cnode(cp
);
491 } else /* cnode in use */ {
495 vnode_clearfsnode(vp
);
500 extern int (**hfs_vnodeop_p
) (void *);
501 extern int (**hfs_specop_p
) (void *);
503 extern int (**hfs_fifoop_p
) (void *);
507 * hfs_getnewvnode - get new default vnode
509 * The vnode is returned with an iocount and the cnode locked
514 struct hfsmount
*hfsmp
,
516 struct componentname
*cnp
,
517 struct cat_desc
*descp
,
519 struct cat_attr
*attrp
,
520 struct cat_fork
*forkp
,
523 struct mount
*mp
= HFSTOVFS(hfsmp
);
524 struct vnode
*vp
= NULL
;
526 struct vnode
*tvp
= NULLVP
;
527 struct cnode
*cp
= NULL
;
528 struct filefork
*fp
= NULL
;
532 struct vnode_fsparam vfsp
;
538 if (attrp
->ca_fileid
== 0) {
544 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
549 vtype
= IFTOVT(attrp
->ca_mode
);
550 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
551 wantrsrc
= flags
& GNV_WANTRSRC
;
553 #ifdef HFS_CHECK_LOCK_ORDER
555 * The only case were its permissible to hold the parent cnode
556 * lock is during a create operation (hfs_makenode) or when
557 * we don't need the cnode lock (GNV_SKIPLOCK).
560 (flags
& (GNV_CREATE
| GNV_SKIPLOCK
)) == 0 &&
561 VTOC(dvp
)->c_lockowner
== current_thread()) {
562 panic("hfs_getnewvnode: unexpected hold of parent cnode %p", VTOC(dvp
));
564 #endif /* HFS_CHECK_LOCK_ORDER */
567 * Get a cnode (new or existing)
569 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (flags
& GNV_SKIPLOCK
));
572 * If the id is no longer valid for lookups we'll get back a NULL cp.
578 /* Hardlinks may need an updated catalog descriptor */
579 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
580 replace_desc(cp
, descp
);
582 /* Check if we found a matching vnode */
587 * If this is a new cnode then initialize it.
589 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
590 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
592 /* Make sure its still valid (ie exists on disk). */
593 if (!(flags
& GNV_CREATE
) &&
594 !hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
596 hfs_reclaim_cnode(cp
);
600 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
601 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
603 /* The name was inherited so clear descriptor state... */
604 descp
->cd_namelen
= 0;
605 descp
->cd_nameptr
= NULL
;
606 descp
->cd_flags
&= ~CD_HASBUF
;
609 if ((vtype
== VREG
|| vtype
== VDIR
) &&
610 ((descp
->cd_cnid
!= attrp
->ca_fileid
) ||
611 (attrp
->ca_recflags
& kHFSHasLinkChainMask
))) {
612 cp
->c_flag
|= C_HARDLINK
;
615 * Fix-up dir link counts.
617 * Earlier versions of Leopard used ca_linkcount for posix
618 * nlink support (effectively the sub-directory count + 2).
619 * That is now accomplished using the ca_dircount field with
620 * the corresponding kHFSHasFolderCountMask flag.
622 * For directories the ca_linkcount is the true link count,
623 * tracking the number of actual hardlinks to a directory.
625 * We only do this if the mount has HFS_FOLDERCOUNT set;
626 * at the moment, we only set that for HFSX volumes.
628 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
630 !(attrp
->ca_recflags
& kHFSHasFolderCountMask
) &&
631 (cp
->c_attr
.ca_linkcount
> 1)) {
632 if (cp
->c_attr
.ca_entries
== 0)
633 cp
->c_attr
.ca_dircount
= 0;
635 cp
->c_attr
.ca_dircount
= cp
->c_attr
.ca_linkcount
- 2;
637 cp
->c_attr
.ca_linkcount
= 1;
638 cp
->c_attr
.ca_recflags
|= kHFSHasFolderCountMask
;
639 if ( !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) )
640 cp
->c_flag
|= C_MODIFIED
;
643 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
644 for (i
= 0; i
< MAXQUOTAS
; i
++)
645 cp
->c_dquot
[i
] = NODQUOT
;
651 if (cp
->c_vp
!= NULL
)
652 panic("hfs_getnewvnode: orphaned vnode (data)");
655 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
656 panic("hfs_getnewvnode: bad ca_blocks (too small)");
658 * Allocate and initialize a file fork...
660 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
661 M_HFSFORK
, M_WAITOK
);
664 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
666 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
667 rl_init(&fp
->ff_invalidranges
);
668 fp
->ff_sysfileinfo
= 0;
671 if (cp
->c_rsrcfork
!= NULL
)
672 panic("hfs_getnewvnode: orphaned rsrc fork");
673 if (cp
->c_rsrc_vp
!= NULL
)
674 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
676 cvpp
= &cp
->c_rsrc_vp
;
677 if ( (tvp
= cp
->c_vp
) != NULLVP
)
678 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
680 if (cp
->c_datafork
!= NULL
)
681 panic("hfs_getnewvnode: orphaned data fork");
682 if (cp
->c_vp
!= NULL
)
683 panic("hfs_getnewvnode: orphaned vnode (data)");
686 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
687 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
692 * grab an iocount on the vnode we weren't
693 * interested in (i.e. we want the resource fork
694 * but the cnode already has the data fork)
695 * to prevent it from being
696 * recycled by us when we call vnode_create
697 * which will result in a deadlock when we
698 * try to take the cnode lock in hfs_vnop_fsync or
699 * hfs_vnop_reclaim... vnode_get can be called here
700 * because we already hold the cnode lock which will
701 * prevent the vnode from changing identity until
702 * we drop it.. vnode_get will not block waiting for
703 * a change of state... however, it will return an
704 * error if the current iocount == 0 and we've already
705 * started to terminate the vnode... we don't need/want to
706 * grab an iocount in the case since we can't cause
707 * the fileystem to be re-entered on this thread for this vp
709 * the matching vnode_put will happen in hfs_unlock
710 * after we've dropped the cnode lock
712 if ( vnode_get(tvp
) != 0)
713 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
716 vfsp
.vnfs_vtype
= vtype
;
717 vfsp
.vnfs_str
= "hfs";
718 if ((cp
->c_flag
& C_HARDLINK
) && (vtype
== VDIR
)) {
719 vfsp
.vnfs_dvp
= NULL
; /* no parent for me! */
720 vfsp
.vnfs_cnp
= NULL
; /* no name for me! */
725 vfsp
.vnfs_fsnode
= cp
;
728 vfsp
.vnfs_vops
= hfs_fifoop_p
;
731 if (vtype
== VBLK
|| vtype
== VCHR
)
732 vfsp
.vnfs_vops
= hfs_specop_p
;
734 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
736 if (vtype
== VBLK
|| vtype
== VCHR
)
737 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
742 vfsp
.vnfs_filesize
= forkp
->cf_size
;
744 vfsp
.vnfs_filesize
= 0;
746 vfsp
.vnfs_flags
= VNFS_ADDFSREF
;
747 if (dvp
== NULLVP
|| cnp
== NULL
|| !(cnp
->cn_flags
& MAKEENTRY
))
748 vfsp
.vnfs_flags
|= VNFS_NOCACHE
;
750 /* Tag system files */
751 vfsp
.vnfs_marksystem
= issystemfile
;
753 /* Tag root directory */
754 if (descp
->cd_cnid
== kHFSRootFolderID
)
755 vfsp
.vnfs_markroot
= 1;
757 vfsp
.vnfs_markroot
= 0;
759 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
761 if (fp
== cp
->c_datafork
)
762 cp
->c_datafork
= NULL
;
764 cp
->c_rsrcfork
= NULL
;
766 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
769 * If this is a newly created cnode or a vnode reclaim
770 * occurred during the attachment, then cleanup the cnode.
772 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
774 hfs_reclaim_cnode(cp
);
777 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
778 if ((flags
& GNV_SKIPLOCK
) == 0){
786 vnode_settag(vp
, VT_HFS
);
787 if (cp
->c_flag
& C_HARDLINK
) {
788 vnode_setmultipath(vp
);
791 * Tag resource fork vnodes as needing an VNOP_INACTIVE
792 * so that any deferred removes (open unlinked files)
793 * have the chance to process the resource fork.
795 if (VNODE_IS_RSRC(vp
)) {
796 /* Force VL_NEEDINACTIVE on this vnode */
800 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
803 * Stop tracking an active hot file.
805 if (!(flags
& GNV_CREATE
) && (vtype
!= VDIR
) && !issystemfile
) {
806 (void) hfs_removehotfile(vp
);
815 hfs_reclaim_cnode(struct cnode
*cp
)
820 for (i
= 0; i
< MAXQUOTAS
; i
++) {
821 if (cp
->c_dquot
[i
] != NODQUOT
) {
822 dqreclaim(cp
->c_dquot
[i
]);
823 cp
->c_dquot
[i
] = NODQUOT
;
829 * If the descriptor has a name then release it
831 if ((cp
->c_desc
.cd_flags
& CD_HASBUF
) && (cp
->c_desc
.cd_nameptr
!= 0)) {
834 nameptr
= (const char *) cp
->c_desc
.cd_nameptr
;
835 cp
->c_desc
.cd_nameptr
= 0;
836 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
837 cp
->c_desc
.cd_namelen
= 0;
838 vfs_removename(nameptr
);
841 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
842 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
843 bzero(cp
, sizeof(struct cnode
));
844 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
850 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
852 struct cat_attr attr
;
853 struct cat_desc cndesc
;
857 /* System files are always valid */
858 if (cnid
< kHFSFirstUserCatalogNodeID
)
861 /* XXX optimization: check write count in dvp */
863 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
866 bzero(&cndesc
, sizeof(cndesc
));
867 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
868 cndesc
.cd_namelen
= cnp
->cn_namelen
;
869 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_fileid
;
870 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
872 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
873 (cnid
== attr
.ca_fileid
)) {
877 if (cat_idlookup(hfsmp
, cnid
, 0, NULL
, NULL
, NULL
) == 0) {
881 hfs_systemfile_unlock(hfsmp
, lockflags
);
887 * Touch cnode times based on c_touch_xxx flags
889 * cnode must be locked exclusive
891 * This will also update the volume modify time
895 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
897 /* don't modify times if volume is read-only */
898 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
899 cp
->c_touch_acctime
= FALSE
;
900 cp
->c_touch_chgtime
= FALSE
;
901 cp
->c_touch_modtime
= FALSE
;
903 else if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
904 /* HFS Standard doesn't support access times */
905 cp
->c_touch_acctime
= FALSE
;
909 * Skip access time updates if:
910 * . MNT_NOATIME is set
911 * . a file system freeze is in progress
912 * . a file system resize is in progress
914 if (cp
->c_touch_acctime
) {
915 if ((vfs_flags(hfsmp
->hfs_mp
) & MNT_NOATIME
) ||
916 (hfsmp
->hfs_freezing_proc
!= NULL
) ||
917 (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
))
918 cp
->c_touch_acctime
= FALSE
;
920 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
926 if (cp
->c_touch_acctime
) {
927 cp
->c_atime
= tv
.tv_sec
;
929 * When the access time is the only thing changing
930 * then make sure its sufficiently newer before
931 * committing it to disk.
933 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
934 ATIME_ONDISK_ACCURACY
)) {
935 cp
->c_flag
|= C_MODIFIED
;
937 cp
->c_touch_acctime
= FALSE
;
939 if (cp
->c_touch_modtime
) {
940 cp
->c_mtime
= tv
.tv_sec
;
941 cp
->c_touch_modtime
= FALSE
;
942 cp
->c_flag
|= C_MODIFIED
;
946 * HFS dates that WE set must be adjusted for DST
948 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
953 if (cp
->c_touch_chgtime
) {
954 cp
->c_ctime
= tv
.tv_sec
;
955 cp
->c_touch_chgtime
= FALSE
;
956 cp
->c_flag
|= C_MODIFIED
;
960 /* Touch the volume modtime if needed */
963 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
973 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
975 void * thread
= current_thread();
977 if (cp
->c_lockowner
== thread
) {
979 * Only the extents and bitmap file's support lock recursion.
981 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
982 (cp
->c_fileid
== kHFSAllocationFileID
)) {
983 cp
->c_syslockcount
++;
985 panic("hfs_lock: locking against myself!");
987 } else if (locktype
== HFS_SHARED_LOCK
) {
988 lck_rw_lock_shared(&cp
->c_rwlock
);
989 cp
->c_lockowner
= HFS_SHARED_OWNER
;
991 } else /* HFS_EXCLUSIVE_LOCK */ {
992 lck_rw_lock_exclusive(&cp
->c_rwlock
);
993 cp
->c_lockowner
= thread
;
996 * Only the extents and bitmap file's support lock recursion.
998 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
999 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1000 cp
->c_syslockcount
= 1;
1004 #ifdef HFS_CHECK_LOCK_ORDER
1006 * Regular cnodes (non-system files) cannot be locked
1007 * while holding the journal lock or a system file lock.
1009 if (!(cp
->c_desc
.cd_flags
& CD_ISMETA
) &&
1010 ((cp
->c_fileid
> kHFSFirstUserCatalogNodeID
) || (cp
->c_fileid
== kHFSRootFolderID
))) {
1011 vnode_t vp
= NULLVP
;
1013 /* Find corresponding vnode. */
1014 if (cp
->c_vp
!= NULLVP
&& VTOC(cp
->c_vp
) == cp
) {
1016 } else if (cp
->c_rsrc_vp
!= NULLVP
&& VTOC(cp
->c_rsrc_vp
) == cp
) {
1020 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1022 if (hfsmp
->jnl
&& (journal_owner(hfsmp
->jnl
) == thread
)) {
1023 /* This will eventually be a panic here. */
1024 printf("hfs_lock: bad lock order (cnode after journal)\n");
1026 if (hfsmp
->hfs_catalog_cp
&& hfsmp
->hfs_catalog_cp
->c_lockowner
== thread
) {
1027 panic("hfs_lock: bad lock order (cnode after catalog)");
1029 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== thread
) {
1030 panic("hfs_lock: bad lock order (cnode after attribute)");
1032 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== thread
) {
1033 panic("hfs_lock: bad lock order (cnode after extents)");
1037 #endif /* HFS_CHECK_LOCK_ORDER */
1040 * Skip cnodes that no longer exist (were deleted).
1042 if ((locktype
!= HFS_FORCE_LOCK
) &&
1043 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
1044 (cp
->c_flag
& C_NOEXISTS
)) {
1052 * Lock a pair of cnodes.
1056 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
1058 struct cnode
*first
, *last
;
1062 * If cnodes match then just lock one.
1065 return hfs_lock(cp1
, locktype
);
1069 * Lock in cnode address order.
1079 if ( (error
= hfs_lock(first
, locktype
))) {
1082 if ( (error
= hfs_lock(last
, locktype
))) {
1090 * Check ordering of two cnodes. Return true if they are are in-order.
1093 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
1097 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
1099 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
1102 * Locking order is cnode address order.
1108 * Acquire 4 cnode locks.
1109 * - locked in cnode address order (lesser address first).
1110 * - all or none of the locks are taken
1111 * - only one lock taken per cnode (dup cnodes are skipped)
1112 * - some of the cnode pointers may be null
1116 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
1117 struct cnode
*cp4
, enum hfslocktype locktype
)
1119 struct cnode
* a
[3];
1120 struct cnode
* b
[3];
1121 struct cnode
* list
[4];
1126 if (hfs_isordered(cp1
, cp2
)) {
1127 a
[0] = cp1
; a
[1] = cp2
;
1129 a
[0] = cp2
; a
[1] = cp1
;
1131 if (hfs_isordered(cp3
, cp4
)) {
1132 b
[0] = cp3
; b
[1] = cp4
;
1134 b
[0] = cp4
; b
[1] = cp3
;
1136 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1137 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1140 * Build the lock list, skipping over duplicates
1142 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
1143 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
1144 if (k
== 0 || tmp
!= list
[k
-1])
1149 * Now we can lock using list[0 - k].
1150 * Skip over NULL entries.
1152 for (i
= 0; i
< k
; ++i
) {
1154 if ((error
= hfs_lock(list
[i
], locktype
))) {
1155 /* Drop any locks we acquired. */
1158 hfs_unlock(list
[i
]);
1172 hfs_unlock(struct cnode
*cp
)
1174 vnode_t rvp
= NULLVP
;
1175 vnode_t vp
= NULLVP
;
1180 * Only the extents and bitmap file's support lock recursion.
1182 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1183 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1184 if (--cp
->c_syslockcount
> 0) {
1188 c_flag
= cp
->c_flag
;
1189 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
| C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
);
1191 if (c_flag
& (C_NEED_DVNODE_PUT
| C_NEED_DATA_SETSIZE
)) {
1194 if (c_flag
& (C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
)) {
1195 rvp
= cp
->c_rsrc_vp
;
1198 lockowner
= cp
->c_lockowner
;
1199 if (lockowner
== current_thread()) {
1200 cp
->c_lockowner
= NULL
;
1201 lck_rw_unlock_exclusive(&cp
->c_rwlock
);
1203 lck_rw_unlock_shared(&cp
->c_rwlock
);
1206 /* Perform any vnode post processing after cnode lock is dropped. */
1208 if (c_flag
& C_NEED_DATA_SETSIZE
)
1210 if (c_flag
& C_NEED_DVNODE_PUT
)
1214 if (c_flag
& C_NEED_RSRC_SETSIZE
)
1215 ubc_setsize(rvp
, 0);
1216 if (c_flag
& C_NEED_RVNODE_PUT
)
1222 * Unlock a pair of cnodes.
1226 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1234 * Unlock a group of cnodes.
1238 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1240 struct cnode
* list
[4];
1248 for (i
= 0; i
< k
; ++i
) {
1257 for (i
= 0; i
< k
; ++i
) {
1266 for (i
= 0; i
< k
; ++i
) {
1276 * Protect a cnode against a truncation.
1278 * Used mainly by read/write since they don't hold the
1279 * cnode lock across calls to the cluster layer.
1281 * The process doing a truncation must take the lock
1282 * exclusive. The read/write processes can take it
1287 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1289 #ifdef HFS_CHECK_LOCK_ORDER
1290 if (cp
->c_lockowner
== current_thread())
1291 panic("hfs_lock_truncate: cnode %p locked!", cp
);
1292 #endif /* HFS_CHECK_LOCK_ORDER */
1295 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1297 lck_rw_lock_shared(&cp
->c_truncatelock
);
1302 hfs_unlock_truncate(struct cnode
*cp
, int exclusive
)
1305 lck_rw_unlock_exclusive(&cp
->c_truncatelock
);
1307 lck_rw_unlock_shared(&cp
->c_truncatelock
);