]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs.h
xnu-344.32.tar.gz
[apple/xnu.git] / bsd / hfs / hfs.h
CommitLineData
1c79356b 1/*
9bccf70c 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
de355530
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
de355530
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
de355530
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
1c79356b
A
22
23#ifndef __HFS__
24#define __HFS__
25
9bccf70c
A
26#include <sys/appleapiopts.h>
27
28#ifdef KERNEL
29#ifdef __APPLE_API_PRIVATE
1c79356b
A
30#include <sys/param.h>
31#include <sys/lock.h>
32#include <sys/queue.h>
9bccf70c
A
33#include <sys/mount.h>
34#include <sys/namei.h>
35#include <sys/vnode.h>
36#include <sys/quota.h>
1c79356b
A
37#include <sys/dirent.h>
38
b4c24cb9
A
39#include <vfs/vfs_journal.h>
40
1c79356b 41#include <hfs/hfs_format.h>
9bccf70c
A
42#include <hfs/hfs_catalog.h>
43#include <hfs/hfs_cnode.h>
1c79356b
A
44#include <hfs/hfs_macos_defs.h>
45#include <hfs/hfs_encodings.h>
46
47
9bccf70c
A
48struct uio; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
49struct hfslockf; /* For advisory locking */
1c79356b
A
50
51/*
52 * Just reported via MIG interface.
53 */
54#define VERSION_STRING "hfs-2 (4-12-99)"
55
56#define HFS_LINK_MAX 32767
57
9bccf70c
A
58#define HFS_MAX_DEFERED_ALLOC (1024*1024)
59
1c79356b
A
60
61enum { kMDBSize = 512 }; /* Size of I/O transfer to read entire MDB */
62
63enum { kMasterDirectoryBlock = 2 }; /* MDB offset on disk in 512-byte blocks */
64enum { kMDBOffset = kMasterDirectoryBlock * 512 }; /* MDB offset on disk in bytes */
65
66enum {
67 kUnknownID = 0,
68 kRootParID = 1,
69 kRootDirID = 2
70};
71
72enum {
1c79356b
A
73 kDataFork,
74 kRsrcFork,
9bccf70c 75 kDirectory
1c79356b
A
76};
77
0b4e3aa0
A
78/* number of locked buffer caches to hold for b-tree meta data */
79#define kMaxLockedMetaBuffers 32
1c79356b
A
80
81/*
82 * File type and creator for symbolic links
83 */
84enum {
9bccf70c
A
85 kSymLinkFileType = 0x736C6E6B, /* 'slnk' */
86 kSymLinkCreator = 0x72686170 /* 'rhap' */
1c79356b
A
87};
88
1c79356b
A
89
90extern struct timezone gTimeZone;
91
1c79356b 92
0b4e3aa0
A
93/* How many free extents to cache per volume */
94#define kMaxFreeExtents 10
95
9bccf70c
A
96/*
97 * HFS_MINFREE gives the minimum acceptable percentage
98 * of file system blocks which may be free (but this
99 * minimum will never exceed HFS_MAXRESERVE bytes). If
100 * the free block count drops below this level only the
101 * superuser may continue to allocate blocks.
102 */
103#define HFS_MINFREE 1
104#define HFS_MAXRESERVE (u_int64_t)(250*1024*1024)
105
1c79356b
A
106/* Internal Data structures*/
107
108struct vcb_t {
1c79356b 109 u_int16_t vcbSigWord;
1c79356b 110 int16_t vcbAtrb;
9bccf70c
A
111 int16_t vcbFlags;
112 int16_t vcbspare;
b4c24cb9 113 u_int32_t vcbJinfoBlock;
0b4e3aa0
A
114
115 u_int32_t vcbCrDate;
116 u_int32_t vcbLsMod;
1c79356b 117 u_int32_t vcbVolBkUp;
0b4e3aa0 118
1c79356b
A
119 int32_t vcbFilCnt;
120 int32_t vcbDirCnt;
9bccf70c
A
121 u_int32_t blockSize; /* size of allocation blocks */
122 u_int32_t totalBlocks; /* total allocation blocks */
123 u_int32_t freeBlocks; /* free allocation blocks */
124 u_int32_t nextAllocation; /* start of next allocation search */
0b4e3aa0
A
125 int32_t vcbClpSiz;
126 u_int32_t vcbNxtCNID;
9bccf70c
A
127 u_int32_t vcbCNIDGen;
128 int32_t vcbWrCnt;
0b4e3aa0 129
1c79356b 130 int32_t vcbFndrInfo[8];
0b4e3aa0
A
131
132 u_int64_t encodingsBitmap; /* HFS Plus only */
133
9bccf70c 134 u_int16_t vcbNmFls; /* HFS only */
0b4e3aa0 135 u_int16_t vcbNmRtDirs; /* HFS only */
9bccf70c
A
136 int16_t vcbVBMSt; /* HFS only */
137 int16_t vcbAlBlSt; /* HFS only */
0b4e3aa0 138
1c79356b
A
139 struct vnode * extentsRefNum;
140 struct vnode * catalogRefNum;
141 struct vnode * allocationsRefNum;
0b4e3aa0 142
9bccf70c 143 u_int8_t vcbVN[256]; /* volume name in UTF-8 */
0b4e3aa0 144 u_int32_t volumeNameEncodingHint;
9bccf70c 145 u_int32_t hfsPlusIOPosOffset; /* Disk block where HFS+ starts */
0b4e3aa0 146 u_int32_t vcbVBMIOSize; /* volume bitmap I/O size */
0b4e3aa0 147
9bccf70c
A
148 /* cache of largest known free extents */
149 u_int32_t vcbFreeExtCnt;
150 HFSPlusExtentDescriptor vcbFreeExt[kMaxFreeExtents];
151
152 u_int32_t reserveBlocks; /* free block reserve */
153 u_int32_t loanedBlocks; /* blocks on loan for delayed allocations */
0b4e3aa0 154
1c79356b
A
155 u_int32_t localCreateDate; /* creation times for HFS+ volumes are in local time */
156 simple_lock_data_t vcbSimpleLock; /* simple lock to allow concurrent access to vcb data */
157};
158typedef struct vcb_t ExtendedVCB;
159
0b4e3aa0 160
9bccf70c 161#define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
1c79356b 162
9bccf70c
A
163/* XXX */
164#define MARK_VOLUMEDAMAGED(fcb)
1c79356b
A
165
166/*
167 * NOTE: The code relies on being able to cast an ExtendedVCB* to a vfsVCB* in order
168 * to gain access to the mount point pointer from a pointer
169 * to an ExtendedVCB. DO NOT INSERT OTHER FIELDS BEFORE THE vcb FIELD!!
170 *
171 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
172 * are locked by the hfs_lock simple lock.
173 */
174typedef struct vfsVCB {
175 ExtendedVCB vcb_vcb;
176 struct hfsmount *vcb_hfsmp; /* Pointer to hfsmount structure */
177} vfsVCB_t;
178
179
180
181/* This structure describes the HFS specific mount structure data. */
182typedef struct hfsmount {
1c79356b
A
183 u_int8_t hfs_fs_ronly; /* Whether this was mounted as read-initially */
184 u_int8_t hfs_unknownpermissions; /* Whether this was mounted with MNT_UNKNOWNPERMISSIONS */
9bccf70c 185 u_int8_t hfs_media_writeable;
b4c24cb9 186 u_int8_t hfs_orphans_cleaned;
1c79356b
A
187
188 /* Physical Description */
189 u_long hfs_phys_block_count; /* Num of PHYSICAL blocks of volume */
190 u_long hfs_phys_block_size; /* Always a multiple of 512 */
191
192 /* Access to VFS and devices */
193 struct mount *hfs_mp; /* filesystem vfs structure */
194 struct vnode *hfs_devvp; /* block device mounted vnode */
195 dev_t hfs_raw_dev; /* device mounted */
196 struct netexport hfs_export; /* Export information */
197 u_int32_t hfs_logBlockSize; /* Size of buffer cache buffer for I/O */
198
199 /* Default values for HFS standard and non-init access */
200 uid_t hfs_uid; /* uid to set as owner of the files */
201 gid_t hfs_gid; /* gid to set as owner of the files */
202 mode_t hfs_dir_mask; /* mask to and with directory protection bits */
203 mode_t hfs_file_mask; /* mask to and with file protection bits */
204 u_long hfs_encoding; /* Defualt encoding for non hfs+ volumes */
205
206 /* simple lock for shared meta renaming */
207 simple_lock_data_t hfs_renamelock;
208
209 /* HFS Specific */
210 struct vfsVCB hfs_vcb;
9bccf70c
A
211 struct cat_desc hfs_privdir_desc;
212 struct cat_attr hfs_privdir_attr;
1c79356b
A
213 u_int32_t hfs_metadata_createdate;
214 hfs_to_unicode_func_t hfs_get_unicode;
215 unicode_to_hfs_func_t hfs_get_hfsname;
9bccf70c
A
216
217 struct quotafile hfs_qfiles[MAXQUOTAS]; /* quota files */
b4c24cb9
A
218
219 // XXXdbg
220 void *jnl; // the journal for this volume (if one exists)
221 struct vnode *jvp; // device where the journal lives (may be equal to devvp)
222 u_int32_t jnl_start; // start block of the journal file (so we don't delete it)
223 u_int32_t hfs_jnlfileid;
224 u_int32_t hfs_jnlinfoblkid;
225 volatile int readers;
226 volatile int blocker;
1c79356b
A
227} hfsmount_t;
228
9bccf70c 229#define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid
1c79356b 230
b4c24cb9
A
231#define hfs_global_shared_lock_acquire(hfsmp) \
232 do { \
233 if (hfsmp->blocker) { \
234 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
235 continue; \
236 } \
237 hfsmp->readers++; \
238 break; \
239 } while (1)
240
241#define hfs_global_shared_lock_release(hfsmp) \
242 do { \
243 hfsmp->readers--; \
244 if (hfsmp->readers == 0) { \
245 wakeup((caddr_t)&hfsmp->readers); \
246 } \
247 } while (0)
248
249#define hfs_global_exclusive_lock_acquire(hfsmp) \
250 do { \
251 if (hfsmp->blocker) { \
252 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
253 continue; \
254 } \
255 if (hfsmp->readers != 0) { \
256 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
257 continue; \
258 } \
259 hfsmp->blocker = 1; \
260 break; \
261 } while (1)
262
263#define hfs_global_exclusive_lock_release(hfsmp) \
264 hfsmp->blocker = 0; \
265 wakeup((caddr_t)&hfsmp->blocker)
266
1c79356b 267#define MAXHFSVNODELEN 31
1c79356b 268
1c79356b 269
9bccf70c 270typedef struct filefork FCB;
1c79356b 271
1c79356b
A
272
273#define MAKE_INODE_NAME(name,linkno) \
274 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
275
1c79356b
A
276/*
277 * Write check macro
278 */
279#define WRITE_CK(VNODE, FUNC_NAME) { \
280 if ((VNODE)->v_mount->mnt_flag & MNT_RDONLY) { \
281 DBG_ERR(("%s: ATTEMPT TO WRITE A READONLY VOLUME\n", \
282 FUNC_NAME)); \
283 return(EROFS); \
284 } \
285}
286
1c79356b
A
287/* structure to hold a "." or ".." directory entry (12 bytes) */
288typedef struct hfsdotentry {
289 u_int32_t d_fileno; /* unique file number */
290 u_int16_t d_reclen; /* length of this structure */
291 u_int8_t d_type; /* dirent file type */
292 u_int8_t d_namelen; /* len of filename */
293 char d_name[4]; /* "." or ".." */
294} hfsdotentry;
295
9bccf70c
A
296#define HFS_AVERAGE_NAME_SIZE 22
297#define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
1c79356b
A
298#define MAX_HFSDIRENTRY_SIZE sizeof(struct dirent)
299
300#define DIRENTRY_SIZE(namlen) \
301 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
302
1c79356b
A
303
304enum {
305 kCatalogFolderNode = 1,
306 kCatalogFileNode = 2
307};
308
309/*
310 * CatalogNodeData has same layout as the on-disk HFS Plus file/dir records.
311 * Classic hfs file/dir records are converted to match this layout.
312 *
313 * The cnd_extra padding allows big hfs plus thread records (520 bytes max)
314 * to be read onto this stucture during a cnid lookup.
315 *
1c79356b
A
316 */
317struct CatalogNodeData {
318 int16_t cnd_type;
319 u_int16_t cnd_flags;
320 u_int32_t cnd_valence; /* dirs only */
321 u_int32_t cnd_nodeID;
322 u_int32_t cnd_createDate;
323 u_int32_t cnd_contentModDate;
324 u_int32_t cnd_attributeModDate;
325 u_int32_t cnd_accessDate;
326 u_int32_t cnd_backupDate;
327 u_int32_t cnd_ownerID;
328 u_int32_t cnd_groupID;
329 u_int8_t cnd_adminFlags; /* super-user changeable flags */
330 u_int8_t cnd_ownerFlags; /* owner changeable flags */
331 u_int16_t cnd_mode; /* file type + permission bits */
332 union {
333 u_int32_t cndu_iNodeNum; /* indirect links only */
334 u_int32_t cndu_linkCount; /* indirect nodes only */
335 u_int32_t cndu_rawDevice; /* special files (FBLK and FCHR) only */
336 } cnd_un;
337 u_int8_t cnd_finderInfo[32];
338 u_int32_t cnd_textEncoding;
339 u_int32_t cnd_reserved;
340 HFSPlusForkData cnd_datafork;
341 HFSPlusForkData cnd_rsrcfork;
342 u_int32_t cnd_iNodeNumCopy;
765c9de3
A
343 u_int32_t cnd_linkCNID; /* for hard links only */
344 u_int8_t cnd_extra[264]; /* make struct at least 520 bytes long */
1c79356b
A
345};
346typedef struct CatalogNodeData CatalogNodeData;
347
348#define cnd_iNodeNum cnd_un.cndu_iNodeNum
349#define cnd_linkCount cnd_un.cndu_linkCount
350#define cnd_rawDevice cnd_un.cndu_rawDevice
351
9bccf70c 352
1c79356b
A
353
354enum { kHFSPlusMaxFileNameBytes = kHFSPlusMaxFileNameChars * 3 };
355
356enum { kdirentMaxNameBytes = NAME_MAX };
357
1c79356b
A
358
359/* macro to determine if hfs or hfsplus */
360#define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
361#define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
362
363
1c79356b
A
364/*
365 * Various ways to acquire a VFS mount point pointer:
366 */
367#define VTOVFS(VP) ((VP)->v_mount)
1c79356b
A
368#define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
369#define VCBTOVFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp->hfs_mp)
370
371/*
372 * Various ways to acquire an HFS mount point pointer:
373 */
374#define VTOHFS(VP) ((struct hfsmount *)((VP)->v_mount->mnt_data))
1c79356b
A
375#define VFSTOHFS(MP) ((struct hfsmount *)(MP)->mnt_data)
376#define VCBTOHFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp)
b4c24cb9 377#define FCBTOHFS(FCB) ((struct hfsmount *)(FCB)->ff_cp->c_vp->v_mount->mnt_data)
1c79356b
A
378
379/*
380 * Various ways to acquire a VCB pointer:
381 */
382#define VTOVCB(VP) (&(((struct hfsmount *)((VP)->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
1c79356b
A
383#define VFSTOVCB(MP) (&(((struct hfsmount *)(MP)->mnt_data)->hfs_vcb.vcb_vcb))
384#define HFSTOVCB(HFSMP) (&(HFSMP)->hfs_vcb.vcb_vcb)
b4c24cb9 385#define FCBTOVCB(FCB) (&(((struct hfsmount *)((FCB)->ff_cp->c_vp->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
1c79356b
A
386
387
388#define E_NONE 0
389#define kHFSBlockSize 512
1c79356b 390
9bccf70c
A
391/*
392 * Macros for getting the MDB/VH sector and offset
393 */
394#define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
395#define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
d52fe63f
A
396
397#define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
398#define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
399
1c79356b
A
400/*
401 * This is the straight GMT conversion constant:
402 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
403 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
404 */
405#define MAC_GMT_FACTOR 2082844800UL
406
1c79356b
A
407
408u_int32_t to_bsd_time(u_int32_t hfs_time);
409u_int32_t to_hfs_time(u_int32_t bsd_time);
410
9bccf70c
A
411int hfs_flushfiles(struct mount *mp, int flags, struct proc *p);
412int hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush);
413#define HFS_ALTFLUSH 1
1c79356b 414
1c79356b
A
415short hfsUnmount(struct hfsmount *hfsmp, struct proc *p);
416
9bccf70c
A
417
418extern int hfs_getcnode(struct hfsmount *hfsmp, cnid_t cnid, struct cat_desc *descp,
419 int wantrsrc, struct cat_attr *attrp, struct cat_fork *forkp,
420 struct vnode **vpp);
421
422extern int hfs_getnewvnode(struct hfsmount *hfsmp, struct cnode *cp,
423 struct cat_desc *descp, int wantrsrc, struct cat_attr *attrp,
424 struct cat_fork *forkp, struct vnode **vpp);
425
1c79356b 426extern int hfs_metafilelocking(struct hfsmount *hfsmp, u_long fileID, u_int flags, struct proc *p);
1c79356b 427
9bccf70c
A
428extern u_int32_t hfs_freeblks(struct hfsmount * hfsmp, int wantreserve);
429
b4c24cb9
A
430extern void hfs_remove_orphans(struct hfsmount *);
431
1c79356b
A
432
433short MacToVFSError(OSErr err);
1c79356b 434
9bccf70c
A
435extern int hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, struct ucred *cred,
436 struct proc *p, int invokesuperuserstatus);
437
1c79356b
A
438u_long FindMetaDataDirectory(ExtendedVCB *vcb);
439
9bccf70c
A
440#define kMaxSecsForFsync 5
441#define HFS_SYNCTRANS 1
442
443extern int hfs_btsync(struct vnode *vp, int sync_transaction);
b4c24cb9
A
444// used as a callback by the journaling code
445extern void hfs_sync_metadata(void *arg);
1c79356b
A
446
447short make_dir_entry(FCB **fileptr, char *name, u_int32_t fileID);
448
9bccf70c 449
1c79356b
A
450unsigned long BestBlockSizeFit(unsigned long allocationBlockSize,
451 unsigned long blockSizeLimit,
452 unsigned long baseMultiple);
453
454OSErr hfs_MountHFSVolume(struct hfsmount *hfsmp, HFSMasterDirectoryBlock *mdb,
d52fe63f 455 struct proc *p);
1c79356b 456OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
b4c24cb9
A
457 off_t embeddedOffset, u_int64_t disksize, struct proc *p, void *args);
458
459extern int hfs_early_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
460 void *_args, int embeddedOffset, int mdb_offset,
461 HFSMasterDirectoryBlock *mdbp, struct ucred *cred);
462extern u_long GetFileInfo(ExtendedVCB *vcb, u_int32_t dirid, char *name,
463 struct cat_attr *fattr, struct cat_fork *forkinfo);
1c79356b
A
464
465int hfs_getconverter(u_int32_t encoding, hfs_to_unicode_func_t *get_unicode,
466 unicode_to_hfs_func_t *get_hfsname);
467
468int hfs_relconverter(u_int32_t encoding);
469
470int hfs_to_utf8(ExtendedVCB *vcb, Str31 hfs_str, ByteCount maxDstLen,
471 ByteCount *actualDstLen, unsigned char* dstStr);
472
473int utf8_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, const unsigned char* srcStr,
474 Str31 dstStr);
475
476int mac_roman_to_utf8(Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen,
477 unsigned char* dstStr);
478
479int utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr);
480
0b4e3aa0
A
481u_int32_t hfs_pickencoding(const u_int16_t *src, int len);
482
9bccf70c
A
483enum volop {VOL_UPDATE, VOL_MKDIR, VOL_RMDIR, VOL_MKFILE, VOL_RMFILE};
484
485extern int hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot);
486
487extern void hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding);
488
489
490extern void replace_desc(struct cnode *cp, struct cat_desc *cdp);
491
d7e50217
A
492extern int hfs_namecmp(const char *, size_t, const char *, size_t);
493
494
9bccf70c
A
495#endif /* __APPLE_API_PRIVATE */
496#endif /* KERNEL */
1c79356b 497#endif /* __HFS__ */