]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_cnode.h
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_cnode.h
CommitLineData
9bccf70c 1/*
b0d623f7 2 * Copyright (c) 2002-2008 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
9bccf70c
A
48
49/*
50 * The filefork is used to represent an HFS file fork (data or resource).
51 * Reading or writing any of these fields requires holding cnode lock.
52 */
53struct filefork {
91447636
A
54 struct cnode *ff_cp; /* cnode associated with this fork */
55 struct rl_head ff_invalidranges; /* Areas of disk that should read back as zeroes */
9bccf70c 56 union {
91447636
A
57 void *ffu_sysfileinfo; /* additional info for system files */
58 char *ffu_symlinkptr; /* symbolic link pathname */
59 } ff_union;
60 struct cat_fork ff_data; /* fork data (size, extents) */
9bccf70c 61};
55e303ae 62typedef struct filefork filefork_t;
9bccf70c
A
63
64/* Aliases for common fields */
91447636 65#define ff_size ff_data.cf_size
593a1d5f 66#define ff_new_size ff_data.cf_new_size
91447636
A
67#define ff_clumpsize ff_data.cf_clump
68#define ff_bytesread ff_data.cf_bytesread
69#define ff_blocks ff_data.cf_blocks
70#define ff_extents ff_data.cf_extents
55e303ae
A
71#define ff_unallocblocks ff_data.cf_vblocks
72
91447636
A
73#define ff_symlinkptr ff_union.ffu_symlinkptr
74#define ff_sysfileinfo ff_union.ffu_sysfileinfo
9bccf70c
A
75
76
77/* The btree code still needs these... */
91447636
A
78#define fcbEOF ff_size
79#define fcbExtents ff_extents
80#define fcbBTCBPtr ff_sysfileinfo
9bccf70c 81
91447636 82typedef u_int8_t atomicflag_t;
2d21ac55
A
83
84
85/*
86 * Hardlink Origin (for hardlinked directories).
87 */
88struct linkorigin {
89 TAILQ_ENTRY(linkorigin) lo_link; /* chain */
90 void * lo_thread; /* thread that performed the lookup */
91 cnid_t lo_cnid; /* hardlink's cnid */
92 cnid_t lo_parentcnid; /* hardlink's parent cnid */
93};
94typedef struct linkorigin linkorigin_t;
95
96#define MAX_CACHED_ORIGINS 10
935ed37a 97#define MAX_CACHED_FILE_ORIGINS 8
2d21ac55 98
9bccf70c
A
99/*
100 * The cnode is used to represent each active (or recently active)
101 * file or directory in the HFS filesystem.
102 *
103 * Reading or writing any of these fields requires holding c_lock.
104 */
105struct cnode {
91447636
A
106 lck_rw_t c_rwlock; /* cnode's lock */
107 void * c_lockowner; /* cnode's lock owner (exclusive case only) */
108 lck_rw_t c_truncatelock; /* protects file from truncation during read/write */
9bccf70c
A
109 LIST_ENTRY(cnode) c_hash; /* cnode's hash chain */
110 u_int32_t c_flag; /* cnode's runtime flags */
91447636 111 u_int32_t c_hflag; /* cnode's flags for maintaining hash - protected by global hash lock */
9bccf70c
A
112 struct vnode *c_vp; /* vnode for data fork or dir */
113 struct vnode *c_rsrc_vp; /* vnode for resource fork */
b0d623f7 114 struct dquot *c_dquot[MAXQUOTAS]; /* cnode's quota info */
2d21ac55
A
115 u_int32_t c_childhint; /* catalog hint for children (small dirs only) */
116 u_int32_t c_dirthreadhint; /* catalog hint for directory's thread rec */
9bccf70c
A
117 struct cat_desc c_desc; /* cnode's descriptor */
118 struct cat_attr c_attr; /* cnode's attributes */
2d21ac55
A
119 TAILQ_HEAD(hfs_originhead, linkorigin) c_originlist; /* hardlink origin cache */
120 TAILQ_HEAD(hfs_hinthead, directoryhint) c_hintlist; /* readdir directory hint list */
91447636
A
121 int16_t c_dirhinttag; /* directory hint tag */
122 union {
123 int16_t cu_dirhintcnt; /* directory hint count */
124 int16_t cu_syslockcount; /* system file use only */
125 } c_union;
2d21ac55 126 u_int32_t c_dirchangecnt; /* changes each insert/delete (in-core only) */
9bccf70c
A
127 struct filefork *c_datafork; /* cnode's data fork */
128 struct filefork *c_rsrcfork; /* cnode's rsrc fork */
91447636
A
129 atomicflag_t c_touch_acctime;
130 atomicflag_t c_touch_chgtime;
131 atomicflag_t c_touch_modtime;
b0d623f7
A
132#if HFS_COMPRESSION
133 decmpfs_cnode *c_decmp;
134#endif /* HFS_COMPRESSION */
9bccf70c 135};
55e303ae 136typedef struct cnode cnode_t;
9bccf70c
A
137
138/* Aliases for common cnode fields */
139#define c_cnid c_desc.cd_cnid
140#define c_hint c_desc.cd_hint
141#define c_parentcnid c_desc.cd_parentcnid
142#define c_encoding c_desc.cd_encoding
143
144#define c_fileid c_attr.ca_fileid
145#define c_mode c_attr.ca_mode
2d21ac55 146#define c_linkcount c_attr.ca_linkcount
9bccf70c
A
147#define c_uid c_attr.ca_uid
148#define c_gid c_attr.ca_gid
2d21ac55 149#define c_rdev c_attr.ca_union1.cau_rdev
9bccf70c
A
150#define c_atime c_attr.ca_atime
151#define c_mtime c_attr.ca_mtime
9bccf70c
A
152#define c_ctime c_attr.ca_ctime
153#define c_itime c_attr.ca_itime
154#define c_btime c_attr.ca_btime
155#define c_flags c_attr.ca_flags
156#define c_finderinfo c_attr.ca_finderinfo
2d21ac55
A
157#define c_blocks c_attr.ca_union2.cau_blocks
158#define c_entries c_attr.ca_union2.cau_entries
9bccf70c
A
159#define c_zftimeout c_childhint
160
91447636
A
161#define c_dirhintcnt c_union.cu_dirhintcnt
162#define c_syslockcount c_union.cu_syslockcount
9bccf70c 163
55e303ae 164
91447636
A
165/* hash maintenance flags kept in c_hflag and protected by hfs_chash_mutex */
166#define H_ALLOC 0x00001 /* CNode is being allocated */
167#define H_ATTACH 0x00002 /* CNode is being attached to by another vnode */
168#define H_TRANSIT 0x00004 /* CNode is getting recycled */
169#define H_WAITING 0x00008 /* CNode is being waited for */
170
9bccf70c 171
91447636
A
172/* Runtime cnode flags (kept in c_flag) */
173#define C_NEED_RVNODE_PUT 0x00001 /* Need to do a vnode_put on c_rsrc_vp after the unlock */
174#define C_NEED_DVNODE_PUT 0x00002 /* Need to do a vnode_put on c_vp after the unlock */
175#define C_ZFWANTSYNC 0x00004 /* fsync requested and file has holes */
176#define C_FROMSYNC 0x00008 /* fsync was called from sync */
9bccf70c 177
91447636
A
178#define C_MODIFIED 0x00010 /* CNode has been modified */
179#define C_NOEXISTS 0x00020 /* CNode has been deleted, catalog entry is gone */
180#define C_DELETED 0x00040 /* CNode has been marked to be deleted */
2d21ac55 181#define C_HARDLINK 0x00080 /* CNode is a hard link (file or dir) */
9bccf70c 182
91447636
A
183#define C_FORCEUPDATE 0x00100 /* force the catalog entry update */
184#define C_HASXATTRS 0x00200 /* cnode has extended attributes */
2d21ac55
A
185#define C_NEG_ENTRIES 0x00400 /* directory has negative name entries */
186#define C_WARNED_RSRC 0x00800 /* cnode lookup warning has been issued */
9bccf70c 187
0c530ab8
A
188#define C_NEED_DATA_SETSIZE 0x01000 /* Do a ubc_setsize(0) on c_rsrc_vp after the unlock */
189#define C_NEED_RSRC_SETSIZE 0x02000 /* Do a ubc_setsize(0) on c_vp after the unlock */
2d21ac55 190#define C_DIR_MODIFICATION 0x04000 /* Directory is being modified, wait for lookups */
b0d623f7 191#define C_ALWAYS_ZEROFILL 0x08000 /* Always zero-fill the file on an fsync */
9bccf70c
A
192
193#define ZFTIMELIMIT (5 * 60)
194
2d21ac55
A
195/*
196 * The following is the "invisible" bit from the fdFlags field
197 * in the FndrFileInfo.
198 */
199enum { kFinderInvisibleMask = 1 << 14 };
200
201
9bccf70c
A
202/*
203 * Convert between cnode pointers and vnode pointers
204 */
91447636 205#define VTOC(vp) ((struct cnode *)vnode_fsnode((vp)))
9bccf70c
A
206
207#define CTOV(cp,rsrc) (((rsrc) && S_ISREG((cp)->c_mode)) ? \
208 (cp)->c_rsrc_vp : (cp)->c_vp)
209
210/*
211 * Convert between vnode pointers and file forks
212 *
213 * Note: no CTOF since that is ambiguous
214 */
215
216#define FTOC(fp) ((fp)->ff_cp)
217
218#define VTOF(vp) ((vp) == VTOC((vp))->c_rsrc_vp ? \
219 VTOC((vp))->c_rsrcfork : \
220 VTOC((vp))->c_datafork)
221
2d21ac55
A
222#define VCTOF(vp, cp) ((vp) == (cp)->c_rsrc_vp ? \
223 (cp)->c_rsrcfork : \
224 (cp)->c_datafork)
225
9bccf70c
A
226#define FTOV(fp) ((fp) == FTOC(fp)->c_rsrcfork ? \
227 FTOC(fp)->c_rsrc_vp : \
228 FTOC(fp)->c_vp)
229
b7266188
A
230/*
231 * This is a helper function used for determining whether or not a cnode has become open
232 * unlinked in between the time we acquired its vnode and the time we acquire the cnode lock
233 * to start manipulating it. Due to the SMP nature of VFS, it is probably necessary to
234 * use this macro every time we acquire a cnode lock, as the content of the Cnode may have
235 * been modified in betweeen the lookup and a VNOP. Whether or not to call this is dependent
236 * upon the VNOP in question. Sometimes it is OK to use an open-unlinked file, for example, in,
237 * reading. But other times, such as on the source of a VNOP_RENAME, it should be disallowed.
238 */
239int hfs_checkdeleted (struct cnode *cp);
55e303ae 240
9bccf70c
A
241/*
242 * Test for a resource fork
243 */
244#define FORK_IS_RSRC(fp) ((fp) == FTOC(fp)->c_rsrcfork)
245
246#define VNODE_IS_RSRC(vp) ((vp) == VTOC((vp))->c_rsrc_vp)
247
b0d623f7
A
248#if HFS_COMPRESSION
249/*
250 * VTOCMP(vp) returns a pointer to vp's decmpfs_cnode; this could be NULL
251 * if the file is not compressed or if hfs_file_is_compressed() hasn't
252 * yet been called on this file.
253 */
254#define VTOCMP(vp) (VTOC((vp))->c_decmp)
255int hfs_file_is_compressed(struct cnode *cp, int skiplock);
256int hfs_uncompressed_size_of_compressed_file(struct hfsmount *hfsmp, struct vnode *vp, cnid_t fid, off_t *size, int skiplock);
257int hfs_hides_rsrc(vfs_context_t ctx, struct cnode *cp, int skiplock);
258int hfs_hides_xattr(vfs_context_t ctx, struct cnode *cp, const char *name, int skiplock);
259#endif
9bccf70c 260
55e303ae 261#define ATIME_ONDISK_ACCURACY 300
9bccf70c 262
91447636
A
263
264/* This overlays the FileID portion of NFS file handles. */
9bccf70c 265struct hfsfid {
9bccf70c
A
266 u_int32_t hfsfid_cnid; /* Catalog node ID. */
267 u_int32_t hfsfid_gen; /* Generation number (create date). */
268};
269
270
2d21ac55
A
271/* Get new default vnode */
272extern int hfs_getnewvnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp,
273 struct cat_desc *descp, int flags, struct cat_attr *attrp,
274 struct cat_fork *forkp, struct vnode **vpp);
275
276
277#define GNV_WANTRSRC 0x01 /* Request the resource fork vnode. */
278#define GNV_SKIPLOCK 0x02 /* Skip taking the cnode lock (when getting resource fork). */
279#define GNV_CREATE 0x04 /* The vnode is for a newly created item. */
280
281
282/* Touch cnode times based on c_touch_xxx flags */
91447636
A
283extern void hfs_touchtimes(struct hfsmount *, struct cnode *);
284
9bccf70c
A
285/*
286 * HFS cnode hash functions.
287 */
288extern void hfs_chashinit(void);
b0d623f7
A
289extern void hfs_chashinit_finish(struct hfsmount *hfsmp);
290extern void hfs_delete_chash(struct hfsmount *hfsmp);
291extern int hfs_chashremove(struct hfsmount *hfsmp, struct cnode *cp);
292extern void hfs_chash_abort(struct hfsmount *hfsmp, struct cnode *cp);
293extern void hfs_chash_rehash(struct hfsmount *hfsmp, struct cnode *cp1, struct cnode *cp2);
294extern void hfs_chashwakeup(struct hfsmount *hfsmp, struct cnode *cp, int flags);
295extern void hfs_chash_mark_in_transit(struct hfsmount *hfsmp, struct cnode *cp);
296
297extern struct vnode * hfs_chash_getvnode(struct hfsmount *hfsmp, ino_t inum, int wantrsrc, int skiplock);
298extern struct cnode * hfs_chash_getcnode(struct hfsmount *hfsmp, ino_t inum, struct vnode **vpp, int wantrsrc, int skiplock);
299extern int hfs_chash_snoop(struct hfsmount *, ino_t, int (*)(const struct cat_desc *,
91447636 300 const struct cat_attr *, void *), void *);
2d21ac55 301extern int hfs_valid_cnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp, cnid_t cnid);
91447636 302
b0d623f7 303extern int hfs_chash_set_childlinkbit(struct hfsmount *hfsmp, cnid_t cnid);
91447636
A
304
305/*
306 * HFS cnode lock functions.
307 *
308 * HFS Locking Order:
309 *
310 * 1. cnode truncate lock (if needed)
311 * 2. cnode lock (in parent-child order if related, otherwise by address order)
312 * 3. journal (if needed)
313 * 4. system files (as needed)
314 * A. Catalog B-tree file
315 * B. Attributes B-tree file
2d21ac55
A
316 * C. Startup file (if there is one)
317 * D. Allocation Bitmap file (always exclusive, supports recursion)
318 * E. Overflow Extents B-tree file (always exclusive, supports recursion)
91447636
A
319 * 5. hfs mount point (always last)
320 *
321 */
322enum hfslocktype {HFS_SHARED_LOCK = 1, HFS_EXCLUSIVE_LOCK = 2, HFS_FORCE_LOCK = 3};
323#define HFS_SHARED_OWNER (void *)0xffffffff
324
325extern int hfs_lock(struct cnode *, enum hfslocktype);
326extern int hfs_lockpair(struct cnode *, struct cnode *, enum hfslocktype);
327extern int hfs_lockfour(struct cnode *, struct cnode *, struct cnode *, struct cnode *,
b0d623f7 328 enum hfslocktype, struct cnode **);
91447636
A
329
330extern void hfs_unlock(struct cnode *);
331extern void hfs_unlockpair(struct cnode *, struct cnode *);
332extern void hfs_unlockfour(struct cnode *, struct cnode *, struct cnode *, struct cnode *);
333
334extern void hfs_lock_truncate(struct cnode *, int);
2d21ac55 335extern void hfs_unlock_truncate(struct cnode *, int);
9bccf70c
A
336
337#endif /* __APPLE_API_PRIVATE */
338#endif /* KERNEL */
339
340#endif /* ! _HFS_CNODE_H_ */