2 * Copyright (c) 2013-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <sys/systm.h>
29 #include <sys/kauth.h>
31 #include <sys/vnode_internal.h>
32 #include <sys/mount_internal.h>
34 #include <sys/buf_internal.h>
35 #include <vfs/vfs_journal.h>
36 #include <miscfs/specfs/specdev.h>
39 #include "hfs_catalog.h"
40 #include "hfs_cnode.h"
41 #include "hfs_endian.h"
42 #include "hfs_btreeio.h"
43 #include "hfs_cprotect.h"
45 /* Enable/disable debugging code for live volume resizing */
46 int hfs_resize_debug
= 0;
48 static errno_t
hfs_file_extent_overlaps(struct hfsmount
*hfsmp
, u_int32_t allocLimit
,
49 struct HFSPlusCatalogFile
*filerec
, bool *overlaps
);
50 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, u_int32_t reclaimblks
, vfs_context_t context
);
51 static int hfs_extend_journal(struct hfsmount
*hfsmp
, u_int32_t sector_size
, u_int64_t sector_count
, vfs_context_t context
);
54 * Extend a file system.
57 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
59 struct proc
*p
= vfs_context_proc(context
);
60 kauth_cred_t cred
= vfs_context_ucred(context
);
64 struct filefork
*fp
= NULL
;
66 struct cat_fork forkdata
;
69 u_int64_t prev_phys_block_count
;
71 u_int64_t sector_count
;
72 u_int32_t sector_size
;
73 u_int32_t phys_sector_size
;
74 u_int32_t overage_blocks
;
75 daddr64_t prev_fs_alt_sector
;
79 int64_t oldBitmapSize
;
81 Boolean usedExtendFileC
= false;
82 int transaction_begun
= 0;
84 devvp
= hfsmp
->hfs_devvp
;
85 vcb
= HFSTOVCB(hfsmp
);
88 * - HFS Plus file systems only.
89 * - Journaling must be enabled.
90 * - No embedded volumes.
92 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
93 (hfsmp
->jnl
== NULL
) ||
94 (vcb
->hfsPlusIOPosOffset
!= 0)) {
98 * If extending file system by non-root, then verify
99 * ownership and check permissions.
101 if (suser(cred
, NULL
)) {
102 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0, 0);
106 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
108 error
= hfs_write_access(vp
, cred
, p
, false);
110 hfs_unlock(VTOC(vp
));
115 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
119 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§or_size
, 0, context
)) {
122 if (sector_size
!= hfsmp
->hfs_logical_block_size
) {
125 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§or_count
, 0, context
)) {
128 /* Check if partition size is correct for new file system size */
129 if ((sector_size
* sector_count
) < newsize
) {
130 printf("hfs_extendfs: not enough space on device (vol=%s)\n", hfsmp
->vcbVN
);
133 error
= VNOP_IOCTL(devvp
, DKIOCGETPHYSICALBLOCKSIZE
, (caddr_t
)&phys_sector_size
, 0, context
);
135 if ((error
!= ENOTSUP
) && (error
!= ENOTTY
)) {
138 /* If ioctl is not supported, force physical and logical sector size to be same */
139 phys_sector_size
= sector_size
;
141 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
146 if ((newsize
<= oldsize
) || (newsize
% sector_size
) || (newsize
% phys_sector_size
)) {
147 printf("hfs_extendfs: invalid size (newsize=%qu, oldsize=%qu)\n", newsize
, oldsize
);
150 newblkcnt
= newsize
/ vcb
->blockSize
;
151 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF) {
152 printf ("hfs_extendfs: current blockSize=%u too small for newsize=%qu\n", hfsmp
->blockSize
, newsize
);
156 addblks
= newblkcnt
- vcb
->totalBlocks
;
158 if (hfs_resize_debug
) {
159 printf ("hfs_extendfs: old: size=%qu, blkcnt=%u\n", oldsize
, hfsmp
->totalBlocks
);
160 printf ("hfs_extendfs: new: size=%qu, blkcnt=%u, addblks=%u\n", newsize
, (u_int32_t
)newblkcnt
, addblks
);
162 printf("hfs_extendfs: will extend \"%s\" by %d blocks\n", vcb
->vcbVN
, addblks
);
164 hfs_lock_mount (hfsmp
);
165 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
166 hfs_unlock_mount(hfsmp
);
170 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
171 hfs_unlock_mount (hfsmp
);
173 /* Start with a clean journal. */
174 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
177 * Enclose changes inside a transaction.
179 if (hfs_start_transaction(hfsmp
) != 0) {
183 transaction_begun
= 1;
186 /* Update the hfsmp fields for the physical information about the device */
187 prev_phys_block_count
= hfsmp
->hfs_logical_block_count
;
188 prev_fs_alt_sector
= hfsmp
->hfs_fs_avh_sector
;
190 hfsmp
->hfs_logical_block_count
= sector_count
;
191 hfsmp
->hfs_logical_bytes
= (uint64_t) sector_count
* (uint64_t) sector_size
;
194 * It is possible that the new file system is smaller than the partition size.
195 * Therefore, update offsets for AVH accordingly.
197 if (hfs_resize_debug
) {
198 printf ("hfs_extendfs: old: partition_avh_sector=%qu, fs_avh_sector=%qu\n",
199 hfsmp
->hfs_partition_avh_sector
, hfsmp
->hfs_fs_avh_sector
);
201 hfsmp
->hfs_partition_avh_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sector_size
) +
202 HFS_ALT_SECTOR(sector_size
, hfsmp
->hfs_logical_block_count
);
204 hfsmp
->hfs_fs_avh_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sector_size
) +
205 HFS_ALT_SECTOR(sector_size
, (newsize
/hfsmp
->hfs_logical_block_size
));
206 if (hfs_resize_debug
) {
207 printf ("hfs_extendfs: new: partition_avh_sector=%qu, fs_avh_sector=%qu\n",
208 hfsmp
->hfs_partition_avh_sector
, hfsmp
->hfs_fs_avh_sector
);
212 * Note: we take the attributes lock in case we have an attribute data vnode
213 * which needs to change size.
215 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
216 vp
= vcb
->allocationsRefNum
;
218 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
221 * Calculate additional space required (if any) by allocation bitmap.
223 oldBitmapSize
= fp
->ff_size
;
224 bitmapblks
= roundup((newblkcnt
+7) / 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
225 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
226 bitmapblks
-= fp
->ff_blocks
;
231 * The allocation bitmap can contain unused bits that are beyond end of
232 * current volume's allocation blocks. Usually they are supposed to be
233 * zero'ed out but there can be cases where they might be marked as used.
234 * After extending the file system, those bits can represent valid
235 * allocation blocks, so we mark all the bits from the end of current
236 * volume to end of allocation bitmap as "free".
238 * Figure out the number of overage blocks before proceeding though,
239 * so we don't add more bytes to our I/O than necessary.
240 * First figure out the total number of blocks representable by the
241 * end of the bitmap file vs. the total number of blocks in the new FS.
242 * Then subtract away the number of blocks in the current FS. This is how much
243 * we can mark as free right now without having to grow the bitmap file.
245 overage_blocks
= fp
->ff_blocks
* vcb
->blockSize
* 8;
246 overage_blocks
= MIN (overage_blocks
, newblkcnt
);
247 overage_blocks
-= vcb
->totalBlocks
;
249 BlockMarkFreeUnused(vcb
, vcb
->totalBlocks
, overage_blocks
);
251 if (bitmapblks
> 0) {
257 * Get the bitmap's current size (in allocation blocks) so we know
258 * where to start zero filling once the new space is added. We've
259 * got to do this before the bitmap is grown.
261 blkno
= (daddr64_t
)fp
->ff_blocks
;
264 * Try to grow the allocation file in the normal way, using allocation
265 * blocks already existing in the file system. This way, we might be
266 * able to grow the bitmap contiguously, or at least in the metadata
269 error
= ExtendFileC(vcb
, fp
, bitmapblks
* vcb
->blockSize
, 0,
270 kEFAllMask
| kEFNoClumpMask
| kEFReserveMask
271 | kEFMetadataMask
| kEFContigMask
, &bytesAdded
);
274 usedExtendFileC
= true;
277 * If the above allocation failed, fall back to allocating the new
278 * extent of the bitmap from the space we're going to add. Since those
279 * blocks don't yet belong to the file system, we have to update the
280 * extent list directly, and manually adjust the file size.
283 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
285 printf("hfs_extendfs: error %d adding extents\n", error
);
288 fp
->ff_blocks
+= bitmapblks
;
289 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
290 VTOC(vp
)->c_flag
|= C_MODIFIED
;
294 * Update the allocation file's size to include the newly allocated
295 * blocks. Note that ExtendFileC doesn't do this, which is why this
296 * statement is outside the above "if" statement.
298 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
301 * Zero out the new bitmap blocks.
308 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
315 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
317 error
= (int)buf_bwrite(bp
);
325 printf("hfs_extendfs: error %d clearing blocks\n", error
);
329 * Mark the new bitmap space as allocated.
331 * Note that ExtendFileC will have marked any blocks it allocated, so
332 * this is only needed if we used AddFileExtent. Also note that this
333 * has to come *after* the zero filling of new blocks in the case where
334 * we used AddFileExtent (since the part of the bitmap we're touching
335 * is in those newly allocated blocks).
337 if (!usedExtendFileC
) {
338 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
340 printf("hfs_extendfs: error %d setting bitmap\n", error
);
343 vcb
->freeBlocks
-= bitmapblks
;
348 * Mark the new alternate VH as allocated.
350 if (vcb
->blockSize
== 512)
351 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
353 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
355 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
360 * Mark the old alternate VH as free.
362 if (vcb
->blockSize
== 512)
363 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
365 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
368 * Adjust file system variables for new space.
370 vcb
->totalBlocks
+= addblks
;
371 vcb
->freeBlocks
+= addblks
;
373 error
= hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
375 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
377 * Restore to old state.
379 if (usedExtendFileC
) {
380 (void) TruncateFileC(vcb
, fp
, oldBitmapSize
, 0, FORK_IS_RSRC(fp
),
381 FTOC(fp
)->c_fileid
, false);
383 fp
->ff_blocks
-= bitmapblks
;
384 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
386 * No need to mark the excess blocks free since those bitmap blocks
387 * are no longer part of the bitmap. But we do need to undo the
388 * effect of the "vcb->freeBlocks -= bitmapblks" above.
390 vcb
->freeBlocks
+= bitmapblks
;
392 vcb
->totalBlocks
-= addblks
;
393 vcb
->freeBlocks
-= addblks
;
394 hfsmp
->hfs_logical_block_count
= prev_phys_block_count
;
395 hfsmp
->hfs_fs_avh_sector
= prev_fs_alt_sector
;
396 /* Do not revert hfs_partition_avh_sector because the
397 * partition size is larger than file system size
400 if (vcb
->blockSize
== 512) {
401 if (BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2)) {
402 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
405 if (BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1)) {
406 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
412 * Invalidate the old alternate volume header. We are growing the filesystem so
413 * this sector must be returned to the FS as free space.
416 if (prev_fs_alt_sector
) {
417 if (buf_meta_bread(hfsmp
->hfs_devvp
,
418 HFS_PHYSBLK_ROUNDDOWN(prev_fs_alt_sector
, hfsmp
->hfs_log_per_phys
),
419 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
) == 0) {
420 journal_modify_block_start(hfsmp
->jnl
, bp
);
422 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
), kMDBSize
);
424 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
431 * Update the metadata zone size based on current volume size
433 hfs_metadatazone_init(hfsmp
, false);
436 * Adjust the size of hfsmp->hfs_attrdata_vp
438 if (hfsmp
->hfs_attrdata_vp
) {
439 struct cnode
*attr_cp
;
440 struct filefork
*attr_fp
;
442 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
443 attr_cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
444 attr_fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
446 attr_cp
->c_blocks
= newblkcnt
;
447 attr_fp
->ff_blocks
= newblkcnt
;
448 attr_fp
->ff_extents
[0].blockCount
= newblkcnt
;
449 attr_fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
450 ubc_setsize(hfsmp
->hfs_attrdata_vp
, attr_fp
->ff_size
);
451 vnode_put(hfsmp
->hfs_attrdata_vp
);
456 * We only update hfsmp->allocLimit if totalBlocks actually increased.
459 UpdateAllocLimit(hfsmp
, hfsmp
->totalBlocks
);
462 /* Release all locks and sync up journal content before
463 * checking and extending, if required, the journal
466 hfs_systemfile_unlock(hfsmp
, lockflags
);
469 if (transaction_begun
) {
470 hfs_end_transaction(hfsmp
);
471 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
472 transaction_begun
= 0;
475 /* Increase the journal size, if required. */
476 error
= hfs_extend_journal(hfsmp
, sector_size
, sector_count
, context
);
478 printf ("hfs_extendfs: Could not extend journal size\n");
482 /* Log successful extending */
483 printf("hfs_extendfs: extended \"%s\" to %d blocks (was %d blocks)\n",
484 hfsmp
->vcbVN
, hfsmp
->totalBlocks
, (u_int32_t
)(oldsize
/hfsmp
->blockSize
));
488 /* Restore allocation fork. */
489 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
490 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
495 hfs_lock_mount (hfsmp
);
496 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
497 hfs_unlock_mount (hfsmp
);
499 hfs_systemfile_unlock(hfsmp
, lockflags
);
501 if (transaction_begun
) {
502 hfs_end_transaction(hfsmp
);
503 /* Just to be sure, sync all data to the disk */
504 int flush_error
= hfs_flush(hfsmp
, HFS_FLUSH_FULL
);
505 if (flush_error
&& !error
)
509 printf ("hfs_extentfs: failed error=%d on vol=%s\n", MacToVFSError(error
), hfsmp
->vcbVN
);
512 return MacToVFSError(error
);
515 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
518 * Truncate a file system (while still mounted).
521 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
525 u_int32_t reclaimblks
= 0;
527 int transaction_begun
= 0;
528 Boolean updateFreeBlocks
= false;
529 Boolean disable_sparse
= false;
532 hfs_lock_mount (hfsmp
);
533 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
534 hfs_unlock_mount (hfsmp
);
537 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
538 hfsmp
->hfs_resize_blocksmoved
= 0;
539 hfsmp
->hfs_resize_totalblocks
= 0;
540 hfsmp
->hfs_resize_progress
= 0;
541 hfs_unlock_mount (hfsmp
);
544 * - Journaled HFS Plus volumes only.
545 * - No embedded volumes.
547 if ((hfsmp
->jnl
== NULL
) ||
548 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
552 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
553 newblkcnt
= newsize
/ hfsmp
->blockSize
;
554 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
556 if (hfs_resize_debug
) {
557 printf ("hfs_truncatefs: old: size=%qu, blkcnt=%u, freeblks=%u\n", oldsize
, hfsmp
->totalBlocks
, hfs_freeblks(hfsmp
, 1));
558 printf ("hfs_truncatefs: new: size=%qu, blkcnt=%u, reclaimblks=%u\n", newsize
, newblkcnt
, reclaimblks
);
561 /* Make sure new size is valid. */
562 if ((newsize
< HFS_MIN_SIZE
) ||
563 (newsize
>= oldsize
) ||
564 (newsize
% hfsmp
->hfs_logical_block_size
) ||
565 (newsize
% hfsmp
->hfs_physical_block_size
)) {
566 printf ("hfs_truncatefs: invalid size (newsize=%qu, oldsize=%qu)\n", newsize
, oldsize
);
572 * Make sure that the file system has enough free blocks reclaim.
574 * Before resize, the disk is divided into four zones -
575 * A. Allocated_Stationary - These are allocated blocks that exist
576 * before the new end of disk. These blocks will not be
577 * relocated or modified during resize.
578 * B. Free_Stationary - These are free blocks that exist before the
579 * new end of disk. These blocks can be used for any new
580 * allocations during resize, including allocation for relocating
581 * data from the area of disk being reclaimed.
582 * C. Allocated_To-Reclaim - These are allocated blocks that exist
583 * beyond the new end of disk. These blocks need to be reclaimed
584 * during resize by allocating equal number of blocks in Free
585 * Stationary zone and copying the data.
586 * D. Free_To-Reclaim - These are free blocks that exist beyond the
587 * new end of disk. Nothing special needs to be done to reclaim
590 * Total number of blocks on the disk before resize:
591 * ------------------------------------------------
592 * Total Blocks = Allocated_Stationary + Free_Stationary +
593 * Allocated_To-Reclaim + Free_To-Reclaim
595 * Total number of blocks that need to be reclaimed:
596 * ------------------------------------------------
597 * Blocks to Reclaim = Allocated_To-Reclaim + Free_To-Reclaim
599 * Note that the check below also makes sure that we have enough space
600 * to relocate data from Allocated_To-Reclaim to Free_Stationary.
601 * Therefore we do not need to check total number of blocks to relocate
604 * The condition below gets converted to:
606 * Allocated To-Reclaim + Free To-Reclaim >= Free Stationary + Free To-Reclaim
608 * which is equivalent to:
610 * Allocated To-Reclaim >= Free Stationary
612 if (reclaimblks
>= hfs_freeblks(hfsmp
, 1)) {
613 printf("hfs_truncatefs: insufficient space (need %u blocks; have %u free blocks)\n", reclaimblks
, hfs_freeblks(hfsmp
, 1));
618 /* Start with a clean journal. */
619 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
621 if (hfs_start_transaction(hfsmp
) != 0) {
625 transaction_begun
= 1;
627 /* Take the bitmap lock to update the alloc limit field */
628 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
631 * Prevent new allocations from using the part we're trying to truncate.
633 * NOTE: allocLimit is set to the allocation block number where the new
634 * alternate volume header will be. That way there will be no files to
635 * interfere with allocating the new alternate volume header, and no files
636 * in the allocation blocks beyond (i.e. the blocks we're trying to
639 if (hfsmp
->blockSize
== 512) {
640 error
= UpdateAllocLimit (hfsmp
, newblkcnt
- 2);
643 error
= UpdateAllocLimit (hfsmp
, newblkcnt
- 1);
646 /* Sparse devices use first fit allocation which is not ideal
647 * for volume resize which requires best fit allocation. If a
648 * sparse device is being truncated, disable the sparse device
649 * property temporarily for the duration of resize. Also reset
650 * the free extent cache so that it is rebuilt as sorted by
651 * totalBlocks instead of startBlock.
653 * Note that this will affect all allocations on the volume and
654 * ideal fix would be just to modify resize-related allocations,
655 * but it will result in complexity like handling of two free
656 * extent caches sorted differently, etc. So we stick to this
659 hfs_lock_mount (hfsmp
);
660 if (hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) {
661 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
662 ResetVCBFreeExtCache(hfsmp
);
663 disable_sparse
= true;
667 * Update the volume free block count to reflect the total number
668 * of free blocks that will exist after a successful resize.
669 * Relocation of extents will result in no net change in the total
670 * free space on the disk. Therefore the code that allocates
671 * space for new extent and deallocates the old extent explicitly
672 * prevents updating the volume free block count. It will also
673 * prevent false disk full error when the number of blocks in
674 * an extent being relocated is more than the free blocks that
675 * will exist after the volume is resized.
677 hfsmp
->reclaimBlocks
= reclaimblks
;
678 hfsmp
->freeBlocks
-= reclaimblks
;
679 updateFreeBlocks
= true;
680 hfs_unlock_mount(hfsmp
);
683 hfs_systemfile_unlock(hfsmp
, lockflags
);
688 * Update the metadata zone size to match the new volume size,
689 * and if it too less, metadata zone might be disabled.
691 hfs_metadatazone_init(hfsmp
, false);
694 * If some files have blocks at or beyond the location of the
695 * new alternate volume header, recalculate free blocks and
696 * reclaim blocks. Otherwise just update free blocks count.
698 * The current allocLimit is set to the location of new alternate
699 * volume header, and reclaimblks are the total number of blocks
700 * that need to be reclaimed. So the check below is really
701 * ignoring the blocks allocated for old alternate volume header.
703 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
705 * hfs_reclaimspace will use separate transactions when
706 * relocating files (so we don't overwhelm the journal).
708 hfs_end_transaction(hfsmp
);
709 transaction_begun
= 0;
711 /* Attempt to reclaim some space. */
712 error
= hfs_reclaimspace(hfsmp
, hfsmp
->allocLimit
, reclaimblks
, context
);
714 printf("hfs_truncatefs: couldn't reclaim space on %s (error=%d)\n", hfsmp
->vcbVN
, error
);
719 if (hfs_start_transaction(hfsmp
) != 0) {
723 transaction_begun
= 1;
725 /* Check if we're clear now. */
726 error
= hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
);
728 printf("hfs_truncatefs: didn't reclaim enough space on %s (error=%d)\n", hfsmp
->vcbVN
, error
);
729 error
= EAGAIN
; /* tell client to try again */
735 * Note: we take the attributes lock in case we have an attribute data vnode
736 * which needs to change size.
738 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
741 * Allocate last 1KB for alternate volume header.
743 error
= BlockMarkAllocated(hfsmp
, hfsmp
->allocLimit
, (hfsmp
->blockSize
== 512) ? 2 : 1);
745 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error
);
750 * Mark the old alternate volume header as free.
751 * We don't bother shrinking allocation bitmap file.
753 if (hfsmp
->blockSize
== 512)
754 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
756 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
758 /* Don't invalidate the old AltVH yet. It is still valid until the partition size is updated ! */
760 /* Log successful shrinking. */
761 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
762 hfsmp
->vcbVN
, newblkcnt
, hfsmp
->totalBlocks
);
765 * Adjust file system variables and flush them to disk.
767 * Note that although the logical block size is updated here, it is only
768 * done for the benefit/convenience of the partition management software. The
769 * logical block count change has not yet actually been propagated to
770 * the disk device yet (and we won't get any notification when it does).
772 hfsmp
->totalBlocks
= newblkcnt
;
773 hfsmp
->hfs_logical_block_count
= newsize
/ hfsmp
->hfs_logical_block_size
;
774 hfsmp
->hfs_logical_bytes
= (uint64_t) hfsmp
->hfs_logical_block_count
* (uint64_t) hfsmp
->hfs_logical_block_size
;
775 hfsmp
->reclaimBlocks
= 0;
778 * At this point, a smaller HFS file system exists in a larger volume.
779 * As per volume format, the alternate volume header is located 1024 bytes
780 * before end of the partition. So, until the partition is also resized,
781 * a valid alternate volume header will need to be updated at 1024 bytes
782 * before end of the volume. Under normal circumstances, a file system
783 * resize is always followed by a volume resize, so we also need to
784 * write a copy of the new alternate volume header at 1024 bytes before
785 * end of the new file system.
787 if (hfs_resize_debug
) {
788 printf ("hfs_truncatefs: old: partition_avh_sector=%qu, fs_avh_sector=%qu\n",
789 hfsmp
->hfs_partition_avh_sector
, hfsmp
->hfs_fs_avh_sector
);
791 hfsmp
->hfs_fs_avh_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
, hfsmp
->hfs_logical_block_count
);
792 /* Note hfs_partition_avh_sector stays unchanged! partition size has not yet been modified */
793 if (hfs_resize_debug
) {
794 printf ("hfs_truncatefs: new: partition_avh_sector=%qu, fs_avh_sector=%qu\n",
795 hfsmp
->hfs_partition_avh_sector
, hfsmp
->hfs_fs_avh_sector
);
799 error
= hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
801 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
805 * Adjust the size of hfsmp->hfs_attrdata_vp
807 if (hfsmp
->hfs_attrdata_vp
) {
811 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
812 cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
813 fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
815 cp
->c_blocks
= newblkcnt
;
816 fp
->ff_blocks
= newblkcnt
;
817 fp
->ff_extents
[0].blockCount
= newblkcnt
;
818 fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
819 ubc_setsize(hfsmp
->hfs_attrdata_vp
, fp
->ff_size
);
820 vnode_put(hfsmp
->hfs_attrdata_vp
);
826 * Update the allocLimit to acknowledge the last one or two blocks now.
827 * Add it to the tree as well if necessary.
829 UpdateAllocLimit (hfsmp
, hfsmp
->totalBlocks
);
831 hfs_lock_mount (hfsmp
);
832 if (disable_sparse
== true) {
833 /* Now that resize is completed, set the volume to be sparse
834 * device again so that all further allocations will be first
835 * fit instead of best fit. Reset free extent cache so that
838 hfsmp
->hfs_flags
|= HFS_HAS_SPARSE_DEVICE
;
839 ResetVCBFreeExtCache(hfsmp
);
842 if (error
&& (updateFreeBlocks
== true)) {
843 hfsmp
->freeBlocks
+= reclaimblks
;
845 hfsmp
->reclaimBlocks
= 0;
847 if (hfsmp
->nextAllocation
>= hfsmp
->allocLimit
) {
848 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
850 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
851 hfs_unlock_mount (hfsmp
);
853 /* On error, reset the metadata zone for original volume size */
854 if (error
&& (updateFreeBlocks
== true)) {
855 hfs_metadatazone_init(hfsmp
, false);
859 hfs_systemfile_unlock(hfsmp
, lockflags
);
861 if (transaction_begun
) {
862 hfs_end_transaction(hfsmp
);
863 /* Just to be sure, sync all data to the disk */
864 int flush_error
= hfs_flush(hfsmp
, HFS_FLUSH_FULL
);
865 if (flush_error
&& !error
)
870 printf ("hfs_truncatefs: failed error=%d on vol=%s\n", MacToVFSError(error
), hfsmp
->vcbVN
);
873 return MacToVFSError(error
);
878 * Invalidate the physical block numbers associated with buffer cache blocks
879 * in the given extent of the given vnode.
881 struct hfs_inval_blk_no
{
882 daddr64_t sectorStart
;
883 daddr64_t sectorCount
;
886 hfs_invalidate_block_numbers_callback(buf_t bp
, void *args_in
)
889 struct hfs_inval_blk_no
*args
;
891 blkno
= buf_blkno(bp
);
894 if (blkno
>= args
->sectorStart
&& blkno
< args
->sectorStart
+args
->sectorCount
)
895 buf_setblkno(bp
, buf_lblkno(bp
));
900 hfs_invalidate_sectors(struct vnode
*vp
, daddr64_t sectorStart
, daddr64_t sectorCount
)
902 struct hfs_inval_blk_no args
;
903 args
.sectorStart
= sectorStart
;
904 args
.sectorCount
= sectorCount
;
906 buf_iterate(vp
, hfs_invalidate_block_numbers_callback
, BUF_SCAN_DIRTY
|BUF_SCAN_CLEAN
, &args
);
911 * Copy the contents of an extent to a new location. Also invalidates the
912 * physical block number of any buffer cache block in the copied extent
913 * (so that if the block is written, it will go through VNOP_BLOCKMAP to
914 * determine the new physical block number).
916 * At this point, for regular files, we hold the truncate lock exclusive
917 * and the cnode lock exclusive.
921 struct hfsmount
*hfsmp
,
922 struct vnode
*vp
, /* The file whose extent is being copied. */
923 u_int32_t oldStart
, /* The start of the source extent. */
924 u_int32_t newStart
, /* The start of the destination extent. */
925 u_int32_t blockCount
, /* The number of allocation blocks to copy. */
926 __unused vfs_context_t context
)
931 struct vfsioattr ioattr
;
935 u_int32_t ioSizeSectors
; /* Device sectors in this I/O */
936 daddr64_t srcSector
, destSector
;
937 u_int32_t sectorsPerBlock
= hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
943 * Sanity check that we have locked the vnode of the file we're copying.
945 * But since hfs_systemfile_lock() doesn't actually take the lock on
946 * the allocation file if a journal is active, ignore the check if the
947 * file being copied is the allocation file.
949 struct cnode
*cp
= VTOC(vp
);
950 if (cp
!= hfsmp
->hfs_allocation_cp
&& cp
->c_lockowner
!= current_thread())
951 panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp
, cp
);
955 * Prepare the CP blob and get it ready for use, if necessary.
957 * Note that we specifically *exclude* system vnodes (catalog, bitmap, extents, EAs),
958 * because they are implicitly protected via the media key on iOS. As such, they
959 * must not be relocated except with the media key. So it is OK to not pass down
960 * a special cpentry to the IOMedia/LwVM code for handling.
962 if (!vnode_issystem (vp
) && vnode_isreg(vp
) && cp_fs_protected (hfsmp
->hfs_mp
)) {
968 * Determine the I/O size to use
970 * NOTE: Many external drives will result in an ioSize of 128KB.
971 * TODO: Should we use a larger buffer, doing several consecutive
972 * reads, then several consecutive writes?
974 vfs_ioattr(hfsmp
->hfs_mp
, &ioattr
);
975 bufferSize
= MIN(ioattr
.io_maxreadcnt
, ioattr
.io_maxwritecnt
);
976 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &buffer
, bufferSize
, VM_KERN_MEMORY_FILE
))
979 /* Get a buffer for doing the I/O */
980 bp
= buf_alloc(hfsmp
->hfs_devvp
);
981 buf_setdataptr(bp
, (uintptr_t)buffer
);
983 resid
= (off_t
) blockCount
* (off_t
) hfsmp
->blockSize
;
984 srcSector
= (daddr64_t
) oldStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
985 destSector
= (daddr64_t
) newStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
987 ioSize
= MIN(bufferSize
, (size_t) resid
);
988 ioSizeSectors
= ioSize
/ hfsmp
->hfs_logical_block_size
;
990 /* Prepare the buffer for reading */
991 buf_reset(bp
, B_READ
);
992 buf_setsize(bp
, ioSize
);
993 buf_setcount(bp
, ioSize
);
994 buf_setblkno(bp
, srcSector
);
995 buf_setlblkno(bp
, srcSector
);
998 * Note that because this is an I/O to the device vp
999 * it is correct to have lblkno and blkno both point to the
1000 * start sector being read from. If it were being issued against the
1001 * underlying file then that would be different.
1004 /* Attach the new CP blob to the buffer if needed */
1007 /* attach the RELOCATION_INFLIGHT flag for the underlying call to VNOP_STRATEGY */
1008 cp
->c_cpentry
->cp_flags
|= CP_RELOCATION_INFLIGHT
;
1009 bufattr_setcpx(buf_attr(bp
), hfsmp
->hfs_resize_cpx
);
1011 /* Initialize the content protection file offset to start at 0 */
1012 buf_setcpoff (bp
, 0);
1017 err
= VNOP_STRATEGY(bp
);
1019 err
= buf_biowait(bp
);
1022 /* Turn the flag off in error cases. */
1024 cp
->c_cpentry
->cp_flags
&= ~CP_RELOCATION_INFLIGHT
;
1027 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err
);
1031 /* Prepare the buffer for writing */
1032 buf_reset(bp
, B_WRITE
);
1033 buf_setsize(bp
, ioSize
);
1034 buf_setcount(bp
, ioSize
);
1035 buf_setblkno(bp
, destSector
);
1036 buf_setlblkno(bp
, destSector
);
1037 if (vnode_issystem(vp
) && journal_uses_fua(hfsmp
->jnl
))
1041 /* Attach the CP to the buffer if needed */
1043 bufattr_setcpx(buf_attr(bp
), hfsmp
->hfs_resize_cpx
);
1045 * The last STRATEGY call may have updated the cp file offset behind our
1046 * back, so we cannot trust it. Re-initialize the content protection
1047 * file offset back to 0 before initiating the write portion of this I/O.
1049 buf_setcpoff (bp
, 0);
1054 vnode_startwrite(hfsmp
->hfs_devvp
);
1055 err
= VNOP_STRATEGY(bp
);
1057 err
= buf_biowait(bp
);
1060 /* Turn the flag off regardless once the strategy call finishes. */
1062 cp
->c_cpentry
->cp_flags
&= ~CP_RELOCATION_INFLIGHT
;
1066 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err
);
1071 srcSector
+= ioSizeSectors
;
1072 destSector
+= ioSizeSectors
;
1077 kmem_free(kernel_map
, (vm_offset_t
)buffer
, bufferSize
);
1079 /* Make sure all writes have been flushed to disk. */
1080 if (vnode_issystem(vp
) && !journal_uses_fua(hfsmp
->jnl
)) {
1082 err
= hfs_flush(hfsmp
, HFS_FLUSH_CACHE
);
1084 printf("hfs_copy_extent: hfs_flush failed (%d)\n", err
);
1085 err
= 0; /* Don't fail the copy. */
1090 hfs_invalidate_sectors(vp
, (daddr64_t
)oldStart
*sectorsPerBlock
, (daddr64_t
)blockCount
*sectorsPerBlock
);
1096 /* Structure to store state of reclaiming extents from a
1097 * given file. hfs_reclaim_file()/hfs_reclaim_xattr()
1098 * initializes the values in this structure which are then
1099 * used by code that reclaims and splits the extents.
1101 struct hfs_reclaim_extent_info
{
1105 u_int8_t is_dirlink
; /* Extent belongs to directory hard link */
1106 u_int8_t is_sysfile
; /* Extent belongs to system file */
1107 u_int8_t is_xattr
; /* Extent belongs to extent-based xattr */
1108 u_int8_t extent_index
;
1109 int lockflags
; /* Locks that reclaim and split code should grab before modifying the extent record */
1110 u_int32_t blocks_relocated
; /* Total blocks relocated for this file till now */
1111 u_int32_t recStartBlock
; /* File allocation block number (FABN) for current extent record */
1112 u_int32_t cur_blockCount
; /* Number of allocation blocks that have been checked for reclaim */
1113 struct filefork
*catalog_fp
; /* If non-NULL, extent is from catalog record */
1115 HFSPlusExtentRecord overflow
;/* Extent record from overflow extents btree */
1116 HFSPlusAttrRecord xattr
; /* Attribute record for large EAs */
1118 HFSPlusExtentDescriptor
*extents
; /* Pointer to current extent record being processed.
1119 * For catalog extent record, points to the correct
1120 * extent information in filefork. For overflow extent
1121 * record, or xattr record, points to extent record
1122 * in the structure above
1124 struct cat_desc
*dirlink_desc
;
1125 struct cat_attr
*dirlink_attr
;
1126 struct filefork
*dirlink_fork
; /* For directory hard links, fp points actually to this */
1127 struct BTreeIterator
*iterator
; /* Shared read/write iterator, hfs_reclaim_file/xattr()
1128 * use it for reading and hfs_reclaim_extent()/hfs_split_extent()
1129 * use it for writing updated extent record
1131 struct FSBufferDescriptor btdata
; /* Shared btdata for reading/writing extent record, same as iterator above */
1132 u_int16_t recordlen
;
1133 int overflow_count
; /* For debugging, counter for overflow extent record */
1134 FCB
*fcb
; /* Pointer to the current btree being traversed */
1138 * Split the current extent into two extents, with first extent
1139 * to contain given number of allocation blocks. Splitting of
1140 * extent creates one new extent entry which can result in
1141 * shifting of many entries through all the extent records of a
1142 * file, and/or creating a new extent record in the overflow
1146 * The diagram below represents two consecutive extent records,
1147 * for simplicity, lets call them record X and X+1 respectively.
1148 * Interesting extent entries have been denoted by letters.
1149 * If the letter is unchanged before and after split, it means
1150 * that the extent entry was not modified during the split.
1151 * A '.' means that the entry remains unchanged after the split
1152 * and is not relevant for our example. A '0' means that the
1153 * extent entry is empty.
1155 * If there isn't sufficient contiguous free space to relocate
1156 * an extent (extent "C" below), we will have to break the one
1157 * extent into multiple smaller extents, and relocate each of
1158 * the smaller extents individually. The way we do this is by
1159 * finding the largest contiguous free space that is currently
1160 * available (N allocation blocks), and then convert extent "C"
1161 * into two extents, C1 and C2, that occupy exactly the same
1162 * allocation blocks as extent C. Extent C1 is the first
1163 * N allocation blocks of extent C, and extent C2 is the remainder
1164 * of extent C. Then we can relocate extent C1 since we know
1165 * we have enough contiguous free space to relocate it in its
1166 * entirety. We then repeat the process starting with extent C2.
1168 * In record X, only the entries following entry C are shifted, and
1169 * the original entry C is replaced with two entries C1 and C2 which
1170 * are actually two extent entries for contiguous allocation blocks.
1172 * Note that the entry E from record X is shifted into record X+1 as
1173 * the new first entry. Since the first entry of record X+1 is updated,
1174 * the FABN will also get updated with the blockCount of entry E.
1175 * This also results in shifting of all extent entries in record X+1.
1176 * Note that the number of empty entries after the split has been
1177 * changed from 3 to 2.
1180 * record X record X+1
1181 * ---------------------===--------- ---------------------------------
1182 * | A | . | . | . | B | C | D | E | | F | . | . | . | G | 0 | 0 | 0 |
1183 * ---------------------===--------- ---------------------------------
1186 * ---------------------=======----- ---------------------------------
1187 * | A | . | . | . | B | C1| C2| D | | E | F | . | . | . | G | 0 | 0 |
1188 * ---------------------=======----- ---------------------------------
1190 * C1.startBlock = C.startBlock
1193 * C2.startBlock = C.startBlock + N
1194 * C2.blockCount = C.blockCount - N
1196 * FABN = old FABN - E.blockCount
1199 * extent_info - This is the structure that contains state about
1200 * the current file, extent, and extent record that
1201 * is being relocated. This structure is shared
1202 * among code that traverses through all the extents
1203 * of the file, code that relocates extents, and
1204 * code that splits the extent.
1205 * newBlockCount - The blockCount of the extent to be split after
1206 * successfully split operation.
1208 * Zero on success, non-zero on failure.
1211 hfs_split_extent(struct hfs_reclaim_extent_info
*extent_info
, uint32_t newBlockCount
)
1214 int index
= extent_info
->extent_index
;
1216 HFSPlusExtentDescriptor shift_extent
; /* Extent entry that should be shifted into next extent record */
1217 HFSPlusExtentDescriptor last_extent
;
1218 HFSPlusExtentDescriptor
*extents
; /* Pointer to current extent record being manipulated */
1219 HFSPlusExtentRecord
*extents_rec
= NULL
;
1220 HFSPlusExtentKey
*extents_key
= NULL
;
1221 HFSPlusAttrRecord
*xattr_rec
= NULL
;
1222 HFSPlusAttrKey
*xattr_key
= NULL
;
1223 struct BTreeIterator iterator
;
1224 struct FSBufferDescriptor btdata
;
1226 uint32_t read_recStartBlock
; /* Starting allocation block number to read old extent record */
1227 uint32_t write_recStartBlock
; /* Starting allocation block number to insert newly updated extent record */
1228 Boolean create_record
= false;
1232 is_xattr
= extent_info
->is_xattr
;
1233 extents
= extent_info
->extents
;
1234 cp
= VTOC(extent_info
->vp
);
1236 if (newBlockCount
== 0) {
1237 if (hfs_resize_debug
) {
1238 printf ("hfs_split_extent: No splitting required for newBlockCount=0\n");
1243 if (hfs_resize_debug
) {
1244 printf ("hfs_split_extent: Split record:%u recStartBlock=%u %u:(%u,%u) for %u blocks\n", extent_info
->overflow_count
, extent_info
->recStartBlock
, index
, extents
[index
].startBlock
, extents
[index
].blockCount
, newBlockCount
);
1247 /* Extents overflow btree can not have more than 8 extents.
1248 * No split allowed if the 8th extent is already used.
1250 if ((extent_info
->fileID
== kHFSExtentsFileID
) && (extents
[kHFSPlusExtentDensity
- 1].blockCount
!= 0)) {
1251 printf ("hfs_split_extent: Maximum 8 extents allowed for extents overflow btree, cannot split further.\n");
1256 /* Determine the starting allocation block number for the following
1257 * overflow extent record, if any, before the current record
1260 read_recStartBlock
= extent_info
->recStartBlock
;
1261 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
1262 if (extents
[i
].blockCount
== 0) {
1265 read_recStartBlock
+= extents
[i
].blockCount
;
1268 /* Shift and split */
1269 if (index
== kHFSPlusExtentDensity
-1) {
1270 /* The new extent created after split will go into following overflow extent record */
1271 shift_extent
.startBlock
= extents
[index
].startBlock
+ newBlockCount
;
1272 shift_extent
.blockCount
= extents
[index
].blockCount
- newBlockCount
;
1274 /* Last extent in the record will be split, so nothing to shift */
1276 /* Splitting of extents can result in at most of one
1277 * extent entry to be shifted into following overflow extent
1278 * record. So, store the last extent entry for later.
1280 shift_extent
= extents
[kHFSPlusExtentDensity
-1];
1281 if ((hfs_resize_debug
) && (shift_extent
.blockCount
!= 0)) {
1282 printf ("hfs_split_extent: Save 7:(%u,%u) to shift into overflow record\n", shift_extent
.startBlock
, shift_extent
.blockCount
);
1285 /* Start shifting extent information from the end of the extent
1286 * record to the index where we want to insert the new extent.
1287 * Note that kHFSPlusExtentDensity-1 is already saved above, and
1288 * does not need to be shifted. The extent entry that is being
1289 * split does not get shifted.
1291 for (i
= kHFSPlusExtentDensity
-2; i
> index
; i
--) {
1292 if (hfs_resize_debug
) {
1293 if (extents
[i
].blockCount
) {
1294 printf ("hfs_split_extent: Shift %u:(%u,%u) to %u:(%u,%u)\n", i
, extents
[i
].startBlock
, extents
[i
].blockCount
, i
+1, extents
[i
].startBlock
, extents
[i
].blockCount
);
1297 extents
[i
+1] = extents
[i
];
1301 if (index
== kHFSPlusExtentDensity
-1) {
1302 /* The second half of the extent being split will be the overflow
1303 * entry that will go into following overflow extent record. The
1304 * value has been stored in 'shift_extent' above, so there is
1305 * nothing to be done here.
1308 /* Update the values in the second half of the extent being split
1309 * before updating the first half of the split. Note that the
1310 * extent to split or first half of the split is at index 'index'
1311 * and a new extent or second half of the split will be inserted at
1312 * 'index+1' or into following overflow extent record.
1314 extents
[index
+1].startBlock
= extents
[index
].startBlock
+ newBlockCount
;
1315 extents
[index
+1].blockCount
= extents
[index
].blockCount
- newBlockCount
;
1317 /* Update the extent being split, only the block count will change */
1318 extents
[index
].blockCount
= newBlockCount
;
1320 if (hfs_resize_debug
) {
1321 printf ("hfs_split_extent: Split %u:(%u,%u) and ", index
, extents
[index
].startBlock
, extents
[index
].blockCount
);
1322 if (index
!= kHFSPlusExtentDensity
-1) {
1323 printf ("%u:(%u,%u)\n", index
+1, extents
[index
+1].startBlock
, extents
[index
+1].blockCount
);
1325 printf ("overflow:(%u,%u)\n", shift_extent
.startBlock
, shift_extent
.blockCount
);
1329 /* Write out information about the newly split extent to the disk */
1330 if (extent_info
->catalog_fp
) {
1331 /* (extent_info->catalog_fp != NULL) means the newly split
1332 * extent exists in the catalog record. This means that
1333 * the cnode was updated. Therefore, to write out the changes,
1334 * mark the cnode as modified. We cannot call hfs_update()
1335 * in this function because the caller hfs_reclaim_extent()
1336 * is holding the catalog lock currently.
1338 cp
->c_flag
|= C_MODIFIED
;
1340 /* The newly split extent is for large EAs or is in overflow
1341 * extent record, so update it directly in the btree using the
1342 * iterator information from the shared extent_info structure
1344 error
= BTReplaceRecord(extent_info
->fcb
, extent_info
->iterator
,
1345 &(extent_info
->btdata
), extent_info
->recordlen
);
1347 printf ("hfs_split_extent: fileID=%u BTReplaceRecord returned error=%d\n", extent_info
->fileID
, error
);
1352 /* No extent entry to be shifted into another extent overflow record */
1353 if (shift_extent
.blockCount
== 0) {
1354 if (hfs_resize_debug
) {
1355 printf ("hfs_split_extent: No extent entry to be shifted into overflow records\n");
1361 /* The overflow extent entry has to be shifted into an extent
1362 * overflow record. This means that we might have to shift
1363 * extent entries from all subsequent overflow records by one.
1364 * We start iteration from the first record to the last record,
1365 * and shift the extent entry from one record to another.
1366 * We might have to create a new extent record for the last
1367 * extent entry for the file.
1370 /* Initialize iterator to search the next record */
1371 bzero(&iterator
, sizeof(iterator
));
1373 /* Copy the key from the iterator that was used to update the modified attribute record. */
1374 xattr_key
= (HFSPlusAttrKey
*)&(iterator
.key
);
1375 bcopy((HFSPlusAttrKey
*)&(extent_info
->iterator
->key
), xattr_key
, sizeof(HFSPlusAttrKey
));
1376 /* Note: xattr_key->startBlock will be initialized later in the iteration loop */
1378 MALLOC(xattr_rec
, HFSPlusAttrRecord
*,
1379 sizeof(HFSPlusAttrRecord
), M_TEMP
, M_WAITOK
);
1380 if (xattr_rec
== NULL
) {
1384 btdata
.bufferAddress
= xattr_rec
;
1385 btdata
.itemSize
= sizeof(HFSPlusAttrRecord
);
1386 btdata
.itemCount
= 1;
1387 extents
= xattr_rec
->overflowExtents
.extents
;
1389 /* Initialize the extent key for the current file */
1390 extents_key
= (HFSPlusExtentKey
*) &(iterator
.key
);
1391 extents_key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
1392 extents_key
->forkType
= extent_info
->forkType
;
1393 extents_key
->fileID
= extent_info
->fileID
;
1394 /* Note: extents_key->startBlock will be initialized later in the iteration loop */
1396 MALLOC(extents_rec
, HFSPlusExtentRecord
*,
1397 sizeof(HFSPlusExtentRecord
), M_TEMP
, M_WAITOK
);
1398 if (extents_rec
== NULL
) {
1402 btdata
.bufferAddress
= extents_rec
;
1403 btdata
.itemSize
= sizeof(HFSPlusExtentRecord
);
1404 btdata
.itemCount
= 1;
1405 extents
= extents_rec
[0];
1408 /* The overflow extent entry has to be shifted into an extent
1409 * overflow record. This means that we might have to shift
1410 * extent entries from all subsequent overflow records by one.
1411 * We start iteration from the first record to the last record,
1412 * examine one extent record in each iteration and shift one
1413 * extent entry from one record to another. We might have to
1414 * create a new extent record for the last extent entry for the
1417 * If shift_extent.blockCount is non-zero, it means that there is
1418 * an extent entry that needs to be shifted into the next
1419 * overflow extent record. We keep on going till there are no such
1420 * entries left to be shifted. This will also change the starting
1421 * allocation block number of the extent record which is part of
1422 * the key for the extent record in each iteration. Note that
1423 * because the extent record key is changing while we are searching,
1424 * the record can not be updated directly, instead it has to be
1425 * deleted and inserted again.
1427 while (shift_extent
.blockCount
) {
1428 if (hfs_resize_debug
) {
1429 printf ("hfs_split_extent: Will shift (%u,%u) into overflow record with startBlock=%u\n", shift_extent
.startBlock
, shift_extent
.blockCount
, read_recStartBlock
);
1432 /* Search if there is any existing overflow extent record
1433 * that matches the current file and the logical start block
1436 * For this, the logical start block number in the key is
1437 * the value calculated based on the logical start block
1438 * number of the current extent record and the total number
1439 * of blocks existing in the current extent record.
1442 xattr_key
->startBlock
= read_recStartBlock
;
1444 extents_key
->startBlock
= read_recStartBlock
;
1446 error
= BTSearchRecord(extent_info
->fcb
, &iterator
, &btdata
, &reclen
, &iterator
);
1448 if (error
!= btNotFound
) {
1449 printf ("hfs_split_extent: fileID=%u startBlock=%u BTSearchRecord error=%d\n", extent_info
->fileID
, read_recStartBlock
, error
);
1452 /* No matching record was found, so create a new extent record.
1453 * Note: Since no record was found, we can't rely on the
1454 * btree key in the iterator any longer. This will be initialized
1455 * later before we insert the record.
1457 create_record
= true;
1460 /* The extra extent entry from the previous record is being inserted
1461 * as the first entry in the current extent record. This will change
1462 * the file allocation block number (FABN) of the current extent
1463 * record, which is the startBlock value from the extent record key.
1464 * Since one extra entry is being inserted in the record, the new
1465 * FABN for the record will less than old FABN by the number of blocks
1466 * in the new extent entry being inserted at the start. We have to
1467 * do this before we update read_recStartBlock to point at the
1468 * startBlock of the following record.
1470 write_recStartBlock
= read_recStartBlock
- shift_extent
.blockCount
;
1471 if (hfs_resize_debug
) {
1472 if (create_record
) {
1473 printf ("hfs_split_extent: No records found for startBlock=%u, will create new with startBlock=%u\n", read_recStartBlock
, write_recStartBlock
);
1477 /* Now update the read_recStartBlock to account for total number
1478 * of blocks in this extent record. It will now point to the
1479 * starting allocation block number for the next extent record.
1481 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
1482 if (extents
[i
].blockCount
== 0) {
1485 read_recStartBlock
+= extents
[i
].blockCount
;
1488 if (create_record
== true) {
1489 /* Initialize new record content with only one extent entry */
1490 bzero(extents
, sizeof(HFSPlusExtentRecord
));
1491 /* The new record will contain only one extent entry */
1492 extents
[0] = shift_extent
;
1493 /* There are no more overflow extents to be shifted */
1494 shift_extent
.startBlock
= shift_extent
.blockCount
= 0;
1497 /* BTSearchRecord above returned btNotFound,
1498 * but since the attribute btree is never empty
1499 * if we are trying to insert new overflow
1500 * record for the xattrs, the extents_key will
1501 * contain correct data. So we don't need to
1502 * re-initialize it again like below.
1505 /* Initialize the new xattr record */
1506 xattr_rec
->recordType
= kHFSPlusAttrExtents
;
1507 xattr_rec
->overflowExtents
.reserved
= 0;
1508 reclen
= sizeof(HFSPlusAttrExtents
);
1510 /* BTSearchRecord above returned btNotFound,
1511 * which means that extents_key content might
1512 * not correspond to the record that we are
1513 * trying to create, especially when the extents
1514 * overflow btree is empty. So we reinitialize
1515 * the extents_key again always.
1517 extents_key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
1518 extents_key
->forkType
= extent_info
->forkType
;
1519 extents_key
->fileID
= extent_info
->fileID
;
1521 /* Initialize the new extent record */
1522 reclen
= sizeof(HFSPlusExtentRecord
);
1525 /* The overflow extent entry from previous record will be
1526 * the first entry in this extent record. If the last
1527 * extent entry in this record is valid, it will be shifted
1528 * into the following extent record as its first entry. So
1529 * save the last entry before shifting entries in current
1532 last_extent
= extents
[kHFSPlusExtentDensity
-1];
1534 /* Shift all entries by one index towards the end */
1535 for (i
= kHFSPlusExtentDensity
-2; i
>= 0; i
--) {
1536 extents
[i
+1] = extents
[i
];
1539 /* Overflow extent entry saved from previous record
1540 * is now the first entry in the current record.
1542 extents
[0] = shift_extent
;
1544 if (hfs_resize_debug
) {
1545 printf ("hfs_split_extent: Shift overflow=(%u,%u) to record with updated startBlock=%u\n", shift_extent
.startBlock
, shift_extent
.blockCount
, write_recStartBlock
);
1548 /* The last entry from current record will be the
1549 * overflow entry which will be the first entry for
1550 * the following extent record.
1552 shift_extent
= last_extent
;
1554 /* Since the key->startBlock is being changed for this record,
1555 * it should be deleted and inserted with the new key.
1557 error
= BTDeleteRecord(extent_info
->fcb
, &iterator
);
1559 printf ("hfs_split_extent: fileID=%u startBlock=%u BTDeleteRecord error=%d\n", extent_info
->fileID
, read_recStartBlock
, error
);
1562 if (hfs_resize_debug
) {
1563 printf ("hfs_split_extent: Deleted extent record with startBlock=%u\n", (is_xattr
? xattr_key
->startBlock
: extents_key
->startBlock
));
1567 /* Insert the newly created or modified extent record */
1568 bzero(&iterator
.hint
, sizeof(iterator
.hint
));
1570 xattr_key
->startBlock
= write_recStartBlock
;
1572 extents_key
->startBlock
= write_recStartBlock
;
1574 error
= BTInsertRecord(extent_info
->fcb
, &iterator
, &btdata
, reclen
);
1576 printf ("hfs_split_extent: fileID=%u, startBlock=%u BTInsertRecord error=%d\n", extent_info
->fileID
, write_recStartBlock
, error
);
1579 if (hfs_resize_debug
) {
1580 printf ("hfs_split_extent: Inserted extent record with startBlock=%u\n", write_recStartBlock
);
1586 * Extents overflow btree or attributes btree headers might have
1587 * been modified during the split/shift operation, so flush the
1588 * changes to the disk while we are inside journal transaction.
1589 * We should only be able to generate I/O that modifies the B-Tree
1590 * header nodes while we're in the middle of a journal transaction.
1591 * Otherwise it might result in panic during unmount.
1593 BTFlushPath(extent_info
->fcb
);
1596 FREE (extents_rec
, M_TEMP
);
1599 FREE (xattr_rec
, M_TEMP
);
1606 * Relocate an extent if it lies beyond the expected end of volume.
1608 * This function is called for every extent of the file being relocated.
1609 * It allocates space for relocation, copies the data, deallocates
1610 * the old extent, and update corresponding on-disk extent. If the function
1611 * does not find contiguous space to relocate an extent, it splits the
1612 * extent in smaller size to be able to relocate it out of the area of
1613 * disk being reclaimed. As an optimization, if an extent lies partially
1614 * in the area of the disk being reclaimed, it is split so that we only
1615 * have to relocate the area that was overlapping with the area of disk
1618 * Note that every extent is relocated in its own transaction so that
1619 * they do not overwhelm the journal. This function handles the extent
1620 * record that exists in the catalog record, extent record from overflow
1621 * extents btree, and extents for large EAs.
1624 * extent_info - This is the structure that contains state about
1625 * the current file, extent, and extent record that
1626 * is being relocated. This structure is shared
1627 * among code that traverses through all the extents
1628 * of the file, code that relocates extents, and
1629 * code that splits the extent.
1632 hfs_reclaim_extent(struct hfsmount
*hfsmp
, const u_long allocLimit
, struct hfs_reclaim_extent_info
*extent_info
, vfs_context_t context
)
1637 u_int32_t oldStartBlock
;
1638 u_int32_t oldBlockCount
;
1639 u_int32_t newStartBlock
;
1640 u_int32_t newBlockCount
;
1641 u_int32_t roundedBlockCount
;
1643 uint32_t remainder_blocks
;
1644 u_int32_t alloc_flags
;
1645 int blocks_allocated
= false;
1647 index
= extent_info
->extent_index
;
1648 cp
= VTOC(extent_info
->vp
);
1650 oldStartBlock
= extent_info
->extents
[index
].startBlock
;
1651 oldBlockCount
= extent_info
->extents
[index
].blockCount
;
1653 if (0 && hfs_resize_debug
) {
1654 printf ("hfs_reclaim_extent: Examine record:%u recStartBlock=%u, %u:(%u,%u)\n", extent_info
->overflow_count
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
);
1657 /* If the current extent lies completely within allocLimit,
1658 * it does not require any relocation.
1660 if ((oldStartBlock
+ oldBlockCount
) <= allocLimit
) {
1661 extent_info
->cur_blockCount
+= oldBlockCount
;
1665 /* Every extent should be relocated in its own transaction
1666 * to make sure that we don't overflow the journal buffer.
1668 error
= hfs_start_transaction(hfsmp
);
1672 extent_info
->lockflags
= hfs_systemfile_lock(hfsmp
, extent_info
->lockflags
, HFS_EXCLUSIVE_LOCK
);
1674 /* Check if the extent lies partially in the area to reclaim,
1675 * i.e. it starts before allocLimit and ends beyond allocLimit.
1676 * We have already skipped extents that lie completely within
1677 * allocLimit in the check above, so we only check for the
1678 * startBlock. If it lies partially, split it so that we
1679 * only relocate part of the extent.
1681 if (oldStartBlock
< allocLimit
) {
1682 newBlockCount
= allocLimit
- oldStartBlock
;
1684 if (hfs_resize_debug
) {
1685 int idx
= extent_info
->extent_index
;
1686 printf ("hfs_reclaim_extent: Split straddling extent %u:(%u,%u) for %u blocks\n", idx
, extent_info
->extents
[idx
].startBlock
, extent_info
->extents
[idx
].blockCount
, newBlockCount
);
1689 /* If the extent belongs to a btree, check and trim
1690 * it to be multiple of the node size.
1692 if (extent_info
->is_sysfile
) {
1693 node_size
= get_btree_nodesize(extent_info
->vp
);
1694 /* If the btree node size is less than the block size,
1695 * splitting this extent will not split a node across
1696 * different extents. So we only check and trim if
1697 * node size is more than the allocation block size.
1699 if (node_size
> hfsmp
->blockSize
) {
1700 remainder_blocks
= newBlockCount
% (node_size
/ hfsmp
->blockSize
);
1701 if (remainder_blocks
) {
1702 newBlockCount
-= remainder_blocks
;
1703 if (hfs_resize_debug
) {
1704 printf ("hfs_reclaim_extent: Round-down newBlockCount to be multiple of nodeSize, node_allocblks=%u, old=%u, new=%u\n", node_size
/hfsmp
->blockSize
, newBlockCount
+ remainder_blocks
, newBlockCount
);
1708 /* The newBlockCount is zero because of rounding-down so that
1709 * btree nodes are not split across extents. Therefore this
1710 * straddling extent across resize-boundary does not require
1711 * splitting. Skip over to relocating of complete extent.
1713 if (newBlockCount
== 0) {
1714 if (hfs_resize_debug
) {
1715 printf ("hfs_reclaim_extent: After round-down newBlockCount=0, skip split, relocate full extent\n");
1717 goto relocate_full_extent
;
1721 /* Split the extents into two parts --- the first extent lies
1722 * completely within allocLimit and therefore does not require
1723 * relocation. The second extent will require relocation which
1724 * will be handled when the caller calls this function again
1725 * for the next extent.
1727 error
= hfs_split_extent(extent_info
, newBlockCount
);
1729 /* Split success, no relocation required */
1732 /* Split failed, so try to relocate entire extent */
1733 if (hfs_resize_debug
) {
1734 int idx
= extent_info
->extent_index
;
1735 printf ("hfs_reclaim_extent: Split straddling extent %u:(%u,%u) for %u blocks failed, relocate full extent\n", idx
, extent_info
->extents
[idx
].startBlock
, extent_info
->extents
[idx
].blockCount
, newBlockCount
);
1739 relocate_full_extent
:
1740 /* At this point, the current extent requires relocation.
1741 * We will try to allocate space equal to the size of the extent
1742 * being relocated first to try to relocate it without splitting.
1743 * If the allocation fails, we will try to allocate contiguous
1744 * blocks out of metadata zone. If that allocation also fails,
1745 * then we will take a whatever contiguous block run is returned
1746 * by the allocation, split the extent into two parts, and then
1747 * relocate the first splitted extent.
1749 alloc_flags
= HFS_ALLOC_FORCECONTIG
| HFS_ALLOC_SKIPFREEBLKS
;
1750 if (extent_info
->is_sysfile
) {
1751 alloc_flags
|= HFS_ALLOC_METAZONE
;
1754 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
, alloc_flags
,
1755 &newStartBlock
, &newBlockCount
);
1756 if ((extent_info
->is_sysfile
== false) &&
1757 ((error
== dskFulErr
) || (error
== ENOSPC
))) {
1758 /* For non-system files, try reallocating space in metadata zone */
1759 alloc_flags
|= HFS_ALLOC_METAZONE
;
1760 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
,
1761 alloc_flags
, &newStartBlock
, &newBlockCount
);
1763 if ((error
== dskFulErr
) || (error
== ENOSPC
)) {
1765 * We did not find desired contiguous space for this
1766 * extent, when we asked for it, including the metazone allocations.
1767 * At this point we are not worrying about getting contiguity anymore.
1769 * HOWEVER, if we now allow blocks to be used which were recently
1770 * de-allocated, we may find a contiguous range (though this seems
1771 * unlikely). As a result, assume that we will have to split the
1772 * current extent into two pieces, but if we are able to satisfy
1773 * the request with a single extent, detect that as well.
1775 alloc_flags
&= ~HFS_ALLOC_FORCECONTIG
;
1776 alloc_flags
|= HFS_ALLOC_FLUSHTXN
;
1778 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
,
1779 alloc_flags
, &newStartBlock
, &newBlockCount
);
1781 printf ("hfs_reclaim_extent: fileID=%u start=%u, %u:(%u,%u) BlockAllocate error=%d\n", extent_info
->fileID
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
, error
);
1786 * Allowing recently deleted extents may now allow us to find
1787 * a single contiguous extent in the amount & size desired. If so,
1788 * do NOT split this extent into two pieces. This is technically a
1789 * check for "< oldBlockCount", but we use != to highlight the point
1790 * that the special case is when they're equal. The allocator should
1791 * never vend back more blocks than were requested.
1793 if (newBlockCount
!= oldBlockCount
) {
1794 blocks_allocated
= true;
1796 /* The number of blocks allocated is less than the requested
1797 * number of blocks. For btree extents, check and trim the
1798 * extent to be multiple of the node size.
1800 if (extent_info
->is_sysfile
) {
1801 node_size
= get_btree_nodesize(extent_info
->vp
);
1802 if (node_size
> hfsmp
->blockSize
) {
1803 remainder_blocks
= newBlockCount
% (node_size
/ hfsmp
->blockSize
);
1804 if (remainder_blocks
) {
1805 roundedBlockCount
= newBlockCount
- remainder_blocks
;
1806 /* Free tail-end blocks of the newly allocated extent */
1807 BlockDeallocate(hfsmp
, newStartBlock
+ roundedBlockCount
,
1808 newBlockCount
- roundedBlockCount
,
1809 HFS_ALLOC_SKIPFREEBLKS
);
1810 newBlockCount
= roundedBlockCount
;
1811 if (hfs_resize_debug
) {
1812 printf ("hfs_reclaim_extent: Fixing extent block count, node_blks=%u, old=%u, new=%u\n", node_size
/hfsmp
->blockSize
, newBlockCount
+ remainder_blocks
, newBlockCount
);
1814 if (newBlockCount
== 0) {
1815 printf ("hfs_reclaim_extent: Not enough contiguous blocks available to relocate fileID=%d\n", extent_info
->fileID
);
1823 /* The number of blocks allocated is less than the number of
1824 * blocks requested, so split this extent --- the first extent
1825 * will be relocated as part of this function call and the caller
1826 * will handle relocating the second extent by calling this
1827 * function again for the second extent.
1829 error
= hfs_split_extent(extent_info
, newBlockCount
);
1831 printf ("hfs_reclaim_extent: fileID=%u start=%u, %u:(%u,%u) split error=%d\n", extent_info
->fileID
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
, error
);
1834 oldBlockCount
= newBlockCount
;
1835 } /* end oldBlockCount != newBlockCount */
1836 } /* end allocation request for any available free space */
1839 printf ("hfs_reclaim_extent: fileID=%u start=%u, %u:(%u,%u) contig BlockAllocate error=%d\n", extent_info
->fileID
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
, error
);
1842 blocks_allocated
= true;
1844 /* Copy data from old location to new location */
1845 error
= hfs_copy_extent(hfsmp
, extent_info
->vp
, oldStartBlock
,
1846 newStartBlock
, newBlockCount
, context
);
1848 printf ("hfs_reclaim_extent: fileID=%u start=%u, %u:(%u,%u)=>(%u,%u) hfs_copy_extent error=%d\n", extent_info
->fileID
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
, newStartBlock
, newBlockCount
, error
);
1852 /* Update the extent record with the new start block information */
1853 extent_info
->extents
[index
].startBlock
= newStartBlock
;
1855 /* Sync the content back to the disk */
1856 if (extent_info
->catalog_fp
) {
1857 /* Update the extents in catalog record */
1858 if (extent_info
->is_dirlink
) {
1859 error
= cat_update_dirlink(hfsmp
, extent_info
->forkType
,
1860 extent_info
->dirlink_desc
, extent_info
->dirlink_attr
,
1861 &(extent_info
->dirlink_fork
->ff_data
));
1863 cp
->c_flag
|= C_MODIFIED
;
1864 /* If this is a system file, sync volume headers on disk */
1865 if (extent_info
->is_sysfile
) {
1866 error
= hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
1870 /* Replace record for extents overflow or extents-based xattrs */
1871 error
= BTReplaceRecord(extent_info
->fcb
, extent_info
->iterator
,
1872 &(extent_info
->btdata
), extent_info
->recordlen
);
1875 printf ("hfs_reclaim_extent: fileID=%u, update record error=%u\n", extent_info
->fileID
, error
);
1879 /* Deallocate the old extent */
1880 error
= BlockDeallocate(hfsmp
, oldStartBlock
, oldBlockCount
, HFS_ALLOC_SKIPFREEBLKS
);
1882 printf ("hfs_reclaim_extent: fileID=%u start=%u, %u:(%u,%u) BlockDeallocate error=%d\n", extent_info
->fileID
, extent_info
->recStartBlock
, index
, oldStartBlock
, oldBlockCount
, error
);
1885 extent_info
->blocks_relocated
+= newBlockCount
;
1887 if (hfs_resize_debug
) {
1888 printf ("hfs_reclaim_extent: Relocated record:%u %u:(%u,%u) to (%u,%u)\n", extent_info
->overflow_count
, index
, oldStartBlock
, oldBlockCount
, newStartBlock
, newBlockCount
);
1893 if (blocks_allocated
== true) {
1894 BlockDeallocate(hfsmp
, newStartBlock
, newBlockCount
, HFS_ALLOC_SKIPFREEBLKS
);
1897 /* On success, increment the total allocation blocks processed */
1898 extent_info
->cur_blockCount
+= newBlockCount
;
1901 hfs_systemfile_unlock(hfsmp
, extent_info
->lockflags
);
1903 /* For a non-system file, if an extent entry from catalog record
1904 * was modified, sync the in-memory changes to the catalog record
1905 * on disk before ending the transaction.
1907 if ((extent_info
->catalog_fp
) &&
1908 (extent_info
->is_sysfile
== false)) {
1909 hfs_update(extent_info
->vp
, 0);
1912 hfs_end_transaction(hfsmp
);
1917 /* Report intermediate progress during volume resize */
1919 hfs_truncatefs_progress(struct hfsmount
*hfsmp
)
1921 u_int32_t cur_progress
= 0;
1923 hfs_resize_progress(hfsmp
, &cur_progress
);
1924 if (cur_progress
> (hfsmp
->hfs_resize_progress
+ 9)) {
1925 printf("hfs_truncatefs: %d%% done...\n", cur_progress
);
1926 hfsmp
->hfs_resize_progress
= cur_progress
;
1932 * Reclaim space at the end of a volume for given file and forktype.
1934 * This routine attempts to move any extent which contains allocation blocks
1935 * at or after "allocLimit." A separate transaction is used for every extent
1936 * that needs to be moved. If there is not contiguous space available for
1937 * moving an extent, it can be split into smaller extents. The contents of
1938 * any moved extents are read and written via the volume's device vnode --
1939 * NOT via "vp." During the move, moved blocks which are part of a transaction
1940 * have their physical block numbers invalidated so they will eventually be
1941 * written to their new locations.
1943 * This function is also called for directory hard links. Directory hard links
1944 * are regular files with no data fork and resource fork that contains alias
1945 * information for backward compatibility with pre-Leopard systems. However
1946 * non-Mac OS X implementation can add/modify data fork or resource fork
1947 * information to directory hard links, so we check, and if required, relocate
1948 * both data fork and resource fork.
1951 * hfsmp The volume being resized.
1952 * vp The vnode for the system file.
1953 * fileID ID of the catalog record that needs to be relocated
1954 * forktype The type of fork that needs relocated,
1955 * kHFSResourceForkType for resource fork,
1956 * kHFSDataForkType for data fork
1957 * allocLimit Allocation limit for the new volume size,
1958 * do not use this block or beyond. All extents
1959 * that use this block or any blocks beyond this limit
1960 * will be relocated.
1963 * hfsmp->hfs_resize_blocksmoved is incremented by the number of allocation
1964 * blocks that were relocated.
1967 hfs_reclaim_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, u_int32_t fileID
,
1968 u_int8_t forktype
, u_long allocLimit
, vfs_context_t context
)
1971 struct hfs_reclaim_extent_info
*extent_info
;
1975 struct filefork
*fp
;
1976 int took_truncate_lock
= false;
1977 int release_desc
= false;
1978 HFSPlusExtentKey
*key
;
1980 /* If there is no vnode for this file, then there's nothing to do. */
1987 if (hfs_resize_debug
) {
1988 const char *filename
= (const char *) cp
->c_desc
.cd_nameptr
;
1989 int namelen
= cp
->c_desc
.cd_namelen
;
1991 if (filename
== NULL
) {
1995 printf("hfs_reclaim_file: reclaiming '%.*s'\n", namelen
, filename
);
1998 MALLOC(extent_info
, struct hfs_reclaim_extent_info
*,
1999 sizeof(struct hfs_reclaim_extent_info
), M_TEMP
, M_WAITOK
);
2000 if (extent_info
== NULL
) {
2003 bzero(extent_info
, sizeof(struct hfs_reclaim_extent_info
));
2004 extent_info
->vp
= vp
;
2005 extent_info
->fileID
= fileID
;
2006 extent_info
->forkType
= forktype
;
2007 extent_info
->is_sysfile
= vnode_issystem(vp
);
2008 if (vnode_isdir(vp
) && (cp
->c_flag
& C_HARDLINK
)) {
2009 extent_info
->is_dirlink
= true;
2011 /* We always need allocation bitmap and extent btree lock */
2012 lockflags
= SFL_BITMAP
| SFL_EXTENTS
;
2013 if ((fileID
== kHFSCatalogFileID
) || (extent_info
->is_dirlink
== true)) {
2014 lockflags
|= SFL_CATALOG
;
2015 } else if (fileID
== kHFSAttributesFileID
) {
2016 lockflags
|= SFL_ATTRIBUTE
;
2017 } else if (fileID
== kHFSStartupFileID
) {
2018 lockflags
|= SFL_STARTUP
;
2020 extent_info
->lockflags
= lockflags
;
2021 extent_info
->fcb
= VTOF(hfsmp
->hfs_extents_vp
);
2023 /* Flush data associated with current file on disk.
2025 * If the current vnode is directory hard link, no flushing of
2026 * journal or vnode is required. The current kernel does not
2027 * modify data/resource fork of directory hard links, so nothing
2028 * will be in the cache. If a directory hard link is newly created,
2029 * the resource fork data is written directly using devvp and
2030 * the code that actually relocates data (hfs_copy_extent()) also
2031 * uses devvp for its I/O --- so they will see a consistent copy.
2033 if (extent_info
->is_sysfile
) {
2034 /* If the current vnode is system vnode, flush journal
2035 * to make sure that all data is written to the disk.
2037 error
= hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
2039 printf ("hfs_reclaim_file: journal_flush returned %d\n", error
);
2042 } else if (extent_info
->is_dirlink
== false) {
2043 /* Flush all blocks associated with this regular file vnode.
2044 * Normally there should not be buffer cache blocks for regular
2045 * files, but for objects like symlinks, we can have buffer cache
2046 * blocks associated with the vnode. Therefore we call
2047 * buf_flushdirtyblks() also.
2049 buf_flushdirtyblks(vp
, 0, BUF_SKIP_LOCKED
, "hfs_reclaim_file");
2052 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2053 took_truncate_lock
= true;
2054 (void) cluster_push(vp
, 0);
2055 error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2060 /* If the file no longer exists, nothing left to do */
2061 if (cp
->c_flag
& C_NOEXISTS
) {
2066 /* Wait for any in-progress writes to this vnode to complete, so that we'll
2067 * be copying consistent bits. (Otherwise, it's possible that an async
2068 * write will complete to the old extent after we read from it. That
2069 * could lead to corruption.)
2071 error
= vnode_waitforwrites(vp
, 0, 0, 0, "hfs_reclaim_file");
2077 if (hfs_resize_debug
) {
2078 printf("hfs_reclaim_file: === Start reclaiming %sfork for %sid=%u ===\n", (forktype
? "rsrc" : "data"), (extent_info
->is_dirlink
? "dirlink" : "file"), fileID
);
2081 if (extent_info
->is_dirlink
) {
2082 MALLOC(extent_info
->dirlink_desc
, struct cat_desc
*,
2083 sizeof(struct cat_desc
), M_TEMP
, M_WAITOK
);
2084 MALLOC(extent_info
->dirlink_attr
, struct cat_attr
*,
2085 sizeof(struct cat_attr
), M_TEMP
, M_WAITOK
);
2086 MALLOC(extent_info
->dirlink_fork
, struct filefork
*,
2087 sizeof(struct filefork
), M_TEMP
, M_WAITOK
);
2088 if ((extent_info
->dirlink_desc
== NULL
) ||
2089 (extent_info
->dirlink_attr
== NULL
) ||
2090 (extent_info
->dirlink_fork
== NULL
)) {
2095 /* Lookup catalog record for directory hard link and
2096 * create a fake filefork for the value looked up from
2099 fp
= extent_info
->dirlink_fork
;
2100 bzero(extent_info
->dirlink_fork
, sizeof(struct filefork
));
2101 extent_info
->dirlink_fork
->ff_cp
= cp
;
2102 lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
2103 error
= cat_lookup_dirlink(hfsmp
, fileID
, forktype
,
2104 extent_info
->dirlink_desc
, extent_info
->dirlink_attr
,
2105 &(extent_info
->dirlink_fork
->ff_data
));
2106 hfs_systemfile_unlock(hfsmp
, lockflags
);
2108 printf ("hfs_reclaim_file: cat_lookup_dirlink for fileID=%u returned error=%u\n", fileID
, error
);
2111 release_desc
= true;
2116 extent_info
->catalog_fp
= fp
;
2117 extent_info
->recStartBlock
= 0;
2118 extent_info
->extents
= extent_info
->catalog_fp
->ff_extents
;
2119 /* Relocate extents from the catalog record */
2120 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
2121 if (fp
->ff_extents
[i
].blockCount
== 0) {
2124 extent_info
->extent_index
= i
;
2125 error
= hfs_reclaim_extent(hfsmp
, allocLimit
, extent_info
, context
);
2127 printf ("hfs_reclaim_file: fileID=%u #%d %u:(%u,%u) hfs_reclaim_extent error=%d\n", fileID
, extent_info
->overflow_count
, i
, fp
->ff_extents
[i
].startBlock
, fp
->ff_extents
[i
].blockCount
, error
);
2132 /* If the number of allocation blocks processed for reclaiming
2133 * are less than total number of blocks for the file, continuing
2134 * working on overflow extents record.
2136 if (fp
->ff_blocks
<= extent_info
->cur_blockCount
) {
2137 if (0 && hfs_resize_debug
) {
2138 printf ("hfs_reclaim_file: Nothing more to relocate, offset=%d, ff_blocks=%u, cur_blockCount=%u\n", i
, fp
->ff_blocks
, extent_info
->cur_blockCount
);
2143 if (hfs_resize_debug
) {
2144 printf ("hfs_reclaim_file: Will check overflow records, offset=%d, ff_blocks=%u, cur_blockCount=%u\n", i
, fp
->ff_blocks
, extent_info
->cur_blockCount
);
2147 MALLOC(extent_info
->iterator
, struct BTreeIterator
*, sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
2148 if (extent_info
->iterator
== NULL
) {
2152 bzero(extent_info
->iterator
, sizeof(struct BTreeIterator
));
2153 key
= (HFSPlusExtentKey
*) &(extent_info
->iterator
->key
);
2154 key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
2155 key
->forkType
= forktype
;
2156 key
->fileID
= fileID
;
2157 key
->startBlock
= extent_info
->cur_blockCount
;
2159 extent_info
->btdata
.bufferAddress
= extent_info
->record
.overflow
;
2160 extent_info
->btdata
.itemSize
= sizeof(HFSPlusExtentRecord
);
2161 extent_info
->btdata
.itemCount
= 1;
2163 extent_info
->catalog_fp
= NULL
;
2165 /* Search the first overflow extent with expected startBlock as 'cur_blockCount' */
2166 lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
2167 error
= BTSearchRecord(extent_info
->fcb
, extent_info
->iterator
,
2168 &(extent_info
->btdata
), &(extent_info
->recordlen
),
2169 extent_info
->iterator
);
2170 hfs_systemfile_unlock(hfsmp
, lockflags
);
2171 while (error
== 0) {
2172 extent_info
->overflow_count
++;
2173 extent_info
->recStartBlock
= key
->startBlock
;
2174 extent_info
->extents
= extent_info
->record
.overflow
;
2175 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2176 if (extent_info
->record
.overflow
[i
].blockCount
== 0) {
2179 extent_info
->extent_index
= i
;
2180 error
= hfs_reclaim_extent(hfsmp
, allocLimit
, extent_info
, context
);
2182 printf ("hfs_reclaim_file: fileID=%u #%d %u:(%u,%u) hfs_reclaim_extent error=%d\n", fileID
, extent_info
->overflow_count
, i
, extent_info
->record
.overflow
[i
].startBlock
, extent_info
->record
.overflow
[i
].blockCount
, error
);
2187 /* Look for more overflow records */
2188 lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
2189 error
= BTIterateRecord(extent_info
->fcb
, kBTreeNextRecord
,
2190 extent_info
->iterator
, &(extent_info
->btdata
),
2191 &(extent_info
->recordlen
));
2192 hfs_systemfile_unlock(hfsmp
, lockflags
);
2196 /* Stop when we encounter a different file or fork. */
2197 if ((key
->fileID
!= fileID
) || (key
->forkType
!= forktype
)) {
2201 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
2206 /* If any blocks were relocated, account them and report progress */
2207 if (extent_info
->blocks_relocated
) {
2208 hfsmp
->hfs_resize_blocksmoved
+= extent_info
->blocks_relocated
;
2209 hfs_truncatefs_progress(hfsmp
);
2210 if (fileID
< kHFSFirstUserCatalogNodeID
) {
2211 printf ("hfs_reclaim_file: Relocated %u blocks from fileID=%u on \"%s\"\n",
2212 extent_info
->blocks_relocated
, fileID
, hfsmp
->vcbVN
);
2215 if (extent_info
->iterator
) {
2216 FREE(extent_info
->iterator
, M_TEMP
);
2218 if (release_desc
== true) {
2219 cat_releasedesc(extent_info
->dirlink_desc
);
2221 if (extent_info
->dirlink_desc
) {
2222 FREE(extent_info
->dirlink_desc
, M_TEMP
);
2224 if (extent_info
->dirlink_attr
) {
2225 FREE(extent_info
->dirlink_attr
, M_TEMP
);
2227 if (extent_info
->dirlink_fork
) {
2228 FREE(extent_info
->dirlink_fork
, M_TEMP
);
2230 if ((extent_info
->blocks_relocated
!= 0) && (extent_info
->is_sysfile
== false)) {
2233 if (took_truncate_lock
) {
2234 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2237 FREE(extent_info
, M_TEMP
);
2239 if (hfs_resize_debug
) {
2240 printf("hfs_reclaim_file: === Finished relocating %sfork for fileid=%u (error=%d) ===\n", (forktype
? "rsrc" : "data"), fileID
, error
);
2248 * This journal_relocate callback updates the journal info block to point
2249 * at the new journal location. This write must NOT be done using the
2250 * transaction. We must write the block immediately. We must also force
2251 * it to get to the media so that the new journal location will be seen by
2252 * the replay code before we can safely let journaled blocks be written
2253 * to their normal locations.
2255 * The tests for journal_uses_fua below are mildly hacky. Since the journal
2256 * and the file system are both on the same device, I'm leveraging what
2257 * the journal has decided about FUA.
2259 struct hfs_journal_relocate_args
{
2260 struct hfsmount
*hfsmp
;
2261 vfs_context_t context
;
2262 u_int32_t newStartBlock
;
2263 u_int32_t newBlockCount
;
2267 hfs_journal_relocate_callback(void *_args
)
2270 struct hfs_journal_relocate_args
*args
= _args
;
2271 struct hfsmount
*hfsmp
= args
->hfsmp
;
2273 JournalInfoBlock
*jibp
;
2275 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
2276 (uint64_t)hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
2277 hfsmp
->blockSize
, vfs_context_ucred(args
->context
), &bp
);
2279 printf("hfs_journal_relocate_callback: failed to read JIB (%d)\n", error
);
2285 jibp
= (JournalInfoBlock
*) buf_dataptr(bp
);
2286 jibp
->offset
= SWAP_BE64((u_int64_t
)args
->newStartBlock
* hfsmp
->blockSize
);
2287 jibp
->size
= SWAP_BE64((u_int64_t
)args
->newBlockCount
* hfsmp
->blockSize
);
2288 if (journal_uses_fua(hfsmp
->jnl
))
2290 error
= buf_bwrite(bp
);
2292 printf("hfs_journal_relocate_callback: failed to write JIB (%d)\n", error
);
2295 if (!journal_uses_fua(hfsmp
->jnl
)) {
2296 error
= hfs_flush(hfsmp
, HFS_FLUSH_CACHE
);
2298 printf("hfs_journal_relocate_callback: hfs_flush failed (%d)\n", error
);
2299 error
= 0; /* Don't fail the operation. */
2307 /* Type of resize operation in progress */
2308 #define HFS_RESIZE_TRUNCATE 1
2309 #define HFS_RESIZE_EXTEND 2
2312 * Core function to relocate the journal file. This function takes the
2313 * journal size of the newly relocated journal --- the caller can
2314 * provide a new journal size if they want to change the size of
2315 * the journal. The function takes care of updating the journal info
2316 * block and all other data structures correctly.
2318 * Note: This function starts a transaction and grabs the btree locks.
2321 hfs_relocate_journal_file(struct hfsmount
*hfsmp
, u_int32_t jnl_size
, int resize_type
, vfs_context_t context
)
2326 u_int32_t oldStartBlock
;
2327 u_int32_t newStartBlock
;
2328 u_int32_t oldBlockCount
;
2329 u_int32_t newBlockCount
;
2330 u_int32_t jnlBlockCount
;
2331 u_int32_t alloc_skipfreeblks
;
2332 struct cat_desc journal_desc
;
2333 struct cat_attr journal_attr
;
2334 struct cat_fork journal_fork
;
2335 struct hfs_journal_relocate_args callback_args
;
2337 /* Calculate the number of allocation blocks required for the journal */
2338 jnlBlockCount
= howmany(jnl_size
, hfsmp
->blockSize
);
2341 * During truncatefs(), the volume free block count is updated
2342 * before relocating data and reflects the total number of free
2343 * blocks that will exist on volume after the resize is successful.
2344 * This means that the allocation blocks required for relocation
2345 * have already been reserved and accounted for in the free block
2346 * count. Therefore, block allocation and deallocation routines
2347 * can skip the free block check by passing HFS_ALLOC_SKIPFREEBLKS
2350 * This special handling is not required when the file system
2351 * is being extended as we want all the allocated and deallocated
2352 * blocks to be accounted for correctly.
2354 if (resize_type
== HFS_RESIZE_TRUNCATE
) {
2355 alloc_skipfreeblks
= HFS_ALLOC_SKIPFREEBLKS
;
2357 alloc_skipfreeblks
= 0;
2360 error
= hfs_start_transaction(hfsmp
);
2362 printf("hfs_relocate_journal_file: hfs_start_transaction returned %d\n", error
);
2365 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2367 error
= BlockAllocate(hfsmp
, 1, jnlBlockCount
, jnlBlockCount
,
2368 HFS_ALLOC_METAZONE
| HFS_ALLOC_FORCECONTIG
| HFS_ALLOC_FLUSHTXN
| alloc_skipfreeblks
,
2369 &newStartBlock
, &newBlockCount
);
2371 printf("hfs_relocate_journal_file: BlockAllocate returned %d\n", error
);
2374 if (newBlockCount
!= jnlBlockCount
) {
2375 printf("hfs_relocate_journal_file: newBlockCount != jnlBlockCount (%u, %u)\n", newBlockCount
, jnlBlockCount
);
2379 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlfileid
, 1, 0, &journal_desc
, &journal_attr
, &journal_fork
);
2381 printf("hfs_relocate_journal_file: cat_idlookup returned %d\n", error
);
2385 oldStartBlock
= journal_fork
.cf_extents
[0].startBlock
;
2386 oldBlockCount
= journal_fork
.cf_extents
[0].blockCount
;
2387 error
= BlockDeallocate(hfsmp
, oldStartBlock
, oldBlockCount
, alloc_skipfreeblks
);
2389 printf("hfs_relocate_journal_file: BlockDeallocate returned %d\n", error
);
2393 /* Update the catalog record for .journal */
2394 journal_fork
.cf_size
= hfs_blk_to_bytes(newBlockCount
, hfsmp
->blockSize
);
2395 journal_fork
.cf_extents
[0].startBlock
= newStartBlock
;
2396 journal_fork
.cf_extents
[0].blockCount
= newBlockCount
;
2397 journal_fork
.cf_blocks
= newBlockCount
;
2398 error
= cat_update(hfsmp
, &journal_desc
, &journal_attr
, &journal_fork
, NULL
);
2399 cat_releasedesc(&journal_desc
); /* all done with cat descriptor */
2401 printf("hfs_relocate_journal_file: cat_update returned %d\n", error
);
2406 * If the journal is part of the file system, then tell the journal
2407 * code about the new location. If the journal is on an external
2408 * device, then just keep using it as-is.
2410 if (hfsmp
->jvp
== hfsmp
->hfs_devvp
) {
2411 callback_args
.hfsmp
= hfsmp
;
2412 callback_args
.context
= context
;
2413 callback_args
.newStartBlock
= newStartBlock
;
2414 callback_args
.newBlockCount
= newBlockCount
;
2416 error
= journal_relocate(hfsmp
->jnl
, (off_t
)newStartBlock
*hfsmp
->blockSize
,
2417 (off_t
)newBlockCount
*hfsmp
->blockSize
, 0,
2418 hfs_journal_relocate_callback
, &callback_args
);
2420 /* NOTE: journal_relocate will mark the journal invalid. */
2421 printf("hfs_relocate_journal_file: journal_relocate returned %d\n", error
);
2424 if (hfs_resize_debug
) {
2425 printf ("hfs_relocate_journal_file: Successfully relocated journal from (%u,%u) to (%u,%u)\n", oldStartBlock
, oldBlockCount
, newStartBlock
, newBlockCount
);
2427 hfsmp
->jnl_start
= newStartBlock
;
2428 hfsmp
->jnl_size
= (off_t
)newBlockCount
* hfsmp
->blockSize
;
2431 hfs_systemfile_unlock(hfsmp
, lockflags
);
2432 error
= hfs_end_transaction(hfsmp
);
2434 printf("hfs_relocate_journal_file: hfs_end_transaction returned %d\n", error
);
2440 journal_err
= BlockDeallocate(hfsmp
, newStartBlock
, newBlockCount
, HFS_ALLOC_SKIPFREEBLKS
);
2442 printf("hfs_relocate_journal_file: BlockDeallocate returned %d\n", error
);
2443 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
2446 hfs_systemfile_unlock(hfsmp
, lockflags
);
2447 (void) hfs_end_transaction(hfsmp
);
2448 if (hfs_resize_debug
) {
2449 printf ("hfs_relocate_journal_file: Error relocating journal file (error=%d)\n", error
);
2456 * Relocate the journal file when the file system is being truncated.
2457 * We do not down-size the journal when the file system size is
2458 * reduced, so we always provide the current journal size to the
2462 hfs_reclaim_journal_file(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, vfs_context_t context
)
2465 u_int32_t startBlock
;
2466 u_int32_t blockCount
= hfsmp
->jnl_size
/ hfsmp
->blockSize
;
2469 * Figure out the location of the .journal file. When the journal
2470 * is on an external device, we need to look up the .journal file.
2472 if (hfsmp
->jvp
== hfsmp
->hfs_devvp
) {
2473 startBlock
= hfsmp
->jnl_start
;
2474 blockCount
= hfsmp
->jnl_size
/ hfsmp
->blockSize
;
2477 u_int32_t old_jnlfileid
;
2478 struct cat_attr attr
;
2479 struct cat_fork fork
;
2482 * The cat_lookup inside GetFileInfo will fail because hfs_jnlfileid
2483 * is set, and it is trying to hide the .journal file. So temporarily
2484 * unset the field while calling GetFileInfo.
2486 old_jnlfileid
= hfsmp
->hfs_jnlfileid
;
2487 hfsmp
->hfs_jnlfileid
= 0;
2488 fileid
= GetFileInfo(hfsmp
, kHFSRootFolderID
, ".journal", &attr
, &fork
);
2489 hfsmp
->hfs_jnlfileid
= old_jnlfileid
;
2490 if (fileid
!= old_jnlfileid
) {
2491 printf("hfs_reclaim_journal_file: cannot find .journal file!\n");
2495 startBlock
= fork
.cf_extents
[0].startBlock
;
2496 blockCount
= fork
.cf_extents
[0].blockCount
;
2499 if (startBlock
+ blockCount
<= allocLimit
) {
2500 /* The journal file does not require relocation */
2504 error
= hfs_relocate_journal_file(hfsmp
, hfs_blk_to_bytes(blockCount
, hfsmp
->blockSize
),
2505 HFS_RESIZE_TRUNCATE
, context
);
2507 hfsmp
->hfs_resize_blocksmoved
+= blockCount
;
2508 hfs_truncatefs_progress(hfsmp
);
2509 printf ("hfs_reclaim_journal_file: Relocated %u blocks from journal on \"%s\"\n",
2510 blockCount
, hfsmp
->vcbVN
);
2518 * Move the journal info block to a new location. We have to make sure the
2519 * new copy of the journal info block gets to the media first, then change
2520 * the field in the volume header and the catalog record.
2523 hfs_reclaim_journal_info_block(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, vfs_context_t context
)
2530 u_int32_t blockCount
;
2531 struct cat_desc jib_desc
;
2532 struct cat_attr jib_attr
;
2533 struct cat_fork jib_fork
;
2534 buf_t old_bp
, new_bp
;
2536 if (hfsmp
->vcbJinfoBlock
<= allocLimit
) {
2537 /* The journal info block does not require relocation */
2541 error
= hfs_start_transaction(hfsmp
);
2543 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error
);
2546 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2548 error
= BlockAllocate(hfsmp
, 1, 1, 1,
2549 HFS_ALLOC_METAZONE
| HFS_ALLOC_FORCECONTIG
| HFS_ALLOC_SKIPFREEBLKS
| HFS_ALLOC_FLUSHTXN
,
2550 &newBlock
, &blockCount
);
2552 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error
);
2555 if (blockCount
!= 1) {
2556 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount
);
2560 /* Copy the old journal info block content to the new location */
2561 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
2562 (uint64_t)hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
2563 hfsmp
->blockSize
, vfs_context_ucred(context
), &old_bp
);
2565 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error
);
2571 new_bp
= buf_getblk(hfsmp
->hfs_devvp
,
2572 (uint64_t)newBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
2573 hfsmp
->blockSize
, 0, 0, BLK_META
);
2574 bcopy((char*)buf_dataptr(old_bp
), (char*)buf_dataptr(new_bp
), hfsmp
->blockSize
);
2576 if (journal_uses_fua(hfsmp
->jnl
))
2577 buf_markfua(new_bp
);
2578 error
= buf_bwrite(new_bp
);
2580 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error
);
2583 if (!journal_uses_fua(hfsmp
->jnl
)) {
2584 error
= hfs_flush(hfsmp
, HFS_FLUSH_CACHE
);
2586 printf("hfs_reclaim_journal_info_block: hfs_flush failed (%d)\n", error
);
2587 /* Don't fail the operation. */
2591 /* Deallocate the old block once the new one has the new valid content */
2592 error
= BlockDeallocate(hfsmp
, hfsmp
->vcbJinfoBlock
, 1, HFS_ALLOC_SKIPFREEBLKS
);
2594 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error
);
2599 /* Update the catalog record for .journal_info_block */
2600 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlinfoblkid
, 1, 0, &jib_desc
, &jib_attr
, &jib_fork
);
2602 printf("hfs_reclaim_journal_info_block: cat_idlookup returned %d\n", error
);
2605 oldBlock
= jib_fork
.cf_extents
[0].startBlock
;
2606 jib_fork
.cf_size
= hfsmp
->blockSize
;
2607 jib_fork
.cf_extents
[0].startBlock
= newBlock
;
2608 jib_fork
.cf_extents
[0].blockCount
= 1;
2609 jib_fork
.cf_blocks
= 1;
2610 error
= cat_update(hfsmp
, &jib_desc
, &jib_attr
, &jib_fork
, NULL
);
2611 cat_releasedesc(&jib_desc
); /* all done with cat descriptor */
2613 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error
);
2617 /* Update the pointer to the journal info block in the volume header. */
2618 hfsmp
->vcbJinfoBlock
= newBlock
;
2619 error
= hfs_flushvolumeheader(hfsmp
, HFS_FVH_WAIT
| HFS_FVH_WRITE_ALT
);
2621 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error
);
2624 hfs_systemfile_unlock(hfsmp
, lockflags
);
2625 error
= hfs_end_transaction(hfsmp
);
2627 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error
);
2629 error
= hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL
);
2631 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error
);
2634 /* Account for the block relocated and print progress */
2635 hfsmp
->hfs_resize_blocksmoved
+= 1;
2636 hfs_truncatefs_progress(hfsmp
);
2638 printf ("hfs_reclaim_journal_info: Relocated 1 block from journal info on \"%s\"\n",
2640 if (hfs_resize_debug
) {
2641 printf ("hfs_reclaim_journal_info_block: Successfully relocated journal info block from (%u,%u) to (%u,%u)\n", oldBlock
, blockCount
, newBlock
, blockCount
);
2647 journal_err
= BlockDeallocate(hfsmp
, newBlock
, blockCount
, HFS_ALLOC_SKIPFREEBLKS
);
2649 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error
);
2650 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
2654 hfs_systemfile_unlock(hfsmp
, lockflags
);
2655 (void) hfs_end_transaction(hfsmp
);
2656 if (hfs_resize_debug
) {
2657 printf ("hfs_reclaim_journal_info_block: Error relocating journal info block (error=%d)\n", error
);
2664 calculate_journal_size(struct hfsmount
*hfsmp
, u_int32_t sector_size
, u_int64_t sector_count
)
2666 u_int64_t journal_size
;
2667 u_int32_t journal_scale
;
2669 #define DEFAULT_JOURNAL_SIZE (8*1024*1024)
2670 #define MAX_JOURNAL_SIZE (512*1024*1024)
2672 /* Calculate the journal size for this volume. We want
2673 * at least 8 MB of journal for each 100 GB of disk space.
2674 * We cap the size at 512 MB, unless the allocation block
2675 * size is larger, in which case, we use one allocation
2678 journal_scale
= (sector_size
* sector_count
) / ((u_int64_t
)100 * 1024 * 1024 * 1024);
2679 journal_size
= DEFAULT_JOURNAL_SIZE
* (journal_scale
+ 1);
2680 if (journal_size
> MAX_JOURNAL_SIZE
) {
2681 journal_size
= MAX_JOURNAL_SIZE
;
2683 if (journal_size
< hfsmp
->blockSize
) {
2684 journal_size
= hfsmp
->blockSize
;
2686 return journal_size
;
2691 * Calculate the expected journal size based on current partition size.
2692 * If the size of the current journal is less than the calculated size,
2693 * force journal relocation with the new journal size.
2696 hfs_extend_journal(struct hfsmount
*hfsmp
, u_int32_t sector_size
, u_int64_t sector_count
, vfs_context_t context
)
2699 u_int64_t calc_journal_size
;
2701 if (hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2702 if (hfs_resize_debug
) {
2703 printf("hfs_extend_journal: not resizing the journal because it is on an external device.\n");
2708 calc_journal_size
= calculate_journal_size(hfsmp
, sector_size
, sector_count
);
2709 if (calc_journal_size
<= hfsmp
->jnl_size
) {
2710 /* The journal size requires no modification */
2714 if (hfs_resize_debug
) {
2715 printf ("hfs_extend_journal: journal old=%u, new=%qd\n", hfsmp
->jnl_size
, calc_journal_size
);
2718 /* Extend the journal to the new calculated size */
2719 error
= hfs_relocate_journal_file(hfsmp
, calc_journal_size
, HFS_RESIZE_EXTEND
, context
);
2721 printf ("hfs_extend_journal: Extended journal size to %u bytes on \"%s\"\n",
2722 hfsmp
->jnl_size
, hfsmp
->vcbVN
);
2730 * This function traverses through all extended attribute records for a given
2731 * fileID, and calls function that reclaims data blocks that exist in the
2732 * area of the disk being reclaimed which in turn is responsible for allocating
2733 * new space, copying extent data, deallocating new space, and if required,
2734 * splitting the extent.
2736 * Note: The caller has already acquired the cnode lock on the file. Therefore
2737 * we are assured that no other thread would be creating/deleting/modifying
2738 * extended attributes for this file.
2741 * hfsmp->hfs_resize_blocksmoved is incremented by the number of allocation
2742 * blocks that were relocated.
2745 * 0 on success, non-zero on failure.
2748 hfs_reclaim_xattr(struct hfsmount
*hfsmp
, struct vnode
*vp
, u_int32_t fileID
, u_int32_t allocLimit
, vfs_context_t context
)
2751 struct hfs_reclaim_extent_info
*extent_info
;
2753 HFSPlusAttrKey
*key
;
2756 if (hfs_resize_debug
) {
2757 printf("hfs_reclaim_xattr: === Start reclaiming xattr for id=%u ===\n", fileID
);
2760 MALLOC(extent_info
, struct hfs_reclaim_extent_info
*,
2761 sizeof(struct hfs_reclaim_extent_info
), M_TEMP
, M_WAITOK
);
2762 if (extent_info
== NULL
) {
2765 bzero(extent_info
, sizeof(struct hfs_reclaim_extent_info
));
2766 extent_info
->vp
= vp
;
2767 extent_info
->fileID
= fileID
;
2768 extent_info
->is_xattr
= true;
2769 extent_info
->is_sysfile
= vnode_issystem(vp
);
2770 extent_info
->fcb
= VTOF(hfsmp
->hfs_attribute_vp
);
2771 lockflags
= &(extent_info
->lockflags
);
2772 *lockflags
= SFL_ATTRIBUTE
| SFL_BITMAP
;
2774 /* Initialize iterator from the extent_info structure */
2775 MALLOC(extent_info
->iterator
, struct BTreeIterator
*,
2776 sizeof(struct BTreeIterator
), M_TEMP
, M_WAITOK
);
2777 if (extent_info
->iterator
== NULL
) {
2781 bzero(extent_info
->iterator
, sizeof(struct BTreeIterator
));
2783 /* Build attribute key */
2784 key
= (HFSPlusAttrKey
*)&(extent_info
->iterator
->key
);
2785 error
= hfs_buildattrkey(fileID
, NULL
, key
);
2790 /* Initialize btdata from extent_info structure. Note that the
2791 * buffer pointer actually points to the xattr record from the
2792 * extent_info structure itself.
2794 extent_info
->btdata
.bufferAddress
= &(extent_info
->record
.xattr
);
2795 extent_info
->btdata
.itemSize
= sizeof(HFSPlusAttrRecord
);
2796 extent_info
->btdata
.itemCount
= 1;
2799 * Sync all extent-based attribute data to the disk.
2801 * All extent-based attribute data I/O is performed via cluster
2802 * I/O using a virtual file that spans across entire file system
2805 hfs_lock_truncate(VTOC(hfsmp
->hfs_attrdata_vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2806 (void)cluster_push(hfsmp
->hfs_attrdata_vp
, 0);
2807 error
= vnode_waitforwrites(hfsmp
->hfs_attrdata_vp
, 0, 0, 0, "hfs_reclaim_xattr");
2808 hfs_unlock_truncate(VTOC(hfsmp
->hfs_attrdata_vp
), HFS_LOCK_DEFAULT
);
2813 /* Search for extended attribute for current file. This
2814 * will place the iterator before the first matching record.
2816 *lockflags
= hfs_systemfile_lock(hfsmp
, *lockflags
, HFS_EXCLUSIVE_LOCK
);
2817 error
= BTSearchRecord(extent_info
->fcb
, extent_info
->iterator
,
2818 &(extent_info
->btdata
), &(extent_info
->recordlen
),
2819 extent_info
->iterator
);
2820 hfs_systemfile_unlock(hfsmp
, *lockflags
);
2822 if (error
!= btNotFound
) {
2825 /* btNotFound is expected here, so just mask it */
2830 /* Iterate to the next record */
2831 *lockflags
= hfs_systemfile_lock(hfsmp
, *lockflags
, HFS_EXCLUSIVE_LOCK
);
2832 error
= BTIterateRecord(extent_info
->fcb
, kBTreeNextRecord
,
2833 extent_info
->iterator
, &(extent_info
->btdata
),
2834 &(extent_info
->recordlen
));
2835 hfs_systemfile_unlock(hfsmp
, *lockflags
);
2837 /* Stop the iteration if we encounter end of btree or xattr with different fileID */
2838 if (error
|| key
->fileID
!= fileID
) {
2839 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
2845 /* We only care about extent-based EAs */
2846 if ((extent_info
->record
.xattr
.recordType
!= kHFSPlusAttrForkData
) &&
2847 (extent_info
->record
.xattr
.recordType
!= kHFSPlusAttrExtents
)) {
2851 if (extent_info
->record
.xattr
.recordType
== kHFSPlusAttrForkData
) {
2852 extent_info
->overflow_count
= 0;
2853 extent_info
->extents
= extent_info
->record
.xattr
.forkData
.theFork
.extents
;
2854 } else if (extent_info
->record
.xattr
.recordType
== kHFSPlusAttrExtents
) {
2855 extent_info
->overflow_count
++;
2856 extent_info
->extents
= extent_info
->record
.xattr
.overflowExtents
.extents
;
2859 extent_info
->recStartBlock
= key
->startBlock
;
2860 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2861 if (extent_info
->extents
[i
].blockCount
== 0) {
2864 extent_info
->extent_index
= i
;
2865 error
= hfs_reclaim_extent(hfsmp
, allocLimit
, extent_info
, context
);
2867 printf ("hfs_reclaim_xattr: fileID=%u hfs_reclaim_extent error=%d\n", fileID
, error
);
2874 /* If any blocks were relocated, account them and report progress */
2875 if (extent_info
->blocks_relocated
) {
2876 hfsmp
->hfs_resize_blocksmoved
+= extent_info
->blocks_relocated
;
2877 hfs_truncatefs_progress(hfsmp
);
2879 if (extent_info
->iterator
) {
2880 FREE(extent_info
->iterator
, M_TEMP
);
2883 FREE(extent_info
, M_TEMP
);
2885 if (hfs_resize_debug
) {
2886 printf("hfs_reclaim_xattr: === Finished relocating xattr for fileid=%u (error=%d) ===\n", fileID
, error
);
2892 * Reclaim any extent-based extended attributes allocation blocks from
2893 * the area of the disk that is being truncated.
2895 * The function traverses the attribute btree to find out the fileIDs
2896 * of the extended attributes that need to be relocated. For every
2897 * file whose large EA requires relocation, it looks up the cnode and
2898 * calls hfs_reclaim_xattr() to do all the work for allocating
2899 * new space, copying data, deallocating old space, and if required,
2900 * splitting the extents.
2903 * allocLimit - starting block of the area being reclaimed
2906 * returns 0 on success, non-zero on failure.
2909 hfs_reclaim_xattrspace(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, vfs_context_t context
)
2913 struct BTreeIterator
*iterator
= NULL
;
2914 struct FSBufferDescriptor btdata
;
2915 HFSPlusAttrKey
*key
;
2916 HFSPlusAttrRecord rec
;
2918 cnid_t prev_fileid
= 0;
2921 int btree_operation
;
2922 u_int32_t files_moved
= 0;
2923 u_int32_t prev_blocksmoved
;
2926 fcb
= VTOF(hfsmp
->hfs_attribute_vp
);
2927 /* Store the value to print total blocks moved by this function in end */
2928 prev_blocksmoved
= hfsmp
->hfs_resize_blocksmoved
;
2930 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
), VM_KERN_MEMORY_FILE
)) {
2933 bzero(iterator
, sizeof(*iterator
));
2934 key
= (HFSPlusAttrKey
*)&iterator
->key
;
2935 btdata
.bufferAddress
= &rec
;
2936 btdata
.itemSize
= sizeof(rec
);
2937 btdata
.itemCount
= 1;
2939 need_relocate
= false;
2940 btree_operation
= kBTreeFirstRecord
;
2941 /* Traverse the attribute btree to find extent-based EAs to reclaim */
2943 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
2944 error
= BTIterateRecord(fcb
, btree_operation
, iterator
, &btdata
, NULL
);
2945 hfs_systemfile_unlock(hfsmp
, lockflags
);
2947 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
2952 btree_operation
= kBTreeNextRecord
;
2954 /* If the extents of current fileID were already relocated, skip it */
2955 if (prev_fileid
== key
->fileID
) {
2959 /* Check if any of the extents in the current record need to be relocated */
2960 need_relocate
= false;
2961 switch(rec
.recordType
) {
2962 case kHFSPlusAttrForkData
:
2963 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2964 if (rec
.forkData
.theFork
.extents
[i
].blockCount
== 0) {
2967 if ((rec
.forkData
.theFork
.extents
[i
].startBlock
+
2968 rec
.forkData
.theFork
.extents
[i
].blockCount
) > allocLimit
) {
2969 need_relocate
= true;
2975 case kHFSPlusAttrExtents
:
2976 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2977 if (rec
.overflowExtents
.extents
[i
].blockCount
== 0) {
2980 if ((rec
.overflowExtents
.extents
[i
].startBlock
+
2981 rec
.overflowExtents
.extents
[i
].blockCount
) > allocLimit
) {
2982 need_relocate
= true;
2989 /* Continue iterating to next attribute record */
2990 if (need_relocate
== false) {
2994 /* Look up the vnode for corresponding file. The cnode
2995 * will be locked which will ensure that no one modifies
2996 * the xattrs when we are relocating them.
2998 * We want to allow open-unlinked files to be moved,
2999 * so provide allow_deleted == 1 for hfs_vget().
3001 if (hfs_vget(hfsmp
, key
->fileID
, &vp
, 0, 1) != 0) {
3005 error
= hfs_reclaim_xattr(hfsmp
, vp
, key
->fileID
, allocLimit
, context
);
3006 hfs_unlock(VTOC(vp
));
3009 printf ("hfs_reclaim_xattrspace: Error relocating xattrs for fileid=%u (error=%d)\n", key
->fileID
, error
);
3012 prev_fileid
= key
->fileID
;
3017 printf("hfs_reclaim_xattrspace: Relocated %u xattr blocks from %u files on \"%s\"\n",
3018 (hfsmp
->hfs_resize_blocksmoved
- prev_blocksmoved
),
3019 files_moved
, hfsmp
->vcbVN
);
3022 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3027 * Reclaim blocks from regular files.
3029 * This function iterates over all the record in catalog btree looking
3030 * for files with extents that overlap into the space we're trying to
3031 * free up. If a file extent requires relocation, it looks up the vnode
3032 * and calls function to relocate the data.
3035 * Zero on success, non-zero on failure.
3038 hfs_reclaim_filespace(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, vfs_context_t context
)
3042 struct BTreeIterator
*iterator
= NULL
;
3043 struct FSBufferDescriptor btdata
;
3044 int btree_operation
;
3046 struct HFSPlusCatalogFile filerec
;
3049 struct filefork
*datafork
;
3050 u_int32_t files_moved
= 0;
3051 u_int32_t prev_blocksmoved
;
3054 int keys_generated
= 0;
3057 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
3058 /* Store the value to print total blocks moved by this function at the end */
3059 prev_blocksmoved
= hfsmp
->hfs_resize_blocksmoved
;
3061 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
), VM_KERN_MEMORY_FILE
)) {
3063 goto reclaim_filespace_done
;
3068 * For content-protected filesystems, we may need to relocate files that
3069 * are encrypted. If they use the new-style offset-based IVs, then
3070 * we can move them regardless of the lock state. We create a temporary
3071 * key here that we use to read/write the data, then we discard it at the
3072 * end of the function.
3074 if (cp_fs_protected (hfsmp
->hfs_mp
)) {
3075 error
= cpx_gentempkeys(&hfsmp
->hfs_resize_cpx
, hfsmp
);
3081 printf("hfs_reclaimspace: Error generating temporary keys for resize (%d)\n", error
);
3082 goto reclaim_filespace_done
;
3088 bzero(iterator
, sizeof(*iterator
));
3090 btdata
.bufferAddress
= &filerec
;
3091 btdata
.itemSize
= sizeof(filerec
);
3092 btdata
.itemCount
= 1;
3094 btree_operation
= kBTreeFirstRecord
;
3096 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3097 error
= BTIterateRecord(fcb
, btree_operation
, iterator
, &btdata
, NULL
);
3098 hfs_systemfile_unlock(hfsmp
, lockflags
);
3100 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
3105 btree_operation
= kBTreeNextRecord
;
3107 if (filerec
.recordType
!= kHFSPlusFileRecord
) {
3111 /* Check if any of the extents require relocation */
3113 error
= hfs_file_extent_overlaps(hfsmp
, allocLimit
, &filerec
, &overlaps
);
3120 /* We want to allow open-unlinked files to be moved, so allow_deleted == 1 */
3121 if (hfs_vget(hfsmp
, filerec
.fileID
, &vp
, 0, 1) != 0) {
3122 if (hfs_resize_debug
) {
3123 printf("hfs_reclaim_filespace: hfs_vget(%u) failed.\n", filerec
.fileID
);
3128 /* If data fork exists or item is a directory hard link, relocate blocks */
3129 datafork
= VTOF(vp
);
3130 if ((datafork
&& datafork
->ff_blocks
> 0) || vnode_isdir(vp
)) {
3131 error
= hfs_reclaim_file(hfsmp
, vp
, filerec
.fileID
,
3132 kHFSDataForkType
, allocLimit
, context
);
3134 printf ("hfs_reclaimspace: Error reclaiming datafork blocks of fileid=%u (error=%d)\n", filerec
.fileID
, error
);
3135 hfs_unlock(VTOC(vp
));
3141 /* If resource fork exists or item is a directory hard link, relocate blocks */
3142 if (((VTOC(vp
)->c_blocks
- (datafork
? datafork
->ff_blocks
: 0)) > 0) || vnode_isdir(vp
)) {
3143 if (vnode_isdir(vp
)) {
3144 /* Resource fork vnode lookup is invalid for directory hard link.
3145 * So we fake data fork vnode as resource fork vnode.
3149 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
);
3151 printf ("hfs_reclaimspace: Error looking up rvp for fileid=%u (error=%d)\n", filerec
.fileID
, error
);
3152 hfs_unlock(VTOC(vp
));
3156 VTOC(rvp
)->c_flag
|= C_NEED_RVNODE_PUT
;
3159 error
= hfs_reclaim_file(hfsmp
, rvp
, filerec
.fileID
,
3160 kHFSResourceForkType
, allocLimit
, context
);
3162 printf ("hfs_reclaimspace: Error reclaiming rsrcfork blocks of fileid=%u (error=%d)\n", filerec
.fileID
, error
);
3163 hfs_unlock(VTOC(vp
));
3169 /* The file forks were relocated successfully, now drop the
3170 * cnode lock and vnode reference, and continue iterating to
3171 * next catalog record.
3173 hfs_unlock(VTOC(vp
));
3179 printf("hfs_reclaim_filespace: Relocated %u blocks from %u files on \"%s\"\n",
3180 (hfsmp
->hfs_resize_blocksmoved
- prev_blocksmoved
),
3181 files_moved
, hfsmp
->vcbVN
);
3184 reclaim_filespace_done
:
3186 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3190 if (keys_generated
) {
3191 cpx_free(hfsmp
->hfs_resize_cpx
);
3192 hfsmp
->hfs_resize_cpx
= NULL
;
3199 * Reclaim space at the end of a file system.
3202 * allocLimit - start block of the space being reclaimed
3203 * reclaimblks - number of allocation blocks to reclaim
3206 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_int32_t allocLimit
, u_int32_t reclaimblks
, vfs_context_t context
)
3211 * Preflight the bitmap to find out total number of blocks that need
3214 * Note: Since allocLimit is set to the location of new alternate volume
3215 * header, the check below does not account for blocks allocated for old
3216 * alternate volume header.
3218 error
= hfs_count_allocated(hfsmp
, allocLimit
, reclaimblks
, &(hfsmp
->hfs_resize_totalblocks
));
3220 printf ("hfs_reclaimspace: Unable to determine total blocks to reclaim error=%d\n", error
);
3223 if (hfs_resize_debug
) {
3224 printf ("hfs_reclaimspace: Total number of blocks to reclaim = %u\n", hfsmp
->hfs_resize_totalblocks
);
3227 /* Just to be safe, sync the content of the journal to the disk before we proceed */
3228 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
3230 /* First, relocate journal file blocks if they're in the way.
3231 * Doing this first will make sure that journal relocate code
3232 * gets access to contiguous blocks on disk first. The journal
3233 * file has to be contiguous on the disk, otherwise resize will
3236 error
= hfs_reclaim_journal_file(hfsmp
, allocLimit
, context
);
3238 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error
);
3242 /* Relocate journal info block blocks if they're in the way. */
3243 error
= hfs_reclaim_journal_info_block(hfsmp
, allocLimit
, context
);
3245 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error
);
3249 /* Relocate extents of the Extents B-tree if they're in the way.
3250 * Relocating extents btree before other btrees is important as
3251 * this will provide access to largest contiguous block range on
3252 * the disk for relocating extents btree. Note that extents btree
3253 * can only have maximum of 8 extents.
3255 error
= hfs_reclaim_file(hfsmp
, hfsmp
->hfs_extents_vp
, kHFSExtentsFileID
,
3256 kHFSDataForkType
, allocLimit
, context
);
3258 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error
);
3262 /* Relocate extents of the Allocation file if they're in the way. */
3263 error
= hfs_reclaim_file(hfsmp
, hfsmp
->hfs_allocation_vp
, kHFSAllocationFileID
,
3264 kHFSDataForkType
, allocLimit
, context
);
3266 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error
);
3270 /* Relocate extents of the Catalog B-tree if they're in the way. */
3271 error
= hfs_reclaim_file(hfsmp
, hfsmp
->hfs_catalog_vp
, kHFSCatalogFileID
,
3272 kHFSDataForkType
, allocLimit
, context
);
3274 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error
);
3278 /* Relocate extents of the Attributes B-tree if they're in the way. */
3279 error
= hfs_reclaim_file(hfsmp
, hfsmp
->hfs_attribute_vp
, kHFSAttributesFileID
,
3280 kHFSDataForkType
, allocLimit
, context
);
3282 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error
);
3286 /* Relocate extents of the Startup File if there is one and they're in the way. */
3287 error
= hfs_reclaim_file(hfsmp
, hfsmp
->hfs_startup_vp
, kHFSStartupFileID
,
3288 kHFSDataForkType
, allocLimit
, context
);
3290 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error
);
3295 * We need to make sure the alternate volume header gets flushed if we moved
3296 * any extents in the volume header. But we need to do that before
3297 * shrinking the size of the volume, or else the journal code will panic
3298 * with an invalid (too large) block number.
3300 * Note that blks_moved will be set if ANY extent was moved, even
3301 * if it was just an overflow extent. In this case, the journal_flush isn't
3302 * strictly required, but shouldn't hurt.
3304 if (hfsmp
->hfs_resize_blocksmoved
) {
3305 hfs_flush(hfsmp
, HFS_FLUSH_JOURNAL_META
);
3308 /* Reclaim extents from catalog file records */
3309 error
= hfs_reclaim_filespace(hfsmp
, allocLimit
, context
);
3311 printf ("hfs_reclaimspace: hfs_reclaim_filespace returned error=%d\n", error
);
3315 /* Reclaim extents from extent-based extended attributes, if any */
3316 error
= hfs_reclaim_xattrspace(hfsmp
, allocLimit
, context
);
3318 printf ("hfs_reclaimspace: hfs_reclaim_xattrspace returned error=%d\n", error
);
3323 * Make sure reserved ranges in the region we're to allocate don't
3326 struct rl_entry
*range
;
3328 int lockf
= hfs_systemfile_lock(hfsmp
, SFL_BITMAP
, HFS_SHARED_LOCK
);
3329 TAILQ_FOREACH(range
, &hfsmp
->hfs_reserved_ranges
[HFS_LOCKED_BLOCKS
], rl_link
) {
3330 if (rl_overlap(range
, hfsmp
->allocLimit
, RL_INFINITY
) != RL_NOOVERLAP
) {
3332 hfs_systemfile_unlock(hfsmp
, lockf
);
3333 msleep(hfs_reclaimspace
, NULL
, PINOD
, "waiting on reserved blocks",
3334 &(struct timespec
){ 0, 100 * 1000000 });
3338 hfs_systemfile_unlock(hfsmp
, lockf
);
3345 * Check if there are any extents (including overflow extents) that overlap
3346 * into the disk space that is being reclaimed.
3349 * true - One of the extents need to be relocated
3350 * false - No overflow extents need to be relocated, or there was an error
3353 hfs_file_extent_overlaps(struct hfsmount
*hfsmp
, u_int32_t allocLimit
,
3354 struct HFSPlusCatalogFile
*filerec
, bool *overlaps
)
3356 struct BTreeIterator
* iterator
= NULL
;
3357 struct FSBufferDescriptor btdata
;
3358 HFSPlusExtentRecord extrec
;
3359 HFSPlusExtentKey
*extkeyptr
;
3367 /* Check if data fork overlaps the target space */
3368 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3369 if (filerec
->dataFork
.extents
[i
].blockCount
== 0) {
3372 endblock
= filerec
->dataFork
.extents
[i
].startBlock
+
3373 filerec
->dataFork
.extents
[i
].blockCount
;
3374 if (endblock
> allocLimit
) {
3380 /* Check if resource fork overlaps the target space */
3381 for (j
= 0; j
< kHFSPlusExtentDensity
; ++j
) {
3382 if (filerec
->resourceFork
.extents
[j
].blockCount
== 0) {
3385 endblock
= filerec
->resourceFork
.extents
[j
].startBlock
+
3386 filerec
->resourceFork
.extents
[j
].blockCount
;
3387 if (endblock
> allocLimit
) {
3393 /* Return back if there are no overflow extents for this file */
3394 if ((i
< kHFSPlusExtentDensity
) && (j
< kHFSPlusExtentDensity
)) {
3399 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
3401 bzero(iterator
, sizeof(*iterator
));
3402 extkeyptr
= (HFSPlusExtentKey
*)&iterator
->key
;
3403 extkeyptr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
3404 extkeyptr
->forkType
= 0;
3405 extkeyptr
->fileID
= filerec
->fileID
;
3406 extkeyptr
->startBlock
= 0;
3408 btdata
.bufferAddress
= &extrec
;
3409 btdata
.itemSize
= sizeof(extrec
);
3410 btdata
.itemCount
= 1;
3412 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
3414 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
, HFS_SHARED_LOCK
);
3416 /* This will position the iterator just before the first overflow
3417 * extent record for given fileID. It will always return btNotFound,
3418 * so we special case the error code.
3420 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
3421 if (error
&& (error
!= btNotFound
)) {
3422 ret
= MacToVFSError(error
);
3426 /* BTIterateRecord() might return error if the btree is empty, and
3427 * therefore we return that the extent does not overflow to the caller
3429 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3430 while (error
== 0) {
3431 /* Stop when we encounter a different file. */
3432 if (extkeyptr
->fileID
!= filerec
->fileID
) {
3435 /* Check if any of the forks exist in the target space. */
3436 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3437 if (extrec
[i
].blockCount
== 0) {
3440 endblock
= extrec
[i
].startBlock
+ extrec
[i
].blockCount
;
3441 if (endblock
> allocLimit
) {
3446 /* Look for more records. */
3447 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3450 if (error
&& error
!= btNotFound
) {
3451 ret
= MacToVFSError(error
);
3459 hfs_systemfile_unlock(hfsmp
, lockflags
);
3462 FREE(iterator
, M_TEMP
);
3469 * Calculate the progress of a file system resize operation.
3473 hfs_resize_progress(struct hfsmount
*hfsmp
, u_int32_t
*progress
)
3475 if ((hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) == 0) {
3479 if (hfsmp
->hfs_resize_totalblocks
> 0) {
3480 *progress
= (u_int32_t
)((hfsmp
->hfs_resize_blocksmoved
* 100ULL) / hfsmp
->hfs_resize_totalblocks
);