2 * Copyright (c) 2002-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 #include <sys/appleapiopts.h>
34 #ifdef __APPLE_API_PRIVATE
35 #include <sys/types.h>
36 #include <sys/queue.h>
38 #include <sys/vnode.h>
39 #include <sys/quota.h>
41 #include <kern/locks.h>
43 #include <hfs/hfs_catalog.h>
44 #include <hfs/rangelist.h>
48 * The filefork is used to represent an HFS file fork (data or resource).
49 * Reading or writing any of these fields requires holding cnode lock.
52 struct cnode
*ff_cp
; /* cnode associated with this fork */
53 struct rl_head ff_invalidranges
; /* Areas of disk that should read back as zeroes */
55 void *ffu_sysfileinfo
; /* additional info for system files */
56 char *ffu_symlinkptr
; /* symbolic link pathname */
58 struct cat_fork ff_data
; /* fork data (size, extents) */
60 typedef struct filefork filefork_t
;
62 /* Aliases for common fields */
63 #define ff_size ff_data.cf_size
64 #define ff_clumpsize ff_data.cf_clump
65 #define ff_bytesread ff_data.cf_bytesread
66 #define ff_blocks ff_data.cf_blocks
67 #define ff_extents ff_data.cf_extents
68 #define ff_unallocblocks ff_data.cf_vblocks
70 #define ff_symlinkptr ff_union.ffu_symlinkptr
71 #define ff_sysfileinfo ff_union.ffu_sysfileinfo
74 /* The btree code still needs these... */
75 #define fcbEOF ff_size
76 #define fcbExtents ff_extents
77 #define fcbBTCBPtr ff_sysfileinfo
79 typedef u_int8_t atomicflag_t
;
83 * Hardlink Origin (for hardlinked directories).
86 TAILQ_ENTRY(linkorigin
) lo_link
; /* chain */
87 void * lo_thread
; /* thread that performed the lookup */
88 cnid_t lo_cnid
; /* hardlink's cnid */
89 cnid_t lo_parentcnid
; /* hardlink's parent cnid */
91 typedef struct linkorigin linkorigin_t
;
93 #define MAX_CACHED_ORIGINS 10
94 #define MAX_CACHED_FILE_ORIGINS 8
97 * The cnode is used to represent each active (or recently active)
98 * file or directory in the HFS filesystem.
100 * Reading or writing any of these fields requires holding c_lock.
103 lck_rw_t c_rwlock
; /* cnode's lock */
104 void * c_lockowner
; /* cnode's lock owner (exclusive case only) */
105 lck_rw_t c_truncatelock
; /* protects file from truncation during read/write */
106 LIST_ENTRY(cnode
) c_hash
; /* cnode's hash chain */
107 u_int32_t c_flag
; /* cnode's runtime flags */
108 u_int32_t c_hflag
; /* cnode's flags for maintaining hash - protected by global hash lock */
109 struct vnode
*c_vp
; /* vnode for data fork or dir */
110 struct vnode
*c_rsrc_vp
; /* vnode for resource fork */
111 dev_t c_dev
; /* cnode's device */
112 struct dquot
*c_dquot
[MAXQUOTAS
]; /* cnode's quota info */
113 struct klist c_knotes
; /* knotes attached to this vnode */
114 u_int32_t c_childhint
; /* catalog hint for children (small dirs only) */
115 u_int32_t c_dirthreadhint
; /* catalog hint for directory's thread rec */
116 struct cat_desc c_desc
; /* cnode's descriptor */
117 struct cat_attr c_attr
; /* cnode's attributes */
118 TAILQ_HEAD(hfs_originhead
, linkorigin
) c_originlist
; /* hardlink origin cache */
119 TAILQ_HEAD(hfs_hinthead
, directoryhint
) c_hintlist
; /* readdir directory hint list */
120 int16_t c_dirhinttag
; /* directory hint tag */
122 int16_t cu_dirhintcnt
; /* directory hint count */
123 int16_t cu_syslockcount
; /* system file use only */
125 u_int32_t c_dirchangecnt
; /* changes each insert/delete (in-core only) */
126 struct filefork
*c_datafork
; /* cnode's data fork */
127 struct filefork
*c_rsrcfork
; /* cnode's rsrc fork */
128 atomicflag_t c_touch_acctime
;
129 atomicflag_t c_touch_chgtime
;
130 atomicflag_t c_touch_modtime
;
132 typedef struct cnode cnode_t
;
134 /* Aliases for common cnode fields */
135 #define c_cnid c_desc.cd_cnid
136 #define c_hint c_desc.cd_hint
137 #define c_parentcnid c_desc.cd_parentcnid
138 #define c_encoding c_desc.cd_encoding
140 #define c_fileid c_attr.ca_fileid
141 #define c_mode c_attr.ca_mode
142 #define c_linkcount c_attr.ca_linkcount
143 #define c_uid c_attr.ca_uid
144 #define c_gid c_attr.ca_gid
145 #define c_rdev c_attr.ca_union1.cau_rdev
146 #define c_atime c_attr.ca_atime
147 #define c_mtime c_attr.ca_mtime
148 #define c_ctime c_attr.ca_ctime
149 #define c_itime c_attr.ca_itime
150 #define c_btime c_attr.ca_btime
151 #define c_flags c_attr.ca_flags
152 #define c_finderinfo c_attr.ca_finderinfo
153 #define c_blocks c_attr.ca_union2.cau_blocks
154 #define c_entries c_attr.ca_union2.cau_entries
155 #define c_zftimeout c_childhint
157 #define c_dirhintcnt c_union.cu_dirhintcnt
158 #define c_syslockcount c_union.cu_syslockcount
161 /* hash maintenance flags kept in c_hflag and protected by hfs_chash_mutex */
162 #define H_ALLOC 0x00001 /* CNode is being allocated */
163 #define H_ATTACH 0x00002 /* CNode is being attached to by another vnode */
164 #define H_TRANSIT 0x00004 /* CNode is getting recycled */
165 #define H_WAITING 0x00008 /* CNode is being waited for */
168 /* Runtime cnode flags (kept in c_flag) */
169 #define C_NEED_RVNODE_PUT 0x00001 /* Need to do a vnode_put on c_rsrc_vp after the unlock */
170 #define C_NEED_DVNODE_PUT 0x00002 /* Need to do a vnode_put on c_vp after the unlock */
171 #define C_ZFWANTSYNC 0x00004 /* fsync requested and file has holes */
172 #define C_FROMSYNC 0x00008 /* fsync was called from sync */
174 #define C_MODIFIED 0x00010 /* CNode has been modified */
175 #define C_NOEXISTS 0x00020 /* CNode has been deleted, catalog entry is gone */
176 #define C_DELETED 0x00040 /* CNode has been marked to be deleted */
177 #define C_HARDLINK 0x00080 /* CNode is a hard link (file or dir) */
179 #define C_FORCEUPDATE 0x00100 /* force the catalog entry update */
180 #define C_HASXATTRS 0x00200 /* cnode has extended attributes */
181 #define C_NEG_ENTRIES 0x00400 /* directory has negative name entries */
182 #define C_WARNED_RSRC 0x00800 /* cnode lookup warning has been issued */
184 #define C_NEED_DATA_SETSIZE 0x01000 /* Do a ubc_setsize(0) on c_rsrc_vp after the unlock */
185 #define C_NEED_RSRC_SETSIZE 0x02000 /* Do a ubc_setsize(0) on c_vp after the unlock */
186 #define C_DIR_MODIFICATION 0x04000 /* Directory is being modified, wait for lookups */
188 #define ZFTIMELIMIT (5 * 60)
191 * The following is the "invisible" bit from the fdFlags field
192 * in the FndrFileInfo.
194 enum { kFinderInvisibleMask
= 1 << 14 };
198 * Convert between cnode pointers and vnode pointers
200 #define VTOC(vp) ((struct cnode *)vnode_fsnode((vp)))
202 #define CTOV(cp,rsrc) (((rsrc) && S_ISREG((cp)->c_mode)) ? \
203 (cp)->c_rsrc_vp : (cp)->c_vp)
206 * Convert between vnode pointers and file forks
208 * Note: no CTOF since that is ambiguous
211 #define FTOC(fp) ((fp)->ff_cp)
213 #define VTOF(vp) ((vp) == VTOC((vp))->c_rsrc_vp ? \
214 VTOC((vp))->c_rsrcfork : \
215 VTOC((vp))->c_datafork)
217 #define VCTOF(vp, cp) ((vp) == (cp)->c_rsrc_vp ? \
221 #define FTOV(fp) ((fp) == FTOC(fp)->c_rsrcfork ? \
222 FTOC(fp)->c_rsrc_vp : \
227 * Test for a resource fork
229 #define FORK_IS_RSRC(fp) ((fp) == FTOC(fp)->c_rsrcfork)
231 #define VNODE_IS_RSRC(vp) ((vp) == VTOC((vp))->c_rsrc_vp)
234 #define ATIME_ONDISK_ACCURACY 300
237 /* This overlays the FileID portion of NFS file handles. */
239 u_int32_t hfsfid_cnid
; /* Catalog node ID. */
240 u_int32_t hfsfid_gen
; /* Generation number (create date). */
244 /* Get new default vnode */
245 extern int hfs_getnewvnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
,
246 struct cat_desc
*descp
, int flags
, struct cat_attr
*attrp
,
247 struct cat_fork
*forkp
, struct vnode
**vpp
);
250 #define GNV_WANTRSRC 0x01 /* Request the resource fork vnode. */
251 #define GNV_SKIPLOCK 0x02 /* Skip taking the cnode lock (when getting resource fork). */
252 #define GNV_CREATE 0x04 /* The vnode is for a newly created item. */
255 /* Touch cnode times based on c_touch_xxx flags */
256 extern void hfs_touchtimes(struct hfsmount
*, struct cnode
*);
259 * HFS cnode hash functions.
261 extern void hfs_chashinit(void);
262 extern void hfs_chashinit_finish(void);
263 extern void hfs_chashinsert(struct cnode
*cp
);
264 extern int hfs_chashremove(struct cnode
*cp
);
265 extern void hfs_chash_abort(struct cnode
*cp
);
266 extern void hfs_chash_rehash(struct cnode
*cp1
, struct cnode
*cp2
);
267 extern void hfs_chashwakeup(struct cnode
*cp
, int flags
);
268 extern void hfs_chash_mark_in_transit(struct cnode
*cp
);
270 extern struct vnode
* hfs_chash_getvnode(dev_t dev
, ino_t inum
, int wantrsrc
, int skiplock
);
271 extern struct cnode
* hfs_chash_getcnode(dev_t dev
, ino_t inum
, struct vnode
**vpp
, int wantrsrc
, int skiplock
);
272 extern int hfs_chash_snoop(dev_t
, ino_t
, int (*)(const struct cat_desc
*,
273 const struct cat_attr
*, void *), void *);
274 extern int hfs_valid_cnode(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct componentname
*cnp
, cnid_t cnid
);
276 extern int hfs_chash_set_childlinkbit(dev_t dev
, cnid_t cnid
);
279 * HFS cnode lock functions.
283 * 1. cnode truncate lock (if needed)
284 * 2. cnode lock (in parent-child order if related, otherwise by address order)
285 * 3. journal (if needed)
286 * 4. system files (as needed)
287 * A. Catalog B-tree file
288 * B. Attributes B-tree file
289 * C. Startup file (if there is one)
290 * D. Allocation Bitmap file (always exclusive, supports recursion)
291 * E. Overflow Extents B-tree file (always exclusive, supports recursion)
292 * 5. hfs mount point (always last)
295 enum hfslocktype
{HFS_SHARED_LOCK
= 1, HFS_EXCLUSIVE_LOCK
= 2, HFS_FORCE_LOCK
= 3};
296 #define HFS_SHARED_OWNER (void *)0xffffffff
298 extern int hfs_lock(struct cnode
*, enum hfslocktype
);
299 extern int hfs_lockpair(struct cnode
*, struct cnode
*, enum hfslocktype
);
300 extern int hfs_lockfour(struct cnode
*, struct cnode
*, struct cnode
*, struct cnode
*,
303 extern void hfs_unlock(struct cnode
*);
304 extern void hfs_unlockpair(struct cnode
*, struct cnode
*);
305 extern void hfs_unlockfour(struct cnode
*, struct cnode
*, struct cnode
*, struct cnode
*);
307 extern void hfs_lock_truncate(struct cnode
*, int);
308 extern void hfs_unlock_truncate(struct cnode
*, int);
310 #endif /* __APPLE_API_PRIVATE */
313 #endif /* ! _HFS_CNODE_H_ */