2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/vnode.h>
34 #include <sys/mount.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/quota.h>
40 #include <sys/kdebug.h>
42 #include <kern/locks.h>
44 #include <miscfs/specfs/specdev.h>
45 #include <miscfs/fifofs/fifo.h>
48 #include <hfs/hfs_catalog.h>
49 #include <hfs/hfs_cnode.h>
50 #include <hfs/hfs_quota.h>
54 extern lck_attr_t
* hfs_lock_attr
;
55 extern lck_grp_t
* hfs_mutex_group
;
56 extern lck_grp_t
* hfs_rwlock_group
;
58 static int hfs_filedone(struct vnode
*vp
, vfs_context_t context
);
60 static void hfs_reclaim_cnode(struct cnode
*);
62 static int hfs_valid_cnode(struct hfsmount
*, struct vnode
*, struct componentname
*, cnid_t
);
64 static int hfs_isordered(struct cnode
*, struct cnode
*);
66 int hfs_vnop_inactive(struct vnop_inactive_args
*);
68 int hfs_vnop_reclaim(struct vnop_reclaim_args
*);
72 * Last reference to an cnode. If necessary, write or delete it.
76 hfs_vnop_inactive(struct vnop_inactive_args
*ap
)
78 struct vnode
*vp
= ap
->a_vp
;
80 struct hfsmount
*hfsmp
= VTOHFS(vp
);
81 struct proc
*p
= vfs_context_proc(ap
->a_context
);
87 int took_trunc_lock
= 0;
93 v_type
= vnode_vtype(vp
);
96 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || vnode_issystem(vp
) ||
97 (hfsmp
->hfs_freezing_proc
== p
)) {
102 * Ignore nodes related to stale file handles.
104 if (cp
->c_mode
== 0) {
109 if ((v_type
== VREG
) &&
110 (ISSET(cp
->c_flag
, C_DELETED
) || VTOF(vp
)->ff_blocks
)) {
111 hfs_lock_truncate(cp
, TRUE
);
116 * We do the ubc_setsize before we take the cnode
117 * lock and before the hfs_truncate (since we'll
118 * be inside a transaction).
120 if ((v_type
== VREG
|| v_type
== VLNK
) &&
121 (cp
->c_flag
& C_DELETED
) &&
122 (VTOF(vp
)->ff_blocks
!= 0)) {
126 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
128 if (v_type
== VREG
&& !ISSET(cp
->c_flag
, C_DELETED
) && VTOF(vp
)->ff_blocks
) {
129 hfs_filedone(vp
, ap
->a_context
);
132 * Remove any directory hints
135 hfs_reldirhints(cp
, 0);
142 /* If needed, get rid of any fork's data for a deleted file */
143 if ((v_type
== VREG
|| v_type
== VLNK
) && (cp
->c_flag
& C_DELETED
)) {
144 if (VTOF(vp
)->ff_blocks
!= 0) {
145 // start the transaction out here so that
146 // the truncate and the removal of the file
147 // are all in one transaction. otherwise
148 // because this cnode is marked for deletion
149 // the truncate won't cause the catalog entry
150 // to get updated which means that we could
151 // free blocks but still keep a reference to
152 // them in the catalog entry and then double
155 // if (hfs_start_transaction(hfsmp) != 0) {
162 * Since we're already inside a transaction,
163 * tell hfs_truncate to skip the ubc_setsize.
165 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
174 * Check for a postponed deletion.
175 * (only delete cnode when the last fork goes inactive)
177 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
179 * Mark cnode in transit so that no one can get this
180 * cnode from cnode hash.
182 hfs_chash_mark_in_transit(cp
);
184 cp
->c_flag
&= ~C_DELETED
;
185 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
188 if (started_tr
== 0) {
189 if (hfs_start_transaction(hfsmp
) != 0) {
197 * Reserve some space in the Catalog file.
199 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
204 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
206 if (cp
->c_blocks
> 0)
207 printf("hfs_inactive: attempting to delete a non-empty file!");
211 // release the name pointer in the descriptor so that
212 // cat_delete() will use the file-id to do the deletion.
213 // in the case of hard links this is imperative (in the
214 // case of regular files the fileid and cnid are the
215 // same so it doesn't matter).
217 cat_releasedesc(&cp
->c_desc
);
220 * The descriptor name may be zero,
221 * in which case the fileid is used.
223 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
225 if (error
&& truncated
&& (error
!= ENXIO
))
226 printf("hfs_inactive: couldn't delete a truncated file!");
228 /* Update HFS Private Data dir */
230 hfsmp
->hfs_privdir_attr
.ca_entries
--;
231 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
232 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
236 /* Delete any attributes, ignore errors */
237 (void) hfs_removeallattr(hfsmp
, cp
->c_fileid
);
240 hfs_systemfile_unlock(hfsmp
, lockflags
);
246 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
250 cp
->c_flag
|= C_NOEXISTS
;
251 cp
->c_touch_chgtime
= TRUE
;
252 cp
->c_touch_modtime
= TRUE
;
255 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
258 if ((cp
->c_flag
& C_MODIFIED
) ||
259 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
264 cat_postflight(hfsmp
, &cookie
, p
);
266 // XXXdbg - have to do this because a goto could have come here
268 hfs_end_transaction(hfsmp
);
275 hfs_unlock_truncate(cp
);
278 * If we are done with the vnode, reclaim it
279 * so that it can be reused immediately.
281 if (cp
->c_mode
== 0 || recycle
)
288 * File clean-up (zero fill and shrink peof).
291 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
295 struct hfsmount
*hfsmp
;
297 u_long blks
, blocksize
;
304 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
308 (void) cluster_push(vp
, IO_CLOSE
);
309 hfs_lock(cp
, HFS_FORCE_LOCK
);
312 * Explicitly zero out the areas of file
313 * that are currently marked invalid.
315 while (!CIRCLEQ_EMPTY(&fp
->ff_invalidranges
)) {
316 struct rl_entry
*invalid_range
= CIRCLEQ_FIRST(&fp
->ff_invalidranges
);
317 off_t start
= invalid_range
->rl_start
;
318 off_t end
= invalid_range
->rl_end
;
320 /* The range about to be written must be validated
321 * first, so that VNOP_BLOCKMAP() will return the
322 * appropriate mapping for the cluster code:
324 rl_remove(start
, end
, &fp
->ff_invalidranges
);
327 (void) cluster_write(vp
, (struct uio
*) 0,
328 leof
, end
+ 1, start
, (off_t
)0,
329 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
330 hfs_lock(cp
, HFS_FORCE_LOCK
);
331 cp
->c_flag
|= C_MODIFIED
;
333 cp
->c_flag
&= ~C_ZFWANTSYNC
;
335 blocksize
= VTOVCB(vp
)->blockSize
;
336 blks
= leof
/ blocksize
;
337 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
340 * Shrink the peof to the smallest size neccessary to contain the leof.
342 if (blks
< fp
->ff_blocks
)
343 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, context
);
345 (void) cluster_push(vp
, IO_CLOSE
);
346 hfs_lock(cp
, HFS_FORCE_LOCK
);
349 * If the hfs_truncate didn't happen to flush the vnode's
350 * information out to disk, force it to be updated now that
351 * all invalid ranges have been zero-filled and validated:
353 if (cp
->c_flag
& C_MODIFIED
) {
361 * Reclaim a cnode so that it can be used for other purposes.
365 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
367 struct vnode
*vp
= ap
->a_vp
;
369 struct filefork
*fp
= NULL
;
370 struct filefork
*altfp
= NULL
;
371 int reclaim_cnode
= 0;
373 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
377 * Keep track of an inactive hot file.
379 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
380 (void) hfs_addhotfile(vp
);
382 vnode_removefsref(vp
);
385 * Find file fork for this vnode (if any)
386 * Also check if another fork is active
388 if (cp
->c_vp
== vp
) {
390 altfp
= cp
->c_rsrcfork
;
392 cp
->c_datafork
= NULL
;
394 } else if (cp
->c_rsrc_vp
== vp
) {
396 altfp
= cp
->c_datafork
;
398 cp
->c_rsrcfork
= NULL
;
399 cp
->c_rsrc_vp
= NULL
;
401 panic("hfs_vnop_reclaim: vp points to wrong cnode\n");
404 * On the last fork, remove the cnode from its hash chain.
407 /* If we can't remove it then the cnode must persist! */
408 if (hfs_chashremove(cp
) == 0)
411 * Remove any directory hints
413 if (vnode_isdir(vp
)) {
414 hfs_reldirhints(cp
, 0);
417 /* Release the file fork and related data */
419 /* Dump cached symlink data */
420 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
421 FREE(fp
->ff_symlinkptr
, M_TEMP
);
423 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
427 * If there was only one active fork then we can release the cnode.
430 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
431 hfs_reclaim_cnode(cp
);
432 } else /* cnode in use */ {
436 vnode_clearfsnode(vp
);
441 extern int (**hfs_vnodeop_p
) (void *);
442 extern int (**hfs_specop_p
) (void *);
443 extern int (**hfs_fifoop_p
) (void *);
446 * hfs_getnewvnode - get new default vnode
448 * The vnode is returned with an iocount and the cnode locked
453 struct hfsmount
*hfsmp
,
455 struct componentname
*cnp
,
456 struct cat_desc
*descp
,
458 struct cat_attr
*attrp
,
459 struct cat_fork
*forkp
,
462 struct mount
*mp
= HFSTOVFS(hfsmp
);
463 struct vnode
*vp
= NULL
;
465 struct vnode
*tvp
= NULLVP
;
466 struct cnode
*cp
= NULL
;
467 struct filefork
*fp
= NULL
;
471 struct vnode_fsparam vfsp
;
474 if (attrp
->ca_fileid
== 0) {
480 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
485 vtype
= IFTOVT(attrp
->ca_mode
);
486 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
489 * Get a cnode (new or existing)
490 * skip getting the cnode lock if we are getting resource fork (wantrsrc == 2)
492 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (wantrsrc
== 2));
494 /* Hardlinks may need an updated catalog descriptor */
495 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
496 replace_desc(cp
, descp
);
498 /* Check if we found a matching vnode */
503 * If this is a new cnode then initialize it.
505 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
506 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
508 /* Make sure its still valid (ie exists on disk). */
509 if (!hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
511 hfs_reclaim_cnode(cp
);
515 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
516 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
518 /* The name was inherited so clear descriptor state... */
519 descp
->cd_namelen
= 0;
520 descp
->cd_nameptr
= NULL
;
521 descp
->cd_flags
&= ~CD_HASBUF
;
524 if (IFTOVT(cp
->c_mode
) == VREG
&&
525 (descp
->cd_cnid
!= attrp
->ca_fileid
)) {
526 cp
->c_flag
|= C_HARDLINK
;
529 /* Take one dev reference for each non-directory cnode */
530 if (IFTOVT(cp
->c_mode
) != VDIR
) {
531 cp
->c_devvp
= hfsmp
->hfs_devvp
;
532 vnode_ref(cp
->c_devvp
);
535 for (i
= 0; i
< MAXQUOTAS
; i
++)
536 cp
->c_dquot
[i
] = NODQUOT
;
540 if (IFTOVT(cp
->c_mode
) == VDIR
) {
541 if (cp
->c_vp
!= NULL
)
542 panic("hfs_getnewvnode: orphaned vnode (data)");
545 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
546 panic("hfs_getnewvnode: bad ca_blocks (too small)");
548 * Allocate and initialize a file fork...
550 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
551 M_HFSFORK
, M_WAITOK
);
554 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
556 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
557 rl_init(&fp
->ff_invalidranges
);
558 fp
->ff_sysfileinfo
= 0;
561 if (cp
->c_rsrcfork
!= NULL
)
562 panic("hfs_getnewvnode: orphaned rsrc fork");
563 if (cp
->c_rsrc_vp
!= NULL
)
564 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
566 cvpp
= &cp
->c_rsrc_vp
;
567 if ( (tvp
= cp
->c_vp
) != NULLVP
)
568 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
570 if (cp
->c_datafork
!= NULL
)
571 panic("hfs_getnewvnode: orphaned data fork");
572 if (cp
->c_vp
!= NULL
)
573 panic("hfs_getnewvnode: orphaned vnode (data)");
576 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
577 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
582 * grab an iocount on the vnode we weren't
583 * interested in (i.e. we want the resource fork
584 * but the cnode already has the data fork)
585 * to prevent it from being
586 * recycled by us when we call vnode_create
587 * which will result in a deadlock when we
588 * try to take the cnode lock in hfs_vnop_fsync or
589 * hfs_vnop_reclaim... vnode_get can be called here
590 * because we already hold the cnode lock which will
591 * prevent the vnode from changing identity until
592 * we drop it.. vnode_get will not block waiting for
593 * a change of state... however, it will return an
594 * error if the current iocount == 0 and we've already
595 * started to terminate the vnode... we don't need/want to
596 * grab an iocount in the case since we can't cause
597 * the fileystem to be re-entered on this thread for this vp
599 * the matching vnode_put will happen in hfs_unlock
600 * after we've dropped the cnode lock
602 if ( vnode_get(tvp
) != 0)
603 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
606 vfsp
.vnfs_vtype
= vtype
;
607 vfsp
.vnfs_str
= "hfs";
609 vfsp
.vnfs_fsnode
= cp
;
612 vfsp
.vnfs_vops
= hfs_fifoop_p
;
613 else if (vtype
== VBLK
|| vtype
== VCHR
)
614 vfsp
.vnfs_vops
= hfs_specop_p
;
616 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
618 if (vtype
== VBLK
|| vtype
== VCHR
)
619 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
624 vfsp
.vnfs_filesize
= forkp
->cf_size
;
626 vfsp
.vnfs_filesize
= 0;
628 if (dvp
&& cnp
&& (cnp
->cn_flags
& MAKEENTRY
))
631 vfsp
.vnfs_flags
= VNFS_NOCACHE
;
633 /* Tag system files */
634 vfsp
.vnfs_marksystem
= issystemfile
;
636 /* Tag root directory */
637 if (descp
->cd_cnid
== kHFSRootFolderID
)
638 vfsp
.vnfs_markroot
= 1;
640 vfsp
.vnfs_markroot
= 0;
642 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
644 if (fp
== cp
->c_datafork
)
645 cp
->c_datafork
= NULL
;
647 cp
->c_rsrcfork
= NULL
;
649 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
652 * If this is a newly created cnode or a vnode reclaim
653 * occurred during the attachment, then cleanup the cnode.
655 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
657 hfs_reclaim_cnode(cp
);
659 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
667 vnode_settag(vp
, VT_HFS
);
668 if (cp
->c_flag
& C_HARDLINK
)
669 vnode_set_hard_link(vp
);
670 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
673 * Stop tracking an active hot file.
675 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
676 (void) hfs_removehotfile(vp
);
684 hfs_reclaim_cnode(struct cnode
*cp
)
689 for (i
= 0; i
< MAXQUOTAS
; i
++) {
690 if (cp
->c_dquot
[i
] != NODQUOT
) {
691 dqreclaim(cp
->c_dquot
[i
]);
692 cp
->c_dquot
[i
] = NODQUOT
;
698 struct vnode
*tmp_vp
= cp
->c_devvp
;
705 * If the descriptor has a name then release it
707 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
710 nameptr
= cp
->c_desc
.cd_nameptr
;
711 cp
->c_desc
.cd_nameptr
= 0;
712 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
713 cp
->c_desc
.cd_namelen
= 0;
714 vfs_removename(nameptr
);
717 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
718 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
719 bzero(cp
, sizeof(struct cnode
));
720 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
725 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
727 struct cat_attr attr
;
728 struct cat_desc cndesc
;
732 /* System files are always valid */
733 if (cnid
< kHFSFirstUserCatalogNodeID
)
736 /* XXX optimization: check write count in dvp */
738 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
741 bzero(&cndesc
, sizeof(cndesc
));
742 cndesc
.cd_nameptr
= cnp
->cn_nameptr
;
743 cndesc
.cd_namelen
= cnp
->cn_namelen
;
744 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_cnid
;
745 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
747 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
748 (cnid
== attr
.ca_fileid
)) {
752 if (cat_idlookup(hfsmp
, cnid
, NULL
, NULL
, NULL
) == 0) {
756 hfs_systemfile_unlock(hfsmp
, lockflags
);
762 * Touch cnode times based on c_touch_xxx flags
764 * cnode must be locked exclusive
766 * This will also update the volume modify time
770 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
772 /* HFS Standard doesn't support access times */
773 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
774 cp
->c_touch_acctime
= FALSE
;
777 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
783 if (cp
->c_touch_acctime
) {
784 cp
->c_atime
= tv
.tv_sec
;
786 * When the access time is the only thing changing
787 * then make sure its sufficiently newer before
788 * committing it to disk.
790 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
791 ATIME_ONDISK_ACCURACY
)) {
792 cp
->c_flag
|= C_MODIFIED
;
794 cp
->c_touch_acctime
= FALSE
;
796 if (cp
->c_touch_modtime
) {
797 cp
->c_mtime
= tv
.tv_sec
;
798 cp
->c_touch_modtime
= FALSE
;
799 cp
->c_flag
|= C_MODIFIED
;
803 * HFS dates that WE set must be adjusted for DST
805 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
810 if (cp
->c_touch_chgtime
) {
811 cp
->c_ctime
= tv
.tv_sec
;
812 cp
->c_touch_chgtime
= FALSE
;
813 cp
->c_flag
|= C_MODIFIED
;
817 /* Touch the volume modtime if needed */
819 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
820 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
830 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
832 void * thread
= current_thread();
834 /* System files need to keep track of owner */
835 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
836 (cp
->c_fileid
> kHFSRootFolderID
) &&
837 (locktype
!= HFS_SHARED_LOCK
)) {
840 * The extents and bitmap file locks support
841 * recursion and are always taken exclusive.
843 if (cp
->c_fileid
== kHFSExtentsFileID
||
844 cp
->c_fileid
== kHFSAllocationFileID
) {
845 if (cp
->c_lockowner
== thread
) {
846 cp
->c_syslockcount
++;
848 lck_rw_lock_exclusive(&cp
->c_rwlock
);
849 cp
->c_lockowner
= thread
;
850 cp
->c_syslockcount
= 1;
853 lck_rw_lock_exclusive(&cp
->c_rwlock
);
854 cp
->c_lockowner
= thread
;
856 } else if (locktype
== HFS_SHARED_LOCK
) {
857 lck_rw_lock_shared(&cp
->c_rwlock
);
858 cp
->c_lockowner
= HFS_SHARED_OWNER
;
860 lck_rw_lock_exclusive(&cp
->c_rwlock
);
861 cp
->c_lockowner
= thread
;
864 * Skip cnodes that no longer exist (were deleted).
866 if ((locktype
!= HFS_FORCE_LOCK
) &&
867 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
868 (cp
->c_flag
& C_NOEXISTS
)) {
876 * Lock a pair of cnodes.
880 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
882 struct cnode
*first
, *last
;
886 * If cnodes match then just lock one.
889 return hfs_lock(cp1
, locktype
);
893 * Lock in cnode parent-child order (if there is a relationship);
894 * otherwise lock in cnode address order.
896 if ((IFTOVT(cp1
->c_mode
) == VDIR
) && (cp1
->c_fileid
== cp2
->c_parentcnid
)) {
899 } else if (cp1
< cp2
) {
907 if ( (error
= hfs_lock(first
, locktype
))) {
910 if ( (error
= hfs_lock(last
, locktype
))) {
918 * Check ordering of two cnodes. Return true if they are are in-order.
921 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
925 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
927 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
929 if (cp1
->c_fileid
== cp2
->c_parentcnid
)
930 return (1); /* cp1 is the parent and should go first */
931 if (cp2
->c_fileid
== cp1
->c_parentcnid
)
932 return (0); /* cp1 is the child and should go last */
934 return (cp1
< cp2
); /* fall-back is to use address order */
938 * Acquire 4 cnode locks.
939 * - locked in cnode parent-child order (if there is a relationship)
940 * otherwise lock in cnode address order (lesser address first).
941 * - all or none of the locks are taken
942 * - only one lock taken per cnode (dup cnodes are skipped)
943 * - some of the cnode pointers may be null
947 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
948 struct cnode
*cp4
, enum hfslocktype locktype
)
952 struct cnode
* list
[4];
957 if (hfs_isordered(cp1
, cp2
)) {
958 a
[0] = cp1
; a
[1] = cp2
;
960 a
[0] = cp2
; a
[1] = cp1
;
962 if (hfs_isordered(cp3
, cp4
)) {
963 b
[0] = cp3
; b
[1] = cp4
;
965 b
[0] = cp4
; b
[1] = cp3
;
967 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
968 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
971 * Build the lock list, skipping over duplicates
973 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
974 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
975 if (k
== 0 || tmp
!= list
[k
-1])
980 * Now we can lock using list[0 - k].
981 * Skip over NULL entries.
983 for (i
= 0; i
< k
; ++i
) {
985 if ((error
= hfs_lock(list
[i
], locktype
))) {
986 /* Drop any locks we acquired. */
1003 hfs_unlock(struct cnode
*cp
)
1005 vnode_t rvp
= NULLVP
;
1006 vnode_t dvp
= NULLVP
;
1008 /* System files need to keep track of owner */
1009 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
1010 (cp
->c_fileid
> kHFSRootFolderID
) &&
1011 (cp
->c_datafork
!= NULL
)) {
1013 * The extents and bitmap file locks support
1014 * recursion and are always taken exclusive.
1016 if (cp
->c_fileid
== kHFSExtentsFileID
||
1017 cp
->c_fileid
== kHFSAllocationFileID
) {
1018 if (--cp
->c_syslockcount
> 0) {
1023 if (cp
->c_flag
& C_NEED_DVNODE_PUT
)
1026 if (cp
->c_flag
& C_NEED_RVNODE_PUT
)
1027 rvp
= cp
->c_rsrc_vp
;
1029 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
);
1031 cp
-> c_lockowner
= NULL
;
1032 lck_rw_done(&cp
->c_rwlock
);
1041 * Unlock a pair of cnodes.
1045 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1053 * Unlock a group of cnodes.
1057 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1059 struct cnode
* list
[4];
1067 for (i
= 0; i
< k
; ++i
) {
1076 for (i
= 0; i
< k
; ++i
) {
1085 for (i
= 0; i
< k
; ++i
) {
1095 * Protect a cnode against a truncation.
1097 * Used mainly by read/write since they don't hold the
1098 * cnode lock across calls to the cluster layer.
1100 * The process doing a truncation must take the lock
1101 * exclusive. The read/write processes can take it
1106 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1108 if (cp
->c_lockowner
== current_thread())
1109 panic("hfs_lock_truncate: cnode 0x%08x locked!", cp
);
1112 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1114 lck_rw_lock_shared(&cp
->c_truncatelock
);
1119 hfs_unlock_truncate(struct cnode
*cp
)
1121 lck_rw_done(&cp
->c_truncatelock
);