]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_catalog.h
xnu-1228.5.20.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_catalog.h
1 /*
2 * Copyright (c) 2002-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifndef __HFS_CATALOG__
29 #define __HFS_CATALOG__
30
31 #include <sys/appleapiopts.h>
32
33 #ifdef KERNEL
34 #ifdef __APPLE_API_PRIVATE
35 #include <sys/vnode.h>
36
37 #include <hfs/hfs_format.h>
38
39 /* HFS Catalog */
40
41
42 /*
43 * Catalog ADTs
44 *
45 * The cat_desc, cat_attr, and cat_fork structures are
46 * use to import/export data to/from the Catalog file.
47 * The fields in these structures are always in BSD
48 * runtime format (e.g. dates and names).
49 */
50
51 typedef u_int32_t cnid_t;
52
53 /*
54 * Catalog Node Descriptor (runtime)
55 */
56 struct cat_desc {
57 u_int8_t cd_flags; /* see below (8 bits) */
58 u_int8_t cd_encoding; /* name encoding */
59 int16_t cd_namelen; /* length of cnode name */
60 const u_int8_t * cd_nameptr; /* pointer to cnode name */
61 cnid_t cd_parentcnid; /* parent directory CNID */
62 u_long cd_hint; /* catalog file hint */
63 cnid_t cd_cnid; /* cnode id (for getattrlist) */
64 };
65
66 /* cd_flags */
67 #define CD_HASBUF 0x01 /* allocated filename buffer */
68 #define CD_DECOMPOSED 0x02 /* name is fully decomposed */
69 #define CD_ISMETA 0x40 /* describes a metadata file */
70 #define CD_ISDIR 0x80 /* describes a directory */
71
72 /*
73 * Catalog Node Attributes (runtime)
74 */
75 struct cat_attr {
76 cnid_t ca_fileid; /* inode number (for stat) normally == cnid */
77 mode_t ca_mode; /* file access mode and type (16 bits) */
78 u_int16_t ca_recflags; /* catalog record flags (16 bit integer) */
79 u_int32_t ca_linkcount; /* real hard link count */
80 uid_t ca_uid; /* file owner */
81 gid_t ca_gid; /* file group */
82 union {
83 dev_t cau_rdev; /* special file device (VBLK or VCHAR only) */
84 u_int32_t cau_linkref; /* hardlink reference number */
85 } ca_union1;
86 time_t ca_atime; /* last access time */
87 time_t ca_atimeondisk; /* access time value on disk */
88 time_t ca_mtime; /* last data modification time */
89 time_t ca_ctime; /* last file status change */
90 time_t ca_itime; /* file initialization time */
91 time_t ca_btime; /* last backup time */
92 u_int32_t ca_flags; /* status flags (chflags) */
93 union {
94 u_int32_t cau_blocks; /* total file blocks used (rsrc + data) */
95 u_int32_t cau_entries; /* total directory entries (valence) */
96 } ca_union2;
97 union {
98 u_int32_t cau_dircount; /* count of sub dirs (for posix nlink) */
99 u_int32_t cau_firstlink; /* first hardlink link (files only) */
100 } ca_union3;
101 u_int8_t ca_finderinfo[32]; /* Opaque Finder information */
102 };
103
104 /* Aliases for common fields */
105 #define ca_rdev ca_union1.cau_rdev
106 #define ca_linkref ca_union1.cau_linkref
107 #define ca_blocks ca_union2.cau_blocks
108 #define ca_entries ca_union2.cau_entries
109 #define ca_dircount ca_union3.cau_dircount
110 #define ca_firstlink ca_union3.cau_firstlink
111
112 /*
113 * Catalog Node Fork (runtime)
114 *
115 * NOTE: this is not the same as a struct HFSPlusForkData
116 */
117 struct cat_fork {
118 off_t cf_size; /* fork's logical size in bytes */
119 union {
120 u_int32_t cfu_clump; /* fork's clump size in bytes (sys files only) */
121 u_int64_t cfu_bytesread; /* bytes read from this fork */
122 } cf_union;
123 u_int32_t cf_vblocks; /* virtual (unalloated) blocks */
124 u_int32_t cf_blocks; /* total blocks used by this fork */
125 struct HFSPlusExtentDescriptor cf_extents[8]; /* initial set of extents */
126 };
127
128 #define cf_clump cf_union.cfu_clump
129 #define cf_bytesread cf_union.cfu_bytesread
130
131
132 /*
133 * Directory Hint
134 * Used to hold state across directory enumerations.
135 *
136 */
137 struct directoryhint {
138 TAILQ_ENTRY(directoryhint) dh_link; /* chain */
139 int dh_index; /* index into directory (zero relative) */
140 u_int32_t dh_threadhint; /* node hint of a directory's thread record */
141 u_int32_t dh_time;
142 struct cat_desc dh_desc; /* entry's descriptor */
143 };
144 typedef struct directoryhint directoryhint_t;
145
146 /*
147 * HFS_MAXDIRHINTS cannot be larger than 63 without reducing
148 * HFS_INDEX_BITS, because given the 6-bit tag, at most 63 different
149 * tags can exist. When HFS_MAXDIRHINTS is larger than 63, the same
150 * list may contain dirhints of the same tag, and a staled dirhint may
151 * be returned.
152 */
153 #define HFS_MAXDIRHINTS 32
154 #define HFS_DIRHINT_TTL 45
155
156 #define HFS_INDEX_MASK 0x03ffffff
157 #define HFS_INDEX_BITS 26
158
159
160 /*
161 * Catalog Node Entry
162 *
163 * A cat_entry is used for bulk enumerations (hfs_readdirattr).
164 */
165 struct cat_entry {
166 struct cat_desc ce_desc;
167 struct cat_attr ce_attr;
168 off_t ce_datasize;
169 off_t ce_rsrcsize;
170 u_long ce_datablks;
171 u_long ce_rsrcblks;
172 };
173
174 /*
175 * Starting in 10.5, hfs_vnop_readdirattr() only makes one
176 * call to cat_getentriesattr(). So we increased MAXCATENTRIES
177 * while keeping the total size of the CE LIST buffer <= 8K
178 * (which works out to be 60 entries per call). The 8K limit
179 * keeps the memory coming from a kalloc zone instead of
180 * valuable/fragment-able kernel map space.
181 */
182 #define MAXCATENTRIES \
183 (1 + (8192 - sizeof (struct cat_entrylist)) / sizeof (struct cat_entry))
184
185 /*
186 * Catalog Node Entry List
187 *
188 * A cat_entrylist is a list of Catalog Node Entries.
189 */
190 struct cat_entrylist {
191 u_long maxentries; /* number of entries requested */
192 u_long realentries; /* number of valid entries returned */
193 u_long skipentries; /* number of entries skipped (reserved HFS+ files) */
194 struct cat_entry entry[1]; /* array of entries */
195 };
196
197 #define CE_LIST_SIZE(entries) \
198 sizeof (*ce_list) + (((entries) - 1) * sizeof (struct cat_entry))
199
200
201 /*
202 * Catalog Operations Hint
203 *
204 * lower 16 bits: count of B-tree insert operations
205 * upper 16 bits: count of B-tree delete operations
206 *
207 */
208 #define CAT_DELETE 0x00010000
209 #define CAT_CREATE 0x00000002
210 #define CAT_RENAME 0x00010002
211 #define CAT_EXCHANGE 0x00010002
212
213 typedef u_int32_t catops_t;
214
215 /*
216 * The size of cat_cookie_t much match the size of
217 * the nreserve struct (in BTreeNodeReserve.c).
218 */
219 typedef struct cat_cookie_t {
220 char opaque[24];
221 } cat_cookie_t;
222
223 /* Universal catalog key */
224 union CatalogKey {
225 HFSCatalogKey hfs;
226 HFSPlusCatalogKey hfsPlus;
227 };
228 typedef union CatalogKey CatalogKey;
229
230 /* Universal catalog data record */
231 union CatalogRecord {
232 int16_t recordType;
233 HFSCatalogFolder hfsFolder;
234 HFSCatalogFile hfsFile;
235 HFSCatalogThread hfsThread;
236 HFSPlusCatalogFolder hfsPlusFolder;
237 HFSPlusCatalogFile hfsPlusFile;
238 HFSPlusCatalogThread hfsPlusThread;
239 };
240 typedef union CatalogRecord CatalogRecord;
241
242
243 /*
244 * Catalog Interface
245 *
246 * These functions perform a catalog transactions. The
247 * catalog b-tree is abstracted through this interface.
248 * (please don't go around it)
249 */
250
251 struct hfsmount;
252
253 extern void cat_releasedesc(struct cat_desc *descp);
254
255 extern int cat_create ( struct hfsmount *hfsmp,
256 struct cat_desc *descp,
257 struct cat_attr *attrp,
258 struct cat_desc *out_descp);
259
260 extern int cat_delete ( struct hfsmount *hfsmp,
261 struct cat_desc *descp,
262 struct cat_attr *attrp);
263
264 extern int cat_lookup ( struct hfsmount *hfsmp,
265 struct cat_desc *descp,
266 int wantrsrc,
267 struct cat_desc *outdescp,
268 struct cat_attr *attrp,
269 struct cat_fork *forkp,
270 cnid_t *desc_cnid);
271
272 extern int cat_idlookup (struct hfsmount *hfsmp,
273 cnid_t cnid,
274 int allow_system_files,
275 struct cat_desc *outdescp,
276 struct cat_attr *attrp,
277 struct cat_fork *forkp);
278
279 extern int cat_findname (struct hfsmount *hfsmp,
280 cnid_t cnid,
281 struct cat_desc *outdescp);
282
283 extern int cat_getentriesattr(
284 struct hfsmount *hfsmp,
285 directoryhint_t *dirhint,
286 struct cat_entrylist *ce_list);
287
288 extern int cat_rename ( struct hfsmount * hfsmp,
289 struct cat_desc * from_cdp,
290 struct cat_desc * todir_cdp,
291 struct cat_desc * to_cdp,
292 struct cat_desc * cdp);
293
294 extern int cat_update ( struct hfsmount *hfsmp,
295 struct cat_desc *descp,
296 struct cat_attr *attrp,
297 struct cat_fork *dataforkp,
298 struct cat_fork *rsrcforkp);
299
300 extern int cat_getdirentries(
301 struct hfsmount *hfsmp,
302 int entrycnt,
303 directoryhint_t *dirhint,
304 uio_t uio,
305 int extended,
306 int * items,
307 int * eofflag);
308
309 extern int cat_insertfilethread (
310 struct hfsmount *hfsmp,
311 struct cat_desc *descp);
312
313 extern int cat_preflight(
314 struct hfsmount *hfsmp,
315 catops_t ops,
316 cat_cookie_t *cookie,
317 struct proc *p);
318
319 extern void cat_postflight(
320 struct hfsmount *hfsmp,
321 cat_cookie_t *cookie,
322 struct proc *p);
323
324 extern int cat_binarykeycompare(
325 HFSPlusCatalogKey *searchKey,
326 HFSPlusCatalogKey *trialKey);
327
328 extern int CompareCatalogKeys(
329 HFSCatalogKey *searchKey,
330 HFSCatalogKey *trialKey);
331
332 extern int CompareExtendedCatalogKeys(
333 HFSPlusCatalogKey *searchKey,
334 HFSPlusCatalogKey *trialKey);
335
336 extern void cat_convertattr(
337 struct hfsmount *hfsmp,
338 CatalogRecord * recp,
339 struct cat_attr *attrp,
340 struct cat_fork *datafp,
341 struct cat_fork *rsrcfp);
342
343 extern int cat_convertkey(
344 struct hfsmount *hfsmp,
345 CatalogKey *key,
346 CatalogRecord * recp,
347 struct cat_desc *descp);
348
349 extern int cat_getkeyplusattr(
350 struct hfsmount *hfsmp,
351 cnid_t cnid,
352 CatalogKey *key,
353 struct cat_attr *attrp);
354
355 /* Hard link functions. */
356
357 extern int cat_check_link_ancestry(
358 struct hfsmount *hfsmp,
359 cnid_t parentid,
360 cnid_t pointed_at_cnid);
361
362 extern int cat_set_childlinkbit(
363 struct hfsmount *hfsmp,
364 cnid_t cnid);
365
366 #define HFS_IGNORABLE_LINK 0x00000001
367
368 extern int cat_resolvelink( struct hfsmount *hfsmp,
369 u_long linkref,
370 int isdirlink,
371 struct HFSPlusCatalogFile *recp);
372
373 extern int cat_createlink( struct hfsmount *hfsmp,
374 struct cat_desc *descp,
375 struct cat_attr *attr,
376 cnid_t nextlinkid,
377 cnid_t *linkfileid);
378
379 /* Finder Info's file type and creator for directory hard link alias */
380 enum {
381 kHFSAliasType = 0x66647270, /* 'fdrp' */
382 kHFSAliasCreator = 0x4D414353 /* 'MACS' */
383 };
384
385 extern int cat_deletelink( struct hfsmount *hfsmp,
386 struct cat_desc *descp);
387
388 extern int cat_updatelink( struct hfsmount *hfsmp,
389 cnid_t linkfileid,
390 cnid_t prevlinkid,
391 cnid_t nextlinkid);
392
393 extern int cat_lookuplink( struct hfsmount *hfsmp,
394 struct cat_desc *descp,
395 cnid_t *linkfileid,
396 cnid_t *prevlinkid,
397 cnid_t *nextlinkid);
398
399 extern int cat_lookuplinkbyid( struct hfsmount *hfsmp,
400 cnid_t linkfileid,
401 cnid_t *prevlinkid,
402 cnid_t *nextlinkid);
403
404
405 #endif /* __APPLE_API_PRIVATE */
406 #endif /* KERNEL */
407 #endif /* __HFS_CATALOG__ */