]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_cnode.h
xnu-2422.90.20.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_cnode.h
CommitLineData
9bccf70c 1/*
39236c6e 2 * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
9bccf70c 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
9bccf70c 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
9bccf70c
A
27 */
28#ifndef _HFS_CNODE_H_
29#define _HFS_CNODE_H_
30
31#include <sys/appleapiopts.h>
32
33#ifdef KERNEL
34#ifdef __APPLE_API_PRIVATE
35#include <sys/types.h>
9bccf70c
A
36#include <sys/queue.h>
37#include <sys/stat.h>
38#include <sys/vnode.h>
39#include <sys/quota.h>
40
91447636
A
41#include <kern/locks.h>
42
9bccf70c
A
43#include <hfs/hfs_catalog.h>
44#include <hfs/rangelist.h>
b0d623f7
A
45#if HFS_COMPRESSION
46#include <sys/decmpfs.h>
47#endif
6d2010ae
A
48#if CONFIG_PROTECT
49#include <sys/cprotect.h>
50#endif
51
9bccf70c
A
52
53/*
54 * The filefork is used to represent an HFS file fork (data or resource).
55 * Reading or writing any of these fields requires holding cnode lock.
56 */
57struct filefork {
91447636
A
58 struct cnode *ff_cp; /* cnode associated with this fork */
59 struct rl_head ff_invalidranges; /* Areas of disk that should read back as zeroes */
9bccf70c 60 union {
91447636
A
61 void *ffu_sysfileinfo; /* additional info for system files */
62 char *ffu_symlinkptr; /* symbolic link pathname */
63 } ff_union;
64 struct cat_fork ff_data; /* fork data (size, extents) */
9bccf70c 65};
55e303ae 66typedef struct filefork filefork_t;
9bccf70c 67
316670eb
A
68
69#define HFS_TEMPLOOKUP_NAMELEN 32
70
71/*
72 * Catalog Lookup struct (runtime)
73 *
74 * This is used so that when we need to malloc a container for a catalog
75 * lookup operation, we can acquire memory for everything in one fell swoop
76 * as opposed to putting many of these objects on the stack. The cat_fork
77 * data structure can take up 100+bytes easily, and that can add to stack
78 * overhead.
79 *
80 * As a result, we use this to easily pass around the memory needed for a
81 * lookup operation.
82 */
83struct cat_lookup_buffer {
84 struct cat_desc lookup_desc;
85 struct cat_attr lookup_attr;
86 struct filefork lookup_fork;
87 struct componentname lookup_cn;
88 char lookup_name[HFS_TEMPLOOKUP_NAMELEN]; /* for open-unlinked paths only */
89};
90
91
9bccf70c 92/* Aliases for common fields */
91447636 93#define ff_size ff_data.cf_size
593a1d5f 94#define ff_new_size ff_data.cf_new_size
91447636
A
95#define ff_clumpsize ff_data.cf_clump
96#define ff_bytesread ff_data.cf_bytesread
97#define ff_blocks ff_data.cf_blocks
98#define ff_extents ff_data.cf_extents
55e303ae
A
99#define ff_unallocblocks ff_data.cf_vblocks
100
91447636
A
101#define ff_symlinkptr ff_union.ffu_symlinkptr
102#define ff_sysfileinfo ff_union.ffu_sysfileinfo
9bccf70c
A
103
104
105/* The btree code still needs these... */
91447636
A
106#define fcbEOF ff_size
107#define fcbExtents ff_extents
108#define fcbBTCBPtr ff_sysfileinfo
9bccf70c 109
91447636 110typedef u_int8_t atomicflag_t;
2d21ac55
A
111
112
113/*
114 * Hardlink Origin (for hardlinked directories).
115 */
116struct linkorigin {
117 TAILQ_ENTRY(linkorigin) lo_link; /* chain */
118 void * lo_thread; /* thread that performed the lookup */
119 cnid_t lo_cnid; /* hardlink's cnid */
120 cnid_t lo_parentcnid; /* hardlink's parent cnid */
121};
122typedef struct linkorigin linkorigin_t;
123
124#define MAX_CACHED_ORIGINS 10
935ed37a 125#define MAX_CACHED_FILE_ORIGINS 8
2d21ac55 126
9bccf70c
A
127/*
128 * The cnode is used to represent each active (or recently active)
129 * file or directory in the HFS filesystem.
130 *
131 * Reading or writing any of these fields requires holding c_lock.
132 */
133struct cnode {
91447636
A
134 lck_rw_t c_rwlock; /* cnode's lock */
135 void * c_lockowner; /* cnode's lock owner (exclusive case only) */
136 lck_rw_t c_truncatelock; /* protects file from truncation during read/write */
6d2010ae 137 void * c_truncatelockowner; /* truncate lock owner (exclusive case only) */
9bccf70c
A
138 LIST_ENTRY(cnode) c_hash; /* cnode's hash chain */
139 u_int32_t c_flag; /* cnode's runtime flags */
91447636 140 u_int32_t c_hflag; /* cnode's flags for maintaining hash - protected by global hash lock */
9bccf70c
A
141 struct vnode *c_vp; /* vnode for data fork or dir */
142 struct vnode *c_rsrc_vp; /* vnode for resource fork */
b0d623f7 143 struct dquot *c_dquot[MAXQUOTAS]; /* cnode's quota info */
2d21ac55
A
144 u_int32_t c_childhint; /* catalog hint for children (small dirs only) */
145 u_int32_t c_dirthreadhint; /* catalog hint for directory's thread rec */
9bccf70c
A
146 struct cat_desc c_desc; /* cnode's descriptor */
147 struct cat_attr c_attr; /* cnode's attributes */
2d21ac55
A
148 TAILQ_HEAD(hfs_originhead, linkorigin) c_originlist; /* hardlink origin cache */
149 TAILQ_HEAD(hfs_hinthead, directoryhint) c_hintlist; /* readdir directory hint list */
91447636
A
150 int16_t c_dirhinttag; /* directory hint tag */
151 union {
152 int16_t cu_dirhintcnt; /* directory hint count */
153 int16_t cu_syslockcount; /* system file use only */
154 } c_union;
2d21ac55 155 u_int32_t c_dirchangecnt; /* changes each insert/delete (in-core only) */
9bccf70c
A
156 struct filefork *c_datafork; /* cnode's data fork */
157 struct filefork *c_rsrcfork; /* cnode's rsrc fork */
91447636
A
158 atomicflag_t c_touch_acctime;
159 atomicflag_t c_touch_chgtime;
160 atomicflag_t c_touch_modtime;
b0d623f7
A
161#if HFS_COMPRESSION
162 decmpfs_cnode *c_decmp;
163#endif /* HFS_COMPRESSION */
6d2010ae
A
164#if CONFIG_PROTECT
165 cprotect_t c_cpentry; /* content protection data */
166#endif
167
9bccf70c 168};
55e303ae 169typedef struct cnode cnode_t;
9bccf70c
A
170
171/* Aliases for common cnode fields */
172#define c_cnid c_desc.cd_cnid
173#define c_hint c_desc.cd_hint
174#define c_parentcnid c_desc.cd_parentcnid
175#define c_encoding c_desc.cd_encoding
176
177#define c_fileid c_attr.ca_fileid
178#define c_mode c_attr.ca_mode
2d21ac55 179#define c_linkcount c_attr.ca_linkcount
9bccf70c
A
180#define c_uid c_attr.ca_uid
181#define c_gid c_attr.ca_gid
2d21ac55 182#define c_rdev c_attr.ca_union1.cau_rdev
9bccf70c
A
183#define c_atime c_attr.ca_atime
184#define c_mtime c_attr.ca_mtime
9bccf70c
A
185#define c_ctime c_attr.ca_ctime
186#define c_itime c_attr.ca_itime
187#define c_btime c_attr.ca_btime
316670eb 188#define c_bsdflags c_attr.ca_flags
9bccf70c 189#define c_finderinfo c_attr.ca_finderinfo
2d21ac55
A
190#define c_blocks c_attr.ca_union2.cau_blocks
191#define c_entries c_attr.ca_union2.cau_entries
9bccf70c
A
192#define c_zftimeout c_childhint
193
91447636
A
194#define c_dirhintcnt c_union.cu_dirhintcnt
195#define c_syslockcount c_union.cu_syslockcount
9bccf70c 196
55e303ae 197
91447636
A
198/* hash maintenance flags kept in c_hflag and protected by hfs_chash_mutex */
199#define H_ALLOC 0x00001 /* CNode is being allocated */
200#define H_ATTACH 0x00002 /* CNode is being attached to by another vnode */
201#define H_TRANSIT 0x00004 /* CNode is getting recycled */
202#define H_WAITING 0x00008 /* CNode is being waited for */
203
9bccf70c 204
91447636
A
205/* Runtime cnode flags (kept in c_flag) */
206#define C_NEED_RVNODE_PUT 0x00001 /* Need to do a vnode_put on c_rsrc_vp after the unlock */
207#define C_NEED_DVNODE_PUT 0x00002 /* Need to do a vnode_put on c_vp after the unlock */
208#define C_ZFWANTSYNC 0x00004 /* fsync requested and file has holes */
209#define C_FROMSYNC 0x00008 /* fsync was called from sync */
9bccf70c 210
91447636
A
211#define C_MODIFIED 0x00010 /* CNode has been modified */
212#define C_NOEXISTS 0x00020 /* CNode has been deleted, catalog entry is gone */
213#define C_DELETED 0x00040 /* CNode has been marked to be deleted */
2d21ac55 214#define C_HARDLINK 0x00080 /* CNode is a hard link (file or dir) */
9bccf70c 215
91447636
A
216#define C_FORCEUPDATE 0x00100 /* force the catalog entry update */
217#define C_HASXATTRS 0x00200 /* cnode has extended attributes */
2d21ac55 218#define C_NEG_ENTRIES 0x00400 /* directory has negative name entries */
316670eb
A
219/*
220 * For C_SSD_STATIC: SSDs may want to deal with the file payload data in a
221 * different manner knowing that the content is not likely to be modified. This is
222 * purely advisory at the HFS level, and is not maintained after the cnode goes out of core.
223 */
224#define C_SSD_STATIC 0x00800 /* Assume future writes contain static content */
9bccf70c 225
0c530ab8
A
226#define C_NEED_DATA_SETSIZE 0x01000 /* Do a ubc_setsize(0) on c_rsrc_vp after the unlock */
227#define C_NEED_RSRC_SETSIZE 0x02000 /* Do a ubc_setsize(0) on c_vp after the unlock */
2d21ac55 228#define C_DIR_MODIFICATION 0x04000 /* Directory is being modified, wait for lookups */
b0d623f7 229#define C_ALWAYS_ZEROFILL 0x08000 /* Always zero-fill the file on an fsync */
9bccf70c 230
6d2010ae
A
231#define C_RENAMED 0x10000 /* cnode was deleted as part of rename; C_DELETED should also be set */
232#define C_NEEDS_DATEADDED 0x20000 /* cnode needs date-added written to the finderinfo bit */
233#define C_BACKINGSTORE 0x40000 /* cnode is a backing store for an existing or currently-mounting filesystem */
316670eb
A
234#define C_SWAPINPROGRESS 0x80000 /* cnode's data is about to be swapped. Issue synchronous cluster io */
235
39236c6e
A
236/*
237 * For C_SSD_GREEDY_MODE: SSDs may want to write the file payload data using the greedy mode knowing
238 * that the content needs to be written out to the disk quicker than normal at the expense of storage efficiency.
239 * This is purely advisory at the HFS level, and is not maintained after the cnode goes out of core.
240 */
241#define C_SSD_GREEDY_MODE 0x100000 /* Assume future writes are recommended to be written in SLC mode */
316670eb 242
9bccf70c
A
243#define ZFTIMELIMIT (5 * 60)
244
2d21ac55
A
245/*
246 * The following is the "invisible" bit from the fdFlags field
247 * in the FndrFileInfo.
248 */
249enum { kFinderInvisibleMask = 1 << 14 };
250
251
9bccf70c
A
252/*
253 * Convert between cnode pointers and vnode pointers
254 */
91447636 255#define VTOC(vp) ((struct cnode *)vnode_fsnode((vp)))
9bccf70c
A
256
257#define CTOV(cp,rsrc) (((rsrc) && S_ISREG((cp)->c_mode)) ? \
258 (cp)->c_rsrc_vp : (cp)->c_vp)
259
260/*
261 * Convert between vnode pointers and file forks
262 *
263 * Note: no CTOF since that is ambiguous
264 */
265
266#define FTOC(fp) ((fp)->ff_cp)
267
268#define VTOF(vp) ((vp) == VTOC((vp))->c_rsrc_vp ? \
269 VTOC((vp))->c_rsrcfork : \
270 VTOC((vp))->c_datafork)
271
2d21ac55
A
272#define VCTOF(vp, cp) ((vp) == (cp)->c_rsrc_vp ? \
273 (cp)->c_rsrcfork : \
274 (cp)->c_datafork)
275
9bccf70c
A
276#define FTOV(fp) ((fp) == FTOC(fp)->c_rsrcfork ? \
277 FTOC(fp)->c_rsrc_vp : \
278 FTOC(fp)->c_vp)
279
b7266188
A
280/*
281 * This is a helper function used for determining whether or not a cnode has become open
282 * unlinked in between the time we acquired its vnode and the time we acquire the cnode lock
283 * to start manipulating it. Due to the SMP nature of VFS, it is probably necessary to
284 * use this macro every time we acquire a cnode lock, as the content of the Cnode may have
285 * been modified in betweeen the lookup and a VNOP. Whether or not to call this is dependent
286 * upon the VNOP in question. Sometimes it is OK to use an open-unlinked file, for example, in,
287 * reading. But other times, such as on the source of a VNOP_RENAME, it should be disallowed.
288 */
6d2010ae 289int hfs_checkdeleted(struct cnode *cp);
55e303ae 290
9bccf70c
A
291/*
292 * Test for a resource fork
293 */
294#define FORK_IS_RSRC(fp) ((fp) == FTOC(fp)->c_rsrcfork)
295
296#define VNODE_IS_RSRC(vp) ((vp) == VTOC((vp))->c_rsrc_vp)
297
b0d623f7
A
298#if HFS_COMPRESSION
299/*
300 * VTOCMP(vp) returns a pointer to vp's decmpfs_cnode; this could be NULL
301 * if the file is not compressed or if hfs_file_is_compressed() hasn't
302 * yet been called on this file.
303 */
304#define VTOCMP(vp) (VTOC((vp))->c_decmp)
305int hfs_file_is_compressed(struct cnode *cp, int skiplock);
306int hfs_uncompressed_size_of_compressed_file(struct hfsmount *hfsmp, struct vnode *vp, cnid_t fid, off_t *size, int skiplock);
307int hfs_hides_rsrc(vfs_context_t ctx, struct cnode *cp, int skiplock);
308int hfs_hides_xattr(vfs_context_t ctx, struct cnode *cp, const char *name, int skiplock);
309#endif
9bccf70c 310
55e303ae 311#define ATIME_ONDISK_ACCURACY 300
9bccf70c 312
91447636
A
313
314/* This overlays the FileID portion of NFS file handles. */
9bccf70c 315struct hfsfid {
9bccf70c
A
316 u_int32_t hfsfid_cnid; /* Catalog node ID. */
317 u_int32_t hfsfid_gen; /* Generation number (create date). */
318};
319
320
2d21ac55
A
321/* Get new default vnode */
322extern int hfs_getnewvnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp,
323 struct cat_desc *descp, int flags, struct cat_attr *attrp,
6d2010ae 324 struct cat_fork *forkp, struct vnode **vpp, int *out_flags);
2d21ac55 325
6d2010ae 326/* Input flags for hfs_getnewvnode */
2d21ac55
A
327
328#define GNV_WANTRSRC 0x01 /* Request the resource fork vnode. */
329#define GNV_SKIPLOCK 0x02 /* Skip taking the cnode lock (when getting resource fork). */
330#define GNV_CREATE 0x04 /* The vnode is for a newly created item. */
6d2010ae 331#define GNV_NOCACHE 0x08 /* Delay entering this item in the name cache */
2d21ac55 332
6d2010ae
A
333/* Output flags for hfs_getnewvnode */
334#define GNV_CHASH_RENAMED 0x01 /* The cnode was renamed in-flight */
335#define GNV_CAT_DELETED 0x02 /* The cnode was deleted from the catalog */
336#define GNV_NEW_CNODE 0x04 /* We are vending out a newly initialized cnode */
337#define GNV_CAT_ATTRCHANGED 0x08 /* Something in struct cat_attr changed in between cat_lookups */
2d21ac55
A
338
339/* Touch cnode times based on c_touch_xxx flags */
91447636 340extern void hfs_touchtimes(struct hfsmount *, struct cnode *);
6d2010ae
A
341extern void hfs_write_dateadded (struct cat_attr *cattrp, u_int32_t dateadded);
342extern u_int32_t hfs_get_dateadded (struct cnode *cp);
343
39236c6e
A
344/* Gen counter methods */
345extern void hfs_write_gencount(struct cat_attr *cattrp, uint32_t gencount);
346extern uint32_t hfs_get_gencount(struct cnode *cp);
347extern uint32_t hfs_incr_gencount (struct cnode *cp);
348
6d2010ae
A
349/* Zero-fill file and push regions out to disk */
350extern int hfs_filedone(struct vnode *vp, vfs_context_t context);
91447636 351
9bccf70c
A
352/*
353 * HFS cnode hash functions.
354 */
355extern void hfs_chashinit(void);
b0d623f7
A
356extern void hfs_chashinit_finish(struct hfsmount *hfsmp);
357extern void hfs_delete_chash(struct hfsmount *hfsmp);
358extern int hfs_chashremove(struct hfsmount *hfsmp, struct cnode *cp);
359extern void hfs_chash_abort(struct hfsmount *hfsmp, struct cnode *cp);
360extern void hfs_chash_rehash(struct hfsmount *hfsmp, struct cnode *cp1, struct cnode *cp2);
361extern void hfs_chashwakeup(struct hfsmount *hfsmp, struct cnode *cp, int flags);
362extern void hfs_chash_mark_in_transit(struct hfsmount *hfsmp, struct cnode *cp);
363
316670eb
A
364extern struct vnode * hfs_chash_getvnode(struct hfsmount *hfsmp, ino_t inum, int wantrsrc,
365 int skiplock, int allow_deleted);
6d2010ae
A
366extern struct cnode * hfs_chash_getcnode(struct hfsmount *hfsmp, ino_t inum, struct vnode **vpp,
367 int wantrsrc, int skiplock, int *out_flags, int *hflags);
ebb1b9f4 368extern int hfs_chash_snoop(struct hfsmount *, ino_t, int, int (*)(const struct cat_desc *,
91447636 369 const struct cat_attr *, void *), void *);
6d2010ae
A
370extern int hfs_valid_cnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp,
371 cnid_t cnid, struct cat_attr *cattr, int *error);
91447636 372
b0d623f7 373extern int hfs_chash_set_childlinkbit(struct hfsmount *hfsmp, cnid_t cnid);
91447636
A
374
375/*
376 * HFS cnode lock functions.
377 *
378 * HFS Locking Order:
379 *
380 * 1. cnode truncate lock (if needed)
39236c6e
A
381 * hfs_vnop_pagein/out can skip grabbing of this lock by flag option by
382 * HFS_LOCK_SKIP_IF_EXCLUSIVE if the truncate lock is already held exclusive
383 * by current thread from an earlier vnop.
91447636
A
384 * 2. cnode lock (in parent-child order if related, otherwise by address order)
385 * 3. journal (if needed)
386 * 4. system files (as needed)
387 * A. Catalog B-tree file
388 * B. Attributes B-tree file
2d21ac55
A
389 * C. Startup file (if there is one)
390 * D. Allocation Bitmap file (always exclusive, supports recursion)
391 * E. Overflow Extents B-tree file (always exclusive, supports recursion)
91447636
A
392 * 5. hfs mount point (always last)
393 *
ebb1b9f4
A
394 *
395 * I. HFS cnode hash lock (must not acquire any new locks while holding this lock, always taken last)
91447636 396 */
39236c6e
A
397
398
399enum hfs_locktype {
400 HFS_SHARED_LOCK = 1,
401 HFS_EXCLUSIVE_LOCK = 2
402};
403
404/* Option flags for cnode and truncate lock functions */
405enum hfs_lockflags {
406 HFS_LOCK_DEFAULT = 0x0, /* Default flag, no options provided */
407 HFS_LOCK_ALLOW_NOEXISTS = 0x1, /* Allow locking of all cnodes, including cnode marked deleted with no catalog entry */
408 HFS_LOCK_SKIP_IF_EXCLUSIVE = 0x2 /* Skip locking if the current thread already holds the lock exclusive */
409};
91447636
A
410#define HFS_SHARED_OWNER (void *)0xffffffff
411
39236c6e
A
412int hfs_lock(struct cnode *, enum hfs_locktype, enum hfs_lockflags);
413int hfs_lockpair(struct cnode *, struct cnode *, enum hfs_locktype);
6d2010ae 414int hfs_lockfour(struct cnode *, struct cnode *, struct cnode *, struct cnode *,
39236c6e 415 enum hfs_locktype, struct cnode **);
91447636 416
6d2010ae
A
417void hfs_unlock(struct cnode *);
418void hfs_unlockpair(struct cnode *, struct cnode *);
419void hfs_unlockfour(struct cnode *, struct cnode *, struct cnode *, struct cnode *);
420
39236c6e
A
421void hfs_lock_truncate(struct cnode *, enum hfs_locktype, enum hfs_lockflags);
422void hfs_unlock_truncate(struct cnode *, enum hfs_lockflags);
423int hfs_try_trunclock(struct cnode *, enum hfs_locktype, enum hfs_lockflags);
9bccf70c
A
424
425#endif /* __APPLE_API_PRIVATE */
426#endif /* KERNEL */
427
428#endif /* ! _HFS_CNODE_H_ */