2 * Copyright (c) 2000-2002 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 #include <sys/appleapiopts.h>
29 #ifdef __APPLE_API_PRIVATE
30 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/mount.h>
34 #include <sys/namei.h>
35 #include <sys/vnode.h>
36 #include <sys/quota.h>
37 #include <sys/dirent.h>
39 #include <vfs/vfs_journal.h>
41 #include <hfs/hfs_format.h>
42 #include <hfs/hfs_catalog.h>
43 #include <hfs/hfs_cnode.h>
44 #include <hfs/hfs_macos_defs.h>
45 #include <hfs/hfs_encodings.h>
48 struct uio
; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
49 struct hfslockf
; /* For advisory locking */
52 * Just reported via MIG interface.
54 #define VERSION_STRING "hfs-2 (4-12-99)"
56 #define HFS_LINK_MAX 32767
58 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
61 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
63 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
64 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
78 /* number of locked buffer caches to hold for b-tree meta data */
79 #define kMaxLockedMetaBuffers 32
82 * File type and creator for symbolic links
85 kSymLinkFileType
= 0x736C6E6B, /* 'slnk' */
86 kSymLinkCreator
= 0x72686170 /* 'rhap' */
90 extern struct timezone gTimeZone
;
93 /* How many free extents to cache per volume */
94 #define kMaxFreeExtents 10
97 * HFS_MINFREE gives the minimum acceptable percentage
98 * of file system blocks which may be free (but this
99 * minimum will never exceed HFS_MAXRESERVE bytes). If
100 * the free block count drops below this level only the
101 * superuser may continue to allocate blocks.
103 #define HFS_MINFREE 1
104 #define HFS_MAXRESERVE (u_int64_t)(250*1024*1024)
106 /* Internal Data structures*/
109 u_int16_t vcbSigWord
;
113 u_int32_t vcbJinfoBlock
;
117 u_int32_t vcbVolBkUp
;
121 u_int32_t blockSize
; /* size of allocation blocks */
122 u_int32_t totalBlocks
; /* total allocation blocks */
123 u_int32_t freeBlocks
; /* free allocation blocks */
124 u_int32_t nextAllocation
; /* start of next allocation search */
126 u_int32_t vcbNxtCNID
;
127 u_int32_t vcbCNIDGen
;
130 int32_t vcbFndrInfo
[8];
132 u_int64_t encodingsBitmap
; /* HFS Plus only */
134 u_int16_t vcbNmFls
; /* HFS only */
135 u_int16_t vcbNmRtDirs
; /* HFS only */
136 int16_t vcbVBMSt
; /* HFS only */
137 int16_t vcbAlBlSt
; /* HFS only */
139 struct vnode
* extentsRefNum
;
140 struct vnode
* catalogRefNum
;
141 struct vnode
* allocationsRefNum
;
143 u_int8_t vcbVN
[256]; /* volume name in UTF-8 */
144 u_int32_t volumeNameEncodingHint
;
145 u_int32_t hfsPlusIOPosOffset
; /* Disk block where HFS+ starts */
146 u_int32_t vcbVBMIOSize
; /* volume bitmap I/O size */
148 /* cache of largest known free extents */
149 u_int32_t vcbFreeExtCnt
;
150 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
152 u_int32_t reserveBlocks
; /* free block reserve */
153 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
155 u_int32_t localCreateDate
; /* creation times for HFS+ volumes are in local time */
156 simple_lock_data_t vcbSimpleLock
; /* simple lock to allow concurrent access to vcb data */
158 typedef struct vcb_t ExtendedVCB
;
161 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
164 #define MARK_VOLUMEDAMAGED(fcb)
167 * NOTE: The code relies on being able to cast an ExtendedVCB* to a vfsVCB* in order
168 * to gain access to the mount point pointer from a pointer
169 * to an ExtendedVCB. DO NOT INSERT OTHER FIELDS BEFORE THE vcb FIELD!!
171 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
172 * are locked by the hfs_lock simple lock.
174 typedef struct vfsVCB
{
176 struct hfsmount
*vcb_hfsmp
; /* Pointer to hfsmount structure */
181 /* This structure describes the HFS specific mount structure data. */
182 typedef struct hfsmount
{
183 u_int8_t hfs_fs_ronly
; /* Whether this was mounted as read-initially */
184 u_int8_t hfs_unknownpermissions
; /* Whether this was mounted with MNT_UNKNOWNPERMISSIONS */
185 u_int8_t hfs_media_writeable
;
186 u_int8_t hfs_orphans_cleaned
;
188 /* Physical Description */
189 u_long hfs_phys_block_count
; /* Num of PHYSICAL blocks of volume */
190 u_long hfs_phys_block_size
; /* Always a multiple of 512 */
192 /* Access to VFS and devices */
193 struct mount
*hfs_mp
; /* filesystem vfs structure */
194 struct vnode
*hfs_devvp
; /* block device mounted vnode */
195 dev_t hfs_raw_dev
; /* device mounted */
196 struct netexport hfs_export
; /* Export information */
197 u_int32_t hfs_logBlockSize
; /* Size of buffer cache buffer for I/O */
199 /* Default values for HFS standard and non-init access */
200 uid_t hfs_uid
; /* uid to set as owner of the files */
201 gid_t hfs_gid
; /* gid to set as owner of the files */
202 mode_t hfs_dir_mask
; /* mask to and with directory protection bits */
203 mode_t hfs_file_mask
; /* mask to and with file protection bits */
204 u_long hfs_encoding
; /* Defualt encoding for non hfs+ volumes */
206 /* simple lock for shared meta renaming */
207 simple_lock_data_t hfs_renamelock
;
210 struct vfsVCB hfs_vcb
;
211 struct cat_desc hfs_privdir_desc
;
212 struct cat_attr hfs_privdir_attr
;
213 u_int32_t hfs_metadata_createdate
;
214 hfs_to_unicode_func_t hfs_get_unicode
;
215 unicode_to_hfs_func_t hfs_get_hfsname
;
217 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
220 void *jnl
; // the journal for this volume (if one exists)
221 struct vnode
*jvp
; // device where the journal lives (may be equal to devvp)
222 u_int32_t jnl_start
; // start block of the journal file (so we don't delete it)
223 u_int32_t hfs_jnlfileid
;
224 u_int32_t hfs_jnlinfoblkid
;
225 volatile int readers
;
226 volatile int blocker
;
229 #define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid
231 #define hfs_global_shared_lock_acquire(hfsmp) \
233 if (hfsmp->blocker) { \
234 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
241 #define hfs_global_shared_lock_release(hfsmp) \
244 if (hfsmp->readers == 0) { \
245 wakeup((caddr_t)&hfsmp->readers); \
249 #define hfs_global_exclusive_lock_acquire(hfsmp) \
251 if (hfsmp->blocker) { \
252 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
255 if (hfsmp->readers != 0) { \
256 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
259 hfsmp->blocker = 1; \
263 #define hfs_global_exclusive_lock_release(hfsmp) \
264 hfsmp->blocker = 0; \
265 wakeup((caddr_t)&hfsmp->blocker)
267 #define MAXHFSVNODELEN 31
270 typedef struct filefork FCB
;
273 #define MAKE_INODE_NAME(name,linkno) \
274 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
279 #define WRITE_CK(VNODE, FUNC_NAME) { \
280 if ((VNODE)->v_mount->mnt_flag & MNT_RDONLY) { \
281 DBG_ERR(("%s: ATTEMPT TO WRITE A READONLY VOLUME\n", \
287 /* structure to hold a "." or ".." directory entry (12 bytes) */
288 typedef struct hfsdotentry
{
289 u_int32_t d_fileno
; /* unique file number */
290 u_int16_t d_reclen
; /* length of this structure */
291 u_int8_t d_type
; /* dirent file type */
292 u_int8_t d_namelen
; /* len of filename */
293 char d_name
[4]; /* "." or ".." */
296 #define HFS_AVERAGE_NAME_SIZE 22
297 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
298 #define MAX_HFSDIRENTRY_SIZE sizeof(struct dirent)
300 #define DIRENTRY_SIZE(namlen) \
301 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
305 kCatalogFolderNode
= 1,
310 * CatalogNodeData has same layout as the on-disk HFS Plus file/dir records.
311 * Classic hfs file/dir records are converted to match this layout.
313 * The cnd_extra padding allows big hfs plus thread records (520 bytes max)
314 * to be read onto this stucture during a cnid lookup.
317 struct CatalogNodeData
{
320 u_int32_t cnd_valence
; /* dirs only */
321 u_int32_t cnd_nodeID
;
322 u_int32_t cnd_createDate
;
323 u_int32_t cnd_contentModDate
;
324 u_int32_t cnd_attributeModDate
;
325 u_int32_t cnd_accessDate
;
326 u_int32_t cnd_backupDate
;
327 u_int32_t cnd_ownerID
;
328 u_int32_t cnd_groupID
;
329 u_int8_t cnd_adminFlags
; /* super-user changeable flags */
330 u_int8_t cnd_ownerFlags
; /* owner changeable flags */
331 u_int16_t cnd_mode
; /* file type + permission bits */
333 u_int32_t cndu_iNodeNum
; /* indirect links only */
334 u_int32_t cndu_linkCount
; /* indirect nodes only */
335 u_int32_t cndu_rawDevice
; /* special files (FBLK and FCHR) only */
337 u_int8_t cnd_finderInfo
[32];
338 u_int32_t cnd_textEncoding
;
339 u_int32_t cnd_reserved
;
340 HFSPlusForkData cnd_datafork
;
341 HFSPlusForkData cnd_rsrcfork
;
342 u_int32_t cnd_iNodeNumCopy
;
343 u_int32_t cnd_linkCNID
; /* for hard links only */
344 u_int8_t cnd_extra
[264]; /* make struct at least 520 bytes long */
346 typedef struct CatalogNodeData CatalogNodeData
;
348 #define cnd_iNodeNum cnd_un.cndu_iNodeNum
349 #define cnd_linkCount cnd_un.cndu_linkCount
350 #define cnd_rawDevice cnd_un.cndu_rawDevice
354 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
356 enum { kdirentMaxNameBytes
= NAME_MAX
};
359 /* macro to determine if hfs or hfsplus */
360 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
361 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
365 * Various ways to acquire a VFS mount point pointer:
367 #define VTOVFS(VP) ((VP)->v_mount)
368 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
369 #define VCBTOVFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp->hfs_mp)
372 * Various ways to acquire an HFS mount point pointer:
374 #define VTOHFS(VP) ((struct hfsmount *)((VP)->v_mount->mnt_data))
375 #define VFSTOHFS(MP) ((struct hfsmount *)(MP)->mnt_data)
376 #define VCBTOHFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp)
377 #define FCBTOHFS(FCB) ((struct hfsmount *)(FCB)->ff_cp->c_vp->v_mount->mnt_data)
380 * Various ways to acquire a VCB pointer:
382 #define VTOVCB(VP) (&(((struct hfsmount *)((VP)->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
383 #define VFSTOVCB(MP) (&(((struct hfsmount *)(MP)->mnt_data)->hfs_vcb.vcb_vcb))
384 #define HFSTOVCB(HFSMP) (&(HFSMP)->hfs_vcb.vcb_vcb)
385 #define FCBTOVCB(FCB) (&(((struct hfsmount *)((FCB)->ff_cp->c_vp->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
389 #define kHFSBlockSize 512
392 * Macros for getting the MDB/VH sector and offset
394 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
395 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
397 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
398 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
401 * This is the straight GMT conversion constant:
402 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
403 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
405 #define MAC_GMT_FACTOR 2082844800UL
408 u_int32_t
to_bsd_time(u_int32_t hfs_time
);
409 u_int32_t
to_hfs_time(u_int32_t bsd_time
);
411 int hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
);
412 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
413 #define HFS_ALTFLUSH 1
415 short hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
418 extern int hfs_getcnode(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*descp
,
419 int wantrsrc
, struct cat_attr
*attrp
, struct cat_fork
*forkp
,
422 extern int hfs_getnewvnode(struct hfsmount
*hfsmp
, struct cnode
*cp
,
423 struct cat_desc
*descp
, int wantrsrc
, struct cat_attr
*attrp
,
424 struct cat_fork
*forkp
, struct vnode
**vpp
);
426 extern int hfs_metafilelocking(struct hfsmount
*hfsmp
, u_long fileID
, u_int flags
, struct proc
*p
);
428 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
430 extern void hfs_remove_orphans(struct hfsmount
*);
433 short MacToVFSError(OSErr err
);
435 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, struct ucred
*cred
,
436 struct proc
*p
, int invokesuperuserstatus
);
438 u_long
FindMetaDataDirectory(ExtendedVCB
*vcb
);
440 #define kMaxSecsForFsync 5
441 #define HFS_SYNCTRANS 1
443 extern int hfs_btsync(struct vnode
*vp
, int sync_transaction
);
444 // used as a callback by the journaling code
445 extern void hfs_sync_metadata(void *arg
);
447 short make_dir_entry(FCB
**fileptr
, char *name
, u_int32_t fileID
);
450 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
451 unsigned long blockSizeLimit
,
452 unsigned long baseMultiple
);
454 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
456 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
457 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
);
459 extern int hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
460 void *_args
, int embeddedOffset
, int mdb_offset
,
461 HFSMasterDirectoryBlock
*mdbp
, struct ucred
*cred
);
462 extern u_long
GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, char *name
,
463 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
);
465 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
466 unicode_to_hfs_func_t
*get_hfsname
);
468 int hfs_relconverter(u_int32_t encoding
);
470 int hfs_to_utf8(ExtendedVCB
*vcb
, Str31 hfs_str
, ByteCount maxDstLen
,
471 ByteCount
*actualDstLen
, unsigned char* dstStr
);
473 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
476 int mac_roman_to_utf8(Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
477 unsigned char* dstStr
);
479 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
481 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
483 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
485 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
487 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
490 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
492 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
495 #endif /* __APPLE_API_PRIVATE */