]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_resize.c
xnu-3247.10.11.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_resize.c
1 /*
2 * Copyright (c) 2013-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <sys/systm.h>
29 #include <sys/kauth.h>
30 #include <sys/ubc.h>
31 #include <sys/vnode_internal.h>
32 #include <sys/mount_internal.h>
33
34 #include <sys/buf_internal.h>
35 #include <vfs/vfs_journal.h>
36 #include <miscfs/specfs/specdev.h>
37
38 #include "hfs.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"
44
45 /* Enable/disable debugging code for live volume resizing */
46 int hfs_resize_debug = 0;
47
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);
52
53 /*
54 * Extend a file system.
55 */
56 int
57 hfs_extendfs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context)
58 {
59 struct proc *p = vfs_context_proc(context);
60 kauth_cred_t cred = vfs_context_ucred(context);
61 struct vnode *vp;
62 struct vnode *devvp;
63 struct buf *bp;
64 struct filefork *fp = NULL;
65 ExtendedVCB *vcb;
66 struct cat_fork forkdata;
67 u_int64_t oldsize;
68 u_int64_t newblkcnt;
69 u_int64_t prev_phys_block_count;
70 u_int32_t addblks;
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;
76 daddr_t bitmapblks;
77 int lockflags = 0;
78 int error;
79 int64_t oldBitmapSize;
80
81 Boolean usedExtendFileC = false;
82 int transaction_begun = 0;
83
84 devvp = hfsmp->hfs_devvp;
85 vcb = HFSTOVCB(hfsmp);
86
87 /*
88 * - HFS Plus file systems only.
89 * - Journaling must be enabled.
90 * - No embedded volumes.
91 */
92 if ((vcb->vcbSigWord == kHFSSigWord) ||
93 (hfsmp->jnl == NULL) ||
94 (vcb->hfsPlusIOPosOffset != 0)) {
95 return (EPERM);
96 }
97 /*
98 * If extending file system by non-root, then verify
99 * ownership and check permissions.
100 */
101 if (suser(cred, NULL)) {
102 error = hfs_vget(hfsmp, kHFSRootFolderID, &vp, 0, 0);
103
104 if (error)
105 return (error);
106 error = hfs_owner_rights(hfsmp, VTOC(vp)->c_uid, cred, p, 0);
107 if (error == 0) {
108 error = hfs_write_access(vp, cred, p, false);
109 }
110 hfs_unlock(VTOC(vp));
111 vnode_put(vp);
112 if (error)
113 return (error);
114
115 error = vnode_authorize(devvp, NULL, KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, context);
116 if (error)
117 return (error);
118 }
119 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&sector_size, 0, context)) {
120 return (ENXIO);
121 }
122 if (sector_size != hfsmp->hfs_logical_block_size) {
123 return (ENXIO);
124 }
125 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&sector_count, 0, context)) {
126 return (ENXIO);
127 }
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);
131 return (ENOSPC);
132 }
133 error = VNOP_IOCTL(devvp, DKIOCGETPHYSICALBLOCKSIZE, (caddr_t)&phys_sector_size, 0, context);
134 if (error) {
135 if ((error != ENOTSUP) && (error != ENOTTY)) {
136 return (ENXIO);
137 }
138 /* If ioctl is not supported, force physical and logical sector size to be same */
139 phys_sector_size = sector_size;
140 }
141 oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize;
142
143 /*
144 * Validate new size.
145 */
146 if ((newsize <= oldsize) || (newsize % sector_size) || (newsize % phys_sector_size)) {
147 printf("hfs_extendfs: invalid size (newsize=%qu, oldsize=%qu)\n", newsize, oldsize);
148 return (EINVAL);
149 }
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);
153 return (EOVERFLOW);
154 }
155
156 addblks = newblkcnt - vcb->totalBlocks;
157
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);
161 }
162 printf("hfs_extendfs: will extend \"%s\" by %d blocks\n", vcb->vcbVN, addblks);
163
164 hfs_lock_mount (hfsmp);
165 if (hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) {
166 hfs_unlock_mount(hfsmp);
167 error = EALREADY;
168 goto out;
169 }
170 hfsmp->hfs_flags |= HFS_RESIZE_IN_PROGRESS;
171 hfs_unlock_mount (hfsmp);
172
173 /* Start with a clean journal. */
174 hfs_flush(hfsmp, HFS_FLUSH_JOURNAL_META);
175
176 /*
177 * Enclose changes inside a transaction.
178 */
179 if (hfs_start_transaction(hfsmp) != 0) {
180 error = EINVAL;
181 goto out;
182 }
183 transaction_begun = 1;
184
185
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;
189
190 hfsmp->hfs_logical_block_count = sector_count;
191 hfsmp->hfs_logical_bytes = (uint64_t) sector_count * (uint64_t) sector_size;
192
193 /*
194 * It is possible that the new file system is smaller than the partition size.
195 * Therefore, update offsets for AVH accordingly.
196 */
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);
200 }
201 hfsmp->hfs_partition_avh_sector = (hfsmp->hfsPlusIOPosOffset / sector_size) +
202 HFS_ALT_SECTOR(sector_size, hfsmp->hfs_logical_block_count);
203
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);
209 }
210
211 /*
212 * Note: we take the attributes lock in case we have an attribute data vnode
213 * which needs to change size.
214 */
215 lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
216 vp = vcb->allocationsRefNum;
217 fp = VTOF(vp);
218 bcopy(&fp->ff_data, &forkdata, sizeof(forkdata));
219
220 /*
221 * Calculate additional space required (if any) by allocation bitmap.
222 */
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;
227 else
228 bitmapblks = 0;
229
230 /*
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".
237 *
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.
244 */
245 overage_blocks = fp->ff_blocks * vcb->blockSize * 8;
246 overage_blocks = MIN (overage_blocks, newblkcnt);
247 overage_blocks -= vcb->totalBlocks;
248
249 BlockMarkFreeUnused(vcb, vcb->totalBlocks, overage_blocks);
250
251 if (bitmapblks > 0) {
252 daddr64_t blkno;
253 daddr_t blkcnt;
254 off_t bytesAdded;
255
256 /*
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.
260 */
261 blkno = (daddr64_t)fp->ff_blocks;
262
263 /*
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
267 * zone.
268 */
269 error = ExtendFileC(vcb, fp, bitmapblks * vcb->blockSize, 0,
270 kEFAllMask | kEFNoClumpMask | kEFReserveMask
271 | kEFMetadataMask | kEFContigMask, &bytesAdded);
272
273 if (error == 0) {
274 usedExtendFileC = true;
275 } else {
276 /*
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.
281 */
282 bytesAdded = 0;
283 error = AddFileExtent(vcb, fp, vcb->totalBlocks, bitmapblks);
284 if (error) {
285 printf("hfs_extendfs: error %d adding extents\n", error);
286 goto out;
287 }
288 fp->ff_blocks += bitmapblks;
289 VTOC(vp)->c_blocks = fp->ff_blocks;
290 VTOC(vp)->c_flag |= C_MODIFIED;
291 }
292
293 /*
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.
297 */
298 fp->ff_size += (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize;
299
300 /*
301 * Zero out the new bitmap blocks.
302 */
303 {
304
305 bp = NULL;
306 blkcnt = bitmapblks;
307 while (blkcnt > 0) {
308 error = (int)buf_meta_bread(vp, blkno, vcb->blockSize, NOCRED, &bp);
309 if (error) {
310 if (bp) {
311 buf_brelse(bp);
312 }
313 break;
314 }
315 bzero((char *)buf_dataptr(bp), vcb->blockSize);
316 buf_markaged(bp);
317 error = (int)buf_bwrite(bp);
318 if (error)
319 break;
320 --blkcnt;
321 ++blkno;
322 }
323 }
324 if (error) {
325 printf("hfs_extendfs: error %d clearing blocks\n", error);
326 goto out;
327 }
328 /*
329 * Mark the new bitmap space as allocated.
330 *
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).
336 */
337 if (!usedExtendFileC) {
338 error = BlockMarkAllocated(vcb, vcb->totalBlocks, bitmapblks);
339 if (error) {
340 printf("hfs_extendfs: error %d setting bitmap\n", error);
341 goto out;
342 }
343 vcb->freeBlocks -= bitmapblks;
344 }
345 }
346
347 /*
348 * Mark the new alternate VH as allocated.
349 */
350 if (vcb->blockSize == 512)
351 error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 2, 2);
352 else
353 error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 1, 1);
354 if (error) {
355 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error);
356 goto out;
357 }
358
359 /*
360 * Mark the old alternate VH as free.
361 */
362 if (vcb->blockSize == 512)
363 (void) BlockMarkFree(vcb, vcb->totalBlocks - 2, 2);
364 else
365 (void) BlockMarkFree(vcb, vcb->totalBlocks - 1, 1);
366
367 /*
368 * Adjust file system variables for new space.
369 */
370 vcb->totalBlocks += addblks;
371 vcb->freeBlocks += addblks;
372 MarkVCBDirty(vcb);
373 error = hfs_flushvolumeheader(hfsmp, HFS_FVH_WAIT | HFS_FVH_WRITE_ALT);
374 if (error) {
375 printf("hfs_extendfs: couldn't flush volume headers (%d)", error);
376 /*
377 * Restore to old state.
378 */
379 if (usedExtendFileC) {
380 (void) TruncateFileC(vcb, fp, oldBitmapSize, 0, FORK_IS_RSRC(fp),
381 FTOC(fp)->c_fileid, false);
382 } else {
383 fp->ff_blocks -= bitmapblks;
384 fp->ff_size -= (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize;
385 /*
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.
389 */
390 vcb->freeBlocks += bitmapblks;
391 }
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
398 */
399 MarkVCBDirty(vcb);
400 if (vcb->blockSize == 512) {
401 if (BlockMarkAllocated(vcb, vcb->totalBlocks - 2, 2)) {
402 hfs_mark_inconsistent(hfsmp, HFS_ROLLBACK_FAILED);
403 }
404 } else {
405 if (BlockMarkAllocated(vcb, vcb->totalBlocks - 1, 1)) {
406 hfs_mark_inconsistent(hfsmp, HFS_ROLLBACK_FAILED);
407 }
408 }
409 goto out;
410 }
411 /*
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.
414 */
415 bp = NULL;
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);
421
422 bzero((char *)buf_dataptr(bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size), kMDBSize);
423
424 journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL);
425 } else if (bp) {
426 buf_brelse(bp);
427 }
428 }
429
430 /*
431 * Update the metadata zone size based on current volume size
432 */
433 hfs_metadatazone_init(hfsmp, false);
434
435 /*
436 * Adjust the size of hfsmp->hfs_attrdata_vp
437 */
438 if (hfsmp->hfs_attrdata_vp) {
439 struct cnode *attr_cp;
440 struct filefork *attr_fp;
441
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);
445
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);
452 }
453 }
454
455 /*
456 * We only update hfsmp->allocLimit if totalBlocks actually increased.
457 */
458 if (error == 0) {
459 UpdateAllocLimit(hfsmp, hfsmp->totalBlocks);
460 }
461
462 /* Release all locks and sync up journal content before
463 * checking and extending, if required, the journal
464 */
465 if (lockflags) {
466 hfs_systemfile_unlock(hfsmp, lockflags);
467 lockflags = 0;
468 }
469 if (transaction_begun) {
470 hfs_end_transaction(hfsmp);
471 hfs_flush(hfsmp, HFS_FLUSH_JOURNAL_META);
472 transaction_begun = 0;
473 }
474
475 /* Increase the journal size, if required. */
476 error = hfs_extend_journal(hfsmp, sector_size, sector_count, context);
477 if (error) {
478 printf ("hfs_extendfs: Could not extend journal size\n");
479 goto out_noalloc;
480 }
481
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));
485
486 out:
487 if (error && fp) {
488 /* Restore allocation fork. */
489 bcopy(&forkdata, &fp->ff_data, sizeof(forkdata));
490 VTOC(vp)->c_blocks = fp->ff_blocks;
491
492 }
493
494 out_noalloc:
495 hfs_lock_mount (hfsmp);
496 hfsmp->hfs_flags &= ~HFS_RESIZE_IN_PROGRESS;
497 hfs_unlock_mount (hfsmp);
498 if (lockflags) {
499 hfs_systemfile_unlock(hfsmp, lockflags);
500 }
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)
506 error = flush_error;
507 }
508 if (error) {
509 printf ("hfs_extentfs: failed error=%d on vol=%s\n", MacToVFSError(error), hfsmp->vcbVN);
510 }
511
512 return MacToVFSError(error);
513 }
514
515 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
516
517 /*
518 * Truncate a file system (while still mounted).
519 */
520 int
521 hfs_truncatefs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context)
522 {
523 u_int64_t oldsize;
524 u_int32_t newblkcnt;
525 u_int32_t reclaimblks = 0;
526 int lockflags = 0;
527 int transaction_begun = 0;
528 Boolean updateFreeBlocks = false;
529 Boolean disable_sparse = false;
530 int error = 0;
531
532 hfs_lock_mount (hfsmp);
533 if (hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) {
534 hfs_unlock_mount (hfsmp);
535 return (EALREADY);
536 }
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);
542
543 /*
544 * - Journaled HFS Plus volumes only.
545 * - No embedded volumes.
546 */
547 if ((hfsmp->jnl == NULL) ||
548 (hfsmp->hfsPlusIOPosOffset != 0)) {
549 error = EPERM;
550 goto out;
551 }
552 oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize;
553 newblkcnt = newsize / hfsmp->blockSize;
554 reclaimblks = hfsmp->totalBlocks - newblkcnt;
555
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);
559 }
560
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);
567 error = EINVAL;
568 goto out;
569 }
570
571 /*
572 * Make sure that the file system has enough free blocks reclaim.
573 *
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
588 * them.
589 *
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
594 *
595 * Total number of blocks that need to be reclaimed:
596 * ------------------------------------------------
597 * Blocks to Reclaim = Allocated_To-Reclaim + Free_To-Reclaim
598 *
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
602 * later in the code.
603 *
604 * The condition below gets converted to:
605 *
606 * Allocated To-Reclaim + Free To-Reclaim >= Free Stationary + Free To-Reclaim
607 *
608 * which is equivalent to:
609 *
610 * Allocated To-Reclaim >= Free Stationary
611 */
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));
614 error = ENOSPC;
615 goto out;
616 }
617
618 /* Start with a clean journal. */
619 hfs_flush(hfsmp, HFS_FLUSH_JOURNAL_META);
620
621 if (hfs_start_transaction(hfsmp) != 0) {
622 error = EINVAL;
623 goto out;
624 }
625 transaction_begun = 1;
626
627 /* Take the bitmap lock to update the alloc limit field */
628 lockflags = hfs_systemfile_lock(hfsmp, SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
629
630 /*
631 * Prevent new allocations from using the part we're trying to truncate.
632 *
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
637 * truncate away.
638 */
639 if (hfsmp->blockSize == 512) {
640 error = UpdateAllocLimit (hfsmp, newblkcnt - 2);
641 }
642 else {
643 error = UpdateAllocLimit (hfsmp, newblkcnt - 1);
644 }
645
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.
652 *
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
657 * solution for now.
658 */
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;
664 }
665
666 /*
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.
676 */
677 hfsmp->reclaimBlocks = reclaimblks;
678 hfsmp->freeBlocks -= reclaimblks;
679 updateFreeBlocks = true;
680 hfs_unlock_mount(hfsmp);
681
682 if (lockflags) {
683 hfs_systemfile_unlock(hfsmp, lockflags);
684 lockflags = 0;
685 }
686
687 /*
688 * Update the metadata zone size to match the new volume size,
689 * and if it too less, metadata zone might be disabled.
690 */
691 hfs_metadatazone_init(hfsmp, false);
692
693 /*
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.
697 *
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.
702 */
703 if (hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks)) {
704 /*
705 * hfs_reclaimspace will use separate transactions when
706 * relocating files (so we don't overwhelm the journal).
707 */
708 hfs_end_transaction(hfsmp);
709 transaction_begun = 0;
710
711 /* Attempt to reclaim some space. */
712 error = hfs_reclaimspace(hfsmp, hfsmp->allocLimit, reclaimblks, context);
713 if (error != 0) {
714 printf("hfs_truncatefs: couldn't reclaim space on %s (error=%d)\n", hfsmp->vcbVN, error);
715 error = ENOSPC;
716 goto out;
717 }
718
719 if (hfs_start_transaction(hfsmp) != 0) {
720 error = EINVAL;
721 goto out;
722 }
723 transaction_begun = 1;
724
725 /* Check if we're clear now. */
726 error = hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks);
727 if (error != 0) {
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 */
730 goto out;
731 }
732 }
733
734 /*
735 * Note: we take the attributes lock in case we have an attribute data vnode
736 * which needs to change size.
737 */
738 lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
739
740 /*
741 * Allocate last 1KB for alternate volume header.
742 */
743 error = BlockMarkAllocated(hfsmp, hfsmp->allocLimit, (hfsmp->blockSize == 512) ? 2 : 1);
744 if (error) {
745 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error);
746 goto out;
747 }
748
749 /*
750 * Mark the old alternate volume header as free.
751 * We don't bother shrinking allocation bitmap file.
752 */
753 if (hfsmp->blockSize == 512)
754 (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 2, 2);
755 else
756 (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 1, 1);
757
758 /* Don't invalidate the old AltVH yet. It is still valid until the partition size is updated ! */
759
760 /* Log successful shrinking. */
761 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
762 hfsmp->vcbVN, newblkcnt, hfsmp->totalBlocks);
763
764 /*
765 * Adjust file system variables and flush them to disk.
766 *
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).
771 */
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;
776
777 /*
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.
786 */
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);
790 }
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);
796 }
797
798 MarkVCBDirty(hfsmp);
799 error = hfs_flushvolumeheader(hfsmp, HFS_FVH_WAIT | HFS_FVH_WRITE_ALT);
800 if (error) {
801 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error);
802 }
803
804 /*
805 * Adjust the size of hfsmp->hfs_attrdata_vp
806 */
807 if (hfsmp->hfs_attrdata_vp) {
808 struct cnode *cp;
809 struct filefork *fp;
810
811 if (vnode_get(hfsmp->hfs_attrdata_vp) == 0) {
812 cp = VTOC(hfsmp->hfs_attrdata_vp);
813 fp = VTOF(hfsmp->hfs_attrdata_vp);
814
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);
821 }
822 }
823
824 out:
825 /*
826 * Update the allocLimit to acknowledge the last one or two blocks now.
827 * Add it to the tree as well if necessary.
828 */
829 UpdateAllocLimit (hfsmp, hfsmp->totalBlocks);
830
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
836 * it is rebuilt.
837 */
838 hfsmp->hfs_flags |= HFS_HAS_SPARSE_DEVICE;
839 ResetVCBFreeExtCache(hfsmp);
840 }
841
842 if (error && (updateFreeBlocks == true)) {
843 hfsmp->freeBlocks += reclaimblks;
844 }
845 hfsmp->reclaimBlocks = 0;
846
847 if (hfsmp->nextAllocation >= hfsmp->allocLimit) {
848 hfsmp->nextAllocation = hfsmp->hfs_metazone_end + 1;
849 }
850 hfsmp->hfs_flags &= ~HFS_RESIZE_IN_PROGRESS;
851 hfs_unlock_mount (hfsmp);
852
853 /* On error, reset the metadata zone for original volume size */
854 if (error && (updateFreeBlocks == true)) {
855 hfs_metadatazone_init(hfsmp, false);
856 }
857
858 if (lockflags) {
859 hfs_systemfile_unlock(hfsmp, lockflags);
860 }
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)
866 error = flush_error;
867 }
868
869 if (error) {
870 printf ("hfs_truncatefs: failed error=%d on vol=%s\n", MacToVFSError(error), hfsmp->vcbVN);
871 }
872
873 return MacToVFSError(error);
874 }
875
876
877 /*
878 * Invalidate the physical block numbers associated with buffer cache blocks
879 * in the given extent of the given vnode.
880 */
881 struct hfs_inval_blk_no {
882 daddr64_t sectorStart;
883 daddr64_t sectorCount;
884 };
885 static int
886 hfs_invalidate_block_numbers_callback(buf_t bp, void *args_in)
887 {
888 daddr64_t blkno;
889 struct hfs_inval_blk_no *args;
890
891 blkno = buf_blkno(bp);
892 args = args_in;
893
894 if (blkno >= args->sectorStart && blkno < args->sectorStart+args->sectorCount)
895 buf_setblkno(bp, buf_lblkno(bp));
896
897 return BUF_RETURNED;
898 }
899 static void
900 hfs_invalidate_sectors(struct vnode *vp, daddr64_t sectorStart, daddr64_t sectorCount)
901 {
902 struct hfs_inval_blk_no args;
903 args.sectorStart = sectorStart;
904 args.sectorCount = sectorCount;
905
906 buf_iterate(vp, hfs_invalidate_block_numbers_callback, BUF_SCAN_DIRTY|BUF_SCAN_CLEAN, &args);
907 }
908
909
910 /*
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).
915 *
916 * At this point, for regular files, we hold the truncate lock exclusive
917 * and the cnode lock exclusive.
918 */
919 static int
920 hfs_copy_extent(
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)
927 {
928 int err = 0;
929 size_t bufferSize;
930 void *buffer = NULL;
931 struct vfsioattr ioattr;
932 buf_t bp = NULL;
933 off_t resid;
934 size_t ioSize;
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;
938 #if CONFIG_PROTECT
939 int cpenabled = 0;
940 #endif
941
942 /*
943 * Sanity check that we have locked the vnode of the file we're copying.
944 *
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.
948 */
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);
952
953 #if CONFIG_PROTECT
954 /*
955 * Prepare the CP blob and get it ready for use, if necessary.
956 *
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.
961 */
962 if (!vnode_issystem (vp) && vnode_isreg(vp) && cp_fs_protected (hfsmp->hfs_mp)) {
963 cpenabled = 1;
964 }
965 #endif
966
967 /*
968 * Determine the I/O size to use
969 *
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?
973 */
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))
977 return ENOMEM;
978
979 /* Get a buffer for doing the I/O */
980 bp = buf_alloc(hfsmp->hfs_devvp);
981 buf_setdataptr(bp, (uintptr_t)buffer);
982
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;
986 while (resid > 0) {
987 ioSize = MIN(bufferSize, (size_t) resid);
988 ioSizeSectors = ioSize / hfsmp->hfs_logical_block_size;
989
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);
996
997 /*
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.
1002 */
1003
1004 /* Attach the new CP blob to the buffer if needed */
1005 #if CONFIG_PROTECT
1006 if (cpenabled) {
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);
1010
1011 /* Initialize the content protection file offset to start at 0 */
1012 buf_setcpoff (bp, 0);
1013 }
1014 #endif
1015
1016 /* Do the read */
1017 err = VNOP_STRATEGY(bp);
1018 if (!err)
1019 err = buf_biowait(bp);
1020 if (err) {
1021 #if CONFIG_PROTECT
1022 /* Turn the flag off in error cases. */
1023 if (cpenabled) {
1024 cp->c_cpentry->cp_flags &= ~CP_RELOCATION_INFLIGHT;
1025 }
1026 #endif
1027 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err);
1028 break;
1029 }
1030
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))
1038 buf_markfua(bp);
1039
1040 #if CONFIG_PROTECT
1041 /* Attach the CP to the buffer if needed */
1042 if (cpenabled) {
1043 bufattr_setcpx(buf_attr(bp), hfsmp->hfs_resize_cpx);
1044 /*
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.
1048 */
1049 buf_setcpoff (bp, 0);
1050 }
1051 #endif
1052
1053 /* Do the write */
1054 vnode_startwrite(hfsmp->hfs_devvp);
1055 err = VNOP_STRATEGY(bp);
1056 if (!err) {
1057 err = buf_biowait(bp);
1058 }
1059 #if CONFIG_PROTECT
1060 /* Turn the flag off regardless once the strategy call finishes. */
1061 if (cpenabled) {
1062 cp->c_cpentry->cp_flags &= ~CP_RELOCATION_INFLIGHT;
1063 }
1064 #endif
1065 if (err) {
1066 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err);
1067 break;
1068 }
1069
1070 resid -= ioSize;
1071 srcSector += ioSizeSectors;
1072 destSector += ioSizeSectors;
1073 }
1074 if (bp)
1075 buf_free(bp);
1076 if (buffer)
1077 kmem_free(kernel_map, (vm_offset_t)buffer, bufferSize);
1078
1079 /* Make sure all writes have been flushed to disk. */
1080 if (vnode_issystem(vp) && !journal_uses_fua(hfsmp->jnl)) {
1081
1082 err = hfs_flush(hfsmp, HFS_FLUSH_CACHE);
1083 if (err) {
1084 printf("hfs_copy_extent: hfs_flush failed (%d)\n", err);
1085 err = 0; /* Don't fail the copy. */
1086 }
1087 }
1088
1089 if (!err)
1090 hfs_invalidate_sectors(vp, (daddr64_t)oldStart*sectorsPerBlock, (daddr64_t)blockCount*sectorsPerBlock);
1091
1092 return err;
1093 }
1094
1095
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.
1100 */
1101 struct hfs_reclaim_extent_info {
1102 struct vnode *vp;
1103 u_int32_t fileID;
1104 u_int8_t forkType;
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 */
1114 union record {
1115 HFSPlusExtentRecord overflow;/* Extent record from overflow extents btree */
1116 HFSPlusAttrRecord xattr; /* Attribute record for large EAs */
1117 } record;
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
1123 */
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
1130 */
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 */
1135 };
1136
1137 /*
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
1143 * extent btree.
1144 *
1145 * Example:
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.
1154 *
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.
1167 *
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.
1171 *
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.
1178 *
1179 * Before:
1180 * record X record X+1
1181 * ---------------------===--------- ---------------------------------
1182 * | A | . | . | . | B | C | D | E | | F | . | . | . | G | 0 | 0 | 0 |
1183 * ---------------------===--------- ---------------------------------
1184 *
1185 * After:
1186 * ---------------------=======----- ---------------------------------
1187 * | A | . | . | . | B | C1| C2| D | | E | F | . | . | . | G | 0 | 0 |
1188 * ---------------------=======----- ---------------------------------
1189 *
1190 * C1.startBlock = C.startBlock
1191 * C1.blockCount = N
1192 *
1193 * C2.startBlock = C.startBlock + N
1194 * C2.blockCount = C.blockCount - N
1195 *
1196 * FABN = old FABN - E.blockCount
1197 *
1198 * Inputs:
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.
1207 * Output:
1208 * Zero on success, non-zero on failure.
1209 */
1210 static int
1211 hfs_split_extent(struct hfs_reclaim_extent_info *extent_info, uint32_t newBlockCount)
1212 {
1213 int error = 0;
1214 int index = extent_info->extent_index;
1215 int i;
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;
1225 uint16_t reclen;
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;
1229 Boolean is_xattr;
1230 struct cnode *cp;
1231
1232 is_xattr = extent_info->is_xattr;
1233 extents = extent_info->extents;
1234 cp = VTOC(extent_info->vp);
1235
1236 if (newBlockCount == 0) {
1237 if (hfs_resize_debug) {
1238 printf ("hfs_split_extent: No splitting required for newBlockCount=0\n");
1239 }
1240 return error;
1241 }
1242
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);
1245 }
1246
1247 /* Extents overflow btree can not have more than 8 extents.
1248 * No split allowed if the 8th extent is already used.
1249 */
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");
1252 error = ENOSPC;
1253 goto out;
1254 }
1255
1256 /* Determine the starting allocation block number for the following
1257 * overflow extent record, if any, before the current record
1258 * gets modified.
1259 */
1260 read_recStartBlock = extent_info->recStartBlock;
1261 for (i = 0; i < kHFSPlusExtentDensity; i++) {
1262 if (extents[i].blockCount == 0) {
1263 break;
1264 }
1265 read_recStartBlock += extents[i].blockCount;
1266 }
1267
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;
1273
1274 /* Last extent in the record will be split, so nothing to shift */
1275 } else {
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.
1279 */
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);
1283 }
1284
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.
1290 */
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);
1295 }
1296 }
1297 extents[i+1] = extents[i];
1298 }
1299 }
1300
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.
1306 */
1307 } else {
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.
1313 */
1314 extents[index+1].startBlock = extents[index].startBlock + newBlockCount;
1315 extents[index+1].blockCount = extents[index].blockCount - newBlockCount;
1316 }
1317 /* Update the extent being split, only the block count will change */
1318 extents[index].blockCount = newBlockCount;
1319
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);
1324 } else {
1325 printf ("overflow:(%u,%u)\n", shift_extent.startBlock, shift_extent.blockCount);
1326 }
1327 }
1328
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.
1337 */
1338 cp->c_flag |= C_MODIFIED;
1339 } else {
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
1343 */
1344 error = BTReplaceRecord(extent_info->fcb, extent_info->iterator,
1345 &(extent_info->btdata), extent_info->recordlen);
1346 if (error) {
1347 printf ("hfs_split_extent: fileID=%u BTReplaceRecord returned error=%d\n", extent_info->fileID, error);
1348 goto out;
1349 }
1350 }
1351
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");
1356 }
1357 error = 0;
1358 goto out;
1359 }
1360
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.
1368 */
1369
1370 /* Initialize iterator to search the next record */
1371 bzero(&iterator, sizeof(iterator));
1372 if (is_xattr) {
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 */
1377
1378 MALLOC(xattr_rec, HFSPlusAttrRecord *,
1379 sizeof(HFSPlusAttrRecord), M_TEMP, M_WAITOK);
1380 if (xattr_rec == NULL) {
1381 error = ENOMEM;
1382 goto out;
1383 }
1384 btdata.bufferAddress = xattr_rec;
1385 btdata.itemSize = sizeof(HFSPlusAttrRecord);
1386 btdata.itemCount = 1;
1387 extents = xattr_rec->overflowExtents.extents;
1388 } else {
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 */
1395
1396 MALLOC(extents_rec, HFSPlusExtentRecord *,
1397 sizeof(HFSPlusExtentRecord), M_TEMP, M_WAITOK);
1398 if (extents_rec == NULL) {
1399 error = ENOMEM;
1400 goto out;
1401 }
1402 btdata.bufferAddress = extents_rec;
1403 btdata.itemSize = sizeof(HFSPlusExtentRecord);
1404 btdata.itemCount = 1;
1405 extents = extents_rec[0];
1406 }
1407
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
1415 * file.
1416 *
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.
1426 */
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);
1430 }
1431
1432 /* Search if there is any existing overflow extent record
1433 * that matches the current file and the logical start block
1434 * number.
1435 *
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.
1440 */
1441 if (is_xattr) {
1442 xattr_key->startBlock = read_recStartBlock;
1443 } else {
1444 extents_key->startBlock = read_recStartBlock;
1445 }
1446 error = BTSearchRecord(extent_info->fcb, &iterator, &btdata, &reclen, &iterator);
1447 if (error) {
1448 if (error != btNotFound) {
1449 printf ("hfs_split_extent: fileID=%u startBlock=%u BTSearchRecord error=%d\n", extent_info->fileID, read_recStartBlock, error);
1450 goto out;
1451 }
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.
1456 */
1457 create_record = true;
1458 }
1459
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.
1469 */
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);
1474 }
1475 }
1476
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.
1480 */
1481 for (i = 0; i < kHFSPlusExtentDensity; i++) {
1482 if (extents[i].blockCount == 0) {
1483 break;
1484 }
1485 read_recStartBlock += extents[i].blockCount;
1486 }
1487
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;
1495
1496 if (is_xattr) {
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.
1503 */
1504
1505 /* Initialize the new xattr record */
1506 xattr_rec->recordType = kHFSPlusAttrExtents;
1507 xattr_rec->overflowExtents.reserved = 0;
1508 reclen = sizeof(HFSPlusAttrExtents);
1509 } else {
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.
1516 */
1517 extents_key->keyLength = kHFSPlusExtentKeyMaximumLength;
1518 extents_key->forkType = extent_info->forkType;
1519 extents_key->fileID = extent_info->fileID;
1520
1521 /* Initialize the new extent record */
1522 reclen = sizeof(HFSPlusExtentRecord);
1523 }
1524 } else {
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
1530 * record.
1531 */
1532 last_extent = extents[kHFSPlusExtentDensity-1];
1533
1534 /* Shift all entries by one index towards the end */
1535 for (i = kHFSPlusExtentDensity-2; i >= 0; i--) {
1536 extents[i+1] = extents[i];
1537 }
1538
1539 /* Overflow extent entry saved from previous record
1540 * is now the first entry in the current record.
1541 */
1542 extents[0] = shift_extent;
1543
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);
1546 }
1547
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.
1551 */
1552 shift_extent = last_extent;
1553
1554 /* Since the key->startBlock is being changed for this record,
1555 * it should be deleted and inserted with the new key.
1556 */
1557 error = BTDeleteRecord(extent_info->fcb, &iterator);
1558 if (error) {
1559 printf ("hfs_split_extent: fileID=%u startBlock=%u BTDeleteRecord error=%d\n", extent_info->fileID, read_recStartBlock, error);
1560 goto out;
1561 }
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));
1564 }
1565 }
1566
1567 /* Insert the newly created or modified extent record */
1568 bzero(&iterator.hint, sizeof(iterator.hint));
1569 if (is_xattr) {
1570 xattr_key->startBlock = write_recStartBlock;
1571 } else {
1572 extents_key->startBlock = write_recStartBlock;
1573 }
1574 error = BTInsertRecord(extent_info->fcb, &iterator, &btdata, reclen);
1575 if (error) {
1576 printf ("hfs_split_extent: fileID=%u, startBlock=%u BTInsertRecord error=%d\n", extent_info->fileID, write_recStartBlock, error);
1577 goto out;
1578 }
1579 if (hfs_resize_debug) {
1580 printf ("hfs_split_extent: Inserted extent record with startBlock=%u\n", write_recStartBlock);
1581 }
1582 }
1583
1584 out:
1585 /*
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.
1592 */
1593 BTFlushPath(extent_info->fcb);
1594
1595 if (extents_rec) {
1596 FREE (extents_rec, M_TEMP);
1597 }
1598 if (xattr_rec) {
1599 FREE (xattr_rec, M_TEMP);
1600 }
1601 return error;
1602 }
1603
1604
1605 /*
1606 * Relocate an extent if it lies beyond the expected end of volume.
1607 *
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
1616 * being reclaimed.
1617 *
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.
1622 *
1623 * Inputs:
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.
1630 */
1631 static int
1632 hfs_reclaim_extent(struct hfsmount *hfsmp, const u_long allocLimit, struct hfs_reclaim_extent_info *extent_info, vfs_context_t context)
1633 {
1634 int error = 0;
1635 int index;
1636 struct cnode *cp;
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;
1642 uint16_t node_size;
1643 uint32_t remainder_blocks;
1644 u_int32_t alloc_flags;
1645 int blocks_allocated = false;
1646
1647 index = extent_info->extent_index;
1648 cp = VTOC(extent_info->vp);
1649
1650 oldStartBlock = extent_info->extents[index].startBlock;
1651 oldBlockCount = extent_info->extents[index].blockCount;
1652
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);
1655 }
1656
1657 /* If the current extent lies completely within allocLimit,
1658 * it does not require any relocation.
1659 */
1660 if ((oldStartBlock + oldBlockCount) <= allocLimit) {
1661 extent_info->cur_blockCount += oldBlockCount;
1662 return error;
1663 }
1664
1665 /* Every extent should be relocated in its own transaction
1666 * to make sure that we don't overflow the journal buffer.
1667 */
1668 error = hfs_start_transaction(hfsmp);
1669 if (error) {
1670 return error;
1671 }
1672 extent_info->lockflags = hfs_systemfile_lock(hfsmp, extent_info->lockflags, HFS_EXCLUSIVE_LOCK);
1673
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.
1680 */
1681 if (oldStartBlock < allocLimit) {
1682 newBlockCount = allocLimit - oldStartBlock;
1683
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);
1687 }
1688
1689 /* If the extent belongs to a btree, check and trim
1690 * it to be multiple of the node size.
1691 */
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.
1698 */
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);
1705 }
1706 }
1707 }
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.
1712 */
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");
1716 }
1717 goto relocate_full_extent;
1718 }
1719 }
1720
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.
1726 */
1727 error = hfs_split_extent(extent_info, newBlockCount);
1728 if (error == 0) {
1729 /* Split success, no relocation required */
1730 goto out;
1731 }
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);
1736 }
1737 }
1738
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.
1748 */
1749 alloc_flags = HFS_ALLOC_FORCECONTIG | HFS_ALLOC_SKIPFREEBLKS;
1750 if (extent_info->is_sysfile) {
1751 alloc_flags |= HFS_ALLOC_METAZONE;
1752 }
1753
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);
1762 }
1763 if ((error == dskFulErr) || (error == ENOSPC)) {
1764 /*
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.
1768 *
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.
1774 */
1775 alloc_flags &= ~HFS_ALLOC_FORCECONTIG;
1776 alloc_flags |= HFS_ALLOC_FLUSHTXN;
1777
1778 error = BlockAllocate(hfsmp, 1, oldBlockCount, oldBlockCount,
1779 alloc_flags, &newStartBlock, &newBlockCount);
1780 if (error) {
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);
1782 goto out;
1783 }
1784
1785 /*
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.
1792 */
1793 if (newBlockCount != oldBlockCount) {
1794 blocks_allocated = true;
1795
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.
1799 */
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);
1813 }
1814 if (newBlockCount == 0) {
1815 printf ("hfs_reclaim_extent: Not enough contiguous blocks available to relocate fileID=%d\n", extent_info->fileID);
1816 error = ENOSPC;
1817 goto out;
1818 }
1819 }
1820 }
1821 }
1822
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.
1828 */
1829 error = hfs_split_extent(extent_info, newBlockCount);
1830 if (error) {
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);
1832 goto out;
1833 }
1834 oldBlockCount = newBlockCount;
1835 } /* end oldBlockCount != newBlockCount */
1836 } /* end allocation request for any available free space */
1837
1838 if (error) {
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);
1840 goto out;
1841 }
1842 blocks_allocated = true;
1843
1844 /* Copy data from old location to new location */
1845 error = hfs_copy_extent(hfsmp, extent_info->vp, oldStartBlock,
1846 newStartBlock, newBlockCount, context);
1847 if (error) {
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);
1849 goto out;
1850 }
1851
1852 /* Update the extent record with the new start block information */
1853 extent_info->extents[index].startBlock = newStartBlock;
1854
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));
1862 } else {
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);
1867 }
1868 }
1869 } else {
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);
1873 }
1874 if (error) {
1875 printf ("hfs_reclaim_extent: fileID=%u, update record error=%u\n", extent_info->fileID, error);
1876 goto out;
1877 }
1878
1879 /* Deallocate the old extent */
1880 error = BlockDeallocate(hfsmp, oldStartBlock, oldBlockCount, HFS_ALLOC_SKIPFREEBLKS);
1881 if (error) {
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);
1883 goto out;
1884 }
1885 extent_info->blocks_relocated += newBlockCount;
1886
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);
1889 }
1890
1891 out:
1892 if (error != 0) {
1893 if (blocks_allocated == true) {
1894 BlockDeallocate(hfsmp, newStartBlock, newBlockCount, HFS_ALLOC_SKIPFREEBLKS);
1895 }
1896 } else {
1897 /* On success, increment the total allocation blocks processed */
1898 extent_info->cur_blockCount += newBlockCount;
1899 }
1900
1901 hfs_systemfile_unlock(hfsmp, extent_info->lockflags);
1902
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.
1906 */
1907 if ((extent_info->catalog_fp) &&
1908 (extent_info->is_sysfile == false)) {
1909 hfs_update(extent_info->vp, 0);
1910 }
1911
1912 hfs_end_transaction(hfsmp);
1913
1914 return error;
1915 }
1916
1917 /* Report intermediate progress during volume resize */
1918 static void
1919 hfs_truncatefs_progress(struct hfsmount *hfsmp)
1920 {
1921 u_int32_t cur_progress = 0;
1922
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;
1927 }
1928 return;
1929 }
1930
1931 /*
1932 * Reclaim space at the end of a volume for given file and forktype.
1933 *
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.
1942 *
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.
1949 *
1950 * Inputs:
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.
1961 *
1962 * Side Effects:
1963 * hfsmp->hfs_resize_blocksmoved is incremented by the number of allocation
1964 * blocks that were relocated.
1965 */
1966 static int
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)
1969 {
1970 int error = 0;
1971 struct hfs_reclaim_extent_info *extent_info;
1972 int i;
1973 int lockflags = 0;
1974 struct cnode *cp;
1975 struct filefork *fp;
1976 int took_truncate_lock = false;
1977 int release_desc = false;
1978 HFSPlusExtentKey *key;
1979
1980 /* If there is no vnode for this file, then there's nothing to do. */
1981 if (vp == NULL) {
1982 return 0;
1983 }
1984
1985 cp = VTOC(vp);
1986
1987 if (hfs_resize_debug) {
1988 const char *filename = (const char *) cp->c_desc.cd_nameptr;
1989 int namelen = cp->c_desc.cd_namelen;
1990
1991 if (filename == NULL) {
1992 filename = "";
1993 namelen = 0;
1994 }
1995 printf("hfs_reclaim_file: reclaiming '%.*s'\n", namelen, filename);
1996 }
1997
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) {
2001 return ENOMEM;
2002 }
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;
2010 }
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;
2019 }
2020 extent_info->lockflags = lockflags;
2021 extent_info->fcb = VTOF(hfsmp->hfs_extents_vp);
2022
2023 /* Flush data associated with current file on disk.
2024 *
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.
2032 */
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.
2036 */
2037 error = hfs_flush(hfsmp, HFS_FLUSH_JOURNAL_META);
2038 if (error) {
2039 printf ("hfs_reclaim_file: journal_flush returned %d\n", error);
2040 goto out;
2041 }
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.
2048 */
2049 buf_flushdirtyblks(vp, 0, BUF_SKIP_LOCKED, "hfs_reclaim_file");
2050
2051 hfs_unlock(cp);
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);
2056 if (error) {
2057 goto out;
2058 }
2059
2060 /* If the file no longer exists, nothing left to do */
2061 if (cp->c_flag & C_NOEXISTS) {
2062 error = 0;
2063 goto out;
2064 }
2065
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.)
2070 */
2071 error = vnode_waitforwrites(vp, 0, 0, 0, "hfs_reclaim_file");
2072 if (error) {
2073 goto out;
2074 }
2075 }
2076
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);
2079 }
2080
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)) {
2091 error = ENOMEM;
2092 goto out;
2093 }
2094
2095 /* Lookup catalog record for directory hard link and
2096 * create a fake filefork for the value looked up from
2097 * the disk.
2098 */
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);
2107 if (error) {
2108 printf ("hfs_reclaim_file: cat_lookup_dirlink for fileID=%u returned error=%u\n", fileID, error);
2109 goto out;
2110 }
2111 release_desc = true;
2112 } else {
2113 fp = VTOF(vp);
2114 }
2115
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) {
2122 break;
2123 }
2124 extent_info->extent_index = i;
2125 error = hfs_reclaim_extent(hfsmp, allocLimit, extent_info, context);
2126 if (error) {
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);
2128 goto out;
2129 }
2130 }
2131
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.
2135 */
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);
2139 }
2140 goto out;
2141 }
2142
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);
2145 }
2146
2147 MALLOC(extent_info->iterator, struct BTreeIterator *, sizeof(struct BTreeIterator), M_TEMP, M_WAITOK);
2148 if (extent_info->iterator == NULL) {
2149 error = ENOMEM;
2150 goto out;
2151 }
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;
2158
2159 extent_info->btdata.bufferAddress = extent_info->record.overflow;
2160 extent_info->btdata.itemSize = sizeof(HFSPlusExtentRecord);
2161 extent_info->btdata.itemCount = 1;
2162
2163 extent_info->catalog_fp = NULL;
2164
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) {
2177 goto out;
2178 }
2179 extent_info->extent_index = i;
2180 error = hfs_reclaim_extent(hfsmp, allocLimit, extent_info, context);
2181 if (error) {
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);
2183 goto out;
2184 }
2185 }
2186
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);
2193 if (error) {
2194 break;
2195 }
2196 /* Stop when we encounter a different file or fork. */
2197 if ((key->fileID != fileID) || (key->forkType != forktype)) {
2198 break;
2199 }
2200 }
2201 if (error == fsBTRecordNotFoundErr || error == fsBTEndOfIterationErr) {
2202 error = 0;
2203 }
2204
2205 out:
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);
2213 }
2214 }
2215 if (extent_info->iterator) {
2216 FREE(extent_info->iterator, M_TEMP);
2217 }
2218 if (release_desc == true) {
2219 cat_releasedesc(extent_info->dirlink_desc);
2220 }
2221 if (extent_info->dirlink_desc) {
2222 FREE(extent_info->dirlink_desc, M_TEMP);
2223 }
2224 if (extent_info->dirlink_attr) {
2225 FREE(extent_info->dirlink_attr, M_TEMP);
2226 }
2227 if (extent_info->dirlink_fork) {
2228 FREE(extent_info->dirlink_fork, M_TEMP);
2229 }
2230 if ((extent_info->blocks_relocated != 0) && (extent_info->is_sysfile == false)) {
2231 hfs_update(vp, 0);
2232 }
2233 if (took_truncate_lock) {
2234 hfs_unlock_truncate(cp, HFS_LOCK_DEFAULT);
2235 }
2236 if (extent_info) {
2237 FREE(extent_info, M_TEMP);
2238 }
2239 if (hfs_resize_debug) {
2240 printf("hfs_reclaim_file: === Finished relocating %sfork for fileid=%u (error=%d) ===\n", (forktype ? "rsrc" : "data"), fileID, error);
2241 }
2242
2243 return error;
2244 }
2245
2246
2247 /*
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.
2254 *
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.
2258 */
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;
2264 };
2265
2266 static errno_t
2267 hfs_journal_relocate_callback(void *_args)
2268 {
2269 int error;
2270 struct hfs_journal_relocate_args *args = _args;
2271 struct hfsmount *hfsmp = args->hfsmp;
2272 buf_t bp;
2273 JournalInfoBlock *jibp;
2274
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);
2278 if (error) {
2279 printf("hfs_journal_relocate_callback: failed to read JIB (%d)\n", error);
2280 if (bp) {
2281 buf_brelse(bp);
2282 }
2283 return error;
2284 }
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))
2289 buf_markfua(bp);
2290 error = buf_bwrite(bp);
2291 if (error) {
2292 printf("hfs_journal_relocate_callback: failed to write JIB (%d)\n", error);
2293 return error;
2294 }
2295 if (!journal_uses_fua(hfsmp->jnl)) {
2296 error = hfs_flush(hfsmp, HFS_FLUSH_CACHE);
2297 if (error) {
2298 printf("hfs_journal_relocate_callback: hfs_flush failed (%d)\n", error);
2299 error = 0; /* Don't fail the operation. */
2300 }
2301 }
2302
2303 return error;
2304 }
2305
2306
2307 /* Type of resize operation in progress */
2308 #define HFS_RESIZE_TRUNCATE 1
2309 #define HFS_RESIZE_EXTEND 2
2310
2311 /*
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.
2317 *
2318 * Note: This function starts a transaction and grabs the btree locks.
2319 */
2320 static int
2321 hfs_relocate_journal_file(struct hfsmount *hfsmp, u_int32_t jnl_size, int resize_type, vfs_context_t context)
2322 {
2323 int error;
2324 int journal_err;
2325 int lockflags;
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;
2336
2337 /* Calculate the number of allocation blocks required for the journal */
2338 jnlBlockCount = howmany(jnl_size, hfsmp->blockSize);
2339
2340 /*
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
2348 * flag.
2349 *
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.
2353 */
2354 if (resize_type == HFS_RESIZE_TRUNCATE) {
2355 alloc_skipfreeblks = HFS_ALLOC_SKIPFREEBLKS;
2356 } else {
2357 alloc_skipfreeblks = 0;
2358 }
2359
2360 error = hfs_start_transaction(hfsmp);
2361 if (error) {
2362 printf("hfs_relocate_journal_file: hfs_start_transaction returned %d\n", error);
2363 return error;
2364 }
2365 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
2366
2367 error = BlockAllocate(hfsmp, 1, jnlBlockCount, jnlBlockCount,
2368 HFS_ALLOC_METAZONE | HFS_ALLOC_FORCECONTIG | HFS_ALLOC_FLUSHTXN | alloc_skipfreeblks,
2369 &newStartBlock, &newBlockCount);
2370 if (error) {
2371 printf("hfs_relocate_journal_file: BlockAllocate returned %d\n", error);
2372 goto fail;
2373 }
2374 if (newBlockCount != jnlBlockCount) {
2375 printf("hfs_relocate_journal_file: newBlockCount != jnlBlockCount (%u, %u)\n", newBlockCount, jnlBlockCount);
2376 goto free_fail;
2377 }
2378
2379 error = cat_idlookup(hfsmp, hfsmp->hfs_jnlfileid, 1, 0, &journal_desc, &journal_attr, &journal_fork);
2380 if (error) {
2381 printf("hfs_relocate_journal_file: cat_idlookup returned %d\n", error);
2382 goto free_fail;
2383 }
2384
2385 oldStartBlock = journal_fork.cf_extents[0].startBlock;
2386 oldBlockCount = journal_fork.cf_extents[0].blockCount;
2387 error = BlockDeallocate(hfsmp, oldStartBlock, oldBlockCount, alloc_skipfreeblks);
2388 if (error) {
2389 printf("hfs_relocate_journal_file: BlockDeallocate returned %d\n", error);
2390 goto free_fail;
2391 }
2392
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 */
2400 if (error) {
2401 printf("hfs_relocate_journal_file: cat_update returned %d\n", error);
2402 goto free_fail;
2403 }
2404
2405 /*
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.
2409 */
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;
2415
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);
2419 if (error) {
2420 /* NOTE: journal_relocate will mark the journal invalid. */
2421 printf("hfs_relocate_journal_file: journal_relocate returned %d\n", error);
2422 goto fail;
2423 }
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);
2426 }
2427 hfsmp->jnl_start = newStartBlock;
2428 hfsmp->jnl_size = (off_t)newBlockCount * hfsmp->blockSize;
2429 }
2430
2431 hfs_systemfile_unlock(hfsmp, lockflags);
2432 error = hfs_end_transaction(hfsmp);
2433 if (error) {
2434 printf("hfs_relocate_journal_file: hfs_end_transaction returned %d\n", error);
2435 }
2436
2437 return error;
2438
2439 free_fail:
2440 journal_err = BlockDeallocate(hfsmp, newStartBlock, newBlockCount, HFS_ALLOC_SKIPFREEBLKS);
2441 if (journal_err) {
2442 printf("hfs_relocate_journal_file: BlockDeallocate returned %d\n", error);
2443 hfs_mark_inconsistent(hfsmp, HFS_ROLLBACK_FAILED);
2444 }
2445 fail:
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);
2450 }
2451 return error;
2452 }
2453
2454
2455 /*
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
2459 * relocate code.
2460 */
2461 static int
2462 hfs_reclaim_journal_file(struct hfsmount *hfsmp, u_int32_t allocLimit, vfs_context_t context)
2463 {
2464 int error = 0;
2465 u_int32_t startBlock;
2466 u_int32_t blockCount = hfsmp->jnl_size / hfsmp->blockSize;
2467
2468 /*
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.
2471 */
2472 if (hfsmp->jvp == hfsmp->hfs_devvp) {
2473 startBlock = hfsmp->jnl_start;
2474 blockCount = hfsmp->jnl_size / hfsmp->blockSize;
2475 } else {
2476 u_int32_t fileid;
2477 u_int32_t old_jnlfileid;
2478 struct cat_attr attr;
2479 struct cat_fork fork;
2480
2481 /*
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.
2485 */
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");
2492 return EIO;
2493 }
2494
2495 startBlock = fork.cf_extents[0].startBlock;
2496 blockCount = fork.cf_extents[0].blockCount;
2497 }
2498
2499 if (startBlock + blockCount <= allocLimit) {
2500 /* The journal file does not require relocation */
2501 return 0;
2502 }
2503
2504 error = hfs_relocate_journal_file(hfsmp, hfs_blk_to_bytes(blockCount, hfsmp->blockSize),
2505 HFS_RESIZE_TRUNCATE, context);
2506 if (error == 0) {
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);
2511 }
2512
2513 return error;
2514 }
2515
2516
2517 /*
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.
2521 */
2522 static int
2523 hfs_reclaim_journal_info_block(struct hfsmount *hfsmp, u_int32_t allocLimit, vfs_context_t context)
2524 {
2525 int error;
2526 int journal_err;
2527 int lockflags;
2528 u_int32_t oldBlock;
2529 u_int32_t newBlock;
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;
2535
2536 if (hfsmp->vcbJinfoBlock <= allocLimit) {
2537 /* The journal info block does not require relocation */
2538 return 0;
2539 }
2540
2541 error = hfs_start_transaction(hfsmp);
2542 if (error) {
2543 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error);
2544 return error;
2545 }
2546 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
2547
2548 error = BlockAllocate(hfsmp, 1, 1, 1,
2549 HFS_ALLOC_METAZONE | HFS_ALLOC_FORCECONTIG | HFS_ALLOC_SKIPFREEBLKS | HFS_ALLOC_FLUSHTXN,
2550 &newBlock, &blockCount);
2551 if (error) {
2552 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error);
2553 goto fail;
2554 }
2555 if (blockCount != 1) {
2556 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount);
2557 goto free_fail;
2558 }
2559
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);
2564 if (error) {
2565 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error);
2566 if (old_bp) {
2567 buf_brelse(old_bp);
2568 }
2569 goto free_fail;
2570 }
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);
2575 buf_brelse(old_bp);
2576 if (journal_uses_fua(hfsmp->jnl))
2577 buf_markfua(new_bp);
2578 error = buf_bwrite(new_bp);
2579 if (error) {
2580 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error);
2581 goto free_fail;
2582 }
2583 if (!journal_uses_fua(hfsmp->jnl)) {
2584 error = hfs_flush(hfsmp, HFS_FLUSH_CACHE);
2585 if (error) {
2586 printf("hfs_reclaim_journal_info_block: hfs_flush failed (%d)\n", error);
2587 /* Don't fail the operation. */
2588 }
2589 }
2590
2591 /* Deallocate the old block once the new one has the new valid content */
2592 error = BlockDeallocate(hfsmp, hfsmp->vcbJinfoBlock, 1, HFS_ALLOC_SKIPFREEBLKS);
2593 if (error) {
2594 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error);
2595 goto free_fail;
2596 }
2597
2598
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);
2601 if (error) {
2602 printf("hfs_reclaim_journal_info_block: cat_idlookup returned %d\n", error);
2603 goto fail;
2604 }
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 */
2612 if (error) {
2613 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error);
2614 goto fail;
2615 }
2616
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);
2620 if (error) {
2621 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error);
2622 goto fail;
2623 }
2624 hfs_systemfile_unlock(hfsmp, lockflags);
2625 error = hfs_end_transaction(hfsmp);
2626 if (error) {
2627 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error);
2628 }
2629 error = hfs_flush(hfsmp, HFS_FLUSH_JOURNAL);
2630 if (error) {
2631 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error);
2632 }
2633
2634 /* Account for the block relocated and print progress */
2635 hfsmp->hfs_resize_blocksmoved += 1;
2636 hfs_truncatefs_progress(hfsmp);
2637 if (!error) {
2638 printf ("hfs_reclaim_journal_info: Relocated 1 block from journal info on \"%s\"\n",
2639 hfsmp->vcbVN);
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);
2642 }
2643 }
2644 return error;
2645
2646 free_fail:
2647 journal_err = BlockDeallocate(hfsmp, newBlock, blockCount, HFS_ALLOC_SKIPFREEBLKS);
2648 if (journal_err) {
2649 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error);
2650 hfs_mark_inconsistent(hfsmp, HFS_ROLLBACK_FAILED);
2651 }
2652
2653 fail:
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);
2658 }
2659 return error;
2660 }
2661
2662
2663 static u_int64_t
2664 calculate_journal_size(struct hfsmount *hfsmp, u_int32_t sector_size, u_int64_t sector_count)
2665 {
2666 u_int64_t journal_size;
2667 u_int32_t journal_scale;
2668
2669 #define DEFAULT_JOURNAL_SIZE (8*1024*1024)
2670 #define MAX_JOURNAL_SIZE (512*1024*1024)
2671
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
2676 * block.
2677 */
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;
2682 }
2683 if (journal_size < hfsmp->blockSize) {
2684 journal_size = hfsmp->blockSize;
2685 }
2686 return journal_size;
2687 }
2688
2689
2690 /*
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.
2694 */
2695 static int
2696 hfs_extend_journal(struct hfsmount *hfsmp, u_int32_t sector_size, u_int64_t sector_count, vfs_context_t context)
2697 {
2698 int error = 0;
2699 u_int64_t calc_journal_size;
2700
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");
2704 }
2705 return 0;
2706 }
2707
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 */
2711 goto out;
2712 }
2713
2714 if (hfs_resize_debug) {
2715 printf ("hfs_extend_journal: journal old=%u, new=%qd\n", hfsmp->jnl_size, calc_journal_size);
2716 }
2717
2718 /* Extend the journal to the new calculated size */
2719 error = hfs_relocate_journal_file(hfsmp, calc_journal_size, HFS_RESIZE_EXTEND, context);
2720 if (error == 0) {
2721 printf ("hfs_extend_journal: Extended journal size to %u bytes on \"%s\"\n",
2722 hfsmp->jnl_size, hfsmp->vcbVN);
2723 }
2724 out:
2725 return error;
2726 }
2727
2728
2729 /*
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.
2735 *
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.
2739 *
2740 * Side Effects:
2741 * hfsmp->hfs_resize_blocksmoved is incremented by the number of allocation
2742 * blocks that were relocated.
2743 *
2744 * Returns:
2745 * 0 on success, non-zero on failure.
2746 */
2747 static int
2748 hfs_reclaim_xattr(struct hfsmount *hfsmp, struct vnode *vp, u_int32_t fileID, u_int32_t allocLimit, vfs_context_t context)
2749 {
2750 int error = 0;
2751 struct hfs_reclaim_extent_info *extent_info;
2752 int i;
2753 HFSPlusAttrKey *key;
2754 int *lockflags;
2755
2756 if (hfs_resize_debug) {
2757 printf("hfs_reclaim_xattr: === Start reclaiming xattr for id=%u ===\n", fileID);
2758 }
2759
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) {
2763 return ENOMEM;
2764 }
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;
2773
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) {
2778 error = ENOMEM;
2779 goto out;
2780 }
2781 bzero(extent_info->iterator, sizeof(struct BTreeIterator));
2782
2783 /* Build attribute key */
2784 key = (HFSPlusAttrKey *)&(extent_info->iterator->key);
2785 error = hfs_buildattrkey(fileID, NULL, key);
2786 if (error) {
2787 goto out;
2788 }
2789
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.
2793 */
2794 extent_info->btdata.bufferAddress = &(extent_info->record.xattr);
2795 extent_info->btdata.itemSize = sizeof(HFSPlusAttrRecord);
2796 extent_info->btdata.itemCount = 1;
2797
2798 /*
2799 * Sync all extent-based attribute data to the disk.
2800 *
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
2803 * space.
2804 */
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);
2809 if (error) {
2810 goto out;
2811 }
2812
2813 /* Search for extended attribute for current file. This
2814 * will place the iterator before the first matching record.
2815 */
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);
2821 if (error) {
2822 if (error != btNotFound) {
2823 goto out;
2824 }
2825 /* btNotFound is expected here, so just mask it */
2826 error = 0;
2827 }
2828
2829 while (1) {
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);
2836
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) {
2840 error = 0;
2841 }
2842 break;
2843 }
2844
2845 /* We only care about extent-based EAs */
2846 if ((extent_info->record.xattr.recordType != kHFSPlusAttrForkData) &&
2847 (extent_info->record.xattr.recordType != kHFSPlusAttrExtents)) {
2848 continue;
2849 }
2850
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;
2857 }
2858
2859 extent_info->recStartBlock = key->startBlock;
2860 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2861 if (extent_info->extents[i].blockCount == 0) {
2862 break;
2863 }
2864 extent_info->extent_index = i;
2865 error = hfs_reclaim_extent(hfsmp, allocLimit, extent_info, context);
2866 if (error) {
2867 printf ("hfs_reclaim_xattr: fileID=%u hfs_reclaim_extent error=%d\n", fileID, error);
2868 goto out;
2869 }
2870 }
2871 }
2872
2873 out:
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);
2878 }
2879 if (extent_info->iterator) {
2880 FREE(extent_info->iterator, M_TEMP);
2881 }
2882 if (extent_info) {
2883 FREE(extent_info, M_TEMP);
2884 }
2885 if (hfs_resize_debug) {
2886 printf("hfs_reclaim_xattr: === Finished relocating xattr for fileid=%u (error=%d) ===\n", fileID, error);
2887 }
2888 return error;
2889 }
2890
2891 /*
2892 * Reclaim any extent-based extended attributes allocation blocks from
2893 * the area of the disk that is being truncated.
2894 *
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.
2901 *
2902 * Inputs:
2903 * allocLimit - starting block of the area being reclaimed
2904 *
2905 * Returns:
2906 * returns 0 on success, non-zero on failure.
2907 */
2908 static int
2909 hfs_reclaim_xattrspace(struct hfsmount *hfsmp, u_int32_t allocLimit, vfs_context_t context)
2910 {
2911 int error = 0;
2912 FCB *fcb;
2913 struct BTreeIterator *iterator = NULL;
2914 struct FSBufferDescriptor btdata;
2915 HFSPlusAttrKey *key;
2916 HFSPlusAttrRecord rec;
2917 int lockflags = 0;
2918 cnid_t prev_fileid = 0;
2919 struct vnode *vp;
2920 int need_relocate;
2921 int btree_operation;
2922 u_int32_t files_moved = 0;
2923 u_int32_t prev_blocksmoved;
2924 int i;
2925
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;
2929
2930 if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator), VM_KERN_MEMORY_FILE)) {
2931 return ENOMEM;
2932 }
2933 bzero(iterator, sizeof(*iterator));
2934 key = (HFSPlusAttrKey *)&iterator->key;
2935 btdata.bufferAddress = &rec;
2936 btdata.itemSize = sizeof(rec);
2937 btdata.itemCount = 1;
2938
2939 need_relocate = false;
2940 btree_operation = kBTreeFirstRecord;
2941 /* Traverse the attribute btree to find extent-based EAs to reclaim */
2942 while (1) {
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);
2946 if (error) {
2947 if (error == fsBTRecordNotFoundErr || error == fsBTEndOfIterationErr) {
2948 error = 0;
2949 }
2950 break;
2951 }
2952 btree_operation = kBTreeNextRecord;
2953
2954 /* If the extents of current fileID were already relocated, skip it */
2955 if (prev_fileid == key->fileID) {
2956 continue;
2957 }
2958
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) {
2965 break;
2966 }
2967 if ((rec.forkData.theFork.extents[i].startBlock +
2968 rec.forkData.theFork.extents[i].blockCount) > allocLimit) {
2969 need_relocate = true;
2970 break;
2971 }
2972 }
2973 break;
2974
2975 case kHFSPlusAttrExtents:
2976 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2977 if (rec.overflowExtents.extents[i].blockCount == 0) {
2978 break;
2979 }
2980 if ((rec.overflowExtents.extents[i].startBlock +
2981 rec.overflowExtents.extents[i].blockCount) > allocLimit) {
2982 need_relocate = true;
2983 break;
2984 }
2985 }
2986 break;
2987 };
2988
2989 /* Continue iterating to next attribute record */
2990 if (need_relocate == false) {
2991 continue;
2992 }
2993
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.
2997 *
2998 * We want to allow open-unlinked files to be moved,
2999 * so provide allow_deleted == 1 for hfs_vget().
3000 */
3001 if (hfs_vget(hfsmp, key->fileID, &vp, 0, 1) != 0) {
3002 continue;
3003 }
3004
3005 error = hfs_reclaim_xattr(hfsmp, vp, key->fileID, allocLimit, context);
3006 hfs_unlock(VTOC(vp));
3007 vnode_put(vp);
3008 if (error) {
3009 printf ("hfs_reclaim_xattrspace: Error relocating xattrs for fileid=%u (error=%d)\n", key->fileID, error);
3010 break;
3011 }
3012 prev_fileid = key->fileID;
3013 files_moved++;
3014 }
3015
3016 if (files_moved) {
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);
3020 }
3021
3022 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
3023 return error;
3024 }
3025
3026 /*
3027 * Reclaim blocks from regular files.
3028 *
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.
3033 *
3034 * Returns:
3035 * Zero on success, non-zero on failure.
3036 */
3037 static int
3038 hfs_reclaim_filespace(struct hfsmount *hfsmp, u_int32_t allocLimit, vfs_context_t context)
3039 {
3040 int error;
3041 FCB *fcb;
3042 struct BTreeIterator *iterator = NULL;
3043 struct FSBufferDescriptor btdata;
3044 int btree_operation;
3045 int lockflags;
3046 struct HFSPlusCatalogFile filerec;
3047 struct vnode *vp;
3048 struct vnode *rvp;
3049 struct filefork *datafork;
3050 u_int32_t files_moved = 0;
3051 u_int32_t prev_blocksmoved;
3052
3053 #if CONFIG_PROTECT
3054 int keys_generated = 0;
3055 #endif
3056
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;
3060
3061 if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator), VM_KERN_MEMORY_FILE)) {
3062 error = ENOMEM;
3063 goto reclaim_filespace_done;
3064 }
3065
3066 #if CONFIG_PROTECT
3067 /*
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.
3073 */
3074 if (cp_fs_protected (hfsmp->hfs_mp)) {
3075 error = cpx_gentempkeys(&hfsmp->hfs_resize_cpx, hfsmp);
3076 if (error == 0) {
3077 keys_generated = 1;
3078 }
3079
3080 if (error) {
3081 printf("hfs_reclaimspace: Error generating temporary keys for resize (%d)\n", error);
3082 goto reclaim_filespace_done;
3083 }
3084 }
3085
3086 #endif
3087
3088 bzero(iterator, sizeof(*iterator));
3089
3090 btdata.bufferAddress = &filerec;
3091 btdata.itemSize = sizeof(filerec);
3092 btdata.itemCount = 1;
3093
3094 btree_operation = kBTreeFirstRecord;
3095 while (1) {
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);
3099 if (error) {
3100 if (error == fsBTRecordNotFoundErr || error == fsBTEndOfIterationErr) {
3101 error = 0;
3102 }
3103 break;
3104 }
3105 btree_operation = kBTreeNextRecord;
3106
3107 if (filerec.recordType != kHFSPlusFileRecord) {
3108 continue;
3109 }
3110
3111 /* Check if any of the extents require relocation */
3112 bool overlaps;
3113 error = hfs_file_extent_overlaps(hfsmp, allocLimit, &filerec, &overlaps);
3114 if (error)
3115 break;
3116
3117 if (!overlaps)
3118 continue;
3119
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);
3124 }
3125 continue;
3126 }
3127
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);
3133 if (error) {
3134 printf ("hfs_reclaimspace: Error reclaiming datafork blocks of fileid=%u (error=%d)\n", filerec.fileID, error);
3135 hfs_unlock(VTOC(vp));
3136 vnode_put(vp);
3137 break;
3138 }
3139 }
3140
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.
3146 */
3147 rvp = vp;
3148 } else {
3149 error = hfs_vgetrsrc(hfsmp, vp, &rvp);
3150 if (error) {
3151 printf ("hfs_reclaimspace: Error looking up rvp for fileid=%u (error=%d)\n", filerec.fileID, error);
3152 hfs_unlock(VTOC(vp));
3153 vnode_put(vp);
3154 break;
3155 }
3156 VTOC(rvp)->c_flag |= C_NEED_RVNODE_PUT;
3157 }
3158
3159 error = hfs_reclaim_file(hfsmp, rvp, filerec.fileID,
3160 kHFSResourceForkType, allocLimit, context);
3161 if (error) {
3162 printf ("hfs_reclaimspace: Error reclaiming rsrcfork blocks of fileid=%u (error=%d)\n", filerec.fileID, error);
3163 hfs_unlock(VTOC(vp));
3164 vnode_put(vp);
3165 break;
3166 }
3167 }
3168
3169 /* The file forks were relocated successfully, now drop the
3170 * cnode lock and vnode reference, and continue iterating to
3171 * next catalog record.
3172 */
3173 hfs_unlock(VTOC(vp));
3174 vnode_put(vp);
3175 files_moved++;
3176 }
3177
3178 if (files_moved) {
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);
3182 }
3183
3184 reclaim_filespace_done:
3185 if (iterator) {
3186 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
3187 }
3188
3189 #if CONFIG_PROTECT
3190 if (keys_generated) {
3191 cpx_free(hfsmp->hfs_resize_cpx);
3192 hfsmp->hfs_resize_cpx = NULL;
3193 }
3194 #endif
3195 return error;
3196 }
3197
3198 /*
3199 * Reclaim space at the end of a file system.
3200 *
3201 * Inputs -
3202 * allocLimit - start block of the space being reclaimed
3203 * reclaimblks - number of allocation blocks to reclaim
3204 */
3205 static int
3206 hfs_reclaimspace(struct hfsmount *hfsmp, u_int32_t allocLimit, u_int32_t reclaimblks, vfs_context_t context)
3207 {
3208 int error = 0;
3209
3210 /*
3211 * Preflight the bitmap to find out total number of blocks that need
3212 * relocation.
3213 *
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.
3217 */
3218 error = hfs_count_allocated(hfsmp, allocLimit, reclaimblks, &(hfsmp->hfs_resize_totalblocks));
3219 if (error) {
3220 printf ("hfs_reclaimspace: Unable to determine total blocks to reclaim error=%d\n", error);
3221 return error;
3222 }
3223 if (hfs_resize_debug) {
3224 printf ("hfs_reclaimspace: Total number of blocks to reclaim = %u\n", hfsmp->hfs_resize_totalblocks);
3225 }
3226
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);
3229
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
3234 * fail.
3235 */
3236 error = hfs_reclaim_journal_file(hfsmp, allocLimit, context);
3237 if (error) {
3238 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error);
3239 return error;
3240 }
3241
3242 /* Relocate journal info block blocks if they're in the way. */
3243 error = hfs_reclaim_journal_info_block(hfsmp, allocLimit, context);
3244 if (error) {
3245 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error);
3246 return error;
3247 }
3248
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.
3254 */
3255 error = hfs_reclaim_file(hfsmp, hfsmp->hfs_extents_vp, kHFSExtentsFileID,
3256 kHFSDataForkType, allocLimit, context);
3257 if (error) {
3258 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error);
3259 return error;
3260 }
3261
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);
3265 if (error) {
3266 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error);
3267 return error;
3268 }
3269
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);
3273 if (error) {
3274 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error);
3275 return error;
3276 }
3277
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);
3281 if (error) {
3282 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error);
3283 return error;
3284 }
3285
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);
3289 if (error) {
3290 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error);
3291 return error;
3292 }
3293
3294 /*
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.
3299 *
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.
3303 */
3304 if (hfsmp->hfs_resize_blocksmoved) {
3305 hfs_flush(hfsmp, HFS_FLUSH_JOURNAL_META);
3306 }
3307
3308 /* Reclaim extents from catalog file records */
3309 error = hfs_reclaim_filespace(hfsmp, allocLimit, context);
3310 if (error) {
3311 printf ("hfs_reclaimspace: hfs_reclaim_filespace returned error=%d\n", error);
3312 return error;
3313 }
3314
3315 /* Reclaim extents from extent-based extended attributes, if any */
3316 error = hfs_reclaim_xattrspace(hfsmp, allocLimit, context);
3317 if (error) {
3318 printf ("hfs_reclaimspace: hfs_reclaim_xattrspace returned error=%d\n", error);
3319 return error;
3320 }
3321
3322 /*
3323 * Make sure reserved ranges in the region we're to allocate don't
3324 * overlap.
3325 */
3326 struct rl_entry *range;
3327 again:;
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) {
3331 // Wait 100ms
3332 hfs_systemfile_unlock(hfsmp, lockf);
3333 msleep(hfs_reclaimspace, NULL, PINOD, "waiting on reserved blocks",
3334 &(struct timespec){ 0, 100 * 1000000 });
3335 goto again;
3336 }
3337 }
3338 hfs_systemfile_unlock(hfsmp, lockf);
3339
3340 return error;
3341 }
3342
3343
3344 /*
3345 * Check if there are any extents (including overflow extents) that overlap
3346 * into the disk space that is being reclaimed.
3347 *
3348 * Output -
3349 * true - One of the extents need to be relocated
3350 * false - No overflow extents need to be relocated, or there was an error
3351 */
3352 static errno_t
3353 hfs_file_extent_overlaps(struct hfsmount *hfsmp, u_int32_t allocLimit,
3354 struct HFSPlusCatalogFile *filerec, bool *overlaps)
3355 {
3356 struct BTreeIterator * iterator = NULL;
3357 struct FSBufferDescriptor btdata;
3358 HFSPlusExtentRecord extrec;
3359 HFSPlusExtentKey *extkeyptr;
3360 FCB *fcb;
3361 int i, j;
3362 int error;
3363 int lockflags = 0;
3364 u_int32_t endblock;
3365 errno_t ret = 0;
3366
3367 /* Check if data fork overlaps the target space */
3368 for (i = 0; i < kHFSPlusExtentDensity; ++i) {
3369 if (filerec->dataFork.extents[i].blockCount == 0) {
3370 break;
3371 }
3372 endblock = filerec->dataFork.extents[i].startBlock +
3373 filerec->dataFork.extents[i].blockCount;
3374 if (endblock > allocLimit) {
3375 *overlaps = true;
3376 goto out;
3377 }
3378 }
3379
3380 /* Check if resource fork overlaps the target space */
3381 for (j = 0; j < kHFSPlusExtentDensity; ++j) {
3382 if (filerec->resourceFork.extents[j].blockCount == 0) {
3383 break;
3384 }
3385 endblock = filerec->resourceFork.extents[j].startBlock +
3386 filerec->resourceFork.extents[j].blockCount;
3387 if (endblock > allocLimit) {
3388 *overlaps = true;
3389 goto out;
3390 }
3391 }
3392
3393 /* Return back if there are no overflow extents for this file */
3394 if ((i < kHFSPlusExtentDensity) && (j < kHFSPlusExtentDensity)) {
3395 *overlaps = false;
3396 goto out;
3397 }
3398
3399 MALLOC(iterator, BTreeIterator *, sizeof(*iterator), M_TEMP, M_WAITOK);
3400
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;
3407
3408 btdata.bufferAddress = &extrec;
3409 btdata.itemSize = sizeof(extrec);
3410 btdata.itemCount = 1;
3411
3412 fcb = VTOF(hfsmp->hfs_extents_vp);
3413
3414 lockflags = hfs_systemfile_lock(hfsmp, SFL_EXTENTS, HFS_SHARED_LOCK);
3415
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.
3419 */
3420 error = BTSearchRecord(fcb, iterator, &btdata, NULL, iterator);
3421 if (error && (error != btNotFound)) {
3422 ret = MacToVFSError(error);
3423 goto out;
3424 }
3425
3426 /* BTIterateRecord() might return error if the btree is empty, and
3427 * therefore we return that the extent does not overflow to the caller
3428 */
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) {
3433 break;
3434 }
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) {
3438 break;
3439 }
3440 endblock = extrec[i].startBlock + extrec[i].blockCount;
3441 if (endblock > allocLimit) {
3442 *overlaps = true;
3443 goto out;
3444 }
3445 }
3446 /* Look for more records. */
3447 error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL);
3448 }
3449
3450 if (error && error != btNotFound) {
3451 ret = MacToVFSError(error);
3452 goto out;
3453 }
3454
3455 *overlaps = false;
3456
3457 out:
3458 if (lockflags) {
3459 hfs_systemfile_unlock(hfsmp, lockflags);
3460 }
3461
3462 FREE(iterator, M_TEMP);
3463
3464 return ret;
3465 }
3466
3467
3468 /*
3469 * Calculate the progress of a file system resize operation.
3470 */
3471 __private_extern__
3472 int
3473 hfs_resize_progress(struct hfsmount *hfsmp, u_int32_t *progress)
3474 {
3475 if ((hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) == 0) {
3476 return (ENXIO);
3477 }
3478
3479 if (hfsmp->hfs_resize_totalblocks > 0) {
3480 *progress = (u_int32_t)((hfsmp->hfs_resize_blocksmoved * 100ULL) / hfsmp->hfs_resize_totalblocks);
3481 } else {
3482 *progress = 0;
3483 }
3484
3485 return (0);
3486 }