2 * Copyright (c) 2000-2002 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 #include <sys/appleapiopts.h>
32 #ifdef __APPLE_API_PRIVATE
33 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
38 #include <sys/vnode.h>
39 #include <sys/quota.h>
40 #include <sys/dirent.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>
51 struct uio
; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
52 struct hfslockf
; /* For advisory locking */
55 * Just reported via MIG interface.
57 #define VERSION_STRING "hfs-2 (4-12-99)"
59 #define HFS_LINK_MAX 32767
61 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
64 enum { kMDBSize
= 512 }; /* Size of I/O transfer to read entire MDB */
66 enum { kMasterDirectoryBlock
= 2 }; /* MDB offset on disk in 512-byte blocks */
67 enum { kMDBOffset
= kMasterDirectoryBlock
* 512 }; /* MDB offset on disk in bytes */
81 /* number of locked buffer caches to hold for b-tree meta data */
82 #define kMaxLockedMetaBuffers 32
85 * File type and creator for symbolic links
88 kSymLinkFileType
= 0x736C6E6B, /* 'slnk' */
89 kSymLinkCreator
= 0x72686170 /* 'rhap' */
93 extern struct timezone gTimeZone
;
96 /* How many free extents to cache per volume */
97 #define kMaxFreeExtents 10
100 * HFS_MINFREE gives the minimum acceptable percentage
101 * of file system blocks which may be free (but this
102 * minimum will never exceed HFS_MAXRESERVE bytes). If
103 * the free block count drops below this level only the
104 * superuser may continue to allocate blocks.
106 #define HFS_MINFREE 1
107 #define HFS_MAXRESERVE (u_int64_t)(250*1024*1024)
109 /* Internal Data structures*/
112 u_int16_t vcbSigWord
;
116 u_int32_t vcbJinfoBlock
;
120 u_int32_t vcbVolBkUp
;
124 u_int32_t blockSize
; /* size of allocation blocks */
125 u_int32_t totalBlocks
; /* total allocation blocks */
126 u_int32_t freeBlocks
; /* free allocation blocks */
127 u_int32_t nextAllocation
; /* start of next allocation search */
129 u_int32_t vcbNxtCNID
;
130 u_int32_t vcbCNIDGen
;
133 int32_t vcbFndrInfo
[8];
135 u_int64_t encodingsBitmap
; /* HFS Plus only */
137 u_int16_t vcbNmFls
; /* HFS only */
138 u_int16_t vcbNmRtDirs
; /* HFS only */
139 int16_t vcbVBMSt
; /* HFS only */
140 int16_t vcbAlBlSt
; /* HFS only */
142 struct vnode
* extentsRefNum
;
143 struct vnode
* catalogRefNum
;
144 struct vnode
* allocationsRefNum
;
146 u_int8_t vcbVN
[256]; /* volume name in UTF-8 */
147 u_int32_t volumeNameEncodingHint
;
148 u_int32_t hfsPlusIOPosOffset
; /* Disk block where HFS+ starts */
149 u_int32_t vcbVBMIOSize
; /* volume bitmap I/O size */
151 /* cache of largest known free extents */
152 u_int32_t vcbFreeExtCnt
;
153 HFSPlusExtentDescriptor vcbFreeExt
[kMaxFreeExtents
];
155 u_int32_t reserveBlocks
; /* free block reserve */
156 u_int32_t loanedBlocks
; /* blocks on loan for delayed allocations */
158 u_int32_t localCreateDate
; /* creation times for HFS+ volumes are in local time */
159 simple_lock_data_t vcbSimpleLock
; /* simple lock to allow concurrent access to vcb data */
161 typedef struct vcb_t ExtendedVCB
;
164 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
167 #define MARK_VOLUMEDAMAGED(fcb)
170 * NOTE: The code relies on being able to cast an ExtendedVCB* to a vfsVCB* in order
171 * to gain access to the mount point pointer from a pointer
172 * to an ExtendedVCB. DO NOT INSERT OTHER FIELDS BEFORE THE vcb FIELD!!
174 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
175 * are locked by the hfs_lock simple lock.
177 typedef struct vfsVCB
{
179 struct hfsmount
*vcb_hfsmp
; /* Pointer to hfsmount structure */
184 /* This structure describes the HFS specific mount structure data. */
185 typedef struct hfsmount
{
186 u_int8_t hfs_fs_ronly
; /* Whether this was mounted as read-initially */
187 u_int8_t hfs_unknownpermissions
; /* Whether this was mounted with MNT_UNKNOWNPERMISSIONS */
188 u_int8_t hfs_media_writeable
;
189 u_int8_t hfs_orphans_cleaned
;
191 /* Physical Description */
192 u_long hfs_phys_block_count
; /* Num of PHYSICAL blocks of volume */
193 u_long hfs_phys_block_size
; /* Always a multiple of 512 */
195 /* Access to VFS and devices */
196 struct mount
*hfs_mp
; /* filesystem vfs structure */
197 struct vnode
*hfs_devvp
; /* block device mounted vnode */
198 dev_t hfs_raw_dev
; /* device mounted */
199 struct netexport hfs_export
; /* Export information */
200 u_int32_t hfs_logBlockSize
; /* Size of buffer cache buffer for I/O */
202 /* Default values for HFS standard and non-init access */
203 uid_t hfs_uid
; /* uid to set as owner of the files */
204 gid_t hfs_gid
; /* gid to set as owner of the files */
205 mode_t hfs_dir_mask
; /* mask to and with directory protection bits */
206 mode_t hfs_file_mask
; /* mask to and with file protection bits */
207 u_long hfs_encoding
; /* Defualt encoding for non hfs+ volumes */
209 /* simple lock for shared meta renaming */
210 simple_lock_data_t hfs_renamelock
;
213 struct vfsVCB hfs_vcb
;
214 struct cat_desc hfs_privdir_desc
;
215 struct cat_attr hfs_privdir_attr
;
216 u_int32_t hfs_metadata_createdate
;
217 hfs_to_unicode_func_t hfs_get_unicode
;
218 unicode_to_hfs_func_t hfs_get_hfsname
;
220 struct quotafile hfs_qfiles
[MAXQUOTAS
]; /* quota files */
223 void *jnl
; // the journal for this volume (if one exists)
224 struct vnode
*jvp
; // device where the journal lives (may be equal to devvp)
225 u_int32_t jnl_start
; // start block of the journal file (so we don't delete it)
226 u_int32_t hfs_jnlfileid
;
227 u_int32_t hfs_jnlinfoblkid
;
228 volatile int readers
;
229 volatile int blocker
;
232 #define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid
234 #define hfs_global_shared_lock_acquire(hfsmp) \
236 if (hfsmp->blocker) { \
237 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
244 #define hfs_global_shared_lock_release(hfsmp) \
247 if (hfsmp->readers == 0) { \
248 wakeup((caddr_t)&hfsmp->readers); \
252 #define hfs_global_exclusive_lock_acquire(hfsmp) \
254 if (hfsmp->blocker) { \
255 tsleep((caddr_t)&hfsmp->blocker, PRIBIO, "journal_blocker", 0); \
258 if (hfsmp->readers != 0) { \
259 tsleep((caddr_t)&hfsmp->readers, PRIBIO, "journal_enable/disble", 0); \
262 hfsmp->blocker = 1; \
266 #define hfs_global_exclusive_lock_release(hfsmp) \
267 hfsmp->blocker = 0; \
268 wakeup((caddr_t)&hfsmp->blocker)
270 #define MAXHFSVNODELEN 31
273 typedef struct filefork FCB
;
276 #define MAKE_INODE_NAME(name,linkno) \
277 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
282 #define WRITE_CK(VNODE, FUNC_NAME) { \
283 if ((VNODE)->v_mount->mnt_flag & MNT_RDONLY) { \
284 DBG_ERR(("%s: ATTEMPT TO WRITE A READONLY VOLUME\n", \
290 /* structure to hold a "." or ".." directory entry (12 bytes) */
291 typedef struct hfsdotentry
{
292 u_int32_t d_fileno
; /* unique file number */
293 u_int16_t d_reclen
; /* length of this structure */
294 u_int8_t d_type
; /* dirent file type */
295 u_int8_t d_namelen
; /* len of filename */
296 char d_name
[4]; /* "." or ".." */
299 #define HFS_AVERAGE_NAME_SIZE 22
300 #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4)
301 #define MAX_HFSDIRENTRY_SIZE sizeof(struct dirent)
303 #define DIRENTRY_SIZE(namlen) \
304 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
308 kCatalogFolderNode
= 1,
313 * CatalogNodeData has same layout as the on-disk HFS Plus file/dir records.
314 * Classic hfs file/dir records are converted to match this layout.
316 * The cnd_extra padding allows big hfs plus thread records (520 bytes max)
317 * to be read onto this stucture during a cnid lookup.
320 struct CatalogNodeData
{
323 u_int32_t cnd_valence
; /* dirs only */
324 u_int32_t cnd_nodeID
;
325 u_int32_t cnd_createDate
;
326 u_int32_t cnd_contentModDate
;
327 u_int32_t cnd_attributeModDate
;
328 u_int32_t cnd_accessDate
;
329 u_int32_t cnd_backupDate
;
330 u_int32_t cnd_ownerID
;
331 u_int32_t cnd_groupID
;
332 u_int8_t cnd_adminFlags
; /* super-user changeable flags */
333 u_int8_t cnd_ownerFlags
; /* owner changeable flags */
334 u_int16_t cnd_mode
; /* file type + permission bits */
336 u_int32_t cndu_iNodeNum
; /* indirect links only */
337 u_int32_t cndu_linkCount
; /* indirect nodes only */
338 u_int32_t cndu_rawDevice
; /* special files (FBLK and FCHR) only */
340 u_int8_t cnd_finderInfo
[32];
341 u_int32_t cnd_textEncoding
;
342 u_int32_t cnd_reserved
;
343 HFSPlusForkData cnd_datafork
;
344 HFSPlusForkData cnd_rsrcfork
;
345 u_int32_t cnd_iNodeNumCopy
;
346 u_int32_t cnd_linkCNID
; /* for hard links only */
347 u_int8_t cnd_extra
[264]; /* make struct at least 520 bytes long */
349 typedef struct CatalogNodeData CatalogNodeData
;
351 #define cnd_iNodeNum cnd_un.cndu_iNodeNum
352 #define cnd_linkCount cnd_un.cndu_linkCount
353 #define cnd_rawDevice cnd_un.cndu_rawDevice
357 enum { kHFSPlusMaxFileNameBytes
= kHFSPlusMaxFileNameChars
* 3 };
359 enum { kdirentMaxNameBytes
= NAME_MAX
};
362 /* macro to determine if hfs or hfsplus */
363 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
364 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
368 * Various ways to acquire a VFS mount point pointer:
370 #define VTOVFS(VP) ((VP)->v_mount)
371 #define HFSTOVFS(HFSMP) ((HFSMP)->hfs_mp)
372 #define VCBTOVFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp->hfs_mp)
375 * Various ways to acquire an HFS mount point pointer:
377 #define VTOHFS(VP) ((struct hfsmount *)((VP)->v_mount->mnt_data))
378 #define VFSTOHFS(MP) ((struct hfsmount *)(MP)->mnt_data)
379 #define VCBTOHFS(VCB) (((struct vfsVCB *)(VCB))->vcb_hfsmp)
380 #define FCBTOHFS(FCB) ((struct hfsmount *)(FCB)->ff_cp->c_vp->v_mount->mnt_data)
383 * Various ways to acquire a VCB pointer:
385 #define VTOVCB(VP) (&(((struct hfsmount *)((VP)->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
386 #define VFSTOVCB(MP) (&(((struct hfsmount *)(MP)->mnt_data)->hfs_vcb.vcb_vcb))
387 #define HFSTOVCB(HFSMP) (&(HFSMP)->hfs_vcb.vcb_vcb)
388 #define FCBTOVCB(FCB) (&(((struct hfsmount *)((FCB)->ff_cp->c_vp->v_mount->mnt_data))->hfs_vcb.vcb_vcb))
392 #define kHFSBlockSize 512
395 * Macros for getting the MDB/VH sector and offset
397 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
398 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
400 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
401 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
404 * This is the straight GMT conversion constant:
405 * 00:00:00 January 1, 1970 - 00:00:00 January 1, 1904
406 * (3600 * 24 * ((365 * (1970 - 1904)) + (((1970 - 1904) / 4) + 1)))
408 #define MAC_GMT_FACTOR 2082844800UL
411 u_int32_t
to_bsd_time(u_int32_t hfs_time
);
412 u_int32_t
to_hfs_time(u_int32_t bsd_time
);
414 int hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
);
415 int hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
416 #define HFS_ALTFLUSH 1
418 short hfsUnmount(struct hfsmount
*hfsmp
, struct proc
*p
);
421 extern int hfs_getcnode(struct hfsmount
*hfsmp
, cnid_t cnid
, struct cat_desc
*descp
,
422 int wantrsrc
, struct cat_attr
*attrp
, struct cat_fork
*forkp
,
425 extern int hfs_getnewvnode(struct hfsmount
*hfsmp
, struct cnode
*cp
,
426 struct cat_desc
*descp
, int wantrsrc
, struct cat_attr
*attrp
,
427 struct cat_fork
*forkp
, struct vnode
**vpp
);
429 extern int hfs_metafilelocking(struct hfsmount
*hfsmp
, u_long fileID
, u_int flags
, struct proc
*p
);
431 extern u_int32_t
hfs_freeblks(struct hfsmount
* hfsmp
, int wantreserve
);
433 extern void hfs_remove_orphans(struct hfsmount
*);
436 short MacToVFSError(OSErr err
);
438 extern int hfs_owner_rights(struct hfsmount
*hfsmp
, uid_t cnode_uid
, struct ucred
*cred
,
439 struct proc
*p
, int invokesuperuserstatus
);
441 u_long
FindMetaDataDirectory(ExtendedVCB
*vcb
);
443 #define kMaxSecsForFsync 5
444 #define HFS_SYNCTRANS 1
446 extern int hfs_btsync(struct vnode
*vp
, int sync_transaction
);
447 // used as a callback by the journaling code
448 extern void hfs_sync_metadata(void *arg
);
450 short make_dir_entry(FCB
**fileptr
, char *name
, u_int32_t fileID
);
453 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize
,
454 unsigned long blockSizeLimit
,
455 unsigned long baseMultiple
);
457 OSErr
hfs_MountHFSVolume(struct hfsmount
*hfsmp
, HFSMasterDirectoryBlock
*mdb
,
459 OSErr
hfs_MountHFSPlusVolume(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
460 off_t embeddedOffset
, u_int64_t disksize
, struct proc
*p
, void *args
);
462 extern int hfs_early_journal_init(struct hfsmount
*hfsmp
, HFSPlusVolumeHeader
*vhp
,
463 void *_args
, int embeddedOffset
, int mdb_offset
,
464 HFSMasterDirectoryBlock
*mdbp
, struct ucred
*cred
);
465 extern u_long
GetFileInfo(ExtendedVCB
*vcb
, u_int32_t dirid
, char *name
,
466 struct cat_attr
*fattr
, struct cat_fork
*forkinfo
);
468 int hfs_getconverter(u_int32_t encoding
, hfs_to_unicode_func_t
*get_unicode
,
469 unicode_to_hfs_func_t
*get_hfsname
);
471 int hfs_relconverter(u_int32_t encoding
);
473 int hfs_to_utf8(ExtendedVCB
*vcb
, Str31 hfs_str
, ByteCount maxDstLen
,
474 ByteCount
*actualDstLen
, unsigned char* dstStr
);
476 int utf8_to_hfs(ExtendedVCB
*vcb
, ByteCount srcLen
, const unsigned char* srcStr
,
479 int mac_roman_to_utf8(Str31 hfs_str
, ByteCount maxDstLen
, ByteCount
*actualDstLen
,
480 unsigned char* dstStr
);
482 int utf8_to_mac_roman(ByteCount srcLen
, const unsigned char* srcStr
, Str31 dstStr
);
484 u_int32_t
hfs_pickencoding(const u_int16_t
*src
, int len
);
486 enum volop
{VOL_UPDATE
, VOL_MKDIR
, VOL_RMDIR
, VOL_MKFILE
, VOL_RMFILE
};
488 extern int hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
);
490 extern void hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
);
493 extern void replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
);
495 extern int hfs_namecmp(const char *, size_t, const char *, size_t);
498 #endif /* __APPLE_API_PRIVATE */