2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <sys/systm.h>
26 #include <sys/vnode.h>
27 #include <sys/mount.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
32 #include <sys/quota.h>
33 #include <sys/kdebug.h>
35 #include <kern/locks.h>
37 #include <miscfs/specfs/specdev.h>
38 #include <miscfs/fifofs/fifo.h>
41 #include <hfs/hfs_catalog.h>
42 #include <hfs/hfs_cnode.h>
43 #include <hfs/hfs_quota.h>
47 extern lck_attr_t
* hfs_lock_attr
;
48 extern lck_grp_t
* hfs_mutex_group
;
49 extern lck_grp_t
* hfs_rwlock_group
;
51 static int hfs_filedone(struct vnode
*vp
, vfs_context_t context
);
53 static void hfs_reclaim_cnode(struct cnode
*);
55 static int hfs_valid_cnode(struct hfsmount
*, struct vnode
*, struct componentname
*, cnid_t
);
57 static int hfs_isordered(struct cnode
*, struct cnode
*);
59 int hfs_vnop_inactive(struct vnop_inactive_args
*);
61 int hfs_vnop_reclaim(struct vnop_reclaim_args
*);
65 * Last reference to an cnode. If necessary, write or delete it.
69 hfs_vnop_inactive(struct vnop_inactive_args
*ap
)
71 struct vnode
*vp
= ap
->a_vp
;
73 struct hfsmount
*hfsmp
= VTOHFS(vp
);
74 struct proc
*p
= vfs_context_proc(ap
->a_context
);
80 int took_trunc_lock
= 0;
86 v_type
= vnode_vtype(vp
);
89 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || vnode_issystem(vp
) ||
90 (hfsmp
->hfs_freezing_proc
== p
)) {
95 * Ignore nodes related to stale file handles.
97 if (cp
->c_mode
== 0) {
102 if ((v_type
== VREG
) &&
103 (ISSET(cp
->c_flag
, C_DELETED
) || VTOF(vp
)->ff_blocks
)) {
104 hfs_lock_truncate(cp
, TRUE
);
109 * We do the ubc_setsize before we take the cnode
110 * lock and before the hfs_truncate (since we'll
111 * be inside a transaction).
113 if ((v_type
== VREG
|| v_type
== VLNK
) &&
114 (cp
->c_flag
& C_DELETED
) &&
115 (VTOF(vp
)->ff_blocks
!= 0)) {
119 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
121 if (v_type
== VREG
&& !ISSET(cp
->c_flag
, C_DELETED
) && VTOF(vp
)->ff_blocks
) {
122 hfs_filedone(vp
, ap
->a_context
);
125 * Remove any directory hints
128 hfs_reldirhints(cp
, 0);
135 /* If needed, get rid of any fork's data for a deleted file */
136 if ((v_type
== VREG
|| v_type
== VLNK
) && (cp
->c_flag
& C_DELETED
)) {
137 if (VTOF(vp
)->ff_blocks
!= 0) {
138 // start the transaction out here so that
139 // the truncate and the removal of the file
140 // are all in one transaction. otherwise
141 // because this cnode is marked for deletion
142 // the truncate won't cause the catalog entry
143 // to get updated which means that we could
144 // free blocks but still keep a reference to
145 // them in the catalog entry and then double
148 // if (hfs_start_transaction(hfsmp) != 0) {
155 * Since we're already inside a transaction,
156 * tell hfs_truncate to skip the ubc_setsize.
158 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, ap
->a_context
);
167 * Check for a postponed deletion.
168 * (only delete cnode when the last fork goes inactive)
170 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
172 * Mark cnode in transit so that no one can get this
173 * cnode from cnode hash.
175 hfs_chash_mark_in_transit(cp
);
177 cp
->c_flag
&= ~C_DELETED
;
178 cp
->c_flag
|= C_NOEXISTS
; // XXXdbg
181 if (started_tr
== 0) {
182 if (hfs_start_transaction(hfsmp
) != 0) {
190 * Reserve some space in the Catalog file.
192 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, &cookie
, p
))) {
197 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
199 if (cp
->c_blocks
> 0)
200 printf("hfs_inactive: attempting to delete a non-empty file!");
204 // release the name pointer in the descriptor so that
205 // cat_delete() will use the file-id to do the deletion.
206 // in the case of hard links this is imperative (in the
207 // case of regular files the fileid and cnid are the
208 // same so it doesn't matter).
210 cat_releasedesc(&cp
->c_desc
);
213 * The descriptor name may be zero,
214 * in which case the fileid is used.
216 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
218 if (error
&& truncated
&& (error
!= ENXIO
))
219 printf("hfs_inactive: couldn't delete a truncated file!");
221 /* Update HFS Private Data dir */
223 hfsmp
->hfs_privdir_attr
.ca_entries
--;
224 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
225 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
229 /* Delete any attributes, ignore errors */
230 (void) hfs_removeallattr(hfsmp
, cp
->c_fileid
);
233 hfs_systemfile_unlock(hfsmp
, lockflags
);
239 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
243 cp
->c_flag
|= C_NOEXISTS
;
244 cp
->c_touch_chgtime
= TRUE
;
245 cp
->c_touch_modtime
= TRUE
;
248 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
251 if ((cp
->c_flag
& C_MODIFIED
) ||
252 cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
257 cat_postflight(hfsmp
, &cookie
, p
);
259 // XXXdbg - have to do this because a goto could have come here
261 hfs_end_transaction(hfsmp
);
268 hfs_unlock_truncate(cp
);
271 * If we are done with the vnode, reclaim it
272 * so that it can be reused immediately.
274 if (cp
->c_mode
== 0 || recycle
)
281 * File clean-up (zero fill and shrink peof).
284 hfs_filedone(struct vnode
*vp
, vfs_context_t context
)
288 struct hfsmount
*hfsmp
;
290 u_long blks
, blocksize
;
297 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (fp
->ff_blocks
== 0))
301 (void) cluster_push(vp
, IO_CLOSE
);
302 hfs_lock(cp
, HFS_FORCE_LOCK
);
305 * Explicitly zero out the areas of file
306 * that are currently marked invalid.
308 while (!CIRCLEQ_EMPTY(&fp
->ff_invalidranges
)) {
309 struct rl_entry
*invalid_range
= CIRCLEQ_FIRST(&fp
->ff_invalidranges
);
310 off_t start
= invalid_range
->rl_start
;
311 off_t end
= invalid_range
->rl_end
;
313 /* The range about to be written must be validated
314 * first, so that VNOP_BLOCKMAP() will return the
315 * appropriate mapping for the cluster code:
317 rl_remove(start
, end
, &fp
->ff_invalidranges
);
320 (void) cluster_write(vp
, (struct uio
*) 0,
321 leof
, end
+ 1, start
, (off_t
)0,
322 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
323 hfs_lock(cp
, HFS_FORCE_LOCK
);
324 cp
->c_flag
|= C_MODIFIED
;
326 cp
->c_flag
&= ~C_ZFWANTSYNC
;
328 blocksize
= VTOVCB(vp
)->blockSize
;
329 blks
= leof
/ blocksize
;
330 if (((off_t
)blks
* (off_t
)blocksize
) != leof
)
333 * Shrink the peof to the smallest size neccessary to contain the leof.
335 if (blks
< fp
->ff_blocks
)
336 (void) hfs_truncate(vp
, leof
, IO_NDELAY
, 0, context
);
338 (void) cluster_push(vp
, IO_CLOSE
);
339 hfs_lock(cp
, HFS_FORCE_LOCK
);
342 * If the hfs_truncate didn't happen to flush the vnode's
343 * information out to disk, force it to be updated now that
344 * all invalid ranges have been zero-filled and validated:
346 if (cp
->c_flag
& C_MODIFIED
) {
354 * Reclaim a cnode so that it can be used for other purposes.
358 hfs_vnop_reclaim(struct vnop_reclaim_args
*ap
)
360 struct vnode
*vp
= ap
->a_vp
;
362 struct filefork
*fp
= NULL
;
363 struct filefork
*altfp
= NULL
;
364 int reclaim_cnode
= 0;
366 (void) hfs_lock(VTOC(vp
), HFS_FORCE_LOCK
);
370 * Keep track of an inactive hot file.
372 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
373 (void) hfs_addhotfile(vp
);
375 vnode_removefsref(vp
);
378 * Find file fork for this vnode (if any)
379 * Also check if another fork is active
381 if (cp
->c_vp
== vp
) {
383 altfp
= cp
->c_rsrcfork
;
385 cp
->c_datafork
= NULL
;
387 } else if (cp
->c_rsrc_vp
== vp
) {
389 altfp
= cp
->c_datafork
;
391 cp
->c_rsrcfork
= NULL
;
392 cp
->c_rsrc_vp
= NULL
;
394 panic("hfs_vnop_reclaim: vp points to wrong cnode\n");
397 * On the last fork, remove the cnode from its hash chain.
400 /* If we can't remove it then the cnode must persist! */
401 if (hfs_chashremove(cp
) == 0)
404 * Remove any directory hints
406 if (vnode_isdir(vp
)) {
407 hfs_reldirhints(cp
, 0);
410 /* Release the file fork and related data */
412 /* Dump cached symlink data */
413 if (vnode_islnk(vp
) && (fp
->ff_symlinkptr
!= NULL
)) {
414 FREE(fp
->ff_symlinkptr
, M_TEMP
);
416 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
420 * If there was only one active fork then we can release the cnode.
423 hfs_chashwakeup(cp
, H_ALLOC
| H_TRANSIT
);
424 hfs_reclaim_cnode(cp
);
425 } else /* cnode in use */ {
429 vnode_clearfsnode(vp
);
434 extern int (**hfs_vnodeop_p
) (void *);
435 extern int (**hfs_specop_p
) (void *);
436 extern int (**hfs_fifoop_p
) (void *);
439 * hfs_getnewvnode - get new default vnode
441 * The vnode is returned with an iocount and the cnode locked
446 struct hfsmount
*hfsmp
,
448 struct componentname
*cnp
,
449 struct cat_desc
*descp
,
451 struct cat_attr
*attrp
,
452 struct cat_fork
*forkp
,
455 struct mount
*mp
= HFSTOVFS(hfsmp
);
456 struct vnode
*vp
= NULL
;
458 struct vnode
*tvp
= NULLVP
;
459 struct cnode
*cp
= NULL
;
460 struct filefork
*fp
= NULL
;
464 struct vnode_fsparam vfsp
;
467 if (attrp
->ca_fileid
== 0) {
473 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
478 vtype
= IFTOVT(attrp
->ca_mode
);
479 issystemfile
= (descp
->cd_flags
& CD_ISMETA
) && (vtype
== VREG
);
482 * Get a cnode (new or existing)
483 * skip getting the cnode lock if we are getting resource fork (wantrsrc == 2)
485 cp
= hfs_chash_getcnode(hfsmp
->hfs_raw_dev
, attrp
->ca_fileid
, vpp
, wantrsrc
, (wantrsrc
== 2));
487 /* Hardlinks may need an updated catalog descriptor */
488 if ((cp
->c_flag
& C_HARDLINK
) && descp
->cd_nameptr
&& descp
->cd_namelen
> 0) {
489 replace_desc(cp
, descp
);
491 /* Check if we found a matching vnode */
496 * If this is a new cnode then initialize it.
498 if (ISSET(cp
->c_hflag
, H_ALLOC
)) {
499 lck_rw_init(&cp
->c_truncatelock
, hfs_rwlock_group
, hfs_lock_attr
);
501 /* Make sure its still valid (ie exists on disk). */
502 if (!hfs_valid_cnode(hfsmp
, dvp
, (wantrsrc
? NULL
: cnp
), cp
->c_fileid
)) {
504 hfs_reclaim_cnode(cp
);
508 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
509 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
511 /* The name was inherited so clear descriptor state... */
512 descp
->cd_namelen
= 0;
513 descp
->cd_nameptr
= NULL
;
514 descp
->cd_flags
&= ~CD_HASBUF
;
517 if (IFTOVT(cp
->c_mode
) == VREG
&&
518 (descp
->cd_cnid
!= attrp
->ca_fileid
)) {
519 cp
->c_flag
|= C_HARDLINK
;
522 /* Take one dev reference for each non-directory cnode */
523 if (IFTOVT(cp
->c_mode
) != VDIR
) {
524 cp
->c_devvp
= hfsmp
->hfs_devvp
;
525 vnode_ref(cp
->c_devvp
);
528 for (i
= 0; i
< MAXQUOTAS
; i
++)
529 cp
->c_dquot
[i
] = NODQUOT
;
533 if (IFTOVT(cp
->c_mode
) == VDIR
) {
534 if (cp
->c_vp
!= NULL
)
535 panic("hfs_getnewvnode: orphaned vnode (data)");
538 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
539 panic("hfs_getnewvnode: bad ca_blocks (too small)");
541 * Allocate and initialize a file fork...
543 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
544 M_HFSFORK
, M_WAITOK
);
547 bcopy(forkp
, &fp
->ff_data
, sizeof(struct cat_fork
));
549 bzero(&fp
->ff_data
, sizeof(struct cat_fork
));
550 rl_init(&fp
->ff_invalidranges
);
551 fp
->ff_sysfileinfo
= 0;
554 if (cp
->c_rsrcfork
!= NULL
)
555 panic("hfs_getnewvnode: orphaned rsrc fork");
556 if (cp
->c_rsrc_vp
!= NULL
)
557 panic("hfs_getnewvnode: orphaned vnode (rsrc)");
559 cvpp
= &cp
->c_rsrc_vp
;
560 if ( (tvp
= cp
->c_vp
) != NULLVP
)
561 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
563 if (cp
->c_datafork
!= NULL
)
564 panic("hfs_getnewvnode: orphaned data fork");
565 if (cp
->c_vp
!= NULL
)
566 panic("hfs_getnewvnode: orphaned vnode (data)");
569 if ( (tvp
= cp
->c_rsrc_vp
) != NULLVP
)
570 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
575 * grab an iocount on the vnode we weren't
576 * interested in (i.e. we want the resource fork
577 * but the cnode already has the data fork)
578 * to prevent it from being
579 * recycled by us when we call vnode_create
580 * which will result in a deadlock when we
581 * try to take the cnode lock in hfs_vnop_fsync or
582 * hfs_vnop_reclaim... vnode_get can be called here
583 * because we already hold the cnode lock which will
584 * prevent the vnode from changing identity until
585 * we drop it.. vnode_get will not block waiting for
586 * a change of state... however, it will return an
587 * error if the current iocount == 0 and we've already
588 * started to terminate the vnode... we don't need/want to
589 * grab an iocount in the case since we can't cause
590 * the fileystem to be re-entered on this thread for this vp
592 * the matching vnode_put will happen in hfs_unlock
593 * after we've dropped the cnode lock
595 if ( vnode_get(tvp
) != 0)
596 cp
->c_flag
&= ~(C_NEED_RVNODE_PUT
| C_NEED_DVNODE_PUT
);
599 vfsp
.vnfs_vtype
= vtype
;
600 vfsp
.vnfs_str
= "hfs";
602 vfsp
.vnfs_fsnode
= cp
;
605 vfsp
.vnfs_vops
= hfs_fifoop_p
;
606 else if (vtype
== VBLK
|| vtype
== VCHR
)
607 vfsp
.vnfs_vops
= hfs_specop_p
;
609 vfsp
.vnfs_vops
= hfs_vnodeop_p
;
611 if (vtype
== VBLK
|| vtype
== VCHR
)
612 vfsp
.vnfs_rdev
= attrp
->ca_rdev
;
617 vfsp
.vnfs_filesize
= forkp
->cf_size
;
619 vfsp
.vnfs_filesize
= 0;
621 if (dvp
&& cnp
&& (cnp
->cn_flags
& MAKEENTRY
))
624 vfsp
.vnfs_flags
= VNFS_NOCACHE
;
626 /* Tag system files */
627 vfsp
.vnfs_marksystem
= issystemfile
;
629 /* Tag root directory */
630 if (descp
->cd_cnid
== kHFSRootFolderID
)
631 vfsp
.vnfs_markroot
= 1;
633 vfsp
.vnfs_markroot
= 0;
635 if ((retval
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, cvpp
))) {
637 if (fp
== cp
->c_datafork
)
638 cp
->c_datafork
= NULL
;
640 cp
->c_rsrcfork
= NULL
;
642 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
645 * If this is a newly created cnode or a vnode reclaim
646 * occurred during the attachment, then cleanup the cnode.
648 if ((cp
->c_vp
== NULL
) && (cp
->c_rsrc_vp
== NULL
)) {
650 hfs_reclaim_cnode(cp
);
652 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
660 vnode_settag(vp
, VT_HFS
);
661 if (cp
->c_flag
& C_HARDLINK
)
662 vnode_set_hard_link(vp
);
663 hfs_chashwakeup(cp
, H_ALLOC
| H_ATTACH
);
666 * Stop tracking an active hot file.
668 if (!vnode_isdir(vp
) && !vnode_issystem(vp
))
669 (void) hfs_removehotfile(vp
);
677 hfs_reclaim_cnode(struct cnode
*cp
)
682 for (i
= 0; i
< MAXQUOTAS
; i
++) {
683 if (cp
->c_dquot
[i
] != NODQUOT
) {
684 dqreclaim(cp
->c_dquot
[i
]);
685 cp
->c_dquot
[i
] = NODQUOT
;
691 struct vnode
*tmp_vp
= cp
->c_devvp
;
698 * If the descriptor has a name then release it
700 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
703 nameptr
= cp
->c_desc
.cd_nameptr
;
704 cp
->c_desc
.cd_nameptr
= 0;
705 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
706 cp
->c_desc
.cd_namelen
= 0;
707 vfs_removename(nameptr
);
710 lck_rw_destroy(&cp
->c_rwlock
, hfs_rwlock_group
);
711 lck_rw_destroy(&cp
->c_truncatelock
, hfs_rwlock_group
);
712 bzero(cp
, sizeof(struct cnode
));
713 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
718 hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
)
720 struct cat_attr attr
;
721 struct cat_desc cndesc
;
725 /* System files are always valid */
726 if (cnid
< kHFSFirstUserCatalogNodeID
)
729 /* XXX optimization: check write count in dvp */
731 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
734 bzero(&cndesc
, sizeof(cndesc
));
735 cndesc
.cd_nameptr
= cnp
->cn_nameptr
;
736 cndesc
.cd_namelen
= cnp
->cn_namelen
;
737 cndesc
.cd_parentcnid
= VTOC(dvp
)->c_cnid
;
738 cndesc
.cd_hint
= VTOC(dvp
)->c_childhint
;
740 if ((cat_lookup(hfsmp
, &cndesc
, 0, NULL
, &attr
, NULL
, NULL
) == 0) &&
741 (cnid
== attr
.ca_fileid
)) {
745 if (cat_idlookup(hfsmp
, cnid
, NULL
, NULL
, NULL
) == 0) {
749 hfs_systemfile_unlock(hfsmp
, lockflags
);
755 * Touch cnode times based on c_touch_xxx flags
757 * cnode must be locked exclusive
759 * This will also update the volume modify time
763 hfs_touchtimes(struct hfsmount
*hfsmp
, struct cnode
* cp
)
765 /* HFS Standard doesn't support access times */
766 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
767 cp
->c_touch_acctime
= FALSE
;
770 if (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
) {
776 if (cp
->c_touch_acctime
) {
777 cp
->c_atime
= tv
.tv_sec
;
779 * When the access time is the only thing changing
780 * then make sure its sufficiently newer before
781 * committing it to disk.
783 if ((((u_int32_t
)cp
->c_atime
- (u_int32_t
)(cp
)->c_attr
.ca_atimeondisk
) >
784 ATIME_ONDISK_ACCURACY
)) {
785 cp
->c_flag
|= C_MODIFIED
;
787 cp
->c_touch_acctime
= FALSE
;
789 if (cp
->c_touch_modtime
) {
790 cp
->c_mtime
= tv
.tv_sec
;
791 cp
->c_touch_modtime
= FALSE
;
792 cp
->c_flag
|= C_MODIFIED
;
796 * HFS dates that WE set must be adjusted for DST
798 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
803 if (cp
->c_touch_chgtime
) {
804 cp
->c_ctime
= tv
.tv_sec
;
805 cp
->c_touch_chgtime
= FALSE
;
806 cp
->c_flag
|= C_MODIFIED
;
810 /* Touch the volume modtime if needed */
812 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
813 HFSTOVCB(hfsmp
)->vcbLsMod
= tv
.tv_sec
;
823 hfs_lock(struct cnode
*cp
, enum hfslocktype locktype
)
825 void * thread
= current_thread();
827 /* System files need to keep track of owner */
828 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
829 (cp
->c_fileid
> kHFSRootFolderID
) &&
830 (locktype
!= HFS_SHARED_LOCK
)) {
833 * The extents and bitmap file locks support
834 * recursion and are always taken exclusive.
836 if (cp
->c_fileid
== kHFSExtentsFileID
||
837 cp
->c_fileid
== kHFSAllocationFileID
) {
838 if (cp
->c_lockowner
== thread
) {
839 cp
->c_syslockcount
++;
841 lck_rw_lock_exclusive(&cp
->c_rwlock
);
842 cp
->c_lockowner
= thread
;
843 cp
->c_syslockcount
= 1;
846 lck_rw_lock_exclusive(&cp
->c_rwlock
);
847 cp
->c_lockowner
= thread
;
849 } else if (locktype
== HFS_SHARED_LOCK
) {
850 lck_rw_lock_shared(&cp
->c_rwlock
);
851 cp
->c_lockowner
= HFS_SHARED_OWNER
;
853 lck_rw_lock_exclusive(&cp
->c_rwlock
);
854 cp
->c_lockowner
= thread
;
857 * Skip cnodes that no longer exist (were deleted).
859 if ((locktype
!= HFS_FORCE_LOCK
) &&
860 ((cp
->c_desc
.cd_flags
& CD_ISMETA
) == 0) &&
861 (cp
->c_flag
& C_NOEXISTS
)) {
869 * Lock a pair of cnodes.
873 hfs_lockpair(struct cnode
*cp1
, struct cnode
*cp2
, enum hfslocktype locktype
)
875 struct cnode
*first
, *last
;
879 * If cnodes match then just lock one.
882 return hfs_lock(cp1
, locktype
);
886 * Lock in cnode parent-child order (if there is a relationship);
887 * otherwise lock in cnode address order.
889 if ((IFTOVT(cp1
->c_mode
) == VDIR
) && (cp1
->c_fileid
== cp2
->c_parentcnid
)) {
892 } else if (cp1
< cp2
) {
900 if ( (error
= hfs_lock(first
, locktype
))) {
903 if ( (error
= hfs_lock(last
, locktype
))) {
911 * Check ordering of two cnodes. Return true if they are are in-order.
914 hfs_isordered(struct cnode
*cp1
, struct cnode
*cp2
)
918 if (cp1
== NULL
|| cp2
== (struct cnode
*)0xffffffff)
920 if (cp2
== NULL
|| cp1
== (struct cnode
*)0xffffffff)
922 if (cp1
->c_fileid
== cp2
->c_parentcnid
)
923 return (1); /* cp1 is the parent and should go first */
924 if (cp2
->c_fileid
== cp1
->c_parentcnid
)
925 return (0); /* cp1 is the child and should go last */
927 return (cp1
< cp2
); /* fall-back is to use address order */
931 * Acquire 4 cnode locks.
932 * - locked in cnode parent-child order (if there is a relationship)
933 * otherwise lock in cnode address order (lesser address first).
934 * - all or none of the locks are taken
935 * - only one lock taken per cnode (dup cnodes are skipped)
936 * - some of the cnode pointers may be null
940 hfs_lockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
,
941 struct cnode
*cp4
, enum hfslocktype locktype
)
945 struct cnode
* list
[4];
950 if (hfs_isordered(cp1
, cp2
)) {
951 a
[0] = cp1
; a
[1] = cp2
;
953 a
[0] = cp2
; a
[1] = cp1
;
955 if (hfs_isordered(cp3
, cp4
)) {
956 b
[0] = cp3
; b
[1] = cp4
;
958 b
[0] = cp4
; b
[1] = cp3
;
960 a
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
961 b
[2] = (struct cnode
*)0xffffffff; /* sentinel value */
964 * Build the lock list, skipping over duplicates
966 for (i
= 0, j
= 0, k
= 0; (i
< 2 || j
< 2); ) {
967 tmp
= hfs_isordered(a
[i
], b
[j
]) ? a
[i
++] : b
[j
++];
968 if (k
== 0 || tmp
!= list
[k
-1])
973 * Now we can lock using list[0 - k].
974 * Skip over NULL entries.
976 for (i
= 0; i
< k
; ++i
) {
978 if ((error
= hfs_lock(list
[i
], locktype
))) {
979 /* Drop any locks we acquired. */
996 hfs_unlock(struct cnode
*cp
)
998 vnode_t rvp
= NULLVP
;
999 vnode_t dvp
= NULLVP
;
1001 /* System files need to keep track of owner */
1002 if ((cp
->c_fileid
< kHFSFirstUserCatalogNodeID
) &&
1003 (cp
->c_fileid
> kHFSRootFolderID
) &&
1004 (cp
->c_datafork
!= NULL
)) {
1006 * The extents and bitmap file locks support
1007 * recursion and are always taken exclusive.
1009 if (cp
->c_fileid
== kHFSExtentsFileID
||
1010 cp
->c_fileid
== kHFSAllocationFileID
) {
1011 if (--cp
->c_syslockcount
> 0) {
1016 if (cp
->c_flag
& C_NEED_DVNODE_PUT
)
1019 if (cp
->c_flag
& C_NEED_RVNODE_PUT
)
1020 rvp
= cp
->c_rsrc_vp
;
1022 cp
->c_flag
&= ~(C_NEED_DVNODE_PUT
| C_NEED_RVNODE_PUT
);
1024 cp
-> c_lockowner
= NULL
;
1025 lck_rw_done(&cp
->c_rwlock
);
1034 * Unlock a pair of cnodes.
1038 hfs_unlockpair(struct cnode
*cp1
, struct cnode
*cp2
)
1046 * Unlock a group of cnodes.
1050 hfs_unlockfour(struct cnode
*cp1
, struct cnode
*cp2
, struct cnode
*cp3
, struct cnode
*cp4
)
1052 struct cnode
* list
[4];
1060 for (i
= 0; i
< k
; ++i
) {
1069 for (i
= 0; i
< k
; ++i
) {
1078 for (i
= 0; i
< k
; ++i
) {
1088 * Protect a cnode against a truncation.
1090 * Used mainly by read/write since they don't hold the
1091 * cnode lock across calls to the cluster layer.
1093 * The process doing a truncation must take the lock
1094 * exclusive. The read/write processes can take it
1099 hfs_lock_truncate(struct cnode
*cp
, int exclusive
)
1101 if (cp
->c_lockowner
== current_thread())
1102 panic("hfs_lock_truncate: cnode 0x%08x locked!", cp
);
1105 lck_rw_lock_exclusive(&cp
->c_truncatelock
);
1107 lck_rw_lock_shared(&cp
->c_truncatelock
);
1112 hfs_unlock_truncate(struct cnode
*cp
)
1114 lck_rw_done(&cp
->c_truncatelock
);