2 * Copyright (c) 2002 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>
30 #include <sys/quota.h>
32 #include <miscfs/specfs/specdev.h>
33 #include <miscfs/fifofs/fifo.h>
36 #include <hfs/hfs_catalog.h>
37 #include <hfs/hfs_cnode.h>
38 #include <hfs/hfs_quota.h>
43 extern void hfs_relnamehints(struct cnode
*dcp
);
47 * Last reference to an cnode. If necessary, write or delete it.
52 struct vop_inactive_args
/* {
56 struct vnode
*vp
= ap
->a_vp
;
57 struct cnode
*cp
= VTOC(vp
);
58 struct hfsmount
*hfsmp
= VTOHFS(vp
);
59 struct proc
*p
= ap
->a_p
;
65 int started_tr
= 0, grabbed_lock
= 0;
67 if (prtactive
&& vp
->v_usecount
!= 0)
68 vprint("hfs_inactive: pushing active", vp
);
71 * Ignore nodes related to stale file handles.
76 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
84 /* If needed, get rid of any fork's data for a deleted file */
85 if ((cp
->c_flag
& C_DELETED
) &&
87 (VTOF(vp
)->ff_blocks
!= 0)) {
88 error
= VOP_TRUNCATE(vp
, (off_t
)0, IO_NDELAY
, NOCRED
, p
);
90 // have to do this to prevent the lost ubc_info panic
91 SET(cp
->c_flag
, C_TRANSIT
);
97 * Check for a postponed deletion.
98 * (only delete cnode when the last fork goes inactive)
100 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
102 * Mark cnode in transit so that one can get this
103 * cnode from cnode hash.
105 SET(cp
->c_flag
, C_TRANSIT
);
106 cp
->c_flag
&= ~C_DELETED
;
110 hfs_global_shared_lock_acquire(hfsmp
);
113 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
120 /* Lock catalog b-tree */
121 error
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_EXCLUSIVE
, p
);
124 if (cp
->c_blocks
> 0)
125 printf("hfs_inactive: attempting to delete a non-empty file!");
128 * The descriptor name may be zero,
129 * in which case the fileid is used.
131 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
133 if (error
&& truncated
&& (error
!= ENXIO
))
134 printf("hfs_inactive: couldn't delete a truncated file!");
136 /* Update HFS Private Data dir */
138 hfsmp
->hfs_privdir_attr
.ca_entries
--;
139 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
140 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
143 /* Unlock catalog b-tree */
144 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
148 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
152 cp
->c_flag
|= C_NOEXISTS
| C_CHANGE
| C_UPDATE
;
155 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
158 /* Push any defered access times to disk */
159 if (cp
->c_flag
& C_ATIMEMOD
) {
160 cp
->c_flag
&= ~C_ATIMEMOD
;
161 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
162 cp
->c_flag
|= C_MODIFIED
;
165 if (cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) {
167 VOP_UPDATE(vp
, &tv
, &tv
, 0);
170 // XXXdbg - have to do this because a goto could have come here
172 journal_end_transaction(hfsmp
->jnl
);
176 hfs_global_shared_lock_release(hfsmp
);
179 VOP_UNLOCK(vp
, 0, p
);
181 * If we are done with the vnode, reclaim it
182 * so that it can be reused immediately.
184 if (cp
->c_mode
== 0 || recycle
)
185 vrecycle(vp
, (struct slock
*)0, p
);
192 * Reclaim a cnode so that it can be used for other purposes.
197 struct vop_reclaim_args
/* {
201 struct vnode
*vp
= ap
->a_vp
;
202 struct cnode
*cp
= VTOC(vp
);
203 struct vnode
*devvp
= NULL
;
204 struct filefork
*fp
= NULL
;
205 struct filefork
*altfp
= NULL
;
208 if (prtactive
&& vp
->v_usecount
!= 0)
209 vprint("hfs_reclaim(): pushing active", vp
);
211 devvp
= cp
->c_devvp
; /* For later releasing */
214 * Find file fork for this vnode (if any)
215 * Also check if another fork is active
217 if ((fp
= cp
->c_datafork
) && (cp
->c_vp
== vp
)) {
218 cp
->c_datafork
= NULL
;
220 altfp
= cp
->c_rsrcfork
;
221 } else if ((fp
= cp
->c_rsrcfork
) && (cp
->c_rsrc_vp
== vp
)) {
222 cp
->c_rsrcfork
= NULL
;
223 cp
->c_rsrc_vp
= NULL
;
224 altfp
= cp
->c_datafork
;
232 * On the last fork, remove the cnode from its hash chain.
237 /* Release the file fork and related data (can block) */
240 /* Dump cached symlink data */
241 if ((vp
->v_type
== VLNK
) && (fp
->ff_symlinkptr
!= NULL
)) {
242 FREE(fp
->ff_symlinkptr
, M_TEMP
);
243 fp
->ff_symlinkptr
= NULL
;
245 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
250 * Purge old data structures associated with the cnode.
253 if (devvp
&& altfp
== NULL
) {
261 * If there was only one active fork then we can release the cnode.
265 for (i
= 0; i
< MAXQUOTAS
; i
++) {
266 if (cp
->c_dquot
[i
] != NODQUOT
) {
267 dqreclaim(vp
, cp
->c_dquot
[i
]);
268 cp
->c_dquot
[i
] = NODQUOT
;
273 * Free any left over directory indices
275 if (vp
->v_type
== VDIR
)
276 hfs_relnamehints(cp
);
279 * If the descriptor has a name then release it
281 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
284 nameptr
= cp
->c_desc
.cd_nameptr
;
285 cp
->c_desc
.cd_nameptr
= 0;
286 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
287 cp
->c_desc
.cd_namelen
= 0;
288 FREE(nameptr
, M_TEMP
);
290 CLR(cp
->c_flag
, (C_ALLOC
| C_TRANSIT
));
291 if (ISSET(cp
->c_flag
, C_WALLOC
) || ISSET(cp
->c_flag
, C_WTRANSIT
))
293 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
304 * called by hfs_lookup and hfs_vget (descp == NULL)
306 * returns a locked vnode for cnode for given cnid/fileid
310 hfs_getcnode(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*descp
, int wantrsrc
,
311 struct cat_attr
*attrp
, struct cat_fork
*forkp
, struct vnode
**vpp
)
313 dev_t dev
= hfsmp
->hfs_raw_dev
;
314 struct vnode
*vp
= NULL
;
315 struct vnode
*rvp
= NULL
;
316 struct vnode
*new_vp
= NULL
;
317 struct cnode
*cp
= NULL
;
318 struct proc
*p
= current_proc();
321 /* Check if unmount in progress */
322 if (HFSTOVFS(hfsmp
)->mnt_kern_flag
& MNTK_UNMOUNT
) {
328 * Check the hash for an active cnode
330 cp
= hfs_chashget(dev
, cnid
, wantrsrc
, &vp
, &rvp
);
332 /* hide open files that have been deleted */
333 if ((hfsmp
->hfs_private_metadata_dir
!= 0)
334 && (cp
->c_parentcnid
== hfsmp
->hfs_private_metadata_dir
)
335 && (cp
->c_nlink
== 0)) {
340 /* Hide private journal files */
342 (cp
->c_parentcnid
== kRootDirID
) &&
343 ((cp
->c_cnid
== hfsmp
->hfs_jnlfileid
) ||
344 (cp
->c_cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
349 if (wantrsrc
&& rvp
!= NULL
) {
354 if (!wantrsrc
&& vp
!= NULL
) {
355 /* Hardlinks need an updated catalog descriptor */
356 if (descp
&& cp
->c_flag
& C_HARDLINK
) {
357 replace_desc(cp
, descp
);
359 /* We have a vnode so we're done. */
365 * There was no active vnode so get a new one.
366 * Use the existing cnode (if any).
370 * hfs_lookup case, use descp, attrp and forkp
372 retval
= hfs_getnewvnode(hfsmp
, cp
, descp
, wantrsrc
, attrp
,
375 struct cat_desc cndesc
= {0};
376 struct cat_attr cnattr
= {0};
377 struct cat_fork cnfork
= {0};
380 * hfs_vget case, need to lookup entry (by file id)
382 if (cnid
== kRootParID
) {
383 static char hfs_rootname
[] = "/";
385 cndesc
.cd_nameptr
= &hfs_rootname
[0];
386 cndesc
.cd_namelen
= 1;
387 cndesc
.cd_parentcnid
= kRootParID
;
388 cndesc
.cd_cnid
= kRootParID
;
389 cndesc
.cd_flags
= CD_ISDIR
;
391 cnattr
.ca_fileid
= kRootParID
;
393 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
395 /* Lock catalog b-tree */
396 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_SHARED
, p
);
400 retval
= cat_idlookup(hfsmp
, cnid
, &cndesc
, &cnattr
, &cnfork
);
402 /* Unlock catalog b-tree */
403 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
407 /* Hide open files that have been deleted */
408 if ((hfsmp
->hfs_private_metadata_dir
!= 0) &&
409 (cndesc
.cd_parentcnid
== hfsmp
->hfs_private_metadata_dir
)) {
410 cat_releasedesc(&cndesc
);
416 retval
= hfs_getnewvnode(hfsmp
, cp
, &cndesc
, 0, &cnattr
, &cnfork
, &new_vp
);
418 /* Hardlinks may need an updated catalog descriptor */
421 && (VTOC(new_vp
)->c_flag
& C_HARDLINK
)
423 && cndesc
.cd_namelen
> 0) {
424 replace_desc(VTOC(new_vp
), &cndesc
);
426 cat_releasedesc(&cndesc
);
429 /* Release reference taken on opposite vnode (if any). */
441 /* The cnode's vnode should be in vp. */
443 panic("hfs_getcnode: missing vp!");
445 UBCINFOCHECK("hfs_getcnode", vp
);
452 * hfs_getnewvnode - get new default vnode
454 * the vnode is returned locked
456 extern int (**hfs_vnodeop_p
) (void *);
457 extern int (**hfs_specop_p
) (void *);
458 extern int (**hfs_fifoop_p
) (void *);
462 hfs_getnewvnode(struct hfsmount
*hfsmp
, struct cnode
*cp
,
463 struct cat_desc
*descp
, int wantrsrc
,
464 struct cat_attr
*attrp
, struct cat_fork
*forkp
,
467 struct mount
*mp
= HFSTOVFS(hfsmp
);
468 struct vnode
*vp
= NULL
;
469 struct vnode
*rvp
= NULL
;
470 struct vnode
*new_vp
= NULL
;
471 struct cnode
*cp2
= NULL
;
472 struct filefork
*fp
= NULL
;
477 struct proc
*p
= current_proc();
479 /* Bail when unmount is in progress */
480 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
486 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
491 dev
= hfsmp
->hfs_raw_dev
;
493 /* If no cnode was passed in then create one */
495 MALLOC_ZONE(cp2
, struct cnode
*, sizeof(struct cnode
),
496 M_HFSNODE
, M_WAITOK
);
497 bzero(cp2
, sizeof(struct cnode
));
499 SET(cp2
->c_flag
, C_ALLOC
);
500 cp2
->c_cnid
= descp
->cd_cnid
;
501 cp2
->c_fileid
= attrp
->ca_fileid
;
503 lockinit(&cp2
->c_lock
, PINOD
, "cnode", 0, 0);
504 (void) lockmgr(&cp2
->c_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
506 * There were several blocking points since we first
507 * checked the hash. Now that we're through blocking,
508 * check the hash again in case we're racing for the
511 cp
= hfs_chashget(dev
, attrp
->ca_fileid
, wantrsrc
, &vp
, &rvp
);
513 /* We lost the race - use the winner's cnode */
514 FREE_ZONE(cp2
, sizeof(struct cnode
), M_HFSNODE
);
516 if (wantrsrc
&& rvp
!= NULL
) {
520 if (!wantrsrc
&& vp
!= NULL
) {
524 } else /* allocated */ {
530 /* Allocate a new vnode. If unsuccesful, leave after freeing memory */
531 if ((retval
= getnewvnode(VT_HFS
, mp
, hfs_vnodeop_p
, &new_vp
))) {
534 if (ISSET(cp
->c_flag
, C_WALLOC
)) {
535 CLR(cp
->c_flag
, C_WALLOC
);
538 FREE_ZONE(cp2
, sizeof(struct cnode
), M_HFSNODE
);
549 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
550 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
553 if (wantrsrc
&& S_ISREG(cp
->c_mode
))
554 cp
->c_rsrc_vp
= new_vp
;
558 /* Release reference taken on opposite vnode (if any). */
565 vp
->v_ubcinfo
= UBC_NOINFO
;
568 * If this is a new cnode then initialize it using descp and attrp...
571 /* The name was inherited so clear descriptor state... */
572 descp
->cd_namelen
= 0;
573 descp
->cd_nameptr
= NULL
;
574 descp
->cd_flags
&= ~CD_HASBUF
;
577 if (IFTOVT(cp
->c_mode
) == VREG
&&
578 (descp
->cd_cnid
!= attrp
->ca_fileid
)) {
579 cp
->c_flag
|= C_HARDLINK
;
582 /* Take one dev reference for each non-directory cnode */
583 if (IFTOVT(cp
->c_mode
) != VDIR
) {
584 cp
->c_devvp
= hfsmp
->hfs_devvp
;
588 for (i
= 0; i
< MAXQUOTAS
; i
++)
589 cp
->c_dquot
[i
] = NODQUOT
;
593 if (IFTOVT(cp
->c_mode
) != VDIR
) {
594 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
595 panic("hfs_getnewvnode: bad ca_blocks (too small)");
597 * Allocate and initialize a file fork...
599 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
600 M_HFSFORK
, M_WAITOK
);
601 bzero(fp
, sizeof(struct filefork
));
604 bcopy(forkp
, &fp
->ff_data
, sizeof(HFSPlusForkData
));
605 if (fp
->ff_clumpsize
== 0)
606 fp
->ff_clumpsize
= HFSTOVCB(hfsmp
)->vcbClpSiz
;
607 rl_init(&fp
->ff_invalidranges
);
609 if (cp
->c_rsrcfork
!= NULL
)
610 panic("stale rsrc fork");
613 if (cp
->c_datafork
!= NULL
)
614 panic("stale data fork");
620 * Finish vnode initialization.
621 * Setting the v_type 'stamps' the vnode as 'complete',
622 * so should be done almost last.
624 * At this point the vnode should be locked and fully
625 * allocated. And ready to be used or accessed. (though
626 * having it locked prevents most of this, it can still
627 * be accessed through lists and hashes).
629 vp
->v_type
= IFTOVT(cp
->c_mode
);
631 /* Tag system files */
632 if ((descp
->cd_cnid
< kHFSFirstUserCatalogNodeID
) && (vp
->v_type
== VREG
))
633 vp
->v_flag
|= VSYSTEM
;
634 /* Tag root directory */
635 if (cp
->c_cnid
== kRootDirID
)
638 if ((vp
->v_type
== VREG
) && !(vp
->v_flag
& VSYSTEM
)
639 && (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))) {
642 vp
->v_ubcinfo
= UBC_NOINFO
;
645 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
648 vp
->v_op
= hfs_specop_p
;
649 if ((nvp
= checkalias(vp
, cp
->c_rdev
, mp
))) {
651 * Discard unneeded vnode, but save its cnode.
652 * Note that the lock is carried over in the
653 * cnode to the replacement vnode.
655 nvp
->v_data
= vp
->v_data
;
657 vp
->v_op
= spec_vnodeop_p
;
661 * Reinitialize aliased cnode.
662 * Assume its not a resource fork.
667 } else if (vp
->v_type
== VFIFO
) {
669 vp
->v_op
= hfs_fifoop_p
;
673 /* Vnode is now initialized - see if anyone was waiting for it. */
674 CLR(cp
->c_flag
, C_ALLOC
);
675 if (ISSET(cp
->c_flag
, C_WALLOC
)) {
676 CLR(cp
->c_flag
, C_WALLOC
);