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