2 * Copyright (c) 2002-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #ifndef __HFS_CATALOG__
29 #define __HFS_CATALOG__
31 #include <sys/appleapiopts.h>
34 #ifdef __APPLE_API_PRIVATE
35 #include <sys/vnode.h>
37 #include <hfs/hfs_format.h>
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).
51 typedef u_int32_t cnid_t
;
54 * Catalog Node Descriptor (runtime)
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 cnid_t cd_parentcnid
; /* parent directory CNID */
61 u_int32_t cd_hint
; /* catalog file hint */
62 cnid_t cd_cnid
; /* cnode id (for getattrlist) */
63 const u_int8_t
* cd_nameptr
; /* pointer to cnode name */
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.
75 #define CD_HASBUF 0x01 /* allocated filename buffer */
76 #define CD_DECOMPOSED 0x02 /* name is fully decomposed */
77 #define CD_EOF 0x04 /* see above */
78 #define CD_ISMETA 0x40 /* describes a metadata file */
79 #define CD_ISDIR 0x80 /* describes a directory */
82 * Catalog Node Attributes (runtime)
85 cnid_t ca_fileid
; /* inode number (for stat) normally == cnid */
86 mode_t ca_mode
; /* file access mode and type (16 bits) */
87 u_int16_t ca_recflags
; /* catalog record flags (16 bit integer) */
88 u_int32_t ca_linkcount
; /* real hard link count */
89 uid_t ca_uid
; /* file owner */
90 gid_t ca_gid
; /* file group */
92 dev_t cau_rdev
; /* special file device (VBLK or VCHAR only) */
93 u_int32_t cau_linkref
; /* hardlink reference number */
95 time_t ca_atime
; /* last access time */
96 time_t ca_atimeondisk
; /* access time value on disk */
97 time_t ca_mtime
; /* last data modification time */
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 */
101 u_int32_t ca_flags
; /* status flags (chflags) */
103 u_int32_t cau_blocks
; /* total file blocks used (rsrc + data) */
104 u_int32_t cau_entries
; /* total directory entries (valence) */
107 u_int32_t cau_dircount
; /* count of sub dirs (for posix nlink) */
108 u_int32_t cau_firstlink
; /* first hardlink link (files only) */
111 u_int8_t ca_finderinfo
[32]; /* Opaque Finder information */
113 FndrFileInfo ca_finderfileinfo
;
114 struct FndrExtendedFileInfo ca_finderextendedfileinfo
;
117 FndrDirInfo ca_finderdirinfo
;
118 struct FndrExtendedDirInfo ca_finderextendeddirinfo
;
123 /* Aliases for common fields */
124 #define ca_rdev ca_union1.cau_rdev
125 #define ca_linkref ca_union1.cau_linkref
126 #define ca_blocks ca_union2.cau_blocks
127 #define ca_entries ca_union2.cau_entries
128 #define ca_dircount ca_union3.cau_dircount
129 #define ca_firstlink ca_union3.cau_firstlink
132 * Catalog Node Fork (runtime)
134 * NOTE: this is not the same as a struct HFSPlusForkData
136 * NOTE: if cf_new_size > cf_size, then a write is in progress and is extending
137 * the EOF; the new EOF will be cf_new_size. Writes and pageouts may validly
138 * write up to cf_new_size, but reads should only read up to cf_size. When
139 * an extending write is not in progress, cf_new_size is zero.
142 off_t cf_size
; /* fork's logical size in bytes */
143 off_t cf_new_size
; /* fork's logical size after write completes */
145 u_int32_t cfu_clump
; /* fork's clump size in bytes (sys files only) */
146 u_int64_t cfu_bytesread
; /* bytes read from this fork */
148 u_int32_t cf_vblocks
; /* virtual (unalloated) blocks */
149 u_int32_t cf_blocks
; /* total blocks used by this fork */
150 struct HFSPlusExtentDescriptor cf_extents
[8]; /* initial set of extents */
153 #define cf_clump cf_union.cfu_clump
154 #define cf_bytesread cf_union.cfu_bytesread
159 * Used to hold state across directory enumerations.
162 struct directoryhint
{
163 TAILQ_ENTRY(directoryhint
) dh_link
; /* chain */
164 int dh_index
; /* index into directory (zero relative) */
165 u_int32_t dh_threadhint
; /* node hint of a directory's thread record */
167 struct cat_desc dh_desc
; /* entry's descriptor */
169 typedef struct directoryhint directoryhint_t
;
172 * HFS_MAXDIRHINTS cannot be larger than 63 without reducing
173 * HFS_INDEX_BITS, because given the 6-bit tag, at most 63 different
174 * tags can exist. When HFS_MAXDIRHINTS is larger than 63, the same
175 * list may contain dirhints of the same tag, and a staled dirhint may
178 #define HFS_MAXDIRHINTS 32
179 #define HFS_DIRHINT_TTL 45
181 #define HFS_INDEX_MASK 0x03ffffff
182 #define HFS_INDEX_BITS 26
188 * A cat_entry is used for bulk enumerations (hfs_readdirattr).
191 struct cat_desc ce_desc
;
192 struct cat_attr ce_attr
;
195 u_int32_t ce_datablks
;
196 u_int32_t ce_rsrcblks
;
200 * Starting in 10.5, hfs_vnop_readdirattr() only makes one
201 * call to cat_getentriesattr(). So we increased MAXCATENTRIES
202 * while keeping the total size of the CE LIST buffer <= 8K
203 * (which works out to be 60 entries per call). The 8K limit
204 * keeps the memory coming from a kalloc zone instead of
205 * valuable/fragment-able kernel map space.
207 #define MAXCATENTRIES \
208 (1 + (8192 - sizeof (struct cat_entrylist)) / sizeof (struct cat_entry))
211 * Catalog Node Entry List
213 * A cat_entrylist is a list of Catalog Node Entries.
215 struct cat_entrylist
{
216 u_int32_t maxentries
; /* number of entries requested */
217 u_int32_t realentries
; /* number of valid entries returned */
218 u_int32_t skipentries
; /* number of entries skipped (reserved HFS+ files) */
219 struct cat_entry entry
[1]; /* array of entries */
222 #define CE_LIST_SIZE(entries) \
223 sizeof (*ce_list) + (((entries) - 1) * sizeof (struct cat_entry))
228 * Catalog FileID/CNID Acquisition / Lookup
230 * Some use-cases require that we find a valid CNID
231 * before we may be ready to enter the item into the namespace.
232 * In order to resolve this, we support a hashtable attached to
233 * the mount that is secured by the catalog lock.
235 * Finding the next valid CNID is easy if the wraparound bit is
236 * not set -- you just pull from the hfsmp next pointer.
237 * If it is set then you must find a free entry in the catalog
238 * and also query the hashtable to see if the item is free or not.
240 * If you want to request a CNID before there is a backing item
241 * in the catalog, you must find one that is valid, then insert
242 * it into the hash table until such time that the item is
243 * inserted into the catalog. After successful catalog insertion,
244 * you must remove the item from the hashtable.
247 typedef struct cat_preflightid
{
249 LIST_ENTRY(cat_preflightid
) id_hash
;
252 extern int cat_remove_idhash (cat_preflightid_t
*preflight
);
253 extern int cat_insert_idhash (struct hfsmount
*hfsmp
, cat_preflightid_t
*preflight
);
254 extern int cat_check_idhash (struct hfsmount
*hfsmp
, cnid_t test_fileid
);
256 /* initialize the id look up hashtable during mount */
257 extern void hfs_idhash_init (struct hfsmount
*hfsmp
);
259 /* release the id lookup hashtable during unmount */
260 extern void hfs_idhash_destroy (struct hfsmount
*hfsmp
);
262 /* Get a new CNID for use */
263 extern int cat_acquire_cnid (struct hfsmount
*hfsmp
, cnid_t
*new_cnid
);
266 /* default size of ID hash is 64 entries */
267 #define HFS_IDHASH_DEFAULT 64
271 * Catalog Operations Hint
273 * lower 16 bits: count of B-tree insert operations
274 * upper 16 bits: count of B-tree delete operations
277 #define CAT_DELETE 0x00010000
278 #define CAT_CREATE 0x00000002
279 #define CAT_RENAME 0x00010002
280 #define CAT_EXCHANGE 0x00010002
282 typedef u_int32_t catops_t
;
285 * The size of cat_cookie_t much match the size of
286 * the nreserve struct (in BTreeNodeReserve.c).
288 typedef struct cat_cookie_t
{
289 #if defined(__LP64__)
296 /* Universal catalog key */
299 HFSPlusCatalogKey hfsPlus
;
301 typedef union CatalogKey CatalogKey
;
303 /* Universal catalog data record */
304 union CatalogRecord
{
306 HFSCatalogFolder hfsFolder
;
307 HFSCatalogFile hfsFile
;
308 HFSCatalogThread hfsThread
;
309 HFSPlusCatalogFolder hfsPlusFolder
;
310 HFSPlusCatalogFile hfsPlusFile
;
311 HFSPlusCatalogThread hfsPlusThread
;
313 typedef union CatalogRecord CatalogRecord
;
315 /* Constants for HFS fork types */
317 kHFSDataForkType
= 0x0, /* data fork */
318 kHFSResourceForkType
= 0xff /* resource fork */
324 * These functions perform a catalog transactions. The
325 * catalog b-tree is abstracted through this interface.
326 * (please don't go around it)
330 extern void cat_releasedesc(struct cat_desc
*descp
);
332 extern int cat_create ( struct hfsmount
*hfsmp
,
334 struct cat_desc
*descp
,
335 struct cat_attr
*attrp
,
336 struct cat_desc
*out_descp
);
338 extern int cat_delete ( struct hfsmount
*hfsmp
,
339 struct cat_desc
*descp
,
340 struct cat_attr
*attrp
);
342 extern int cat_lookup ( struct hfsmount
*hfsmp
,
343 struct cat_desc
*descp
,
345 int force_casesensitive_lookup
,
346 struct cat_desc
*outdescp
,
347 struct cat_attr
*attrp
,
348 struct cat_fork
*forkp
,
351 extern int cat_idlookup (struct hfsmount
*hfsmp
,
353 int allow_system_files
,
355 struct cat_desc
*outdescp
,
356 struct cat_attr
*attrp
,
357 struct cat_fork
*forkp
);
359 extern int cat_findname (struct hfsmount
*hfsmp
,
361 struct cat_desc
*outdescp
);
363 extern int cat_getentriesattr(
364 struct hfsmount
*hfsmp
,
365 directoryhint_t
*dirhint
,
366 struct cat_entrylist
*ce_list
,
369 extern int cat_rename ( struct hfsmount
* hfsmp
,
370 struct cat_desc
* from_cdp
,
371 struct cat_desc
* todir_cdp
,
372 struct cat_desc
* to_cdp
,
373 struct cat_desc
* cdp
);
375 extern int cat_update ( struct hfsmount
*hfsmp
,
376 struct cat_desc
*descp
,
377 struct cat_attr
*attrp
,
378 const struct cat_fork
*dataforkp
,
379 const struct cat_fork
*rsrcforkp
);
381 extern int cat_getdirentries(
382 struct hfsmount
*hfsmp
,
384 directoryhint_t
*dirhint
,
390 extern int cat_insertfilethread (
391 struct hfsmount
*hfsmp
,
392 struct cat_desc
*descp
);
394 extern int cat_preflight(
395 struct hfsmount
*hfsmp
,
397 cat_cookie_t
*cookie
,
400 extern void cat_postflight(
401 struct hfsmount
*hfsmp
,
402 cat_cookie_t
*cookie
,
405 extern int cat_binarykeycompare(
406 HFSPlusCatalogKey
*searchKey
,
407 HFSPlusCatalogKey
*trialKey
);
409 extern int CompareCatalogKeys(
410 HFSCatalogKey
*searchKey
,
411 HFSCatalogKey
*trialKey
);
413 extern int CompareExtendedCatalogKeys(
414 HFSPlusCatalogKey
*searchKey
,
415 HFSPlusCatalogKey
*trialKey
);
417 extern void cat_convertattr(
418 struct hfsmount
*hfsmp
,
419 CatalogRecord
* recp
,
420 struct cat_attr
*attrp
,
421 struct cat_fork
*datafp
,
422 struct cat_fork
*rsrcfp
);
424 extern int cat_convertkey(
425 struct hfsmount
*hfsmp
,
427 CatalogRecord
* recp
,
428 struct cat_desc
*descp
);
430 extern int cat_getkeyplusattr(
431 struct hfsmount
*hfsmp
,
434 struct cat_attr
*attrp
);
436 /* Hard link functions. */
438 extern int cat_check_link_ancestry(
439 struct hfsmount
*hfsmp
,
441 cnid_t pointed_at_cnid
);
443 extern int cat_set_childlinkbit(
444 struct hfsmount
*hfsmp
,
447 #define HFS_IGNORABLE_LINK 0x00000001
449 extern int cat_resolvelink( struct hfsmount
*hfsmp
,
452 struct HFSPlusCatalogFile
*recp
);
454 extern int cat_createlink( struct hfsmount
*hfsmp
,
455 struct cat_desc
*descp
,
456 struct cat_attr
*attr
,
460 /* Finder Info's file type and creator for directory hard link alias */
462 kHFSAliasType
= 0x66647270, /* 'fdrp' */
463 kHFSAliasCreator
= 0x4D414353 /* 'MACS' */
466 extern int cat_deletelink( struct hfsmount
*hfsmp
,
467 struct cat_desc
*descp
);
469 extern int cat_update_siblinglinks( struct hfsmount
*hfsmp
,
474 extern int cat_lookuplink( struct hfsmount
*hfsmp
,
475 struct cat_desc
*descp
,
480 extern int cat_lookup_siblinglinks( struct hfsmount
*hfsmp
,
485 extern int cat_lookup_lastlink( struct hfsmount
*hfsmp
,
488 struct cat_desc
*cdesc
);
490 extern int cat_lookup_dirlink(struct hfsmount
*hfsmp
,
493 struct cat_desc
*outdescp
,
494 struct cat_attr
*attrp
,
495 struct cat_fork
*forkp
);
497 extern int cat_update_dirlink(struct hfsmount
*hfsmp
,
499 struct cat_desc
*descp
,
500 struct cat_attr
*attrp
,
501 struct cat_fork
*rsrcforkp
);
503 #endif /* __APPLE_API_PRIVATE */
505 #endif /* __HFS_CATALOG__ */