]> git.saurik.com Git - apple/hfs.git/blob - livefiles_hfs_plugin/lf_hfs_cnode.h
hfs-522.100.5.tar.gz
[apple/hfs.git] / livefiles_hfs_plugin / lf_hfs_cnode.h
1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
2 *
3 * lf_hfs_cnode.h
4 * livefiles_hfs
5 *
6 * Created by Or Haimovich on 18/3/18.
7 */
8
9 #ifndef lf_hfs_cnode_h
10 #define lf_hfs_cnode_h
11
12 #include "lf_hfs_locks.h"
13 #include "lf_hfs_catalog.h"
14 #include "lf_hfs_rangelist.h"
15 #include "lf_hfs_vnode.h"
16 #include <sys/stat.h>
17
18 enum hfs_locktype {
19 HFS_SHARED_LOCK = 1,
20 HFS_EXCLUSIVE_LOCK = 2,
21 HFS_TRY_EXCLUSIVE_LOCK = 3
22 };
23
24 /* Option flags for cnode and truncate lock functions */
25 enum hfs_lockflags {
26 HFS_LOCK_DEFAULT = 0x0, /* Default flag, no options provided */
27 HFS_LOCK_ALLOW_NOEXISTS = 0x1, /* Allow locking of all cnodes, including cnode marked deleted with no catalog entry */
28 HFS_LOCK_SKIP_IF_EXCLUSIVE = 0x2, /* Skip locking if the current thread already holds the lock exclusive */
29
30 // Used when you do not want to check return from hfs_lock
31 HFS_LOCK_ALWAYS = HFS_LOCK_ALLOW_NOEXISTS,
32 };
33 #define HFS_SHARED_OWNER (void *)0xffffffff
34
35 #define ZFTIMELIMIT (5 * 60)
36
37 /* Zero-fill file and push regions out to disk */
38 enum {
39 // Use this flag if you're going to sync later
40 HFS_FILE_DONE_NO_SYNC = 1,
41 };
42
43 /*
44 * The filefork is used to represent an HFS file fork (data or resource).
45 * Reading or writing any of these fields requires holding cnode lock.
46 */
47 struct filefork {
48 struct cnode *ff_cp; /* cnode associated with this fork */
49 struct rl_head ff_invalidranges; /* Areas of disk that should read back as zeroes */
50 union {
51 void *ffu_sysfileinfo; /* additional info for system files */
52 char *ffu_symlinkptr; /* symbolic link pathname */
53 } ff_union;
54 struct cat_fork ff_data; /* fork data (size, extents) */
55 };
56 typedef struct filefork filefork_t;
57
58 /* Aliases for common fields */
59 #define ff_size ff_data.cf_size
60 #define ff_new_size ff_data.cf_new_size
61 #define ff_clumpsize ff_data.cf_clump
62 #define ff_bytesread ff_data.cf_bytesread
63 #define ff_extents ff_data.cf_extents
64
65 /*
66 * Note that the blocks fields are protected by the cnode lock, *not*
67 * the truncate lock.
68 */
69 #define ff_blocks ff_data.cf_blocks
70 #define ff_unallocblocks ff_data.cf_vblocks
71
72 #define ff_symlinkptr ff_union.ffu_symlinkptr
73 #define ff_sysfileinfo ff_union.ffu_sysfileinfo
74
75 /* The btree code still needs these... */
76 #define fcbEOF ff_size
77 #define fcbExtents ff_extents
78 #define fcbBTCBPtr ff_sysfileinfo
79
80 typedef u_int8_t atomicflag_t;
81
82 /*
83 * Hardlink Origin (for hardlinked directories).
84 */
85 struct linkorigin {
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 */
90 };
91 typedef struct linkorigin linkorigin_t;
92
93 #define MAX_CACHED_ORIGINS 10
94 #define MAX_CACHED_FILE_ORIGINS 8
95
96 /*
97 * The cnode is used to represent each active (or recently active)
98 * file or directory in the HFS filesystem.
99 *
100 * Reading or writing any of these fields requires holding c_lock.
101 */
102 struct cnode {
103 pthread_rwlock_t c_rwlock; /* cnode's lock */
104 pthread_t c_lockowner; /* cnode's lock owner (exclusive case only) */
105 pthread_rwlock_t c_truncatelock; /* protects file from truncation during read/write */
106 pthread_t c_truncatelockowner; /* truncate lock owner (exclusive case only) */
107 pthread_cond_t c_cacsh_cond; /* cond for cnode cacsh*/
108
109 LIST_ENTRY(cnode) c_hash; /* cnode's hash chain */
110 u_int32_t c_flag; /* cnode's runtime flags */
111 u_int32_t c_hflag; /* cnode's flags for maintaining hash - protected by global hash lock */
112 struct vnode *c_vp; /* vnode for data fork or dir */
113 struct vnode *c_rsrc_vp; /* vnode for resource fork */
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 */
121 union {
122 int16_t cu_dirhintcnt; /* directory hint count */
123 int16_t cu_syslockcount; /* system file use only */
124 } c_union;
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;
131
132 // The following flags are protected by the truncate lock
133 union {
134 struct {
135 bool c_need_dvnode_put_after_truncate_unlock : 1;
136 bool c_need_rvnode_put_after_truncate_unlock : 1;
137 };
138 uint8_t c_tflags;
139 };
140
141 /*
142 * Where we're using a journal, we keep track of the last
143 * transaction that we did an update in. If a minor modification
144 * is made, we'll still push it if we're still on the same
145 * transaction.
146 */
147 uint32_t c_update_txn;
148
149 volatile uint32_t uOpenLookupRefCount;
150
151 };
152 typedef struct cnode cnode_t;
153
154 /* Aliases for common cnode fields */
155 #define c_cnid c_desc.cd_cnid
156 #define c_hint c_desc.cd_hint
157 #define c_parentcnid c_desc.cd_parentcnid
158 #define c_encoding c_desc.cd_encoding
159
160 #define c_fileid c_attr.ca_fileid
161 #define c_mode c_attr.ca_mode
162 #define c_linkcount c_attr.ca_linkcount
163 #define c_uid c_attr.ca_uid
164 #define c_gid c_attr.ca_gid
165 #define c_rdev c_attr.ca_union1.cau_rdev
166 #define c_atime c_attr.ca_atime
167 #define c_mtime c_attr.ca_mtime
168 #define c_ctime c_attr.ca_ctime
169 #define c_itime c_attr.ca_itime
170 #define c_btime c_attr.ca_btime
171 #define c_bsdflags c_attr.ca_bsdflags
172 #define c_finderinfo c_attr.ca_finderinfo
173 #define c_blocks c_attr.ca_union2.cau_blocks
174 #define c_entries c_attr.ca_union2.cau_entries
175 #define c_zftimeout c_childhint
176
177 #define c_dirhintcnt c_union.cu_dirhintcnt
178 #define c_syslockcount c_union.cu_syslockcount
179
180 /* hash maintenance flags kept in c_hflag and protected by hfs_chash_mutex */
181 #define H_ALLOC 0x00001 /* CNode is being allocated */
182 #define H_ATTACH 0x00002 /* CNode is being attached to by another vnode */
183 #define H_TRANSIT 0x00004 /* CNode is getting recycled */
184 #define H_WAITING 0x00008 /* CNode is being waited for */
185
186 /*
187 * Runtime cnode flags (kept in c_flag)
188 */
189 #define C_NEED_RVNODE_PUT 0x0000001 /* Need to do a vnode_put on c_rsrc_vp after the unlock */
190 #define C_NEED_DVNODE_PUT 0x0000002 /* Need to do a vnode_put on c_vp after the unlock */
191 #define C_ZFWANTSYNC 0x0000004 /* fsync requested and file has holes */
192 #define C_FROMSYNC 0x0000008 /* fsync was called from sync */
193
194 #define C_MODIFIED 0x0000010 /* CNode has been modified */
195 #define C_NOEXISTS 0x0000020 /* CNode has been deleted, catalog entry is gone */
196 #define C_DELETED 0x0000040 /* CNode has been marked to be deleted */
197 #define C_HARDLINK 0x0000080 /* CNode is a hard link (file or dir) */
198
199 /*
200 * A minor modification is one where the volume would not be inconsistent if
201 * the change was not pushed to disk. For example, changes to times.
202 */
203 #define C_MINOR_MOD 0x0000100 /* CNode has a minor modification */
204
205 #define C_HASXATTRS 0x0000200 /* cnode has extended attributes */
206 /*
207 * For C_SSD_STATIC: SSDs may want to deal with the file payload data in a
208 * different manner knowing that the content is not likely to be modified. This is
209 * purely advisory at the HFS level, and is not maintained after the cnode goes out of core.
210 */
211 #define C_SSD_STATIC 0x0000800 /* Assume future writes contain static content */
212
213 #define C_NEED_DATA_SETSIZE 0x0001000 /* Do a ubc_setsize(0) on c_rsrc_vp after the unlock */
214 #define C_NEED_RSRC_SETSIZE 0x0002000 /* Do a ubc_setsize(0) on c_vp after the unlock */
215 #define C_DIR_MODIFICATION 0x0004000 /* Directory is being modified, wait for lookups */
216 #define C_ALWAYS_ZEROFILL 0x0008000 /* Always zero-fill the file on an fsync */
217
218 #define C_RENAMED 0x0010000 /* cnode was deleted as part of rename; C_DELETED should also be set */
219 #define C_NEEDS_DATEADDED 0x0020000 /* cnode needs date-added written to the finderinfo bit */
220 #define C_BACKINGSTORE 0x0040000 /* cnode is a backing store for an existing or currently-mounting filesystem */
221
222 /*
223 * This flag indicates the cnode might be dirty because it
224 * was mapped writable so if we get any page-outs, update
225 * the modification and change times.
226 */
227 #define C_MIGHT_BE_DIRTY_FROM_MAPPING 0x0080000
228
229 /*
230 * Convert between cnode pointers and vnode pointers
231 */
232 #define VTOC(vp) ((struct cnode *) (vp)->sFSParams.vnfs_fsnode)
233
234 #define CTOV(cp,rsrc) (((rsrc) && S_ISREG((cp)->c_mode)) ? \
235 (cp)->c_rsrc_vp : (cp)->c_vp)
236
237 /*
238 * Convert between vnode pointers and file forks
239 *
240 * Note: no CTOF since that is ambiguous
241 */
242
243 #define FTOC(fp) ((fp)->ff_cp)
244
245 #define VTOF(vp) ((vp) == VTOC((vp))->c_rsrc_vp ? \
246 VTOC((vp))->c_rsrcfork : \
247 VTOC((vp))->c_datafork)
248
249 #define VCTOF(vp, cp) ((vp) == (cp)->c_rsrc_vp ? \
250 (cp)->c_rsrcfork : \
251 (cp)->c_datafork)
252
253 #define FTOV(fp) ((fp) == FTOC(fp)->c_rsrcfork ? \
254 FTOC(fp)->c_rsrc_vp : \
255 FTOC(fp)->c_vp)
256 /*
257 * Test for a resource fork
258 */
259 #define FORK_IS_RSRC(fp) ((fp) == FTOC(fp)->c_rsrcfork)
260
261 #define VNODE_IS_RSRC(vp) ((vp) == VTOC((vp))->c_rsrc_vp)
262
263 /*
264 * The following is the "invisible" bit from the fdFlags field
265 * in the FndrFileInfo.
266 */
267 enum { kFinderInvisibleMask = 1 << 14 };
268
269 /*
270 * HFS cnode hash functions.
271 */
272 void hfs_chashinit(void);
273 void hfs_chashinit_finish(struct hfsmount *hfsmp);
274 void hfs_delete_chash(struct hfsmount *hfsmp);
275
276 /* Get new default vnode */
277 int hfs_getnewvnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp, struct cat_desc *descp, int flags, struct cat_attr *attrp, struct cat_fork *forkp, struct vnode **vpp, int *out_flags);
278
279 #define ATIME_ONDISK_ACCURACY 300
280
281 static inline bool hfs_should_save_atime(cnode_t *cp)
282 {
283 /*
284 * We only write atime updates to disk if the delta is greater
285 * than ATIME_ONDISK_ACCURACY.
286 */
287 return (cp->c_atime < cp->c_attr.ca_atimeondisk || cp->c_atime - cp->c_attr.ca_atimeondisk > ATIME_ONDISK_ACCURACY);
288 }
289
290 typedef enum {
291 HFS_NOT_DIRTY = 0,
292 HFS_DIRTY = 1,
293 HFS_DIRTY_ATIME = 2
294 } hfs_dirty_t;
295
296
297 static inline hfs_dirty_t hfs_is_dirty(cnode_t *cp)
298 {
299 if (ISSET(cp->c_flag, C_NOEXISTS))
300 return HFS_NOT_DIRTY;
301
302 if (ISSET(cp->c_flag, C_MODIFIED | C_MINOR_MOD | C_NEEDS_DATEADDED)
303 || cp->c_touch_chgtime || cp->c_touch_modtime) {
304 return HFS_DIRTY;
305 }
306
307 if (cp->c_touch_acctime || hfs_should_save_atime(cp))
308 return HFS_DIRTY_ATIME;
309
310 return HFS_NOT_DIRTY;
311 }
312
313 /*
314 * Catalog Lookup struct (runtime)
315 *
316 * This is used so that when we need to malloc a container for a catalog
317 * lookup operation, we can acquire memory for everything in one fell swoop
318 * as opposed to putting many of these objects on the stack. The cat_fork
319 * data structure can take up 100+bytes easily, and that can add to stack
320 * overhead.
321 *
322 * As a result, we use this to easily pass around the memory needed for a
323 * lookup operation.
324 */
325 #define HFS_TEMPLOOKUP_NAMELEN 32
326
327 struct cat_lookup_buffer {
328 struct cat_desc lookup_desc;
329 struct cat_attr lookup_attr;
330 struct filefork lookup_fork;
331 struct componentname lookup_cn;
332 char lookup_name[HFS_TEMPLOOKUP_NAMELEN]; /* for open-unlinked paths only */
333 };
334
335 /* Input flags for hfs_getnewvnode */
336
337 #define GNV_WANTRSRC 0x01 /* Request the resource fork vnode. */
338 #define GNV_SKIPLOCK 0x02 /* Skip taking the cnode lock (when getting resource fork). */
339 #define GNV_CREATE 0x04 /* The vnode is for a newly created item. */
340 #define GNV_NOCACHE 0x08 /* Delay entering this item in the name cache */
341 #define GNV_USE_VP 0x10 /* Use the vnode provided in *vpp instead of creating a new one */
342
343 /* Output flags for hfs_getnewvnode */
344
345 #define GNV_CHASH_RENAMED 0x01 /* The cnode was renamed in-flight */
346 #define GNV_CAT_DELETED 0x02 /* The cnode was deleted from the catalog */
347 #define GNV_NEW_CNODE 0x04 /* We are vending out a newly initialized cnode */
348 #define GNV_CAT_ATTRCHANGED 0x08 /* Something in struct cat_attr changed in between cat_lookups */
349
350
351 int hfs_valid_cnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp, cnid_t cnid, struct cat_attr *cattr, int *error);
352 int hfs_lock(struct cnode *cp, enum hfs_locktype locktype, enum hfs_lockflags flags);
353 void hfs_unlock(struct cnode *cp);
354 void hfs_lock_truncate(struct cnode *cp, enum hfs_locktype locktype, enum hfs_lockflags flags);
355 void hfs_unlock_truncate(struct cnode *cp, enum hfs_lockflags flags);
356 int hfs_lockpair(struct cnode *cp1, struct cnode *cp2, enum hfs_locktype locktype);
357 void hfs_unlockpair(struct cnode *cp1, struct cnode *cp2);
358 int hfs_lockfour(struct cnode *cp1, struct cnode *cp2, struct cnode *cp3, struct cnode *cp4, enum hfs_locktype locktype, struct cnode **error_cnode);
359 void hfs_unlockfour(struct cnode *cp1, struct cnode *cp2, struct cnode *cp3, struct cnode *cp4);
360 uint32_t hfs_incr_gencount (struct cnode *cp);
361 void hfs_clear_might_be_dirty_flag(cnode_t *cp);
362 void hfs_write_dateadded (struct cat_attr *attrp, uint64_t dateadded);
363 u_int32_t hfs_get_dateadded(struct cnode *cp);
364 void hfs_touchtimes(struct hfsmount *hfsmp, struct cnode* cp);
365 void hfs_write_gencount (struct cat_attr *attrp, uint32_t gencount);
366 int hfs_vnop_reclaim(struct vnode *vp);
367 #endif /* lf_hfs_cnode_h */