]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
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. | |
1c79356b | 11 | * |
e5568f75 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
1c79356b A |
22 | |
23 | #include <sys/param.h> | |
24 | #include <sys/systm.h> | |
25 | #include <sys/buf.h> | |
9bccf70c | 26 | #include <sys/kernel.h> |
91447636 | 27 | #include <sys/malloc.h> |
1c79356b A |
28 | #include <sys/mount.h> |
29 | #include <sys/vnode.h> | |
30 | ||
31 | ||
32 | #include "hfs.h" | |
9bccf70c | 33 | #include "hfs_cnode.h" |
1c79356b A |
34 | #include "hfs_dbg.h" |
35 | #include "hfs_endian.h" | |
36 | ||
37 | #include "hfscommon/headers/FileMgrInternal.h" | |
38 | #include "hfscommon/headers/BTreesPrivate.h" | |
39 | ||
40 | #define FORCESYNCBTREEWRITES 0 | |
41 | ||
1c79356b A |
42 | |
43 | static int ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount); | |
44 | ||
45 | ||
9bccf70c | 46 | __private_extern__ |
1c79356b A |
47 | OSStatus SetBTreeBlockSize(FileReference vp, ByteCount blockSize, ItemCount minBlockCount) |
48 | { | |
49 | BTreeControlBlockPtr bTreePtr; | |
50 | ||
51 | DBG_ASSERT(vp != NULL); | |
1c79356b A |
52 | DBG_ASSERT(blockSize >= kMinNodeSize); |
53 | if (blockSize > MAXBSIZE ) | |
54 | return (fsBTBadNodeSize); | |
55 | ||
9bccf70c | 56 | bTreePtr = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr; |
1c79356b A |
57 | bTreePtr->nodeSize = blockSize; |
58 | ||
59 | return (E_NONE); | |
60 | } | |
61 | ||
62 | ||
9bccf70c | 63 | __private_extern__ |
1c79356b A |
64 | OSStatus GetBTreeBlock(FileReference vp, UInt32 blockNum, GetBlockOptions options, BlockDescriptor *block) |
65 | { | |
66 | OSStatus retval = E_NONE; | |
67 | struct buf *bp = NULL; | |
68 | ||
91447636 A |
69 | if (options & kGetEmptyBlock) { |
70 | daddr64_t blkno; | |
71 | off_t offset; | |
1c79356b | 72 | |
91447636 A |
73 | offset = (daddr64_t)blockNum * (daddr64_t)block->blockSize; |
74 | bp = buf_getblk(vp, (daddr64_t)blockNum, block->blockSize, 0, 0, BLK_META); | |
75 | if (bp && | |
76 | VNOP_BLOCKMAP(vp, offset, block->blockSize, &blkno, NULL, NULL, 0, NULL) == 0) { | |
77 | buf_setblkno(bp, blkno); | |
78 | } | |
79 | } else { | |
80 | retval = buf_meta_bread(vp, (daddr64_t)blockNum, block->blockSize, NOCRED, &bp); | |
81 | } | |
1c79356b A |
82 | if (bp == NULL) |
83 | retval = -1; //XXX need better error | |
84 | ||
85 | if (retval == E_NONE) { | |
86 | block->blockHeader = bp; | |
91447636 A |
87 | block->buffer = (char *)buf_dataptr(bp); |
88 | block->blockReadFromDisk = (buf_fromcache(bp) == 0); /* not found in cache ==> came from disk */ | |
1c79356b | 89 | |
b4c24cb9 A |
90 | // XXXdbg |
91 | block->isModified = 0; | |
92 | ||
1c79356b A |
93 | #if BYTE_ORDER == LITTLE_ENDIAN |
94 | /* Endian swap B-Tree node (only if it's a valid block) */ | |
95 | if (!(options & kGetEmptyBlock)) { | |
96 | /* This happens when we first open the b-tree, we might not have all the node data on hand */ | |
97 | if ((((BTNodeDescriptor *)block->buffer)->kind == kBTHeaderNode) && | |
91447636 A |
98 | (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize != buf_count(bp)) && |
99 | (SWAP_BE16 (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize) != buf_count(bp))) { | |
1c79356b A |
100 | |
101 | /* Don't swap the descriptors at all, we don't care (this block will be invalidated) */ | |
9bccf70c | 102 | SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), VTOC(vp)->c_fileid, 3); |
1c79356b A |
103 | |
104 | /* The node needs swapping */ | |
105 | } else if (*((UInt16 *)((char *)block->buffer + (block->blockSize - sizeof (UInt16)))) == 0x0e00) { | |
9bccf70c | 106 | SWAP_BT_NODE (block, ISHFSPLUS(VTOVCB(vp)), VTOC(vp)->c_fileid, 0); |
1c79356b A |
107 | #if 0 |
108 | /* The node is not already in native byte order, hence corrupt */ | |
109 | } else if (*((UInt16 *)((char *)block->buffer + (block->blockSize - sizeof (UInt16)))) != 0x000e) { | |
110 | panic ("%s Corrupt B-Tree node detected!\n", "GetBTreeBlock:"); | |
111 | #endif | |
112 | } | |
113 | } | |
114 | #endif | |
115 | } else { | |
116 | if (bp) | |
91447636 | 117 | buf_brelse(bp); |
1c79356b A |
118 | block->blockHeader = NULL; |
119 | block->buffer = NULL; | |
120 | } | |
121 | ||
122 | return (retval); | |
123 | } | |
124 | ||
125 | ||
b4c24cb9 A |
126 | __private_extern__ |
127 | void ModifyBlockStart(FileReference vp, BlockDescPtr blockPtr) | |
128 | { | |
129 | struct hfsmount *hfsmp = VTOHFS(vp); | |
130 | struct buf *bp = NULL; | |
131 | ||
132 | if (hfsmp->jnl == NULL) { | |
133 | return; | |
134 | } | |
135 | ||
136 | bp = (struct buf *) blockPtr->blockHeader; | |
137 | if (bp == NULL) { | |
138 | panic("ModifyBlockStart: null bp for blockdescptr 0x%x?!?\n", blockPtr); | |
139 | return; | |
140 | } | |
141 | ||
142 | journal_modify_block_start(hfsmp->jnl, bp); | |
143 | blockPtr->isModified = 1; | |
144 | } | |
145 | ||
55e303ae A |
146 | static int |
147 | btree_journal_modify_block_end(struct hfsmount *hfsmp, struct buf *bp) | |
148 | { | |
149 | #if BYTE_ORDER == LITTLE_ENDIAN | |
91447636 | 150 | struct vnode *vp = buf_vnode(bp); |
55e303ae A |
151 | BlockDescriptor block; |
152 | ||
153 | /* Prepare the block pointer */ | |
154 | block.blockHeader = bp; | |
91447636 | 155 | block.buffer = (char *)buf_dataptr(bp); |
55e303ae | 156 | /* not found in cache ==> came from disk */ |
91447636 A |
157 | block.blockReadFromDisk = (buf_fromcache(bp) == 0); |
158 | block.blockSize = buf_count(bp); | |
55e303ae A |
159 | |
160 | // XXXdbg have to swap the data before it goes in the journal | |
161 | SWAP_BT_NODE (&block, ISHFSPLUS (VTOVCB(vp)), VTOC(vp)->c_fileid, 1); | |
162 | #endif | |
163 | ||
164 | return journal_modify_block_end(hfsmp->jnl, bp); | |
165 | } | |
166 | ||
b4c24cb9 | 167 | |
9bccf70c | 168 | __private_extern__ |
1c79356b A |
169 | OSStatus ReleaseBTreeBlock(FileReference vp, BlockDescPtr blockPtr, ReleaseBlockOptions options) |
170 | { | |
b4c24cb9 | 171 | struct hfsmount *hfsmp = VTOHFS(vp); |
9bccf70c | 172 | extern int bdwrite_internal(struct buf *, int); |
1c79356b A |
173 | OSStatus retval = E_NONE; |
174 | struct buf *bp = NULL; | |
175 | ||
176 | bp = (struct buf *) blockPtr->blockHeader; | |
177 | ||
178 | if (bp == NULL) { | |
1c79356b A |
179 | retval = -1; |
180 | goto exit; | |
181 | } | |
182 | ||
183 | if (options & kTrashBlock) { | |
91447636 A |
184 | buf_markinvalid(bp); |
185 | ||
186 | if (hfsmp->jnl && (buf_flags(bp) & B_LOCKED)) { | |
b4c24cb9 A |
187 | journal_kill_block(hfsmp->jnl, bp); |
188 | } else { | |
91447636 | 189 | buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */ |
b4c24cb9 | 190 | } |
1c79356b A |
191 | } else { |
192 | if (options & kForceWriteBlock) { | |
b4c24cb9 A |
193 | if (hfsmp->jnl) { |
194 | if (blockPtr->isModified == 0) { | |
195 | panic("hfs: releaseblock: modified is 0 but forcewrite set! bp 0x%x\n", bp); | |
196 | } | |
55e303ae A |
197 | |
198 | retval = btree_journal_modify_block_end(hfsmp, bp); | |
b4c24cb9 A |
199 | blockPtr->isModified = 0; |
200 | } else { | |
91447636 | 201 | retval = VNOP_BWRITE(bp); |
b4c24cb9 | 202 | } |
1c79356b | 203 | } else if (options & kMarkBlockDirty) { |
91447636 A |
204 | struct timeval tv; |
205 | microuptime(&tv); | |
b4c24cb9 | 206 | if ((options & kLockTransaction) && hfsmp->jnl == NULL) { |
9bccf70c A |
207 | /* |
208 | * | |
91447636 | 209 | * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move |
9bccf70c | 210 | * the buffer onto the LOCKED free list. This is necessary, otherwise |
91447636 | 211 | * getnewbuf() would try to reclaim the buffers using buf_bawrite, which |
9bccf70c A |
212 | * isn't going to work. |
213 | * | |
214 | */ | |
91447636 A |
215 | extern int count_lock_queue(void); |
216 | ||
9bccf70c A |
217 | /* Don't hog all the buffers... */ |
218 | if (count_lock_queue() > kMaxLockedMetaBuffers) { | |
219 | hfs_btsync(vp, HFS_SYNCTRANS); | |
220 | /* Rollback sync time to cause a sync on lock release... */ | |
91447636 | 221 | (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1)); |
9bccf70c | 222 | } |
91447636 | 223 | buf_setflags(bp, B_LOCKED); |
b4c24cb9 A |
224 | } |
225 | ||
9bccf70c A |
226 | /* |
227 | * Delay-write this block. | |
228 | * If the maximum delayed buffers has been exceeded then | |
229 | * free up some buffers and fall back to an asynchronous write. | |
230 | */ | |
b4c24cb9 A |
231 | if (hfsmp->jnl) { |
232 | if (blockPtr->isModified == 0) { | |
233 | panic("hfs: releaseblock: modified is 0 but markdirty set! bp 0x%x\n", bp); | |
234 | } | |
55e303ae | 235 | retval = btree_journal_modify_block_end(hfsmp, bp); |
b4c24cb9 A |
236 | blockPtr->isModified = 0; |
237 | } else if (bdwrite_internal(bp, 1) != 0) { | |
9bccf70c A |
238 | hfs_btsync(vp, 0); |
239 | /* Rollback sync time to cause a sync on lock release... */ | |
91447636 A |
240 | (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1)); |
241 | ||
242 | buf_clearflags(bp, B_LOCKED); | |
243 | buf_bawrite(bp); | |
9bccf70c | 244 | } |
1c79356b | 245 | } else { |
b4c24cb9 | 246 | // check if we had previously called journal_modify_block_start() |
91447636 | 247 | // on this block and if so, abort it (which will call buf_brelse()). |
b4c24cb9 A |
248 | if (hfsmp->jnl && blockPtr->isModified) { |
249 | // XXXdbg - I don't want to call modify_block_abort() | |
250 | // because I think it may be screwing up the | |
251 | // journal and blowing away a block that has | |
252 | // valid data in it. | |
253 | // | |
254 | // journal_modify_block_abort(hfsmp->jnl, bp); | |
255 | //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp); | |
55e303ae | 256 | btree_journal_modify_block_end(hfsmp, bp); |
b4c24cb9 A |
257 | blockPtr->isModified = 0; |
258 | } else { | |
91447636 | 259 | buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */ |
b4c24cb9 | 260 | } |
1c79356b A |
261 | }; |
262 | }; | |
263 | ||
264 | exit: | |
265 | return (retval); | |
266 | } | |
267 | ||
268 | ||
9bccf70c | 269 | __private_extern__ |
1c79356b A |
270 | OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF) |
271 | { | |
272 | #pragma unused (maxEOF) | |
273 | ||
91447636 | 274 | OSStatus retval = 0, ret = 0; |
b4c24cb9 | 275 | UInt64 actualBytesAdded, origSize; |
1c79356b | 276 | UInt64 bytesToAdd; |
9bccf70c A |
277 | u_int32_t startAllocation; |
278 | u_int32_t fileblocks; | |
1c79356b A |
279 | BTreeInfoRec btInfo; |
280 | ExtendedVCB *vcb; | |
281 | FCB *filePtr; | |
282 | struct proc *p = NULL; | |
91447636 A |
283 | UInt64 trim = 0; |
284 | int lockflags = 0; | |
1c79356b A |
285 | |
286 | filePtr = GetFileControlBlock(vp); | |
287 | ||
288 | if ( minEOF > filePtr->fcbEOF ) | |
289 | { | |
290 | bytesToAdd = minEOF - filePtr->fcbEOF; | |
291 | ||
9bccf70c A |
292 | if (bytesToAdd < filePtr->ff_clumpsize) |
293 | bytesToAdd = filePtr->ff_clumpsize; //XXX why not always be a mutiple of clump size? | |
1c79356b A |
294 | } |
295 | else | |
296 | { | |
1c79356b A |
297 | return -1; |
298 | } | |
299 | ||
9bccf70c | 300 | vcb = VTOVCB(vp); |
1c79356b A |
301 | |
302 | /* | |
303 | * The Extents B-tree can't have overflow extents. ExtendFileC will | |
304 | * return an error if an attempt is made to extend the Extents B-tree | |
305 | * when the resident extents are exhausted. | |
306 | */ | |
1c79356b | 307 | |
91447636 A |
308 | /* Protect allocation bitmap and extents overflow file. */ |
309 | lockflags = SFL_BITMAP; | |
310 | if (VTOC(vp)->c_fileid != kHFSExtentsFileID) | |
311 | lockflags |= SFL_EXTENTS; | |
312 | lockflags = hfs_systemfile_lock(vcb, lockflags, HFS_EXCLUSIVE_LOCK); | |
313 | ||
314 | (void) BTGetInformation(filePtr, 0, &btInfo); | |
1c79356b | 315 | |
b4c24cb9 | 316 | #if 0 // XXXdbg |
1c79356b | 317 | /* |
9bccf70c | 318 | * The b-tree code expects nodes to be contiguous. So when |
1c79356b | 319 | * the allocation block size is less than the b-tree node |
9bccf70c A |
320 | * size, we need to force disk allocations to be contiguous. |
321 | */ | |
1c79356b A |
322 | if (vcb->blockSize >= btInfo.nodeSize) { |
323 | extendFlags = 0; | |
324 | } else { | |
325 | /* Ensure that all b-tree nodes are contiguous on disk */ | |
b4c24cb9 | 326 | extendFlags = kEFContigMask; |
1c79356b | 327 | } |
b4c24cb9 | 328 | #endif |
1c79356b | 329 | |
b4c24cb9 | 330 | origSize = filePtr->fcbEOF; |
9bccf70c A |
331 | fileblocks = filePtr->ff_blocks; |
332 | startAllocation = vcb->nextAllocation; | |
1c79356b | 333 | |
b4c24cb9 A |
334 | // loop trying to get a contiguous chunk that's an integer multiple |
335 | // of the btree node size. if we can't get a contiguous chunk that | |
336 | // is at least the node size then we break out of the loop and let | |
337 | // the error propagate back up. | |
338 | do { | |
55e303ae A |
339 | retval = ExtendFileC(vcb, filePtr, bytesToAdd, 0, |
340 | kEFContigMask | kEFMetadataMask, | |
341 | &actualBytesAdded); | |
b4c24cb9 A |
342 | if (retval == dskFulErr && actualBytesAdded == 0) { |
343 | ||
344 | if (bytesToAdd == btInfo.nodeSize || bytesToAdd < (minEOF - origSize)) { | |
345 | // if we're here there's nothing else to try, we're out | |
346 | // of space so we break and bail out. | |
347 | break; | |
348 | } else { | |
349 | bytesToAdd >>= 1; | |
350 | if (bytesToAdd < btInfo.nodeSize) { | |
351 | bytesToAdd = btInfo.nodeSize; | |
352 | } else if ((bytesToAdd % btInfo.nodeSize) != 0) { | |
353 | // make sure it's an integer multiple of the nodeSize | |
354 | bytesToAdd -= (bytesToAdd % btInfo.nodeSize); | |
355 | } | |
356 | } | |
357 | } | |
358 | } while (retval == dskFulErr && actualBytesAdded == 0); | |
91447636 | 359 | |
9bccf70c A |
360 | /* |
361 | * If a new extent was added then move the roving allocator | |
362 | * reference forward by the current b-tree file size so | |
363 | * there's plenty of room to grow. | |
364 | */ | |
365 | if ((retval == 0) && | |
55e303ae | 366 | ((VCBTOHFS(vcb)->hfs_flags & HFS_METADATA_ZONE) == 0) && |
9bccf70c A |
367 | (vcb->nextAllocation > startAllocation) && |
368 | ((vcb->nextAllocation + fileblocks) < vcb->totalBlocks)) { | |
369 | vcb->nextAllocation += fileblocks; | |
370 | } | |
371 | ||
b4c24cb9 A |
372 | filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize; |
373 | ||
374 | // XXXdbg ExtendFileC() could have returned an error even though | |
375 | // it grew the file to be big enough for our needs. If this is | |
376 | // the case, we don't care about retval so we blow it away. | |
377 | // | |
378 | if (filePtr->fcbEOF >= minEOF && retval != 0) { | |
379 | retval = 0; | |
380 | } | |
381 | ||
382 | // XXXdbg if the file grew but isn't large enough or isn't an | |
383 | // even multiple of the nodeSize then trim things back. if | |
384 | // the file isn't large enough we trim back to the original | |
385 | // size. otherwise we trim back to be an even multiple of the | |
386 | // btree node size. | |
387 | // | |
388 | if ((filePtr->fcbEOF < minEOF) || (actualBytesAdded % btInfo.nodeSize) != 0) { | |
389 | ||
390 | if (filePtr->fcbEOF < minEOF) { | |
391 | retval = dskFulErr; | |
392 | ||
393 | if (filePtr->fcbEOF < origSize) { | |
394 | panic("hfs: btree file eof %lld less than orig size %lld!\n", | |
395 | filePtr->fcbEOF, origSize); | |
396 | } | |
397 | ||
398 | trim = filePtr->fcbEOF - origSize; | |
399 | if (trim != actualBytesAdded) { | |
400 | panic("hfs: trim == %lld but actualBytesAdded == %lld\n", | |
401 | trim, actualBytesAdded); | |
402 | } | |
403 | } else { | |
404 | trim = (actualBytesAdded % btInfo.nodeSize); | |
405 | } | |
406 | ||
407 | ret = TruncateFileC(vcb, filePtr, filePtr->fcbEOF - trim, 0); | |
408 | filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize; | |
409 | ||
410 | // XXXdbg - panic if the file didn't get trimmed back properly | |
411 | if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) { | |
412 | panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb 0x%x\n", | |
413 | filePtr->fcbEOF, btInfo.nodeSize, filePtr); | |
414 | } | |
415 | ||
416 | if (ret) { | |
417 | // XXXdbg - this probably doesn't need to be a panic() | |
418 | panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %d)\n", | |
419 | filePtr->fcbEOF, trim, ret); | |
91447636 | 420 | goto out; |
b4c24cb9 A |
421 | } |
422 | actualBytesAdded -= trim; | |
423 | } | |
424 | ||
9bccf70c A |
425 | if(VTOC(vp)->c_fileid != kHFSExtentsFileID) { |
426 | /* | |
427 | * Get any extents overflow b-tree changes to disk ASAP! | |
428 | */ | |
b4c24cb9 | 429 | (void) BTFlushPath(VTOF(vcb->extentsRefNum)); |
91447636 | 430 | (void) hfs_fsync(vcb->extentsRefNum, MNT_WAIT, 0, p); |
9bccf70c | 431 | } |
91447636 A |
432 | hfs_systemfile_unlock(vcb, lockflags); |
433 | lockflags = 0; | |
1c79356b | 434 | |
b4c24cb9 A |
435 | if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) { |
436 | panic("hfs: extendbtree: fcb 0x%x has eof 0x%llx not a multiple of 0x%x (trim %llx)\n", | |
437 | filePtr, filePtr->fcbEOF, btInfo.nodeSize, trim); | |
438 | } | |
439 | ||
1c79356b A |
440 | /* |
441 | * Update the Alternate MDB or Alternate VolumeHeader | |
442 | */ | |
9bccf70c A |
443 | if ((VTOC(vp)->c_fileid == kHFSExtentsFileID) || |
444 | (VTOC(vp)->c_fileid == kHFSCatalogFileID) || | |
445 | (VTOC(vp)->c_fileid == kHFSAttributesFileID) | |
1c79356b | 446 | ) { |
91447636 | 447 | VTOC(vp)->c_flag |= C_MODIFIED; |
1c79356b | 448 | MarkVCBDirty( vcb ); |
b4c24cb9 | 449 | ret = hfs_flushvolumeheader(VCBTOHFS(vcb), MNT_WAIT, HFS_ALTFLUSH); |
55e303ae | 450 | } else { |
91447636 A |
451 | VTOC(vp)->c_touch_chgtime = TRUE; |
452 | VTOC(vp)->c_touch_modtime = TRUE; | |
453 | (void) hfs_update(vp, TRUE); | |
1c79356b | 454 | } |
b4c24cb9 A |
455 | |
456 | ret = ClearBTNodes(vp, btInfo.nodeSize, filePtr->fcbEOF - actualBytesAdded, actualBytesAdded); | |
91447636 A |
457 | out: |
458 | if (retval == 0) | |
459 | retval = ret; | |
460 | ||
461 | if (lockflags) | |
462 | hfs_systemfile_unlock(vcb, lockflags); | |
1c79356b A |
463 | |
464 | return retval; | |
465 | } | |
466 | ||
467 | ||
1c79356b A |
468 | /* |
469 | * Clear out (zero) new b-tree nodes on disk. | |
470 | */ | |
471 | static int | |
472 | ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount) | |
473 | { | |
b4c24cb9 | 474 | struct hfsmount *hfsmp = VTOHFS(vp); |
1c79356b | 475 | struct buf *bp = NULL; |
91447636 A |
476 | daddr64_t blk; |
477 | daddr64_t blkcnt; | |
1c79356b A |
478 | |
479 | blk = offset / blksize; | |
480 | blkcnt = amount / blksize; | |
481 | ||
482 | while (blkcnt > 0) { | |
91447636 | 483 | bp = buf_getblk(vp, blk, blksize, 0, 0, BLK_META); |
1c79356b A |
484 | if (bp == NULL) |
485 | continue; | |
b4c24cb9 A |
486 | |
487 | // XXXdbg | |
488 | if (hfsmp->jnl) { | |
489 | // XXXdbg -- skipping this for now since it makes a transaction | |
490 | // become *way* too large | |
491 | //journal_modify_block_start(hfsmp->jnl, bp); | |
492 | } | |
91447636 | 493 | bzero((char *)buf_dataptr(bp), blksize); |
b4c24cb9 | 494 | |
91447636 | 495 | buf_markaged(bp); |
1c79356b | 496 | |
b4c24cb9 A |
497 | // XXXdbg |
498 | if (hfsmp->jnl) { | |
499 | // XXXdbg -- skipping this for now since it makes a transaction | |
500 | // become *way* too large | |
501 | //journal_modify_block_end(hfsmp->jnl, bp); | |
502 | ||
503 | // XXXdbg - remove this once we decide what to do with the | |
504 | // writes to the journal | |
505 | if ((blk % 32) == 0) | |
91447636 | 506 | VNOP_BWRITE(bp); |
b4c24cb9 | 507 | else |
91447636 | 508 | buf_bawrite(bp); |
b4c24cb9 A |
509 | } else { |
510 | /* wait/yield every 32 blocks so we don't hog all the buffers */ | |
511 | if ((blk % 32) == 0) | |
91447636 | 512 | VNOP_BWRITE(bp); |
b4c24cb9 | 513 | else |
91447636 | 514 | buf_bawrite(bp); |
b4c24cb9 | 515 | } |
1c79356b A |
516 | --blkcnt; |
517 | ++blk; | |
518 | } | |
519 | ||
520 | return (0); | |
521 | } | |
91447636 A |
522 | |
523 | ||
524 | extern char hfs_attrname[]; | |
525 | ||
526 | extern int hfs_attrkeycompare(HFSPlusAttrKey *searchKey, HFSPlusAttrKey *trialKey); | |
527 | ||
528 | int hfs_create_attr_btree(struct hfsmount *hfsmp, uint32_t nodesize, uint32_t nodecnt); | |
529 | ||
530 | /* | |
531 | * Create an HFS+ Attribute B-tree File. | |
532 | * | |
533 | * A journal transaction must be already started. | |
534 | */ | |
535 | int | |
536 | hfs_create_attr_btree(struct hfsmount *hfsmp, uint32_t nodesize, uint32_t nodecnt) | |
537 | { | |
538 | struct vnode* vp = NULL; | |
539 | struct cat_desc cndesc; | |
540 | struct cat_attr cnattr; | |
541 | struct cat_fork cfork; | |
542 | BlockDescriptor blkdesc; | |
543 | BTNodeDescriptor *ndp; | |
544 | BTHeaderRec *bthp; | |
545 | BTreeControlBlockPtr btcb = NULL; | |
546 | struct buf *bp = NULL; | |
547 | void * buffer; | |
548 | u_int16_t *index; | |
549 | u_int16_t offset; | |
550 | int result; | |
551 | ||
552 | printf("Creating HFS+ Attribute B-tree File (%d nodes) on %s\n", nodecnt, hfsmp->vcbVN); | |
553 | ||
554 | /* | |
555 | * Set up Attribute B-tree vnode | |
556 | */ | |
557 | bzero(&cndesc, sizeof(cndesc)); | |
558 | cndesc.cd_parentcnid = kHFSRootParentID; | |
559 | cndesc.cd_flags |= CD_ISMETA; | |
560 | cndesc.cd_nameptr = hfs_attrname; | |
561 | cndesc.cd_namelen = strlen(hfs_attrname); | |
562 | cndesc.cd_cnid = kHFSAttributesFileID; | |
563 | ||
564 | bzero(&cnattr, sizeof(cnattr)); | |
565 | cnattr.ca_nlink = 1; | |
566 | cnattr.ca_mode = S_IFREG; | |
567 | cnattr.ca_fileid = cndesc.cd_cnid; | |
568 | ||
569 | bzero(&cfork, sizeof(cfork)); | |
570 | cfork.cf_clump = nodesize * nodecnt; | |
571 | ||
572 | result = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cfork, &vp); | |
573 | if (result) | |
574 | return (result); | |
575 | ||
576 | /* | |
577 | * Set up Attribute B-tree control block | |
578 | */ | |
579 | MALLOC(btcb, BTreeControlBlock *, sizeof(BTreeControlBlock), M_TEMP, M_WAITOK); | |
580 | bzero(btcb, sizeof(BTreeControlBlock)); | |
581 | ||
582 | btcb->nodeSize = nodesize; | |
583 | btcb->maxKeyLength = kHFSPlusAttrKeyMaximumLength; | |
584 | btcb->btreeType = 0xFF; | |
585 | btcb->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask; | |
586 | btcb->version = kBTreeVersion; | |
587 | btcb->writeCount = 1; | |
588 | btcb->flags = 0; /* kBTHeaderDirty */ | |
589 | btcb->fileRefNum = vp; | |
590 | btcb->getBlockProc = GetBTreeBlock; | |
591 | btcb->releaseBlockProc = ReleaseBTreeBlock; | |
592 | btcb->setEndOfForkProc = ExtendBTreeFile; | |
593 | btcb->keyCompareProc = (KeyCompareProcPtr)hfs_attrkeycompare; | |
594 | VTOF(vp)->fcbBTCBPtr = btcb; | |
595 | ||
596 | /* | |
597 | * Allocate some space | |
598 | */ | |
599 | result = ExtendBTreeFile(vp, nodesize, cfork.cf_clump); | |
600 | if (result) | |
601 | goto exit; | |
602 | ||
603 | btcb->totalNodes = VTOF(vp)->ff_size / nodesize; | |
604 | btcb->freeNodes = btcb->totalNodes - 1; | |
605 | ||
606 | /* | |
607 | * Initialize the b-tree header on disk | |
608 | */ | |
609 | bp = buf_getblk(vp, 0, nodesize, 0, 0, BLK_META); | |
610 | if (bp == NULL) { | |
611 | result = EIO; | |
612 | goto exit; | |
613 | } | |
614 | ||
615 | buffer = (void *)buf_dataptr(bp); | |
616 | blkdesc.buffer = buffer; | |
617 | blkdesc.blockHeader = (void *)bp; | |
618 | blkdesc.blockReadFromDisk = 0; | |
619 | blkdesc.isModified = 0; | |
620 | ||
621 | ModifyBlockStart(vp, &blkdesc); | |
622 | ||
623 | if (buf_size(bp) != nodesize) | |
624 | panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp)); | |
625 | ||
626 | bzero(buffer, nodesize); | |
627 | index = (int16_t *)buffer; | |
628 | ||
629 | /* FILL IN THE NODE DESCRIPTOR: */ | |
630 | ndp = (BTNodeDescriptor *)buffer; | |
631 | ndp->kind = kBTHeaderNode; | |
632 | ndp->numRecords = 3; | |
633 | offset = sizeof(BTNodeDescriptor); | |
634 | index[(nodesize / 2) - 1] = offset; | |
635 | ||
636 | /* FILL IN THE HEADER RECORD: */ | |
637 | bthp = (BTHeaderRec *)((UInt8 *)buffer + offset); | |
638 | bthp->nodeSize = nodesize; | |
639 | bthp->totalNodes = btcb->totalNodes; | |
640 | bthp->freeNodes = btcb->freeNodes; | |
641 | bthp->clumpSize = cfork.cf_clump; | |
642 | bthp->btreeType = 0xFF; | |
643 | bthp->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask; | |
644 | bthp->maxKeyLength = kHFSPlusAttrKeyMaximumLength; | |
645 | bthp->keyCompareType = kHFSBinaryCompare; | |
646 | offset += sizeof(BTHeaderRec); | |
647 | index[(nodesize / 2) - 2] = offset; | |
648 | ||
649 | /* FILL IN THE USER RECORD: */ | |
650 | offset += kBTreeHeaderUserBytes; | |
651 | index[(nodesize / 2) - 3] = offset; | |
652 | ||
653 | /* FILL IN THE MAP RECORD (only one node in use). */ | |
654 | *((u_int8_t *)buffer + offset) = 0x80; | |
655 | offset += nodesize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec) | |
656 | - kBTreeHeaderUserBytes - (4 * sizeof(int16_t)); | |
657 | index[(nodesize / 2) - 4] = offset; | |
658 | ||
659 | if (hfsmp->jnl) { | |
660 | result = btree_journal_modify_block_end(hfsmp, bp); | |
661 | } else { | |
662 | result = VNOP_BWRITE(bp); | |
663 | } | |
664 | if (result) | |
665 | goto exit; | |
666 | ||
667 | /* Publish new btree file */ | |
668 | hfsmp->hfs_attribute_vp = vp; | |
669 | (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); | |
670 | ||
671 | exit: | |
672 | hfs_unlock(VTOC(vp)); | |
673 | if (result) { | |
674 | if (btcb) { | |
675 | FREE (btcb, M_TEMP); | |
676 | } | |
677 | vnode_put(vp); | |
678 | // hfs_truncate(); /* XXX need to give back blocks */ | |
679 | } | |
680 | return (result); | |
681 | } | |
682 | ||
683 | ||
684 |