2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <sys/vnode.h>
29 #include <sys/mount.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
33 #include <sys/quota.h>
35 #include <miscfs/specfs/specdev.h>
36 #include <miscfs/fifofs/fifo.h>
39 #include <hfs/hfs_catalog.h>
40 #include <hfs/hfs_cnode.h>
41 #include <hfs/hfs_quota.h>
46 extern void hfs_relnamehints(struct cnode
*dcp
);
50 * Last reference to an cnode. If necessary, write or delete it.
55 struct vop_inactive_args
/* {
59 struct vnode
*vp
= ap
->a_vp
;
60 struct cnode
*cp
= VTOC(vp
);
61 struct hfsmount
*hfsmp
= VTOHFS(vp
);
62 struct proc
*p
= ap
->a_p
;
68 int started_tr
= 0, grabbed_lock
= 0;
70 if (prtactive
&& vp
->v_usecount
!= 0)
71 vprint("hfs_inactive: pushing active", vp
);
74 * Ignore nodes related to stale file handles.
79 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
87 /* If needed, get rid of any fork's data for a deleted file */
88 if ((cp
->c_flag
& C_DELETED
) &&
90 (VTOF(vp
)->ff_blocks
!= 0)) {
91 error
= VOP_TRUNCATE(vp
, (off_t
)0, IO_NDELAY
, NOCRED
, p
);
93 // have to do this to prevent the lost ubc_info panic
94 SET(cp
->c_flag
, C_TRANSIT
);
100 * Check for a postponed deletion.
101 * (only delete cnode when the last fork goes inactive)
103 if ((cp
->c_flag
& C_DELETED
) && (forkcount
<= 1)) {
105 * Mark cnode in transit so that one can get this
106 * cnode from cnode hash.
108 SET(cp
->c_flag
, C_TRANSIT
);
109 cp
->c_flag
&= ~C_DELETED
;
113 hfs_global_shared_lock_acquire(hfsmp
);
116 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
123 /* Lock catalog b-tree */
124 error
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_EXCLUSIVE
, p
);
127 if (cp
->c_blocks
> 0)
128 printf("hfs_inactive: attempting to delete a non-empty file!");
131 * The descriptor name may be zero,
132 * in which case the fileid is used.
134 error
= cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
136 if (error
&& truncated
&& (error
!= ENXIO
))
137 printf("hfs_inactive: couldn't delete a truncated file!");
139 /* Update HFS Private Data dir */
141 hfsmp
->hfs_privdir_attr
.ca_entries
--;
142 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
143 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
146 /* Unlock catalog b-tree */
147 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
151 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
155 cp
->c_flag
|= C_NOEXISTS
| C_CHANGE
| C_UPDATE
;
158 hfs_volupdate(hfsmp
, VOL_RMFILE
, 0);
161 /* Push any defered access times to disk */
162 if (cp
->c_flag
& C_ATIMEMOD
) {
163 cp
->c_flag
&= ~C_ATIMEMOD
;
164 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
165 cp
->c_flag
|= C_MODIFIED
;
168 if (cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) {
170 VOP_UPDATE(vp
, &tv
, &tv
, 0);
173 // XXXdbg - have to do this because a goto could have come here
175 journal_end_transaction(hfsmp
->jnl
);
179 hfs_global_shared_lock_release(hfsmp
);
182 VOP_UNLOCK(vp
, 0, p
);
184 * If we are done with the vnode, reclaim it
185 * so that it can be reused immediately.
187 if (cp
->c_mode
== 0 || recycle
)
188 vrecycle(vp
, (struct slock
*)0, p
);
195 * Reclaim a cnode so that it can be used for other purposes.
200 struct vop_reclaim_args
/* {
204 struct vnode
*vp
= ap
->a_vp
;
205 struct cnode
*cp
= VTOC(vp
);
206 struct vnode
*devvp
= NULL
;
207 struct filefork
*fp
= NULL
;
208 struct filefork
*altfp
= NULL
;
211 if (prtactive
&& vp
->v_usecount
!= 0)
212 vprint("hfs_reclaim(): pushing active", vp
);
214 devvp
= cp
->c_devvp
; /* For later releasing */
217 * Find file fork for this vnode (if any)
218 * Also check if another fork is active
220 if ((fp
= cp
->c_datafork
) && (cp
->c_vp
== vp
)) {
221 cp
->c_datafork
= NULL
;
223 altfp
= cp
->c_rsrcfork
;
224 } else if ((fp
= cp
->c_rsrcfork
) && (cp
->c_rsrc_vp
== vp
)) {
225 cp
->c_rsrcfork
= NULL
;
226 cp
->c_rsrc_vp
= NULL
;
227 altfp
= cp
->c_datafork
;
235 * On the last fork, remove the cnode from its hash chain.
240 /* Release the file fork and related data (can block) */
243 /* Dump cached symlink data */
244 if ((vp
->v_type
== VLNK
) && (fp
->ff_symlinkptr
!= NULL
)) {
245 FREE(fp
->ff_symlinkptr
, M_TEMP
);
246 fp
->ff_symlinkptr
= NULL
;
248 FREE_ZONE(fp
, sizeof(struct filefork
), M_HFSFORK
);
253 * Purge old data structures associated with the cnode.
256 if (devvp
&& altfp
== NULL
) {
264 * If there was only one active fork then we can release the cnode.
268 for (i
= 0; i
< MAXQUOTAS
; i
++) {
269 if (cp
->c_dquot
[i
] != NODQUOT
) {
270 dqreclaim(vp
, cp
->c_dquot
[i
]);
271 cp
->c_dquot
[i
] = NODQUOT
;
276 * Free any left over directory indices
278 if (vp
->v_type
== VDIR
)
279 hfs_relnamehints(cp
);
282 * If the descriptor has a name then release it
284 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
287 nameptr
= cp
->c_desc
.cd_nameptr
;
288 cp
->c_desc
.cd_nameptr
= 0;
289 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
290 cp
->c_desc
.cd_namelen
= 0;
291 FREE(nameptr
, M_TEMP
);
293 CLR(cp
->c_flag
, (C_ALLOC
| C_TRANSIT
));
294 if (ISSET(cp
->c_flag
, C_WALLOC
) || ISSET(cp
->c_flag
, C_WTRANSIT
))
296 FREE_ZONE(cp
, sizeof(struct cnode
), M_HFSNODE
);
307 * called by hfs_lookup and hfs_vget (descp == NULL)
309 * returns a locked vnode for cnode for given cnid/fileid
313 hfs_getcnode(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*descp
, int wantrsrc
,
314 struct cat_attr
*attrp
, struct cat_fork
*forkp
, struct vnode
**vpp
)
316 dev_t dev
= hfsmp
->hfs_raw_dev
;
317 struct vnode
*vp
= NULL
;
318 struct vnode
*rvp
= NULL
;
319 struct vnode
*new_vp
= NULL
;
320 struct cnode
*cp
= NULL
;
321 struct proc
*p
= current_proc();
324 /* Check if unmount in progress */
325 if (HFSTOVFS(hfsmp
)->mnt_kern_flag
& MNTK_UNMOUNT
) {
331 * Check the hash for an active cnode
333 cp
= hfs_chashget(dev
, cnid
, wantrsrc
, &vp
, &rvp
);
335 /* hide open files that have been deleted */
336 if ((hfsmp
->hfs_private_metadata_dir
!= 0)
337 && (cp
->c_parentcnid
== hfsmp
->hfs_private_metadata_dir
)
338 && (cp
->c_nlink
== 0)) {
343 /* Hide private journal files */
345 (cp
->c_parentcnid
== kRootDirID
) &&
346 ((cp
->c_cnid
== hfsmp
->hfs_jnlfileid
) ||
347 (cp
->c_cnid
== hfsmp
->hfs_jnlinfoblkid
))) {
352 if (wantrsrc
&& rvp
!= NULL
) {
357 if (!wantrsrc
&& vp
!= NULL
) {
358 /* Hardlinks need an updated catalog descriptor */
359 if (descp
&& cp
->c_flag
& C_HARDLINK
) {
360 replace_desc(cp
, descp
);
362 /* We have a vnode so we're done. */
368 * There was no active vnode so get a new one.
369 * Use the existing cnode (if any).
373 * hfs_lookup case, use descp, attrp and forkp
375 retval
= hfs_getnewvnode(hfsmp
, cp
, descp
, wantrsrc
, attrp
,
378 struct cat_desc cndesc
= {0};
379 struct cat_attr cnattr
= {0};
380 struct cat_fork cnfork
= {0};
383 * hfs_vget case, need to lookup entry (by file id)
385 if (cnid
== kRootParID
) {
386 static char hfs_rootname
[] = "/";
388 cndesc
.cd_nameptr
= &hfs_rootname
[0];
389 cndesc
.cd_namelen
= 1;
390 cndesc
.cd_parentcnid
= kRootParID
;
391 cndesc
.cd_cnid
= kRootParID
;
392 cndesc
.cd_flags
= CD_ISDIR
;
394 cnattr
.ca_fileid
= kRootParID
;
396 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
398 /* Lock catalog b-tree */
399 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_SHARED
, p
);
403 retval
= cat_idlookup(hfsmp
, cnid
, &cndesc
, &cnattr
, &cnfork
);
405 /* Unlock catalog b-tree */
406 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
410 /* Hide open files that have been deleted */
411 if ((hfsmp
->hfs_private_metadata_dir
!= 0) &&
412 (cndesc
.cd_parentcnid
== hfsmp
->hfs_private_metadata_dir
)) {
413 cat_releasedesc(&cndesc
);
419 retval
= hfs_getnewvnode(hfsmp
, cp
, &cndesc
, 0, &cnattr
, &cnfork
, &new_vp
);
421 /* Hardlinks may need an updated catalog descriptor */
424 && (VTOC(new_vp
)->c_flag
& C_HARDLINK
)
426 && cndesc
.cd_namelen
> 0) {
427 replace_desc(VTOC(new_vp
), &cndesc
);
429 cat_releasedesc(&cndesc
);
432 /* Release reference taken on opposite vnode (if any). */
444 /* The cnode's vnode should be in vp. */
446 panic("hfs_getcnode: missing vp!");
448 UBCINFOCHECK("hfs_getcnode", vp
);
455 * hfs_getnewvnode - get new default vnode
457 * the vnode is returned locked
459 extern int (**hfs_vnodeop_p
) (void *);
460 extern int (**hfs_specop_p
) (void *);
461 extern int (**hfs_fifoop_p
) (void *);
465 hfs_getnewvnode(struct hfsmount
*hfsmp
, struct cnode
*cp
,
466 struct cat_desc
*descp
, int wantrsrc
,
467 struct cat_attr
*attrp
, struct cat_fork
*forkp
,
470 struct mount
*mp
= HFSTOVFS(hfsmp
);
471 struct vnode
*vp
= NULL
;
472 struct vnode
*rvp
= NULL
;
473 struct vnode
*new_vp
= NULL
;
474 struct cnode
*cp2
= NULL
;
475 struct filefork
*fp
= NULL
;
480 struct proc
*p
= current_proc();
482 /* Bail when unmount is in progress */
483 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
489 if (IFTOVT(attrp
->ca_mode
) == VFIFO
) {
494 dev
= hfsmp
->hfs_raw_dev
;
496 /* If no cnode was passed in then create one */
498 MALLOC_ZONE(cp2
, struct cnode
*, sizeof(struct cnode
),
499 M_HFSNODE
, M_WAITOK
);
500 bzero(cp2
, sizeof(struct cnode
));
502 SET(cp2
->c_flag
, C_ALLOC
);
503 cp2
->c_cnid
= descp
->cd_cnid
;
504 cp2
->c_fileid
= attrp
->ca_fileid
;
506 lockinit(&cp2
->c_lock
, PINOD
, "cnode", 0, 0);
507 (void) lockmgr(&cp2
->c_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
509 * There were several blocking points since we first
510 * checked the hash. Now that we're through blocking,
511 * check the hash again in case we're racing for the
514 cp
= hfs_chashget(dev
, attrp
->ca_fileid
, wantrsrc
, &vp
, &rvp
);
516 /* We lost the race - use the winner's cnode */
517 FREE_ZONE(cp2
, sizeof(struct cnode
), M_HFSNODE
);
519 if (wantrsrc
&& rvp
!= NULL
) {
523 if (!wantrsrc
&& vp
!= NULL
) {
527 } else /* allocated */ {
533 /* Allocate a new vnode. If unsuccesful, leave after freeing memory */
534 if ((retval
= getnewvnode(VT_HFS
, mp
, hfs_vnodeop_p
, &new_vp
))) {
537 if (ISSET(cp
->c_flag
, C_WALLOC
)) {
538 CLR(cp
->c_flag
, C_WALLOC
);
541 FREE_ZONE(cp2
, sizeof(struct cnode
), M_HFSNODE
);
552 bcopy(attrp
, &cp
->c_attr
, sizeof(struct cat_attr
));
553 bcopy(descp
, &cp
->c_desc
, sizeof(struct cat_desc
));
556 if (wantrsrc
&& S_ISREG(cp
->c_mode
))
557 cp
->c_rsrc_vp
= new_vp
;
561 /* Release reference taken on opposite vnode (if any). */
568 vp
->v_ubcinfo
= UBC_NOINFO
;
571 * If this is a new cnode then initialize it using descp and attrp...
574 /* The name was inherited so clear descriptor state... */
575 descp
->cd_namelen
= 0;
576 descp
->cd_nameptr
= NULL
;
577 descp
->cd_flags
&= ~CD_HASBUF
;
580 if (IFTOVT(cp
->c_mode
) == VREG
&&
581 (descp
->cd_cnid
!= attrp
->ca_fileid
)) {
582 cp
->c_flag
|= C_HARDLINK
;
585 /* Take one dev reference for each non-directory cnode */
586 if (IFTOVT(cp
->c_mode
) != VDIR
) {
587 cp
->c_devvp
= hfsmp
->hfs_devvp
;
591 for (i
= 0; i
< MAXQUOTAS
; i
++)
592 cp
->c_dquot
[i
] = NODQUOT
;
596 if (IFTOVT(cp
->c_mode
) != VDIR
) {
597 if (forkp
&& attrp
->ca_blocks
< forkp
->cf_blocks
)
598 panic("hfs_getnewvnode: bad ca_blocks (too small)");
600 * Allocate and initialize a file fork...
602 MALLOC_ZONE(fp
, struct filefork
*, sizeof(struct filefork
),
603 M_HFSFORK
, M_WAITOK
);
604 bzero(fp
, sizeof(struct filefork
));
607 bcopy(forkp
, &fp
->ff_data
, sizeof(HFSPlusForkData
));
608 if (fp
->ff_clumpsize
== 0)
609 fp
->ff_clumpsize
= HFSTOVCB(hfsmp
)->vcbClpSiz
;
610 rl_init(&fp
->ff_invalidranges
);
612 if (cp
->c_rsrcfork
!= NULL
)
613 panic("stale rsrc fork");
616 if (cp
->c_datafork
!= NULL
)
617 panic("stale data fork");
623 * Finish vnode initialization.
624 * Setting the v_type 'stamps' the vnode as 'complete',
625 * so should be done almost last.
627 * At this point the vnode should be locked and fully
628 * allocated. And ready to be used or accessed. (though
629 * having it locked prevents most of this, it can still
630 * be accessed through lists and hashes).
632 vp
->v_type
= IFTOVT(cp
->c_mode
);
634 /* Tag system files */
635 if ((descp
->cd_cnid
< kHFSFirstUserCatalogNodeID
) && (vp
->v_type
== VREG
))
636 vp
->v_flag
|= VSYSTEM
;
637 /* Tag root directory */
638 if (cp
->c_cnid
== kRootDirID
)
641 if ((vp
->v_type
== VREG
) && !(vp
->v_flag
& VSYSTEM
)
642 && (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))) {
645 vp
->v_ubcinfo
= UBC_NOINFO
;
648 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
651 vp
->v_op
= hfs_specop_p
;
652 if ((nvp
= checkalias(vp
, cp
->c_rdev
, mp
))) {
654 * Discard unneeded vnode, but save its cnode.
655 * Note that the lock is carried over in the
656 * cnode to the replacement vnode.
658 nvp
->v_data
= vp
->v_data
;
660 vp
->v_op
= spec_vnodeop_p
;
664 * Reinitialize aliased cnode.
665 * Assume its not a resource fork.
670 } else if (vp
->v_type
== VFIFO
) {
672 vp
->v_op
= hfs_fifoop_p
;
676 /* Vnode is now initialized - see if anyone was waiting for it. */
677 CLR(cp
->c_flag
, C_ALLOC
);
678 if (ISSET(cp
->c_flag
, C_WALLOC
)) {
679 CLR(cp
->c_flag
, C_WALLOC
);