2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 #define HFS_SPARSE_DEV 1
31 #include <sys/appleapiopts.h>
34 #ifdef __APPLE_API_PRIVATE
35 #include <sys/param.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>
45 #include <vfs/vfs_journal.h>
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>
55 struct uio
; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
56 struct hfslockf
; /* For advisory locking */
59 * Just reported via MIG interface.
61 #define VERSION_STRING "hfs-2 (4-12-99)"
63 #define HFS_LINK_MAX 32767
65 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
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)
73 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
75 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
76 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
90 /* number of locked buffer caches to hold for b-tree meta data */
91 #define kMaxLockedMetaBuffers 32
94 * File type and creator for symbolic links
97 kSymLinkFileType
= 0x736C6E6B, /* 'slnk' */
98 kSymLinkCreator
= 0x72686170 /* 'rhap' */
102 extern struct timezone gTimeZone
;
105 /* How many free extents to cache per volume */
106 #define kMaxFreeExtents 10
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.
115 #define HFS_MINFREE 1
116 #define HFS_MAXRESERVE ((u_int64_t)(250*1024*1024))
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
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))
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))
135 /* Internal Data structures*/
138 u_int16_t vcbSigWord
;
142 u_int32_t vcbJinfoBlock
;
146 u_int32_t vcbVolBkUp
;
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 */
155 u_int32_t vcbNxtCNID
;
156 u_int32_t vcbCNIDGen
;
159 int32_t vcbFndrInfo
[8];
161 u_int64_t encodingsBitmap
; /* HFS Plus only */
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 */
168 struct vnode
* extentsRefNum
;
169 struct vnode
* catalogRefNum
;
170 struct vnode
* allocationsRefNum
;
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 */
177 /* cache of largest known free extents */
178 u_int32_t vcbFreeExtCnt
;
179 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
181 u_int32_t reserveBlocks
; /* free block reserve */
182 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
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 */
187 typedef struct vcb_t ExtendedVCB
;
190 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
193 #define MARK_VOLUMEDAMAGED(fcb)
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!!
200 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
201 * are locked by the hfs_lock simple lock.
203 typedef struct vfsVCB
{
205 struct hfsmount
*vcb_hfsmp
; /* Pointer to hfsmount structure */
210 /* This structure describes the HFS specific mount structure data. */
211 typedef struct hfsmount
{
212 u_int32_t hfs_flags
; /* see below */
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 */
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 */
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 */
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
;
240 /* Quota variables: */
241 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
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)
248 u_int32_t hfs_jnlfileid
;
249 u_int32_t hfs_jnlinfoblkid
;
250 volatile int readers
;
251 volatile int blocker
;
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
;
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
;
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
;
276 #ifdef HFS_SPARSE_DEV
277 /* Sparse device variables: */
278 struct vnode
* hfs_backingfs_rootvp
;
279 int hfs_sparsebandblks
;
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
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
296 #ifdef HFS_SPARSE_DEV
297 #define HFS_HAS_SPARSE_DEVICE 0x400
301 #define hfs_global_shared_lock_acquire(hfsmp) \
303 if (hfsmp->blocker) { \
304 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
311 #define hfs_global_shared_lock_release(hfsmp) \
314 if (hfsmp->readers == 0) { \
315 wakeup((caddr_t)&hfsmp->readers); \
319 #define hfs_global_exclusive_lock_acquire(hfsmp) \
321 if (hfsmp->blocker) { \
322 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
325 if (hfsmp->readers != 0) { \
326 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
329 hfsmp->blocker = 1; \
333 #define hfs_global_exclusive_lock_release(hfsmp) \
334 hfsmp->blocker = 0; \
335 wakeup((caddr_t)&hfsmp->blocker)
337 #define MAXHFSVNODELEN 31
340 typedef struct filefork FCB
;
343 #define MAKE_INODE_NAME(name,linkno) \
344 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
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 ".." */
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)
360 #define DIRENTRY_SIZE(namlen) \
361 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
365 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
367 enum { kdirentMaxNameBytes
= NAME_MAX
};
370 /* macro to determine if hfs or hfsplus */
371 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
372 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
376 * Various ways to acquire a VFS mount point pointer:
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)
383 * Various ways to acquire an HFS mount point pointer:
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)
391 * Various ways to acquire a VCB pointer:
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))
399 #define HFS_KNOTE(vp, hint) KNOTE(&VTOC(vp)->c_knotes, (hint))
403 #define kHFSBlockSize 512
406 * Macros for getting the MDB/VH sector and offset
408 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
409 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
411 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
412 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
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)))
419 #define MAC_GMT_FACTOR 2082844800UL
422 u_int32_t
to_bsd_time(u_int32_t hfs_time
);
423 u_int32_t
to_hfs_time(u_int32_t bsd_time
);
425 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
426 #define HFS_ALTFLUSH 1
428 extern int hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
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
,
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
);
439 extern int hfs_metafilelocking(struct hfsmount
*hfsmp
, u_long fileID
, u_int flags
, struct proc
*p
);
441 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
443 extern void hfs_remove_orphans(struct hfsmount
*);
446 short MacToVFSError(OSErr err
);
448 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, struct ucred
*cred
,
449 struct proc
*p
, int invokesuperuserstatus
);
451 u_long
FindMetaDataDirectory(ExtendedVCB
*vcb
);
453 #define kMaxSecsForFsync 5
454 #define HFS_SYNCTRANS 1
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
);
460 short make_dir_entry(FCB
**fileptr
, char *name
, u_int32_t fileID
);
463 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
464 unsigned long blockSizeLimit
,
465 unsigned long baseMultiple
);
467 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
469 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
470 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
);
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
);
478 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
479 unicode_to_hfs_func_t
*get_hfsname
);
481 int hfs_relconverter(u_int32_t encoding
);
483 int hfs_to_utf8(ExtendedVCB
*vcb
, Str31 hfs_str
, ByteCount maxDstLen
,
484 ByteCount
*actualDstLen
, unsigned char* dstStr
);
486 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
489 int mac_roman_to_utf8(Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
490 unsigned char* dstStr
);
492 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
494 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
496 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
498 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
500 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
503 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
505 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
507 extern int hfs_virtualmetafile(struct cnode
*);
509 void hfs_generate_volume_notifications(struct hfsmount
*hfsmp
);
512 #endif /* __APPLE_API_PRIVATE */