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
) {
483 /* Release the file fork and related data */
485 /* Dump cached symlink data */
486 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
487 FREE(fp
->ff_symlinkptr
, M_TEMP
);
489 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
493 * If there was only one active fork then we can release the cnode.
496 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
497 hfs_reclaim_cnode(cp
);
498 } else /* cnode in use */ {
502 vnode_clearfsnode(vp
);
507 extern int (**hfs_vnodeop_p
) (void *);
508 extern int (**hfs_specop_p
) (void *);
510 extern int (**hfs_fifoop_p
) (void *);
514 * hfs_getnewvnode - get new default vnode
516 * The vnode is returned with an iocount and the cnode locked
521 struct hfsmount
*hfsmp
,
523 struct componentname
*cnp
,
524 struct cat_desc
*descp
,
526 struct cat_attr
*attrp
,
527 struct cat_fork
*forkp
,
530 struct mount
*mp
= HFSTOVFS(hfsmp
);
531 struct vnode
*vp
= NULL
;
533 struct vnode
*tvp
= NULLVP
;
534 struct cnode
*cp
= NULL
;
535 struct filefork
*fp
= NULL
;
539 struct vnode_fsparam vfsp
;
545 if (attrp
->ca_fileid
== 0) {
551 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
556 vtype
= IFTOVT(attrp
->ca_mode
);
557 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
558 wantrsrc
= flags
& GNV_WANTRSRC
;
560 #ifdef HFS_CHECK_LOCK_ORDER
562 * The only case were its permissible to hold the parent cnode
563 * lock is during a create operation (hfs_makenode) or when
564 * we don't need the cnode lock (GNV_SKIPLOCK).
567 (flags
& (GNV_CREATE
| GNV_SKIPLOCK
)) == 0 &&
568 VTOC(dvp
)->c_lockowner
== current_thread()) {
569 panic("hfs_getnewvnode: unexpected hold of parent cnode %p", VTOC(dvp
));
571 #endif /* HFS_CHECK_LOCK_ORDER */
574 * Get a cnode (new or existing)
576 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (flags
& GNV_SKIPLOCK
));
579 * If the id is no longer valid for lookups we'll get back a NULL cp.
585 /* Hardlinks may need an updated catalog descriptor */
586 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
587 replace_desc(cp
, descp
);
589 /* Check if we found a matching vnode */
594 * If this is a new cnode then initialize it.
596 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
597 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
599 /* Make sure its still valid (ie exists on disk). */
600 if (!(flags
& GNV_CREATE
) &&
601 !hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
603 hfs_reclaim_cnode(cp
);
607 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
608 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
610 /* The name was inherited so clear descriptor state... */
611 descp
->cd_namelen
= 0;
612 descp
->cd_nameptr
= NULL
;
613 descp
->cd_flags
&= ~CD_HASBUF
;
616 if ((vtype
== VREG
|| vtype
== VDIR
) &&
617 ((descp
->cd_cnid
!= attrp
->ca_fileid
) ||
618 (attrp
->ca_recflags
& kHFSHasLinkChainMask
))) {
619 cp
->c_flag
|= C_HARDLINK
;
622 * Fix-up dir link counts.
624 * Earlier versions of Leopard used ca_linkcount for posix
625 * nlink support (effectively the sub-directory count + 2).
626 * That is now accomplished using the ca_dircount field with
627 * the corresponding kHFSHasFolderCountMask flag.
629 * For directories the ca_linkcount is the true link count,
630 * tracking the number of actual hardlinks to a directory.
632 * We only do this if the mount has HFS_FOLDERCOUNT set;
633 * at the moment, we only set that for HFSX volumes.
635 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
637 !(attrp
->ca_recflags
& kHFSHasFolderCountMask
) &&
638 (cp
->c_attr
.ca_linkcount
> 1)) {
639 if (cp
->c_attr
.ca_entries
== 0)
640 cp
->c_attr
.ca_dircount
= 0;
642 cp
->c_attr
.ca_dircount
= cp
->c_attr
.ca_linkcount
- 2;
644 cp
->c_attr
.ca_linkcount
= 1;
645 cp
->c_attr
.ca_recflags
|= kHFSHasFolderCountMask
;
646 if ( !(hfsmp
->hfs_flags
& HFS_READ_ONLY
) )
647 cp
->c_flag
|= C_MODIFIED
;
650 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
651 for (i
= 0; i
< MAXQUOTAS
; i
++)
652 cp
->c_dquot
[i
] = NODQUOT
;
658 if (cp
->c_vp
!= NULL
)
659 panic("hfs_getnewvnode: orphaned vnode (data)");
662 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
663 panic("hfs_getnewvnode: bad ca_blocks (too small)");
665 * Allocate and initialize a file fork...
667 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
668 M_HFSFORK
, M_WAITOK
);
671 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
673 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
674 rl_init(&fp
->ff_invalidranges
);
675 fp
->ff_sysfileinfo
= 0;
678 if (cp
->c_rsrcfork
!= NULL
)
679 panic("hfs_getnewvnode: orphaned rsrc fork");
680 if (cp
->c_rsrc_vp
!= NULL
)
681 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
683 cvpp
= &cp
->c_rsrc_vp
;
684 if ( (tvp
= cp
->c_vp
) != NULLVP
)
685 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
687 if (cp
->c_datafork
!= NULL
)
688 panic("hfs_getnewvnode: orphaned data fork");
689 if (cp
->c_vp
!= NULL
)
690 panic("hfs_getnewvnode: orphaned vnode (data)");
693 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
694 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
699 * grab an iocount on the vnode we weren't
700 * interested in (i.e. we want the resource fork
701 * but the cnode already has the data fork)
702 * to prevent it from being
703 * recycled by us when we call vnode_create
704 * which will result in a deadlock when we
705 * try to take the cnode lock in hfs_vnop_fsync or
706 * hfs_vnop_reclaim... vnode_get can be called here
707 * because we already hold the cnode lock which will
708 * prevent the vnode from changing identity until
709 * we drop it.. vnode_get will not block waiting for
710 * a change of state... however, it will return an
711 * error if the current iocount == 0 and we've already
712 * started to terminate the vnode... we don't need/want to
713 * grab an iocount in the case since we can't cause
714 * the fileystem to be re-entered on this thread for this vp
716 * the matching vnode_put will happen in hfs_unlock
717 * after we've dropped the cnode lock
719 if ( vnode_get(tvp
) != 0)
720 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
723 vfsp
.vnfs_vtype
= vtype
;
724 vfsp
.vnfs_str
= "hfs";
725 if ((cp
->c_flag
& C_HARDLINK
) && (vtype
== VDIR
)) {
726 vfsp
.vnfs_dvp
= NULL
; /* no parent for me! */
727 vfsp
.vnfs_cnp
= NULL
; /* no name for me! */
732 vfsp
.vnfs_fsnode
= cp
;
735 vfsp
.vnfs_vops
= hfs_fifoop_p
;
738 if (vtype
== VBLK
|| vtype
== VCHR
)
739 vfsp
.vnfs_vops
= hfs_specop_p
;
741 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
743 if (vtype
== VBLK
|| vtype
== VCHR
)
744 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
749 vfsp
.vnfs_filesize
= forkp
->cf_size
;
751 vfsp
.vnfs_filesize
= 0;
753 vfsp
.vnfs_flags
= VNFS_ADDFSREF
;
754 if (dvp
== NULLVP
|| cnp
== NULL
|| !(cnp
->cn_flags
& MAKEENTRY
))
755 vfsp
.vnfs_flags
|= VNFS_NOCACHE
;
757 /* Tag system files */
758 vfsp
.vnfs_marksystem
= issystemfile
;
760 /* Tag root directory */
761 if (descp
->cd_cnid
== kHFSRootFolderID
)
762 vfsp
.vnfs_markroot
= 1;
764 vfsp
.vnfs_markroot
= 0;
766 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
768 if (fp
== cp
->c_datafork
)
769 cp
->c_datafork
= NULL
;
771 cp
->c_rsrcfork
= NULL
;
773 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
776 * If this is a newly created cnode or a vnode reclaim
777 * occurred during the attachment, then cleanup the cnode.
779 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
781 hfs_reclaim_cnode(cp
);
784 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
785 if ((flags
& GNV_SKIPLOCK
) == 0){
793 vnode_settag(vp
, VT_HFS
);
794 if (cp
->c_flag
& C_HARDLINK
) {
795 vnode_setmultipath(vp
);
798 * Tag resource fork vnodes as needing an VNOP_INACTIVE
799 * so that any deferred removes (open unlinked files)
800 * have the chance to process the resource fork.
802 if (VNODE_IS_RSRC(vp
)) {
803 /* Force VL_NEEDINACTIVE on this vnode */
807 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
810 * Stop tracking an active hot file.
812 if (!(flags
& GNV_CREATE
) && (vtype
!= VDIR
) && !issystemfile
) {
813 (void) hfs_removehotfile(vp
);
822 hfs_reclaim_cnode(struct cnode
*cp
)
827 for (i
= 0; i
< MAXQUOTAS
; i
++) {
828 if (cp
->c_dquot
[i
] != NODQUOT
) {
829 dqreclaim(cp
->c_dquot
[i
]);
830 cp
->c_dquot
[i
] = NODQUOT
;
836 * If the descriptor has a name then release it
838 if ((cp
->c_desc
.cd_flags
& CD_HASBUF
) && (cp
->c_desc
.cd_nameptr
!= 0)) {
841 nameptr
= (const char *) cp
->c_desc
.cd_nameptr
;
842 cp
->c_desc
.cd_nameptr
= 0;
843 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
844 cp
->c_desc
.cd_namelen
= 0;
845 vfs_removename(nameptr
);
848 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
849 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
850 bzero(cp
, sizeof(struct cnode
));
851 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
857 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
859 struct cat_attr attr
;
860 struct cat_desc cndesc
;
864 /* System files are always valid */
865 if (cnid
< kHFSFirstUserCatalogNodeID
)
868 /* XXX optimization: check write count in dvp */
870 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
873 bzero(&cndesc
, sizeof(cndesc
));
874 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
875 cndesc
.cd_namelen
= cnp
->cn_namelen
;
876 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_fileid
;
877 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
879 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
880 (cnid
== attr
.ca_fileid
)) {
884 if (cat_idlookup(hfsmp
, cnid
, 0, NULL
, NULL
, NULL
) == 0) {
888 hfs_systemfile_unlock(hfsmp
, lockflags
);
894 * Touch cnode times based on c_touch_xxx flags
896 * cnode must be locked exclusive
898 * This will also update the volume modify time
902 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
904 /* don't modify times if volume is read-only */
905 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
906 cp
->c_touch_acctime
= FALSE
;
907 cp
->c_touch_chgtime
= FALSE
;
908 cp
->c_touch_modtime
= FALSE
;
910 else if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
911 /* HFS Standard doesn't support access times */
912 cp
->c_touch_acctime
= FALSE
;
916 * Skip access time updates if:
917 * . MNT_NOATIME is set
918 * . a file system freeze is in progress
919 * . a file system resize is in progress
921 if (cp
->c_touch_acctime
) {
922 if ((vfs_flags(hfsmp
->hfs_mp
) & MNT_NOATIME
) ||
923 (hfsmp
->hfs_freezing_proc
!= NULL
) ||
924 (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
))
925 cp
->c_touch_acctime
= FALSE
;
927 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
933 if (cp
->c_touch_acctime
) {
934 cp
->c_atime
= tv
.tv_sec
;
936 * When the access time is the only thing changing
937 * then make sure its sufficiently newer before
938 * committing it to disk.
940 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
941 ATIME_ONDISK_ACCURACY
)) {
942 cp
->c_flag
|= C_MODIFIED
;
944 cp
->c_touch_acctime
= FALSE
;
946 if (cp
->c_touch_modtime
) {
947 cp
->c_mtime
= tv
.tv_sec
;
948 cp
->c_touch_modtime
= FALSE
;
949 cp
->c_flag
|= C_MODIFIED
;
953 * HFS dates that WE set must be adjusted for DST
955 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
960 if (cp
->c_touch_chgtime
) {
961 cp
->c_ctime
= tv
.tv_sec
;
962 cp
->c_touch_chgtime
= FALSE
;
963 cp
->c_flag
|= C_MODIFIED
;
967 /* Touch the volume modtime if needed */
970 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
980 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
982 void * thread
= current_thread();
984 if (cp
->c_lockowner
== thread
) {
986 * Only the extents and bitmap file's support lock recursion.
988 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
989 (cp
->c_fileid
== kHFSAllocationFileID
)) {
990 cp
->c_syslockcount
++;
992 panic("hfs_lock: locking against myself!");
994 } else if (locktype
== HFS_SHARED_LOCK
) {
995 lck_rw_lock_shared(&cp
->c_rwlock
);
996 cp
->c_lockowner
= HFS_SHARED_OWNER
;
998 } else /* HFS_EXCLUSIVE_LOCK */ {
999 lck_rw_lock_exclusive(&cp
->c_rwlock
);
1000 cp
->c_lockowner
= thread
;
1003 * Only the extents and bitmap file's support lock recursion.
1005 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1006 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1007 cp
->c_syslockcount
= 1;
1011 #ifdef HFS_CHECK_LOCK_ORDER
1013 * Regular cnodes (non-system files) cannot be locked
1014 * while holding the journal lock or a system file lock.
1016 if (!(cp
->c_desc
.cd_flags
& CD_ISMETA
) &&
1017 ((cp
->c_fileid
> kHFSFirstUserCatalogNodeID
) || (cp
->c_fileid
== kHFSRootFolderID
))) {
1018 vnode_t vp
= NULLVP
;
1020 /* Find corresponding vnode. */
1021 if (cp
->c_vp
!= NULLVP
&& VTOC(cp
->c_vp
) == cp
) {
1023 } else if (cp
->c_rsrc_vp
!= NULLVP
&& VTOC(cp
->c_rsrc_vp
) == cp
) {
1027 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1029 if (hfsmp
->jnl
&& (journal_owner(hfsmp
->jnl
) == thread
)) {
1030 /* This will eventually be a panic here. */
1031 printf("hfs_lock: bad lock order (cnode after journal)\n");
1033 if (hfsmp
->hfs_catalog_cp
&& hfsmp
->hfs_catalog_cp
->c_lockowner
== thread
) {
1034 panic("hfs_lock: bad lock order (cnode after catalog)");
1036 if (hfsmp
->hfs_attribute_cp
&& hfsmp
->hfs_attribute_cp
->c_lockowner
== thread
) {
1037 panic("hfs_lock: bad lock order (cnode after attribute)");
1039 if (hfsmp
->hfs_extents_cp
&& hfsmp
->hfs_extents_cp
->c_lockowner
== thread
) {
1040 panic("hfs_lock: bad lock order (cnode after extents)");
1044 #endif /* HFS_CHECK_LOCK_ORDER */
1047 * Skip cnodes that no longer exist (were deleted).
1049 if ((locktype
!= HFS_FORCE_LOCK
) &&
1050 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
1051 (cp
->c_flag
& C_NOEXISTS
)) {
1059 * Lock a pair of cnodes.
1063 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
1065 struct cnode
*first
, *last
;
1069 * If cnodes match then just lock one.
1072 return hfs_lock(cp1
, locktype
);
1076 * Lock in cnode address order.
1086 if ( (error
= hfs_lock(first
, locktype
))) {
1089 if ( (error
= hfs_lock(last
, locktype
))) {
1097 * Check ordering of two cnodes. Return true if they are are in-order.
1100 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
1104 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
1106 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
1109 * Locking order is cnode address order.
1115 * Acquire 4 cnode locks.
1116 * - locked in cnode address order (lesser address first).
1117 * - all or none of the locks are taken
1118 * - only one lock taken per cnode (dup cnodes are skipped)
1119 * - some of the cnode pointers may be null
1123 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
1124 struct cnode
*cp4
, enum hfslocktype locktype
)
1126 struct cnode
* a
[3];
1127 struct cnode
* b
[3];
1128 struct cnode
* list
[4];
1133 if (hfs_isordered(cp1
, cp2
)) {
1134 a
[0] = cp1
; a
[1] = cp2
;
1136 a
[0] = cp2
; a
[1] = cp1
;
1138 if (hfs_isordered(cp3
, cp4
)) {
1139 b
[0] = cp3
; b
[1] = cp4
;
1141 b
[0] = cp4
; b
[1] = cp3
;
1143 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1144 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
1147 * Build the lock list, skipping over duplicates
1149 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
1150 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
1151 if (k
== 0 || tmp
!= list
[k
-1])
1156 * Now we can lock using list[0 - k].
1157 * Skip over NULL entries.
1159 for (i
= 0; i
< k
; ++i
) {
1161 if ((error
= hfs_lock(list
[i
], locktype
))) {
1162 /* Drop any locks we acquired. */
1165 hfs_unlock(list
[i
]);
1179 hfs_unlock(struct cnode
*cp
)
1181 vnode_t rvp
= NULLVP
;
1182 vnode_t vp
= NULLVP
;
1187 * Only the extents and bitmap file's support lock recursion.
1189 if ((cp
->c_fileid
== kHFSExtentsFileID
) ||
1190 (cp
->c_fileid
== kHFSAllocationFileID
)) {
1191 if (--cp
->c_syslockcount
> 0) {
1195 c_flag
= cp
->c_flag
;
1196 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
| C_NEED_DATA_SETSIZE
| C_NEED_RSRC_SETSIZE
);
1198 if (c_flag
& (C_NEED_DVNODE_PUT
| C_NEED_DATA_SETSIZE
)) {
1201 if (c_flag
& (C_NEED_RVNODE_PUT
| C_NEED_RSRC_SETSIZE
)) {
1202 rvp
= cp
->c_rsrc_vp
;
1205 lockowner
= cp
->c_lockowner
;
1206 if (lockowner
== current_thread()) {
1207 cp
->c_lockowner
= NULL
;
1208 lck_rw_unlock_exclusive(&cp
->c_rwlock
);
1210 lck_rw_unlock_shared(&cp
->c_rwlock
);
1213 /* Perform any vnode post processing after cnode lock is dropped. */
1215 if (c_flag
& C_NEED_DATA_SETSIZE
)
1217 if (c_flag
& C_NEED_DVNODE_PUT
)
1221 if (c_flag
& C_NEED_RSRC_SETSIZE
)
1222 ubc_setsize(rvp
, 0);
1223 if (c_flag
& C_NEED_RVNODE_PUT
)
1229 * Unlock a pair of cnodes.
1233 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1241 * Unlock a group of cnodes.
1245 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1247 struct cnode
* list
[4];
1255 for (i
= 0; i
< k
; ++i
) {
1264 for (i
= 0; i
< k
; ++i
) {
1273 for (i
= 0; i
< k
; ++i
) {
1283 * Protect a cnode against a truncation.
1285 * Used mainly by read/write since they don't hold the
1286 * cnode lock across calls to the cluster layer.
1288 * The process doing a truncation must take the lock
1289 * exclusive. The read/write processes can take it
1294 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1296 #ifdef HFS_CHECK_LOCK_ORDER
1297 if (cp
->c_lockowner
== current_thread())
1298 panic("hfs_lock_truncate: cnode %p locked!", cp
);
1299 #endif /* HFS_CHECK_LOCK_ORDER */
1302 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1304 lck_rw_lock_shared(&cp
->c_truncatelock
);
1309 hfs_unlock_truncate(struct cnode
*cp
, int exclusive
)
1312 lck_rw_unlock_exclusive(&cp
->c_truncatelock
);
1314 lck_rw_unlock_shared(&cp
->c_truncatelock
);