]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs.h
d10297f97b45df42da28845fd5ee3e0ddad285fd
[apple/xnu.git] / bsd / hfs / hfs.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #ifndef __HFS__
32 #define __HFS__
33
34 #define HFS_SPARSE_DEV 1
35
36 #include <sys/appleapiopts.h>
37
38 #ifdef KERNEL
39 #ifdef __APPLE_API_PRIVATE
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/quota.h>
45 #include <sys/dirent.h>
46 #include <sys/event.h>
47
48 #include <kern/locks.h>
49
50 #include <vfs/vfs_journal.h>
51
52 #include <hfs/hfs_format.h>
53 #include <hfs/hfs_catalog.h>
54 #include <hfs/hfs_cnode.h>
55 #include <hfs/hfs_macos_defs.h>
56 #include <hfs/hfs_encodings.h>
57 #include <hfs/hfs_hotfiles.h>
58
59 /*
60 * Just reported via MIG interface.
61 */
62 #define VERSION_STRING "hfs-2 (4-12-99)"
63
64 #define HFS_LINK_MAX 32767
65
66 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
67
68 // 32 gigs is a "big" file (i.e. one that when deleted
69 // would touch enough data that we should break it into
70 // multiple separate transactions
71 #define HFS_BIGFILE_SIZE (32LL * 1024LL * 1024LL * 1024LL)
72
73
74 enum { kMDBSize = 512 }; /* Size of I/O transfer to read entire MDB */
75
76 enum { kMasterDirectoryBlock = 2 }; /* MDB offset on disk in 512-byte blocks */
77 enum { kMDBOffset = kMasterDirectoryBlock * 512 }; /* MDB offset on disk in bytes */
78
79 #define kRootDirID kHFSRootFolderID
80
81
82 /* number of locked buffer caches to hold for b-tree meta data */
83 #define kMaxLockedMetaBuffers 32
84
85 /*
86 * File type and creator for symbolic links
87 */
88 enum {
89 kSymLinkFileType = 0x736C6E6B, /* 'slnk' */
90 kSymLinkCreator = 0x72686170 /* 'rhap' */
91 };
92
93
94 extern struct timezone gTimeZone;
95
96
97 /* How many free extents to cache per volume */
98 #define kMaxFreeExtents 10
99
100 /*
101 * HFS_MINFREE gives the minimum acceptable percentage
102 * of file system blocks which may be free (but this
103 * minimum will never exceed HFS_MAXRESERVE bytes). If
104 * the free block count drops below this level only the
105 * superuser may continue to allocate blocks.
106 */
107 #define HFS_MINFREE 1
108 #define HFS_MAXRESERVE ((u_int64_t)(250*1024*1024))
109
110 /*
111 * The system distinguishes between the desirable low-disk
112 * notifiaction levels for root volumes and non-root volumes.
113 * The various thresholds are computed as a fraction of the
114 * volume size, all capped at a certain fixed level
115 */
116
117 #define HFS_ROOTLOWDISKTRIGGERFRACTION 5
118 #define HFS_ROOTLOWDISKTRIGGERLEVEL ((u_int64_t)(250*1024*1024))
119 #define HFS_ROOTLOWDISKSHUTOFFFRACTION 6
120 #define HFS_ROOTLOWDISKSHUTOFFLEVEL ((u_int64_t)(375*1024*1024))
121
122 #define HFS_LOWDISKTRIGGERFRACTION 1
123 #define HFS_LOWDISKTRIGGERLEVEL ((u_int64_t)(50*1024*1024))
124 #define HFS_LOWDISKSHUTOFFFRACTION 2
125 #define HFS_LOWDISKSHUTOFFLEVEL ((u_int64_t)(75*1024*1024))
126
127 /* Internal Data structures*/
128
129
130 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
131
132 /* XXX */
133 #define MARK_VOLUMEDAMAGED(fcb)
134
135
136 /* This structure describes the HFS specific mount structure data. */
137 typedef struct hfsmount {
138 u_int32_t hfs_flags; /* see below */
139
140 /* Physical Description */
141 u_long hfs_phys_block_size; /* Always a multiple of 512 */
142 daddr64_t hfs_phys_block_count; /* Num of PHYSICAL blocks of volume */
143 daddr64_t hfs_alt_id_sector; /* location of alternate VH/MDB */
144
145 /* Access to VFS and devices */
146 struct mount *hfs_mp; /* filesystem vfs structure */
147 struct vnode *hfs_devvp; /* block device mounted vnode */
148 struct vnode * hfs_extents_vp;
149 struct vnode * hfs_catalog_vp;
150 struct vnode * hfs_allocation_vp;
151 struct vnode * hfs_attribute_vp;
152 dev_t hfs_raw_dev; /* device mounted */
153 u_int32_t hfs_logBlockSize; /* Size of buffer cache buffer for I/O */
154
155 /* Default values for HFS standard and non-init access */
156 uid_t hfs_uid; /* uid to set as owner of the files */
157 gid_t hfs_gid; /* gid to set as owner of the files */
158 mode_t hfs_dir_mask; /* mask to and with directory protection bits */
159 mode_t hfs_file_mask; /* mask to and with file protection bits */
160 u_long hfs_encoding; /* Defualt encoding for non hfs+ volumes */
161
162 /* Persistent fields (on disk, dynamic) */
163 time_t hfs_mtime; /* file system last modification time */
164 u_int32_t hfs_filecount; /* number of files in file system */
165 u_int32_t hfs_dircount; /* number of directories in file system */
166 u_int32_t freeBlocks; /* free allocation blocks */
167 u_int32_t nextAllocation; /* start of next allocation search */
168 u_int32_t vcbNxtCNID; /* next unused catalog node ID */
169 u_int32_t vcbWrCnt; /* file system write count */
170 u_int64_t encodingsBitmap; /* in-use encodings */
171 u_int16_t vcbNmFls; /* HFS Only - root dir file count */
172 u_int16_t vcbNmRtDirs; /* HFS Only - root dir directory count */
173
174 /* Persistent fields (on disk, static) */
175 u_int16_t vcbSigWord;
176 int16_t vcbFlags;
177 u_int32_t vcbAtrb;
178 u_int32_t vcbJinfoBlock;
179 time_t hfs_itime; /* file system creation time */
180 time_t hfs_btime; /* file system last backup time */
181 u_int32_t blockSize; /* size of allocation blocks */
182 u_int32_t totalBlocks; /* total allocation blocks */
183 int32_t vcbClpSiz;
184 u_int32_t vcbFndrInfo[8];
185 int16_t vcbVBMSt; /* HFS only */
186 int16_t vcbAlBlSt; /* HFS only */
187
188 /* vcb stuff */
189 u_int8_t vcbVN[256]; /* volume name in UTF-8 */
190 u_int32_t volumeNameEncodingHint;
191 u_int32_t hfsPlusIOPosOffset; /* Disk block where HFS+ starts */
192 u_int32_t vcbVBMIOSize; /* volume bitmap I/O size */
193
194 /* cache of largest known free extents */
195 u_int32_t vcbFreeExtCnt;
196 HFSPlusExtentDescriptor vcbFreeExt[kMaxFreeExtents];
197
198 u_int32_t reserveBlocks; /* free block reserve */
199 u_int32_t loanedBlocks; /* blocks on loan for delayed allocations */
200
201 u_int32_t localCreateDate; /* creation times for HFS+ volumes are in local time */
202 struct cat_desc hfs_privdir_desc;
203 struct cat_attr hfs_privdir_attr;
204 u_int32_t hfs_metadata_createdate;
205 hfs_to_unicode_func_t hfs_get_unicode;
206 unicode_to_hfs_func_t hfs_get_hfsname;
207
208 /* Quota variables: */
209 struct quotafile hfs_qfiles[MAXQUOTAS]; /* quota files */
210
211 /* Journaling variables: */
212 void *jnl; // the journal for this volume (if one exists)
213 struct vnode *jvp; // device where the journal lives (may be equal to devvp)
214 u_int32_t jnl_start; // start block of the journal file (so we don't delete it)
215 u_int32_t jnl_size;
216 u_int32_t hfs_jnlfileid;
217 u_int32_t hfs_jnlinfoblkid;
218 lck_rw_t hfs_global_lock;
219 u_int32_t hfs_global_lock_nesting;
220
221 /* Notification variables: */
222 unsigned long hfs_notification_conditions;
223 u_int32_t hfs_freespace_notify_warninglimit;
224 u_int32_t hfs_freespace_notify_desiredlevel;
225
226 /* time mounted and last mounted mod time "snapshot" */
227 time_t hfs_mount_time;
228 time_t hfs_last_mounted_mtime;
229
230 /* Metadata allocation zone variables: */
231 u_int32_t hfs_metazone_start;
232 u_int32_t hfs_metazone_end;
233 u_int32_t hfs_hotfile_start;
234 u_int32_t hfs_hotfile_end;
235 int hfs_hotfile_freeblks;
236 int hfs_hotfile_maxblks;
237 int hfs_overflow_maxblks;
238 int hfs_catalog_maxblks;
239
240 /* Hot File Clustering variables: */
241 lck_mtx_t hfc_mutex; /* serialize hot file stages */
242 enum hfc_stage hfc_stage; /* what are we up to... */
243 time_t hfc_timebase; /* recording period start time */
244 time_t hfc_timeout; /* recording period stop time */
245 void * hfc_recdata; /* recording data (opaque) */
246 int hfc_maxfiles; /* maximum files to track */
247 struct vnode * hfc_filevp;
248
249 #ifdef HFS_SPARSE_DEV
250 /* Sparse device variables: */
251 struct vnode * hfs_backingfs_rootvp;
252 int hfs_sparsebandblks;
253 #endif
254 size_t hfs_max_inline_attrsize;
255
256 lck_mtx_t hfs_mutex; /* protects access to hfsmount data */
257 void *hfs_freezing_proc; /* who froze the fs */
258 lck_rw_t hfs_insync; /* protects sync/freeze interaction */
259
260 /* Resize variables: */
261 u_int32_t hfs_resize_filesmoved;
262 u_int32_t hfs_resize_totalfiles;
263 } hfsmount_t;
264
265 typedef hfsmount_t ExtendedVCB;
266
267 /* Aliases for legacy field names */
268 #define vcbCrDate hfs_itime
269 #define vcbLsMod hfs_mtime
270 #define vcbVolBkUp hfs_btime
271 #define extentsRefNum hfs_extents_vp
272 #define catalogRefNum hfs_catalog_vp
273 #define allocationsRefNum hfs_allocation_vp
274 #define vcbFilCnt hfs_filecount
275 #define vcbDirCnt hfs_dircount
276
277
278 /* HFS mount point flags */
279 #define HFS_READ_ONLY 0x001
280 #define HFS_UNKNOWN_PERMS 0x002
281 #define HFS_WRITEABLE_MEDIA 0x004
282 #define HFS_CLEANED_ORPHANS 0x008
283 #define HFS_X 0x010
284 #define HFS_CASE_SENSITIVE 0x020
285 #define HFS_STANDARD 0x040
286 #define HFS_METADATA_ZONE 0x080
287 #define HFS_FRAGMENTED_FREESPACE 0x100
288 #define HFS_NEED_JNL_RESET 0x200
289 #define HFS_HAS_SPARSE_DEVICE 0x400
290 #define HFS_RESIZE_IN_PROGRESS 0x800
291
292
293 #define HFS_MOUNT_LOCK(hfsmp, metadata) \
294 { \
295 if ((metadata) && 1) \
296 lck_mtx_lock(&(hfsmp)->hfs_mutex); \
297 } \
298
299 #define HFS_MOUNT_UNLOCK(hfsmp, metadata) \
300 { \
301 if ((metadata) && 1) \
302 lck_mtx_unlock(&(hfsmp)->hfs_mutex); \
303 } \
304
305 #define hfs_global_exclusive_lock_acquire(hfsmp) lck_rw_lock_exclusive(&(hfsmp)->hfs_global_lock)
306 #define hfs_global_exclusive_lock_release(hfsmp) lck_rw_done(&(hfsmp)->hfs_global_lock)
307
308
309 #define MAXHFSVNODELEN 31
310
311
312 typedef struct filefork FCB;
313
314
315 #define MAKE_INODE_NAME(name,linkno) \
316 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
317
318 #define HFS_INODE_PREFIX_LEN 5
319
320
321 #define HFS_AVERAGE_NAME_SIZE 22
322 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
323
324 #define STD_DIRENT_LEN(namlen) \
325 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
326
327 #define EXT_DIRENT_LEN(namlen) \
328 ((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 3) & ~3)
329
330
331 enum { kHFSPlusMaxFileNameBytes = kHFSPlusMaxFileNameChars * 3 };
332
333
334 /* macro to determine if hfs or hfsplus */
335 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
336 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
337
338
339 /*
340 * Various ways to acquire a VFS mount point pointer:
341 */
342 #define VTOVFS(VP) vnode_mount((VP))
343 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
344 #define VCBTOVFS(VCB) HFSTOVFS(VCB)
345
346 /*
347 * Various ways to acquire an HFS mount point pointer:
348 */
349 #define VTOHFS(VP) ((struct hfsmount *)vfs_fsprivate(vnode_mount((VP))))
350 #define VFSTOHFS(MP) ((struct hfsmount *)vfs_fsprivate((MP)))
351 #define VCBTOHFS(VCB) (VCB)
352 #define FCBTOHFS(FCB) ((struct hfsmount *)vfs_fsprivate(vnode_mount((FCB)->ff_cp->c_vp)))
353
354 /*
355 * Various ways to acquire a VCB (legacy) pointer:
356 */
357 #define VTOVCB(VP) VTOHFS(VP)
358 #define VFSTOVCB(MP) VFSTOHFS(MP)
359 #define HFSTOVCB(HFSMP) (HFSMP)
360 #define FCBTOVCB(FCB) FCBTOHFS(FCB)
361
362
363 #define HFS_KNOTE(vp, hint) KNOTE(&VTOC(vp)->c_knotes, (hint))
364
365
366 #define E_NONE 0
367 #define kHFSBlockSize 512
368
369 /*
370 * Macros for getting the MDB/VH sector and offset
371 */
372 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
373 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
374
375 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
376 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
377
378
379 /*
380 * HFS specific fcntl()'s
381 */
382 #define HFS_BULKACCESS (FCNTL_FS_SPECIFIC_BASE + 0x00001)
383 #define HFS_GET_MOUNT_TIME (FCNTL_FS_SPECIFIC_BASE + 0x00002)
384 #define HFS_GET_LAST_MTIME (FCNTL_FS_SPECIFIC_BASE + 0x00003)
385 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
386 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
387
388
389 /*
390 * This is the straight GMT conversion constant:
391 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
392 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
393 */
394 #define MAC_GMT_FACTOR 2082844800UL
395
396
397 time_t to_bsd_time(u_int32_t hfs_time);
398 u_int32_t to_hfs_time(time_t bsd_time);
399
400 int hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush);
401 #define HFS_ALTFLUSH 1
402
403 extern int hfsUnmount(struct hfsmount *hfsmp, struct proc *p);
404
405 extern int hfs_getnewvnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp,
406 struct cat_desc *descp, int wantrsrc, struct cat_attr *attrp,
407 struct cat_fork *forkp, struct vnode **vpp);
408
409 extern u_int32_t hfs_freeblks(struct hfsmount * hfsmp, int wantreserve);
410
411 extern void hfs_remove_orphans(struct hfsmount *);
412
413
414 short MacToVFSError(OSErr err);
415
416 extern int hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, struct ucred *cred,
417 struct proc *p, int invokesuperuserstatus);
418
419 u_long FindMetaDataDirectory(ExtendedVCB *vcb);
420
421 #define kMaxSecsForFsync 5
422 #define HFS_SYNCTRANS 1
423
424 extern int hfs_btsync(struct vnode *vp, int sync_transaction);
425 // used as a callback by the journaling code
426 extern void hfs_sync_metadata(void *arg);
427
428 short make_dir_entry(FCB **fileptr, char *name, u_int32_t fileID);
429
430
431 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize,
432 unsigned long blockSizeLimit,
433 unsigned long baseMultiple);
434
435 OSErr hfs_MountHFSVolume(struct hfsmount *hfsmp, HFSMasterDirectoryBlock *mdb,
436 struct proc *p);
437 OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
438 off_t embeddedOffset, u_int64_t disksize, struct proc *p, void *args, kauth_cred_t cred);
439
440 extern int hfs_early_journal_init(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
441 void *_args, off_t embeddedOffset, daddr64_t mdb_offset,
442 HFSMasterDirectoryBlock *mdbp, struct ucred *cred);
443 extern u_long GetFileInfo(ExtendedVCB *vcb, u_int32_t dirid, const char *name,
444 struct cat_attr *fattr, struct cat_fork *forkinfo);
445
446 int hfs_getconverter(u_int32_t encoding, hfs_to_unicode_func_t *get_unicode,
447 unicode_to_hfs_func_t *get_hfsname);
448
449 int hfs_relconverter(u_int32_t encoding);
450
451 int hfs_to_utf8(ExtendedVCB *vcb, Str31 hfs_str, ByteCount maxDstLen,
452 ByteCount *actualDstLen, unsigned char* dstStr);
453
454 int utf8_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, const unsigned char* srcStr,
455 Str31 dstStr);
456
457 int mac_roman_to_utf8(Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen,
458 unsigned char* dstStr);
459
460 int utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr);
461
462 u_int32_t hfs_pickencoding(const u_int16_t *src, int len);
463
464 enum volop {VOL_UPDATE, VOL_MKDIR, VOL_RMDIR, VOL_MKFILE, VOL_RMFILE};
465
466 extern int hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot);
467
468 extern void hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding);
469
470
471 extern void replace_desc(struct cnode *cp, struct cat_desc *cdp);
472
473 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
474
475 extern int hfs_virtualmetafile(struct cnode *);
476
477 void hfs_generate_volume_notifications(struct hfsmount *hfsmp);
478
479 __private_extern__ u_int32_t hfs_getencodingbias(void);
480 __private_extern__ void hfs_setencodingbias(u_int32_t bias);
481
482 extern int hfs_vgetrsrc(struct hfsmount *hfsmp, struct vnode *vp,
483 struct vnode **rvpp, struct proc *p);
484
485 extern int hfs_update(struct vnode *, int);
486
487 extern int hfs_truncate(struct vnode *, off_t, int, int, vfs_context_t);
488
489 extern int hfs_fsync(struct vnode *, int, int, struct proc *);
490
491 extern int hfs_access(struct vnode *, mode_t, struct ucred *, struct proc *);
492
493 extern int hfs_vget(struct hfsmount *, cnid_t, struct vnode **, int);
494
495 extern int hfs_bmap(struct vnode *, daddr_t, struct vnode **, daddr64_t *, int *);
496
497 extern int hfs_removeallattr(struct hfsmount *hfsmp, u_int32_t fileid);
498
499 __private_extern__ int hfs_start_transaction(struct hfsmount *hfsmp);
500 __private_extern__ int hfs_end_transaction(struct hfsmount *hfsmp);
501
502 extern int hfs_setextendedsecurity(struct hfsmount *hfsmp, int state);
503 extern void hfs_checkextendedsecurity(struct hfsmount *hfsmp);
504
505 extern int hfs_extendfs(struct hfsmount *, u_int64_t, vfs_context_t);
506 extern int hfs_truncatefs(struct hfsmount *, u_int64_t, vfs_context_t);
507 extern int hfs_resize_progress(struct hfsmount *, u_int32_t *);
508
509 extern int hfs_isallocated(struct hfsmount *, u_long, u_long);
510
511
512 /* HFS System file locking */
513 #define SFL_CATALOG 0x0001
514 #define SFL_EXTENTS 0x0002
515 #define SFL_BITMAP 0x0004
516 #define SFL_ATTRIBUTE 0x0008
517 #define SFL_VALIDMASK (SFL_CATALOG | SFL_EXTENTS | SFL_BITMAP | SFL_ATTRIBUTE)
518
519 extern int hfs_systemfile_lock(struct hfsmount *, int, enum hfslocktype);
520 extern void hfs_systemfile_unlock(struct hfsmount *, int);
521
522 #endif /* __APPLE_API_PRIVATE */
523 #endif /* KERNEL */
524 #endif /* __HFS__ */