]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs.h
721668939660778c3deb89e9c085fa5643bb7b76
[apple/xnu.git] / bsd / hfs / hfs.h
1 /*
2 * Copyright (c) 2000-2003 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 #define HFS_SPARSE_DEV 1
30
31 #include <sys/appleapiopts.h>
32
33 #ifdef KERNEL
34 #ifdef __APPLE_API_PRIVATE
35 #include <sys/param.h>
36 #include <sys/lock.h>
37 #include <sys/queue.h>
38 #include <sys/mount.h>
39 #include <sys/namei.h>
40 #include <sys/vnode.h>
41 #include <sys/quota.h>
42 #include <sys/dirent.h>
43 #include <sys/event.h>
44
45 #include <vfs/vfs_journal.h>
46
47 #include <hfs/hfs_format.h>
48 #include <hfs/hfs_catalog.h>
49 #include <hfs/hfs_cnode.h>
50 #include <hfs/hfs_macos_defs.h>
51 #include <hfs/hfs_encodings.h>
52 #include <hfs/hfs_hotfiles.h>
53
54
55 struct uio; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
56 struct hfslockf; /* For advisory locking */
57
58 /*
59 * Just reported via MIG interface.
60 */
61 #define VERSION_STRING "hfs-2 (4-12-99)"
62
63 #define HFS_LINK_MAX 32767
64
65 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
66
67 // 32 gigs is a "big" file (i.e. one that when deleted
68 // would touch enough data that we should break it into
69 // multiple separate transactions
70 #define HFS_BIGFILE_SIZE (32LL * 1024LL * 1024LL * 1024LL)
71
72
73 enum { kMDBSize = 512 }; /* Size of I/O transfer to read entire MDB */
74
75 enum { kMasterDirectoryBlock = 2 }; /* MDB offset on disk in 512-byte blocks */
76 enum { kMDBOffset = kMasterDirectoryBlock * 512 }; /* MDB offset on disk in bytes */
77
78 enum {
79 kUnknownID = 0,
80 kRootParID = 1,
81 kRootDirID = 2
82 };
83
84 enum {
85 kDataFork,
86 kRsrcFork,
87 kDirectory
88 };
89
90 /* number of locked buffer caches to hold for b-tree meta data */
91 #define kMaxLockedMetaBuffers 32
92
93 /*
94 * File type and creator for symbolic links
95 */
96 enum {
97 kSymLinkFileType = 0x736C6E6B, /* 'slnk' */
98 kSymLinkCreator = 0x72686170 /* 'rhap' */
99 };
100
101
102 extern struct timezone gTimeZone;
103
104
105 /* How many free extents to cache per volume */
106 #define kMaxFreeExtents 10
107
108 /*
109 * HFS_MINFREE gives the minimum acceptable percentage
110 * of file system blocks which may be free (but this
111 * minimum will never exceed HFS_MAXRESERVE bytes). If
112 * the free block count drops below this level only the
113 * superuser may continue to allocate blocks.
114 */
115 #define HFS_MINFREE 1
116 #define HFS_MAXRESERVE ((u_int64_t)(250*1024*1024))
117
118 /*
119 * The system distinguishes between the desirable low-disk
120 * notifiaction levels for root volumes and non-root volumes.
121 * The various thresholds are computed as a fraction of the
122 * volume size, all capped at a certain fixed level
123 */
124
125 #define HFS_ROOTLOWDISKTRIGGERFRACTION 5
126 #define HFS_ROOTLOWDISKTRIGGERLEVEL ((u_int64_t)(250*1024*1024))
127 #define HFS_ROOTLOWDISKSHUTOFFFRACTION 6
128 #define HFS_ROOTLOWDISKSHUTOFFLEVEL ((u_int64_t)(375*1024*1024))
129
130 #define HFS_LOWDISKTRIGGERFRACTION 1
131 #define HFS_LOWDISKTRIGGERLEVEL ((u_int64_t)(50*1024*1024))
132 #define HFS_LOWDISKSHUTOFFFRACTION 2
133 #define HFS_LOWDISKSHUTOFFLEVEL ((u_int64_t)(75*1024*1024))
134
135 /* Internal Data structures*/
136
137 struct vcb_t {
138 u_int16_t vcbSigWord;
139 int16_t vcbAtrb;
140 int16_t vcbFlags;
141 int16_t vcbspare;
142 u_int32_t vcbJinfoBlock;
143
144 u_int32_t vcbCrDate;
145 u_int32_t vcbLsMod;
146 u_int32_t vcbVolBkUp;
147
148 int32_t vcbFilCnt;
149 int32_t vcbDirCnt;
150 u_int32_t blockSize; /* size of allocation blocks */
151 u_int32_t totalBlocks; /* total allocation blocks */
152 u_int32_t freeBlocks; /* free allocation blocks */
153 u_int32_t nextAllocation; /* start of next allocation search */
154 int32_t vcbClpSiz;
155 u_int32_t vcbNxtCNID;
156 u_int32_t vcbCNIDGen;
157 int32_t vcbWrCnt;
158
159 int32_t vcbFndrInfo[8];
160
161 u_int64_t encodingsBitmap; /* HFS Plus only */
162
163 u_int16_t vcbNmFls; /* HFS only */
164 u_int16_t vcbNmRtDirs; /* HFS only */
165 int16_t vcbVBMSt; /* HFS only */
166 int16_t vcbAlBlSt; /* HFS only */
167
168 struct vnode * extentsRefNum;
169 struct vnode * catalogRefNum;
170 struct vnode * allocationsRefNum;
171
172 u_int8_t vcbVN[256]; /* volume name in UTF-8 */
173 u_int32_t volumeNameEncodingHint;
174 u_int32_t hfsPlusIOPosOffset; /* Disk block where HFS+ starts */
175 u_int32_t vcbVBMIOSize; /* volume bitmap I/O size */
176
177 /* cache of largest known free extents */
178 u_int32_t vcbFreeExtCnt;
179 HFSPlusExtentDescriptor vcbFreeExt[kMaxFreeExtents];
180
181 u_int32_t reserveBlocks; /* free block reserve */
182 u_int32_t loanedBlocks; /* blocks on loan for delayed allocations */
183
184 u_int32_t localCreateDate; /* creation times for HFS+ volumes are in local time */
185 simple_lock_data_t vcbSimpleLock; /* simple lock to allow concurrent access to vcb data */
186 };
187 typedef struct vcb_t ExtendedVCB;
188
189
190 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
191
192 /* XXX */
193 #define MARK_VOLUMEDAMAGED(fcb)
194
195 /*
196 * NOTE: The code relies on being able to cast an ExtendedVCB* to a vfsVCB* in order
197 * to gain access to the mount point pointer from a pointer
198 * to an ExtendedVCB. DO NOT INSERT OTHER FIELDS BEFORE THE vcb FIELD!!
199 *
200 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
201 * are locked by the hfs_lock simple lock.
202 */
203 typedef struct vfsVCB {
204 ExtendedVCB vcb_vcb;
205 struct hfsmount *vcb_hfsmp; /* Pointer to hfsmount structure */
206 } vfsVCB_t;
207
208
209
210 /* This structure describes the HFS specific mount structure data. */
211 typedef struct hfsmount {
212 u_int32_t hfs_flags; /* see below */
213
214 /* Physical Description */
215 u_long hfs_phys_block_count; /* Num of PHYSICAL blocks of volume */
216 u_long hfs_phys_block_size; /* Always a multiple of 512 */
217
218 /* Access to VFS and devices */
219 struct mount *hfs_mp; /* filesystem vfs structure */
220 struct vnode *hfs_devvp; /* block device mounted vnode */
221 dev_t hfs_raw_dev; /* device mounted */
222 struct netexport hfs_export; /* Export information */
223 u_int32_t hfs_logBlockSize; /* Size of buffer cache buffer for I/O */
224
225 /* Default values for HFS standard and non-init access */
226 uid_t hfs_uid; /* uid to set as owner of the files */
227 gid_t hfs_gid; /* gid to set as owner of the files */
228 mode_t hfs_dir_mask; /* mask to and with directory protection bits */
229 mode_t hfs_file_mask; /* mask to and with file protection bits */
230 u_long hfs_encoding; /* Defualt encoding for non hfs+ volumes */
231
232 /* HFS Specific */
233 struct vfsVCB hfs_vcb;
234 struct cat_desc hfs_privdir_desc;
235 struct cat_attr hfs_privdir_attr;
236 u_int32_t hfs_metadata_createdate;
237 hfs_to_unicode_func_t hfs_get_unicode;
238 unicode_to_hfs_func_t hfs_get_hfsname;
239
240 /* Quota variables: */
241 struct quotafile hfs_qfiles[MAXQUOTAS]; /* quota files */
242
243 /* Journaling variables: */
244 void *jnl; // the journal for this volume (if one exists)
245 struct vnode *jvp; // device where the journal lives (may be equal to devvp)
246 u_int32_t jnl_start; // start block of the journal file (so we don't delete it)
247 u_int32_t jnl_size;
248 u_int32_t hfs_jnlfileid;
249 u_int32_t hfs_jnlinfoblkid;
250 volatile int readers;
251 volatile int blocker;
252
253 /* Notification variables: */
254 unsigned long hfs_notification_conditions;
255 u_int32_t hfs_freespace_notify_warninglimit;
256 u_int32_t hfs_freespace_notify_desiredlevel;
257
258 /* Metadata allocation zone variables: */
259 u_int32_t hfs_metazone_start;
260 u_int32_t hfs_metazone_end;
261 u_int32_t hfs_hotfile_start;
262 u_int32_t hfs_hotfile_end;
263 int hfs_hotfile_freeblks;
264 int hfs_hotfile_maxblks;
265 int hfs_overflow_maxblks;
266 int hfs_catalog_maxblks;
267
268 /* Hot File Clustering variables: */
269 enum hfc_stage hfc_stage; /* what are we up to... */
270 time_t hfc_timebase; /* recording period start time */
271 time_t hfc_timeout; /* recording period stop time */
272 void * hfc_recdata; /* recording data (opaque) */
273 int hfc_maxfiles; /* maximum files to track */
274 struct vnode * hfc_filevp;
275
276 #ifdef HFS_SPARSE_DEV
277 /* Sparse device variables: */
278 struct vnode * hfs_backingfs_rootvp;
279 int hfs_sparsebandblks;
280 #endif
281 } hfsmount_t;
282
283
284 /* HFS mount point flags */
285 #define HFS_READ_ONLY 0x001
286 #define HFS_UNKNOWN_PERMS 0x002
287 #define HFS_WRITEABLE_MEDIA 0x004
288 #define HFS_CLEANED_ORPHANS 0x008
289 #define HFS_X 0x010
290 #define HFS_CASE_SENSITIVE 0x020
291 #define HFS_STANDARD 0x040
292 #define HFS_METADATA_ZONE 0x080
293 #define HFS_FRAGMENTED_FREESPACE 0x100
294 #define HFS_NEED_JNL_RESET 0x200
295
296 #ifdef HFS_SPARSE_DEV
297 #define HFS_HAS_SPARSE_DEVICE 0x400
298 #endif
299
300
301 #define hfs_global_shared_lock_acquire(hfsmp) \
302 do { \
303 if (hfsmp->blocker) { \
304 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
305 continue; \
306 } \
307 hfsmp->readers++; \
308 break; \
309 } while (1)
310
311 #define hfs_global_shared_lock_release(hfsmp) \
312 do { \
313 hfsmp->readers--; \
314 if (hfsmp->readers == 0) { \
315 wakeup((caddr_t)&hfsmp->readers); \
316 } \
317 } while (0)
318
319 #define hfs_global_exclusive_lock_acquire(hfsmp) \
320 do { \
321 if (hfsmp->blocker) { \
322 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
323 continue; \
324 } \
325 if (hfsmp->readers != 0) { \
326 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
327 continue; \
328 } \
329 hfsmp->blocker = 1; \
330 break; \
331 } while (1)
332
333 #define hfs_global_exclusive_lock_release(hfsmp) \
334 hfsmp->blocker = 0; \
335 wakeup((caddr_t)&hfsmp->blocker)
336
337 #define MAXHFSVNODELEN 31
338
339
340 typedef struct filefork FCB;
341
342
343 #define MAKE_INODE_NAME(name,linkno) \
344 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
345
346
347 /* structure to hold a "." or ".." directory entry (12 bytes) */
348 typedef struct hfsdotentry {
349 u_int32_t d_fileno; /* unique file number */
350 u_int16_t d_reclen; /* length of this structure */
351 u_int8_t d_type; /* dirent file type */
352 u_int8_t d_namelen; /* len of filename */
353 char d_name[4]; /* "." or ".." */
354 } hfsdotentry;
355
356 #define HFS_AVERAGE_NAME_SIZE 22
357 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
358 #define MAX_HFSDIRENTRY_SIZE sizeof(struct dirent)
359
360 #define DIRENTRY_SIZE(namlen) \
361 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
362
363
364
365 enum { kHFSPlusMaxFileNameBytes = kHFSPlusMaxFileNameChars * 3 };
366
367 enum { kdirentMaxNameBytes = NAME_MAX };
368
369
370 /* macro to determine if hfs or hfsplus */
371 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
372 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
373
374
375 /*
376 * Various ways to acquire a VFS mount point pointer:
377 */
378 #define VTOVFS(VP) ((VP)->v_mount)
379 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
380 #define VCBTOVFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp->hfs_mp)
381
382 /*
383 * Various ways to acquire an HFS mount point pointer:
384 */
385 #define VTOHFS(VP) ((struct hfsmount *)((VP)->v_mount->mnt_data))
386 #define VFSTOHFS(MP) ((struct hfsmount *)(MP)->mnt_data)
387 #define VCBTOHFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp)
388 #define FCBTOHFS(FCB) ((struct hfsmount *)(FCB)->ff_cp->c_vp->v_mount->mnt_data)
389
390 /*
391 * Various ways to acquire a VCB pointer:
392 */
393 #define VTOVCB(VP) (&(((struct hfsmount *)((VP)->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
394 #define VFSTOVCB(MP) (&(((struct hfsmount *)(MP)->mnt_data)->hfs_vcb.vcb_vcb))
395 #define HFSTOVCB(HFSMP) (&(HFSMP)->hfs_vcb.vcb_vcb)
396 #define FCBTOVCB(FCB) (&(((struct hfsmount *)((FCB)->ff_cp->c_vp->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
397
398
399 #define HFS_KNOTE(vp, hint) KNOTE(&VTOC(vp)->c_knotes, (hint))
400
401
402 #define E_NONE 0
403 #define kHFSBlockSize 512
404
405 /*
406 * Macros for getting the MDB/VH sector and offset
407 */
408 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
409 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
410
411 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
412 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
413
414 /*
415 * This is the straight GMT conversion constant:
416 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
417 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
418 */
419 #define MAC_GMT_FACTOR 2082844800UL
420
421
422 u_int32_t to_bsd_time(u_int32_t hfs_time);
423 u_int32_t to_hfs_time(u_int32_t bsd_time);
424
425 int hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush);
426 #define HFS_ALTFLUSH 1
427
428 extern int hfsUnmount(struct hfsmount *hfsmp, struct proc *p);
429
430
431 extern int hfs_getcnode(struct hfsmount *hfsmp, cnid_t cnid, struct cat_desc *descp,
432 int wantrsrc, struct cat_attr *attrp, struct cat_fork *forkp,
433 struct vnode **vpp);
434
435 extern int hfs_getnewvnode(struct hfsmount *hfsmp, struct cnode *cp,
436 struct cat_desc *descp, int wantrsrc, struct cat_attr *attrp,
437 struct cat_fork *forkp, struct vnode **vpp);
438
439 extern int hfs_metafilelocking(struct hfsmount *hfsmp, u_long fileID, u_int flags, struct proc *p);
440
441 extern u_int32_t hfs_freeblks(struct hfsmount * hfsmp, int wantreserve);
442
443 extern void hfs_remove_orphans(struct hfsmount *);
444
445
446 short MacToVFSError(OSErr err);
447
448 extern int hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, struct ucred *cred,
449 struct proc *p, int invokesuperuserstatus);
450
451 u_long FindMetaDataDirectory(ExtendedVCB *vcb);
452
453 #define kMaxSecsForFsync 5
454 #define HFS_SYNCTRANS 1
455
456 extern int hfs_btsync(struct vnode *vp, int sync_transaction);
457 // used as a callback by the journaling code
458 extern void hfs_sync_metadata(void *arg);
459
460 short make_dir_entry(FCB **fileptr, char *name, u_int32_t fileID);
461
462
463 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize,
464 unsigned long blockSizeLimit,
465 unsigned long baseMultiple);
466
467 OSErr hfs_MountHFSVolume(struct hfsmount *hfsmp, HFSMasterDirectoryBlock *mdb,
468 struct proc *p);
469 OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
470 off_t embeddedOffset, u_int64_t disksize, struct proc *p, void *args);
471
472 extern int hfs_early_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
473 void *_args, int embeddedOffset, int mdb_offset,
474 HFSMasterDirectoryBlock *mdbp, struct ucred *cred);
475 extern u_long GetFileInfo(ExtendedVCB *vcb, u_int32_t dirid, char *name,
476 struct cat_attr *fattr, struct cat_fork *forkinfo);
477
478 int hfs_getconverter(u_int32_t encoding, hfs_to_unicode_func_t *get_unicode,
479 unicode_to_hfs_func_t *get_hfsname);
480
481 int hfs_relconverter(u_int32_t encoding);
482
483 int hfs_to_utf8(ExtendedVCB *vcb, Str31 hfs_str, ByteCount maxDstLen,
484 ByteCount *actualDstLen, unsigned char* dstStr);
485
486 int utf8_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, const unsigned char* srcStr,
487 Str31 dstStr);
488
489 int mac_roman_to_utf8(Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen,
490 unsigned char* dstStr);
491
492 int utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr);
493
494 u_int32_t hfs_pickencoding(const u_int16_t *src, int len);
495
496 enum volop {VOL_UPDATE, VOL_MKDIR, VOL_RMDIR, VOL_MKFILE, VOL_RMFILE};
497
498 extern int hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot);
499
500 extern void hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding);
501
502
503 extern void replace_desc(struct cnode *cp, struct cat_desc *cdp);
504
505 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
506
507 extern int hfs_virtualmetafile(struct cnode *);
508
509 void hfs_generate_volume_notifications(struct hfsmount *hfsmp);
510
511
512 #endif /* __APPLE_API_PRIVATE */
513 #endif /* KERNEL */
514 #endif /* __HFS__ */