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 <hfs/hfs_format.h>
40 #include <hfs/hfs_catalog.h>
41 #include <hfs/hfs_cnode.h>
42 #include <hfs/hfs_macos_defs.h>
43 #include <hfs/hfs_encodings.h>
46 struct uio
; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
47 struct hfslockf
; /* For advisory locking */
50 * Just reported via MIG interface.
52 #define VERSION_STRING "hfs-2 (4-12-99)"
54 #define HFS_LINK_MAX 32767
56 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
59 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
61 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
62 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
76 /* number of locked buffer caches to hold for b-tree meta data */
77 #define kMaxLockedMetaBuffers 32
80 * File type and creator for symbolic links
83 kSymLinkFileType
= 0x736C6E6B, /* 'slnk' */
84 kSymLinkCreator
= 0x72686170 /* 'rhap' */
88 extern struct timezone gTimeZone
;
91 /* How many free extents to cache per volume */
92 #define kMaxFreeExtents 10
95 * HFS_MINFREE gives the minimum acceptable percentage
96 * of file system blocks which may be free (but this
97 * minimum will never exceed HFS_MAXRESERVE bytes). If
98 * the free block count drops below this level only the
99 * superuser may continue to allocate blocks.
101 #define HFS_MINFREE 1
102 #define HFS_MAXRESERVE (u_int64_t)(250*1024*1024)
104 /* Internal Data structures*/
107 u_int16_t vcbSigWord
;
114 u_int32_t vcbVolBkUp
;
118 u_int32_t blockSize
; /* size of allocation blocks */
119 u_int32_t totalBlocks
; /* total allocation blocks */
120 u_int32_t freeBlocks
; /* free allocation blocks */
121 u_int32_t nextAllocation
; /* start of next allocation search */
123 u_int32_t vcbNxtCNID
;
124 u_int32_t vcbCNIDGen
;
127 int32_t vcbFndrInfo
[8];
129 u_int64_t encodingsBitmap
; /* HFS Plus only */
131 u_int16_t vcbNmFls
; /* HFS only */
132 u_int16_t vcbNmRtDirs
; /* HFS only */
133 int16_t vcbVBMSt
; /* HFS only */
134 int16_t vcbAlBlSt
; /* HFS only */
136 struct vnode
* extentsRefNum
;
137 struct vnode
* catalogRefNum
;
138 struct vnode
* allocationsRefNum
;
140 u_int8_t vcbVN
[256]; /* volume name in UTF-8 */
141 u_int32_t volumeNameEncodingHint
;
142 u_int32_t hfsPlusIOPosOffset
; /* Disk block where HFS+ starts */
143 u_int32_t vcbVBMIOSize
; /* volume bitmap I/O size */
145 /* cache of largest known free extents */
146 u_int32_t vcbFreeExtCnt
;
147 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
149 u_int32_t reserveBlocks
; /* free block reserve */
150 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
152 u_int32_t localCreateDate
; /* creation times for HFS+ volumes are in local time */
153 simple_lock_data_t vcbSimpleLock
; /* simple lock to allow concurrent access to vcb data */
155 typedef struct vcb_t ExtendedVCB
;
158 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
161 #define MARK_VOLUMEDAMAGED(fcb)
164 * NOTE: The code relies on being able to cast an ExtendedVCB* to a vfsVCB* in order
165 * to gain access to the mount point pointer from a pointer
166 * to an ExtendedVCB. DO NOT INSERT OTHER FIELDS BEFORE THE vcb FIELD!!
168 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
169 * are locked by the hfs_lock simple lock.
171 typedef struct vfsVCB
{
173 struct hfsmount
*vcb_hfsmp
; /* Pointer to hfsmount structure */
178 /* This structure describes the HFS specific mount structure data. */
179 typedef struct hfsmount
{
180 u_int8_t hfs_fs_ronly
; /* Whether this was mounted as read-initially */
181 u_int8_t hfs_unknownpermissions
; /* Whether this was mounted with MNT_UNKNOWNPERMISSIONS */
182 u_int8_t hfs_media_writeable
;
184 /* Physical Description */
185 u_long hfs_phys_block_count
; /* Num of PHYSICAL blocks of volume */
186 u_long hfs_phys_block_size
; /* Always a multiple of 512 */
188 /* Access to VFS and devices */
189 struct mount
*hfs_mp
; /* filesystem vfs structure */
190 struct vnode
*hfs_devvp
; /* block device mounted vnode */
191 dev_t hfs_raw_dev
; /* device mounted */
192 struct netexport hfs_export
; /* Export information */
193 u_int32_t hfs_logBlockSize
; /* Size of buffer cache buffer for I/O */
195 /* Default values for HFS standard and non-init access */
196 uid_t hfs_uid
; /* uid to set as owner of the files */
197 gid_t hfs_gid
; /* gid to set as owner of the files */
198 mode_t hfs_dir_mask
; /* mask to and with directory protection bits */
199 mode_t hfs_file_mask
; /* mask to and with file protection bits */
200 u_long hfs_encoding
; /* Defualt encoding for non hfs+ volumes */
202 /* simple lock for shared meta renaming */
203 simple_lock_data_t hfs_renamelock
;
206 struct vfsVCB hfs_vcb
;
207 struct cat_desc hfs_privdir_desc
;
208 struct cat_attr hfs_privdir_attr
;
209 u_int32_t hfs_metadata_createdate
;
210 hfs_to_unicode_func_t hfs_get_unicode
;
211 unicode_to_hfs_func_t hfs_get_hfsname
;
213 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
216 #define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid
218 #define MAXHFSVNODELEN 31
221 typedef struct filefork FCB
;
224 #define MAKE_INODE_NAME(name,linkno) \
225 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
230 #define WRITE_CK(VNODE, FUNC_NAME) { \
231 if ((VNODE)->v_mount->mnt_flag & MNT_RDONLY) { \
232 DBG_ERR(("%s: ATTEMPT TO WRITE A READONLY VOLUME\n", \
238 /* structure to hold a "." or ".." directory entry (12 bytes) */
239 typedef struct hfsdotentry
{
240 u_int32_t d_fileno
; /* unique file number */
241 u_int16_t d_reclen
; /* length of this structure */
242 u_int8_t d_type
; /* dirent file type */
243 u_int8_t d_namelen
; /* len of filename */
244 char d_name
[4]; /* "." or ".." */
247 #define HFS_AVERAGE_NAME_SIZE 22
248 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
249 #define MAX_HFSDIRENTRY_SIZE sizeof(struct dirent)
251 #define DIRENTRY_SIZE(namlen) \
252 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
256 kCatalogFolderNode
= 1,
261 * CatalogNodeData has same layout as the on-disk HFS Plus file/dir records.
262 * Classic hfs file/dir records are converted to match this layout.
264 * The cnd_extra padding allows big hfs plus thread records (520 bytes max)
265 * to be read onto this stucture during a cnid lookup.
268 struct CatalogNodeData
{
271 u_int32_t cnd_valence
; /* dirs only */
272 u_int32_t cnd_nodeID
;
273 u_int32_t cnd_createDate
;
274 u_int32_t cnd_contentModDate
;
275 u_int32_t cnd_attributeModDate
;
276 u_int32_t cnd_accessDate
;
277 u_int32_t cnd_backupDate
;
278 u_int32_t cnd_ownerID
;
279 u_int32_t cnd_groupID
;
280 u_int8_t cnd_adminFlags
; /* super-user changeable flags */
281 u_int8_t cnd_ownerFlags
; /* owner changeable flags */
282 u_int16_t cnd_mode
; /* file type + permission bits */
284 u_int32_t cndu_iNodeNum
; /* indirect links only */
285 u_int32_t cndu_linkCount
; /* indirect nodes only */
286 u_int32_t cndu_rawDevice
; /* special files (FBLK and FCHR) only */
288 u_int8_t cnd_finderInfo
[32];
289 u_int32_t cnd_textEncoding
;
290 u_int32_t cnd_reserved
;
291 HFSPlusForkData cnd_datafork
;
292 HFSPlusForkData cnd_rsrcfork
;
293 u_int32_t cnd_iNodeNumCopy
;
294 u_int32_t cnd_linkCNID
; /* for hard links only */
295 u_int8_t cnd_extra
[264]; /* make struct at least 520 bytes long */
297 typedef struct CatalogNodeData CatalogNodeData
;
299 #define cnd_iNodeNum cnd_un.cndu_iNodeNum
300 #define cnd_linkCount cnd_un.cndu_linkCount
301 #define cnd_rawDevice cnd_un.cndu_rawDevice
305 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
307 enum { kdirentMaxNameBytes
= NAME_MAX
};
310 /* macro to determine if hfs or hfsplus */
311 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
312 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
316 * Various ways to acquire a VFS mount point pointer:
318 #define VTOVFS(VP) ((VP)->v_mount)
319 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
320 #define VCBTOVFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp->hfs_mp)
323 * Various ways to acquire an HFS mount point pointer:
325 #define VTOHFS(VP) ((struct hfsmount *)((VP)->v_mount->mnt_data))
326 #define VFSTOHFS(MP) ((struct hfsmount *)(MP)->mnt_data)
327 #define VCBTOHFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp)
330 * Various ways to acquire a VCB pointer:
332 #define VTOVCB(VP) (&(((struct hfsmount *)((VP)->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
333 #define VFSTOVCB(MP) (&(((struct hfsmount *)(MP)->mnt_data)->hfs_vcb.vcb_vcb))
334 #define HFSTOVCB(HFSMP) (&(HFSMP)->hfs_vcb.vcb_vcb)
338 #define kHFSBlockSize 512
341 * Macros for getting the MDB/VH sector and offset
343 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
344 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
346 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
347 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
350 * This is the straight GMT conversion constant:
351 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
352 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
354 #define MAC_GMT_FACTOR 2082844800UL
357 u_int32_t
to_bsd_time(u_int32_t hfs_time
);
358 u_int32_t
to_hfs_time(u_int32_t bsd_time
);
360 int hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
);
361 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
362 #define HFS_ALTFLUSH 1
364 short hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
367 extern int hfs_getcnode(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*descp
,
368 int wantrsrc
, struct cat_attr
*attrp
, struct cat_fork
*forkp
,
371 extern int hfs_getnewvnode(struct hfsmount
*hfsmp
, struct cnode
*cp
,
372 struct cat_desc
*descp
, int wantrsrc
, struct cat_attr
*attrp
,
373 struct cat_fork
*forkp
, struct vnode
**vpp
);
375 extern int hfs_metafilelocking(struct hfsmount
*hfsmp
, u_long fileID
, u_int flags
, struct proc
*p
);
377 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
380 short MacToVFSError(OSErr err
);
382 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, struct ucred
*cred
,
383 struct proc
*p
, int invokesuperuserstatus
);
385 u_long
FindMetaDataDirectory(ExtendedVCB
*vcb
);
387 #define kMaxSecsForFsync 5
388 #define HFS_SYNCTRANS 1
390 extern int hfs_btsync(struct vnode
*vp
, int sync_transaction
);
392 short make_dir_entry(FCB
**fileptr
, char *name
, u_int32_t fileID
);
395 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
396 unsigned long blockSizeLimit
,
397 unsigned long baseMultiple
);
399 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
401 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
402 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
);
404 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
405 unicode_to_hfs_func_t
*get_hfsname
);
407 int hfs_relconverter(u_int32_t encoding
);
409 int hfs_to_utf8(ExtendedVCB
*vcb
, Str31 hfs_str
, ByteCount maxDstLen
,
410 ByteCount
*actualDstLen
, unsigned char* dstStr
);
412 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
415 int mac_roman_to_utf8(Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
416 unsigned char* dstStr
);
418 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
420 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
422 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
424 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
426 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
429 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
431 #endif /* __APPLE_API_PRIVATE */