2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 /* If set to 1, enables the code to allocate blocks from the start
33 * of the disk instead of the nextAllocation for sparse devices like
34 * sparse disk images or sparsebundle images. The free extent cache
35 * for such volumes is also maintained based on the start block instead
36 * of number of contiguous allocation blocks. These devices prefer
37 * allocation of blocks near the start of the disk to avoid the
38 * increasing the image size, but it can also result in file fragmentation.
40 #define HFS_SPARSE_DEV 1
43 #define HFS_CHECK_LOCK_ORDER 1
46 #include <sys/appleapiopts.h>
49 #ifdef __APPLE_API_PRIVATE
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/quota.h>
55 #include <sys/dirent.h>
56 #include <sys/event.h>
58 #include <kern/thread_call.h>
60 #include <kern/locks.h>
62 #include <vfs/vfs_journal.h>
64 #include <hfs/hfs_format.h>
65 #include <hfs/hfs_catalog.h>
66 #include <hfs/hfs_cnode.h>
67 #include <hfs/hfs_macos_defs.h>
68 #include <hfs/hfs_encodings.h>
69 #include <hfs/hfs_hotfiles.h>
70 #include <hfs/hfs_fsctl.h>
73 /* Forward declare the cprotect struct */
78 * Just reported via MIG interface.
80 #define VERSION_STRING "hfs-2 (4-12-99)"
82 #define HFS_LINK_MAX 32767
84 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
86 // 400 megs is a "big" file (i.e. one that when deleted
87 // would touch enough data that we should break it into
88 // multiple separate transactions)
89 #define HFS_BIGFILE_SIZE (400LL * 1024LL * 1024LL)
92 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
94 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
95 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
97 #define kRootDirID kHFSRootFolderID
100 /* number of locked buffer caches to hold for b-tree meta data */
101 #define kMaxLockedMetaBuffers 32
103 extern struct timezone gTimeZone
;
106 /* How many free extents to cache per volume */
107 #define kMaxFreeExtents 10
109 /* The maximum time hfs locks can be held while performing hfs statistics gathering */
110 #define HFS_FSINFO_MAX_LOCKHELD_TIME 20 * 1000000ULL /* at most 20 milliseconds. */
113 * HFS_MINFREE gives the minimum acceptable percentage
114 * of file system blocks which may be free (but this
115 * minimum will never exceed HFS_MAXRESERVE bytes). If
116 * the free block count drops below this level only the
117 * superuser may continue to allocate blocks.
119 #define HFS_MINFREE 1
120 #define HFS_MAXRESERVE ((u_int64_t)(250*1024*1024))
121 #define HFS_BT_MAXRESERVE ((u_int64_t)(10*1024*1024))
124 * The system distinguishes between the desirable low-disk
125 * notifiaction levels for root volumes and non-root volumes.
126 * The various thresholds are computed as a fraction of the
127 * volume size, all capped at a certain fixed level
130 #define HFS_ROOTVERYLOWDISKTRIGGERFRACTION 5
131 #define HFS_ROOTVERYLOWDISKTRIGGERLEVEL ((u_int64_t)(512*1024*1024))
132 #define HFS_ROOTLOWDISKTRIGGERFRACTION 10
133 #define HFS_ROOTLOWDISKTRIGGERLEVEL ((u_int64_t)(1024*1024*1024))
134 #define HFS_ROOTLOWDISKSHUTOFFFRACTION 11
135 #define HFS_ROOTLOWDISKSHUTOFFLEVEL ((u_int64_t)(1024*1024*1024 + 250*1024*1024))
137 #define HFS_VERYLOWDISKTRIGGERFRACTION 1
138 #define HFS_VERYLOWDISKTRIGGERLEVEL ((u_int64_t)(100*1024*1024))
139 #define HFS_LOWDISKTRIGGERFRACTION 2
140 #define HFS_LOWDISKTRIGGERLEVEL ((u_int64_t)(150*1024*1024))
141 #define HFS_LOWDISKSHUTOFFFRACTION 3
142 #define HFS_LOWDISKSHUTOFFLEVEL ((u_int64_t)(200*1024*1024))
144 /* Internal Data structures*/
146 /* This structure describes the HFS specific mount structure data. */
147 typedef struct hfsmount
{
148 u_int32_t hfs_flags
; /* see below */
150 /* Physical Description */
151 u_int32_t hfs_logical_block_size
; /* Logical block size of the disk as reported by ioctl(DKIOCGETBLOCKSIZE), always a multiple of 512 */
152 daddr64_t hfs_logical_block_count
; /* Number of logical blocks on the disk, as reported by ioctl(DKIOCGETBLOCKCOUNT) */
153 u_int64_t hfs_logical_bytes
; /* Number of bytes on the disk device this HFS is mounted on (blockcount * blocksize) */
155 * Regarding the two AVH sector fields below:
156 * Under normal circumstances, the filesystem's notion of the "right" location for the AVH is such that
157 * the partition and filesystem's are in sync. However, during a filesystem resize, HFS proactively
158 * writes a new AVH at the end of the filesystem, assuming that the partition will be resized accordingly.
160 * However, it is not technically a corruption if the partition size is never modified. As a result, we need
161 * to keep two copies of the AVH around "just in case" the partition size is not modified.
163 daddr64_t hfs_partition_avh_sector
; /* location of Alt VH w.r.t partition size */
164 daddr64_t hfs_fs_avh_sector
; /* location of Alt VH w.r.t filesystem size */
166 u_int32_t hfs_physical_block_size
; /* Physical block size of the disk as reported by ioctl(DKIOCGETPHYSICALBLOCKSIZE) */
167 u_int32_t hfs_log_per_phys
; /* Number of logical blocks per physical block size */
169 /* Access to VFS and devices */
170 struct mount
*hfs_mp
; /* filesystem vfs structure */
171 struct vnode
*hfs_devvp
; /* block device mounted vnode */
172 struct vnode
* hfs_extents_vp
;
173 struct vnode
* hfs_catalog_vp
;
174 struct vnode
* hfs_allocation_vp
;
175 struct vnode
* hfs_attribute_vp
;
176 struct vnode
* hfs_startup_vp
;
177 struct vnode
* hfs_attrdata_vp
; /* pseudo file */
178 struct cnode
* hfs_extents_cp
;
179 struct cnode
* hfs_catalog_cp
;
180 struct cnode
* hfs_allocation_cp
;
181 struct cnode
* hfs_attribute_cp
;
182 struct cnode
* hfs_startup_cp
;
183 dev_t hfs_raw_dev
; /* device mounted */
184 u_int32_t hfs_logBlockSize
; /* Size of buffer cache buffer for I/O */
186 /* Default values for HFS standard and non-init access */
187 uid_t hfs_uid
; /* uid to set as owner of the files */
188 gid_t hfs_gid
; /* gid to set as owner of the files */
189 mode_t hfs_dir_mask
; /* mask to and with directory protection bits */
190 mode_t hfs_file_mask
; /* mask to and with file protection bits */
191 u_int32_t hfs_encoding
; /* Default encoding for non hfs+ volumes */
193 /* Persistent fields (on disk, dynamic) */
194 time_t hfs_mtime
; /* file system last modification time */
195 u_int32_t hfs_filecount
; /* number of files in file system */
196 u_int32_t hfs_dircount
; /* number of directories in file system */
197 u_int32_t freeBlocks
; /* free allocation blocks */
198 u_int32_t nextAllocation
; /* start of next allocation search */
199 u_int32_t sparseAllocation
; /* start of allocations for sparse devices */
200 u_int32_t vcbNxtCNID
; /* next unused catalog node ID - protected by catalog lock */
201 u_int32_t vcbWrCnt
; /* file system write count */
202 u_int64_t encodingsBitmap
; /* in-use encodings */
203 u_int16_t vcbNmFls
; /* HFS Only - root dir file count */
204 u_int16_t vcbNmRtDirs
; /* HFS Only - root dir directory count */
206 /* Persistent fields (on disk, static) */
207 u_int16_t vcbSigWord
;
208 int16_t vcbFlags
; /* Runtime flag to indicate if volume is dirty/clean */
210 u_int32_t vcbJinfoBlock
;
211 u_int32_t localCreateDate
;/* volume create time from volume header (For HFS+, value is in local time) */
212 time_t hfs_itime
; /* file system creation time (creation date of the root folder) */
213 time_t hfs_btime
; /* file system last backup time */
214 u_int32_t blockSize
; /* size of allocation blocks */
215 u_int32_t totalBlocks
; /* total allocation blocks */
216 u_int32_t allocLimit
; /* Do not allocate this block or beyond */
218 * NOTE: When resizing a volume to make it smaller, allocLimit is set to the allocation
219 * block number which will contain the new alternate volume header. At all other times,
220 * allocLimit is set to totalBlocks. The allocation code uses allocLimit instead of
221 * totalBlocks to limit which blocks may be allocated, so that during a resize, we don't
222 * put new content into the blocks we're trying to truncate away.
225 u_int32_t vcbFndrInfo
[8];
226 int16_t vcbVBMSt
; /* HFS only */
227 int16_t vcbAlBlSt
; /* HFS only */
230 u_int8_t vcbVN
[256]; /* volume name in UTF-8 */
231 u_int32_t volumeNameEncodingHint
;
232 u_int32_t hfsPlusIOPosOffset
; /* Disk block where HFS+ starts */
233 u_int32_t vcbVBMIOSize
; /* volume bitmap I/O size */
235 /* cache of largest known free extents */
236 u_int32_t vcbFreeExtCnt
;
237 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
238 lck_spin_t vcbFreeExtLock
;
241 u_int8_t
*hfs_summary_table
; /* Each bit is 1 vcbVBMIOSize of bitmap, byte indexed */
242 u_int32_t hfs_summary_size
; /* number of BITS in summary table defined above (not bytes!) */
243 u_int32_t hfs_summary_bytes
; /* number of BYTES in summary table */
245 u_int32_t scan_var
; /* For initializing the summary table */
248 u_int32_t reserveBlocks
; /* free block reserve */
249 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
253 * HFS+ Private system directories (two). Any access
254 * (besides looking at the cd_cnid) requires holding
255 * the Catalog File lock.
257 struct cat_desc hfs_private_desc
[2];
258 struct cat_attr hfs_private_attr
[2];
260 u_int32_t hfs_metadata_createdate
;
261 hfs_to_unicode_func_t hfs_get_unicode
;
262 unicode_to_hfs_func_t hfs_get_hfsname
;
264 /* Quota variables: */
265 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
267 /* Journaling variables: */
268 struct journal
*jnl
; // the journal for this volume (if one exists)
269 struct vnode
*jvp
; // device where the journal lives (may be equal to devvp)
270 u_int32_t jnl_start
; // start block of the journal file (so we don't delete it)
272 u_int32_t hfs_jnlfileid
;
273 u_int32_t hfs_jnlinfoblkid
;
274 lck_rw_t hfs_global_lock
;
275 u_int32_t hfs_global_lock_nesting
;
276 thread_t hfs_global_lockowner
;
278 /* Notification variables: */
279 u_int32_t hfs_notification_conditions
;
280 u_int32_t hfs_freespace_notify_dangerlimit
;
281 u_int32_t hfs_freespace_notify_warninglimit
;
282 u_int32_t hfs_freespace_notify_desiredlevel
;
284 /* time mounted and last mounted mod time "snapshot" */
285 time_t hfs_mount_time
;
286 time_t hfs_last_mounted_mtime
;
288 /* Metadata allocation zone variables: */
289 u_int32_t hfs_metazone_start
;
290 u_int32_t hfs_metazone_end
;
291 u_int32_t hfs_hotfile_start
;
292 u_int32_t hfs_hotfile_end
;
293 u_int32_t hfs_min_alloc_start
;
294 u_int32_t hfs_freed_block_count
;
295 int hfs_hotfile_freeblks
;
296 int hfs_hotfile_maxblks
;
297 int hfs_overflow_maxblks
;
298 int hfs_catalog_maxblks
;
300 /* Hot File Clustering variables: */
301 lck_mtx_t hfc_mutex
; /* serialize hot file stages */
302 enum hfc_stage hfc_stage
; /* what are we up to... */
303 time_t hfc_timebase
; /* recording period start time */
304 time_t hfc_timeout
; /* recording period stop time */
305 void * hfc_recdata
; /* recording data (opaque) */
306 int hfc_maxfiles
; /* maximum files to track */
307 struct vnode
* hfc_filevp
;
310 /* Sparse device variables: */
311 struct vnode
* hfs_backingfs_rootvp
;
312 u_int32_t hfs_last_backingstatfs
;
313 u_int32_t hfs_sparsebandblks
;
314 u_int64_t hfs_backingfs_maxblocks
;
316 size_t hfs_max_inline_attrsize
;
318 lck_mtx_t hfs_mutex
; /* protects access to hfsmount data */
320 uint32_t hfs_syncers
; // Count of the number of syncers running
323 HFS_WANT_TO_FREEZE
, // This state stops hfs_sync from starting
324 HFS_FREEZING
, // We're in this state whilst we're flushing
325 HFS_FROZEN
// Everything gets blocked in hfs_lock_global
329 * When we're freezing (HFS_FREEZING) but not yet
330 * frozen (HFS_FROZEN), we record the freezing thread
331 * so that we stop other threads from taking locks,
332 * but allow the freezing thread.
334 const struct thread
*hfs_freezing_thread
;
336 * Once we have frozen (HFS_FROZEN), we record the
337 * process so that if it dies, we can automatically
340 proc_t hfs_freezing_proc
;
343 thread_t hfs_downgrading_thread
; /* thread who's downgrading to rdonly */
345 /* Resize variables: */
346 u_int32_t hfs_resize_blocksmoved
;
347 u_int32_t hfs_resize_totalblocks
;
348 u_int32_t hfs_resize_progress
;
350 /* Data Protection fields */
351 struct cprotect
*hfs_resize_cpentry
;
352 u_int16_t hfs_running_cp_major_vers
;
353 uint32_t default_cp_class
; /* default effective class value */
354 uint64_t cproot_flags
;
355 uint8_t cp_crypto_generation
;
356 uint8_t hfs_cp_lock_state
; /* per-mount device lock state info */
360 /* Per mount cnode hash variables: */
361 lck_mtx_t hfs_chash_mutex
; /* protects access to cnode hash table */
362 u_long hfs_cnodehash
; /* size of cnode hash table - 1 */
363 LIST_HEAD(cnodehashhead
, cnode
) *hfs_cnodehashtbl
; /* base of cnode hash */
365 /* Per mount fileid hash variables (protected by catalog lock!) */
366 u_long hfs_idhash
; /* size of cnid/fileid hash table -1 */
367 LIST_HEAD(idhashhead
, cat_preflightid
) *hfs_idhashtbl
; /* base of ID hash */
369 // Records the oldest outstanding sync request
370 struct timeval hfs_sync_req_oldest
;
372 // Records whether a sync has been queued or is in progress
373 boolean_t hfs_sync_incomplete
;
375 thread_call_t hfs_syncer
; // removeable devices get sync'ed by this guy
377 /* Records the syncer thread so that we can avoid the syncer
378 queing more syncs. */
379 thread_t hfs_syncer_thread
;
381 // Not currently used except for debugging purposes
382 uint32_t hfs_active_threads
;
386 * HFS_META_DELAY is a duration (in usecs) used for triggering the
387 * hfs_syncer() routine. We will back off if writes are in
389 * HFS_MAX_META_DELAY is the maximum time we will allow the
390 * syncer to be delayed.
393 HFS_META_DELAY
= 100 * 1000, // 0.1 secs
394 HFS_MAX_META_DELAY
= 5000 * 1000 // 5 secs
397 typedef hfsmount_t ExtendedVCB
;
399 /* Aliases for legacy (Mac OS 9) field names */
400 #define vcbLsMod hfs_mtime
401 #define vcbVolBkUp hfs_btime
402 #define extentsRefNum hfs_extents_vp
403 #define catalogRefNum hfs_catalog_vp
404 #define allocationsRefNum hfs_allocation_vp
405 #define vcbFilCnt hfs_filecount
406 #define vcbDirCnt hfs_dircount
408 /* Inline functions to set/reset vcbFlags. Upper 8 bits indicate if the volume
409 * header/VCB is clean/dirty --- if set, volume header is dirty, and
410 * if clear, volume header is clean. This value is checked to determine
411 * if the in-memory copy of volume header should be flushed to the disk
414 /* Set runtime flag to indicate that volume is dirty */
415 static __inline__
void MarkVCBDirty(ExtendedVCB
*vcb
)
417 vcb
->vcbFlags
|= 0xFF00;
420 /* Clear runtime flag to indicate that volume is dirty */
421 static __inline__
void MarkVCBClean(ExtendedVCB
*vcb
)
423 vcb
->vcbFlags
&= 0x00FF;
426 /* Check runtime flag to determine if the volume is dirty or not */
427 static __inline__ Boolean
IsVCBDirty(ExtendedVCB
*vcb
)
429 return (vcb
->vcbFlags
& 0xFF00 ? true : false);
433 * There are two private directories in HFS+.
435 * One contains inodes for files that are hardlinked or open/unlinked.
436 * The other contains inodes for directories that are hardlinked.
438 enum privdirtype
{FILE_HARDLINKS
, DIR_HARDLINKS
};
440 #define HFS_ALLOCATOR_SCAN_INFLIGHT 0x0001 /* scan started */
441 #define HFS_ALLOCATOR_SCAN_COMPLETED 0x0002 /* initial scan was completed */
443 /* HFS mount point flags */
444 #define HFS_READ_ONLY 0x00001
445 #define HFS_UNKNOWN_PERMS 0x00002
446 #define HFS_WRITEABLE_MEDIA 0x00004
447 #define HFS_CLEANED_ORPHANS 0x00008
448 #define HFS_X 0x00010
449 #define HFS_CASE_SENSITIVE 0x00020
450 #define HFS_STANDARD 0x00040
451 #define HFS_METADATA_ZONE 0x00080
452 #define HFS_FRAGMENTED_FREESPACE 0x00100
453 #define HFS_NEED_JNL_RESET 0x00200
454 #define HFS_HAS_SPARSE_DEVICE 0x00400
455 #define HFS_RESIZE_IN_PROGRESS 0x00800
456 #define HFS_QUOTAS 0x01000
457 #define HFS_CREATING_BTREE 0x02000
458 /* When set, do not update nextAllocation in the mount structure */
459 #define HFS_SKIP_UPDATE_NEXT_ALLOCATION 0x04000
460 /* When set, the file system supports extent-based extended attributes */
461 #define HFS_XATTR_EXTENTS 0x08000
462 #define HFS_FOLDERCOUNT 0x10000
463 /* When set, the file system exists on a virtual device, like disk image */
464 #define HFS_VIRTUAL_DEVICE 0x20000
465 /* When set, we're in hfs_changefs, so hfs_sync should do nothing. */
466 #define HFS_IN_CHANGEFS 0x40000
467 /* When set, we are in process of downgrading or have downgraded to read-only,
468 * so hfs_start_transaction should return EROFS.
470 #define HFS_RDONLY_DOWNGRADE 0x80000
471 #define HFS_DID_CONTIG_SCAN 0x100000
472 #define HFS_UNMAP 0x200000
473 #define HFS_SSD 0x400000
474 #define HFS_SUMMARY_TABLE 0x800000
475 #define HFS_CS 0x1000000
478 /* Macro to update next allocation block in the HFS mount structure. If
479 * the HFS_SKIP_UPDATE_NEXT_ALLOCATION is set, do not update
480 * nextAllocation block.
482 #define HFS_UPDATE_NEXT_ALLOCATION(hfsmp, new_nextAllocation) \
484 if ((hfsmp->hfs_flags & HFS_SKIP_UPDATE_NEXT_ALLOCATION) == 0)\
485 hfsmp->nextAllocation = new_nextAllocation; \
488 /* Macro for incrementing and decrementing the folder count in a cnode
489 * attribute only if the HFS_FOLDERCOUNT bit is set in the mount flags
490 * and kHFSHasFolderCount bit is set in the cnode flags. Currently these
491 * bits are only set for case sensitive HFS+ volumes.
493 #define INC_FOLDERCOUNT(hfsmp, cattr) \
494 if ((hfsmp->hfs_flags & HFS_FOLDERCOUNT) && \
495 (cattr.ca_recflags & kHFSHasFolderCountMask)) { \
496 cattr.ca_dircount++; \
499 #define DEC_FOLDERCOUNT(hfsmp, cattr) \
500 if ((hfsmp->hfs_flags & HFS_FOLDERCOUNT) && \
501 (cattr.ca_recflags & kHFSHasFolderCountMask) && \
502 (cattr.ca_dircount > 0)) { \
503 cattr.ca_dircount--; \
506 typedef struct filefork FCB;
509 * Macros for creating item names for our special/private directories.
511 #define MAKE_INODE_NAME(name, size, linkno) \
512 (void) snprintf((name), size, "%s%d", HFS_INODE_PREFIX, (linkno))
513 #define HFS_INODE_PREFIX_LEN 5
515 #define MAKE_DIRINODE_NAME(name, size, linkno) \
516 (void) snprintf((name), size, "%s%d", HFS_DIRINODE_PREFIX, (linkno))
517 #define HFS_DIRINODE_PREFIX_LEN 4
519 #define MAKE_DELETED_NAME(NAME, size, FID) \
520 (void) snprintf((NAME), size, "%s%d", HFS_DELETE_PREFIX, (FID))
521 #define HFS_DELETE_PREFIX_LEN 4
524 #define HFS_AVERAGE_NAME_SIZE 22
525 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
527 #define STD_DIRENT_LEN(namlen) \
528 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
530 #define EXT_DIRENT_LEN(namlen) \
531 ((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 7) & ~7)
534 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
537 /* macro to determine if hfs or hfsplus */
538 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
539 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
543 * Various ways to acquire a VFS mount point pointer:
545 #define VTOVFS(VP) vnode_mount((VP))
546 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
547 #define VCBTOVFS(VCB) HFSTOVFS(VCB)
550 * Various ways to acquire an HFS mount point pointer:
552 #define VTOHFS(VP) ((struct hfsmount *)vfs_fsprivate(vnode_mount((VP))))
553 #define VFSTOHFS(MP) ((struct hfsmount *)vfs_fsprivate((MP)))
554 #define VCBTOHFS(VCB) (VCB)
555 #define FCBTOHFS(FCB) ((struct hfsmount *)vfs_fsprivate(vnode_mount((FCB)->ff_cp->c_vp)))
558 * Various ways to acquire a VCB (legacy) pointer:
560 #define VTOVCB(VP) VTOHFS(VP)
561 #define VFSTOVCB(MP) VFSTOHFS(MP)
562 #define HFSTOVCB(HFSMP) (HFSMP)
563 #define FCBTOVCB(FCB) FCBTOHFS(FCB)
567 #define kHFSBlockSize 512
570 * Macros for getting the MDB/VH sector and offset
572 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
573 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
575 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
576 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
578 /* Convert the logical sector number to be aligned on physical block size boundary.
579 * We are assuming the partition is a multiple of physical block size.
581 #define HFS_PHYSBLK_ROUNDDOWN(sector_num, log_per_phys) ((sector_num / log_per_phys) * log_per_phys)
584 * HFS specific fcntl()'s
586 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
587 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
588 /* See HFSIOC_EXT_BULKACCESS and friends for HFS specific fsctls*/
593 * This is the straight GMT conversion constant:
594 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
595 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
597 #define MAC_GMT_FACTOR 2082844800UL
599 static inline __attribute__((const))
600 uint64_t hfs_blk_to_bytes(uint32_t blk
, uint32_t blk_size
)
602 return (uint64_t)blk
* blk_size
; // Avoid the overflow
605 /*****************************************************************************
607 ******************************************************************************/
609 /*****************************************************************************
610 hfs_vnop_xxx functions from different files
611 ******************************************************************************/
612 int hfs_vnop_readdirattr(struct vnop_readdirattr_args
*); /* in hfs_attrlist.c */
613 int hfs_vnop_getattrlistbulk(struct vnop_getattrlistbulk_args
*); /* in hfs_attrlist.c */
615 int hfs_vnop_inactive(struct vnop_inactive_args
*); /* in hfs_cnode.c */
616 int hfs_vnop_reclaim(struct vnop_reclaim_args
*); /* in hfs_cnode.c */
618 int hfs_set_backingstore (struct vnode
*vp
, int val
); /* in hfs_cnode.c */
619 int hfs_is_backingstore (struct vnode
*vp
, int *val
); /* in hfs_cnode.c */
621 int hfs_vnop_link(struct vnop_link_args
*); /* in hfs_link.c */
623 int hfs_vnop_lookup(struct vnop_lookup_args
*); /* in hfs_lookup.c */
625 int hfs_vnop_search(struct vnop_searchfs_args
*); /* in hfs_search.c */
627 int hfs_vnop_read(struct vnop_read_args
*); /* in hfs_readwrite.c */
628 int hfs_vnop_write(struct vnop_write_args
*); /* in hfs_readwrite.c */
629 int hfs_vnop_ioctl(struct vnop_ioctl_args
*); /* in hfs_readwrite.c */
630 int hfs_vnop_select(struct vnop_select_args
*); /* in hfs_readwrite.c */
631 int hfs_vnop_strategy(struct vnop_strategy_args
*); /* in hfs_readwrite.c */
632 int hfs_vnop_allocate(struct vnop_allocate_args
*); /* in hfs_readwrite.c */
633 int hfs_vnop_pagein(struct vnop_pagein_args
*); /* in hfs_readwrite.c */
634 int hfs_vnop_pageout(struct vnop_pageout_args
*); /* in hfs_readwrite.c */
635 int hfs_vnop_bwrite(struct vnop_bwrite_args
*); /* in hfs_readwrite.c */
636 int hfs_vnop_blktooff(struct vnop_blktooff_args
*); /* in hfs_readwrite.c */
637 int hfs_vnop_offtoblk(struct vnop_offtoblk_args
*); /* in hfs_readwrite.c */
638 int hfs_vnop_blockmap(struct vnop_blockmap_args
*); /* in hfs_readwrite.c */
640 int hfs_vnop_getxattr(struct vnop_getxattr_args
*); /* in hfs_xattr.c */
641 int hfs_vnop_setxattr(struct vnop_setxattr_args
*); /* in hfs_xattr.c */
642 int hfs_vnop_removexattr(struct vnop_removexattr_args
*); /* in hfs_xattr.c */
643 int hfs_vnop_listxattr(struct vnop_listxattr_args
*); /* in hfs_xattr.c */
645 extern int hfs_vnop_getnamedstream(struct vnop_getnamedstream_args
*);
646 extern int hfs_vnop_makenamedstream(struct vnop_makenamedstream_args
*);
647 extern int hfs_vnop_removenamedstream(struct vnop_removenamedstream_args
*);
651 /*****************************************************************************
652 Functions from MacOSStubs.c
653 ******************************************************************************/
654 time_t to_bsd_time(u_int32_t hfs_time
);
656 u_int32_t
to_hfs_time(time_t bsd_time
);
659 /*****************************************************************************
660 Functions from hfs_encodinghint.c
661 ******************************************************************************/
662 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
664 u_int32_t
hfs_getencodingbias(void);
666 void hfs_setencodingbias(u_int32_t bias
);
669 /*****************************************************************************
670 Functions from hfs_encodings.c
671 ******************************************************************************/
672 void hfs_converterinit(void);
674 int hfs_relconverter (u_int32_t encoding
);
676 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
677 unicode_to_hfs_func_t
*get_hfsname
);
680 int hfs_to_utf8(ExtendedVCB
*vcb
, const Str31 hfs_str
, ByteCount maxDstLen
,
681 ByteCount
*actualDstLen
, unsigned char* dstStr
);
683 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
686 int mac_roman_to_utf8(const Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
687 unsigned char* dstStr
);
689 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
691 int mac_roman_to_unicode(const Str31 hfs_str
, UniChar
*uni_str
, u_int32_t maxCharLen
, u_int32_t
*usedCharLen
);
693 int unicode_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, u_int16_t
* srcStr
, Str31 dstStr
, int retry
);
696 /*****************************************************************************
697 Functions from hfs_notifications.c
698 ******************************************************************************/
699 void hfs_generate_volume_notifications(struct hfsmount
*hfsmp
);
702 /*****************************************************************************
703 Functions from hfs_readwrite.c
704 ******************************************************************************/
705 extern int hfs_relocate(struct vnode
*, u_int32_t
, kauth_cred_t
, struct proc
*);
707 /* Flags for HFS truncate */
708 #define HFS_TRUNCATE_SKIPUPDATE 0x00000001
709 #define HFS_TRUNCATE_SKIPTIMES 0x00000002 /* implied by skipupdate; it is a subset */
712 extern int hfs_truncate(struct vnode
*, off_t
, int, int, vfs_context_t
);
714 extern int hfs_release_storage (struct hfsmount
*hfsmp
, struct filefork
*datafork
,
715 struct filefork
*rsrcfork
, u_int32_t fileid
);
717 extern int hfs_prepare_release_storage (struct hfsmount
*hfsmp
, struct vnode
*vp
);
719 extern int hfs_bmap(struct vnode
*, daddr_t
, struct vnode
**, daddr64_t
*, unsigned int *);
721 extern errno_t
hfs_ubc_setsize(vnode_t vp
, off_t len
, bool have_cnode_lock
);
724 /*****************************************************************************
725 Functions from hfs_resize.c
726 ******************************************************************************/
727 int hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
);
728 int hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
);
731 /*****************************************************************************
732 Functions from hfs_vfsops.c
733 ******************************************************************************/
734 int hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
);
736 /* used as a callback by the journaling code */
737 extern void hfs_sync_metadata(void *arg
);
739 extern int hfs_vget(struct hfsmount
*, cnid_t
, struct vnode
**, int, int);
741 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
743 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
744 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
746 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
747 #define HFS_ALTFLUSH 1
749 extern int hfs_extendfs(struct hfsmount
*, u_int64_t
, vfs_context_t
);
750 extern int hfs_truncatefs(struct hfsmount
*, u_int64_t
, vfs_context_t
);
751 extern int hfs_resize_progress(struct hfsmount
*, u_int32_t
*);
753 /* If a runtime corruption is detected, mark the volume inconsistent
754 * bit in the volume attributes.
758 HFS_INCONSISTENCY_DETECTED
,
760 // Used when unable to rollback an operation that failed
763 // Used when the latter part of an operation failed, but we chose not to roll back
766 // Used when someone told us to force an fsck on next mount
768 } hfs_inconsistency_reason_t
;
770 void hfs_mark_inconsistent(struct hfsmount
*hfsmp
,
771 hfs_inconsistency_reason_t reason
);
773 void hfs_scan_blocks (struct hfsmount
*hfsmp
);
775 /*****************************************************************************
776 Functions from hfs_vfsutils.c
777 ******************************************************************************/
778 u_int32_t
BestBlockSizeFit(u_int32_t allocationBlockSize
,
779 u_int32_t blockSizeLimit
,
780 u_int32_t baseMultiple
);
783 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
786 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
787 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
, kauth_cred_t cred
);
789 OSErr
hfs_ValidateHFSPlusVolumeHeader(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
);
791 extern int hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
793 extern bool overflow_extents(struct filefork
*fp
);
795 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, kauth_cred_t cred
,
796 struct proc
*p
, int invokesuperuserstatus
);
798 extern int check_for_tracked_file(struct vnode
*vp
, time_t ctime
, uint64_t op_type
, void *arg
);
799 extern int check_for_dataless_file(struct vnode
*vp
, uint64_t op_type
);
800 extern int hfs_generate_document_id(struct hfsmount
*hfsmp
, uint32_t *docid
);
802 /* Return information about number of metadata blocks for volume */
803 extern int hfs_getinfo_metadata_blocks(struct hfsmount
*hfsmp
, struct hfsinfo_metadata
*hinfo
);
806 * Journal lock function prototypes
808 int hfs_lock_global (struct hfsmount
*hfsmp
, enum hfs_locktype locktype
);
809 void hfs_unlock_global (struct hfsmount
*hfsmp
);
811 /* HFS mount lock/unlock prototypes */
812 void hfs_lock_mount (struct hfsmount
*hfsmp
);
813 void hfs_unlock_mount (struct hfsmount
*hfsmp
);
816 /* HFS System file locking */
817 #define SFL_CATALOG 0x0001
818 #define SFL_EXTENTS 0x0002
819 #define SFL_BITMAP 0x0004
820 #define SFL_ATTRIBUTE 0x0008
821 #define SFL_STARTUP 0x0010
822 #define SFL_VM_PRIV 0x0020
823 #define SFL_VALIDMASK (SFL_CATALOG | SFL_EXTENTS | SFL_BITMAP | SFL_ATTRIBUTE | SFL_STARTUP | SFL_VM_PRIV)
825 extern int hfs_systemfile_lock(struct hfsmount
*, int, enum hfs_locktype
);
826 extern void hfs_systemfile_unlock(struct hfsmount
*, int);
828 extern u_int32_t
GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, const char *name
,
829 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
);
831 extern void hfs_remove_orphans(struct hfsmount
*);
833 u_int32_t
GetLogicalBlockSize(struct vnode
*vp
);
835 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
837 short MacToVFSError(OSErr err
);
839 void hfs_metadatazone_init(struct hfsmount
*hfsmp
, int disable
);
841 /* HFS directory hint functions. */
842 extern directoryhint_t
* hfs_getdirhint(struct cnode
*, int, int);
843 extern void hfs_reldirhint(struct cnode
*, directoryhint_t
*);
844 extern void hfs_reldirhints(struct cnode
*, int);
845 extern void hfs_insertdirhint(struct cnode
*, directoryhint_t
*);
847 extern int hfs_namecmp(const u_int8_t
*str1
, size_t len1
, const u_int8_t
*str2
, size_t len2
);
849 extern int hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
850 void *_args
, off_t embeddedOffset
, daddr64_t mdb_offset
,
851 HFSMasterDirectoryBlock
*mdbp
, kauth_cred_t cred
);
853 extern int hfs_virtualmetafile(struct cnode
*);
855 extern int hfs_start_transaction(struct hfsmount
*hfsmp
);
856 extern int hfs_end_transaction(struct hfsmount
*hfsmp
);
857 extern void hfs_journal_lock(struct hfsmount
*hfsmp
);
858 extern void hfs_journal_unlock(struct hfsmount
*hfsmp
);
859 extern int hfs_journal_flush(struct hfsmount
*hfsmp
, boolean_t wait_for_IO
);
860 extern void hfs_syncer_lock(struct hfsmount
*hfsmp
);
861 extern void hfs_syncer_unlock(struct hfsmount
*hfsmp
);
862 extern void hfs_syncer_wait(struct hfsmount
*hfsmp
);
863 extern void hfs_syncer_wakeup(struct hfsmount
*hfsmp
);
864 extern void hfs_syncer_queue(thread_call_t syncer
);
865 extern void hfs_sync_ejectable(struct hfsmount
*hfsmp
);
867 extern void hfs_trim_callback(void *arg
, uint32_t extent_count
, const dk_extent_t
*extents
);
869 /* Erase unused Catalog nodes due to <rdar://problem/6947811>. */
870 extern int hfs_erase_unused_nodes(struct hfsmount
*hfsmp
);
872 extern uint64_t hfs_usecs_to_deadline(uint64_t usecs
);
874 extern int hfs_freeze(struct hfsmount
*hfsmp
);
875 extern int hfs_thaw(struct hfsmount
*hfsmp
, const struct proc
*process
);
878 /*****************************************************************************
879 Functions from hfs_vnops.c
880 ******************************************************************************/
881 int hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
);
883 int hfs_chmod(struct vnode
*vp
, int mode
, kauth_cred_t cred
, struct proc
*p
);
885 int hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
, struct proc
*p
);
887 #define kMaxSecsForFsync 5
888 #define HFS_SYNCTRANS 1
889 extern int hfs_btsync(struct vnode
*vp
, int sync_transaction
);
891 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
893 extern int hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
,
894 struct vnode
**rvpp
);
896 extern int hfs_update(struct vnode
*, int);
898 extern int hfs_fsync(struct vnode
*, int, int, struct proc
*);
900 /*****************************************************************************
901 Functions from hfs_xattr.c
902 ******************************************************************************/
905 * Maximum extended attribute size supported for all extended attributes except
906 * resource fork and finder info.
908 #define HFS_XATTR_MAXSIZE INT32_MAX
910 /* Number of bits used to represent maximum extended attribute size */
911 #define HFS_XATTR_SIZE_BITS 31
913 int hfs_attrkeycompare(HFSPlusAttrKey
*searchKey
, HFSPlusAttrKey
*trialKey
);
914 int hfs_buildattrkey(u_int32_t fileID
, const char *attrname
, HFSPlusAttrKey
*key
);
915 void hfs_xattr_init(struct hfsmount
* hfsmp
);
916 int file_attribute_exist(struct hfsmount
*hfsmp
, uint32_t fileID
);
917 int init_attrdata_vnode(struct hfsmount
*hfsmp
);
918 int hfs_xattr_read(vnode_t vp
, const char *name
, void *data
, size_t *size
);
919 int hfs_getxattr_internal(cnode_t
*, struct vnop_getxattr_args
*,
920 struct hfsmount
*, u_int32_t
);
921 int hfs_xattr_write(vnode_t vp
, const char *name
, const void *data
, size_t size
);
922 int hfs_setxattr_internal(struct cnode
*, const void *, size_t,
923 struct vnop_setxattr_args
*, struct hfsmount
*, u_int32_t
);
924 extern int hfs_removeallattr(struct hfsmount
*hfsmp
, u_int32_t fileid
);
925 extern int hfs_set_volxattr(struct hfsmount
*hfsmp
, unsigned int xattrtype
, int state
);
929 /*****************************************************************************
930 Functions from hfs_link.c
931 ******************************************************************************/
933 extern int hfs_unlink(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct vnode
*vp
,
934 struct componentname
*cnp
, int skip_reserve
);
935 extern int hfs_lookup_siblinglinks(struct hfsmount
*hfsmp
, cnid_t linkfileid
,
936 cnid_t
*prevlinkid
, cnid_t
*nextlinkid
);
937 extern int hfs_lookup_lastlink(struct hfsmount
*hfsmp
, cnid_t linkfileid
,
938 cnid_t
*nextlinkid
, struct cat_desc
*cdesc
);
939 extern void hfs_privatedir_init(struct hfsmount
*, enum privdirtype
);
941 extern void hfs_savelinkorigin(cnode_t
*cp
, cnid_t parentcnid
);
942 extern void hfs_relorigins(struct cnode
*cp
);
943 extern void hfs_relorigin(struct cnode
*cp
, cnid_t parentcnid
);
944 extern int hfs_haslinkorigin(cnode_t
*cp
);
945 extern cnid_t
hfs_currentparent(cnode_t
*cp
);
946 extern cnid_t
hfs_currentcnid(cnode_t
*cp
);
949 /*****************************************************************************
950 Functions from VolumeAllocation.c
951 ******************************************************************************/
952 extern int hfs_isallocated(struct hfsmount
*hfsmp
, u_int32_t startingBlock
,
953 u_int32_t numBlocks
);
955 extern int hfs_count_allocated(struct hfsmount
*hfsmp
, u_int32_t startBlock
,
956 u_int32_t numBlocks
, u_int32_t
*alloc_count
);
958 extern int hfs_isrbtree_active (struct hfsmount
*hfsmp
);
960 /*****************************************************************************
961 Functions from hfs_fsinfo.c
962 ******************************************************************************/
963 extern errno_t
hfs_get_fsinfo(struct hfsmount
*hfsmp
, void *a_data
);
964 extern void hfs_fsinfo_data_add(struct hfs_fsinfo_data
*fsinfo
, uint64_t entry
);
966 #endif /* __APPLE_API_PRIVATE */