]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
13f56ec4 | 2 | * Copyright (c) 2000-2011 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
1c79356b A |
28 | |
29 | #include <sys/param.h> | |
30 | #include <sys/systm.h> | |
31 | #include <sys/buf.h> | |
2d21ac55 | 32 | #include <sys/buf_internal.h> |
9bccf70c | 33 | #include <sys/kernel.h> |
91447636 | 34 | #include <sys/malloc.h> |
1c79356b A |
35 | #include <sys/mount.h> |
36 | #include <sys/vnode.h> | |
37 | ||
38 | ||
39 | #include "hfs.h" | |
9bccf70c | 40 | #include "hfs_cnode.h" |
1c79356b A |
41 | #include "hfs_dbg.h" |
42 | #include "hfs_endian.h" | |
2d21ac55 | 43 | #include "hfs_btreeio.h" |
1c79356b A |
44 | |
45 | #include "hfscommon/headers/FileMgrInternal.h" | |
46 | #include "hfscommon/headers/BTreesPrivate.h" | |
47 | ||
48 | #define FORCESYNCBTREEWRITES 0 | |
49 | ||
2d21ac55 A |
50 | /* From bsd/vfs/vfs_bio.c */ |
51 | extern int bdwrite_internal(struct buf *, int); | |
1c79356b A |
52 | |
53 | static int ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount); | |
2d21ac55 | 54 | static int btree_journal_modify_block_end(struct hfsmount *hfsmp, struct buf *bp); |
1c79356b | 55 | |
6d2010ae | 56 | void btree_swap_node(struct buf *bp, __unused void *arg); |
1c79356b | 57 | |
13f56ec4 A |
58 | /* |
59 | * Return btree node size for given vnode. | |
60 | * | |
61 | * Returns: | |
62 | * For btree vnode, returns btree node size. | |
63 | * For non-btree vnodes, returns 0. | |
64 | */ | |
65 | u_int16_t get_btree_nodesize(struct vnode *vp) | |
66 | { | |
67 | BTreeControlBlockPtr btree; | |
68 | u_int16_t node_size = 0; | |
69 | ||
70 | if (vnode_issystem(vp)) { | |
71 | btree = (BTreeControlBlockPtr) VTOF(vp)->fcbBTCBPtr; | |
72 | if (btree) { | |
73 | node_size = btree->nodeSize; | |
74 | } | |
75 | } | |
76 | ||
77 | return node_size; | |
78 | } | |
79 | ||
2d21ac55 | 80 | OSStatus SetBTreeBlockSize(FileReference vp, ByteCount blockSize, __unused ItemCount minBlockCount) |
1c79356b A |
81 | { |
82 | BTreeControlBlockPtr bTreePtr; | |
83 | ||
84 | DBG_ASSERT(vp != NULL); | |
1c79356b A |
85 | DBG_ASSERT(blockSize >= kMinNodeSize); |
86 | if (blockSize > MAXBSIZE ) | |
87 | return (fsBTBadNodeSize); | |
88 | ||
9bccf70c | 89 | bTreePtr = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr; |
1c79356b A |
90 | bTreePtr->nodeSize = blockSize; |
91 | ||
92 | return (E_NONE); | |
93 | } | |
94 | ||
95 | ||
2d21ac55 | 96 | OSStatus GetBTreeBlock(FileReference vp, u_int32_t blockNum, GetBlockOptions options, BlockDescriptor *block) |
1c79356b A |
97 | { |
98 | OSStatus retval = E_NONE; | |
99 | struct buf *bp = NULL; | |
935ed37a A |
100 | u_int8_t allow_empty_node; |
101 | ||
102 | /* If the btree block is being read using hint, it is | |
103 | * fine for the swap code to find zeroed out nodes. | |
104 | */ | |
105 | if (options & kGetBlockHint) { | |
106 | allow_empty_node = true; | |
107 | } else { | |
108 | allow_empty_node = false; | |
109 | } | |
1c79356b | 110 | |
91447636 A |
111 | if (options & kGetEmptyBlock) { |
112 | daddr64_t blkno; | |
113 | off_t offset; | |
1c79356b | 114 | |
91447636 A |
115 | offset = (daddr64_t)blockNum * (daddr64_t)block->blockSize; |
116 | bp = buf_getblk(vp, (daddr64_t)blockNum, block->blockSize, 0, 0, BLK_META); | |
117 | if (bp && | |
118 | VNOP_BLOCKMAP(vp, offset, block->blockSize, &blkno, NULL, NULL, 0, NULL) == 0) { | |
119 | buf_setblkno(bp, blkno); | |
120 | } | |
121 | } else { | |
122 | retval = buf_meta_bread(vp, (daddr64_t)blockNum, block->blockSize, NOCRED, &bp); | |
123 | } | |
1c79356b A |
124 | if (bp == NULL) |
125 | retval = -1; //XXX need better error | |
126 | ||
127 | if (retval == E_NONE) { | |
128 | block->blockHeader = bp; | |
91447636 | 129 | block->buffer = (char *)buf_dataptr(bp); |
3a60a9f5 | 130 | block->blockNum = buf_lblkno(bp); |
91447636 | 131 | block->blockReadFromDisk = (buf_fromcache(bp) == 0); /* not found in cache ==> came from disk */ |
1c79356b | 132 | |
b4c24cb9 A |
133 | // XXXdbg |
134 | block->isModified = 0; | |
135 | ||
3a60a9f5 | 136 | /* Check and endian swap B-Tree node (only if it's a valid block) */ |
1c79356b A |
137 | if (!(options & kGetEmptyBlock)) { |
138 | /* This happens when we first open the b-tree, we might not have all the node data on hand */ | |
139 | if ((((BTNodeDescriptor *)block->buffer)->kind == kBTHeaderNode) && | |
91447636 A |
140 | (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize != buf_count(bp)) && |
141 | (SWAP_BE16 (((BTHeaderRec *)((char *)block->buffer + 14))->nodeSize) != buf_count(bp))) { | |
1c79356b | 142 | |
3a60a9f5 A |
143 | /* |
144 | * Don't swap the node descriptor, record offsets, or other records. | |
145 | * This record will be invalidated and re-read with the correct node | |
146 | * size once the B-tree control block is set up with the node size | |
147 | * from the header record. | |
148 | */ | |
935ed37a | 149 | retval = hfs_swap_BTNode (block, vp, kSwapBTNodeHeaderRecordOnly, allow_empty_node); |
3a60a9f5 A |
150 | |
151 | } else if (block->blockReadFromDisk) { | |
152 | /* | |
153 | * The node was just read from disk, so always swap/check it. | |
154 | * This is necessary on big endian since the test below won't trigger. | |
155 | */ | |
935ed37a | 156 | retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost, allow_empty_node); |
2d21ac55 | 157 | } else if (*((u_int16_t *)((char *)block->buffer + (block->blockSize - sizeof (u_int16_t)))) == 0x0e00) { |
3a60a9f5 A |
158 | /* |
159 | * The node was left in the cache in non-native order, so swap it. | |
160 | * This only happens on little endian, after the node is written | |
161 | * back to disk. | |
162 | */ | |
935ed37a | 163 | retval = hfs_swap_BTNode (block, vp, kSwapBTNodeBigToHost, allow_empty_node); |
1c79356b | 164 | } |
3a60a9f5 A |
165 | |
166 | /* | |
167 | * If we got an error, then the node is only partially swapped. | |
168 | * We mark the buffer invalid so that the next attempt to get the | |
169 | * node will read it and attempt to swap again, and will notice | |
170 | * the error again. If we didn't do this, the next attempt to get | |
171 | * the node might use the partially swapped node as-is. | |
172 | */ | |
173 | if (retval) | |
174 | buf_markinvalid(bp); | |
1c79356b | 175 | } |
3a60a9f5 A |
176 | } |
177 | ||
178 | if (retval) { | |
1c79356b | 179 | if (bp) |
3a60a9f5 | 180 | buf_brelse(bp); |
1c79356b A |
181 | block->blockHeader = NULL; |
182 | block->buffer = NULL; | |
183 | } | |
184 | ||
185 | return (retval); | |
186 | } | |
187 | ||
188 | ||
b4c24cb9 A |
189 | void ModifyBlockStart(FileReference vp, BlockDescPtr blockPtr) |
190 | { | |
191 | struct hfsmount *hfsmp = VTOHFS(vp); | |
192 | struct buf *bp = NULL; | |
193 | ||
194 | if (hfsmp->jnl == NULL) { | |
195 | return; | |
196 | } | |
197 | ||
198 | bp = (struct buf *) blockPtr->blockHeader; | |
199 | if (bp == NULL) { | |
b0d623f7 | 200 | panic("hfs: ModifyBlockStart: null bp for blockdescptr %p?!?\n", blockPtr); |
b4c24cb9 A |
201 | return; |
202 | } | |
203 | ||
204 | journal_modify_block_start(hfsmp->jnl, bp); | |
205 | blockPtr->isModified = 1; | |
206 | } | |
207 | ||
6d2010ae | 208 | void |
2d21ac55 | 209 | btree_swap_node(struct buf *bp, __unused void *arg) |
55e303ae | 210 | { |
2d21ac55 | 211 | // struct hfsmount *hfsmp = (struct hfsmount *)arg; |
3a60a9f5 | 212 | int retval; |
91447636 | 213 | struct vnode *vp = buf_vnode(bp); |
55e303ae A |
214 | BlockDescriptor block; |
215 | ||
216 | /* Prepare the block pointer */ | |
217 | block.blockHeader = bp; | |
91447636 | 218 | block.buffer = (char *)buf_dataptr(bp); |
3a60a9f5 | 219 | block.blockNum = buf_lblkno(bp); |
55e303ae | 220 | /* not found in cache ==> came from disk */ |
91447636 A |
221 | block.blockReadFromDisk = (buf_fromcache(bp) == 0); |
222 | block.blockSize = buf_count(bp); | |
55e303ae | 223 | |
935ed37a A |
224 | /* Swap the data now that this node is ready to go to disk. |
225 | * We allow swapping of zeroed out nodes here because we might | |
226 | * be writing node whose last record just got deleted. | |
227 | */ | |
228 | retval = hfs_swap_BTNode (&block, vp, kSwapBTNodeHostToBig, true); | |
3a60a9f5 | 229 | if (retval) |
b0d623f7 | 230 | panic("hfs: btree_swap_node: about to write corrupt node!\n"); |
2d21ac55 A |
231 | } |
232 | ||
55e303ae | 233 | |
2d21ac55 A |
234 | static int |
235 | btree_journal_modify_block_end(struct hfsmount *hfsmp, struct buf *bp) | |
236 | { | |
237 | return journal_modify_block_end(hfsmp->jnl, bp, btree_swap_node, hfsmp); | |
55e303ae A |
238 | } |
239 | ||
b4c24cb9 | 240 | |
1c79356b A |
241 | OSStatus ReleaseBTreeBlock(FileReference vp, BlockDescPtr blockPtr, ReleaseBlockOptions options) |
242 | { | |
b4c24cb9 | 243 | struct hfsmount *hfsmp = VTOHFS(vp); |
1c79356b A |
244 | OSStatus retval = E_NONE; |
245 | struct buf *bp = NULL; | |
246 | ||
247 | bp = (struct buf *) blockPtr->blockHeader; | |
248 | ||
249 | if (bp == NULL) { | |
1c79356b A |
250 | retval = -1; |
251 | goto exit; | |
252 | } | |
253 | ||
254 | if (options & kTrashBlock) { | |
91447636 A |
255 | buf_markinvalid(bp); |
256 | ||
257 | if (hfsmp->jnl && (buf_flags(bp) & B_LOCKED)) { | |
b4c24cb9 A |
258 | journal_kill_block(hfsmp->jnl, bp); |
259 | } else { | |
91447636 | 260 | buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */ |
b4c24cb9 | 261 | } |
d1ecb069 A |
262 | |
263 | /* Don't let anyone else try to use this bp, it's been consumed */ | |
264 | blockPtr->blockHeader = NULL; | |
265 | ||
1c79356b A |
266 | } else { |
267 | if (options & kForceWriteBlock) { | |
b4c24cb9 A |
268 | if (hfsmp->jnl) { |
269 | if (blockPtr->isModified == 0) { | |
2d21ac55 | 270 | panic("hfs: releaseblock: modified is 0 but forcewrite set! bp %p\n", bp); |
b4c24cb9 | 271 | } |
55e303ae A |
272 | |
273 | retval = btree_journal_modify_block_end(hfsmp, bp); | |
b4c24cb9 A |
274 | blockPtr->isModified = 0; |
275 | } else { | |
91447636 | 276 | retval = VNOP_BWRITE(bp); |
b4c24cb9 | 277 | } |
d1ecb069 A |
278 | |
279 | /* Don't let anyone else try to use this bp, it's been consumed */ | |
280 | blockPtr->blockHeader = NULL; | |
281 | ||
1c79356b | 282 | } else if (options & kMarkBlockDirty) { |
91447636 A |
283 | struct timeval tv; |
284 | microuptime(&tv); | |
b4c24cb9 | 285 | if ((options & kLockTransaction) && hfsmp->jnl == NULL) { |
9bccf70c A |
286 | /* |
287 | * | |
91447636 | 288 | * Set the B_LOCKED flag and unlock the buffer, causing buf_brelse to move |
9bccf70c | 289 | * the buffer onto the LOCKED free list. This is necessary, otherwise |
91447636 | 290 | * getnewbuf() would try to reclaim the buffers using buf_bawrite, which |
9bccf70c A |
291 | * isn't going to work. |
292 | * | |
293 | */ | |
9bccf70c A |
294 | /* Don't hog all the buffers... */ |
295 | if (count_lock_queue() > kMaxLockedMetaBuffers) { | |
296 | hfs_btsync(vp, HFS_SYNCTRANS); | |
297 | /* Rollback sync time to cause a sync on lock release... */ | |
91447636 | 298 | (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1)); |
9bccf70c | 299 | } |
91447636 | 300 | buf_setflags(bp, B_LOCKED); |
b4c24cb9 A |
301 | } |
302 | ||
9bccf70c A |
303 | /* |
304 | * Delay-write this block. | |
305 | * If the maximum delayed buffers has been exceeded then | |
306 | * free up some buffers and fall back to an asynchronous write. | |
307 | */ | |
b4c24cb9 A |
308 | if (hfsmp->jnl) { |
309 | if (blockPtr->isModified == 0) { | |
2d21ac55 | 310 | panic("hfs: releaseblock: modified is 0 but markdirty set! bp %p\n", bp); |
b4c24cb9 | 311 | } |
55e303ae | 312 | retval = btree_journal_modify_block_end(hfsmp, bp); |
b4c24cb9 A |
313 | blockPtr->isModified = 0; |
314 | } else if (bdwrite_internal(bp, 1) != 0) { | |
9bccf70c A |
315 | hfs_btsync(vp, 0); |
316 | /* Rollback sync time to cause a sync on lock release... */ | |
91447636 A |
317 | (void) BTSetLastSync(VTOF(vp), tv.tv_sec - (kMaxSecsForFsync + 1)); |
318 | ||
319 | buf_clearflags(bp, B_LOCKED); | |
320 | buf_bawrite(bp); | |
9bccf70c | 321 | } |
d1ecb069 A |
322 | |
323 | /* Don't let anyone else try to use this bp, it's been consumed */ | |
324 | blockPtr->blockHeader = NULL; | |
325 | ||
1c79356b | 326 | } else { |
b4c24cb9 | 327 | // check if we had previously called journal_modify_block_start() |
91447636 | 328 | // on this block and if so, abort it (which will call buf_brelse()). |
b4c24cb9 A |
329 | if (hfsmp->jnl && blockPtr->isModified) { |
330 | // XXXdbg - I don't want to call modify_block_abort() | |
331 | // because I think it may be screwing up the | |
332 | // journal and blowing away a block that has | |
333 | // valid data in it. | |
334 | // | |
335 | // journal_modify_block_abort(hfsmp->jnl, bp); | |
336 | //panic("hfs: releaseblock called for 0x%x but mod_block_start previously called.\n", bp); | |
55e303ae | 337 | btree_journal_modify_block_end(hfsmp, bp); |
b4c24cb9 A |
338 | blockPtr->isModified = 0; |
339 | } else { | |
91447636 | 340 | buf_brelse(bp); /* note: B-tree code will clear blockPtr->blockHeader and blockPtr->buffer */ |
b4c24cb9 | 341 | } |
d1ecb069 A |
342 | |
343 | /* Don't let anyone else try to use this bp, it's been consumed */ | |
344 | blockPtr->blockHeader = NULL; | |
345 | } | |
346 | } | |
1c79356b A |
347 | |
348 | exit: | |
349 | return (retval); | |
350 | } | |
351 | ||
352 | ||
353 | OSStatus ExtendBTreeFile(FileReference vp, FSSize minEOF, FSSize maxEOF) | |
354 | { | |
355 | #pragma unused (maxEOF) | |
356 | ||
91447636 | 357 | OSStatus retval = 0, ret = 0; |
6601e61a A |
358 | int64_t actualBytesAdded, origSize; |
359 | u_int64_t bytesToAdd; | |
9bccf70c A |
360 | u_int32_t startAllocation; |
361 | u_int32_t fileblocks; | |
6601e61a | 362 | BTreeInfoRec btInfo; |
1c79356b | 363 | ExtendedVCB *vcb; |
6601e61a A |
364 | FCB *filePtr; |
365 | struct proc *p = NULL; | |
366 | int64_t trim = 0; | |
367 | int lockflags = 0; | |
1c79356b A |
368 | |
369 | filePtr = GetFileControlBlock(vp); | |
370 | ||
6601e61a | 371 | if ( (off_t)minEOF > filePtr->fcbEOF ) |
1c79356b A |
372 | { |
373 | bytesToAdd = minEOF - filePtr->fcbEOF; | |
374 | ||
9bccf70c A |
375 | if (bytesToAdd < filePtr->ff_clumpsize) |
376 | bytesToAdd = filePtr->ff_clumpsize; //XXX why not always be a mutiple of clump size? | |
1c79356b A |
377 | } |
378 | else | |
379 | { | |
1c79356b A |
380 | return -1; |
381 | } | |
382 | ||
9bccf70c | 383 | vcb = VTOVCB(vp); |
6601e61a | 384 | |
1c79356b A |
385 | /* |
386 | * The Extents B-tree can't have overflow extents. ExtendFileC will | |
387 | * return an error if an attempt is made to extend the Extents B-tree | |
388 | * when the resident extents are exhausted. | |
389 | */ | |
1c79356b | 390 | |
91447636 A |
391 | /* Protect allocation bitmap and extents overflow file. */ |
392 | lockflags = SFL_BITMAP; | |
393 | if (VTOC(vp)->c_fileid != kHFSExtentsFileID) | |
394 | lockflags |= SFL_EXTENTS; | |
395 | lockflags = hfs_systemfile_lock(vcb, lockflags, HFS_EXCLUSIVE_LOCK); | |
396 | ||
397 | (void) BTGetInformation(filePtr, 0, &btInfo); | |
1c79356b | 398 | |
b4c24cb9 | 399 | #if 0 // XXXdbg |
1c79356b | 400 | /* |
9bccf70c | 401 | * The b-tree code expects nodes to be contiguous. So when |
1c79356b | 402 | * the allocation block size is less than the b-tree node |
9bccf70c A |
403 | * size, we need to force disk allocations to be contiguous. |
404 | */ | |
1c79356b A |
405 | if (vcb->blockSize >= btInfo.nodeSize) { |
406 | extendFlags = 0; | |
407 | } else { | |
408 | /* Ensure that all b-tree nodes are contiguous on disk */ | |
b4c24cb9 | 409 | extendFlags = kEFContigMask; |
1c79356b | 410 | } |
b4c24cb9 | 411 | #endif |
1c79356b | 412 | |
b4c24cb9 | 413 | origSize = filePtr->fcbEOF; |
9bccf70c A |
414 | fileblocks = filePtr->ff_blocks; |
415 | startAllocation = vcb->nextAllocation; | |
1c79356b | 416 | |
b4c24cb9 A |
417 | // loop trying to get a contiguous chunk that's an integer multiple |
418 | // of the btree node size. if we can't get a contiguous chunk that | |
419 | // is at least the node size then we break out of the loop and let | |
420 | // the error propagate back up. | |
6601e61a A |
421 | while((off_t)bytesToAdd >= btInfo.nodeSize) { |
422 | do { | |
55e303ae | 423 | retval = ExtendFileC(vcb, filePtr, bytesToAdd, 0, |
6601e61a A |
424 | kEFContigMask | kEFMetadataMask | kEFNoClumpMask, |
425 | (int64_t *)&actualBytesAdded); | |
b4c24cb9 | 426 | if (retval == dskFulErr && actualBytesAdded == 0) { |
6601e61a A |
427 | bytesToAdd >>= 1; |
428 | if (bytesToAdd < btInfo.nodeSize) { | |
429 | break; | |
430 | } else if ((bytesToAdd % btInfo.nodeSize) != 0) { | |
431 | // make sure it's an integer multiple of the nodeSize | |
432 | bytesToAdd -= (bytesToAdd % btInfo.nodeSize); | |
433 | } | |
b4c24cb9 | 434 | } |
6601e61a A |
435 | } while (retval == dskFulErr && actualBytesAdded == 0); |
436 | ||
437 | if (retval == dskFulErr && actualBytesAdded == 0 && bytesToAdd <= btInfo.nodeSize) { | |
438 | break; | |
439 | } | |
440 | ||
441 | filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize; | |
442 | bytesToAdd = minEOF - filePtr->fcbEOF; | |
443 | } | |
91447636 | 444 | |
9bccf70c A |
445 | /* |
446 | * If a new extent was added then move the roving allocator | |
447 | * reference forward by the current b-tree file size so | |
448 | * there's plenty of room to grow. | |
449 | */ | |
450 | if ((retval == 0) && | |
55e303ae | 451 | ((VCBTOHFS(vcb)->hfs_flags & HFS_METADATA_ZONE) == 0) && |
9bccf70c | 452 | (vcb->nextAllocation > startAllocation) && |
2d21ac55 A |
453 | ((vcb->nextAllocation + fileblocks) < vcb->allocLimit)) { |
454 | HFS_UPDATE_NEXT_ALLOCATION(vcb, vcb->nextAllocation + fileblocks); | |
9bccf70c A |
455 | } |
456 | ||
b4c24cb9 A |
457 | filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize; |
458 | ||
459 | // XXXdbg ExtendFileC() could have returned an error even though | |
460 | // it grew the file to be big enough for our needs. If this is | |
461 | // the case, we don't care about retval so we blow it away. | |
462 | // | |
6601e61a | 463 | if (filePtr->fcbEOF >= (off_t)minEOF && retval != 0) { |
b4c24cb9 A |
464 | retval = 0; |
465 | } | |
466 | ||
467 | // XXXdbg if the file grew but isn't large enough or isn't an | |
468 | // even multiple of the nodeSize then trim things back. if | |
469 | // the file isn't large enough we trim back to the original | |
470 | // size. otherwise we trim back to be an even multiple of the | |
471 | // btree node size. | |
472 | // | |
6601e61a | 473 | if ((filePtr->fcbEOF < (off_t)minEOF) || ((filePtr->fcbEOF - origSize) % btInfo.nodeSize) != 0) { |
b4c24cb9 | 474 | |
6601e61a | 475 | if (filePtr->fcbEOF < (off_t)minEOF) { |
b4c24cb9 A |
476 | retval = dskFulErr; |
477 | ||
478 | if (filePtr->fcbEOF < origSize) { | |
479 | panic("hfs: btree file eof %lld less than orig size %lld!\n", | |
480 | filePtr->fcbEOF, origSize); | |
481 | } | |
482 | ||
483 | trim = filePtr->fcbEOF - origSize; | |
b4c24cb9 | 484 | } else { |
6601e61a | 485 | trim = ((filePtr->fcbEOF - origSize) % btInfo.nodeSize); |
b4c24cb9 A |
486 | } |
487 | ||
6d2010ae | 488 | ret = TruncateFileC(vcb, filePtr, filePtr->fcbEOF - trim, 0, 0, FTOC(filePtr)->c_fileid, 0); |
b4c24cb9 A |
489 | filePtr->fcbEOF = (u_int64_t)filePtr->ff_blocks * (u_int64_t)vcb->blockSize; |
490 | ||
491 | // XXXdbg - panic if the file didn't get trimmed back properly | |
492 | if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) { | |
6601e61a | 493 | panic("hfs: truncate file didn't! fcbEOF %lld nsize %d fcb %p\n", |
b4c24cb9 A |
494 | filePtr->fcbEOF, btInfo.nodeSize, filePtr); |
495 | } | |
496 | ||
497 | if (ret) { | |
498 | // XXXdbg - this probably doesn't need to be a panic() | |
6601e61a | 499 | panic("hfs: error truncating btree files (sz 0x%llx, trim %lld, ret %ld)\n", |
b0d623f7 | 500 | filePtr->fcbEOF, trim, (long)ret); |
91447636 | 501 | goto out; |
b4c24cb9 | 502 | } |
b4c24cb9 A |
503 | } |
504 | ||
9bccf70c A |
505 | if(VTOC(vp)->c_fileid != kHFSExtentsFileID) { |
506 | /* | |
507 | * Get any extents overflow b-tree changes to disk ASAP! | |
508 | */ | |
b4c24cb9 | 509 | (void) BTFlushPath(VTOF(vcb->extentsRefNum)); |
91447636 | 510 | (void) hfs_fsync(vcb->extentsRefNum, MNT_WAIT, 0, p); |
9bccf70c | 511 | } |
91447636 A |
512 | hfs_systemfile_unlock(vcb, lockflags); |
513 | lockflags = 0; | |
1c79356b | 514 | |
b4c24cb9 | 515 | if ((filePtr->fcbEOF % btInfo.nodeSize) != 0) { |
6601e61a | 516 | panic("hfs: extendbtree: fcb %p has eof 0x%llx not a multiple of 0x%x (trim %llx)\n", |
b4c24cb9 A |
517 | filePtr, filePtr->fcbEOF, btInfo.nodeSize, trim); |
518 | } | |
519 | ||
1c79356b A |
520 | /* |
521 | * Update the Alternate MDB or Alternate VolumeHeader | |
522 | */ | |
9bccf70c A |
523 | if ((VTOC(vp)->c_fileid == kHFSExtentsFileID) || |
524 | (VTOC(vp)->c_fileid == kHFSCatalogFileID) || | |
525 | (VTOC(vp)->c_fileid == kHFSAttributesFileID) | |
1c79356b | 526 | ) { |
91447636 | 527 | VTOC(vp)->c_flag |= C_MODIFIED; |
1c79356b | 528 | MarkVCBDirty( vcb ); |
b4c24cb9 | 529 | ret = hfs_flushvolumeheader(VCBTOHFS(vcb), MNT_WAIT, HFS_ALTFLUSH); |
55e303ae | 530 | } else { |
91447636 A |
531 | VTOC(vp)->c_touch_chgtime = TRUE; |
532 | VTOC(vp)->c_touch_modtime = TRUE; | |
533 | (void) hfs_update(vp, TRUE); | |
1c79356b | 534 | } |
b4c24cb9 | 535 | |
6601e61a | 536 | ret = ClearBTNodes(vp, btInfo.nodeSize, origSize, (filePtr->fcbEOF - origSize)); |
91447636 A |
537 | out: |
538 | if (retval == 0) | |
539 | retval = ret; | |
540 | ||
541 | if (lockflags) | |
542 | hfs_systemfile_unlock(vcb, lockflags); | |
1c79356b A |
543 | |
544 | return retval; | |
545 | } | |
546 | ||
547 | ||
1c79356b A |
548 | /* |
549 | * Clear out (zero) new b-tree nodes on disk. | |
550 | */ | |
551 | static int | |
552 | ClearBTNodes(struct vnode *vp, long blksize, off_t offset, off_t amount) | |
553 | { | |
b4c24cb9 | 554 | struct hfsmount *hfsmp = VTOHFS(vp); |
1c79356b | 555 | struct buf *bp = NULL; |
91447636 A |
556 | daddr64_t blk; |
557 | daddr64_t blkcnt; | |
1c79356b A |
558 | |
559 | blk = offset / blksize; | |
560 | blkcnt = amount / blksize; | |
561 | ||
562 | while (blkcnt > 0) { | |
91447636 | 563 | bp = buf_getblk(vp, blk, blksize, 0, 0, BLK_META); |
1c79356b A |
564 | if (bp == NULL) |
565 | continue; | |
b4c24cb9 A |
566 | |
567 | // XXXdbg | |
568 | if (hfsmp->jnl) { | |
569 | // XXXdbg -- skipping this for now since it makes a transaction | |
570 | // become *way* too large | |
571 | //journal_modify_block_start(hfsmp->jnl, bp); | |
572 | } | |
91447636 | 573 | bzero((char *)buf_dataptr(bp), blksize); |
b4c24cb9 | 574 | |
91447636 | 575 | buf_markaged(bp); |
1c79356b | 576 | |
b4c24cb9 A |
577 | // XXXdbg |
578 | if (hfsmp->jnl) { | |
579 | // XXXdbg -- skipping this for now since it makes a transaction | |
580 | // become *way* too large | |
581 | //journal_modify_block_end(hfsmp->jnl, bp); | |
582 | ||
583 | // XXXdbg - remove this once we decide what to do with the | |
584 | // writes to the journal | |
585 | if ((blk % 32) == 0) | |
91447636 | 586 | VNOP_BWRITE(bp); |
b4c24cb9 | 587 | else |
91447636 | 588 | buf_bawrite(bp); |
b4c24cb9 A |
589 | } else { |
590 | /* wait/yield every 32 blocks so we don't hog all the buffers */ | |
591 | if ((blk % 32) == 0) | |
91447636 | 592 | VNOP_BWRITE(bp); |
b4c24cb9 | 593 | else |
91447636 | 594 | buf_bawrite(bp); |
b4c24cb9 | 595 | } |
1c79356b A |
596 | --blkcnt; |
597 | ++blk; | |
598 | } | |
599 | ||
600 | return (0); | |
601 | } | |
91447636 A |
602 | |
603 | ||
604 | extern char hfs_attrname[]; | |
605 | ||
91447636 A |
606 | /* |
607 | * Create an HFS+ Attribute B-tree File. | |
608 | * | |
2d21ac55 | 609 | * No global resources should be held. |
91447636 A |
610 | */ |
611 | int | |
2d21ac55 | 612 | hfs_create_attr_btree(struct hfsmount *hfsmp, u_int32_t nodesize, u_int32_t nodecnt) |
91447636 | 613 | { |
2d21ac55 | 614 | struct vnode* vp = NULLVP; |
91447636 A |
615 | struct cat_desc cndesc; |
616 | struct cat_attr cnattr; | |
617 | struct cat_fork cfork; | |
618 | BlockDescriptor blkdesc; | |
619 | BTNodeDescriptor *ndp; | |
620 | BTHeaderRec *bthp; | |
621 | BTreeControlBlockPtr btcb = NULL; | |
622 | struct buf *bp = NULL; | |
623 | void * buffer; | |
b0d623f7 | 624 | u_int8_t *bitmap; |
91447636 | 625 | u_int16_t *index; |
b0d623f7 A |
626 | u_int32_t node_num, num_map_nodes; |
627 | u_int32_t bytes_per_map_record; | |
628 | u_int32_t temp; | |
91447636 | 629 | u_int16_t offset; |
2d21ac55 | 630 | int intrans = 0; |
91447636 | 631 | int result; |
6d2010ae A |
632 | int newvnode_flags = 0; |
633 | ||
2d21ac55 A |
634 | again: |
635 | /* | |
636 | * Serialize creation using HFS_CREATING_BTREE flag. | |
637 | */ | |
638 | lck_mtx_lock(&hfsmp->hfs_mutex); | |
639 | if (hfsmp->hfs_flags & HFS_CREATING_BTREE) { | |
640 | /* Someone else beat us, wait for them to finish. */ | |
641 | (void) msleep(hfsmp->hfs_attribute_cp, &hfsmp->hfs_mutex, | |
642 | PDROP | PINOD, "hfs_create_attr_btree", 0); | |
643 | if (hfsmp->hfs_attribute_vp) { | |
644 | return (0); | |
645 | } | |
646 | goto again; | |
647 | } | |
648 | hfsmp->hfs_flags |= HFS_CREATING_BTREE; | |
649 | lck_mtx_unlock(&hfsmp->hfs_mutex); | |
91447636 | 650 | |
2d21ac55 A |
651 | /* Check if were out of usable disk space. */ |
652 | if ((hfs_freeblks(hfsmp, 1) == 0)) { | |
653 | result = ENOSPC; | |
654 | goto exit; | |
655 | } | |
91447636 A |
656 | |
657 | /* | |
658 | * Set up Attribute B-tree vnode | |
2d21ac55 A |
659 | * (this must be done before we start a transaction |
660 | * or take any system file locks) | |
91447636 A |
661 | */ |
662 | bzero(&cndesc, sizeof(cndesc)); | |
663 | cndesc.cd_parentcnid = kHFSRootParentID; | |
664 | cndesc.cd_flags |= CD_ISMETA; | |
2d21ac55 | 665 | cndesc.cd_nameptr = (const u_int8_t *)hfs_attrname; |
91447636 A |
666 | cndesc.cd_namelen = strlen(hfs_attrname); |
667 | cndesc.cd_cnid = kHFSAttributesFileID; | |
668 | ||
669 | bzero(&cnattr, sizeof(cnattr)); | |
2d21ac55 | 670 | cnattr.ca_linkcount = 1; |
91447636 A |
671 | cnattr.ca_mode = S_IFREG; |
672 | cnattr.ca_fileid = cndesc.cd_cnid; | |
673 | ||
674 | bzero(&cfork, sizeof(cfork)); | |
675 | cfork.cf_clump = nodesize * nodecnt; | |
676 | ||
6d2010ae A |
677 | result = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, |
678 | &cfork, &vp, &newvnode_flags); | |
2d21ac55 A |
679 | if (result) { |
680 | goto exit; | |
681 | } | |
91447636 A |
682 | /* |
683 | * Set up Attribute B-tree control block | |
684 | */ | |
685 | MALLOC(btcb, BTreeControlBlock *, sizeof(BTreeControlBlock), M_TEMP, M_WAITOK); | |
686 | bzero(btcb, sizeof(BTreeControlBlock)); | |
687 | ||
688 | btcb->nodeSize = nodesize; | |
689 | btcb->maxKeyLength = kHFSPlusAttrKeyMaximumLength; | |
690 | btcb->btreeType = 0xFF; | |
691 | btcb->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask; | |
692 | btcb->version = kBTreeVersion; | |
693 | btcb->writeCount = 1; | |
694 | btcb->flags = 0; /* kBTHeaderDirty */ | |
695 | btcb->fileRefNum = vp; | |
696 | btcb->getBlockProc = GetBTreeBlock; | |
697 | btcb->releaseBlockProc = ReleaseBTreeBlock; | |
698 | btcb->setEndOfForkProc = ExtendBTreeFile; | |
699 | btcb->keyCompareProc = (KeyCompareProcPtr)hfs_attrkeycompare; | |
700 | VTOF(vp)->fcbBTCBPtr = btcb; | |
701 | ||
702 | /* | |
703 | * Allocate some space | |
704 | */ | |
2d21ac55 A |
705 | if (hfs_start_transaction(hfsmp) != 0) { |
706 | result = EINVAL; | |
707 | goto exit; | |
708 | } | |
709 | intrans = 1; | |
710 | ||
711 | /* Note ExtendBTreeFile will acquire the necessary system file locks. */ | |
91447636 A |
712 | result = ExtendBTreeFile(vp, nodesize, cfork.cf_clump); |
713 | if (result) | |
714 | goto exit; | |
715 | ||
716 | btcb->totalNodes = VTOF(vp)->ff_size / nodesize; | |
91447636 | 717 | |
b0d623f7 A |
718 | /* |
719 | * Figure out how many map nodes we'll need. | |
720 | * | |
721 | * bytes_per_map_record = the number of bytes in the map record of a | |
722 | * map node. Since that is the only record in the node, it is the size | |
723 | * of the node minus the node descriptor at the start, and two record | |
724 | * offsets at the end of the node. The "- 2" is to round the size down | |
725 | * to a multiple of 4 bytes (since sizeof(BTNodeDescriptor) is not a | |
726 | * multiple of 4). | |
727 | * | |
728 | * The value "temp" here is the number of *bits* in the map record of | |
729 | * the header node. | |
730 | */ | |
731 | bytes_per_map_record = nodesize - sizeof(BTNodeDescriptor) - 2*sizeof(u_int16_t) - 2; | |
732 | temp = 8 * (nodesize - sizeof(BTNodeDescriptor) | |
733 | - sizeof(BTHeaderRec) | |
734 | - kBTreeHeaderUserBytes | |
735 | - 4 * sizeof(u_int16_t)); | |
736 | if (btcb->totalNodes > temp) { | |
737 | num_map_nodes = howmany(btcb->totalNodes - temp, bytes_per_map_record * 8); | |
738 | } | |
739 | else { | |
740 | num_map_nodes = 0; | |
741 | } | |
742 | ||
743 | btcb->freeNodes = btcb->totalNodes - 1 - num_map_nodes; | |
744 | ||
91447636 A |
745 | /* |
746 | * Initialize the b-tree header on disk | |
747 | */ | |
748 | bp = buf_getblk(vp, 0, nodesize, 0, 0, BLK_META); | |
749 | if (bp == NULL) { | |
750 | result = EIO; | |
751 | goto exit; | |
752 | } | |
753 | ||
754 | buffer = (void *)buf_dataptr(bp); | |
755 | blkdesc.buffer = buffer; | |
756 | blkdesc.blockHeader = (void *)bp; | |
757 | blkdesc.blockReadFromDisk = 0; | |
758 | blkdesc.isModified = 0; | |
759 | ||
760 | ModifyBlockStart(vp, &blkdesc); | |
761 | ||
762 | if (buf_size(bp) != nodesize) | |
763 | panic("hfs_create_attr_btree: bad buffer size (%d)\n", buf_size(bp)); | |
764 | ||
765 | bzero(buffer, nodesize); | |
2d21ac55 | 766 | index = (u_int16_t *)buffer; |
91447636 A |
767 | |
768 | /* FILL IN THE NODE DESCRIPTOR: */ | |
769 | ndp = (BTNodeDescriptor *)buffer; | |
b0d623f7 A |
770 | if (num_map_nodes != 0) |
771 | ndp->fLink = 1; | |
91447636 A |
772 | ndp->kind = kBTHeaderNode; |
773 | ndp->numRecords = 3; | |
774 | offset = sizeof(BTNodeDescriptor); | |
775 | index[(nodesize / 2) - 1] = offset; | |
776 | ||
777 | /* FILL IN THE HEADER RECORD: */ | |
2d21ac55 | 778 | bthp = (BTHeaderRec *)((u_int8_t *)buffer + offset); |
91447636 A |
779 | bthp->nodeSize = nodesize; |
780 | bthp->totalNodes = btcb->totalNodes; | |
781 | bthp->freeNodes = btcb->freeNodes; | |
782 | bthp->clumpSize = cfork.cf_clump; | |
783 | bthp->btreeType = 0xFF; | |
784 | bthp->attributes = kBTVariableIndexKeysMask | kBTBigKeysMask; | |
785 | bthp->maxKeyLength = kHFSPlusAttrKeyMaximumLength; | |
786 | bthp->keyCompareType = kHFSBinaryCompare; | |
787 | offset += sizeof(BTHeaderRec); | |
788 | index[(nodesize / 2) - 2] = offset; | |
789 | ||
790 | /* FILL IN THE USER RECORD: */ | |
791 | offset += kBTreeHeaderUserBytes; | |
792 | index[(nodesize / 2) - 3] = offset; | |
793 | ||
b0d623f7 A |
794 | /* Mark the header node and map nodes in use in the map record. |
795 | * | |
796 | * NOTE: Assumes that the header node's map record has at least | |
797 | * (num_map_nodes + 1) bits. | |
798 | */ | |
799 | bitmap = (u_int8_t *) buffer + offset; | |
800 | temp = num_map_nodes + 1; /* +1 for the header node */ | |
801 | while (temp >= 8) { | |
802 | *(bitmap++) = 0xFF; | |
803 | temp -= 8; | |
804 | } | |
805 | *bitmap = ~(0xFF >> temp); | |
806 | ||
91447636 A |
807 | offset += nodesize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec) |
808 | - kBTreeHeaderUserBytes - (4 * sizeof(int16_t)); | |
809 | index[(nodesize / 2) - 4] = offset; | |
810 | ||
811 | if (hfsmp->jnl) { | |
812 | result = btree_journal_modify_block_end(hfsmp, bp); | |
813 | } else { | |
814 | result = VNOP_BWRITE(bp); | |
815 | } | |
816 | if (result) | |
817 | goto exit; | |
818 | ||
b0d623f7 A |
819 | /* Create the map nodes: node numbers 1 .. num_map_nodes */ |
820 | for (node_num=1; node_num <= num_map_nodes; ++node_num) { | |
821 | bp = buf_getblk(vp, node_num, nodesize, 0, 0, BLK_META); | |
822 | if (bp == NULL) { | |
823 | result = EIO; | |
824 | goto exit; | |
825 | } | |
826 | buffer = (void *)buf_dataptr(bp); | |
827 | blkdesc.buffer = buffer; | |
828 | blkdesc.blockHeader = (void *)bp; | |
829 | blkdesc.blockReadFromDisk = 0; | |
830 | blkdesc.isModified = 0; | |
831 | ||
832 | ModifyBlockStart(vp, &blkdesc); | |
833 | ||
834 | bzero(buffer, nodesize); | |
835 | index = (u_int16_t *)buffer; | |
836 | ||
837 | /* Fill in the node descriptor */ | |
838 | ndp = (BTNodeDescriptor *)buffer; | |
839 | if (node_num != num_map_nodes) | |
840 | ndp->fLink = node_num + 1; | |
841 | ndp->kind = kBTMapNode; | |
842 | ndp->numRecords = 1; | |
843 | offset = sizeof(BTNodeDescriptor); | |
844 | index[(nodesize / 2) - 1] = offset; | |
845 | ||
846 | ||
847 | /* Fill in the map record's offset */ | |
848 | /* Note: We assume that the map record is all zeroes */ | |
849 | offset = sizeof(BTNodeDescriptor) + bytes_per_map_record; | |
850 | index[(nodesize / 2) - 2] = offset; | |
851 | ||
852 | if (hfsmp->jnl) { | |
853 | result = btree_journal_modify_block_end(hfsmp, bp); | |
854 | } else { | |
855 | result = VNOP_BWRITE(bp); | |
856 | } | |
857 | if (result) | |
858 | goto exit; | |
859 | } | |
860 | ||
2d21ac55 A |
861 | /* Update vp/cp for attribute btree */ |
862 | lck_mtx_lock(&hfsmp->hfs_mutex); | |
863 | hfsmp->hfs_attribute_cp = VTOC(vp); | |
91447636 | 864 | hfsmp->hfs_attribute_vp = vp; |
2d21ac55 | 865 | lck_mtx_unlock(&hfsmp->hfs_mutex); |
91447636 | 866 | |
2d21ac55 | 867 | (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); |
91447636 | 868 | exit: |
2d21ac55 A |
869 | if (vp) { |
870 | hfs_unlock(VTOC(vp)); | |
871 | } | |
91447636 A |
872 | if (result) { |
873 | if (btcb) { | |
874 | FREE (btcb, M_TEMP); | |
875 | } | |
2d21ac55 A |
876 | if (vp) { |
877 | vnode_put(vp); | |
878 | } | |
879 | /* XXX need to give back blocks ? */ | |
880 | } | |
881 | if (intrans) { | |
882 | hfs_end_transaction(hfsmp); | |
91447636 | 883 | } |
91447636 | 884 | |
2d21ac55 A |
885 | /* |
886 | * All done, clear HFS_CREATING_BTREE, and wake up any sleepers. | |
887 | */ | |
888 | lck_mtx_lock(&hfsmp->hfs_mutex); | |
889 | hfsmp->hfs_flags &= ~HFS_CREATING_BTREE; | |
890 | wakeup((caddr_t)hfsmp->hfs_attribute_cp); | |
891 | lck_mtx_unlock(&hfsmp->hfs_mutex); | |
91447636 | 892 | |
2d21ac55 A |
893 | return (result); |
894 | } | |
91447636 | 895 |