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