]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs.h
xnu-344.tar.gz
[apple/xnu.git] / bsd / hfs / hfs.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #ifndef __HFS__
24 #define __HFS__
25
26 #include <sys/appleapiopts.h>
27
28 #ifdef KERNEL
29 #ifdef __APPLE_API_PRIVATE
30 #include <sys/param.h>
31 #include <sys/lock.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>
38
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>
44
45
46 struct uio; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined...
47 struct hfslockf; /* For advisory locking */
48
49 /*
50 * Just reported via MIG interface.
51 */
52 #define VERSION_STRING "hfs-2 (4-12-99)"
53
54 #define HFS_LINK_MAX 32767
55
56 #define HFS_MAX_DEFERED_ALLOC (1024*1024)
57
58
59 enum { kMDBSize = 512 }; /* Size of I/O transfer to read entire MDB */
60
61 enum { kMasterDirectoryBlock = 2 }; /* MDB offset on disk in 512-byte blocks */
62 enum { kMDBOffset = kMasterDirectoryBlock * 512 }; /* MDB offset on disk in bytes */
63
64 enum {
65 kUnknownID = 0,
66 kRootParID = 1,
67 kRootDirID = 2
68 };
69
70 enum {
71 kDataFork,
72 kRsrcFork,
73 kDirectory
74 };
75
76 /* number of locked buffer caches to hold for b-tree meta data */
77 #define kMaxLockedMetaBuffers 32
78
79 /*
80 * File type and creator for symbolic links
81 */
82 enum {
83 kSymLinkFileType = 0x736C6E6B, /* 'slnk' */
84 kSymLinkCreator = 0x72686170 /* 'rhap' */
85 };
86
87
88 extern struct timezone gTimeZone;
89
90
91 /* How many free extents to cache per volume */
92 #define kMaxFreeExtents 10
93
94 /*
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.
100 */
101 #define HFS_MINFREE 1
102 #define HFS_MAXRESERVE (u_int64_t)(250*1024*1024)
103
104 /* Internal Data structures*/
105
106 struct vcb_t {
107 u_int16_t vcbSigWord;
108 int16_t vcbAtrb;
109 int16_t vcbFlags;
110 int16_t vcbspare;
111
112 u_int32_t vcbCrDate;
113 u_int32_t vcbLsMod;
114 u_int32_t vcbVolBkUp;
115
116 int32_t vcbFilCnt;
117 int32_t vcbDirCnt;
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 */
122 int32_t vcbClpSiz;
123 u_int32_t vcbNxtCNID;
124 u_int32_t vcbCNIDGen;
125 int32_t vcbWrCnt;
126
127 int32_t vcbFndrInfo[8];
128
129 u_int64_t encodingsBitmap; /* HFS Plus only */
130
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 */
135
136 struct vnode * extentsRefNum;
137 struct vnode * catalogRefNum;
138 struct vnode * allocationsRefNum;
139
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 */
144
145 /* cache of largest known free extents */
146 u_int32_t vcbFreeExtCnt;
147 HFSPlusExtentDescriptor vcbFreeExt[kMaxFreeExtents];
148
149 u_int32_t reserveBlocks; /* free block reserve */
150 u_int32_t loanedBlocks; /* blocks on loan for delayed allocations */
151
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 */
154 };
155 typedef struct vcb_t ExtendedVCB;
156
157
158 #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */
159
160 /* XXX */
161 #define MARK_VOLUMEDAMAGED(fcb)
162
163 /*
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!!
167 *
168 * vcbFlags, vcbLsMod, vcbFilCnt, vcbDirCnt, vcbNxtCNID, etc
169 * are locked by the hfs_lock simple lock.
170 */
171 typedef struct vfsVCB {
172 ExtendedVCB vcb_vcb;
173 struct hfsmount *vcb_hfsmp; /* Pointer to hfsmount structure */
174 } vfsVCB_t;
175
176
177
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;
183
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 */
187
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 */
194
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 */
201
202 /* simple lock for shared meta renaming */
203 simple_lock_data_t hfs_renamelock;
204
205 /* HFS Specific */
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;
212
213 struct quotafile hfs_qfiles[MAXQUOTAS]; /* quota files */
214 } hfsmount_t;
215
216 #define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid
217
218 #define MAXHFSVNODELEN 31
219
220
221 typedef struct filefork FCB;
222
223
224 #define MAKE_INODE_NAME(name,linkno) \
225 (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno))
226
227 /*
228 * Write check macro
229 */
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", \
233 FUNC_NAME)); \
234 return(EROFS); \
235 } \
236 }
237
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 ".." */
245 } hfsdotentry;
246
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)
250
251 #define DIRENTRY_SIZE(namlen) \
252 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen)+1 + 3) &~ 3))
253
254
255 enum {
256 kCatalogFolderNode = 1,
257 kCatalogFileNode = 2
258 };
259
260 /*
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.
263 *
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.
266 *
267 */
268 struct CatalogNodeData {
269 int16_t cnd_type;
270 u_int16_t cnd_flags;
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 */
283 union {
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 */
287 } cnd_un;
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 */
296 };
297 typedef struct CatalogNodeData CatalogNodeData;
298
299 #define cnd_iNodeNum cnd_un.cndu_iNodeNum
300 #define cnd_linkCount cnd_un.cndu_linkCount
301 #define cnd_rawDevice cnd_un.cndu_rawDevice
302
303
304
305 enum { kHFSPlusMaxFileNameBytes = kHFSPlusMaxFileNameChars * 3 };
306
307 enum { kdirentMaxNameBytes = NAME_MAX };
308
309
310 /* macro to determine if hfs or hfsplus */
311 #define ISHFSPLUS(VCB) ((VCB)->vcbSigWord == kHFSPlusSigWord)
312 #define ISHFS(VCB) ((VCB)->vcbSigWord == kHFSSigWord)
313
314
315 /*
316 * Various ways to acquire a VFS mount point pointer:
317 */
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)
321
322 /*
323 * Various ways to acquire an HFS mount point pointer:
324 */
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)
328
329 /*
330 * Various ways to acquire a VCB pointer:
331 */
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)
335
336
337 #define E_NONE 0
338 #define kHFSBlockSize 512
339
340 /*
341 * Macros for getting the MDB/VH sector and offset
342 */
343 #define HFS_PRI_SECTOR(blksize) (1024 / (blksize))
344 #define HFS_PRI_OFFSET(blksize) ((blksize) > 1024 ? 1024 : 0)
345
346 #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize)))
347 #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0)
348
349 /*
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)))
353 */
354 #define MAC_GMT_FACTOR 2082844800UL
355
356
357 u_int32_t to_bsd_time(u_int32_t hfs_time);
358 u_int32_t to_hfs_time(u_int32_t bsd_time);
359
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
363
364 short hfsUnmount(struct hfsmount *hfsmp, struct proc *p);
365
366
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,
369 struct vnode **vpp);
370
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);
374
375 extern int hfs_metafilelocking(struct hfsmount *hfsmp, u_long fileID, u_int flags, struct proc *p);
376
377 extern u_int32_t hfs_freeblks(struct hfsmount * hfsmp, int wantreserve);
378
379
380 short MacToVFSError(OSErr err);
381
382 extern int hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, struct ucred *cred,
383 struct proc *p, int invokesuperuserstatus);
384
385 u_long FindMetaDataDirectory(ExtendedVCB *vcb);
386
387 #define kMaxSecsForFsync 5
388 #define HFS_SYNCTRANS 1
389
390 extern int hfs_btsync(struct vnode *vp, int sync_transaction);
391
392 short make_dir_entry(FCB **fileptr, char *name, u_int32_t fileID);
393
394
395 unsigned long BestBlockSizeFit(unsigned long allocationBlockSize,
396 unsigned long blockSizeLimit,
397 unsigned long baseMultiple);
398
399 OSErr hfs_MountHFSVolume(struct hfsmount *hfsmp, HFSMasterDirectoryBlock *mdb,
400 struct proc *p);
401 OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp,
402 off_t embeddedOffset, u_int64_t disksize, struct proc *p);
403
404 int hfs_getconverter(u_int32_t encoding, hfs_to_unicode_func_t *get_unicode,
405 unicode_to_hfs_func_t *get_hfsname);
406
407 int hfs_relconverter(u_int32_t encoding);
408
409 int hfs_to_utf8(ExtendedVCB *vcb, Str31 hfs_str, ByteCount maxDstLen,
410 ByteCount *actualDstLen, unsigned char* dstStr);
411
412 int utf8_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, const unsigned char* srcStr,
413 Str31 dstStr);
414
415 int mac_roman_to_utf8(Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen,
416 unsigned char* dstStr);
417
418 int utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr);
419
420 u_int32_t hfs_pickencoding(const u_int16_t *src, int len);
421
422 enum volop {VOL_UPDATE, VOL_MKDIR, VOL_RMDIR, VOL_MKFILE, VOL_RMFILE};
423
424 extern int hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot);
425
426 extern void hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding);
427
428
429 extern void replace_desc(struct cnode *cp, struct cat_desc *cdp);
430
431 #endif /* __APPLE_API_PRIVATE */
432 #endif /* KERNEL */
433 #endif /* __HFS__ */