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