2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 #define HFS_SPARSE_DEV 1
28 #include <sys/appleapiopts.h>
31 #ifdef __APPLE_API_PRIVATE
32 #include <sys/param.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>
42 #include <vfs/vfs_journal.h>
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>
52 struct uio
; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
53 struct hfslockf
; /* For advisory locking */
56 * Just reported via MIG interface.
58 #define VERSION_STRING "hfs-2 (4-12-99)"
60 #define HFS_LINK_MAX 32767
62 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
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)
70 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
72 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
73 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
87 /* number of locked buffer caches to hold for b-tree meta data */
88 #define kMaxLockedMetaBuffers 32
91 * File type and creator for symbolic links
94 kSymLinkFileType
= 0x736C6E6B, /* 'slnk' */
95 kSymLinkCreator
= 0x72686170 /* 'rhap' */
99 extern struct timezone gTimeZone
;
102 /* How many free extents to cache per volume */
103 #define kMaxFreeExtents 10
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.
112 #define HFS_MINFREE 1
113 #define HFS_MAXRESERVE ((u_int64_t)(250*1024*1024))
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
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))
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))
132 /* Internal Data structures*/
135 u_int16_t vcbSigWord
;
139 u_int32_t vcbJinfoBlock
;
143 u_int32_t vcbVolBkUp
;
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 */
152 u_int32_t vcbNxtCNID
;
153 u_int32_t vcbCNIDGen
;
156 int32_t vcbFndrInfo
[8];
158 u_int64_t encodingsBitmap
; /* HFS Plus only */
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 */
165 struct vnode
* extentsRefNum
;
166 struct vnode
* catalogRefNum
;
167 struct vnode
* allocationsRefNum
;
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 */
174 /* cache of largest known free extents */
175 u_int32_t vcbFreeExtCnt
;
176 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
178 u_int32_t reserveBlocks
; /* free block reserve */
179 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
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 */
184 typedef struct vcb_t ExtendedVCB
;
187 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
190 #define MARK_VOLUMEDAMAGED(fcb)
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!!
197 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
198 * are locked by the hfs_lock simple lock.
200 typedef struct vfsVCB
{
202 struct hfsmount
*vcb_hfsmp
; /* Pointer to hfsmount structure */
207 /* This structure describes the HFS specific mount structure data. */
208 typedef struct hfsmount
{
209 u_int32_t hfs_flags
; /* see below */
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 */
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 */
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 */
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
;
237 /* Quota variables: */
238 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
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)
245 u_int32_t hfs_jnlfileid
;
246 u_int32_t hfs_jnlinfoblkid
;
247 volatile int readers
;
248 volatile int blocker
;
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
;
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
;
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
;
273 #ifdef HFS_SPARSE_DEV
274 /* Sparse device variables: */
275 struct vnode
* hfs_backingfs_rootvp
;
276 int hfs_sparsebandblks
;
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
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
293 #ifdef HFS_SPARSE_DEV
294 #define HFS_HAS_SPARSE_DEVICE 0x400
298 #define hfs_global_shared_lock_acquire(hfsmp) \
300 if (hfsmp->blocker) { \
301 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
308 #define hfs_global_shared_lock_release(hfsmp) \
311 if (hfsmp->readers == 0) { \
312 wakeup((caddr_t)&hfsmp->readers); \
316 #define hfs_global_exclusive_lock_acquire(hfsmp) \
318 if (hfsmp->blocker) { \
319 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
322 if (hfsmp->readers != 0) { \
323 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
326 hfsmp->blocker = 1; \
330 #define hfs_global_exclusive_lock_release(hfsmp) \
331 hfsmp->blocker = 0; \
332 wakeup((caddr_t)&hfsmp->blocker)
334 #define MAXHFSVNODELEN 31
337 typedef struct filefork FCB
;
340 #define MAKE_INODE_NAME(name,linkno) \
341 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
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 ".." */
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)
357 #define DIRENTRY_SIZE(namlen) \
358 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
362 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
364 enum { kdirentMaxNameBytes
= NAME_MAX
};
367 /* macro to determine if hfs or hfsplus */
368 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
369 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
373 * Various ways to acquire a VFS mount point pointer:
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)
380 * Various ways to acquire an HFS mount point pointer:
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)
388 * Various ways to acquire a VCB pointer:
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))
396 #define HFS_KNOTE(vp, hint) KNOTE(&VTOC(vp)->c_knotes, (hint))
400 #define kHFSBlockSize 512
403 * Macros for getting the MDB/VH sector and offset
405 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
406 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
408 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
409 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
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)))
416 #define MAC_GMT_FACTOR 2082844800UL
419 u_int32_t
to_bsd_time(u_int32_t hfs_time
);
420 u_int32_t
to_hfs_time(u_int32_t bsd_time
);
422 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
423 #define HFS_ALTFLUSH 1
425 extern int hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
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
,
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
);
436 extern int hfs_metafilelocking(struct hfsmount
*hfsmp
, u_long fileID
, u_int flags
, struct proc
*p
);
438 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
440 extern void hfs_remove_orphans(struct hfsmount
*);
443 short MacToVFSError(OSErr err
);
445 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, struct ucred
*cred
,
446 struct proc
*p
, int invokesuperuserstatus
);
448 u_long
FindMetaDataDirectory(ExtendedVCB
*vcb
);
450 #define kMaxSecsForFsync 5
451 #define HFS_SYNCTRANS 1
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
);
457 short make_dir_entry(FCB
**fileptr
, char *name
, u_int32_t fileID
);
460 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
461 unsigned long blockSizeLimit
,
462 unsigned long baseMultiple
);
464 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
466 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
467 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
);
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
);
475 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
476 unicode_to_hfs_func_t
*get_hfsname
);
478 int hfs_relconverter(u_int32_t encoding
);
480 int hfs_to_utf8(ExtendedVCB
*vcb
, Str31 hfs_str
, ByteCount maxDstLen
,
481 ByteCount
*actualDstLen
, unsigned char* dstStr
);
483 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
486 int mac_roman_to_utf8(Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
487 unsigned char* dstStr
);
489 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
491 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
493 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
495 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
497 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
500 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
502 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
504 extern int hfs_virtualmetafile(struct cnode
*);
506 void hfs_generate_volume_notifications(struct hfsmount
*hfsmp
);
509 #endif /* __APPLE_API_PRIVATE */