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