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);
132 if (cp
->c_flag
& C_HARDLINK
) {
141 /* If needed, get rid of any fork's data for a deleted file */
142 if ((v_type
== VREG
|| v_type
== VLNK
) && (cp
->c_flag
& C_DELETED
)) {
143 if (VTOF(vp
)->ff_blocks
!= 0) {
145 * Since we're already inside a transaction,
146 * tell hfs_truncate to skip the ubc_setsize.
148 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
156 * Check if there's any resource fork blocks that need to
157 * be reclaimed. This covers the case where there is a
158 * resource fork but its not in core.
160 if ((cp
->c_blocks
> 0) && (forkcount
== 1) && (vp
!= cp
->c_rsrc_vp
)) {
161 struct vnode
*rvp
= NULLVP
;
163 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, FALSE
);
167 * Defer the vnode_put and ubc_setsize on rvp until hfs_unlock().
169 cp
->c_flag
|= C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
;
170 error
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
173 vnode_recycle(rvp
); /* all done with this vnode */
177 // If needed, get rid of any xattrs that this file may have.
178 // Note that this must happen outside of any other transactions
179 // because it starts/ends its own transactions and grabs its
180 // own locks. This is to prevent a file with a lot of attributes
181 // from creating a transaction that is too large (which panics).
183 if ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0 && (cp
->c_flag
& C_DELETED
)) {
184 hfs_removeallattr(hfsmp
, cp
->c_fileid
);
188 * Check for a postponed deletion.
189 * (only delete cnode when the last fork goes inactive)
191 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
193 * Mark cnode in transit so that no one can get this
194 * cnode from cnode hash.
196 // hfs_chash_mark_in_transit(cp);
197 // XXXdbg - remove the cnode from the hash table since it's deleted
198 // otherwise someone could go to sleep on the cnode and not
199 // be woken up until this vnode gets recycled which could be
200 // a very long time...
203 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
206 if (started_tr
== 0) {
207 if (hfs_start_transaction(hfsmp
) != 0) {
215 * Reserve some space in the Catalog file.
217 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
222 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
224 if (cp
->c_blocks
> 0) {
225 printf("hfs_inactive: deleting non-empty%sfile %d, "
226 "blks %d\n", VNODE_IS_RSRC(vp
) ? " rsrc " : " ",
227 (int)cp
->c_fileid
, (int)cp
->c_blocks
);
231 // release the name pointer in the descriptor so that
232 // cat_delete() will use the file-id to do the deletion.
233 // in the case of hard links this is imperative (in the
234 // case of regular files the fileid and cnid are the
235 // same so it doesn't matter).
237 cat_releasedesc(&cp
->c_desc
);
240 * The descriptor name may be zero,
241 * in which case the fileid is used.
243 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
245 if (error
&& truncated
&& (error
!= ENXIO
))
246 printf("hfs_inactive: couldn't delete a truncated file!");
248 /* Update HFS Private Data dir */
250 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
--;
251 if (vnode_isdir(vp
)) {
252 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
254 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
255 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
258 hfs_systemfile_unlock(hfsmp
, lockflags
);
264 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
265 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
269 cp
->c_flag
&= ~C_DELETED
;
270 cp
->c_touch_chgtime
= TRUE
;
271 cp
->c_touch_modtime
= TRUE
;
274 hfs_volupdate(hfsmp
, (v_type
== VDIR
) ? VOL_RMDIR
: VOL_RMFILE
, 0);
278 * A file may have had delayed allocations, in which case hfs_update
279 * would not have updated the catalog record (cat_update). We need
280 * to do that now, before we lose our fork data. We also need to
281 * force the update, or hfs_update will again skip the cat_update.
283 if ((cp
->c_flag
& C_MODIFIED
) ||
284 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
285 if ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_modtime
){
286 cp
->c_flag
|= C_FORCEUPDATE
;
292 cat_postflight(hfsmp
, &cookie
, p
);
294 // XXXdbg - have to do this because a goto could have come here
296 hfs_end_transaction(hfsmp
);
303 hfs_unlock_truncate(cp
, TRUE
);
306 * If we are done with the vnode, reclaim it
307 * so that it can be reused immediately.
309 if (cp
->c_mode
== 0 || recycle
)
316 * File clean-up (zero fill and shrink peof).
319 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
323 struct hfsmount
*hfsmp
;
325 u_long blks
, blocksize
;
332 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
336 (void) cluster_push(vp
, IO_CLOSE
);
337 hfs_lock(cp
, HFS_FORCE_LOCK
);
340 * Explicitly zero out the areas of file
341 * that are currently marked invalid.
343 while (!CIRCLEQ_EMPTY(&fp
->ff_invalidranges
)) {
344 struct rl_entry
*invalid_range
= CIRCLEQ_FIRST(&fp
->ff_invalidranges
);
345 off_t start
= invalid_range
->rl_start
;
346 off_t end
= invalid_range
->rl_end
;
348 /* The range about to be written must be validated
349 * first, so that VNOP_BLOCKMAP() will return the
350 * appropriate mapping for the cluster code:
352 rl_remove(start
, end
, &fp
->ff_invalidranges
);
355 (void) cluster_write(vp
, (struct uio
*) 0,
356 leof
, end
+ 1, start
, (off_t
)0,
357 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
358 hfs_lock(cp
, HFS_FORCE_LOCK
);
359 cp
->c_flag
|= C_MODIFIED
;
361 cp
->c_flag
&= ~C_ZFWANTSYNC
;
363 blocksize
= VTOVCB(vp
)->blockSize
;
364 blks
= leof
/ blocksize
;
365 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
368 * Shrink the peof to the smallest size neccessary to contain the leof.
370 if (blks
< fp
->ff_blocks
)
371 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, context
);
373 (void) cluster_push(vp
, IO_CLOSE
);
374 hfs_lock(cp
, HFS_FORCE_LOCK
);
377 * If the hfs_truncate didn't happen to flush the vnode's
378 * information out to disk, force it to be updated now that
379 * all invalid ranges have been zero-filled and validated:
381 if (cp
->c_flag
& C_MODIFIED
) {
389 * Reclaim a cnode so that it can be used for other purposes.
393 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
395 struct vnode
*vp
= ap
->a_vp
;
397 struct filefork
*fp
= NULL
;
398 struct filefork
*altfp
= NULL
;
399 int reclaim_cnode
= 0;
401 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
405 * Check if a deleted resource fork vnode missed a
406 * VNOP_INACTIVE call and requires truncation.
408 if (VNODE_IS_RSRC(vp
) &&
409 (cp
->c_flag
& C_DELETED
) &&
410 (VTOF(vp
)->ff_blocks
!= 0)) {
414 hfs_lock_truncate(cp
, TRUE
);
415 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
417 (void) hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
419 hfs_unlock_truncate(cp
, TRUE
);
422 * A file may have had delayed allocations, in which case hfs_update
423 * would not have updated the catalog record (cat_update). We need
424 * to do that now, before we lose our fork data. We also need to
425 * force the update, or hfs_update will again skip the cat_update.
427 if ((cp
->c_flag
& C_MODIFIED
) ||
428 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
429 if ((cp
->c_flag
& C_MODIFIED
) || cp
->c_touch_modtime
){
430 cp
->c_flag
|= C_FORCEUPDATE
;
436 * Keep track of an inactive hot file.
438 if (!vnode_isdir(vp
) &&
439 !vnode_issystem(vp
) &&
440 !(cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ) {
441 (void) hfs_addhotfile(vp
);
443 vnode_removefsref(vp
);
446 * Find file fork for this vnode (if any)
447 * Also check if another fork is active
449 if (cp
->c_vp
== vp
) {
451 altfp
= cp
->c_rsrcfork
;
453 cp
->c_datafork
= NULL
;
455 } else if (cp
->c_rsrc_vp
== vp
) {
457 altfp
= cp
->c_datafork
;
459 cp
->c_rsrcfork
= NULL
;
460 cp
->c_rsrc_vp
= NULL
;
462 panic("hfs_vnop_reclaim: vp points to wrong cnode\n");
465 * On the last fork, remove the cnode from its hash chain.
468 /* If we can't remove it then the cnode must persist! */
469 if (hfs_chashremove(cp
) == 0)
472 * Remove any directory hints
474 if (vnode_isdir(vp
)) {
475 hfs_reldirhints(cp
, 0);
478 if (cp
->c_flag
& C_HARDLINK
) {
482 /* Release the file fork and related data */
484 /* Dump cached symlink data */
485 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
486 FREE(fp
->ff_symlinkptr
, M_TEMP
);
488 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
492 * If there was only one active fork then we can release the cnode.
495 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
496 hfs_reclaim_cnode(cp
);
497 } else /* cnode in use */ {
501 vnode_clearfsnode(vp
);
506 extern int (**hfs_vnodeop_p
) (void *);
507 extern int (**hfs_specop_p
) (void *);
509 extern int (**hfs_fifoop_p
) (void *);
513 * hfs_getnewvnode - get new default vnode
515 * The vnode is returned with an iocount and the cnode locked
520 struct hfsmount
*hfsmp
,
522 struct componentname
*cnp
,
523 struct cat_desc
*descp
,
525 struct cat_attr
*attrp
,
526 struct cat_fork
*forkp
,
529 struct mount
*mp
= HFSTOVFS(hfsmp
);
530 struct vnode
*vp
= NULL
;
532 struct vnode
*tvp
= NULLVP
;
533 struct cnode
*cp
= NULL
;
534 struct filefork
*fp
= NULL
;
538 struct vnode_fsparam vfsp
;
544 if (attrp
->ca_fileid
== 0) {
550 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
555 vtype
= IFTOVT(attrp
->ca_mode
);
556 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
557 wantrsrc
= flags
& GNV_WANTRSRC
;
559 #ifdef HFS_CHECK_LOCK_ORDER
561 * The only case were its permissible to hold the parent cnode
562 * lock is during a create operation (hfs_makenode) or when
563 * we don't need the cnode lock (GNV_SKIPLOCK).
566 (flags
& (GNV_CREATE
| GNV_SKIPLOCK
)) == 0 &&
567 VTOC(dvp
)->c_lockowner
== current_thread()) {
568 panic("hfs_getnewvnode: unexpected hold of parent cnode %p", VTOC(dvp
));
570 #endif /* HFS_CHECK_LOCK_ORDER */
573 * Get a cnode (new or existing)
575 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (flags
& GNV_SKIPLOCK
));
578 * If the id is no longer valid for lookups we'll get back a NULL cp.
584 /* Hardlinks may need an updated catalog descriptor */
585 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
586 replace_desc(cp
, descp
);
588 /* Check if we found a matching vnode */
593 * If this is a new cnode then initialize it.
595 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
596 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
598 /* Make sure its still valid (ie exists on disk). */
599 if (!(flags
& GNV_CREATE
) &&
600 !hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
602 hfs_reclaim_cnode(cp
);
606 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
607 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
609 /* The name was inherited so clear descriptor state... */
610 descp
->cd_namelen
= 0;
611 descp
->cd_nameptr
= NULL
;
612 descp
->cd_flags
&= ~CD_HASBUF
;
615 if ((vtype
== VREG
|| vtype
== VDIR
) &&
616 ((descp
->cd_cnid
!= attrp
->ca_fileid
) ||
617 (attrp
->ca_recflags
& kHFSHasLinkChainMask
))) {
618 cp
->c_flag
|= C_HARDLINK
;
621 * Fix-up dir link counts.
623 * Earlier versions of Leopard used ca_linkcount for posix
624 * nlink support (effectively the sub-directory count + 2).
625 * That is now accomplished using the ca_dircount field with
626 * the corresponding kHFSHasFolderCountMask flag.
628 * For directories the ca_linkcount is the true link count,
629 * tracking the number of actual hardlinks to a directory.
631 * We only do this if the mount has HFS_FOLDERCOUNT set;
632 * at the moment, we only set that for HFSX volumes.
634 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
636 !(attrp
->ca_recflags
& kHFSHasFolderCountMask
) &&
637 (cp
->c_attr
.ca_linkcount
> 1)) {
638 if (cp
->c_attr
.ca_entries
== 0)
639 cp
->c_attr
.ca_dircount
= 0;
641 cp
->c_attr
.ca_dircount
= cp
->c_attr
.ca_linkcount
- 2;
643 cp
->c_attr
.ca_linkcount
= 1;
644 cp
->c_attr
.ca_recflags
|= kHFSHasFolderCountMask
;
645 if ( !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) )
646 cp
->c_flag
|= C_MODIFIED
;
649 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
650 for (i
= 0; i
< MAXQUOTAS
; i
++)
651 cp
->c_dquot
[i
] = NODQUOT
;
657 if (cp
->c_vp
!= NULL
)
658 panic("hfs_getnewvnode: orphaned vnode (data)");
661 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
662 panic("hfs_getnewvnode: bad ca_blocks (too small)");
664 * Allocate and initialize a file fork...
666 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
667 M_HFSFORK
, M_WAITOK
);
670 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
672 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
673 rl_init(&fp
->ff_invalidranges
);
674 fp
->ff_sysfileinfo
= 0;
677 if (cp
->c_rsrcfork
!= NULL
)
678 panic("hfs_getnewvnode: orphaned rsrc fork");
679 if (cp
->c_rsrc_vp
!= NULL
)
680 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
682 cvpp
= &cp
->c_rsrc_vp
;
683 if ( (tvp
= cp
->c_vp
) != NULLVP
)
684 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
686 if (cp
->c_datafork
!= NULL
)
687 panic("hfs_getnewvnode: orphaned data fork");
688 if (cp
->c_vp
!= NULL
)
689 panic("hfs_getnewvnode: orphaned vnode (data)");
692 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
693 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
698 * grab an iocount on the vnode we weren't
699 * interested in (i.e. we want the resource fork
700 * but the cnode already has the data fork)
701 * to prevent it from being
702 * recycled by us when we call vnode_create
703 * which will result in a deadlock when we
704 * try to take the cnode lock in hfs_vnop_fsync or
705 * hfs_vnop_reclaim... vnode_get can be called here
706 * because we already hold the cnode lock which will
707 * prevent the vnode from changing identity until
708 * we drop it.. vnode_get will not block waiting for
709 * a change of state... however, it will return an
710 * error if the current iocount == 0 and we've already
711 * started to terminate the vnode... we don't need/want to
712 * grab an iocount in the case since we can't cause
713 * the fileystem to be re-entered on this thread for this vp
715 * the matching vnode_put will happen in hfs_unlock
716 * after we've dropped the cnode lock
718 if ( vnode_get(tvp
) != 0)
719 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
722 vfsp
.vnfs_vtype
= vtype
;
723 vfsp
.vnfs_str
= "hfs";
724 if ((cp
->c_flag
& C_HARDLINK
) && (vtype
== VDIR
)) {
725 vfsp
.vnfs_dvp
= NULL
; /* no parent for me! */
726 vfsp
.vnfs_cnp
= NULL
; /* no name for me! */
731 vfsp
.vnfs_fsnode
= cp
;
734 vfsp
.vnfs_vops
= hfs_fifoop_p
;
737 if (vtype
== VBLK
|| vtype
== VCHR
)
738 vfsp
.vnfs_vops
= hfs_specop_p
;
740 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
742 if (vtype
== VBLK
|| vtype
== VCHR
)
743 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
748 vfsp
.vnfs_filesize
= forkp
->cf_size
;
750 vfsp
.vnfs_filesize
= 0;
752 vfsp
.vnfs_flags
= VNFS_ADDFSREF
;
753 if (dvp
== NULLVP
|| cnp
== NULL
|| !(cnp
->cn_flags
& MAKEENTRY
))
754 vfsp
.vnfs_flags
|= VNFS_NOCACHE
;
756 /* Tag system files */
757 vfsp
.vnfs_marksystem
= issystemfile
;
759 /* Tag root directory */
760 if (descp
->cd_cnid
== kHFSRootFolderID
)
761 vfsp
.vnfs_markroot
= 1;
763 vfsp
.vnfs_markroot
= 0;
765 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
767 if (fp
== cp
->c_datafork
)
768 cp
->c_datafork
= NULL
;
770 cp
->c_rsrcfork
= NULL
;
772 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
775 * If this is a newly created cnode or a vnode reclaim
776 * occurred during the attachment, then cleanup the cnode.
778 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
780 hfs_reclaim_cnode(cp
);
783 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
784 if ((flags
& GNV_SKIPLOCK
) == 0){
792 vnode_settag(vp
, VT_HFS
);
793 if (cp
->c_flag
& C_HARDLINK
) {
794 vnode_setmultipath(vp
);
797 * Tag resource fork vnodes as needing an VNOP_INACTIVE
798 * so that any deferred removes (open unlinked files)
799 * have the chance to process the resource fork.
801 if (VNODE_IS_RSRC(vp
)) {
802 /* Force VL_NEEDINACTIVE on this vnode */
806 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
809 * Stop tracking an active hot file.
811 if (!(flags
& GNV_CREATE
) && (vtype
!= VDIR
) && !issystemfile
) {
812 (void) hfs_removehotfile(vp
);
821 hfs_reclaim_cnode(struct cnode
*cp
)
826 for (i
= 0; i
< MAXQUOTAS
; i
++) {
827 if (cp
->c_dquot
[i
] != NODQUOT
) {
828 dqreclaim(cp
->c_dquot
[i
]);
829 cp
->c_dquot
[i
] = NODQUOT
;
835 * If the descriptor has a name then release it
837 if ((cp
->c_desc
.cd_flags
& CD_HASBUF
) && (cp
->c_desc
.cd_nameptr
!= 0)) {
840 nameptr
= (const char *) cp
->c_desc
.cd_nameptr
;
841 cp
->c_desc
.cd_nameptr
= 0;
842 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
843 cp
->c_desc
.cd_namelen
= 0;
844 vfs_removename(nameptr
);
847 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
848 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
849 bzero(cp
, sizeof(struct cnode
));
850 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
856 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
858 struct cat_attr attr
;
859 struct cat_desc cndesc
;
863 /* System files are always valid */
864 if (cnid
< kHFSFirstUserCatalogNodeID
)
867 /* XXX optimization: check write count in dvp */
869 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
872 bzero(&cndesc
, sizeof(cndesc
));
873 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
874 cndesc
.cd_namelen
= cnp
->cn_namelen
;
875 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_fileid
;
876 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
878 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
879 (cnid
== attr
.ca_fileid
)) {
883 if (cat_idlookup(hfsmp
, cnid
, 0, NULL
, NULL
, NULL
) == 0) {
887 hfs_systemfile_unlock(hfsmp
, lockflags
);
893 * Touch cnode times based on c_touch_xxx flags
895 * cnode must be locked exclusive
897 * This will also update the volume modify time
901 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
903 /* don't modify times if volume is read-only */
904 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
905 cp
->c_touch_acctime
= FALSE
;
906 cp
->c_touch_chgtime
= FALSE
;
907 cp
->c_touch_modtime
= FALSE
;
909 else if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
910 /* HFS Standard doesn't support access times */
911 cp
->c_touch_acctime
= FALSE
;
915 * Skip access time updates if:
916 * . MNT_NOATIME is set
917 * . a file system freeze is in progress
918 * . a file system resize is in progress
920 if (cp
->c_touch_acctime
) {
921 if ((vfs_flags(hfsmp
->hfs_mp
) & MNT_NOATIME
) ||
922 (hfsmp
->hfs_freezing_proc
!= NULL
) ||
923 (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
))
924 cp
->c_touch_acctime
= FALSE
;
926 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
932 if (cp
->c_touch_acctime
) {
933 cp
->c_atime
= tv
.tv_sec
;
935 * When the access time is the only thing changing
936 * then make sure its sufficiently newer before
937 * committing it to disk.
939 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
940 ATIME_ONDISK_ACCURACY
)) {
941 cp
->c_flag
|= C_MODIFIED
;
943 cp
->c_touch_acctime
= FALSE
;
945 if (cp
->c_touch_modtime
) {
946 cp
->c_mtime
= tv
.tv_sec
;
947 cp
->c_touch_modtime
= FALSE
;
948 cp
->c_flag
|= C_MODIFIED
;
952 * HFS dates that WE set must be adjusted for DST
954 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
959 if (cp
->c_touch_chgtime
) {
960 cp
->c_ctime
= tv
.tv_sec
;
961 cp
->c_touch_chgtime
= FALSE
;
962 cp
->c_flag
|= C_MODIFIED
;
966 /* Touch the volume modtime if needed */
969 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
979 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
981 void * thread
= current_thread();
983 if (cp
->c_lockowner
== thread
) {
985 * Only the extents and bitmap file's support lock recursion.
987 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
988 (cp
->c_fileid
== kHFSAllocationFileID
)) {
989 cp
->c_syslockcount
++;
991 panic("hfs_lock: locking against myself!");
993 } else if (locktype
== HFS_SHARED_LOCK
) {
994 lck_rw_lock_shared(&cp
->c_rwlock
);
995 cp
->c_lockowner
= HFS_SHARED_OWNER
;
997 } else /* HFS_EXCLUSIVE_LOCK */ {
998 lck_rw_lock_exclusive(&cp
->c_rwlock
);
999 cp
->c_lockowner
= thread
;
1002 * Only the extents and bitmap file's support lock recursion.
1004 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1005 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1006 cp
->c_syslockcount
= 1;
1010 #ifdef HFS_CHECK_LOCK_ORDER
1012 * Regular cnodes (non-system files) cannot be locked
1013 * while holding the journal lock or a system file lock.
1015 if (!(cp
->c_desc
.cd_flags
& CD_ISMETA
) &&
1016 ((cp
->c_fileid
> kHFSFirstUserCatalogNodeID
) || (cp
->c_fileid
== kHFSRootFolderID
))) {
1017 vnode_t vp
= NULLVP
;
1019 /* Find corresponding vnode. */
1020 if (cp
->c_vp
!= NULLVP
&& VTOC(cp
->c_vp
) == cp
) {
1022 } else if (cp
->c_rsrc_vp
!= NULLVP
&& VTOC(cp
->c_rsrc_vp
) == cp
) {
1026 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1028 if (hfsmp
->jnl
&& (journal_owner(hfsmp
->jnl
) == thread
)) {
1029 /* This will eventually be a panic here. */
1030 printf("hfs_lock: bad lock order (cnode after journal)\n");
1032 if (hfsmp
->hfs_catalog_cp
&& hfsmp
->hfs_catalog_cp
->c_lockowner
== thread
) {
1033 panic("hfs_lock: bad lock order (cnode after catalog)");
1035 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== thread
) {
1036 panic("hfs_lock: bad lock order (cnode after attribute)");
1038 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== thread
) {
1039 panic("hfs_lock: bad lock order (cnode after extents)");
1043 #endif /* HFS_CHECK_LOCK_ORDER */
1046 * Skip cnodes that no longer exist (were deleted).
1048 if ((locktype
!= HFS_FORCE_LOCK
) &&
1049 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
1050 (cp
->c_flag
& C_NOEXISTS
)) {
1058 * Lock a pair of cnodes.
1062 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
1064 struct cnode
*first
, *last
;
1068 * If cnodes match then just lock one.
1071 return hfs_lock(cp1
, locktype
);
1075 * Lock in cnode address order.
1085 if ( (error
= hfs_lock(first
, locktype
))) {
1088 if ( (error
= hfs_lock(last
, locktype
))) {
1096 * Check ordering of two cnodes. Return true if they are are in-order.
1099 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
1103 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
1105 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
1108 * Locking order is cnode address order.
1114 * Acquire 4 cnode locks.
1115 * - locked in cnode address order (lesser address first).
1116 * - all or none of the locks are taken
1117 * - only one lock taken per cnode (dup cnodes are skipped)
1118 * - some of the cnode pointers may be null
1122 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
1123 struct cnode
*cp4
, enum hfslocktype locktype
)
1125 struct cnode
* a
[3];
1126 struct cnode
* b
[3];
1127 struct cnode
* list
[4];
1132 if (hfs_isordered(cp1
, cp2
)) {
1133 a
[0] = cp1
; a
[1] = cp2
;
1135 a
[0] = cp2
; a
[1] = cp1
;
1137 if (hfs_isordered(cp3
, cp4
)) {
1138 b
[0] = cp3
; b
[1] = cp4
;
1140 b
[0] = cp4
; b
[1] = cp3
;
1142 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1143 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1146 * Build the lock list, skipping over duplicates
1148 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
1149 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
1150 if (k
== 0 || tmp
!= list
[k
-1])
1155 * Now we can lock using list[0 - k].
1156 * Skip over NULL entries.
1158 for (i
= 0; i
< k
; ++i
) {
1160 if ((error
= hfs_lock(list
[i
], locktype
))) {
1161 /* Drop any locks we acquired. */
1164 hfs_unlock(list
[i
]);
1178 hfs_unlock(struct cnode
*cp
)
1180 vnode_t rvp
= NULLVP
;
1181 vnode_t vp
= NULLVP
;
1186 * Only the extents and bitmap file's support lock recursion.
1188 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1189 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1190 if (--cp
->c_syslockcount
> 0) {
1194 c_flag
= cp
->c_flag
;
1195 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
| C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
);
1197 if (c_flag
& (C_NEED_DVNODE_PUT
| C_NEED_DATA_SETSIZE
)) {
1200 if (c_flag
& (C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
)) {
1201 rvp
= cp
->c_rsrc_vp
;
1204 lockowner
= cp
->c_lockowner
;
1205 if (lockowner
== current_thread()) {
1206 cp
->c_lockowner
= NULL
;
1207 lck_rw_unlock_exclusive(&cp
->c_rwlock
);
1209 lck_rw_unlock_shared(&cp
->c_rwlock
);
1212 /* Perform any vnode post processing after cnode lock is dropped. */
1214 if (c_flag
& C_NEED_DATA_SETSIZE
)
1216 if (c_flag
& C_NEED_DVNODE_PUT
)
1220 if (c_flag
& C_NEED_RSRC_SETSIZE
)
1221 ubc_setsize(rvp
, 0);
1222 if (c_flag
& C_NEED_RVNODE_PUT
)
1228 * Unlock a pair of cnodes.
1232 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1240 * Unlock a group of cnodes.
1244 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1246 struct cnode
* list
[4];
1254 for (i
= 0; i
< k
; ++i
) {
1263 for (i
= 0; i
< k
; ++i
) {
1272 for (i
= 0; i
< k
; ++i
) {
1282 * Protect a cnode against a truncation.
1284 * Used mainly by read/write since they don't hold the
1285 * cnode lock across calls to the cluster layer.
1287 * The process doing a truncation must take the lock
1288 * exclusive. The read/write processes can take it
1293 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1295 #ifdef HFS_CHECK_LOCK_ORDER
1296 if (cp
->c_lockowner
== current_thread())
1297 panic("hfs_lock_truncate: cnode %p locked!", cp
);
1298 #endif /* HFS_CHECK_LOCK_ORDER */
1301 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1303 lck_rw_lock_shared(&cp
->c_truncatelock
);
1308 hfs_unlock_truncate(struct cnode
*cp
, int exclusive
)
1311 lck_rw_unlock_exclusive(&cp
->c_truncatelock
);
1313 lck_rw_unlock_shared(&cp
->c_truncatelock
);