2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <sys/param.h>
23 #include <sys/systm.h>
25 #include <sys/vnode.h>
26 #include <sys/mount.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
31 #include <sys/quota.h>
32 #include <sys/kdebug.h>
34 #include <kern/locks.h>
36 #include <miscfs/specfs/specdev.h>
37 #include <miscfs/fifofs/fifo.h>
40 #include <hfs/hfs_catalog.h>
41 #include <hfs/hfs_cnode.h>
42 #include <hfs/hfs_quota.h>
46 extern lck_attr_t
* hfs_lock_attr
;
47 extern lck_grp_t
* hfs_mutex_group
;
48 extern lck_grp_t
* hfs_rwlock_group
;
50 static int hfs_filedone(struct vnode
*vp
, vfs_context_t context
);
52 static void hfs_reclaim_cnode(struct cnode
*);
54 static int hfs_valid_cnode(struct hfsmount
*, struct vnode
*, struct componentname
*, cnid_t
);
56 static int hfs_isordered(struct cnode
*, struct cnode
*);
58 int hfs_vnop_inactive(struct vnop_inactive_args
*);
60 int hfs_vnop_reclaim(struct vnop_reclaim_args
*);
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
) &&
102 (ISSET(cp
->c_flag
, C_DELETED
) || VTOF(vp
)->ff_blocks
)) {
103 hfs_lock_truncate(cp
, TRUE
);
108 * We do the ubc_setsize before we take the cnode
109 * lock and before the hfs_truncate (since we'll
110 * be inside a transaction).
112 if ((v_type
== VREG
|| v_type
== VLNK
) &&
113 (cp
->c_flag
& C_DELETED
) &&
114 (VTOF(vp
)->ff_blocks
!= 0)) {
118 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
120 if (v_type
== VREG
&& !ISSET(cp
->c_flag
, C_DELETED
) && VTOF(vp
)->ff_blocks
) {
121 hfs_filedone(vp
, ap
->a_context
);
124 * Remove any directory hints
127 hfs_reldirhints(cp
, 0);
134 /* If needed, get rid of any fork's data for a deleted file */
135 if ((v_type
== VREG
|| v_type
== VLNK
) && (cp
->c_flag
& C_DELETED
)) {
136 if (VTOF(vp
)->ff_blocks
!= 0) {
137 // start the transaction out here so that
138 // the truncate and the removal of the file
139 // are all in one transaction. otherwise
140 // because this cnode is marked for deletion
141 // the truncate won't cause the catalog entry
142 // to get updated which means that we could
143 // free blocks but still keep a reference to
144 // them in the catalog entry and then double
147 // if (hfs_start_transaction(hfsmp) != 0) {
154 * Since we're already inside a transaction,
155 * tell hfs_truncate to skip the ubc_setsize.
157 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
166 * Check for a postponed deletion.
167 * (only delete cnode when the last fork goes inactive)
169 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
171 * Mark cnode in transit so that no one can get this
172 * cnode from cnode hash.
174 hfs_chash_mark_in_transit(cp
);
176 cp
->c_flag
&= ~C_DELETED
;
177 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
180 if (started_tr
== 0) {
181 if (hfs_start_transaction(hfsmp
) != 0) {
189 * Reserve some space in the Catalog file.
191 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
196 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
198 if (cp
->c_blocks
> 0)
199 printf("hfs_inactive: attempting to delete a non-empty file!");
203 // release the name pointer in the descriptor so that
204 // cat_delete() will use the file-id to do the deletion.
205 // in the case of hard links this is imperative (in the
206 // case of regular files the fileid and cnid are the
207 // same so it doesn't matter).
209 cat_releasedesc(&cp
->c_desc
);
212 * The descriptor name may be zero,
213 * in which case the fileid is used.
215 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
217 if (error
&& truncated
&& (error
!= ENXIO
))
218 printf("hfs_inactive: couldn't delete a truncated file!");
220 /* Update HFS Private Data dir */
222 hfsmp
->hfs_privdir_attr
.ca_entries
--;
223 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
224 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
228 /* Delete any attributes, ignore errors */
229 (void) hfs_removeallattr(hfsmp
, cp
->c_fileid
);
232 hfs_systemfile_unlock(hfsmp
, lockflags
);
238 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
242 cp
->c_flag
|= C_NOEXISTS
;
243 cp
->c_touch_chgtime
= TRUE
;
244 cp
->c_touch_modtime
= TRUE
;
247 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
250 if ((cp
->c_flag
& C_MODIFIED
) ||
251 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
256 cat_postflight(hfsmp
, &cookie
, p
);
258 // XXXdbg - have to do this because a goto could have come here
260 hfs_end_transaction(hfsmp
);
267 hfs_unlock_truncate(cp
);
270 * If we are done with the vnode, reclaim it
271 * so that it can be reused immediately.
273 if (cp
->c_mode
== 0 || recycle
)
280 * File clean-up (zero fill and shrink peof).
283 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
287 struct hfsmount
*hfsmp
;
289 u_long blks
, blocksize
;
296 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
300 (void) cluster_push(vp
, IO_CLOSE
);
301 hfs_lock(cp
, HFS_FORCE_LOCK
);
304 * Explicitly zero out the areas of file
305 * that are currently marked invalid.
307 while (!CIRCLEQ_EMPTY(&fp
->ff_invalidranges
)) {
308 struct rl_entry
*invalid_range
= CIRCLEQ_FIRST(&fp
->ff_invalidranges
);
309 off_t start
= invalid_range
->rl_start
;
310 off_t end
= invalid_range
->rl_end
;
312 /* The range about to be written must be validated
313 * first, so that VNOP_BLOCKMAP() will return the
314 * appropriate mapping for the cluster code:
316 rl_remove(start
, end
, &fp
->ff_invalidranges
);
319 (void) cluster_write(vp
, (struct uio
*) 0,
320 leof
, end
+ 1, start
, (off_t
)0,
321 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
322 hfs_lock(cp
, HFS_FORCE_LOCK
);
323 cp
->c_flag
|= C_MODIFIED
;
325 cp
->c_flag
&= ~C_ZFWANTSYNC
;
327 blocksize
= VTOVCB(vp
)->blockSize
;
328 blks
= leof
/ blocksize
;
329 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
332 * Shrink the peof to the smallest size neccessary to contain the leof.
334 if (blks
< fp
->ff_blocks
)
335 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, context
);
337 (void) cluster_push(vp
, IO_CLOSE
);
338 hfs_lock(cp
, HFS_FORCE_LOCK
);
341 * If the hfs_truncate didn't happen to flush the vnode's
342 * information out to disk, force it to be updated now that
343 * all invalid ranges have been zero-filled and validated:
345 if (cp
->c_flag
& C_MODIFIED
) {
353 * Reclaim a cnode so that it can be used for other purposes.
357 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
359 struct vnode
*vp
= ap
->a_vp
;
361 struct filefork
*fp
= NULL
;
362 struct filefork
*altfp
= NULL
;
363 int reclaim_cnode
= 0;
365 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
369 * Keep track of an inactive hot file.
371 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
372 (void) hfs_addhotfile(vp
);
374 vnode_removefsref(vp
);
377 * Find file fork for this vnode (if any)
378 * Also check if another fork is active
380 if (cp
->c_vp
== vp
) {
382 altfp
= cp
->c_rsrcfork
;
384 cp
->c_datafork
= NULL
;
386 } else if (cp
->c_rsrc_vp
== vp
) {
388 altfp
= cp
->c_datafork
;
390 cp
->c_rsrcfork
= NULL
;
391 cp
->c_rsrc_vp
= NULL
;
393 panic("hfs_vnop_reclaim: vp points to wrong cnode\n");
396 * On the last fork, remove the cnode from its hash chain.
399 /* If we can't remove it then the cnode must persist! */
400 if (hfs_chashremove(cp
) == 0)
403 * Remove any directory hints
405 if (vnode_isdir(vp
)) {
406 hfs_reldirhints(cp
, 0);
409 /* Release the file fork and related data */
411 /* Dump cached symlink data */
412 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
413 FREE(fp
->ff_symlinkptr
, M_TEMP
);
415 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
419 * If there was only one active fork then we can release the cnode.
422 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
423 hfs_reclaim_cnode(cp
);
424 } else /* cnode in use */ {
428 vnode_clearfsnode(vp
);
433 extern int (**hfs_vnodeop_p
) (void *);
434 extern int (**hfs_specop_p
) (void *);
435 extern int (**hfs_fifoop_p
) (void *);
438 * hfs_getnewvnode - get new default vnode
440 * The vnode is returned with an iocount and the cnode locked
445 struct hfsmount
*hfsmp
,
447 struct componentname
*cnp
,
448 struct cat_desc
*descp
,
450 struct cat_attr
*attrp
,
451 struct cat_fork
*forkp
,
454 struct mount
*mp
= HFSTOVFS(hfsmp
);
455 struct vnode
*vp
= NULL
;
457 struct vnode
*tvp
= NULLVP
;
458 struct cnode
*cp
= NULL
;
459 struct filefork
*fp
= NULL
;
463 struct vnode_fsparam vfsp
;
466 if (attrp
->ca_fileid
== 0) {
472 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
477 vtype
= IFTOVT(attrp
->ca_mode
);
478 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
481 * Get a cnode (new or existing)
482 * skip getting the cnode lock if we are getting resource fork (wantrsrc == 2)
484 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (wantrsrc
== 2));
486 /* Hardlinks may need an updated catalog descriptor */
487 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
488 replace_desc(cp
, descp
);
490 /* Check if we found a matching vnode */
495 * If this is a new cnode then initialize it.
497 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
498 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
500 /* Make sure its still valid (ie exists on disk). */
501 if (!hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
503 hfs_reclaim_cnode(cp
);
507 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
508 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
510 /* The name was inherited so clear descriptor state... */
511 descp
->cd_namelen
= 0;
512 descp
->cd_nameptr
= NULL
;
513 descp
->cd_flags
&= ~CD_HASBUF
;
516 if (IFTOVT(cp
->c_mode
) == VREG
&&
517 (descp
->cd_cnid
!= attrp
->ca_fileid
)) {
518 cp
->c_flag
|= C_HARDLINK
;
521 /* Take one dev reference for each non-directory cnode */
522 if (IFTOVT(cp
->c_mode
) != VDIR
) {
523 cp
->c_devvp
= hfsmp
->hfs_devvp
;
524 vnode_ref(cp
->c_devvp
);
527 for (i
= 0; i
< MAXQUOTAS
; i
++)
528 cp
->c_dquot
[i
] = NODQUOT
;
532 if (IFTOVT(cp
->c_mode
) == VDIR
) {
533 if (cp
->c_vp
!= NULL
)
534 panic("hfs_getnewvnode: orphaned vnode (data)");
537 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
538 panic("hfs_getnewvnode: bad ca_blocks (too small)");
540 * Allocate and initialize a file fork...
542 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
543 M_HFSFORK
, M_WAITOK
);
546 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
548 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
549 rl_init(&fp
->ff_invalidranges
);
550 fp
->ff_sysfileinfo
= 0;
553 if (cp
->c_rsrcfork
!= NULL
)
554 panic("hfs_getnewvnode: orphaned rsrc fork");
555 if (cp
->c_rsrc_vp
!= NULL
)
556 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
558 cvpp
= &cp
->c_rsrc_vp
;
559 if ( (tvp
= cp
->c_vp
) != NULLVP
)
560 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
562 if (cp
->c_datafork
!= NULL
)
563 panic("hfs_getnewvnode: orphaned data fork");
564 if (cp
->c_vp
!= NULL
)
565 panic("hfs_getnewvnode: orphaned vnode (data)");
568 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
569 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
574 * grab an iocount on the vnode we weren't
575 * interested in (i.e. we want the resource fork
576 * but the cnode already has the data fork)
577 * to prevent it from being
578 * recycled by us when we call vnode_create
579 * which will result in a deadlock when we
580 * try to take the cnode lock in hfs_vnop_fsync or
581 * hfs_vnop_reclaim... vnode_get can be called here
582 * because we already hold the cnode lock which will
583 * prevent the vnode from changing identity until
584 * we drop it.. vnode_get will not block waiting for
585 * a change of state... however, it will return an
586 * error if the current iocount == 0 and we've already
587 * started to terminate the vnode... we don't need/want to
588 * grab an iocount in the case since we can't cause
589 * the fileystem to be re-entered on this thread for this vp
591 * the matching vnode_put will happen in hfs_unlock
592 * after we've dropped the cnode lock
594 if ( vnode_get(tvp
) != 0)
595 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
598 vfsp
.vnfs_vtype
= vtype
;
599 vfsp
.vnfs_str
= "hfs";
601 vfsp
.vnfs_fsnode
= cp
;
604 vfsp
.vnfs_vops
= hfs_fifoop_p
;
605 else if (vtype
== VBLK
|| vtype
== VCHR
)
606 vfsp
.vnfs_vops
= hfs_specop_p
;
608 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
610 if (vtype
== VBLK
|| vtype
== VCHR
)
611 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
616 vfsp
.vnfs_filesize
= forkp
->cf_size
;
618 vfsp
.vnfs_filesize
= 0;
620 if (dvp
&& cnp
&& (cnp
->cn_flags
& MAKEENTRY
))
623 vfsp
.vnfs_flags
= VNFS_NOCACHE
;
625 /* Tag system files */
626 vfsp
.vnfs_marksystem
= issystemfile
;
628 /* Tag root directory */
629 if (descp
->cd_cnid
== kHFSRootFolderID
)
630 vfsp
.vnfs_markroot
= 1;
632 vfsp
.vnfs_markroot
= 0;
634 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
636 if (fp
== cp
->c_datafork
)
637 cp
->c_datafork
= NULL
;
639 cp
->c_rsrcfork
= NULL
;
641 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
644 * If this is a newly created cnode or a vnode reclaim
645 * occurred during the attachment, then cleanup the cnode.
647 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
649 hfs_reclaim_cnode(cp
);
651 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
659 vnode_settag(vp
, VT_HFS
);
660 if (cp
->c_flag
& C_HARDLINK
)
661 vnode_set_hard_link(vp
);
662 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
665 * Stop tracking an active hot file.
667 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
668 (void) hfs_removehotfile(vp
);
676 hfs_reclaim_cnode(struct cnode
*cp
)
681 for (i
= 0; i
< MAXQUOTAS
; i
++) {
682 if (cp
->c_dquot
[i
] != NODQUOT
) {
683 dqreclaim(cp
->c_dquot
[i
]);
684 cp
->c_dquot
[i
] = NODQUOT
;
690 struct vnode
*tmp_vp
= cp
->c_devvp
;
697 * If the descriptor has a name then release it
699 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
702 nameptr
= cp
->c_desc
.cd_nameptr
;
703 cp
->c_desc
.cd_nameptr
= 0;
704 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
705 cp
->c_desc
.cd_namelen
= 0;
706 vfs_removename(nameptr
);
709 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
710 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
711 bzero(cp
, sizeof(struct cnode
));
712 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
717 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
719 struct cat_attr attr
;
720 struct cat_desc cndesc
;
724 /* System files are always valid */
725 if (cnid
< kHFSFirstUserCatalogNodeID
)
728 /* XXX optimization: check write count in dvp */
730 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
733 bzero(&cndesc
, sizeof(cndesc
));
734 cndesc
.cd_nameptr
= cnp
->cn_nameptr
;
735 cndesc
.cd_namelen
= cnp
->cn_namelen
;
736 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_cnid
;
737 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
739 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
740 (cnid
== attr
.ca_fileid
)) {
744 if (cat_idlookup(hfsmp
, cnid
, NULL
, NULL
, NULL
) == 0) {
748 hfs_systemfile_unlock(hfsmp
, lockflags
);
754 * Touch cnode times based on c_touch_xxx flags
756 * cnode must be locked exclusive
758 * This will also update the volume modify time
762 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
764 /* HFS Standard doesn't support access times */
765 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
766 cp
->c_touch_acctime
= FALSE
;
769 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
775 if (cp
->c_touch_acctime
) {
776 cp
->c_atime
= tv
.tv_sec
;
778 * When the access time is the only thing changing
779 * then make sure its sufficiently newer before
780 * committing it to disk.
782 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
783 ATIME_ONDISK_ACCURACY
)) {
784 cp
->c_flag
|= C_MODIFIED
;
786 cp
->c_touch_acctime
= FALSE
;
788 if (cp
->c_touch_modtime
) {
789 cp
->c_mtime
= tv
.tv_sec
;
790 cp
->c_touch_modtime
= FALSE
;
791 cp
->c_flag
|= C_MODIFIED
;
795 * HFS dates that WE set must be adjusted for DST
797 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
802 if (cp
->c_touch_chgtime
) {
803 cp
->c_ctime
= tv
.tv_sec
;
804 cp
->c_touch_chgtime
= FALSE
;
805 cp
->c_flag
|= C_MODIFIED
;
809 /* Touch the volume modtime if needed */
811 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
812 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
822 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
824 void * thread
= current_thread();
826 /* System files need to keep track of owner */
827 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
828 (cp
->c_fileid
> kHFSRootFolderID
) &&
829 (locktype
!= HFS_SHARED_LOCK
)) {
832 * The extents and bitmap file locks support
833 * recursion and are always taken exclusive.
835 if (cp
->c_fileid
== kHFSExtentsFileID
||
836 cp
->c_fileid
== kHFSAllocationFileID
) {
837 if (cp
->c_lockowner
== thread
) {
838 cp
->c_syslockcount
++;
840 lck_rw_lock_exclusive(&cp
->c_rwlock
);
841 cp
->c_lockowner
= thread
;
842 cp
->c_syslockcount
= 1;
845 lck_rw_lock_exclusive(&cp
->c_rwlock
);
846 cp
->c_lockowner
= thread
;
848 } else if (locktype
== HFS_SHARED_LOCK
) {
849 lck_rw_lock_shared(&cp
->c_rwlock
);
850 cp
->c_lockowner
= HFS_SHARED_OWNER
;
852 lck_rw_lock_exclusive(&cp
->c_rwlock
);
853 cp
->c_lockowner
= thread
;
856 * Skip cnodes that no longer exist (were deleted).
858 if ((locktype
!= HFS_FORCE_LOCK
) &&
859 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
860 (cp
->c_flag
& C_NOEXISTS
)) {
868 * Lock a pair of cnodes.
872 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
874 struct cnode
*first
, *last
;
878 * If cnodes match then just lock one.
881 return hfs_lock(cp1
, locktype
);
885 * Lock in cnode parent-child order (if there is a relationship);
886 * otherwise lock in cnode address order.
888 if ((IFTOVT(cp1
->c_mode
) == VDIR
) && (cp1
->c_fileid
== cp2
->c_parentcnid
)) {
891 } else if (cp1
< cp2
) {
899 if ( (error
= hfs_lock(first
, locktype
))) {
902 if ( (error
= hfs_lock(last
, locktype
))) {
910 * Check ordering of two cnodes. Return true if they are are in-order.
913 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
917 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
919 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
921 if (cp1
->c_fileid
== cp2
->c_parentcnid
)
922 return (1); /* cp1 is the parent and should go first */
923 if (cp2
->c_fileid
== cp1
->c_parentcnid
)
924 return (0); /* cp1 is the child and should go last */
926 return (cp1
< cp2
); /* fall-back is to use address order */
930 * Acquire 4 cnode locks.
931 * - locked in cnode parent-child order (if there is a relationship)
932 * otherwise lock in cnode address order (lesser address first).
933 * - all or none of the locks are taken
934 * - only one lock taken per cnode (dup cnodes are skipped)
935 * - some of the cnode pointers may be null
939 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
940 struct cnode
*cp4
, enum hfslocktype locktype
)
944 struct cnode
* list
[4];
949 if (hfs_isordered(cp1
, cp2
)) {
950 a
[0] = cp1
; a
[1] = cp2
;
952 a
[0] = cp2
; a
[1] = cp1
;
954 if (hfs_isordered(cp3
, cp4
)) {
955 b
[0] = cp3
; b
[1] = cp4
;
957 b
[0] = cp4
; b
[1] = cp3
;
959 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
960 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
963 * Build the lock list, skipping over duplicates
965 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
966 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
967 if (k
== 0 || tmp
!= list
[k
-1])
972 * Now we can lock using list[0 - k].
973 * Skip over NULL entries.
975 for (i
= 0; i
< k
; ++i
) {
977 if ((error
= hfs_lock(list
[i
], locktype
))) {
978 /* Drop any locks we acquired. */
995 hfs_unlock(struct cnode
*cp
)
997 vnode_t rvp
= NULLVP
;
998 vnode_t dvp
= NULLVP
;
1000 /* System files need to keep track of owner */
1001 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
1002 (cp
->c_fileid
> kHFSRootFolderID
) &&
1003 (cp
->c_datafork
!= NULL
)) {
1005 * The extents and bitmap file locks support
1006 * recursion and are always taken exclusive.
1008 if (cp
->c_fileid
== kHFSExtentsFileID
||
1009 cp
->c_fileid
== kHFSAllocationFileID
) {
1010 if (--cp
->c_syslockcount
> 0) {
1015 if (cp
->c_flag
& C_NEED_DVNODE_PUT
)
1018 if (cp
->c_flag
& C_NEED_RVNODE_PUT
)
1019 rvp
= cp
->c_rsrc_vp
;
1021 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
);
1023 cp
-> c_lockowner
= NULL
;
1024 lck_rw_done(&cp
->c_rwlock
);
1033 * Unlock a pair of cnodes.
1037 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1045 * Unlock a group of cnodes.
1049 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1051 struct cnode
* list
[4];
1059 for (i
= 0; i
< k
; ++i
) {
1068 for (i
= 0; i
< k
; ++i
) {
1077 for (i
= 0; i
< k
; ++i
) {
1087 * Protect a cnode against a truncation.
1089 * Used mainly by read/write since they don't hold the
1090 * cnode lock across calls to the cluster layer.
1092 * The process doing a truncation must take the lock
1093 * exclusive. The read/write processes can take it
1098 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1100 if (cp
->c_lockowner
== current_thread())
1101 panic("hfs_lock_truncate: cnode 0x%08x locked!", cp
);
1104 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1106 lck_rw_lock_shared(&cp
->c_truncatelock
);
1111 hfs_unlock_truncate(struct cnode
*cp
)
1113 lck_rw_done(&cp
->c_truncatelock
);