]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
9bccf70c | 2 | * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. |
1c79356b A |
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 | */ | |
1c79356b A |
22 | |
23 | #ifndef __HFS__ | |
24 | #define __HFS__ | |
25 | ||
9bccf70c A |
26 | #include <sys/appleapiopts.h> |
27 | ||
28 | #ifdef KERNEL | |
29 | #ifdef __APPLE_API_PRIVATE | |
1c79356b A |
30 | #include <sys/param.h> |
31 | #include <sys/lock.h> | |
32 | #include <sys/queue.h> | |
9bccf70c A |
33 | #include <sys/mount.h> |
34 | #include <sys/namei.h> | |
35 | #include <sys/vnode.h> | |
36 | #include <sys/quota.h> | |
1c79356b A |
37 | #include <sys/dirent.h> |
38 | ||
39 | #include <hfs/hfs_format.h> | |
9bccf70c A |
40 | #include <hfs/hfs_catalog.h> |
41 | #include <hfs/hfs_cnode.h> | |
1c79356b A |
42 | #include <hfs/hfs_macos_defs.h> |
43 | #include <hfs/hfs_encodings.h> | |
44 | ||
45 | ||
9bccf70c A |
46 | struct uio; // This is more effective than #include <sys/uio.h> in case KERNEL is undefined... |
47 | struct hfslockf; /* For advisory locking */ | |
1c79356b A |
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 | ||
9bccf70c A |
56 | #define HFS_MAX_DEFERED_ALLOC (1024*1024) |
57 | ||
1c79356b A |
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 { | |
1c79356b A |
71 | kDataFork, |
72 | kRsrcFork, | |
9bccf70c | 73 | kDirectory |
1c79356b A |
74 | }; |
75 | ||
0b4e3aa0 A |
76 | /* number of locked buffer caches to hold for b-tree meta data */ |
77 | #define kMaxLockedMetaBuffers 32 | |
1c79356b A |
78 | |
79 | /* | |
80 | * File type and creator for symbolic links | |
81 | */ | |
82 | enum { | |
9bccf70c A |
83 | kSymLinkFileType = 0x736C6E6B, /* 'slnk' */ |
84 | kSymLinkCreator = 0x72686170 /* 'rhap' */ | |
1c79356b A |
85 | }; |
86 | ||
1c79356b A |
87 | |
88 | extern struct timezone gTimeZone; | |
89 | ||
1c79356b | 90 | |
0b4e3aa0 A |
91 | /* How many free extents to cache per volume */ |
92 | #define kMaxFreeExtents 10 | |
93 | ||
9bccf70c A |
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 | ||
1c79356b A |
104 | /* Internal Data structures*/ |
105 | ||
106 | struct vcb_t { | |
1c79356b | 107 | u_int16_t vcbSigWord; |
1c79356b | 108 | int16_t vcbAtrb; |
9bccf70c A |
109 | int16_t vcbFlags; |
110 | int16_t vcbspare; | |
0b4e3aa0 A |
111 | |
112 | u_int32_t vcbCrDate; | |
113 | u_int32_t vcbLsMod; | |
1c79356b | 114 | u_int32_t vcbVolBkUp; |
0b4e3aa0 | 115 | |
1c79356b A |
116 | int32_t vcbFilCnt; |
117 | int32_t vcbDirCnt; | |
9bccf70c A |
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 */ | |
0b4e3aa0 A |
122 | int32_t vcbClpSiz; |
123 | u_int32_t vcbNxtCNID; | |
9bccf70c A |
124 | u_int32_t vcbCNIDGen; |
125 | int32_t vcbWrCnt; | |
0b4e3aa0 | 126 | |
1c79356b | 127 | int32_t vcbFndrInfo[8]; |
0b4e3aa0 A |
128 | |
129 | u_int64_t encodingsBitmap; /* HFS Plus only */ | |
130 | ||
9bccf70c | 131 | u_int16_t vcbNmFls; /* HFS only */ |
0b4e3aa0 | 132 | u_int16_t vcbNmRtDirs; /* HFS only */ |
9bccf70c A |
133 | int16_t vcbVBMSt; /* HFS only */ |
134 | int16_t vcbAlBlSt; /* HFS only */ | |
0b4e3aa0 | 135 | |
1c79356b A |
136 | struct vnode * extentsRefNum; |
137 | struct vnode * catalogRefNum; | |
138 | struct vnode * allocationsRefNum; | |
0b4e3aa0 | 139 | |
9bccf70c | 140 | u_int8_t vcbVN[256]; /* volume name in UTF-8 */ |
0b4e3aa0 | 141 | u_int32_t volumeNameEncodingHint; |
9bccf70c | 142 | u_int32_t hfsPlusIOPosOffset; /* Disk block where HFS+ starts */ |
0b4e3aa0 | 143 | u_int32_t vcbVBMIOSize; /* volume bitmap I/O size */ |
0b4e3aa0 | 144 | |
9bccf70c A |
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 */ | |
0b4e3aa0 | 151 | |
1c79356b A |
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 | ||
0b4e3aa0 | 157 | |
9bccf70c | 158 | #define kHFS_DamagedVolume 0x1 /* This volume has errors, unmount dirty */ |
1c79356b | 159 | |
9bccf70c A |
160 | /* XXX */ |
161 | #define MARK_VOLUMEDAMAGED(fcb) | |
1c79356b A |
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 { | |
1c79356b A |
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 */ | |
9bccf70c | 182 | u_int8_t hfs_media_writeable; |
1c79356b A |
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; | |
9bccf70c A |
207 | struct cat_desc hfs_privdir_desc; |
208 | struct cat_attr hfs_privdir_attr; | |
1c79356b A |
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; | |
9bccf70c A |
212 | |
213 | struct quotafile hfs_qfiles[MAXQUOTAS]; /* quota files */ | |
1c79356b A |
214 | } hfsmount_t; |
215 | ||
9bccf70c | 216 | #define hfs_private_metadata_dir hfs_privdir_desc.cd_cnid |
1c79356b A |
217 | |
218 | #define MAXHFSVNODELEN 31 | |
1c79356b | 219 | |
1c79356b | 220 | |
9bccf70c | 221 | typedef struct filefork FCB; |
1c79356b | 222 | |
1c79356b A |
223 | |
224 | #define MAKE_INODE_NAME(name,linkno) \ | |
225 | (void) sprintf((name), "%s%d", HFS_INODE_PREFIX, (linkno)) | |
226 | ||
1c79356b A |
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 | ||
1c79356b A |
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 | ||
9bccf70c A |
247 | #define HFS_AVERAGE_NAME_SIZE 22 |
248 | #define AVERAGE_HFSDIRENTRY_SIZE (8+HFS_AVERAGE_NAME_SIZE+4) | |
1c79356b A |
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 | ||
1c79356b A |
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 | * | |
1c79356b A |
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; | |
765c9de3 A |
294 | u_int32_t cnd_linkCNID; /* for hard links only */ |
295 | u_int8_t cnd_extra[264]; /* make struct at least 520 bytes long */ | |
1c79356b A |
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 | ||
9bccf70c | 303 | |
1c79356b A |
304 | |
305 | enum { kHFSPlusMaxFileNameBytes = kHFSPlusMaxFileNameChars * 3 }; | |
306 | ||
307 | enum { kdirentMaxNameBytes = NAME_MAX }; | |
308 | ||
1c79356b A |
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 | ||
1c79356b A |
315 | /* |
316 | * Various ways to acquire a VFS mount point pointer: | |
317 | */ | |
318 | #define VTOVFS(VP) ((VP)->v_mount) | |
1c79356b A |
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)) | |
1c79356b A |
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)) | |
1c79356b A |
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 | |
1c79356b | 339 | |
9bccf70c A |
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) | |
d52fe63f A |
345 | |
346 | #define HFS_ALT_SECTOR(blksize, blkcnt) (((blkcnt) - 1) - (512 / (blksize))) | |
347 | #define HFS_ALT_OFFSET(blksize) ((blksize) > 1024 ? (blksize) - 1024 : 0) | |
348 | ||
1c79356b A |
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 | ||
1c79356b A |
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 | ||
9bccf70c A |
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 | |
1c79356b | 363 | |
1c79356b A |
364 | short hfsUnmount(struct hfsmount *hfsmp, struct proc *p); |
365 | ||
9bccf70c A |
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 | ||
1c79356b | 375 | extern int hfs_metafilelocking(struct hfsmount *hfsmp, u_long fileID, u_int flags, struct proc *p); |
1c79356b | 376 | |
9bccf70c A |
377 | extern u_int32_t hfs_freeblks(struct hfsmount * hfsmp, int wantreserve); |
378 | ||
1c79356b A |
379 | |
380 | short MacToVFSError(OSErr err); | |
1c79356b | 381 | |
9bccf70c A |
382 | extern int hfs_owner_rights(struct hfsmount *hfsmp, uid_t cnode_uid, struct ucred *cred, |
383 | struct proc *p, int invokesuperuserstatus); | |
384 | ||
1c79356b A |
385 | u_long FindMetaDataDirectory(ExtendedVCB *vcb); |
386 | ||
9bccf70c A |
387 | #define kMaxSecsForFsync 5 |
388 | #define HFS_SYNCTRANS 1 | |
389 | ||
390 | extern int hfs_btsync(struct vnode *vp, int sync_transaction); | |
1c79356b A |
391 | |
392 | short make_dir_entry(FCB **fileptr, char *name, u_int32_t fileID); | |
393 | ||
9bccf70c | 394 | |
1c79356b A |
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, | |
d52fe63f | 400 | struct proc *p); |
1c79356b | 401 | OSErr hfs_MountHFSPlusVolume(struct hfsmount *hfsmp, HFSPlusVolumeHeader *vhp, |
9bccf70c | 402 | off_t embeddedOffset, u_int64_t disksize, struct proc *p); |
1c79356b A |
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 | ||
0b4e3aa0 A |
420 | u_int32_t hfs_pickencoding(const u_int16_t *src, int len); |
421 | ||
9bccf70c A |
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 */ | |
1c79356b | 433 | #endif /* __HFS__ */ |