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